chatccc 0.2.252 → 0.2.254
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 +10 -1
- package/bin/cccagent.mjs +12 -3
- package/bin/chatccc.mjs +15 -6
- package/config.sample.json +2 -1
- package/deepccc-agent/README.md +40 -35
- package/deepccc-agent/os-prompts/darwin.md +8 -8
- package/deepccc-agent/os-prompts/linux.md +8 -8
- package/deepccc-agent/os-prompts/win32.md +11 -11
- package/deepccc-agent/package.json +65 -65
- package/dist/deepccc-agent/src/cli.js +634 -0
- package/dist/deepccc-agent/src/config.js +81 -0
- package/dist/deepccc-agent/src/context.js +348 -0
- package/dist/deepccc-agent/src/file-log.js +34 -0
- package/dist/deepccc-agent/src/file-tools.js +1208 -0
- package/dist/deepccc-agent/src/index.js +601 -0
- package/dist/deepccc-agent/src/permissions.js +170 -0
- package/dist/deepccc-agent/src/privacy.js +124 -0
- package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
- package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
- package/dist/deepccc-agent/src/progress/reducer.js +102 -0
- package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
- package/dist/deepccc-agent/src/progress/view.js +30 -0
- package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
- package/dist/deepccc-agent/src/session-search.js +276 -0
- package/dist/deepccc-agent/src/session-select.js +23 -0
- package/dist/deepccc-agent/src/sigint.js +26 -0
- package/dist/deepccc-agent/src/skills.js +178 -0
- package/dist/deepccc-agent/src/web-tools.js +246 -0
- package/dist/src/adapters/adapter-interface.js +19 -0
- package/dist/src/adapters/ccc-adapter.js +112 -0
- package/dist/src/adapters/claude-adapter.js +497 -0
- package/dist/src/adapters/claude-session-meta-store.js +92 -0
- package/dist/src/adapters/codex-adapter.js +279 -0
- package/dist/src/adapters/codex-session-meta-store.js +94 -0
- package/dist/src/adapters/cursor-adapter.js +491 -0
- package/dist/src/adapters/cursor-session-meta-store.js +116 -0
- package/dist/src/adapters/jsonl-stream.js +104 -0
- package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
- package/dist/src/adapters/raw-stream-log.js +106 -0
- package/dist/src/adapters/resource-monitor.js +113 -0
- package/dist/src/agent-activity.js +133 -0
- package/dist/src/agent-delegate-task-rpc.js +129 -0
- package/dist/src/agent-delegate-task.js +48 -0
- package/dist/src/agent-file-rpc.js +152 -0
- package/dist/src/agent-image-rpc.js +148 -0
- package/dist/src/agent-platform-routing.js +13 -0
- package/dist/src/agent-reload-config-rpc.js +23 -0
- package/dist/src/agent-rpc-body.js +87 -0
- package/dist/src/agent-stop-stuck.js +110 -0
- package/dist/src/agent-team/application/board-service.js +142 -0
- package/dist/src/agent-team/application/main-agent-service.js +151 -0
- package/dist/src/agent-team/domain/board.js +69 -0
- package/dist/src/agent-team/http/board-routes.js +213 -0
- package/dist/src/agent-team/infrastructure/filesystem-browser.js +112 -0
- package/dist/src/agent-team/infrastructure/main-agent-session-runtime.js +30 -0
- package/dist/src/agent-team/main-agent-bootstrap.js +15 -0
- package/dist/src/agent-team/repositories/board-repository.js +10 -0
- package/dist/src/agent-team/repositories/feishu-p2p-contact-store.js +64 -0
- package/dist/src/agent-team/repositories/json-board-repository.js +209 -0
- package/dist/src/agent-team/repositories/main-agent-binding-repository.js +84 -0
- package/dist/src/agent-team/sync/board-sync-adapter.js +1 -0
- package/dist/src/agent-team/web/agent-team-page.js +219 -0
- package/dist/src/agent-tool.js +10 -0
- package/dist/src/card-action-routing.js +7 -0
- package/dist/src/card-plain-text.js +101 -0
- package/dist/src/cardkit.js +158 -0
- package/dist/src/cards.js +573 -0
- package/dist/src/chatgpt-subscription-rpc.js +18 -0
- package/dist/src/chatgpt-subscription.js +199 -0
- package/dist/src/chrome-devtools-guard.js +238 -0
- package/dist/src/claude-sdk-installer.js +249 -0
- package/dist/src/codex-reset-actions.js +143 -0
- package/dist/src/config-utils.js +149 -0
- package/dist/src/config.js +808 -0
- package/dist/src/cursor-usage.js +77 -0
- package/dist/src/exit-banner.js +28 -0
- package/dist/src/feishu-api.js +1404 -0
- package/dist/src/feishu-message-ingress.js +137 -0
- package/dist/src/feishu-platform.js +97 -0
- package/dist/src/format-message.js +252 -0
- package/dist/src/git-command.js +155 -0
- package/dist/src/im-skills.js +121 -0
- package/dist/src/index.js +835 -0
- package/dist/src/litellm-proxy.js +300 -0
- package/dist/src/orchestrator.js +2096 -0
- package/dist/src/package-root.js +26 -0
- package/dist/src/platform-adapter.js +7 -0
- package/dist/src/platform-startup.js +6 -0
- package/dist/src/privacy.js +100 -0
- package/dist/src/progress/reducer.js +102 -0
- package/dist/src/progress/terminal-renderer.js +264 -0
- package/dist/src/progress/view.js +30 -0
- package/dist/src/response-stall.js +14 -0
- package/dist/src/runtime-entry.js +13 -0
- package/dist/src/runtime-reload.js +19 -0
- package/dist/src/session-chat-binding.js +183 -0
- package/dist/src/session-name.js +7 -0
- package/dist/src/session.js +2147 -0
- package/dist/src/shared-prefix.js +16 -0
- package/dist/src/shared.js +493 -0
- package/dist/src/sim-agent.js +105 -0
- package/dist/src/sim-platform.js +142 -0
- package/dist/src/sim-store.js +231 -0
- package/dist/src/simplify.js +99 -0
- package/dist/src/startup-lifecycle.js +209 -0
- package/dist/src/stream-state.js +141 -0
- package/dist/src/terminal-error.js +100 -0
- package/dist/src/trace.js +50 -0
- package/dist/src/turn-cards.js +92 -0
- package/dist/src/update-command-guard.js +114 -0
- package/{src/web-ui.ts → dist/src/web-ui.js} +753 -839
- package/dist/src/wechat-platform.js +545 -0
- package/package.json +76 -74
- package/deepccc-agent/LICENSE +0 -201
- package/deepccc-agent/bin/deepccc.mjs +0 -26
- package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
- package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
- package/deepccc-agent/package-lock.json +0 -2027
- package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -877
- package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
- package/deepccc-agent/src/__tests__/config.test.ts +0 -34
- package/deepccc-agent/src/__tests__/context.test.ts +0 -341
- package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
- package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
- package/deepccc-agent/src/cli.ts +0 -682
- package/deepccc-agent/src/config.ts +0 -101
- package/deepccc-agent/src/context.ts +0 -465
- package/deepccc-agent/src/file-log.ts +0 -38
- package/deepccc-agent/src/file-tools.ts +0 -1493
- package/deepccc-agent/src/index.ts +0 -710
- package/deepccc-agent/src/permissions.ts +0 -226
- package/deepccc-agent/src/privacy.ts +0 -141
- package/deepccc-agent/src/proc-tree-kill.ts +0 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
- package/deepccc-agent/src/progress/reducer.ts +0 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
- package/deepccc-agent/src/progress/view.ts +0 -77
- package/deepccc-agent/src/raw-stream-log.ts +0 -124
- package/deepccc-agent/src/session-search.ts +0 -370
- package/deepccc-agent/src/session-select.ts +0 -48
- package/deepccc-agent/src/sigint.ts +0 -50
- package/deepccc-agent/src/skills.ts +0 -205
- package/deepccc-agent/src/web-tools.ts +0 -313
- package/deepccc-agent/tsconfig.build.json +0 -13
- package/deepccc-agent/tsconfig.json +0 -13
- package/deepccc-agent/vitest.config.ts +0 -7
- package/src/__tests__/adapter-interface.test.ts +0 -152
- package/src/__tests__/agent-activity.test.ts +0 -86
- package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
- package/src/__tests__/agent-image-rpc.test.ts +0 -34
- package/src/__tests__/agent-platform-routing.test.ts +0 -26
- package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
- package/src/__tests__/agent-rpc-body.test.ts +0 -42
- package/src/__tests__/builtin-chat-session.test.ts +0 -532
- package/src/__tests__/builtin-cli-json.test.ts +0 -39
- package/src/__tests__/builtin-config.test.ts +0 -26
- package/src/__tests__/builtin-context.test.ts +0 -319
- package/src/__tests__/builtin-file-tools.test.ts +0 -240
- package/src/__tests__/builtin-permissions.test.ts +0 -219
- package/src/__tests__/builtin-session-search.test.ts +0 -262
- package/src/__tests__/builtin-session-select.test.ts +0 -116
- package/src/__tests__/builtin-sigint.test.ts +0 -56
- package/src/__tests__/builtin-skills.test.ts +0 -284
- package/src/__tests__/builtin-web-tools.test.ts +0 -220
- package/src/__tests__/card-action-routing.test.ts +0 -18
- package/src/__tests__/card-plain-text.test.ts +0 -45
- package/src/__tests__/cardkit.test.ts +0 -60
- package/src/__tests__/cards.test.ts +0 -607
- package/src/__tests__/ccc-adapter.test.ts +0 -194
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
- package/src/__tests__/chatgpt-subscription.test.ts +0 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
- package/src/__tests__/claude-adapter.test.ts +0 -614
- package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
- package/src/__tests__/claude-sdk-installer.test.ts +0 -285
- package/src/__tests__/codex-adapter.test.ts +0 -331
- package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
- package/src/__tests__/codex-reset-actions.test.ts +0 -146
- package/src/__tests__/config-reload.test.ts +0 -284
- package/src/__tests__/config-sample.test.ts +0 -97
- package/src/__tests__/config-utils.test.ts +0 -40
- package/src/__tests__/config.test.ts +0 -395
- package/src/__tests__/crash-logging.test.ts +0 -360
- package/src/__tests__/cursor-adapter.test.ts +0 -890
- package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
- package/src/__tests__/feishu-api.test.ts +0 -60
- package/src/__tests__/feishu-avatar.test.ts +0 -504
- package/src/__tests__/feishu-message-ingress.test.ts +0 -138
- package/src/__tests__/feishu-platform.test.ts +0 -75
- package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
- package/src/__tests__/format-message.test.ts +0 -316
- package/src/__tests__/git-command.test.ts +0 -288
- package/src/__tests__/im-skills.test.ts +0 -125
- package/src/__tests__/jsonl-stream.test.ts +0 -79
- package/src/__tests__/orchestrator.test.ts +0 -1268
- package/src/__tests__/package-files.test.ts +0 -24
- package/src/__tests__/platform-startup.test.ts +0 -19
- package/src/__tests__/privacy.test.ts +0 -198
- package/src/__tests__/proc-tree-kill.test.ts +0 -108
- package/src/__tests__/progress-reducer.test.ts +0 -121
- package/src/__tests__/raw-stream-log.test.ts +0 -106
- package/src/__tests__/response-stall.test.ts +0 -49
- package/src/__tests__/restart.test.ts +0 -232
- package/src/__tests__/session-ccc-config.test.ts +0 -66
- package/src/__tests__/session.test.ts +0 -3004
- package/src/__tests__/shared-prefix.test.ts +0 -36
- package/src/__tests__/sim-agent.test.ts +0 -174
- package/src/__tests__/sim-platform.test.ts +0 -93
- package/src/__tests__/sim-store.test.ts +0 -214
- package/src/__tests__/simplify.test.ts +0 -283
- package/src/__tests__/startup-lifecycle.test.ts +0 -231
- package/src/__tests__/stop-session.test.ts +0 -162
- package/src/__tests__/stream-state.test.ts +0 -164
- package/src/__tests__/terminal-error.test.ts +0 -54
- package/src/__tests__/terminal-renderer.test.ts +0 -247
- package/src/__tests__/update-command-guard.test.ts +0 -144
- package/src/__tests__/web-ui.test.ts +0 -438
- package/src/__tests__/wechat-platform.test.ts +0 -111
- package/src/adapters/adapter-interface.ts +0 -217
- package/src/adapters/ccc-adapter.ts +0 -150
- package/src/adapters/claude-adapter.ts +0 -673
- package/src/adapters/claude-session-meta-store.ts +0 -120
- package/src/adapters/codex-adapter.ts +0 -426
- package/src/adapters/codex-session-meta-store.ts +0 -131
- package/src/adapters/cursor-adapter.ts +0 -681
- package/src/adapters/cursor-session-meta-store.ts +0 -154
- package/src/adapters/jsonl-stream.ts +0 -157
- package/src/adapters/raw-stream-log.ts +0 -124
- package/src/adapters/resource-monitor.ts +0 -141
- package/src/agent-activity.ts +0 -175
- package/src/agent-delegate-task-rpc.ts +0 -153
- package/src/agent-delegate-task.ts +0 -91
- package/src/agent-file-rpc.ts +0 -172
- package/src/agent-image-rpc.ts +0 -168
- package/src/agent-platform-routing.ts +0 -28
- package/src/agent-reload-config-rpc.ts +0 -34
- package/src/agent-rpc-body.ts +0 -92
- package/src/agent-stop-stuck.ts +0 -129
- package/src/card-action-routing.ts +0 -14
- package/src/card-plain-text.ts +0 -108
- package/src/cardkit.ts +0 -179
- package/src/cards.ts +0 -684
- package/src/chatgpt-subscription-rpc.ts +0 -27
- package/src/chatgpt-subscription.ts +0 -299
- package/src/chrome-devtools-guard.ts +0 -318
- package/src/claude-sdk-installer.ts +0 -324
- package/src/codex-reset-actions.ts +0 -184
- package/src/config-utils.ts +0 -211
- package/src/config.ts +0 -1063
- package/src/cursor-usage.ts +0 -128
- package/src/exit-banner.ts +0 -33
- package/src/feishu-api.ts +0 -1616
- package/src/feishu-message-ingress.ts +0 -195
- package/src/feishu-platform.ts +0 -159
- package/src/format-message.ts +0 -293
- package/src/git-command.ts +0 -202
- package/src/im-skills.ts +0 -149
- package/src/index.ts +0 -1089
- package/src/litellm-proxy.ts +0 -374
- package/src/orchestrator.ts +0 -2543
- package/src/platform-adapter.ts +0 -70
- package/src/platform-startup.ts +0 -16
- package/src/privacy.ts +0 -118
- package/src/progress/reducer.ts +0 -113
- package/src/progress/terminal-renderer.ts +0 -294
- package/src/progress/view.ts +0 -77
- package/src/response-stall.ts +0 -28
- package/src/runtime-reload.ts +0 -34
- package/src/session-chat-binding.ts +0 -292
- package/src/session-name.ts +0 -8
- package/src/session.ts +0 -2659
- package/src/shared-prefix.ts +0 -29
- package/src/shared.ts +0 -552
- package/src/sim-agent.ts +0 -167
- package/src/sim-platform.ts +0 -177
- package/src/sim-store.ts +0 -317
- package/src/simplify.ts +0 -120
- package/src/startup-lifecycle.ts +0 -250
- package/src/stream-state.ts +0 -177
- package/src/terminal-error.ts +0 -129
- package/src/trace.ts +0 -51
- package/src/turn-cards.ts +0 -118
- package/src/update-command-guard.ts +0 -165
- package/src/wechat-platform.ts +0 -680
package/src/litellm-proxy.ts
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
import http, { createServer, type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from "node:http";
|
|
2
|
-
import https from "node:https";
|
|
3
|
-
import { createWriteStream } from "node:fs";
|
|
4
|
-
import { mkdir, writeFile, appendFile } from "node:fs/promises";
|
|
5
|
-
import { homedir } from "node:os";
|
|
6
|
-
import { dirname, join } from "node:path";
|
|
7
|
-
|
|
8
|
-
type ProxyConfig = {
|
|
9
|
-
host: string;
|
|
10
|
-
port: number;
|
|
11
|
-
upstream: URL;
|
|
12
|
-
logDir: string;
|
|
13
|
-
logSecrets: boolean;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type RequestLog = {
|
|
17
|
-
id: string;
|
|
18
|
-
timestamp: string;
|
|
19
|
-
method: string;
|
|
20
|
-
path: string;
|
|
21
|
-
upstreamUrl: string;
|
|
22
|
-
headers: Record<string, string | string[] | undefined>;
|
|
23
|
-
bodyBytes: number;
|
|
24
|
-
bodyFile: string;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type ResponseLog = {
|
|
28
|
-
id: string;
|
|
29
|
-
timestamp: string;
|
|
30
|
-
status: number;
|
|
31
|
-
statusText: string;
|
|
32
|
-
headers: Record<string, string>;
|
|
33
|
-
bodyBytes: number;
|
|
34
|
-
bodyFile: string;
|
|
35
|
-
durationMs: number;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type RawUpstreamResponse = {
|
|
39
|
-
statusCode: number;
|
|
40
|
-
statusMessage: string;
|
|
41
|
-
headers: IncomingHttpHeaders;
|
|
42
|
-
stream: IncomingMessage;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const HOP_BY_HOP_HEADERS = new Set([
|
|
46
|
-
"connection",
|
|
47
|
-
"keep-alive",
|
|
48
|
-
"proxy-authenticate",
|
|
49
|
-
"proxy-authorization",
|
|
50
|
-
"te",
|
|
51
|
-
"trailer",
|
|
52
|
-
"transfer-encoding",
|
|
53
|
-
"upgrade",
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
const SENSITIVE_HEADERS = new Set([
|
|
57
|
-
"authorization",
|
|
58
|
-
"cookie",
|
|
59
|
-
"set-cookie",
|
|
60
|
-
"x-api-key",
|
|
61
|
-
"api-key",
|
|
62
|
-
]);
|
|
63
|
-
|
|
64
|
-
let requestSeq = 0;
|
|
65
|
-
|
|
66
|
-
function parseArgs(argv: string[]): Partial<ProxyConfig> {
|
|
67
|
-
const parsed: Partial<ProxyConfig> = {};
|
|
68
|
-
for (let i = 0; i < argv.length; i++) {
|
|
69
|
-
const arg = argv[i];
|
|
70
|
-
const next = argv[i + 1];
|
|
71
|
-
const readValue = () => {
|
|
72
|
-
if (!next) throw new Error(`${arg} requires a value`);
|
|
73
|
-
i++;
|
|
74
|
-
return next;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
if (arg === "--host") parsed.host = readValue();
|
|
78
|
-
else if (arg === "--port") parsed.port = Number(readValue());
|
|
79
|
-
else if (arg === "--upstream") parsed.upstream = new URL(readValue());
|
|
80
|
-
else if (arg === "--log-dir") parsed.logDir = readValue();
|
|
81
|
-
else if (arg === "--log-secrets") parsed.logSecrets = true;
|
|
82
|
-
else if (arg === "--help" || arg === "-h") {
|
|
83
|
-
printHelp();
|
|
84
|
-
process.exit(0);
|
|
85
|
-
} else {
|
|
86
|
-
throw new Error(`Unknown argument: ${arg}`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return parsed;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function readConfig(): ProxyConfig {
|
|
93
|
-
const args = parseArgs(process.argv.slice(2));
|
|
94
|
-
const upstreamRaw = args.upstream?.toString() ??
|
|
95
|
-
process.env.CHATCCC_LITELLM_PROXY_UPSTREAM ??
|
|
96
|
-
"https://litellm.hypergryph.net";
|
|
97
|
-
|
|
98
|
-
const portRaw = args.port ?? Number(process.env.CHATCCC_LITELLM_PROXY_PORT ?? "18081");
|
|
99
|
-
if (!Number.isInteger(portRaw) || portRaw <= 0 || portRaw > 65535) {
|
|
100
|
-
throw new Error(`Invalid port: ${portRaw}`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
host: args.host ?? process.env.CHATCCC_LITELLM_PROXY_HOST ?? "127.0.0.1",
|
|
105
|
-
port: portRaw,
|
|
106
|
-
upstream: new URL(upstreamRaw),
|
|
107
|
-
logDir: args.logDir ?? process.env.CHATCCC_LITELLM_PROXY_LOG_DIR ??
|
|
108
|
-
join(homedir(), ".chatccc", "logs", "litellm-proxy"),
|
|
109
|
-
logSecrets: args.logSecrets ?? process.env.CHATCCC_LITELLM_PROXY_LOG_SECRETS === "1",
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function printHelp(): void {
|
|
114
|
-
console.log(`Usage: npm run claude-proxy -- [options]
|
|
115
|
-
|
|
116
|
-
Options:
|
|
117
|
-
--host <host> Listen host, default 127.0.0.1
|
|
118
|
-
--port <port> Listen port, default 18081
|
|
119
|
-
--upstream <url> Upstream LiteLLM base URL, default https://litellm.hypergryph.net
|
|
120
|
-
--log-dir <path> Log directory, default %USERPROFILE%/.chatccc/logs/litellm-proxy
|
|
121
|
-
--log-secrets Log sensitive headers instead of redacting them
|
|
122
|
-
|
|
123
|
-
Equivalent env vars:
|
|
124
|
-
CHATCCC_LITELLM_PROXY_HOST
|
|
125
|
-
CHATCCC_LITELLM_PROXY_PORT
|
|
126
|
-
CHATCCC_LITELLM_PROXY_UPSTREAM
|
|
127
|
-
CHATCCC_LITELLM_PROXY_LOG_DIR
|
|
128
|
-
CHATCCC_LITELLM_PROXY_LOG_SECRETS=1
|
|
129
|
-
`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function nextRequestId(): string {
|
|
133
|
-
const stamp = new Date().toISOString()
|
|
134
|
-
.replace(/[-:]/g, "")
|
|
135
|
-
.replace(/\.\d{3}Z$/, "Z");
|
|
136
|
-
requestSeq = (requestSeq + 1) % 1_000_000;
|
|
137
|
-
return `${stamp}-${String(requestSeq).padStart(6, "0")}`;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function dayDir(baseDir: string): string {
|
|
141
|
-
return join(baseDir, new Date().toISOString().slice(0, 10));
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function toUpstreamUrl(reqUrl: string | undefined, upstream: URL): URL {
|
|
145
|
-
const path = reqUrl && reqUrl.startsWith("/") ? reqUrl : "/";
|
|
146
|
-
return new URL(path, upstream);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function isHopByHopHeader(name: string): boolean {
|
|
150
|
-
return HOP_BY_HOP_HEADERS.has(name.toLowerCase());
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function copyRequestHeaders(headers: IncomingHttpHeaders, bodyLength: number): Record<string, string | string[]> {
|
|
154
|
-
const next: Record<string, string | string[]> = {};
|
|
155
|
-
for (const [name, value] of Object.entries(headers)) {
|
|
156
|
-
const lower = name.toLowerCase();
|
|
157
|
-
if (lower === "host" || lower === "content-length" || isHopByHopHeader(lower)) continue;
|
|
158
|
-
if (Array.isArray(value)) {
|
|
159
|
-
next[name] = value;
|
|
160
|
-
} else if (typeof value === "string") {
|
|
161
|
-
next[name] = value;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (bodyLength > 0) next["content-length"] = String(bodyLength);
|
|
165
|
-
return next;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function safeHeaders<T extends Record<string, string | string[] | undefined>>(
|
|
169
|
-
headers: T,
|
|
170
|
-
logSecrets: boolean,
|
|
171
|
-
): T {
|
|
172
|
-
if (logSecrets) return headers;
|
|
173
|
-
const redacted = { ...headers };
|
|
174
|
-
for (const key of Object.keys(redacted)) {
|
|
175
|
-
if (SENSITIVE_HEADERS.has(key.toLowerCase())) {
|
|
176
|
-
redacted[key as keyof T] = "[REDACTED]" as T[keyof T];
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return redacted;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function responseHeaders(headers: IncomingHttpHeaders, logSecrets: boolean): Record<string, string> {
|
|
183
|
-
const result: Record<string, string> = {};
|
|
184
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
185
|
-
if (value === undefined) continue;
|
|
186
|
-
result[key] = SENSITIVE_HEADERS.has(key.toLowerCase()) && !logSecrets
|
|
187
|
-
? "[REDACTED]"
|
|
188
|
-
: Array.isArray(value) ? value.join(", ") : value;
|
|
189
|
-
}
|
|
190
|
-
return result;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function readBody(req: IncomingMessage): Promise<Buffer> {
|
|
194
|
-
const chunks: Buffer[] = [];
|
|
195
|
-
for await (const chunk of req) {
|
|
196
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
197
|
-
}
|
|
198
|
-
return Buffer.concat(chunks);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function appendJsonl(filePath: string, value: unknown): Promise<void> {
|
|
202
|
-
await mkdir(dirname(filePath), { recursive: true });
|
|
203
|
-
await appendFile(filePath, JSON.stringify(value) + "\n", "utf-8");
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async function logRequest(
|
|
207
|
-
config: ProxyConfig,
|
|
208
|
-
id: string,
|
|
209
|
-
req: IncomingMessage,
|
|
210
|
-
upstreamUrl: URL,
|
|
211
|
-
body: Buffer,
|
|
212
|
-
bodyFile: string,
|
|
213
|
-
): Promise<void> {
|
|
214
|
-
const log: RequestLog = {
|
|
215
|
-
id,
|
|
216
|
-
timestamp: new Date().toISOString(),
|
|
217
|
-
method: req.method ?? "GET",
|
|
218
|
-
path: req.url ?? "/",
|
|
219
|
-
upstreamUrl: upstreamUrl.toString(),
|
|
220
|
-
headers: safeHeaders(req.headers, config.logSecrets),
|
|
221
|
-
bodyBytes: body.byteLength,
|
|
222
|
-
bodyFile,
|
|
223
|
-
};
|
|
224
|
-
await writeFile(bodyFile, body);
|
|
225
|
-
await appendJsonl(join(dayDir(config.logDir), "events.jsonl"), { event: "request", ...log });
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function logResponse(
|
|
229
|
-
config: ProxyConfig,
|
|
230
|
-
log: ResponseLog,
|
|
231
|
-
): Promise<void> {
|
|
232
|
-
await appendJsonl(join(dayDir(config.logDir), "events.jsonl"), { event: "response", ...log });
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function requestUpstream(
|
|
236
|
-
upstreamUrl: URL,
|
|
237
|
-
req: IncomingMessage,
|
|
238
|
-
body: Buffer,
|
|
239
|
-
abortSignal: AbortSignal,
|
|
240
|
-
): Promise<RawUpstreamResponse> {
|
|
241
|
-
return new Promise((resolve, reject) => {
|
|
242
|
-
const transport = upstreamUrl.protocol === "https:" ? https : http;
|
|
243
|
-
const upstreamReq = transport.request({
|
|
244
|
-
protocol: upstreamUrl.protocol,
|
|
245
|
-
hostname: upstreamUrl.hostname,
|
|
246
|
-
port: upstreamUrl.port || undefined,
|
|
247
|
-
method: req.method,
|
|
248
|
-
path: `${upstreamUrl.pathname}${upstreamUrl.search}`,
|
|
249
|
-
headers: copyRequestHeaders(req.headers, body.byteLength),
|
|
250
|
-
}, (upstreamRes) => {
|
|
251
|
-
resolve({
|
|
252
|
-
statusCode: upstreamRes.statusCode ?? 502,
|
|
253
|
-
statusMessage: upstreamRes.statusMessage ?? "",
|
|
254
|
-
headers: upstreamRes.headers,
|
|
255
|
-
stream: upstreamRes,
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
upstreamReq.on("error", reject);
|
|
260
|
-
abortSignal.addEventListener("abort", () => upstreamReq.destroy(new Error("client aborted")), { once: true });
|
|
261
|
-
|
|
262
|
-
if (body.byteLength > 0) upstreamReq.write(body);
|
|
263
|
-
upstreamReq.end();
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function setResponseHeaders(res: ServerResponse, headers: IncomingHttpHeaders): void {
|
|
268
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
269
|
-
if (!value || isHopByHopHeader(key)) continue;
|
|
270
|
-
res.setHeader(key, value);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
async function writeError(res: ServerResponse, status: number, error: string): Promise<void> {
|
|
275
|
-
if (res.headersSent) {
|
|
276
|
-
res.end();
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
res.writeHead(status, { "content-type": "application/json; charset=utf-8" });
|
|
280
|
-
res.end(JSON.stringify({ error }));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
async function handleProxyRequest(config: ProxyConfig, req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
284
|
-
const started = Date.now();
|
|
285
|
-
const id = nextRequestId();
|
|
286
|
-
const dir = dayDir(config.logDir);
|
|
287
|
-
await mkdir(dir, { recursive: true });
|
|
288
|
-
|
|
289
|
-
const upstreamUrl = toUpstreamUrl(req.url, config.upstream);
|
|
290
|
-
const requestBody = await readBody(req);
|
|
291
|
-
const requestBodyFile = join(dir, `${id}.request.body`);
|
|
292
|
-
const responseBodyFile = join(dir, `${id}.response.body`);
|
|
293
|
-
await logRequest(config, id, req, upstreamUrl, requestBody, requestBodyFile);
|
|
294
|
-
|
|
295
|
-
const abortController = new AbortController();
|
|
296
|
-
req.on("aborted", () => abortController.abort());
|
|
297
|
-
|
|
298
|
-
let upstreamResponse: RawUpstreamResponse;
|
|
299
|
-
try {
|
|
300
|
-
upstreamResponse = await requestUpstream(upstreamUrl, req, requestBody, abortController.signal);
|
|
301
|
-
} catch (err) {
|
|
302
|
-
await appendJsonl(join(dir, "events.jsonl"), {
|
|
303
|
-
event: "error",
|
|
304
|
-
id,
|
|
305
|
-
timestamp: new Date().toISOString(),
|
|
306
|
-
message: (err as Error).message,
|
|
307
|
-
durationMs: Date.now() - started,
|
|
308
|
-
});
|
|
309
|
-
await writeError(res, 502, `Proxy upstream request failed: ${(err as Error).message}`);
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
res.statusCode = upstreamResponse.statusCode;
|
|
314
|
-
res.statusMessage = upstreamResponse.statusMessage;
|
|
315
|
-
setResponseHeaders(res, upstreamResponse.headers);
|
|
316
|
-
|
|
317
|
-
let responseBytes = 0;
|
|
318
|
-
const out = createWriteStream(responseBodyFile);
|
|
319
|
-
try {
|
|
320
|
-
for await (const chunk of upstreamResponse.stream) {
|
|
321
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
322
|
-
responseBytes += buf.byteLength;
|
|
323
|
-
out.write(buf);
|
|
324
|
-
if (!res.write(buf)) {
|
|
325
|
-
await new Promise<void>((resolve) => res.once("drain", resolve));
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
} catch (err) {
|
|
329
|
-
await appendJsonl(join(dir, "events.jsonl"), {
|
|
330
|
-
event: "stream_error",
|
|
331
|
-
id,
|
|
332
|
-
timestamp: new Date().toISOString(),
|
|
333
|
-
message: (err as Error).message,
|
|
334
|
-
durationMs: Date.now() - started,
|
|
335
|
-
});
|
|
336
|
-
} finally {
|
|
337
|
-
out.end();
|
|
338
|
-
res.end();
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
await logResponse(config, {
|
|
342
|
-
id,
|
|
343
|
-
timestamp: new Date().toISOString(),
|
|
344
|
-
status: upstreamResponse.statusCode,
|
|
345
|
-
statusText: upstreamResponse.statusMessage,
|
|
346
|
-
headers: responseHeaders(upstreamResponse.headers, config.logSecrets),
|
|
347
|
-
bodyBytes: responseBytes,
|
|
348
|
-
bodyFile: responseBodyFile,
|
|
349
|
-
durationMs: Date.now() - started,
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
async function main(): Promise<void> {
|
|
354
|
-
const config = readConfig();
|
|
355
|
-
await mkdir(config.logDir, { recursive: true });
|
|
356
|
-
|
|
357
|
-
const server = createServer((req, res) => {
|
|
358
|
-
void handleProxyRequest(config, req, res).catch(async (err) => {
|
|
359
|
-
console.error(`[litellm-proxy] unhandled request error: ${(err as Error).stack ?? (err as Error).message}`);
|
|
360
|
-
await writeError(res, 500, `Proxy internal error: ${(err as Error).message}`);
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
server.listen(config.port, config.host, () => {
|
|
365
|
-
console.log(`[litellm-proxy] listening on http://${config.host}:${config.port}`);
|
|
366
|
-
console.log(`[litellm-proxy] upstream ${config.upstream.toString()}`);
|
|
367
|
-
console.log(`[litellm-proxy] logs ${config.logDir}`);
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
void main().catch((err) => {
|
|
372
|
-
console.error(`[litellm-proxy] failed to start: ${(err as Error).message}`);
|
|
373
|
-
process.exit(1);
|
|
374
|
-
});
|