@nick3/copilot-api 1.2.1 → 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.
@@ -1,10 +1,10 @@
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-BY_xYGQ7.js";
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";
5
5
  import path from "node:path";
6
6
  import { randomUUID, timingSafeEqual } from "node:crypto";
7
- import fs$1, { existsSync, readFileSync } from "node:fs";
7
+ import fs$1, { existsSync } from "node:fs";
8
8
  import { Hono } from "hono";
9
9
  import { cors } from "hono/cors";
10
10
  import { logger } from "hono/logger";
@@ -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
- "X-Initiator": initiator
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,12 +2256,14 @@ 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";
2254
- const CODEX_PHASE_MODEL = "gpt-5.3-codex";
2259
+ const COMPACTION_SIGNATURE_PREFIX = "cm1#";
2260
+ const COMPACTION_SIGNATURE_SEPARATOR = "@";
2255
2261
  const THINKING_TEXT$1 = "Thinking...";
2256
2262
  const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) => {
2257
2263
  const model = modelOverride ?? payload.model;
2258
2264
  const input = [];
2259
- for (const message of payload.messages) input.push(...translateMessage(message, payload.model));
2265
+ const applyPhase = shouldApplyPhase(payload.model);
2266
+ for (const message of payload.messages) input.push(...translateMessage(message, payload.model, applyPhase));
2260
2267
  const translatedTools = convertAnthropicTools(payload.tools);
2261
2268
  const toolChoice = convertAnthropicToolChoice(payload.tool_choice);
2262
2269
  const { safetyIdentifier, promptCacheKey } = parseUserId(payload.metadata?.user_id);
@@ -2282,9 +2289,26 @@ const translateAnthropicMessagesToResponsesPayload = (payload, modelOverride) =>
2282
2289
  include: ["reasoning.encrypted_content"]
2283
2290
  };
2284
2291
  };
2285
- const translateMessage = (message, model) => {
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
+ };
2309
+ const translateMessage = (message, model, applyPhase) => {
2286
2310
  if (message.role === "user") return translateUserMessage(message);
2287
- return translateAssistantMessage(message, model);
2311
+ return translateAssistantMessage(message, model, applyPhase);
2288
2312
  };
2289
2313
  const translateUserMessage = (message) => {
2290
2314
  if (typeof message.content === "string") return [createMessage("user", message.content)];
@@ -2303,8 +2327,8 @@ const translateUserMessage = (message) => {
2303
2327
  flushPendingContent(pendingContent, items, { role: "user" });
2304
2328
  return items;
2305
2329
  };
2306
- const translateAssistantMessage = (message, model) => {
2307
- const assistantPhase = resolveAssistantPhase(model, message.content);
2330
+ const translateAssistantMessage = (message, model, applyPhase) => {
2331
+ const assistantPhase = resolveAssistantPhase(model, message.content, applyPhase);
2308
2332
  if (typeof message.content === "string") return [createMessage("assistant", message.content, assistantPhase)];
2309
2333
  if (!Array.isArray(message.content)) return [];
2310
2334
  const items = [];
@@ -2318,13 +2342,24 @@ const translateAssistantMessage = (message, model) => {
2318
2342
  items.push(createFunctionToolCall(block));
2319
2343
  continue;
2320
2344
  }
2321
- if (block.type === "thinking" && block.signature && block.signature.includes("@")) {
2322
- flushPendingContent(pendingContent, items, {
2323
- role: "assistant",
2324
- phase: assistantPhase
2325
- });
2326
- items.push(createReasoningContent(block));
2327
- continue;
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);
@@ -2360,14 +2395,16 @@ const createMessage = (role, content, phase) => ({
2360
2395
  content,
2361
2396
  ...role === "assistant" && phase ? { phase } : {}
2362
2397
  });
2363
- const resolveAssistantPhase = (model, content) => {
2364
- if (!shouldApplyCodexPhase(model)) return;
2398
+ const resolveAssistantPhase = (_model, content, applyPhase) => {
2399
+ if (!applyPhase) return;
2365
2400
  if (typeof content === "string") return "final_answer";
2366
2401
  if (!Array.isArray(content)) return;
2367
2402
  if (!content.some((block) => block.type === "text")) return;
2368
2403
  return content.some((block) => block.type === "tool_use") ? "commentary" : "final_answer";
2369
2404
  };
2370
- const shouldApplyCodexPhase = (model) => model === CODEX_PHASE_MODEL;
2405
+ const shouldApplyPhase = (model) => {
2406
+ return getExtraPromptForModel(model).includes("## Intermediary updates");
2407
+ };
2371
2408
  const createTextContent = (text) => ({
2372
2409
  type: "input_text",
2373
2410
  text
@@ -2382,9 +2419,7 @@ const createImageContent = (block) => ({
2382
2419
  detail: "auto"
2383
2420
  });
2384
2421
  const createReasoningContent = (block) => {
2385
- const array = block.signature.split("@");
2386
- const signature = array[0];
2387
- const id = array[1];
2422
+ const { encryptedContent, id } = parseReasoningSignature(block.signature);
2388
2423
  const thinking = block.thinking === THINKING_TEXT$1 ? "" : block.thinking;
2389
2424
  return {
2390
2425
  id,
@@ -2393,7 +2428,27 @@ const createReasoningContent = (block) => {
2393
2428
  type: "summary_text",
2394
2429
  text: thinking
2395
2430
  }] : [],
2396
- encrypted_content: signature
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)
2397
2452
  };
2398
2453
  };
2399
2454
  const createFunctionToolCall = (block) => ({
@@ -2484,6 +2539,11 @@ const mapOutputToAnthropicContent = (output) => {
2484
2539
  });
2485
2540
  break;
2486
2541
  }
2542
+ case "compaction": {
2543
+ const compactionBlock = createCompactionThinkingBlock(item);
2544
+ if (compactionBlock) contentBlocks.push(compactionBlock);
2545
+ break;
2546
+ }
2487
2547
  default: {
2488
2548
  const combinedText = combineMessageTextContent(item.content);
2489
2549
  if (combinedText.length > 0) contentBlocks.push({
@@ -2541,6 +2601,17 @@ const createToolUseContentBlock = (call) => {
2541
2601
  input
2542
2602
  };
2543
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
+ };
2544
2615
  const parseFunctionCallArguments = (rawArguments) => {
2545
2616
  if (typeof rawArguments !== "string" || rawArguments.trim().length === 0) return {};
2546
2617
  try {
@@ -2645,8 +2716,10 @@ const createChatCompletions = async (payload, account, options) => {
2645
2716
  const initiator = options?.initiator ?? getChatInitiator(payload.messages);
2646
2717
  const headers = {
2647
2718
  ...copilotHeaders(ctx, enableVision, options?.upstreamRequestId),
2648
- "X-Initiator": initiator
2719
+ "x-initiator": options?.subagentMarker ? "agent" : initiator
2649
2720
  };
2721
+ if (options?.subagentMarker) headers["x-interaction-type"] = "conversation-subagent";
2722
+ if (options?.sessionId) headers["x-interaction-id"] = options.sessionId;
2650
2723
  const upstreamPayload = applyDefaultReasoningEffort(payload);
2651
2724
  const response = await fetch(`${copilotBaseUrl(ctx)}/chat/completions`, {
2652
2725
  method: "POST",
@@ -2724,7 +2797,10 @@ async function handleCompletion$1(c) {
2724
2797
  if (state.manualApprove) await awaitApproval();
2725
2798
  const payloadWithMaxTokens = applyDefaultMaxTokens(upstreamPayload, selectedModel);
2726
2799
  const accountCtx = toAccountContext(account);
2727
- request.upstreamRequestId = randomUUID();
2800
+ const upstreamRequestId = generateRequestIdFromPayload(payloadWithMaxTokens);
2801
+ const upstreamSessionId = getUUID(upstreamRequestId);
2802
+ request.upstreamRequestId = upstreamRequestId;
2803
+ request.upstreamSessionId = upstreamSessionId;
2728
2804
  if (streamRequested) return handleStreamingRequest({
2729
2805
  c,
2730
2806
  store,
@@ -2827,7 +2903,10 @@ async function handleStreamingRequest(params) {
2827
2903
  const { c, store, request, payload, selection, accountCtx, clientModel, premiumRemainingBefore, premiumUnlimitedBefore } = params;
2828
2904
  let response;
2829
2905
  try {
2830
- response = await createChatCompletions(payload, accountCtx, { upstreamRequestId: request.upstreamRequestId });
2906
+ response = await createChatCompletions(payload, accountCtx, {
2907
+ upstreamRequestId: request.upstreamRequestId,
2908
+ sessionId: request.upstreamSessionId
2909
+ });
2831
2910
  } catch (error) {
2832
2911
  return handleUpstreamCreateError$1({
2833
2912
  store,
@@ -3011,7 +3090,10 @@ async function handleNonStreamingRequest(params) {
3011
3090
  let errorMessage;
3012
3091
  let finishedAtMs;
3013
3092
  try {
3014
- const response = await createChatCompletions(payload, accountCtx, { upstreamRequestId: request.upstreamRequestId });
3093
+ const response = await createChatCompletions(payload, accountCtx, {
3094
+ upstreamRequestId: request.upstreamRequestId,
3095
+ sessionId: request.upstreamSessionId
3096
+ });
3015
3097
  finishedAtMs = Date.now();
3016
3098
  if (!isNonStreaming$1(response)) {
3017
3099
  logger$3.debug("Unexpected streaming response");
@@ -3714,8 +3796,34 @@ const handleOutputItemAdded$1 = (rawEvent, state$1) => {
3714
3796
  const handleOutputItemDone$1 = (rawEvent, state$1) => {
3715
3797
  const events$1 = new Array();
3716
3798
  const item = rawEvent.item;
3717
- if (item.type !== "reasoning") return events$1;
3799
+ const itemType = item.type;
3718
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;
3719
3827
  const blockIndex = openThinkingBlockIfNeeded(state$1, outputIndex, events$1);
3720
3828
  const signature = (item.encrypted_content ?? "") + "@" + item.id;
3721
3829
  if (signature) {
@@ -4063,6 +4171,31 @@ const isAgentRole = (item) => {
4063
4171
  const hasVisionInput = (payload) => {
4064
4172
  return getPayloadItems(payload).some((item) => containsVisionContent(item));
4065
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
+ };
4066
4199
  const getPayloadItems = (payload) => {
4067
4200
  const result = [];
4068
4201
  const { input } = payload;
@@ -4111,8 +4244,10 @@ const createMessages = async (payload, account, options) => {
4111
4244
  const initiator = options?.initiator ?? getMessagesInitiator(payload);
4112
4245
  const headers = {
4113
4246
  ...copilotHeaders(ctx, enableVision, options?.upstreamRequestId),
4114
- "X-Initiator": initiator
4247
+ "x-initiator": options?.subagentMarker ? "agent" : initiator
4115
4248
  };
4249
+ if (options?.subagentMarker) headers["x-interaction-type"] = "conversation-subagent";
4250
+ if (options?.sessionId) headers["x-interaction-id"] = options.sessionId;
4116
4251
  const anthropicBeta = buildAnthropicBetaHeader(options?.anthropicBetaHeader, payload.thinking);
4117
4252
  if (anthropicBeta) headers["anthropic-beta"] = anthropicBeta;
4118
4253
  const response = await fetch(`${copilotBaseUrl(ctx)}/v1/messages`, {
@@ -4451,6 +4586,8 @@ async function handleCompletion(c) {
4451
4586
  const subagentMarker = parseSubagentMarkerFromFirstUser(anthropicPayload);
4452
4587
  const initiatorOverride = subagentMarker ? "agent" : void 0;
4453
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);
4454
4591
  const anthropicBeta = c.req.header("anthropic-beta");
4455
4592
  const isCompact = isCompactRequest(anthropicPayload);
4456
4593
  if (anthropicBeta && isWarmupProbeRequest(anthropicPayload)) anthropicPayload.model = getSmallModel();
@@ -4458,6 +4595,8 @@ async function handleCompletion(c) {
4458
4595
  logger$2.debug("Is compact request:", isCompact);
4459
4596
  if (shouldCompactUseSmallModel()) anthropicPayload.model = getSmallModel();
4460
4597
  } else mergeToolResultForClaude(anthropicPayload);
4598
+ const upstreamRequestId = generateRequestIdFromPayload(anthropicPayload, sessionId);
4599
+ logger$2.debug("Generated request ID:", upstreamRequestId);
4461
4600
  const clientModel = anthropicPayload.model;
4462
4601
  const streamRequested = Boolean(anthropicPayload.stream);
4463
4602
  const rawUserId = anthropicPayload.metadata?.user_id;
@@ -4541,6 +4680,7 @@ async function handleCompletion(c) {
4541
4680
  upstreamEndpoint: endpoint,
4542
4681
  upstreamModel: selectedModel.id,
4543
4682
  costUnits,
4683
+ upstreamRequestId,
4544
4684
  premiumRemainingBefore,
4545
4685
  premiumUnlimitedBefore
4546
4686
  };
@@ -4549,6 +4689,8 @@ async function handleCompletion(c) {
4549
4689
  anthropicPayload,
4550
4690
  anthropicBetaHeader: anthropicBeta ?? void 0,
4551
4691
  initiatorOverride,
4692
+ subagentMarker,
4693
+ sessionId,
4552
4694
  instr,
4553
4695
  selectedModel
4554
4696
  });
@@ -4557,6 +4699,8 @@ async function handleCompletion(c) {
4557
4699
  anthropicPayload,
4558
4700
  openAIPayload,
4559
4701
  initiatorOverride,
4702
+ subagentMarker,
4703
+ sessionId,
4560
4704
  selectedModel,
4561
4705
  instr
4562
4706
  });
@@ -4564,23 +4708,25 @@ async function handleCompletion(c) {
4564
4708
  c,
4565
4709
  openAIPayload,
4566
4710
  initiatorOverride,
4711
+ subagentMarker,
4712
+ sessionId,
4567
4713
  selectedModel,
4568
4714
  instr
4569
4715
  });
4570
4716
  }
4571
4717
  const handleWithChatCompletions = async (params) => {
4572
- const { c, openAIPayload, initiatorOverride, selectedModel, instr } = params;
4718
+ const { c, openAIPayload, initiatorOverride, subagentMarker, sessionId, selectedModel, instr } = params;
4573
4719
  logger$2.debug("Translated OpenAI request payload:", JSON.stringify(openAIPayload));
4574
4720
  const ctx = toAccountContext(instr.account);
4575
4721
  const initiator = initiatorOverride ?? getChatInitiator(openAIPayload.messages);
4576
- const upstreamRequestId = randomUUID();
4577
4722
  instr.initiator = initiator;
4578
- instr.upstreamRequestId = upstreamRequestId;
4579
4723
  let response;
4580
4724
  try {
4581
4725
  response = await createChatCompletions(openAIPayload, ctx, {
4582
- upstreamRequestId,
4583
- initiator
4726
+ upstreamRequestId: instr.upstreamRequestId,
4727
+ initiator,
4728
+ subagentMarker,
4729
+ sessionId
4584
4730
  });
4585
4731
  } catch (error) {
4586
4732
  return await handleChatCompletionsCreateError({
@@ -4611,21 +4757,23 @@ const handleWithChatCompletions = async (params) => {
4611
4757
  }));
4612
4758
  };
4613
4759
  const handleWithResponsesApi = async (params) => {
4614
- const { c, anthropicPayload, openAIPayload, initiatorOverride, selectedModel, instr } = params;
4760
+ const { c, anthropicPayload, openAIPayload, initiatorOverride, subagentMarker, sessionId, selectedModel, instr } = params;
4615
4761
  const responsesPayload = translateAnthropicMessagesToResponsesPayload(anthropicPayload, selectedModel.id);
4762
+ applyResponsesApiContextManagement(responsesPayload, selectedModel.capabilities.limits.max_prompt_tokens);
4763
+ compactInputByLatestCompaction(responsesPayload);
4616
4764
  logger$2.debug("Translated Responses payload:", JSON.stringify(responsesPayload));
4617
4765
  const { vision, initiator } = getResponsesRequestOptions(responsesPayload);
4618
4766
  const resolvedInitiator = initiatorOverride ?? initiator;
4619
4767
  const ctx = toAccountContext(instr.account);
4620
- const upstreamRequestId = randomUUID();
4621
4768
  instr.initiator = resolvedInitiator;
4622
- instr.upstreamRequestId = upstreamRequestId;
4623
4769
  let response;
4624
4770
  try {
4625
4771
  response = await createResponses(responsesPayload, {
4626
4772
  vision,
4627
4773
  initiator: resolvedInitiator,
4628
- upstreamRequestId
4774
+ upstreamRequestId: instr.upstreamRequestId,
4775
+ subagentMarker,
4776
+ sessionId
4629
4777
  }, ctx);
4630
4778
  } catch (error) {
4631
4779
  return await handleResponsesCreateError({
@@ -5076,27 +5224,31 @@ async function streamMessagesAndLog(params) {
5076
5224
  }
5077
5225
  }
5078
5226
  const handleWithMessagesApi = async (params) => {
5079
- const { c, anthropicPayload, anthropicBetaHeader, initiatorOverride, instr, selectedModel } = params;
5227
+ const { c, anthropicPayload, anthropicBetaHeader, initiatorOverride, subagentMarker, sessionId, instr, selectedModel } = params;
5080
5228
  for (const msg of anthropicPayload.messages) if (msg.role === "assistant" && Array.isArray(msg.content)) msg.content = msg.content.filter((block) => {
5081
5229
  if (block.type !== "thinking") return true;
5082
5230
  return block.thinking && block.thinking !== "Thinking..." && block.signature && !block.signature.includes("@");
5083
5231
  });
5084
- if (selectedModel.capabilities.supports.adaptive_thinking) {
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) {
5085
5237
  anthropicPayload.thinking = { type: "adaptive" };
5086
5238
  anthropicPayload.output_config = { effort: getAnthropicEffortForModel(anthropicPayload.model) };
5087
5239
  }
5088
5240
  logger$2.debug("Translated Messages payload:", JSON.stringify(anthropicPayload));
5089
5241
  const ctx = toAccountContext(instr.account);
5090
- const upstreamRequestId = randomUUID();
5091
5242
  const initiator = initiatorOverride ?? getMessagesInitiator(anthropicPayload);
5092
5243
  instr.initiator = initiator;
5093
- instr.upstreamRequestId = upstreamRequestId;
5094
5244
  let response;
5095
5245
  try {
5096
5246
  response = await createMessages(anthropicPayload, ctx, {
5097
5247
  anthropicBetaHeader,
5098
- upstreamRequestId,
5099
- initiator
5248
+ upstreamRequestId: instr.upstreamRequestId,
5249
+ initiator,
5250
+ subagentMarker,
5251
+ sessionId
5100
5252
  });
5101
5253
  } catch (error) {
5102
5254
  return await handleMessagesCreateError({
@@ -5239,6 +5391,7 @@ const handleResponses = async (c) => {
5239
5391
  const clientModel = payload.model;
5240
5392
  logger$1.debug("Responses request payload:", JSON.stringify(payload));
5241
5393
  removeWebSearchTool(payload);
5394
+ compactInputByLatestCompaction(payload);
5242
5395
  const streamRequested = Boolean(payload.stream);
5243
5396
  const { initiator: initialInitiator } = getResponsesRequestOptions(payload);
5244
5397
  const userId = payload.metadata?.user_id;
@@ -5280,13 +5433,18 @@ const handleResponses = async (c) => {
5280
5433
  model: selectedModel.id
5281
5434
  };
5282
5435
  useFunctionApplyPatch(upstreamPayload);
5436
+ applyResponsesApiContextManagement(upstreamPayload, selectedModel.capabilities.limits.max_prompt_tokens);
5437
+ compactInputByLatestCompaction(upstreamPayload);
5283
5438
  const premiumRemainingBefore = account.premiumRemaining;
5284
5439
  const premiumUnlimitedBefore = account.unlimited;
5285
5440
  const { vision, initiator } = getResponsesRequestOptions(upstreamPayload);
5286
5441
  request.initiator = initiator;
5287
5442
  if (state.manualApprove) await awaitApproval();
5288
5443
  const accountCtx = toAccountContext(account);
5289
- request.upstreamRequestId = randomUUID();
5444
+ const upstreamRequestId = generateRequestIdFromPayload({ messages: upstreamPayload.input });
5445
+ const upstreamSessionId = getUUID(upstreamRequestId);
5446
+ request.upstreamRequestId = upstreamRequestId;
5447
+ request.upstreamSessionId = upstreamSessionId;
5290
5448
  if (streamRequested) return handleStreamingResponses({
5291
5449
  c,
5292
5450
  store,
@@ -5397,7 +5555,8 @@ async function handleStreamingResponses(params) {
5397
5555
  response = await createResponses(payload, {
5398
5556
  vision,
5399
5557
  initiator,
5400
- upstreamRequestId: request.upstreamRequestId
5558
+ upstreamRequestId: request.upstreamRequestId,
5559
+ sessionId: request.upstreamSessionId
5401
5560
  }, accountCtx);
5402
5561
  } catch (error) {
5403
5562
  return handleUpstreamCreateError({
@@ -5582,7 +5741,8 @@ async function handleNonStreamingResponses(params) {
5582
5741
  const response = await createResponses(payload, {
5583
5742
  vision,
5584
5743
  initiator,
5585
- upstreamRequestId: request.upstreamRequestId
5744
+ upstreamRequestId: request.upstreamRequestId,
5745
+ sessionId: request.upstreamSessionId
5586
5746
  }, accountCtx);
5587
5747
  finishedAtMs = Date.now();
5588
5748
  const streamResponse = handleUnexpectedResponsesStream(c, response);
@@ -5745,19 +5905,10 @@ const server = new Hono();
5745
5905
  server.use(logger());
5746
5906
  server.use(cors());
5747
5907
  server.use("*", createAuthMiddleware({
5748
- allowUnauthenticatedPaths: [
5749
- "/",
5750
- "/usage-viewer",
5751
- "/usage-viewer/"
5752
- ],
5908
+ allowUnauthenticatedPaths: ["/"],
5753
5909
  allowUnauthenticatedPathPrefixes: ["/admin", "/api/admin"]
5754
5910
  }));
5755
5911
  server.get("/", (c) => c.text("Server running"));
5756
- server.get("/usage-viewer", (c) => {
5757
- const usageViewerFileUrl = new URL("../pages/index.html", import.meta.url);
5758
- return c.html(readFileSync(usageViewerFileUrl, "utf8"));
5759
- });
5760
- server.get("/usage-viewer/", (c) => c.redirect("/usage-viewer", 301));
5761
5912
  server.route("/chat/completions", completionRoutes);
5762
5913
  server.route("/models", modelRoutes);
5763
5914
  server.route("/embeddings", embeddingRoutes);
@@ -5774,4 +5925,4 @@ server.route("/v1/messages", messageRoutes);
5774
5925
 
5775
5926
  //#endregion
5776
5927
  export { server };
5777
- //# sourceMappingURL=server-B6nFgTkW.js.map
5928
+ //# sourceMappingURL=server-Cxlbm6kJ.js.map