sonandmjyS
Convex Community8mo ago
3 replies
sonandmjy

[AI agent] tool call status update

Hello! loving experimenting with the AI agent component, literally built a chatbot in a afternoon, but I am a bit confused when I am supposed to 'complete' a message since after tool calls, it goes to 'pending' then to 'failed' once I do a followup message so not sure where in the flow i am supposed to call the API? For more context, for text messages without tool calls it seems ok

currently this is my setup. (truncated for brevity). Any help would be appreciated!

export const kaolinAgent = new Agent(components.agent, {
  chat: xai("grok-3"),
  name: "Kaolin agent",
  ...
  tools: {
    searchContacts: createTool({
      description: "Search for a contact by name, email, or phone number",
      args: z.object({
        searchQuery: z.string(),
      }),
      handler: async (ctx, args) => {
        const program: Effect.Effect<
          TSearchContactsHandlerReturn,
          never,
          never
        > = Effect.gen(function* () {
          const contacts = yield* Effect.promise(() =>
            ctx.runQuery(internal.ai.tools.searchContacts, {
              userId: ctx.userId!,
              searchQuery: args.searchQuery,
            })
          );

          yield* Effect.promise(() =>
            ctx.agent.completeMessage(ctx, {
              threadId: ctx.threadId!,
              messageId: ctx.messageId!,
              result: { kind: "success" },
            })
          );

          return yield* Effect.succeed(contacts);
        });

        return program.pipe(
          Effect.catchAllCause((cause) => {
            return Effect.gen(function* () {
              yield* Effect.promise(() =>
                ctx.agent.completeMessage(ctx, {
                  threadId: ctx.threadId!,
                  messageId: ctx.messageId!,
                  result: { kind: "error", error: cause._tag },
                })
              );
            });
          }),
          Effect.runPromise
        );
      },
    }),
  },
});
Was this page helpful?