skalpel 2.0.6 → 2.0.7

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.
@@ -4,42 +4,84 @@
4
4
  import http from "http";
5
5
 
6
6
  // src/proxy/streaming.ts
7
- async function handleStreamingRequest(_req, res, _config, _source, body, forwardUrl, forwardHeaders, logger) {
8
- const HOP_BY_HOP = /* @__PURE__ */ new Set([
9
- "connection",
10
- "keep-alive",
11
- "proxy-authenticate",
12
- "proxy-authorization",
13
- "te",
14
- "trailer",
15
- "transfer-encoding",
16
- "upgrade"
17
- ]);
18
- let response;
19
- try {
20
- response = await fetch(forwardUrl, {
21
- method: "POST",
22
- headers: forwardHeaders,
23
- body
24
- });
25
- } catch (err) {
26
- logger.error(`streaming fetch failed: ${err.message}`);
7
+ var HOP_BY_HOP = /* @__PURE__ */ new Set([
8
+ "connection",
9
+ "keep-alive",
10
+ "proxy-authenticate",
11
+ "proxy-authorization",
12
+ "te",
13
+ "trailer",
14
+ "transfer-encoding",
15
+ "upgrade"
16
+ ]);
17
+ var STRIP_HEADERS = /* @__PURE__ */ new Set([
18
+ ...HOP_BY_HOP,
19
+ "content-encoding",
20
+ "content-length"
21
+ ]);
22
+ function stripSkalpelHeaders(headers) {
23
+ const cleaned = { ...headers };
24
+ delete cleaned["X-Skalpel-API-Key"];
25
+ delete cleaned["X-Skalpel-Source"];
26
+ delete cleaned["X-Skalpel-Agent-Type"];
27
+ delete cleaned["X-Skalpel-SDK-Version"];
28
+ return cleaned;
29
+ }
30
+ function isSkalpelBackendFailure(response, err) {
31
+ if (err) return true;
32
+ if (!response) return true;
33
+ if (response.status >= 500) return true;
34
+ return false;
35
+ }
36
+ async function doStreamingFetch(url, body, headers) {
37
+ return fetch(url, { method: "POST", headers, body });
38
+ }
39
+ async function handleStreamingRequest(_req, res, _config, _source, body, skalpelUrl, directUrl, useSkalpel, forwardHeaders, logger) {
40
+ let response = null;
41
+ let fetchError = null;
42
+ let usedFallback = false;
43
+ if (useSkalpel) {
44
+ try {
45
+ response = await doStreamingFetch(skalpelUrl, body, forwardHeaders);
46
+ } catch (err) {
47
+ fetchError = err;
48
+ }
49
+ if (isSkalpelBackendFailure(response, fetchError)) {
50
+ logger.warn(`streaming: Skalpel backend failed (${fetchError ? fetchError.message : `status ${response?.status}`}), falling back to direct Anthropic API`);
51
+ usedFallback = true;
52
+ response = null;
53
+ fetchError = null;
54
+ const directHeaders = stripSkalpelHeaders(forwardHeaders);
55
+ try {
56
+ response = await doStreamingFetch(directUrl, body, directHeaders);
57
+ } catch (err) {
58
+ fetchError = err;
59
+ }
60
+ }
61
+ } else {
62
+ try {
63
+ response = await doStreamingFetch(directUrl, body, forwardHeaders);
64
+ } catch (err) {
65
+ fetchError = err;
66
+ }
67
+ }
68
+ if (!response || fetchError) {
69
+ const errMsg = fetchError ? fetchError.message : "no response from upstream";
70
+ logger.error(`streaming fetch failed: ${errMsg}`);
27
71
  res.writeHead(502, {
28
72
  "Content-Type": "text/event-stream",
29
73
  "Cache-Control": "no-cache"
30
74
  });
31
75
  res.write(`event: error
32
- data: ${JSON.stringify({ error: err.message })}
76
+ data: ${JSON.stringify({ error: errMsg })}
33
77
 
34
78
  `);
35
79
  res.end();
36
80
  return;
37
81
  }
38
- const STRIP_HEADERS = /* @__PURE__ */ new Set([
39
- ...HOP_BY_HOP,
40
- "content-encoding",
41
- "content-length"
42
- ]);
82
+ if (usedFallback) {
83
+ logger.info("streaming: using direct Anthropic API fallback");
84
+ }
43
85
  if (response.status >= 300) {
44
86
  const errorBody = Buffer.from(await response.arrayBuffer());
45
87
  logger.error(`streaming upstream error: status=${response.status} body=${errorBody.toString().slice(0, 500)}`);
@@ -105,6 +147,67 @@ function shouldRouteToSkalpel(path4, source) {
105
147
  const pathname = path4.split("?")[0];
106
148
  return SKALPEL_EXACT_PATHS.has(pathname);
107
149
  }
150
+ function isSkalpelBackendFailure2(response, err) {
151
+ if (err) return true;
152
+ if (!response) return true;
153
+ if (response.status >= 500) return true;
154
+ return false;
155
+ }
156
+ function buildForwardHeaders(req, config2, source, useSkalpel) {
157
+ const forwardHeaders = {};
158
+ for (const [key, value] of Object.entries(req.headers)) {
159
+ if (value !== void 0) {
160
+ forwardHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
161
+ }
162
+ }
163
+ delete forwardHeaders["host"];
164
+ delete forwardHeaders["connection"];
165
+ if (useSkalpel) {
166
+ forwardHeaders["X-Skalpel-API-Key"] = config2.apiKey;
167
+ forwardHeaders["X-Skalpel-Source"] = source;
168
+ forwardHeaders["X-Skalpel-Agent-Type"] = source;
169
+ forwardHeaders["X-Skalpel-SDK-Version"] = "proxy-1.0.0";
170
+ if (source === "claude-code" && !forwardHeaders["x-api-key"]) {
171
+ const authHeader = forwardHeaders["authorization"] ?? "";
172
+ if (authHeader.toLowerCase().startsWith("bearer ")) {
173
+ const token = authHeader.slice(7).trim();
174
+ if (token.startsWith("sk-ant-")) {
175
+ forwardHeaders["x-api-key"] = token;
176
+ }
177
+ }
178
+ }
179
+ }
180
+ return forwardHeaders;
181
+ }
182
+ function stripSkalpelHeaders2(headers) {
183
+ const cleaned = { ...headers };
184
+ delete cleaned["X-Skalpel-API-Key"];
185
+ delete cleaned["X-Skalpel-Source"];
186
+ delete cleaned["X-Skalpel-Agent-Type"];
187
+ delete cleaned["X-Skalpel-SDK-Version"];
188
+ return cleaned;
189
+ }
190
+ var STRIP_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
191
+ "connection",
192
+ "keep-alive",
193
+ "proxy-authenticate",
194
+ "proxy-authorization",
195
+ "te",
196
+ "trailer",
197
+ "transfer-encoding",
198
+ "upgrade",
199
+ "content-encoding",
200
+ "content-length"
201
+ ]);
202
+ function extractResponseHeaders(response) {
203
+ const headers = {};
204
+ for (const [key, value] of response.headers.entries()) {
205
+ if (!STRIP_RESPONSE_HEADERS.has(key)) {
206
+ headers[key] = value;
207
+ }
208
+ }
209
+ return headers;
210
+ }
108
211
  async function handleRequest(req, res, config2, source, logger) {
109
212
  const start = Date.now();
110
213
  const method = req.method ?? "GET";
@@ -112,30 +215,7 @@ async function handleRequest(req, res, config2, source, logger) {
112
215
  try {
113
216
  const body = await collectBody(req);
114
217
  const useSkalpel = shouldRouteToSkalpel(path4, source);
115
- const forwardUrl = `${useSkalpel ? config2.remoteBaseUrl : config2.anthropicDirectUrl}${path4}`;
116
- const forwardHeaders = {};
117
- for (const [key, value] of Object.entries(req.headers)) {
118
- if (value !== void 0) {
119
- forwardHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
120
- }
121
- }
122
- delete forwardHeaders["host"];
123
- delete forwardHeaders["connection"];
124
- if (useSkalpel) {
125
- forwardHeaders["X-Skalpel-API-Key"] = config2.apiKey;
126
- forwardHeaders["X-Skalpel-Source"] = source;
127
- forwardHeaders["X-Skalpel-Agent-Type"] = source;
128
- forwardHeaders["X-Skalpel-SDK-Version"] = "proxy-1.0.0";
129
- if (source === "claude-code" && !forwardHeaders["x-api-key"]) {
130
- const authHeader = forwardHeaders["authorization"] ?? "";
131
- if (authHeader.toLowerCase().startsWith("bearer ")) {
132
- const token = authHeader.slice(7).trim();
133
- if (token.startsWith("sk-ant-")) {
134
- forwardHeaders["x-api-key"] = token;
135
- }
136
- }
137
- }
138
- }
218
+ const forwardHeaders = buildForwardHeaders(req, config2, source, useSkalpel);
139
219
  let isStreaming = false;
140
220
  if (body) {
141
221
  try {
@@ -145,38 +225,52 @@ async function handleRequest(req, res, config2, source, logger) {
145
225
  }
146
226
  }
147
227
  if (isStreaming) {
148
- await handleStreamingRequest(req, res, config2, source, body, forwardUrl, forwardHeaders, logger);
228
+ const skalpelUrl2 = `${config2.remoteBaseUrl}${path4}`;
229
+ const directUrl2 = `${config2.anthropicDirectUrl}${path4}`;
230
+ await handleStreamingRequest(req, res, config2, source, body, skalpelUrl2, directUrl2, useSkalpel, forwardHeaders, logger);
149
231
  logger.info(`${method} ${path4} source=${source} streaming latency=${Date.now() - start}ms`);
150
232
  return;
151
233
  }
152
- const response = await fetch(forwardUrl, {
153
- method,
154
- headers: forwardHeaders,
155
- body: method !== "GET" && method !== "HEAD" ? body : void 0
156
- });
157
- const STRIP_HEADERS = /* @__PURE__ */ new Set([
158
- "connection",
159
- "keep-alive",
160
- "proxy-authenticate",
161
- "proxy-authorization",
162
- "te",
163
- "trailer",
164
- "transfer-encoding",
165
- "upgrade",
166
- "content-encoding",
167
- "content-length"
168
- ]);
169
- const responseHeaders = {};
170
- for (const [key, value] of response.headers.entries()) {
171
- if (!STRIP_HEADERS.has(key)) {
172
- responseHeaders[key] = value;
234
+ const skalpelUrl = `${config2.remoteBaseUrl}${path4}`;
235
+ const directUrl = `${config2.anthropicDirectUrl}${path4}`;
236
+ const fetchBody = method !== "GET" && method !== "HEAD" ? body : void 0;
237
+ let response = null;
238
+ let fetchError = null;
239
+ let usedFallback = false;
240
+ if (useSkalpel) {
241
+ try {
242
+ response = await fetch(skalpelUrl, { method, headers: forwardHeaders, body: fetchBody });
243
+ } catch (err) {
244
+ fetchError = err;
245
+ }
246
+ if (isSkalpelBackendFailure2(response, fetchError)) {
247
+ logger.warn(`${method} ${path4} Skalpel backend failed (${fetchError ? fetchError.message : `status ${response?.status}`}), falling back to direct Anthropic API`);
248
+ usedFallback = true;
249
+ response = null;
250
+ fetchError = null;
251
+ const directHeaders = stripSkalpelHeaders2(forwardHeaders);
252
+ try {
253
+ response = await fetch(directUrl, { method, headers: directHeaders, body: fetchBody });
254
+ } catch (err) {
255
+ fetchError = err;
256
+ }
257
+ }
258
+ } else {
259
+ try {
260
+ response = await fetch(directUrl, { method, headers: forwardHeaders, body: fetchBody });
261
+ } catch (err) {
262
+ fetchError = err;
173
263
  }
174
264
  }
265
+ if (!response || fetchError) {
266
+ throw fetchError ?? new Error("no response from upstream");
267
+ }
268
+ const responseHeaders = extractResponseHeaders(response);
175
269
  const responseBody = Buffer.from(await response.arrayBuffer());
176
270
  responseHeaders["content-length"] = String(responseBody.length);
177
271
  res.writeHead(response.status, responseHeaders);
178
272
  res.end(responseBody);
179
- logger.info(`${method} ${path4} source=${source} status=${response.status} latency=${Date.now() - start}ms`);
273
+ logger.info(`${method} ${path4} source=${source} status=${response.status}${usedFallback ? " (fallback)" : ""} latency=${Date.now() - start}ms`);
180
274
  } catch (err) {
181
275
  logger.error(`${method} ${path4} source=${source} error=${err.message}`);
182
276
  if (!res.headersSent) {
@@ -292,6 +386,24 @@ function startProxy(config2) {
292
386
  }
293
387
  handleRequest(req, res, config2, "codex", logger);
294
388
  });
389
+ anthropicServer.on("error", (err) => {
390
+ if (err.code === "EADDRINUSE") {
391
+ logger.error(`Port ${config2.anthropicPort} is already in use. Another Skalpel proxy or process may be running.`);
392
+ } else {
393
+ logger.error(`Anthropic proxy failed to bind port ${config2.anthropicPort}: ${err.message}`);
394
+ }
395
+ removePid(config2.pidFile);
396
+ process.exit(1);
397
+ });
398
+ openaiServer.on("error", (err) => {
399
+ if (err.code === "EADDRINUSE") {
400
+ logger.error(`Port ${config2.openaiPort} is already in use. Another Skalpel proxy or process may be running.`);
401
+ } else {
402
+ logger.error(`OpenAI proxy failed to bind port ${config2.openaiPort}: ${err.message}`);
403
+ }
404
+ removePid(config2.pidFile);
405
+ process.exit(1);
406
+ });
295
407
  anthropicServer.listen(config2.anthropicPort, () => {
296
408
  logger.info(`Anthropic proxy listening on port ${config2.anthropicPort}`);
297
409
  });
@@ -309,6 +421,16 @@ function startProxy(config2) {
309
421
  };
310
422
  process.on("SIGTERM", cleanup);
311
423
  process.on("SIGINT", cleanup);
424
+ process.on("uncaughtException", (err) => {
425
+ logger.error(`Uncaught exception: ${err.message}`);
426
+ removePid(config2.pidFile);
427
+ process.exit(1);
428
+ });
429
+ process.on("unhandledRejection", (reason) => {
430
+ logger.error(`Unhandled rejection: ${reason}`);
431
+ removePid(config2.pidFile);
432
+ process.exit(1);
433
+ });
312
434
  return { anthropicServer, openaiServer };
313
435
  }
314
436
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/proxy/server.ts","../../src/proxy/streaming.ts","../../src/proxy/handler.ts","../../src/proxy/health.ts","../../src/proxy/pid.ts","../../src/proxy/logger.ts","../../src/proxy/config.ts","../../src/cli/proxy-runner.ts"],"sourcesContent":["import http from 'node:http';\nimport type { ProxyConfig, ProxyStatus } from './types.js';\nimport { handleRequest } from './handler.js';\nimport { handleHealthRequest } from './health.js';\nimport { writePid, readPid, removePid } from './pid.js';\nimport { Logger } from './logger.js';\n\nlet proxyStartTime = 0;\n\nexport function startProxy(config: ProxyConfig): { anthropicServer: http.Server; openaiServer: http.Server } {\n const logger = new Logger(config.logFile, config.logLevel);\n const startTime = Date.now();\n proxyStartTime = Date.now();\n\n const anthropicServer = http.createServer((req, res) => {\n if (req.url === '/health' && req.method === 'GET') {\n handleHealthRequest(res, config, startTime);\n return;\n }\n handleRequest(req, res, config, 'claude-code', logger);\n });\n\n const openaiServer = http.createServer((req, res) => {\n if (req.url === '/health' && req.method === 'GET') {\n handleHealthRequest(res, config, startTime);\n return;\n }\n handleRequest(req, res, config, 'codex', logger);\n });\n\n anthropicServer.listen(config.anthropicPort, () => {\n logger.info(`Anthropic proxy listening on port ${config.anthropicPort}`);\n });\n\n openaiServer.listen(config.openaiPort, () => {\n logger.info(`OpenAI proxy listening on port ${config.openaiPort}`);\n });\n\n writePid(config.pidFile);\n logger.info(`Proxy started (pid=${process.pid}) ports=${config.anthropicPort},${config.openaiPort}`);\n\n const cleanup = () => {\n logger.info('Shutting down proxy...');\n anthropicServer.close();\n openaiServer.close();\n removePid(config.pidFile);\n process.exit(0);\n };\n\n process.on('SIGTERM', cleanup);\n process.on('SIGINT', cleanup);\n\n return { anthropicServer, openaiServer };\n}\n\nexport function stopProxy(config: ProxyConfig): boolean {\n const pid = readPid(config.pidFile);\n if (pid === null) return false;\n\n try {\n process.kill(pid, 'SIGTERM');\n } catch {\n // Process already gone\n }\n\n removePid(config.pidFile);\n return true;\n}\n\nexport function getProxyStatus(config: ProxyConfig): ProxyStatus {\n const pid = readPid(config.pidFile);\n return {\n running: pid !== null,\n pid,\n uptime: proxyStartTime > 0 ? Date.now() - proxyStartTime : 0,\n anthropicPort: config.anthropicPort,\n openaiPort: config.openaiPort,\n };\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\nimport type { Logger } from './logger.js';\n\nexport async function handleStreamingRequest(\n _req: IncomingMessage,\n res: ServerResponse,\n _config: ProxyConfig,\n _source: string,\n body: string,\n forwardUrl: string,\n forwardHeaders: Record<string, string>,\n logger: Logger,\n): Promise<void> {\n const HOP_BY_HOP = new Set([\n 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',\n 'te', 'trailer', 'transfer-encoding', 'upgrade',\n ]);\n\n let response: Response;\n try {\n response = await fetch(forwardUrl, {\n method: 'POST',\n headers: forwardHeaders,\n body,\n });\n } catch (err) {\n logger.error(`streaming fetch failed: ${(err as Error).message}`);\n res.writeHead(502, {\n 'Content-Type': 'text/event-stream',\n 'Cache-Control': 'no-cache',\n });\n res.write(`event: error\\ndata: ${JSON.stringify({ error: (err as Error).message })}\\n\\n`);\n res.end();\n return;\n }\n\n // Headers to strip: hop-by-hop + content-encoding/content-length (fetch\n // auto-decompresses, so forwarding the original encoding header causes\n // the client to try decompressing already-decompressed data → ZlibError)\n const STRIP_HEADERS = new Set([\n ...HOP_BY_HOP,\n 'content-encoding', 'content-length',\n ]);\n\n // For non-2xx responses, pass through as-is so the SDK can parse error bodies correctly\n if (response.status >= 300) {\n const errorBody = Buffer.from(await response.arrayBuffer());\n logger.error(`streaming upstream error: status=${response.status} body=${errorBody.toString().slice(0, 500)}`);\n const passthroughHeaders: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_HEADERS.has(key)) {\n passthroughHeaders[key] = value;\n }\n }\n passthroughHeaders['content-length'] = String(errorBody.length);\n res.writeHead(response.status, passthroughHeaders);\n res.end(errorBody);\n return;\n }\n\n // Build SSE headers, stripping hop-by-hop/encoding and normalizing content-type\n const sseHeaders: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_HEADERS.has(key) && key !== 'content-type') {\n sseHeaders[key] = value;\n }\n }\n sseHeaders['Content-Type'] = 'text/event-stream';\n sseHeaders['Cache-Control'] = 'no-cache';\n res.writeHead(response.status, sseHeaders);\n\n if (!response.body) {\n res.write(`event: error\\ndata: ${JSON.stringify({ error: 'no response body' })}\\n\\n`);\n res.end();\n return;\n }\n\n try {\n const reader = (response.body as ReadableStream<Uint8Array>).getReader();\n const decoder = new TextDecoder();\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const chunk = decoder.decode(value, { stream: true });\n res.write(chunk);\n }\n } catch (err) {\n logger.error(`streaming error: ${(err as Error).message}`);\n res.write(`event: error\\ndata: ${JSON.stringify({ error: (err as Error).message })}\\n\\n`);\n }\n\n res.end();\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\nimport { handleStreamingRequest } from './streaming.js';\nimport type { Logger } from './logger.js';\n\n// Exact paths that should route through Skalpel optimization for Claude Code.\n// Sub-paths like /v1/messages/count_tokens go direct to Anthropic.\nconst SKALPEL_EXACT_PATHS = new Set(['/v1/messages']);\n\nfunction collectBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = [];\n req.on('data', (chunk: Buffer) => chunks.push(chunk));\n req.on('end', () => resolve(Buffer.concat(chunks).toString()));\n req.on('error', reject);\n });\n}\n\nexport function shouldRouteToSkalpel(path: string, source: string): boolean {\n if (source !== 'claude-code') return true;\n // Strip query string — Claude Code sends /v1/messages?beta=true\n const pathname = path.split('?')[0];\n return SKALPEL_EXACT_PATHS.has(pathname);\n}\n\nexport async function handleRequest(\n req: IncomingMessage,\n res: ServerResponse,\n config: ProxyConfig,\n source: string,\n logger: Logger,\n): Promise<void> {\n const start = Date.now();\n const method = req.method ?? 'GET';\n const path = req.url ?? '/';\n\n try {\n const body = await collectBody(req);\n const useSkalpel = shouldRouteToSkalpel(path, source);\n const forwardUrl = `${useSkalpel ? config.remoteBaseUrl : config.anthropicDirectUrl}${path}`;\n\n const forwardHeaders: Record<string, string> = {};\n for (const [key, value] of Object.entries(req.headers)) {\n if (value !== undefined) {\n forwardHeaders[key] = Array.isArray(value) ? value.join(', ') : value;\n }\n }\n delete forwardHeaders['host'];\n delete forwardHeaders['connection'];\n\n if (useSkalpel) {\n forwardHeaders['X-Skalpel-API-Key'] = config.apiKey;\n forwardHeaders['X-Skalpel-Source'] = source;\n forwardHeaders['X-Skalpel-Agent-Type'] = source;\n forwardHeaders['X-Skalpel-SDK-Version'] = 'proxy-1.0.0';\n\n // By default, requests go through Skalpel's optimization pipeline.\n // The backend resolves provider credentials from its KeyVault/DB.\n // Passthrough mode (skipping optimization) can be enabled per-request\n // by setting the X-Skalpel-Auth-Mode header on the client side.\n\n // Claude Code may send either x-api-key (API key auth) or\n // Authorization: Bearer (OAuth auth). Only convert Bearer to x-api-key\n // for actual API keys (sk-ant-*). OAuth tokens must stay as\n // Authorization: Bearer — Anthropic rejects them in x-api-key.\n if (source === 'claude-code' && !forwardHeaders['x-api-key']) {\n const authHeader = forwardHeaders['authorization'] ?? '';\n if (authHeader.toLowerCase().startsWith('bearer ')) {\n const token = authHeader.slice(7).trim();\n if (token.startsWith('sk-ant-')) {\n forwardHeaders['x-api-key'] = token;\n }\n // else: OAuth token — leave Authorization: Bearer intact\n }\n }\n }\n\n let isStreaming = false;\n if (body) {\n try {\n const parsed = JSON.parse(body);\n isStreaming = parsed.stream === true;\n } catch {\n // Not JSON — treat as non-streaming\n }\n }\n\n if (isStreaming) {\n await handleStreamingRequest(req, res, config, source, body, forwardUrl, forwardHeaders, logger);\n logger.info(`${method} ${path} source=${source} streaming latency=${Date.now() - start}ms`);\n return;\n }\n\n const response = await fetch(forwardUrl, {\n method,\n headers: forwardHeaders,\n body: method !== 'GET' && method !== 'HEAD' ? body : undefined,\n });\n\n // Strip hop-by-hop headers that should not be forwarded by a proxy.\n // Also strip content-encoding and content-length because fetch()\n // automatically decompresses gzip/deflate/br responses — the body we\n // read via arrayBuffer() is already decompressed, so forwarding the\n // original content-encoding header causes the client (Claude Code) to\n // try to decompress plain text → ZlibError.\n const STRIP_HEADERS = new Set([\n 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',\n 'te', 'trailer', 'transfer-encoding', 'upgrade',\n 'content-encoding', 'content-length',\n ]);\n const responseHeaders: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_HEADERS.has(key)) {\n responseHeaders[key] = value;\n }\n }\n\n const responseBody = Buffer.from(await response.arrayBuffer());\n responseHeaders['content-length'] = String(responseBody.length);\n res.writeHead(response.status, responseHeaders);\n res.end(responseBody);\n\n logger.info(`${method} ${path} source=${source} status=${response.status} latency=${Date.now() - start}ms`);\n } catch (err) {\n logger.error(`${method} ${path} source=${source} error=${(err as Error).message}`);\n if (!res.headersSent) {\n res.writeHead(502, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ error: 'proxy_error', message: (err as Error).message }));\n }\n }\n}\n","import type { ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\n\nexport function handleHealthRequest(\n res: ServerResponse,\n config: ProxyConfig,\n startTime: number,\n): void {\n const body = JSON.stringify({\n status: 'ok',\n uptime: Date.now() - startTime,\n ports: {\n anthropic: config.anthropicPort,\n openai: config.openaiPort,\n },\n version: 'proxy-1.0.0',\n });\n\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(body);\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\n\nexport function writePid(pidFile: string): void {\n fs.mkdirSync(path.dirname(pidFile), { recursive: true });\n fs.writeFileSync(pidFile, String(process.pid));\n}\n\nexport function readPid(pidFile: string): number | null {\n try {\n const raw = fs.readFileSync(pidFile, 'utf-8').trim();\n const pid = parseInt(raw, 10);\n if (isNaN(pid)) return null;\n return isRunning(pid) ? pid : null;\n } catch {\n return null;\n }\n}\n\nexport function isRunning(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function removePid(pidFile: string): void {\n try {\n fs.unlinkSync(pidFile);\n } catch {\n // Already removed\n }\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\n\nconst MAX_SIZE = 5 * 1024 * 1024; // 5MB\nconst MAX_ROTATIONS = 3;\n\nconst LEVELS = { debug: 0, info: 1, warn: 2, error: 3 } as const;\n\nexport class Logger {\n private logFile: string;\n private level: keyof typeof LEVELS;\n\n constructor(logFile: string, level: keyof typeof LEVELS = 'info') {\n this.logFile = logFile;\n this.level = level;\n fs.mkdirSync(path.dirname(logFile), { recursive: true });\n }\n\n debug(msg: string): void { this.log('debug', msg); }\n info(msg: string): void { this.log('info', msg); }\n warn(msg: string): void { this.log('warn', msg); }\n error(msg: string): void { this.log('error', msg); }\n\n private log(level: keyof typeof LEVELS, msg: string): void {\n if (LEVELS[level] < LEVELS[this.level]) return;\n\n const line = `${new Date().toISOString()} [${level.toUpperCase()}] ${msg}\\n`;\n\n if (level === 'debug' || level === 'error') {\n process.stderr.write(line);\n }\n\n try {\n this.rotate();\n fs.appendFileSync(this.logFile, line);\n } catch {\n // Best-effort logging\n }\n }\n\n private rotate(): void {\n try {\n const stat = fs.statSync(this.logFile);\n if (stat.size < MAX_SIZE) return;\n } catch {\n return;\n }\n\n for (let i = MAX_ROTATIONS; i >= 1; i--) {\n const src = i === 1 ? this.logFile : `${this.logFile}.${i - 1}`;\n const dst = `${this.logFile}.${i}`;\n try {\n fs.renameSync(src, dst);\n } catch {\n // File may not exist\n }\n }\n }\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport os from 'node:os';\nimport type { ProxyConfig } from './types.js';\n\nfunction expandHome(filePath: string): string {\n if (filePath.startsWith('~')) {\n return path.join(os.homedir(), filePath.slice(1));\n }\n return filePath;\n}\n\nconst DEFAULTS: ProxyConfig = {\n apiKey: '',\n remoteBaseUrl: 'https://api.skalpel.ai',\n anthropicDirectUrl: 'https://api.anthropic.com',\n anthropicPort: 18100,\n openaiPort: 18101,\n logLevel: 'info',\n logFile: '~/.skalpel/logs/proxy.log',\n pidFile: '~/.skalpel/proxy.pid',\n configFile: '~/.skalpel/config.json',\n};\n\nexport function loadConfig(configPath?: string): ProxyConfig {\n const filePath = expandHome(configPath ?? DEFAULTS.configFile);\n let fileConfig: Partial<ProxyConfig> = {};\n\n try {\n const raw = fs.readFileSync(filePath, 'utf-8');\n fileConfig = JSON.parse(raw) as Partial<ProxyConfig>;\n } catch {\n // Config file doesn't exist or is invalid — use defaults\n }\n\n return {\n apiKey: fileConfig.apiKey ?? DEFAULTS.apiKey,\n remoteBaseUrl: fileConfig.remoteBaseUrl ?? DEFAULTS.remoteBaseUrl,\n anthropicDirectUrl: fileConfig.anthropicDirectUrl ?? DEFAULTS.anthropicDirectUrl,\n anthropicPort: fileConfig.anthropicPort ?? DEFAULTS.anthropicPort,\n openaiPort: fileConfig.openaiPort ?? DEFAULTS.openaiPort,\n logLevel: fileConfig.logLevel ?? DEFAULTS.logLevel,\n logFile: expandHome(fileConfig.logFile ?? DEFAULTS.logFile),\n pidFile: expandHome(fileConfig.pidFile ?? DEFAULTS.pidFile),\n configFile: filePath,\n };\n}\n\nexport function saveConfig(config: ProxyConfig): void {\n const dir = path.dirname(config.configFile);\n fs.mkdirSync(dir, { recursive: true });\n fs.writeFileSync(config.configFile, JSON.stringify(config, null, 2) + '\\n');\n}\n","import { startProxy } from '../proxy/server.js';\nimport { loadConfig } from '../proxy/config.js';\n\nconst config = loadConfig();\nstartProxy(config);\n"],"mappings":";;;AAAA,OAAO,UAAU;;;ACIjB,eAAsB,uBACpB,MACA,KACA,SACA,SACA,MACA,YACA,gBACA,QACe;AACf,QAAM,aAAa,oBAAI,IAAI;AAAA,IACzB;AAAA,IAAc;AAAA,IAAc;AAAA,IAAsB;AAAA,IAClD;AAAA,IAAM;AAAA,IAAW;AAAA,IAAqB;AAAA,EACxC,CAAC;AAED,MAAI;AACJ,MAAI;AACF,e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IACL,QAAQ,WAAW,UAAU,SAAS;AAAA,IACtC,eAAe,WAAW,iBAAiB,SAAS;AAAA,IACpD,oBAAoB,WAAW,sBAAsB,SAAS;AAAA,IAC9D,eAAe,WAAW,iBAAiB,SAAS;AAAA,IACpD,YAAY,WAAW,cAAc,SAAS;AAAA,IAC9C,UAAU,WAAW,YAAY,SAAS;AAAA,IAC1C,SAAS,WAAW,WAAW,WAAW,SAAS,OAAO;AAAA,IAC1D,SAAS,WAAW,WAAW,WAAW,SAAS,OAAO;AAAA,IAC1D,YAAY;AAAA,EACd;AACF;;;AC3CA,IAAM,SAAS,WAAW;AAC1B,WAAW,MAAM;","names":["path","config","config","fs","path","config","fs","path"]}
1
+ {"version":3,"sources":["../../src/proxy/server.ts","../../src/proxy/streaming.ts","../../src/proxy/handler.ts","../../src/proxy/health.ts","../../src/proxy/pid.ts","../../src/proxy/logger.ts","../../src/proxy/config.ts","../../src/cli/proxy-runner.ts"],"sourcesContent":["import http from 'node:http';\nimport type { ProxyConfig, ProxyStatus } from './types.js';\nimport { handleRequest } from './handler.js';\nimport { handleHealthRequest } from './health.js';\nimport { writePid, readPid, removePid } from './pid.js';\nimport { Logger } from './logger.js';\n\nlet proxyStartTime = 0;\n\nexport function startProxy(config: ProxyConfig): { anthropicServer: http.Server; openaiServer: http.Server } {\n const logger = new Logger(config.logFile, config.logLevel);\n const startTime = Date.now();\n proxyStartTime = Date.now();\n\n const anthropicServer = http.createServer((req, res) => {\n if (req.url === '/health' && req.method === 'GET') {\n handleHealthRequest(res, config, startTime);\n return;\n }\n handleRequest(req, res, config, 'claude-code', logger);\n });\n\n const openaiServer = http.createServer((req, res) => {\n if (req.url === '/health' && req.method === 'GET') {\n handleHealthRequest(res, config, startTime);\n return;\n }\n handleRequest(req, res, config, 'codex', logger);\n });\n\n // Handle port binding errors (EADDRINUSE, EACCES, etc.)\n anthropicServer.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'EADDRINUSE') {\n logger.error(`Port ${config.anthropicPort} is already in use. Another Skalpel proxy or process may be running.`);\n } else {\n logger.error(`Anthropic proxy failed to bind port ${config.anthropicPort}: ${err.message}`);\n }\n removePid(config.pidFile);\n process.exit(1);\n });\n\n openaiServer.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'EADDRINUSE') {\n logger.error(`Port ${config.openaiPort} is already in use. Another Skalpel proxy or process may be running.`);\n } else {\n logger.error(`OpenAI proxy failed to bind port ${config.openaiPort}: ${err.message}`);\n }\n removePid(config.pidFile);\n process.exit(1);\n });\n\n anthropicServer.listen(config.anthropicPort, () => {\n logger.info(`Anthropic proxy listening on port ${config.anthropicPort}`);\n });\n\n openaiServer.listen(config.openaiPort, () => {\n logger.info(`OpenAI proxy listening on port ${config.openaiPort}`);\n });\n\n writePid(config.pidFile);\n logger.info(`Proxy started (pid=${process.pid}) ports=${config.anthropicPort},${config.openaiPort}`);\n\n const cleanup = () => {\n logger.info('Shutting down proxy...');\n anthropicServer.close();\n openaiServer.close();\n removePid(config.pidFile);\n process.exit(0);\n };\n\n process.on('SIGTERM', cleanup);\n process.on('SIGINT', cleanup);\n\n // Catch unexpected errors so PID file is always cleaned up\n process.on('uncaughtException', (err) => {\n logger.error(`Uncaught exception: ${err.message}`);\n removePid(config.pidFile);\n process.exit(1);\n });\n\n process.on('unhandledRejection', (reason) => {\n logger.error(`Unhandled rejection: ${reason}`);\n removePid(config.pidFile);\n process.exit(1);\n });\n\n return { anthropicServer, openaiServer };\n}\n\nexport function stopProxy(config: ProxyConfig): boolean {\n const pid = readPid(config.pidFile);\n if (pid === null) return false;\n\n try {\n process.kill(pid, 'SIGTERM');\n } catch {\n // Process already gone\n }\n\n removePid(config.pidFile);\n return true;\n}\n\nexport function getProxyStatus(config: ProxyConfig): ProxyStatus {\n const pid = readPid(config.pidFile);\n return {\n running: pid !== null,\n pid,\n uptime: proxyStartTime > 0 ? Date.now() - proxyStartTime : 0,\n anthropicPort: config.anthropicPort,\n openaiPort: config.openaiPort,\n };\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\nimport type { Logger } from './logger.js';\n\nconst HOP_BY_HOP = new Set([\n 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',\n 'te', 'trailer', 'transfer-encoding', 'upgrade',\n]);\n\nconst STRIP_HEADERS = new Set([\n ...HOP_BY_HOP,\n 'content-encoding', 'content-length',\n]);\n\n/** Remove Skalpel-specific headers so a direct-to-Anthropic fallback request is clean. */\nfunction stripSkalpelHeaders(headers: Record<string, string>): Record<string, string> {\n const cleaned = { ...headers };\n delete cleaned['X-Skalpel-API-Key'];\n delete cleaned['X-Skalpel-Source'];\n delete cleaned['X-Skalpel-Agent-Type'];\n delete cleaned['X-Skalpel-SDK-Version'];\n return cleaned;\n}\n\n/** Returns true if the error or HTTP status indicates the Skalpel backend\n * itself is unreachable or broken (not a normal API error from Anthropic). */\nfunction isSkalpelBackendFailure(response: Response | null, err: unknown): boolean {\n if (err) return true;\n if (!response) return true;\n if (response.status >= 500) return true;\n return false;\n}\n\nasync function doStreamingFetch(\n url: string,\n body: string,\n headers: Record<string, string>,\n): Promise<Response> {\n return fetch(url, { method: 'POST', headers, body });\n}\n\nexport async function handleStreamingRequest(\n _req: IncomingMessage,\n res: ServerResponse,\n _config: ProxyConfig,\n _source: string,\n body: string,\n skalpelUrl: string,\n directUrl: string,\n useSkalpel: boolean,\n forwardHeaders: Record<string, string>,\n logger: Logger,\n): Promise<void> {\n let response: Response | null = null;\n let fetchError: unknown = null;\n let usedFallback = false;\n\n // Try Skalpel backend first (if this request should be optimized)\n if (useSkalpel) {\n try {\n response = await doStreamingFetch(skalpelUrl, body, forwardHeaders);\n } catch (err) {\n fetchError = err;\n }\n\n // If Skalpel backend is down, fall back to direct Anthropic API\n if (isSkalpelBackendFailure(response, fetchError)) {\n logger.warn(`streaming: Skalpel backend failed (${fetchError ? (fetchError as Error).message : `status ${response?.status}`}), falling back to direct Anthropic API`);\n usedFallback = true;\n response = null;\n fetchError = null;\n const directHeaders = stripSkalpelHeaders(forwardHeaders);\n try {\n response = await doStreamingFetch(directUrl, body, directHeaders);\n } catch (err) {\n fetchError = err;\n }\n }\n } else {\n // Non-Skalpel path — go direct\n try {\n response = await doStreamingFetch(directUrl, body, forwardHeaders);\n } catch (err) {\n fetchError = err;\n }\n }\n\n // If even the direct request failed, return error\n if (!response || fetchError) {\n const errMsg = fetchError ? (fetchError as Error).message : 'no response from upstream';\n logger.error(`streaming fetch failed: ${errMsg}`);\n res.writeHead(502, {\n 'Content-Type': 'text/event-stream',\n 'Cache-Control': 'no-cache',\n });\n res.write(`event: error\\ndata: ${JSON.stringify({ error: errMsg })}\\n\\n`);\n res.end();\n return;\n }\n\n if (usedFallback) {\n logger.info('streaming: using direct Anthropic API fallback');\n }\n\n // For non-2xx responses, pass through as-is so the SDK can parse error bodies correctly\n if (response.status >= 300) {\n const errorBody = Buffer.from(await response.arrayBuffer());\n logger.error(`streaming upstream error: status=${response.status} body=${errorBody.toString().slice(0, 500)}`);\n const passthroughHeaders: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_HEADERS.has(key)) {\n passthroughHeaders[key] = value;\n }\n }\n passthroughHeaders['content-length'] = String(errorBody.length);\n res.writeHead(response.status, passthroughHeaders);\n res.end(errorBody);\n return;\n }\n\n // Build SSE headers, stripping hop-by-hop/encoding and normalizing content-type\n const sseHeaders: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_HEADERS.has(key) && key !== 'content-type') {\n sseHeaders[key] = value;\n }\n }\n sseHeaders['Content-Type'] = 'text/event-stream';\n sseHeaders['Cache-Control'] = 'no-cache';\n res.writeHead(response.status, sseHeaders);\n\n if (!response.body) {\n res.write(`event: error\\ndata: ${JSON.stringify({ error: 'no response body' })}\\n\\n`);\n res.end();\n return;\n }\n\n try {\n const reader = (response.body as ReadableStream<Uint8Array>).getReader();\n const decoder = new TextDecoder();\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const chunk = decoder.decode(value, { stream: true });\n res.write(chunk);\n }\n } catch (err) {\n logger.error(`streaming error: ${(err as Error).message}`);\n res.write(`event: error\\ndata: ${JSON.stringify({ error: (err as Error).message })}\\n\\n`);\n }\n\n res.end();\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\nimport { handleStreamingRequest } from './streaming.js';\nimport type { Logger } from './logger.js';\n\n// Exact paths that should route through Skalpel optimization for Claude Code.\n// Sub-paths like /v1/messages/count_tokens go direct to Anthropic.\nconst SKALPEL_EXACT_PATHS = new Set(['/v1/messages']);\n\nfunction collectBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = [];\n req.on('data', (chunk: Buffer) => chunks.push(chunk));\n req.on('end', () => resolve(Buffer.concat(chunks).toString()));\n req.on('error', reject);\n });\n}\n\nexport function shouldRouteToSkalpel(path: string, source: string): boolean {\n if (source !== 'claude-code') return true;\n // Strip query string — Claude Code sends /v1/messages?beta=true\n const pathname = path.split('?')[0];\n return SKALPEL_EXACT_PATHS.has(pathname);\n}\n\n/** Returns true if the error or HTTP status indicates the Skalpel backend\n * itself is unreachable or broken (not a normal API error from Anthropic). */\nfunction isSkalpelBackendFailure(response: Response | null, err: unknown): boolean {\n // Network-level failure (DNS, connection refused, timeout, etc.)\n if (err) return true;\n if (!response) return true;\n // 5xx from the Skalpel backend means the backend is having issues\n if (response.status >= 500) return true;\n return false;\n}\n\n/** Build the set of headers to forward, adding Skalpel-specific headers when routing through Skalpel. */\nexport function buildForwardHeaders(\n req: IncomingMessage,\n config: ProxyConfig,\n source: string,\n useSkalpel: boolean,\n): Record<string, string> {\n const forwardHeaders: Record<string, string> = {};\n for (const [key, value] of Object.entries(req.headers)) {\n if (value !== undefined) {\n forwardHeaders[key] = Array.isArray(value) ? value.join(', ') : value;\n }\n }\n delete forwardHeaders['host'];\n delete forwardHeaders['connection'];\n\n if (useSkalpel) {\n forwardHeaders['X-Skalpel-API-Key'] = config.apiKey;\n forwardHeaders['X-Skalpel-Source'] = source;\n forwardHeaders['X-Skalpel-Agent-Type'] = source;\n forwardHeaders['X-Skalpel-SDK-Version'] = 'proxy-1.0.0';\n\n // Claude Code may send either x-api-key (API key auth) or\n // Authorization: Bearer (OAuth auth). Only convert Bearer to x-api-key\n // for actual API keys (sk-ant-*). OAuth tokens must stay as\n // Authorization: Bearer — Anthropic rejects them in x-api-key.\n if (source === 'claude-code' && !forwardHeaders['x-api-key']) {\n const authHeader = forwardHeaders['authorization'] ?? '';\n if (authHeader.toLowerCase().startsWith('bearer ')) {\n const token = authHeader.slice(7).trim();\n if (token.startsWith('sk-ant-')) {\n forwardHeaders['x-api-key'] = token;\n }\n }\n }\n }\n\n return forwardHeaders;\n}\n\n/** Remove Skalpel-specific headers so a direct-to-Anthropic fallback request is clean. */\nfunction stripSkalpelHeaders(headers: Record<string, string>): Record<string, string> {\n const cleaned = { ...headers };\n delete cleaned['X-Skalpel-API-Key'];\n delete cleaned['X-Skalpel-Source'];\n delete cleaned['X-Skalpel-Agent-Type'];\n delete cleaned['X-Skalpel-SDK-Version'];\n return cleaned;\n}\n\n// Headers that should not be forwarded by a proxy.\n// Also strips content-encoding and content-length because fetch()\n// automatically decompresses gzip/deflate/br responses — the body we\n// read via arrayBuffer() is already decompressed, so forwarding the\n// original content-encoding header causes the client to try to decompress\n// plain text → ZlibError.\nconst STRIP_RESPONSE_HEADERS = new Set([\n 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',\n 'te', 'trailer', 'transfer-encoding', 'upgrade',\n 'content-encoding', 'content-length',\n]);\n\nfunction extractResponseHeaders(response: Response): Record<string, string> {\n const headers: Record<string, string> = {};\n for (const [key, value] of response.headers.entries()) {\n if (!STRIP_RESPONSE_HEADERS.has(key)) {\n headers[key] = value;\n }\n }\n return headers;\n}\n\nexport async function handleRequest(\n req: IncomingMessage,\n res: ServerResponse,\n config: ProxyConfig,\n source: string,\n logger: Logger,\n): Promise<void> {\n const start = Date.now();\n const method = req.method ?? 'GET';\n const path = req.url ?? '/';\n\n try {\n const body = await collectBody(req);\n const useSkalpel = shouldRouteToSkalpel(path, source);\n const forwardHeaders = buildForwardHeaders(req, config, source, useSkalpel);\n\n let isStreaming = false;\n if (body) {\n try {\n const parsed = JSON.parse(body);\n isStreaming = parsed.stream === true;\n } catch {\n // Not JSON — treat as non-streaming\n }\n }\n\n if (isStreaming) {\n const skalpelUrl = `${config.remoteBaseUrl}${path}`;\n const directUrl = `${config.anthropicDirectUrl}${path}`;\n await handleStreamingRequest(req, res, config, source, body, skalpelUrl, directUrl, useSkalpel, forwardHeaders, logger);\n logger.info(`${method} ${path} source=${source} streaming latency=${Date.now() - start}ms`);\n return;\n }\n\n const skalpelUrl = `${config.remoteBaseUrl}${path}`;\n const directUrl = `${config.anthropicDirectUrl}${path}`;\n const fetchBody = method !== 'GET' && method !== 'HEAD' ? body : undefined;\n\n let response: Response | null = null;\n let fetchError: unknown = null;\n let usedFallback = false;\n\n // Try Skalpel backend first (if this request should be optimized)\n if (useSkalpel) {\n try {\n response = await fetch(skalpelUrl, { method, headers: forwardHeaders, body: fetchBody });\n } catch (err) {\n fetchError = err;\n }\n\n // If Skalpel backend is down, fall back to direct Anthropic API\n if (isSkalpelBackendFailure(response, fetchError)) {\n logger.warn(`${method} ${path} Skalpel backend failed (${fetchError ? (fetchError as Error).message : `status ${response?.status}`}), falling back to direct Anthropic API`);\n usedFallback = true;\n response = null;\n fetchError = null;\n const directHeaders = stripSkalpelHeaders(forwardHeaders);\n try {\n response = await fetch(directUrl, { method, headers: directHeaders, body: fetchBody });\n } catch (err) {\n fetchError = err;\n }\n }\n } else {\n // Non-Skalpel path — go direct\n try {\n response = await fetch(directUrl, { method, headers: forwardHeaders, body: fetchBody });\n } catch (err) {\n fetchError = err;\n }\n }\n\n // If even the direct request failed, return 502\n if (!response || fetchError) {\n throw fetchError ?? new Error('no response from upstream');\n }\n\n const responseHeaders = extractResponseHeaders(response);\n const responseBody = Buffer.from(await response.arrayBuffer());\n responseHeaders['content-length'] = String(responseBody.length);\n res.writeHead(response.status, responseHeaders);\n res.end(responseBody);\n\n logger.info(`${method} ${path} source=${source} status=${response.status}${usedFallback ? ' (fallback)' : ''} latency=${Date.now() - start}ms`);\n } catch (err) {\n logger.error(`${method} ${path} source=${source} error=${(err as Error).message}`);\n if (!res.headersSent) {\n res.writeHead(502, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ error: 'proxy_error', message: (err as Error).message }));\n }\n }\n}\n","import type { ServerResponse } from 'node:http';\nimport type { ProxyConfig } from './types.js';\n\nexport function handleHealthRequest(\n res: ServerResponse,\n config: ProxyConfig,\n startTime: number,\n): void {\n const body = JSON.stringify({\n status: 'ok',\n uptime: Date.now() - startTime,\n ports: {\n anthropic: config.anthropicPort,\n openai: config.openaiPort,\n },\n version: 'proxy-1.0.0',\n });\n\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(body);\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\n\nexport function writePid(pidFile: string): void {\n fs.mkdirSync(path.dirname(pidFile), { recursive: true });\n fs.writeFileSync(pidFile, String(process.pid));\n}\n\nexport function readPid(pidFile: string): number | null {\n try {\n const raw = fs.readFileSync(pidFile, 'utf-8').trim();\n const pid = parseInt(raw, 10);\n if (isNaN(pid)) return null;\n return isRunning(pid) ? pid : null;\n } catch {\n return null;\n }\n}\n\nexport function isRunning(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function removePid(pidFile: string): void {\n try {\n fs.unlinkSync(pidFile);\n } catch {\n // Already removed\n }\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\n\nconst MAX_SIZE = 5 * 1024 * 1024; // 5MB\nconst MAX_ROTATIONS = 3;\n\nconst LEVELS = { debug: 0, info: 1, warn: 2, error: 3 } as const;\n\nexport class Logger {\n private logFile: string;\n private level: keyof typeof LEVELS;\n\n constructor(logFile: string, level: keyof typeof LEVELS = 'info') {\n this.logFile = logFile;\n this.level = level;\n fs.mkdirSync(path.dirname(logFile), { recursive: true });\n }\n\n debug(msg: string): void { this.log('debug', msg); }\n info(msg: string): void { this.log('info', msg); }\n warn(msg: string): void { this.log('warn', msg); }\n error(msg: string): void { this.log('error', msg); }\n\n private log(level: keyof typeof LEVELS, msg: string): void {\n if (LEVELS[level] < LEVELS[this.level]) return;\n\n const line = `${new Date().toISOString()} [${level.toUpperCase()}] ${msg}\\n`;\n\n if (level === 'debug' || level === 'error') {\n process.stderr.write(line);\n }\n\n try {\n this.rotate();\n fs.appendFileSync(this.logFile, line);\n } catch {\n // Best-effort logging\n }\n }\n\n private rotate(): void {\n try {\n const stat = fs.statSync(this.logFile);\n if (stat.size < MAX_SIZE) return;\n } catch {\n return;\n }\n\n for (let i = MAX_ROTATIONS; i >= 1; i--) {\n const src = i === 1 ? this.logFile : `${this.logFile}.${i - 1}`;\n const dst = `${this.logFile}.${i}`;\n try {\n fs.renameSync(src, dst);\n } catch {\n // File may not exist\n }\n }\n }\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport os from 'node:os';\nimport type { ProxyConfig } from './types.js';\n\nfunction expandHome(filePath: string): string {\n if (filePath.startsWith('~')) {\n return path.join(os.homedir(), filePath.slice(1));\n }\n return filePath;\n}\n\nconst DEFAULTS: ProxyConfig = {\n apiKey: '',\n remoteBaseUrl: 'https://api.skalpel.ai',\n anthropicDirectUrl: 'https://api.anthropic.com',\n anthropicPort: 18100,\n openaiPort: 18101,\n logLevel: 'info',\n logFile: '~/.skalpel/logs/proxy.log',\n pidFile: '~/.skalpel/proxy.pid',\n configFile: '~/.skalpel/config.json',\n};\n\nexport function loadConfig(configPath?: string): ProxyConfig {\n const filePath = expandHome(configPath ?? DEFAULTS.configFile);\n let fileConfig: Partial<ProxyConfig> = {};\n\n try {\n const raw = fs.readFileSync(filePath, 'utf-8');\n fileConfig = JSON.parse(raw) as Partial<ProxyConfig>;\n } catch {\n // Config file doesn't exist or is invalid — use defaults\n }\n\n return {\n apiKey: fileConfig.apiKey ?? DEFAULTS.apiKey,\n remoteBaseUrl: fileConfig.remoteBaseUrl ?? DEFAULTS.remoteBaseUrl,\n anthropicDirectUrl: fileConfig.anthropicDirectUrl ?? DEFAULTS.anthropicDirectUrl,\n anthropicPort: fileConfig.anthropicPort ?? DEFAULTS.anthropicPort,\n openaiPort: fileConfig.openaiPort ?? DEFAULTS.openaiPort,\n logLevel: fileConfig.logLevel ?? DEFAULTS.logLevel,\n logFile: expandHome(fileConfig.logFile ?? DEFAULTS.logFile),\n pidFile: expandHome(fileConfig.pidFile ?? DEFAULTS.pidFile),\n configFile: filePath,\n };\n}\n\nexport function saveConfig(config: ProxyConfig): void {\n const dir = path.dirname(config.configFile);\n fs.mkdirSync(dir, { recursive: true });\n fs.writeFileSync(config.configFile, JSON.stringify(config, null, 2) + '\\n');\n}\n","import { startProxy } from '../proxy/server.js';\nimport { loadConfig } from '../proxy/config.js';\n\nconst config = loadConfig();\nstartProxy(config);\n"],"mappings":";;;AAAA,OAAO,UAAU;;;ACIjB,IAAM,aAAa,oBAAI,IAAI;AAAA,EACzB;AAAA,EAAc;AAAA,EAAc;AAAA,EAAsB;AAAA,EAClD;AAAA,EAAM;AAAA,EAAW;AAAA,EAAqB;AACxC,CAAC;AAED,IAAM,gBAAgB,oBAAI,IAAI;AAAA,EAC5B,GAAG;AAAA,EACH;AAAA,EAAoB;AACtB,CAAC;AAGD,SAAS,oBAAoB,SAAyD;AACpF,QAAM,UAAU,EAAE,GAAG,QAAQ;AAC7B,SAAO,QAAQ,mBAAmB;AAClC,SAAO,QAAQ,kBAAkB;AACjC,SAAO,QAAQ,sBAAsB;AACrC,SAAO,QAAQ,uBAAuB;AACtC,SAAO;AACT;AAIA,SAAS,wBAAwB,UAA2B,KAAuB;AACjF,MAAI,IAAK,QAAO;AAChB,MAAI,CAAC,SAAU,QAAO;AACtB,MAAI,SAAS,UAAU,IAAK,QAAO;AACnC,SAAO;AACT;AAEA,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