@jeffreycao/copilot-api 2.2.13 → 2.2.15
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/main.js +1 -1
- package/dist/{server-Bmd_z-tv.js → server-BRGh2SlD.js} +202 -106
- package/dist/server-BRGh2SlD.js.map +1 -0
- package/dist/{start-z_xmbJbc.js → start-Dk0QorqU.js} +2 -2
- package/dist/{start-z_xmbJbc.js.map → start-Dk0QorqU.js.map} +1 -1
- package/package.json +1 -1
- package/dist/server-Bmd_z-tv.js.map +0 -1
package/dist/main.js
CHANGED
|
@@ -30,7 +30,7 @@ if (isMcpFastPath(process.argv)) {
|
|
|
30
30
|
const { auth } = await import("./auth-C-RWNXGU.js");
|
|
31
31
|
const { debug } = await import("./debug-41w6Whae.js");
|
|
32
32
|
const { mcp } = await import("./mcp-fpSlKZxK.js");
|
|
33
|
-
const { start } = await import("./start-
|
|
33
|
+
const { start } = await import("./start-Dk0QorqU.js");
|
|
34
34
|
await runMain(defineCommand({
|
|
35
35
|
meta: {
|
|
36
36
|
name: "copilot-api",
|
|
@@ -374,6 +374,21 @@ async function resolveProviderConfig(providerName) {
|
|
|
374
374
|
}
|
|
375
375
|
return getProviderConfig(normalizedProviderName);
|
|
376
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Returns the parsed "provider/model" alias only when that provider is
|
|
379
|
+
* actually configured, otherwise null so the caller falls through to its
|
|
380
|
+
* default flow. GitHub Copilot enterprise models can ship with namespaced ids
|
|
381
|
+
* such as "org/family/model": without this check the first segment is
|
|
382
|
+
* misrouted to a non-existent provider and surfaced as a 400/404.
|
|
383
|
+
*/
|
|
384
|
+
async function ensureConfiguredProviderModelAlias(alias, resolveConfig = resolveProviderConfig) {
|
|
385
|
+
if (!alias) return null;
|
|
386
|
+
return await resolveConfig(alias.provider) ? alias : null;
|
|
387
|
+
}
|
|
388
|
+
/** parseProviderModelAlias + ensureConfiguredProviderModelAlias. */
|
|
389
|
+
async function resolveConfiguredProviderModelAlias(model, resolveConfig = resolveProviderConfig) {
|
|
390
|
+
return ensureConfiguredProviderModelAlias(parseProviderModelAlias(model), resolveConfig);
|
|
391
|
+
}
|
|
377
392
|
//#endregion
|
|
378
393
|
//#region src/lib/event-bus.ts
|
|
379
394
|
var EventBus = class {
|
|
@@ -2845,10 +2860,12 @@ async function handleAlphaSearchRequest(c, resolvedProviderConfig) {
|
|
|
2845
2860
|
}
|
|
2846
2861
|
}
|
|
2847
2862
|
const fallbackRequestedModel = messagesBackedModel ? payload.model : requestedModel;
|
|
2848
|
-
if (providerModelAlias)
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2863
|
+
if (providerModelAlias) {
|
|
2864
|
+
if (await ensureConfiguredProviderModelAlias(providerModelAlias, alphaSearchRouteDependencies.resolveProviderConfig)) return await alphaSearchRouteDependencies.handleAlphaSearchResponses(c, {
|
|
2865
|
+
provider: providerModelAlias.provider,
|
|
2866
|
+
request: payload
|
|
2867
|
+
});
|
|
2868
|
+
}
|
|
2852
2869
|
return await alphaSearchRouteDependencies.handleAlphaSearchResponses(c, { request: {
|
|
2853
2870
|
...payload,
|
|
2854
2871
|
model: fallbackRequestedModel
|
|
@@ -3127,7 +3144,7 @@ async function handleCompletion$1(c) {
|
|
|
3127
3144
|
const requestedModel = payload.model;
|
|
3128
3145
|
payload.model = resolveMappedModel(payload.model);
|
|
3129
3146
|
if (payload.model !== requestedModel) consola.debug(`Resolved model mapping: ${requestedModel} -> ${payload.model}`);
|
|
3130
|
-
const providerModelAlias =
|
|
3147
|
+
const providerModelAlias = await resolveConfiguredProviderModelAlias(payload.model);
|
|
3131
3148
|
if (providerModelAlias) {
|
|
3132
3149
|
payload.model = providerModelAlias.model;
|
|
3133
3150
|
return await handleProviderChatCompletionsForProvider(c, {
|
|
@@ -4419,7 +4436,7 @@ async function handleCountTokens(c) {
|
|
|
4419
4436
|
const anthropicPayload = await c.req.json();
|
|
4420
4437
|
anthropicPayload.model = resolveMappedModel(anthropicPayload.model);
|
|
4421
4438
|
normalizeSystemMessages(anthropicPayload);
|
|
4422
|
-
const providerModelAlias =
|
|
4439
|
+
const providerModelAlias = await resolveConfiguredProviderModelAlias(anthropicPayload.model);
|
|
4423
4440
|
if (providerModelAlias) {
|
|
4424
4441
|
anthropicPayload.model = providerModelAlias.model;
|
|
4425
4442
|
return await handleProviderCountTokensForProvider(c, {
|
|
@@ -4520,6 +4537,7 @@ function completePendingMessage(state, events, chunk) {
|
|
|
4520
4537
|
if (chunk?.usage) state.pendingMessageDelta.usage = getAnthropicUsageFromOpenAIChunk(chunk);
|
|
4521
4538
|
events.push(state.pendingMessageDelta, { type: "message_stop" });
|
|
4522
4539
|
state.pendingMessageDelta = void 0;
|
|
4540
|
+
state.messageCompleted = true;
|
|
4523
4541
|
}
|
|
4524
4542
|
function handleFinish(choice, state, context) {
|
|
4525
4543
|
const { events, chunk } = context;
|
|
@@ -4797,6 +4815,15 @@ function closeThinkingBlockIfOpen(state, events) {
|
|
|
4797
4815
|
state.thinkingBlockOpen = false;
|
|
4798
4816
|
}
|
|
4799
4817
|
}
|
|
4818
|
+
function translateErrorToAnthropicErrorEvent() {
|
|
4819
|
+
return {
|
|
4820
|
+
type: "error",
|
|
4821
|
+
error: {
|
|
4822
|
+
type: "api_error",
|
|
4823
|
+
message: "An unexpected error occurred during streaming."
|
|
4824
|
+
}
|
|
4825
|
+
};
|
|
4826
|
+
}
|
|
4800
4827
|
//#endregion
|
|
4801
4828
|
//#region src/routes/messages/responses-translation.ts
|
|
4802
4829
|
const MESSAGE_TYPE = "message";
|
|
@@ -6144,6 +6171,7 @@ const containsVisionContent = (value) => {
|
|
|
6144
6171
|
//#region src/routes/messages/web-search/fulfill.ts
|
|
6145
6172
|
const webSearchFlowDependencies = {
|
|
6146
6173
|
createResponses,
|
|
6174
|
+
resolveProviderConfig,
|
|
6147
6175
|
createUsageRecorder: (payload, sessionId, webSearchModel) => createCopilotTokenUsageRecorder({
|
|
6148
6176
|
endpoint: "responses",
|
|
6149
6177
|
fallbackSessionId: sessionId,
|
|
@@ -6165,14 +6193,17 @@ const stripWebSearchServerTool = (payload) => {
|
|
|
6165
6193
|
if (!Array.isArray(payload.tools)) return;
|
|
6166
6194
|
payload.tools = payload.tools.filter((tool) => !isWebSearchServerTool(tool));
|
|
6167
6195
|
};
|
|
6168
|
-
const resolveWebSearchRoute = (payload, options) => {
|
|
6196
|
+
const resolveWebSearchRoute = async (payload, options) => {
|
|
6169
6197
|
const { webSearchModel, responsesWebSearchEnabled } = options;
|
|
6170
6198
|
if (!webSearchModel || !isWebSearchOnlyRequest(payload)) return { kind: "strip" };
|
|
6171
6199
|
const alias = parseProviderModelAlias(webSearchModel);
|
|
6172
|
-
if (alias)
|
|
6173
|
-
|
|
6174
|
-
|
|
6175
|
-
|
|
6200
|
+
if (alias) {
|
|
6201
|
+
const configuredAlias = await ensureConfiguredProviderModelAlias(alias, options.resolveProviderConfig ?? resolveProviderConfig);
|
|
6202
|
+
return configuredAlias ? {
|
|
6203
|
+
kind: "provider",
|
|
6204
|
+
alias: configuredAlias
|
|
6205
|
+
} : { kind: "strip" };
|
|
6206
|
+
}
|
|
6176
6207
|
if (responsesWebSearchEnabled) return {
|
|
6177
6208
|
kind: "responses",
|
|
6178
6209
|
model: webSearchModel
|
|
@@ -6274,9 +6305,10 @@ const createUsageRecorder = (payload, sessionId, webSearchModel) => webSearchFlo
|
|
|
6274
6305
|
const tryHandleWebSearch = async (c, payload, options) => {
|
|
6275
6306
|
if (!hasWebSearchServerTool(payload)) return null;
|
|
6276
6307
|
normalizeSystemMessages(payload);
|
|
6277
|
-
const route = resolveWebSearchRoute(payload, {
|
|
6308
|
+
const route = await resolveWebSearchRoute(payload, {
|
|
6278
6309
|
webSearchModel: getMessageApiWebSearchModel(),
|
|
6279
|
-
responsesWebSearchEnabled: isResponsesApiWebSearchEnabled()
|
|
6310
|
+
responsesWebSearchEnabled: isResponsesApiWebSearchEnabled(),
|
|
6311
|
+
resolveProviderConfig: webSearchFlowDependencies.resolveProviderConfig
|
|
6280
6312
|
});
|
|
6281
6313
|
if (route.kind === "provider") {
|
|
6282
6314
|
payload.model = route.alias.model;
|
|
@@ -6916,35 +6948,54 @@ const streamProviderMessages = ({ c, modelConfig, payload, pricingCurrency, prov
|
|
|
6916
6948
|
const recordUsage = createProviderMessagesUsageRecorder(payload, provider, modelConfig, pricingCurrency, usageEndpoint);
|
|
6917
6949
|
return streamSSE(c, async (stream) => {
|
|
6918
6950
|
let usage = {};
|
|
6951
|
+
let messageStopSeen = false;
|
|
6952
|
+
let errorSeen = false;
|
|
6919
6953
|
const openRouterThinkingState = {
|
|
6920
6954
|
signedThinkingBlockIndexes: /* @__PURE__ */ new Set(),
|
|
6921
6955
|
thinkingBlockIndexes: /* @__PURE__ */ new Set()
|
|
6922
6956
|
};
|
|
6923
|
-
|
|
6924
|
-
|
|
6925
|
-
|
|
6926
|
-
|
|
6927
|
-
|
|
6928
|
-
|
|
6929
|
-
|
|
6957
|
+
try {
|
|
6958
|
+
for await (const chunk of events(upstreamResponse)) {
|
|
6959
|
+
logger$10.debug("provider.messages.raw_stream_event:", chunk.data);
|
|
6960
|
+
const eventName = chunk.event;
|
|
6961
|
+
if (eventName === "ping") {
|
|
6962
|
+
await stream.writeSSE({
|
|
6963
|
+
event: "ping",
|
|
6964
|
+
data: "{\"type\":\"ping\"}"
|
|
6965
|
+
});
|
|
6966
|
+
continue;
|
|
6967
|
+
}
|
|
6968
|
+
let data = chunk.data;
|
|
6969
|
+
if (!data) continue;
|
|
6970
|
+
if (chunk.data === "[DONE]") break;
|
|
6971
|
+
const parsed = parseProviderStreamEvent(data);
|
|
6972
|
+
if (parsed) {
|
|
6973
|
+
usage = mergeAnthropicUsage(usage, parsed.usage);
|
|
6974
|
+
data = parsed.data;
|
|
6975
|
+
if (parsed.type === "message_stop") messageStopSeen = true;
|
|
6976
|
+
else if (parsed.type === "error") errorSeen = true;
|
|
6977
|
+
}
|
|
6978
|
+
const streamEvents = provider === "openrouter" ? normalizeOpenRouterStreamEvents(eventName, data, openRouterThinkingState) : [{
|
|
6979
|
+
data,
|
|
6980
|
+
event: eventName
|
|
6981
|
+
}];
|
|
6982
|
+
for (const streamEvent of streamEvents) await stream.writeSSE({
|
|
6983
|
+
event: streamEvent.event,
|
|
6984
|
+
data: streamEvent.data
|
|
6930
6985
|
});
|
|
6931
|
-
continue;
|
|
6932
|
-
}
|
|
6933
|
-
let data = chunk.data;
|
|
6934
|
-
if (!data) continue;
|
|
6935
|
-
if (chunk.data === "[DONE]") break;
|
|
6936
|
-
const parsed = parseProviderStreamEvent(data);
|
|
6937
|
-
if (parsed) {
|
|
6938
|
-
usage = mergeAnthropicUsage(usage, parsed.usage);
|
|
6939
|
-
data = parsed.data;
|
|
6940
6986
|
}
|
|
6941
|
-
|
|
6942
|
-
|
|
6943
|
-
|
|
6944
|
-
|
|
6945
|
-
|
|
6946
|
-
|
|
6947
|
-
|
|
6987
|
+
} catch (error) {
|
|
6988
|
+
logger$10.warn("provider.messages.stream_interrupted:", {
|
|
6989
|
+
error,
|
|
6990
|
+
provider
|
|
6991
|
+
});
|
|
6992
|
+
}
|
|
6993
|
+
if (!messageStopSeen && !errorSeen) {
|
|
6994
|
+
logger$10.warn("provider.messages.stream_incomplete:", { provider });
|
|
6995
|
+
const errorEvent = translateErrorToAnthropicErrorEvent();
|
|
6996
|
+
await stream.writeSSE({
|
|
6997
|
+
event: errorEvent.type,
|
|
6998
|
+
data: JSON.stringify(errorEvent)
|
|
6948
6999
|
});
|
|
6949
7000
|
}
|
|
6950
7001
|
recordUsage(usage);
|
|
@@ -6957,36 +7008,44 @@ const streamOpenAICompatibleProviderMessages = ({ c, modelConfig, payload, prici
|
|
|
6957
7008
|
let usage = {};
|
|
6958
7009
|
const streamState = {
|
|
6959
7010
|
messageStartSent: false,
|
|
7011
|
+
messageCompleted: false,
|
|
6960
7012
|
contentBlockIndex: 0,
|
|
6961
7013
|
contentBlockOpen: false,
|
|
6962
7014
|
toolCalls: {},
|
|
6963
7015
|
thinkingBlockOpen: false
|
|
6964
7016
|
};
|
|
6965
|
-
|
|
6966
|
-
|
|
6967
|
-
|
|
6968
|
-
|
|
6969
|
-
|
|
6970
|
-
|
|
6971
|
-
|
|
6972
|
-
|
|
6973
|
-
|
|
6974
|
-
|
|
6975
|
-
if (chunk.data === "[DONE]")
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
|
|
6981
|
-
|
|
6982
|
-
|
|
6983
|
-
const
|
|
6984
|
-
|
|
6985
|
-
|
|
6986
|
-
|
|
6987
|
-
|
|
6988
|
-
|
|
7017
|
+
try {
|
|
7018
|
+
for await (const chunk of events(upstreamResponse)) {
|
|
7019
|
+
logger$10.debug("provider.messages.openai_compatible.raw_stream_event:", chunk.data);
|
|
7020
|
+
if (chunk.event === "ping") {
|
|
7021
|
+
await stream.writeSSE({
|
|
7022
|
+
event: "ping",
|
|
7023
|
+
data: "{\"type\":\"ping\"}"
|
|
7024
|
+
});
|
|
7025
|
+
continue;
|
|
7026
|
+
}
|
|
7027
|
+
if (!chunk.data || chunk.data === "[DONE]") {
|
|
7028
|
+
if (chunk.data === "[DONE]") break;
|
|
7029
|
+
continue;
|
|
7030
|
+
}
|
|
7031
|
+
const parsed = parseOpenAICompatibleStreamChunk(chunk.data);
|
|
7032
|
+
if (!parsed) continue;
|
|
7033
|
+
if (parsed.usage) usage = normalizeOpenAIUsage(parsed.usage);
|
|
7034
|
+
const events = translateChunkToAnthropicEvents(parsed, streamState);
|
|
7035
|
+
for (const event of events) {
|
|
7036
|
+
const eventData = JSON.stringify(event);
|
|
7037
|
+
debugLazy(logger$10, () => ["provider.messages.openai_compatible.translated_event:", eventData]);
|
|
7038
|
+
await stream.writeSSE({
|
|
7039
|
+
event: event.type,
|
|
7040
|
+
data: eventData
|
|
7041
|
+
});
|
|
7042
|
+
}
|
|
6989
7043
|
}
|
|
7044
|
+
} catch (error) {
|
|
7045
|
+
logger$10.warn("provider.messages.openai_compatible.stream_interrupted:", {
|
|
7046
|
+
error,
|
|
7047
|
+
provider
|
|
7048
|
+
});
|
|
6990
7049
|
}
|
|
6991
7050
|
for (const event of flushPendingAnthropicStreamEvents(streamState)) {
|
|
6992
7051
|
const eventData = JSON.stringify(event);
|
|
@@ -6996,6 +7055,14 @@ const streamOpenAICompatibleProviderMessages = ({ c, modelConfig, payload, prici
|
|
|
6996
7055
|
data: eventData
|
|
6997
7056
|
});
|
|
6998
7057
|
}
|
|
7058
|
+
if (!streamState.messageCompleted) {
|
|
7059
|
+
logger$10.warn("provider.messages.openai_compatible.stream_incomplete:", { provider });
|
|
7060
|
+
const errorEvent = translateErrorToAnthropicErrorEvent();
|
|
7061
|
+
await stream.writeSSE({
|
|
7062
|
+
event: errorEvent.type,
|
|
7063
|
+
data: JSON.stringify(errorEvent)
|
|
7064
|
+
});
|
|
7065
|
+
}
|
|
6999
7066
|
recordUsage(usage);
|
|
7000
7067
|
});
|
|
7001
7068
|
};
|
|
@@ -7072,14 +7139,17 @@ const parseProviderStreamEvent = (data) => {
|
|
|
7072
7139
|
if (parsed.type === "message_start") return {
|
|
7073
7140
|
data: JSON.stringify(parsed),
|
|
7074
7141
|
model: parsed.message.model,
|
|
7142
|
+
type: parsed.type,
|
|
7075
7143
|
usage: normalizeAnthropicUsage(parsed.message.usage)
|
|
7076
7144
|
};
|
|
7077
7145
|
if (parsed.type === "message_delta") return {
|
|
7078
7146
|
data: JSON.stringify(parsed),
|
|
7147
|
+
type: parsed.type,
|
|
7079
7148
|
usage: normalizeAnthropicUsage(parsed.usage)
|
|
7080
7149
|
};
|
|
7081
7150
|
return {
|
|
7082
7151
|
data: JSON.stringify(parsed),
|
|
7152
|
+
type: parsed.type,
|
|
7083
7153
|
usage: {}
|
|
7084
7154
|
};
|
|
7085
7155
|
} catch (error) {
|
|
@@ -7283,29 +7353,34 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
|
|
|
7283
7353
|
let usage = {};
|
|
7284
7354
|
const streamState = {
|
|
7285
7355
|
messageStartSent: false,
|
|
7356
|
+
messageCompleted: false,
|
|
7286
7357
|
contentBlockIndex: 0,
|
|
7287
7358
|
contentBlockOpen: false,
|
|
7288
7359
|
toolCalls: {},
|
|
7289
7360
|
thinkingBlockOpen: false
|
|
7290
7361
|
};
|
|
7291
|
-
|
|
7292
|
-
|
|
7293
|
-
|
|
7294
|
-
|
|
7295
|
-
|
|
7296
|
-
|
|
7297
|
-
|
|
7298
|
-
|
|
7299
|
-
|
|
7300
|
-
|
|
7301
|
-
|
|
7302
|
-
const
|
|
7303
|
-
|
|
7304
|
-
|
|
7305
|
-
|
|
7306
|
-
|
|
7307
|
-
|
|
7362
|
+
try {
|
|
7363
|
+
for await (const rawEvent of response) {
|
|
7364
|
+
debugJson(logger, "Copilot raw stream event:", rawEvent);
|
|
7365
|
+
if (rawEvent.data === "[DONE]") break;
|
|
7366
|
+
if (!rawEvent.data) continue;
|
|
7367
|
+
const chunk = JSON.parse(rawEvent.data);
|
|
7368
|
+
if (chunk.usage || chunk.copilot_usage) usage = {
|
|
7369
|
+
...normalizeOpenAIUsage(chunk.usage),
|
|
7370
|
+
total_nano_aiu: normalizeOptionalToken(chunk.copilot_usage?.total_nano_aiu)
|
|
7371
|
+
};
|
|
7372
|
+
const events = translateChunkToAnthropicEvents(chunk, streamState);
|
|
7373
|
+
for (const event of events) {
|
|
7374
|
+
const eventData = JSON.stringify(event);
|
|
7375
|
+
debugLazy(logger, () => ["Translated Anthropic event:", eventData]);
|
|
7376
|
+
await stream.writeSSE({
|
|
7377
|
+
event: event.type,
|
|
7378
|
+
data: eventData
|
|
7379
|
+
});
|
|
7380
|
+
}
|
|
7308
7381
|
}
|
|
7382
|
+
} catch (error) {
|
|
7383
|
+
logger.warn("Chat completions stream interrupted:", error);
|
|
7309
7384
|
}
|
|
7310
7385
|
for (const event of flushPendingAnthropicStreamEvents(streamState)) {
|
|
7311
7386
|
const eventData = JSON.stringify(event);
|
|
@@ -7315,6 +7390,14 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
|
|
|
7315
7390
|
data: eventData
|
|
7316
7391
|
});
|
|
7317
7392
|
}
|
|
7393
|
+
if (!streamState.messageCompleted) {
|
|
7394
|
+
logger.warn("Chat completions stream ended without completion; sending error event");
|
|
7395
|
+
const errorEvent = translateErrorToAnthropicErrorEvent();
|
|
7396
|
+
await stream.writeSSE({
|
|
7397
|
+
event: errorEvent.type,
|
|
7398
|
+
data: JSON.stringify(errorEvent)
|
|
7399
|
+
});
|
|
7400
|
+
}
|
|
7318
7401
|
recordUsage(usage);
|
|
7319
7402
|
});
|
|
7320
7403
|
};
|
|
@@ -7414,24 +7497,40 @@ const handleWithMessagesApi = async (c, anthropicPayload, options) => {
|
|
|
7414
7497
|
logger.debug("Streaming response from Copilot (Messages API)");
|
|
7415
7498
|
return streamSSE(c, async (stream) => {
|
|
7416
7499
|
let usage = {};
|
|
7417
|
-
|
|
7418
|
-
|
|
7419
|
-
|
|
7420
|
-
|
|
7421
|
-
|
|
7422
|
-
|
|
7423
|
-
|
|
7424
|
-
|
|
7425
|
-
|
|
7426
|
-
|
|
7427
|
-
|
|
7428
|
-
|
|
7429
|
-
|
|
7430
|
-
|
|
7431
|
-
|
|
7500
|
+
let messageStopSeen = false;
|
|
7501
|
+
let errorSeen = false;
|
|
7502
|
+
try {
|
|
7503
|
+
for await (const event of response) {
|
|
7504
|
+
const eventName = event.event;
|
|
7505
|
+
const data = event.data ?? "";
|
|
7506
|
+
if (data === "[DONE]") break;
|
|
7507
|
+
if (!data) continue;
|
|
7508
|
+
debugLazy(logger, () => ["Messages raw stream event:", data]);
|
|
7509
|
+
const parsedEvent = parseAnthropicStreamEvent(data);
|
|
7510
|
+
if (parsedEvent?.type === "message_start") usage = mergeAnthropicUsage(usage, {
|
|
7511
|
+
...normalizeAnthropicUsage(parsedEvent.message.usage),
|
|
7512
|
+
...normalizeCopilotUsage(parsedEvent.message.copilot_usage)
|
|
7513
|
+
});
|
|
7514
|
+
else if (parsedEvent?.type === "message_delta") usage = mergeAnthropicUsage(usage, {
|
|
7515
|
+
...normalizeAnthropicUsage(parsedEvent.usage),
|
|
7516
|
+
...normalizeCopilotUsage(parsedEvent.copilot_usage)
|
|
7517
|
+
});
|
|
7518
|
+
if (parsedEvent?.type === "message_stop" || eventName === "message_stop") messageStopSeen = true;
|
|
7519
|
+
else if (parsedEvent?.type === "error" || eventName === "error") errorSeen = true;
|
|
7520
|
+
await stream.writeSSE({
|
|
7521
|
+
event: eventName,
|
|
7522
|
+
data
|
|
7523
|
+
});
|
|
7524
|
+
}
|
|
7525
|
+
} catch (error) {
|
|
7526
|
+
logger.warn("Messages stream interrupted:", error);
|
|
7527
|
+
}
|
|
7528
|
+
if (!messageStopSeen && !errorSeen) {
|
|
7529
|
+
logger.warn("Messages stream ended without completion; sending error event");
|
|
7530
|
+
const errorEvent = translateErrorToAnthropicErrorEvent();
|
|
7432
7531
|
await stream.writeSSE({
|
|
7433
|
-
event:
|
|
7434
|
-
data
|
|
7532
|
+
event: errorEvent.type,
|
|
7533
|
+
data: JSON.stringify(errorEvent)
|
|
7435
7534
|
});
|
|
7436
7535
|
}
|
|
7437
7536
|
recordUsage(usage);
|
|
@@ -7560,7 +7659,7 @@ async function handleCompletionPayload(c, anthropicPayload, dispatchOptions = {}
|
|
|
7560
7659
|
consola.debug(`Claude auto model override: ${anthropicPayload.model} -> ${claudeAutoModel}`);
|
|
7561
7660
|
anthropicPayload.model = claudeAutoModel;
|
|
7562
7661
|
}
|
|
7563
|
-
const providerModelAlias =
|
|
7662
|
+
const providerModelAlias = await resolveConfiguredProviderModelAlias(anthropicPayload.model, providerMessagesHandlerDependencies.resolveProviderConfig);
|
|
7564
7663
|
if (providerModelAlias) {
|
|
7565
7664
|
anthropicPayload.model = providerModelAlias.model;
|
|
7566
7665
|
return await handleProviderMessagesForProvider(c, {
|
|
@@ -9148,12 +9247,14 @@ function appendUserBlock(messages, block) {
|
|
|
9148
9247
|
};
|
|
9149
9248
|
messages.push(message);
|
|
9150
9249
|
}
|
|
9151
|
-
function parseFunctionArguments(value,
|
|
9250
|
+
function parseFunctionArguments(value, _path) {
|
|
9152
9251
|
try {
|
|
9153
9252
|
const parsed = JSON.parse(value);
|
|
9154
9253
|
if (isRecord(parsed)) return parsed;
|
|
9155
|
-
} catch {
|
|
9156
|
-
|
|
9254
|
+
} catch {
|
|
9255
|
+
return {};
|
|
9256
|
+
}
|
|
9257
|
+
return {};
|
|
9157
9258
|
}
|
|
9158
9259
|
function parseDataUrl(value) {
|
|
9159
9260
|
const match = /^data:([^;,]+);base64,(.*)$/su.exec(value);
|
|
@@ -9394,12 +9495,7 @@ async function* translateMessagesStream(chunks, context) {
|
|
|
9394
9495
|
if (state.messageStopped) break;
|
|
9395
9496
|
}
|
|
9396
9497
|
if (!state.initialized) throw new ResponsesMessagesTranslationError("Messages API returned an empty stream", 502);
|
|
9397
|
-
if (!state.messageStopped)
|
|
9398
|
-
for (const translated of closeAllBlocks(state)) yield translated;
|
|
9399
|
-
for (const translated of finishCompaction(state)) yield translated;
|
|
9400
|
-
state.messageStopped = true;
|
|
9401
|
-
yield createTerminalEvent(state);
|
|
9402
|
-
}
|
|
9498
|
+
if (!state.messageStopped) throw new ResponsesMessagesTranslationError("Messages stream ended without a message_stop event", 502);
|
|
9403
9499
|
} catch (error) {
|
|
9404
9500
|
if (!state.initialized) throw error;
|
|
9405
9501
|
yield createErrorEvent(state, error);
|
|
@@ -10315,7 +10411,7 @@ const handleResponses = async (c) => {
|
|
|
10315
10411
|
const requestedModel = payload.model;
|
|
10316
10412
|
payload.model = responsesHandlerDependencies.resolveMappedModel(payload.model);
|
|
10317
10413
|
if (payload.model !== requestedModel) consola.debug(`Resolved model mapping: ${requestedModel} -> ${payload.model}`);
|
|
10318
|
-
const providerModelAlias =
|
|
10414
|
+
const providerModelAlias = await resolveConfiguredProviderModelAlias(payload.model, providerResponsesHandlerDependencies.resolveProviderConfig);
|
|
10319
10415
|
if (providerModelAlias) {
|
|
10320
10416
|
payload.model = providerModelAlias.model;
|
|
10321
10417
|
return await handleProviderResponsesForProvider(c, {
|
|
@@ -10622,4 +10718,4 @@ server.route("/:provider/images", providerImageRoutes);
|
|
|
10622
10718
|
//#endregion
|
|
10623
10719
|
export { server };
|
|
10624
10720
|
|
|
10625
|
-
//# sourceMappingURL=server-
|
|
10721
|
+
//# sourceMappingURL=server-BRGh2SlD.js.map
|