@jeffreycao/copilot-api 1.12.2 → 1.12.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 CHANGED
@@ -589,7 +589,6 @@ stream_idle_timeout_ms = 300000
589
589
 
590
590
  [features]
591
591
  remote_compaction_v2 = true
592
- enable_request_compression = false
593
592
 
594
593
  [analytics]
595
594
  enabled = false
package/README.zh-CN.md CHANGED
@@ -593,7 +593,6 @@ stream_idle_timeout_ms = 300000
593
593
 
594
594
  [features]
595
595
  remote_compaction_v2 = true
596
- enable_request_compression = false
597
596
 
598
597
  [analytics]
599
598
  enabled = false
package/dist/main.js CHANGED
@@ -43,7 +43,7 @@ const { auth } = await import("./auth-hKS7kdCx.js");
43
43
  const { checkUsage } = await import("./check-usage-Cq76N0LH.js");
44
44
  const { debug } = await import("./debug-BiX0ewij.js");
45
45
  const { mcp } = await import("./mcp-CTb-DbQH.js");
46
- const { start } = await import("./start-DHpjos-p.js");
46
+ const { start } = await import("./start-Q4n3uMt-.js");
47
47
  await runMain(defineCommand({
48
48
  meta: {
49
49
  name: "copilot-api",
@@ -11,6 +11,7 @@ import { z } from "zod";
11
11
  import { Hono } from "hono";
12
12
  import { cors } from "hono/cors";
13
13
  import { logger } from "hono/logger";
14
+ import { decompress } from "fzstd";
14
15
  import { streamSSE } from "hono/streaming";
15
16
  import util from "node:util";
16
17
  //#region src/lib/request-auth.ts
@@ -84,6 +85,58 @@ const traceIdMiddleware = async (c, next) => {
84
85
  });
85
86
  };
86
87
  //#endregion
88
+ //#region src/lib/zstd-request.ts
89
+ const ZSTD_CONTENT_ENCODING = "zstd";
90
+ const INVALID_BODY_STATUS = 400;
91
+ let nodeZlibPromise = null;
92
+ const zstdDecompressionMiddleware = async (c, next) => {
93
+ if (c.req.header("content-encoding")?.trim().toLowerCase() !== ZSTD_CONTENT_ENCODING) return next();
94
+ try {
95
+ const decompressedBody = await decompressZstd(new Uint8Array(await c.req.raw.arrayBuffer()));
96
+ const headers = new Headers(c.req.raw.headers);
97
+ headers.delete("content-encoding");
98
+ headers.delete("content-length");
99
+ c.req.raw = new Request(c.req.raw.url, {
100
+ body: decompressedBody,
101
+ headers,
102
+ method: c.req.raw.method,
103
+ signal: c.req.raw.signal
104
+ });
105
+ c.req.bodyCache = {};
106
+ } catch {
107
+ return c.json({ error: {
108
+ message: "Failed to decompress zstd request body.",
109
+ type: "invalid_request_error"
110
+ } }, INVALID_BODY_STATUS);
111
+ }
112
+ return next();
113
+ };
114
+ const decompressZstd = async (input) => {
115
+ const bun = getBunRuntime();
116
+ if (bun?.zstdDecompress) return toUint8Array(await bun.zstdDecompress(input));
117
+ const nodeZlib = await getNodeZlib();
118
+ if (nodeZlib?.zstdDecompress) return new Promise((resolve, reject) => {
119
+ nodeZlib.zstdDecompress?.(input, (error, result) => {
120
+ if (error) {
121
+ reject(error);
122
+ return;
123
+ }
124
+ resolve(toUint8Array(result));
125
+ });
126
+ });
127
+ return decompress(input);
128
+ };
129
+ const getBunRuntime = () => globalThis.Bun;
130
+ const getNodeZlib = async () => {
131
+ nodeZlibPromise ??= import("node:zlib").then((module) => module).catch(() => null);
132
+ return nodeZlibPromise;
133
+ };
134
+ const toUint8Array = (data) => {
135
+ if (data instanceof Uint8Array) return data;
136
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
137
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
138
+ };
139
+ //#endregion
87
140
  //#region src/lib/approval.ts
88
141
  const awaitApproval = async () => {
89
142
  if (!await consola.prompt(`Accept incoming request?`, { type: "confirm" })) throw new HTTPError("Request rejected", Response.json({ message: "Request rejected" }, { status: 403 }));
@@ -4198,10 +4251,16 @@ const createCompactionContextManagement = (compactThreshold) => [{
4198
4251
  compact_threshold: compactThreshold
4199
4252
  }];
4200
4253
  const applyResponsesApiContextManagement = (payload, maxPromptTokens, compactThresholdRatio = DEFAULT_RESPONSES_COMPACT_THRESHOLD_RATIO) => {
4254
+ if (hasTerminalCompactionTrigger(payload)) return;
4201
4255
  if (payload.context_management !== void 0) return;
4202
4256
  if (!responsesUtilsDependencies.isResponsesApiContextManagementEnabled()) return;
4203
4257
  payload.context_management = createCompactionContextManagement(getModelResponsesApiCompactThreshold(payload.model) ?? resolveResponsesCompactThreshold(maxPromptTokens, compactThresholdRatio));
4204
4258
  };
4259
+ const hasTerminalCompactionTrigger = (payload) => {
4260
+ const { input } = payload;
4261
+ if (!Array.isArray(input) || input.length === 0) return false;
4262
+ return isResponseInputItemType(input.at(-1), "compaction_trigger");
4263
+ };
4205
4264
  const compactInputByLatestCompaction = (payload) => {
4206
4265
  if (!Array.isArray(payload.input) || payload.input.length === 0) return;
4207
4266
  const latestCompactionMessageIndex = getLatestCompactionMessageIndex(payload.input);
@@ -4212,7 +4271,10 @@ const getLatestCompactionMessageIndex = (input) => {
4212
4271
  for (let index = input.length - 1; index >= 0; index -= 1) if (isCompactionInputItem(input[index])) return index;
4213
4272
  };
4214
4273
  const isCompactionInputItem = (value) => {
4215
- return "type" in value && typeof value.type === "string" && value.type === "compaction";
4274
+ return isResponseInputItemType(value, "compaction");
4275
+ };
4276
+ const isResponseInputItemType = (value, type) => {
4277
+ return typeof value === "object" && value !== null && "type" in value && value.type === type;
4216
4278
  };
4217
4279
  const getPayloadItems = (payload) => {
4218
4280
  const result = [];
@@ -6167,6 +6229,7 @@ server.use("/admin/*", createAuthMiddleware({
6167
6229
  allowUnauthenticatedPaths: [],
6168
6230
  allowWhenNoApiKeys: false
6169
6231
  }));
6232
+ server.use(zstdDecompressionMiddleware);
6170
6233
  server.get("/", (c) => c.text("Server running"));
6171
6234
  server.get("/usage-viewer", (c) => {
6172
6235
  const usageViewerFileUrl = new URL("../pages/index.html", import.meta.url);
@@ -6191,4 +6254,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
6191
6254
  //#endregion
6192
6255
  export { server };
6193
6256
 
6194
- //# sourceMappingURL=server-DffuE4H8.js.map
6257
+ //# sourceMappingURL=server-DT_EVrGN.js.map