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