opencode-catpaw-auth 1.2.0 → 1.3.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/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/src/config.d.ts +14 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +44 -0
- package/dist/src/constants.d.ts +23 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +22 -0
- package/dist/src/plugin.d.ts +15 -0
- package/dist/src/plugin.d.ts.map +1 -0
- package/dist/src/plugin.js +111 -0
- package/dist/src/request.d.ts +12 -0
- package/dist/src/request.d.ts.map +1 -0
- package/dist/src/request.js +47 -0
- package/dist/src/types.d.ts +78 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +5 -0
- package/package.json +25 -10
- package/index.ts +0 -1
- package/src/config.ts +0 -44
- package/src/plugin.ts +0 -131
- package/src/request.ts +0 -47
- package/src/types.ts +0 -88
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从 mcopilot-cli 配置文件中加载 API key
|
|
3
|
+
* 配置文件路径: ~/.config/mcopilot-cli/.config.yaml
|
|
4
|
+
*/
|
|
5
|
+
export declare function loadApiKeyFromConfig(): Promise<string | null>;
|
|
6
|
+
/**
|
|
7
|
+
* 获取工作目录(用户主目录)
|
|
8
|
+
*/
|
|
9
|
+
export declare function getWorkingDir(): string;
|
|
10
|
+
/**
|
|
11
|
+
* 获取配置文件路径(用于调试)
|
|
12
|
+
*/
|
|
13
|
+
export declare function getConfigPath(): string;
|
|
14
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAyBnE;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
const CONFIG_PATH = join(homedir(), ".config", "mcopilot-cli", ".config.yaml");
|
|
6
|
+
/**
|
|
7
|
+
* 从 mcopilot-cli 配置文件中加载 API key
|
|
8
|
+
* 配置文件路径: ~/.config/mcopilot-cli/.config.yaml
|
|
9
|
+
*/
|
|
10
|
+
export async function loadApiKeyFromConfig() {
|
|
11
|
+
try {
|
|
12
|
+
// 检查配置文件是否存在
|
|
13
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
14
|
+
console.error(`CatPaw config file not found: ${CONFIG_PATH}`);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
// 读取并解析 YAML
|
|
18
|
+
const content = readFileSync(CONFIG_PATH, "utf-8");
|
|
19
|
+
const config = yaml.load(content);
|
|
20
|
+
// 获取 AUTHORIZATION 字段
|
|
21
|
+
const apiKey = config?.AUTHORIZATION;
|
|
22
|
+
if (typeof apiKey !== "string" || !apiKey) {
|
|
23
|
+
console.error("AUTHORIZATION not found in config file");
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return apiKey;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error("Failed to load CatPaw API key:", error);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 获取工作目录(用户主目录)
|
|
35
|
+
*/
|
|
36
|
+
export function getWorkingDir() {
|
|
37
|
+
return homedir();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 获取配置文件路径(用于调试)
|
|
41
|
+
*/
|
|
42
|
+
export function getConfigPath() {
|
|
43
|
+
return CONFIG_PATH;
|
|
44
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constants for Catpaw Auth plugin
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Provider identifier for Catpaw/Quotio
|
|
6
|
+
*/
|
|
7
|
+
export declare const CATPAW_PROVIDER_ID = "quotio";
|
|
8
|
+
/**
|
|
9
|
+
* Default base URL for Quotio API
|
|
10
|
+
*/
|
|
11
|
+
export declare const QUOTIO_DEFAULT_BASE_URL = "http://127.0.0.1:8317/v1";
|
|
12
|
+
/**
|
|
13
|
+
* Anthropic API version header
|
|
14
|
+
*/
|
|
15
|
+
export declare const ANTHROPIC_API_VERSION = "2023-06-01";
|
|
16
|
+
/**
|
|
17
|
+
* Headers for Anthropic API requests
|
|
18
|
+
*/
|
|
19
|
+
export declare const ANTHROPIC_HEADERS: {
|
|
20
|
+
readonly "anthropic-version": "2023-06-01";
|
|
21
|
+
readonly "content-type": "application/json";
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C;;GAEG;AACH,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE;;GAEG;AACH,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,iBAAiB;;;CAGpB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constants for Catpaw Auth plugin
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Provider identifier for Catpaw/Quotio
|
|
6
|
+
*/
|
|
7
|
+
export const CATPAW_PROVIDER_ID = "quotio";
|
|
8
|
+
/**
|
|
9
|
+
* Default base URL for Quotio API
|
|
10
|
+
*/
|
|
11
|
+
export const QUOTIO_DEFAULT_BASE_URL = "http://127.0.0.1:8317/v1";
|
|
12
|
+
/**
|
|
13
|
+
* Anthropic API version header
|
|
14
|
+
*/
|
|
15
|
+
export const ANTHROPIC_API_VERSION = "2023-06-01";
|
|
16
|
+
/**
|
|
17
|
+
* Headers for Anthropic API requests
|
|
18
|
+
*/
|
|
19
|
+
export const ANTHROPIC_HEADERS = {
|
|
20
|
+
"anthropic-version": ANTHROPIC_API_VERSION,
|
|
21
|
+
"content-type": "application/json",
|
|
22
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
export declare const CATPAW_PROVIDER_ID = "anthropic";
|
|
3
|
+
/**
|
|
4
|
+
* CatPaw Anthropic Proxy Plugin for OpenCode
|
|
5
|
+
*
|
|
6
|
+
* This plugin acts as a proxy to mcli.sankuai.com with automatic authentication:
|
|
7
|
+
* 1. Loads API key from ~/.config/mcopilot-cli/.config.yaml
|
|
8
|
+
* 2. Intercepts requests to localhost:2819
|
|
9
|
+
* 3. Forwards to https://mcli.sankuai.com with auth headers
|
|
10
|
+
* 4. Supports streaming responses
|
|
11
|
+
*/
|
|
12
|
+
declare const plugin: Plugin;
|
|
13
|
+
export default plugin;
|
|
14
|
+
export { plugin as CatPawAuthPlugin };
|
|
15
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAgC,MAAM,qBAAqB,CAAC;AAKhF,eAAO,MAAM,kBAAkB,cAAc,CAAC;AAE9C;;;;;;;;GAQG;AACH,QAAA,MAAM,MAAM,EAAE,MAqHb,CAAC;AAEF,eAAe,MAAM,CAAC;AACtB,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { loadApiKeyFromConfig, getWorkingDir } from "./config";
|
|
2
|
+
import { isTargetRequest, prepareProxyRequest } from "./request";
|
|
3
|
+
export const CATPAW_PROVIDER_ID = "anthropic";
|
|
4
|
+
/**
|
|
5
|
+
* CatPaw Anthropic Proxy Plugin for OpenCode
|
|
6
|
+
*
|
|
7
|
+
* This plugin acts as a proxy to mcli.sankuai.com with automatic authentication:
|
|
8
|
+
* 1. Loads API key from ~/.config/mcopilot-cli/.config.yaml
|
|
9
|
+
* 2. Intercepts requests to localhost:2819
|
|
10
|
+
* 3. Forwards to https://mcli.sankuai.com with auth headers
|
|
11
|
+
* 4. Supports streaming responses
|
|
12
|
+
*/
|
|
13
|
+
const plugin = async ({ client, }) => {
|
|
14
|
+
// 预加载配置
|
|
15
|
+
const apiKey = await loadApiKeyFromConfig();
|
|
16
|
+
const workingDir = getWorkingDir();
|
|
17
|
+
if (!apiKey) {
|
|
18
|
+
client.app.log({
|
|
19
|
+
body: {
|
|
20
|
+
service: "catpaw-auth",
|
|
21
|
+
level: "error",
|
|
22
|
+
message: "Failed to load API key from ~/.config/mcopilot-cli/.config.yaml",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
client.app.log({
|
|
28
|
+
body: {
|
|
29
|
+
service: "catpaw-auth",
|
|
30
|
+
level: "info",
|
|
31
|
+
message: "CatPaw proxy loaded successfully",
|
|
32
|
+
extra: { workingDir, apiKeyPrefix: apiKey.slice(0, 8) + "..." },
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
config: async (config) => {
|
|
38
|
+
// 配置 anthropic provider,指向本地代理
|
|
39
|
+
config.provider = config.provider || {};
|
|
40
|
+
config.provider[CATPAW_PROVIDER_ID] = {
|
|
41
|
+
npm: "@ai-sdk/anthropic",
|
|
42
|
+
name: "CatPaw Anthropic Proxy",
|
|
43
|
+
options: {
|
|
44
|
+
baseURL: "http://127.0.0.1:2819/v1",
|
|
45
|
+
},
|
|
46
|
+
models: {
|
|
47
|
+
"claude-sonnet-4.5": {
|
|
48
|
+
name: "Claude Sonnet 4.5",
|
|
49
|
+
limit: {
|
|
50
|
+
context: 200000,
|
|
51
|
+
output: 8192,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
"claude-haiku-4.5": {
|
|
55
|
+
name: "Claude Haiku 4.5",
|
|
56
|
+
limit: {
|
|
57
|
+
context: 200000,
|
|
58
|
+
output: 64000,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
"claude-opus-4.5": {
|
|
62
|
+
name: "Claude Opus 4.5",
|
|
63
|
+
limit: {
|
|
64
|
+
context: 200000,
|
|
65
|
+
output: 64000,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
auth: {
|
|
72
|
+
provider: CATPAW_PROVIDER_ID,
|
|
73
|
+
loader: async (getAuth, provider) => {
|
|
74
|
+
return {
|
|
75
|
+
apiKey: "dummy-key", // 使用自定义 fetch,这里只需要一个非空值
|
|
76
|
+
async fetch(input, init) {
|
|
77
|
+
// 检查是否是目标请求(发送到 localhost:2819)
|
|
78
|
+
if (!isTargetRequest(input)) {
|
|
79
|
+
return fetch(input, init);
|
|
80
|
+
}
|
|
81
|
+
if (!apiKey) {
|
|
82
|
+
throw new Error("CatPaw API key not loaded. Check ~/.config/mcopilot-cli/.config.yaml");
|
|
83
|
+
}
|
|
84
|
+
// 准备代理请求,转发到目标服务器
|
|
85
|
+
const { request, init: requestInit } = prepareProxyRequest(input, init, apiKey, workingDir);
|
|
86
|
+
// 发送请求
|
|
87
|
+
return fetch(request, requestInit);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
methods: [
|
|
92
|
+
{
|
|
93
|
+
type: "api",
|
|
94
|
+
label: "Auto-config from mcopilot-cli",
|
|
95
|
+
authorize: async () => {
|
|
96
|
+
if (!apiKey) {
|
|
97
|
+
return { type: "failed" };
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
type: "success",
|
|
101
|
+
key: apiKey,
|
|
102
|
+
provider: CATPAW_PROVIDER_ID,
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
export default plugin;
|
|
111
|
+
export { plugin as CatPawAuthPlugin };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查请求是否是发送到代理服务器的请求
|
|
3
|
+
*/
|
|
4
|
+
export declare function isTargetRequest(input: RequestInfo | URL | string): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* 准备代理请求,转发到目标服务器并注入认证 headers
|
|
7
|
+
*/
|
|
8
|
+
export declare function prepareProxyRequest(input: RequestInfo | URL | string, init: RequestInit | undefined, apiKey: string, workingDir: string): {
|
|
9
|
+
request: RequestInfo | URL | string;
|
|
10
|
+
init: RequestInit;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,GAAG,MAAM,GAAG,OAAO,CAQ1E;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,WAAW,GAAG,GAAG,GAAG,MAAM,EACjC,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,GACjB;IAAE,OAAO,EAAE,WAAW,GAAG,GAAG,GAAG,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CAwC5D"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const TARGET_BASE_URL = "https://mcli.sankuai.com";
|
|
2
|
+
const PROXY_HOST = "127.0.0.1:2819";
|
|
3
|
+
/**
|
|
4
|
+
* 检查请求是否是发送到代理服务器的请求
|
|
5
|
+
*/
|
|
6
|
+
export function isTargetRequest(input) {
|
|
7
|
+
const url = typeof input === "string"
|
|
8
|
+
? input
|
|
9
|
+
: input instanceof URL
|
|
10
|
+
? input.toString()
|
|
11
|
+
: input.url;
|
|
12
|
+
return url.includes(PROXY_HOST);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 准备代理请求,转发到目标服务器并注入认证 headers
|
|
16
|
+
*/
|
|
17
|
+
export function prepareProxyRequest(input, init, apiKey, workingDir) {
|
|
18
|
+
// 解析原始 URL
|
|
19
|
+
const originalUrl = typeof input === "string"
|
|
20
|
+
? input
|
|
21
|
+
: input instanceof URL
|
|
22
|
+
? input.toString()
|
|
23
|
+
: input.url;
|
|
24
|
+
// 将 localhost:2819 替换为目标服务器
|
|
25
|
+
const targetUrl = originalUrl.replace(/http:\/\/127\.0\.0\.1:2819/, TARGET_BASE_URL);
|
|
26
|
+
// 准备 headers
|
|
27
|
+
const headers = new Headers(init?.headers);
|
|
28
|
+
// 移除可能冲突的 headers
|
|
29
|
+
headers.delete("host");
|
|
30
|
+
headers.delete("authorization");
|
|
31
|
+
// 注入 CatPaw 必需的认证 headers
|
|
32
|
+
headers.set("x-api-key", apiKey);
|
|
33
|
+
headers.set("x-working-dir", workingDir);
|
|
34
|
+
// 确保 Content-Type 存在
|
|
35
|
+
if (!headers.has("Content-Type") && !headers.has("content-type")) {
|
|
36
|
+
headers.set("Content-Type", "application/json");
|
|
37
|
+
}
|
|
38
|
+
// 创建新的请求配置
|
|
39
|
+
const requestInit = {
|
|
40
|
+
...init,
|
|
41
|
+
headers,
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
request: targetUrl,
|
|
45
|
+
init: requestInit,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenCode Plugin Types
|
|
3
|
+
* 这些类型定义基于 @opencode-ai/plugin
|
|
4
|
+
*/
|
|
5
|
+
export interface PluginContext {
|
|
6
|
+
project: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
directory: string;
|
|
10
|
+
};
|
|
11
|
+
directory: string;
|
|
12
|
+
worktree: string;
|
|
13
|
+
client: PluginClient;
|
|
14
|
+
}
|
|
15
|
+
export interface PluginClient {
|
|
16
|
+
app: {
|
|
17
|
+
log: (params: {
|
|
18
|
+
body: {
|
|
19
|
+
service: string;
|
|
20
|
+
level: "debug" | "info" | "warn" | "error";
|
|
21
|
+
message: string;
|
|
22
|
+
extra?: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
}) => Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export interface PluginResult {
|
|
28
|
+
config?: (config: Config) => Promise<void> | void;
|
|
29
|
+
auth?: {
|
|
30
|
+
provider: string;
|
|
31
|
+
loader: (getAuth: GetAuth, provider: Provider) => Promise<LoaderResult | null>;
|
|
32
|
+
methods: AuthMethod[];
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export interface Config {
|
|
36
|
+
provider?: Record<string, ProviderConfig>;
|
|
37
|
+
command?: Record<string, CommandConfig>;
|
|
38
|
+
}
|
|
39
|
+
export interface ProviderConfig {
|
|
40
|
+
npm?: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
options?: Record<string, unknown>;
|
|
43
|
+
models?: Record<string, ModelConfig>;
|
|
44
|
+
}
|
|
45
|
+
export interface ModelConfig {
|
|
46
|
+
name?: string;
|
|
47
|
+
limit?: {
|
|
48
|
+
context?: number;
|
|
49
|
+
output?: number;
|
|
50
|
+
};
|
|
51
|
+
options?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
export interface CommandConfig {
|
|
54
|
+
description: string;
|
|
55
|
+
template: string;
|
|
56
|
+
}
|
|
57
|
+
export interface Provider {
|
|
58
|
+
options?: Record<string, unknown>;
|
|
59
|
+
models?: Record<string, ModelConfig>;
|
|
60
|
+
}
|
|
61
|
+
export interface AuthMethod {
|
|
62
|
+
provider?: string;
|
|
63
|
+
label: string;
|
|
64
|
+
type: "oauth" | "api" | "token";
|
|
65
|
+
authorize?: (inputs?: Record<string, string>) => Promise<{
|
|
66
|
+
type: "success";
|
|
67
|
+
key: string;
|
|
68
|
+
provider?: string;
|
|
69
|
+
} | {
|
|
70
|
+
type: "failed";
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
export type GetAuth = () => Promise<unknown>;
|
|
74
|
+
export interface LoaderResult {
|
|
75
|
+
apiKey: string;
|
|
76
|
+
fetch: (input: RequestInfo | URL | string, init?: RequestInit) => Promise<Response>;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-catpaw-auth",
|
|
3
|
-
"
|
|
4
|
-
"version": "1.2.0",
|
|
3
|
+
"version": "1.3.0",
|
|
5
4
|
"description": "CatPaw AI provider plugin for OpenCode - Auto-auth from mcopilot-cli config",
|
|
6
5
|
"author": "zu1k",
|
|
7
6
|
"repository": {
|
|
@@ -19,26 +18,42 @@
|
|
|
19
18
|
"ai",
|
|
20
19
|
"provider"
|
|
21
20
|
],
|
|
22
|
-
"files": [
|
|
23
|
-
"index.ts",
|
|
24
|
-
"src"
|
|
25
|
-
],
|
|
26
21
|
"license": "MIT",
|
|
27
22
|
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
28
37
|
"scripts": {
|
|
38
|
+
"build": "tsc",
|
|
29
39
|
"dev": "bun run --watch index.ts",
|
|
30
40
|
"check": "tsc --noEmit",
|
|
31
|
-
"clean": "rm -rf node_modules bun.lock"
|
|
41
|
+
"clean": "rm -rf dist node_modules bun.lock",
|
|
42
|
+
"prepublishOnly": "npm run build"
|
|
32
43
|
},
|
|
33
44
|
"devDependencies": {
|
|
34
45
|
"@types/bun": "latest",
|
|
35
|
-
"@types/js-yaml": "^4.0.9"
|
|
36
|
-
|
|
37
|
-
"peerDependencies": {
|
|
46
|
+
"@types/js-yaml": "^4.0.9",
|
|
47
|
+
"@types/node": "^22.13.9",
|
|
38
48
|
"typescript": "^5.9.3"
|
|
39
49
|
},
|
|
40
50
|
"dependencies": {
|
|
41
51
|
"@opencode-ai/plugin": "^1.2.10",
|
|
52
|
+
"@opencode-ai/sdk": "^1.2.10",
|
|
42
53
|
"js-yaml": "^4.1.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@opencode-ai/plugin": "^1.2.0",
|
|
57
|
+
"@opencode-ai/sdk": "^1.2.0"
|
|
43
58
|
}
|
|
44
59
|
}
|
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { CatPawAuthPlugin } from "./src/plugin";
|
package/src/config.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { readFileSync, existsSync } from "fs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
import { homedir } from "os";
|
|
4
|
-
import yaml from "js-yaml";
|
|
5
|
-
|
|
6
|
-
const CONFIG_PATH = join(homedir(), ".config", "mcopilot-cli", ".config.yaml");
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 从 mcopilot-cli 配置文件中加载 API key
|
|
10
|
-
* 配置文件路径: ~/.config/mcopilot-cli/.config.yaml
|
|
11
|
-
*/
|
|
12
|
-
export async function loadApiKeyFromConfig(): Promise<string | null> {
|
|
13
|
-
try {
|
|
14
|
-
// 检查配置文件是否存在
|
|
15
|
-
if (!existsSync(CONFIG_PATH)) {
|
|
16
|
-
console.error(`CatPaw config file not found: ${CONFIG_PATH}`);
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 读取并解析 YAML
|
|
21
|
-
const content = readFileSync(CONFIG_PATH, "utf-8");
|
|
22
|
-
const config = yaml.load(content) as Record<string, unknown>;
|
|
23
|
-
|
|
24
|
-
// 获取 AUTHORIZATION 字段
|
|
25
|
-
const apiKey = config?.AUTHORIZATION;
|
|
26
|
-
|
|
27
|
-
if (typeof apiKey !== "string" || !apiKey) {
|
|
28
|
-
console.error("AUTHORIZATION not found in config file");
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return apiKey;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
console.error("Failed to load CatPaw API key:", error);
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 获取配置文件路径(用于调试)
|
|
41
|
-
*/
|
|
42
|
-
export function getConfigPath(): string {
|
|
43
|
-
return CONFIG_PATH;
|
|
44
|
-
}
|
package/src/plugin.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import type { PluginContext, PluginResult, Provider } from "./types";
|
|
2
|
-
import { loadApiKeyFromConfig } from "./config";
|
|
3
|
-
import { isCatPawRequest, prepareCatPawRequest } from "./request";
|
|
4
|
-
|
|
5
|
-
export const CATPAW_PROVIDER_ID = "catpaw";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* CatPaw AI Provider Plugin for OpenCode
|
|
9
|
-
*
|
|
10
|
-
* This plugin automatically:
|
|
11
|
-
* 1. Loads API key from ~/.config/mcopilot-cli/.config.yaml
|
|
12
|
-
* 2. Injects required headers (x-api-key, x-working-dir)
|
|
13
|
-
* 3. Supports multiple models: LongCat-Flash-Chat, kimi-k2.5, glm-5, MiniMax-M2.5, claude-sonnet-4.5
|
|
14
|
-
*/
|
|
15
|
-
export const CatPawAuthPlugin = async ({ client }: PluginContext): Promise<PluginResult> => ({
|
|
16
|
-
config: async (config) => {
|
|
17
|
-
// 配置 CatPaw provider
|
|
18
|
-
config.provider = config.provider || {};
|
|
19
|
-
config.provider[CATPAW_PROVIDER_ID] = {
|
|
20
|
-
npm: "@ai-sdk/openai-compatible",
|
|
21
|
-
name: "CatPaw AI",
|
|
22
|
-
options: {
|
|
23
|
-
baseURL: "https://mcli.sankuai.com/v1",
|
|
24
|
-
},
|
|
25
|
-
models: {
|
|
26
|
-
"LongCat-Flash-Chat": {
|
|
27
|
-
name: "LongCat Flash Chat",
|
|
28
|
-
limit: {
|
|
29
|
-
context: 256000,
|
|
30
|
-
output: 8192,
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
"kimi-k2.5": {
|
|
34
|
-
name: "Kimi K2.5",
|
|
35
|
-
limit: {
|
|
36
|
-
context: 256000,
|
|
37
|
-
output: 8192,
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
"glm-5": {
|
|
41
|
-
name: "GLM-5",
|
|
42
|
-
limit: {
|
|
43
|
-
context: 200000,
|
|
44
|
-
output: 8192,
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
"MiniMax-M2.5": {
|
|
48
|
-
name: "MiniMax M2.5",
|
|
49
|
-
limit: {
|
|
50
|
-
context: 200000,
|
|
51
|
-
output: 8192,
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
"claude-sonnet-4.5": {
|
|
55
|
-
name: "Claude Sonnet 4.5",
|
|
56
|
-
limit: {
|
|
57
|
-
context: 200000,
|
|
58
|
-
output: 8192,
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
},
|
|
64
|
-
auth: {
|
|
65
|
-
provider: CATPAW_PROVIDER_ID,
|
|
66
|
-
loader: async (getAuth, provider: Provider) => {
|
|
67
|
-
// 从配置文件加载 API key
|
|
68
|
-
const apiKey = await loadApiKeyFromConfig();
|
|
69
|
-
|
|
70
|
-
if (!apiKey) {
|
|
71
|
-
client.app.log({
|
|
72
|
-
body: {
|
|
73
|
-
service: "catpaw-auth",
|
|
74
|
-
level: "error",
|
|
75
|
-
message: "Failed to load API key from ~/.config/mcopilot-cli/.config.yaml",
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const workingDir = process.env.HOME || process.env.USERPROFILE || "/";
|
|
82
|
-
|
|
83
|
-
client.app.log({
|
|
84
|
-
body: {
|
|
85
|
-
service: "catpaw-auth",
|
|
86
|
-
level: "info",
|
|
87
|
-
message: "CatPaw auth loaded successfully",
|
|
88
|
-
extra: { workingDir },
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
apiKey: "", // 使用自定义 fetch,不需要标准 apiKey
|
|
94
|
-
async fetch(input, init) {
|
|
95
|
-
// 只处理 CatPaw 相关的请求
|
|
96
|
-
if (!isCatPawRequest(input)) {
|
|
97
|
-
return fetch(input, init);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 准备请求,注入必要的 headers
|
|
101
|
-
const { request, init: requestInit } = prepareCatPawRequest(input, init, apiKey, workingDir);
|
|
102
|
-
|
|
103
|
-
// 发送请求
|
|
104
|
-
return fetch(request, requestInit);
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
},
|
|
108
|
-
methods: [
|
|
109
|
-
{
|
|
110
|
-
provider: CATPAW_PROVIDER_ID,
|
|
111
|
-
type: "api",
|
|
112
|
-
label: "Auto-config from mcopilot-cli",
|
|
113
|
-
authorize: async () => {
|
|
114
|
-
// 从 mcopilot-cli 配置文件加载 API key
|
|
115
|
-
const apiKey = await loadApiKeyFromConfig();
|
|
116
|
-
|
|
117
|
-
if (!apiKey) {
|
|
118
|
-
return { type: "failed" as const };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 返回成功,使用 apiKey 作为 key
|
|
122
|
-
return {
|
|
123
|
-
type: "success" as const,
|
|
124
|
-
key: apiKey,
|
|
125
|
-
provider: CATPAW_PROVIDER_ID,
|
|
126
|
-
};
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
},
|
|
131
|
-
});
|
package/src/request.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 检查请求是否是 CatPaw API 请求
|
|
3
|
-
*/
|
|
4
|
-
export function isCatPawRequest(input: RequestInfo | URL | string): boolean {
|
|
5
|
-
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
6
|
-
return url.includes("mcli.sankuai.com");
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 准备 CatPaw 请求,注入必要的 headers
|
|
11
|
-
*/
|
|
12
|
-
export function prepareCatPawRequest(
|
|
13
|
-
input: RequestInfo | URL | string,
|
|
14
|
-
init: RequestInit | undefined,
|
|
15
|
-
apiKey: string,
|
|
16
|
-
workingDir: string
|
|
17
|
-
): { request: RequestInfo | URL | string; init: RequestInit } {
|
|
18
|
-
// 解析原始 URL
|
|
19
|
-
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
20
|
-
|
|
21
|
-
// 准备 headers
|
|
22
|
-
const headers = new Headers(init?.headers);
|
|
23
|
-
|
|
24
|
-
// 移除可能冲突的 headers
|
|
25
|
-
headers.delete("host");
|
|
26
|
-
headers.delete("authorization");
|
|
27
|
-
|
|
28
|
-
// 注入 CatPaw 必需的 headers
|
|
29
|
-
headers.set("x-api-key", apiKey);
|
|
30
|
-
headers.set("x-working-dir", workingDir);
|
|
31
|
-
|
|
32
|
-
// 确保 Content-Type 存在
|
|
33
|
-
if (!headers.has("Content-Type") && !headers.has("content-type")) {
|
|
34
|
-
headers.set("Content-Type", "application/json");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 创建新的请求配置
|
|
38
|
-
const requestInit: RequestInit = {
|
|
39
|
-
...init,
|
|
40
|
-
headers,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
request: input,
|
|
45
|
-
init: requestInit,
|
|
46
|
-
};
|
|
47
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenCode Plugin Types
|
|
3
|
-
* 这些类型定义基于 @opencode-ai/plugin
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export interface PluginContext {
|
|
7
|
-
project: {
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
directory: string;
|
|
11
|
-
};
|
|
12
|
-
directory: string;
|
|
13
|
-
worktree: string;
|
|
14
|
-
client: PluginClient;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface PluginClient {
|
|
18
|
-
app: {
|
|
19
|
-
log: (params: {
|
|
20
|
-
body: {
|
|
21
|
-
service: string;
|
|
22
|
-
level: "debug" | "info" | "warn" | "error";
|
|
23
|
-
message: string;
|
|
24
|
-
extra?: Record<string, unknown>;
|
|
25
|
-
};
|
|
26
|
-
}) => Promise<void>;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface PluginResult {
|
|
31
|
-
config?: (config: Config) => Promise<void> | void;
|
|
32
|
-
auth?: {
|
|
33
|
-
provider: string;
|
|
34
|
-
loader: (
|
|
35
|
-
getAuth: GetAuth,
|
|
36
|
-
provider: Provider
|
|
37
|
-
) => Promise<LoaderResult | null>;
|
|
38
|
-
methods: AuthMethod[];
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface Config {
|
|
43
|
-
provider?: Record<string, ProviderConfig>;
|
|
44
|
-
command?: Record<string, CommandConfig>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface ProviderConfig {
|
|
48
|
-
npm?: string;
|
|
49
|
-
name?: string;
|
|
50
|
-
options?: Record<string, unknown>;
|
|
51
|
-
models?: Record<string, ModelConfig>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface ModelConfig {
|
|
55
|
-
name?: string;
|
|
56
|
-
limit?: {
|
|
57
|
-
context?: number;
|
|
58
|
-
output?: number;
|
|
59
|
-
};
|
|
60
|
-
options?: Record<string, unknown>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface CommandConfig {
|
|
64
|
-
description: string;
|
|
65
|
-
template: string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface Provider {
|
|
69
|
-
options?: Record<string, unknown>;
|
|
70
|
-
models?: Record<string, ModelConfig>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface AuthMethod {
|
|
74
|
-
provider?: string;
|
|
75
|
-
label: string;
|
|
76
|
-
type: "oauth" | "api" | "token";
|
|
77
|
-
authorize?: (inputs?: Record<string, string>) => Promise<
|
|
78
|
-
| { type: "success"; key: string; provider?: string }
|
|
79
|
-
| { type: "failed" }
|
|
80
|
-
>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type GetAuth = () => Promise<unknown>;
|
|
84
|
-
|
|
85
|
-
export interface LoaderResult {
|
|
86
|
-
apiKey: string;
|
|
87
|
-
fetch: (input: RequestInfo | URL | string, init?: RequestInit) => Promise<Response>;
|
|
88
|
-
}
|