@convex-dev/agent 0.0.11-alpha.2 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -52,21 +52,22 @@ export const continueThread = action({
52
52
  });
53
53
 
54
54
  // Or use it within a workflow, specific to a user:
55
- export const supportAgentStep = supportAgent.asAction({ maxSteps: 10 });
55
+ export const { generateText: getSupport } = supportAgent.asActions({ maxSteps: 10 });
56
56
 
57
57
  const workflow = new WorkflowManager(components.workflow);
58
- const s = internal.example; // where steps are defined
59
58
 
60
59
  export const supportAgentWorkflow = workflow.define({
61
60
  args: { prompt: v.string(), userId: v.string(), threadId: v.string() },
62
61
  handler: async (step, { prompt, userId, threadId }) => {
63
- const suggestion = await step.runAction(s.supportAgentStep, {
64
- threadId, generateText: { prompt },
62
+ const suggestion = await step.runAction(internal.example.getSupport, {
63
+ threadId, userId, prompt,
65
64
  });
66
- const polished = await step.runAction(s.adaptSuggestionForUser, {
67
- suggestion, userId,
65
+ const polished = await step.runAction(internal.example.adaptSuggestionForUser, {
66
+ userId, suggestion,
67
+ });
68
+ await step.runMutation(internal.example.sendUserMessage, {
69
+ userId, message: polished.message,
68
70
  });
69
- await step.runMutation(s.sendUserMessage, { userId, message: polished.message });
70
71
  },
71
72
  });
72
73
  ```
@@ -216,45 +217,53 @@ export const continueThread = action({
216
217
 
217
218
  ### Exposing the agent as a Convex action
218
219
 
220
+ You can expose the agent as a Convex internal action.
221
+ This is generally used from a workflow, where each step is a new thread message.
222
+
219
223
  ```ts
220
- export const supportAgentStep = supportAgent.asAction({ maxSteps: 10 });
224
+ export const getSupport = supportAgent.asTextAction({
225
+ maxSteps: 10,
226
+ });
227
+ ```
221
228
 
222
- // Then from within another action:
223
- export const callSupportAgent = action({
224
- args: { prompt: v.string(), userId: v.string(), threadId: v.string() },
225
- handler: async (ctx, { prompt, userId, threadId }) => {
226
- const suggestion = await ctx.runAction(s.supportAgentStep, {
227
- threadId, userId, generateText: { prompt },
228
- });
229
- },
229
+ You can also expose an action that generates an object.
230
+
231
+ ```ts
232
+ export const getStructuredSupport = supportAgent.asObjectAction({
233
+ schema: z.object({
234
+ analysis: z.string().describe("A detailed analysis of the user's request."),
235
+ suggestion: z.string().describe("A suggested action to take.")}),
230
236
  });
231
237
  ```
232
238
 
233
- ### Using the agent within a workflow
239
+ ### Using the agent actions within a workflow
234
240
 
235
241
  You can use the [Workflow component](https://convex.dev/components/workflow)
236
- to run, with retries and guarantees of eventually completing, surviving server restarts,
237
- and more. Read more about durable workflows
242
+ to run agent flows. It handles retries and guarantees of eventually completing,
243
+ surviving server restarts, and more. Read more about durable workflows
238
244
  [in this Stack post](https://stack.convex.dev/durable-workflows-and-strong-guarantees).
239
245
 
240
246
  ```ts
241
247
  const workflow = new WorkflowManager(components.workflow);
242
- const s = internal.example; // where steps are defined
243
248
 
244
249
  export const supportAgentWorkflow = workflow.define({
245
250
  args: { prompt: v.string(), userId: v.string(), threadId: v.string() },
246
251
  handler: async (step, { prompt, userId, threadId }) => {
247
- const suggestion = await step.runAction(s.supportAgentStep, {
248
- threadId, userId, generateText: { prompt },
252
+ const suggestion = await step.runAction(internal.example.getSupport, {
253
+ threadId, userId, prompt,
254
+ });
255
+ const polished = await step.runAction(internal.example.adaptSuggestionForUser, {
256
+ userId, suggestion,
249
257
  });
250
- const polished = await step.runAction(s.adaptSuggestionForUser, {
251
- threadId, userId, generateText: { prompt: suggestion },
258
+ await step.runMutation(internal.example.sendUserMessage, {
259
+ userId, message: polished.message,
252
260
  });
253
- await step.runMutation(s.sendUserMessage, { userId, message: polished.message });
254
261
  },
255
262
  });
256
263
  ```
257
264
 
265
+ See another example in [example.ts](./example/convex/example.ts#L120).
266
+
258
267
  ### Fetching thread history
259
268
 
260
269
  ```ts