后端修复: Ollama adapter 移除死代码/修复超时硬编码/iteration硬编码/pullModel无超时/流异常断开补发DONE; SSE解析器支持非字符串arguments; Sandbox fail-closed安全加固; IPC移除未使用变量 新增功能: TaskOrchestrator子任务编排器; DelegateTaskTool委派工具; Agent Loop并行工具执行; 上下文自动压缩; 记忆检索注入System Prompt 前端修复: useAgentStream text_delta thought累积bug; 工具调用状态正确流转; 修复重复stateChange事件; TraceStep.thought正确填充
89 lines
3.2 KiB
TypeScript
89 lines
3.2 KiB
TypeScript
/**
|
|
* Policy Engine — 权限策略引擎
|
|
*
|
|
* 三级权限模型:Read / Write / External Action
|
|
*
|
|
* @see docs/生产级通用 AI Agent 智能体桌面应用:完整设计与构建指南.html — 第十章
|
|
*/
|
|
|
|
export enum PermissionLevel {
|
|
READ = 'read',
|
|
WRITE = 'write',
|
|
EXTERNAL_ACTION = 'external',
|
|
}
|
|
|
|
export interface PermissionPolicy {
|
|
toolName: string;
|
|
requiredLevel: PermissionLevel;
|
|
allowedPatterns?: RegExp[];
|
|
deniedPatterns?: RegExp[];
|
|
maxFrequency?: number;
|
|
requireConfirmation?: boolean;
|
|
}
|
|
|
|
export const DEFAULT_POLICIES: PermissionPolicy[] = [
|
|
{ toolName: 'read_file', requiredLevel: PermissionLevel.READ, deniedPatterns: [/\/etc\//, /\/proc\//, /C:\\Windows\\/i, /C:\\System32\\/i, /MEMORY\.md/i] },
|
|
{ toolName: 'web_search', requiredLevel: PermissionLevel.READ, maxFrequency: 10 },
|
|
{ toolName: 'list_directory', requiredLevel: PermissionLevel.READ },
|
|
{ toolName: 'search_files', requiredLevel: PermissionLevel.READ },
|
|
{ toolName: 'memory_search', requiredLevel: PermissionLevel.READ },
|
|
{ toolName: 'write_file', requiredLevel: PermissionLevel.WRITE, deniedPatterns: [/\/etc\//, /\/proc\//, /\/System\//, /C:\\Windows\\/i, /C:\\System32\\/i, /MEMORY\.md/i], requireConfirmation: true, maxFrequency: 5 },
|
|
{ toolName: 'memory_store', requiredLevel: PermissionLevel.WRITE },
|
|
{ toolName: 'run_command', requiredLevel: PermissionLevel.EXTERNAL_ACTION, deniedPatterns: [/MEMORY\.md/i], requireConfirmation: true, maxFrequency: 3 },
|
|
{ toolName: 'web_fetch', requiredLevel: PermissionLevel.READ },
|
|
// web_browser — 统一浏览器工具(合并自 9 个独立 browser_* 工具)
|
|
// 由于该工具可执行 JS、点击元素等高风险操作,统一设为 EXTERNAL_ACTION
|
|
{ toolName: 'web_browser', requiredLevel: PermissionLevel.EXTERNAL_ACTION, requireConfirmation: true, maxFrequency: 20 },
|
|
];
|
|
|
|
export class PolicyEngine {
|
|
private policies: Map<string, PermissionPolicy> = new Map();
|
|
|
|
constructor(customPolicies: PermissionPolicy[] = []) {
|
|
for (const policy of DEFAULT_POLICIES) {
|
|
this.policies.set(policy.toolName, policy);
|
|
}
|
|
for (const policy of customPolicies) {
|
|
this.policies.set(policy.toolName, policy);
|
|
}
|
|
}
|
|
|
|
checkAuthorization(toolName: string, args: Record<string, unknown>): {
|
|
authorized: boolean;
|
|
reason?: string;
|
|
level: PermissionLevel;
|
|
requiresConfirmation: boolean;
|
|
} {
|
|
const policy = this.policies.get(toolName);
|
|
|
|
if (!policy) {
|
|
return {
|
|
authorized: false,
|
|
reason: `No policy configured for tool: ${toolName}`,
|
|
level: PermissionLevel.EXTERNAL_ACTION,
|
|
requiresConfirmation: true,
|
|
};
|
|
}
|
|
|
|
if (policy.deniedPatterns) {
|
|
const argsStr = JSON.stringify(args);
|
|
for (const pattern of policy.deniedPatterns) {
|
|
if (pattern.test(argsStr)) {
|
|
return {
|
|
authorized: false,
|
|
reason: `Arguments match denied pattern: ${pattern.source}`,
|
|
level: policy.requiredLevel,
|
|
requiresConfirmation: false,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
authorized: true,
|
|
level: policy.requiredLevel,
|
|
requiresConfirmation: policy.requireConfirmation ?? false,
|
|
};
|
|
}
|
|
}
|