@effect/ai 0.14.0 → 0.15.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,184 +3,155 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.provideSystem = exports.make = exports.isPart = exports.empty = exports.ToolCallResolvedPart = exports.ToolCallPart = exports.TextPart = exports.SystemInstruction = exports.SchemaJson = exports.Schema = exports.PartTypeId = exports.Part = exports.MessageTypeId = exports.Message = exports.ImageUrlPart = exports.ImageQuality = exports.ImagePart = exports.Base64DataUrl = void 0;
7
- var FileSystem = _interopRequireWildcard(require("@effect/platform/FileSystem"));
8
- var Path = _interopRequireWildcard(require("@effect/platform/Path"));
9
- var Chunk = _interopRequireWildcard(require("effect/Chunk"));
10
- var Context = _interopRequireWildcard(require("effect/Context"));
11
- var Effect = _interopRequireWildcard(require("effect/Effect"));
12
- var Encoding = _interopRequireWildcard(require("effect/Encoding"));
6
+ exports.make = exports.isPart = exports.isMessage = exports.is = exports.empty = exports.concat = exports.UserMessagePart = exports.UserMessage = exports.TypeId = exports.ToolMessagePart = exports.ToolMessage = exports.ToolCallResultPart = exports.ToolCallPart = exports.ToolCallId = exports.TextPart = exports.RedactedReasoningPart = exports.ReasoningPart = exports.PartTypeId = exports.MessageTypeId = exports.Message = exports.ImageUrlPart = exports.ImagePart = exports.FromJson = exports.FileUrlPart = exports.FilePart = exports.AssistantMessagePart = exports.AssistantMessage = exports.AiInput = void 0;
13
7
  var _Function = require("effect/Function");
14
- var Option = _interopRequireWildcard(require("effect/Option"));
15
- var ParseResult = _interopRequireWildcard(require("effect/ParseResult"));
16
8
  var Predicate = _interopRequireWildcard(require("effect/Predicate"));
17
- var Schema_ = _interopRequireWildcard(require("effect/Schema"));
18
- var _AiResponse = require("./AiResponse.js");
19
- var AiRole = _interopRequireWildcard(require("./AiRole.js"));
9
+ var Schema = _interopRequireWildcard(require("effect/Schema"));
10
+ var AiResponse = _interopRequireWildcard(require("./AiResponse.js"));
11
+ var InternalCommon = _interopRequireWildcard(require("./internal/common.js"));
20
12
  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); }
21
13
  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; }
22
- const constDisableValidation = {
23
- disableValidation: true
24
- };
25
14
  /**
26
15
  * @since 1.0.0
27
- * @category parts
28
16
  */
29
- const PartTypeId = exports.PartTypeId = /*#__PURE__*/Symbol("@effect/ai/AiInput/Part");
17
+
18
+ /**
19
+ * @since 1.0.0
20
+ * @category Type Ids
21
+ */
22
+ const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiInput");
30
23
  /**
24
+ * Represents input to a large language model.
25
+ *
31
26
  * @since 1.0.0
32
- * @category parts
27
+ * @category Models
33
28
  */
34
- class TextPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/TextPart")("Text", {
35
- content: Schema_.String
29
+ class AiInput extends /*#__PURE__*/Schema.Class("@effect/ai/AiInput")({
30
+ messages: /*#__PURE__*/Schema.Array( /*#__PURE__*/Schema.suspend(() => Message))
36
31
  }) {
37
32
  /**
38
33
  * @since 1.0.0
39
34
  */
40
- [PartTypeId] = PartTypeId;
35
+ [TypeId] = TypeId;
36
+ }
37
+ /**
38
+ * @since 1.0.0
39
+ * @category Models
40
+ */
41
+ exports.AiInput = AiInput;
42
+ const FromJson = exports.FromJson = /*#__PURE__*/Schema.parseJson(AiInput);
43
+ // =============================================================================
44
+ // Message
45
+ // =============================================================================
46
+ /**
47
+ * @since 1.0.0
48
+ * @category Type Ids
49
+ */
50
+ const MessageTypeId = exports.MessageTypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiInput/Message");
51
+ /**
52
+ * @since 1.0.0
53
+ * @category Models
54
+ */
55
+ class UserMessage extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/Message/UserMessage")("UserMessage", {
56
+ parts: /*#__PURE__*/Schema.Array( /*#__PURE__*/Schema.suspend(() => UserMessagePart)),
57
+ userName: /*#__PURE__*/Schema.optional(Schema.String)
58
+ }) {
41
59
  /**
42
60
  * @since 1.0.0
43
61
  */
44
- static fromContent(content) {
45
- return new TextPart({
46
- content
47
- }, constDisableValidation);
48
- }
62
+ [MessageTypeId] = MessageTypeId;
49
63
  }
50
64
  /**
51
65
  * @since 1.0.0
52
- * @category parts
66
+ * @category Models
53
67
  */
54
- exports.TextPart = TextPart;
55
- const ImageQuality = exports.ImageQuality = /*#__PURE__*/Schema_.Literal("low", "high", "auto");
68
+ exports.UserMessage = UserMessage;
69
+ class AssistantMessage extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/Message/AssistantMessage")("AssistantMessage", {
70
+ parts: /*#__PURE__*/Schema.Array( /*#__PURE__*/Schema.suspend(() => AssistantMessagePart))
71
+ }) {
72
+ /**
73
+ * @since 1.0.0
74
+ */
75
+ [MessageTypeId] = MessageTypeId;
76
+ }
56
77
  /**
57
78
  * @since 1.0.0
58
- * @category parts
79
+ * @category Models
59
80
  */
60
- class ImageUrlPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/ImageUrlPart")("ImageUrl", {
61
- url: Schema_.String,
62
- quality: /*#__PURE__*/ImageQuality.pipe(Schema_.propertySignature, /*#__PURE__*/Schema_.withConstructorDefault(() => "auto"))
81
+ exports.AssistantMessage = AssistantMessage;
82
+ class ToolMessage extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/Message/ToolMessage")("ToolMessage", {
83
+ parts: /*#__PURE__*/Schema.Array( /*#__PURE__*/Schema.suspend(() => ToolMessagePart))
63
84
  }) {
64
85
  /**
65
86
  * @since 1.0.0
66
87
  */
67
- [PartTypeId] = PartTypeId;
88
+ [MessageTypeId] = MessageTypeId;
68
89
  }
69
- exports.ImageUrlPart = ImageUrlPart;
70
- const base64ContentTypeRegex = /^data:(.*?);base64$/;
71
90
  /**
72
91
  * @since 1.0.0
73
- * @category base64
92
+ * @category Models
74
93
  */
75
- const Base64DataUrl = exports.Base64DataUrl = /*#__PURE__*/Schema_.transformOrFail( /*#__PURE__*/Schema_.String.annotations({
76
- title: "Base64 Data URL",
77
- description: "A base64 data URL"
78
- }), /*#__PURE__*/Schema_.Struct({
79
- data: Schema_.Uint8ArrayFromSelf,
80
- contentType: Schema_.String
81
- }), {
82
- decode(base64Url, _, ast) {
83
- const commaIndex = base64Url.indexOf(",");
84
- if (commaIndex === -1) {
85
- return ParseResult.fail(new ParseResult.Type(ast, base64Url));
86
- }
87
- const header = base64Url.slice(0, commaIndex);
88
- const data = base64Url.slice(commaIndex + 1);
89
- const contentType = base64ContentTypeRegex.exec(header);
90
- if (contentType === null) {
91
- return ParseResult.fail(new ParseResult.Type(ast, base64Url));
92
- }
93
- return Encoding.decodeBase64(data).pipe(ParseResult.mapError(_ => new ParseResult.Type(ast, base64Url)), ParseResult.map(data => ({
94
- data,
95
- contentType: contentType[1]
96
- })));
97
- },
98
- encode({
99
- contentType,
100
- data
101
- }) {
102
- const base64 = Encoding.encodeBase64(data);
103
- return ParseResult.succeed(`data:${contentType};base64,${base64}`);
104
- }
105
- });
94
+ exports.ToolMessage = ToolMessage;
95
+ const Message = exports.Message = /*#__PURE__*/Schema.Union(UserMessage, AssistantMessage, ToolMessage);
96
+ // =============================================================================
97
+ // Part
98
+ // =============================================================================
99
+ /**
100
+ * @since 1.0.0
101
+ * @category Type Ids
102
+ */
103
+ const PartTypeId = exports.PartTypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiInput/Message/Part");
106
104
  /**
105
+ * Represents a text part of a message.
106
+ *
107
107
  * @since 1.0.0
108
- * @category parts
108
+ * @category Models
109
109
  */
110
- class ImagePart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/ImagePart")("Image", {
111
- image: Base64DataUrl,
112
- quality: /*#__PURE__*/ImageQuality.pipe(Schema_.propertySignature, /*#__PURE__*/Schema_.withConstructorDefault(() => "auto"))
110
+ class TextPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/TextPart")("TextPart", {
111
+ /**
112
+ * The text content.
113
+ */
114
+ text: Schema.String
113
115
  }) {
114
116
  /**
115
117
  * @since 1.0.0
116
118
  */
117
119
  [PartTypeId] = PartTypeId;
120
+ }
121
+ /**
122
+ * Represents an image part of a message with binary image data.
123
+ *
124
+ * @since 1.0.0
125
+ * @category Models
126
+ */
127
+ exports.TextPart = TextPart;
128
+ class ImagePart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/ImagePart")("ImagePart", {
118
129
  /**
119
- * @since 1.0.0
130
+ * The binary image data.
120
131
  */
121
- static fromPath(path, quality = "auto") {
122
- return FileSystem.FileSystem.pipe(Effect.bindTo("fs"), Effect.bind("Path", () => Path.Path), Effect.bind("data", ({
123
- fs
124
- }) => fs.readFile(path)), Effect.map(({
125
- Path,
126
- data
127
- }) => {
128
- const ext = Path.extname(path);
129
- let contentType;
130
- switch (ext) {
131
- case ".jpg":
132
- case ".jpeg":
133
- {
134
- contentType = "image/jpeg";
135
- break;
136
- }
137
- default:
138
- {
139
- if (ext.startsWith(".")) {
140
- contentType = `image/${ext.slice(1)}`;
141
- } else {
142
- contentType = "image/png";
143
- }
144
- break;
145
- }
146
- }
147
- return new ImagePart({
148
- image: {
149
- data,
150
- contentType
151
- },
152
- quality
153
- }, constDisableValidation);
154
- }));
155
- }
132
+ data: Schema.Uint8ArrayFromBase64,
133
+ /**
134
+ * The optional MIME type for the image.
135
+ */
136
+ mediaType: /*#__PURE__*/Schema.optional(Schema.String)
137
+ }) {
156
138
  /**
157
139
  * @since 1.0.0
158
140
  */
159
- static fromBlob(blob, quality = "auto") {
160
- return Effect.promise(() => blob.arrayBuffer()).pipe(Effect.map(buffer => new ImagePart({
161
- image: {
162
- contentType: blob.type,
163
- data: new Uint8Array(buffer)
164
- },
165
- quality
166
- })));
167
- }
168
- get asBase64() {
169
- return Encoding.encodeBase64(this.image.data);
170
- }
171
- get asDataUri() {
172
- return `data:${this.image.contentType};base64,${this.asBase64}`;
173
- }
141
+ [PartTypeId] = PartTypeId;
174
142
  }
175
143
  /**
144
+ * Represents an image part of a message with a URL pointing to the image.
145
+ *
176
146
  * @since 1.0.0
177
- * @category parts
147
+ * @category Models
178
148
  */
179
149
  exports.ImagePart = ImagePart;
180
- class ToolCallPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/ToolCallPart")("ToolCall", {
181
- id: _AiResponse.ToolCallId,
182
- name: Schema_.String,
183
- params: Schema_.Unknown
150
+ class ImageUrlPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/ImageUrlPart")("ImageUrlPart", {
151
+ /**
152
+ * The URL that points to the image.
153
+ */
154
+ url: Schema.URL
184
155
  }) {
185
156
  /**
186
157
  * @since 1.0.0
@@ -188,13 +159,25 @@ class ToolCallPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/
188
159
  [PartTypeId] = PartTypeId;
189
160
  }
190
161
  /**
162
+ * Represents a file part of a message with binary file data.
163
+ *
191
164
  * @since 1.0.0
192
- * @category parts
165
+ * @category Models
193
166
  */
194
- exports.ToolCallPart = ToolCallPart;
195
- class ToolCallResolvedPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/AiInput/ToolCallResolvedPart")("ToolCallResolved", {
196
- toolCallId: _AiResponse.ToolCallId,
197
- value: Schema_.Unknown
167
+ exports.ImageUrlPart = ImageUrlPart;
168
+ class FilePart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/FilePart")("FilePart", {
169
+ /**
170
+ * The binary file data.
171
+ */
172
+ data: Schema.Uint8ArrayFromBase64,
173
+ /**
174
+ * The optional name of the file.
175
+ */
176
+ name: /*#__PURE__*/Schema.optional(Schema.String),
177
+ /**
178
+ * The optional MIME type for the image.
179
+ */
180
+ mediaType: /*#__PURE__*/Schema.optional(Schema.String)
198
181
  }) {
199
182
  /**
200
183
  * @since 1.0.0
@@ -202,162 +185,266 @@ class ToolCallResolvedPart extends /*#__PURE__*/Schema_.TaggedClass("@effect/ai/
202
185
  [PartTypeId] = PartTypeId;
203
186
  }
204
187
  /**
188
+ * Represents a file part of a message with a URL pointing to the file.
189
+ *
205
190
  * @since 1.0.0
206
- * @category parts
191
+ * @category Models
207
192
  */
208
- exports.ToolCallResolvedPart = ToolCallResolvedPart;
209
- const isPart = u => Predicate.hasProperty(u, PartTypeId);
193
+ exports.FilePart = FilePart;
194
+ class FileUrlPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/FileUrlPart")("FileUrlPart", {
195
+ /**
196
+ * The URL that points to the file.
197
+ */
198
+ url: Schema.URL
199
+ }) {
200
+ /**
201
+ * @since 1.0.0
202
+ */
203
+ [PartTypeId] = PartTypeId;
204
+ }
210
205
  /**
206
+ * Represents a part of a message containing reasoning that the model used to
207
+ * generate its output.
208
+ *
211
209
  * @since 1.0.0
212
- * @category parts
210
+ * @category Models
213
211
  */
214
- exports.isPart = isPart;
215
- const Part = exports.Part = /*#__PURE__*/Schema_.Union(TextPart, ToolCallPart, ToolCallResolvedPart, ImagePart, ImageUrlPart);
212
+ exports.FileUrlPart = FileUrlPart;
213
+ class ReasoningPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/ReasoningPart")("ReasoningPart", {
214
+ /**
215
+ * The reasoning text that the model used to return the output.
216
+ */
217
+ reasoningText: Schema.String,
218
+ /**
219
+ * An optional signature which verifies that the reasoning text was generated
220
+ * by the model.
221
+ */
222
+ signature: /*#__PURE__*/Schema.optional(Schema.String)
223
+ }) {
224
+ /**
225
+ * @since 1.0.0
226
+ */
227
+ [PartTypeId] = PartTypeId;
228
+ }
229
+ /**
230
+ * Represents a part of a message containing content in the model's reasoning
231
+ * that was encrypted by the model provider for safety reasons.
232
+ *
233
+ * @since 1.0.0
234
+ * @category Models
235
+ */
236
+ exports.ReasoningPart = ReasoningPart;
237
+ class RedactedReasoningPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/RedactedReasoningPart")("RedactedReasoningPart", {
238
+ /**
239
+ * The content in the reasoning that was encrypted by the model provider for
240
+ * safety reasons.
241
+ */
242
+ redactedText: Schema.String
243
+ }) {
244
+ /**
245
+ * @since 1.0.0
246
+ */
247
+ [PartTypeId] = PartTypeId;
248
+ }
216
249
  /**
250
+ * Represents the identifier generated by a model when a tool call is requested.
251
+ *
217
252
  * @since 1.0.0
218
- * @category message
253
+ * @category Models
219
254
  */
220
- const MessageTypeId = exports.MessageTypeId = /*#__PURE__*/Symbol("@effect/ai/AiInput/Message");
255
+ exports.RedactedReasoningPart = RedactedReasoningPart;
256
+ const ToolCallId = exports.ToolCallId = InternalCommon.ToolCallId;
221
257
  /**
258
+ * Represents a part of a message containing a tool call that the model has
259
+ * requested invocation of.
260
+ *
222
261
  * @since 1.0.0
223
- * @category message
262
+ * @category Models
224
263
  */
225
- class Message extends /*#__PURE__*/Schema_.Class("@effect/ai/AiInput/Message")({
226
- role: AiRole.AiRole,
227
- parts: /*#__PURE__*/Schema_.Chunk(Part)
228
- }) {
264
+ class ToolCallPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/ToolCallPart")("ToolCallPart", {
229
265
  /**
230
- * @since 1.0.0
266
+ * The identifier generated by a model when requesting a tool call.
231
267
  */
232
- [MessageTypeId] = MessageTypeId;
268
+ id: ToolCallId,
233
269
  /**
234
- * @since 1.0.0
270
+ * The name of the tool to call.
235
271
  */
236
- static is(u) {
237
- return Predicate.hasProperty(u, MessageTypeId);
238
- }
272
+ name: Schema.String,
239
273
  /**
240
- * @since 1.0.0
274
+ * The arguments to call the tool with as a JSON-serializable object that
275
+ * matches the tool call input schema.
241
276
  */
242
- static fromInput(input, role = AiRole.user) {
243
- if (typeof input === "string") {
244
- return new Message({
245
- role,
246
- parts: Chunk.of(TextPart.fromContent(input))
247
- }, constDisableValidation);
248
- } else if (isPart(input)) {
249
- return new Message({
250
- role,
251
- parts: Chunk.of(input)
252
- }, constDisableValidation);
253
- }
254
- return new Message({
255
- role,
256
- parts: Chunk.fromIterable(input)
257
- }, constDisableValidation);
258
- }
277
+ params: Schema.Unknown
278
+ }) {
259
279
  /**
260
280
  * @since 1.0.0
261
281
  */
262
- static fromResponse(response) {
263
- if (Chunk.isEmpty(response.parts)) {
264
- return Option.none();
265
- }
266
- return Option.some(new Message({
267
- role: response.role,
268
- parts: Chunk.map(response.parts, part => {
269
- switch (part._tag) {
270
- case "Text":
271
- {
272
- return TextPart.fromContent(part.content);
273
- }
274
- case "ToolCall":
275
- {
276
- return new ToolCallPart(part, constDisableValidation);
277
- }
278
- case "ImageUrl":
279
- {
280
- return new ImageUrlPart(part, constDisableValidation);
281
- }
282
- }
283
- })
284
- }, constDisableValidation));
282
+ [PartTypeId] = PartTypeId;
283
+ constructor(props, options) {
284
+ super(props, options);
285
285
  }
286
+ }
287
+ /**
288
+ * Represents a part of a message containing the results of tool calls that the
289
+ * model requested invocation of.
290
+ *
291
+ * @since 1.0.0
292
+ * @category Models
293
+ */
294
+ exports.ToolCallPart = ToolCallPart;
295
+ class ToolCallResultPart extends /*#__PURE__*/Schema.TaggedClass("@effect/ai/AiInput/ToolCallResultPart")("ToolCallResultPart", {
296
+ /**
297
+ * The identifier generated by a model when requesting a tool call.
298
+ */
299
+ id: ToolCallId,
300
+ /**
301
+ * The result of the tool call as a JSON-serializable object.
302
+ */
303
+ result: Schema.Unknown
304
+ }) {
286
305
  /**
287
306
  * @since 1.0.0
288
307
  */
289
- static fromWithResolved(response) {
290
- const toolParts = [];
291
- for (const [toolCallId, value] of response.encoded) {
292
- toolParts.push(new ToolCallResolvedPart({
293
- toolCallId,
294
- value
295
- }, constDisableValidation));
296
- }
297
- const toolPartsChunk = Chunk.unsafeFromArray(toolParts);
298
- return Option.match(Message.fromResponse(response.response), {
299
- onNone: () => new Message({
300
- role: AiRole.model,
301
- parts: toolPartsChunk
302
- }, constDisableValidation),
303
- onSome: message => new Message({
304
- role: message.role,
305
- parts: Chunk.appendAll(message.parts, toolPartsChunk)
306
- }, constDisableValidation)
307
- });
308
- }
308
+ [PartTypeId] = PartTypeId;
309
309
  }
310
310
  /**
311
+ * The valid parts of a user message.
312
+ *
311
313
  * @since 1.0.0
312
- * @category constructors
314
+ * @category Models
313
315
  */
314
- exports.Message = Message;
315
- const make = (input, options) => {
316
- if (typeof input !== "string" && Predicate.isIterable(input)) {
317
- const chunk = Chunk.fromIterable(input);
318
- if (Chunk.isEmpty(chunk)) {
319
- return chunk;
320
- } else if (Message.is(Chunk.unsafeHead(chunk))) {
321
- return chunk;
322
- }
323
- return Chunk.of(Message.fromInput(chunk, options?.role));
324
- } else if (_AiResponse.AiResponse.is(input)) {
325
- return Option.match(Message.fromResponse(input), {
326
- onNone: Chunk.empty,
327
- onSome: Chunk.of
328
- });
329
- } else if (_AiResponse.WithResolved.is(input)) {
330
- return Chunk.of(Message.fromWithResolved(input));
331
- } else if (Message.is(input)) {
332
- return Chunk.of(input);
333
- }
334
- return Chunk.of(Message.fromInput(input, options?.role));
335
- };
316
+ exports.ToolCallResultPart = ToolCallResultPart;
317
+ const UserMessagePart = exports.UserMessagePart = /*#__PURE__*/Schema.Union(TextPart, ImagePart, ImageUrlPart, FilePart, FileUrlPart);
336
318
  /**
319
+ * The valid parts of an assistant message.
320
+ *
337
321
  * @since 1.0.0
338
- * @category constructors
322
+ * @category Models
339
323
  */
340
- exports.make = make;
341
- const empty = exports.empty = /*#__PURE__*/Chunk.empty();
324
+ const AssistantMessagePart = exports.AssistantMessagePart = /*#__PURE__*/Schema.Union(TextPart, ReasoningPart, RedactedReasoningPart, ToolCallPart);
325
+ /**
326
+ * The valid parts of a tool message.
327
+ *
328
+ * @since 1.0.0
329
+ * @category Models
330
+ */
331
+ const ToolMessagePart = exports.ToolMessagePart = ToolCallResultPart;
332
+ /**
333
+ * @since 1.0.0
334
+ * @category Guards
335
+ */
336
+ const is = u => Predicate.hasProperty(u, TypeId);
342
337
  /**
343
338
  * @since 1.0.0
344
- * @category schemas
339
+ * @category Guards
345
340
  */
346
- const Schema = exports.Schema = /*#__PURE__*/Schema_.Chunk(Message);
341
+ exports.is = is;
342
+ const isMessage = u => Predicate.hasProperty(u, MessageTypeId);
343
+ /**
344
+ * @since 1.0.0
345
+ * @category Guards
346
+ */
347
+ exports.isMessage = isMessage;
348
+ const isPart = u => Predicate.hasProperty(u, PartTypeId);
347
349
  /**
348
350
  * @since 1.0.0
349
- * @category schemas
351
+ * @category Constructors
350
352
  */
351
- const SchemaJson = exports.SchemaJson = /*#__PURE__*/Schema_.parseJson(Schema);
353
+ exports.isPart = isPart;
354
+ const empty = exports.empty = /*#__PURE__*/new AiInput({
355
+ messages: []
356
+ });
352
357
  /**
358
+ * Constructs a new `AiInput` from raw user input.
359
+ *
353
360
  * @since 1.0.0
354
- * @category system
361
+ * @category Constructors
355
362
  */
356
- class SystemInstruction extends /*#__PURE__*/Context.Tag("@effect/ai/AiInput/SystemInstruction")() {}
363
+ const make = input => {
364
+ if (Predicate.isString(input)) {
365
+ const textPart = new TextPart({
366
+ text: input
367
+ });
368
+ const message = new UserMessage({
369
+ parts: [textPart]
370
+ });
371
+ return new AiInput({
372
+ messages: [message]
373
+ });
374
+ }
375
+ if (isMessage(input)) {
376
+ return new AiInput({
377
+ messages: [input]
378
+ });
379
+ }
380
+ if (Predicate.isIterable(input)) {
381
+ return new AiInput({
382
+ messages: Array.from(input)
383
+ });
384
+ }
385
+ if (is(input)) {
386
+ return input;
387
+ }
388
+ if (AiResponse.isStructured(input)) {
389
+ const assistantMessages = fromResponse(input).messages;
390
+ const toolPart = new ToolCallResultPart({
391
+ id: input.id,
392
+ result: input.value
393
+ });
394
+ const toolMessage = new ToolMessage({
395
+ parts: [toolPart]
396
+ });
397
+ return new AiInput({
398
+ messages: [...assistantMessages, toolMessage]
399
+ });
400
+ }
401
+ if (AiResponse.hasToolCallResults(input)) {
402
+ const assistantMessages = fromResponse(input).messages;
403
+ const toolParts = [];
404
+ for (const [id, result] of input.encodedResults) {
405
+ toolParts.push(new ToolCallResultPart({
406
+ id,
407
+ result
408
+ }));
409
+ }
410
+ const toolMessage = new ToolMessage({
411
+ parts: toolParts
412
+ });
413
+ return new AiInput({
414
+ messages: [...assistantMessages, toolMessage]
415
+ });
416
+ }
417
+ return fromResponse(input);
418
+ };
419
+ exports.make = make;
420
+ const EXCLUDED_RESPONSE_PARTS = ["MetadataPart", "ReasoningPart", "RedactedReasoningPart", "FinishPart"];
421
+ const validResponseParts = part => !EXCLUDED_RESPONSE_PARTS.includes(part._tag);
422
+ const fromResponse = response => {
423
+ if (response.parts.length === 0) {
424
+ return empty;
425
+ }
426
+ const parts = response.parts.filter(validResponseParts).map(part => part._tag === "TextPart" ? new TextPart({
427
+ text: part.text
428
+ }) : new ToolCallPart({
429
+ id: part.id,
430
+ name: part.name,
431
+ params: part.params
432
+ }));
433
+ const message = new AssistantMessage({
434
+ parts
435
+ });
436
+ return new AiInput({
437
+ messages: [message]
438
+ });
439
+ };
357
440
  /**
441
+ * Concatenates the messages of one `AiInput` onto the messages of another,
442
+ * creating a new `AiInput` with the messages from both.
443
+ *
358
444
  * @since 1.0.0
359
- * @category system
445
+ * @category Combination
360
446
  */
361
- exports.SystemInstruction = SystemInstruction;
362
- const provideSystem = exports.provideSystem = /*#__PURE__*/(0, _Function.dual)(2, (effect, input) => Effect.provideService(effect, SystemInstruction, input));
447
+ const concat = exports.concat = /*#__PURE__*/(0, _Function.dual)(2, (self, other) => AiInput.make({
448
+ messages: [...self.messages, ...other.messages]
449
+ }));
363
450
  //# sourceMappingURL=AiInput.js.map