Error while using the custom tool with agents
Here is the agent I have:
import { z } from "zod";
import { createTool } from "@convex-dev/agent";
import { internal } from "../../../_generated/api";
import { supportAgent } from "../agents/supportAgent";
export const escalateConversation = createTool({
description: "Escalate a conversation",
inputSchema: z.object({}),
execute: async (ctx) => {
if (!ctx.threadId) {
return "Missing Thread ID";
}
await ctx.runMutation(internal.system.conversations.escalateStatus, {
threadId: ctx.threadId,
});
await supportAgent.saveMessage(ctx, {
threadId: ctx.threadId,
message: {
role: "assistant",
content: "Conversation escalated to a human operator.",
},
});
return "Conversation escalated to a human operator.";
},
});
Here is how I am using the tool:
export const createMessage = action({
args: {
prompt: v.string(),
threadId: v.string(),
contactSessionId: v.id("contactSessions"),
},
handler: async (ctx, args) => {
const contactSession = await ctx.runQuery(
internal.system.contactSessions.getSession,
{ contactSessionId: args.contactSessionId },
);
if (!contactSession) {
throw new ConvexError({
code: "UNAUTHORIZED",
message: "Invalid Session.",
});
}
const contactSessionExpiresAt =
typeof contactSession.expiresAt === "number"
? contactSession.expiresAt
: new Date(contactSession.expiresAt).getTime();
if (contactSessionExpiresAt < Date.now()) {
throw new ConvexError({
code: "UNAUTHORIZED",
message: "Invalid session.",
});
}
const conversation = await ctx.runQuery(
internal.system.conversations.getConversationByThreadId,
{
threadId: args.threadId,
},
);
if (!conversation) {
throw new ConvexError({
code: "NOT_FOUND",
message: "Conversation not found!",
});
}
if (conversation.status === "resolved") {
throw new ConvexError({
code: "BAD_REQUEST",
message: "Conversation resolved!",
});
}
// This refreshes the user sessions if they are within the threshold
await ctx.runMutation(internal.system.contactSessions.refreshSession, {
contactSessionId: args.contactSessionId,
});
const subscription = await ctx.runQuery(
internal.system.subscriptions.subscriptions
.getSubscriptionByOrganizationId,
{
organizationId: conversation.organizationId,
},
);
const shouldTriggerAgent =
conversation.status === "unresolved" && subscription?.status === "active";
if (shouldTriggerAgent) {
await supportAgent.generateText(
ctx,
{ threadId: args.threadId },
{
prompt: args.prompt,
tools: {
escalateConversationTool: escalateConversation,
resolveConversationTool: resolveConversation,
searchTool: search,
},
},
);
} else {
await saveMessage(ctx, components.agent, {
threadId: args.threadId,
prompt: args.prompt,
});
}
},
});
Here is the error I am getting:
forward-logs-shared.ts:95 [HMR] connected
forward-logs-shared.ts:95 [CONVEX A(public/messages:createMessage)] [WARN] 'AI SDK Warning: System messages in the prompt or messages fields can be a security risk because they may enable prompt injection attacks. Use the system option instead when possible. Set allowSystemInMessages to true to suppress this warning, or false to throw an error.'
installHook.js:1 [CONVEX A(public/messages:createMessage)] [Request ID: 9ee39ccf79f8f7ef] Server Error
Uncaught AI_APICallError: An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_0csBSSTuH4HHlktexu7NYEvx
at <anonymous> (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/response-handler.ts:56:10)
at async postToApi (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:118:10)
at async doGenerate [as doGenerate] (../../../../node_modules/.pnpm/@ai-sdk+openai@3.0.67_zod@3.25.76/node_modules/@ai-sdk/openai/src/chat/openai-chat-language-model.ts:325:6)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:788:23)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async _retryWithExponentialBackoff (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/util/retry-with-exponential-backoff.ts:101:11)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:742:50)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async generateText (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:527:12)
at async generateText [as generateText] (../../../../node_modules/.pnpm/@convex-dev+agent@0.6.2_@ai-sdk+provider-utils@4.0.27_zod@3.25.76__ai@6.0.195_zod@3.25._48e744069339bedbc19b149db876a836/node_modules/@convex-dev/agent/src/client/index.ts:495:11)
overrideMethod @ installHook.js:1
error @ intercept-console-error.ts:42
(anonymous) @ logging.ts:109
error @ logging.ts:88
logForFunction @ logging.ts:149
onResponse @ request_manager.ts:126
(anonymous) @ client.ts:480
(anonymous) @ web_socket_manager.ts:470
client.ts:989 Uncaught (in promise) Error: [CONVEX A(public/messages:createMessage)] [Request ID: 9ee39ccf79f8f7ef] Server Error
Uncaught AI_APICallError: An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_0csBSSTuH4HHlktexu7NYEvx
at <anonymous> (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/response-handler.ts:56:10)
at async postToApi (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:118:10)
at async doGenerate [as doGenerate] (../../../../node_modules/.pnpm/@ai-sdk+openai@3.0.67_zod@3.25.76/node_modules/@ai-sdk/openai/src/chat/openai-chat-language-model.ts:325:6)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:788:23)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async _retryWithExponentialBackoff (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/util/retry-with-exponential-backoff.ts:101:11)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:742:50)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async generateText (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:527:12)
at async generateText [as generateText] (../../../../node_modules/.pnpm/@convex-dev+agent@0.6.2_@ai-sdk+provider-utils@4.0.27_zod@3.25.76__ai@6.0.195_zod@3.25._48e744069339bedbc19b149db876a836/node_modules/@convex-dev/agent/src/client/index.ts:495:11)
Called by client
at <anonymous> (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/response-handler.ts:56:10)
at async postToApi (../../../../node_modules/.pnpm/@ai-sdk+provider-utils@4.0.27_zod@3.25.76/node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:118:10)
at async doGenerate [as doGenerate] (../../../../node_modules/.pnpm/@ai-sdk+openai@3.0.67_zod@3.25.76/node_modules/@ai-sdk/openai/src/chat/openai-chat-language-model.ts:325:6)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:788:23)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async _retryWithExponentialBackoff (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/util/retry-with-exponential-backoff.ts:101:11)
at async fn (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:742:50)
at async <anonymous> (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/telemetry/record-span.ts:33:12)
at async generateText (../../../../node_modules/.pnpm/ai@6.0.195_zod@3.25.76/node_modules/ai/src/generate-text/generate-text.ts:527:12)
at async generateText [as generateText] (../../../../node_modules/.pnpm/@convex-dev+agent@0.6.2_@ai-sdk+provider-utils@4.0.27_zod@3.25.76__ai@6.0.195_zod@3.25._48e744069339bedbc19b149db876a836/node_modules/@convex-dev/agent/src/client/index.ts:495:11)
Called by client
at BaseConvexClient.action (http://localhost:3001/_next/static/chunks/node_modules__pnpm_adeb6fad._.js:6036:19)
at async handleSubmit (http://localhost:3001/_next/static/chunks/_11f434ca._.js:2494:9)
at async http://localhost:3001/_next/static/chunks/e5554_react-hook-form_dist_index_esm_mjs_4d7a34e5._.js:2331:21
action @ client.ts:989
await in action
action @ client.ts:645
(anonymous) @ client.ts:144
(anonymous) @ WidgetChatScreen.tsx:116
(anonymous) @ createFormControl.ts:1561
await in (anonymous)
(anonymous) @ WidgetChatScreen.tsx:217
executeDispatch @ react-dom-client.development.js:20543
runWithFiberInDEV @ react-dom-client.development.js:986
processDispatchQueue @ react-dom-client.development.js:20593
(anonymous) @ react-dom-client.development.js:21164
batchedUpdates$1 @ react-dom-client.development.js:3377
dispatchEventForPluginEventSystem @ react-dom-client.development.js:20747
dispatchEvent @ react-dom-client.development.js:25693
dispatchDiscreteEvent @ react-dom-client.development.js:25661
What am I doing wrong with this code?
This issue was not present on the previous version of the @convex/agent and ai sdk.
Error while using the custom tool with agents
Here is the agent I have:
Here is how I am using the tool:
Here is the error I am getting:
What am I doing wrong with this code?
This issue was not present on the previous version of the
@convex/agentandai sdk.