@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
package/src/AiResponse.ts DELETED
@@ -1,986 +0,0 @@
1
- /**
2
- * @since 1.0.0
3
- */
4
- import type * as Context from "effect/Context"
5
- import * as Effect from "effect/Effect"
6
- import { dual } from "effect/Function"
7
- import * as Option from "effect/Option"
8
- import * as Predicate from "effect/Predicate"
9
- import * as Schema from "effect/Schema"
10
- import { AiError } from "./AiError.js"
11
- import type * as AiTool from "./AiTool.js"
12
- import * as InternalCommon from "./internal/common.js"
13
-
14
- const constDisableValidation = { disableValidation: true }
15
-
16
- /**
17
- * @since 1.0.0
18
- * @category Type Ids
19
- */
20
- export const TypeId = Symbol.for("@effect/ai/AiResponse")
21
-
22
- /**
23
- * @since 1.0.0
24
- * @category Type Ids
25
- */
26
- export type TypeId = typeof TypeId
27
-
28
- /**
29
- * Represents a response received from a large language model.
30
- *
31
- * @since 1.0.0
32
- * @category Models
33
- */
34
- export class AiResponse extends Schema.Class<AiResponse>(
35
- "@effect/ai/AiResponse"
36
- )({
37
- /**
38
- * The parts of the response.
39
- */
40
- parts: Schema.Array(Schema.suspend(() => Part))
41
- }) {
42
- /**
43
- * @since 1.0.0
44
- */
45
- readonly [TypeId]: TypeId = TypeId
46
-
47
- /**
48
- * Returns the generated text content of the response.
49
- */
50
- get text(): string {
51
- let text = ""
52
- let found = false
53
- for (const part of this.parts) {
54
- if (part._tag === "TextPart") {
55
- text += found ? "\n\n" + part.text : part.text
56
- found = true
57
- }
58
- }
59
- return text
60
- }
61
-
62
- /**
63
- * Returns the finish reason for the response, or `"unknown"` if the finish
64
- * reason is not known.
65
- */
66
- get finishReason(): FinishReason {
67
- const finishPart = this.parts.find((part) => part._tag === "FinishPart")
68
- return finishPart?.reason ?? "unknown"
69
- }
70
-
71
- /**
72
- * Returns all tool calls contained within the response.
73
- */
74
- get toolCalls(): ReadonlyArray<ToolCallPart> {
75
- return this.parts.filter((part) => part._tag === "ToolCallPart")
76
- }
77
-
78
- /**
79
- * Attempts to retrieve provider-specific response metadata.
80
- */
81
- getProviderMetadata<I, S>(tag: Context.Tag<I, S>): Option.Option<S> {
82
- const finishPart = this.parts.find((part) => part._tag === "FinishPart")
83
- return Option.fromNullable(finishPart?.providerMetadata[tag.key] as S)
84
- }
85
- }
86
-
87
- /**
88
- * @since 1.0.0
89
- * @category Models
90
- */
91
- export const FromJson: Schema.transform<
92
- Schema.SchemaClass<unknown, string, never>,
93
- typeof AiResponse
94
- > = Schema.parseJson(AiResponse)
95
-
96
- /**
97
- * @since 1.0.0
98
- * @category Type Ids
99
- */
100
- export const StructuredResponseTypeId = Symbol.for("@effect/ai/AiResponse/StructuredResponse")
101
-
102
- /**
103
- * @since 1.0.0
104
- * @category Type Ids
105
- */
106
- export type StructuredResponseTypeId = typeof StructuredResponseTypeId
107
-
108
- /**
109
- * Represents a response generated by a large language model that includes
110
- * structured output.
111
- *
112
- * @since 1.0.0
113
- * @category Models
114
- */
115
- export class WithStructuredOutput<A> extends AiResponse {
116
- /**
117
- * @since 1.0.0
118
- */
119
- readonly [StructuredResponseTypeId]: StructuredResponseTypeId = StructuredResponseTypeId
120
-
121
- /**
122
- * The identifier of the tool which generated the structured output.
123
- */
124
- readonly id: ToolCallId
125
-
126
- /**
127
- * The name of the tool which generated the structured output.
128
- */
129
- readonly name: string
130
-
131
- /**
132
- * The structured output generated by the model.
133
- */
134
- readonly value: A
135
-
136
- constructor(props: {
137
- /**
138
- * The identifier of the tool which generated the structured output.
139
- */
140
- readonly id: ToolCallId
141
- /**
142
- * The name of the tool which generated the structured output.
143
- */
144
- readonly name: string
145
- /**
146
- * The structured output generated by the model.
147
- */
148
- readonly value: A
149
- /**
150
- * The parts of the response.
151
- */
152
- readonly parts: ReadonlyArray<Part>
153
- }, options?: Schema.MakeOptions) {
154
- super({ parts: props.parts }, options)
155
- this.id = props.id
156
- this.name = props.name
157
- this.value = props.value
158
- }
159
- }
160
-
161
- /**
162
- * @since 1.0.0
163
- * @category Type Ids
164
- */
165
- export const WithToolCallResultsTypeId = Symbol.for("@effect/ai/AiResponse/WithToolCallResults")
166
-
167
- /**
168
- * @since 1.0.0
169
- * @category Type Ids
170
- */
171
- export type WithToolCallResultsTypeId = typeof WithToolCallResultsTypeId
172
-
173
- /**
174
- * Represents a response generated by a large language model that includes
175
- * tool call results.
176
- *
177
- * @since 1.0.0
178
- * @category Models
179
- */
180
- export class WithToolCallResults<Tools extends AiTool.Any> extends AiResponse {
181
- /**
182
- * @since 1.0.0
183
- */
184
- readonly [WithToolCallResultsTypeId]: WithToolCallResultsTypeId = WithToolCallResultsTypeId
185
-
186
- /**
187
- * The tool call results, represented as a mapping between the tool call
188
- * identifier and the result of the tool call handler.
189
- */
190
- readonly results: ReadonlyMap<ToolCallId, {
191
- readonly name: string
192
- readonly result: AiTool.Success<Tools>
193
- }>
194
- /**
195
- * The encoded tool call results, suitable for incorporation into subsequent
196
- * requests to the large language model.
197
- */
198
- readonly encodedResults: ReadonlyMap<ToolCallId, {
199
- readonly name: string
200
- readonly result: unknown
201
- }>
202
-
203
- constructor(props: {
204
- /**
205
- * The tool call results, represented as a mapping between the tool call
206
- * identifier and the result of the tool call handler.
207
- */
208
- readonly results: ReadonlyMap<ToolCallId, {
209
- readonly name: string
210
- readonly result: AiTool.Success<Tools>
211
- }>
212
- /**
213
- * The encoded tool call results, suitable for incorporation into subsequent
214
- * requests to the large language model.
215
- */
216
- readonly encodedResults: ReadonlyMap<ToolCallId, {
217
- readonly name: string
218
- readonly result: unknown
219
- }>
220
- /**
221
- * The parts of the response.
222
- */
223
- readonly parts: ReadonlyArray<Part>
224
- }, options?: Schema.MakeOptions) {
225
- super({ parts: props.parts }, options)
226
- this.results = props.results
227
- this.encodedResults = props.encodedResults
228
- }
229
-
230
- getToolCallResult(toolName: AiTool.Name<Tools>): Option.Option<AiTool.Success<Tools>> {
231
- for (const { name, result } of this.results.values()) {
232
- if (name === toolName) {
233
- return Option.some(result)
234
- }
235
- }
236
- return Option.none()
237
- }
238
- }
239
-
240
- // =============================================================================
241
- // Part
242
- // =============================================================================
243
-
244
- /**
245
- * @since 1.0.0
246
- * @category Type Ids
247
- */
248
- export const PartTypeId = Symbol.for("@effect/ai/AiResponse/Part")
249
-
250
- /**
251
- * @since 1.0.0
252
- * @category Type Ids
253
- */
254
- export type PartTypeId = typeof PartTypeId
255
-
256
- /**
257
- * Represents a content source that was used to generate a model response.
258
- *
259
- * @since 1.0.0
260
- * @category Models
261
- */
262
- export class ContentSourceAnnotation extends Schema.TaggedClass<ContentSourceAnnotation>(
263
- "@effect/ai/AiResponse/Annotation/ContentSourceAnnotation"
264
- )("ContentSourceAnnotation", {
265
- /**
266
- * The identifier for the content source.
267
- */
268
- id: Schema.String,
269
- /**
270
- * The index of the content source in the list of sources provided in the
271
- * model request parameters.
272
- */
273
- index: Schema.Int,
274
- /**
275
- * The provider-specific type of the file annotation.
276
- *
277
- * For example, when using Anthropic the type may be `char_location`,
278
- * `page_location`, or `content_block_location`.
279
- */
280
- type: Schema.String,
281
- /**
282
- * The content used from the content source in the message generated by the
283
- * model.
284
- */
285
- referencedContent: Schema.String,
286
- /**
287
- * The index of the first character of the content referenced by the content
288
- * source in the message generated by the model.
289
- */
290
- startIndex: Schema.Int,
291
- /**
292
- * The index of the last character of the content referenced by the content
293
- * source in the message generated by the model.
294
- */
295
- endIndex: Schema.Int
296
- }) {}
297
-
298
- /**
299
- * Represents a file that was used to generate a model response.
300
- *
301
- * @since 1.0.0
302
- * @category Models
303
- */
304
- export class FileAnnotation extends Schema.TaggedClass<FileAnnotation>(
305
- "@effect/ai/AiResponse/Annotation/FileAnnotation"
306
- )("FileAnnotation", {
307
- /**
308
- * The identifier for the file.
309
- */
310
- id: Schema.String,
311
- /**
312
- * The provider-specific type of the file annotation.
313
- *
314
- * For example, when using OpenAi the type may be `file_citation` or
315
- * `file_path`.
316
- */
317
- type: Schema.String,
318
- /**
319
- * The index of the file in the list of files provided in the model request
320
- * parameters.
321
- */
322
- index: Schema.Int
323
- }) {}
324
-
325
- /**
326
- * Represents a web resource that was used to generate a model response.
327
- *
328
- * @since 1.0.0
329
- * @category Models
330
- */
331
- export class UrlAnnotation extends Schema.TaggedClass<UrlAnnotation>(
332
- "@effect/ai/AiResponse/Annotation/UrlAnnotation"
333
- )("UrlAnnotation", {
334
- /**
335
- * The URL of the web resource.
336
- */
337
- url: Schema.String,
338
- /**
339
- * The title of the web resource.
340
- */
341
- title: Schema.String,
342
- /**
343
- * The index of the first character of the content referenced by the web
344
- * resource in the message generated by the model.
345
- */
346
- startIndex: Schema.Int,
347
- /**
348
- * The index of the last character of the content referenced by the web
349
- * resource in the message generated by the model.
350
- */
351
- endIndex: Schema.Int
352
- }) {}
353
-
354
- /**
355
- * Represents annotations that were used to support the message generated by
356
- * a model.
357
- *
358
- * @since 1.0.0
359
- * @category Models
360
- */
361
- export const Annotation: Schema.Union<[
362
- typeof ContentSourceAnnotation,
363
- typeof FileAnnotation,
364
- typeof UrlAnnotation
365
- ]> = Schema.Union(
366
- ContentSourceAnnotation,
367
- FileAnnotation,
368
- UrlAnnotation
369
- )
370
-
371
- /**
372
- * @since 1.0.0
373
- * @category Models
374
- */
375
- export type Annotation = typeof Annotation.Type
376
-
377
- /**
378
- * Represents part of the text generated by the model.
379
- *
380
- * @since 1.0.0
381
- * @category Models
382
- */
383
- export class TextPart extends Schema.TaggedClass<TextPart>(
384
- "@effect/ai/AiResponse/TextPart"
385
- )("TextPart", {
386
- /**
387
- * The text content generated by the model.
388
- */
389
- text: Schema.String,
390
- /**
391
- * The annotations used to support the text generated by the model.
392
- */
393
- annotations: Schema.optionalWith(Schema.Array(Annotation), {
394
- default: () => []
395
- })
396
- }) {
397
- /**
398
- * @since 1.0.0
399
- */
400
- readonly [PartTypeId]: PartTypeId = PartTypeId
401
- }
402
-
403
- /**
404
- * Represents part of the reasoning carried out by the model to generate a
405
- * response.
406
- *
407
- * @since 1.0.0
408
- * @category Models
409
- */
410
- export class ReasoningPart extends Schema.TaggedClass<ReasoningPart>(
411
- "@effect/ai/AiResponse/ReasoningPart"
412
- )("ReasoningPart", {
413
- /**
414
- * The reasoning content that the model used to return the output.
415
- */
416
- reasoningText: Schema.String,
417
- /**
418
- * An optional signature which verifies that the reasoning text was generated
419
- * by the model.
420
- */
421
- signature: Schema.optional(Schema.String)
422
- }) {
423
- /**
424
- * @since 1.0.0
425
- */
426
- readonly [PartTypeId]: PartTypeId = PartTypeId
427
- }
428
-
429
- /**
430
- * Represents part of the reasoning carried out by the model to generate a
431
- * response which needed to be encrypted by the model provider for safety
432
- * reasons.
433
- *
434
- * @since 1.0.0
435
- * @category Models
436
- */
437
- export class RedactedReasoningPart extends Schema.TaggedClass<RedactedReasoningPart>(
438
- "@effect/ai/AiResponse/RedactedReasoningPart"
439
- )("RedactedReasoningPart", {
440
- /**
441
- * The content in the reasoning that was encrypted by the model provider for
442
- * safety reasons.
443
- */
444
- redactedText: Schema.String
445
- }) {
446
- /**
447
- * @since 1.0.0
448
- */
449
- readonly [PartTypeId]: PartTypeId = PartTypeId
450
- }
451
-
452
- /**
453
- * Represents the identifier generated by a model when a tool call is requested.
454
- *
455
- * @since 1.0.0
456
- * @category Models
457
- */
458
- export const ToolCallId: Schema.brand<typeof Schema.String, "@effect/ai/ToolCallId"> = InternalCommon.ToolCallId
459
-
460
- /**
461
- * @since 1.0.0
462
- * @category Models
463
- */
464
- export type ToolCallId = typeof ToolCallId.Type
465
-
466
- /**
467
- * Represents a request by a model to call a specific tool that it has been
468
- * provided with.
469
- *
470
- * @since 1.0.0
471
- * @category Models
472
- */
473
- export class ToolCallPart extends Schema.TaggedClass<ToolCallPart>(
474
- "@effect/ai/AiResponse/ToolCallPart"
475
- )("ToolCallPart", {
476
- /**
477
- * The identifier generated by a model when requesting a tool call.
478
- */
479
- id: ToolCallId,
480
- /**
481
- * The name of the tool to call.
482
- */
483
- name: Schema.String,
484
- /**
485
- * The arguments to call the tool with as a JSON-serializable object that
486
- * matches the tool call input schema.
487
- */
488
- params: Schema.Unknown
489
- }) {
490
- /**
491
- * @since 1.0.0
492
- */
493
- readonly [PartTypeId]: PartTypeId = PartTypeId
494
-
495
- /**
496
- * Converts a raw tool call into a `ToolCallPart` by parsing tool call
497
- * parameters as a JSON string. If your tool call parameters are already
498
- * parsed, use `ToolCallPart.fromUnknown`.
499
- *
500
- * @since 1.0.0
501
- */
502
- static fromJson({ id, name, params }: {
503
- readonly id: string
504
- readonly name: string
505
- readonly params: string
506
- }): Effect.Effect<ToolCallPart, AiError> {
507
- return Effect.try({
508
- try() {
509
- return new ToolCallPart({
510
- id: ToolCallId.make(id, constDisableValidation),
511
- name,
512
- params: JSON.parse(params)
513
- }, constDisableValidation)
514
- },
515
- catch: (cause) =>
516
- new AiError({
517
- module: "AiResponse",
518
- method: "ToolCall.fromJson",
519
- description: `Failed to parse parameters from JSON:\n${params}`,
520
- cause
521
- })
522
- })
523
- }
524
-
525
- /**
526
- * Converts a raw tool call into a `ToolCallPart` assuming that the tool call
527
- * parameters have already been parsed. If your tool call parameters have not
528
- * already been parsed, use `ToolCallPart.fromJson`.
529
- *
530
- * @since 1.0.0
531
- */
532
- static fromUnknown({ id, name, params }: {
533
- readonly id: string
534
- readonly name: string
535
- readonly params: unknown
536
- }): ToolCallPart {
537
- return new ToolCallPart({
538
- id: ToolCallId.make(id, constDisableValidation),
539
- name,
540
- params
541
- }, constDisableValidation)
542
- }
543
- }
544
-
545
- /**
546
- * Represents the initial response metadata generated by a model when responding
547
- * to a request.
548
- *
549
- * @since 1.0.0
550
- * @categor Models
551
- */
552
- export class MetadataPart extends Schema.TaggedClass<MetadataPart>(
553
- "@effect/ai/AiResponse/MetadataPart"
554
- )("MetadataPart", {
555
- /**
556
- * The unique identifier for the response. Each chunk of the response should
557
- * have the same identifier.
558
- */
559
- id: Schema.optional(Schema.String),
560
- /**
561
- * The model that was used to generate the response.
562
- */
563
- model: Schema.String,
564
- /**
565
- * The Unix timestamp of when the model began generated the response.
566
- */
567
- timestamp: Schema.optional(Schema.DateFromNumber)
568
- }) {
569
- /**
570
- * @since 1.0.0
571
- */
572
- readonly [PartTypeId]: PartTypeId = PartTypeId
573
- }
574
-
575
- /**
576
- * Represents the reason why a model finished generation of a response.
577
- *
578
- * Possible finish reasons:
579
- * - `"stop"`: The model generated a stop sequence.
580
- * - `"length"`: The model exceeded its token budget.
581
- * - `"content-filter"`: The model generated content which violated a content filter.
582
- * - `"tool-calls"`: The model triggered a tool call.
583
- * - `"error"`: The model encountered an error.
584
- * - `"other"`: The model stopped for a reason not supported by this protocol.
585
- * - `"unknown"`: The model did not specify a finish reason.
586
- *
587
- * @since 1.0.0
588
- * @category Models
589
- */
590
- export const FinishReason: Schema.Literal<[
591
- "stop",
592
- "length",
593
- "content-filter",
594
- "tool-calls",
595
- "error",
596
- "other",
597
- "unknown"
598
- ]> = Schema.Literal(
599
- "stop",
600
- "length",
601
- "content-filter",
602
- "tool-calls",
603
- "error",
604
- "other",
605
- "unknown"
606
- )
607
-
608
- /**
609
- * @since 1.0.0
610
- * @category Models
611
- */
612
- export type FinishReason = typeof FinishReason.Type
613
-
614
- /**
615
- * Represents information about the number of tokens used by the model to
616
- * generate a response.
617
- *
618
- * @since 1.0.0
619
- * @category Models
620
- */
621
- export class Usage extends Schema.Class<Usage>(
622
- "@effect/ai/AiResponse/Usage"
623
- )({
624
- /**
625
- * The number of tokens sent in the request to the model.
626
- */
627
- inputTokens: Schema.Number,
628
- /**
629
- * The number of tokens that the model generated for the request.
630
- */
631
- outputTokens: Schema.Number,
632
- /**
633
- * The total of number of input tokens and output tokens generated by the
634
- * model.
635
- */
636
- totalTokens: Schema.Number,
637
- /**
638
- * The number of reasoning tokens that the model used to generate the output
639
- * for the request.
640
- */
641
- reasoningTokens: Schema.Number,
642
- /**
643
- * The number of input tokens read from the prompt cache for the request.
644
- */
645
- cacheReadInputTokens: Schema.Number,
646
- /**
647
- * The number of input tokens written to the prompt cache for the request.
648
- */
649
- cacheWriteInputTokens: Schema.Number
650
- }) {}
651
-
652
- /**
653
- * Represents additional provider-specific metadata that was returned by the
654
- * model. Specific providers will use module augmentation to add their own
655
- * specific provider metadata.
656
- *
657
- * The outer record is keyed by provider name, while the inner record is keyed
658
- * by the names of the provider-specific metadata properties.
659
- *
660
- * For example:
661
- *
662
- * ```ts
663
- * const providerMeta = {
664
- * "amazon-bedrock": {
665
- * // Provider specific metadata
666
- * }
667
- * }
668
- * ```
669
- *
670
- * @since 1.0.0
671
- * @category Models
672
- */
673
- export interface ProviderMetadata {}
674
-
675
- /**
676
- * Represents the final part of a response generated by a large language model.
677
- *
678
- * Contains useful information such as tokens used as part of the interaction
679
- * with the model.
680
- *
681
- * @since 1.0.0
682
- * @category Models
683
- */
684
- export class FinishPart extends Schema.TaggedClass<FinishPart>(
685
- "@effect/ai/AiResponse/FinishPart"
686
- )("FinishPart", {
687
- /**
688
- * The usage information for the response.
689
- */
690
- usage: Usage,
691
- /**
692
- * The reason the model finished generating a response.
693
- */
694
- reason: FinishReason,
695
- /**
696
- * Provider-specific metadata associated with the response.
697
- */
698
- providerMetadata: Schema.optionalWith(
699
- Schema.Record({
700
- key: Schema.String,
701
- value: Schema.Record({ key: Schema.String, value: Schema.Unknown })
702
- }),
703
- { default: () => ({}) }
704
- )
705
- }) {
706
- /**
707
- * @since 1.0.0
708
- */
709
- readonly [PartTypeId]: PartTypeId = PartTypeId
710
- }
711
-
712
- /**
713
- * Represents an single part of a response received from a large language model.
714
- *
715
- * @since 1.0.0
716
- * @category Models
717
- */
718
- export const Part: Schema.Union<[
719
- typeof TextPart,
720
- typeof ReasoningPart,
721
- typeof RedactedReasoningPart,
722
- typeof ToolCallPart,
723
- typeof MetadataPart,
724
- typeof FinishPart
725
- ]> = Schema.Union(
726
- TextPart,
727
- ReasoningPart,
728
- RedactedReasoningPart,
729
- ToolCallPart,
730
- MetadataPart,
731
- FinishPart
732
- )
733
-
734
- /**
735
- * @since 1.0.0
736
- * @category Models
737
- */
738
- export type Part = typeof Part.Type
739
-
740
- /**
741
- * @since 1.0.0
742
- * @category Guards
743
- */
744
- export const is = (u: unknown): u is AiResponse => Predicate.hasProperty(u, TypeId)
745
-
746
- /**
747
- * @since 1.0.0
748
- * @category Guards
749
- */
750
- export const isPart = (u: unknown): u is Part => Predicate.hasProperty(u, PartTypeId)
751
-
752
- /**
753
- * @since 1.0.0
754
- * @category Guards
755
- */
756
- export const isStructured = (u: unknown): u is WithStructuredOutput<any> =>
757
- Predicate.hasProperty(u, StructuredResponseTypeId)
758
-
759
- /**
760
- * @since 1.0.0
761
- * @category Guards
762
- */
763
- export const hasToolCallResults = (u: unknown): u is WithToolCallResults<any> =>
764
- Predicate.hasProperty(u, WithToolCallResultsTypeId)
765
-
766
- /**
767
- * @since 1.0.0
768
- * @category Constructors
769
- */
770
- export const empty: AiResponse = new AiResponse(
771
- { parts: [] },
772
- constDisableValidation
773
- )
774
-
775
- /**
776
- * Combines two responses into a single response.
777
- *
778
- * @since 1.0.0
779
- * @category Combination
780
- */
781
- export const merge: {
782
- /**
783
- * Combines two responses into a single response.
784
- *
785
- * @since 1.0.0
786
- * @category Combination
787
- */
788
- (other: AiResponse): (self: AiResponse) => AiResponse
789
- /**
790
- * Combines two responses into a single response.
791
- *
792
- * @since 1.0.0
793
- * @category Combination
794
- */
795
- (self: AiResponse, other: AiResponse): AiResponse
796
- } = dual<
797
- /**
798
- * Combines two responses into a single response.
799
- *
800
- * @since 1.0.0
801
- * @category Combination
802
- */
803
- (other: AiResponse) => (self: AiResponse) => AiResponse,
804
- /**
805
- * Combines two responses into a single response.
806
- *
807
- * @since 1.0.0
808
- * @category Combination
809
- */
810
- (self: AiResponse, other: AiResponse) => AiResponse
811
- >(2, (self, other) => {
812
- const newParts = mergeParts(self, other)
813
- if (hasToolCallResults(self) && hasToolCallResults(other)) {
814
- return new WithToolCallResults({
815
- results: new Map([...self.results, ...other.results]) as any,
816
- encodedResults: new Map([...self.encodedResults, ...other.encodedResults]),
817
- parts: newParts
818
- }, constDisableValidation)
819
- } else if (hasToolCallResults(self)) {
820
- return new WithToolCallResults({
821
- results: self.results as any,
822
- encodedResults: self.encodedResults,
823
- parts: newParts
824
- }, constDisableValidation)
825
- } else if (hasToolCallResults(other)) {
826
- return new WithToolCallResults({
827
- results: other.results as any,
828
- encodedResults: other.encodedResults,
829
- parts: newParts
830
- }, constDisableValidation)
831
- } else if (isStructured(self) && isStructured(other)) {
832
- return new WithStructuredOutput({
833
- id: self.id,
834
- name: self.name,
835
- value: other.value,
836
- parts: newParts
837
- }, constDisableValidation)
838
- } else if (isStructured(self)) {
839
- return new WithStructuredOutput({
840
- id: self.id,
841
- name: self.name,
842
- value: self.value,
843
- parts: newParts
844
- }, constDisableValidation)
845
- } else if (isStructured(other)) {
846
- return new WithStructuredOutput({
847
- id: other.id,
848
- name: other.name,
849
- value: other.value,
850
- parts: newParts
851
- }, constDisableValidation)
852
- }
853
- return new AiResponse({ parts: newParts }, constDisableValidation)
854
- })
855
-
856
- const mergeParts = (self: AiResponse, other: AiResponse): ReadonlyArray<Part> => {
857
- if (other.parts.length === 0) {
858
- return self.parts
859
- }
860
- if (self.parts.length === 0) {
861
- return other.parts
862
- }
863
-
864
- const result: Array<Part> = []
865
- let accumulatedText = ""
866
-
867
- const flushText = () => {
868
- if (accumulatedText.length > 0) {
869
- result.push(new TextPart({ text: accumulatedText }, constDisableValidation))
870
- accumulatedText = ""
871
- }
872
- }
873
-
874
- for (const part of self.parts) {
875
- if (part._tag === "TextPart") {
876
- accumulatedText += part.text
877
- } else {
878
- flushText()
879
- result.push(part)
880
- }
881
- }
882
-
883
- for (const part of other.parts) {
884
- if (part._tag === "TextPart") {
885
- accumulatedText += part.text
886
- } else {
887
- flushText()
888
- result.push(part)
889
- }
890
- }
891
-
892
- flushText()
893
-
894
- return result
895
- }
896
-
897
- /**
898
- * Adds the specified tool calls to the provided `AiResponse`.
899
- *
900
- * **NOTE**: With this method, the tool call parameters will be parsed as a
901
- * JSON string. If your tool call parameters are already parsed, use
902
- * `AiResponse.withToolCallsUnknown`.
903
- *
904
- * @since 1.0.0
905
- * @category Combination
906
- */
907
- export const withToolCallsJson: {
908
- /**
909
- * Adds the specified tool calls to the provided `AiResponse`.
910
- *
911
- * **NOTE**: With this method, the tool call parameters will be parsed as a
912
- * JSON string. If your tool call parameters are already parsed, use
913
- * `AiResponse.withToolCallsUnknown`.
914
- *
915
- * @since 1.0.0
916
- * @category Combination
917
- */
918
- (
919
- toolCalls: Iterable<{
920
- readonly id: string
921
- readonly name: string
922
- readonly params: string
923
- }>
924
- ): (self: AiResponse) => Effect.Effect<AiResponse, AiError>
925
- /**
926
- * Adds the specified tool calls to the provided `AiResponse`.
927
- *
928
- * **NOTE**: With this method, the tool call parameters will be parsed as a
929
- * JSON string. If your tool call parameters are already parsed, use
930
- * `AiResponse.withToolCallsUnknown`.
931
- *
932
- * @since 1.0.0
933
- * @category Combination
934
- */
935
- (
936
- self: AiResponse,
937
- toolCalls: Iterable<{
938
- readonly id: string
939
- readonly name: string
940
- readonly params: string
941
- }>
942
- ): Effect.Effect<AiResponse, AiError>
943
- } = dual<
944
- /**
945
- * Adds the specified tool calls to the provided `AiResponse`.
946
- *
947
- * **NOTE**: With this method, the tool call parameters will be parsed as a
948
- * JSON string. If your tool call parameters are already parsed, use
949
- * `AiResponse.withToolCallsUnknown`.
950
- *
951
- * @since 1.0.0
952
- * @category Combination
953
- */
954
- (
955
- toolCalls: Iterable<{
956
- readonly id: string
957
- readonly name: string
958
- readonly params: string
959
- }>
960
- ) => (self: AiResponse) => Effect.Effect<AiResponse, AiError>,
961
- /**
962
- * Adds the specified tool calls to the provided `AiResponse`.
963
- *
964
- * **NOTE**: With this method, the tool call parameters will be parsed as a
965
- * JSON string. If your tool call parameters are already parsed, use
966
- * `AiResponse.withToolCallsUnknown`.
967
- *
968
- * @since 1.0.0
969
- * @category Combination
970
- */
971
- (
972
- self: AiResponse,
973
- toolCalls: Iterable<{
974
- readonly id: string
975
- readonly name: string
976
- readonly params: string
977
- }>
978
- ) => Effect.Effect<AiResponse, AiError>
979
- >(2, (self, toolCalls) =>
980
- Effect.forEach(toolCalls, (toolCall) => ToolCallPart.fromJson(toolCall)).pipe(
981
- Effect.map((parts) =>
982
- new AiResponse({
983
- parts: [...self.parts, ...parts]
984
- }, constDisableValidation)
985
- )
986
- ))