David AlonsoD
Convex Community2y ago
5 replies
David Alonso

Passing `ctx` to AI agent tools

I'm currently doing the following inside a convex action:
const result = await createBlockFromPromptAls.run(
      {
        ctx: ctx,
        clerkOrgId: args.clerkOrgId,
        blockId: args.blockId,
        workspaceCollectionInfo: args.workspaceCollectionInfo,
      },
      () =>
        generateText({
          model: anthropic("claude-3-5-sonnet-20240620"),
          prompt,
          maxToolRoundtrips: 5, // allow up to 5 tool roundtrips
          tools: {
            getFields: FireviewAITools.getFields,
            createTableBlockOneShot: FireviewAITools.createTableBlockOneShot,
          },
        })
    );


where I'm using an ALS like so:
export const createBlockFromPromptAls = new AsyncLocalStorage<{
  ctx: AuthActionCtx;
  clerkOrgId: string;
  blockId: Id<"blocks">;
  workspaceCollectionInfo: z.infer<typeof zWorkspaceCollectionsInfo>;
}>();


so that tools can access the ctx to query or write to the Convex db, e.g.

export const getFields = tool({
    parameters: ...
    execute: async ({ collectionId }) => {
      const store = createBlockFromPromptAls.getStore();
      const { ctx, clerkOrgId } = store;
      ctx.runQuery...


HOWEVER, I'm running into:
[ERROR] Could not resolve "node:async_hooks"
[CONVEX]     lib/ai/als.ts:1:34:
[CONVEX]       1 │ import { AsyncLocalStorage } from "node:async_hooks";
[CONVEX]         ╵                                   ~~~~~~~~~~~~~~~~~~
[CONVEX] 
[CONVEX]   The package "node:async_hooks" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.


which i could fix by using the use node directive, but I'm trying to stay away from it for performance reasons. Any ideas on how do do this with edge runtime packages?
Was this page helpful?