@convex-dev/agent 0.2.0-alpha.2 → 0.2.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 (96) hide show
  1. package/dist/client/createTool.d.ts +3 -5
  2. package/dist/client/createTool.d.ts.map +1 -1
  3. package/dist/client/createTool.js +0 -1
  4. package/dist/client/createTool.js.map +1 -1
  5. package/dist/client/definePlaygroundAPI.d.ts +28 -28
  6. package/dist/client/files.d.ts +8 -1
  7. package/dist/client/files.d.ts.map +1 -1
  8. package/dist/client/files.js +60 -5
  9. package/dist/client/files.js.map +1 -1
  10. package/dist/client/index.d.ts +223 -239
  11. package/dist/client/index.d.ts.map +1 -1
  12. package/dist/client/index.js +368 -382
  13. package/dist/client/index.js.map +1 -1
  14. package/dist/client/messages.d.ts +91 -373
  15. package/dist/client/messages.d.ts.map +1 -1
  16. package/dist/client/messages.js +7 -1
  17. package/dist/client/messages.js.map +1 -1
  18. package/dist/client/search.d.ts +15 -15
  19. package/dist/client/search.d.ts.map +1 -1
  20. package/dist/client/search.js +3 -3
  21. package/dist/client/search.js.map +1 -1
  22. package/dist/client/streaming.d.ts +21 -22
  23. package/dist/client/streaming.d.ts.map +1 -1
  24. package/dist/client/streaming.js +35 -40
  25. package/dist/client/streaming.js.map +1 -1
  26. package/dist/client/types.d.ts +148 -61
  27. package/dist/client/types.d.ts.map +1 -1
  28. package/dist/component/_generated/api.d.ts +47 -357
  29. package/dist/component/messages.d.ts +157 -150
  30. package/dist/component/messages.d.ts.map +1 -1
  31. package/dist/component/messages.js +124 -70
  32. package/dist/component/messages.js.map +1 -1
  33. package/dist/component/schema.d.ts +1147 -2687
  34. package/dist/component/schema.d.ts.map +1 -1
  35. package/dist/component/schema.js +5 -4
  36. package/dist/component/schema.js.map +1 -1
  37. package/dist/component/streams.d.ts +29 -323
  38. package/dist/component/streams.d.ts.map +1 -1
  39. package/dist/component/streams.js +104 -60
  40. package/dist/component/streams.js.map +1 -1
  41. package/dist/component/threads.d.ts +4 -4
  42. package/dist/component/users.d.ts +3 -3
  43. package/dist/mapping.d.ts +4 -1
  44. package/dist/mapping.d.ts.map +1 -1
  45. package/dist/mapping.js +17 -1
  46. package/dist/mapping.js.map +1 -1
  47. package/dist/parts.d.ts +3 -0
  48. package/dist/parts.d.ts.map +1 -0
  49. package/dist/parts.js +34 -0
  50. package/dist/parts.js.map +1 -0
  51. package/dist/react/SmoothText.d.ts +5 -0
  52. package/dist/react/SmoothText.d.ts.map +1 -0
  53. package/dist/react/SmoothText.js +6 -0
  54. package/dist/react/SmoothText.js.map +1 -0
  55. package/dist/react/deltas.d.ts +3 -2
  56. package/dist/react/deltas.d.ts.map +1 -1
  57. package/dist/react/deltas.js +19 -79
  58. package/dist/react/deltas.js.map +1 -1
  59. package/dist/react/index.d.ts +1 -0
  60. package/dist/react/index.d.ts.map +1 -1
  61. package/dist/react/index.js +31 -11
  62. package/dist/react/index.js.map +1 -1
  63. package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
  64. package/dist/react/optimisticallySendMessage.js +2 -8
  65. package/dist/react/optimisticallySendMessage.js.map +1 -1
  66. package/dist/react/useSmoothText.d.ts +13 -12
  67. package/dist/react/useSmoothText.d.ts.map +1 -1
  68. package/dist/react/useSmoothText.js +1 -1
  69. package/dist/react/useSmoothText.js.map +1 -1
  70. package/dist/validators.d.ts +109 -1590
  71. package/dist/validators.d.ts.map +1 -1
  72. package/dist/validators.js +4 -81
  73. package/dist/validators.js.map +1 -1
  74. package/package.json +3 -3
  75. package/src/client/createTool.ts +4 -9
  76. package/src/client/files.ts +88 -10
  77. package/src/client/index.test.ts +9 -21
  78. package/src/client/index.ts +664 -650
  79. package/src/client/messages.ts +16 -2
  80. package/src/client/search.ts +10 -7
  81. package/src/client/streaming.ts +53 -57
  82. package/src/client/types.ts +227 -97
  83. package/src/component/_generated/api.d.ts +47 -357
  84. package/src/component/messages.ts +157 -96
  85. package/src/component/schema.ts +4 -4
  86. package/src/component/streams.ts +180 -69
  87. package/src/mapping.ts +23 -2
  88. package/src/parts.ts +39 -0
  89. package/src/react/SmoothText.tsx +9 -0
  90. package/src/react/deltas.test.ts +38 -42
  91. package/src/react/deltas.ts +24 -86
  92. package/src/react/index.ts +35 -13
  93. package/src/react/optimisticallySendMessage.ts +2 -8
  94. package/src/react/useSmoothText.ts +14 -15
  95. package/src/validators.test.ts +0 -17
  96. package/src/validators.ts +8 -101
@@ -1,49 +1,50 @@
1
- import type { LanguageModelV2 } from "@ai-sdk/provider";
2
1
  import type {
3
- ModelMessage,
4
- DeepPartial,
5
- generateObject,
2
+ InferSchema,
3
+ ProviderOptions,
4
+ Schema,
5
+ } from "@ai-sdk/provider-utils";
6
+ import type { JSONValue } from "@ai-sdk/provider";
7
+ import type {
6
8
  GenerateObjectResult,
7
9
  generateText,
8
10
  GenerateTextResult,
9
- JSONValue,
10
11
  LanguageModelRequestMetadata,
11
12
  LanguageModelResponseMetadata,
12
- RepairTextFunction,
13
+ LanguageModelUsage,
14
+ LanguageModel,
13
15
  streamObject,
14
- StreamObjectResult,
15
16
  streamText,
16
17
  StreamTextResult,
17
- TelemetrySettings,
18
18
  ToolChoice,
19
19
  ToolSet,
20
- Schema,
21
- LanguageModelUsage,
20
+ RepairTextFunction,
21
+ TelemetrySettings,
22
+ CallSettings,
23
+ Prompt,
22
24
  } from "ai";
23
25
  import type {
24
26
  Auth,
25
27
  Expand,
26
- FunctionReference,
27
- StorageActionWriter,
28
- StorageReader,
29
- WithoutSystemFields,
30
28
  FunctionArgs,
29
+ FunctionReference,
31
30
  FunctionReturnType,
32
31
  GenericActionCtx,
33
32
  GenericDataModel,
33
+ StorageActionWriter,
34
+ StorageReader,
35
+ WithoutSystemFields,
34
36
  } from "convex/server";
35
37
  import type { GenericId } from "convex/values";
36
- import type { z } from "zod/v3";
37
38
  import type { Mounts } from "../component/_generated/api.js";
38
39
  import type { MessageDoc, ThreadDoc } from "../component/schema.js";
39
40
  import type {
40
- CallSettings,
41
41
  ProviderMetadata,
42
- ProviderOptions,
43
42
  StreamDelta,
44
43
  StreamMessage,
45
44
  } from "../validators.js";
46
45
  import type { StreamingOptions } from "./streaming.js";
46
+ import type * as z3 from "zod/v3";
47
+ import type * as z4 from "zod/v4";
47
48
 
48
49
  /**
49
50
  * Options to configure what messages are fetched as context,
@@ -119,16 +120,23 @@ export type GenerationOutputMetadata = {
119
120
  /**
120
121
  * The ID of the prompt message for the generation.
121
122
  */
122
- messageId?: string;
123
+ promptMessageId?: string;
123
124
  /**
124
- * The order of the prompt message for the generation.
125
+ * The order of the prompt message and responses for the generation.
126
+ * Each order starts with a user message, then followed by agent responses.
127
+ * If a promptMessageId is provided, that dictates the order.
125
128
  */
126
129
  order?: number;
127
130
  /**
128
131
  * The messages saved for the generation - both saved input and output.
129
132
  * If you passed promptMessageId, it will not include that message.
130
133
  */
131
- messages?: MessageDoc[];
134
+ savedMessages?: MessageDoc[];
135
+ /**
136
+ * @deprecated Use promptMessageId instead.
137
+ * The ID of the prompt message for the generation.
138
+ */
139
+ messageId?: string;
132
140
  };
133
141
 
134
142
  export type UsageHandler = (
@@ -184,7 +192,7 @@ export type TextArgs<
184
192
  * The model to use for the LLM calls. This will override the model specified
185
193
  * in the Agent constructor.
186
194
  */
187
- model?: LanguageModelV2;
195
+ model?: LanguageModel;
188
196
  /**
189
197
  * The tools to use for the tool calls. This will override tools specified
190
198
  * in the Agent constructor or createThread / continueThread.
@@ -223,7 +231,7 @@ export type StreamingTextArgs<
223
231
  * The model to use for the tool calls. This will override the model specified
224
232
  * in the Agent constructor.
225
233
  */
226
- model?: LanguageModelV2;
234
+ model?: LanguageModel;
227
235
  /**
228
236
  * The tools to use for the tool calls. This will override tools specified
229
237
  * in the Agent constructor or createThread / continueThread.
@@ -236,77 +244,142 @@ export type StreamingTextArgs<
236
244
  toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
237
245
  };
238
246
 
239
- type BaseGenerateObjectOptions = CallSettings & {
240
- /**
241
- * The model to use for the object generation. This will override the model
242
- * specified in the Agent constructor.
243
- */
244
- model?: LanguageModelV2;
245
- /**
246
- * The system prompt to use for the object generation. This will override the
247
- * system prompt specified in the Agent constructor.
248
- */
249
- system?: string;
250
- /**
251
- * The prompt to the LLM to use for the object generation.
252
- * Specify this or messages, but not both.
253
- */
254
- prompt?: string | Array<ModelMessage>;
255
- /**
256
- * The messages to use for the object generation.
257
- * Note: recent messages are automatically added based on the thread it's
258
- * associated with and your contextOptions.
259
- */
260
- messages?: Array<ModelMessage>;
261
- /**
262
- * The message to use as the "prompt" for the object generation.
263
- * If this is provided, it will be used instead of the prompt or messages.
264
- * This is useful if you want to first save a user message, then use it as
265
- * the prompt for the object generation in another call.
266
- */
267
- promptMessageId?: string;
268
- experimental_repairText?: RepairTextFunction;
269
- experimental_telemetry?: TelemetrySettings;
270
- providerOptions?: ProviderOptions;
271
- experimental_providerMetadata?: ProviderMetadata;
272
- };
247
+ export type ObjectMode = "object" | "array" | "enum" | "no-schema";
248
+ export type ObjectSchema = z3.Schema | z4.core.$ZodType | Schema;
249
+ export type DefaultObjectSchema = z4.core.$ZodType<JSONValue>;
273
250
 
274
- type StandardGenerateObjectOptions<T> = {
275
- schema: z.Schema<T>;
276
- schemaName?: string;
277
- schemaDescription?: string;
278
- output?: "object" | "array";
279
- mode?: "auto" | "json" | "tool";
280
- };
251
+ /**
252
+ * Due to some issues with the type inference of the ai sdk, we need to
253
+ * manually type the arguments for the generateObject function.
254
+ * This is a workaround to allow the model to be optional.
255
+ */
256
+ export type GenerateObjectArgs<
257
+ SCHEMA extends ObjectSchema = DefaultObjectSchema,
258
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
259
+ ? "enum"
260
+ : "object",
261
+ RESULT = OUTPUT extends "array"
262
+ ? Array<InferSchema<SCHEMA>>
263
+ : InferSchema<SCHEMA>,
264
+ > = Omit<CallSettings, "stopSequences"> &
265
+ Prompt &
266
+ (OUTPUT extends "enum"
267
+ ? {
268
+ /**
269
+ The enum values that the model should use.
270
+ */
271
+ enum: Array<RESULT>;
272
+ mode?: "json";
273
+ output: "enum";
274
+ }
275
+ : OUTPUT extends "no-schema"
276
+ ? // eslint-disable-next-line @typescript-eslint/no-empty-object-type
277
+ {}
278
+ : {
279
+ /**
280
+ The schema of the object that the model should generate.
281
+ */
282
+ schema: SCHEMA;
283
+ /**
284
+ Optional name of the output that should be generated.
285
+ Used by some providers for additional LLM guidance, e.g.
286
+ via tool or schema name.
287
+ */
288
+ schemaName?: string;
289
+ /**
290
+ Optional description of the output that should be generated.
291
+ Used by some providers for additional LLM guidance, e.g.
292
+ via tool or schema description.
293
+ */
294
+ schemaDescription?: string;
295
+ /**
296
+ The mode to use for object generation.
297
+
298
+ The schema is converted into a JSON schema and used in one of the following ways
281
299
 
282
- // TODO: simplify this to just use the generateObject args, with an optional
283
- // model and tool/toolChoice types
284
- type GenerateObjectArgs<T> = BaseGenerateObjectOptions &
285
- (
286
- | StandardGenerateObjectOptions<T>
287
- | { output: "enum"; enum: Array<T>; mode?: "auto" | "json" | "tool" }
288
- | { output: "any"; schema: undefined; mode: "json" }
289
- );
300
+ - 'auto': The provider will choose the best mode for the model.
301
+ - 'tool': A tool with the JSON schema as parameters is provided and the provider is instructed to use it.
302
+ - 'json': The JSON schema and an instruction are injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.
290
303
 
291
- type StreamObjectArgs<T> = BaseGenerateObjectOptions &
292
- (
293
- | StandardGenerateObjectOptions<T>
294
- | { output: "any"; schema: undefined; mode: "json" }
295
- );
304
+ Please note that most providers do not support all modes.
305
+
306
+ Default and recommended: 'auto' (best mode for the model).
307
+ */
308
+ mode?: "auto" | "json" | "tool";
309
+ }) & {
310
+ output?: OUTPUT;
311
+ /**
312
+ The language model to use.
313
+ */
314
+ model?: LanguageModel;
315
+ /**
316
+ A function that attempts to repair the raw output of the model
317
+ to enable JSON parsing.
318
+ */
319
+ experimental_repairText?: RepairTextFunction;
320
+ /**
321
+ Optional telemetry configuration (experimental).
322
+ */
323
+ experimental_telemetry?: TelemetrySettings;
324
+ /**
325
+ Additional provider-specific options. They are passed through
326
+ to the provider from the AI SDK and enable provider-specific
327
+ functionality that can be fully encapsulated in the provider.
328
+ */
329
+ providerOptions?: ProviderOptions;
330
+ /**
331
+ * Internal. For test use only. May change without notice.
332
+ */
333
+ _internal?: { generateId?: () => string; currentDate?: () => Date };
334
+ };
296
335
 
297
- export type OurObjectArgs<T> = GenerateObjectArgs<T> &
336
+ export type StreamObjectArgs<
337
+ SCHEMA extends ObjectSchema = DefaultObjectSchema,
338
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
339
+ ? "enum"
340
+ : "object",
341
+ RESULT = OUTPUT extends "array"
342
+ ? Array<InferSchema<SCHEMA>>
343
+ : InferSchema<SCHEMA>,
344
+ > = GenerateObjectArgs<SCHEMA, OUTPUT, RESULT> &
298
345
  Pick<
299
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
300
- Parameters<typeof generateObject<any>>[0],
301
- "experimental_repairText" | "abortSignal"
346
+ Parameters<typeof streamObject<SCHEMA, OUTPUT, RESULT>>[0],
347
+ "onError" | "onFinish" | "_internal"
302
348
  >;
303
349
 
304
- export type OurStreamObjectArgs<T extends Schema | z.Schema> =
305
- StreamObjectArgs<T> &
306
- Pick<
307
- Parameters<typeof streamObject<T>>[0],
308
- "onError" | "onFinish" | "abortSignal"
309
- >;
350
+ export type MaybeCustomCtx<
351
+ CustomCtx,
352
+ DataModel extends GenericDataModel,
353
+ AgentTools extends ToolSet,
354
+ > =
355
+ CustomCtx extends Record<string, unknown>
356
+ ? {
357
+ /**
358
+ * If you have a custom ctx that you use with the Agent
359
+ * (e.g. new Agent<{ orgId: string }>(...))
360
+ * you need to provide this function to add any extra fields.
361
+ * e.g.
362
+ * ```ts
363
+ * const myAgent = new Agent<{ orgId: string }>(...);
364
+ * const myAction = myAgent.asTextAction({
365
+ * customCtx: (ctx: ActionCtx, target, llmArgs) => {
366
+ * const orgId = await lookupOrgId(ctx, target.threadId);
367
+ * return { orgId };
368
+ * },
369
+ * });
370
+ * ```
371
+ * Then, in your tools, you can
372
+ */
373
+ customCtx: (
374
+ ctx: GenericActionCtx<DataModel>,
375
+ target: {
376
+ userId?: string | undefined;
377
+ threadId?: string | undefined;
378
+ },
379
+ llmArgs: TextArgs<AgentTools>,
380
+ ) => CustomCtx;
381
+ }
382
+ : { customCtx?: never };
310
383
 
311
384
  type ThreadOutputMetadata = Required<GenerationOutputMetadata>;
312
385
 
@@ -344,7 +417,7 @@ export interface Thread<DefaultTools extends ToolSet> {
344
417
  OUTPUT = never,
345
418
  OUTPUT_PARTIAL = never,
346
419
  >(
347
- args: TextArgs<
420
+ generateTextArgs: TextArgs<
348
421
  TOOLS extends undefined ? DefaultTools : TOOLS,
349
422
  TOOLS,
350
423
  OUTPUT,
@@ -371,7 +444,7 @@ export interface Thread<DefaultTools extends ToolSet> {
371
444
  OUTPUT = never,
372
445
  PARTIAL_OUTPUT = never,
373
446
  >(
374
- args: StreamingTextArgs<
447
+ streamTextArgs: StreamingTextArgs<
375
448
  TOOLS extends undefined ? DefaultTools : TOOLS,
376
449
  TOOLS,
377
450
  OUTPUT,
@@ -407,10 +480,35 @@ export interface Thread<DefaultTools extends ToolSet> {
407
480
  * for the {@link ContextOptions} and {@link StorageOptions}.
408
481
  * @returns The result of the generateObject function.
409
482
  */
410
- generateObject<T = JSONValue>(
411
- args: OurObjectArgs<T>,
483
+ generateObject<
484
+ SCHEMA extends ObjectSchema = DefaultObjectSchema,
485
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
486
+ ? "enum"
487
+ : "object",
488
+ RESULT = OUTPUT extends "array"
489
+ ? Array<InferSchema<SCHEMA>>
490
+ : InferSchema<SCHEMA>,
491
+ >(
492
+ generateObjectArgs: GenerateObjectArgs<SCHEMA, OUTPUT, RESULT> & {
493
+ /**
494
+ * If provided, this message will be used as the "prompt" for the LLM call,
495
+ * instead of the prompt or messages.
496
+ * This is useful if you want to first save a user message, then use it as
497
+ * the prompt for the LLM call in another call.
498
+ */
499
+ promptMessageId?: string;
500
+ /**
501
+ * The model to use for the LLM calls. This will override the model specified
502
+ * in the Agent constructor.
503
+ */
504
+ model?: LanguageModel;
505
+ /**
506
+ * The tools to use for the tool calls. This will override tools specified
507
+ * in the Agent constructor or createThread / continueThread.
508
+ */
509
+ },
412
510
  options?: Options,
413
- ): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
511
+ ): Promise<GenerateObjectResult<RESULT> & ThreadOutputMetadata>;
414
512
  /**
415
513
  * This behaves like {@link streamObject} from the "ai" package except that
416
514
  * it add context based on the userId and threadId and saves the input and
@@ -421,11 +519,40 @@ export interface Thread<DefaultTools extends ToolSet> {
421
519
  * for the {@link ContextOptions} and {@link StorageOptions}.
422
520
  * @returns The result of the streamObject function.
423
521
  */
424
- streamObject<T extends z.Schema | Schema>(
425
- args: OurStreamObjectArgs<T>,
522
+ streamObject<
523
+ SCHEMA extends ObjectSchema = DefaultObjectSchema,
524
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
525
+ ? "enum"
526
+ : "object",
527
+ RESULT = OUTPUT extends "array"
528
+ ? Array<InferSchema<SCHEMA>>
529
+ : InferSchema<SCHEMA>,
530
+ >(
531
+ /**
532
+ * The same arguments you'd pass to "ai" sdk {@link streamObject}.
533
+ */
534
+ streamObjectArgs: StreamObjectArgs<SCHEMA, OUTPUT, RESULT> & {
535
+ /**
536
+ * If provided, this message will be used as the "prompt" for the LLM call,
537
+ * instead of the prompt or messages.
538
+ * This is useful if you want to first save a user message, then use it as
539
+ * the prompt for the LLM call in another call.
540
+ */
541
+ promptMessageId?: string;
542
+ /**
543
+ * The model to use for the LLM calls. This will override the model specified
544
+ * in the Agent constructor.
545
+ */
546
+ model?: LanguageModel;
547
+ /**
548
+ * The tools to use for the tool calls. This will override tools specified
549
+ * in the Agent constructor or createThread / continueThread.
550
+ */
551
+ },
426
552
  options?: Options,
427
553
  ): Promise<
428
- StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata
554
+ ReturnType<typeof streamObject<SCHEMA, OUTPUT, RESULT>> &
555
+ ThreadOutputMetadata
429
556
  >;
430
557
  }
431
558
 
@@ -438,6 +565,11 @@ export type Options = {
438
565
  * The storage options to use for saving the input and output messages to the thread.
439
566
  */
440
567
  storageOptions?: StorageOptions;
568
+ /**
569
+ * The usage handler to use for this thread. Overrides any handler
570
+ * set in the agent constructor.
571
+ */
572
+ usageHandler?: UsageHandler;
441
573
  };
442
574
 
443
575
  export type SyncStreamsReturnValue =
@@ -469,9 +601,7 @@ export type ActionCtx = RunActionCtx & {
469
601
  auth: Auth;
470
602
  storage: StorageActionWriter;
471
603
  };
472
- export type QueryCtx = RunQueryCtx & {
473
- storage: StorageReader;
474
- };
604
+ export type QueryCtx = RunQueryCtx & { storage: StorageReader };
475
605
 
476
606
  export type OpaqueIds<T> =
477
607
  T extends GenericId<infer _T>