cc-x10ded 3.0.0
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/.serena/project.yml +84 -0
- package/CHANGELOG.md +85 -0
- package/CONTRIBUTING.md +74 -0
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/bun.lock +47 -0
- package/dist/ccx +0 -0
- package/package.json +27 -0
- package/src/commands/config.ts +23 -0
- package/src/commands/doctor.ts +73 -0
- package/src/commands/run.ts +76 -0
- package/src/commands/setup.ts +125 -0
- package/src/core/config.ts +111 -0
- package/src/core/shell.ts +166 -0
- package/src/index.ts +53 -0
- package/src/proxy/map.ts +86 -0
- package/src/proxy/providers.ts +142 -0
- package/src/proxy/server.ts +87 -0
- package/src/proxy/types.ts +21 -0
- package/src/proxy/utils.ts +54 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { serve } from "bun";
|
|
2
|
+
import { parseProviderModel } from "./map";
|
|
3
|
+
import { streamOpenAI, streamGemini, streamPassThrough } from "./providers";
|
|
4
|
+
import { Config } from "../core/config";
|
|
5
|
+
import { AnthropicRequest } from "./types";
|
|
6
|
+
|
|
7
|
+
export function startProxyServer(config: Config, port: number = 17870) {
|
|
8
|
+
return serve({
|
|
9
|
+
port,
|
|
10
|
+
hostname: "127.0.0.1",
|
|
11
|
+
async fetch(req) {
|
|
12
|
+
const url = new URL(req.url);
|
|
13
|
+
|
|
14
|
+
if (url.pathname === "/healthz") return Response.json({ ok: true });
|
|
15
|
+
if (req.method !== "POST" || url.pathname !== "/v1/messages") {
|
|
16
|
+
return new Response("Not Found", { status: 404 });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const body = await req.json() as AnthropicRequest;
|
|
21
|
+
const { provider, model } = parseProviderModel(body.model);
|
|
22
|
+
|
|
23
|
+
const headers = {
|
|
24
|
+
"Content-Type": "text/event-stream",
|
|
25
|
+
"Cache-Control": "no-cache",
|
|
26
|
+
"Connection": "keep-alive",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const providers = config.providers;
|
|
30
|
+
|
|
31
|
+
// OpenAI-compatible handlers
|
|
32
|
+
if (provider === "openai") {
|
|
33
|
+
const conf = providers.openai;
|
|
34
|
+
if (!conf?.apiKey) throw new Error("Missing OpenAI API Key");
|
|
35
|
+
return new Response(streamOpenAI(body, model, conf.apiKey, conf.baseUrl || "https://api.openai.com/v1"), { headers });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (provider === "openrouter") {
|
|
39
|
+
const conf = providers.openrouter;
|
|
40
|
+
if (!conf?.apiKey) throw new Error("Missing OpenRouter API Key");
|
|
41
|
+
return new Response(streamOpenAI(body, model, conf.apiKey, conf.baseUrl || "https://openrouter.ai/api/v1"), { headers });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (provider === "gemini") {
|
|
45
|
+
const conf = providers.gemini;
|
|
46
|
+
if (!conf?.apiKey) throw new Error("Missing Gemini API Key");
|
|
47
|
+
return new Response(streamGemini(body, model, conf.apiKey, conf.baseUrl || "https://generativelanguage.googleapis.com/v1beta"), { headers });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Anthropic-compatible handlers (Passthrough)
|
|
51
|
+
let baseUrl = "";
|
|
52
|
+
let apiKey = "";
|
|
53
|
+
let extraHeaders: Record<string, string> = {};
|
|
54
|
+
|
|
55
|
+
if (provider === "anthropic") {
|
|
56
|
+
const conf = providers.anthropic;
|
|
57
|
+
if (!conf?.apiKey) throw new Error("Missing Anthropic API Key");
|
|
58
|
+
baseUrl = conf.baseUrl || "https://api.anthropic.com";
|
|
59
|
+
apiKey = conf.apiKey;
|
|
60
|
+
extraHeaders["x-api-key"] = apiKey;
|
|
61
|
+
} else if (provider === "minimax") {
|
|
62
|
+
apiKey = config.minimaxApiKey || "";
|
|
63
|
+
if (!apiKey) throw new Error("Missing Minimax API Key");
|
|
64
|
+
baseUrl = "https://api.minimax.io/anthropic";
|
|
65
|
+
extraHeaders["Authorization"] = `Bearer ${apiKey}`;
|
|
66
|
+
} else {
|
|
67
|
+
// GLM (Default)
|
|
68
|
+
apiKey = config.zaiApiKey || "";
|
|
69
|
+
if (!apiKey) throw new Error("Missing Z.AI API Key");
|
|
70
|
+
baseUrl = "https://api.z.ai/api/anthropic";
|
|
71
|
+
extraHeaders["Authorization"] = `Bearer ${apiKey}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const apiHeaders = {
|
|
75
|
+
"Content-Type": "application/json",
|
|
76
|
+
"anthropic-version": "2023-06-01",
|
|
77
|
+
...extraHeaders
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return new Response(streamPassThrough(body, baseUrl, apiHeaders), { headers });
|
|
81
|
+
|
|
82
|
+
} catch (e: any) {
|
|
83
|
+
return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: { "Content-Type": "application/json" } });
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ProviderKey = "openai" | "openrouter" | "gemini" | "glm" | "anthropic" | "minimax";
|
|
2
|
+
|
|
3
|
+
export interface ProviderModel {
|
|
4
|
+
provider: ProviderKey;
|
|
5
|
+
model: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface AnthropicMessage {
|
|
9
|
+
role: "user" | "assistant";
|
|
10
|
+
content: string | Array<{ type: "text"; text: string } | { type: "tool_result"; content: string | any }>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AnthropicRequest {
|
|
14
|
+
model: string; // "provider:model" or just "model"
|
|
15
|
+
messages: AnthropicMessage[];
|
|
16
|
+
max_tokens: number;
|
|
17
|
+
temperature?: number;
|
|
18
|
+
system?: string;
|
|
19
|
+
tools?: any[];
|
|
20
|
+
stream?: boolean;
|
|
21
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export function createSSEMessage(event: string, data: any) {
|
|
2
|
+
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function createStartMessage(model: string) {
|
|
6
|
+
const id = `msg_${Date.now()}`;
|
|
7
|
+
return (
|
|
8
|
+
createSSEMessage("message_start", {
|
|
9
|
+
type: "message_start",
|
|
10
|
+
message: {
|
|
11
|
+
id,
|
|
12
|
+
type: "message",
|
|
13
|
+
role: "assistant",
|
|
14
|
+
model,
|
|
15
|
+
content: [],
|
|
16
|
+
stop_reason: null,
|
|
17
|
+
stop_sequence: null,
|
|
18
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
19
|
+
}
|
|
20
|
+
}) +
|
|
21
|
+
createSSEMessage("content_block_start", {
|
|
22
|
+
type: "content_block_start",
|
|
23
|
+
index: 0,
|
|
24
|
+
content_block: { type: "text", text: "" }
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createDelta(text: string) {
|
|
30
|
+
if (!text) return "";
|
|
31
|
+
return createSSEMessage("content_block_delta", {
|
|
32
|
+
type: "content_block_delta",
|
|
33
|
+
index: 0,
|
|
34
|
+
delta: { type: "text_delta", text }
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createStopMessage() {
|
|
39
|
+
return (
|
|
40
|
+
createSSEMessage("content_block_stop", { type: "content_block_stop", index: 0 }) +
|
|
41
|
+
createSSEMessage("message_delta", {
|
|
42
|
+
type: "message_delta",
|
|
43
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
44
|
+
usage: { output_tokens: 0 }
|
|
45
|
+
}) +
|
|
46
|
+
createSSEMessage("message_stop", { type: "message_stop" })
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class ApiError extends Error {
|
|
51
|
+
constructor(public message: string, public statusCode: number = 500) {
|
|
52
|
+
super(message);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
"noUncheckedIndexedAccess": true,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false
|
|
28
|
+
}
|
|
29
|
+
}
|