@convex-dev/agent 0.1.1 → 0.1.2-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.
Files changed (109) hide show
  1. package/README.md +9 -3
  2. package/dist/commonjs/client/createTool.d.ts +49 -0
  3. package/dist/commonjs/client/createTool.d.ts.map +1 -0
  4. package/dist/commonjs/client/createTool.js +45 -0
  5. package/dist/commonjs/client/createTool.js.map +1 -0
  6. package/dist/commonjs/client/index.d.ts +101 -322
  7. package/dist/commonjs/client/index.d.ts.map +1 -1
  8. package/dist/commonjs/client/index.js +142 -194
  9. package/dist/commonjs/client/index.js.map +1 -1
  10. package/dist/commonjs/client/streaming.d.ts +54 -0
  11. package/dist/commonjs/client/streaming.d.ts.map +1 -0
  12. package/dist/commonjs/client/streaming.js +121 -0
  13. package/dist/commonjs/client/streaming.js.map +1 -0
  14. package/dist/commonjs/client/types.d.ts +290 -2
  15. package/dist/commonjs/client/types.d.ts.map +1 -1
  16. package/dist/commonjs/component/messages.d.ts +104 -138
  17. package/dist/commonjs/component/messages.d.ts.map +1 -1
  18. package/dist/commonjs/component/messages.js +15 -14
  19. package/dist/commonjs/component/messages.js.map +1 -1
  20. package/dist/commonjs/component/schema.d.ts +873 -3
  21. package/dist/commonjs/component/schema.d.ts.map +1 -1
  22. package/dist/commonjs/component/schema.js +39 -1
  23. package/dist/commonjs/component/schema.js.map +1 -1
  24. package/dist/commonjs/component/streams.d.ts +191 -0
  25. package/dist/commonjs/component/streams.d.ts.map +1 -0
  26. package/dist/commonjs/component/streams.js +169 -0
  27. package/dist/commonjs/component/streams.js.map +1 -0
  28. package/dist/commonjs/component/threads.d.ts +1 -1
  29. package/dist/commonjs/component/threads.js +3 -3
  30. package/dist/commonjs/component/threads.js.map +1 -1
  31. package/dist/commonjs/component/users.js +2 -2
  32. package/dist/commonjs/component/users.js.map +1 -1
  33. package/dist/commonjs/mapping.d.ts.map +1 -1
  34. package/dist/commonjs/mapping.js +3 -1
  35. package/dist/commonjs/mapping.js.map +1 -1
  36. package/dist/commonjs/react/index.d.ts +99 -0
  37. package/dist/commonjs/react/index.d.ts.map +1 -1
  38. package/dist/commonjs/react/index.js +505 -1
  39. package/dist/commonjs/react/index.js.map +1 -1
  40. package/dist/commonjs/react/toUIMessages.d.ts +8 -0
  41. package/dist/commonjs/react/toUIMessages.d.ts.map +1 -0
  42. package/dist/commonjs/react/toUIMessages.js +134 -0
  43. package/dist/commonjs/react/toUIMessages.js.map +1 -0
  44. package/dist/commonjs/validators.d.ts +506 -4
  45. package/dist/commonjs/validators.d.ts.map +1 -1
  46. package/dist/commonjs/validators.js +49 -2
  47. package/dist/commonjs/validators.js.map +1 -1
  48. package/dist/esm/client/createTool.d.ts +49 -0
  49. package/dist/esm/client/createTool.d.ts.map +1 -0
  50. package/dist/esm/client/createTool.js +45 -0
  51. package/dist/esm/client/createTool.js.map +1 -0
  52. package/dist/esm/client/index.d.ts +101 -322
  53. package/dist/esm/client/index.d.ts.map +1 -1
  54. package/dist/esm/client/index.js +142 -194
  55. package/dist/esm/client/index.js.map +1 -1
  56. package/dist/esm/client/streaming.d.ts +54 -0
  57. package/dist/esm/client/streaming.d.ts.map +1 -0
  58. package/dist/esm/client/streaming.js +121 -0
  59. package/dist/esm/client/streaming.js.map +1 -0
  60. package/dist/esm/client/types.d.ts +290 -2
  61. package/dist/esm/client/types.d.ts.map +1 -1
  62. package/dist/esm/component/messages.d.ts +104 -138
  63. package/dist/esm/component/messages.d.ts.map +1 -1
  64. package/dist/esm/component/messages.js +15 -14
  65. package/dist/esm/component/messages.js.map +1 -1
  66. package/dist/esm/component/schema.d.ts +873 -3
  67. package/dist/esm/component/schema.d.ts.map +1 -1
  68. package/dist/esm/component/schema.js +39 -1
  69. package/dist/esm/component/schema.js.map +1 -1
  70. package/dist/esm/component/streams.d.ts +191 -0
  71. package/dist/esm/component/streams.d.ts.map +1 -0
  72. package/dist/esm/component/streams.js +169 -0
  73. package/dist/esm/component/streams.js.map +1 -0
  74. package/dist/esm/component/threads.d.ts +1 -1
  75. package/dist/esm/component/threads.js +3 -3
  76. package/dist/esm/component/threads.js.map +1 -1
  77. package/dist/esm/component/users.js +2 -2
  78. package/dist/esm/component/users.js.map +1 -1
  79. package/dist/esm/mapping.d.ts.map +1 -1
  80. package/dist/esm/mapping.js +3 -1
  81. package/dist/esm/mapping.js.map +1 -1
  82. package/dist/esm/react/index.d.ts +99 -0
  83. package/dist/esm/react/index.d.ts.map +1 -1
  84. package/dist/esm/react/index.js +505 -1
  85. package/dist/esm/react/index.js.map +1 -1
  86. package/dist/esm/react/toUIMessages.d.ts +8 -0
  87. package/dist/esm/react/toUIMessages.d.ts.map +1 -0
  88. package/dist/esm/react/toUIMessages.js +134 -0
  89. package/dist/esm/react/toUIMessages.js.map +1 -0
  90. package/dist/esm/validators.d.ts +506 -4
  91. package/dist/esm/validators.d.ts.map +1 -1
  92. package/dist/esm/validators.js +49 -2
  93. package/dist/esm/validators.js.map +1 -1
  94. package/package.json +5 -1
  95. package/src/client/createTool.ts +108 -0
  96. package/src/client/index.ts +270 -689
  97. package/src/client/streaming.ts +166 -0
  98. package/src/client/types.ts +430 -5
  99. package/src/component/_generated/api.d.ts +339 -188
  100. package/src/component/messages.ts +15 -14
  101. package/src/component/schema.ts +46 -0
  102. package/src/component/streams.ts +206 -0
  103. package/src/component/threads.ts +3 -3
  104. package/src/component/users.ts +2 -2
  105. package/src/mapping.ts +3 -1
  106. package/src/react/index.ts +687 -1
  107. package/src/react/toUIMessages.ts +153 -0
  108. package/src/validators.test.ts +18 -0
  109. package/src/validators.ts +72 -2
@@ -0,0 +1,166 @@
1
+ import { ChunkDetector, smoothStream, StreamTextTransform, ToolSet } from "ai";
2
+ import { ProviderOptions, StreamDelta, TextStreamPart } from "../validators";
3
+ import type { AgentComponent, MessageDoc } from "./index";
4
+ import { RunActionCtx } from "./types";
5
+
6
+ export type StreamingOptions = {
7
+ /**
8
+ * The minimum granularity of deltas to save.
9
+ * Note: this is not a guarantee that every delta will be exactly one line.
10
+ * E.g. if "line" is specified, it won't save any deltas until it encounters
11
+ * a newline character.
12
+ * Defaults to "word".
13
+ */
14
+ chunking?: "word" | "line" | RegExp | ChunkDetector;
15
+ /**
16
+ * The minimum number of milliseconds to wait between saving deltas.
17
+ * Defaults to 250.
18
+ */
19
+ throttleMs?: number;
20
+ };
21
+ export const DEFAULT_STREAMING_OPTIONS = {
22
+ chunking: "word",
23
+ throttleMs: 250,
24
+ } satisfies StreamingOptions;
25
+
26
+ export function mergeTransforms<TOOLS extends ToolSet>(
27
+ options: StreamingOptions | boolean | undefined,
28
+ existing:
29
+ | StreamTextTransform<TOOLS>
30
+ | Array<StreamTextTransform<TOOLS>>
31
+ | undefined
32
+ ) {
33
+ if (!options) {
34
+ return existing;
35
+ }
36
+ const chunking =
37
+ typeof options === "boolean"
38
+ ? DEFAULT_STREAMING_OPTIONS.chunking
39
+ : options.chunking;
40
+ const transforms = Array.isArray(existing)
41
+ ? existing
42
+ : existing
43
+ ? [existing]
44
+ : [];
45
+ transforms.push(smoothStream({ delayInMs: null, chunking }));
46
+ return transforms;
47
+ }
48
+
49
+ export class DeltaStreamer {
50
+ public streamId: string | undefined;
51
+ public readonly options: Required<StreamingOptions>;
52
+ #nextParts: TextStreamPart[] = [];
53
+ #nextOrder: number;
54
+ #nextStepOrder: number;
55
+ #latestWrite: number = 0;
56
+ #ongoingWrite: Promise<void> | undefined;
57
+ #cursor: number = 0;
58
+
59
+ constructor(
60
+ public readonly component: AgentComponent,
61
+ public readonly ctx: RunActionCtx,
62
+ options: true | StreamingOptions,
63
+ public readonly metadata: {
64
+ threadId: string;
65
+ agentName: string | undefined;
66
+ model: string | undefined;
67
+ provider: string | undefined;
68
+ providerOptions: ProviderOptions | undefined;
69
+ userId: string | undefined;
70
+ order: number | undefined;
71
+ stepOrder: number | undefined;
72
+ }
73
+ ) {
74
+ this.options =
75
+ typeof options === "boolean"
76
+ ? DEFAULT_STREAMING_OPTIONS
77
+ : {
78
+ ...DEFAULT_STREAMING_OPTIONS,
79
+ ...options,
80
+ };
81
+ this.metadata = metadata;
82
+ this.#nextParts = [];
83
+ this.#nextOrder = metadata.order ?? 0;
84
+ this.#nextStepOrder = (metadata.stepOrder ?? 0) + 1;
85
+ }
86
+ public async addParts(parts: TextStreamPart[]) {
87
+ if (!this.streamId) {
88
+ this.streamId = await this.ctx.runMutation(
89
+ this.component.streams.create,
90
+ {
91
+ ...this.metadata,
92
+ order: this.#nextOrder,
93
+ stepOrder: this.#nextStepOrder,
94
+ }
95
+ );
96
+ }
97
+ this.#nextParts.push(...parts);
98
+ if (
99
+ !this.#ongoingWrite &&
100
+ Date.now() - this.#latestWrite >= this.options.throttleMs
101
+ ) {
102
+ this.#ongoingWrite = this.#sendDelta();
103
+ }
104
+ }
105
+
106
+ async #sendDelta() {
107
+ const delta = this.#createDelta();
108
+ this.#latestWrite = Date.now();
109
+ await this.ctx.runMutation(this.component.streams.addDelta, delta);
110
+ // Now that we've sent the delta, check if we need to send another one.
111
+ if (
112
+ this.#nextParts.length > 0 &&
113
+ Date.now() - this.#latestWrite >= this.options.throttleMs
114
+ ) {
115
+ // We send again immediately with the accumulated deltas.
116
+ this.#ongoingWrite = this.#sendDelta();
117
+ console.debug("Sent delta immediately");
118
+ } else {
119
+ this.#ongoingWrite = undefined;
120
+ }
121
+ }
122
+
123
+ #createDelta(): StreamDelta {
124
+ const start = this.#cursor;
125
+ const end = start + this.#nextParts.length;
126
+ this.#cursor = end;
127
+ const parts = this.#nextParts;
128
+ this.#nextParts = [];
129
+ if (!this.streamId) {
130
+ throw new Error("Creating a delta before the stream is created");
131
+ }
132
+ return {
133
+ streamId: this.streamId,
134
+ start,
135
+ end,
136
+ parts,
137
+ };
138
+ }
139
+
140
+ public async finish(messages: MessageDoc[]) {
141
+ if (this.#ongoingWrite) {
142
+ await this.#ongoingWrite;
143
+ this.#ongoingWrite = undefined;
144
+ }
145
+ if (!this.streamId) {
146
+ throw new Error("Finish called before stream is created");
147
+ }
148
+ const lastMessage = messages.at(-1);
149
+ if (lastMessage) {
150
+ this.#nextOrder = lastMessage.order;
151
+ this.#nextStepOrder = lastMessage.stepOrder + 1;
152
+ } else {
153
+ console.warn("Step finished without generating a message");
154
+ }
155
+ const finalDelta =
156
+ this.#nextParts.length > 0 ? this.#createDelta() : undefined;
157
+ this.#nextParts = [];
158
+ const streamId = this.streamId;
159
+ this.streamId = undefined;
160
+ this.#cursor = 0;
161
+ await this.ctx.runMutation(this.component.streams.finish, {
162
+ streamId,
163
+ finalDelta,
164
+ });
165
+ }
166
+ }
@@ -1,13 +1,438 @@
1
- import {
2
- GenericQueryCtx,
3
- GenericDataModel,
4
- GenericMutationCtx,
1
+ import type {
2
+ CoreMessage,
3
+ DeepPartial,
4
+ GenerateObjectResult,
5
+ GenerateTextResult,
6
+ JSONValue,
7
+ RepairTextFunction,
8
+ StreamObjectResult,
9
+ StreamTextResult,
10
+ TelemetrySettings,
11
+ ToolChoice,
12
+ ToolSet,
13
+ } from "ai";
14
+ import type {
15
+ generateObject,
16
+ generateText,
17
+ LanguageModelV1,
18
+ streamObject,
19
+ streamText,
20
+ } from "ai";
21
+ import type {
5
22
  Expand,
6
23
  FunctionReference,
7
24
  GenericActionCtx,
25
+ GenericDataModel,
26
+ GenericMutationCtx,
27
+ GenericQueryCtx,
8
28
  } from "convex/server";
9
- import { GenericId } from "convex/values";
29
+ import type { GenericId } from "convex/values";
30
+ import type {
31
+ CallSettings,
32
+ ProviderMetadata,
33
+ ProviderOptions,
34
+ StreamDelta,
35
+ StreamMessage,
36
+ Usage,
37
+ } from "../validators.js";
38
+ import type { Mounts } from "../component/_generated/api.js";
39
+ import type { Schema } from "zod";
40
+ import type { StreamingOptions } from "./streaming.js";
41
+
42
+ /**
43
+ * Options to configure what messages are fetched as context,
44
+ * automatically with thread.generateText, or directly via search.
45
+ */
46
+ export type ContextOptions = {
47
+ /** @deprecated Use excludeToolMessages instead. */
48
+ includeToolCalls?: boolean;
49
+ /**
50
+ * Whether to include tool messages in the context.
51
+ * By default, tool calls and results are not included.
52
+ */
53
+ excludeToolMessages?: boolean;
54
+ /**
55
+ * How many recent messages to include. These are added after the search
56
+ * messages, and do not count against the search limit.
57
+ * Default: 100
58
+ */
59
+ recentMessages?: number;
60
+ /**
61
+ * Options for searching messages.
62
+ */
63
+ searchOptions?: {
64
+ /**
65
+ * The maximum number of messages to fetch. Default is 10.
66
+ */
67
+ limit: number;
68
+ /**
69
+ * Whether to use text search to find messages. Default is false.
70
+ */
71
+ textSearch?: boolean;
72
+ /**
73
+ * Whether to use vector search to find messages. Default is false.
74
+ * At least one of textSearch or vectorSearch must be true.
75
+ */
76
+ vectorSearch?: boolean;
77
+ /**
78
+ * What messages around the search results to include.
79
+ * Default: { before: 2, after: 1 }
80
+ * (two before, and one after each message found in the search)
81
+ * Note, this is after the limit is applied.
82
+ * By default this will quadruple the number of messages fetched.
83
+ */
84
+ messageRange?: { before: number; after: number };
85
+ };
86
+ /**
87
+ * Whether to search across other threads for relevant messages.
88
+ * By default, only the current thread is searched.
89
+ */
90
+ searchOtherThreads?: boolean;
91
+ };
92
+
93
+ /**
94
+ * Options to configure the automatic saving of messages
95
+ * when generating text / objects in a thread.
96
+ */
97
+ export type StorageOptions = {
98
+ /**
99
+ * Defaults to false, allowing you to pass in arbitrary context that will
100
+ * be in addition to automatically fetched content.
101
+ * Pass true to have all input messages saved to the thread history.
102
+ */
103
+ saveAllInputMessages?: boolean;
104
+ /** Defaults to true, saving the prompt, or last message passed to generateText. */
105
+ saveAnyInputMessages?: boolean;
106
+ /** Defaults to true. Whether to save messages generated while chatting. */
107
+ saveOutputMessages?: boolean;
108
+ };
109
+
110
+ export type GenerationOutputMetadata = { messageId?: string };
111
+
112
+ export type UsageHandler = (
113
+ ctx: RunActionCtx,
114
+ args: {
115
+ userId: string | undefined;
116
+ threadId: string | undefined;
117
+ agentName: string | undefined;
118
+ usage: Usage;
119
+ // Often has more information, like cached token usage in the case of openai.
120
+ providerMetadata: ProviderMetadata | undefined;
121
+ model: string;
122
+ provider: string;
123
+ }
124
+ ) => void | Promise<void>;
125
+
126
+ export type AgentComponent = UseApi<Mounts>;
127
+
128
+ export type TextArgs<
129
+ AgentTools extends ToolSet,
130
+ TOOLS extends ToolSet | undefined = undefined,
131
+ OUTPUT = never,
132
+ OUTPUT_PARTIAL = never,
133
+ > = Omit<
134
+ Parameters<
135
+ typeof generateText<
136
+ TOOLS extends undefined ? AgentTools : TOOLS,
137
+ OUTPUT,
138
+ OUTPUT_PARTIAL
139
+ >
140
+ >[0],
141
+ "toolChoice" | "tools" | "model"
142
+ > & {
143
+ /**
144
+ * If provided, this message will be used as the "prompt" for the LLM call,
145
+ * instead of the prompt or messages.
146
+ * This is useful if you want to first save a user message, then use it as
147
+ * the prompt for the LLM call in another call.
148
+ */
149
+ promptMessageId?: string;
150
+ /**
151
+ * The model to use for the tool calls. This will override the model specified
152
+ * in the Agent constructor.
153
+ */
154
+ model?: LanguageModelV1;
155
+ /**
156
+ * The tools to use for the tool calls. This will override tools specified
157
+ * in the Agent constructor or createThread / continueThread.
158
+ */
159
+ tools?: TOOLS;
160
+ /**
161
+ * The tool choice to use for the tool calls. This must be one of the tools
162
+ * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
163
+ */
164
+ toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
165
+ };
166
+
167
+ export type StreamingTextArgs<
168
+ AgentTools extends ToolSet,
169
+ TOOLS extends ToolSet | undefined = undefined,
170
+ OUTPUT = never,
171
+ OUTPUT_PARTIAL = never,
172
+ > = Omit<
173
+ Parameters<
174
+ typeof streamText<
175
+ TOOLS extends undefined ? AgentTools : TOOLS,
176
+ OUTPUT,
177
+ OUTPUT_PARTIAL
178
+ >
179
+ >[0],
180
+ "toolChoice" | "tools" | "model"
181
+ > & {
182
+ /**
183
+ * The model to use for the tool calls. This will override the model specified
184
+ * in the Agent constructor.
185
+ */
186
+ model?: LanguageModelV1;
187
+ /**
188
+ * The tools to use for the tool calls. This will override tools specified
189
+ * in the Agent constructor or createThread / continueThread.
190
+ */
191
+ tools?: TOOLS;
192
+ /**
193
+ * The tool choice to use for the tool calls. This must be one of the tools
194
+ * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
195
+ */
196
+ toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
197
+ };
198
+
199
+ type BaseGenerateObjectOptions = CallSettings & {
200
+ /**
201
+ * The model to use for the object generation. This will override the model
202
+ * specified in the Agent constructor.
203
+ */
204
+ model?: LanguageModelV1;
205
+ /**
206
+ * The system prompt to use for the object generation. This will override the
207
+ * system prompt specified in the Agent constructor.
208
+ */
209
+ system?: string;
210
+ /**
211
+ * The prompt to the LLM to use for the object generation.
212
+ * Specify this or messages, but not both.
213
+ */
214
+ prompt?: string;
215
+ /**
216
+ * The messages to use for the object generation.
217
+ * Note: recent messages are automatically added based on the thread it's
218
+ * associated with and your contextOptions.
219
+ */
220
+ messages?: CoreMessage[];
221
+ /**
222
+ * The message to use as the "prompt" for the object generation.
223
+ * If this is provided, it will be used instead of the prompt or messages.
224
+ * This is useful if you want to first save a user message, then use it as
225
+ * the prompt for the object generation in another call.
226
+ */
227
+ promptMessageId?: string;
228
+ experimental_repairText?: RepairTextFunction;
229
+ experimental_telemetry?: TelemetrySettings;
230
+ providerOptions?: ProviderOptions;
231
+ experimental_providerMetadata?: ProviderMetadata;
232
+ };
233
+
234
+ type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
235
+ BaseGenerateObjectOptions & {
236
+ output?: "object";
237
+ mode?: "auto" | "json" | "tool";
238
+ schema: Schema<T>;
239
+ schemaName?: string;
240
+ schemaDescription?: string;
241
+ };
242
+
243
+ type GenerateObjectArrayOptions<T> = BaseGenerateObjectOptions & {
244
+ output: "array";
245
+ mode?: "auto" | "json" | "tool";
246
+ schema: Schema<T>;
247
+ schemaName?: string;
248
+ schemaDescription?: string;
249
+ };
250
+
251
+ type GenerateObjectWithEnumOptions<T extends string> =
252
+ BaseGenerateObjectOptions & {
253
+ output: "enum";
254
+ enum: Array<T>;
255
+ mode?: "auto" | "json" | "tool";
256
+ };
257
+
258
+ type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
259
+ schema?: undefined;
260
+ mode?: "json";
261
+ };
262
+
263
+ // TODO: simplify this to just use the generateObject args, with an optional
264
+ // model and tool/toolChoice types
265
+ type GenerateObjectArgs<T> =
266
+ T extends Record<string, unknown>
267
+ ? GenerateObjectObjectOptions<T>
268
+ : T extends Array<unknown>
269
+ ? GenerateObjectArrayOptions<T>
270
+ : T extends string
271
+ ? GenerateObjectWithEnumOptions<T>
272
+ : GenerateObjectNoSchemaOptions;
273
+
274
+ type StreamObjectArgs<T> =
275
+ T extends Record<string, unknown>
276
+ ? GenerateObjectObjectOptions<T>
277
+ : T extends Array<unknown>
278
+ ? GenerateObjectArrayOptions<T>
279
+ : GenerateObjectNoSchemaOptions;
280
+
281
+ export type OurObjectArgs<T> = GenerateObjectArgs<T> &
282
+ Pick<
283
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
284
+ Parameters<typeof generateObject<any>>[0],
285
+ "experimental_repairText" | "abortSignal"
286
+ >;
287
+
288
+ export type OurStreamObjectArgs<T> = StreamObjectArgs<T> &
289
+ Pick<
290
+ Parameters<typeof streamObject<T>>[0],
291
+ "onError" | "onFinish" | "abortSignal"
292
+ >;
293
+
294
+ type ThreadOutputMetadata = GenerationOutputMetadata & {
295
+ messageId: string;
296
+ };
297
+
298
+ /**
299
+ * The interface for a thread returned from {@link createThread} or {@link continueThread}.
300
+ * This is contextual to a thread and/or user.
301
+ */
302
+ export interface Thread<DefaultTools extends ToolSet> {
303
+ /**
304
+ * The target threadId, from the startThread or continueThread initializers.
305
+ */
306
+ threadId: string;
307
+ /**
308
+ * This behaves like {@link generateText} from the "ai" package except that
309
+ * it add context based on the userId and threadId and saves the input and
310
+ * resulting messages to the thread, if specified.
311
+ * Use {@link continueThread} to get a version of this function already scoped
312
+ * to a thread (and optionally userId).
313
+ * @param args The arguments to the generateText function, along with extra controls
314
+ * for the {@link ContextOptions} and {@link StorageOptions}.
315
+ * @returns The result of the generateText function.
316
+ */
317
+ generateText<
318
+ TOOLS extends ToolSet | undefined = undefined,
319
+ OUTPUT = never,
320
+ OUTPUT_PARTIAL = never,
321
+ >(
322
+ args: TextArgs<
323
+ TOOLS extends undefined ? DefaultTools : TOOLS,
324
+ TOOLS,
325
+ OUTPUT,
326
+ OUTPUT_PARTIAL
327
+ >,
328
+ options?: Options
329
+ ): Promise<
330
+ GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
331
+ ThreadOutputMetadata
332
+ >;
333
+
334
+ /**
335
+ * This behaves like {@link streamText} from the "ai" package except that
336
+ * it add context based on the userId and threadId and saves the input and
337
+ * resulting messages to the thread, if specified.
338
+ * Use {@link continueThread} to get a version of this function already scoped
339
+ * to a thread (and optionally userId).
340
+ * @param args The arguments to the streamText function, along with extra controls
341
+ * for the {@link ContextOptions} and {@link StorageOptions}.
342
+ * @returns The result of the streamText function.
343
+ */
344
+ streamText<
345
+ TOOLS extends ToolSet | undefined = undefined,
346
+ OUTPUT = never,
347
+ PARTIAL_OUTPUT = never,
348
+ >(
349
+ args: StreamingTextArgs<
350
+ TOOLS extends undefined ? DefaultTools : TOOLS,
351
+ TOOLS,
352
+ OUTPUT,
353
+ PARTIAL_OUTPUT
354
+ >,
355
+ options?: Options & {
356
+ /**
357
+ * Whether to save incremental data (deltas) from streaming responses.
358
+ * Defaults to false.
359
+ * If false, it will not save any deltas to the database.
360
+ * If true, it will save deltas with {@link DEFAULT_STREAMING_OPTIONS}.
361
+ *
362
+ * Regardless of this option, when streaming you are able to use this
363
+ * `streamText` function as you would with the "ai" package's version:
364
+ * iterating over the text, streaming it over HTTP, etc.
365
+ */
366
+ saveStreamDeltas?: boolean | StreamingOptions;
367
+ }
368
+ ): Promise<
369
+ StreamTextResult<
370
+ TOOLS extends undefined ? DefaultTools : TOOLS,
371
+ PARTIAL_OUTPUT
372
+ > &
373
+ ThreadOutputMetadata
374
+ >;
375
+ /**
376
+ * This behaves like {@link generateObject} from the "ai" package except that
377
+ * it add context based on the userId and threadId and saves the input and
378
+ * resulting messages to the thread, if specified. This overload is for objects, arrays, and enums.
379
+ * Use {@link continueThread} to get a version of this function already scoped
380
+ * to a thread (and optionally userId).
381
+ * @param args The arguments to the generateObject function, along with extra controls
382
+ * for the {@link ContextOptions} and {@link StorageOptions}.
383
+ * @returns The result of the generateObject function.
384
+ */
385
+ generateObject<T>(
386
+ args: OurObjectArgs<T>,
387
+ options?: Options
388
+ ): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
389
+ /**
390
+ * This behaves like {@link generateObject} from the "ai" package except that
391
+ * it add context based on the userId and threadId and saves the input and
392
+ * resulting messages to the thread, if specified. This overload is for when there's no schema.
393
+ * Use {@link continueThread} to get a version of this function already scoped
394
+ * to a thread (and optionally userId).
395
+ * @param args The arguments to the generateObject function, along with extra controls
396
+ * for the {@link ContextOptions} and {@link StorageOptions}.
397
+ * @returns The result of the generateObject function.
398
+ */
399
+ generateObject(
400
+ args: GenerateObjectNoSchemaOptions,
401
+ options?: Options
402
+ ): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
403
+ /**
404
+ * This behaves like {@link streamObject} from the "ai" package except that
405
+ * it add context based on the userId and threadId and saves the input and
406
+ * resulting messages to the thread, if specified.
407
+ * Use {@link continueThread} to get a version of this function already scoped
408
+ * to a thread (and optionally userId).
409
+ * @param args The arguments to the streamObject function, along with extra controls
410
+ * for the {@link ContextOptions} and {@link StorageOptions}.
411
+ * @returns The result of the streamObject function.
412
+ */
413
+ streamObject<T>(
414
+ args: OurStreamObjectArgs<T>,
415
+ options?: Options
416
+ ): Promise<
417
+ StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata
418
+ >;
419
+ }
420
+
421
+ export type Options = {
422
+ /**
423
+ * The context options to use for passing in message history to the LLM.
424
+ */
425
+ contextOptions?: ContextOptions;
426
+ /**
427
+ * The storage options to use for saving the input and output messages to the thread.
428
+ */
429
+ storageOptions?: StorageOptions;
430
+ };
10
431
 
432
+ export type SyncStreamsReturnValue =
433
+ | { kind: "list"; messages: StreamMessage[] }
434
+ | { kind: "deltas"; deltas: StreamDelta[] }
435
+ | undefined;
11
436
 
12
437
  /* Type utils follow */
13
438
  export type RunQueryCtx = {