@convex-dev/agent 0.1.18-alpha.0 → 0.5.0-alpha.1

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 (71) hide show
  1. package/dist/client/createTool.d.ts +30 -9
  2. package/dist/client/createTool.d.ts.map +1 -1
  3. package/dist/client/createTool.js +24 -9
  4. package/dist/client/createTool.js.map +1 -1
  5. package/dist/client/definePlaygroundAPI.d.ts +176 -201
  6. package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
  7. package/dist/client/definePlaygroundAPI.js +12 -19
  8. package/dist/client/definePlaygroundAPI.js.map +1 -1
  9. package/dist/client/files.js +4 -4
  10. package/dist/client/files.js.map +1 -1
  11. package/dist/client/index.d.ts +166 -63
  12. package/dist/client/index.d.ts.map +1 -1
  13. package/dist/client/index.js +75 -45
  14. package/dist/client/index.js.map +1 -1
  15. package/dist/client/search.d.ts +27 -6
  16. package/dist/client/search.d.ts.map +1 -1
  17. package/dist/client/search.js.map +1 -1
  18. package/dist/client/streaming.d.ts +3 -2
  19. package/dist/client/streaming.d.ts.map +1 -1
  20. package/dist/client/streaming.js.map +1 -1
  21. package/dist/client/types.d.ts +27 -41
  22. package/dist/client/types.d.ts.map +1 -1
  23. package/dist/component/_generated/api.d.ts +436 -73
  24. package/dist/component/messages.d.ts +246 -43
  25. package/dist/component/messages.d.ts.map +1 -1
  26. package/dist/component/messages.js +2 -2
  27. package/dist/component/messages.js.map +1 -1
  28. package/dist/component/schema.d.ts +1453 -152
  29. package/dist/component/schema.d.ts.map +1 -1
  30. package/dist/component/schema.js +2 -2
  31. package/dist/component/schema.js.map +1 -1
  32. package/dist/component/streams.d.ts +180 -6
  33. package/dist/component/streams.d.ts.map +1 -1
  34. package/dist/mapping.d.ts +12 -14
  35. package/dist/mapping.d.ts.map +1 -1
  36. package/dist/mapping.js +187 -47
  37. package/dist/mapping.js.map +1 -1
  38. package/dist/react/deltas.d.ts +0 -3
  39. package/dist/react/deltas.d.ts.map +1 -1
  40. package/dist/react/deltas.js +140 -44
  41. package/dist/react/deltas.js.map +1 -1
  42. package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
  43. package/dist/react/optimisticallySendMessage.js +2 -1
  44. package/dist/react/optimisticallySendMessage.js.map +1 -1
  45. package/dist/react/toUIMessages.d.ts +5 -4
  46. package/dist/react/toUIMessages.d.ts.map +1 -1
  47. package/dist/react/toUIMessages.js +103 -40
  48. package/dist/react/toUIMessages.js.map +1 -1
  49. package/dist/validators.d.ts +1798 -259
  50. package/dist/validators.d.ts.map +1 -1
  51. package/dist/validators.js +79 -15
  52. package/dist/validators.js.map +1 -1
  53. package/package.json +2 -2
  54. package/src/client/createTool.ts +68 -37
  55. package/src/client/definePlaygroundAPI.ts +25 -27
  56. package/src/client/files.ts +4 -4
  57. package/src/client/index.test.ts +14 -12
  58. package/src/client/index.ts +134 -94
  59. package/src/client/search.ts +3 -2
  60. package/src/client/streaming.ts +4 -3
  61. package/src/client/types.ts +34 -70
  62. package/src/component/_generated/api.d.ts +436 -73
  63. package/src/component/messages.ts +2 -2
  64. package/src/component/schema.ts +2 -2
  65. package/src/mapping.ts +228 -75
  66. package/src/react/deltas.ts +165 -52
  67. package/src/react/optimisticallySendMessage.ts +4 -1
  68. package/src/react/toUIMessages.test.ts +154 -36
  69. package/src/react/toUIMessages.ts +136 -57
  70. package/src/validators.test.ts +2 -99
  71. package/src/validators.ts +95 -17
@@ -9,7 +9,7 @@ import type {
9
9
  StreamArgs,
10
10
  StreamDelta,
11
11
  StreamMessage,
12
- TextStreamPart,
12
+ vTextStreamPartV5,
13
13
  } from "../validators.js";
14
14
  import type { MessageDoc } from "../component/schema.js";
15
15
  import type {
@@ -20,6 +20,7 @@ import type {
20
20
  SyncStreamsReturnValue,
21
21
  } from "./types.js";
22
22
  import { omit } from "convex-helpers";
23
+ import type { Infer } from "convex/values";
23
24
 
24
25
  /**
25
26
  * A function that handles fetching stream deltas, used with the React hooks
@@ -158,7 +159,7 @@ export function mergeTransforms<TOOLS extends ToolSet>(
158
159
  export class DeltaStreamer {
159
160
  public streamId: string | undefined;
160
161
  public readonly options: Required<StreamingOptions>;
161
- #nextParts: TextStreamPart[] = [];
162
+ #nextParts: Infer<typeof vTextStreamPartV5>[] = [];
162
163
  #nextOrder: number;
163
164
  #nextStepOrder: number;
164
165
  #latestWrite: number = 0;
@@ -205,7 +206,7 @@ export class DeltaStreamer {
205
206
  });
206
207
  }
207
208
  }
208
- public async addParts(parts: TextStreamPart[]) {
209
+ public async addParts(parts: Infer<typeof vTextStreamPartV5>[]) {
209
210
  if (this.abortController.signal.aborted) {
210
211
  return;
211
212
  }
@@ -1,5 +1,6 @@
1
+ import type { LanguageModelV2 } from "@ai-sdk/provider";
1
2
  import type {
2
- CoreMessage,
3
+ ModelMessage,
3
4
  DeepPartial,
4
5
  generateObject,
5
6
  GenerateObjectResult,
@@ -8,7 +9,6 @@ import type {
8
9
  JSONValue,
9
10
  LanguageModelRequestMetadata,
10
11
  LanguageModelResponseMetadata,
11
- LanguageModelV1,
12
12
  RepairTextFunction,
13
13
  streamObject,
14
14
  StreamObjectResult,
@@ -17,6 +17,8 @@ import type {
17
17
  TelemetrySettings,
18
18
  ToolChoice,
19
19
  ToolSet,
20
+ Schema,
21
+ LanguageModelUsage,
20
22
  } from "ai";
21
23
  import type {
22
24
  Auth,
@@ -31,7 +33,7 @@ import type {
31
33
  GenericDataModel,
32
34
  } from "convex/server";
33
35
  import type { GenericId } from "convex/values";
34
- import type { Schema } from "zod";
36
+ import type { z } from "zod";
35
37
  import type { Mounts } from "../component/_generated/api.js";
36
38
  import type { ThreadDoc } from "../component/schema.js";
37
39
  import type {
@@ -40,7 +42,6 @@ import type {
40
42
  ProviderOptions,
41
43
  StreamDelta,
42
44
  StreamMessage,
43
- Usage,
44
45
  } from "../validators.js";
45
46
  import type { StreamingOptions } from "./streaming.js";
46
47
 
@@ -122,7 +123,7 @@ export type UsageHandler = (
122
123
  userId: string | undefined;
123
124
  threadId: string | undefined;
124
125
  agentName: string | undefined;
125
- usage: Usage;
126
+ usage: LanguageModelUsage;
126
127
  // Often has more information, like cached token usage in the case of openai.
127
128
  providerMetadata: ProviderMetadata | undefined;
128
129
  model: string;
@@ -169,7 +170,7 @@ export type TextArgs<
169
170
  * The model to use for the LLM calls. This will override the model specified
170
171
  * in the Agent constructor.
171
172
  */
172
- model?: LanguageModelV1;
173
+ model?: LanguageModelV2;
173
174
  /**
174
175
  * The tools to use for the tool calls. This will override tools specified
175
176
  * in the Agent constructor or createThread / continueThread.
@@ -208,7 +209,7 @@ export type StreamingTextArgs<
208
209
  * The model to use for the tool calls. This will override the model specified
209
210
  * in the Agent constructor.
210
211
  */
211
- model?: LanguageModelV1;
212
+ model?: LanguageModelV2;
212
213
  /**
213
214
  * The tools to use for the tool calls. This will override tools specified
214
215
  * in the Agent constructor or createThread / continueThread.
@@ -226,7 +227,7 @@ type BaseGenerateObjectOptions = CallSettings & {
226
227
  * The model to use for the object generation. This will override the model
227
228
  * specified in the Agent constructor.
228
229
  */
229
- model?: LanguageModelV1;
230
+ model?: LanguageModelV2;
230
231
  /**
231
232
  * The system prompt to use for the object generation. This will override the
232
233
  * system prompt specified in the Agent constructor.
@@ -236,13 +237,13 @@ type BaseGenerateObjectOptions = CallSettings & {
236
237
  * The prompt to the LLM to use for the object generation.
237
238
  * Specify this or messages, but not both.
238
239
  */
239
- prompt?: string;
240
+ prompt?: string | Array<ModelMessage>;
240
241
  /**
241
242
  * The messages to use for the object generation.
242
243
  * Note: recent messages are automatically added based on the thread it's
243
244
  * associated with and your contextOptions.
244
245
  */
245
- messages?: CoreMessage[];
246
+ messages?: Array<ModelMessage>;
246
247
  /**
247
248
  * The message to use as the "prompt" for the object generation.
248
249
  * If this is provided, it will be used instead of the prompt or messages.
@@ -256,52 +257,28 @@ type BaseGenerateObjectOptions = CallSettings & {
256
257
  experimental_providerMetadata?: ProviderMetadata;
257
258
  };
258
259
 
259
- type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
260
- BaseGenerateObjectOptions & {
261
- output?: "object";
262
- mode?: "auto" | "json" | "tool";
263
- schema: Schema<T>;
264
- schemaName?: string;
265
- schemaDescription?: string;
266
- };
267
-
268
- type GenerateObjectArrayOptions<T> = BaseGenerateObjectOptions & {
269
- output: "array";
270
- mode?: "auto" | "json" | "tool";
271
- schema: Schema<T>;
260
+ type StandardGenerateObjectOptions<T> = {
261
+ schema: z.Schema<T>;
272
262
  schemaName?: string;
273
263
  schemaDescription?: string;
274
- };
275
-
276
- type GenerateObjectWithEnumOptions<T extends string> =
277
- BaseGenerateObjectOptions & {
278
- output: "enum";
279
- enum: Array<T>;
280
- mode?: "auto" | "json" | "tool";
281
- };
282
-
283
- type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
284
- schema?: undefined;
285
- mode?: "json";
264
+ output?: "object" | "array";
265
+ mode?: "auto" | "json" | "tool";
286
266
  };
287
267
 
288
268
  // TODO: simplify this to just use the generateObject args, with an optional
289
269
  // model and tool/toolChoice types
290
- type GenerateObjectArgs<T> =
291
- T extends Record<string, unknown>
292
- ? GenerateObjectObjectOptions<T>
293
- : T extends Array<unknown>
294
- ? GenerateObjectArrayOptions<T>
295
- : T extends string
296
- ? GenerateObjectWithEnumOptions<T>
297
- : GenerateObjectNoSchemaOptions;
270
+ type GenerateObjectArgs<T> = BaseGenerateObjectOptions &
271
+ (
272
+ | StandardGenerateObjectOptions<T>
273
+ | { output: "enum"; enum: Array<T>; mode?: "auto" | "json" | "tool" }
274
+ | { output: "any"; schema: undefined; mode: "json" }
275
+ );
298
276
 
299
- type StreamObjectArgs<T> =
300
- T extends Record<string, unknown>
301
- ? GenerateObjectObjectOptions<T>
302
- : T extends Array<unknown>
303
- ? GenerateObjectArrayOptions<T>
304
- : GenerateObjectNoSchemaOptions;
277
+ type StreamObjectArgs<T> = BaseGenerateObjectOptions &
278
+ (
279
+ | StandardGenerateObjectOptions<T>
280
+ | { output: "any"; schema: undefined; mode: "json" }
281
+ );
305
282
 
306
283
  export type OurObjectArgs<T> = GenerateObjectArgs<T> &
307
284
  Pick<
@@ -310,11 +287,12 @@ export type OurObjectArgs<T> = GenerateObjectArgs<T> &
310
287
  "experimental_repairText" | "abortSignal"
311
288
  >;
312
289
 
313
- export type OurStreamObjectArgs<T> = StreamObjectArgs<T> &
314
- Pick<
315
- Parameters<typeof streamObject<T>>[0],
316
- "onError" | "onFinish" | "abortSignal"
317
- >;
290
+ export type OurStreamObjectArgs<T extends Schema | z.Schema> =
291
+ StreamObjectArgs<T> &
292
+ Pick<
293
+ Parameters<typeof streamObject<T>>[0],
294
+ "onError" | "onFinish" | "abortSignal"
295
+ >;
318
296
 
319
297
  type ThreadOutputMetadata = Required<GenerationOutputMetadata>;
320
298
 
@@ -415,24 +393,10 @@ export interface Thread<DefaultTools extends ToolSet> {
415
393
  * for the {@link ContextOptions} and {@link StorageOptions}.
416
394
  * @returns The result of the generateObject function.
417
395
  */
418
- generateObject<T>(
396
+ generateObject<T = JSONValue>(
419
397
  args: OurObjectArgs<T>,
420
398
  options?: Options,
421
399
  ): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
422
- /**
423
- * This behaves like {@link generateObject} from the "ai" package except that
424
- * it add context based on the userId and threadId and saves the input and
425
- * resulting messages to the thread, if specified. This overload is for when there's no schema.
426
- * Use {@link continueThread} to get a version of this function already scoped
427
- * to a thread (and optionally userId).
428
- * @param args The arguments to the generateObject function, along with extra controls
429
- * for the {@link ContextOptions} and {@link StorageOptions}.
430
- * @returns The result of the generateObject function.
431
- */
432
- generateObject(
433
- args: GenerateObjectNoSchemaOptions,
434
- options?: Options,
435
- ): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
436
400
  /**
437
401
  * This behaves like {@link streamObject} from the "ai" package except that
438
402
  * it add context based on the userId and threadId and saves the input and
@@ -443,7 +407,7 @@ export interface Thread<DefaultTools extends ToolSet> {
443
407
  * for the {@link ContextOptions} and {@link StorageOptions}.
444
408
  * @returns The result of the streamObject function.
445
409
  */
446
- streamObject<T>(
410
+ streamObject<T extends z.Schema | Schema>(
447
411
  args: OurStreamObjectArgs<T>,
448
412
  options?: Options,
449
413
  ): Promise<