@core-ai/openai 0.22.0 → 0.24.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.
- package/dist/{chunk-MKAXZHMO.js → chunk-SCXEGDFV.js} +63 -14
- package/dist/compat.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
|
@@ -271,7 +271,10 @@ var openaiCompatProviderOptionsSchema = openaiChatGenerateProviderOptionsSchema;
|
|
|
271
271
|
var openaiCompatGenerateProviderOptionsSchema = openaiChatGenerateProviderOptionsSchema;
|
|
272
272
|
|
|
273
273
|
// src/chat-completions/chat-model.ts
|
|
274
|
-
import {
|
|
274
|
+
import {
|
|
275
|
+
createObjectStream,
|
|
276
|
+
createChatStream
|
|
277
|
+
} from "@core-ai/core-ai";
|
|
275
278
|
|
|
276
279
|
// src/chat-completions/chat-adapter.ts
|
|
277
280
|
import {
|
|
@@ -875,6 +878,7 @@ import {
|
|
|
875
878
|
ContextLengthExceededError,
|
|
876
879
|
ModelOverloadedError,
|
|
877
880
|
ProviderError,
|
|
881
|
+
ProviderQuotaExceededError,
|
|
878
882
|
RateLimitError,
|
|
879
883
|
ServiceUnavailableError,
|
|
880
884
|
asRecord,
|
|
@@ -895,6 +899,7 @@ var TOKEN_LIMIT_PATTERNS = [
|
|
|
895
899
|
/maximum context length is (\d+) tokens.*you requested (\d+) tokens/i,
|
|
896
900
|
/configured limit of (\d+) tokens.*resulted in (\d+) tokens/i
|
|
897
901
|
];
|
|
902
|
+
var CONTEXT_LENGTH_MESSAGE_PATTERN = /\binput exceeds the context window\b/i;
|
|
898
903
|
var OVERLOAD_MESSAGE_ELIGIBLE_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
|
|
899
904
|
function wrapOpenAIError(error, provider = "openai") {
|
|
900
905
|
if (isOpenAIAbortError(error)) {
|
|
@@ -902,7 +907,11 @@ function wrapOpenAIError(error, provider = "openai") {
|
|
|
902
907
|
}
|
|
903
908
|
const message = getErrorMessage(error);
|
|
904
909
|
const statusCode = error instanceof APIError ? error.status : getHttpStatusCode(error, ["status"]);
|
|
905
|
-
const options = {
|
|
910
|
+
const options = {
|
|
911
|
+
statusCode,
|
|
912
|
+
code: getProviderErrorCode(error) ?? getProviderErrorType(error),
|
|
913
|
+
cause: error
|
|
914
|
+
};
|
|
906
915
|
const contextLength = getContextLengthDetails(error, message);
|
|
907
916
|
if (contextLength) {
|
|
908
917
|
return new ContextLengthExceededError(message, provider, {
|
|
@@ -913,8 +922,8 @@ function wrapOpenAIError(error, provider = "openai") {
|
|
|
913
922
|
if (isAzureOpenAIBackendCapacityError(error)) {
|
|
914
923
|
return new ServiceUnavailableError(message, provider, options);
|
|
915
924
|
}
|
|
916
|
-
if (
|
|
917
|
-
return new
|
|
925
|
+
if (isOpenAIQuotaExceeded(error, statusCode)) {
|
|
926
|
+
return new ProviderQuotaExceededError(message, provider, options);
|
|
918
927
|
}
|
|
919
928
|
if (isAzureOpenAINoCapacity(error)) {
|
|
920
929
|
return new ModelOverloadedError(message, provider, options);
|
|
@@ -928,11 +937,16 @@ function wrapOpenAIError(error, provider = "openai") {
|
|
|
928
937
|
retryAfterSeconds: getRetryAfterSecondsFromError(error)
|
|
929
938
|
});
|
|
930
939
|
}
|
|
931
|
-
if (isTransientUnavailableStatus(statusCode)) {
|
|
940
|
+
if (isTransientUnavailableStatus(statusCode) || isOpenAIServerError(error)) {
|
|
932
941
|
return new ServiceUnavailableError(message, provider, options);
|
|
933
942
|
}
|
|
934
943
|
return new ProviderError(message, provider, options);
|
|
935
944
|
}
|
|
945
|
+
function isOpenAIServerError(error) {
|
|
946
|
+
const code = getProviderErrorCode(error);
|
|
947
|
+
const type = getProviderErrorType(error);
|
|
948
|
+
return code === "server_error" || code === "service_unavailable" || type === "server_error";
|
|
949
|
+
}
|
|
936
950
|
function isOpenAIAbortError(error) {
|
|
937
951
|
return error instanceof APIUserAbortError || isAbortErrorByName(error);
|
|
938
952
|
}
|
|
@@ -963,7 +977,7 @@ function getContextLengthDetails(error, fallbackMessage) {
|
|
|
963
977
|
if (tokenCounts) {
|
|
964
978
|
return tokenCounts;
|
|
965
979
|
}
|
|
966
|
-
if (!isKnownContextLengthCode) {
|
|
980
|
+
if (!isKnownContextLengthCode && !CONTEXT_LENGTH_MESSAGE_PATTERN.test(providerMessage)) {
|
|
967
981
|
return void 0;
|
|
968
982
|
}
|
|
969
983
|
return {};
|
|
@@ -991,13 +1005,14 @@ function isAzureOpenAIBackendCapacityError(error) {
|
|
|
991
1005
|
const providerMessage = getProviderMessage(error);
|
|
992
1006
|
return type === "invalid_request_error" && providerMessage?.toLowerCase() === "backend error.";
|
|
993
1007
|
}
|
|
994
|
-
function
|
|
1008
|
+
function isOpenAIQuotaExceeded(error, statusCode) {
|
|
995
1009
|
const code = getProviderErrorCode(error);
|
|
996
1010
|
const type = getProviderErrorType(error);
|
|
997
|
-
return code === "insufficient_quota" || type === "insufficient_quota";
|
|
1011
|
+
return statusCode === 402 || code === "insufficient_quota" || type === "insufficient_quota" || code === "credit_balance_exhausted";
|
|
998
1012
|
}
|
|
999
1013
|
function isAzureOpenAINoCapacity(error) {
|
|
1000
|
-
|
|
1014
|
+
const code = getProviderErrorCode(error);
|
|
1015
|
+
return code === "NoCapacity" || code === "no_capacity";
|
|
1001
1016
|
}
|
|
1002
1017
|
function getProviderMessage(error) {
|
|
1003
1018
|
if (error instanceof APIError) {
|
|
@@ -1323,7 +1338,10 @@ function createOpenAIChatCompletionsModel(client, modelId, modelOptions) {
|
|
|
1323
1338
|
await callOpenAIChatCompletionsApi(request, options.signal),
|
|
1324
1339
|
adapterOptions
|
|
1325
1340
|
),
|
|
1326
|
-
{
|
|
1341
|
+
{
|
|
1342
|
+
signal: options.signal,
|
|
1343
|
+
mapError: (error) => wrapOpenAIError(error, provider)
|
|
1344
|
+
}
|
|
1327
1345
|
);
|
|
1328
1346
|
}
|
|
1329
1347
|
return {
|
|
@@ -1383,6 +1401,7 @@ import {
|
|
|
1383
1401
|
import { createObjectStream as createObjectStream2, createChatStream as createChatStream2 } from "@core-ai/core-ai";
|
|
1384
1402
|
|
|
1385
1403
|
// src/chat-adapter.ts
|
|
1404
|
+
import { APIError as APIError2 } from "openai";
|
|
1386
1405
|
import {
|
|
1387
1406
|
getProviderMetadata as getProviderMetadata2,
|
|
1388
1407
|
clampReasoningEffort as clampReasoningEffort2,
|
|
@@ -1644,6 +1663,13 @@ function mapOpenAIProviderOptionsToRequestFields2(options) {
|
|
|
1644
1663
|
};
|
|
1645
1664
|
}
|
|
1646
1665
|
function mapGenerateResponse2(response, adapterOptions = {}) {
|
|
1666
|
+
if (response.status === "failed") {
|
|
1667
|
+
throw createInBandStreamError(
|
|
1668
|
+
response.error ?? {
|
|
1669
|
+
message: "Response failed without error details"
|
|
1670
|
+
}
|
|
1671
|
+
);
|
|
1672
|
+
}
|
|
1647
1673
|
const providerId = adapterOptions.providerId ?? DEFAULT_PROVIDER_ID;
|
|
1648
1674
|
const parts = [];
|
|
1649
1675
|
for (const item of response.output) {
|
|
@@ -1845,6 +1871,16 @@ async function* transformStream2(stream, adapterOptions = {}) {
|
|
|
1845
1871
|
};
|
|
1846
1872
|
};
|
|
1847
1873
|
for await (const event of stream) {
|
|
1874
|
+
if (event.type === "error") {
|
|
1875
|
+
throw createInBandStreamError(event);
|
|
1876
|
+
}
|
|
1877
|
+
if (event.type === "response.failed") {
|
|
1878
|
+
throw createInBandStreamError(
|
|
1879
|
+
event.response.error ?? {
|
|
1880
|
+
message: "Response failed without error details"
|
|
1881
|
+
}
|
|
1882
|
+
);
|
|
1883
|
+
}
|
|
1848
1884
|
if (event.type === "response.reasoning_summary_text.delta") {
|
|
1849
1885
|
const summaryPart = {
|
|
1850
1886
|
itemId: event.item_id,
|
|
@@ -1984,7 +2020,10 @@ async function* transformStream2(stream, adapterOptions = {}) {
|
|
|
1984
2020
|
}
|
|
1985
2021
|
}
|
|
1986
2022
|
const reasoningEndEvent2 = getNextReasoningEndEvent(
|
|
1987
|
-
createReasoningProviderMetadata(
|
|
2023
|
+
createReasoningProviderMetadata(
|
|
2024
|
+
providerId,
|
|
2025
|
+
encryptedContent
|
|
2026
|
+
)
|
|
1988
2027
|
);
|
|
1989
2028
|
if (reasoningEndEvent2) {
|
|
1990
2029
|
yield reasoningEndEvent2;
|
|
@@ -2021,7 +2060,7 @@ async function* transformStream2(stream, adapterOptions = {}) {
|
|
|
2021
2060
|
}
|
|
2022
2061
|
continue;
|
|
2023
2062
|
}
|
|
2024
|
-
if (event.type === "response.completed") {
|
|
2063
|
+
if (event.type === "response.completed" || event.type === "response.incomplete") {
|
|
2025
2064
|
latestResponse = event.response;
|
|
2026
2065
|
yield* closeText();
|
|
2027
2066
|
const reasoningEndEvent2 = getNextReasoningEndEvent(
|
|
@@ -2069,6 +2108,9 @@ async function* transformStream2(stream, adapterOptions = {}) {
|
|
|
2069
2108
|
usage
|
|
2070
2109
|
};
|
|
2071
2110
|
}
|
|
2111
|
+
function createInBandStreamError(body) {
|
|
2112
|
+
return new APIError2(void 0, body, body.message, void 0);
|
|
2113
|
+
}
|
|
2072
2114
|
function mapReasoningToRequestFields2(options, capabilities) {
|
|
2073
2115
|
if (!options.reasoning) {
|
|
2074
2116
|
return {};
|
|
@@ -2118,7 +2160,11 @@ function createOpenAIChatModel(client, modelId, capabilities, providerId = DEFAU
|
|
|
2118
2160
|
request,
|
|
2119
2161
|
options.signal
|
|
2120
2162
|
);
|
|
2121
|
-
|
|
2163
|
+
try {
|
|
2164
|
+
return mapGenerateResponse2(response, { providerId: provider });
|
|
2165
|
+
} catch (error) {
|
|
2166
|
+
throw wrapOpenAIError(error, provider);
|
|
2167
|
+
}
|
|
2122
2168
|
}
|
|
2123
2169
|
async function streamChat(options) {
|
|
2124
2170
|
const request = createStreamRequest2(modelId, options, adapterOptions);
|
|
@@ -2127,7 +2173,10 @@ function createOpenAIChatModel(client, modelId, capabilities, providerId = DEFAU
|
|
|
2127
2173
|
await callOpenAIResponsesApi(request, options.signal),
|
|
2128
2174
|
{ providerId: provider }
|
|
2129
2175
|
),
|
|
2130
|
-
{
|
|
2176
|
+
{
|
|
2177
|
+
signal: options.signal,
|
|
2178
|
+
mapError: (error) => wrapOpenAIError(error, provider)
|
|
2179
|
+
}
|
|
2131
2180
|
);
|
|
2132
2181
|
}
|
|
2133
2182
|
return {
|
package/dist/compat.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
openaiImageProviderOptionsSchema,
|
|
11
11
|
openaiResponsesGenerateProviderOptionsSchema,
|
|
12
12
|
openaiResponsesProviderOptionsSchema
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-SCXEGDFV.js";
|
|
14
14
|
|
|
15
15
|
// src/provider.ts
|
|
16
16
|
function createOpenAI(options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@core-ai/openai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "OpenAI provider package for @core-ai/core-ai",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Omnifact (https://omnifact.ai)",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"test:watch": "vitest"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@core-ai/core-ai": "^0.
|
|
53
|
+
"@core-ai/core-ai": "^0.24.0",
|
|
54
54
|
"openai": "^6.46.0"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|