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