Skip to content

Improve error when Node-only deps are used in tool handlers (“dynamic module import unsupported” shows from unrelated query) #112

@eliotgevers

Description

@eliotgevers

I keep on getting the same error and I have no clue why:

Q - ai:listThreadMessages - error

Error getting version:' [TypeError: dynamic module import unsupported]"

listThreadMessages function:

export const listThreadMessages = query({
  args: {
    // These arguments are required:
    threadId: v.string(),
    paginationOpts: paginationOptsValidator, // Used to paginate the messages.
    streamArgs: vStreamArgs, // Used to stream messages.
  },
  handler: async (ctx, args) => {
    const { threadId, paginationOpts, streamArgs } = args;
    await authorizeThreadAccess(ctx, threadId);

    const paginated = await listMessages(ctx, components.agent, {
      threadId,
      paginationOpts,
    });

    const streams = await syncStreams(ctx, components.agent, {
      threadId,
      streamArgs,
      includeStatuses: ["aborted", "streaming", "finished"],
    });

    return {
      ...paginated,
      streams,

      // ... you can return other metadata here too.
      // note: this function will be called with various permutations of delta
      // and message args, so returning derived data .
    };
  },
});

Problem

Using a Node-only lib (e.g., @mendable/firecrawl-js) inside a createTool handler throws a generic “dynamic module import unsupported” that surfaces from an unrelated function like ai.listThreadMessages, hiding the real cause.

Repro

  1. Import a Node-only lib in a createTool handler.
  2. Use the agent with ai.listThreadMessages.
  3. See: “Error getting version: [TypeError: dynamic module import unsupported]”.

Expected

Error should point to the offending tool (name + file) and say Node-only code must run in an action file with "use node" and be invoked via ctx.runAction(...).

Workaround

Move Node-only usage to an internalAction file with "use node" and call it from the tool via ctx.runAction(...).

VERSION

"@convex-dev/agent": "^0.1.17",

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions