@jeffreycao/copilot-api 1.2.1 → 1.2.3
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/README.md +21 -5
- package/dist/{config-Zv1NG6Kd.js → config-BD6sOCuT.js} +134 -15
- package/dist/config-BD6sOCuT.js.map +1 -0
- package/dist/main.js +4 -2
- package/dist/main.js.map +1 -1
- package/dist/{server-BSFMHogd.js → server-IvwXytB7.js} +222 -45
- package/dist/server-IvwXytB7.js.map +1 -0
- package/package.json +1 -1
- package/dist/config-Zv1NG6Kd.js.map +0 -1
- package/dist/server-BSFMHogd.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HTTPError, PATHS, cacheModels, copilotBaseUrl, copilotHeaders, forwardError, getConfig, getCopilotUsage, getExtraPromptForModel, getReasoningEffortForModel, getSmallModel, isNullish, shouldCompactUseSmallModel, sleep, state } from "./config-
|
|
1
|
+
import { HTTPError, PATHS, cacheModels, copilotBaseUrl, copilotHeaders, forwardError, generateRequestIdFromPayload, getConfig, getCopilotUsage, getExtraPromptForModel, getReasoningEffortForModel, getRootSessionId, getSmallModel, getUUID, isNullish, isResponsesApiContextManagementModel, shouldCompactUseSmallModel, sleep, state } from "./config-BD6sOCuT.js";
|
|
2
2
|
import consola from "consola";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import fs, { readFileSync } from "node:fs";
|
|
@@ -426,9 +426,14 @@ const createChatCompletions = async (payload, options) => {
|
|
|
426
426
|
if (lastMessage) isAgentCall = ["assistant", "tool"].includes(lastMessage.role);
|
|
427
427
|
}
|
|
428
428
|
const headers = {
|
|
429
|
-
...copilotHeaders(state, enableVision),
|
|
430
|
-
"
|
|
429
|
+
...copilotHeaders(state, options.requestId, enableVision),
|
|
430
|
+
"x-initiator": isAgentCall ? "agent" : "user"
|
|
431
431
|
};
|
|
432
|
+
if (options.subagentMarker) {
|
|
433
|
+
headers["x-initiator"] = "agent";
|
|
434
|
+
headers["x-interaction-type"] = "conversation-subagent";
|
|
435
|
+
}
|
|
436
|
+
if (options.sessionId) headers["x-interaction-id"] = options.sessionId;
|
|
432
437
|
const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
|
|
433
438
|
method: "POST",
|
|
434
439
|
headers,
|
|
@@ -466,7 +471,14 @@ async function handleCompletion$1(c) {
|
|
|
466
471
|
};
|
|
467
472
|
logger$3.debug("Set max_tokens to:", JSON.stringify(payload.max_tokens));
|
|
468
473
|
}
|
|
469
|
-
const
|
|
474
|
+
const requestId = generateRequestIdFromPayload(payload);
|
|
475
|
+
logger$3.debug("Generated request ID:", requestId);
|
|
476
|
+
const sessionId = getUUID(requestId);
|
|
477
|
+
logger$3.debug("Extracted session ID:", sessionId);
|
|
478
|
+
const response = await createChatCompletions(payload, {
|
|
479
|
+
requestId,
|
|
480
|
+
sessionId
|
|
481
|
+
});
|
|
470
482
|
if (isNonStreaming$1(response)) {
|
|
471
483
|
logger$3.debug("Non-streaming response:", JSON.stringify(response));
|
|
472
484
|
return c.json(response);
|
|
@@ -805,12 +817,17 @@ async function handleCountTokens(c) {
|
|
|
805
817
|
|
|
806
818
|
//#endregion
|
|
807
819
|
//#region src/services/copilot/create-responses.ts
|
|
808
|
-
const createResponses = async (payload, { vision, initiator }) => {
|
|
820
|
+
const createResponses = async (payload, { vision, initiator, subagentMarker, requestId, sessionId }) => {
|
|
809
821
|
if (!state.copilotToken) throw new Error("Copilot token not found");
|
|
810
822
|
const headers = {
|
|
811
|
-
...copilotHeaders(state, vision),
|
|
812
|
-
"
|
|
823
|
+
...copilotHeaders(state, requestId, vision),
|
|
824
|
+
"x-initiator": initiator
|
|
813
825
|
};
|
|
826
|
+
if (subagentMarker) {
|
|
827
|
+
headers["x-initiator"] = "agent";
|
|
828
|
+
headers["x-interaction-type"] = "conversation-subagent";
|
|
829
|
+
}
|
|
830
|
+
if (sessionId) headers["x-interaction-id"] = sessionId;
|
|
814
831
|
payload.service_tier = null;
|
|
815
832
|
const response = await fetch(`${copilotBaseUrl(state)}/responses`, {
|
|
816
833
|
method: "POST",
|
|
@@ -828,11 +845,13 @@ const createResponses = async (payload, { vision, initiator }) => {
|
|
|
828
845
|
//#endregion
|
|
829
846
|
//#region src/routes/messages/responses-translation.ts
|
|
830
847
|
const MESSAGE_TYPE = "message";
|
|
831
|
-
const
|
|
848
|
+
const COMPACTION_SIGNATURE_PREFIX = "cm1#";
|
|
849
|
+
const COMPACTION_SIGNATURE_SEPARATOR = "@";
|
|
832
850
|
const THINKING_TEXT$1 = "Thinking...";
|
|
833
851
|
const translateAnthropicMessagesToResponsesPayload = (payload) => {
|
|
834
852
|
const input = [];
|
|
835
|
-
|
|
853
|
+
const applyPhase = shouldApplyPhase(payload.model);
|
|
854
|
+
for (const message of payload.messages) input.push(...translateMessage(message, payload.model, applyPhase));
|
|
836
855
|
const translatedTools = convertAnthropicTools(payload.tools);
|
|
837
856
|
const toolChoice = convertAnthropicToolChoice(payload.tool_choice);
|
|
838
857
|
const { safetyIdentifier, promptCacheKey } = parseUserId(payload.metadata?.user_id);
|
|
@@ -858,9 +877,26 @@ const translateAnthropicMessagesToResponsesPayload = (payload) => {
|
|
|
858
877
|
include: ["reasoning.encrypted_content"]
|
|
859
878
|
};
|
|
860
879
|
};
|
|
861
|
-
const
|
|
880
|
+
const encodeCompactionCarrierSignature = (compaction) => {
|
|
881
|
+
return `${COMPACTION_SIGNATURE_PREFIX}${compaction.encrypted_content}${COMPACTION_SIGNATURE_SEPARATOR}${compaction.id}`;
|
|
882
|
+
};
|
|
883
|
+
const decodeCompactionCarrierSignature = (signature) => {
|
|
884
|
+
if (signature.startsWith(COMPACTION_SIGNATURE_PREFIX)) {
|
|
885
|
+
const raw = signature.slice(4);
|
|
886
|
+
const separatorIndex = raw.indexOf(COMPACTION_SIGNATURE_SEPARATOR);
|
|
887
|
+
if (separatorIndex <= 0 || separatorIndex === raw.length - 1) return;
|
|
888
|
+
const encrypted_content = raw.slice(0, separatorIndex);
|
|
889
|
+
const id = raw.slice(separatorIndex + 1);
|
|
890
|
+
if (!encrypted_content) return;
|
|
891
|
+
return {
|
|
892
|
+
id,
|
|
893
|
+
encrypted_content
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
const translateMessage = (message, model, applyPhase) => {
|
|
862
898
|
if (message.role === "user") return translateUserMessage(message);
|
|
863
|
-
return translateAssistantMessage(message, model);
|
|
899
|
+
return translateAssistantMessage(message, model, applyPhase);
|
|
864
900
|
};
|
|
865
901
|
const translateUserMessage = (message) => {
|
|
866
902
|
if (typeof message.content === "string") return [createMessage("user", message.content)];
|
|
@@ -879,8 +915,8 @@ const translateUserMessage = (message) => {
|
|
|
879
915
|
flushPendingContent(pendingContent, items, { role: "user" });
|
|
880
916
|
return items;
|
|
881
917
|
};
|
|
882
|
-
const translateAssistantMessage = (message, model) => {
|
|
883
|
-
const assistantPhase = resolveAssistantPhase(model, message.content);
|
|
918
|
+
const translateAssistantMessage = (message, model, applyPhase) => {
|
|
919
|
+
const assistantPhase = resolveAssistantPhase(model, message.content, applyPhase);
|
|
884
920
|
if (typeof message.content === "string") return [createMessage("assistant", message.content, assistantPhase)];
|
|
885
921
|
if (!Array.isArray(message.content)) return [];
|
|
886
922
|
const items = [];
|
|
@@ -894,13 +930,24 @@ const translateAssistantMessage = (message, model) => {
|
|
|
894
930
|
items.push(createFunctionToolCall(block));
|
|
895
931
|
continue;
|
|
896
932
|
}
|
|
897
|
-
if (block.type === "thinking" && block.signature
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
933
|
+
if (block.type === "thinking" && block.signature) {
|
|
934
|
+
const compactionContent = createCompactionContent(block);
|
|
935
|
+
if (compactionContent) {
|
|
936
|
+
flushPendingContent(pendingContent, items, {
|
|
937
|
+
role: "assistant",
|
|
938
|
+
phase: assistantPhase
|
|
939
|
+
});
|
|
940
|
+
items.push(compactionContent);
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
if (block.signature.includes("@")) {
|
|
944
|
+
flushPendingContent(pendingContent, items, {
|
|
945
|
+
role: "assistant",
|
|
946
|
+
phase: assistantPhase
|
|
947
|
+
});
|
|
948
|
+
items.push(createReasoningContent(block));
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
904
951
|
}
|
|
905
952
|
const converted = translateAssistantContentBlock(block);
|
|
906
953
|
if (converted) pendingContent.push(converted);
|
|
@@ -936,14 +983,16 @@ const createMessage = (role, content, phase) => ({
|
|
|
936
983
|
content,
|
|
937
984
|
...role === "assistant" && phase ? { phase } : {}
|
|
938
985
|
});
|
|
939
|
-
const resolveAssistantPhase = (
|
|
940
|
-
if (!
|
|
986
|
+
const resolveAssistantPhase = (_model, content, applyPhase) => {
|
|
987
|
+
if (!applyPhase) return;
|
|
941
988
|
if (typeof content === "string") return "final_answer";
|
|
942
989
|
if (!Array.isArray(content)) return;
|
|
943
990
|
if (!content.some((block) => block.type === "text")) return;
|
|
944
991
|
return content.some((block) => block.type === "tool_use") ? "commentary" : "final_answer";
|
|
945
992
|
};
|
|
946
|
-
const
|
|
993
|
+
const shouldApplyPhase = (model) => {
|
|
994
|
+
return getExtraPromptForModel(model).includes("## Intermediary updates");
|
|
995
|
+
};
|
|
947
996
|
const createTextContent = (text) => ({
|
|
948
997
|
type: "input_text",
|
|
949
998
|
text
|
|
@@ -958,9 +1007,7 @@ const createImageContent = (block) => ({
|
|
|
958
1007
|
detail: "auto"
|
|
959
1008
|
});
|
|
960
1009
|
const createReasoningContent = (block) => {
|
|
961
|
-
const
|
|
962
|
-
const signature = array[0];
|
|
963
|
-
const id = array[1];
|
|
1010
|
+
const { encryptedContent, id } = parseReasoningSignature(block.signature);
|
|
964
1011
|
const thinking = block.thinking === THINKING_TEXT$1 ? "" : block.thinking;
|
|
965
1012
|
return {
|
|
966
1013
|
id,
|
|
@@ -969,7 +1016,27 @@ const createReasoningContent = (block) => {
|
|
|
969
1016
|
type: "summary_text",
|
|
970
1017
|
text: thinking
|
|
971
1018
|
}] : [],
|
|
972
|
-
encrypted_content:
|
|
1019
|
+
encrypted_content: encryptedContent
|
|
1020
|
+
};
|
|
1021
|
+
};
|
|
1022
|
+
const createCompactionContent = (block) => {
|
|
1023
|
+
const compaction = decodeCompactionCarrierSignature(block.signature);
|
|
1024
|
+
if (!compaction) return;
|
|
1025
|
+
return {
|
|
1026
|
+
id: compaction.id,
|
|
1027
|
+
type: "compaction",
|
|
1028
|
+
encrypted_content: compaction.encrypted_content
|
|
1029
|
+
};
|
|
1030
|
+
};
|
|
1031
|
+
const parseReasoningSignature = (signature) => {
|
|
1032
|
+
const splitIndex = signature.lastIndexOf("@");
|
|
1033
|
+
if (splitIndex <= 0 || splitIndex === signature.length - 1) return {
|
|
1034
|
+
encryptedContent: signature,
|
|
1035
|
+
id: ""
|
|
1036
|
+
};
|
|
1037
|
+
return {
|
|
1038
|
+
encryptedContent: signature.slice(0, splitIndex),
|
|
1039
|
+
id: signature.slice(splitIndex + 1)
|
|
973
1040
|
};
|
|
974
1041
|
};
|
|
975
1042
|
const createFunctionToolCall = (block) => ({
|
|
@@ -1060,6 +1127,11 @@ const mapOutputToAnthropicContent = (output) => {
|
|
|
1060
1127
|
});
|
|
1061
1128
|
break;
|
|
1062
1129
|
}
|
|
1130
|
+
case "compaction": {
|
|
1131
|
+
const compactionBlock = createCompactionThinkingBlock(item);
|
|
1132
|
+
if (compactionBlock) contentBlocks.push(compactionBlock);
|
|
1133
|
+
break;
|
|
1134
|
+
}
|
|
1063
1135
|
default: {
|
|
1064
1136
|
const combinedText = combineMessageTextContent(item.content);
|
|
1065
1137
|
if (combinedText.length > 0) contentBlocks.push({
|
|
@@ -1117,6 +1189,17 @@ const createToolUseContentBlock = (call) => {
|
|
|
1117
1189
|
input
|
|
1118
1190
|
};
|
|
1119
1191
|
};
|
|
1192
|
+
const createCompactionThinkingBlock = (item) => {
|
|
1193
|
+
if (!item.id || !item.encrypted_content) return null;
|
|
1194
|
+
return {
|
|
1195
|
+
type: "thinking",
|
|
1196
|
+
thinking: THINKING_TEXT$1,
|
|
1197
|
+
signature: encodeCompactionCarrierSignature({
|
|
1198
|
+
id: item.id,
|
|
1199
|
+
encrypted_content: item.encrypted_content
|
|
1200
|
+
})
|
|
1201
|
+
};
|
|
1202
|
+
};
|
|
1120
1203
|
const parseFunctionCallArguments = (rawArguments) => {
|
|
1121
1204
|
if (typeof rawArguments !== "string" || rawArguments.trim().length === 0) return {};
|
|
1122
1205
|
try {
|
|
@@ -1279,8 +1362,34 @@ const handleOutputItemAdded$1 = (rawEvent, state$1) => {
|
|
|
1279
1362
|
const handleOutputItemDone$1 = (rawEvent, state$1) => {
|
|
1280
1363
|
const events$1 = new Array();
|
|
1281
1364
|
const item = rawEvent.item;
|
|
1282
|
-
|
|
1365
|
+
const itemType = item.type;
|
|
1283
1366
|
const outputIndex = rawEvent.output_index;
|
|
1367
|
+
if (itemType === "compaction") {
|
|
1368
|
+
if (!item.id || !item.encrypted_content) return events$1;
|
|
1369
|
+
const blockIndex$1 = openThinkingBlockIfNeeded(state$1, outputIndex, events$1);
|
|
1370
|
+
if (!state$1.blockHasDelta.has(blockIndex$1)) events$1.push({
|
|
1371
|
+
type: "content_block_delta",
|
|
1372
|
+
index: blockIndex$1,
|
|
1373
|
+
delta: {
|
|
1374
|
+
type: "thinking_delta",
|
|
1375
|
+
thinking: THINKING_TEXT$1
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
events$1.push({
|
|
1379
|
+
type: "content_block_delta",
|
|
1380
|
+
index: blockIndex$1,
|
|
1381
|
+
delta: {
|
|
1382
|
+
type: "signature_delta",
|
|
1383
|
+
signature: encodeCompactionCarrierSignature({
|
|
1384
|
+
id: item.id,
|
|
1385
|
+
encrypted_content: item.encrypted_content
|
|
1386
|
+
})
|
|
1387
|
+
}
|
|
1388
|
+
});
|
|
1389
|
+
state$1.blockHasDelta.add(blockIndex$1);
|
|
1390
|
+
return events$1;
|
|
1391
|
+
}
|
|
1392
|
+
if (itemType !== "reasoning") return events$1;
|
|
1284
1393
|
const blockIndex = openThinkingBlockIfNeeded(state$1, outputIndex, events$1);
|
|
1285
1394
|
const signature = (item.encrypted_content ?? "") + "@" + item.id;
|
|
1286
1395
|
if (signature) {
|
|
@@ -1618,6 +1727,31 @@ const hasAgentInitiator = (payload) => {
|
|
|
1618
1727
|
const hasVisionInput = (payload) => {
|
|
1619
1728
|
return getPayloadItems(payload).some((item) => containsVisionContent(item));
|
|
1620
1729
|
};
|
|
1730
|
+
const resolveResponsesCompactThreshold = (maxPromptTokens) => {
|
|
1731
|
+
if (typeof maxPromptTokens === "number" && maxPromptTokens > 0) return Math.floor(maxPromptTokens * .9);
|
|
1732
|
+
return 5e4;
|
|
1733
|
+
};
|
|
1734
|
+
const createCompactionContextManagement = (compactThreshold) => [{
|
|
1735
|
+
type: "compaction",
|
|
1736
|
+
compact_threshold: compactThreshold
|
|
1737
|
+
}];
|
|
1738
|
+
const applyResponsesApiContextManagement = (payload, maxPromptTokens) => {
|
|
1739
|
+
if (payload.context_management !== void 0) return;
|
|
1740
|
+
if (!isResponsesApiContextManagementModel(payload.model)) return;
|
|
1741
|
+
payload.context_management = createCompactionContextManagement(resolveResponsesCompactThreshold(maxPromptTokens));
|
|
1742
|
+
};
|
|
1743
|
+
const compactInputByLatestCompaction = (payload) => {
|
|
1744
|
+
if (!Array.isArray(payload.input) || payload.input.length === 0) return;
|
|
1745
|
+
const latestCompactionMessageIndex = getLatestCompactionMessageIndex(payload.input);
|
|
1746
|
+
if (latestCompactionMessageIndex === void 0) return;
|
|
1747
|
+
payload.input = payload.input.slice(latestCompactionMessageIndex);
|
|
1748
|
+
};
|
|
1749
|
+
const getLatestCompactionMessageIndex = (input) => {
|
|
1750
|
+
for (let index = input.length - 1; index >= 0; index -= 1) if (isCompactionInputItem(input[index])) return index;
|
|
1751
|
+
};
|
|
1752
|
+
const isCompactionInputItem = (value) => {
|
|
1753
|
+
return "type" in value && typeof value.type === "string" && value.type === "compaction";
|
|
1754
|
+
};
|
|
1621
1755
|
const getPayloadItems = (payload) => {
|
|
1622
1756
|
const result = [];
|
|
1623
1757
|
const { input } = payload;
|
|
@@ -1659,11 +1793,15 @@ const createMessages = async (payload, anthropicBetaHeader, options) => {
|
|
|
1659
1793
|
let isInitiateRequest = false;
|
|
1660
1794
|
const lastMessage = payload.messages.at(-1);
|
|
1661
1795
|
if (lastMessage?.role === "user") isInitiateRequest = Array.isArray(lastMessage.content) ? lastMessage.content.some((block) => block.type !== "tool_result") : true;
|
|
1662
|
-
const initiator = options?.initiator ?? (isInitiateRequest ? "user" : "agent");
|
|
1663
1796
|
const headers = {
|
|
1664
|
-
...copilotHeaders(state, enableVision),
|
|
1665
|
-
"
|
|
1797
|
+
...copilotHeaders(state, options.requestId, enableVision),
|
|
1798
|
+
"x-initiator": isInitiateRequest ? "user" : "agent"
|
|
1666
1799
|
};
|
|
1800
|
+
if (options.subagentMarker) {
|
|
1801
|
+
headers["x-initiator"] = "agent";
|
|
1802
|
+
headers["x-interaction-type"] = "conversation-subagent";
|
|
1803
|
+
}
|
|
1804
|
+
if (options.sessionId) headers["x-interaction-id"] = options.sessionId;
|
|
1667
1805
|
const anthropicBeta = buildAnthropicBetaHeader(anthropicBetaHeader, payload.thinking);
|
|
1668
1806
|
if (anthropicBeta) headers["anthropic-beta"] = anthropicBeta;
|
|
1669
1807
|
const response = await fetch(`${copilotBaseUrl(state)}/v1/messages`, {
|
|
@@ -1983,8 +2121,9 @@ async function handleCompletion(c) {
|
|
|
1983
2121
|
const anthropicPayload = await c.req.json();
|
|
1984
2122
|
logger$2.debug("Anthropic request payload:", JSON.stringify(anthropicPayload));
|
|
1985
2123
|
const subagentMarker = parseSubagentMarkerFromFirstUser(anthropicPayload);
|
|
1986
|
-
const initiatorOverride = subagentMarker ? "agent" : void 0;
|
|
1987
2124
|
if (subagentMarker) logger$2.debug("Detected Subagent marker:", JSON.stringify(subagentMarker));
|
|
2125
|
+
const sessionId = getRootSessionId(anthropicPayload, c);
|
|
2126
|
+
logger$2.debug("Extracted session ID:", sessionId);
|
|
1988
2127
|
const isCompact = isCompactRequest(anthropicPayload);
|
|
1989
2128
|
const anthropicBeta = c.req.header("anthropic-beta");
|
|
1990
2129
|
logger$2.debug("Anthropic Beta header:", anthropicBeta);
|
|
@@ -1994,22 +2133,40 @@ async function handleCompletion(c) {
|
|
|
1994
2133
|
logger$2.debug("Is compact request:", isCompact);
|
|
1995
2134
|
if (shouldCompactUseSmallModel()) anthropicPayload.model = getSmallModel();
|
|
1996
2135
|
} else mergeToolResultForClaude(anthropicPayload);
|
|
2136
|
+
const requestId = generateRequestIdFromPayload(anthropicPayload, sessionId);
|
|
2137
|
+
logger$2.debug("Generated request ID:", requestId);
|
|
1997
2138
|
if (state.manualApprove) await awaitApproval();
|
|
1998
2139
|
const selectedModel = state.models?.data.find((m) => m.id === anthropicPayload.model);
|
|
1999
2140
|
if (shouldUseMessagesApi(selectedModel)) return await handleWithMessagesApi(c, anthropicPayload, {
|
|
2000
2141
|
anthropicBetaHeader: anthropicBeta,
|
|
2001
|
-
|
|
2002
|
-
selectedModel
|
|
2142
|
+
subagentMarker,
|
|
2143
|
+
selectedModel,
|
|
2144
|
+
requestId,
|
|
2145
|
+
sessionId
|
|
2146
|
+
});
|
|
2147
|
+
if (shouldUseResponsesApi(selectedModel)) return await handleWithResponsesApi(c, anthropicPayload, {
|
|
2148
|
+
subagentMarker,
|
|
2149
|
+
selectedModel,
|
|
2150
|
+
requestId,
|
|
2151
|
+
sessionId
|
|
2152
|
+
});
|
|
2153
|
+
return await handleWithChatCompletions(c, anthropicPayload, {
|
|
2154
|
+
subagentMarker,
|
|
2155
|
+
requestId,
|
|
2156
|
+
sessionId
|
|
2003
2157
|
});
|
|
2004
|
-
if (shouldUseResponsesApi(selectedModel)) return await handleWithResponsesApi(c, anthropicPayload, initiatorOverride);
|
|
2005
|
-
return await handleWithChatCompletions(c, anthropicPayload, initiatorOverride);
|
|
2006
2158
|
}
|
|
2007
2159
|
const RESPONSES_ENDPOINT$1 = "/responses";
|
|
2008
2160
|
const MESSAGES_ENDPOINT = "/v1/messages";
|
|
2009
|
-
const handleWithChatCompletions = async (c, anthropicPayload,
|
|
2161
|
+
const handleWithChatCompletions = async (c, anthropicPayload, options) => {
|
|
2162
|
+
const { subagentMarker, requestId, sessionId } = options;
|
|
2010
2163
|
const openAIPayload = translateToOpenAI(anthropicPayload);
|
|
2011
2164
|
logger$2.debug("Translated OpenAI request payload:", JSON.stringify(openAIPayload));
|
|
2012
|
-
const response = await createChatCompletions(openAIPayload, {
|
|
2165
|
+
const response = await createChatCompletions(openAIPayload, {
|
|
2166
|
+
subagentMarker,
|
|
2167
|
+
requestId,
|
|
2168
|
+
sessionId
|
|
2169
|
+
});
|
|
2013
2170
|
if (isNonStreaming(response)) {
|
|
2014
2171
|
logger$2.debug("Non-streaming response from Copilot:", JSON.stringify(response));
|
|
2015
2172
|
const anthropicResponse = translateToAnthropic(response);
|
|
@@ -2041,13 +2198,19 @@ const handleWithChatCompletions = async (c, anthropicPayload, initiatorOverride)
|
|
|
2041
2198
|
}
|
|
2042
2199
|
});
|
|
2043
2200
|
};
|
|
2044
|
-
const handleWithResponsesApi = async (c, anthropicPayload,
|
|
2201
|
+
const handleWithResponsesApi = async (c, anthropicPayload, options) => {
|
|
2202
|
+
const { subagentMarker, selectedModel, requestId, sessionId } = options;
|
|
2045
2203
|
const responsesPayload = translateAnthropicMessagesToResponsesPayload(anthropicPayload);
|
|
2204
|
+
applyResponsesApiContextManagement(responsesPayload, selectedModel?.capabilities.limits.max_prompt_tokens);
|
|
2205
|
+
compactInputByLatestCompaction(responsesPayload);
|
|
2046
2206
|
logger$2.debug("Translated Responses payload:", JSON.stringify(responsesPayload));
|
|
2047
2207
|
const { vision, initiator } = getResponsesRequestOptions(responsesPayload);
|
|
2048
2208
|
const response = await createResponses(responsesPayload, {
|
|
2049
2209
|
vision,
|
|
2050
|
-
initiator
|
|
2210
|
+
initiator,
|
|
2211
|
+
subagentMarker,
|
|
2212
|
+
requestId,
|
|
2213
|
+
sessionId
|
|
2051
2214
|
});
|
|
2052
2215
|
if (responsesPayload.stream && isAsyncIterable$1(response)) {
|
|
2053
2216
|
logger$2.debug("Streaming response from Copilot (Responses API)");
|
|
@@ -2094,7 +2257,7 @@ const handleWithResponsesApi = async (c, anthropicPayload, initiatorOverride) =>
|
|
|
2094
2257
|
return c.json(anthropicResponse);
|
|
2095
2258
|
};
|
|
2096
2259
|
const handleWithMessagesApi = async (c, anthropicPayload, options) => {
|
|
2097
|
-
const { anthropicBetaHeader,
|
|
2260
|
+
const { anthropicBetaHeader, subagentMarker, selectedModel, requestId, sessionId } = options;
|
|
2098
2261
|
for (const msg of anthropicPayload.messages) if (msg.role === "assistant" && Array.isArray(msg.content)) msg.content = msg.content.filter((block) => {
|
|
2099
2262
|
if (block.type !== "thinking") return true;
|
|
2100
2263
|
return block.thinking && block.thinking !== "Thinking..." && block.signature && !block.signature.includes("@");
|
|
@@ -2104,7 +2267,11 @@ const handleWithMessagesApi = async (c, anthropicPayload, options) => {
|
|
|
2104
2267
|
anthropicPayload.output_config = { effort: getAnthropicEffortForModel(anthropicPayload.model) };
|
|
2105
2268
|
}
|
|
2106
2269
|
logger$2.debug("Translated Messages payload:", JSON.stringify(anthropicPayload));
|
|
2107
|
-
const response = await createMessages(anthropicPayload, anthropicBetaHeader, {
|
|
2270
|
+
const response = await createMessages(anthropicPayload, anthropicBetaHeader, {
|
|
2271
|
+
subagentMarker,
|
|
2272
|
+
requestId,
|
|
2273
|
+
sessionId
|
|
2274
|
+
});
|
|
2108
2275
|
if (isAsyncIterable$1(response)) {
|
|
2109
2276
|
logger$2.debug("Streaming response from Copilot (Messages API)");
|
|
2110
2277
|
return streamSSE(c, async (stream) => {
|
|
@@ -2275,17 +2442,27 @@ const handleResponses = async (c) => {
|
|
|
2275
2442
|
await checkRateLimit(state);
|
|
2276
2443
|
const payload = await c.req.json();
|
|
2277
2444
|
logger$1.debug("Responses request payload:", JSON.stringify(payload));
|
|
2445
|
+
const requestId = generateRequestIdFromPayload({ messages: payload.input });
|
|
2446
|
+
logger$1.debug("Generated request ID:", requestId);
|
|
2447
|
+
const sessionId = getUUID(requestId);
|
|
2448
|
+
logger$1.debug("Extracted session ID:", sessionId);
|
|
2278
2449
|
useFunctionApplyPatch(payload);
|
|
2279
2450
|
removeWebSearchTool(payload);
|
|
2280
|
-
|
|
2451
|
+
compactInputByLatestCompaction(payload);
|
|
2452
|
+
const selectedModel = state.models?.data.find((model) => model.id === payload.model);
|
|
2453
|
+
if (!(selectedModel?.supported_endpoints?.includes(RESPONSES_ENDPOINT) ?? false)) return c.json({ error: {
|
|
2281
2454
|
message: "This model does not support the responses endpoint. Please choose a different model.",
|
|
2282
2455
|
type: "invalid_request_error"
|
|
2283
2456
|
} }, 400);
|
|
2457
|
+
applyResponsesApiContextManagement(payload, selectedModel?.capabilities.limits.max_prompt_tokens);
|
|
2458
|
+
logger$1.debug("Translated Responses payload:", JSON.stringify(payload));
|
|
2284
2459
|
const { vision, initiator } = getResponsesRequestOptions(payload);
|
|
2285
2460
|
if (state.manualApprove) await awaitApproval();
|
|
2286
2461
|
const response = await createResponses(payload, {
|
|
2287
2462
|
vision,
|
|
2288
|
-
initiator
|
|
2463
|
+
initiator,
|
|
2464
|
+
requestId,
|
|
2465
|
+
sessionId
|
|
2289
2466
|
});
|
|
2290
2467
|
if (isStreamingRequested(payload) && isAsyncIterable(response)) {
|
|
2291
2468
|
logger$1.debug("Forwarding native Responses stream");
|
|
@@ -2408,4 +2585,4 @@ server.route("/v1/messages", messageRoutes);
|
|
|
2408
2585
|
|
|
2409
2586
|
//#endregion
|
|
2410
2587
|
export { server };
|
|
2411
|
-
//# sourceMappingURL=server-
|
|
2588
|
+
//# sourceMappingURL=server-IvwXytB7.js.map
|