@convex-dev/agent 0.2.11-alpha.2 → 0.2.11-alpha.4

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 (44) hide show
  1. package/README.md +2 -2
  2. package/dist/UIMessages.d.ts +1 -0
  3. package/dist/UIMessages.d.ts.map +1 -1
  4. package/dist/UIMessages.js +51 -0
  5. package/dist/UIMessages.js.map +1 -1
  6. package/dist/client/definePlaygroundAPI.d.ts +20 -20
  7. package/dist/client/index.d.ts +28 -107
  8. package/dist/client/index.d.ts.map +1 -1
  9. package/dist/client/index.js +17 -25
  10. package/dist/client/index.js.map +1 -1
  11. package/dist/client/threads.d.ts +4 -4
  12. package/dist/client/types.d.ts +61 -169
  13. package/dist/client/types.d.ts.map +1 -1
  14. package/dist/component/files.d.ts +4 -4
  15. package/dist/component/messages.d.ts +9 -9
  16. package/dist/component/messages.d.ts.map +1 -1
  17. package/dist/component/messages.js.map +1 -1
  18. package/dist/component/streams.d.ts +2 -6
  19. package/dist/component/streams.d.ts.map +1 -1
  20. package/dist/component/streams.js.map +1 -1
  21. package/dist/component/threads.d.ts +4 -4
  22. package/dist/component/users.d.ts +4 -4
  23. package/dist/deltas.d.ts +0 -1
  24. package/dist/deltas.d.ts.map +1 -1
  25. package/dist/deltas.js +0 -51
  26. package/dist/deltas.js.map +1 -1
  27. package/dist/react/useUIMessages.d.ts.map +1 -1
  28. package/dist/react/useUIMessages.js +1 -2
  29. package/dist/react/useUIMessages.js.map +1 -1
  30. package/package.json +61 -20
  31. package/src/UIMessages.test.ts +273 -0
  32. package/src/UIMessages.ts +64 -0
  33. package/src/client/index.ts +47 -123
  34. package/src/client/types.ts +85 -192
  35. package/src/component/messages.ts +16 -2
  36. package/src/component/streams.ts +8 -1
  37. package/src/deltas.test.ts +0 -272
  38. package/src/deltas.ts +0 -63
  39. package/src/react/useUIMessages.ts +5 -2
  40. package/src/test.ts +17 -0
  41. package/dist/client/_generated/_ignore.d.ts +0 -1
  42. package/dist/client/_generated/_ignore.d.ts.map +0 -1
  43. package/dist/client/_generated/_ignore.js +0 -3
  44. package/dist/client/_generated/_ignore.js.map +0 -1
@@ -1,8 +1,8 @@
1
1
  import type {
2
+ FlexibleSchema,
2
3
  InferSchema,
3
4
  ModelMessage,
4
5
  ProviderOptions,
5
- Schema,
6
6
  } from "@ai-sdk/provider-utils";
7
7
  import type { JSONValue } from "@ai-sdk/provider";
8
8
  import type {
@@ -17,13 +17,9 @@ import type {
17
17
  streamObject,
18
18
  streamText,
19
19
  StreamTextResult,
20
- ToolChoice,
21
20
  ToolSet,
22
- RepairTextFunction,
23
- TelemetrySettings,
24
21
  CallSettings,
25
- Prompt,
26
- Experimental_DownloadFunction,
22
+ generateObject,
27
23
  } from "ai";
28
24
  import type {
29
25
  Auth,
@@ -38,7 +34,7 @@ import type {
38
34
  WithoutSystemFields,
39
35
  } from "convex/server";
40
36
  import type { GenericId } from "convex/values";
41
- import type { Mounts } from "../component/_generated/api.js";
37
+ import type { api } from "../component/_generated/api.js";
42
38
  import type {
43
39
  MessageDoc,
44
40
  ProviderMetadata,
@@ -47,8 +43,48 @@ import type {
47
43
  ThreadDoc,
48
44
  } from "../validators.js";
49
45
  import type { StreamingOptions } from "./streaming.js";
50
- import type * as z3 from "zod/v3";
51
- import type * as z4 from "zod/v4";
46
+
47
+ export type AgentPrompt = {
48
+ /**
49
+ * System message to include in the prompt. Overwrites Agent instructions.
50
+ */
51
+ system?: string;
52
+ /**
53
+ * A prompt. It can be either a text prompt or a list of messages.
54
+ * If used with `promptMessageId`, it will be used in place of that
55
+ * prompt message and no input messages will be saved.
56
+ * Otherwise, if used with the storageOptions "promptAndOutput" (default),
57
+ * it will be the only message saved.
58
+ * If a string is provided, it will be a user message.
59
+ */
60
+ prompt?: string | Array<ModelMessage> | undefined;
61
+ /**
62
+ * A list of messages to use as context before the prompt.
63
+ * If used with `prompt`, these will precede the prompt.
64
+ * If used with the storageOptions "promptAndOutput" (default),
65
+ * none of these messages will be saved.
66
+ */
67
+ messages?: Array<ModelMessage> | undefined;
68
+ /**
69
+ * If provided, it uses this existing message to anchor the prompt:
70
+ * - The specified message will be included, unless `prompt` is also
71
+ * provided, in which case that will be inserted in place of this
72
+ * specified message.
73
+ * - Recent and search messages will not include messages after this
74
+ * message's order.
75
+ * - If there are already responses on the same order,
76
+ * for example, tool calls and responses,
77
+ * those will be included automatically.
78
+ *
79
+ * Note: if this is provided, no input messages will be saved by default.
80
+ */
81
+ promptMessageId?: string | undefined;
82
+ /**
83
+ * The model to use for the LLM calls. This will override the languageModel
84
+ * specified in the Agent config.
85
+ */
86
+ model?: LanguageModel;
87
+ };
52
88
 
53
89
  export type Config = {
54
90
  /**
@@ -295,7 +331,7 @@ export type RawRequestResponseHandler = (
295
331
  },
296
332
  ) => void | Promise<void>;
297
333
 
298
- export type AgentComponent = UseApi<Mounts>;
334
+ export type AgentComponent = UseApi<typeof api>;
299
335
 
300
336
  export type TextArgs<
301
337
  AgentTools extends ToolSet,
@@ -310,31 +346,14 @@ export type TextArgs<
310
346
  OUTPUT_PARTIAL
311
347
  >
312
348
  >[0],
313
- "toolChoice" | "tools" | "model"
349
+ "model" | "prompt" | "messages"
314
350
  > & {
315
- /**
316
- * If provided, this message will be used as the "prompt" for the LLM call,
317
- * instead of the prompt or messages.
318
- * This is useful if you want to first save a user message, then use it as
319
- * the prompt for the LLM call in another call.
320
- */
321
- promptMessageId?: string;
322
- /**
323
- * The model to use for the LLM calls. This will override the model specified
324
- * in the Agent constructor.
325
- */
326
- model?: LanguageModel;
327
351
  /**
328
352
  * The tools to use for the tool calls. This will override tools specified
329
353
  * in the Agent constructor or createThread / continueThread.
330
354
  */
331
355
  tools?: TOOLS;
332
- /**
333
- * The tool choice to use for the tool calls. This must be one of the tools
334
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
335
- */
336
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
337
- };
356
+ } & AgentPrompt;
338
357
 
339
358
  export type StreamingTextArgs<
340
359
  AgentTools extends ToolSet,
@@ -349,147 +368,50 @@ export type StreamingTextArgs<
349
368
  OUTPUT_PARTIAL
350
369
  >
351
370
  >[0],
352
- "toolChoice" | "tools" | "model"
371
+ "model" | "prompt" | "messages"
353
372
  > & {
354
- /**
355
- * If provided, this message will be used as the "prompt" for the LLM call,
356
- * instead of the prompt or messages.
357
- * This is useful if you want to first save a user message, then use it as
358
- * the prompt for the LLM call in another call.
359
- */
360
- promptMessageId?: string;
361
- /**
362
- * The model to use for the tool calls. This will override the model specified
363
- * in the Agent constructor.
364
- */
365
- model?: LanguageModel;
366
373
  /**
367
374
  * The tools to use for the tool calls. This will override tools specified
368
375
  * in the Agent constructor or createThread / continueThread.
369
376
  */
370
377
  tools?: TOOLS;
371
- /**
372
- * The tool choice to use for the tool calls. This must be one of the tools
373
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
374
- */
375
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
376
- };
378
+ } & AgentPrompt;
377
379
 
378
380
  export type ObjectMode = "object" | "array" | "enum" | "no-schema";
379
- export type ObjectSchema = z3.Schema | z4.core.$ZodType | Schema;
380
- export type DefaultObjectSchema = z4.core.$ZodType<JSONValue>;
381
381
 
382
- /**
383
- * Due to some issues with the type inference of the ai sdk, we need to
384
- * manually type the arguments for the generateObject function.
385
- * This is a workaround to allow the model to be optional.
386
- */
387
382
  export type GenerateObjectArgs<
388
- SCHEMA extends ObjectSchema = DefaultObjectSchema,
383
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
389
384
  OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
390
385
  ? "enum"
391
386
  : "object",
392
387
  RESULT = OUTPUT extends "array"
393
388
  ? Array<InferSchema<SCHEMA>>
394
389
  : InferSchema<SCHEMA>,
395
- > = Omit<CallSettings, "stopSequences"> &
396
- Partial<Prompt> & {
397
- /**
398
- * If provided, this message will be used as the "prompt" for the LLM call,
399
- * instead of the prompt or messages.
400
- * This is useful if you want to first save a user message, then use it as
401
- * the prompt for the LLM call in another call.
402
- */
403
- promptMessageId?: string;
404
- } & (OUTPUT extends "enum"
405
- ? {
406
- /**
407
- The enum values that the model should use.
408
- */
409
- enum: Array<RESULT>;
410
- mode?: "json";
411
- output: "enum";
412
- }
413
- : OUTPUT extends "no-schema"
414
- ? // eslint-disable-next-line @typescript-eslint/no-empty-object-type
415
- {}
416
- : {
417
- /**
418
- The schema of the object that the model should generate.
419
- */
420
- schema: SCHEMA;
421
- /**
422
- Optional name of the output that should be generated.
423
- Used by some providers for additional LLM guidance, e.g.
424
- via tool or schema name.
425
- */
426
- schemaName?: string;
427
- /**
428
- Optional description of the output that should be generated.
429
- Used by some providers for additional LLM guidance, e.g.
430
- via tool or schema description.
431
- */
432
- schemaDescription?: string;
433
- /**
434
- The mode to use for object generation.
435
-
436
- The schema is converted into a JSON schema and used in one of the following ways
437
-
438
- - 'auto': The provider will choose the best mode for the model.
439
- - 'tool': A tool with the JSON schema as parameters is provided and the provider is instructed to use it.
440
- - '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.
441
-
442
- Please note that most providers do not support all modes.
443
-
444
- Default and recommended: 'auto' (best mode for the model).
445
- */
446
- mode?: "auto" | "json" | "tool";
447
- }) & {
448
- output?: OUTPUT;
449
- /**
450
- The language model to use.
451
- */
452
- model?: LanguageModel;
453
- /**
454
- A function that attempts to repair the raw output of the model
455
- to enable JSON parsing.
456
- */
457
- experimental_repairText?: RepairTextFunction;
458
- /**
459
- Optional telemetry configuration (experimental).
460
- */
461
- experimental_telemetry?: TelemetrySettings;
462
- /**
463
- Custom download function to use for URLs.
464
-
465
- By default, files are downloaded if the model does not support the URL for the given media type.
466
- */
467
- experimental_download?: Experimental_DownloadFunction | undefined;
468
- /**
469
- Additional provider-specific options. They are passed through
470
- to the provider from the AI SDK and enable provider-specific
471
- functionality that can be fully encapsulated in the provider.
472
- */
473
- providerOptions?: ProviderOptions;
474
- /**
475
- * Internal. For test use only. May change without notice.
476
- */
477
- _internal?: { generateId?: () => string; currentDate?: () => Date };
390
+ > = AgentPrompt &
391
+ Omit<
392
+ Parameters<typeof generateObject<SCHEMA, OUTPUT, RESULT>>[0],
393
+ "model" | "prompt" | "messages"
394
+ > & {
395
+ schema?: SCHEMA;
396
+ enum?: Array<RESULT>;
478
397
  };
479
398
 
480
399
  export type StreamObjectArgs<
481
- SCHEMA extends ObjectSchema = DefaultObjectSchema,
400
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
482
401
  OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
483
402
  ? "enum"
484
403
  : "object",
485
404
  RESULT = OUTPUT extends "array"
486
405
  ? Array<InferSchema<SCHEMA>>
487
406
  : InferSchema<SCHEMA>,
488
- > = GenerateObjectArgs<SCHEMA, OUTPUT, RESULT> &
489
- Pick<
407
+ > = AgentPrompt &
408
+ Omit<
490
409
  Parameters<typeof streamObject<SCHEMA, OUTPUT, RESULT>>[0],
491
- "onError" | "onFinish" | "_internal"
492
- >;
410
+ "model" | "prompt" | "messages"
411
+ > & {
412
+ schema?: SCHEMA;
413
+ enum?: Array<RESULT>;
414
+ };
493
415
 
494
416
  export type MaybeCustomCtx<
495
417
  CustomCtx,
@@ -561,20 +483,13 @@ export interface Thread<DefaultTools extends ToolSet> {
561
483
  OUTPUT = never,
562
484
  OUTPUT_PARTIAL = never,
563
485
  >(
564
- generateTextArgs: TextArgs<
565
- TOOLS extends undefined ? DefaultTools : TOOLS,
566
- TOOLS,
567
- OUTPUT,
568
- OUTPUT_PARTIAL
569
- > & {
570
- /**
571
- * If provided, this message will be used as the "prompt" for the LLM call,
572
- * instead of the prompt or messages.
573
- * This is useful if you want to first save a user message, then use it as
574
- * the prompt for the LLM call in another call.
575
- */
576
- promptMessageId?: string;
577
- },
486
+ generateTextArgs: AgentPrompt &
487
+ TextArgs<
488
+ TOOLS extends undefined ? DefaultTools : TOOLS,
489
+ TOOLS,
490
+ OUTPUT,
491
+ OUTPUT_PARTIAL
492
+ >,
578
493
  options?: Options,
579
494
  ): Promise<
580
495
  GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
@@ -596,20 +511,13 @@ export interface Thread<DefaultTools extends ToolSet> {
596
511
  OUTPUT = never,
597
512
  PARTIAL_OUTPUT = never,
598
513
  >(
599
- streamTextArgs: StreamingTextArgs<
600
- TOOLS extends undefined ? DefaultTools : TOOLS,
601
- TOOLS,
602
- OUTPUT,
603
- PARTIAL_OUTPUT
604
- > & {
605
- /**
606
- * If provided, this message will be used as the "prompt" for the LLM call,
607
- * instead of the prompt or messages.
608
- * This is useful if you want to first save a user message, then use it as
609
- * the prompt for the LLM call in another call.
610
- */
611
- promptMessageId?: string;
612
- },
514
+ streamTextArgs: AgentPrompt &
515
+ StreamingTextArgs<
516
+ TOOLS extends undefined ? DefaultTools : TOOLS,
517
+ TOOLS,
518
+ OUTPUT,
519
+ PARTIAL_OUTPUT
520
+ >,
613
521
  options?: Options & {
614
522
  /**
615
523
  * Whether to save incremental data (deltas) from streaming responses.
@@ -641,7 +549,7 @@ export interface Thread<DefaultTools extends ToolSet> {
641
549
  * @returns The result of the generateObject function.
642
550
  */
643
551
  generateObject<
644
- SCHEMA extends ObjectSchema = DefaultObjectSchema,
552
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
645
553
  OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
646
554
  ? "enum"
647
555
  : "object",
@@ -649,15 +557,8 @@ export interface Thread<DefaultTools extends ToolSet> {
649
557
  ? Array<InferSchema<SCHEMA>>
650
558
  : InferSchema<SCHEMA>,
651
559
  >(
652
- generateObjectArgs: GenerateObjectArgs<SCHEMA, OUTPUT, RESULT> & {
653
- /**
654
- * If provided, this message will be used as the "prompt" for the LLM call,
655
- * instead of the prompt or messages.
656
- * This is useful if you want to first save a user message, then use it as
657
- * the prompt for the LLM call in another call.
658
- */
659
- promptMessageId?: string;
660
- },
560
+ generateObjectArgs: AgentPrompt &
561
+ GenerateObjectArgs<SCHEMA, OUTPUT, RESULT>,
661
562
  options?: Options,
662
563
  ): Promise<GenerateObjectResult<RESULT> & ThreadOutputMetadata>;
663
564
  /**
@@ -671,7 +572,7 @@ export interface Thread<DefaultTools extends ToolSet> {
671
572
  * @returns The result of the streamObject function.
672
573
  */
673
574
  streamObject<
674
- SCHEMA extends ObjectSchema = DefaultObjectSchema,
575
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
675
576
  OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
676
577
  ? "enum"
677
578
  : "object",
@@ -682,15 +583,7 @@ export interface Thread<DefaultTools extends ToolSet> {
682
583
  /**
683
584
  * The same arguments you'd pass to "ai" sdk {@link streamObject}.
684
585
  */
685
- streamObjectArgs: StreamObjectArgs<SCHEMA, OUTPUT, RESULT> & {
686
- /**
687
- * If provided, this message will be used as the "prompt" for the LLM call,
688
- * instead of the prompt or messages.
689
- * This is useful if you want to first save a user message, then use it as
690
- * the prompt for the LLM call in another call.
691
- */
692
- promptMessageId?: string;
693
- },
586
+ streamObjectArgs: AgentPrompt & StreamObjectArgs<SCHEMA, OUTPUT, RESULT>,
694
587
  options?: Options,
695
588
  ): Promise<
696
589
  ReturnType<typeof streamObject<SCHEMA, OUTPUT, RESULT>> &
@@ -93,7 +93,14 @@ export const deleteByOrder = mutation({
93
93
  lastOrder: v.optional(v.number()),
94
94
  lastStepOrder: v.optional(v.number()),
95
95
  }),
96
- handler: async (ctx, args) => {
96
+ handler: async (
97
+ ctx,
98
+ args,
99
+ ): Promise<{
100
+ isDone: boolean;
101
+ lastOrder?: number;
102
+ lastStepOrder?: number;
103
+ }> => {
97
104
  const messages = await orderedMessagesStream(
98
105
  ctx,
99
106
  args.threadId,
@@ -770,7 +777,14 @@ export const getMessageSearchFields = query({
770
777
  embedding: v.optional(v.array(v.number())),
771
778
  embeddingModel: v.optional(v.string()),
772
779
  }),
773
- handler: async (ctx, args) => {
780
+ handler: async (
781
+ ctx,
782
+ args,
783
+ ): Promise<{
784
+ text?: string | undefined;
785
+ embedding?: number[] | undefined;
786
+ embeddingModel?: string | undefined;
787
+ }> => {
774
788
  const message = await ctx.db.get(args.messageId);
775
789
  const text = message?.text;
776
790
  let embedding = undefined;
@@ -414,7 +414,14 @@ export const deleteAllStreamsForThreadIdAsync = mutation({
414
414
  streamOrder: v.optional(v.number()),
415
415
  deltaCursor: v.optional(v.string()),
416
416
  }),
417
- handler: async (ctx, args) => {
417
+ handler: async (
418
+ ctx,
419
+ args,
420
+ ): Promise<{
421
+ isDone: boolean;
422
+ streamOrder?: number;
423
+ deltaCursor?: string;
424
+ }> => {
418
425
  const result = await deleteStreamsPageForThreadId(ctx, args);
419
426
  if (!result.isDone) {
420
427
  await ctx.scheduler.runAfter(