@effect/ai-openai-compat 4.0.4 → 4.0.6
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.
- package/ai-docs/package.json +1 -1
- package/ai-docs/src/06_schedule/10_schedules.ts +5 -4
- package/ai-docs/src/52_graphql/10_basics.ts +19 -13
- package/ai-docs/src/52_graphql/30_batching.ts +6 -4
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +6 -4
- package/ai-docs/tsconfig.json +3 -1
- package/dist/OpenAiClient.d.ts.map +1 -1
- package/dist/OpenAiClient.js +3 -2
- package/dist/OpenAiClient.js.map +1 -1
- package/dist/OpenAiEmbeddingModel.d.ts +4 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -1
- package/dist/OpenAiEmbeddingModel.js +6 -0
- package/dist/OpenAiEmbeddingModel.js.map +1 -1
- package/dist/OpenAiError.d.ts +10 -10
- package/dist/OpenAiError.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.d.ts +16 -16
- package/dist/OpenAiLanguageModel.d.ts.map +1 -1
- package/dist/OpenAiLanguageModel.js +19 -15
- package/dist/OpenAiLanguageModel.js.map +1 -1
- package/dist/internal/errors.js +34 -22
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiClient.ts +3 -2
- package/src/OpenAiEmbeddingModel.ts +9 -43
- package/src/OpenAiError.ts +10 -10
- package/src/OpenAiLanguageModel.ts +40 -70
- package/src/internal/errors.ts +29 -22
package/src/OpenAiError.ts
CHANGED
|
@@ -77,7 +77,7 @@ declare module "effect/ai/AiError" {
|
|
|
77
77
|
* @since 4.0.0
|
|
78
78
|
*/
|
|
79
79
|
export interface RateLimitErrorMetadata {
|
|
80
|
-
readonly openai?: OpenAiRateLimitMetadata | null
|
|
80
|
+
readonly "openai-compat"?: OpenAiRateLimitMetadata | null
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
@@ -88,7 +88,7 @@ declare module "effect/ai/AiError" {
|
|
|
88
88
|
* @since 4.0.0
|
|
89
89
|
*/
|
|
90
90
|
export interface QuotaExhaustedErrorMetadata {
|
|
91
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
91
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
@@ -99,7 +99,7 @@ declare module "effect/ai/AiError" {
|
|
|
99
99
|
* @since 4.0.0
|
|
100
100
|
*/
|
|
101
101
|
export interface AuthenticationErrorMetadata {
|
|
102
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
102
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
@@ -110,7 +110,7 @@ declare module "effect/ai/AiError" {
|
|
|
110
110
|
* @since 4.0.0
|
|
111
111
|
*/
|
|
112
112
|
export interface ContentPolicyErrorMetadata {
|
|
113
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
113
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
/**
|
|
@@ -121,7 +121,7 @@ declare module "effect/ai/AiError" {
|
|
|
121
121
|
* @since 4.0.0
|
|
122
122
|
*/
|
|
123
123
|
export interface InvalidRequestErrorMetadata {
|
|
124
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
124
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
/**
|
|
@@ -132,7 +132,7 @@ declare module "effect/ai/AiError" {
|
|
|
132
132
|
* @since 4.0.0
|
|
133
133
|
*/
|
|
134
134
|
export interface InternalProviderErrorMetadata {
|
|
135
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
135
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
/**
|
|
@@ -143,7 +143,7 @@ declare module "effect/ai/AiError" {
|
|
|
143
143
|
* @since 4.0.0
|
|
144
144
|
*/
|
|
145
145
|
export interface InvalidOutputErrorMetadata {
|
|
146
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
146
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
/**
|
|
@@ -154,7 +154,7 @@ declare module "effect/ai/AiError" {
|
|
|
154
154
|
* @since 4.0.0
|
|
155
155
|
*/
|
|
156
156
|
export interface StructuredOutputErrorMetadata {
|
|
157
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
157
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
/**
|
|
@@ -165,7 +165,7 @@ declare module "effect/ai/AiError" {
|
|
|
165
165
|
* @since 4.0.0
|
|
166
166
|
*/
|
|
167
167
|
export interface UnsupportedSchemaErrorMetadata {
|
|
168
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
168
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
/**
|
|
@@ -176,6 +176,6 @@ declare module "effect/ai/AiError" {
|
|
|
176
176
|
* @since 4.0.0
|
|
177
177
|
*/
|
|
178
178
|
export interface UnknownErrorMetadata {
|
|
179
|
-
readonly openai?: OpenAiErrorMetadata | null
|
|
179
|
+
readonly "openai-compat"?: OpenAiErrorMetadata | null
|
|
180
180
|
}
|
|
181
181
|
}
|
|
@@ -16,6 +16,7 @@ import { toCodecOpenAI } from "effect/ai/OpenAiStructuredOutput"
|
|
|
16
16
|
import type * as Prompt from "effect/ai/Prompt"
|
|
17
17
|
import type * as Response from "effect/ai/Response"
|
|
18
18
|
import * as Tool from "effect/ai/Tool"
|
|
19
|
+
import * as Arr from "effect/Array"
|
|
19
20
|
import * as Context from "effect/Context"
|
|
20
21
|
import * as DateTime from "effect/DateTime"
|
|
21
22
|
import * as Effect from "effect/Effect"
|
|
@@ -134,7 +135,7 @@ declare module "effect/ai/Prompt" {
|
|
|
134
135
|
/**
|
|
135
136
|
* Provider-specific file options for OpenAI-compatible APIs.
|
|
136
137
|
*/
|
|
137
|
-
readonly openai?: {
|
|
138
|
+
readonly "openai-compat"?: {
|
|
138
139
|
/**
|
|
139
140
|
* The detail level of the image to be sent to the model. One of `high`, `low`, or `auto`. Defaults to `auto`.
|
|
140
141
|
*/
|
|
@@ -153,7 +154,7 @@ declare module "effect/ai/Prompt" {
|
|
|
153
154
|
/**
|
|
154
155
|
* Provider-specific reasoning options for OpenAI-compatible APIs.
|
|
155
156
|
*/
|
|
156
|
-
readonly openai?: {
|
|
157
|
+
readonly "openai-compat"?: {
|
|
157
158
|
/**
|
|
158
159
|
* The ID of the item to reference.
|
|
159
160
|
*/
|
|
@@ -184,7 +185,7 @@ declare module "effect/ai/Prompt" {
|
|
|
184
185
|
/**
|
|
185
186
|
* Provider-specific tool-call options for OpenAI-compatible APIs.
|
|
186
187
|
*/
|
|
187
|
-
readonly openai?: {
|
|
188
|
+
readonly "openai-compat"?: {
|
|
188
189
|
/**
|
|
189
190
|
* The ID of the item to reference.
|
|
190
191
|
*/
|
|
@@ -211,7 +212,7 @@ declare module "effect/ai/Prompt" {
|
|
|
211
212
|
/**
|
|
212
213
|
* Provider-specific tool-result options for OpenAI-compatible APIs.
|
|
213
214
|
*/
|
|
214
|
-
readonly openai?: {
|
|
215
|
+
readonly "openai-compat"?: {
|
|
215
216
|
/**
|
|
216
217
|
* The ID of the item to reference.
|
|
217
218
|
*/
|
|
@@ -234,7 +235,7 @@ declare module "effect/ai/Prompt" {
|
|
|
234
235
|
/**
|
|
235
236
|
* Provider-specific text options for OpenAI-compatible APIs.
|
|
236
237
|
*/
|
|
237
|
-
readonly openai?: {
|
|
238
|
+
readonly "openai-compat"?: {
|
|
238
239
|
/**
|
|
239
240
|
* The ID of the item to reference.
|
|
240
241
|
*/
|
|
@@ -263,7 +264,7 @@ declare module "effect/ai/Response" {
|
|
|
263
264
|
/**
|
|
264
265
|
* Provider-specific metadata returned for the text part.
|
|
265
266
|
*/
|
|
266
|
-
readonly openai?: {
|
|
267
|
+
readonly "openai-compat"?: {
|
|
267
268
|
/**
|
|
268
269
|
* The OpenAI item ID associated with the text part.
|
|
269
270
|
*/
|
|
@@ -296,7 +297,7 @@ declare module "effect/ai/Response" {
|
|
|
296
297
|
/**
|
|
297
298
|
* Provider-specific metadata returned for the streamed text start.
|
|
298
299
|
*/
|
|
299
|
-
readonly openai?: {
|
|
300
|
+
readonly "openai-compat"?: {
|
|
300
301
|
/**
|
|
301
302
|
* The OpenAI item ID associated with the streamed text part.
|
|
302
303
|
*/
|
|
@@ -315,7 +316,7 @@ declare module "effect/ai/Response" {
|
|
|
315
316
|
/**
|
|
316
317
|
* Provider-specific metadata returned for the streamed text end.
|
|
317
318
|
*/
|
|
318
|
-
readonly openai?: {
|
|
319
|
+
readonly "openai-compat"?: {
|
|
319
320
|
/**
|
|
320
321
|
* The OpenAI item ID associated with the streamed text part.
|
|
321
322
|
*/
|
|
@@ -338,7 +339,7 @@ declare module "effect/ai/Response" {
|
|
|
338
339
|
/**
|
|
339
340
|
* Provider-specific metadata returned for the reasoning part.
|
|
340
341
|
*/
|
|
341
|
-
readonly openai?: {
|
|
342
|
+
readonly "openai-compat"?: {
|
|
342
343
|
/**
|
|
343
344
|
* The OpenAI item ID associated with the reasoning part.
|
|
344
345
|
*/
|
|
@@ -365,7 +366,7 @@ declare module "effect/ai/Response" {
|
|
|
365
366
|
/**
|
|
366
367
|
* Provider-specific metadata returned for the streamed reasoning start.
|
|
367
368
|
*/
|
|
368
|
-
readonly openai?: {
|
|
369
|
+
readonly "openai-compat"?: {
|
|
369
370
|
/**
|
|
370
371
|
* The OpenAI item ID associated with the reasoning part.
|
|
371
372
|
*/
|
|
@@ -392,7 +393,7 @@ declare module "effect/ai/Response" {
|
|
|
392
393
|
/**
|
|
393
394
|
* Provider-specific metadata returned for the streamed reasoning delta.
|
|
394
395
|
*/
|
|
395
|
-
readonly openai?: {
|
|
396
|
+
readonly "openai-compat"?: {
|
|
396
397
|
/**
|
|
397
398
|
* The OpenAI item ID associated with the reasoning part.
|
|
398
399
|
*/
|
|
@@ -411,7 +412,7 @@ declare module "effect/ai/Response" {
|
|
|
411
412
|
/**
|
|
412
413
|
* Provider-specific metadata returned for the streamed reasoning end.
|
|
413
414
|
*/
|
|
414
|
-
readonly openai?: {
|
|
415
|
+
readonly "openai-compat"?: {
|
|
415
416
|
/**
|
|
416
417
|
* The OpenAI item ID associated with the reasoning part.
|
|
417
418
|
*/
|
|
@@ -434,7 +435,7 @@ declare module "effect/ai/Response" {
|
|
|
434
435
|
/**
|
|
435
436
|
* Provider-specific metadata returned for the tool call.
|
|
436
437
|
*/
|
|
437
|
-
readonly openai?: {
|
|
438
|
+
readonly "openai-compat"?: {
|
|
438
439
|
/**
|
|
439
440
|
* The OpenAI item ID associated with the tool call.
|
|
440
441
|
*/
|
|
@@ -457,7 +458,7 @@ declare module "effect/ai/Response" {
|
|
|
457
458
|
/**
|
|
458
459
|
* Provider-specific citation metadata for OpenAI-compatible APIs.
|
|
459
460
|
*/
|
|
460
|
-
readonly openai?:
|
|
461
|
+
readonly "openai-compat"?:
|
|
461
462
|
| {
|
|
462
463
|
/**
|
|
463
464
|
* Identifies a citation to an uploaded file.
|
|
@@ -514,7 +515,7 @@ declare module "effect/ai/Response" {
|
|
|
514
515
|
/**
|
|
515
516
|
* Provider-specific URL citation metadata for OpenAI-compatible APIs.
|
|
516
517
|
*/
|
|
517
|
-
readonly openai?: {
|
|
518
|
+
readonly "openai-compat"?: {
|
|
518
519
|
/**
|
|
519
520
|
* Identifies a citation to a URL.
|
|
520
521
|
*/
|
|
@@ -541,7 +542,7 @@ declare module "effect/ai/Response" {
|
|
|
541
542
|
/**
|
|
542
543
|
* Provider-specific metadata returned when generation finishes.
|
|
543
544
|
*/
|
|
544
|
-
readonly openai?: {
|
|
545
|
+
readonly "openai-compat"?: {
|
|
545
546
|
/**
|
|
546
547
|
* The service tier reported by the OpenAI-compatible provider.
|
|
547
548
|
*/
|
|
@@ -784,45 +785,9 @@ export const withConfigOverride: {
|
|
|
784
785
|
*/
|
|
785
786
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
|
|
786
787
|
} = dual<
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
* **When to use**
|
|
791
|
-
*
|
|
792
|
-
* Use to override request configuration for a single language model effect
|
|
793
|
-
* without changing the defaults supplied to `model`, `make`, or `layer`.
|
|
794
|
-
*
|
|
795
|
-
* **Details**
|
|
796
|
-
*
|
|
797
|
-
* Existing `Config` values from the Effect context are merged with `overrides`,
|
|
798
|
-
* and the override values take precedence.
|
|
799
|
-
*
|
|
800
|
-
* @see {@link Config} for the configuration shape
|
|
801
|
-
*
|
|
802
|
-
* @stability unstable
|
|
803
|
-
* @category configuration
|
|
804
|
-
* @since 4.0.0
|
|
805
|
-
*/
|
|
806
|
-
(overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
807
|
-
/**
|
|
808
|
-
* Provides scoped config overrides for OpenAI-compatible language model operations.
|
|
809
|
-
*
|
|
810
|
-
* **When to use**
|
|
811
|
-
*
|
|
812
|
-
* Use to override request configuration for a single language model effect
|
|
813
|
-
* without changing the defaults supplied to `model`, `make`, or `layer`.
|
|
814
|
-
*
|
|
815
|
-
* **Details**
|
|
816
|
-
*
|
|
817
|
-
* Existing `Config` values from the Effect context are merged with `overrides`,
|
|
818
|
-
* and the override values take precedence.
|
|
819
|
-
*
|
|
820
|
-
* @see {@link Config} for the configuration shape
|
|
821
|
-
*
|
|
822
|
-
* @stability unstable
|
|
823
|
-
* @category configuration
|
|
824
|
-
* @since 4.0.0
|
|
825
|
-
*/
|
|
788
|
+
(
|
|
789
|
+
overrides: typeof Config.Service
|
|
790
|
+
) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
826
791
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
827
792
|
>(2, (self, overrides) =>
|
|
828
793
|
Effect.flatMap(
|
|
@@ -945,7 +910,7 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
945
910
|
|
|
946
911
|
case "reasoning": {
|
|
947
912
|
reasoning += part.text
|
|
948
|
-
reasoningField ??= part.options
|
|
913
|
+
reasoningField ??= part.options["openai-compat"]?.reasoningField ?? undefined
|
|
949
914
|
break
|
|
950
915
|
}
|
|
951
916
|
|
|
@@ -1081,7 +1046,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1081
1046
|
const reasoningField = getResponseReasoningField(message)
|
|
1082
1047
|
const reasoning = reasoningField !== undefined ? message[reasoningField] : undefined
|
|
1083
1048
|
if (reasoningField !== undefined && Predicate.isNotNullish(reasoning) && reasoning.length > 0) {
|
|
1084
|
-
parts.push({ type: "reasoning", text: reasoning, metadata: { openai: { reasoningField } } })
|
|
1049
|
+
parts.push({ type: "reasoning", text: reasoning, metadata: { "openai-compat": { reasoningField } } })
|
|
1085
1050
|
}
|
|
1086
1051
|
|
|
1087
1052
|
if (
|
|
@@ -1115,7 +1080,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1115
1080
|
name: toolName,
|
|
1116
1081
|
params,
|
|
1117
1082
|
metadata: {
|
|
1118
|
-
openai: {
|
|
1083
|
+
"openai-compat": {
|
|
1119
1084
|
...makeItemIdMetadata(toolCall.id),
|
|
1120
1085
|
...makeExtraContentMetadata(toolCall.extra_content ?? undefined)
|
|
1121
1086
|
}
|
|
@@ -1136,7 +1101,7 @@ const makeResponse = Effect.fnUntraced(
|
|
|
1136
1101
|
reason: finishReason,
|
|
1137
1102
|
usage: getUsage(rawResponse.usage),
|
|
1138
1103
|
response: buildHttpResponseDetails(response),
|
|
1139
|
-
...(serviceTier !== undefined && { metadata: { openai: { serviceTier } } })
|
|
1104
|
+
...(serviceTier !== undefined && { metadata: { "openai-compat": { serviceTier } } })
|
|
1140
1105
|
})
|
|
1141
1106
|
|
|
1142
1107
|
return parts
|
|
@@ -1180,7 +1145,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1180
1145
|
parts.push({
|
|
1181
1146
|
type: "reasoning-end",
|
|
1182
1147
|
id: reasoningId,
|
|
1183
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId) } }
|
|
1148
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId) } }
|
|
1184
1149
|
})
|
|
1185
1150
|
}
|
|
1186
1151
|
|
|
@@ -1188,7 +1153,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1188
1153
|
parts.push({
|
|
1189
1154
|
type: "text-end",
|
|
1190
1155
|
id: textId,
|
|
1191
|
-
metadata: { openai: { ...makeItemIdMetadata(textId) } }
|
|
1156
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(textId) } }
|
|
1192
1157
|
})
|
|
1193
1158
|
}
|
|
1194
1159
|
|
|
@@ -1214,7 +1179,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1214
1179
|
name: toolCall.name,
|
|
1215
1180
|
params,
|
|
1216
1181
|
metadata: {
|
|
1217
|
-
openai: {
|
|
1182
|
+
"openai-compat": {
|
|
1218
1183
|
...makeItemIdMetadata(toolCall.id),
|
|
1219
1184
|
...makeExtraContentMetadata(toolCall.extraContent)
|
|
1220
1185
|
}
|
|
@@ -1230,7 +1195,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1230
1195
|
usage: getUsage(usage),
|
|
1231
1196
|
response: buildHttpResponseDetails(response),
|
|
1232
1197
|
...(normalizedServiceTier !== undefined
|
|
1233
|
-
? { metadata: { openai: { serviceTier: normalizedServiceTier } } }
|
|
1198
|
+
? { metadata: { "openai-compat": { serviceTier: normalizedServiceTier } } }
|
|
1234
1199
|
: undefined)
|
|
1235
1200
|
})
|
|
1236
1201
|
return parts
|
|
@@ -1277,7 +1242,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1277
1242
|
parts.push({
|
|
1278
1243
|
type: "reasoning-start",
|
|
1279
1244
|
id: reasoningId,
|
|
1280
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId), reasoningField } }
|
|
1245
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId), reasoningField } }
|
|
1281
1246
|
})
|
|
1282
1247
|
}
|
|
1283
1248
|
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningDelta })
|
|
@@ -1289,7 +1254,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1289
1254
|
parts.push({
|
|
1290
1255
|
type: "reasoning-end",
|
|
1291
1256
|
id: reasoningId,
|
|
1292
|
-
metadata: { openai: { ...makeItemIdMetadata(reasoningId) } }
|
|
1257
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(reasoningId) } }
|
|
1293
1258
|
})
|
|
1294
1259
|
}
|
|
1295
1260
|
|
|
@@ -1298,7 +1263,7 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1298
1263
|
parts.push({
|
|
1299
1264
|
type: "text-start",
|
|
1300
1265
|
id: textId,
|
|
1301
|
-
metadata: { openai: { ...makeItemIdMetadata(textId) } }
|
|
1266
|
+
metadata: { "openai-compat": { ...makeItemIdMetadata(textId) } }
|
|
1302
1267
|
})
|
|
1303
1268
|
}
|
|
1304
1269
|
parts.push({ type: "text-delta", id: textId, delta: choice.delta.content })
|
|
@@ -1344,7 +1309,8 @@ const makeStreamResponse = Effect.fnUntraced(
|
|
|
1344
1309
|
|
|
1345
1310
|
return parts
|
|
1346
1311
|
})),
|
|
1347
|
-
Stream.
|
|
1312
|
+
Stream.filter(Arr.isArrayNonEmpty),
|
|
1313
|
+
Stream.flattenArray
|
|
1348
1314
|
)
|
|
1349
1315
|
}
|
|
1350
1316
|
)
|
|
@@ -1404,7 +1370,7 @@ const annotateStreamResponse = (span: Span, part: Response.StreamPartEncoded) =>
|
|
|
1404
1370
|
})
|
|
1405
1371
|
}
|
|
1406
1372
|
if (part.type === "finish") {
|
|
1407
|
-
const serviceTier = (part.metadata as any)?.openai?.serviceTier as string | undefined
|
|
1373
|
+
const serviceTier = (part.metadata as any)?.["openai-compat"]?.serviceTier as string | undefined
|
|
1408
1374
|
addGenAIAnnotations(span, {
|
|
1409
1375
|
response: {
|
|
1410
1376
|
finishReasons: [part.reason]
|
|
@@ -1745,6 +1711,10 @@ const toChatMessageContent = (
|
|
|
1745
1711
|
return content
|
|
1746
1712
|
}
|
|
1747
1713
|
|
|
1714
|
+
if (content.length === 1 && content[0].type === "input_text") {
|
|
1715
|
+
return content[0].text
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1748
1718
|
const parts: Array<ChatCompletionContentPart> = []
|
|
1749
1719
|
|
|
1750
1720
|
for (const part of content) {
|
|
@@ -1804,7 +1774,7 @@ const isFileId = (data: string, config: typeof Config.Service): boolean =>
|
|
|
1804
1774
|
|
|
1805
1775
|
const getExtraContent = (
|
|
1806
1776
|
part: Prompt.ToolCallPart
|
|
1807
|
-
): Schema.JsonObject | null => part.options
|
|
1777
|
+
): Schema.JsonObject | null => part.options["openai-compat"]?.extraContent ?? null
|
|
1808
1778
|
|
|
1809
1779
|
/**
|
|
1810
1780
|
* The chat message field carrying reasoning text. Providers differ: DeepSeek,
|
|
@@ -1823,7 +1793,7 @@ const getResponseReasoningField = (message: {
|
|
|
1823
1793
|
? "reasoning_content"
|
|
1824
1794
|
: undefined
|
|
1825
1795
|
|
|
1826
|
-
const getImageDetail = (part: Prompt.FilePart): ImageDetail => part.options
|
|
1796
|
+
const getImageDetail = (part: Prompt.FilePart): ImageDetail => part.options["openai-compat"]?.imageDetail ?? "auto"
|
|
1827
1797
|
|
|
1828
1798
|
const makeItemIdMetadata = (itemId: string | undefined) => itemId !== undefined ? { itemId } : undefined
|
|
1829
1799
|
|
package/src/internal/errors.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import * as AiError from "effect/ai/AiError"
|
|
2
2
|
import type * as Response from "effect/ai/Response"
|
|
3
3
|
import * as Arr from "effect/Array"
|
|
4
|
-
import * as
|
|
4
|
+
import * as Clock from "effect/Clock"
|
|
5
|
+
import type * as Duration from "effect/Duration"
|
|
5
6
|
import * as Effect from "effect/Effect"
|
|
6
7
|
import type * as Sse from "effect/encoding/Sse"
|
|
7
8
|
import { dual } from "effect/Function"
|
|
9
|
+
import * as Headers from "effect/http/Headers"
|
|
8
10
|
import type * as HttpClientError from "effect/http/HttpClientError"
|
|
9
11
|
import type * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
10
12
|
import type * as HttpClientResponse from "effect/http/HttpClientResponse"
|
|
11
13
|
import * as Number from "effect/Number"
|
|
12
14
|
import * as Option from "effect/Option"
|
|
13
15
|
import * as Redactable from "effect/Redactable"
|
|
16
|
+
import * as Result from "effect/Result"
|
|
14
17
|
import * as Schema from "effect/Schema"
|
|
15
18
|
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
16
19
|
import * as String from "effect/String"
|
|
@@ -47,7 +50,7 @@ export const mapSchemaError = dual<
|
|
|
47
50
|
AiError.make({
|
|
48
51
|
module: "OpenAiClient",
|
|
49
52
|
method,
|
|
50
|
-
reason: AiError.
|
|
53
|
+
reason: AiError.InvalidResponseError.fromSchemaError(error)
|
|
51
54
|
}))
|
|
52
55
|
|
|
53
56
|
/** @internal */
|
|
@@ -58,7 +61,7 @@ export const mapSseError = dual<
|
|
|
58
61
|
AiError.make({
|
|
59
62
|
module: "OpenAiClient",
|
|
60
63
|
method,
|
|
61
|
-
reason: new AiError.
|
|
64
|
+
reason: new AiError.InvalidResponseError({ description: error.message })
|
|
62
65
|
}))
|
|
63
66
|
|
|
64
67
|
/** @internal */
|
|
@@ -140,7 +143,9 @@ const mapStatusCodeError = Effect.fnUntraced(function*(
|
|
|
140
143
|
)
|
|
141
144
|
const decoded = OpenAiErrorBodyJson(body)
|
|
142
145
|
|
|
146
|
+
const now = yield* Clock.currentTimeMillis
|
|
143
147
|
const reason = mapStatusCodeToReason({
|
|
148
|
+
now,
|
|
144
149
|
status,
|
|
145
150
|
headers,
|
|
146
151
|
message: Option.isSome(decoded) ? decoded.value.error.message : undefined,
|
|
@@ -159,16 +164,16 @@ const mapStatusCodeError = Effect.fnUntraced(function*(
|
|
|
159
164
|
return yield* AiError.make({ module: "OpenAiClient", method, reason })
|
|
160
165
|
})
|
|
161
166
|
|
|
167
|
+
const retryAfterFromHeader = (raw: string, now: number): Duration.Duration | undefined => {
|
|
168
|
+
const retryAfter = Headers.parseRetryAfter(raw)
|
|
169
|
+
return Result.isSuccess(retryAfter) ? Headers.retryAfterDelay(retryAfter.success, now) : undefined
|
|
170
|
+
}
|
|
171
|
+
|
|
162
172
|
/** @internal */
|
|
163
|
-
export const parseRateLimitHeaders = (headers: Record<string, string
|
|
173
|
+
export const parseRateLimitHeaders = (headers: Record<string, string>, now: number) => {
|
|
164
174
|
const retryAfterRaw = headers["retry-after"]
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const parsed = Number.parse(retryAfterRaw)
|
|
168
|
-
if (Option.isSome(parsed)) {
|
|
169
|
-
retryAfter = Duration.seconds(parsed.value)
|
|
170
|
-
}
|
|
171
|
-
}
|
|
175
|
+
// an invalid Retry-After is treated as absent
|
|
176
|
+
const retryAfter = retryAfterRaw === undefined ? undefined : retryAfterFromHeader(retryAfterRaw, now)
|
|
172
177
|
const remainingRaw = headers["x-ratelimit-remaining-requests"]
|
|
173
178
|
const remaining = remainingRaw !== undefined ? Option.getOrNull(Number.parse(remainingRaw)) : null
|
|
174
179
|
return {
|
|
@@ -208,8 +213,10 @@ export const buildHttpContext = (params: {
|
|
|
208
213
|
})
|
|
209
214
|
|
|
210
215
|
/** @internal */
|
|
211
|
-
export const mapStatusCodeToReason = ({ status, headers, message, metadata, http }: {
|
|
216
|
+
export const mapStatusCodeToReason = ({ status, headers, message, metadata, http, now }: {
|
|
212
217
|
readonly status: number
|
|
218
|
+
/** Current time in epoch milliseconds, used to measure a Retry-After date. */
|
|
219
|
+
readonly now: number
|
|
213
220
|
readonly headers: Record<string, string>
|
|
214
221
|
readonly message: string | undefined
|
|
215
222
|
readonly metadata: OpenAiErrorMetadata
|
|
@@ -230,34 +237,34 @@ export const mapStatusCodeToReason = ({ status, headers, message, metadata, http
|
|
|
230
237
|
case 400:
|
|
231
238
|
return new AiError.InvalidRequestError({
|
|
232
239
|
description: errorDescription,
|
|
233
|
-
metadata: { openai: metadata },
|
|
240
|
+
metadata: { "openai-compat": metadata },
|
|
234
241
|
http
|
|
235
242
|
})
|
|
236
243
|
case 401:
|
|
237
244
|
return new AiError.AuthenticationError({
|
|
238
245
|
kind: "InvalidKey",
|
|
239
246
|
description: errorDescription,
|
|
240
|
-
metadata,
|
|
247
|
+
metadata: { "openai-compat": metadata },
|
|
241
248
|
http
|
|
242
249
|
})
|
|
243
250
|
case 403:
|
|
244
251
|
return new AiError.AuthenticationError({
|
|
245
252
|
kind: "InsufficientPermissions",
|
|
246
253
|
description: errorDescription,
|
|
247
|
-
metadata,
|
|
254
|
+
metadata: { "openai-compat": metadata },
|
|
248
255
|
http
|
|
249
256
|
})
|
|
250
257
|
case 404:
|
|
251
258
|
return new AiError.InvalidRequestError({
|
|
252
259
|
description: errorDescription,
|
|
253
|
-
metadata: { openai: metadata },
|
|
260
|
+
metadata: { "openai-compat": metadata },
|
|
254
261
|
http
|
|
255
262
|
})
|
|
256
263
|
case 409:
|
|
257
264
|
case 422:
|
|
258
265
|
return new AiError.InvalidRequestError({
|
|
259
266
|
description: errorDescription,
|
|
260
|
-
metadata: { openai: metadata },
|
|
267
|
+
metadata: { "openai-compat": metadata },
|
|
261
268
|
http
|
|
262
269
|
})
|
|
263
270
|
case 429: {
|
|
@@ -268,15 +275,15 @@ export const mapStatusCodeToReason = ({ status, headers, message, metadata, http
|
|
|
268
275
|
metadata.errorType?.includes("exhausted")
|
|
269
276
|
) {
|
|
270
277
|
return new AiError.QuotaExhaustedError({
|
|
271
|
-
metadata: { openai: metadata },
|
|
278
|
+
metadata: { "openai-compat": metadata },
|
|
272
279
|
http
|
|
273
280
|
})
|
|
274
281
|
}
|
|
275
|
-
const { retryAfter, ...rateLimitMetadata } = parseRateLimitHeaders(headers)
|
|
282
|
+
const { retryAfter, ...rateLimitMetadata } = parseRateLimitHeaders(headers, now)
|
|
276
283
|
return new AiError.RateLimitError({
|
|
277
284
|
retryAfter,
|
|
278
285
|
metadata: {
|
|
279
|
-
openai: {
|
|
286
|
+
"openai-compat": {
|
|
280
287
|
...metadata,
|
|
281
288
|
...rateLimitMetadata
|
|
282
289
|
}
|
|
@@ -288,13 +295,13 @@ export const mapStatusCodeToReason = ({ status, headers, message, metadata, http
|
|
|
288
295
|
if (status >= 500) {
|
|
289
296
|
return new AiError.InternalProviderError({
|
|
290
297
|
description: message ?? "Server error",
|
|
291
|
-
metadata,
|
|
298
|
+
metadata: { "openai-compat": metadata },
|
|
292
299
|
http
|
|
293
300
|
})
|
|
294
301
|
}
|
|
295
302
|
return new AiError.UnknownError({
|
|
296
303
|
description: message,
|
|
297
|
-
metadata,
|
|
304
|
+
metadata: { "openai-compat": metadata },
|
|
298
305
|
http
|
|
299
306
|
})
|
|
300
307
|
}
|