@zhin.js/agent 0.1.0 → 0.1.2
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/lib/cron-engine.d.ts +16 -1
- package/lib/cron-engine.d.ts.map +1 -1
- package/lib/cron-engine.js +47 -13
- package/lib/cron-engine.js.map +1 -1
- package/lib/discover-skills.d.ts +3 -1
- package/lib/discover-skills.d.ts.map +1 -1
- package/lib/discover-skills.js +7 -9
- package/lib/discover-skills.js.map +1 -1
- package/lib/discover-tools.d.ts +1 -6
- package/lib/discover-tools.d.ts.map +1 -1
- package/lib/discover-tools.js +2 -6
- package/lib/discover-tools.js.map +1 -1
- package/lib/index.d.ts +2 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -2
- package/lib/index.js.map +1 -1
- package/lib/init/create-zhin-agent.d.ts.map +1 -1
- package/lib/init/create-zhin-agent.js +31 -20
- package/lib/init/create-zhin-agent.js.map +1 -1
- package/lib/init/register-ai-trigger.d.ts.map +1 -1
- package/lib/init/register-ai-trigger.js +10 -3
- package/lib/init/register-ai-trigger.js.map +1 -1
- package/lib/init/register-builtin-tools.d.ts.map +1 -1
- package/lib/init/register-builtin-tools.js +46 -14
- package/lib/init/register-builtin-tools.js.map +1 -1
- package/lib/init/register-db-models.d.ts.map +1 -1
- package/lib/init/register-db-models.js +1 -3
- package/lib/init/register-db-models.js.map +1 -1
- package/lib/init/register-db-upgrade.d.ts.map +1 -1
- package/lib/init/register-db-upgrade.js +1 -8
- package/lib/init/register-db-upgrade.js.map +1 -1
- package/lib/init/register-management-tools.d.ts.map +1 -1
- package/lib/init/register-management-tools.js +33 -20
- package/lib/init/register-management-tools.js.map +1 -1
- package/lib/service.d.ts.map +1 -1
- package/lib/service.js +0 -8
- package/lib/service.js.map +1 -1
- package/lib/zhin-agent/builtin-tools.d.ts +0 -2
- package/lib/zhin-agent/builtin-tools.d.ts.map +1 -1
- package/lib/zhin-agent/builtin-tools.js +0 -55
- package/lib/zhin-agent/builtin-tools.js.map +1 -1
- package/lib/zhin-agent/config.d.ts +2 -1
- package/lib/zhin-agent/config.d.ts.map +1 -1
- package/lib/zhin-agent/config.js +1 -1
- package/lib/zhin-agent/config.js.map +1 -1
- package/lib/zhin-agent/index.d.ts +1 -6
- package/lib/zhin-agent/index.d.ts.map +1 -1
- package/lib/zhin-agent/index.js +26 -34
- package/lib/zhin-agent/index.js.map +1 -1
- package/lib/zhin-agent/prompt.d.ts.map +1 -1
- package/lib/zhin-agent/prompt.js +31 -76
- package/lib/zhin-agent/prompt.js.map +1 -1
- package/lib/zhin-agent/tool-collector.d.ts.map +1 -1
- package/lib/zhin-agent/tool-collector.js +7 -7
- package/lib/zhin-agent/tool-collector.js.map +1 -1
- package/package.json +7 -4
- package/CHANGELOG.md +0 -190
- package/lib/follow-up.d.ts +0 -131
- package/lib/follow-up.d.ts.map +0 -1
- package/lib/follow-up.js +0 -265
- package/lib/follow-up.js.map +0 -1
- package/src/agent.ts +0 -6
- package/src/bootstrap.ts +0 -309
- package/src/builtin-tools.ts +0 -958
- package/src/compaction.ts +0 -28
- package/src/context-manager.ts +0 -15
- package/src/conversation-memory.ts +0 -5
- package/src/cron-engine.ts +0 -338
- package/src/discover-agents.ts +0 -138
- package/src/discover-skills.ts +0 -325
- package/src/discover-tools.ts +0 -302
- package/src/discovery-utils.ts +0 -96
- package/src/file-policy.ts +0 -333
- package/src/follow-up.ts +0 -357
- package/src/hooks.ts +0 -223
- package/src/index.ts +0 -183
- package/src/init/create-zhin-agent.ts +0 -161
- package/src/init/register-ai-service.ts +0 -53
- package/src/init/register-ai-trigger.ts +0 -253
- package/src/init/register-builtin-tools.ts +0 -308
- package/src/init/register-db-models.ts +0 -31
- package/src/init/register-db-upgrade.ts +0 -77
- package/src/init/register-management-tools.ts +0 -71
- package/src/init/register-message-recorder.ts +0 -31
- package/src/init/register-tool-service.ts +0 -9
- package/src/init/shared-refs.ts +0 -20
- package/src/init/types.ts +0 -18
- package/src/init.ts +0 -50
- package/src/output.ts +0 -15
- package/src/rate-limiter.ts +0 -5
- package/src/service.ts +0 -228
- package/src/session.ts +0 -13
- package/src/storage.ts +0 -9
- package/src/subagent.ts +0 -209
- package/src/tone-detector.ts +0 -5
- package/src/tools.ts +0 -214
- package/src/user-profile.ts +0 -182
- package/src/zhin-agent/builtin-tools.ts +0 -247
- package/src/zhin-agent/config.ts +0 -124
- package/src/zhin-agent/exec-policy.ts +0 -285
- package/src/zhin-agent/index.ts +0 -633
- package/src/zhin-agent/prompt.ts +0 -305
- package/src/zhin-agent/tool-collector.ts +0 -249
- package/tests/ai/follow-up.test.ts +0 -175
- package/tests/ai/integration.test.ts +0 -582
- package/tests/ai/multimodal.test.ts +0 -106
- package/tests/ai/setup.ts +0 -186
- package/tests/ai/subagent.test.ts +0 -270
- package/tests/ai/tools-builtin.test.ts +0 -310
- package/tests/ai/user-profile.test.ts +0 -73
- package/tests/ai/zhin-agent.test.ts +0 -306
- package/tests/exec-policy.test.ts +0 -355
- package/tests/file-policy.test.ts +0 -405
- package/tsconfig.json +0 -22
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ZhinAgent 执行策略 — bash 命令的安全检查与工具包装
|
|
3
|
-
*
|
|
4
|
-
* 参考 Claude Code bashPermissions.ts 的纵深防御策略:
|
|
5
|
-
* 1. 危险命令黑名单 — 即使 full 模式也阻止解释器/提权命令
|
|
6
|
-
* 2. 环境变量前缀剥离 — `FOO=bar cmd` → 按 `cmd` 做白名单匹配
|
|
7
|
-
* 3. Safe wrapper 剥离 — `timeout 10 cmd` → 按 `cmd` 做匹配
|
|
8
|
-
* 4. 复合命令拆分 — `&&` `||` `;` 逐段独立检查,deny 优先
|
|
9
|
-
* 5. 只读命令自动放行 — 与 file-policy classifyBashCommand 集成
|
|
10
|
-
* 6. ask_user 集成 — execAsk=true 时返回需审批标记(而非无法交互的抛错)
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { AgentTool } from '@zhin.js/core';
|
|
14
|
-
import type { ZhinAgentConfig } from './config.js';
|
|
15
|
-
import { classifyBashCommand } from '../file-policy.js';
|
|
16
|
-
|
|
17
|
-
// ── 预设命令白名单 ──────────────────────────────────────────────────
|
|
18
|
-
|
|
19
|
-
const PRESET_READONLY = ['ls', 'cat', 'pwd', 'date', 'whoami', 'grep', 'find', 'head', 'tail', 'wc', 'stat', 'file'];
|
|
20
|
-
const PRESET_NETWORK = [...PRESET_READONLY, 'curl', 'wget', 'ping', 'dig', 'nslookup', 'host'];
|
|
21
|
-
const PRESET_DEVELOPMENT = [...PRESET_NETWORK, 'npm', 'npx', 'node', 'git', 'gh', 'python', 'python3', 'pip', 'pnpm', 'yarn', 'tsc', 'bun'];
|
|
22
|
-
|
|
23
|
-
export const EXEC_PRESETS: Record<string, string[]> = {
|
|
24
|
-
readonly: PRESET_READONLY,
|
|
25
|
-
network: PRESET_NETWORK,
|
|
26
|
-
development: PRESET_DEVELOPMENT,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// ── 危险命令黑名单(参考 Claude Code DANGEROUS_COMMANDS)──────────────
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 即使在 full 模式下也会被阻止的危险命令。
|
|
33
|
-
* 这些命令可以执行任意代码、提权或造成不可逆破坏。
|
|
34
|
-
*/
|
|
35
|
-
const DANGEROUS_COMMANDS: ReadonlySet<string> = new Set([
|
|
36
|
-
// 提权
|
|
37
|
-
'sudo', 'su', 'doas',
|
|
38
|
-
// Shell 元命令 — 可执行任意代码
|
|
39
|
-
'eval', 'exec',
|
|
40
|
-
// 系统级破坏
|
|
41
|
-
'dd', 'mkfs', 'fdisk', 'parted',
|
|
42
|
-
// 进程注入
|
|
43
|
-
'gdb', 'strace', 'ltrace', 'ptrace',
|
|
44
|
-
// 环境注入(敏感变量可被设置)
|
|
45
|
-
'export',
|
|
46
|
-
]);
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 检查命令是否在危险黑名单中。
|
|
50
|
-
*/
|
|
51
|
-
export function isDangerousCommand(cmdName: string): boolean {
|
|
52
|
-
return DANGEROUS_COMMANDS.has(cmdName);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// ── 环境变量前缀剥离(参考 Claude Code stripEnvVars)──────────────
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 剥离命令前面的 `KEY=value` 环境变量前缀。
|
|
59
|
-
* 例如 `FOO=bar BAZ=1 curl http://...` → `curl http://...`
|
|
60
|
-
*
|
|
61
|
-
* 只剥离安全的 key=value 对,不剥离含特殊字符的值(可能是注入)。
|
|
62
|
-
*/
|
|
63
|
-
export function stripEnvVarPrefix(command: string): string {
|
|
64
|
-
// env 前缀环境变量格式: WORD=VALUE (VALUE 可以被引号或不含空格的字符串)
|
|
65
|
-
return command.replace(
|
|
66
|
-
/^(\s*[A-Za-z_][A-Za-z0-9_]*=(('[^']*'|"[^"]*"|[^\s;|&]*))\s*)+/,
|
|
67
|
-
'',
|
|
68
|
-
).trim();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ── Safe wrapper 剥离(参考 Claude Code stripSafeWrappers)──────────
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Safe wrapper 命令列表 — 这些命令本身是安全的"包装器",
|
|
75
|
-
* 真正需要检查的是它们后面的实际命令。
|
|
76
|
-
*/
|
|
77
|
-
const SAFE_WRAPPERS: ReadonlySet<string> = new Set([
|
|
78
|
-
'timeout', 'time', 'nice', 'nohup', 'ionice', 'stdbuf', 'unbuffer',
|
|
79
|
-
]);
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 剥离命令前面的 safe wrapper(如 `timeout 10`、`nice -n 5`)。
|
|
83
|
-
* 只剥离 wrapper + 它的标志/参数(数字、-flag 形式),直到遇到实际命令。
|
|
84
|
-
*/
|
|
85
|
-
export function stripSafeWrappers(command: string): string {
|
|
86
|
-
let remaining = command.trim();
|
|
87
|
-
let changed = true;
|
|
88
|
-
// 防止无限循环,最多剥离 5 层
|
|
89
|
-
let maxIter = 5;
|
|
90
|
-
while (changed && maxIter-- > 0) {
|
|
91
|
-
changed = false;
|
|
92
|
-
const tokens = remaining.split(/\s+/);
|
|
93
|
-
if (tokens.length < 2) break;
|
|
94
|
-
if (SAFE_WRAPPERS.has(tokens[0])) {
|
|
95
|
-
// 跳过 wrapper 本身和它的参数(-flag 或纯数字/duration)
|
|
96
|
-
let i = 1;
|
|
97
|
-
while (i < tokens.length && /^(-[A-Za-z0-9]|[0-9]+[smhd]?$)/.test(tokens[i])) {
|
|
98
|
-
i++;
|
|
99
|
-
}
|
|
100
|
-
if (i < tokens.length) {
|
|
101
|
-
remaining = tokens.slice(i).join(' ');
|
|
102
|
-
changed = true;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return remaining;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// ── 复合命令拆分(参考 Claude Code compound command checking)──────
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 将复合命令按 `&&`, `||`, `;` 拆分为独立子命令。
|
|
113
|
-
* 管道 `|` 不拆分 — 管道中的只读性由 classifyBashCommand 判断。
|
|
114
|
-
*
|
|
115
|
-
* 注意:不处理 subshell `$(...)` 和反引号 — 这些场景由危险黑名单覆盖。
|
|
116
|
-
*/
|
|
117
|
-
export function splitCompoundCommand(command: string): string[] {
|
|
118
|
-
// 按 &&, ||, ; 拆分,保留管道作为整体
|
|
119
|
-
return command.split(/\s*(?:&&|\|\||;)\s*/).map(s => s.trim()).filter(Boolean);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* 从命令字符串中提取实际的可执行程序名。
|
|
124
|
-
* 先剥离环境变量前缀和 safe wrapper。
|
|
125
|
-
*/
|
|
126
|
-
export function extractCommandName(command: string): string {
|
|
127
|
-
const stripped = stripSafeWrappers(stripEnvVarPrefix(command));
|
|
128
|
-
// 取第一个非管道 token
|
|
129
|
-
const name = stripped.split(/[\s|]/)[0] || '';
|
|
130
|
-
return name;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// ── 策略检查结果 ────────────────────────────────────────────────────
|
|
134
|
-
|
|
135
|
-
export interface ExecPolicyResult {
|
|
136
|
-
allowed: boolean;
|
|
137
|
-
/** 如果不允许,拒绝原因 */
|
|
138
|
-
reason?: string;
|
|
139
|
-
/** 如果需要用户确认(execAsk=true 且命令不在白名单但也不在黑名单) */
|
|
140
|
-
needsApproval?: boolean;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ── 核心检查函数 ────────────────────────────────────────────────────
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Resolves the effective allowlist by merging preset commands with custom allowlist.
|
|
147
|
-
*/
|
|
148
|
-
export function resolveExecAllowlist(config: Required<ZhinAgentConfig>): string[] {
|
|
149
|
-
const preset = config.execPreset;
|
|
150
|
-
const presetList = (preset && preset !== 'custom') ? (EXEC_PRESETS[preset] ?? []) : [];
|
|
151
|
-
const custom = config.execAllowlist ?? [];
|
|
152
|
-
const merged = [...new Set([...presetList, ...custom])];
|
|
153
|
-
return merged;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 检查单条子命令是否允许执行。
|
|
158
|
-
* 内部函数 — 不做复合命令拆分。
|
|
159
|
-
*/
|
|
160
|
-
function checkSingleCommand(
|
|
161
|
-
cmdName: string,
|
|
162
|
-
fullSubCommand: string,
|
|
163
|
-
allowlist: string[],
|
|
164
|
-
security: string,
|
|
165
|
-
execAsk: boolean,
|
|
166
|
-
): ExecPolicyResult {
|
|
167
|
-
// 1. 危险黑名单 — 任何模式都拒绝
|
|
168
|
-
if (isDangerousCommand(cmdName)) {
|
|
169
|
-
return { allowed: false, reason: `拒绝执行危险命令「${cmdName}」— 该命令可提权或执行任意代码。` };
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// 2. full 模式 — 通过黑名单后全部放行
|
|
173
|
-
if (security === 'full') {
|
|
174
|
-
return { allowed: true };
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// 3. 只读命令自动放行(与 file-policy classifyBashCommand 集成)
|
|
178
|
-
const classification = classifyBashCommand(fullSubCommand);
|
|
179
|
-
if (classification.isReadOnly) {
|
|
180
|
-
return { allowed: true };
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// 4. 白名单匹配
|
|
184
|
-
const allowed = allowlist.some(pattern => {
|
|
185
|
-
try {
|
|
186
|
-
const re = new RegExp(`^${pattern}$`);
|
|
187
|
-
return re.test(cmdName);
|
|
188
|
-
} catch {
|
|
189
|
-
return cmdName === pattern;
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
if (allowed) {
|
|
194
|
-
return { allowed: true };
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// 5. 需审批或拒绝
|
|
198
|
-
if (execAsk) {
|
|
199
|
-
return {
|
|
200
|
-
allowed: false,
|
|
201
|
-
needsApproval: true,
|
|
202
|
-
reason: `命令「${cmdName}」不在允许列表中,需要用户确认后执行。`,
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return {
|
|
207
|
-
allowed: false,
|
|
208
|
-
reason: `命令「${cmdName}」不在允许列表中,已被拒绝。可将命令加入 ai.agent.execAllowlist 或改用 execPreset。`,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Check if a bash command is allowed under the current exec policy.
|
|
214
|
-
* 支持复合命令拆分、环境变量剥离、safe wrapper 剥离、只读自动放行。
|
|
215
|
-
*
|
|
216
|
-
* @returns ExecPolicyResult — 允许/拒绝/需审批
|
|
217
|
-
*/
|
|
218
|
-
export function checkExecPolicy(config: Required<ZhinAgentConfig>, command: string): ExecPolicyResult {
|
|
219
|
-
const security = config.execSecurity ?? 'deny';
|
|
220
|
-
if (security === 'deny') {
|
|
221
|
-
return { allowed: false, reason: '当前配置禁止执行 Shell 命令(execSecurity=deny)。如需开放请在配置中设置 ai.agent.execSecurity。' };
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const allowlist = resolveExecAllowlist(config);
|
|
225
|
-
const execAsk = config.execAsk ?? false;
|
|
226
|
-
const cmd = (command || '').trim();
|
|
227
|
-
|
|
228
|
-
if (!cmd) {
|
|
229
|
-
return { allowed: false, reason: '命令为空' };
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// 拆分复合命令 — 每段独立检查,deny 优先
|
|
233
|
-
const subCommands = splitCompoundCommand(cmd);
|
|
234
|
-
let pendingApproval: ExecPolicyResult | null = null;
|
|
235
|
-
|
|
236
|
-
for (const sub of subCommands) {
|
|
237
|
-
const cmdName = extractCommandName(sub);
|
|
238
|
-
if (!cmdName) continue;
|
|
239
|
-
|
|
240
|
-
const result = checkSingleCommand(cmdName, sub, allowlist, security, execAsk);
|
|
241
|
-
|
|
242
|
-
// deny 立即返回(deny > ask 优先级)
|
|
243
|
-
if (!result.allowed && !result.needsApproval) {
|
|
244
|
-
return result;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// 记录第一个需要审批的
|
|
248
|
-
if (!result.allowed && result.needsApproval && !pendingApproval) {
|
|
249
|
-
pendingApproval = result;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// 有需要审批的段
|
|
254
|
-
if (pendingApproval) {
|
|
255
|
-
return pendingApproval;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return { allowed: true };
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Wrap `bash` tools with exec policy enforcement.
|
|
263
|
-
* 当 execAsk=true 且命令需审批时,返回提示信息而非抛错。
|
|
264
|
-
*/
|
|
265
|
-
export function applyExecPolicyToTools(config: Required<ZhinAgentConfig>, tools: AgentTool[]): AgentTool[] {
|
|
266
|
-
return tools.map(t => {
|
|
267
|
-
if (t.name !== 'bash') return t;
|
|
268
|
-
const original = t.execute;
|
|
269
|
-
return {
|
|
270
|
-
...t,
|
|
271
|
-
execute: async (args: Record<string, any>) => {
|
|
272
|
-
const cmd = args?.command != null ? String(args.command) : '';
|
|
273
|
-
const result = checkExecPolicy(config, cmd);
|
|
274
|
-
if (!result.allowed) {
|
|
275
|
-
if (result.needsApproval) {
|
|
276
|
-
// 返回可读消息让 AI 用 ask_user 向 Owner 确认
|
|
277
|
-
return `⚠️ ${result.reason}\n请使用 ask_user 工具向 Owner 确认是否允许执行此命令。`;
|
|
278
|
-
}
|
|
279
|
-
throw new Error(result.reason!);
|
|
280
|
-
}
|
|
281
|
-
return original(args);
|
|
282
|
-
},
|
|
283
|
-
};
|
|
284
|
-
});
|
|
285
|
-
}
|