@nick3/copilot-api 1.2.2 → 1.2.4
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/{accounts-manager-DellX80M.js → accounts-manager-eec8Wj3_.js} +112 -7
- package/dist/accounts-manager-eec8Wj3_.js.map +1 -0
- package/dist/main.js +4 -2
- package/dist/main.js.map +1 -1
- package/dist/{server-dAi_n8ee.js → server-Cxlbm6kJ.js} +197 -39
- package/dist/server-Cxlbm6kJ.js.map +1 -0
- package/package.json +1 -1
- package/dist/accounts-manager-DellX80M.js.map +0 -1
- package/dist/server-dAi_n8ee.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HTTPError, PATHS, accountFromState, accountsManager, copilotBaseUrl, copilotHeaders, forwardError, getAliasTargetSet, getConfig, getCopilotUsage, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getReasoningEffortForModel, getSmallModel, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isNullish, listAccountsFromRegistry, mergeConfigWithDefaults, shouldCompactUseSmallModel, sleep, state } from "./accounts-manager-
|
|
1
|
+
import { HTTPError, PATHS, accountFromState, accountsManager, copilotBaseUrl, copilotHeaders, forwardError, generateRequestIdFromPayload, getAliasTargetSet, getConfig, getCopilotUsage, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getReasoningEffortForModel, getRootSessionId, getSmallModel, getUUID, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isNullish, isResponsesApiContextManagementModel, listAccountsFromRegistry, mergeConfigWithDefaults, shouldCompactUseSmallModel, sleep, state } from "./accounts-manager-eec8Wj3_.js";
|
|
2
2
|
import consola from "consola";
|
|
3
3
|
import fs, { readFile } from "node:fs/promises";
|
|
4
4
|
import * as path$1 from "node:path";
|
|
@@ -2227,13 +2227,18 @@ const getTokenCount = async (payload, model) => {
|
|
|
2227
2227
|
|
|
2228
2228
|
//#endregion
|
|
2229
2229
|
//#region src/services/copilot/create-responses.ts
|
|
2230
|
-
const createResponses = async (payload, { vision, initiator, upstreamRequestId }, account) => {
|
|
2230
|
+
const createResponses = async (payload, { vision, initiator, upstreamRequestId, subagentMarker, sessionId }, account) => {
|
|
2231
2231
|
const ctx = account ?? accountFromState();
|
|
2232
2232
|
if (!ctx.copilotToken) throw new Error("Copilot token not found");
|
|
2233
2233
|
const headers = {
|
|
2234
2234
|
...copilotHeaders(ctx, vision, upstreamRequestId),
|
|
2235
|
-
"
|
|
2235
|
+
"x-initiator": initiator
|
|
2236
2236
|
};
|
|
2237
|
+
if (subagentMarker) {
|
|
2238
|
+
headers["x-initiator"] = "agent";
|
|
2239
|
+
headers["x-interaction-type"] = "conversation-subagent";
|
|
2240
|
+
}
|
|
2241
|
+
if (sessionId) headers["x-interaction-id"] = sessionId;
|
|
2237
2242
|
payload.service_tier = null;
|
|
2238
2243
|
const response = await fetch(`${copilotBaseUrl(ctx)}/responses`, {
|
|
2239
2244
|
method: "POST",
|
|
@@ -2251,6 +2256,8 @@ const createResponses = async (payload, { vision, initiator, upstreamRequestId }
|
|
|
2251
2256
|
//#endregion
|
|
2252
2257
|
//#region src/routes/messages/responses-translation.ts
|
|
2253
2258
|
const MESSAGE_TYPE = "message";
|
|
2259
|
+
const COMPACTION_SIGNATURE_PREFIX = "cm1#";
|
|
2260
|
+
const COMPACTION_SIGNATURE_SEPARATOR = "@";
|
|
2254
2261
|
const THINKING_TEXT$1 = "Thinking...";
|
|
2255
2262
|
const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) => {
|
|
2256
2263
|
const model = modelOverride ?? payload.model;
|
|
@@ -2282,6 +2289,23 @@ const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) =>
|
|
|
2282
2289
|
include: ["reasoning.encrypted_content"]
|
|
2283
2290
|
};
|
|
2284
2291
|
};
|
|
2292
|
+
const encodeCompactionCarrierSignature = (compaction) => {
|
|
2293
|
+
return `${COMPACTION_SIGNATURE_PREFIX}${compaction.encrypted_content}${COMPACTION_SIGNATURE_SEPARATOR}${compaction.id}`;
|
|
2294
|
+
};
|
|
2295
|
+
const decodeCompactionCarrierSignature = (signature) => {
|
|
2296
|
+
if (signature.startsWith(COMPACTION_SIGNATURE_PREFIX)) {
|
|
2297
|
+
const raw = signature.slice(4);
|
|
2298
|
+
const separatorIndex = raw.indexOf(COMPACTION_SIGNATURE_SEPARATOR);
|
|
2299
|
+
if (separatorIndex <= 0 || separatorIndex === raw.length - 1) return;
|
|
2300
|
+
const encrypted_content = raw.slice(0, separatorIndex);
|
|
2301
|
+
const id = raw.slice(separatorIndex + 1);
|
|
2302
|
+
if (!encrypted_content) return;
|
|
2303
|
+
return {
|
|
2304
|
+
id,
|
|
2305
|
+
encrypted_content
|
|
2306
|
+
};
|
|
2307
|
+
}
|
|
2308
|
+
};
|
|
2285
2309
|
const translateMessage = (message, model, applyPhase) => {
|
|
2286
2310
|
if (message.role === "user") return translateUserMessage(message);
|
|
2287
2311
|
return translateAssistantMessage(message, model, applyPhase);
|
|
@@ -2318,13 +2342,24 @@ const translateAssistantMessage = (message, model, applyPhase) => {
|
|
|
2318
2342
|
items.push(createFunctionToolCall(block));
|
|
2319
2343
|
continue;
|
|
2320
2344
|
}
|
|
2321
|
-
if (block.type === "thinking" && block.signature
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2345
|
+
if (block.type === "thinking" && block.signature) {
|
|
2346
|
+
const compactionContent = createCompactionContent(block);
|
|
2347
|
+
if (compactionContent) {
|
|
2348
|
+
flushPendingContent(pendingContent, items, {
|
|
2349
|
+
role: "assistant",
|
|
2350
|
+
phase: assistantPhase
|
|
2351
|
+
});
|
|
2352
|
+
items.push(compactionContent);
|
|
2353
|
+
continue;
|
|
2354
|
+
}
|
|
2355
|
+
if (block.signature.includes("@")) {
|
|
2356
|
+
flushPendingContent(pendingContent, items, {
|
|
2357
|
+
role: "assistant",
|
|
2358
|
+
phase: assistantPhase
|
|
2359
|
+
});
|
|
2360
|
+
items.push(createReasoningContent(block));
|
|
2361
|
+
continue;
|
|
2362
|
+
}
|
|
2328
2363
|
}
|
|
2329
2364
|
const converted = translateAssistantContentBlock(block);
|
|
2330
2365
|
if (converted) pendingContent.push(converted);
|
|
@@ -2384,9 +2419,7 @@ const createImageContent = (block) => ({
|
|
|
2384
2419
|
detail: "auto"
|
|
2385
2420
|
});
|
|
2386
2421
|
const createReasoningContent = (block) => {
|
|
2387
|
-
const
|
|
2388
|
-
const signature = array[0];
|
|
2389
|
-
const id = array[1];
|
|
2422
|
+
const { encryptedContent, id } = parseReasoningSignature(block.signature);
|
|
2390
2423
|
const thinking = block.thinking === THINKING_TEXT$1 ? "" : block.thinking;
|
|
2391
2424
|
return {
|
|
2392
2425
|
id,
|
|
@@ -2395,7 +2428,27 @@ const createReasoningContent = (block) => {
|
|
|
2395
2428
|
type: "summary_text",
|
|
2396
2429
|
text: thinking
|
|
2397
2430
|
}] : [],
|
|
2398
|
-
encrypted_content:
|
|
2431
|
+
encrypted_content: encryptedContent
|
|
2432
|
+
};
|
|
2433
|
+
};
|
|
2434
|
+
const createCompactionContent = (block) => {
|
|
2435
|
+
const compaction = decodeCompactionCarrierSignature(block.signature);
|
|
2436
|
+
if (!compaction) return;
|
|
2437
|
+
return {
|
|
2438
|
+
id: compaction.id,
|
|
2439
|
+
type: "compaction",
|
|
2440
|
+
encrypted_content: compaction.encrypted_content
|
|
2441
|
+
};
|
|
2442
|
+
};
|
|
2443
|
+
const parseReasoningSignature = (signature) => {
|
|
2444
|
+
const splitIndex = signature.lastIndexOf("@");
|
|
2445
|
+
if (splitIndex <= 0 || splitIndex === signature.length - 1) return {
|
|
2446
|
+
encryptedContent: signature,
|
|
2447
|
+
id: ""
|
|
2448
|
+
};
|
|
2449
|
+
return {
|
|
2450
|
+
encryptedContent: signature.slice(0, splitIndex),
|
|
2451
|
+
id: signature.slice(splitIndex + 1)
|
|
2399
2452
|
};
|
|
2400
2453
|
};
|
|
2401
2454
|
const createFunctionToolCall = (block) => ({
|
|
@@ -2486,6 +2539,11 @@ const mapOutputToAnthropicContent = (output) => {
|
|
|
2486
2539
|
});
|
|
2487
2540
|
break;
|
|
2488
2541
|
}
|
|
2542
|
+
case "compaction": {
|
|
2543
|
+
const compactionBlock = createCompactionThinkingBlock(item);
|
|
2544
|
+
if (compactionBlock) contentBlocks.push(compactionBlock);
|
|
2545
|
+
break;
|
|
2546
|
+
}
|
|
2489
2547
|
default: {
|
|
2490
2548
|
const combinedText = combineMessageTextContent(item.content);
|
|
2491
2549
|
if (combinedText.length > 0) contentBlocks.push({
|
|
@@ -2543,6 +2601,17 @@ const createToolUseContentBlock = (call) => {
|
|
|
2543
2601
|
input
|
|
2544
2602
|
};
|
|
2545
2603
|
};
|
|
2604
|
+
const createCompactionThinkingBlock = (item) => {
|
|
2605
|
+
if (!item.id || !item.encrypted_content) return null;
|
|
2606
|
+
return {
|
|
2607
|
+
type: "thinking",
|
|
2608
|
+
thinking: THINKING_TEXT$1,
|
|
2609
|
+
signature: encodeCompactionCarrierSignature({
|
|
2610
|
+
id: item.id,
|
|
2611
|
+
encrypted_content: item.encrypted_content
|
|
2612
|
+
})
|
|
2613
|
+
};
|
|
2614
|
+
};
|
|
2546
2615
|
const parseFunctionCallArguments = (rawArguments) => {
|
|
2547
2616
|
if (typeof rawArguments !== "string" || rawArguments.trim().length === 0) return {};
|
|
2548
2617
|
try {
|
|
@@ -2647,8 +2716,10 @@ const createChatCompletions = async (payload, account, options) => {
|
|
|
2647
2716
|
const initiator = options?.initiator ?? getChatInitiator(payload.messages);
|
|
2648
2717
|
const headers = {
|
|
2649
2718
|
...copilotHeaders(ctx, enableVision, options?.upstreamRequestId),
|
|
2650
|
-
"
|
|
2719
|
+
"x-initiator": options?.subagentMarker ? "agent" : initiator
|
|
2651
2720
|
};
|
|
2721
|
+
if (options?.subagentMarker) headers["x-interaction-type"] = "conversation-subagent";
|
|
2722
|
+
if (options?.sessionId) headers["x-interaction-id"] = options.sessionId;
|
|
2652
2723
|
const upstreamPayload = applyDefaultReasoningEffort(payload);
|
|
2653
2724
|
const response = await fetch(`${copilotBaseUrl(ctx)}/chat/completions`, {
|
|
2654
2725
|
method: "POST",
|
|
@@ -2726,7 +2797,10 @@ async function handleCompletion$1(c) {
|
|
|
2726
2797
|
if (state.manualApprove) await awaitApproval();
|
|
2727
2798
|
const payloadWithMaxTokens = applyDefaultMaxTokens(upstreamPayload, selectedModel);
|
|
2728
2799
|
const accountCtx = toAccountContext(account);
|
|
2729
|
-
|
|
2800
|
+
const upstreamRequestId = generateRequestIdFromPayload(payloadWithMaxTokens);
|
|
2801
|
+
const upstreamSessionId = getUUID(upstreamRequestId);
|
|
2802
|
+
request.upstreamRequestId = upstreamRequestId;
|
|
2803
|
+
request.upstreamSessionId = upstreamSessionId;
|
|
2730
2804
|
if (streamRequested) return handleStreamingRequest({
|
|
2731
2805
|
c,
|
|
2732
2806
|
store,
|
|
@@ -2829,7 +2903,10 @@ async function handleStreamingRequest(params) {
|
|
|
2829
2903
|
const { c, store, request, payload, selection, accountCtx, clientModel, premiumRemainingBefore, premiumUnlimitedBefore } = params;
|
|
2830
2904
|
let response;
|
|
2831
2905
|
try {
|
|
2832
|
-
response = await createChatCompletions(payload, accountCtx, {
|
|
2906
|
+
response = await createChatCompletions(payload, accountCtx, {
|
|
2907
|
+
upstreamRequestId: request.upstreamRequestId,
|
|
2908
|
+
sessionId: request.upstreamSessionId
|
|
2909
|
+
});
|
|
2833
2910
|
} catch (error) {
|
|
2834
2911
|
return handleUpstreamCreateError$1({
|
|
2835
2912
|
store,
|
|
@@ -3013,7 +3090,10 @@ async function handleNonStreamingRequest(params) {
|
|
|
3013
3090
|
let errorMessage;
|
|
3014
3091
|
let finishedAtMs;
|
|
3015
3092
|
try {
|
|
3016
|
-
const response = await createChatCompletions(payload, accountCtx, {
|
|
3093
|
+
const response = await createChatCompletions(payload, accountCtx, {
|
|
3094
|
+
upstreamRequestId: request.upstreamRequestId,
|
|
3095
|
+
sessionId: request.upstreamSessionId
|
|
3096
|
+
});
|
|
3017
3097
|
finishedAtMs = Date.now();
|
|
3018
3098
|
if (!isNonStreaming$1(response)) {
|
|
3019
3099
|
logger$3.debug("Unexpected streaming response");
|
|
@@ -3716,8 +3796,34 @@ const handleOutputItemAdded$1 = (rawEvent, state$1) => {
|
|
|
3716
3796
|
const handleOutputItemDone$1 = (rawEvent, state$1) => {
|
|
3717
3797
|
const events$1 = new Array();
|
|
3718
3798
|
const item = rawEvent.item;
|
|
3719
|
-
|
|
3799
|
+
const itemType = item.type;
|
|
3720
3800
|
const outputIndex = rawEvent.output_index;
|
|
3801
|
+
if (itemType === "compaction") {
|
|
3802
|
+
if (!item.id || !item.encrypted_content) return events$1;
|
|
3803
|
+
const blockIndex$1 = openThinkingBlockIfNeeded(state$1, outputIndex, events$1);
|
|
3804
|
+
if (!state$1.blockHasDelta.has(blockIndex$1)) events$1.push({
|
|
3805
|
+
type: "content_block_delta",
|
|
3806
|
+
index: blockIndex$1,
|
|
3807
|
+
delta: {
|
|
3808
|
+
type: "thinking_delta",
|
|
3809
|
+
thinking: THINKING_TEXT$1
|
|
3810
|
+
}
|
|
3811
|
+
});
|
|
3812
|
+
events$1.push({
|
|
3813
|
+
type: "content_block_delta",
|
|
3814
|
+
index: blockIndex$1,
|
|
3815
|
+
delta: {
|
|
3816
|
+
type: "signature_delta",
|
|
3817
|
+
signature: encodeCompactionCarrierSignature({
|
|
3818
|
+
id: item.id,
|
|
3819
|
+
encrypted_content: item.encrypted_content
|
|
3820
|
+
})
|
|
3821
|
+
}
|
|
3822
|
+
});
|
|
3823
|
+
state$1.blockHasDelta.add(blockIndex$1);
|
|
3824
|
+
return events$1;
|
|
3825
|
+
}
|
|
3826
|
+
if (itemType !== "reasoning") return events$1;
|
|
3721
3827
|
const blockIndex = openThinkingBlockIfNeeded(state$1, outputIndex, events$1);
|
|
3722
3828
|
const signature = (item.encrypted_content ?? "") + "@" + item.id;
|
|
3723
3829
|
if (signature) {
|
|
@@ -4065,6 +4171,31 @@ const isAgentRole = (item) => {
|
|
|
4065
4171
|
const hasVisionInput = (payload) => {
|
|
4066
4172
|
return getPayloadItems(payload).some((item) => containsVisionContent(item));
|
|
4067
4173
|
};
|
|
4174
|
+
const resolveResponsesCompactThreshold = (maxPromptTokens) => {
|
|
4175
|
+
if (typeof maxPromptTokens === "number" && maxPromptTokens > 0) return Math.floor(maxPromptTokens * .9);
|
|
4176
|
+
return 5e4;
|
|
4177
|
+
};
|
|
4178
|
+
const createCompactionContextManagement = (compactThreshold) => [{
|
|
4179
|
+
type: "compaction",
|
|
4180
|
+
compact_threshold: compactThreshold
|
|
4181
|
+
}];
|
|
4182
|
+
const applyResponsesApiContextManagement = (payload, maxPromptTokens) => {
|
|
4183
|
+
if (payload.context_management !== void 0) return;
|
|
4184
|
+
if (!isResponsesApiContextManagementModel(payload.model)) return;
|
|
4185
|
+
payload.context_management = createCompactionContextManagement(resolveResponsesCompactThreshold(maxPromptTokens));
|
|
4186
|
+
};
|
|
4187
|
+
const compactInputByLatestCompaction = (payload) => {
|
|
4188
|
+
if (!Array.isArray(payload.input) || payload.input.length === 0) return;
|
|
4189
|
+
const latestCompactionMessageIndex = getLatestCompactionMessageIndex(payload.input);
|
|
4190
|
+
if (latestCompactionMessageIndex === void 0) return;
|
|
4191
|
+
payload.input = payload.input.slice(latestCompactionMessageIndex);
|
|
4192
|
+
};
|
|
4193
|
+
const getLatestCompactionMessageIndex = (input) => {
|
|
4194
|
+
for (let index = input.length - 1; index >= 0; index -= 1) if (isCompactionInputItem(input[index])) return index;
|
|
4195
|
+
};
|
|
4196
|
+
const isCompactionInputItem = (value) => {
|
|
4197
|
+
return "type" in value && typeof value.type === "string" && value.type === "compaction";
|
|
4198
|
+
};
|
|
4068
4199
|
const getPayloadItems = (payload) => {
|
|
4069
4200
|
const result = [];
|
|
4070
4201
|
const { input } = payload;
|
|
@@ -4113,8 +4244,10 @@ const createMessages = async (payload, account, options) => {
|
|
|
4113
4244
|
const initiator = options?.initiator ?? getMessagesInitiator(payload);
|
|
4114
4245
|
const headers = {
|
|
4115
4246
|
...copilotHeaders(ctx, enableVision, options?.upstreamRequestId),
|
|
4116
|
-
"
|
|
4247
|
+
"x-initiator": options?.subagentMarker ? "agent" : initiator
|
|
4117
4248
|
};
|
|
4249
|
+
if (options?.subagentMarker) headers["x-interaction-type"] = "conversation-subagent";
|
|
4250
|
+
if (options?.sessionId) headers["x-interaction-id"] = options.sessionId;
|
|
4118
4251
|
const anthropicBeta = buildAnthropicBetaHeader(options?.anthropicBetaHeader, payload.thinking);
|
|
4119
4252
|
if (anthropicBeta) headers["anthropic-beta"] = anthropicBeta;
|
|
4120
4253
|
const response = await fetch(`${copilotBaseUrl(ctx)}/v1/messages`, {
|
|
@@ -4453,6 +4586,8 @@ async function handleCompletion(c) {
|
|
|
4453
4586
|
const subagentMarker = parseSubagentMarkerFromFirstUser(anthropicPayload);
|
|
4454
4587
|
const initiatorOverride = subagentMarker ? "agent" : void 0;
|
|
4455
4588
|
if (subagentMarker) logger$2.debug("Detected Subagent marker:", JSON.stringify(subagentMarker));
|
|
4589
|
+
const sessionId = getRootSessionId(anthropicPayload, c);
|
|
4590
|
+
logger$2.debug("Extracted session ID:", sessionId);
|
|
4456
4591
|
const anthropicBeta = c.req.header("anthropic-beta");
|
|
4457
4592
|
const isCompact = isCompactRequest(anthropicPayload);
|
|
4458
4593
|
if (anthropicBeta && isWarmupProbeRequest(anthropicPayload)) anthropicPayload.model = getSmallModel();
|
|
@@ -4460,6 +4595,8 @@ async function handleCompletion(c) {
|
|
|
4460
4595
|
logger$2.debug("Is compact request:", isCompact);
|
|
4461
4596
|
if (shouldCompactUseSmallModel()) anthropicPayload.model = getSmallModel();
|
|
4462
4597
|
} else mergeToolResultForClaude(anthropicPayload);
|
|
4598
|
+
const upstreamRequestId = generateRequestIdFromPayload(anthropicPayload, sessionId);
|
|
4599
|
+
logger$2.debug("Generated request ID:", upstreamRequestId);
|
|
4463
4600
|
const clientModel = anthropicPayload.model;
|
|
4464
4601
|
const streamRequested = Boolean(anthropicPayload.stream);
|
|
4465
4602
|
const rawUserId = anthropicPayload.metadata?.user_id;
|
|
@@ -4543,6 +4680,7 @@ async function handleCompletion(c) {
|
|
|
4543
4680
|
upstreamEndpoint: endpoint,
|
|
4544
4681
|
upstreamModel: selectedModel.id,
|
|
4545
4682
|
costUnits,
|
|
4683
|
+
upstreamRequestId,
|
|
4546
4684
|
premiumRemainingBefore,
|
|
4547
4685
|
premiumUnlimitedBefore
|
|
4548
4686
|
};
|
|
@@ -4551,6 +4689,8 @@ async function handleCompletion(c) {
|
|
|
4551
4689
|
anthropicPayload,
|
|
4552
4690
|
anthropicBetaHeader: anthropicBeta ?? void 0,
|
|
4553
4691
|
initiatorOverride,
|
|
4692
|
+
subagentMarker,
|
|
4693
|
+
sessionId,
|
|
4554
4694
|
instr,
|
|
4555
4695
|
selectedModel
|
|
4556
4696
|
});
|
|
@@ -4559,6 +4699,8 @@ async function handleCompletion(c) {
|
|
|
4559
4699
|
anthropicPayload,
|
|
4560
4700
|
openAIPayload,
|
|
4561
4701
|
initiatorOverride,
|
|
4702
|
+
subagentMarker,
|
|
4703
|
+
sessionId,
|
|
4562
4704
|
selectedModel,
|
|
4563
4705
|
instr
|
|
4564
4706
|
});
|
|
@@ -4566,23 +4708,25 @@ async function handleCompletion(c) {
|
|
|
4566
4708
|
c,
|
|
4567
4709
|
openAIPayload,
|
|
4568
4710
|
initiatorOverride,
|
|
4711
|
+
subagentMarker,
|
|
4712
|
+
sessionId,
|
|
4569
4713
|
selectedModel,
|
|
4570
4714
|
instr
|
|
4571
4715
|
});
|
|
4572
4716
|
}
|
|
4573
4717
|
const handleWithChatCompletions = async (params) => {
|
|
4574
|
-
const { c, openAIPayload, initiatorOverride, selectedModel, instr } = params;
|
|
4718
|
+
const { c, openAIPayload, initiatorOverride, subagentMarker, sessionId, selectedModel, instr } = params;
|
|
4575
4719
|
logger$2.debug("Translated OpenAI request payload:", JSON.stringify(openAIPayload));
|
|
4576
4720
|
const ctx = toAccountContext(instr.account);
|
|
4577
4721
|
const initiator = initiatorOverride ?? getChatInitiator(openAIPayload.messages);
|
|
4578
|
-
const upstreamRequestId = randomUUID();
|
|
4579
4722
|
instr.initiator = initiator;
|
|
4580
|
-
instr.upstreamRequestId = upstreamRequestId;
|
|
4581
4723
|
let response;
|
|
4582
4724
|
try {
|
|
4583
4725
|
response = await createChatCompletions(openAIPayload, ctx, {
|
|
4584
|
-
upstreamRequestId,
|
|
4585
|
-
initiator
|
|
4726
|
+
upstreamRequestId: instr.upstreamRequestId,
|
|
4727
|
+
initiator,
|
|
4728
|
+
subagentMarker,
|
|
4729
|
+
sessionId
|
|
4586
4730
|
});
|
|
4587
4731
|
} catch (error) {
|
|
4588
4732
|
return await handleChatCompletionsCreateError({
|
|
@@ -4613,21 +4757,23 @@ const handleWithChatCompletions = async (params) => {
|
|
|
4613
4757
|
}));
|
|
4614
4758
|
};
|
|
4615
4759
|
const handleWithResponsesApi = async (params) => {
|
|
4616
|
-
const { c, anthropicPayload, openAIPayload, initiatorOverride, selectedModel, instr } = params;
|
|
4760
|
+
const { c, anthropicPayload, openAIPayload, initiatorOverride, subagentMarker, sessionId, selectedModel, instr } = params;
|
|
4617
4761
|
const responsesPayload = translateAnthropicMessagesToResponsesPayload(anthropicPayload, selectedModel.id);
|
|
4762
|
+
applyResponsesApiContextManagement(responsesPayload, selectedModel.capabilities.limits.max_prompt_tokens);
|
|
4763
|
+
compactInputByLatestCompaction(responsesPayload);
|
|
4618
4764
|
logger$2.debug("Translated Responses payload:", JSON.stringify(responsesPayload));
|
|
4619
4765
|
const { vision, initiator } = getResponsesRequestOptions(responsesPayload);
|
|
4620
4766
|
const resolvedInitiator = initiatorOverride ?? initiator;
|
|
4621
4767
|
const ctx = toAccountContext(instr.account);
|
|
4622
|
-
const upstreamRequestId = randomUUID();
|
|
4623
4768
|
instr.initiator = resolvedInitiator;
|
|
4624
|
-
instr.upstreamRequestId = upstreamRequestId;
|
|
4625
4769
|
let response;
|
|
4626
4770
|
try {
|
|
4627
4771
|
response = await createResponses(responsesPayload, {
|
|
4628
4772
|
vision,
|
|
4629
4773
|
initiator: resolvedInitiator,
|
|
4630
|
-
upstreamRequestId
|
|
4774
|
+
upstreamRequestId: instr.upstreamRequestId,
|
|
4775
|
+
subagentMarker,
|
|
4776
|
+
sessionId
|
|
4631
4777
|
}, ctx);
|
|
4632
4778
|
} catch (error) {
|
|
4633
4779
|
return await handleResponsesCreateError({
|
|
@@ -5078,27 +5224,31 @@ async function streamMessagesAndLog(params) {
|
|
|
5078
5224
|
}
|
|
5079
5225
|
}
|
|
5080
5226
|
const handleWithMessagesApi = async (params) => {
|
|
5081
|
-
const { c, anthropicPayload, anthropicBetaHeader, initiatorOverride, instr, selectedModel } = params;
|
|
5227
|
+
const { c, anthropicPayload, anthropicBetaHeader, initiatorOverride, subagentMarker, sessionId, instr, selectedModel } = params;
|
|
5082
5228
|
for (const msg of anthropicPayload.messages) if (msg.role === "assistant" && Array.isArray(msg.content)) msg.content = msg.content.filter((block) => {
|
|
5083
5229
|
if (block.type !== "thinking") return true;
|
|
5084
5230
|
return block.thinking && block.thinking !== "Thinking..." && block.signature && !block.signature.includes("@");
|
|
5085
5231
|
});
|
|
5086
|
-
|
|
5232
|
+
const toolChoice = anthropicPayload.tool_choice;
|
|
5233
|
+
if (toolChoice?.type === "any" || toolChoice?.type === "tool") {
|
|
5234
|
+
delete anthropicPayload.thinking;
|
|
5235
|
+
delete anthropicPayload.output_config;
|
|
5236
|
+
} else if (selectedModel.capabilities.supports.adaptive_thinking) {
|
|
5087
5237
|
anthropicPayload.thinking = { type: "adaptive" };
|
|
5088
5238
|
anthropicPayload.output_config = { effort: getAnthropicEffortForModel(anthropicPayload.model) };
|
|
5089
5239
|
}
|
|
5090
5240
|
logger$2.debug("Translated Messages payload:", JSON.stringify(anthropicPayload));
|
|
5091
5241
|
const ctx = toAccountContext(instr.account);
|
|
5092
|
-
const upstreamRequestId = randomUUID();
|
|
5093
5242
|
const initiator = initiatorOverride ?? getMessagesInitiator(anthropicPayload);
|
|
5094
5243
|
instr.initiator = initiator;
|
|
5095
|
-
instr.upstreamRequestId = upstreamRequestId;
|
|
5096
5244
|
let response;
|
|
5097
5245
|
try {
|
|
5098
5246
|
response = await createMessages(anthropicPayload, ctx, {
|
|
5099
5247
|
anthropicBetaHeader,
|
|
5100
|
-
upstreamRequestId,
|
|
5101
|
-
initiator
|
|
5248
|
+
upstreamRequestId: instr.upstreamRequestId,
|
|
5249
|
+
initiator,
|
|
5250
|
+
subagentMarker,
|
|
5251
|
+
sessionId
|
|
5102
5252
|
});
|
|
5103
5253
|
} catch (error) {
|
|
5104
5254
|
return await handleMessagesCreateError({
|
|
@@ -5241,6 +5391,7 @@ const handleResponses = async (c) => {
|
|
|
5241
5391
|
const clientModel = payload.model;
|
|
5242
5392
|
logger$1.debug("Responses request payload:", JSON.stringify(payload));
|
|
5243
5393
|
removeWebSearchTool(payload);
|
|
5394
|
+
compactInputByLatestCompaction(payload);
|
|
5244
5395
|
const streamRequested = Boolean(payload.stream);
|
|
5245
5396
|
const { initiator: initialInitiator } = getResponsesRequestOptions(payload);
|
|
5246
5397
|
const userId = payload.metadata?.user_id;
|
|
@@ -5282,13 +5433,18 @@ const handleResponses = async (c) => {
|
|
|
5282
5433
|
model: selectedModel.id
|
|
5283
5434
|
};
|
|
5284
5435
|
useFunctionApplyPatch(upstreamPayload);
|
|
5436
|
+
applyResponsesApiContextManagement(upstreamPayload, selectedModel.capabilities.limits.max_prompt_tokens);
|
|
5437
|
+
compactInputByLatestCompaction(upstreamPayload);
|
|
5285
5438
|
const premiumRemainingBefore = account.premiumRemaining;
|
|
5286
5439
|
const premiumUnlimitedBefore = account.unlimited;
|
|
5287
5440
|
const { vision, initiator } = getResponsesRequestOptions(upstreamPayload);
|
|
5288
5441
|
request.initiator = initiator;
|
|
5289
5442
|
if (state.manualApprove) await awaitApproval();
|
|
5290
5443
|
const accountCtx = toAccountContext(account);
|
|
5291
|
-
|
|
5444
|
+
const upstreamRequestId = generateRequestIdFromPayload({ messages: upstreamPayload.input });
|
|
5445
|
+
const upstreamSessionId = getUUID(upstreamRequestId);
|
|
5446
|
+
request.upstreamRequestId = upstreamRequestId;
|
|
5447
|
+
request.upstreamSessionId = upstreamSessionId;
|
|
5292
5448
|
if (streamRequested) return handleStreamingResponses({
|
|
5293
5449
|
c,
|
|
5294
5450
|
store,
|
|
@@ -5399,7 +5555,8 @@ async function handleStreamingResponses(params) {
|
|
|
5399
5555
|
response = await createResponses(payload, {
|
|
5400
5556
|
vision,
|
|
5401
5557
|
initiator,
|
|
5402
|
-
upstreamRequestId: request.upstreamRequestId
|
|
5558
|
+
upstreamRequestId: request.upstreamRequestId,
|
|
5559
|
+
sessionId: request.upstreamSessionId
|
|
5403
5560
|
}, accountCtx);
|
|
5404
5561
|
} catch (error) {
|
|
5405
5562
|
return handleUpstreamCreateError({
|
|
@@ -5584,7 +5741,8 @@ async function handleNonStreamingResponses(params) {
|
|
|
5584
5741
|
const response = await createResponses(payload, {
|
|
5585
5742
|
vision,
|
|
5586
5743
|
initiator,
|
|
5587
|
-
upstreamRequestId: request.upstreamRequestId
|
|
5744
|
+
upstreamRequestId: request.upstreamRequestId,
|
|
5745
|
+
sessionId: request.upstreamSessionId
|
|
5588
5746
|
}, accountCtx);
|
|
5589
5747
|
finishedAtMs = Date.now();
|
|
5590
5748
|
const streamResponse = handleUnexpectedResponsesStream(c, response);
|
|
@@ -5767,4 +5925,4 @@ server.route("/v1/messages", messageRoutes);
|
|
|
5767
5925
|
|
|
5768
5926
|
//#endregion
|
|
5769
5927
|
export { server };
|
|
5770
|
-
//# sourceMappingURL=server-
|
|
5928
|
+
//# sourceMappingURL=server-Cxlbm6kJ.js.map
|