@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
@@ -1,681 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.withToolCallsJson = exports.merge = exports.isStructured = exports.isPart = exports.is = exports.hasToolCallResults = exports.empty = exports.WithToolCallResultsTypeId = exports.WithToolCallResults = exports.WithStructuredOutput = exports.Usage = exports.UrlAnnotation = exports.TypeId = exports.ToolCallPart = exports.ToolCallId = exports.TextPart = exports.StructuredResponseTypeId = exports.RedactedReasoningPart = exports.ReasoningPart = exports.PartTypeId = exports.Part = exports.MetadataPart = exports.FromJson = exports.FinishReason = exports.FinishPart = exports.FileAnnotation = exports.ContentSourceAnnotation = exports.Annotation = exports.AiResponse = void 0;
7
- var Effect = _interopRequireWildcard(require("effect/Effect"));
8
- var _Function = require("effect/Function");
9
- var Option = _interopRequireWildcard(require("effect/Option"));
10
- var Predicate = _interopRequireWildcard(require("effect/Predicate"));
11
- var Schema = _interopRequireWildcard(require("effect/Schema"));
12
- var _AiError = require("./AiError.js");
13
- var InternalCommon = _interopRequireWildcard(require("./internal/common.js"));
14
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
15
- const constDisableValidation = {
16
- disableValidation: true
17
- };
18
- /**
19
- * @since 1.0.0
20
- * @category Type Ids
21
- */
22
- const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiResponse");
23
- /**
24
- * Represents a response received from a large language model.
25
- *
26
- * @since 1.0.0
27
- * @category Models
28
- */
29
- class AiResponse extends /*#__PURE__*/Schema.Class("@effect/ai/AiResponse")({
30
- /**
31
- * The parts of the response.
32
- */
33
- parts: /*#__PURE__*/Schema.Array(/*#__PURE__*/Schema.suspend(() => Part))
34
- }) {
35
- /**
36
- * @since 1.0.0
37
- */
38
- [TypeId] = TypeId;
39
- /**
40
- * Returns the generated text content of the response.
41
- */
42
- get text() {
43
- let text = "";
44
- let found = false;
45
- for (const part of this.parts) {
46
- if (part._tag === "TextPart") {
47
- text += found ? "\n\n" + part.text : part.text;
48
- found = true;
49
- }
50
- }
51
- return text;
52
- }
53
- /**
54
- * Returns the finish reason for the response, or `"unknown"` if the finish
55
- * reason is not known.
56
- */
57
- get finishReason() {
58
- const finishPart = this.parts.find(part => part._tag === "FinishPart");
59
- return finishPart?.reason ?? "unknown";
60
- }
61
- /**
62
- * Returns all tool calls contained within the response.
63
- */
64
- get toolCalls() {
65
- return this.parts.filter(part => part._tag === "ToolCallPart");
66
- }
67
- /**
68
- * Attempts to retrieve provider-specific response metadata.
69
- */
70
- getProviderMetadata(tag) {
71
- const finishPart = this.parts.find(part => part._tag === "FinishPart");
72
- return Option.fromNullable(finishPart?.providerMetadata[tag.key]);
73
- }
74
- }
75
- /**
76
- * @since 1.0.0
77
- * @category Models
78
- */
79
- exports.AiResponse = AiResponse;
80
- const FromJson = exports.FromJson = /*#__PURE__*/Schema.parseJson(AiResponse);
81
- /**
82
- * @since 1.0.0
83
- * @category Type Ids
84
- */
85
- const StructuredResponseTypeId = exports.StructuredResponseTypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiResponse/StructuredResponse");
86
- /**
87
- * Represents a response generated by a large language model that includes
88
- * structured output.
89
- *
90
- * @since 1.0.0
91
- * @category Models
92
- */
93
- class WithStructuredOutput extends AiResponse {
94
- /**
95
- * @since 1.0.0
96
- */
97
- [StructuredResponseTypeId] = StructuredResponseTypeId;
98
- /**
99
- * The identifier of the tool which generated the structured output.
100
- */
101
- id;
102
- /**
103
- * The name of the tool which generated the structured output.
104
- */
105
- name;
106
- /**
107
- * The structured output generated by the model.
108
- */
109
- value;
110
- constructor(props, options) {
111
- super({
112
- parts: props.parts
113
- }, options);
114
- this.id = props.id;
115
- this.name = props.name;
116
- this.value = props.value;
117
- }
118
- }
119
- /**
120
- * @since 1.0.0
121
- * @category Type Ids
122
- */
123
- exports.WithStructuredOutput = WithStructuredOutput;
124
- const WithToolCallResultsTypeId = exports.WithToolCallResultsTypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiResponse/WithToolCallResults");
125
- /**
126
- * Represents a response generated by a large language model that includes
127
- * tool call results.
128
- *
129
- * @since 1.0.0
130
- * @category Models
131
- */
132
- class WithToolCallResults extends AiResponse {
133
- /**
134
- * @since 1.0.0
135
- */
136
- [WithToolCallResultsTypeId] = WithToolCallResultsTypeId;
137
- /**
138
- * The tool call results, represented as a mapping between the tool call
139
- * identifier and the result of the tool call handler.
140
- */
141
- results;
142
- /**
143
- * The encoded tool call results, suitable for incorporation into subsequent
144
- * requests to the large language model.
145
- */
146
- encodedResults;
147
- constructor(props, options) {
148
- super({
149
- parts: props.parts
150
- }, options);
151
- this.results = props.results;
152
- this.encodedResults = props.encodedResults;
153
- }
154
- getToolCallResult(toolName) {
155
- for (const {
156
- name,
157
- result
158
- } of this.results.values()) {
159
- if (name === toolName) {
160
- return Option.some(result);
161
- }
162
- }
163
- return Option.none();
164
- }
165
- }
166
- // =============================================================================
167
- // Part
168
- // =============================================================================
169
- /**
170
- * @since 1.0.0
171
- * @category Type Ids
172
- */
173
- exports.WithToolCallResults = WithToolCallResults;
174
- const PartTypeId = exports.PartTypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiResponse/Part");
175
- /**
176
- * Represents a content source that was used to generate a model response.
177
- *
178
- * @since 1.0.0
179
- * @category Models
180
- */
181
- class ContentSourceAnnotation extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/Annotation/ContentSourceAnnotation")("ContentSourceAnnotation", {
182
- /**
183
- * The identifier for the content source.
184
- */
185
- id: Schema.String,
186
- /**
187
- * The index of the content source in the list of sources provided in the
188
- * model request parameters.
189
- */
190
- index: Schema.Int,
191
- /**
192
- * The provider-specific type of the file annotation.
193
- *
194
- * For example, when using Anthropic the type may be `char_location`,
195
- * `page_location`, or `content_block_location`.
196
- */
197
- type: Schema.String,
198
- /**
199
- * The content used from the content source in the message generated by the
200
- * model.
201
- */
202
- referencedContent: Schema.String,
203
- /**
204
- * The index of the first character of the content referenced by the content
205
- * source in the message generated by the model.
206
- */
207
- startIndex: Schema.Int,
208
- /**
209
- * The index of the last character of the content referenced by the content
210
- * source in the message generated by the model.
211
- */
212
- endIndex: Schema.Int
213
- }) {}
214
- /**
215
- * Represents a file that was used to generate a model response.
216
- *
217
- * @since 1.0.0
218
- * @category Models
219
- */
220
- exports.ContentSourceAnnotation = ContentSourceAnnotation;
221
- class FileAnnotation extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/Annotation/FileAnnotation")("FileAnnotation", {
222
- /**
223
- * The identifier for the file.
224
- */
225
- id: Schema.String,
226
- /**
227
- * The provider-specific type of the file annotation.
228
- *
229
- * For example, when using OpenAi the type may be `file_citation` or
230
- * `file_path`.
231
- */
232
- type: Schema.String,
233
- /**
234
- * The index of the file in the list of files provided in the model request
235
- * parameters.
236
- */
237
- index: Schema.Int
238
- }) {}
239
- /**
240
- * Represents a web resource that was used to generate a model response.
241
- *
242
- * @since 1.0.0
243
- * @category Models
244
- */
245
- exports.FileAnnotation = FileAnnotation;
246
- class UrlAnnotation extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/Annotation/UrlAnnotation")("UrlAnnotation", {
247
- /**
248
- * The URL of the web resource.
249
- */
250
- url: Schema.String,
251
- /**
252
- * The title of the web resource.
253
- */
254
- title: Schema.String,
255
- /**
256
- * The index of the first character of the content referenced by the web
257
- * resource in the message generated by the model.
258
- */
259
- startIndex: Schema.Int,
260
- /**
261
- * The index of the last character of the content referenced by the web
262
- * resource in the message generated by the model.
263
- */
264
- endIndex: Schema.Int
265
- }) {}
266
- /**
267
- * Represents annotations that were used to support the message generated by
268
- * a model.
269
- *
270
- * @since 1.0.0
271
- * @category Models
272
- */
273
- exports.UrlAnnotation = UrlAnnotation;
274
- const Annotation = exports.Annotation = /*#__PURE__*/Schema.Union(ContentSourceAnnotation, FileAnnotation, UrlAnnotation);
275
- /**
276
- * Represents part of the text generated by the model.
277
- *
278
- * @since 1.0.0
279
- * @category Models
280
- */
281
- class TextPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/TextPart")("TextPart", {
282
- /**
283
- * The text content generated by the model.
284
- */
285
- text: Schema.String,
286
- /**
287
- * The annotations used to support the text generated by the model.
288
- */
289
- annotations: /*#__PURE__*/Schema.optionalWith(/*#__PURE__*/Schema.Array(Annotation), {
290
- default: () => []
291
- })
292
- }) {
293
- /**
294
- * @since 1.0.0
295
- */
296
- [PartTypeId] = PartTypeId;
297
- }
298
- /**
299
- * Represents part of the reasoning carried out by the model to generate a
300
- * response.
301
- *
302
- * @since 1.0.0
303
- * @category Models
304
- */
305
- exports.TextPart = TextPart;
306
- class ReasoningPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/ReasoningPart")("ReasoningPart", {
307
- /**
308
- * The reasoning content that the model used to return the output.
309
- */
310
- reasoningText: Schema.String,
311
- /**
312
- * An optional signature which verifies that the reasoning text was generated
313
- * by the model.
314
- */
315
- signature: /*#__PURE__*/Schema.optional(Schema.String)
316
- }) {
317
- /**
318
- * @since 1.0.0
319
- */
320
- [PartTypeId] = PartTypeId;
321
- }
322
- /**
323
- * Represents part of the reasoning carried out by the model to generate a
324
- * response which needed to be encrypted by the model provider for safety
325
- * reasons.
326
- *
327
- * @since 1.0.0
328
- * @category Models
329
- */
330
- exports.ReasoningPart = ReasoningPart;
331
- class RedactedReasoningPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/RedactedReasoningPart")("RedactedReasoningPart", {
332
- /**
333
- * The content in the reasoning that was encrypted by the model provider for
334
- * safety reasons.
335
- */
336
- redactedText: Schema.String
337
- }) {
338
- /**
339
- * @since 1.0.0
340
- */
341
- [PartTypeId] = PartTypeId;
342
- }
343
- /**
344
- * Represents the identifier generated by a model when a tool call is requested.
345
- *
346
- * @since 1.0.0
347
- * @category Models
348
- */
349
- exports.RedactedReasoningPart = RedactedReasoningPart;
350
- const ToolCallId = exports.ToolCallId = InternalCommon.ToolCallId;
351
- /**
352
- * Represents a request by a model to call a specific tool that it has been
353
- * provided with.
354
- *
355
- * @since 1.0.0
356
- * @category Models
357
- */
358
- class ToolCallPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/ToolCallPart")("ToolCallPart", {
359
- /**
360
- * The identifier generated by a model when requesting a tool call.
361
- */
362
- id: ToolCallId,
363
- /**
364
- * The name of the tool to call.
365
- */
366
- name: Schema.String,
367
- /**
368
- * The arguments to call the tool with as a JSON-serializable object that
369
- * matches the tool call input schema.
370
- */
371
- params: Schema.Unknown
372
- }) {
373
- /**
374
- * @since 1.0.0
375
- */
376
- [PartTypeId] = PartTypeId;
377
- /**
378
- * Converts a raw tool call into a `ToolCallPart` by parsing tool call
379
- * parameters as a JSON string. If your tool call parameters are already
380
- * parsed, use `ToolCallPart.fromUnknown`.
381
- *
382
- * @since 1.0.0
383
- */
384
- static fromJson({
385
- id,
386
- name,
387
- params
388
- }) {
389
- return Effect.try({
390
- try() {
391
- return new ToolCallPart({
392
- id: ToolCallId.make(id, constDisableValidation),
393
- name,
394
- params: JSON.parse(params)
395
- }, constDisableValidation);
396
- },
397
- catch: cause => new _AiError.AiError({
398
- module: "AiResponse",
399
- method: "ToolCall.fromJson",
400
- description: `Failed to parse parameters from JSON:\n${params}`,
401
- cause
402
- })
403
- });
404
- }
405
- /**
406
- * Converts a raw tool call into a `ToolCallPart` assuming that the tool call
407
- * parameters have already been parsed. If your tool call parameters have not
408
- * already been parsed, use `ToolCallPart.fromJson`.
409
- *
410
- * @since 1.0.0
411
- */
412
- static fromUnknown({
413
- id,
414
- name,
415
- params
416
- }) {
417
- return new ToolCallPart({
418
- id: ToolCallId.make(id, constDisableValidation),
419
- name,
420
- params
421
- }, constDisableValidation);
422
- }
423
- }
424
- /**
425
- * Represents the initial response metadata generated by a model when responding
426
- * to a request.
427
- *
428
- * @since 1.0.0
429
- * @categor Models
430
- */
431
- exports.ToolCallPart = ToolCallPart;
432
- class MetadataPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/MetadataPart")("MetadataPart", {
433
- /**
434
- * The unique identifier for the response. Each chunk of the response should
435
- * have the same identifier.
436
- */
437
- id: /*#__PURE__*/Schema.optional(Schema.String),
438
- /**
439
- * The model that was used to generate the response.
440
- */
441
- model: Schema.String,
442
- /**
443
- * The Unix timestamp of when the model began generated the response.
444
- */
445
- timestamp: /*#__PURE__*/Schema.optional(Schema.DateFromNumber)
446
- }) {
447
- /**
448
- * @since 1.0.0
449
- */
450
- [PartTypeId] = PartTypeId;
451
- }
452
- /**
453
- * Represents the reason why a model finished generation of a response.
454
- *
455
- * Possible finish reasons:
456
- * - `"stop"`: The model generated a stop sequence.
457
- * - `"length"`: The model exceeded its token budget.
458
- * - `"content-filter"`: The model generated content which violated a content filter.
459
- * - `"tool-calls"`: The model triggered a tool call.
460
- * - `"error"`: The model encountered an error.
461
- * - `"other"`: The model stopped for a reason not supported by this protocol.
462
- * - `"unknown"`: The model did not specify a finish reason.
463
- *
464
- * @since 1.0.0
465
- * @category Models
466
- */
467
- exports.MetadataPart = MetadataPart;
468
- const FinishReason = exports.FinishReason = /*#__PURE__*/Schema.Literal("stop", "length", "content-filter", "tool-calls", "error", "other", "unknown");
469
- /**
470
- * Represents information about the number of tokens used by the model to
471
- * generate a response.
472
- *
473
- * @since 1.0.0
474
- * @category Models
475
- */
476
- class Usage extends /*#__PURE__*/Schema.Class("@effect/ai/AiResponse/Usage")({
477
- /**
478
- * The number of tokens sent in the request to the model.
479
- */
480
- inputTokens: Schema.Number,
481
- /**
482
- * The number of tokens that the model generated for the request.
483
- */
484
- outputTokens: Schema.Number,
485
- /**
486
- * The total of number of input tokens and output tokens generated by the
487
- * model.
488
- */
489
- totalTokens: Schema.Number,
490
- /**
491
- * The number of reasoning tokens that the model used to generate the output
492
- * for the request.
493
- */
494
- reasoningTokens: Schema.Number,
495
- /**
496
- * The number of input tokens read from the prompt cache for the request.
497
- */
498
- cacheReadInputTokens: Schema.Number,
499
- /**
500
- * The number of input tokens written to the prompt cache for the request.
501
- */
502
- cacheWriteInputTokens: Schema.Number
503
- }) {}
504
- /**
505
- * Represents the final part of a response generated by a large language model.
506
- *
507
- * Contains useful information such as tokens used as part of the interaction
508
- * with the model.
509
- *
510
- * @since 1.0.0
511
- * @category Models
512
- */
513
- exports.Usage = Usage;
514
- class FinishPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiResponse/FinishPart")("FinishPart", {
515
- /**
516
- * The usage information for the response.
517
- */
518
- usage: Usage,
519
- /**
520
- * The reason the model finished generating a response.
521
- */
522
- reason: FinishReason,
523
- /**
524
- * Provider-specific metadata associated with the response.
525
- */
526
- providerMetadata: /*#__PURE__*/Schema.optionalWith(/*#__PURE__*/Schema.Record({
527
- key: Schema.String,
528
- value: /*#__PURE__*/Schema.Record({
529
- key: Schema.String,
530
- value: Schema.Unknown
531
- })
532
- }), {
533
- default: () => ({})
534
- })
535
- }) {
536
- /**
537
- * @since 1.0.0
538
- */
539
- [PartTypeId] = PartTypeId;
540
- }
541
- /**
542
- * Represents an single part of a response received from a large language model.
543
- *
544
- * @since 1.0.0
545
- * @category Models
546
- */
547
- exports.FinishPart = FinishPart;
548
- const Part = exports.Part = /*#__PURE__*/Schema.Union(TextPart, ReasoningPart, RedactedReasoningPart, ToolCallPart, MetadataPart, FinishPart);
549
- /**
550
- * @since 1.0.0
551
- * @category Guards
552
- */
553
- const is = u => Predicate.hasProperty(u, TypeId);
554
- /**
555
- * @since 1.0.0
556
- * @category Guards
557
- */
558
- exports.is = is;
559
- const isPart = u => Predicate.hasProperty(u, PartTypeId);
560
- /**
561
- * @since 1.0.0
562
- * @category Guards
563
- */
564
- exports.isPart = isPart;
565
- const isStructured = u => Predicate.hasProperty(u, StructuredResponseTypeId);
566
- /**
567
- * @since 1.0.0
568
- * @category Guards
569
- */
570
- exports.isStructured = isStructured;
571
- const hasToolCallResults = u => Predicate.hasProperty(u, WithToolCallResultsTypeId);
572
- /**
573
- * @since 1.0.0
574
- * @category Constructors
575
- */
576
- exports.hasToolCallResults = hasToolCallResults;
577
- const empty = exports.empty = /*#__PURE__*/new AiResponse({
578
- parts: []
579
- }, constDisableValidation);
580
- /**
581
- * Combines two responses into a single response.
582
- *
583
- * @since 1.0.0
584
- * @category Combination
585
- */
586
- const merge = exports.merge = /*#__PURE__*/(0, _Function.dual)(2, (self, other) => {
587
- const newParts = mergeParts(self, other);
588
- if (hasToolCallResults(self) && hasToolCallResults(other)) {
589
- return new WithToolCallResults({
590
- results: new Map([...self.results, ...other.results]),
591
- encodedResults: new Map([...self.encodedResults, ...other.encodedResults]),
592
- parts: newParts
593
- }, constDisableValidation);
594
- } else if (hasToolCallResults(self)) {
595
- return new WithToolCallResults({
596
- results: self.results,
597
- encodedResults: self.encodedResults,
598
- parts: newParts
599
- }, constDisableValidation);
600
- } else if (hasToolCallResults(other)) {
601
- return new WithToolCallResults({
602
- results: other.results,
603
- encodedResults: other.encodedResults,
604
- parts: newParts
605
- }, constDisableValidation);
606
- } else if (isStructured(self) && isStructured(other)) {
607
- return new WithStructuredOutput({
608
- id: self.id,
609
- name: self.name,
610
- value: other.value,
611
- parts: newParts
612
- }, constDisableValidation);
613
- } else if (isStructured(self)) {
614
- return new WithStructuredOutput({
615
- id: self.id,
616
- name: self.name,
617
- value: self.value,
618
- parts: newParts
619
- }, constDisableValidation);
620
- } else if (isStructured(other)) {
621
- return new WithStructuredOutput({
622
- id: other.id,
623
- name: other.name,
624
- value: other.value,
625
- parts: newParts
626
- }, constDisableValidation);
627
- }
628
- return new AiResponse({
629
- parts: newParts
630
- }, constDisableValidation);
631
- });
632
- const mergeParts = (self, other) => {
633
- if (other.parts.length === 0) {
634
- return self.parts;
635
- }
636
- if (self.parts.length === 0) {
637
- return other.parts;
638
- }
639
- const result = [];
640
- let accumulatedText = "";
641
- const flushText = () => {
642
- if (accumulatedText.length > 0) {
643
- result.push(new TextPart({
644
- text: accumulatedText
645
- }, constDisableValidation));
646
- accumulatedText = "";
647
- }
648
- };
649
- for (const part of self.parts) {
650
- if (part._tag === "TextPart") {
651
- accumulatedText += part.text;
652
- } else {
653
- flushText();
654
- result.push(part);
655
- }
656
- }
657
- for (const part of other.parts) {
658
- if (part._tag === "TextPart") {
659
- accumulatedText += part.text;
660
- } else {
661
- flushText();
662
- result.push(part);
663
- }
664
- }
665
- flushText();
666
- return result;
667
- };
668
- /**
669
- * Adds the specified tool calls to the provided `AiResponse`.
670
- *
671
- * **NOTE**: With this method, the tool call parameters will be parsed as a
672
- * JSON string. If your tool call parameters are already parsed, use
673
- * `AiResponse.withToolCallsUnknown`.
674
- *
675
- * @since 1.0.0
676
- * @category Combination
677
- */
678
- const withToolCallsJson = exports.withToolCallsJson = /*#__PURE__*/(0, _Function.dual)(2, (self, toolCalls) => Effect.forEach(toolCalls, toolCall => ToolCallPart.fromJson(toolCall)).pipe(Effect.map(parts => new AiResponse({
679
- parts: [...self.parts, ...parts]
680
- }, constDisableValidation))));
681
- //# sourceMappingURL=AiResponse.js.map