@cicctencent/agent-core 0.1.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 +952 -0
- package/dist/agent/engine.d.ts +113 -0
- package/dist/agent/engine.d.ts.map +1 -0
- package/dist/agent/history-manager.d.ts +78 -0
- package/dist/agent/history-manager.d.ts.map +1 -0
- package/dist/agent/llm-caller.d.ts +58 -0
- package/dist/agent/llm-caller.d.ts.map +1 -0
- package/dist/agent/tool-executor.d.ts +95 -0
- package/dist/agent/tool-executor.d.ts.map +1 -0
- package/dist/agent/types.d.ts +128 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent/workflow.d.ts +74 -0
- package/dist/agent/workflow.d.ts.map +1 -0
- package/dist/context/collector.d.ts +12 -0
- package/dist/context/collector.d.ts.map +1 -0
- package/dist/context/manager.d.ts +16 -0
- package/dist/context/manager.d.ts.map +1 -0
- package/dist/context/types.d.ts +39 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/hooks/types.d.ts +47 -0
- package/dist/hooks/types.d.ts.map +1 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/llm/anthropic-provider.d.ts +28 -0
- package/dist/llm/anthropic-provider.d.ts.map +1 -0
- package/dist/llm/factory.d.ts +19 -0
- package/dist/llm/factory.d.ts.map +1 -0
- package/dist/llm/ollama-provider.d.ts +13 -0
- package/dist/llm/ollama-provider.d.ts.map +1 -0
- package/dist/llm/openai-provider.d.ts +18 -0
- package/dist/llm/openai-provider.d.ts.map +1 -0
- package/dist/llm/provider.d.ts +7 -0
- package/dist/llm/provider.d.ts.map +1 -0
- package/dist/llm/types.d.ts +78 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/mcp/base-client.d.ts +50 -0
- package/dist/mcp/base-client.d.ts.map +1 -0
- package/dist/mcp/client.d.ts +27 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/factory.d.ts +16 -0
- package/dist/mcp/factory.d.ts.map +1 -0
- package/dist/mcp/http-client.d.ts +24 -0
- package/dist/mcp/http-client.d.ts.map +1 -0
- package/dist/mcp/proxy-agent.d.ts +49 -0
- package/dist/mcp/proxy-agent.d.ts.map +1 -0
- package/dist/mcp/stdio-client.d.ts +16 -0
- package/dist/mcp/stdio-client.d.ts.map +1 -0
- package/dist/mcp/types.d.ts +60 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/memory/checkpoint.d.ts +62 -0
- package/dist/memory/checkpoint.d.ts.map +1 -0
- package/dist/memory/extractor.d.ts +59 -0
- package/dist/memory/extractor.d.ts.map +1 -0
- package/dist/memory/persona.d.ts +38 -0
- package/dist/memory/persona.d.ts.map +1 -0
- package/dist/memory/pipeline.d.ts +50 -0
- package/dist/memory/pipeline.d.ts.map +1 -0
- package/dist/memory/prompts.d.ts +9 -0
- package/dist/memory/prompts.d.ts.map +1 -0
- package/dist/memory/recall.d.ts +66 -0
- package/dist/memory/recall.d.ts.map +1 -0
- package/dist/memory/scene.d.ts +49 -0
- package/dist/memory/scene.d.ts.map +1 -0
- package/dist/memory/types.d.ts +43 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/middleware/types.d.ts +52 -0
- package/dist/middleware/types.d.ts.map +1 -0
- package/dist/sandbox/types.d.ts +59 -0
- package/dist/sandbox/types.d.ts.map +1 -0
- package/dist/security/guard.d.ts +41 -0
- package/dist/security/guard.d.ts.map +1 -0
- package/dist/security/types.d.ts +36 -0
- package/dist/security/types.d.ts.map +1 -0
- package/dist/skill/manager.d.ts +95 -0
- package/dist/skill/manager.d.ts.map +1 -0
- package/dist/skill/router.d.ts +26 -0
- package/dist/skill/router.d.ts.map +1 -0
- package/dist/skill/types.d.ts +55 -0
- package/dist/skill/types.d.ts.map +1 -0
- package/dist/tool/delegate.d.ts +34 -0
- package/dist/tool/delegate.d.ts.map +1 -0
- package/dist/tool/registry.d.ts +24 -0
- package/dist/tool/registry.d.ts.map +1 -0
- package/dist/tool/types.d.ts +101 -0
- package/dist/tool/types.d.ts.map +1 -0
- package/dist/types.d.ts +150 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/token-counter.d.ts +22 -0
- package/dist/utils/token-counter.d.ts.map +1 -0
- package/package.json +42 -0
- package/src/agent/engine.ts +875 -0
- package/src/agent/history-manager.ts +323 -0
- package/src/agent/llm-caller.ts +383 -0
- package/src/agent/tool-executor.ts +713 -0
- package/src/agent/types.ts +136 -0
- package/src/agent/workflow.ts +339 -0
- package/src/context/collector.ts +59 -0
- package/src/context/manager.ts +40 -0
- package/src/context/types.ts +51 -0
- package/src/hooks/types.ts +77 -0
- package/src/index.ts +178 -0
- package/src/llm/anthropic-provider.ts +412 -0
- package/src/llm/factory.ts +34 -0
- package/src/llm/ollama-provider.ts +39 -0
- package/src/llm/openai-provider.ts +200 -0
- package/src/llm/provider.ts +15 -0
- package/src/llm/types.ts +91 -0
- package/src/mcp/base-client.ts +159 -0
- package/src/mcp/client.ts +81 -0
- package/src/mcp/factory.ts +45 -0
- package/src/mcp/http-client.ts +149 -0
- package/src/mcp/proxy-agent.ts +227 -0
- package/src/mcp/stdio-client.ts +104 -0
- package/src/mcp/types.ts +65 -0
- package/src/memory/checkpoint.ts +146 -0
- package/src/memory/extractor.ts +164 -0
- package/src/memory/persona.ts +106 -0
- package/src/memory/pipeline.ts +192 -0
- package/src/memory/prompts.ts +73 -0
- package/src/memory/recall.ts +217 -0
- package/src/memory/scene.ts +137 -0
- package/src/memory/types.ts +54 -0
- package/src/middleware/types.ts +62 -0
- package/src/sandbox/types.ts +185 -0
- package/src/security/guard.ts +249 -0
- package/src/security/types.ts +46 -0
- package/src/skill/manager.ts +420 -0
- package/src/skill/router.ts +158 -0
- package/src/skill/types.ts +67 -0
- package/src/tool/delegate.ts +105 -0
- package/src/tool/registry.ts +64 -0
- package/src/tool/types.ts +124 -0
- package/src/types.ts +188 -0
- package/src/utils/token-counter.ts +108 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// SecurityGuard — 命令安全检查 + 凭证泄露检测(带规则编译缓存)
|
|
3
|
+
// ============================================================
|
|
4
|
+
|
|
5
|
+
import type { SanitizeResult, SecurityRule, SecurityPolicy } from './types.ts';
|
|
6
|
+
|
|
7
|
+
/** 凭证泄露检测模式 */
|
|
8
|
+
const CREDENTIAL_PATTERNS: Array<{ name: string; pattern: RegExp }> = [
|
|
9
|
+
{ name: 'openai-key', pattern: /sk-[a-zA-Z0-9]{20,}/g },
|
|
10
|
+
{ name: 'anthropic-key', pattern: /sk-ant-[a-zA-Z0-9-]{20,}/g },
|
|
11
|
+
{ name: 'aws-access-key', pattern: /AKIA[0-9A-Z]{16}/g },
|
|
12
|
+
{ name: 'aws-secret-key', pattern: /(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])/g },
|
|
13
|
+
{ name: 'github-token', pattern: /ghp_[a-zA-Z0-9]{36}/g },
|
|
14
|
+
{ name: 'github-fine-grained', pattern: /github_pat_[a-zA-Z0-9_]{82}/g },
|
|
15
|
+
{ name: 'generic-bearer', pattern: /Bearer\s+[a-zA-Z0-9._-]+/gi },
|
|
16
|
+
{ name: 'generic-secret', pattern: /(?:password|secret|token|api_key|apikey)\s*[:=]\s*['"][^'"]{8,}['"]/gi },
|
|
17
|
+
{ name: 'private-key', pattern: /-----BEGIN (?:RSA |EC |DSA )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |DSA )?PRIVATE KEY-----/g },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/** 默认安全策略 */
|
|
21
|
+
const DEFAULT_POLICY: SecurityPolicy = {
|
|
22
|
+
deleteProtection: true,
|
|
23
|
+
trashDir: '~/.workagent/.trash',
|
|
24
|
+
blacklistEnabled: true,
|
|
25
|
+
rules: [],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** 内置黑名单规则 */
|
|
29
|
+
const BUILTIN_BLACKLIST: SecurityRule[] = [
|
|
30
|
+
{
|
|
31
|
+
name: 'block-rm-root',
|
|
32
|
+
pattern: /^rm\s+(-[a-zA-Z]*\s+)*\/$/m,
|
|
33
|
+
severity: 'blocked',
|
|
34
|
+
action: 'block',
|
|
35
|
+
description: 'Block rm -rf / (destroy root filesystem)',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'block-dd-disk',
|
|
39
|
+
pattern: /^dd\s+.*of=\/dev\/[sh]d/m,
|
|
40
|
+
severity: 'blocked',
|
|
41
|
+
action: 'block',
|
|
42
|
+
description: 'Block dd to disk device',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'block-mkfs',
|
|
46
|
+
pattern: /^mkfs\./,
|
|
47
|
+
severity: 'blocked',
|
|
48
|
+
action: 'block',
|
|
49
|
+
description: 'Block filesystem format commands',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: 'block-fork-bomb',
|
|
53
|
+
pattern: /^:\(\)\s*\{\s*:\|:\s*&\s*\}\s*;\s*:/,
|
|
54
|
+
severity: 'blocked',
|
|
55
|
+
action: 'block',
|
|
56
|
+
description: 'Block fork bomb',
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
/** 内置删除保护规则(rm → mv 到回收站,回收站路径通过 policy.trashDir 配置) */
|
|
61
|
+
function createDeleteProtectionRule(trashDir: string): SecurityRule {
|
|
62
|
+
return {
|
|
63
|
+
name: 'delete-protection',
|
|
64
|
+
pattern: /^(sudo\s+)?rm\s+((?:-[a-zA-Z]+\s+)*)(.+)$/s,
|
|
65
|
+
severity: 'warn',
|
|
66
|
+
action: 'rewrite',
|
|
67
|
+
rewriter: (command: string) => {
|
|
68
|
+
const match = command.match(/^(sudo\s+)?rm\s+((?:-[a-zA-Z]+\s+)*)(.+)$/s);
|
|
69
|
+
if (!match) return command;
|
|
70
|
+
const targets = match[3];
|
|
71
|
+
const ts = Date.now();
|
|
72
|
+
return `mkdir -p ${trashDir} && mv ${targets} ${trashDir}/_del_${ts}`;
|
|
73
|
+
},
|
|
74
|
+
description: 'Rewrite rm to mv trash for delete protection',
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class SecurityGuard {
|
|
79
|
+
private policy: SecurityPolicy;
|
|
80
|
+
|
|
81
|
+
/** 已编译的正则规则缓存(避免每次 sanitize 重复遍历数组) */
|
|
82
|
+
private compiledBlacklist: SecurityRule[] | null = null;
|
|
83
|
+
private compiledCustomRewrite: SecurityRule[] | null = null;
|
|
84
|
+
private compiledCustomWarn: SecurityRule[] | null = null;
|
|
85
|
+
/** 删除保护规则(使用 policy.trashDir) */
|
|
86
|
+
private deleteProtectionRule: SecurityRule;
|
|
87
|
+
|
|
88
|
+
constructor(policy?: Partial<SecurityPolicy>) {
|
|
89
|
+
this.policy = { ...DEFAULT_POLICY, ...policy };
|
|
90
|
+
this.deleteProtectionRule = createDeleteProtectionRule(this.policy.trashDir);
|
|
91
|
+
this.invalidateCompiledRules();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 编译规则缓存(policy 变更时调用) */
|
|
95
|
+
private invalidateCompiledRules(): void {
|
|
96
|
+
this.compiledBlacklist = null;
|
|
97
|
+
this.compiledCustomRewrite = null;
|
|
98
|
+
this.compiledCustomWarn = null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private getCompiledBlacklist(): SecurityRule[] {
|
|
102
|
+
if (!this.compiledBlacklist) {
|
|
103
|
+
this.compiledBlacklist = [...BUILTIN_BLACKLIST, ...this.policy.rules.filter((r) => r.action === 'block')];
|
|
104
|
+
}
|
|
105
|
+
return this.compiledBlacklist;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private getCompiledCustomRewrite(): SecurityRule[] {
|
|
109
|
+
if (!this.compiledCustomRewrite) {
|
|
110
|
+
this.compiledCustomRewrite = this.policy.rules.filter((r) => r.action === 'rewrite' && r.rewriter);
|
|
111
|
+
}
|
|
112
|
+
return this.compiledCustomRewrite;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private getCompiledCustomWarn(): SecurityRule[] {
|
|
116
|
+
if (!this.compiledCustomWarn) {
|
|
117
|
+
this.compiledCustomWarn = this.policy.rules.filter((r) => r.action === 'warn');
|
|
118
|
+
}
|
|
119
|
+
return this.compiledCustomWarn;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 检查命令安全性 */
|
|
123
|
+
sanitize(command: string): SanitizeResult {
|
|
124
|
+
const trimmedCmd = command.trim();
|
|
125
|
+
|
|
126
|
+
// ① 黑名单检查
|
|
127
|
+
if (this.policy.blacklistEnabled) {
|
|
128
|
+
for (const rule of this.getCompiledBlacklist()) {
|
|
129
|
+
if (this.matchRule(trimmedCmd, rule)) {
|
|
130
|
+
return {
|
|
131
|
+
safe: false,
|
|
132
|
+
reason: `BLOCKED: ${rule.description || rule.name}`,
|
|
133
|
+
severity: rule.severity,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ② 删除保护(rm → mv 回收站)
|
|
140
|
+
if (this.policy.deleteProtection) {
|
|
141
|
+
if (this.matchRule(trimmedCmd, this.deleteProtectionRule)) {
|
|
142
|
+
const rewritten = this.deleteProtectionRule.rewriter!(trimmedCmd);
|
|
143
|
+
return {
|
|
144
|
+
safe: true,
|
|
145
|
+
rewritten,
|
|
146
|
+
reason: 'Delete protection: rm rewritten to mv trash',
|
|
147
|
+
severity: 'warn',
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ③ 自定义 rewrite 规则
|
|
153
|
+
for (const rule of this.getCompiledCustomRewrite()) {
|
|
154
|
+
if (!this.matchRule(trimmedCmd, rule)) continue;
|
|
155
|
+
return {
|
|
156
|
+
safe: true,
|
|
157
|
+
rewritten: rule.rewriter!(trimmedCmd),
|
|
158
|
+
reason: `Rewritten by rule: ${rule.name}`,
|
|
159
|
+
severity: rule.severity,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ④ 自定义 warn 规则
|
|
164
|
+
for (const rule of this.getCompiledCustomWarn()) {
|
|
165
|
+
if (this.matchRule(trimmedCmd, rule)) {
|
|
166
|
+
return {
|
|
167
|
+
safe: true,
|
|
168
|
+
reason: `Warning: ${rule.description || rule.name}`,
|
|
169
|
+
severity: rule.severity,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ⑤ 安全
|
|
175
|
+
return { safe: true, severity: 'safe' };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 添加自定义安全规则 */
|
|
179
|
+
addRule(rule: SecurityRule): void {
|
|
180
|
+
this.policy.rules.push(rule);
|
|
181
|
+
this.invalidateCompiledRules();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** 移除规则 */
|
|
185
|
+
removeRule(name: string): void {
|
|
186
|
+
this.policy.rules = this.policy.rules.filter((r) => r.name !== name);
|
|
187
|
+
this.invalidateCompiledRules();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 更新安全策略 */
|
|
191
|
+
setPolicy(policy: Partial<SecurityPolicy>): void {
|
|
192
|
+
this.policy = { ...this.policy, ...policy };
|
|
193
|
+
if (policy.trashDir) {
|
|
194
|
+
this.deleteProtectionRule = createDeleteProtectionRule(this.policy.trashDir);
|
|
195
|
+
}
|
|
196
|
+
this.invalidateCompiledRules();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 获取当前策略 */
|
|
200
|
+
getPolicy(): SecurityPolicy {
|
|
201
|
+
return { ...this.policy };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ---- 凭证泄露检测 ----
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 扫描并脱敏文本中的敏感信息
|
|
208
|
+
* 将检测到的 API Key、Token、密码等替换为 [REDACTED:xxx]
|
|
209
|
+
* @param text 输入文本(通常是工具结果)
|
|
210
|
+
* @returns 脱敏后的文本
|
|
211
|
+
*/
|
|
212
|
+
sanitizeCredentials(text: string): string {
|
|
213
|
+
if (!text || text.length < 10) return text;
|
|
214
|
+
|
|
215
|
+
let result = text;
|
|
216
|
+
for (const { name, pattern } of CREDENTIAL_PATTERNS) {
|
|
217
|
+
// 重置正则 lastIndex(全局标志下)
|
|
218
|
+
pattern.lastIndex = 0;
|
|
219
|
+
result = result.replace(pattern, `[REDACTED:${name}]`);
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 检测文本是否包含敏感信息
|
|
226
|
+
* @param text 输入文本
|
|
227
|
+
* @returns 检测到的敏感信息类型列表
|
|
228
|
+
*/
|
|
229
|
+
detectCredentials(text: string): string[] {
|
|
230
|
+
if (!text) return [];
|
|
231
|
+
const detected: string[] = [];
|
|
232
|
+
for (const { name, pattern } of CREDENTIAL_PATTERNS) {
|
|
233
|
+
pattern.lastIndex = 0;
|
|
234
|
+
if (pattern.test(text)) {
|
|
235
|
+
detected.push(name);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return detected;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ---- 内部辅助 ----
|
|
242
|
+
|
|
243
|
+
private matchRule(command: string, rule: SecurityRule): boolean {
|
|
244
|
+
if (rule.pattern instanceof RegExp) {
|
|
245
|
+
return rule.pattern.test(command);
|
|
246
|
+
}
|
|
247
|
+
return rule.pattern(command);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Security Guard 类型定义
|
|
3
|
+
// ============================================================
|
|
4
|
+
|
|
5
|
+
// ---- SanitizeResult ----
|
|
6
|
+
|
|
7
|
+
export type Severity = 'safe' | 'warn' | 'danger' | 'blocked';
|
|
8
|
+
|
|
9
|
+
export interface SanitizeResult {
|
|
10
|
+
/** 是否允许执行 */
|
|
11
|
+
safe: boolean;
|
|
12
|
+
/** 改写后的命令(如 rm → mv 到回收站) */
|
|
13
|
+
rewritten?: string;
|
|
14
|
+
/** 拒绝/警告原因 */
|
|
15
|
+
reason?: string;
|
|
16
|
+
/** 严重程度 */
|
|
17
|
+
severity: Severity;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ---- SecurityRule / SecurityPolicy ----
|
|
21
|
+
|
|
22
|
+
export interface SecurityRule {
|
|
23
|
+
/** 规则名称 */
|
|
24
|
+
name: string;
|
|
25
|
+
/** 匹配模式(正则或函数) */
|
|
26
|
+
pattern: RegExp | ((command: string) => boolean);
|
|
27
|
+
/** 命中后的严重程度 */
|
|
28
|
+
severity: Severity;
|
|
29
|
+
/** 命中后的处理:'block' 阻止 | 'rewrite' 改写 | 'warn' 放行但记录 */
|
|
30
|
+
action: 'block' | 'rewrite' | 'warn';
|
|
31
|
+
/** 改写函数(action='rewrite' 时使用) */
|
|
32
|
+
rewriter?: (command: string) => string;
|
|
33
|
+
/** 规则描述 */
|
|
34
|
+
description?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SecurityPolicy {
|
|
38
|
+
/** 是否启用删除保护(rm → mv 到回收站) */
|
|
39
|
+
deleteProtection: boolean;
|
|
40
|
+
/** 回收站路径 */
|
|
41
|
+
trashDir: string;
|
|
42
|
+
/** 是否启用黑名单 */
|
|
43
|
+
blacklistEnabled: boolean;
|
|
44
|
+
/** 自定义规则 */
|
|
45
|
+
rules: SecurityRule[];
|
|
46
|
+
}
|