glmcode 0.1.3 → 0.1.5

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/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## 0.1.4
4
+ - Cross-platform OS and shell detection.
5
+ - Safe browser launching for Termux, Linux, macOS and Windows.
6
+ - Headless Linux/Alpine environments no longer crash when no browser launcher exists.
7
+ - Added `glmcode auth login --no-browser`.
8
+ - Added shell inspection commands.
package/README.md CHANGED
@@ -1,50 +1,29 @@
1
- # GLMCode
1
+ # GLMCode 0.1.4
2
2
 
3
- GLMCode 是一个 Node.js / npm GLM Coding Agent,界面和交互以移动端 Python 原版 `glmcode.py` 为基础,同时提供文件工具、Shell、Skills、Plugins、MCP、Session 和模型切换。
4
-
5
- ## 安装
3
+ Cross-platform GLM CLI for Termux, Linux/Alpine, macOS and Windows.
6
4
 
5
+ Install:
7
6
  ```bash
8
7
  npm install -g glmcode
9
8
  ```
10
9
 
11
- npm 会自动安装 package.json 中声明的运行依赖;不需要手动 `pip install`,也不需要手动创建 PATH 脚本。npm 的全局 bin 会提供:
12
-
10
+ Run:
13
11
  ```bash
14
12
  glmcode
15
- glm
16
- gc
17
13
  ```
18
14
 
19
- 三种启动命令。
20
-
21
- ## 登录
15
+ Aliases: `glm`, `gc`
22
16
 
17
+ Authentication:
23
18
  ```bash
24
19
  glmcode auth login
20
+ glmcode auth login --no-browser
25
21
  ```
26
22
 
27
- GLMCode 会尝试在手机、Linux、macOS、Windows 上打开智谱官方平台页面,并自动检测已有的 `ZHIPU_API_KEY` / `GLM_API_KEY` 或本地凭据。
28
-
29
- 注意:智谱公开开发文档当前仍以“登录开放平台 → 创建 API Key → 使用 API Key 调用”为标准流程;普通网页登录并不会自动把 API Key 暴露给第三方 CLI。因此 GLMCode 不会伪造网页登录,也不会从浏览器 Cookie 中窃取凭据。
30
-
31
- ## 依赖
32
-
33
- Python 原版需要 `requests` 与 `prompt_toolkit`,并使用 Python 标准库的 `zipfile`。Node 版对应为:
34
-
35
- - Node.js >= 18:内置 `fetch`、readline、child_process、fs 等能力
36
- - `adm-zip`:自动解压 `skills/*.zip` 技能包
37
-
38
- 用户通过 npm 安装时这些 npm 依赖会自动安装。
39
-
40
- ## 技能
41
-
42
- 支持:
43
-
44
- - `.glmcode/skills/**/SKILL.md`
45
- - `~/.glmcode/skills/**/SKILL.md`
46
- - `skills/*.zip` 自动解压
47
-
48
- ## 启动界面
23
+ Shell inspection:
24
+ ```bash
25
+ glmcode shell
26
+ glmcode shell list
27
+ ```
49
28
 
50
- 保持 Python 原版的 GLM Logo、状态栏、`┃ 输入消息... ┃`、斜杠命令和技能补全风格。
29
+ Headless environments print the login URL instead of crashing when no browser launcher exists.
Binary file
Binary file
package/package.json CHANGED
@@ -1,38 +1,17 @@
1
1
  {
2
2
  "name": "glmcode",
3
- "version": "0.1.3",
4
- "description": "GLM Code · mobile coding agent with the GLM terminal UI, tools, skills, plugins and MCP.",
3
+ "version": "0.1.5",
4
+ "description": "Cross-platform GLM coding agent CLI",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "glmcode": "src/cli.js",
8
8
  "glm": "src/cli.js",
9
9
  "gc": "src/cli.js"
10
10
  },
11
- "engines": {
12
- "node": ">=18"
13
- },
14
- "files": [
15
- "src",
16
- "templates",
17
- "README.md",
18
- "LICENSE",
19
- "glmcode.example.json"
20
- ],
21
11
  "scripts": {
22
12
  "start": "node src/cli.js",
23
- "check": "node --check src/cli.js"
13
+ "check": "node --check src/cli.js && node --check src/auth.js && node --check src/platform/shell.js && node --check src/platform/browser.js"
24
14
  },
25
- "keywords": [
26
- "glm",
27
- "zhipu",
28
- "coding-agent",
29
- "cli",
30
- "mcp",
31
- "skills",
32
- "plugins"
33
- ],
34
- "license": "MIT",
35
- "dependencies": {
36
- "adm-zip": "^0.5.16"
37
- }
15
+ "engines": { "node": ">=18" },
16
+ "license": "MIT"
38
17
  }
package/src/agent.js CHANGED
@@ -1,68 +1,46 @@
1
- import { chatCompletion, streamCompletion } from './provider.js';
2
- import { TOOL_DEFS, makeToolExecutor } from './tools.js';
3
- import { loadSkills, loadPlugins, skillsPrompt } from './skills.js';
4
- import { discoverMcpTools, callMcpTool } from './mcp.js';
5
- import { nowId } from './utils.js';
6
- import fs from 'node:fs/promises';
7
- import path from 'node:path';
8
- import { SESSIONS_FILE, readJson, writeJson } from './config.js';
1
+ import { getApiKey } from "./auth.js";
2
+ import { discoverSkills } from "./skills.js";
9
3
 
10
- const BASE_SYSTEM = `You are GLMCode, a terminal coding agent.\nYou work directly in the user's project.\nRules:\n- Inspect before modifying.\n- Prefer small, reversible edits.\n- Use tools for files and commands instead of pretending.\n- Never claim a command/file change happened unless the tool result says so.\n- Keep the user informed about important actions.\n- Respect project instructions and loaded skills.`;
4
+ const endpoint = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
11
5
 
12
- export async function runAgent({ cwd, config, apiKey, prompt, model, sessionId, quiet = false }) {
13
- if (!apiKey) throw new Error('No Zhipu API key. Run: glmcode auth login');
14
- const skills = await loadSkills(cwd);
15
- const plugins = await loadPlugins(cwd, config.plugins);
16
- const pluginTools = plugins.flatMap(p => p.module?.tools || []);
17
- const mcpTools = await discoverMcpTools(Object.entries(config.mcp ?? {}).map(([name, value]) => ({ name, ...value })));
18
- const mcpDefs = mcpTools.filter(t => !t._error).map(t => ({ type: 'function', function: { name: `mcp__${t._mcpName}__${t.name}`, description: `[MCP ${t._mcpName}] ${t.description || t.name}`, parameters: t.inputSchema || { type: 'object', properties: {} } } }));
19
- const tools = [...TOOL_DEFS, ...pluginTools, ...mcpDefs];
20
- const sessionKey = sessionId || nowId('sess');
21
- const sessions = await readJson(SESSIONS_FILE, {});
22
- const session = sessions[sessionKey] || { id: sessionKey, cwd, createdAt: new Date().toISOString(), messages: [] };
23
- const projectInstruction = await readProjectInstruction(cwd);
24
- const activeSkill = config.__activeSkill ? `\n\nACTIVE SKILL INSTRUCTIONS:\n${config.__activeSkill}` : '';
25
- const system = BASE_SYSTEM + (projectInstruction ? `\n\nPROJECT INSTRUCTIONS:\n${projectInstruction}` : '') + skillsPrompt(skills) + activeSkill;
26
- const messages = [{ role: 'system', content: system }, ...session.messages, { role: 'user', content: prompt }];
27
- const execute = makeToolExecutor({ cwd, approval: config.approval });
28
-
29
- for (let turn = 0; turn < 16; turn++) {
30
- if (!quiet) process.stdout.write('\n');
31
- const answer = await streamCompletion({
32
- baseUrl: config.baseUrl, apiKey, model: model || config.model, messages,
33
- tools, maxTokens: config.maxTokens, temperature: config.temperature, thinking: config.thinking,
34
- onText: t => process.stdout.write(t)
6
+ export function createAgent({ getConfig }) {
7
+ return { async run(input) {
8
+ const key = getApiKey();
9
+ if (!key) throw new Error("没有可用的 GLM API Key,请运行 glmcode auth login。");
10
+ const cfg = getConfig();
11
+ const skillText = discoverSkills().map(s => `## Skill: ${s.name}\n${s.content}`).join("\n\n");
12
+ const res = await fetch(endpoint, {
13
+ method: "POST",
14
+ headers: { "Content-Type": "application/json", "Authorization": `Bearer ${key}` },
15
+ body: JSON.stringify({
16
+ model: cfg.model,
17
+ messages: [
18
+ { role: "system", content: `You are GLMCode, a coding assistant.\n${skillText}` },
19
+ { role: "user", content: input }
20
+ ],
21
+ stream: true
22
+ })
35
23
  });
36
- const assistant = { role: 'assistant', content: answer.content || null };
37
- if (answer.tool_calls?.length) assistant.tool_calls = answer.tool_calls;
38
- messages.push(assistant);
39
- if (!answer.tool_calls?.length) break;
40
- for (const call of answer.tool_calls) {
41
- const name = call.function.name;
42
- let args; try { args = JSON.parse(call.function.arguments || '{}'); } catch { args = {}; }
43
- let result;
44
- try {
45
- if (name.startsWith('mcp__')) {
46
- const parts = name.split('__');
47
- const serverName = parts[1]; const toolName = parts.slice(2).join('__');
48
- const match = mcpTools.find(t => t._mcpName === serverName && t.name === toolName);
49
- if (!match) throw new Error(`MCP tool not found: ${name}`);
50
- result = JSON.stringify(await callMcpTool(match, args));
51
- } else { result = await execute(name, args); }
52
- } catch (e) { result = `ERROR: ${e.message}`; }
53
- messages.push({ role: 'tool', tool_call_id: call.id, name, content: String(result) });
54
- if (!quiet) process.stdout.write(`\n[tool:${name}] ${String(result).slice(0, 1200)}\n`);
24
+ if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
25
+ const reader = res.body.getReader();
26
+ const decoder = new TextDecoder();
27
+ let buffer = "";
28
+ while (true) {
29
+ const { value, done } = await reader.read();
30
+ if (done) break;
31
+ buffer += decoder.decode(value, { stream: true });
32
+ const lines = buffer.split("\n");
33
+ buffer = lines.pop() || "";
34
+ for (const line of lines) {
35
+ if (!line.startsWith("data:")) continue;
36
+ const data = line.slice(5).trim();
37
+ if (data === "[DONE]") continue;
38
+ try {
39
+ const delta = JSON.parse(data).choices?.[0]?.delta?.content;
40
+ if (delta) process.stdout.write(delta);
41
+ } catch {}
42
+ }
55
43
  }
56
- }
57
- session.messages = messages.filter(m => m.role !== 'system').slice(-80);
58
- sessions[sessionKey] = session;
59
- await writeJson(SESSIONS_FILE, sessions);
60
- return sessionKey;
61
- }
62
-
63
- async function readProjectInstruction(cwd) {
64
- const files = ['AGENTS.md', 'CLAUDE.md', '.glmcode/INSTRUCTIONS.md'];
65
- const chunks = [];
66
- for (const f of files) { try { chunks.push(`# ${f}\n${await fs.readFile(path.join(cwd, f), 'utf8')}`); } catch {} }
67
- return chunks.join('\n\n');
44
+ process.stdout.write("\n");
45
+ }};
68
46
  }
package/src/auth.js CHANGED
@@ -1,58 +1,40 @@
1
- import fs from 'node:fs/promises';
2
- import os from 'node:os';
3
- import path from 'node:path';
4
- import { spawn } from 'node:child_process';
5
- import { AUTH_FILE, readJson, writeJson, getAuth } from './config.js';
1
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { openBrowser } from "./platform/browser.js";
6
5
 
7
- export const LOGIN_URL = 'https://open.bigmodel.cn/usercenter/apikeys';
8
- export const PLATFORM_URL = 'https://open.bigmodel.cn/';
6
+ const dir = join(homedir(), ".glmcode");
7
+ const authFile = join(dir, "auth.json");
8
+ const loginUrl = "https://open.bigmodel.cn/usercenter/proj-mgmt/apikeys";
9
9
 
10
- export async function openUrl(url) {
11
- const attempts = process.platform === 'win32'
12
- ? [['cmd', ['/c', 'start', '', url]]]
13
- : process.platform === 'darwin'
14
- ? [['open', [url]]]
15
- : [['termux-open-url', [url]], ['xdg-open', [url]]];
16
- for (const [cmd, args] of attempts) {
17
- try {
18
- const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
19
- child.unref();
20
- return true;
21
- } catch {}
10
+ export function getApiKey() {
11
+ for (const k of ["ZHIPU_API_KEY", "GLM_API_KEY", "BIGMODEL_API_KEY"]) {
12
+ if (process.env[k]) return process.env[k];
22
13
  }
23
- return false;
14
+ try {
15
+ const x = JSON.parse(readFileSync(authFile, "utf8"));
16
+ return x.apiKey || x.api_key || "";
17
+ } catch { return ""; }
24
18
  }
25
19
 
26
- export async function detectApiKey() {
27
- const candidates = [
28
- process.env.ZHIPU_API_KEY,
29
- process.env.GLM_API_KEY,
30
- process.env.BIGMODEL_API_KEY,
31
- await getAuth('zhipu')
32
- ].filter(Boolean).map(x => String(x).trim()).filter(Boolean);
33
- return candidates[0] || '';
34
- }
20
+ export async function login({ noBrowser = false } = {}) {
21
+ mkdirSync(dir, { recursive: true });
22
+ console.log("正在打开智谱 GLM 登录/API Key 页面...");
23
+ console.log("GLM API 使用 API Key 进行 API 认证。");
35
24
 
36
- export async function browserLogin() {
37
- console.log('正在打开智谱 GLM 登录/API Key 页面...');
38
- const opened = await openUrl(LOGIN_URL);
39
- if (!opened) {
40
- console.log(`请手动打开: ${LOGIN_URL}`);
41
- }
42
- console.log('');
43
- console.log('登录说明:GLM 官方公开文档目前要求通过智谱开放平台创建 API Key。');
44
- console.log('如果本机已有 ZHIPU_API_KEY / GLM_API_KEY,GLMCode 会自动检测并直接使用。');
45
- console.log('浏览器登录本身不会把网页里的 API Key 自动暴露给第三方 CLI。');
46
- console.log('');
47
- const key = await detectApiKey();
48
- if (key) {
49
- console.log('检测到已有 API Key,正在使用已保存凭据。');
50
- return key;
25
+ if (getApiKey()) {
26
+ console.log("已检测到现有 API Key,无需再次输入。");
27
+ return true;
51
28
  }
52
- return '';
29
+
30
+ if (noBrowser) console.log(`请在浏览器中打开:\n${loginUrl}\n`);
31
+ else await openBrowser(loginUrl);
32
+ return Boolean(getApiKey());
53
33
  }
54
34
 
55
- export async function authStatus() {
56
- const key = await detectApiKey();
57
- return { authenticated: Boolean(key), source: process.env.ZHIPU_API_KEY ? 'ZHIPU_API_KEY' : process.env.GLM_API_KEY ? 'GLM_API_KEY' : key ? 'local' : null };
35
+ export function saveApiKey(apiKey) {
36
+ if (!apiKey?.trim()) throw new Error("API Key 不能为空");
37
+ mkdirSync(dir, { recursive: true });
38
+ writeFileSync(authFile, JSON.stringify({ apiKey: apiKey.trim() }, null, 2), { mode: 0o600 });
39
+ console.log(`Saved to ${authFile}`);
58
40
  }
package/src/cli.js CHANGED
@@ -1,174 +1,88 @@
1
1
  #!/usr/bin/env node
2
- import readline from 'node:readline';
3
- import fs from 'node:fs/promises';
4
- import path from 'node:path';
5
- import process from 'node:process';
6
- import { ensureState, AUTH_FILE, GLOBAL_DIR, getProjectConfig, getAuth, saveAuth, deleteAuth, readJson, writeJson } from './config.js';
7
- import { runAgent } from './agent.js';
8
- import { listMcp, saveMcpConfig } from './mcp.js';
9
- import { loadSkills, loadPlugins } from './skills.js';
10
- import { browserLogin, authStatus, LOGIN_URL } from './auth.js';
2
+ import readline from "node:readline";
3
+ import { getApiKey, login } from "./auth.js";
4
+ import { createAgent } from "./agent.js";
5
+ import { getConfig, saveConfig } from "./config.js";
6
+ import { discoverSkills } from "./skills.js";
7
+ import { detectPlatform, detectShell, listAvailableShells } from "./platform/shell.js";
11
8
 
12
- await ensureState();
13
- const VERSION = '0.1.3';
14
- const cwd0 = process.cwd();
15
-
16
- const C = { rst:'\x1b[0m', dim:'\x1b[2m', bold:'\x1b[1m', cya:'\x1b[36m', cyb:'\x1b[1;36m', grn:'\x1b[32m', yel:'\x1b[33m', red:'\x1b[31m', mag:'\x1b[35m', gry:'\x1b[90m' };
17
- const color = (s, c) => `${c}${s}${C.rst}`;
18
- const LOGO = [
19
- ' ██████╗ ██╗ ███╗ ███╗',
20
- '██╔════╝ ██║ ████╗ ████║',
21
- '██║ ███╗ ██║ ██╔████╔██║',
22
- '██║ ██║ ██║ ██║╚██╔╝██║',
23
- '╚██████╔╝ ███████╗ ██║ ╚═╝ ██║',
24
- ' ╚═════╝ ╚══════╝ ╚═╝ ╚═╝'
9
+ const VERSION = "0.1.5";
10
+ const models = [
11
+ ["glm-5.2", "GLM-5.2"],
12
+ ["glm-4.5-air", "GLM-4.5-Air"],
13
+ ["glm-4.5-flash", "GLM-4.5-Flash (free)"],
14
+ ["glm-z1-flash", "GLM-Z1-Flash (free)"]
25
15
  ];
26
- const BASE_COMMANDS = {
27
- '/help':'显示全部命令','/clear':'清空对话','/thinking':'切换思考模式(开/关)','/model':'切换模型 /model <ID>',
28
- '/pwd':'显示当前目录','/cd':'切换目录 /cd <路径>','/ls':'列出文件','/cat':'查看文件 /cat <路径>','/run':'执行命令 /run <shell>','/exit':'退出'
29
- };
30
16
 
31
- function startPage() {
32
- const W = 44, center = s => s.padStart(Math.floor((W+s.length)/2)).padEnd(W);
33
- const lines = [
34
- '', '✧ GLM ✧', '', ...LOGO, '', 'GLM Code · 移动端', `v${VERSION}`, '', '/help 查看命令 · /thinking 思考开关', ''
35
- ];
36
- console.log();
37
- for (const [i, line] of lines.entries()) console.log(color(center(line), i >= 1 && i <= 8 ? C.cyb : C.gry));
17
+ function printShell() {
18
+ const p = detectPlatform(), s = detectShell();
19
+ console.log(`OS: ${p.platform}${p.isTermux ? " (Termux)" : ""}`);
20
+ console.log(`Architecture: ${p.arch}`);
21
+ console.log(`Shell: ${s.name}`);
22
+ console.log(`Executable: ${s.executable}`);
38
23
  }
39
24
 
40
- function commandMap(skills) {
41
- const d = { ...BASE_COMMANDS };
42
- for (const s of skills) d['/' + s.name] = '技能: ' + s.description;
43
- return d;
25
+ function help() {
26
+ console.log(`
27
+ /help 帮助
28
+ /models 模型列表
29
+ /model <id> 切换模型
30
+ /thinking 切换思考模式
31
+ /auth 登录
32
+ /skills Skills
33
+ /shell Shell 信息
34
+ /shell list 可用 Shell
35
+ /pwd 当前目录
36
+ /clear 清屏
37
+ /exit 退出
38
+ `);
44
39
  }
45
40
 
46
- function drawStatus(model, thinking, cwd, skillCount) {
47
- const text = `思考:${thinking ? '开' : '关'} 模型:${model}${skillCount ? ` 技能:${skillCount}` : ''} ${cwd}`;
48
- process.stdout.write(`\x1b[2K\r${color(text, C.gry)}\n`);
49
- }
41
+ async function main() {
42
+ const args = process.argv.slice(2);
43
+ if (args.includes("--version") || args.includes("-v")) return console.log(VERSION);
50
44
 
51
- function printHelp(commands) {
52
- console.log();
53
- for (const [c,d] of Object.entries(commands)) console.log(` ${color(c.padEnd(18), C.cyb)} ${color(d, C.gry)}`);
54
- console.log();
55
- }
56
-
57
- async function authCmd(args) {
58
- const sub = args[0] || 'status';
59
- if (sub === 'login') {
60
- const detected = await browserLogin();
61
- if (detected) {
62
- await saveAuth('zhipu', detected);
63
- console.log(color(`已检测并保存现有 API 凭据到 ${AUTH_FILE}`, C.grn));
64
- return;
65
- }
66
- console.log(color('未检测到本机已有 API Key。', C.yel));
67
- console.log(color('请先在浏览器登录智谱开放平台并创建 API Key,然后设置 ZHIPU_API_KEY。', C.gry));
68
- console.log(color(`登录入口: ${LOGIN_URL}`, C.gry));
45
+ if (args[0] === "auth" && args[1] === "login") {
46
+ await login({ noBrowser: args.includes("--no-browser") });
47
+ return;
48
+ }
49
+ if (args[0] === "models") {
50
+ models.forEach(([id, label]) => console.log(`${id}\t${label}`));
51
+ return;
52
+ }
53
+ if (args[0] === "shell") {
54
+ if (args[1] === "list") return listAvailableShells().forEach(x => console.log(x));
55
+ printShell();
69
56
  return;
70
57
  }
71
- if (sub === 'logout') { await deleteAuth('zhipu'); console.log(color('已退出智谱登录。', C.grn)); return; }
72
- const status = await authStatus();
73
- console.log(status.authenticated ? color(`zhipu: authenticated (${status.source})`, C.grn) : color('zhipu: not authenticated', C.yel));
74
- }
75
-
76
- function hiddenInput(prompt) {
77
- return new Promise((resolve, reject) => {
78
- if (!process.stdin.isTTY) { const rl = readline.createInterface({input:process.stdin, output:process.stdout}); rl.question(prompt, a=>{rl.close();resolve(a)}); return; }
79
- process.stdout.write(color(prompt, C.cyb));
80
- readline.emitKeypressEvents(process.stdin); process.stdin.setRawMode(true);
81
- let value = '', plain = false;
82
- const onKey = (str, key={}) => {
83
- if (key.name === 'return' || key.name === 'enter') { cleanup(); process.stdout.write('\n'); resolve(value); }
84
- else if (key.ctrl && key.name === 'c') { cleanup(); reject(new Error('Cancelled')); }
85
- else if (key.ctrl && key.name === 'r') { plain = !plain; redraw(); }
86
- else if (key.name === 'backspace') { value = value.slice(0,-1); redraw(); }
87
- else if (str && !key.ctrl && !key.meta) { value += str; process.stdout.write(plain ? str : '*'); }
88
- };
89
- const redraw=()=>{process.stdout.write(`\r\x1b[2K${color(prompt,C.cyb)}${plain?value:'*'.repeat(value.length)}`)};
90
- const cleanup=()=>{process.stdin.off('keypress',onKey);process.stdin.setRawMode(false)};
91
- process.stdin.on('keypress',onKey);
92
- });
93
- }
94
58
 
95
- function modelsCmd() {
96
- console.log([
97
- 'zhipu/glm-5.2','zhipu/glm-5','zhipu/glm-4.7','zhipu/glm-4.5',
98
- 'zhipu/glm-4.5-flash (free preset)','zhipu/glm-z1-flash (free preset)'
99
- ].join('\n'));
100
- }
59
+ if (!getApiKey()) await login();
101
60
 
102
- async function modelCmd(args, config, cwd) {
103
- const wanted = args[0] === 'free' ? config.freeModel : args[0];
104
- if (!wanted) return console.log(`当前模型: ${config.model}`);
105
- const file = path.join(cwd,'glmcode.json'); let local={};
106
- try { local=JSON.parse(await fs.readFile(file,'utf8')); } catch {}
107
- local.model=wanted; await fs.writeFile(file,JSON.stringify(local,null,2)+'\n');
108
- console.log(color(`模型已切换: ${wanted}`, C.grn));
109
- }
61
+ console.log(`GLMCode v${VERSION}`);
62
+ printShell();
63
+ console.log(`模型: ${getConfig().model} Skills: ${discoverSkills().length}`);
110
64
 
111
- async function sessionCmd(args) {
112
- const file=path.join(GLOBAL_DIR,'sessions.json'); const data=await readJson(file,{});
113
- if(args[0]==='clear'){delete data[args[1]];await writeJson(file,data);console.log('Deleted.');return;}
114
- for(const s of Object.values(data).sort((a,b)=>String(b.createdAt).localeCompare(String(a.createdAt)))) console.log(`${s.id}\t${s.cwd}\t${s.createdAt}`);
115
- }
116
- async function skillCmd(args,cwd){const skills=await loadSkills(cwd);if(args[0]!=='list')return console.log('Usage: glmcode skill list');skills.forEach(s=>console.log(`${s.name}\t${s.path}`));}
117
- async function pluginCmd(args,config,cwd){const plugins=await loadPlugins(cwd,config.plugins);if(args[0]!=='list')return console.log('Usage: glmcode plugin list');plugins.forEach(p=>console.log(`${p.id}${p.error?`\tERROR ${p.error}`:''}`));}
118
- async function mcpCmd(args,config,cwd){const sub=args[0]||'list';const file=path.join(cwd,'glmcode.json');if(sub==='list')return console.log(JSON.stringify(await listMcp(config),null,2));if(sub==='add'){const name=args[1],command=args.slice(2);if(!name||!command.length)return console.log('Usage: glmcode mcp add <name> <command> [args...]');await saveMcpConfig(file,name,{type:'stdio',command});return console.log(color(`已添加 MCP: ${name}`,C.grn));}if(sub==='remove'){const name=args[1];let local={};try{local=JSON.parse(await fs.readFile(file,'utf8'))}catch{}if(local.mcp)delete local.mcp[name];await fs.writeFile(file,JSON.stringify(local,null,2)+'\n');return console.log(color(`已删除 MCP: ${name}`,C.grn));}console.log('Usage: glmcode mcp list | add <name> <command> [args...] | remove <name>');}
65
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: "\n> " });
66
+ const agent = createAgent({ getConfig });
67
+ rl.prompt();
119
68
 
120
- async function readLineInput(prompt, completer) {
121
- return new Promise(resolve => {
122
- const rl=readline.createInterface({input:process.stdin,output:process.stdout,completer});
123
- rl.question(prompt, a=>{rl.close();resolve(a)});
69
+ rl.on("line", async line => {
70
+ const text = line.trim();
71
+ if (!text) return rl.prompt();
72
+ if (text === "/exit" || text === "/quit") return rl.close();
73
+ if (text === "/help") { help(); return rl.prompt(); }
74
+ if (text === "/clear") { console.clear(); return rl.prompt(); }
75
+ if (text === "/auth") { await login(); return rl.prompt(); }
76
+ if (text === "/shell") { printShell(); return rl.prompt(); }
77
+ if (text === "/shell list") { listAvailableShells().forEach(x => console.log(x)); return rl.prompt(); }
78
+ if (text === "/skills") { discoverSkills().forEach(s => console.log(`${s.name}\t${s.path}`)); return rl.prompt(); }
79
+ if (text === "/models") { models.forEach(([id, label]) => console.log(`${id}\t${label}`)); return rl.prompt(); }
80
+ if (text.startsWith("/model ")) { saveConfig({ model: text.slice(7).trim() }); console.log(`模型已切换为 ${getConfig().model}`); return rl.prompt(); }
81
+ if (text === "/thinking") { saveConfig({ thinking: !getConfig().thinking }); console.log(`思考模式: ${getConfig().thinking ? "开" : "关"}`); return rl.prompt(); }
82
+ try { await agent.run(text); } catch (e) { console.error(`\n错误: ${e.message}`); }
83
+ rl.prompt();
124
84
  });
85
+ rl.on("close", () => process.exit(0));
125
86
  }
126
87
 
127
- async function interactive() {
128
- const config=await getProjectConfig(cwd0); let model=config.model, thinking=Boolean(config.thinking), cwd=cwd0;
129
- let apiKey=await getAuth(); if(!apiKey){ apiKey=await browserLogin(); if(apiKey) await saveAuth('zhipu', apiKey); } if(!apiKey){console.log(color('未检测到 API Key。请在浏览器登录智谱开放平台后配置 API Key,或设置 ZHIPU_API_KEY。',C.red));return;}
130
- let skills=await loadSkills(cwd), commands=commandMap(skills), sessionId;
131
- startPage();
132
- console.log(color(`提示: 思考模式默认${thinking?'开启':'关闭'},输入 /thinking ${thinking?'关闭':'开启'}`,C.gry));
133
- if(skills.length) console.log(color(`已加载技能: ${skills.map(s=>s.name).join(', ')}`,C.mag));
134
- console.log();
135
- const completer=(line)=>{const hits=Object.entries(commands).filter(([c])=>c.startsWith(line)).map(([c,d])=>[c,d]);return [hits.length?hits:[],line]};
136
- let interrupted=false;
137
- const onSig=()=>{if(interrupted){process.exit(0)}interrupted=true;console.log(color('\n[已停止,再按一次 Ctrl+C 退出]',C.yel));setTimeout(()=>interrupted=false,1200)};
138
- process.on('SIGINT',onSig);
139
- while(true){
140
- drawStatus(model,thinking,cwd,skills.length);
141
- const user=(await readLineInput(color('┃ 输入消息... ┃ ',C.cyb),completer)).trim();
142
- if(!user)continue;
143
- if(user.startsWith('/')){
144
- const [cmd,...rest]=user.split(/\s+/), arg=rest.join(' ');
145
- if(cmd==='/exit'||cmd==='/quit'){console.log(color('再见',C.cya));break;}
146
- if(cmd==='/help'){printHelp(commands);continue;}
147
- if(cmd==='/clear'){console.clear();startPage();continue;}
148
- if(cmd==='/thinking'){thinking=!thinking;config.thinking=thinking;console.log(color(`思考模式: ${thinking?'开启':'关闭'}`,C.yel));continue;}
149
- if(cmd==='/model'){if(arg){model=arg;console.log(color(`模型已切换: ${model}`,C.grn));}else console.log(`当前模型: ${model}`);continue;}
150
- if(cmd==='/pwd'){console.log(cwd);continue;}
151
- if(cmd==='/cd'){try{process.chdir(path.resolve(cwd,arg||'.'));cwd=process.cwd();console.log(color(`-> ${cwd}`,C.grn));skills=await loadSkills(cwd);commands=commandMap(skills);}catch(e){console.log(color(`切换失败: ${e.message}`,C.red));}continue;}
152
- if(cmd==='/ls'){try{for(const f of (await fs.readdir(cwd,{withFileTypes:true})).sort((a,b)=>a.name.localeCompare(b.name)))console.log(` ${f.name}${f.isDirectory()?'/':''}`)}catch(e){console.log(color(`失败: ${e.message}`,C.red));}continue;}
153
- if(cmd==='/cat'){try{console.log(await fs.readFile(path.resolve(cwd,arg),'utf8').then(x=>x.slice(0,4000)))}catch(e){console.log(color(`失败: ${e.message}`,C.red));}continue;}
154
- if(cmd==='/run'){if(!arg)console.log('用法: /run <命令>');else {try{const { execSync }=await import('node:child_process');console.log(execSync(arg,{cwd,encoding:'utf8',timeout:60000,stdio:['ignore','pipe','pipe']}).slice(0,4000))}catch(e){console.log(color(`执行失败: ${e.stdout||e.message}`,C.red));}}continue;}
155
- if(skills.some(s=>'/'+s.name===cmd)){const s=skills.find(s=>s.name===cmd);console.log(color(`已加载技能:${s.name}`,C.grn));console.log(color('技能指令已注入,现在输入你的任务:',C.gry));config.__activeSkill=s.content;continue;}
156
- console.log(color(`未知命令: ${cmd}(输入 / 查看全部)`,C.red));continue;
157
- }
158
- try{const merged={...config,model,thinking};if(config.__activeSkill)merged.__activeSkill=config.__activeSkill;sessionId=await runAgent({cwd,config:merged,apiKey,prompt:user,model,sessionId});console.log(color(`\n[session ${sessionId}]`,C.gry));}catch(e){console.log(color(`\n[错误] ${e.message}`,C.red));}
159
- }
160
- process.removeListener('SIGINT',onSig);
161
- }
162
-
163
- async function main(){
164
- const [command,...argv]=process.argv.slice(2); const cwd=process.cwd();
165
- if(!command||command==='chat')return interactive();
166
- if(command==='-h'||command==='--help'||command==='help'){console.log(`GLMCode ${VERSION}\n\n用法:\n glmcode 启动 GLM 风格交互界面
167
- glm / gc 快捷启动 GLMCode\n glmcode run <prompt> 执行一次 coding task\n glmcode auth login|logout 管理智谱 API Key\n glmcode models 查看模型\n glmcode model <id>|free 切换模型\n glmcode session list|clear <id> 管理会话\n glmcode skill list 查看技能\n glmcode plugin list 查看插件\n glmcode mcp list|add|remove 管理 MCP\n glmcode config 查看配置`);return;}
168
- if(command==='-v'||command==='--version')return console.log(VERSION);
169
- const config=await getProjectConfig(cwd);
170
- if(command==='run'){let key=await getAuth();if(!key) key=await browserLogin();if(!key) throw new Error('No Zhipu API key detected. Run: glmcode auth login');return runAgent({cwd,config,apiKey:key,prompt:argv.join(' '),model:config.model});}
171
- if(command==='auth')return authCmd(argv);if(command==='models')return modelsCmd();if(command==='model')return modelCmd(argv,config,cwd);if(command==='session')return sessionCmd(argv);if(command==='skill')return skillCmd(argv,cwd);if(command==='plugin')return pluginCmd(argv,config,cwd);if(command==='mcp')return mcpCmd(argv,config,cwd);if(command==='config')return console.log(JSON.stringify(config,null,2));
172
- console.error(`Unknown command: ${command}`);process.exitCode=1;
173
- }
174
- main().catch(e=>{console.error(color(`Error: ${e.message}`,C.red));process.exit(1)});
88
+ main().catch(e => { console.error(e); process.exit(1); });
package/src/config.js CHANGED
@@ -1,65 +1,17 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import os from 'node:os';
1
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
4
 
5
- export const GLOBAL_DIR = path.join(os.homedir(), '.glmcode');
6
- export const CONFIG_FILE = path.join(GLOBAL_DIR, 'config.json');
7
- export const AUTH_FILE = path.join(GLOBAL_DIR, 'auth.json');
8
- export const SESSIONS_FILE = path.join(GLOBAL_DIR, 'sessions.json');
5
+ const dir = join(homedir(), ".glmcode");
6
+ const file = join(dir, "config.json");
9
7
 
10
- const defaultConfig = {
11
- provider: 'zhipu',
12
- model: 'glm-5.2',
13
- freeModel: 'glm-4.5-flash',
14
- baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
15
- maxTokens: 8192,
16
- temperature: 0.2,
17
- thinking: 'disabled',
18
- approval: 'ask',
19
- autoCompact: true,
20
- skillsDirs: ['.glmcode/skills', '~/.glmcode/skills'],
21
- plugins: [],
22
- mcp: {},
23
- startupCommand: 'glmcode',
24
- autoOpenLogin: true
25
- };
26
-
27
- export async function ensureState() {
28
- await fs.mkdir(GLOBAL_DIR, { recursive: true, mode: 0o700 });
29
- for (const file of [CONFIG_FILE, AUTH_FILE, SESSIONS_FILE]) {
30
- try { await fs.access(file); } catch { await fs.writeFile(file, file === CONFIG_FILE ? JSON.stringify(defaultConfig, null, 2) : '{}', { mode: 0o600 }); }
31
- }
32
- }
33
-
34
- export async function readJson(file, fallback = {}) {
35
- try { return JSON.parse(await fs.readFile(file, 'utf8')); } catch { return fallback; }
36
- }
37
- export async function writeJson(file, data) {
38
- await fs.mkdir(path.dirname(file), { recursive: true });
39
- await fs.writeFile(file, JSON.stringify(data, null, 2) + '\n', { mode: 0o600 });
40
- }
41
-
42
- export async function getProjectConfig(cwd) {
43
- const global = await readJson(CONFIG_FILE, defaultConfig);
44
- const localFile = path.join(cwd, 'glmcode.json');
45
- const local = await readJson(localFile, {});
46
- return { ...defaultConfig, ...global, ...local, cwd };
8
+ export function getConfig() {
9
+ try { return { model: "glm-4.5-air", thinking: false, ...JSON.parse(readFileSync(file, "utf8")) }; }
10
+ catch { return { model: "glm-4.5-air", thinking: false }; }
47
11
  }
48
-
49
- export async function getAuth(provider = 'zhipu') {
50
- const auth = await readJson(AUTH_FILE, {});
51
- const envKey = provider === 'zhipu' ? process.env.ZHIPU_API_KEY : undefined;
52
- return auth[provider]?.apiKey || envKey || '';
53
- }
54
-
55
- export async function saveAuth(provider, apiKey) {
56
- const auth = await readJson(AUTH_FILE, {});
57
- auth[provider] = { apiKey, savedAt: new Date().toISOString() };
58
- await writeJson(AUTH_FILE, auth);
59
- }
60
-
61
- export async function deleteAuth(provider) {
62
- const auth = await readJson(AUTH_FILE, {});
63
- delete auth[provider];
64
- await writeJson(AUTH_FILE, auth);
12
+ export function saveConfig(patch) {
13
+ mkdirSync(dir, { recursive: true });
14
+ const next = { ...getConfig(), ...patch };
15
+ writeFileSync(file, JSON.stringify(next, null, 2));
16
+ return next;
65
17
  }
package/src/mcp.js CHANGED
@@ -1,71 +1,13 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { spawn } from 'node:child_process';
4
-
5
- export async function listMcp(config) {
6
- return Object.entries(config?.mcp ?? {}).map(([name, value]) => ({ name, ...value }));
7
- }
8
-
9
- function startServer(server) {
10
- if (server.type !== 'stdio') throw new Error('MCP server type must be "stdio" in GLMCode 0.1.x.');
11
- const [command, ...args] = server.command;
12
- if (!command) throw new Error('MCP stdio server requires command[]');
13
- const child = spawn(command, args, { env: { ...process.env, ...(server.env || {}) }, stdio: ['pipe', 'pipe', 'inherit'] });
14
- let buffer = '';
15
- const pending = new Map();
16
- child.stdout.on('data', data => {
17
- buffer += data.toString();
18
- const lines = buffer.split('\n');
19
- buffer = lines.pop() || '';
20
- for (const line of lines) {
21
- if (!line.trim()) continue;
22
- try {
23
- const msg = JSON.parse(line);
24
- if (msg.id != null && pending.has(msg.id)) {
25
- const p = pending.get(msg.id); pending.delete(msg.id); p(msg);
26
- }
27
- } catch {}
28
- }
29
- });
30
- child.on('exit', () => { for (const p of pending.values()) p({ error: { message: 'MCP process exited' } }); pending.clear(); });
31
- let nextId = 1;
32
- function request(method, params = {}) {
33
- const id = nextId++;
34
- return new Promise((resolve, reject) => {
35
- pending.set(id, msg => msg.error ? reject(new Error(msg.error.message || JSON.stringify(msg.error))) : resolve(msg.result));
36
- child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n');
37
- setTimeout(() => { if (pending.has(id)) { pending.delete(id); reject(new Error(`MCP timeout: ${method}`)); } }, server.timeout ?? 15000).unref();
38
- });
39
- }
40
- return { child, request };
41
- }
42
-
43
- export async function discoverMcpTools(servers) {
44
- const outputs = [];
45
- for (const serverInfo of servers) {
46
- try {
47
- const server = startServer(serverInfo);
48
- await server.request('initialize', { protocolVersion: '2025-03-26', capabilities: {}, clientInfo: { name: 'glmcode', version: '0.1.1' } });
49
- server.child.stdin.write(JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized', params: {} }) + '\n');
50
- const result = await server.request('tools/list');
51
- for (const tool of result?.tools || []) outputs.push({ ...tool, _server: serverInfo, _runtime: server, _mcpName: serverInfo.name });
52
- } catch (e) {
53
- outputs.push({ name: `__error__${serverInfo.name}`, description: `MCP connection error: ${e.message}`, inputSchema: { type: 'object', properties: {} }, _error: e.message, _server: serverInfo });
54
- }
55
- }
56
- return outputs;
57
- }
58
-
59
- export async function callMcpTool(runtimeTool, args) {
60
- if (!runtimeTool._runtime) throw new Error(runtimeTool._error || 'MCP server unavailable');
61
- return runtimeTool._runtime.request('tools/call', { name: runtimeTool.name, arguments: args });
62
- }
63
-
64
- export async function saveMcpConfig(file, name, server) {
65
- let cfg = {};
66
- try { cfg = JSON.parse(await fs.readFile(file, 'utf8')); } catch {}
67
- cfg.mcp ??= {};
68
- cfg.mcp[name] = server;
69
- await fs.mkdir(path.dirname(file), { recursive: true });
70
- await fs.writeFile(file, JSON.stringify(cfg, null, 2) + '\n');
1
+ import { readFileSync, mkdirSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ const file = join(homedir(), ".glmcode", "mcp.json");
6
+ export function loadMcp() {
7
+ try { return JSON.parse(readFileSync(file, "utf8")); }
8
+ catch { return { mcpServers: {} }; }
9
+ }
10
+ export function saveMcp(config) {
11
+ mkdirSync(join(homedir(), ".glmcode"), { recursive: true });
12
+ writeFileSync(file, JSON.stringify(config, null, 2));
71
13
  }
@@ -0,0 +1,30 @@
1
+ import { execFile } from "node:child_process";
2
+ import { detectPlatform } from "./shell.js";
3
+
4
+ function tryRun(command, args) {
5
+ return new Promise(resolve => {
6
+ const child = execFile(command, args, { windowsHide: true }, error => resolve(!error));
7
+ child.on("error", () => resolve(false));
8
+ });
9
+ }
10
+
11
+ export async function openBrowser(url) {
12
+ const p = detectPlatform();
13
+
14
+ if (process.env.GLMCODE_NO_BROWSER === "1") {
15
+ console.log(`\n浏览器自动打开已禁用。\n请手动打开:\n${url}\n`);
16
+ return false;
17
+ }
18
+
19
+ if (p.platform === "win32") {
20
+ if (await tryRun("cmd.exe", ["/c", "start", "", url])) return true;
21
+ } else if (p.platform === "darwin") {
22
+ if (await tryRun("open", [url])) return true;
23
+ } else {
24
+ if (p.isTermux && await tryRun("termux-open-url", [url])) return true;
25
+ if (await tryRun("xdg-open", [url])) return true;
26
+ }
27
+
28
+ console.log(`\n当前环境没有可用的浏览器启动器。\n请在浏览器中打开:\n${url}\n`);
29
+ return false;
30
+ }
@@ -0,0 +1,64 @@
1
+ import os from "node:os";
2
+ import { execFileSync } from "node:child_process";
3
+
4
+ function exists(command) {
5
+ try {
6
+ if (process.platform === "win32") {
7
+ execFileSync("where", [command], { stdio: "ignore" });
8
+ } else {
9
+ execFileSync("sh", ["-lc", `command -v ${JSON.stringify(command)}`], { stdio: "ignore" });
10
+ }
11
+ return true;
12
+ } catch { return false; }
13
+ }
14
+
15
+ export function detectPlatform() {
16
+ return {
17
+ platform: process.platform,
18
+ arch: process.arch,
19
+ isTermux: Boolean(process.env.TERMUX_VERSION || process.env.PREFIX?.includes("com.termux")),
20
+ release: os.release()
21
+ };
22
+ }
23
+
24
+ export function detectShell() {
25
+ const p = detectPlatform();
26
+ if (p.platform === "win32") {
27
+ if (process.env.PSModulePath && exists("pwsh")) return { name: "PowerShell", executable: "pwsh" };
28
+ if (process.env.PSModulePath) return { name: "PowerShell", executable: "powershell.exe" };
29
+ return { name: "CMD", executable: process.env.ComSpec || "cmd.exe" };
30
+ }
31
+
32
+ const envShell = process.env.SHELL || "";
33
+ const name = envShell.split("/").pop();
34
+ if (["bash", "zsh", "fish", "sh", "dash", "ksh", "nu"].includes(name) && exists(name)) {
35
+ return { name, executable: envShell };
36
+ }
37
+
38
+ for (const candidate of ["bash", "zsh", "fish", "sh"]) {
39
+ if (exists(candidate)) return { name: candidate, executable: candidate };
40
+ }
41
+ return { name: "sh", executable: "/bin/sh" };
42
+ }
43
+
44
+ export function listAvailableShells() {
45
+ if (process.platform === "win32") {
46
+ return [
47
+ ...(exists("pwsh") ? ["powershell"] : []),
48
+ ...(exists("powershell.exe") ? ["powershell.exe"] : []),
49
+ "cmd.exe",
50
+ ...(exists("bash.exe") ? ["bash.exe"] : [])
51
+ ];
52
+ }
53
+ return ["sh", ...(exists("bash") ? ["bash"] : []), ...(exists("zsh") ? ["zsh"] : []), ...(exists("fish") ? ["fish"] : [])];
54
+ }
55
+
56
+ export function shellCommand(command, shell = detectShell().executable) {
57
+ if (process.platform === "win32" && /powershell|pwsh/i.test(shell)) {
58
+ return { file: shell, args: ["-NoLogo", "-NoProfile", "-Command", command] };
59
+ }
60
+ if (process.platform === "win32" && /cmd/i.test(shell)) {
61
+ return { file: shell, args: ["/d", "/s", "/c", command] };
62
+ }
63
+ return { file: shell, args: ["-lc", command] };
64
+ }
package/src/skills.js CHANGED
@@ -1,51 +1,17 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { expandHome, exists } from './utils.js';
4
- import AdmZip from 'adm-zip';
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { homedir } from "node:os";
5
4
 
6
- async function extractZipSkills(root) {
7
- if (!await exists(root)) return;
8
- for (const name of await fs.readdir(root)) {
9
- if (!name.toLowerCase().endsWith('.zip')) continue;
10
- const zipPath = path.join(root, name);
11
- const target = path.join(root, name.slice(0, -4));
12
- try {
13
- if (!await exists(target)) new AdmZip(zipPath).extractAllTo(target, true);
14
- } catch {}
15
- }
16
- }
17
-
18
- export async function loadSkills(cwd) {
19
- const dirs = [path.join(cwd, '.glmcode', 'skills'), path.join(expandHome('~/.glmcode'), 'skills')];
20
- const skills = [];
21
- for (const root of dirs) {
22
- await extractZipSkills(root);
23
- if (!await exists(root)) continue;
24
- for (const name of await fs.readdir(root)) {
25
- const file = path.join(root, name, 'SKILL.md');
26
- if (!await exists(file)) continue;
27
- const content = await fs.readFile(file, 'utf8');
28
- skills.push({ name, path: file, content });
29
- }
30
- }
31
- return skills;
32
- }
33
-
34
- export async function loadPlugins(cwd, configured = []) {
35
- const roots = [path.join(cwd, '.glmcode', 'plugins'), path.join(expandHome('~/.glmcode'), 'plugins')];
36
- const mods = [...configured];
5
+ export function discoverSkills() {
6
+ const roots = [join(process.cwd(), ".glmcode", "skills"), join(homedir(), ".glmcode", "skills")];
7
+ const out = [];
37
8
  for (const root of roots) {
38
- if (!await exists(root)) continue;
39
- for (const name of await fs.readdir(root)) mods.push(path.join(root, name));
40
- }
41
- const plugins = [];
42
- for (const item of mods) {
43
- try { const mod = await import(item.startsWith('.') ? path.resolve(cwd, item) : item); plugins.push({ id: item, module: mod }); } catch (e) { plugins.push({ id: item, error: e.message }); }
9
+ if (!existsSync(root)) continue;
10
+ for (const e of readdirSync(root, { withFileTypes: true })) {
11
+ if (!e.isDirectory()) continue;
12
+ const p = join(root, e.name, "SKILL.md");
13
+ if (existsSync(p)) out.push({ name: e.name, path: p, content: readFileSync(p, "utf8") });
14
+ }
44
15
  }
45
- return plugins;
46
- }
47
-
48
- export function skillsPrompt(skills) {
49
- if (!skills.length) return '';
50
- return '\n\nAVAILABLE SKILLS:\n' + skills.map(s => `## ${s.name}\n${s.content}`).join('\n\n');
16
+ return out;
51
17
  }
package/src/tools.js CHANGED
@@ -1,86 +1,14 @@
1
- import fs from 'node:fs/promises';
2
- import { spawn } from 'node:child_process';
3
- import readline from 'node:readline/promises';
4
- import { stdin as input, stdout as output } from 'node:process';
5
- import path from 'node:path';
6
- import { relSafe, trimOutput, jsonText } from './utils.js';
7
-
8
- export const TOOL_DEFS = [
9
- { type: 'function', function: { name: 'read_file', description: 'Read a UTF-8 text file inside the project.', parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] } } },
10
- { type: 'function', function: { name: 'write_file', description: 'Create or replace a UTF-8 text file inside the project.', parameters: { type: 'object', properties: { path: { type: 'string' }, content: { type: 'string' } }, required: ['path', 'content'] } } },
11
- { type: 'function', function: { name: 'list_files', description: 'List files/directories recursively or at one level inside the project.', parameters: { type: 'object', properties: { path: { type: 'string' }, depth: { type: 'integer', minimum: 1, maximum: 4 } } } } },
12
- { type: 'function', function: { name: 'search_text', description: 'Search text in files inside the project.', parameters: { type: 'object', properties: { query: { type: 'string' }, path: { type: 'string' } }, required: ['query'] } } },
13
- { type: 'function', function: { name: 'run_shell', description: 'Run a shell command in the project directory. Use for builds, tests, git and development commands.', parameters: { type: 'object', properties: { command: { type: 'string' }, timeoutMs: { type: 'integer', minimum: 1000, maximum: 300000 } }, required: ['command'] } } }
14
- ];
15
-
16
- async function approve(action, details, mode) {
17
- if (mode === 'auto') return true;
18
- if (mode === 'deny') return false;
19
- const rl = readline.createInterface({ input, output });
20
- const answer = await rl.question(`\n⚠ ${action}\n${details}\nAllow? [y/N] `);
21
- rl.close();
22
- return /^y(es)?$/i.test(answer.trim());
23
- }
24
-
25
- async function listTree(dir, root, depth, out = []) {
26
- if (depth < 0) return out;
27
- for (const ent of await fs.readdir(dir, { withFileTypes: true })) {
28
- if (['.git', 'node_modules', '.glmcode/cache'].includes(path.relative(root, path.join(dir, ent.name))) || ent.name === '.git') continue;
29
- const full = path.join(dir, ent.name);
30
- out.push({ path: path.relative(root, full) || '.', type: ent.isDirectory() ? 'dir' : 'file' });
31
- if (ent.isDirectory() && depth > 0) await listTree(full, root, depth - 1, out);
32
- if (out.length > 500) break;
33
- }
34
- return out;
35
- }
36
-
37
- export function makeToolExecutor({ cwd, approval = 'ask' }) {
38
- return async function execute(name, args) {
39
- switch (name) {
40
- case 'read_file': {
41
- const file = relSafe(cwd, args.path);
42
- return trimOutput(await fs.readFile(file, 'utf8'));
43
- }
44
- case 'write_file': {
45
- const file = relSafe(cwd, args.path);
46
- if (!await approve('File write', file, approval)) return 'DENIED by user.';
47
- await fs.mkdir(path.dirname(file), { recursive: true });
48
- await fs.writeFile(file, args.content, 'utf8');
49
- return `Wrote ${path.relative(cwd, file)} (${Buffer.byteLength(args.content, 'utf8')} bytes).`;
50
- }
51
- case 'list_files': {
52
- const dir = relSafe(cwd, args.path || '.');
53
- return jsonText(await listTree(dir, cwd, Math.min(args.depth ?? 1, 4)));
54
- }
55
- case 'search_text': {
56
- const base = relSafe(cwd, args.path || '.');
57
- const hits = [];
58
- async function walk(d) {
59
- for (const ent of await fs.readdir(d, { withFileTypes: true })) {
60
- if (['.git', 'node_modules', 'dist', 'build'].includes(ent.name)) continue;
61
- const f = path.join(d, ent.name);
62
- if (ent.isDirectory()) await walk(f);
63
- else if (hits.length < 100) {
64
- try { const text = await fs.readFile(f, 'utf8'); const lines = text.split(/\r?\n/); lines.forEach((line, i) => { if (line.toLowerCase().includes(String(args.query).toLowerCase()) && hits.length < 100) hits.push({ path: path.relative(cwd, f), line: i + 1, text: line.slice(0, 500) }); }); } catch {}
65
- }
66
- }
67
- }
68
- await walk(base);
69
- return jsonText(hits);
70
- }
71
- case 'run_shell': {
72
- if (!await approve('Shell execution', args.command, approval)) return 'DENIED by user.';
73
- const timeout = Math.max(1000, Math.min(args.timeoutMs ?? 120000, 300000));
74
- return await new Promise((resolve) => {
75
- const child = spawn(args.command, { cwd, shell: true, env: process.env, stdio: ['ignore', 'pipe', 'pipe'] });
76
- let stdout = '', stderr = '';
77
- const timer = setTimeout(() => child.kill('SIGTERM'), timeout);
78
- child.stdout.on('data', d => stdout += d);
79
- child.stderr.on('data', d => stderr += d);
80
- child.on('close', code => { clearTimeout(timer); resolve(`exit=${code}\nSTDOUT:\n${trimOutput(stdout)}\nSTDERR:\n${trimOutput(stderr)}`); });
81
- });
82
- }
83
- default: throw new Error(`Unknown tool: ${name}`);
84
- }
85
- };
1
+ import { promises as fs } from "node:fs";
2
+ import { execFile } from "node:child_process";
3
+ import { detectShell, shellCommand } from "./platform/shell.js";
4
+
5
+ export async function readFile(path) { return fs.readFile(path, "utf8"); }
6
+ export async function writeFile(path, content) { await fs.writeFile(path, content, "utf8"); return `wrote ${path}`; }
7
+
8
+ export async function runShell(command, cwd = process.cwd()) {
9
+ const s = detectShell(), spec = shellCommand(command, s.executable);
10
+ return await new Promise((resolve, reject) => {
11
+ execFile(spec.file, spec.args, { cwd, maxBuffer: 10 * 1024 * 1024 },
12
+ (error, stdout, stderr) => error ? reject(new Error(stderr || error.message)) : resolve({ stdout, stderr, code: 0, shell: s.name }));
13
+ });
86
14
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 GLMCode contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,19 +0,0 @@
1
- {
2
- "provider": "zhipu",
3
- "model": "glm-5.2",
4
- "freeModel": "glm-4.5-flash",
5
- "baseUrl": "https://open.bigmodel.cn/api/paas/v4",
6
- "approval": "ask",
7
- "thinking": "enabled",
8
- "maxTokens": 8192,
9
- "temperature": 0.2,
10
- "plugins": [],
11
- "mcp": {
12
- "example": {
13
- "type": "stdio",
14
- "command": ["npx", "-y", "<your-mcp-server>"],
15
- "env": {},
16
- "timeout": 15000
17
- }
18
- }
19
- }
package/src/provider.js DELETED
@@ -1,54 +0,0 @@
1
- export async function chatCompletion({ baseUrl, apiKey, model, messages, tools, maxTokens, temperature, thinking = 'enabled', stream = false }) {
2
- const res = await fetch(`${baseUrl.replace(/\/$/, '')}/chat/completions`, {
3
- method: 'POST',
4
- headers: { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` },
5
- body: JSON.stringify({
6
- model, messages, tools,
7
- max_tokens: maxTokens,
8
- temperature,
9
- thinking: { type: thinking },
10
- stream
11
- })
12
- });
13
- const text = await res.text();
14
- if (!res.ok) throw new Error(`GLM API ${res.status}: ${text.slice(0, 1000)}`);
15
- return JSON.parse(text);
16
- }
17
-
18
- export async function streamCompletion({ baseUrl, apiKey, model, messages, tools, maxTokens, temperature, thinking = 'enabled', onText }) {
19
- const res = await fetch(`${baseUrl.replace(/\/$/, '')}/chat/completions`, {
20
- method: 'POST',
21
- headers: { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` },
22
- body: JSON.stringify({ model, messages, tools, max_tokens: maxTokens, temperature, thinking: { type: thinking }, stream: true })
23
- });
24
- if (!res.ok) throw new Error(`GLM API ${res.status}: ${await res.text()}`);
25
- const reader = res.body.getReader();
26
- const decoder = new TextDecoder();
27
- let buf = '';
28
- let final = { role: 'assistant', content: '', tool_calls: [] };
29
- while (true) {
30
- const { value, done } = await reader.read();
31
- if (done) break;
32
- buf += decoder.decode(value, { stream: true });
33
- const parts = buf.split('\n'); buf = parts.pop() ?? '';
34
- for (const line of parts) {
35
- if (!line.startsWith('data:')) continue;
36
- const data = line.slice(5).trim();
37
- if (data === '[DONE]') continue;
38
- let chunk; try { chunk = JSON.parse(data); } catch { continue; }
39
- const delta = chunk.choices?.[0]?.delta;
40
- if (!delta) continue;
41
- if (delta.content) { final.content += delta.content; onText?.(delta.content); }
42
- if (delta.tool_calls) {
43
- for (const tc of delta.tool_calls) {
44
- const i = tc.index ?? 0;
45
- final.tool_calls[i] ??= { id: tc.id || '', type: 'function', function: { name: '', arguments: '' } };
46
- if (tc.id) final.tool_calls[i].id = tc.id;
47
- if (tc.function?.name) final.tool_calls[i].function.name += tc.function.name;
48
- if (tc.function?.arguments) final.tool_calls[i].function.arguments += tc.function.arguments;
49
- }
50
- }
51
- }
52
- }
53
- return final;
54
- }
package/src/utils.js DELETED
@@ -1,15 +0,0 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import os from 'node:os';
4
-
5
- export function expandHome(p) { return p.startsWith('~/') ? path.join(os.homedir(), p.slice(2)) : p; }
6
- export async function exists(p) { try { await fs.access(p); return true; } catch { return false; } }
7
- export function trimOutput(s, max = 12000) { return s.length <= max ? s : s.slice(0, max) + `\n… [truncated to ${max} chars]`; }
8
- export function jsonText(value) { return JSON.stringify(value, null, 2); }
9
- export function relSafe(cwd, target) {
10
- const abs = path.resolve(cwd, target);
11
- const root = path.resolve(cwd);
12
- if (abs !== root && !abs.startsWith(root + path.sep)) throw new Error(`Path escapes project root: ${target}`);
13
- return abs;
14
- }
15
- export function nowId(prefix = 's') { return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`; }
@@ -1,2 +0,0 @@
1
- export const tools = [];
2
- // Plugins can export `tools` using the same OpenAI-compatible tool definition format.
@@ -1,3 +0,0 @@
1
- # Code Review
2
-
3
- Review changes for correctness, edge cases, security, maintainability and tests. Before changing code, inspect the relevant files and verify the result with the project's test/build command when available.