@veryfront/ext-llm-openai 0.1.1270 → 0.1.1271-rc.23066
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/esm/extensions/ext-llm-openai/src/openai-chat-stream.js +2 -2
- package/esm/extensions/ext-llm-openai/src/openai-provider.d.ts.map +1 -1
- package/esm/extensions/ext-llm-openai/src/openai-provider.js +72 -14
- package/esm/extensions/ext-llm-openai/src/openai-responses-stream.d.ts.map +1 -1
- package/esm/extensions/ext-llm-openai/src/openai-responses-stream.js +27 -3
- package/package.json +3 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { mergeUsage,
|
|
1
|
+
import { mergeUsage, ProviderStreamProtocolError, readGatewayBillingMode, readGatewayUsageCosts, readRecord, } from "veryfront/provider/shared";
|
|
2
2
|
import { appendOpenAIStreamToolArgument, isJsonObjectText, joinOpenAIStreamToolArguments, MAX_OPENAI_STREAM_TOOL_ARGUMENT_BYTES, MAX_OPENAI_STREAM_TOOL_ARGUMENT_FRAGMENTS, stripDoubleClosedToolArguments, } from "./openai-tool-input.js";
|
|
3
3
|
import { isBoundedOpenAIStreamString, MAX_OPENAI_STREAM_CONTENT_PARTS, MAX_OPENAI_STREAM_IDENTIFIER_BYTES, MAX_OPENAI_STREAM_TOOL_NAME_BYTES, } from "./openai-stream-metadata.js";
|
|
4
4
|
import { appendOpenAISseChunk, finishOpenAISseDecoding, parseOpenAISseBuffer, } from "./openai-sse-buffer.js";
|
|
5
5
|
export const MAX_OPENAI_STREAM_TOOL_CALLS = 1_024;
|
|
6
6
|
function invalidOpenAIStream(context, issue) {
|
|
7
7
|
const providerKind = context.providerKind ?? "openai";
|
|
8
|
-
return new
|
|
8
|
+
return new ProviderStreamProtocolError({
|
|
9
9
|
provider: providerKind,
|
|
10
10
|
status: 200,
|
|
11
11
|
message: `${context.providerLabel ?? providerKind} request failed: invalid successful stream (${issue})`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-provider.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-llm-openai/src/openai-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC/E,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EACZ,2BAA2B,EAC5B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,kBAAkB,
|
|
1
|
+
{"version":3,"file":"openai-provider.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-llm-openai/src/openai-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC/E,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EACZ,2BAA2B,EAC5B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,kBAAkB,EAQlB,aAAa,EACb,UAAU,EAEV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EAQpB,+BAA+B,EAEhC,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,KAAK,6BAA6B,EAClC,KAAK,+BAA+B,EACrC,MAAM,kCAAkC,CAAC;AA+B1C,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,+BAA+B,GAChC,CAAC;AAEF,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,+BAA+B;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,gGAAgG;IAChG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uBAAuB,CAAC,EAAE,6BAA6B,CAAC;IACxD,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AAk7BD,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAAC,+BAA+B,EAAE,2BAA2B,CAAC,CAqF5E;AAMD,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAAC,+BAA+B,EAAE,2BAA2B,CAAC,CA6J5E;AA0DD,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,gBAAgB,CA4ClB;AAED,qBAAa,cAAe,YAAW,WAAW;IAChD,QAAQ,CAAC,EAAE,YAAY;IAEvB,WAAW,CACT,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,iBAAiB,GACxB,YAAY,CAAC,+BAA+B,EAAE,2BAA2B,CAAC;IAyC7E,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,GAAG,gBAAgB;IAa7E,eAAe,CACb,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,iBAAiB,GACxB,YAAY,CAAC,+BAA+B,EAAE,2BAA2B,CAAC;CAY9E"}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module extensions/ext-llm-openai/openai-provider
|
|
9
9
|
*/
|
|
10
|
-
import { buildProviderError, createOpenAIRequestInit, createWarningCollector, getOpenAIChatCompletionsUrl, getOpenAIEmbeddingUrl, getOpenAIResponsesUrl, isNumberArray, mergeUsage, parseRetryAfterMs, ProviderError, ProviderOverloadedError, ProviderQuotaError, ProviderRateLimitError, ProviderRequestError, readGatewayBillingMode, readGatewayUsageCosts, readRecord, requestJson, requestStream, stringifyJsonValue, TOOL_INPUT_PENDING_THRESHOLD_MS, } from "veryfront/provider/shared";
|
|
10
|
+
import { buildProviderError, createOpenAIRequestInit, createWarningCollector, DEFAULT_PROVIDER_STREAM_HEADERS_TIMEOUT_MS, DEFAULT_PROVIDER_STREAM_TOTAL_HEADERS_BUDGET_MS, getOpenAIChatCompletionsUrl, getOpenAIEmbeddingUrl, getOpenAIResponsesUrl, isNumberArray, mergeUsage, notifyProviderRequestRetry, parseRetryAfterMs, ProviderError, ProviderOverloadedError, ProviderQuotaError, ProviderRateLimitError, ProviderRequestError, readGatewayBillingMode, readGatewayUsageCosts, readRecord, requestJson, requestStream, stringifyJsonValue, TOOL_INPUT_PENDING_THRESHOLD_MS, waitForProviderStreamRetry, } from "veryfront/provider/shared";
|
|
11
11
|
import { buildOpenAIChatRequest, } from "./openai-chat-request-builder.js";
|
|
12
12
|
import { MAX_OPENAI_STREAM_TOOL_CALLS, streamOpenAICompatibleParts } from "./openai-chat-stream.js";
|
|
13
13
|
import { buildOpenAIResponsesRequest } from "./openai-responses-request-builder.js";
|
|
@@ -756,6 +756,9 @@ export function createOpenAIModelRuntime(config, modelId) {
|
|
|
756
756
|
},
|
|
757
757
|
};
|
|
758
758
|
}
|
|
759
|
+
const MAX_OPENAI_RESPONSES_STREAM_REPLAYS = 2;
|
|
760
|
+
const OPENAI_RESPONSES_STREAM_REPLAY_DELAY_MS = 1_000;
|
|
761
|
+
const OPENAI_RESPONSES_STREAM_REPLAY_HEADERS_BUDGET_MS = 10_000;
|
|
759
762
|
export function createOpenAIResponsesRuntime(config, modelId) {
|
|
760
763
|
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
761
764
|
const providerLabel = getOpenAIProviderLabel(config);
|
|
@@ -800,27 +803,82 @@ export function createOpenAIResponsesRuntime(config, modelId) {
|
|
|
800
803
|
const body = buildOpenAIResponsesRequest(modelId, providerName, options, true, warnings);
|
|
801
804
|
const webSearchToolName = resolveOpenAIWebSearchDescriptor(options.tools)?.name;
|
|
802
805
|
const providerAbortScope = createOpenAIProviderAbortScope(options.abortSignal);
|
|
803
|
-
|
|
804
|
-
|
|
806
|
+
const requestInit = createOpenAIRequestInit({
|
|
807
|
+
apiKey: config.apiKey,
|
|
808
|
+
extraHeaders: options.headers,
|
|
809
|
+
body: JSON.stringify(body),
|
|
810
|
+
signal: providerAbortScope.controller.signal,
|
|
811
|
+
});
|
|
812
|
+
const headersBudgetStartedAt = Math.floor(performance.now());
|
|
813
|
+
const remainingHeadersBudgetMs = () => Math.max(0, DEFAULT_PROVIDER_STREAM_TOTAL_HEADERS_BUDGET_MS -
|
|
814
|
+
(Math.floor(performance.now()) - headersBudgetStartedAt));
|
|
815
|
+
const issueStream = (replay = false) => {
|
|
816
|
+
const budget = Math.min(replay
|
|
817
|
+
? OPENAI_RESPONSES_STREAM_REPLAY_HEADERS_BUDGET_MS
|
|
818
|
+
: DEFAULT_PROVIDER_STREAM_TOTAL_HEADERS_BUDGET_MS, remainingHeadersBudgetMs());
|
|
819
|
+
providerAbortScope.controller.signal.throwIfAborted();
|
|
820
|
+
return requestStream({
|
|
805
821
|
url,
|
|
806
822
|
fetchImpl,
|
|
807
823
|
providerLabel,
|
|
808
824
|
providerKind,
|
|
809
825
|
modelId,
|
|
810
|
-
init:
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
}),
|
|
826
|
+
init: requestInit,
|
|
827
|
+
headersTimeoutMs: Math.min(replay
|
|
828
|
+
? OPENAI_RESPONSES_STREAM_REPLAY_HEADERS_BUDGET_MS
|
|
829
|
+
: DEFAULT_PROVIDER_STREAM_HEADERS_TIMEOUT_MS, budget),
|
|
830
|
+
totalHeadersBudgetMs: budget,
|
|
816
831
|
});
|
|
832
|
+
};
|
|
833
|
+
try {
|
|
834
|
+
const responseStream = await issueStream();
|
|
835
|
+
// Match the existing provider retry bound/backoff within one header
|
|
836
|
+
// budget. A single yielded part permanently forbids replay, including
|
|
837
|
+
// tool and reasoning metadata; no consumer-visible work is duplicated.
|
|
838
|
+
const replayTransientStream = async function* () {
|
|
839
|
+
let currentStream = responseStream;
|
|
840
|
+
for (let replayCount = 0;; replayCount++) {
|
|
841
|
+
let yielded = false;
|
|
842
|
+
try {
|
|
843
|
+
for await (const part of streamOpenAIResponsesParts(currentStream, {
|
|
844
|
+
...responseContext,
|
|
845
|
+
webSearchToolName,
|
|
846
|
+
preserveRawOutputItems: true,
|
|
847
|
+
})) {
|
|
848
|
+
yielded = true;
|
|
849
|
+
yield part;
|
|
850
|
+
}
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
catch (error) {
|
|
854
|
+
const transient = (error instanceof ProviderOverloadedError ||
|
|
855
|
+
error instanceof ProviderRateLimitError) && error.retryable;
|
|
856
|
+
if (yielded || replayCount >= MAX_OPENAI_RESPONSES_STREAM_REPLAYS || !transient) {
|
|
857
|
+
throw error;
|
|
858
|
+
}
|
|
859
|
+
providerAbortScope.controller.signal.throwIfAborted();
|
|
860
|
+
const delayMs = OPENAI_RESPONSES_STREAM_REPLAY_DELAY_MS * 2 ** replayCount;
|
|
861
|
+
if (delayMs >= remainingHeadersBudgetMs())
|
|
862
|
+
throw error;
|
|
863
|
+
notifyProviderRequestRetry({
|
|
864
|
+
providerLabel,
|
|
865
|
+
modelId,
|
|
866
|
+
reason: "stream interrupted",
|
|
867
|
+
attempt: replayCount + 2,
|
|
868
|
+
maxAttempts: MAX_OPENAI_RESPONSES_STREAM_REPLAYS + 1,
|
|
869
|
+
delayMs,
|
|
870
|
+
});
|
|
871
|
+
await waitForProviderStreamRetry(delayMs, providerAbortScope.controller.signal);
|
|
872
|
+
providerAbortScope.controller.signal.throwIfAborted();
|
|
873
|
+
if (remainingHeadersBudgetMs() <= 0)
|
|
874
|
+
throw error;
|
|
875
|
+
currentStream = await issueStream(true);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
};
|
|
817
879
|
const drained = warnings.drain();
|
|
818
880
|
return {
|
|
819
|
-
stream: createCancelableOpenAIProviderStream(
|
|
820
|
-
...responseContext,
|
|
821
|
-
webSearchToolName,
|
|
822
|
-
preserveRawOutputItems: true,
|
|
823
|
-
}), providerAbortScope.controller, providerAbortScope.dispose),
|
|
881
|
+
stream: createCancelableOpenAIProviderStream(replayTransientStream(), providerAbortScope.controller, providerAbortScope.dispose),
|
|
824
882
|
...(drained.length > 0 ? { warnings: drained } : {}),
|
|
825
883
|
};
|
|
826
884
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses-stream.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-llm-openai/src/openai-responses-stream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"openai-responses-stream.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-llm-openai/src/openai-responses-stream.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AA8E9D,KAAK,4BAA4B,GAAG;IAClC,YAAY,CAAC,EAAE,QAAQ,GAAG,SAAS,GAAG,YAAY,CAAC;IACnD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAEF,eAAO,MAAM,wCAAwC,QAAkB,CAAC;AACxE,eAAO,MAAM,uCAAuC,QAAyC,CAAC;AAC9F,eAAO,MAAM,wCAAwC,OAAQ,CAAC;AAC9D,eAAO,MAAM,yCAAyC,OAAQ,CAAC;AA4C/D;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,GAAG,SAAS,CA+CtF;AAED,wBAAgB,oCAAoC,CAClD,GAAG,EAAE,OAAO,GACX,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAiBlD;AAED;;;GAGG;AACH,wBAAuB,0BAA0B,CAC/C,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,GAAE,4BAAiC,GACzC,aAAa,CAAC,OAAO,CAAC,CAgiDxB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mergeUsage, ProviderRequestError, readGatewayBillingMode, readGatewayUsageCosts, readRecord, stringifyJsonValue, } from "veryfront/provider/shared";
|
|
1
|
+
import { mergeUsage, ProviderOverloadedError, ProviderQuotaError, ProviderRateLimitError, ProviderRequestError, ProviderStreamProtocolError, readGatewayBillingMode, readGatewayUsageCosts, readRecord, stringifyJsonValue, } from "veryfront/provider/shared";
|
|
2
2
|
import { appendOpenAIStreamToolArgument, isJsonObjectText, joinOpenAIStreamToolArguments, MAX_OPENAI_STREAM_TOOL_ARGUMENT_BYTES, MAX_OPENAI_STREAM_TOOL_ARGUMENT_FRAGMENTS, } from "./openai-tool-input.js";
|
|
3
3
|
import { isBoundedOpenAIStreamString, MAX_OPENAI_STREAM_IDENTIFIER_BYTES, MAX_OPENAI_STREAM_ITEM_TYPE_BYTES, MAX_OPENAI_STREAM_TOOL_NAME_BYTES, } from "./openai-stream-metadata.js";
|
|
4
4
|
import { appendOpenAISseChunk, finishOpenAISseDecoding, parseOpenAISseBuffer, } from "./openai-sse-buffer.js";
|
|
@@ -11,7 +11,7 @@ const MAX_OPENAI_STREAM_MESSAGE_DELTA_BATCH_BYTES = 64 * 1024;
|
|
|
11
11
|
const MAX_OPENAI_STREAM_MESSAGE_DELTA_BATCH_FRAGMENTS = 256;
|
|
12
12
|
function invalidOpenAIResponsesStream(context, issue) {
|
|
13
13
|
const providerKind = context.providerKind ?? "openai";
|
|
14
|
-
return new
|
|
14
|
+
return new ProviderStreamProtocolError({
|
|
15
15
|
provider: providerKind,
|
|
16
16
|
status: 200,
|
|
17
17
|
message: `${context.providerLabel ?? providerKind} request failed: invalid successful stream (${issue})`,
|
|
@@ -486,7 +486,31 @@ export async function* streamOpenAIResponsesParts(stream, context = {}) {
|
|
|
486
486
|
throw invalidOpenAIResponsesStream(context, "event type was missing");
|
|
487
487
|
}
|
|
488
488
|
if (type === "error") {
|
|
489
|
-
|
|
489
|
+
// The service also emits a nested error object after accepting HTTP 200.
|
|
490
|
+
// An explicitly malformed nested envelope must not fall back to flat fields.
|
|
491
|
+
const error = Object.hasOwn(record, "error") ? readRecord(record.error) : record;
|
|
492
|
+
if (!error ||
|
|
493
|
+
(error.code !== null && typeof error.code !== "string") ||
|
|
494
|
+
typeof error.message !== "string") {
|
|
495
|
+
throw invalidOpenAIResponsesStream(context, "provider error event was malformed");
|
|
496
|
+
}
|
|
497
|
+
const options = {
|
|
498
|
+
provider: context.providerKind ?? "openai",
|
|
499
|
+
// The stream was accepted with HTTP 200; the event does not supply a
|
|
500
|
+
// replacement HTTP status. Only known error codes grant retryability.
|
|
501
|
+
status: 200,
|
|
502
|
+
message: "Provider declared a response stream failure",
|
|
503
|
+
retryable: false,
|
|
504
|
+
};
|
|
505
|
+
if (error.code === "server_error") {
|
|
506
|
+
throw new ProviderOverloadedError({ ...options, retryable: true });
|
|
507
|
+
}
|
|
508
|
+
if (error.code === "rate_limit_exceeded") {
|
|
509
|
+
throw new ProviderRateLimitError({ ...options, retryable: true });
|
|
510
|
+
}
|
|
511
|
+
if (error.code === "insufficient_quota")
|
|
512
|
+
throw new ProviderQuotaError(options);
|
|
513
|
+
throw new ProviderRequestError(options);
|
|
490
514
|
}
|
|
491
515
|
if (type === "response.output_item.added") {
|
|
492
516
|
const { item, itemType, itemId } = readEventItem(record, "added output");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfront/ext-llm-openai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1271-rc.23066",
|
|
4
4
|
"description": "Veryfront first-party extension package for ext-llm-openai",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"veryfront",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"capabilities": []
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"veryfront": "^0.1.
|
|
48
|
+
"veryfront": "^0.1.1271-rc.23066"
|
|
49
49
|
},
|
|
50
50
|
"type": "module",
|
|
51
51
|
"types": "./esm/extensions/ext-llm-openai/src/index.d.ts",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"NOTICE",
|
|
56
56
|
"README.md"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "e017bc1e7e5f440fd3e9ee25019d2acc253ce927"
|
|
59
59
|
}
|