@jeffreycao/copilot-api 2.2.14 → 2.2.16

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.
@@ -1,7 +1,7 @@
1
- import { A as isResponsesApiWebSocketEnabled, C as getClaudeTokenMultiplier, D as isAlphaSearchCodexPriorityEnabled, E as getResponsesTransportConfig, N as PATHS, O as isMessagesApiEnabled, S as getClaudeAutoModel, T as getMessageApiWebSearchModel, _ as resolveMappedModel, b as getAlphaSearchModel, d as getModelResponsesApiCompactThreshold$1, f as getReasoningEffortForModel, g as isGpt56OrAbove, h as isContextManagementEnabledForResponses, i as listEnabledProviders, k as isResponsesApiWebSearchEnabled, l as getExtraPromptForModel, m as isContextManagementEnabledForMessages, n as getRawProviderConfig, o as resolveEffectiveProviderType, p as getSmallModel, s as resolveProviderAuthType, t as getProviderConfig, u as getModelMappings, v as setModelMappings, x as getAnthropicApiKey } from "./config-BK3_YOxx.js";
2
- import { B as fetchResponsesWithLifecycle, C as compactAutoContinuePromptStarts, D as compactTextOnlyGuard, E as compactSystemPromptStarts, F as forwardCodexResponses, G as createWebSocketUrl, H as encodePoolKeyPart, I as generateTraceId, J as forwardError, K as state, L as requestContext, N as CODEX_API_BASE_URL, O as createAuthMiddleware, P as buildCodexRequestHeaders, R as resolveTraceId$1, U as isTerminalResponsesStreamChunk, V as createResponsesSafeStream, W as createPooledWebSocketStream, b as prepareInteractionHeaders, c as getUUID, d as isResponsesStream, f as parseUserIdMetadata, g as copilotHeaders, h as copilotBaseUrl, k as getConfiguredAdminApiKeys, l as isAsyncIterable, o as generateRequestIdFromPayload, p as getCopilotUsage, q as HTTPError, r as setupCodexToken, s as getRootSessionId, u as isNullish, v as copilotWebSocketHeaders, w as compactMessageSections, x as prepareMessageProxyHeaders, y as prepareForCompact, z as createResponsesHttpEventStream } from "./token-u7yV0_hw.js";
1
+ import { A as isResponsesApiWebSocketEnabled, C as getClaudeTokenMultiplier, D as isAlphaSearchCodexPriorityEnabled, E as getResponsesTransportConfig, N as PATHS, O as isMessagesApiEnabled, S as getClaudeAutoModel, T as getMessageApiWebSearchModel, _ as resolveMappedModel, b as getAlphaSearchModel, d as getModelResponsesApiCompactThreshold$1, f as getReasoningEffortForModel, g as isGpt56OrAbove, h as isContextManagementEnabledForResponses, i as listEnabledProviders, k as isResponsesApiWebSearchEnabled, l as getExtraPromptForModel, m as isContextManagementEnabledForMessages, n as getRawProviderConfig, o as resolveEffectiveProviderType, p as getSmallModel, s as resolveProviderAuthType, t as getProviderConfig, u as getModelMappings, v as setModelMappings, x as getAnthropicApiKey } from "./config-CEGVuc_4.js";
2
+ import { B as fetchResponsesWithLifecycle, C as compactAutoContinuePromptStarts, D as compactTextOnlyGuard, E as compactSystemPromptStarts, F as forwardCodexResponses, G as createWebSocketUrl, H as encodePoolKeyPart, I as generateTraceId, J as forwardError, K as state, L as requestContext, N as CODEX_API_BASE_URL, O as createAuthMiddleware, P as buildCodexRequestHeaders, R as resolveTraceId$1, U as isTerminalResponsesStreamChunk, V as createResponsesSafeStream, W as createPooledWebSocketStream, b as prepareInteractionHeaders, c as getUUID, d as isResponsesStream, f as parseUserIdMetadata, g as copilotHeaders, h as copilotBaseUrl, k as getConfiguredAdminApiKeys, l as isAsyncIterable, o as generateRequestIdFromPayload, p as getCopilotUsage, q as HTTPError, r as setupCodexToken, s as getRootSessionId, u as isNullish, v as copilotWebSocketHeaders, w as compactMessageSections, x as prepareMessageProxyHeaders, y as prepareForCompact, z as createResponsesHttpEventStream } from "./token-CzyAtvCM.js";
3
3
  import { a as isDeferredToolName, c as parseMcpToolSearchSentinel, d as shouldEnableResponsesToolSearch, i as isBridgeToolSearchName, l as resolveBridgeToolSearchName, o as listDeferredToolNames, r as formatToolSearchBridgeArguments, s as normalizeToolSearchBridgeArguments, t as BRIDGE_TOOL_SEARCH_NAME, u as selectDeferredToolsByNames } from "./tool-search-Ds1vbmGG.js";
4
- import { i as toClientModelId, r as normalizeSdkModelId, t as findEndpointModel } from "./models-LIZvOolN.js";
4
+ import { i as toClientModelId, r as normalizeSdkModelId, t as findEndpointModel } from "./models-BWz0NqC_.js";
5
5
  import consola from "consola";
6
6
  import { createHash } from "node:crypto";
7
7
  import fs, { readFileSync } from "node:fs";
@@ -4537,6 +4537,7 @@ function completePendingMessage(state, events, chunk) {
4537
4537
  if (chunk?.usage) state.pendingMessageDelta.usage = getAnthropicUsageFromOpenAIChunk(chunk);
4538
4538
  events.push(state.pendingMessageDelta, { type: "message_stop" });
4539
4539
  state.pendingMessageDelta = void 0;
4540
+ state.messageCompleted = true;
4540
4541
  }
4541
4542
  function handleFinish(choice, state, context) {
4542
4543
  const { events, chunk } = context;
@@ -4814,6 +4815,15 @@ function closeThinkingBlockIfOpen(state, events) {
4814
4815
  state.thinkingBlockOpen = false;
4815
4816
  }
4816
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
+ }
4817
4827
  //#endregion
4818
4828
  //#region src/routes/messages/responses-translation.ts
4819
4829
  const MESSAGE_TYPE = "message";
@@ -6938,35 +6948,54 @@ const streamProviderMessages = ({ c, modelConfig, payload, pricingCurrency, prov
6938
6948
  const recordUsage = createProviderMessagesUsageRecorder(payload, provider, modelConfig, pricingCurrency, usageEndpoint);
6939
6949
  return streamSSE(c, async (stream) => {
6940
6950
  let usage = {};
6951
+ let messageStopSeen = false;
6952
+ let errorSeen = false;
6941
6953
  const openRouterThinkingState = {
6942
6954
  signedThinkingBlockIndexes: /* @__PURE__ */ new Set(),
6943
6955
  thinkingBlockIndexes: /* @__PURE__ */ new Set()
6944
6956
  };
6945
- for await (const chunk of events(upstreamResponse)) {
6946
- logger$10.debug("provider.messages.raw_stream_event:", chunk.data);
6947
- const eventName = chunk.event;
6948
- if (eventName === "ping") {
6949
- await stream.writeSSE({
6950
- event: "ping",
6951
- data: "{\"type\":\"ping\"}"
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
6952
6985
  });
6953
- continue;
6954
- }
6955
- let data = chunk.data;
6956
- if (!data) continue;
6957
- if (chunk.data === "[DONE]") break;
6958
- const parsed = parseProviderStreamEvent(data);
6959
- if (parsed) {
6960
- usage = mergeAnthropicUsage(usage, parsed.usage);
6961
- data = parsed.data;
6962
6986
  }
6963
- const streamEvents = provider === "openrouter" ? normalizeOpenRouterStreamEvents(eventName, data, openRouterThinkingState) : [{
6964
- data,
6965
- event: eventName
6966
- }];
6967
- for (const streamEvent of streamEvents) await stream.writeSSE({
6968
- event: streamEvent.event,
6969
- data: streamEvent.data
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)
6970
6999
  });
6971
7000
  }
6972
7001
  recordUsage(usage);
@@ -6979,36 +7008,44 @@ const streamOpenAICompatibleProviderMessages = ({ c, modelConfig, payload, prici
6979
7008
  let usage = {};
6980
7009
  const streamState = {
6981
7010
  messageStartSent: false,
7011
+ messageCompleted: false,
6982
7012
  contentBlockIndex: 0,
6983
7013
  contentBlockOpen: false,
6984
7014
  toolCalls: {},
6985
7015
  thinkingBlockOpen: false
6986
7016
  };
6987
- for await (const chunk of events(upstreamResponse)) {
6988
- logger$10.debug("provider.messages.openai_compatible.raw_stream_event:", chunk.data);
6989
- if (chunk.event === "ping") {
6990
- await stream.writeSSE({
6991
- event: "ping",
6992
- data: "{\"type\":\"ping\"}"
6993
- });
6994
- continue;
6995
- }
6996
- if (!chunk.data || chunk.data === "[DONE]") {
6997
- if (chunk.data === "[DONE]") break;
6998
- continue;
6999
- }
7000
- const parsed = parseOpenAICompatibleStreamChunk(chunk.data);
7001
- if (!parsed) continue;
7002
- if (parsed.usage) usage = normalizeOpenAIUsage(parsed.usage);
7003
- const events = translateChunkToAnthropicEvents(parsed, streamState);
7004
- for (const event of events) {
7005
- const eventData = JSON.stringify(event);
7006
- debugLazy(logger$10, () => ["provider.messages.openai_compatible.translated_event:", eventData]);
7007
- await stream.writeSSE({
7008
- event: event.type,
7009
- data: eventData
7010
- });
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
+ }
7011
7043
  }
7044
+ } catch (error) {
7045
+ logger$10.warn("provider.messages.openai_compatible.stream_interrupted:", {
7046
+ error,
7047
+ provider
7048
+ });
7012
7049
  }
7013
7050
  for (const event of flushPendingAnthropicStreamEvents(streamState)) {
7014
7051
  const eventData = JSON.stringify(event);
@@ -7018,6 +7055,14 @@ const streamOpenAICompatibleProviderMessages = ({ c, modelConfig, payload, prici
7018
7055
  data: eventData
7019
7056
  });
7020
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
+ }
7021
7066
  recordUsage(usage);
7022
7067
  });
7023
7068
  };
@@ -7094,14 +7139,17 @@ const parseProviderStreamEvent = (data) => {
7094
7139
  if (parsed.type === "message_start") return {
7095
7140
  data: JSON.stringify(parsed),
7096
7141
  model: parsed.message.model,
7142
+ type: parsed.type,
7097
7143
  usage: normalizeAnthropicUsage(parsed.message.usage)
7098
7144
  };
7099
7145
  if (parsed.type === "message_delta") return {
7100
7146
  data: JSON.stringify(parsed),
7147
+ type: parsed.type,
7101
7148
  usage: normalizeAnthropicUsage(parsed.usage)
7102
7149
  };
7103
7150
  return {
7104
7151
  data: JSON.stringify(parsed),
7152
+ type: parsed.type,
7105
7153
  usage: {}
7106
7154
  };
7107
7155
  } catch (error) {
@@ -7305,29 +7353,34 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
7305
7353
  let usage = {};
7306
7354
  const streamState = {
7307
7355
  messageStartSent: false,
7356
+ messageCompleted: false,
7308
7357
  contentBlockIndex: 0,
7309
7358
  contentBlockOpen: false,
7310
7359
  toolCalls: {},
7311
7360
  thinkingBlockOpen: false
7312
7361
  };
7313
- for await (const rawEvent of response) {
7314
- debugJson(logger, "Copilot raw stream event:", rawEvent);
7315
- if (rawEvent.data === "[DONE]") break;
7316
- if (!rawEvent.data) continue;
7317
- const chunk = JSON.parse(rawEvent.data);
7318
- if (chunk.usage || chunk.copilot_usage) usage = {
7319
- ...normalizeOpenAIUsage(chunk.usage),
7320
- total_nano_aiu: normalizeOptionalToken(chunk.copilot_usage?.total_nano_aiu)
7321
- };
7322
- const events = translateChunkToAnthropicEvents(chunk, streamState);
7323
- for (const event of events) {
7324
- const eventData = JSON.stringify(event);
7325
- debugLazy(logger, () => ["Translated Anthropic event:", eventData]);
7326
- await stream.writeSSE({
7327
- event: event.type,
7328
- data: eventData
7329
- });
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
+ }
7330
7381
  }
7382
+ } catch (error) {
7383
+ logger.warn("Chat completions stream interrupted:", error);
7331
7384
  }
7332
7385
  for (const event of flushPendingAnthropicStreamEvents(streamState)) {
7333
7386
  const eventData = JSON.stringify(event);
@@ -7337,6 +7390,14 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
7337
7390
  data: eventData
7338
7391
  });
7339
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
+ }
7340
7401
  recordUsage(usage);
7341
7402
  });
7342
7403
  };
@@ -7436,24 +7497,40 @@ const handleWithMessagesApi = async (c, anthropicPayload, options) => {
7436
7497
  logger.debug("Streaming response from Copilot (Messages API)");
7437
7498
  return streamSSE(c, async (stream) => {
7438
7499
  let usage = {};
7439
- for await (const event of response) {
7440
- const eventName = event.event;
7441
- const data = event.data ?? "";
7442
- if (data === "[DONE]") break;
7443
- if (!data) continue;
7444
- debugLazy(logger, () => ["Messages raw stream event:", data]);
7445
- const parsedEvent = parseAnthropicStreamEvent(data);
7446
- if (parsedEvent?.type === "message_start") usage = mergeAnthropicUsage(usage, {
7447
- ...normalizeAnthropicUsage(parsedEvent.message.usage),
7448
- ...normalizeCopilotUsage(parsedEvent.message.copilot_usage)
7449
- });
7450
- else if (parsedEvent?.type === "message_delta") usage = mergeAnthropicUsage(usage, {
7451
- ...normalizeAnthropicUsage(parsedEvent.usage),
7452
- ...normalizeCopilotUsage(parsedEvent.copilot_usage)
7453
- });
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();
7454
7531
  await stream.writeSSE({
7455
- event: eventName,
7456
- data
7532
+ event: errorEvent.type,
7533
+ data: JSON.stringify(errorEvent)
7457
7534
  });
7458
7535
  }
7459
7536
  recordUsage(usage);
@@ -9418,14 +9495,12 @@ async function* translateMessagesStream(chunks, context) {
9418
9495
  if (state.messageStopped) break;
9419
9496
  }
9420
9497
  if (!state.initialized) throw new ResponsesMessagesTranslationError("Messages API returned an empty stream", 502);
9421
- if (!state.messageStopped) {
9422
- for (const translated of closeAllBlocks(state)) yield translated;
9423
- for (const translated of finishCompaction(state)) yield translated;
9424
- state.messageStopped = true;
9425
- yield createTerminalEvent(state);
9426
- }
9498
+ if (!state.messageStopped) throw new ResponsesMessagesTranslationError("Messages stream ended without a message_stop event", 502);
9427
9499
  } catch (error) {
9428
- if (!state.initialized) throw error;
9500
+ if (!state.initialized) {
9501
+ yield createLifecycleEvent(state, "response.created");
9502
+ yield createLifecycleEvent(state, "response.in_progress");
9503
+ }
9429
9504
  yield createErrorEvent(state, error);
9430
9505
  yield createFailedEvent(state, error);
9431
9506
  }
@@ -10566,20 +10641,6 @@ tokenUsageRoute.get("/events", async (c) => {
10566
10641
  return c.json(eventsPage);
10567
10642
  });
10568
10643
  //#endregion
10569
- //#region src/routes/token/route.ts
10570
- const tokenRoute = new Hono();
10571
- tokenRoute.get("/", (c) => {
10572
- try {
10573
- return c.json({ token: state.copilotToken });
10574
- } catch (error) {
10575
- console.error("Error fetching token:", error);
10576
- return c.json({
10577
- error: "Failed to fetch token",
10578
- token: null
10579
- }, 500);
10580
- }
10581
- });
10582
- //#endregion
10583
10644
  //#region src/routes/usage/route.ts
10584
10645
  const usageRoute = new Hono();
10585
10646
  usageRoute.get("/", async (c) => {
@@ -10623,7 +10684,6 @@ server.route("/models", modelRoutes);
10623
10684
  server.route("/embeddings", embeddingRoutes);
10624
10685
  server.route("/usage", usageRoute);
10625
10686
  server.route("/token-usage", tokenUsageRoute);
10626
- server.route("/token", tokenRoute);
10627
10687
  server.route("/responses", responsesRoutes);
10628
10688
  server.route("/alpha/search", alphaSearchRoutes);
10629
10689
  server.route("/images", imageRoutes);
@@ -10646,4 +10706,4 @@ server.route("/:provider/images", providerImageRoutes);
10646
10706
  //#endregion
10647
10707
  export { server };
10648
10708
 
10649
- //# sourceMappingURL=server-B3NoF9oG.js.map
10709
+ //# sourceMappingURL=server-B7mWm6lt.js.map