@convex-dev/agent 0.0.15-alpha.2 → 0.0.16-alpha.0

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.
Files changed (84) hide show
  1. package/README.md +93 -29
  2. package/dist/commonjs/client/index.d.ts +768 -71
  3. package/dist/commonjs/client/index.d.ts.map +1 -1
  4. package/dist/commonjs/client/index.js +81 -42
  5. package/dist/commonjs/client/index.js.map +1 -1
  6. package/dist/commonjs/client/playground.d.ts +474 -0
  7. package/dist/commonjs/client/playground.d.ts.map +1 -0
  8. package/dist/commonjs/client/playground.js +178 -0
  9. package/dist/commonjs/client/playground.js.map +1 -0
  10. package/dist/commonjs/component/apiKeys.d.ts +11 -0
  11. package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
  12. package/dist/commonjs/component/apiKeys.js +69 -0
  13. package/dist/commonjs/component/apiKeys.js.map +1 -0
  14. package/dist/commonjs/component/files.d.ts +31 -0
  15. package/dist/commonjs/component/files.d.ts.map +1 -0
  16. package/dist/commonjs/component/files.js +61 -0
  17. package/dist/commonjs/component/files.js.map +1 -0
  18. package/dist/commonjs/component/messages.d.ts +40 -109
  19. package/dist/commonjs/component/messages.d.ts.map +1 -1
  20. package/dist/commonjs/component/messages.js +44 -260
  21. package/dist/commonjs/component/messages.js.map +1 -1
  22. package/dist/commonjs/component/schema.d.ts +54 -10
  23. package/dist/commonjs/component/schema.d.ts.map +1 -1
  24. package/dist/commonjs/component/schema.js +7 -2
  25. package/dist/commonjs/component/schema.js.map +1 -1
  26. package/dist/commonjs/component/threads.d.ts +95 -0
  27. package/dist/commonjs/component/threads.d.ts.map +1 -0
  28. package/dist/commonjs/component/threads.js +151 -0
  29. package/dist/commonjs/component/threads.js.map +1 -0
  30. package/dist/commonjs/component/users.d.ts +37 -0
  31. package/dist/commonjs/component/users.d.ts.map +1 -0
  32. package/dist/commonjs/component/users.js +118 -0
  33. package/dist/commonjs/component/users.js.map +1 -0
  34. package/dist/commonjs/validators.d.ts +2 -6
  35. package/dist/commonjs/validators.d.ts.map +1 -1
  36. package/dist/commonjs/validators.js +0 -1
  37. package/dist/commonjs/validators.js.map +1 -1
  38. package/dist/esm/client/index.d.ts +768 -71
  39. package/dist/esm/client/index.d.ts.map +1 -1
  40. package/dist/esm/client/index.js +81 -42
  41. package/dist/esm/client/index.js.map +1 -1
  42. package/dist/esm/client/playground.d.ts +474 -0
  43. package/dist/esm/client/playground.d.ts.map +1 -0
  44. package/dist/esm/client/playground.js +178 -0
  45. package/dist/esm/client/playground.js.map +1 -0
  46. package/dist/esm/component/apiKeys.d.ts +11 -0
  47. package/dist/esm/component/apiKeys.d.ts.map +1 -0
  48. package/dist/esm/component/apiKeys.js +69 -0
  49. package/dist/esm/component/apiKeys.js.map +1 -0
  50. package/dist/esm/component/files.d.ts +31 -0
  51. package/dist/esm/component/files.d.ts.map +1 -0
  52. package/dist/esm/component/files.js +61 -0
  53. package/dist/esm/component/files.js.map +1 -0
  54. package/dist/esm/component/messages.d.ts +40 -109
  55. package/dist/esm/component/messages.d.ts.map +1 -1
  56. package/dist/esm/component/messages.js +44 -260
  57. package/dist/esm/component/messages.js.map +1 -1
  58. package/dist/esm/component/schema.d.ts +54 -10
  59. package/dist/esm/component/schema.d.ts.map +1 -1
  60. package/dist/esm/component/schema.js +7 -2
  61. package/dist/esm/component/schema.js.map +1 -1
  62. package/dist/esm/component/threads.d.ts +95 -0
  63. package/dist/esm/component/threads.d.ts.map +1 -0
  64. package/dist/esm/component/threads.js +151 -0
  65. package/dist/esm/component/threads.js.map +1 -0
  66. package/dist/esm/component/users.d.ts +37 -0
  67. package/dist/esm/component/users.d.ts.map +1 -0
  68. package/dist/esm/component/users.js +118 -0
  69. package/dist/esm/component/users.js.map +1 -0
  70. package/dist/esm/validators.d.ts +2 -6
  71. package/dist/esm/validators.d.ts.map +1 -1
  72. package/dist/esm/validators.js +0 -1
  73. package/dist/esm/validators.js.map +1 -1
  74. package/package.json +10 -2
  75. package/src/client/index.ts +150 -117
  76. package/src/client/playground.ts +231 -0
  77. package/src/component/_generated/api.d.ts +319 -107
  78. package/src/component/apiKeys.ts +74 -0
  79. package/src/component/files.ts +72 -0
  80. package/src/component/messages.ts +54 -308
  81. package/src/component/schema.ts +7 -2
  82. package/src/component/threads.ts +184 -0
  83. package/src/component/users.ts +145 -0
  84. package/src/validators.ts +0 -1
package/README.md CHANGED
@@ -135,7 +135,8 @@ const supportAgent = new Agent(components.agent, {
135
135
  myConvexTool: createTool({
136
136
  description: "My Convex tool",
137
137
  args: z.object({...}),
138
- handler: async (ctx, args) => {
138
+ // Note: annotate the return type of the handler to avoid type cycles.
139
+ handler: async (ctx, args): Promise<string> => {
139
140
  return "Hello, world!";
140
141
  },
141
142
  }),
@@ -230,6 +231,30 @@ export const continueThread = action({
230
231
  });
231
232
  ```
232
233
 
234
+ ### Sending a message with configurable message history context
235
+
236
+ You can customize what history is included per-message via `contextOptions`.
237
+ See the [configuring the agent](#configuring-the-agent) section for details.
238
+
239
+ ```ts
240
+ const result = await thread.generateText({ prompt }, { contextOptions });
241
+ ```
242
+
243
+ ### Configuring the storage of messages
244
+
245
+ See the [configuring the agent](#configuring-the-agent) section for details.
246
+ Generally the defaults are fine, but if you want to pass in multiple messages
247
+ and have them all saved (vs. just the last one), or avoid saving any input
248
+ or output messages, you can pass in a `storageOptions` object.
249
+
250
+ The usecase for passing multiple messages is if you want to include some extra
251
+ messages for context to the LLM, but only the last message is the user's actual
252
+ request. e.g. `messages = [...messagesFromRag, messageFromUser]`.
253
+
254
+ ```ts
255
+ const result = await thread.generateText({ messages }, { storageOptions });
256
+ ```
257
+
233
258
  ### Creating a tool with Convex context
234
259
 
235
260
  There are two ways to create a tool that has access to the Convex context.
@@ -275,6 +300,71 @@ Specifying tools at each layer will overwrite the defaults.
275
300
  The tools will be `args.tools ?? thread.tools ?? agent.options.tools`.
276
301
  This allows you to create tools in a context that is convenient.
277
302
 
303
+ ### Fetching thread history
304
+
305
+ Fetch the full messages directly. These will include things like usage, etc.
306
+
307
+ ```ts
308
+ import type { MessageDoc } from "@convex-dev/agent";
309
+
310
+ const messages: MessageDoc[] = await ctx.runQuery(
311
+ components.agent.messages.listMessagesByThreadId, {
312
+ threadId,
313
+ order: "desc",
314
+ paginationOpts: { cursor: null, numItems: 10 }
315
+ });
316
+ ```
317
+
318
+ ### Search for messages
319
+
320
+ This is what the agent does automatically, but it can be useful to do manually, e.g. to find custom context to include.
321
+
322
+ Fetch CoreMessages (e.g. `{ role, content }`) for a user and/or thread.
323
+ Accepts ContextOptions, e.g. includeToolCalls, searchOptions, etc.
324
+ If you provide a `beforeMessageId`, it will only fetch messages from before that message.
325
+
326
+ ```ts
327
+ const coreMessages = await supportAgent.fetchContextMessages(ctx, {
328
+ threadId, messages: [{ role, content }], contextOptions
329
+ });
330
+ ```
331
+
332
+ ### Get and update thread information
333
+
334
+ List threads for a user:
335
+
336
+ ```ts
337
+ const threads = await ctx.runQuery(components.agent.threads.listThreadsByUserId, {
338
+ userId,
339
+ order: "desc",
340
+ paginationOpts: { cursor: null, numItems: 10 }
341
+ });
342
+ ```
343
+
344
+ Get a thread by id:
345
+
346
+ ```ts
347
+ const thread = await ctx.runQuery(components.agent.threads.getThread, {
348
+ threadId,
349
+ });
350
+ ```
351
+
352
+ Update a thread's metadata:
353
+
354
+ ```ts
355
+ await ctx.runMutation(components.agent.threads.updateThread, {
356
+ threadId,
357
+ { title, summary, status }
358
+ });
359
+ ```
360
+
361
+ ## Using the Workflow component for long-lived durable workflows
362
+
363
+ The [Workflow component](https://convex.dev/components/workflow) is a great way to build long-lived, durable workflows.
364
+ It handles retries and guarantees of eventually completing, surviving server restarts, and more.
365
+ Read more about durable workflows in [this Stack post](https://stack.convex.dev/durable-workflows-and-strong-guarantees).
366
+
367
+
278
368
  ### Exposing the agent as Convex actions
279
369
 
280
370
  You can expose the agent as a Convex internal action.
@@ -334,14 +424,7 @@ export const supportAgentWorkflow = workflow.define({
334
424
 
335
425
  See another example in [example.ts](./example/convex/example.ts#L120).
336
426
 
337
- ### Fetching thread history
338
-
339
- ```ts
340
- const messages = await ctx.runQuery(
341
- components.agent.messages.getThreadMessages,
342
- { threadId }
343
- );
344
- ```
427
+ ## Extra control: how to do more things yourself
345
428
 
346
429
  ### Generating text for a user without an associated thread
347
430
 
@@ -349,26 +432,7 @@ const messages = await ctx.runQuery(
349
432
  const result = await supportAgent.generateText(ctx, { userId }, { prompt });
350
433
  ```
351
434
 
352
- ### Manually managing messages
353
-
354
- Fetch the full messages directly. These will include things like usage, etc.
355
-
356
- ```ts
357
- const messages = await ctx.runQuery(
358
- components.agent.messages.getThreadMessages,
359
- { threadId, order: "desc", paginationOpts: { cursor: null, numItems: 10 } }
360
- );
361
- ```
362
-
363
- Fetch CoreMessages (e.g. `{ role, content }`) for a user and/or thread.
364
- Accepts ContextOptions, e.g. includeToolCalls, searchOptions, etc.
365
- If you provide a parentMessageId, it will only fetch messages from before that message.
366
-
367
- ```ts
368
- const coreMessages = await supportAgent.fetchContextMessages(ctx, {
369
- threadId, messages: [{ role, content }], contextOptions
370
- });
371
- ```
435
+ ### Saving messages manually
372
436
 
373
437
  Save messages to the database.
374
438