@unionschool/easy-agents 0.4.0 → 0.4.1

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 CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  | 设备 | 安装和打开 | 设备配置位置 |
8
8
  | --- | --- | --- |
9
- | macOS 14+ | 解压与处理器匹配的包,把 `EasyAgents Client.app` 拖入「应用程序」并打开,点菜单栏图标。当前交付的 `EasyAgents-Mac-Intel.zip` 为 Intel 构建。 | `~/Library/Application Support/EasyAgents Client/` |
9
+ | macOS 14+ | Intel 和 Apple 芯片都使用 `EasyAgents-Mac-Universal.zip`。解压后把 `EasyAgents Client.app` 拖入「应用程序」并打开,点菜单栏图标。 | `~/Library/Application Support/EasyAgents Client/` |
10
10
  | Windows x64 | 双击 `EasyAgents-Setup.exe`,按当前用户安装。从开始菜单打开 **EasyAgents Client**,以后可点托盘图标。在新终端输入 `easy-agents` 也可打开面板。 | `%APPDATA%\EasyAgents\` |
11
11
  | Ubuntu x64 | 在压缩包所在目录执行下面两行,以运行客户端的普通用户安装。 | `~/.easyagents/` |
12
12
  | Android 8+ | 安装交付的 `EasyAgents-Android-test.apk`,打开应用,用 Linux 设备配对码接入,点「上线接任务」。前台通知显示服务状态。 | 应用私有存储和 Android Keystore |
@@ -74,6 +74,7 @@ npm exec --offline -- easy-agents run
74
74
  | `easy-agents autostart on` / `off` | 切换开机启动。系统可能要求管理员授权 |
75
75
  | `easy-agents update`、`auto-update on` / `off` | 检查兼容的 npm 稳定版核心或切换自动检查。重连验证失败会回退,原生面板和内置解释器通过安装包升级 |
76
76
  | `easy-agents terminal on` / `off` | 在支持的 Unix 设备上切换工作台网页终端 |
77
+ | `easy-agents agent-credential <Agent编号>` | 为本机设备上的 Agent 检查平台凭据,缺失或被撤销(401)时领取新凭据(旧凭据随之作废),只输出凭据文件路径。负责人在本机用 `skills/easyagents-task/register.py` 登记任务前用它 |
77
78
 
78
79
  ## 已安装设备怎么升级
79
80
 
@@ -94,3 +95,5 @@ npm exec --offline -- easy-agents run
94
95
  - 客户端保存待回传结果,重连后补传。独立版尚未接入运行中 `/steer`、手动 `/sync`、平台远程读文档和编辑设备上的工具规则。
95
96
 
96
97
  开发者在 `new-client/` 中执行 `npm ci`、`npm pack` 构建通用包。包内包含 `runtime/easyagents_core/` 中的 Git、文件交付和 MCP 模块及其进程支持,并随包附带 WebSocket 依赖。桌面安装器从同一目录取工具、模型、额度和平台 API 模块,按 `scripts/desktop-requirements.txt` 安装固定版本的 Python 依赖。
98
+
99
+ 在 Mac 上,从仓库根目录运行 `uv run --no-project --python 3.12 python new-client/macos/install.py --zip /path/to/EasyAgents-Mac-Universal.zip` 生成通用 ZIP。该模式只写出压缩包。菜单栏程序、Node 和内置 Python 都包含 `x86_64` 与 `arm64`。
@@ -92,6 +92,7 @@ try {
92
92
  + 'easy-agents configure <网关地址> <端口>\n'
93
93
  + 'easy-agents pair\n'
94
94
  + 'easy-agents run\n'
95
+ + 'easy-agents agent-credential <Agent编号>\n'
95
96
  + 'easy-agents status' + (desktop ? '\neasy-agents show | start | stop | restart | logs | doctor | update\neasy-agents auto-update on|off\neasy-agents autostart on|off\neasy-agents terminal on|off\neasy-agents accounts --json' : ''));
96
97
  } else if (['--version', '-v'].includes(command) && !args.length) {
97
98
  console.log(version);
@@ -208,6 +209,23 @@ try {
208
209
  + '\n当前任务:' + (status.task || '空闲') + '\n最近回传:' + (status.report.lastResult?.status || '暂无')
209
210
  + '\n开机启动:' + (status.autostart ? '开启' : '关闭') + '\n自动更新:' + (status.autoUpdate ? '开启' : '关闭')
210
211
  + (status.updateMessage ? '\n更新:' + status.updateMessage : '') : ''));
212
+ } else if (command === 'agent-credential' && args.length === 1) {
213
+ // Gives the lead a working credential for this device's agent to call the platform tools locally
214
+ // (for example skills/easyagents-task/register.py). Prints only the file path, never the token.
215
+ const device = readSettings(devicePath);
216
+ if (!device) throw new Error('请先配对设备');
217
+ const { ensureAgentCredential } = await import('./credentials.js');
218
+ const origin = new URL(device.gateway).origin;
219
+ const file = await ensureAgentCredential({ configDir, origin, id: args[0], version, signal: AbortSignal.timeout(60000),
220
+ issue: async id => {
221
+ const res = await fetch(new URL('/api/client/agents/' + id + '/credential', origin), { method: 'POST', redirect: 'error',
222
+ headers: { Authorization: 'Bearer ' + device.device_token, 'Content-Type': 'application/json' }, body: '{}',
223
+ signal: AbortSignal.timeout(20000) });
224
+ const data = await res.json().catch(() => null);
225
+ if (!res.ok || data?.code !== 0) throw new Error('平台没有发放凭据(HTTP ' + res.status + (data?.msg ? ',' + data.msg : '') + ')');
226
+ return data.data;
227
+ } });
228
+ console.log(file);
211
229
  } else {
212
230
  throw new Error('命令或参数无效。请运行 easy-agents --help');
213
231
  }
package/bin/run.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import { createHash, randomUUID } from 'node:crypto';
2
- import { lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
- import { homedir } from 'node:os';
2
+ import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
4
3
  import { join } from 'node:path';
5
4
  import { setTimeout as sleep } from 'node:timers/promises';
6
5
  import WebSocket from 'ws';
7
6
  import { execute } from './tasks.js';
8
7
  import { openTerminal, terminalAvailable } from './terminal.js';
9
8
  import { protectDeviceFile } from './pair.js';
9
+ import { ensureAgentCredential, findAgentCredential } from './credentials.js';
10
10
 
11
11
  const validId = id => typeof id === 'string' && /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(id);
12
12
 
@@ -53,64 +53,12 @@ export async function runDevice(device, configDir, version, signal, { reporter,
53
53
  const request = (method, path, body) => gatewayRequest(method, '/api/client/runs/' + path, body);
54
54
  const reportQuota = (id, body, cancel) => gatewayRequest('POST', '/api/client/agents/' + id + '/quota', body, cancel);
55
55
  function workCredential(content) {
56
- const id = content.agent?.agent_id;
57
- if (!/^[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}$/i.test(id)) throw new Error('Agent 编号无效');
58
- const dir = join(configDir, 'agents');
59
- const file = join(dir, id + '.json');
60
- const files = [file];
61
- if (process.platform === 'darwin' && configDir === join(homedir(), 'Library/Application Support/EasyAgents Client')) {
62
- files.push(join(homedir(), '.easyagents', 'agents', id + '.json'));
63
- }
64
- for (const path of files) {
65
- try {
66
- const info = lstatSync(path);
67
- if (!info.isFile() || info.uid !== process.getuid() || (info.mode & 0o077)) throw new Error('Agent 凭据文件无效');
68
- return { file: path, gateway: url.origin };
69
- } catch (err) { if (err.code !== 'ENOENT') throw err; }
70
- }
71
- return null;
72
- }
73
- async function credentialWorks(file) {
74
- let auth;
75
- try { auth = JSON.parse(readFileSync(file, 'utf8')); } catch { return false; }
76
- if (typeof auth?.server !== 'string' || auth.server.replace(/\/+$/, '') !== url.origin || !auth.agent_token) return false;
77
- try {
78
- const res = await fetch(new URL('/mcp', url), { method: 'POST', redirect: 'error',
79
- headers: { Authorization: 'Bearer ' + auth.agent_token, 'Content-Type': 'application/json',
80
- Accept: 'application/json, text/event-stream' },
81
- body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-03-26',
82
- capabilities: {}, clientInfo: { name: 'easyagents-client', version } } }),
83
- signal: AbortSignal.any([signal, AbortSignal.timeout(20000)]) });
84
- await res.body?.cancel();
85
- return ![401, 403].includes(res.status);
86
- } catch {
87
- return true; // Network trouble is not proof of a revoked credential; the run reports what fails.
88
- }
89
- }
90
- // Task runs need a working Agent credential for the platform tools. A missing, mismatched or
91
- // revoked one is replaced; the platform revokes the previous credential when it issues a new one.
92
- async function agentCredential(content) {
93
- const id = content.agent?.agent_id;
94
- if (!/^[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}$/i.test(id)) throw new Error('Agent 编号无效');
95
- let found = null;
96
- try { found = workCredential(content); } catch {}
97
- if (found && await credentialWorks(found.file)) return found.file;
98
- const issued = await gatewayRequest('POST', '/api/client/agents/' + id + '/credential', {});
99
- if (typeof issued?.agent_token !== 'string' || !issued.agent_token) throw new Error('Agent 凭据响应无效');
100
- const dir = join(configDir, 'agents');
101
- mkdirSync(dir, { recursive: true, mode: 0o700 });
102
- const file = join(dir, id + '.json');
103
- const tmp = join(dir, '.agent-' + randomUUID());
104
- try {
105
- writeFileSync(tmp, '', { flag: 'wx', mode: 0o600 });
106
- protectDeviceFile(tmp);
107
- writeFileSync(tmp, JSON.stringify({ server: url.origin, agent_token: issued.agent_token }));
108
- renameSync(tmp, file);
109
- } finally {
110
- rmSync(tmp, { force: true });
111
- }
112
- return file;
56
+ const file = findAgentCredential(configDir, content.agent?.agent_id);
57
+ return file ? { file, gateway: url.origin } : null;
113
58
  }
59
+ // Task runs need a working Agent credential for the platform tools (credentials.js).
60
+ const agentCredential = content => ensureAgentCredential({ configDir, origin: url.origin, id: content.agent?.agent_id,
61
+ version, signal, issue: id => gatewayRequest('POST', '/api/client/agents/' + id + '/credential', {}) });
114
62
  function checkDrained() {
115
63
  if (drain?.aborted && !active && !queue.size && !readdirSync(folder).some(f => f.endsWith('.json'))) onDrained?.();
116
64
  }
package/bin/tasks.js CHANGED
@@ -66,24 +66,32 @@ function desktopTask(resources, content, signal, options) {
66
66
  });
67
67
  }
68
68
 
69
- async function workspace(content) {
69
+ // Plain npm serves the one folder it was started in. Mac and Ubuntu desktop installs run as a background
70
+ // service from the home folder and serve every workspace registered for this device, resolved under home.
71
+ async function workspace(content, desktop = false) {
70
72
  const local = content.workspace?.local_path;
71
73
  if (!local || typeof local !== 'string') throw new Error('缺少工作目录');
72
- const selected = await realpath(process.env.EASYAGENTS_WORKSPACE || process.cwd());
73
74
  const directory = await realpath(resolve(homedir(), local.replace(/^~\//, '')));
74
- if (selected !== directory || selected === homedir() || selected === parse(selected).root) {
75
+ if (directory === homedir() || directory === parse(directory).root) throw new Error('工作目录不能是主目录或根目录');
76
+ if (!desktop && await realpath(process.env.EASYAGENTS_WORKSPACE || process.cwd()) !== directory) {
75
77
  throw new Error('派发目录与本机启动时指定的工作目录不同');
76
78
  }
77
79
  return directory;
78
80
  }
79
81
 
80
- async function sync(content, signal) {
81
- const directory = await workspace(content);
82
- const gitRoot = (await invoke('git', ['rev-parse', '--show-toplevel'], directory, '', signal, 10000)).trim();
83
- if (await realpath(gitRoot) !== directory) throw new Error('工作目录不是仓库根目录');
82
+ async function sync(content, signal, resources) {
83
+ let directory;
84
+ try {
85
+ directory = await workspace(content, Boolean(resources));
86
+ const gitRoot = (await invoke('git', ['rev-parse', '--show-toplevel'], directory, '', signal, 10000)).trim();
87
+ if (await realpath(gitRoot) !== directory) throw new Error('工作目录不是仓库根目录');
88
+ } catch (error) {
89
+ if (signal.aborted) return { status: 'cancelled', failure_code: 'cancelled' };
90
+ return { status: 'failed', failure_code: 'workspace_missing', failure_detail: error.message || '工作目录不可用' };
91
+ }
84
92
  const script = fileURLToPath(new URL('../runtime/sync.py', import.meta.url));
85
93
  try {
86
- const python = process.platform === 'win32' ? 'python' : 'python3';
94
+ const python = resources ? join(resources, 'python/bin/python3') : process.platform === 'win32' ? 'python' : 'python3';
87
95
  const result = await invoke(python, ['-I', '-S', '-B', script], directory, JSON.stringify(content), signal, 600000);
88
96
  return JSON.parse(result);
89
97
  } catch (error) {
@@ -94,9 +102,9 @@ async function sync(content, signal) {
94
102
 
95
103
  export async function execute(content, signal, workCredential, { agentCredential, configDir } = {}) {
96
104
  signal.throwIfAborted();
97
- if (content.run_kind === 'sync') return sync(content, signal);
98
105
  const resources = process.platform === 'darwin' ? process.env.EASYAGENTS_MAC_RESOURCES
99
106
  : process.platform === 'linux' ? process.env.EASYAGENTS_DESKTOP_RESOURCES : undefined;
107
+ if (content.run_kind === 'sync') return sync(content, signal, resources);
100
108
  const versioned = content.task && (content.task.git_delivery || content.task.file_delivery || content.task.document_commit_sha);
101
109
  if (TASK_KINDS.includes(content.run_kind) && (versioned || content.run_kind !== 'execute')) {
102
110
  // Windows, Android and the plain npm install do not run task branches yet (user 2026-11-22).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unionschool/easy-agents",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "EasyAgents Client",
5
5
  "easyAgentsDesktop": 1,
6
6
  "type": "module",