feat: v3.0 Tool Calling — AI 本地文件操作系统

新增文件:
- src/main/tool-security.ts: 路径白名单/黑名单、命令安全检查
- src/main/tool-handlers.ts: 7个工具实现(read/write/list/search/create/delete/run)
- src/renderer/services/tool-registry.ts: 工具注册调度中心
- src/renderer/services/agent-engine.ts: Agent Loop 流式多轮工具调用引擎
- src/renderer/components/tool-confirm-modal.ts: 高风险操作确认对话框

修改文件:
- types.d.ts: 新增 ToolCall/ToolResult/ToolCallRecord 等类型
- ollama.ts: chatStream 支持 tools 参数
- ipc.ts: 新增 tool:execute/getConfig/setAllowedDirs IPC
- preload.ts: 暴露 tool API
- chat-area.ts: 渲染工具调用卡片
- input-area.ts: 集成 Agent Loop 引擎
- settings-modal.ts: 工具调用开关设置
- index.html: 工具调用设置面板 + 确认对话框 HTML
- style.css: 工具调用卡片、确认对话框样式
- main.ts: 初始化工具调用配置
This commit is contained in:
thzxx
2026-04-06 13:29:43 +08:00
parent 0a1397771e
commit 5bfb137a8a
15 changed files with 1609 additions and 6 deletions
+69 -2
View File
@@ -1,11 +1,13 @@
// ── Ollama API 类型 ──
export interface OllamaMessage {
role: 'user' | 'assistant' | 'system';
role: 'user' | 'assistant' | 'system' | 'tool';
content: string;
images?: string[];
thinking?: string;
reasoning_content?: string;
tool_calls?: ToolCall[];
tool_name?: string;
}
export interface OllamaChatParams {
@@ -14,6 +16,7 @@ export interface OllamaChatParams {
stream?: boolean;
think?: boolean;
system?: string;
tools?: ToolDefinition[];
keep_alive?: number | string;
options?: {
num_ctx?: number;
@@ -29,6 +32,7 @@ export interface OllamaStreamChunk {
content?: string;
thinking?: string;
reasoning_content?: string;
tool_calls?: ToolCall[];
};
done?: boolean;
eval_count?: number;
@@ -100,6 +104,7 @@ export interface ChatMessage {
_fileContents?: FileContent[];
ragSources?: RagSource[];
stopped?: boolean;
toolCalls?: ToolCallRecord[];
}
export interface ChatSession {
@@ -172,6 +177,11 @@ export interface MetonaDesktopAPI {
readFile: (filePath: string) => Promise<{ success: boolean; content?: string; name?: string; error?: string }>;
writeFile: (filePath: string, content: string) => Promise<{ success: boolean; error?: string }>;
};
tool: {
execute: (toolName: string, args: Record<string, unknown>) => Promise<ToolResult>;
getConfig: () => Promise<{ allowedDirs: string[]; blockedDirs: string[] }>;
setAllowedDirs: (dirs: string[]) => Promise<void>;
};
notify: (title: string, body: string) => void;
window: {
minimize: () => void;
@@ -238,7 +248,62 @@ export type StateKey =
| 'temperature'
| 'thinkEnabled'
| 'presets'
| 'activePresetId';
| 'activePresetId'
| 'toolCallingEnabled'
| 'runCommandEnabled';
// ═══════════════════════════════════════════════════════════
// Tool Calling 类型
// ═══════════════════════════════════════════════════════════
export interface ToolParameterProperty {
type: string;
description?: string;
enum?: string[];
items?: { type: string };
}
export interface ToolParameters {
type: 'object';
required?: string[];
properties: Record<string, ToolParameterProperty>;
}
export interface ToolFunctionDefinition {
name: string;
description: string;
parameters: ToolParameters;
}
export interface ToolDefinition {
type: 'function';
function: ToolFunctionDefinition;
}
export interface ToolCall {
type: 'function';
function: {
name: string;
arguments: Record<string, unknown>;
};
}
export interface ToolResult {
success: boolean;
error?: string;
[key: string]: unknown;
}
export interface ToolCallRecord {
name: string;
arguments: Record<string, unknown>;
result: ToolResult | null;
status: 'pending' | 'running' | 'success' | 'error' | 'cancelled';
confirmed?: boolean;
timestamp: number;
}
export type AgentState = 'idle' | 'sending' | 'accumulating' | 'executing' | 'confirming' | 'done';
// ── Window global 声明 ──
@@ -248,3 +313,5 @@ declare global {
__metonaBridge?: DesktopBridge;
}
}
export {};