@effect/ai 0.26.1 → 0.27.1

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 +2 -2
  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 +635 -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 +4 -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 +884 -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 +2 -2
  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 +612 -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 +328 -192
  102. package/src/Model.ts +155 -0
  103. package/src/Prompt.ts +1621 -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 +1361 -0
  108. package/src/Toolkit.ts +516 -0
  109. package/src/index.ts +499 -13
  110. package/AiChat/package.json +0 -6
  111. package/AiEmbeddingModel/package.json +0 -6
  112. package/AiInput/package.json +0 -6
  113. package/AiLanguageModel/package.json +0 -6
  114. package/AiModel/package.json +0 -6
  115. package/AiResponse/package.json +0 -6
  116. package/AiTelemetry/package.json +0 -6
  117. package/AiTool/package.json +0 -6
  118. package/AiToolkit/package.json +0 -6
  119. package/dist/cjs/AiChat.js +0 -122
  120. package/dist/cjs/AiChat.js.map +0 -1
  121. package/dist/cjs/AiEmbeddingModel.js +0 -109
  122. package/dist/cjs/AiEmbeddingModel.js.map +0 -1
  123. package/dist/cjs/AiInput.js +0 -458
  124. package/dist/cjs/AiInput.js.map +0 -1
  125. package/dist/cjs/AiLanguageModel.js +0 -351
  126. package/dist/cjs/AiLanguageModel.js.map +0 -1
  127. package/dist/cjs/AiModel.js +0 -37
  128. package/dist/cjs/AiModel.js.map +0 -1
  129. package/dist/cjs/AiResponse.js +0 -681
  130. package/dist/cjs/AiResponse.js.map +0 -1
  131. package/dist/cjs/AiTelemetry.js +0 -58
  132. package/dist/cjs/AiTelemetry.js.map +0 -1
  133. package/dist/cjs/AiTool.js +0 -150
  134. package/dist/cjs/AiTool.js.map +0 -1
  135. package/dist/cjs/AiToolkit.js +0 -157
  136. package/dist/cjs/AiToolkit.js.map +0 -1
  137. package/dist/cjs/internal/common.js +0 -21
  138. package/dist/cjs/internal/common.js.map +0 -1
  139. package/dist/dts/AiChat.d.ts +0 -101
  140. package/dist/dts/AiChat.d.ts.map +0 -1
  141. package/dist/dts/AiEmbeddingModel.d.ts +0 -65
  142. package/dist/dts/AiEmbeddingModel.d.ts.map +0 -1
  143. package/dist/dts/AiInput.d.ts +0 -590
  144. package/dist/dts/AiInput.d.ts.map +0 -1
  145. package/dist/dts/AiLanguageModel.d.ts +0 -302
  146. package/dist/dts/AiLanguageModel.d.ts.map +0 -1
  147. package/dist/dts/AiModel.d.ts +0 -25
  148. package/dist/dts/AiModel.d.ts.map +0 -1
  149. package/dist/dts/AiResponse.d.ts +0 -863
  150. package/dist/dts/AiResponse.d.ts.map +0 -1
  151. package/dist/dts/AiTelemetry.d.ts +0 -242
  152. package/dist/dts/AiTelemetry.d.ts.map +0 -1
  153. package/dist/dts/AiTool.d.ts +0 -334
  154. package/dist/dts/AiTool.d.ts.map +0 -1
  155. package/dist/dts/AiToolkit.d.ts +0 -96
  156. package/dist/dts/AiToolkit.d.ts.map +0 -1
  157. package/dist/dts/internal/common.d.ts +0 -2
  158. package/dist/dts/internal/common.d.ts.map +0 -1
  159. package/dist/esm/AiChat.js +0 -111
  160. package/dist/esm/AiChat.js.map +0 -1
  161. package/dist/esm/AiEmbeddingModel.js +0 -98
  162. package/dist/esm/AiEmbeddingModel.js.map +0 -1
  163. package/dist/esm/AiInput.js +0 -433
  164. package/dist/esm/AiInput.js.map +0 -1
  165. package/dist/esm/AiLanguageModel.js +0 -340
  166. package/dist/esm/AiLanguageModel.js.map +0 -1
  167. package/dist/esm/AiModel.js +0 -29
  168. package/dist/esm/AiModel.js.map +0 -1
  169. package/dist/esm/AiResponse.js +0 -657
  170. package/dist/esm/AiResponse.js.map +0 -1
  171. package/dist/esm/AiTelemetry.js +0 -48
  172. package/dist/esm/AiTelemetry.js.map +0 -1
  173. package/dist/esm/AiTool.js +0 -134
  174. package/dist/esm/AiTool.js.map +0 -1
  175. package/dist/esm/AiToolkit.js +0 -147
  176. package/dist/esm/AiToolkit.js.map +0 -1
  177. package/dist/esm/internal/common.js +0 -14
  178. package/dist/esm/internal/common.js.map +0 -1
  179. package/src/AiChat.ts +0 -251
  180. package/src/AiEmbeddingModel.ts +0 -169
  181. package/src/AiInput.ts +0 -602
  182. package/src/AiLanguageModel.ts +0 -685
  183. package/src/AiModel.ts +0 -53
  184. package/src/AiResponse.ts +0 -986
  185. package/src/AiTelemetry.ts +0 -333
  186. package/src/AiTool.ts +0 -579
  187. package/src/AiToolkit.ts +0 -265
  188. package/src/internal/common.ts +0 -12
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Chat.js",
4
+ "module": "../dist/esm/Chat.js",
5
+ "types": "../dist/dts/Chat.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/EmbeddingModel.js",
4
+ "module": "../dist/esm/EmbeddingModel.js",
5
+ "types": "../dist/dts/EmbeddingModel.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/IdGenerator.js",
4
+ "module": "../dist/esm/IdGenerator.js",
5
+ "types": "../dist/dts/IdGenerator.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/LanguageModel.js",
4
+ "module": "../dist/esm/LanguageModel.js",
5
+ "types": "../dist/dts/LanguageModel.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Model.js",
4
+ "module": "../dist/esm/Model.js",
5
+ "types": "../dist/dts/Model.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Prompt.js",
4
+ "module": "../dist/esm/Prompt.js",
5
+ "types": "../dist/dts/Prompt.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Response.js",
4
+ "module": "../dist/esm/Response.js",
5
+ "types": "../dist/dts/Response.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Telemetry.js",
4
+ "module": "../dist/esm/Telemetry.js",
5
+ "types": "../dist/dts/Telemetry.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Tool.js",
4
+ "module": "../dist/esm/Tool.js",
5
+ "types": "../dist/dts/Tool.d.ts"
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": [],
3
+ "main": "../dist/cjs/Toolkit.js",
4
+ "module": "../dist/esm/Toolkit.js",
5
+ "types": "../dist/dts/Toolkit.d.ts"
6
+ }
@@ -3,45 +3,609 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.TypeId = exports.AiError = void 0;
6
+ exports.isAiError = exports.UnknownError = exports.TypeId = exports.MalformedOutput = exports.MalformedInput = exports.HttpResponseError = exports.HttpResponseDetails = exports.HttpRequestError = exports.HttpRequestDetails = exports.AiError = void 0;
7
7
  var Predicate = _interopRequireWildcard(require("effect/Predicate"));
8
8
  var Schema = _interopRequireWildcard(require("effect/Schema"));
9
9
  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); }
10
10
  /**
11
+ * Unique identifier for AI errors.
12
+ *
11
13
  * @since 1.0.0
14
+ * @category Type Ids
12
15
  */
13
-
16
+ const TypeId = exports.TypeId = "~@effect/ai/AiError";
14
17
  /**
18
+ * Type guard to check if a value is an AI error.
19
+ *
20
+ * @param u - The value to check
21
+ * @returns `true` if the value is an `AiError`, `false` otherwise
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { AiError } from "@effect/ai"
26
+ *
27
+ * const someError = new Error("generic error")
28
+ * const aiError = new AiError.UnknownError({
29
+ * module: "Test",
30
+ * method: "example"
31
+ * })
32
+ *
33
+ * console.log(AiError.isAiError(someError)) // false
34
+ * console.log(AiError.isAiError(aiError)) // true
35
+ * ```
36
+ *
15
37
  * @since 1.0.0
16
- * @category type ids
38
+ * @category Guards
17
39
  */
18
- const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiError");
40
+ const isAiError = u => Predicate.hasProperty(u, TypeId);
41
+ // =============================================================================
42
+ // Http Request Error
43
+ // =============================================================================
44
+ /**
45
+ * Schema for HTTP request details used in error reporting.
46
+ *
47
+ * Captures comprehensive information about HTTP requests that failed,
48
+ * enabling detailed error analysis and debugging.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { AiError } from "@effect/ai"
53
+ * import { Option } from "effect"
54
+ *
55
+ * const requestDetails: typeof AiError.HttpRequestDetails.Type = {
56
+ * method: "POST",
57
+ * url: "https://api.openai.com/v1/completions",
58
+ * urlParams: [["model", "gpt-4"], ["stream", "false"]],
59
+ * hash: Option.some("#section1"),
60
+ * headers: {
61
+ * "Content-Type": "application/json",
62
+ * "Authorization": "Bearer sk-..."
63
+ * }
64
+ * }
65
+ * ```
66
+ *
67
+ * @since 1.0.0
68
+ * @category Schemas
69
+ */
70
+ exports.isAiError = isAiError;
71
+ const HttpRequestDetails = exports.HttpRequestDetails = /*#__PURE__*/Schema.Struct({
72
+ method: Schema.Literal("GET", "POST", "PATCH", "PUT", "DELETE", "HEAD", "OPTIONS"),
73
+ url: Schema.String,
74
+ urlParams: Schema.Array(Schema.Tuple(Schema.String, Schema.String)),
75
+ hash: Schema.Option(Schema.String),
76
+ headers: Schema.Record({
77
+ key: Schema.String,
78
+ value: Schema.String
79
+ })
80
+ }).annotations({
81
+ identifier: "HttpRequestDetails"
82
+ });
19
83
  /**
84
+ * Error that occurs during HTTP request processing.
85
+ *
86
+ * This error is raised when issues arise before receiving an HTTP response,
87
+ * such as network connectivity problems, request encoding issues, or invalid
88
+ * URLs.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * import { AiError } from "@effect/ai"
93
+ * import { Effect } from "effect"
94
+ *
95
+ * const handleNetworkError = Effect.gen(function* () {
96
+ * const error = new AiError.HttpRequestError({
97
+ * module: "OpenAI",
98
+ * method: "createCompletion",
99
+ * reason: "Transport",
100
+ * request: {
101
+ * method: "POST",
102
+ * url: "https://api.openai.com/v1/completions",
103
+ * urlParams: [],
104
+ * hash: Option.none(),
105
+ * headers: { "Content-Type": "application/json" }
106
+ * },
107
+ * description: "Connection timeout after 30 seconds"
108
+ * })
109
+ *
110
+ * console.log(error.message)
111
+ * // "Transport: Connection timeout after 30 seconds (POST https://api.openai.com/v1/completions)"
112
+ * })
113
+ * ```
114
+ *
20
115
  * @since 1.0.0
21
- * @category errors
116
+ * @category Errors
22
117
  */
23
- class AiError extends /*#__PURE__*/Schema.TaggedError("@effect/ai/AiError")("AiError", {
118
+ class HttpRequestError extends /*#__PURE__*/Schema.TaggedError("@effect/ai/AiError/HttpRequestError")("HttpRequestError", {
24
119
  module: Schema.String,
25
120
  method: Schema.String,
26
- description: Schema.String,
121
+ reason: /*#__PURE__*/Schema.Literal("Transport", "Encode", "InvalidUrl"),
122
+ request: HttpRequestDetails,
123
+ description: /*#__PURE__*/Schema.optional(Schema.String),
27
124
  cause: /*#__PURE__*/Schema.optional(Schema.Defect)
28
125
  }) {
29
126
  /**
30
127
  * @since 1.0.0
31
128
  */
32
- static is(u) {
33
- return Predicate.hasProperty(u, TypeId);
129
+ [TypeId] = TypeId;
130
+ /**
131
+ * Creates an HttpRequestError from a platform HttpClientError.RequestError.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * import { AiError } from "@effect/ai"
136
+ * import { HttpClientError } from "@effect/platform"
137
+ * import { Option } from "effect"
138
+ *
139
+ * declare const platformError: HttpClientError.RequestError
140
+ *
141
+ * const aiError = AiError.HttpRequestError.fromRequestError({
142
+ * module: "ChatGPT",
143
+ * method: "sendMessage",
144
+ * error: platformError
145
+ * })
146
+ * ```
147
+ *
148
+ * @since 1.0.0
149
+ * @category Constructors
150
+ */
151
+ static fromRequestError({
152
+ error,
153
+ ...params
154
+ }) {
155
+ return new HttpRequestError({
156
+ ...params,
157
+ cause: error,
158
+ description: error.description,
159
+ reason: error.reason,
160
+ request: {
161
+ hash: error.request.hash,
162
+ headers: error.request.headers,
163
+ method: error.request.method,
164
+ url: error.request.url,
165
+ urlParams: error.request.urlParams
166
+ }
167
+ });
34
168
  }
169
+ get message() {
170
+ const methodAndUrl = `${this.request.method} ${this.request.url}`;
171
+ let baseMessage = this.description ? `${this.reason}: ${this.description}` : `${this.reason}: An HTTP request error occurred.`;
172
+ baseMessage += ` (${methodAndUrl})`;
173
+ let suggestion = "";
174
+ switch (this.reason) {
175
+ case "Encode":
176
+ {
177
+ suggestion += "Check that the request body data is properly formatted and matches the expected content type.";
178
+ break;
179
+ }
180
+ case "InvalidUrl":
181
+ {
182
+ suggestion += "Verify that the URL format is correct and that all required parameters have been provided.";
183
+ suggestion += " Check for any special characters that may need encoding.";
184
+ break;
185
+ }
186
+ case "Transport":
187
+ {
188
+ suggestion += "Check your network connection and verify that the requested URL is accessible.";
189
+ break;
190
+ }
191
+ }
192
+ baseMessage += `\n\nSuggestion: ${suggestion}`;
193
+ return baseMessage;
194
+ }
195
+ }
196
+ // =============================================================================
197
+ // Http Response Error
198
+ // =============================================================================
199
+ /**
200
+ * Schema for HTTP response details used in error reporting.
201
+ *
202
+ * Captures essential information about HTTP responses that caused errors,
203
+ * including status codes and headers for debugging purposes.
204
+ *
205
+ * @example
206
+ * ```ts
207
+ * import { AiError } from "@effect/ai"
208
+ *
209
+ * const responseDetails: typeof AiError.HttpResponseDetails.Type = {
210
+ * status: 429,
211
+ * headers: {
212
+ * "Content-Type": "application/json",
213
+ * "X-RateLimit-Remaining": "0",
214
+ * "Retry-After": "60"
215
+ * }
216
+ * }
217
+ * ```
218
+ *
219
+ * @since 1.0.0
220
+ * @category Schemas
221
+ */
222
+ exports.HttpRequestError = HttpRequestError;
223
+ const HttpResponseDetails = exports.HttpResponseDetails = /*#__PURE__*/Schema.Struct({
224
+ status: Schema.Number,
225
+ headers: Schema.Record({
226
+ key: Schema.String,
227
+ value: Schema.String
228
+ })
229
+ }).annotations({
230
+ identifier: "HttpResponseDetails"
231
+ });
232
+ /**
233
+ * Error that occurs during HTTP response processing.
234
+ *
235
+ * This error is thrown when issues arise after receiving an HTTP response,
236
+ * such as unexpected status codes, response decoding failures, or empty
237
+ * response bodies.
238
+ *
239
+ * @example
240
+ * ```ts
241
+ * import { AiError } from "@effect/ai"
242
+ * import { Option } from "effect"
243
+ *
244
+ * const responseError = new AiError.HttpResponseError({
245
+ * module: "OpenAI",
246
+ * method: "createCompletion",
247
+ * reason: "StatusCode",
248
+ * request: {
249
+ * method: "POST",
250
+ * url: "https://api.openai.com/v1/completions",
251
+ * urlParams: [],
252
+ * hash: Option.none(),
253
+ * headers: { "Authorization": "Bearer sk-..." }
254
+ * },
255
+ * response: {
256
+ * status: 429,
257
+ * headers: { "X-RateLimit-Remaining": "0" }
258
+ * },
259
+ * description: "Rate limit exceeded"
260
+ * })
261
+ *
262
+ * console.log(responseError.message)
263
+ * // "StatusCode: Rate limit exceeded (429 POST https://api.openai.com/v1/completions)"
264
+ * ```
265
+ *
266
+ * @since 1.0.0
267
+ * @category Errors
268
+ */
269
+ class HttpResponseError extends /*#__PURE__*/Schema.TaggedError("@effect/ai/AiError/HttpResponseError")("HttpResponseError", {
270
+ module: Schema.String,
271
+ method: Schema.String,
272
+ request: HttpRequestDetails,
273
+ response: HttpResponseDetails,
274
+ reason: /*#__PURE__*/Schema.Literal("StatusCode", "Decode", "EmptyBody"),
275
+ description: /*#__PURE__*/Schema.optional(Schema.String),
276
+ cause: /*#__PURE__*/Schema.optional(Schema.Defect)
277
+ }) {
35
278
  /**
36
279
  * @since 1.0.0
37
280
  */
38
281
  [TypeId] = TypeId;
39
282
  /**
283
+ * Creates an HttpResponseError from a platform HttpClientError.ResponseError.
284
+ *
285
+ * @example
286
+ * ```ts
287
+ * import { AiError } from "@effect/ai"
288
+ * import { Headers, HttpClientError } from "@effect/platform"
289
+ * import { Option } from "effect"
290
+ *
291
+ * declare const platformError: HttpClientError.ResponseError
292
+ *
293
+ * const aiError = AiError.HttpResponseError.fromResponseError({
294
+ * module: "OpenAI",
295
+ * method: "completion",
296
+ * error: platformError
297
+ * })
298
+ * ```
299
+ *
40
300
  * @since 1.0.0
301
+ * @category Constructors
41
302
  */
303
+ static fromResponseError({
304
+ error,
305
+ ...params
306
+ }) {
307
+ return new HttpResponseError({
308
+ ...params,
309
+ cause: error,
310
+ description: error.description,
311
+ reason: error.reason,
312
+ request: {
313
+ hash: error.request.hash,
314
+ headers: error.request.headers,
315
+ method: error.request.method,
316
+ url: error.request.url,
317
+ urlParams: error.request.urlParams
318
+ },
319
+ response: {
320
+ headers: error.response.headers,
321
+ status: error.response.status
322
+ }
323
+ });
324
+ }
42
325
  get message() {
43
- return `${this.module}.${this.method}: ${this.description}`;
326
+ const methodUrlStatus = `${this.response.status} ${this.request.method} ${this.request.url}`;
327
+ let baseMessage = this.description ? `${this.reason}: ${this.description}` : `${this.reason}: An HTTP response error occurred.`;
328
+ baseMessage += ` (${methodUrlStatus})`;
329
+ let suggestion = "";
330
+ switch (this.reason) {
331
+ case "Decode":
332
+ {
333
+ suggestion += "The response format does not match what is expected. " + "Verify API version compatibility, check response content-type, " + "and/or examine if the endpoint schema has changed.";
334
+ break;
335
+ }
336
+ case "EmptyBody":
337
+ {
338
+ suggestion += "The response body was empty. This may indicate a server " + "issue, API version mismatch, or the endpoint may have changed its response format.";
339
+ break;
340
+ }
341
+ case "StatusCode":
342
+ {
343
+ suggestion += getStatusCodeSuggestion(this.response.status);
344
+ break;
345
+ }
346
+ }
347
+ baseMessage += `\n\nSuggestion: ${suggestion}`;
348
+ return baseMessage;
44
349
  }
45
350
  }
46
- exports.AiError = AiError;
351
+ // =============================================================================
352
+ // Malformed Input Error
353
+ // =============================================================================
354
+ /**
355
+ * Error thrown when input data doesn't match the expected format or schema.
356
+ *
357
+ * This error occurs when the data provided to an AI operation fails validation,
358
+ * is missing required fields, or doesn't conform to the expected structure.
359
+ *
360
+ * @example
361
+ * ```ts
362
+ * import { AiError } from "@effect/ai"
363
+ * import { Effect } from "effect"
364
+ *
365
+ * const validateInput = (data: unknown) =>
366
+ * typeof data === "string" && data.length > 0
367
+ * ? Effect.succeed(data)
368
+ * : Effect.fail(new AiError.MalformedInput({
369
+ * module: "ChatBot",
370
+ * method: "processMessage",
371
+ * description: "Input must be a non-empty string"
372
+ * }))
373
+ *
374
+ * const program = validateInput("").pipe(
375
+ * Effect.catchTag("MalformedInput", (error) => {
376
+ * console.log(`Input validation failed: ${error.description}`)
377
+ * return Effect.succeed("Please provide a valid message")
378
+ * })
379
+ * )
380
+ * ```
381
+ *
382
+ * @since 1.0.0
383
+ * @category Errors
384
+ */
385
+ exports.HttpResponseError = HttpResponseError;
386
+ class MalformedInput extends /*#__PURE__*/Schema.TaggedError("@effect/ai/AiError/MalformedInput")("MalformedInput", {
387
+ module: Schema.String,
388
+ method: Schema.String,
389
+ description: /*#__PURE__*/Schema.optional(Schema.String),
390
+ cause: /*#__PURE__*/Schema.optional(Schema.Defect)
391
+ }) {
392
+ /**
393
+ * @since 1.0.0
394
+ */
395
+ [TypeId] = TypeId;
396
+ }
397
+ // =============================================================================
398
+ // Malformed Output Error
399
+ // =============================================================================
400
+ /**
401
+ * Error thrown when output data can't be parsed or validated.
402
+ *
403
+ * This error occurs when AI service responses don't match the expected format,
404
+ * contain invalid data structures, or fail schema validation during parsing.
405
+ *
406
+ * @example
407
+ * ```ts
408
+ * import { AiError } from "@effect/ai"
409
+ * import { Effect, Schema } from "effect"
410
+ *
411
+ * const ResponseSchema = Schema.Struct({
412
+ * message: Schema.String,
413
+ * tokens: Schema.Number
414
+ * })
415
+ *
416
+ * const parseResponse = (data: unknown) =>
417
+ * Schema.decodeUnknown(ResponseSchema)(data).pipe(
418
+ * Effect.mapError(parseError =>
419
+ * new AiError.MalformedOutput({
420
+ * module: "OpenAI",
421
+ * method: "completion",
422
+ * description: "Response doesn't match expected schema",
423
+ * cause: parseError
424
+ * })
425
+ * )
426
+ * )
427
+ *
428
+ * const program = parseResponse({ invalid: "data" }).pipe(
429
+ * Effect.catchTag("MalformedOutput", (error) => {
430
+ * console.log(`Parsing failed: ${error.description}`)
431
+ * return Effect.succeed({ message: "Error", tokens: 0 })
432
+ * })
433
+ * )
434
+ * ```
435
+ *
436
+ * @since 1.0.0
437
+ * @category Errors
438
+ */
439
+ exports.MalformedInput = MalformedInput;
440
+ class MalformedOutput extends /*#__PURE__*/Schema.TaggedError("@effect/ai/AiError/MalformedOutput")("MalformedOutput", {
441
+ module: Schema.String,
442
+ method: Schema.String,
443
+ description: /*#__PURE__*/Schema.optional(Schema.String),
444
+ cause: /*#__PURE__*/Schema.optional(Schema.Defect)
445
+ }) {
446
+ /**
447
+ * @since 1.0.0
448
+ */
449
+ [TypeId] = TypeId;
450
+ /**
451
+ * Creates a MalformedOutput error from a Schema ParseError.
452
+ *
453
+ * @example
454
+ * ```ts
455
+ * import { AiError } from "@effect/ai"
456
+ * import { Effect, Schema } from "effect"
457
+ *
458
+ * const UserSchema = Schema.Struct({
459
+ * name: Schema.String,
460
+ * age: Schema.Number
461
+ * })
462
+ *
463
+ * const parseUser = (data: unknown) =>
464
+ * Schema.decodeUnknown(UserSchema)(data).pipe(
465
+ * Effect.mapError((parseError) =>
466
+ * AiError.MalformedOutput.fromParseError({
467
+ * module: "UserService",
468
+ * method: "parseUserData",
469
+ * error: parseError
470
+ * })
471
+ * )
472
+ * )
473
+ * ```
474
+ *
475
+ * @since 1.0.0
476
+ * @category Constructors
477
+ */
478
+ static fromParseError({
479
+ error,
480
+ ...params
481
+ }) {
482
+ // TODO(Max): enhance
483
+ return new MalformedOutput({
484
+ ...params,
485
+ cause: error
486
+ });
487
+ }
488
+ }
489
+ // =============================================================================
490
+ // Unknown Error
491
+ // =============================================================================
492
+ /**
493
+ * Catch-all error for unexpected runtime errors in AI operations.
494
+ *
495
+ * This error is used when an unexpected exception occurs that doesn't fit
496
+ * into the other specific error categories. It provides context about where
497
+ * the error occurred and preserves the original cause for debugging.
498
+ *
499
+ * @example
500
+ * ```ts
501
+ * import { AiError } from "@effect/ai"
502
+ * import { Effect } from "effect"
503
+ *
504
+ * const riskyOperation = () => {
505
+ * try {
506
+ * // Some operation that might throw
507
+ * throw new Error("Unexpected network issue")
508
+ * } catch (cause) {
509
+ * return Effect.fail(new AiError.UnknownError({
510
+ * module: "ChatService",
511
+ * method: "sendMessage",
512
+ * description: "An unexpected error occurred during message processing",
513
+ * cause
514
+ * }))
515
+ * }
516
+ * }
517
+ *
518
+ * const program = riskyOperation().pipe(
519
+ * Effect.catchTag("UnknownError", (error) => {
520
+ * console.log(error.message)
521
+ * // "ChatService.sendMessage: An unexpected error occurred during message processing"
522
+ * return Effect.succeed("Service temporarily unavailable")
523
+ * })
524
+ * )
525
+ * ```
526
+ *
527
+ * @since 1.0.0
528
+ * @category Errors
529
+ */
530
+ exports.MalformedOutput = MalformedOutput;
531
+ class UnknownError extends /*#__PURE__*/Schema.TaggedError("@effect/ai/UnknownError")("UnknownError", {
532
+ module: Schema.String,
533
+ method: Schema.String,
534
+ description: /*#__PURE__*/Schema.optional(Schema.String),
535
+ cause: /*#__PURE__*/Schema.optional(Schema.Defect)
536
+ }) {
537
+ /**
538
+ * @since 1.0.0
539
+ */
540
+ [TypeId] = TypeId;
541
+ /**
542
+ * @since 1.0.0
543
+ */
544
+ get message() {
545
+ const moduleMethod = `${this.module}.${this.method}`;
546
+ return Predicate.isUndefined(this.description) ? `${moduleMethod}: An error occurred` : `${moduleMethod}: ${this.description}`;
547
+ }
548
+ }
549
+ /**
550
+ * Schema for validating and parsing AI errors.
551
+ *
552
+ * This schema can be used to decode unknown values into properly typed AI
553
+ * errors, ensuring type safety when handling errors from external sources or
554
+ * serialized data.
555
+ *
556
+ * @example
557
+ * ```ts
558
+ * import { AiError } from "@effect/ai"
559
+ * import { Schema, Effect } from "effect"
560
+ *
561
+ * const parseAiError = (data: unknown) =>
562
+ * Schema.decodeUnknown(AiError.AiError)(data).pipe(
563
+ * Effect.map(error => {
564
+ * console.log(`Parsed AI error: ${error._tag}`)
565
+ * return error
566
+ * }),
567
+ * Effect.catchAll(() =>
568
+ * Effect.succeed(new AiError.UnknownError({
569
+ * module: "Parser",
570
+ * method: "parseAiError",
571
+ * description: "Failed to parse error data"
572
+ * }))
573
+ * )
574
+ * )
575
+ * ```
576
+ *
577
+ * @since 1.0.0
578
+ * @category Schemas
579
+ */
580
+ exports.UnknownError = UnknownError;
581
+ const AiError = exports.AiError = /*#__PURE__*/Schema.Union(HttpRequestError, HttpResponseError, MalformedInput, MalformedOutput, UnknownError);
582
+ // =============================================================================
583
+ // Utilities
584
+ // =============================================================================
585
+ const getStatusCodeSuggestion = statusCode => {
586
+ if (statusCode >= 400 && statusCode < 500) {
587
+ switch (statusCode) {
588
+ case 400:
589
+ return "Bad Request - Check request parameters, headers, and body format against API documentation.";
590
+ case 401:
591
+ return "Unauthorized - Verify API key, authentication credentials, or token expiration.";
592
+ case 403:
593
+ return "Forbidden - Check API permissions, usage limits, or resource access rights.";
594
+ case 404:
595
+ return "Not Found - Verify the endpoint URL, API version, and resource identifiers.";
596
+ case 408:
597
+ return "Request Timeout - Consider increasing timeout duration or implementing retry logic.";
598
+ case 422:
599
+ return "Unprocessable Entity - Check request data validation, required fields, and data formats.";
600
+ case 429:
601
+ return "Rate Limited - Implement exponential backoff or reduce request frequency.";
602
+ default:
603
+ return "Client error - Review request format, parameters, and API documentation.";
604
+ }
605
+ } else if (statusCode >= 500) {
606
+ return "Server error - This is likely temporary. Implement retry logic with exponential backoff.";
607
+ } else {
608
+ return "Check API documentation for this status code.";
609
+ }
610
+ };
47
611
  //# sourceMappingURL=AiError.js.map