Stress War
CCConvex Community
•Created by Stress War on 4/20/2025 in #general
I'm trying to use the convex agent

3 replies
CCConvex Community
•Created by Stress War on 4/20/2025 in #general
I'm trying to use the convex agent

3 replies
CCConvex Community
•Created by Stress War on 4/20/2025 in #general
I'm trying to use the convex agent
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,
})
},
})
3 replies