@convex-dev/agent 0.5.0-alpha.1 → 0.6.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 (235) hide show
  1. package/MIGRATION.md +153 -0
  2. package/README.md +32 -27
  3. package/dist/UIMessages.d.ts +46 -0
  4. package/dist/UIMessages.d.ts.map +1 -0
  5. package/dist/UIMessages.js +546 -0
  6. package/dist/UIMessages.js.map +1 -0
  7. package/dist/client/createTool.d.ts +126 -27
  8. package/dist/client/createTool.d.ts.map +1 -1
  9. package/dist/client/createTool.js +67 -12
  10. package/dist/client/createTool.js.map +1 -1
  11. package/dist/client/defaultComponent.d.ts +11 -0
  12. package/dist/client/defaultComponent.d.ts.map +1 -0
  13. package/dist/client/defaultComponent.js +7 -0
  14. package/dist/client/defaultComponent.js.map +1 -0
  15. package/dist/client/definePlaygroundAPI.d.ts +1335 -204
  16. package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
  17. package/dist/client/definePlaygroundAPI.js +52 -28
  18. package/dist/client/definePlaygroundAPI.js.map +1 -1
  19. package/dist/client/files.d.ts +20 -7
  20. package/dist/client/files.d.ts.map +1 -1
  21. package/dist/client/files.js +68 -11
  22. package/dist/client/files.js.map +1 -1
  23. package/dist/client/index.d.ts +1116 -978
  24. package/dist/client/index.d.ts.map +1 -1
  25. package/dist/client/index.js +332 -747
  26. package/dist/client/index.js.map +1 -1
  27. package/dist/client/messages.d.ts +461 -0
  28. package/dist/client/messages.d.ts.map +1 -0
  29. package/dist/client/messages.js +106 -0
  30. package/dist/client/messages.js.map +1 -0
  31. package/dist/client/mockModel.d.ts +42 -0
  32. package/dist/client/mockModel.d.ts.map +1 -0
  33. package/dist/client/mockModel.js +182 -0
  34. package/dist/client/mockModel.js.map +1 -0
  35. package/dist/client/saveInputMessages.d.ts +20 -0
  36. package/dist/client/saveInputMessages.d.ts.map +1 -0
  37. package/dist/client/saveInputMessages.js +58 -0
  38. package/dist/client/saveInputMessages.js.map +1 -0
  39. package/dist/client/search.d.ts +350 -39
  40. package/dist/client/search.d.ts.map +1 -1
  41. package/dist/client/search.js +350 -39
  42. package/dist/client/search.js.map +1 -1
  43. package/dist/client/start.d.ts +84 -0
  44. package/dist/client/start.d.ts.map +1 -0
  45. package/dist/client/start.js +185 -0
  46. package/dist/client/start.js.map +1 -0
  47. package/dist/client/streamText.d.ts +46 -0
  48. package/dist/client/streamText.d.ts.map +1 -0
  49. package/dist/client/streamText.js +117 -0
  50. package/dist/client/streamText.js.map +1 -0
  51. package/dist/client/streaming.d.ts +3716 -32
  52. package/dist/client/streaming.d.ts.map +1 -1
  53. package/dist/client/streaming.js +161 -59
  54. package/dist/client/streaming.js.map +1 -1
  55. package/dist/client/threads.d.ts +46 -0
  56. package/dist/client/threads.d.ts.map +1 -0
  57. package/dist/client/threads.js +49 -0
  58. package/dist/client/threads.js.map +1 -0
  59. package/dist/client/types.d.ts +266 -128
  60. package/dist/client/types.d.ts.map +1 -1
  61. package/dist/client/utils.d.ts +4 -0
  62. package/dist/client/utils.d.ts.map +1 -0
  63. package/dist/client/utils.js +21 -0
  64. package/dist/client/utils.js.map +1 -0
  65. package/dist/component/_generated/api.d.ts +24 -2178
  66. package/dist/component/_generated/api.d.ts.map +1 -1
  67. package/dist/component/_generated/api.js +10 -1
  68. package/dist/component/_generated/api.js.map +1 -1
  69. package/dist/component/_generated/component.d.ts +3120 -0
  70. package/dist/component/_generated/component.d.ts.map +1 -0
  71. package/dist/component/_generated/component.js +11 -0
  72. package/dist/component/_generated/component.js.map +1 -0
  73. package/dist/component/_generated/dataModel.d.ts +4 -18
  74. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  75. package/dist/component/_generated/dataModel.js +11 -0
  76. package/dist/component/_generated/dataModel.js.map +1 -0
  77. package/dist/component/_generated/server.d.ts +10 -38
  78. package/dist/component/_generated/server.d.ts.map +1 -1
  79. package/dist/component/_generated/server.js +9 -5
  80. package/dist/component/_generated/server.js.map +1 -1
  81. package/dist/component/files.d.ts +16 -10
  82. package/dist/component/files.d.ts.map +1 -1
  83. package/dist/component/files.js +10 -2
  84. package/dist/component/files.js.map +1 -1
  85. package/dist/component/messages.d.ts +2578 -366
  86. package/dist/component/messages.d.ts.map +1 -1
  87. package/dist/component/messages.js +397 -154
  88. package/dist/component/messages.js.map +1 -1
  89. package/dist/component/schema.d.ts +5697 -3584
  90. package/dist/component/schema.d.ts.map +1 -1
  91. package/dist/component/schema.js +18 -41
  92. package/dist/component/schema.js.map +1 -1
  93. package/dist/component/streams.d.ts +39 -339
  94. package/dist/component/streams.d.ts.map +1 -1
  95. package/dist/component/streams.js +114 -73
  96. package/dist/component/streams.js.map +1 -1
  97. package/dist/component/threads.d.ts +13 -13
  98. package/dist/component/users.d.ts +7 -7
  99. package/dist/component/vector/index.d.ts +1 -1
  100. package/dist/component/vector/index.d.ts.map +1 -1
  101. package/dist/component/vector/index.js +1 -3
  102. package/dist/component/vector/index.js.map +1 -1
  103. package/dist/deltas.d.ts +43 -0
  104. package/dist/deltas.d.ts.map +1 -0
  105. package/dist/deltas.js +446 -0
  106. package/dist/deltas.js.map +1 -0
  107. package/dist/mapping.d.ts +38 -20
  108. package/dist/mapping.d.ts.map +1 -1
  109. package/dist/mapping.js +365 -97
  110. package/dist/mapping.js.map +1 -1
  111. package/dist/react/SmoothText.d.ts +5 -0
  112. package/dist/react/SmoothText.d.ts.map +1 -0
  113. package/dist/react/SmoothText.js +6 -0
  114. package/dist/react/SmoothText.js.map +1 -0
  115. package/dist/react/index.d.ts +5 -77
  116. package/dist/react/index.d.ts.map +1 -1
  117. package/dist/react/index.js +6 -160
  118. package/dist/react/index.js.map +1 -1
  119. package/dist/react/optimisticallySendMessage.d.ts +36 -3
  120. package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
  121. package/dist/react/optimisticallySendMessage.js +35 -9
  122. package/dist/react/optimisticallySendMessage.js.map +1 -1
  123. package/dist/react/types.d.ts +4 -18
  124. package/dist/react/types.d.ts.map +1 -1
  125. package/dist/react/useDeltaStreams.d.ts +10 -0
  126. package/dist/react/useDeltaStreams.d.ts.map +1 -0
  127. package/dist/react/useDeltaStreams.js +106 -0
  128. package/dist/react/useDeltaStreams.js.map +1 -0
  129. package/dist/react/useSmoothText.d.ts +13 -12
  130. package/dist/react/useSmoothText.d.ts.map +1 -1
  131. package/dist/react/useSmoothText.js +32 -15
  132. package/dist/react/useSmoothText.js.map +1 -1
  133. package/dist/react/useStreamingUIMessages.d.ts +22 -0
  134. package/dist/react/useStreamingUIMessages.d.ts.map +1 -0
  135. package/dist/react/useStreamingUIMessages.js +92 -0
  136. package/dist/react/useStreamingUIMessages.js.map +1 -0
  137. package/dist/react/useThreadMessages.d.ts +104 -0
  138. package/dist/react/useThreadMessages.d.ts.map +1 -0
  139. package/dist/react/useThreadMessages.js +148 -0
  140. package/dist/react/useThreadMessages.js.map +1 -0
  141. package/dist/react/useUIMessages.d.ts +96 -0
  142. package/dist/react/useUIMessages.d.ts.map +1 -0
  143. package/dist/react/useUIMessages.js +108 -0
  144. package/dist/react/useUIMessages.js.map +1 -0
  145. package/dist/shared.d.ts +20 -4
  146. package/dist/shared.d.ts.map +1 -1
  147. package/dist/shared.js +45 -8
  148. package/dist/shared.js.map +1 -1
  149. package/dist/validators.d.ts +22981 -5666
  150. package/dist/validators.d.ts.map +1 -1
  151. package/dist/validators.js +245 -137
  152. package/dist/validators.js.map +1 -1
  153. package/package.json +101 -51
  154. package/src/UIMessages.combineUIMessages.test.ts +239 -0
  155. package/src/UIMessages.test.ts +273 -0
  156. package/src/UIMessages.ts +739 -0
  157. package/src/client/approval.test.ts +350 -0
  158. package/src/client/createTool.ts +291 -76
  159. package/src/client/defaultComponent.ts +17 -0
  160. package/src/client/definePlaygroundAPI.ts +67 -31
  161. package/src/client/files.ts +100 -20
  162. package/src/client/index.test.ts +40 -85
  163. package/src/client/index.ts +638 -1289
  164. package/src/client/messages.ts +237 -0
  165. package/src/client/mockModel.ts +252 -0
  166. package/src/client/saveInputMessages.test.ts +583 -0
  167. package/src/client/saveInputMessages.ts +101 -0
  168. package/src/client/search.test.ts +1207 -0
  169. package/src/client/search.ts +581 -70
  170. package/src/client/start.ts +327 -0
  171. package/src/client/streamText.ts +187 -0
  172. package/src/client/streaming.test.ts +186 -0
  173. package/src/client/streaming.ts +241 -97
  174. package/src/client/threads.ts +83 -0
  175. package/src/client/types.ts +370 -219
  176. package/src/client/utils.ts +27 -0
  177. package/src/component/_generated/api.ts +64 -0
  178. package/src/component/_generated/component.ts +4902 -0
  179. package/src/component/_generated/{server.d.ts → server.ts} +33 -21
  180. package/src/component/files.ts +11 -2
  181. package/src/component/messages.test.ts +195 -51
  182. package/src/component/messages.ts +500 -201
  183. package/src/component/schema.ts +20 -46
  184. package/src/component/setup.test.ts +7 -0
  185. package/src/component/streams.ts +184 -83
  186. package/src/component/users.test.ts +0 -1
  187. package/src/component/vector/index.ts +1 -3
  188. package/src/deltas.test.ts +626 -0
  189. package/src/deltas.ts +569 -0
  190. package/src/fromUIMessages.test.ts +497 -0
  191. package/src/mapping.test.ts +180 -6
  192. package/src/mapping.ts +479 -162
  193. package/src/react/SmoothText.tsx +9 -0
  194. package/src/react/index.ts +10 -230
  195. package/src/react/optimisticallySendMessage.ts +55 -12
  196. package/src/react/types.ts +6 -39
  197. package/src/react/useDeltaStreams.ts +160 -0
  198. package/src/react/useSmoothText.ts +56 -36
  199. package/src/react/useStreamingUIMessages.ts +143 -0
  200. package/src/react/useThreadMessages.ts +262 -0
  201. package/src/react/useUIMessages.test.ts +255 -0
  202. package/src/react/useUIMessages.ts +195 -0
  203. package/src/shared.ts +88 -12
  204. package/src/test.ts +18 -0
  205. package/src/toUIMessages.test.ts +1269 -0
  206. package/src/validators.test.ts +18 -19
  207. package/src/validators.ts +325 -185
  208. package/dist/client/_generated/_ignore.d.ts +0 -1
  209. package/dist/client/_generated/_ignore.d.ts.map +0 -1
  210. package/dist/client/_generated/_ignore.js +0 -3
  211. package/dist/client/_generated/_ignore.js.map +0 -1
  212. package/dist/client/listMessages.d.ts +0 -22
  213. package/dist/client/listMessages.d.ts.map +0 -1
  214. package/dist/client/listMessages.js +0 -25
  215. package/dist/client/listMessages.js.map +0 -1
  216. package/dist/package.json +0 -3
  217. package/dist/react/deltas.d.ts +0 -26
  218. package/dist/react/deltas.d.ts.map +0 -1
  219. package/dist/react/deltas.js +0 -384
  220. package/dist/react/deltas.js.map +0 -1
  221. package/dist/react/toUIMessages.d.ts +0 -15
  222. package/dist/react/toUIMessages.d.ts.map +0 -1
  223. package/dist/react/toUIMessages.js +0 -211
  224. package/dist/react/toUIMessages.js.map +0 -1
  225. package/src/client/listMessages.ts +0 -38
  226. package/src/component/_generated/api.d.ts +0 -2202
  227. package/src/component/_generated/api.js +0 -23
  228. package/src/component/_generated/server.js +0 -90
  229. package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
  230. package/src/react/deltas.test.ts +0 -315
  231. package/src/react/deltas.ts +0 -478
  232. package/src/react/toUIMessages.test.ts +0 -420
  233. package/src/react/toUIMessages.ts +0 -253
  234. package/src/vitest.config.ts +0 -7
  235. /package/src/component/_generated/{dataModel.d.ts → dataModel.ts} +0 -0
@@ -1,49 +1,186 @@
1
- import type { LanguageModelV2 } from "@ai-sdk/provider";
2
1
  import type {
2
+ FlexibleSchema,
3
+ InferSchema,
3
4
  ModelMessage,
4
- DeepPartial,
5
- generateObject,
5
+ ProviderOptions,
6
+ } from "@ai-sdk/provider-utils";
7
+ import type { JSONValue } from "@ai-sdk/provider";
8
+ import type {
9
+ EmbeddingModel,
6
10
  GenerateObjectResult,
7
11
  generateText,
8
12
  GenerateTextResult,
9
- JSONValue,
10
13
  LanguageModelRequestMetadata,
11
14
  LanguageModelResponseMetadata,
12
- RepairTextFunction,
15
+ LanguageModelUsage,
16
+ LanguageModel,
13
17
  streamObject,
14
- StreamObjectResult,
15
18
  streamText,
16
19
  StreamTextResult,
17
- TelemetrySettings,
18
- ToolChoice,
19
20
  ToolSet,
20
- Schema,
21
- LanguageModelUsage,
21
+ CallSettings,
22
+ generateObject,
22
23
  } from "ai";
24
+
25
+ export interface Output<_T = any, _P = any, _E = any> {
26
+ name: string;
27
+ responseFormat: any;
28
+ parseCompleteOutput: any;
29
+ parsePartialOutput: any;
30
+ createElementStreamTransform: any;
31
+ }
23
32
  import type {
24
- Auth,
25
- Expand,
26
- FunctionReference,
27
- StorageActionWriter,
28
- StorageReader,
29
- WithoutSystemFields,
30
- FunctionArgs,
31
- FunctionReturnType,
32
33
  GenericActionCtx,
33
34
  GenericDataModel,
35
+ GenericMutationCtx,
36
+ GenericQueryCtx,
37
+ WithoutSystemFields,
34
38
  } from "convex/server";
35
- import type { GenericId } from "convex/values";
36
- import type { z } from "zod";
37
- import type { Mounts } from "../component/_generated/api.js";
38
- import type { ThreadDoc } from "../component/schema.js";
39
39
  import type {
40
- CallSettings,
40
+ MessageDoc,
41
41
  ProviderMetadata,
42
- ProviderOptions,
43
42
  StreamDelta,
44
43
  StreamMessage,
44
+ ThreadDoc,
45
45
  } from "../validators.js";
46
46
  import type { StreamingOptions } from "./streaming.js";
47
+ import type { ComponentApi } from "../component/_generated/component.js";
48
+
49
+ /**
50
+ * Type-level check that ensures models are from AI SDK v6.
51
+ * If a v5 model (LanguageModelV2) is passed, TypeScript will show the error message string.
52
+ */
53
+ type AssertAISDKv6<T> = T extends { specificationVersion: "v3" }
54
+ ? T
55
+ : "⚠️ @convex-dev/agent v0.6.0 requires AI SDK v6. Update your dependencies: npm install ai@^6.0.35 @ai-sdk/openai@^3.0.10 (or other provider). See: node_modules/@convex-dev/agent/MIGRATION.md";
56
+
57
+ export type AgentPrompt = {
58
+ /**
59
+ * System message to include in the prompt. Overwrites Agent instructions.
60
+ */
61
+ system?: string;
62
+ /**
63
+ * A prompt. It can be either a text prompt or a list of messages.
64
+ * If used with `promptMessageId`, it will be used in place of that
65
+ * prompt message and no input messages will be saved.
66
+ * Otherwise, if used with the storageOptions "promptAndOutput" (default),
67
+ * it will be the only message saved.
68
+ * If a string is provided, it will be a user message.
69
+ */
70
+ prompt?: string | Array<ModelMessage> | undefined;
71
+ /**
72
+ * A list of messages to use as context before the prompt.
73
+ * If used with `prompt`, these will precede the prompt.
74
+ * If used with the storageOptions "promptAndOutput" (default),
75
+ * none of these messages will be saved.
76
+ */
77
+ messages?: Array<ModelMessage> | undefined;
78
+ /**
79
+ * If provided, it uses this existing message to anchor the prompt:
80
+ * - The specified message will be included, unless `prompt` is also
81
+ * provided, in which case that will be inserted in place of this
82
+ * specified message.
83
+ * - Recent and search messages will not include messages after this
84
+ * message's order.
85
+ * - If there are already responses on the same order,
86
+ * for example, tool calls and responses,
87
+ * those will be included automatically.
88
+ *
89
+ * Note: if this is provided, no input messages will be saved by default.
90
+ */
91
+ promptMessageId?: string | undefined;
92
+ /**
93
+ * The model to use for the LLM calls. This will override the languageModel
94
+ * specified in the Agent config.
95
+ */
96
+ model?: LanguageModel;
97
+ };
98
+
99
+ export type Config = {
100
+ /**
101
+ * The LLM model to use for generating / streaming text and objects.
102
+ * Requires AI SDK v6 (@ai-sdk/* packages v3.x).
103
+ *
104
+ * @example
105
+ * import { openai } from "@ai-sdk/openai"
106
+ * const myAgent = new Agent(components.agent, {
107
+ * languageModel: openai.chat("gpt-4o-mini"),
108
+ * })
109
+ */
110
+ languageModel?: AssertAISDKv6<LanguageModel>;
111
+ /**
112
+ * @deprecated Use `embeddingModel` instead.
113
+ */
114
+ textEmbeddingModel?: EmbeddingModel;
115
+ /**
116
+ * The model to use for text embeddings. Optional.
117
+ * If specified, it will use this for generating vector embeddings
118
+ * of chats, and can opt-in to doing vector search for automatic context
119
+ * on generateText, etc.
120
+ * e.g.
121
+ * import { openai } from "@ai-sdk/openai"
122
+ * const myAgent = new Agent(components.agent, {
123
+ * ...
124
+ * embeddingModel: openai.embedding("text-embedding-3-small")
125
+ */
126
+ embeddingModel?: EmbeddingModel;
127
+ /**
128
+ * Options to determine what messages are included as context in message
129
+ * generation. To disable any messages automatically being added, pass:
130
+ * { recentMessages: 0 }
131
+ */
132
+ contextOptions?: ContextOptions;
133
+ /**
134
+ * Determines whether messages are automatically stored when passed as
135
+ * arguments or generated.
136
+ */
137
+ storageOptions?: StorageOptions;
138
+ /**
139
+ * The usage handler to use for this agent.
140
+ */
141
+ usageHandler?: UsageHandler;
142
+ /**
143
+ * By default, messages are ordered with context in `fetchContextWithPrompt`,
144
+ * but you can override this by providing a context handler. Here you can
145
+ * filter, modify, or enrich the context messages. If provided, the default
146
+ * ordering will not apply. This excludes the system message / instructions.
147
+ */
148
+ contextHandler?: ContextHandler;
149
+ /**
150
+ * Called for each LLM request/response, so you can do things like
151
+ * log the raw request body or response headers to a table, or logs.
152
+ */
153
+ rawRequestResponseHandler?: RawRequestResponseHandler;
154
+ /**
155
+ * @deprecated Reach out if you use this. Otherwise will be removed soon.
156
+ * Default provider options to pass for the LLM calls.
157
+ * This can be overridden at each generate/stream callsite on a per-field
158
+ * basis. To clear a default setting, you'll need to pass `undefined`.
159
+ */
160
+ providerOptions?: ProviderOptions;
161
+ /**
162
+ * The default settings to use for the LLM calls.
163
+ * This can be overridden at each generate/stream callsite on a per-field
164
+ * basis. To clear a default setting, you'll need to pass `undefined`.
165
+ */
166
+ callSettings?: CallSettings;
167
+ /**
168
+ * The maximum number of steps to allow for a single generation.
169
+ *
170
+ * For example, if an agent wants to call a tool, that call and tool response
171
+ * will be one step. Generating a response based on the tool call & response
172
+ * will be a second step.
173
+ * If it runs out of steps, it will return the last step result, which may
174
+ * not be an assistant message.
175
+
176
+ * This becomes the default value when `stopWhen` is not specified in the
177
+ * Agent or generation callsite.
178
+ * AI SDK v5 removed the `maxSteps` argument, but this is kept here for
179
+ * convenience and backwards compatibility.
180
+ * Defaults to 1.
181
+ */
182
+ maxSteps?: number;
183
+ };
47
184
 
48
185
  /**
49
186
  * Options to configure what messages are fetched as context,
@@ -115,10 +252,31 @@ export type StorageOptions = {
115
252
  saveMessages?: "all" | "none" | "promptAndOutput";
116
253
  };
117
254
 
118
- export type GenerationOutputMetadata = { messageId?: string; order?: number };
255
+ export type GenerationOutputMetadata = {
256
+ /**
257
+ * The ID of the prompt message for the generation.
258
+ */
259
+ promptMessageId?: string;
260
+ /**
261
+ * The order of the prompt message and responses for the generation.
262
+ * Each order starts with a user message, then followed by agent responses.
263
+ * If a promptMessageId is provided, that dictates the order.
264
+ */
265
+ order?: number;
266
+ /**
267
+ * The messages saved for the generation - both saved input and output.
268
+ * If you passed promptMessageId, it will not include that message.
269
+ */
270
+ savedMessages?: MessageDoc[];
271
+ /**
272
+ * @deprecated Use promptMessageId instead.
273
+ * The ID of the prompt message for the generation.
274
+ */
275
+ messageId?: string;
276
+ };
119
277
 
120
278
  export type UsageHandler = (
121
- ctx: RunActionCtx,
279
+ ctx: ActionCtx,
122
280
  args: {
123
281
  userId: string | undefined;
124
282
  threadId: string | undefined;
@@ -131,6 +289,54 @@ export type UsageHandler = (
131
289
  },
132
290
  ) => void | Promise<void>;
133
291
 
292
+ /**
293
+ * By default, messages are ordered with context in `fetchContextWithPrompt`,
294
+ * but you can override this by providing a context handler. Here you can filter
295
+ * out, add in, or reorder messages.
296
+ */
297
+ export type ContextHandler = (
298
+ ctx: ActionCtx,
299
+ args: {
300
+ /**
301
+ * All messages in the default order.
302
+ */
303
+ allMessages: ModelMessage[];
304
+ /**
305
+ * The messages fetched from search.
306
+ */
307
+ search: ModelMessage[];
308
+ /**
309
+ * The recent messages already in the thread history,
310
+ * excluding any messages that came after promptMessageId.
311
+ */
312
+ recent: ModelMessage[];
313
+ /**
314
+ * The messages passed as the `messages` argument to e.g. generateText.
315
+ */
316
+ inputMessages: ModelMessage[];
317
+ /**
318
+ * The message(s) passed as the `prompt` argument to e.g. generateText.
319
+ * Otherwise, if `promptMessageId` was provided, the message at that id.
320
+ * `prompt` will override the message at `promptMessageId`.
321
+ */
322
+ inputPrompt: ModelMessage[];
323
+ /**
324
+ * Any messages on the same `order` as the promptMessageId message after the
325
+ * prompt message. These are presumably existing responses to the prompt
326
+ * message.
327
+ */
328
+ existingResponses: ModelMessage[];
329
+ /**
330
+ * The user associated with the generation, if any.
331
+ */
332
+ userId: string | undefined;
333
+ /**
334
+ * The thread associated with the generation, if any.
335
+ */
336
+ threadId: string | undefined;
337
+ },
338
+ ) => ModelMessage[] | Promise<ModelMessage[]>;
339
+
134
340
  export type RawRequestResponseHandler = (
135
341
  ctx: ActionCtx,
136
342
  args: {
@@ -142,157 +348,117 @@ export type RawRequestResponseHandler = (
142
348
  },
143
349
  ) => void | Promise<void>;
144
350
 
145
- export type AgentComponent = UseApi<Mounts>;
351
+ export type AgentComponent = ComponentApi;
146
352
 
147
353
  export type TextArgs<
148
354
  AgentTools extends ToolSet,
149
355
  TOOLS extends ToolSet | undefined = undefined,
150
- OUTPUT = never,
151
- OUTPUT_PARTIAL = never,
356
+ OUTPUT extends Output<any, any, any> = never,
152
357
  > = Omit<
153
358
  Parameters<
154
359
  typeof generateText<
155
360
  TOOLS extends undefined ? AgentTools : TOOLS,
156
- OUTPUT,
157
- OUTPUT_PARTIAL
361
+ OUTPUT
158
362
  >
159
363
  >[0],
160
- "toolChoice" | "tools" | "model"
364
+ "model" | "prompt" | "messages"
161
365
  > & {
162
- /**
163
- * If provided, this message will be used as the "prompt" for the LLM call,
164
- * instead of the prompt or messages.
165
- * This is useful if you want to first save a user message, then use it as
166
- * the prompt for the LLM call in another call.
167
- */
168
- promptMessageId?: string;
169
- /**
170
- * The model to use for the LLM calls. This will override the model specified
171
- * in the Agent constructor.
172
- */
173
- model?: LanguageModelV2;
174
366
  /**
175
367
  * The tools to use for the tool calls. This will override tools specified
176
368
  * in the Agent constructor or createThread / continueThread.
177
369
  */
178
370
  tools?: TOOLS;
179
- /**
180
- * The tool choice to use for the tool calls. This must be one of the tools
181
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
182
- */
183
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
184
- };
371
+ } & AgentPrompt;
185
372
 
186
373
  export type StreamingTextArgs<
187
374
  AgentTools extends ToolSet,
188
375
  TOOLS extends ToolSet | undefined = undefined,
189
- OUTPUT = never,
190
- OUTPUT_PARTIAL = never,
376
+ OUTPUT extends Output<any, any, any> = never,
191
377
  > = Omit<
192
378
  Parameters<
193
379
  typeof streamText<
194
380
  TOOLS extends undefined ? AgentTools : TOOLS,
195
- OUTPUT,
196
- OUTPUT_PARTIAL
381
+ OUTPUT
197
382
  >
198
383
  >[0],
199
- "toolChoice" | "tools" | "model"
384
+ "model" | "prompt" | "messages"
200
385
  > & {
201
- /**
202
- * If provided, this message will be used as the "prompt" for the LLM call,
203
- * instead of the prompt or messages.
204
- * This is useful if you want to first save a user message, then use it as
205
- * the prompt for the LLM call in another call.
206
- */
207
- promptMessageId?: string;
208
- /**
209
- * The model to use for the tool calls. This will override the model specified
210
- * in the Agent constructor.
211
- */
212
- model?: LanguageModelV2;
213
386
  /**
214
387
  * The tools to use for the tool calls. This will override tools specified
215
388
  * in the Agent constructor or createThread / continueThread.
216
389
  */
217
390
  tools?: TOOLS;
218
- /**
219
- * The tool choice to use for the tool calls. This must be one of the tools
220
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
221
- */
222
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
223
- };
391
+ } & AgentPrompt;
224
392
 
225
- type BaseGenerateObjectOptions = CallSettings & {
226
- /**
227
- * The model to use for the object generation. This will override the model
228
- * specified in the Agent constructor.
229
- */
230
- model?: LanguageModelV2;
231
- /**
232
- * The system prompt to use for the object generation. This will override the
233
- * system prompt specified in the Agent constructor.
234
- */
235
- system?: string;
236
- /**
237
- * The prompt to the LLM to use for the object generation.
238
- * Specify this or messages, but not both.
239
- */
240
- prompt?: string | Array<ModelMessage>;
241
- /**
242
- * The messages to use for the object generation.
243
- * Note: recent messages are automatically added based on the thread it's
244
- * associated with and your contextOptions.
245
- */
246
- messages?: Array<ModelMessage>;
247
- /**
248
- * The message to use as the "prompt" for the object generation.
249
- * If this is provided, it will be used instead of the prompt or messages.
250
- * This is useful if you want to first save a user message, then use it as
251
- * the prompt for the object generation in another call.
252
- */
253
- promptMessageId?: string;
254
- experimental_repairText?: RepairTextFunction;
255
- experimental_telemetry?: TelemetrySettings;
256
- providerOptions?: ProviderOptions;
257
- experimental_providerMetadata?: ProviderMetadata;
258
- };
259
-
260
- type StandardGenerateObjectOptions<T> = {
261
- schema: z.Schema<T>;
262
- schemaName?: string;
263
- schemaDescription?: string;
264
- output?: "object" | "array";
265
- mode?: "auto" | "json" | "tool";
266
- };
267
-
268
- // TODO: simplify this to just use the generateObject args, with an optional
269
- // model and tool/toolChoice types
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
- );
393
+ export type ObjectMode = "object" | "array" | "enum" | "no-schema";
276
394
 
277
- type StreamObjectArgs<T> = BaseGenerateObjectOptions &
278
- (
279
- | StandardGenerateObjectOptions<T>
280
- | { output: "any"; schema: undefined; mode: "json" }
281
- );
395
+ export type GenerateObjectArgs<
396
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
397
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
398
+ ? "enum"
399
+ : "object",
400
+ RESULT = OUTPUT extends "array"
401
+ ? Array<InferSchema<SCHEMA>>
402
+ : InferSchema<SCHEMA>,
403
+ > = AgentPrompt &
404
+ Omit<
405
+ Parameters<typeof generateObject<SCHEMA, OUTPUT, RESULT>>[0],
406
+ "model" | "prompt" | "messages"
407
+ > & {
408
+ schema?: SCHEMA;
409
+ enum?: Array<RESULT>;
410
+ };
282
411
 
283
- export type OurObjectArgs<T> = GenerateObjectArgs<T> &
284
- Pick<
285
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
- Parameters<typeof generateObject<any>>[0],
287
- "experimental_repairText" | "abortSignal"
288
- >;
412
+ export type StreamObjectArgs<
413
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
414
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
415
+ ? "enum"
416
+ : "object",
417
+ RESULT = OUTPUT extends "array"
418
+ ? Array<InferSchema<SCHEMA>>
419
+ : InferSchema<SCHEMA>,
420
+ > = AgentPrompt &
421
+ Omit<
422
+ Parameters<typeof streamObject<SCHEMA, OUTPUT, RESULT>>[0],
423
+ "model" | "prompt" | "messages"
424
+ > & {
425
+ schema?: SCHEMA;
426
+ enum?: Array<RESULT>;
427
+ };
289
428
 
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
- >;
429
+ export type MaybeCustomCtx<
430
+ CustomCtx,
431
+ DataModel extends GenericDataModel,
432
+ AgentTools extends ToolSet,
433
+ > =
434
+ CustomCtx extends Record<string, unknown>
435
+ ? {
436
+ /**
437
+ * If you have a custom ctx that you use with the Agent
438
+ * (e.g. new Agent<{ orgId: string }>(...))
439
+ * you need to provide this function to add any extra fields.
440
+ * e.g.
441
+ * ```ts
442
+ * const myAgent = new Agent<{ orgId: string }>(...);
443
+ * const myAction = myAgent.asTextAction({
444
+ * customCtx: (ctx: ActionCtx, target, llmArgs) => {
445
+ * const orgId = await lookupOrgId(ctx, target.threadId);
446
+ * return { orgId };
447
+ * },
448
+ * });
449
+ * ```
450
+ * Then, in your tools, you can
451
+ */
452
+ customCtx: (
453
+ ctx: GenericActionCtx<DataModel>,
454
+ target: {
455
+ userId?: string | undefined;
456
+ threadId?: string | undefined;
457
+ },
458
+ llmArgs: TextArgs<AgentTools>,
459
+ ) => CustomCtx;
460
+ }
461
+ : { customCtx?: never };
296
462
 
297
463
  type ThreadOutputMetadata = Required<GenerationOutputMetadata>;
298
464
 
@@ -327,15 +493,14 @@ export interface Thread<DefaultTools extends ToolSet> {
327
493
  */
328
494
  generateText<
329
495
  TOOLS extends ToolSet | undefined = undefined,
330
- OUTPUT = never,
331
- OUTPUT_PARTIAL = never,
496
+ OUTPUT extends Output<any, any, any> = never,
332
497
  >(
333
- args: TextArgs<
334
- TOOLS extends undefined ? DefaultTools : TOOLS,
335
- TOOLS,
336
- OUTPUT,
337
- OUTPUT_PARTIAL
338
- >,
498
+ generateTextArgs: AgentPrompt &
499
+ TextArgs<
500
+ TOOLS extends undefined ? DefaultTools : TOOLS,
501
+ TOOLS,
502
+ OUTPUT
503
+ >,
339
504
  options?: Options,
340
505
  ): Promise<
341
506
  GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
@@ -354,15 +519,14 @@ export interface Thread<DefaultTools extends ToolSet> {
354
519
  */
355
520
  streamText<
356
521
  TOOLS extends ToolSet | undefined = undefined,
357
- OUTPUT = never,
358
- PARTIAL_OUTPUT = never,
522
+ OUTPUT extends Output<any, any, any> = never,
359
523
  >(
360
- args: StreamingTextArgs<
361
- TOOLS extends undefined ? DefaultTools : TOOLS,
362
- TOOLS,
363
- OUTPUT,
364
- PARTIAL_OUTPUT
365
- >,
524
+ streamTextArgs: AgentPrompt &
525
+ StreamingTextArgs<
526
+ TOOLS extends undefined ? DefaultTools : TOOLS,
527
+ TOOLS,
528
+ OUTPUT
529
+ >,
366
530
  options?: Options & {
367
531
  /**
368
532
  * Whether to save incremental data (deltas) from streaming responses.
@@ -379,7 +543,7 @@ export interface Thread<DefaultTools extends ToolSet> {
379
543
  ): Promise<
380
544
  StreamTextResult<
381
545
  TOOLS extends undefined ? DefaultTools : TOOLS,
382
- PARTIAL_OUTPUT
546
+ OUTPUT
383
547
  > &
384
548
  ThreadOutputMetadata
385
549
  >;
@@ -393,10 +557,19 @@ export interface Thread<DefaultTools extends ToolSet> {
393
557
  * for the {@link ContextOptions} and {@link StorageOptions}.
394
558
  * @returns The result of the generateObject function.
395
559
  */
396
- generateObject<T = JSONValue>(
397
- args: OurObjectArgs<T>,
560
+ generateObject<
561
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
562
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
563
+ ? "enum"
564
+ : "object",
565
+ RESULT = OUTPUT extends "array"
566
+ ? Array<InferSchema<SCHEMA>>
567
+ : InferSchema<SCHEMA>,
568
+ >(
569
+ generateObjectArgs: AgentPrompt &
570
+ GenerateObjectArgs<SCHEMA, OUTPUT, RESULT>,
398
571
  options?: Options,
399
- ): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
572
+ ): Promise<GenerateObjectResult<RESULT> & ThreadOutputMetadata>;
400
573
  /**
401
574
  * This behaves like {@link streamObject} from the "ai" package except that
402
575
  * it add context based on the userId and threadId and saves the input and
@@ -407,11 +580,23 @@ export interface Thread<DefaultTools extends ToolSet> {
407
580
  * for the {@link ContextOptions} and {@link StorageOptions}.
408
581
  * @returns The result of the streamObject function.
409
582
  */
410
- streamObject<T extends z.Schema | Schema>(
411
- args: OurStreamObjectArgs<T>,
583
+ streamObject<
584
+ SCHEMA extends FlexibleSchema<unknown> = FlexibleSchema<JSONValue>,
585
+ OUTPUT extends ObjectMode = InferSchema<SCHEMA> extends string
586
+ ? "enum"
587
+ : "object",
588
+ RESULT = OUTPUT extends "array"
589
+ ? Array<InferSchema<SCHEMA>>
590
+ : InferSchema<SCHEMA>,
591
+ >(
592
+ /**
593
+ * The same arguments you'd pass to "ai" sdk {@link streamObject}.
594
+ */
595
+ streamObjectArgs: AgentPrompt & StreamObjectArgs<SCHEMA, OUTPUT, RESULT>,
412
596
  options?: Options,
413
597
  ): Promise<
414
- StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata
598
+ ReturnType<typeof streamObject<SCHEMA, OUTPUT, RESULT>> &
599
+ ThreadOutputMetadata
415
600
  >;
416
601
  }
417
602
 
@@ -424,6 +609,18 @@ export type Options = {
424
609
  * The storage options to use for saving the input and output messages to the thread.
425
610
  */
426
611
  storageOptions?: StorageOptions;
612
+ /**
613
+ * The usage handler to use for this thread. Overrides any handler
614
+ * set in the agent constructor.
615
+ */
616
+ usageHandler?: UsageHandler;
617
+ /**
618
+ * By default, messages are ordered with context in `fetchContextWithPrompt`,
619
+ * but you can override this by providing a context handler. Here you can
620
+ * filter, modify, or enrich the context messages. If provided, the default
621
+ * ordering will not apply. This excludes the system message / instructions.
622
+ */
623
+ contextHandler?: ContextHandler;
427
624
  };
428
625
 
429
626
  export type SyncStreamsReturnValue =
@@ -432,58 +629,12 @@ export type SyncStreamsReturnValue =
432
629
  | undefined;
433
630
 
434
631
  /* Type utils follow */
435
- export type RunQueryCtx = {
436
- runQuery: <Query extends FunctionReference<"query", "internal">>(
437
- query: Query,
438
- args: FunctionArgs<Query>,
439
- ) => Promise<FunctionReturnType<Query>>;
440
- };
441
- export type RunMutationCtx = RunQueryCtx & {
442
- runMutation: <Mutation extends FunctionReference<"mutation", "internal">>(
443
- mutation: Mutation,
444
- args: FunctionArgs<Mutation>,
445
- ) => Promise<FunctionReturnType<Mutation>>;
446
- };
447
- export type RunActionCtx = RunMutationCtx & {
448
- runAction<Action extends FunctionReference<"action", "internal">>(
449
- action: Action,
450
- args: FunctionArgs<Action>,
451
- ): Promise<FunctionReturnType<Action>>;
452
- };
453
- export type UserActionCtx = GenericActionCtx<GenericDataModel>;
454
- export type ActionCtx = RunActionCtx & {
455
- auth: Auth;
456
- storage: StorageActionWriter;
457
- };
458
- export type QueryCtx = RunQueryCtx & {
459
- storage: StorageReader;
460
- };
461
-
462
- export type OpaqueIds<T> =
463
- T extends GenericId<infer _T>
464
- ? string
465
- : T extends (infer U)[]
466
- ? OpaqueIds<U>[]
467
- : T extends ArrayBuffer
468
- ? ArrayBuffer
469
- : T extends object
470
- ? { [K in keyof T]: OpaqueIds<T[K]> }
471
- : T;
472
-
473
- export type UseApi<API> = Expand<{
474
- [mod in keyof API]: API[mod] extends FunctionReference<
475
- infer FType,
476
- "public",
477
- infer FArgs,
478
- infer FReturnType,
479
- infer FComponentPath
480
- >
481
- ? FunctionReference<
482
- FType,
483
- "internal",
484
- OpaqueIds<FArgs>,
485
- OpaqueIds<FReturnType>,
486
- FComponentPath
487
- >
488
- : UseApi<API[mod]>;
489
- }>;
632
+ export type QueryCtx = Pick<GenericQueryCtx<GenericDataModel>, "runQuery">;
633
+ export type MutationCtx = Pick<
634
+ GenericMutationCtx<GenericDataModel>,
635
+ "runQuery" | "runMutation"
636
+ >;
637
+ export type ActionCtx = Pick<
638
+ GenericActionCtx<GenericDataModel>,
639
+ "runQuery" | "runMutation" | "runAction" | "storage" | "auth"
640
+ >;