@effect/ai 0.26.0 → 0.27.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 (188) hide show
  1. package/Chat/package.json +6 -0
  2. package/EmbeddingModel/package.json +6 -0
  3. package/IdGenerator/package.json +6 -0
  4. package/LanguageModel/package.json +6 -0
  5. package/Model/package.json +6 -0
  6. package/Prompt/package.json +6 -0
  7. package/Response/package.json +6 -0
  8. package/Telemetry/package.json +6 -0
  9. package/Tool/package.json +6 -0
  10. package/Toolkit/package.json +6 -0
  11. package/dist/cjs/AiError.js +575 -11
  12. package/dist/cjs/AiError.js.map +1 -1
  13. package/dist/cjs/Chat.js +302 -0
  14. package/dist/cjs/Chat.js.map +1 -0
  15. package/dist/cjs/EmbeddingModel.js +184 -0
  16. package/dist/cjs/EmbeddingModel.js.map +1 -0
  17. package/dist/cjs/IdGenerator.js +255 -0
  18. package/dist/cjs/IdGenerator.js.map +1 -0
  19. package/dist/cjs/LanguageModel.js +584 -0
  20. package/dist/cjs/LanguageModel.js.map +1 -0
  21. package/dist/cjs/McpServer.js +12 -4
  22. package/dist/cjs/McpServer.js.map +1 -1
  23. package/dist/cjs/Model.js +118 -0
  24. package/dist/cjs/Model.js.map +1 -0
  25. package/dist/cjs/Prompt.js +649 -0
  26. package/dist/cjs/Prompt.js.map +1 -0
  27. package/dist/cjs/Response.js +635 -0
  28. package/dist/cjs/Response.js.map +1 -0
  29. package/dist/cjs/Telemetry.js +176 -0
  30. package/dist/cjs/Telemetry.js.map +1 -0
  31. package/dist/cjs/Tokenizer.js +87 -8
  32. package/dist/cjs/Tokenizer.js.map +1 -1
  33. package/dist/cjs/Tool.js +556 -0
  34. package/dist/cjs/Tool.js.map +1 -0
  35. package/dist/cjs/Toolkit.js +279 -0
  36. package/dist/cjs/Toolkit.js.map +1 -0
  37. package/dist/cjs/index.js +21 -19
  38. package/dist/dts/AiError.d.ts +577 -9
  39. package/dist/dts/AiError.d.ts.map +1 -1
  40. package/dist/dts/Chat.d.ts +356 -0
  41. package/dist/dts/Chat.d.ts.map +1 -0
  42. package/dist/dts/EmbeddingModel.d.ts +153 -0
  43. package/dist/dts/EmbeddingModel.d.ts.map +1 -0
  44. package/dist/dts/IdGenerator.d.ts +272 -0
  45. package/dist/dts/IdGenerator.d.ts.map +1 -0
  46. package/dist/dts/LanguageModel.d.ts +458 -0
  47. package/dist/dts/LanguageModel.d.ts.map +1 -0
  48. package/dist/dts/McpSchema.d.ts +25 -25
  49. package/dist/dts/McpServer.d.ts +6 -4
  50. package/dist/dts/McpServer.d.ts.map +1 -1
  51. package/dist/dts/Model.d.ts +124 -0
  52. package/dist/dts/Model.d.ts.map +1 -0
  53. package/dist/dts/Prompt.d.ts +1119 -0
  54. package/dist/dts/Prompt.d.ts.map +1 -0
  55. package/dist/dts/Response.d.ts +1519 -0
  56. package/dist/dts/Response.d.ts.map +1 -0
  57. package/dist/dts/Telemetry.d.ts +520 -0
  58. package/dist/dts/Telemetry.d.ts.map +1 -0
  59. package/dist/dts/Tokenizer.d.ts +131 -13
  60. package/dist/dts/Tokenizer.d.ts.map +1 -1
  61. package/dist/dts/Tool.d.ts +876 -0
  62. package/dist/dts/Tool.d.ts.map +1 -0
  63. package/dist/dts/Toolkit.d.ts +310 -0
  64. package/dist/dts/Toolkit.d.ts.map +1 -0
  65. package/dist/dts/index.d.ts +498 -13
  66. package/dist/dts/index.d.ts.map +1 -1
  67. package/dist/esm/AiError.js +570 -10
  68. package/dist/esm/AiError.js.map +1 -1
  69. package/dist/esm/Chat.js +291 -0
  70. package/dist/esm/Chat.js.map +1 -0
  71. package/dist/esm/EmbeddingModel.js +173 -0
  72. package/dist/esm/EmbeddingModel.js.map +1 -0
  73. package/dist/esm/IdGenerator.js +245 -0
  74. package/dist/esm/IdGenerator.js.map +1 -0
  75. package/dist/esm/LanguageModel.js +572 -0
  76. package/dist/esm/LanguageModel.js.map +1 -0
  77. package/dist/esm/McpServer.js +12 -4
  78. package/dist/esm/McpServer.js.map +1 -1
  79. package/dist/esm/Model.js +108 -0
  80. package/dist/esm/Model.js.map +1 -0
  81. package/dist/esm/Prompt.js +633 -0
  82. package/dist/esm/Prompt.js.map +1 -0
  83. package/dist/esm/Response.js +619 -0
  84. package/dist/esm/Response.js.map +1 -0
  85. package/dist/esm/Telemetry.js +166 -0
  86. package/dist/esm/Telemetry.js.map +1 -0
  87. package/dist/esm/Tokenizer.js +87 -8
  88. package/dist/esm/Tokenizer.js.map +1 -1
  89. package/dist/esm/Tool.js +534 -0
  90. package/dist/esm/Tool.js.map +1 -0
  91. package/dist/esm/Toolkit.js +269 -0
  92. package/dist/esm/Toolkit.js.map +1 -0
  93. package/dist/esm/index.js +498 -13
  94. package/dist/esm/index.js.map +1 -1
  95. package/package.json +76 -68
  96. package/src/AiError.ts +739 -9
  97. package/src/Chat.ts +546 -0
  98. package/src/EmbeddingModel.ts +311 -0
  99. package/src/IdGenerator.ts +320 -0
  100. package/src/LanguageModel.ts +1074 -0
  101. package/src/McpServer.ts +337 -194
  102. package/src/Model.ts +155 -0
  103. package/src/Prompt.ts +1616 -0
  104. package/src/Response.ts +2131 -0
  105. package/src/Telemetry.ts +655 -0
  106. package/src/Tokenizer.ts +145 -24
  107. package/src/Tool.ts +1267 -0
  108. package/src/Toolkit.ts +516 -0
  109. package/src/index.ts +499 -13
  110. package/AiChat/package.json +0 -6
  111. package/AiEmbeddingModel/package.json +0 -6
  112. package/AiInput/package.json +0 -6
  113. package/AiLanguageModel/package.json +0 -6
  114. package/AiModel/package.json +0 -6
  115. package/AiResponse/package.json +0 -6
  116. package/AiTelemetry/package.json +0 -6
  117. package/AiTool/package.json +0 -6
  118. package/AiToolkit/package.json +0 -6
  119. package/dist/cjs/AiChat.js +0 -122
  120. package/dist/cjs/AiChat.js.map +0 -1
  121. package/dist/cjs/AiEmbeddingModel.js +0 -109
  122. package/dist/cjs/AiEmbeddingModel.js.map +0 -1
  123. package/dist/cjs/AiInput.js +0 -458
  124. package/dist/cjs/AiInput.js.map +0 -1
  125. package/dist/cjs/AiLanguageModel.js +0 -351
  126. package/dist/cjs/AiLanguageModel.js.map +0 -1
  127. package/dist/cjs/AiModel.js +0 -37
  128. package/dist/cjs/AiModel.js.map +0 -1
  129. package/dist/cjs/AiResponse.js +0 -681
  130. package/dist/cjs/AiResponse.js.map +0 -1
  131. package/dist/cjs/AiTelemetry.js +0 -58
  132. package/dist/cjs/AiTelemetry.js.map +0 -1
  133. package/dist/cjs/AiTool.js +0 -150
  134. package/dist/cjs/AiTool.js.map +0 -1
  135. package/dist/cjs/AiToolkit.js +0 -157
  136. package/dist/cjs/AiToolkit.js.map +0 -1
  137. package/dist/cjs/internal/common.js +0 -21
  138. package/dist/cjs/internal/common.js.map +0 -1
  139. package/dist/dts/AiChat.d.ts +0 -101
  140. package/dist/dts/AiChat.d.ts.map +0 -1
  141. package/dist/dts/AiEmbeddingModel.d.ts +0 -65
  142. package/dist/dts/AiEmbeddingModel.d.ts.map +0 -1
  143. package/dist/dts/AiInput.d.ts +0 -590
  144. package/dist/dts/AiInput.d.ts.map +0 -1
  145. package/dist/dts/AiLanguageModel.d.ts +0 -302
  146. package/dist/dts/AiLanguageModel.d.ts.map +0 -1
  147. package/dist/dts/AiModel.d.ts +0 -25
  148. package/dist/dts/AiModel.d.ts.map +0 -1
  149. package/dist/dts/AiResponse.d.ts +0 -863
  150. package/dist/dts/AiResponse.d.ts.map +0 -1
  151. package/dist/dts/AiTelemetry.d.ts +0 -242
  152. package/dist/dts/AiTelemetry.d.ts.map +0 -1
  153. package/dist/dts/AiTool.d.ts +0 -334
  154. package/dist/dts/AiTool.d.ts.map +0 -1
  155. package/dist/dts/AiToolkit.d.ts +0 -96
  156. package/dist/dts/AiToolkit.d.ts.map +0 -1
  157. package/dist/dts/internal/common.d.ts +0 -2
  158. package/dist/dts/internal/common.d.ts.map +0 -1
  159. package/dist/esm/AiChat.js +0 -111
  160. package/dist/esm/AiChat.js.map +0 -1
  161. package/dist/esm/AiEmbeddingModel.js +0 -98
  162. package/dist/esm/AiEmbeddingModel.js.map +0 -1
  163. package/dist/esm/AiInput.js +0 -433
  164. package/dist/esm/AiInput.js.map +0 -1
  165. package/dist/esm/AiLanguageModel.js +0 -340
  166. package/dist/esm/AiLanguageModel.js.map +0 -1
  167. package/dist/esm/AiModel.js +0 -29
  168. package/dist/esm/AiModel.js.map +0 -1
  169. package/dist/esm/AiResponse.js +0 -657
  170. package/dist/esm/AiResponse.js.map +0 -1
  171. package/dist/esm/AiTelemetry.js +0 -48
  172. package/dist/esm/AiTelemetry.js.map +0 -1
  173. package/dist/esm/AiTool.js +0 -134
  174. package/dist/esm/AiTool.js.map +0 -1
  175. package/dist/esm/AiToolkit.js +0 -147
  176. package/dist/esm/AiToolkit.js.map +0 -1
  177. package/dist/esm/internal/common.js +0 -14
  178. package/dist/esm/internal/common.js.map +0 -1
  179. package/src/AiChat.ts +0 -251
  180. package/src/AiEmbeddingModel.ts +0 -169
  181. package/src/AiInput.ts +0 -602
  182. package/src/AiLanguageModel.ts +0 -685
  183. package/src/AiModel.ts +0 -53
  184. package/src/AiResponse.ts +0 -986
  185. package/src/AiTelemetry.ts +0 -333
  186. package/src/AiTool.ts +0 -579
  187. package/src/AiToolkit.ts +0 -265
  188. package/src/internal/common.ts +0 -12
@@ -0,0 +1,2131 @@
1
+ /**
2
+ * The `Response` module provides data structures to represent responses from
3
+ * large language models.
4
+ *
5
+ * This module defines the complete structure of AI model responses, including
6
+ * various content parts for text, reasoning, tool calls, files, and metadata,
7
+ * supporting both streaming and non-streaming responses.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { Response } from "@effect/ai"
12
+ *
13
+ * // Create a simple text response part
14
+ * const textResponse = Response.makePart("text", {
15
+ * text: "The weather is sunny today!"
16
+ * })
17
+ *
18
+ * // Create a tool call response part
19
+ * const toolCallResponse = Response.makePart("tool-call", {
20
+ * id: "call_123",
21
+ * name: "get_weather",
22
+ * params: { city: "San Francisco" },
23
+ * providerExecuted: false
24
+ * })
25
+ * ```
26
+ *
27
+ * @since 1.0.0
28
+ */
29
+ import { ParseResult } from "effect"
30
+ import type * as DateTime from "effect/DateTime"
31
+ import * as Effect from "effect/Effect"
32
+ import { constFalse } from "effect/Function"
33
+ import type * as Option from "effect/Option"
34
+ import * as Predicate from "effect/Predicate"
35
+ import * as Schema from "effect/Schema"
36
+ import type * as Tool from "./Tool.js"
37
+ import type * as Toolkit from "./Toolkit.js"
38
+
39
+ const constEmptyObject = () => ({})
40
+
41
+ // =============================================================================
42
+ // All Parts
43
+ // =============================================================================
44
+
45
+ /**
46
+ * Unique identifier for Response Part instances.
47
+ *
48
+ * @since 1.0.0
49
+ * @category Type Ids
50
+ */
51
+ export const PartTypeId = "~effect/ai/Content/Part"
52
+
53
+ /**
54
+ * Type-level representation of the Response Part identifier.
55
+ *
56
+ * @since 1.0.0
57
+ * @category Type Ids
58
+ */
59
+ export type PartTypeId = typeof PartTypeId
60
+
61
+ /**
62
+ * Type guard to check if a value is a Response Part.
63
+ *
64
+ * @since 1.0.0
65
+ * @category Guards
66
+ */
67
+ export const isPart = (u: unknown): u is AnyPart => Predicate.hasProperty(u, PartTypeId)
68
+
69
+ /**
70
+ * Union type representing all possible response content parts.
71
+ *
72
+ * @since 1.0.0
73
+ * @category Models
74
+ */
75
+ export type AnyPart =
76
+ | TextPart
77
+ | TextStartPart
78
+ | TextDeltaPart
79
+ | TextEndPart
80
+ | ReasoningPart
81
+ | ReasoningStartPart
82
+ | ReasoningDeltaPart
83
+ | ReasoningEndPart
84
+ | ToolParamsStartPart
85
+ | ToolParamsDeltaPart
86
+ | ToolParamsEndPart
87
+ | ToolCallPart<any, any>
88
+ | ToolResultPart<any, any>
89
+ | FilePart
90
+ | DocumentSourcePart
91
+ | UrlSourcePart
92
+ | ResponseMetadataPart
93
+ | FinishPart
94
+ | ErrorPart
95
+
96
+ /**
97
+ * Encoded representation of all possible response content parts for serialization.
98
+ *
99
+ * @since 1.0.0
100
+ * @category Models
101
+ */
102
+ export type AnyPartEncoded =
103
+ | TextPartEncoded
104
+ | TextStartPartEncoded
105
+ | TextDeltaPartEncoded
106
+ | TextEndPartEncoded
107
+ | ReasoningPartEncoded
108
+ | ReasoningStartPartEncoded
109
+ | ReasoningDeltaPartEncoded
110
+ | ReasoningEndPartEncoded
111
+ | ToolParamsStartPartEncoded
112
+ | ToolParamsDeltaPartEncoded
113
+ | ToolParamsEndPartEncoded
114
+ | ToolCallPartEncoded
115
+ | ToolResultPartEncoded
116
+ | FilePartEncoded
117
+ | DocumentSourcePartEncoded
118
+ | UrlSourcePartEncoded
119
+ | ResponseMetadataPartEncoded
120
+ | FinishPartEncoded
121
+ | ErrorPartEncoded
122
+
123
+ /**
124
+ * Union type for all response parts with tool-specific typing.
125
+ *
126
+ * @since 1.0.0
127
+ * @category Models
128
+ */
129
+ export type AllParts<Tools extends Record<string, Tool.Any>> =
130
+ | TextPart
131
+ | TextStartPart
132
+ | TextDeltaPart
133
+ | TextEndPart
134
+ | ReasoningPart
135
+ | ReasoningStartPart
136
+ | ReasoningDeltaPart
137
+ | ReasoningEndPart
138
+ | ToolParamsStartPart
139
+ | ToolParamsDeltaPart
140
+ | ToolParamsEndPart
141
+ | ToolCallParts<Tools>
142
+ | ToolResultParts<Tools>
143
+ | FilePart
144
+ | DocumentSourcePart
145
+ | UrlSourcePart
146
+ | ResponseMetadataPart
147
+ | FinishPart
148
+ | ErrorPart
149
+
150
+ /**
151
+ * Encoded representation of all response parts for serialization.
152
+ *
153
+ * @since 1.0.0
154
+ * @category Models
155
+ */
156
+ export type AllPartsEncoded =
157
+ | TextPartEncoded
158
+ | TextStartPartEncoded
159
+ | TextDeltaPartEncoded
160
+ | TextEndPartEncoded
161
+ | ReasoningPartEncoded
162
+ | ReasoningStartPartEncoded
163
+ | ReasoningDeltaPartEncoded
164
+ | ReasoningEndPartEncoded
165
+ | ToolParamsStartPartEncoded
166
+ | ToolParamsDeltaPartEncoded
167
+ | ToolParamsEndPartEncoded
168
+ | ToolCallPartEncoded
169
+ | ToolResultPartEncoded
170
+ | FilePartEncoded
171
+ | DocumentSourcePartEncoded
172
+ | UrlSourcePartEncoded
173
+ | ResponseMetadataPartEncoded
174
+ | FinishPartEncoded
175
+ | ErrorPartEncoded
176
+
177
+ /**
178
+ * Creates a Schema for all response parts based on a toolkit.
179
+ *
180
+ * Generates a schema that includes all possible response parts, with tool call
181
+ * and tool result parts dynamically created based on the provided toolkit.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * import { Response, Tool, Toolkit } from "@effect/ai"
186
+ * import { Schema } from "effect"
187
+ *
188
+ * const myToolkit = Toolkit.make(
189
+ * Tool.make("GetWeather", {
190
+ * parameters: { city: Schema.String },
191
+ * success: Schema.Struct({ temperature: Schema.Number })
192
+ * })
193
+ * )
194
+ *
195
+ * const allPartsSchema = Response.AllParts(myToolkit)
196
+ * ```
197
+ *
198
+ * @since 1.0.0
199
+ * @category Schemas
200
+ */
201
+ export const AllParts = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
202
+ toolkit: T
203
+ ): Schema.Schema<
204
+ AllParts<Toolkit.Tools<T>>,
205
+ AllParts<Toolkit.Tools<T>>
206
+ > => {
207
+ const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
208
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
209
+ for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
210
+ toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
211
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
212
+ }
213
+ return Schema.Union(
214
+ TextPart,
215
+ TextStartPart,
216
+ TextDeltaPart,
217
+ TextEndPart,
218
+ ReasoningPart,
219
+ ReasoningStartPart,
220
+ ReasoningDeltaPart,
221
+ ReasoningEndPart,
222
+ ToolParamsStartPart,
223
+ ToolParamsDeltaPart,
224
+ ToolParamsEndPart,
225
+ FilePart,
226
+ DocumentSourcePart,
227
+ UrlSourcePart,
228
+ ResponseMetadataPart,
229
+ FinishPart,
230
+ ErrorPart,
231
+ ...toolCalls,
232
+ ...toolCallResults
233
+ ) as any
234
+ }
235
+
236
+ // =============================================================================
237
+ // Generate Parts
238
+ // =============================================================================
239
+
240
+ /**
241
+ * A type for representing non-streaming response parts with tool-specific
242
+ * typing.
243
+ *
244
+ * @template Tools - Record of tools with their schemas
245
+ *
246
+ * @since 1.0.0
247
+ * @category Models
248
+ */
249
+ export type Part<Tools extends Record<string, Tool.Any>> =
250
+ | TextPart
251
+ | ReasoningPart
252
+ | ToolCallParts<Tools>
253
+ | ToolResultParts<Tools>
254
+ | FilePart
255
+ | DocumentSourcePart
256
+ | UrlSourcePart
257
+ | ResponseMetadataPart
258
+ | FinishPart
259
+
260
+ /**
261
+ * Encoded representation of non-streaming response parts for serialization.
262
+ *
263
+ * @since 1.0.0
264
+ * @category Models
265
+ */
266
+ export type PartEncoded =
267
+ | TextPartEncoded
268
+ | ReasoningPartEncoded
269
+ | ReasoningDeltaPartEncoded
270
+ | ReasoningEndPartEncoded
271
+ | ToolCallPartEncoded
272
+ | ToolResultPartEncoded
273
+ | FilePartEncoded
274
+ | DocumentSourcePartEncoded
275
+ | UrlSourcePartEncoded
276
+ | ResponseMetadataPartEncoded
277
+ | FinishPartEncoded
278
+
279
+ /**
280
+ * Creates a Schema for non-streaming response parts based on a toolkit.
281
+ *
282
+ * @since 1.0.0
283
+ * @category Schemas
284
+ */
285
+ export const Part = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
286
+ toolkit: T
287
+ ): Schema.Schema<Part<Toolkit.Tools<T>>, PartEncoded> => {
288
+ const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
289
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
290
+ for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
291
+ toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
292
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
293
+ }
294
+ return Schema.Union(
295
+ TextPart,
296
+ ReasoningPart,
297
+ FilePart,
298
+ DocumentSourcePart,
299
+ UrlSourcePart,
300
+ ResponseMetadataPart,
301
+ FinishPart,
302
+ ...toolCalls,
303
+ ...toolCallResults
304
+ ) as any
305
+ }
306
+
307
+ // =============================================================================
308
+ // Stream Parts
309
+ // =============================================================================
310
+
311
+ /**
312
+ * A type for representing streaming response parts with tool-specific typing.
313
+ *
314
+ * @template Tools - Record of tools with their schemas
315
+ *
316
+ * @since 1.0.0
317
+ * @category Models
318
+ */
319
+ export type StreamPart<Tools extends Record<string, Tool.Any>> =
320
+ | TextStartPart
321
+ | TextDeltaPart
322
+ | TextEndPart
323
+ | ReasoningStartPart
324
+ | ReasoningDeltaPart
325
+ | ReasoningEndPart
326
+ | ToolParamsStartPart
327
+ | ToolParamsDeltaPart
328
+ | ToolParamsEndPart
329
+ | ToolCallParts<Tools>
330
+ | ToolResultParts<Tools>
331
+ | FilePart
332
+ | DocumentSourcePart
333
+ | UrlSourcePart
334
+ | ResponseMetadataPart
335
+ | FinishPart
336
+ | ErrorPart
337
+
338
+ /**
339
+ * Encoded representation of streaming response parts for serialization.
340
+ *
341
+ * @since 1.0.0
342
+ * @category Models
343
+ */
344
+ export type StreamPartEncoded =
345
+ | TextStartPartEncoded
346
+ | TextDeltaPartEncoded
347
+ | TextEndPartEncoded
348
+ | ReasoningStartPartEncoded
349
+ | ReasoningDeltaPartEncoded
350
+ | ReasoningEndPartEncoded
351
+ | ToolParamsStartPartEncoded
352
+ | ToolParamsDeltaPartEncoded
353
+ | ToolParamsEndPartEncoded
354
+ | ToolCallPartEncoded
355
+ | ToolResultPartEncoded
356
+ | FilePartEncoded
357
+ | DocumentSourcePartEncoded
358
+ | UrlSourcePartEncoded
359
+ | ResponseMetadataPartEncoded
360
+ | FinishPartEncoded
361
+ | ErrorPartEncoded
362
+
363
+ /**
364
+ * Creates a Schema for streaming response parts based on a toolkit.
365
+ *
366
+ * @since 1.0.0
367
+ * @category Schemas
368
+ */
369
+ export const StreamPart = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
370
+ toolkit: T
371
+ ): Schema.Schema<StreamPart<Toolkit.Tools<T>>, StreamPartEncoded> => {
372
+ const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
373
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
374
+ for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
375
+ toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
376
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
377
+ }
378
+ return Schema.Union(
379
+ TextStartPart,
380
+ TextDeltaPart,
381
+ TextEndPart,
382
+ ReasoningStartPart,
383
+ ReasoningDeltaPart,
384
+ ReasoningEndPart,
385
+ ToolParamsStartPart,
386
+ ToolParamsDeltaPart,
387
+ ToolParamsEndPart,
388
+ FilePart,
389
+ DocumentSourcePart,
390
+ UrlSourcePart,
391
+ ResponseMetadataPart,
392
+ FinishPart,
393
+ ErrorPart,
394
+ ...toolCalls,
395
+ ...toolCallResults
396
+ ) as any
397
+ }
398
+
399
+ // =============================================================================
400
+ // Utility Types
401
+ // =============================================================================
402
+
403
+ /**
404
+ * Utility type that extracts tool call parts from a set of tools.
405
+ *
406
+ * @template Tools - Record of tools with their schemas
407
+ *
408
+ * @since 1.0.0
409
+ * @category Utility Types
410
+ */
411
+ export type ToolCallParts<Tools extends Record<string, Tool.Any>> = {
412
+ [Name in keyof Tools]: Name extends string ? ToolCallPart<Name, Tool.ParametersSchema<Tools[Name]>["fields"]>
413
+ : never
414
+ }[keyof Tools]
415
+
416
+ /**
417
+ * Utility type that extracts tool result parts from a set of tools.
418
+ *
419
+ * @template Tools - Record of tools with their schemas
420
+ *
421
+ * @since 1.0.0
422
+ * @category Utility Types
423
+ */
424
+ export type ToolResultParts<Tools extends Record<string, Tool.Any>> = {
425
+ [Name in keyof Tools]: Name extends string ? ToolResultPart<Name, Tool.Success<Tools[Name]>>
426
+ : never
427
+ }[keyof Tools]
428
+
429
+ // =============================================================================
430
+ // Base Part
431
+ // =============================================================================
432
+
433
+ /**
434
+ * Schema for provider-specific metadata which can be attached to response parts.
435
+ *
436
+ * Provider-specific metadata is namespaced by provider and has the structure:
437
+ *
438
+ * ```
439
+ * {
440
+ * "<provider-specific-key>": {
441
+ * // Provider-specific metadata
442
+ * }
443
+ * }
444
+ * ```
445
+ *
446
+ * @since 1.0.0
447
+ * @category Schemas
448
+ */
449
+ export const ProviderMetadata = Schema.Record({
450
+ key: Schema.String,
451
+ value: Schema.UndefinedOr(Schema.Record({
452
+ key: Schema.String,
453
+ value: Schema.Unknown
454
+ }))
455
+ })
456
+
457
+ /**
458
+ * @since 1.0.0
459
+ * @category Models
460
+ */
461
+ export type ProviderMetadata = typeof ProviderMetadata.Type
462
+
463
+ /**
464
+ * Base interface for all response content parts.
465
+ *
466
+ * Provides common structure including type identifier and optional metadata.
467
+ *
468
+ * @template Type - String literal type for the part type
469
+ *
470
+ * @since 1.0.0
471
+ * @category Models
472
+ */
473
+ export interface BasePart<Type extends string, Metadata extends ProviderMetadata> {
474
+ readonly [PartTypeId]: PartTypeId
475
+ /**
476
+ * The type of this response part.
477
+ */
478
+ readonly type: Type
479
+ /**
480
+ * Optional provider-specific metadata for this part.
481
+ */
482
+ readonly metadata: Metadata
483
+ }
484
+
485
+ /**
486
+ * Base interface for encoded response content parts.
487
+ *
488
+ * @template Type - String literal type for the part type
489
+ *
490
+ * @since 1.0.0
491
+ * @category Models
492
+ */
493
+ export interface BasePartEncoded<Type extends string, Metadata extends ProviderMetadata> {
494
+ /**
495
+ * The type of this response part.
496
+ */
497
+ readonly type: Type
498
+ /**
499
+ * Optional provider-specific metadata for this part.
500
+ */
501
+ readonly metadata?: Metadata | undefined
502
+ }
503
+
504
+ /**
505
+ * Creates a new response content part of the specified type.
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * import { Response } from "@effect/ai"
510
+ *
511
+ * const textPart = Response.makePart("text", {
512
+ * text: "Hello, world!"
513
+ * })
514
+ *
515
+ * const toolCallPart = Response.makePart("tool-call", {
516
+ * id: "call_123",
517
+ * name: "get_weather",
518
+ * params: { city: "San Francisco" },
519
+ * providerExecuted: false
520
+ * })
521
+ * ```
522
+ *
523
+ * @since 1.0.0
524
+ * @category Constructors
525
+ */
526
+ export const makePart = <const Type extends AnyPart["type"]>(
527
+ /**
528
+ * The type of part to create.
529
+ */
530
+ type: Type,
531
+ /**
532
+ * Parameters specific to the part type being created.
533
+ */
534
+ params: Omit<Extract<AnyPart, { type: Type }>, PartTypeId | "type" | "metadata"> & {
535
+ /**
536
+ * Optional provider-specific metadata for this part.
537
+ */
538
+ readonly metadata?: Extract<AnyPart, { type: Type }>["metadata"] | undefined
539
+ }
540
+ ): Extract<AnyPart, { type: Type }> =>
541
+ (({
542
+ ...params,
543
+ [PartTypeId]: PartTypeId,
544
+ type,
545
+ metadata: params.metadata
546
+ }) as any)
547
+
548
+ // =============================================================================
549
+ // Text Part
550
+ // =============================================================================
551
+
552
+ /**
553
+ * Response part representing plain text content.
554
+ *
555
+ * @example
556
+ * ```ts
557
+ * import { Response } from "@effect/ai"
558
+ *
559
+ * const textPart: Response.TextPart = Response.makePart("text", {
560
+ * text: "The answer to your question is 42.",
561
+ * })
562
+ * ```
563
+ *
564
+ * @since 1.0.0
565
+ * @category Models
566
+ */
567
+ export interface TextPart extends BasePart<"text", TextPartMetadata> {
568
+ /**
569
+ * The text content.
570
+ */
571
+ readonly text: string
572
+ }
573
+
574
+ /**
575
+ * Encoded representation of text parts for serialization.
576
+ *
577
+ * @since 1.0.0
578
+ * @category Models
579
+ */
580
+ export interface TextPartEncoded extends BasePartEncoded<"text", TextPartMetadata> {
581
+ /**
582
+ * The text content.
583
+ */
584
+ readonly text: string
585
+ }
586
+
587
+ /**
588
+ * Represents provider-specific metadata that can be associated with a
589
+ * `TextPart` through module augmentation.
590
+ *
591
+ * @since 1.0.0
592
+ * @category ProviderOptions
593
+ */
594
+ export interface TextPartMetadata extends ProviderMetadata {}
595
+
596
+ /**
597
+ * Schema for validation and encoding of text parts.
598
+ *
599
+ * @since 1.0.0
600
+ * @category Schemas
601
+ */
602
+ export const TextPart: Schema.Schema<TextPart, TextPartEncoded> = Schema.Struct({
603
+ type: Schema.Literal("text"),
604
+ text: Schema.String,
605
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
606
+ }).pipe(
607
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
608
+ Schema.annotations({ identifier: "TextPart" })
609
+ )
610
+
611
+ // =============================================================================
612
+ // Text Start Part
613
+ // =============================================================================
614
+
615
+ /**
616
+ * Response part indicating the start of streaming text content.
617
+ *
618
+ * Marks the beginning of a text chunk with a unique identifier.
619
+ *
620
+ * @since 1.0.0
621
+ * @category Models
622
+ */
623
+ export interface TextStartPart extends BasePart<"text-start", TextStartPartMetadata> {
624
+ /**
625
+ * Unique identifier for this text chunk.
626
+ */
627
+ readonly id: string
628
+ }
629
+
630
+ /**
631
+ * Encoded representation of text start parts for serialization.
632
+ *
633
+ * @since 1.0.0
634
+ * @category Models
635
+ */
636
+ export interface TextStartPartEncoded extends BasePartEncoded<"text-start", TextStartPartMetadata> {
637
+ /**
638
+ * Unique identifier for this text chunk.
639
+ */
640
+ readonly id: string
641
+ }
642
+
643
+ /**
644
+ * Represents provider-specific metadata that can be associated with a
645
+ * `TextStartPart` through module augmentation.
646
+ *
647
+ * @since 1.0.0
648
+ * @category ProviderOptions
649
+ */
650
+ export interface TextStartPartMetadata extends ProviderMetadata {}
651
+
652
+ /**
653
+ * Schema for validation and encoding of text start parts.
654
+ *
655
+ * @since 1.0.0
656
+ * @category Schemas
657
+ */
658
+ export const TextStartPart: Schema.Schema<TextStartPart, TextStartPartEncoded> = Schema.Struct({
659
+ type: Schema.Literal("text-start"),
660
+ id: Schema.String,
661
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
662
+ }).pipe(
663
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
664
+ Schema.annotations({ identifier: "TextStartPart" })
665
+ )
666
+
667
+ // =============================================================================
668
+ // Text Delta Part
669
+ // =============================================================================
670
+
671
+ /**
672
+ * Response part containing incremental text content to be added to the existing
673
+ * text chunk with the same unique identifier.
674
+ *
675
+ * @since 1.0.0
676
+ * @category Models
677
+ */
678
+ export interface TextDeltaPart extends BasePart<"text-delta", TextDeltaPartMetadata> {
679
+ /**
680
+ * Unique identifier matching the corresponding text chunk.
681
+ */
682
+ readonly id: string
683
+ /**
684
+ * The incremental text content to add.
685
+ */
686
+ readonly delta: string
687
+ }
688
+
689
+ /**
690
+ * Encoded representation of text delta parts for serialization.
691
+ *
692
+ * @since 1.0.0
693
+ * @category Models
694
+ */
695
+ export interface TextDeltaPartEncoded extends BasePartEncoded<"text-delta", TextDeltaPartMetadata> {
696
+ /**
697
+ * Unique identifier matching the corresponding text chunk.
698
+ */
699
+ readonly id: string
700
+ /**
701
+ * The incremental text content to add.
702
+ */
703
+ readonly delta: string
704
+ }
705
+
706
+ /**
707
+ * Represents provider-specific metadata that can be associated with a
708
+ * `TextDeltaPart` through module augmentation.
709
+ *
710
+ * @since 1.0.0
711
+ * @category ProviderOptions
712
+ */
713
+ export interface TextDeltaPartMetadata extends ProviderMetadata {}
714
+
715
+ /**
716
+ * Schema for validation and encoding of text delta parts.
717
+ *
718
+ * @since 1.0.0
719
+ * @category Schemas
720
+ */
721
+ export const TextDeltaPart: Schema.Schema<TextDeltaPart, TextDeltaPartEncoded> = Schema.Struct({
722
+ type: Schema.Literal("text-delta"),
723
+ id: Schema.String,
724
+ delta: Schema.String,
725
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
726
+ }).pipe(
727
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
728
+ Schema.annotations({ identifier: "TextDeltaPart" })
729
+ )
730
+
731
+ // =============================================================================
732
+ // Text End Part
733
+ // =============================================================================
734
+
735
+ /**
736
+ * Response part indicating the end of streaming text content.
737
+ *
738
+ * Marks the completion of a text chunk.
739
+ *
740
+ * @since 1.0.0
741
+ * @category Models
742
+ */
743
+ export interface TextEndPart extends BasePart<"text-end", TextEndPartMetadata> {
744
+ /**
745
+ * Unique identifier matching the corresponding text chunk.
746
+ */
747
+ readonly id: string
748
+ }
749
+
750
+ /**
751
+ * Encoded representation of text end parts for serialization.
752
+ *
753
+ * @since 1.0.0
754
+ * @category Models
755
+ */
756
+ export interface TextEndPartEncoded extends BasePartEncoded<"text-end", TextEndPartMetadata> {
757
+ /**
758
+ * Unique identifier matching the corresponding text chunk.
759
+ */
760
+ readonly id: string
761
+ }
762
+
763
+ /**
764
+ * Represents provider-specific metadata that can be associated with a
765
+ * `TextEndPart` through module augmentation.
766
+ *
767
+ * @since 1.0.0
768
+ * @category ProviderOptions
769
+ */
770
+ export interface TextEndPartMetadata extends ProviderMetadata {}
771
+
772
+ /**
773
+ * Schema for validation and encoding of text end parts.
774
+ *
775
+ * @since 1.0.0
776
+ * @category Schemas
777
+ */
778
+ export const TextEndPart: Schema.Schema<TextEndPart, TextEndPartEncoded> = Schema.Struct({
779
+ type: Schema.Literal("text-end"),
780
+ id: Schema.String,
781
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
782
+ }).pipe(
783
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
784
+ Schema.annotations({ identifier: "TextEndPart" })
785
+ )
786
+
787
+ // =============================================================================
788
+ // Reasoning Part
789
+ // =============================================================================
790
+
791
+ /**
792
+ * Response part representing reasoning or chain-of-thought content.
793
+ *
794
+ * Contains the internal reasoning process or explanation from the large
795
+ * language model.
796
+ *
797
+ * @example
798
+ * ```ts
799
+ * import { Response } from "@effect/ai"
800
+ *
801
+ * const reasoningPart: Response.ReasoningPart = Response.makePart("reasoning", {
802
+ * text: "Let me think step by step: First I need to analyze the user's question...",
803
+ * })
804
+ * ```
805
+ *
806
+ * @since 1.0.0
807
+ * @category Models
808
+ */
809
+ export interface ReasoningPart extends BasePart<"reasoning", ReasoningPartMetadata> {
810
+ /**
811
+ * The reasoning or thought process text.
812
+ */
813
+ readonly text: string
814
+ }
815
+
816
+ /**
817
+ * Encoded representation of reasoning parts for serialization.
818
+ *
819
+ * @since 1.0.0
820
+ * @category Models
821
+ */
822
+ export interface ReasoningPartEncoded extends BasePartEncoded<"reasoning", ReasoningPartMetadata> {
823
+ /**
824
+ * The reasoning or thought process text.
825
+ */
826
+ readonly text: string
827
+ }
828
+
829
+ /**
830
+ * Represents provider-specific metadata that can be associated with a
831
+ * `ReasoningPart` through module augmentation.
832
+ *
833
+ * @since 1.0.0
834
+ * @category ProviderOptions
835
+ */
836
+ export interface ReasoningPartMetadata extends ProviderMetadata {}
837
+
838
+ /**
839
+ * Schema for validation and encoding of reasoning parts.
840
+ *
841
+ * @since 1.0.0
842
+ * @category Schemas
843
+ */
844
+ export const ReasoningPart: Schema.Schema<ReasoningPart, ReasoningPartEncoded> = Schema.Struct({
845
+ type: Schema.Literal("reasoning"),
846
+ text: Schema.String,
847
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
848
+ }).pipe(
849
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
850
+ Schema.annotations({ identifier: "ReasoningPart" })
851
+ )
852
+
853
+ // =============================================================================
854
+ // Reasoning Start Part
855
+ // =============================================================================
856
+
857
+ /**
858
+ * Response part indicating the start of streaming reasoning content.
859
+ *
860
+ * Marks the beginning of a reasoning chunk with a unique identifier.
861
+ *
862
+ * @since 1.0.0
863
+ * @category Models
864
+ */
865
+ export interface ReasoningStartPart extends BasePart<"reasoning-start", ReasoningStartPartMetadata> {
866
+ /**
867
+ * Unique identifier for this reasoning chunk.
868
+ */
869
+ readonly id: string
870
+ }
871
+
872
+ /**
873
+ * Encoded representation of reasoning start parts for serialization.
874
+ *
875
+ * @since 1.0.0
876
+ * @category Models
877
+ */
878
+ export interface ReasoningStartPartEncoded extends BasePartEncoded<"reasoning-start", ReasoningStartPartMetadata> {
879
+ /**
880
+ * Unique identifier for this reasoning stream.
881
+ */
882
+ readonly id: string
883
+ }
884
+
885
+ /**
886
+ * Represents provider-specific metadata that can be associated with a
887
+ * `ReasoningStartPart` through module augmentation.
888
+ *
889
+ * @since 1.0.0
890
+ * @category ProviderOptions
891
+ */
892
+ export interface ReasoningStartPartMetadata extends ProviderMetadata {}
893
+
894
+ /**
895
+ * Schema for validation and encoding of reasoning start parts.
896
+ *
897
+ * @since 1.0.0
898
+ * @category Schemas
899
+ */
900
+ export const ReasoningStartPart: Schema.Schema<ReasoningStartPart, ReasoningStartPartEncoded> = Schema.Struct({
901
+ type: Schema.Literal("reasoning-start"),
902
+ id: Schema.String,
903
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
904
+ }).pipe(
905
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
906
+ Schema.annotations({ identifier: "ReasoningStartPart" })
907
+ )
908
+
909
+ // =============================================================================
910
+ // Reasoning Delta Part
911
+ // =============================================================================
912
+
913
+ /**
914
+ * Response part containing incremental reasoning content to be added to the
915
+ * existing chunk of reasoning text with the same unique identifier.
916
+ *
917
+ * @since 1.0.0
918
+ * @category Models
919
+ */
920
+ export interface ReasoningDeltaPart extends BasePart<"reasoning-delta", ReasoningDeltaPartMetadata> {
921
+ /**
922
+ * Unique identifier matching the corresponding reasoning chunk.
923
+ */
924
+ readonly id: string
925
+ /**
926
+ * The incremental reasoning content to add.
927
+ */
928
+ readonly delta: string
929
+ }
930
+
931
+ /**
932
+ * Encoded representation of reasoning delta parts for serialization.
933
+ *
934
+ * @since 1.0.0
935
+ * @category Models
936
+ */
937
+ export interface ReasoningDeltaPartEncoded extends BasePartEncoded<"reasoning-delta", ReasoningDeltaPartMetadata> {
938
+ /**
939
+ * Unique identifier matching the corresponding reasoning chunk.
940
+ */
941
+ readonly id: string
942
+ /**
943
+ * The incremental reasoning content to add.
944
+ */
945
+ readonly delta: string
946
+ }
947
+
948
+ /**
949
+ * Represents provider-specific metadata that can be associated with a
950
+ * `ReasoningDeltaPart` through module augmentation.
951
+ *
952
+ * @since 1.0.0
953
+ * @category ProviderOptions
954
+ */
955
+ export interface ReasoningDeltaPartMetadata extends ProviderMetadata {}
956
+
957
+ /**
958
+ * Schema for validation and encoding of reasoning delta parts.
959
+ *
960
+ * @since 1.0.0
961
+ * @category Schemas
962
+ */
963
+ export const ReasoningDeltaPart: Schema.Schema<ReasoningDeltaPart, ReasoningDeltaPartEncoded> = Schema.Struct({
964
+ type: Schema.Literal("reasoning-delta"),
965
+ id: Schema.String,
966
+ delta: Schema.String,
967
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
968
+ }).pipe(
969
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
970
+ Schema.annotations({ identifier: "ReasoningDeltaPart" })
971
+ )
972
+
973
+ // =============================================================================
974
+ // Reasoning End Part
975
+ // =============================================================================
976
+
977
+ /**
978
+ * Response part indicating the end of streaming reasoning content.
979
+ *
980
+ * Marks the completion of a chunk of reasoning content.
981
+ *
982
+ * @since 1.0.0
983
+ * @category Models
984
+ */
985
+ export interface ReasoningEndPart extends BasePart<"reasoning-end", ReasoningEndPartMetadata> {
986
+ /**
987
+ * Unique identifier matching the corresponding reasoning chunk.
988
+ */
989
+ readonly id: string
990
+ }
991
+
992
+ /**
993
+ * Encoded representation of reasoning end parts for serialization.
994
+ *
995
+ * @since 1.0.0
996
+ * @category Models
997
+ */
998
+ export interface ReasoningEndPartEncoded extends BasePartEncoded<"reasoning-end", ReasoningEndPartMetadata> {
999
+ /**
1000
+ * Unique identifier matching the corresponding reasoning chunk.
1001
+ */
1002
+ readonly id: string
1003
+ }
1004
+
1005
+ /**
1006
+ * Represents provider-specific metadata that can be associated with a
1007
+ * `ReasoningEndPart` through module augmentation.
1008
+ *
1009
+ * @since 1.0.0
1010
+ * @category ProviderOptions
1011
+ */
1012
+ export interface ReasoningEndPartMetadata extends ProviderMetadata {}
1013
+
1014
+ /**
1015
+ * Schema for validation and encoding of reasoning end parts.
1016
+ *
1017
+ * @since 1.0.0
1018
+ * @category Schemas
1019
+ */
1020
+ export const ReasoningEndPart: Schema.Schema<ReasoningEndPart, ReasoningEndPartEncoded> = Schema.Struct({
1021
+ type: Schema.Literal("reasoning-end"),
1022
+ id: Schema.String,
1023
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1024
+ }).pipe(
1025
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1026
+ Schema.annotations({ identifier: "ReasoningEndPart" })
1027
+ )
1028
+
1029
+ // =============================================================================
1030
+ // Tool Params Start Part
1031
+ // =============================================================================
1032
+
1033
+ /**
1034
+ * Response part indicating the start of streaming tool parameters.
1035
+ *
1036
+ * Marks the beginning of tool parameter streaming with metadata about the tool
1037
+ * call.
1038
+ *
1039
+ * @since 1.0.0
1040
+ * @category Models
1041
+ */
1042
+ export interface ToolParamsStartPart extends BasePart<"tool-params-start", ToolParamsStartPartMetadata> {
1043
+ /**
1044
+ * Unique identifier for this tool parameter chunk.
1045
+ */
1046
+ readonly id: string
1047
+ /**
1048
+ * Name of the tool being called, which corresponds to the name of the tool
1049
+ * in the `Toolkit` included with the request.
1050
+ */
1051
+ readonly name: string
1052
+ /**
1053
+ * Optional provider-specific name for the tool, which can be useful when the
1054
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1055
+ * model are different.
1056
+ *
1057
+ * This is usually happens only with provider-defined tools which require a
1058
+ * user-space handler.
1059
+ */
1060
+ readonly providerName?: string | undefined
1061
+ /**
1062
+ * Whether the tool was executed by the provider (true) or framework (false).
1063
+ */
1064
+ readonly providerExecuted: boolean
1065
+ }
1066
+
1067
+ /**
1068
+ * Encoded representation of tool params start parts for serialization.
1069
+ *
1070
+ * @since 1.0.0
1071
+ * @category Models
1072
+ */
1073
+ export interface ToolParamsStartPartEncoded extends BasePartEncoded<"tool-params-start", ToolParamsStartPartMetadata> {
1074
+ /**
1075
+ * Unique identifier for this tool parameter chunk.
1076
+ */
1077
+ readonly id: string
1078
+ /**
1079
+ * Name of the tool being called, which corresponds to the name of the tool
1080
+ * in the `Toolkit` included with the request.
1081
+ */
1082
+ readonly name: string
1083
+ /**
1084
+ * Optional provider-specific name for the tool, which can be useful when the
1085
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1086
+ * model are different.
1087
+ *
1088
+ * This is usually happens only with provider-defined tools which require a
1089
+ * user-space handler.
1090
+ */
1091
+ readonly providerName?: string | undefined
1092
+ /**
1093
+ * Whether the tool was executed by the provider (true) or framework (false).
1094
+ */
1095
+ readonly providerExecuted?: boolean
1096
+ }
1097
+
1098
+ /**
1099
+ * Represents provider-specific metadata that can be associated with a
1100
+ * `ToolParamsStartPart` through module augmentation.
1101
+ *
1102
+ * @since 1.0.0
1103
+ * @category ProviderOptions
1104
+ */
1105
+ export interface ToolParamsStartPartMetadata extends ProviderMetadata {}
1106
+
1107
+ /**
1108
+ * Schema for validation and encoding of tool params start parts.
1109
+ *
1110
+ * @since 1.0.0
1111
+ * @category Schemas
1112
+ */
1113
+ export const ToolParamsStartPart: Schema.Schema<ToolParamsStartPart, ToolParamsStartPartEncoded> = Schema.Struct({
1114
+ type: Schema.Literal("tool-params-start"),
1115
+ id: Schema.String,
1116
+ name: Schema.String,
1117
+ providerName: Schema.optional(Schema.String),
1118
+ providerExecuted: Schema.optionalWith(Schema.Boolean, { default: constFalse }),
1119
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1120
+ }).pipe(
1121
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1122
+ Schema.annotations({ identifier: "ToolParamsStartPart" })
1123
+ )
1124
+
1125
+ // =============================================================================
1126
+ // Tool Params Delta Part
1127
+ // =============================================================================
1128
+
1129
+ /**
1130
+ * Response part containing incremental tool parameter content.
1131
+ *
1132
+ * Represents a chunk of tool parameters being streamed, containing the
1133
+ * incremental JSON content that forms the tool parameters.
1134
+ *
1135
+ * @since 1.0.0
1136
+ * @category Models
1137
+ */
1138
+ export interface ToolParamsDeltaPart extends BasePart<"tool-params-delta", ToolParamsDeltaPartMetadata> {
1139
+ /**
1140
+ * Unique identifier matching the corresponding tool parameter chunk.
1141
+ */
1142
+ readonly id: string
1143
+ /**
1144
+ * The incremental parameter content (typically JSON fragment) to add.
1145
+ */
1146
+ readonly delta: string
1147
+ }
1148
+
1149
+ /**
1150
+ * Encoded representation of tool params delta parts for serialization.
1151
+ *
1152
+ * @since 1.0.0
1153
+ * @category Models
1154
+ */
1155
+ export interface ToolParamsDeltaPartEncoded extends BasePartEncoded<"tool-params-delta", ToolParamsDeltaPartMetadata> {
1156
+ /**
1157
+ * Unique identifier matching the corresponding tool parameter chunk.
1158
+ */
1159
+ readonly id: string
1160
+ /**
1161
+ * The incremental parameter content (typically JSON fragment) to add.
1162
+ */
1163
+ readonly delta: string
1164
+ }
1165
+
1166
+ /**
1167
+ * Represents provider-specific metadata that can be associated with a
1168
+ * `ToolParamsDeltaPart` through module augmentation.
1169
+ *
1170
+ * @since 1.0.0
1171
+ * @category ProviderOptions
1172
+ */
1173
+ export interface ToolParamsDeltaPartMetadata extends ProviderMetadata {}
1174
+
1175
+ /**
1176
+ * Schema for validation and encoding of tool params delta parts.
1177
+ *
1178
+ * @since 1.0.0
1179
+ * @category Schemas
1180
+ */
1181
+ export const ToolParamsDeltaPart: Schema.Schema<ToolParamsDeltaPart, ToolParamsDeltaPartEncoded> = Schema.Struct({
1182
+ type: Schema.Literal("tool-params-delta"),
1183
+ id: Schema.String,
1184
+ delta: Schema.String,
1185
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1186
+ }).pipe(
1187
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1188
+ Schema.annotations({ identifier: "ToolParamsDeltaPart" })
1189
+ )
1190
+
1191
+ // =============================================================================
1192
+ // Tool Params End Part
1193
+ // =============================================================================
1194
+
1195
+ /**
1196
+ * Response part indicating the end of streaming tool parameters.
1197
+ *
1198
+ * Marks the completion of a tool parameter stream, indicating that all
1199
+ * parameter data has been sent and the tool call is ready to be executed.
1200
+ *
1201
+ * @since 1.0.0
1202
+ * @category Models
1203
+ */
1204
+ export interface ToolParamsEndPart extends BasePart<"tool-params-end", ToolParamsEndPartMetadata> {
1205
+ /**
1206
+ * Unique identifier matching the corresponding tool parameter chunk.
1207
+ */
1208
+ readonly id: string
1209
+ }
1210
+
1211
+ /**
1212
+ * Encoded representation of tool params end parts for serialization.
1213
+ *
1214
+ * @since 1.0.0
1215
+ * @category Models
1216
+ */
1217
+ export interface ToolParamsEndPartEncoded extends BasePartEncoded<"tool-params-end", ToolParamsEndPartMetadata> {
1218
+ /**
1219
+ * Unique identifier matching the corresponding tool parameter stream.
1220
+ */
1221
+ readonly id: string
1222
+ }
1223
+
1224
+ /**
1225
+ * Represents provider-specific metadata that can be associated with a
1226
+ * `ToolParamsEndPart` through module augmentation.
1227
+ *
1228
+ * @since 1.0.0
1229
+ * @category ProviderOptions
1230
+ */
1231
+ export interface ToolParamsEndPartMetadata extends ProviderMetadata {}
1232
+
1233
+ /**
1234
+ * Schema for validation and encoding of tool params end parts.
1235
+ *
1236
+ * @since 1.0.0
1237
+ * @category Schemas
1238
+ */
1239
+ export const ToolParamsEndPart: Schema.Schema<ToolParamsEndPart, ToolParamsEndPartEncoded> = Schema.Struct({
1240
+ type: Schema.Literal("tool-params-end"),
1241
+ id: Schema.String,
1242
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1243
+ }).pipe(
1244
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1245
+ Schema.annotations({ identifier: "ToolParamsEndPart" })
1246
+ )
1247
+
1248
+ // =============================================================================
1249
+ // Tool Call Part
1250
+ // =============================================================================
1251
+
1252
+ /**
1253
+ * Response part representing a tool call request.
1254
+ *
1255
+ * @example
1256
+ * ```ts
1257
+ * import { Response } from "@effect/ai"
1258
+ * import { Schema } from "effect"
1259
+ *
1260
+ * const weatherParams = Schema.Struct({
1261
+ * city: Schema.String,
1262
+ * units: Schema.optional(Schema.Literal("celsius", "fahrenheit"))
1263
+ * })
1264
+ *
1265
+ * const toolCallPart: Response.ToolCallPart<
1266
+ * "get_weather",
1267
+ * typeof weatherParams.fields
1268
+ * > = Response.makePart("tool-call", {
1269
+ * id: "call_123",
1270
+ * name: "get_weather",
1271
+ * params: { city: "San Francisco", units: "celsius" },
1272
+ * providerExecuted: false,
1273
+ * })
1274
+ * ```
1275
+ *
1276
+ * @template Name - String literal type for the tool name
1277
+ * @template Params - Schema fields type for the tool parameters
1278
+ *
1279
+ * @since 1.0.0
1280
+ * @category Models
1281
+ */
1282
+ export interface ToolCallPart<Name extends string, Params extends Schema.Struct.Fields>
1283
+ extends BasePart<"tool-call", ToolCallPartMetadata>
1284
+ {
1285
+ /**
1286
+ * Unique identifier for this tool call.
1287
+ */
1288
+ readonly id: string
1289
+ /**
1290
+ * Name of the tool being called, which corresponds to the name of the tool
1291
+ * in the `Toolkit` included with the request.
1292
+ */
1293
+ readonly name: Name
1294
+ /**
1295
+ * Parameters to pass to the tool.
1296
+ */
1297
+ readonly params: Schema.Struct.Type<Params>
1298
+ /**
1299
+ * Optional provider-specific name for the tool, which can be useful when the
1300
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1301
+ * model are different.
1302
+ *
1303
+ * This is usually happens only with provider-defined tools which require a
1304
+ * user-space handler.
1305
+ */
1306
+ readonly providerName?: string | undefined
1307
+ /**
1308
+ * Whether the tool was executed by the provider (true) or framework (false).
1309
+ */
1310
+ readonly providerExecuted: boolean
1311
+ }
1312
+
1313
+ /**
1314
+ * Encoded representation of tool call parts for serialization.
1315
+ *
1316
+ * @since 1.0.0
1317
+ * @category Models
1318
+ */
1319
+ export interface ToolCallPartEncoded extends BasePartEncoded<"tool-call", ToolCallPartMetadata> {
1320
+ /**
1321
+ * Unique identifier for this tool call.
1322
+ */
1323
+ readonly id: string
1324
+ /**
1325
+ * Name of the tool being called, which corresponds to the name of the tool
1326
+ * in the `Toolkit` included with the request.
1327
+ */
1328
+ readonly name: string
1329
+ /**
1330
+ * Parameters to pass to the tool.
1331
+ */
1332
+ readonly params: unknown
1333
+ /**
1334
+ * Optional provider-specific name for the tool, which can be useful when the
1335
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1336
+ * model are different.
1337
+ *
1338
+ * This is usually happens only with provider-defined tools which require a
1339
+ * user-space handler.
1340
+ */
1341
+ readonly providerName?: string | undefined
1342
+ /**
1343
+ * Whether the tool was executed by the provider (true) or framework (false).
1344
+ */
1345
+ readonly providerExecuted?: boolean | undefined
1346
+ }
1347
+
1348
+ /**
1349
+ * Represents provider-specific metadata that can be associated with a
1350
+ * `ToolCallPart` through module augmentation.
1351
+ *
1352
+ * @since 1.0.0
1353
+ * @category ProviderOptions
1354
+ */
1355
+ export interface ToolCallPartMetadata extends ProviderMetadata {}
1356
+
1357
+ /**
1358
+ * Creates a Schema for tool call parts with specific tool name and parameters.
1359
+ *
1360
+ * @since 1.0.0
1361
+ * @category Schemas
1362
+ */
1363
+ export const ToolCallPart = <const Name extends string, Params extends Schema.Struct.Fields>(
1364
+ /**
1365
+ * Name of the tool.
1366
+ */
1367
+ name: Name,
1368
+ /**
1369
+ * Schema for the tool parameters.
1370
+ */
1371
+ params: Schema.Struct<Params>
1372
+ ): Schema.Schema<ToolCallPart<Name, Params>, ToolCallPartEncoded> =>
1373
+ Schema.Struct({
1374
+ type: Schema.Literal("tool-call"),
1375
+ id: Schema.String,
1376
+ name: Schema.Literal(name),
1377
+ params,
1378
+ providerName: Schema.optional(Schema.String),
1379
+ providerExecuted: Schema.optionalWith(Schema.Boolean, { default: constFalse }),
1380
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1381
+ }).pipe(
1382
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1383
+ Schema.annotations({ identifier: "ToolCallPart" })
1384
+ ) as any
1385
+
1386
+ // =============================================================================
1387
+ // Tool Call Result Part
1388
+ // =============================================================================
1389
+
1390
+ /**
1391
+ * Response part representing the result of a tool call.
1392
+ *
1393
+ * @example
1394
+ * ```ts
1395
+ * import { Response } from "@effect/ai"
1396
+ *
1397
+ * interface WeatherData {
1398
+ * temperature: number
1399
+ * condition: string
1400
+ * humidity: number
1401
+ * }
1402
+ *
1403
+ * const toolResultPart: Response.ToolResultPart<
1404
+ * "get_weather",
1405
+ * WeatherData
1406
+ * > = Response.makePart("tool-result", {
1407
+ * id: "call_123",
1408
+ * name: "get_weather",
1409
+ * result: {
1410
+ * temperature: 22,
1411
+ * condition: "sunny",
1412
+ * humidity: 65
1413
+ * },
1414
+ * encodedResult: {
1415
+ * temperature: 22,
1416
+ * condition: "sunny",
1417
+ * humidity: 65
1418
+ * },
1419
+ * providerExecuted: false
1420
+ * })
1421
+ * ```
1422
+ *
1423
+ * @template Name - String literal type for the tool name
1424
+ * @template Result - Type of the tool result
1425
+ *
1426
+ * @since 1.0.0
1427
+ * @category Models
1428
+ */
1429
+ export interface ToolResultPart<Name extends string, Result> extends BasePart<"tool-result", ToolResultPartMetadata> {
1430
+ /**
1431
+ * Unique identifier matching the original tool call.
1432
+ */
1433
+ readonly id: string
1434
+ /**
1435
+ * Name of the tool being called, which corresponds to the name of the tool
1436
+ * in the `Toolkit` included with the request.
1437
+ */
1438
+ readonly name: Name
1439
+ /**
1440
+ * The decoded result returned by the tool execution.
1441
+ */
1442
+ readonly result: Result
1443
+ /**
1444
+ * The encoded result for serialization purposes.
1445
+ */
1446
+ readonly encodedResult: unknown
1447
+ /**
1448
+ * Optional provider-specific name for the tool, which can be useful when the
1449
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1450
+ * model are different.
1451
+ *
1452
+ * This is usually happens only with provider-defined tools which require a
1453
+ * user-space handler.
1454
+ */
1455
+ readonly providerName?: string | undefined
1456
+ /**
1457
+ * Whether the tool was executed by the provider (true) or framework (false).
1458
+ */
1459
+ readonly providerExecuted: boolean
1460
+ }
1461
+
1462
+ /**
1463
+ * Encoded representation of tool result parts for serialization.
1464
+ *
1465
+ * @since 1.0.0
1466
+ * @category Models
1467
+ */
1468
+ export interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", ToolResultPartMetadata> {
1469
+ /**
1470
+ * Unique identifier matching the original tool call.
1471
+ */
1472
+ readonly id: string
1473
+ /**
1474
+ * Name of the tool being called, which corresponds to the name of the tool
1475
+ * in the `Toolkit` included with the request.
1476
+ */
1477
+ readonly name: string
1478
+ /**
1479
+ * The result returned by the tool execution.
1480
+ */
1481
+ readonly result: unknown
1482
+ /**
1483
+ * Optional provider-specific name for the tool, which can be useful when the
1484
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1485
+ * model are different.
1486
+ *
1487
+ * This is usually happens only with provider-defined tools which require a
1488
+ * user-space handler.
1489
+ */
1490
+ readonly providerName?: string | undefined
1491
+ /**
1492
+ * Whether the tool was executed by the provider (true) or framework (false).
1493
+ */
1494
+ readonly providerExecuted?: boolean | undefined
1495
+ }
1496
+
1497
+ /**
1498
+ * Represents provider-specific metadata that can be associated with a
1499
+ * `ToolResultPart` through module augmentation.
1500
+ *
1501
+ * @since 1.0.0
1502
+ * @category ProviderOptions
1503
+ */
1504
+ export interface ToolResultPartMetadata extends ProviderMetadata {}
1505
+
1506
+ /**
1507
+ * Creates a Schema for tool result parts with specific tool name and result type.
1508
+ *
1509
+ * @since 1.0.0
1510
+ * @category Schemas
1511
+ */
1512
+ export const ToolResultPart = <const Name extends string, Result extends Schema.Schema.Any>(
1513
+ /**
1514
+ * Name of the tool.
1515
+ */
1516
+ name: Name,
1517
+ /**
1518
+ * Schema for the tool result.
1519
+ */
1520
+ result: Result
1521
+ ): Schema.Schema<
1522
+ ToolResultPart<Name, Schema.Schema.Type<Result>>,
1523
+ ToolResultPartEncoded
1524
+ > => {
1525
+ const Base = Schema.Struct({
1526
+ id: Schema.String,
1527
+ type: Schema.Literal("tool-result"),
1528
+ providerName: Schema.optional(Schema.String)
1529
+ })
1530
+ const Encoded = Schema.Struct({
1531
+ ...Base.fields,
1532
+ name: Schema.String,
1533
+ result: Schema.encodedSchema(result),
1534
+ providerExecuted: Schema.optional(Schema.Boolean),
1535
+ metadata: Schema.optional(ProviderMetadata)
1536
+ })
1537
+ const Decoded = Schema.Struct({
1538
+ ...Base.fields,
1539
+ [PartTypeId]: Schema.Literal(PartTypeId),
1540
+ name: Schema.Literal(name),
1541
+ result: Schema.typeSchema(result),
1542
+ encodedResult: Schema.encodedSchema(result),
1543
+ providerExecuted: Schema.Boolean,
1544
+ metadata: ProviderMetadata
1545
+ })
1546
+ const decodeParams = ParseResult.decode<any, any, never>(result as any)
1547
+ const encodeParams = ParseResult.encode<any, any, never>(result as any)
1548
+ return Schema.transformOrFail(
1549
+ Encoded,
1550
+ Decoded,
1551
+ {
1552
+ strict: true,
1553
+ decode: Effect.fnUntraced(function*(encoded) {
1554
+ const decoded = yield* decodeParams(encoded.result)
1555
+ const providerExecuted = encoded.providerExecuted ?? false
1556
+ return {
1557
+ ...encoded,
1558
+ [PartTypeId]: PartTypeId,
1559
+ name: encoded.name as Name,
1560
+ result: decoded,
1561
+ encodedResult: encoded.result,
1562
+ metadata: encoded.metadata ?? {},
1563
+ providerExecuted
1564
+ } as const
1565
+ }),
1566
+ encode: Effect.fnUntraced(function*(decoded) {
1567
+ const encoded = yield* encodeParams(decoded.result)
1568
+ return {
1569
+ id: decoded.id,
1570
+ type: decoded.type,
1571
+ name: decoded.name,
1572
+ result: encoded,
1573
+ ...(decoded.metadata ? { metadata: decoded.metadata } : {}),
1574
+ ...(decoded.providerName ? { providerName: decoded.providerName } : {}),
1575
+ ...(decoded.providerExecuted ? { providerExecuted: true } : {})
1576
+ }
1577
+ })
1578
+ }
1579
+ ).annotations({ identifier: `ToolResultPart(${name})` })
1580
+ }
1581
+
1582
+ // =============================================================================
1583
+ // File Part
1584
+ // =============================================================================
1585
+
1586
+ /**
1587
+ * Response part representing a file attachment.
1588
+ *
1589
+ * Supports various file types including images, documents, and binary data.
1590
+ *
1591
+ * @example
1592
+ * ```ts
1593
+ * import { Response } from "@effect/ai"
1594
+ *
1595
+ * const imagePart: Response.FilePart = Response.makePart("file", {
1596
+ * mediaType: "image/jpeg",
1597
+ * data: new Uint8Array([1, 2, 3]),
1598
+ * })
1599
+ * ```
1600
+ *
1601
+ * @since 1.0.0
1602
+ * @category Models
1603
+ */
1604
+ export interface FilePart extends BasePart<"file", FilePartMetadata> {
1605
+ /**
1606
+ * MIME type of the file (e.g., "image/jpeg", "application/pdf").
1607
+ */
1608
+ readonly mediaType: string
1609
+ /**
1610
+ * File data as a byte array.
1611
+ */
1612
+ readonly data: Uint8Array
1613
+ }
1614
+
1615
+ /**
1616
+ * Encoded representation of file parts for serialization.
1617
+ *
1618
+ * @since 1.0.0
1619
+ * @category Models
1620
+ */
1621
+ export interface FilePartEncoded extends BasePartEncoded<"file", FilePartMetadata> {
1622
+ /**
1623
+ * MIME type of the file (e.g., "image/jpeg", "application/pdf").
1624
+ */
1625
+ readonly mediaType: string
1626
+ /**
1627
+ * File data as a base64 string.
1628
+ */
1629
+ readonly data: string
1630
+ }
1631
+
1632
+ /**
1633
+ * Represents provider-specific metadata that can be associated with a
1634
+ * `FilePart` through module augmentation.
1635
+ *
1636
+ * @since 1.0.0
1637
+ * @category ProviderOptions
1638
+ */
1639
+ export interface FilePartMetadata extends ProviderMetadata {}
1640
+
1641
+ /**
1642
+ * Schema for validation and encoding of file parts.
1643
+ *
1644
+ * @since 1.0.0
1645
+ * @category Schemas
1646
+ */
1647
+ export const FilePart: Schema.Schema<FilePart, FilePartEncoded> = Schema.Struct({
1648
+ type: Schema.Literal("file"),
1649
+ mediaType: Schema.String,
1650
+ data: Schema.Uint8ArrayFromBase64,
1651
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1652
+ }).pipe(
1653
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1654
+ Schema.annotations({ identifier: "FilePart" })
1655
+ )
1656
+
1657
+ // =============================================================================
1658
+ // Document Source Part
1659
+ // =============================================================================
1660
+
1661
+ /**
1662
+ * Response part representing a document source reference.
1663
+ *
1664
+ * Used to reference documents that were used in generating the response.
1665
+ *
1666
+ * @since 1.0.0
1667
+ * @category Models
1668
+ */
1669
+ export interface DocumentSourcePart extends BasePart<"source", DocumentSourcePartMetadata> {
1670
+ /**
1671
+ * Type discriminator for document sources.
1672
+ */
1673
+ readonly sourceType: "document"
1674
+ /**
1675
+ * Unique identifier for the document.
1676
+ */
1677
+ readonly id: string
1678
+ /**
1679
+ * MIME type of the document.
1680
+ */
1681
+ readonly mediaType: string
1682
+ /**
1683
+ * Display title of the document.
1684
+ */
1685
+ readonly title: string
1686
+ /**
1687
+ * Optional filename of the document.
1688
+ */
1689
+ readonly fileName?: string
1690
+ }
1691
+
1692
+ /**
1693
+ * Encoded representation of document source parts for serialization.
1694
+ *
1695
+ * @since 1.0.0
1696
+ * @category Models
1697
+ */
1698
+ export interface DocumentSourcePartEncoded extends BasePartEncoded<"source", DocumentSourcePartMetadata> {
1699
+ /**
1700
+ * Type discriminator for document sources.
1701
+ */
1702
+ readonly sourceType: "document"
1703
+ /**
1704
+ * Unique identifier for the document.
1705
+ */
1706
+ readonly id: string
1707
+ /**
1708
+ * MIME type of the document.
1709
+ */
1710
+ readonly mediaType: string
1711
+ /**
1712
+ * Display title of the document.
1713
+ */
1714
+ readonly title: string
1715
+ /**
1716
+ * Optional filename of the document.
1717
+ */
1718
+ readonly fileName?: string
1719
+ }
1720
+
1721
+ /**
1722
+ * Represents provider-specific metadata that can be associated with a
1723
+ * `DocumentSourcePart` through module augmentation.
1724
+ *
1725
+ * @since 1.0.0
1726
+ * @category ProviderOptions
1727
+ */
1728
+ export interface DocumentSourcePartMetadata extends ProviderMetadata {}
1729
+
1730
+ /**
1731
+ * Schema for validation and encoding of document source parts.
1732
+ *
1733
+ * @since 1.0.0
1734
+ * @category Schemas
1735
+ */
1736
+ export const DocumentSourcePart: Schema.Schema<DocumentSourcePart, DocumentSourcePartEncoded> = Schema.Struct({
1737
+ type: Schema.Literal("source"),
1738
+ sourceType: Schema.Literal("document"),
1739
+ id: Schema.String,
1740
+ mediaType: Schema.String,
1741
+ title: Schema.String,
1742
+ fileName: Schema.optional(Schema.String),
1743
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1744
+ }).pipe(
1745
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1746
+ Schema.annotations({ identifier: "DocumentSourcePart" })
1747
+ )
1748
+
1749
+ // =============================================================================
1750
+ // Url Source Part
1751
+ // =============================================================================
1752
+
1753
+ /**
1754
+ * Response part representing a URL source reference.
1755
+ *
1756
+ * Used to reference web URLs that were used in generating the response.
1757
+ *
1758
+ * @since 1.0.0
1759
+ * @category Models
1760
+ */
1761
+ export interface UrlSourcePart extends BasePart<"source", UrlSourcePartMetadata> {
1762
+ /**
1763
+ * Type discriminator for URL sources.
1764
+ */
1765
+ readonly sourceType: "url"
1766
+ /**
1767
+ * Unique identifier for the URL.
1768
+ */
1769
+ readonly id: string
1770
+ /**
1771
+ * The URL that was referenced.
1772
+ */
1773
+ readonly url: URL
1774
+ /**
1775
+ * Display title of the URL content.
1776
+ */
1777
+ readonly title: string
1778
+ }
1779
+
1780
+ /**
1781
+ * Encoded representation of URL source parts for serialization.
1782
+ *
1783
+ * @since 1.0.0
1784
+ * @category Models
1785
+ */
1786
+ export interface UrlSourcePartEncoded extends BasePartEncoded<"source", UrlSourcePartMetadata> {
1787
+ /**
1788
+ * Type discriminator for URL sources.
1789
+ */
1790
+ readonly sourceType: "url"
1791
+ /**
1792
+ * Unique identifier for the URL.
1793
+ */
1794
+ readonly id: string
1795
+ /**
1796
+ * The URL that was referenced as a string.
1797
+ */
1798
+ readonly url: string
1799
+ /**
1800
+ * Display title of the URL content.
1801
+ */
1802
+ readonly title: string
1803
+ }
1804
+
1805
+ /**
1806
+ * Represents provider-specific metadata that can be associated with a
1807
+ * `UrlSourcePart` through module augmentation.
1808
+ *
1809
+ * @since 1.0.0
1810
+ * @category ProviderOptions
1811
+ */
1812
+ export interface UrlSourcePartMetadata extends ProviderMetadata {}
1813
+
1814
+ /**
1815
+ * Schema for validation and encoding of url source parts.
1816
+ *
1817
+ * @since 1.0.0
1818
+ * @category Schemas
1819
+ */
1820
+ export const UrlSourcePart: Schema.Schema<UrlSourcePart, UrlSourcePartEncoded> = Schema.Struct({
1821
+ type: Schema.Literal("source"),
1822
+ sourceType: Schema.Literal("url"),
1823
+ id: Schema.String,
1824
+ url: Schema.URL,
1825
+ title: Schema.String,
1826
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1827
+ }).pipe(
1828
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1829
+ Schema.annotations({ identifier: "UrlSourcePart" })
1830
+ )
1831
+
1832
+ // =============================================================================
1833
+ // Response Metadata Part
1834
+ // =============================================================================
1835
+
1836
+ /**
1837
+ * Response part containing metadata about the large language model response.
1838
+ *
1839
+ * @example
1840
+ * ```ts
1841
+ * import { Response } from "@effect/ai"
1842
+ * import { Option, DateTime } from "effect"
1843
+ *
1844
+ * const metadataPart: Response.ResponseMetadataPart = Response.makePart("response-metadata", {
1845
+ * id: Option.some("resp_123"),
1846
+ * modelId: Option.some("gpt-4"),
1847
+ * timestamp: Option.some(DateTime.unsafeNow())
1848
+ * })
1849
+ * ```
1850
+ *
1851
+ * @since 1.0.0
1852
+ * @category Models
1853
+ */
1854
+ export interface ResponseMetadataPart extends BasePart<"response-metadata", ResponseMetadataPartMetadata> {
1855
+ /**
1856
+ * Optional unique identifier for this specific response.
1857
+ */
1858
+ readonly id: Option.Option<string>
1859
+ /**
1860
+ * Optional identifier of the AI model that generated the response.
1861
+ */
1862
+ readonly modelId: Option.Option<string>
1863
+ /**
1864
+ * Optional timestamp when the response was generated.
1865
+ */
1866
+ readonly timestamp: Option.Option<DateTime.Utc>
1867
+ }
1868
+
1869
+ /**
1870
+ * Encoded representation of response metadata parts for serialization.
1871
+ *
1872
+ * @since 1.0.0
1873
+ * @category Models
1874
+ */
1875
+ export interface ResponseMetadataPartEncoded
1876
+ extends BasePartEncoded<"response-metadata", ResponseMetadataPartMetadata>
1877
+ {
1878
+ /**
1879
+ * Optional unique identifier for this specific response.
1880
+ */
1881
+ readonly id?: string | undefined
1882
+ /**
1883
+ * Optional identifier of the AI model that generated the response.
1884
+ */
1885
+ readonly modelId?: string | undefined
1886
+ /**
1887
+ * Optional timestamp when the response was generated.
1888
+ */
1889
+ readonly timestamp?: string | undefined
1890
+ }
1891
+
1892
+ /**
1893
+ * Represents provider-specific metadata that can be associated with a
1894
+ * `ResponseMetadataPart` through module augmentation.
1895
+ *
1896
+ * @since 1.0.0
1897
+ * @category ProviderOptions
1898
+ */
1899
+ export interface ResponseMetadataPartMetadata extends ProviderMetadata {}
1900
+
1901
+ /**
1902
+ * Schema for validation and encoding of response metadata parts.
1903
+ *
1904
+ * @since 1.0.0
1905
+ * @category Schemas
1906
+ */
1907
+ export const ResponseMetadataPart: Schema.Schema<ResponseMetadataPart, ResponseMetadataPartEncoded> = Schema.Struct({
1908
+ type: Schema.Literal("response-metadata"),
1909
+ id: Schema.optionalWith(Schema.String, { as: "Option" }),
1910
+ modelId: Schema.optionalWith(Schema.String, { as: "Option" }),
1911
+ timestamp: Schema.optionalWith(Schema.DateTimeUtc, { as: "Option" }),
1912
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
1913
+ }).pipe(
1914
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
1915
+ Schema.annotations({ identifier: "ResponseMetadataPart" })
1916
+ )
1917
+
1918
+ // =============================================================================
1919
+ // Finish Part
1920
+ // =============================================================================
1921
+
1922
+ /**
1923
+ * Represents the reason why a model finished generation of a response.
1924
+ *
1925
+ * Possible finish reasons:
1926
+ * - `"stop"`: The model generated a stop sequence.
1927
+ * - `"length"`: The model exceeded its token budget.
1928
+ * - `"content-filter"`: The model generated content which violated a content filter.
1929
+ * - `"tool-calls"`: The model triggered a tool call.
1930
+ * - `"error"`: The model encountered an error.
1931
+ * - `"pause"`: The model requested to pause execution.
1932
+ * - `"other"`: The model stopped for a reason not supported by this protocol.
1933
+ * - `"unknown"`: The model did not specify a finish reason.
1934
+ *
1935
+ * @since 1.0.0
1936
+ * @category Models
1937
+ */
1938
+ export const FinishReason: Schema.Literal<[
1939
+ "stop",
1940
+ "length",
1941
+ "content-filter",
1942
+ "tool-calls",
1943
+ "error",
1944
+ "pause",
1945
+ "other",
1946
+ "unknown"
1947
+ ]> = Schema.Literal(
1948
+ "stop",
1949
+ "length",
1950
+ "content-filter",
1951
+ "tool-calls",
1952
+ "error",
1953
+ "pause",
1954
+ "other",
1955
+ "unknown"
1956
+ )
1957
+
1958
+ /**
1959
+ * @since 1.0.0
1960
+ * @category Models
1961
+ */
1962
+ export type FinishReason = typeof FinishReason.Type
1963
+
1964
+ /**
1965
+ * Represents usage information for a request to a large language model provider.
1966
+ *
1967
+ * If the model provider returns additional usage information than what is
1968
+ * specified here, you can generally find that information under the provider
1969
+ * metadata of the finish part of the response.
1970
+ *
1971
+ * @since 1.0.0
1972
+ * @category Models
1973
+ */
1974
+ export class Usage extends Schema.Class<Usage>("@effect/ai/AiResponse/Usage")({
1975
+ /**
1976
+ * The number of tokens sent in the request to the model.
1977
+ */
1978
+ inputTokens: Schema.UndefinedOr(Schema.Number),
1979
+ /**
1980
+ * The number of tokens that the model generated for the request.
1981
+ */
1982
+ outputTokens: Schema.UndefinedOr(Schema.Number),
1983
+ /**
1984
+ * The total of number of input tokens and output tokens as reported by the
1985
+ * large language model provider.
1986
+ *
1987
+ * **NOTE**: This value may differ from the sum of `inputTokens` and
1988
+ * `outputTokens` due to inclusion of reasoning tokens or other
1989
+ * provider-specific overhead.
1990
+ */
1991
+ totalTokens: Schema.UndefinedOr(Schema.Number),
1992
+ /**
1993
+ * The number of reasoning tokens that the model used to generate the output
1994
+ * for the request.
1995
+ */
1996
+ reasoningTokens: Schema.optional(Schema.Number),
1997
+ /**
1998
+ * The number of input tokens read from the prompt cache for the request.
1999
+ */
2000
+ cachedInputTokens: Schema.optional(Schema.Number)
2001
+ }) {}
2002
+
2003
+ /**
2004
+ * Response part indicating the completion of a response generation.
2005
+ *
2006
+ * @example
2007
+ * ```ts
2008
+ * import { Response } from "@effect/ai"
2009
+ *
2010
+ * const finishPart: Response.FinishPart = Response.makePart("finish", {
2011
+ * reason: "stop",
2012
+ * usage: {
2013
+ * inputTokens: 50,
2014
+ * outputTokens: 25,
2015
+ * totalTokens: 75
2016
+ * }
2017
+ * })
2018
+ * ```
2019
+ *
2020
+ * @since 1.0.0
2021
+ * @category Models
2022
+ */
2023
+ export interface FinishPart extends BasePart<"finish", FinishPartMetadata> {
2024
+ /**
2025
+ * The reason why the model finished generating the response.
2026
+ */
2027
+ readonly reason: FinishReason
2028
+ /**
2029
+ * Token usage statistics for the request.
2030
+ */
2031
+ readonly usage: Usage
2032
+ }
2033
+
2034
+ /**
2035
+ * Encoded representation of finish parts for serialization.
2036
+ *
2037
+ * @since 1.0.0
2038
+ * @category Models
2039
+ */
2040
+ export interface FinishPartEncoded extends BasePartEncoded<"finish", FinishPartMetadata> {
2041
+ /**
2042
+ * The reason why the model finished generating the response.
2043
+ */
2044
+ readonly reason: typeof FinishReason.Encoded
2045
+ /**
2046
+ * Token usage statistics for the request.
2047
+ */
2048
+ readonly usage: typeof Usage.Encoded
2049
+ }
2050
+
2051
+ /**
2052
+ * Represents provider-specific metadata that can be associated with a
2053
+ * `FinishPart` through module augmentation.
2054
+ *
2055
+ * @since 1.0.0
2056
+ * @category ProviderOptions
2057
+ */
2058
+ export interface FinishPartMetadata extends ProviderMetadata {}
2059
+
2060
+ /**
2061
+ * Schema for validation and encoding of finish parts.
2062
+ *
2063
+ * @since 1.0.0
2064
+ * @category Schemas
2065
+ */
2066
+ export const FinishPart: Schema.Schema<FinishPart, FinishPartEncoded> = Schema.Struct({
2067
+ type: Schema.Literal("finish"),
2068
+ reason: FinishReason,
2069
+ usage: Usage,
2070
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
2071
+ }).pipe(
2072
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
2073
+ Schema.annotations({ identifier: "FinishPart" })
2074
+ )
2075
+
2076
+ // =============================================================================
2077
+ // Error Part
2078
+ // =============================================================================
2079
+
2080
+ /**
2081
+ * Response part indicating that an error occurred generating the response.
2082
+ *
2083
+ * @example
2084
+ * ```ts
2085
+ * import { Response } from "@effect/ai"
2086
+ *
2087
+ * const errorPart: Response.ErrorPart = Response.makePart("error", {
2088
+ * error: new Error("boom")
2089
+ * })
2090
+ * ```
2091
+ *
2092
+ * @since 1.0.0
2093
+ * @category Models
2094
+ */
2095
+ export interface ErrorPart extends BasePart<"error", ErrorPartMetadata> {
2096
+ readonly error: unknown
2097
+ }
2098
+
2099
+ /**
2100
+ * Encoded representation of error parts for serialization.
2101
+ *
2102
+ * @since 1.0.0
2103
+ * @category Models
2104
+ */
2105
+ export interface ErrorPartEncoded extends BasePartEncoded<"error", ErrorPartMetadata> {
2106
+ readonly error: unknown
2107
+ }
2108
+
2109
+ /**
2110
+ * Represents provider-specific metadata that can be associated with a
2111
+ * `ErrorPart` through module augmentation.
2112
+ *
2113
+ * @since 1.0.0
2114
+ * @category ProviderOptions
2115
+ */
2116
+ export interface ErrorPartMetadata extends ProviderMetadata {}
2117
+
2118
+ /**
2119
+ * Schema for validation and encoding of error parts.
2120
+ *
2121
+ * @since 1.0.0
2122
+ * @category Schemas
2123
+ */
2124
+ export const ErrorPart: Schema.Schema<ErrorPart, ErrorPartEncoded> = Schema.Struct({
2125
+ type: Schema.Literal("error"),
2126
+ error: Schema.Unknown,
2127
+ metadata: Schema.optionalWith(ProviderMetadata, { default: constEmptyObject })
2128
+ }).pipe(
2129
+ Schema.attachPropertySignature(PartTypeId, PartTypeId),
2130
+ Schema.annotations({ identifier: "ErrorPart" })
2131
+ )