@convex-dev/agent 0.0.16-alpha.0 → 0.0.17-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 +92 -71
- package/dist/commonjs/client/index.d.ts +1012 -920
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +188 -31
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/playground.d.ts +251 -116
- package/dist/commonjs/client/playground.d.ts.map +1 -1
- package/dist/commonjs/client/playground.js +33 -11
- package/dist/commonjs/client/playground.js.map +1 -1
- package/dist/commonjs/component/messages.d.ts +683 -571
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +56 -33
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +1080 -1324
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +20 -14
- package/dist/commonjs/component/schema.js.map +1 -1
- package/dist/commonjs/component/vector/index.d.ts.map +1 -1
- package/dist/commonjs/component/vector/index.js +4 -6
- package/dist/commonjs/component/vector/index.js.map +1 -1
- package/dist/commonjs/component/vector/tables.d.ts +10 -10
- package/dist/commonjs/component/vector/tables.d.ts.map +1 -1
- package/dist/commonjs/component/vector/tables.js.map +1 -1
- package/dist/commonjs/mapping.d.ts +7 -1
- package/dist/commonjs/mapping.d.ts.map +1 -1
- package/dist/commonjs/mapping.js +39 -18
- package/dist/commonjs/mapping.js.map +1 -1
- package/dist/commonjs/validators.d.ts +1669 -2336
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +35 -25
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +1012 -920
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +188 -31
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/playground.d.ts +251 -116
- package/dist/esm/client/playground.d.ts.map +1 -1
- package/dist/esm/client/playground.js +33 -11
- package/dist/esm/client/playground.js.map +1 -1
- package/dist/esm/component/messages.d.ts +683 -571
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +56 -33
- package/dist/esm/component/messages.js.map +1 -1
- package/dist/esm/component/schema.d.ts +1080 -1324
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +20 -14
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/vector/index.d.ts.map +1 -1
- package/dist/esm/component/vector/index.js +4 -6
- package/dist/esm/component/vector/index.js.map +1 -1
- package/dist/esm/component/vector/tables.d.ts +10 -10
- package/dist/esm/component/vector/tables.d.ts.map +1 -1
- package/dist/esm/component/vector/tables.js.map +1 -1
- package/dist/esm/mapping.d.ts +7 -1
- package/dist/esm/mapping.d.ts.map +1 -1
- package/dist/esm/mapping.js +39 -18
- package/dist/esm/mapping.js.map +1 -1
- package/dist/esm/validators.d.ts +1669 -2336
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +35 -25
- package/dist/esm/validators.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.test.ts +68 -0
- package/src/client/index.ts +219 -35
- package/src/client/playground.ts +49 -23
- package/src/component/_generated/api.d.ts +311 -323
- package/src/component/messages.test.ts +23 -0
- package/src/component/messages.ts +75 -42
- package/src/component/schema.ts +26 -13
- package/src/component/vector/index.ts +5 -6
- package/src/component/vector/tables.ts +15 -15
- package/src/mapping.ts +65 -32
- package/src/validators.ts +47 -27
package/README.md
CHANGED
|
@@ -111,7 +111,7 @@ export default app;
|
|
|
111
111
|
|
|
112
112
|
## Usage
|
|
113
113
|
|
|
114
|
-
###
|
|
114
|
+
### Creating the agent
|
|
115
115
|
|
|
116
116
|
```ts
|
|
117
117
|
import { tool } from "ai";
|
|
@@ -124,8 +124,6 @@ import { components } from "./_generated/api";
|
|
|
124
124
|
const supportAgent = new Agent(components.agent, {
|
|
125
125
|
// The chat completions model to use for the agent.
|
|
126
126
|
chat: openai.chat("gpt-4o-mini"),
|
|
127
|
-
// Embedding model to power vector search of message history (RAG).
|
|
128
|
-
textEmbedding: openai.embedding("text-embedding-3-small"),
|
|
129
127
|
// The default system prompt if not overriden.
|
|
130
128
|
instructions: "You are a helpful assistant.",
|
|
131
129
|
tools: {
|
|
@@ -141,52 +139,20 @@ const supportAgent = new Agent(components.agent, {
|
|
|
141
139
|
},
|
|
142
140
|
}),
|
|
143
141
|
},
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
recentMessages: 100,
|
|
151
|
-
// Options for searching messages via text and/or vector search.
|
|
152
|
-
searchOptions: {
|
|
153
|
-
limit: 10, // The maximum number of messages to fetch.
|
|
154
|
-
textSearch: false, // Whether to use text search to find messages.
|
|
155
|
-
vectorSearch: false, // Whether to use vector search to find messages.
|
|
156
|
-
// Note, this is after the limit is applied.
|
|
157
|
-
// E.g. this will quadruple the number of messages fetched.
|
|
158
|
-
// (two before, and one after each message found in the search)
|
|
159
|
-
messageRange: { before: 2, after: 1 },
|
|
160
|
-
},
|
|
161
|
-
// Whether to search across other threads for relevant messages.
|
|
162
|
-
// By default, only the current thread is searched.
|
|
163
|
-
searchOtherThreads: false,
|
|
164
|
-
},
|
|
165
|
-
// Used for storing messages.
|
|
166
|
-
storageOptions: {
|
|
167
|
-
// When false, allows you to pass in arbitrary context that will
|
|
168
|
-
// be in addition to automatically fetched content.
|
|
169
|
-
// Pass true to have all input messages saved to the thread history.
|
|
170
|
-
saveAllInputMessages: false,
|
|
171
|
-
// By default it saves the input message, or the last message if multiple are provided.
|
|
172
|
-
saveAnyInputMessages: true,
|
|
173
|
-
// Save the generated messages to the thread history.
|
|
174
|
-
saveOutputMessages: true,
|
|
175
|
-
},
|
|
142
|
+
// Embedding model to power vector search of message history (RAG).
|
|
143
|
+
textEmbedding: openai.embedding("text-embedding-3-small"),
|
|
144
|
+
// Used for fetching context messages. See [below](#configuring-the-context-of-messages)
|
|
145
|
+
contextOptions,
|
|
146
|
+
// Used for storing messages. See [below](#configuring-the-storage-of-messages)
|
|
147
|
+
storageOptions,
|
|
176
148
|
// Used for limiting the number of steps when tool calls are involved.
|
|
149
|
+
// NOTE: if you want tool calls to happen automatically with a single call,
|
|
150
|
+
// you need to set this to something greater than 1 (the default).
|
|
177
151
|
maxSteps: 1,
|
|
178
|
-
// Used for limiting the number of retries when a tool call fails.
|
|
152
|
+
// Used for limiting the number of retries when a tool call fails. Default: 3.
|
|
179
153
|
maxRetries: 3,
|
|
180
|
-
// Used for tracking token usage.
|
|
181
|
-
usageHandler: async (ctx,
|
|
182
|
-
const {
|
|
183
|
-
// Who used the tokens
|
|
184
|
-
userId, threadId, agentName,
|
|
185
|
-
// What LLM was used
|
|
186
|
-
model, provider,
|
|
187
|
-
// How many tokens were used (extra info is available in providerMetadata)
|
|
188
|
-
usage, providerMetadata
|
|
189
|
-
} = args;
|
|
154
|
+
// Used for tracking token usage. See [below](#tracking-token-usage)
|
|
155
|
+
usageHandler: async (ctx, { model, usage }) => {
|
|
190
156
|
// ... log, save usage to your database, etc.
|
|
191
157
|
},
|
|
192
158
|
});
|
|
@@ -231,28 +197,87 @@ export const continueThread = action({
|
|
|
231
197
|
});
|
|
232
198
|
```
|
|
233
199
|
|
|
234
|
-
###
|
|
200
|
+
### Generating text
|
|
201
|
+
|
|
202
|
+
The arguments to `generateText` are the same as the AI SDK, except you don't
|
|
203
|
+
have to provide a model. By default it will use the agent's chat model.
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const { thread } = await supportAgent.createThread(ctx, { userId });
|
|
207
|
+
const result = await thread.generateText({ prompt });
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Generating an object
|
|
211
|
+
|
|
212
|
+
Similar to the AI SDK, you can generate or streaman object.
|
|
213
|
+
The same arguments apply, except you don't have to provide a model.
|
|
214
|
+
It will use the agent's default chat model.
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { z } from "zod";
|
|
218
|
+
|
|
219
|
+
const result = await thread.generateObject({
|
|
220
|
+
prompt: "Generate a plan based on the conversation so far",
|
|
221
|
+
schema: z.object({...}),
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
### Configuring the context of messages
|
|
235
227
|
|
|
236
228
|
You can customize what history is included per-message via `contextOptions`.
|
|
237
|
-
|
|
229
|
+
These options can be provided to the Agent constructor, or per-message.
|
|
238
230
|
|
|
239
231
|
```ts
|
|
240
|
-
const result = await thread.generateText({ prompt }, {
|
|
232
|
+
const result = await thread.generateText({ prompt }, {
|
|
233
|
+
// Values shown are the defaults.
|
|
234
|
+
contextOptions: {
|
|
235
|
+
// Whether to include tool messages in the context.
|
|
236
|
+
includeToolCalls: false,
|
|
237
|
+
// How many recent messages to include. These are added after the search
|
|
238
|
+
// messages, and do not count against the search limit.
|
|
239
|
+
recentMessages: 100,
|
|
240
|
+
// Options for searching messages via text and/or vector search.
|
|
241
|
+
searchOptions: {
|
|
242
|
+
limit: 10, // The maximum number of messages to fetch.
|
|
243
|
+
textSearch: false, // Whether to use text search to find messages.
|
|
244
|
+
vectorSearch: false, // Whether to use vector search to find messages.
|
|
245
|
+
// Note, this is after the limit is applied.
|
|
246
|
+
// E.g. this will quadruple the number of messages fetched.
|
|
247
|
+
// (two before, and one after each message found in the search)
|
|
248
|
+
messageRange: { before: 2, after: 1 },
|
|
249
|
+
},
|
|
250
|
+
// Whether to search across other threads for relevant messages.
|
|
251
|
+
// By default, only the current thread is searched.
|
|
252
|
+
searchOtherThreads: false,
|
|
253
|
+
},
|
|
241
254
|
```
|
|
242
255
|
|
|
243
256
|
### Configuring the storage of messages
|
|
244
257
|
|
|
245
|
-
See the [configuring the agent](#configuring-the-agent) section for details.
|
|
246
258
|
Generally the defaults are fine, but if you want to pass in multiple messages
|
|
247
259
|
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
|
|
260
|
+
or output messages, you can pass in a `storageOptions` object, either to the
|
|
261
|
+
Agent constructor or per-message.
|
|
249
262
|
|
|
250
263
|
The usecase for passing multiple messages is if you want to include some extra
|
|
251
264
|
messages for context to the LLM, but only the last message is the user's actual
|
|
252
265
|
request. e.g. `messages = [...messagesFromRag, messageFromUser]`.
|
|
253
266
|
|
|
254
267
|
```ts
|
|
255
|
-
const result = await thread.generateText({ messages }, {
|
|
268
|
+
const result = await thread.generateText({ messages }, {
|
|
269
|
+
// The default values are shown below.
|
|
270
|
+
storageOptions: {
|
|
271
|
+
// When false, allows you to pass in arbitrary context that will
|
|
272
|
+
// be in addition to automatically fetched content.
|
|
273
|
+
// Pass true to have all input messages saved to the thread history.
|
|
274
|
+
saveAllInputMessages: false,
|
|
275
|
+
// By default it saves the input message, or the last message if multiple are provided.
|
|
276
|
+
saveAnyInputMessages: true,
|
|
277
|
+
// Save the generated messages to the thread history.
|
|
278
|
+
saveOutputMessages: true,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
256
281
|
```
|
|
257
282
|
|
|
258
283
|
### Creating a tool with Convex context
|
|
@@ -319,12 +344,14 @@ const messages: MessageDoc[] = await ctx.runQuery(
|
|
|
319
344
|
|
|
320
345
|
This is what the agent does automatically, but it can be useful to do manually, e.g. to find custom context to include.
|
|
321
346
|
|
|
322
|
-
Fetch
|
|
347
|
+
Fetch Messages for a user and/or thread.
|
|
323
348
|
Accepts ContextOptions, e.g. includeToolCalls, searchOptions, etc.
|
|
324
349
|
If you provide a `beforeMessageId`, it will only fetch messages from before that message.
|
|
325
350
|
|
|
326
351
|
```ts
|
|
327
|
-
|
|
352
|
+
import type { MessageDoc } from "@convex-dev/agent";
|
|
353
|
+
|
|
354
|
+
const messages: MessageDoc[] = await supportAgent.fetchContextMessages(ctx, {
|
|
328
355
|
threadId, messages: [{ role, content }], contextOptions
|
|
329
356
|
});
|
|
330
357
|
```
|
|
@@ -508,27 +535,21 @@ See an example in
|
|
|
508
535
|
[this demo](https://github.com/ianmacartney/ai-agent-chat/blob/main/convex/chat.ts)
|
|
509
536
|
that captures usage to a table, then scans it to generate per-user invoices.
|
|
510
537
|
|
|
538
|
+
You can provide a `usageHandler` to the agent, per-thread, or per-message.
|
|
539
|
+
|
|
511
540
|
```ts
|
|
512
541
|
const supportAgent = new Agent(components.agent, {
|
|
513
542
|
...
|
|
514
543
|
usageHandler: async (ctx, args) => {
|
|
515
|
-
const {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
//
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
// ...
|
|
525
|
-
},
|
|
526
|
-
});
|
|
527
|
-
// or when generating text:
|
|
528
|
-
const result = await thread.generateText({
|
|
529
|
-
...
|
|
530
|
-
usageHandler: async (ctx, args) => {
|
|
531
|
-
// ...
|
|
544
|
+
const {
|
|
545
|
+
// Who used the tokens
|
|
546
|
+
userId, threadId, agentName,
|
|
547
|
+
// What LLM was used
|
|
548
|
+
model, provider,
|
|
549
|
+
// How many tokens were used (extra info is available in providerMetadata)
|
|
550
|
+
usage, providerMetadata
|
|
551
|
+
} = args;
|
|
552
|
+
// ... log, save usage to your database, etc.
|
|
532
553
|
},
|
|
533
554
|
});
|
|
534
555
|
```
|