@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.
- package/README.md +93 -29
- package/dist/commonjs/client/index.d.ts +768 -71
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +81 -42
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/playground.d.ts +474 -0
- package/dist/commonjs/client/playground.d.ts.map +1 -0
- package/dist/commonjs/client/playground.js +178 -0
- package/dist/commonjs/client/playground.js.map +1 -0
- package/dist/commonjs/component/apiKeys.d.ts +11 -0
- package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
- package/dist/commonjs/component/apiKeys.js +69 -0
- package/dist/commonjs/component/apiKeys.js.map +1 -0
- package/dist/commonjs/component/files.d.ts +31 -0
- package/dist/commonjs/component/files.d.ts.map +1 -0
- package/dist/commonjs/component/files.js +61 -0
- package/dist/commonjs/component/files.js.map +1 -0
- package/dist/commonjs/component/messages.d.ts +40 -109
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +44 -260
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +54 -10
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +7 -2
- package/dist/commonjs/component/schema.js.map +1 -1
- package/dist/commonjs/component/threads.d.ts +95 -0
- package/dist/commonjs/component/threads.d.ts.map +1 -0
- package/dist/commonjs/component/threads.js +151 -0
- package/dist/commonjs/component/threads.js.map +1 -0
- package/dist/commonjs/component/users.d.ts +37 -0
- package/dist/commonjs/component/users.d.ts.map +1 -0
- package/dist/commonjs/component/users.js +118 -0
- package/dist/commonjs/component/users.js.map +1 -0
- package/dist/commonjs/validators.d.ts +2 -6
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +0 -1
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +768 -71
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +81 -42
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/playground.d.ts +474 -0
- package/dist/esm/client/playground.d.ts.map +1 -0
- package/dist/esm/client/playground.js +178 -0
- package/dist/esm/client/playground.js.map +1 -0
- package/dist/esm/component/apiKeys.d.ts +11 -0
- package/dist/esm/component/apiKeys.d.ts.map +1 -0
- package/dist/esm/component/apiKeys.js +69 -0
- package/dist/esm/component/apiKeys.js.map +1 -0
- package/dist/esm/component/files.d.ts +31 -0
- package/dist/esm/component/files.d.ts.map +1 -0
- package/dist/esm/component/files.js +61 -0
- package/dist/esm/component/files.js.map +1 -0
- package/dist/esm/component/messages.d.ts +40 -109
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +44 -260
- package/dist/esm/component/messages.js.map +1 -1
- package/dist/esm/component/schema.d.ts +54 -10
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +7 -2
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/threads.d.ts +95 -0
- package/dist/esm/component/threads.d.ts.map +1 -0
- package/dist/esm/component/threads.js +151 -0
- package/dist/esm/component/threads.js.map +1 -0
- package/dist/esm/component/users.d.ts +37 -0
- package/dist/esm/component/users.d.ts.map +1 -0
- package/dist/esm/component/users.js +118 -0
- package/dist/esm/component/users.js.map +1 -0
- package/dist/esm/validators.d.ts +2 -6
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +0 -1
- package/dist/esm/validators.js.map +1 -1
- package/package.json +10 -2
- package/src/client/index.ts +150 -117
- package/src/client/playground.ts +231 -0
- package/src/component/_generated/api.d.ts +319 -107
- package/src/component/apiKeys.ts +74 -0
- package/src/component/files.ts +72 -0
- package/src/component/messages.ts +54 -308
- package/src/component/schema.ts +7 -2
- package/src/component/threads.ts +184 -0
- package/src/component/users.ts +145 -0
- 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
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
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
|
|