I'm trying to use the convex agent
I'm trying to use the convex agent component, and following the docs I'm having trouble with saving outputs.
The user messages save correctly, but responses only exist within the
steps
table.
The docs indicate that the full history can be retrieved via components.agent.messages.getThreadMessages
, but I haven't had a single success of getting any responses via a query against that.
Any tips?3 Replies
import { v } from 'convex/values'
import { Agent } from '@convex-dev/agent'
import { action, query } from './_generated/server'
import { components } from './_generated/api'
import { openai } from '@ai-sdk/openai'
import { paginationOptsValidator } from 'convex/server'
const basicAgent = new Agent(components.agent, {
chat: openai.chat('gpt-4o-mini'),
instructions: 'You are a helpful assistant. Keep your responses concise.',
})
export const startAgentChat = action({
args: { prompt: v.string() },
returns: v.object({ threadId: v.string(), responseText: v.string() }),
handler: async (ctx, { prompt }): Promise<{ threadId: string; responseText: string }> => {
const { threadId, thread } = await basicAgent.createThread(ctx, {
title: 'New Chat Thread', // Optional title
})
const result = await thread.generateText({ prompt })
return { threadId: threadId as string, responseText: result.text }
},
})
export const continueAgentChat = action({
args: { prompt: v.string(), threadId: v.string() },
returns: v.object({ responseText: v.string() }),
handler: async (ctx, { prompt, threadId }): Promise<{ responseText: string }> => {
const { thread } = await basicAgent.continueThread(ctx, { threadId })
const result = await thread.generateText({ prompt })
return { responseText: result.text }
},
})
export const getAgentMessages = query({
args: {
threadId: v.union(v.null(), v.string()),
paginationOpts: paginationOptsValidator,
},
returns: v.union(
v.null(),
v.object({
page: v.array(v.any()),
isDone: v.boolean(),
continueCursor: v.union(v.string(), v.null()),
}),
),
handler: async (ctx, args) => {
if (!args.threadId) {
return null
}
return await ctx.runQuery(components.agent.messages.getThreadMessages, {
threadId: args.threadId,
paginationOpts: args.paginationOpts,
})
},
})
import { v } from 'convex/values'
import { Agent } from '@convex-dev/agent'
import { action, query } from './_generated/server'
import { components } from './_generated/api'
import { openai } from '@ai-sdk/openai'
import { paginationOptsValidator } from 'convex/server'
const basicAgent = new Agent(components.agent, {
chat: openai.chat('gpt-4o-mini'),
instructions: 'You are a helpful assistant. Keep your responses concise.',
})
export const startAgentChat = action({
args: { prompt: v.string() },
returns: v.object({ threadId: v.string(), responseText: v.string() }),
handler: async (ctx, { prompt }): Promise<{ threadId: string; responseText: string }> => {
const { threadId, thread } = await basicAgent.createThread(ctx, {
title: 'New Chat Thread', // Optional title
})
const result = await thread.generateText({ prompt })
return { threadId: threadId as string, responseText: result.text }
},
})
export const continueAgentChat = action({
args: { prompt: v.string(), threadId: v.string() },
returns: v.object({ responseText: v.string() }),
handler: async (ctx, { prompt, threadId }): Promise<{ responseText: string }> => {
const { thread } = await basicAgent.continueThread(ctx, { threadId })
const result = await thread.generateText({ prompt })
return { responseText: result.text }
},
})
export const getAgentMessages = query({
args: {
threadId: v.union(v.null(), v.string()),
paginationOpts: paginationOptsValidator,
},
returns: v.union(
v.null(),
v.object({
page: v.array(v.any()),
isDone: v.boolean(),
continueCursor: v.union(v.string(), v.null()),
}),
),
handler: async (ctx, args) => {
if (!args.threadId) {
return null
}
return await ctx.runQuery(components.agent.messages.getThreadMessages, {
threadId: args.threadId,
paginationOpts: args.paginationOpts,
})
},
})

