feat: MetonaAI Desktop 初始项目

- Electron + React + TypeScript 架构
- 三栏布局: Sidebar | ChatPanel | DetailPanel
- 9 个内置工具 (文件系统/网络/记忆/命令)
- SQLite 持久化 (better-sqlite3)
- MUI 暗色/亮色主题系统
- Agent Loop ReAct 状态机引擎
- DeepSeek / Agnes AI / Ollama Provider 适配器
- MCP 协议集成
- 系统托盘 + 全局快捷键
- Tailwind CSS v4 + Tailwind Merge
- 修复: Sidebar 缺失 TextField 导入导致黑屏
This commit is contained in:
thzxx
2026-06-27 21:33:27 +08:00
commit 1d185db6b3
109 changed files with 39155 additions and 0 deletions
+441
View File
@@ -0,0 +1,441 @@
/**
* Agent Loop — ReAct 状态机引擎
*
* 生产级 ReAct Agent Loop,负责:
* 1. 状态机管理循环生命周期
* 2. 调用 Provider Adapter 获取 LLM 响应(支持流式)
* 3. 解析输出、执行工具、注入观察
* 4. 流式事件推送 UI
* 5. 超时、重试、上下文压缩
*
* @see docs/生产级通用 AI Agent 智能体桌面应用:完整设计与构建指南.html — 第四章
*/
import { EventEmitter } from 'events';
import { nanoid } from 'nanoid';
import {
AgentLoopState,
TerminationReason,
type IterationStep,
type Thought,
type AgentLoopConfig,
type AgentLoopOutput,
type TokenUsage,
} from './types';
import type {
MetonaRequest,
MetonaResponse,
MetonaMessage,
MetonaSystemPrompt,
MetonaToolCall,
MetonaToolResult,
MetonaStreamEvent,
IMetonaProviderAdapter,
MetonaToolDef,
} from '../types';
import { MetonaStreamEventType, MetonaFinishReason } from '../types';
const DEFAULT_CONFIG: AgentLoopConfig = {
maxIterations: 20,
timeoutMs: 120_000,
totalTimeoutMs: 600_000,
enableReflection: false,
compressionThreshold: 0.8,
contextWindow: 128_000,
retryCount: 3,
temperature: 0.0,
};
/**
* ReAct Agent Loop 引擎
*/
export class AgentLoopEngine extends EventEmitter {
private currentState: AgentLoopState = AgentLoopState.INIT;
private iterations: IterationStep[] = [];
private currentIteration = 0;
private startTime = 0;
private totalTokens: TokenUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
private aborted = false;
private config: AgentLoopConfig;
private tools: MetonaToolDef[] = [];
private currentSessionId: string = '';
private workspacePath: string = '';
constructor(
config: Partial<AgentLoopConfig> = {},
private adapter: IMetonaProviderAdapter,
private toolRegistry?: import('../tools/registry').ToolRegistry,
private preToolHooks: import('../hooks/pre-tool').PreToolHook[] = [],
private postToolHooks: import('../hooks/post-tool').PostToolHook[] = [],
) {
super();
this.config = { ...DEFAULT_CONFIG, ...config };
}
/**
* 设置工作空间路径(工具执行时传入 context)
*/
setWorkspacePath(path: string): void {
this.workspacePath = path;
}
/**
* 设置可用工具列表
*/
setTools(tools: MetonaToolDef[]): void {
this.tools = tools;
}
/**
* 执行完整的 ReAct 循环(流式模式)
*
* @param userInput 用户输入文本
* @param sessionId 会话 ID
* @param history 历史消息
* @param systemPrompt System Prompt
*/
async runStream(
userMessage: MetonaMessage,
sessionId: string,
history: MetonaMessage[],
systemPrompt: MetonaSystemPrompt,
): Promise<AgentLoopOutput> {
this.startTime = Date.now();
this.aborted = false;
this.iterations = [];
this.currentIteration = 0;
this.currentSessionId = sessionId;
this.totalTokens = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
try {
await this.transitionTo(AgentLoopState.INIT);
// 构建消息列表(保留 images 字段)
const messages: MetonaMessage[] = [
...history,
{
role: 'user',
content: userMessage.content,
images: userMessage.images,
timestamp: Date.now(),
},
];
// === 主循环 ===
while (
this.currentIteration < this.config.maxIterations &&
!this.aborted &&
Date.now() - this.startTime < this.config.totalTimeoutMs
) {
this.currentIteration++;
// 构建请求
const request: MetonaRequest = {
meta: {
sessionId,
iteration: this.currentIteration,
requestId: `r_${nanoid(12)}`,
timestamp: Date.now(),
agentVersion: '1.0.0',
},
systemPrompt,
messages,
tools: this.tools.length > 0 ? this.tools : undefined,
params: {
maxTokens: 8192,
temperature: this.config.temperature,
stream: true,
thinkingEnabled: true,
thinkingEffort: 'high',
},
};
const step = await this.executeOneIterationStream(request, sessionId);
this.iterations.push(step);
// 将 assistant 回复加入消息历史
if (step.thought) {
const assistantMsg: MetonaMessage = {
role: 'assistant',
content: step.thought.content,
reasoningContent: step.thought.reasoningContent,
toolCalls: step.toolCalls,
timestamp: Date.now(),
iteration: this.currentIteration,
};
messages.push(assistantMsg);
}
// 如果没有工具调用,视为最终输出
if (!step.toolCalls || step.toolCalls.length === 0) {
return this.finish(TerminationReason.COMPLETED, step.thought?.content);
}
// 执行工具并将结果加入消息
if (step.toolResults) {
for (const result of step.toolResults) {
messages.push({
role: 'tool',
content: typeof result.result === 'string' ? result.result : JSON.stringify(result.result),
toolResult: result,
timestamp: Date.now(),
iteration: this.currentIteration,
});
}
}
}
// 循环退出判断
if (this.aborted) return this.finish(TerminationReason.USER_INTERRUPT);
if (this.currentIteration >= this.config.maxIterations)
return this.finish(TerminationReason.MAX_ITERATIONS);
return this.finish(TerminationReason.TIMEOUT);
} catch (error) {
this.emit('error', { error: (error as Error).message });
return this.finish(TerminationReason.ERROR, undefined, error as Error);
}
}
/** 中断循环 */
abort(): void {
this.aborted = true;
this.emit('aborted');
}
/** 获取当前状态 */
getState(): AgentLoopState {
return this.currentState;
}
// ========== 私有方法 ==========
/**
* 执行单次迭代(流式模式)
*/
private async executeOneIterationStream(
request: MetonaRequest,
sessionId: string,
): Promise<IterationStep> {
const step: IterationStep = {
iteration: this.currentIteration,
state: AgentLoopState.THINKING,
startedAt: Date.now(),
};
try {
// === THINKING: 流式调用 LLM ===
await this.transitionTo(AgentLoopState.THINKING);
this.emit('stateChange', {
sessionId,
iteration: this.currentIteration,
state: 'THINKING',
});
let fullContent = '';
let reasoningContent = '';
const toolCallsBuffer = new Map<number, { name: string; argsBuffer: string }>();
let tokenUsage: TokenUsage | undefined;
// 流式接收响应
for await (const event of this.adapter.chatStream(request)) {
if (this.aborted) break;
// 转发流式事件到渲染进程
this.emit('streamEvent', event);
switch (event.type) {
case MetonaStreamEventType.TEXT_DELTA:
if (event.delta) fullContent += event.delta;
break;
case MetonaStreamEventType.REASONING_DELTA:
if (event.delta) reasoningContent += event.delta;
break;
case MetonaStreamEventType.TOOL_CALL_DELTA:
if (event.toolCallDelta) {
const { index, name, argsDelta } = event.toolCallDelta;
if (!toolCallsBuffer.has(index)) {
toolCallsBuffer.set(index, { name: name ?? '', argsBuffer: '' });
}
const buf = toolCallsBuffer.get(index)!;
if (name) buf.name = name;
if (argsDelta) buf.argsBuffer += argsDelta;
}
break;
case MetonaStreamEventType.TOOL_CALL_COMPLETE:
if (event.toolCall) {
// 工具调用完成,记录到 step
if (!step.toolCalls) step.toolCalls = [];
step.toolCalls.push(event.toolCall);
}
break;
case MetonaStreamEventType.USAGE:
if (event.usage) {
tokenUsage = {
promptTokens: event.usage.inputTokens ?? 0,
completionTokens: event.usage.outputTokens ?? 0,
totalTokens: event.usage.totalTokens ?? 0,
};
}
break;
case MetonaStreamEventType.DONE:
break;
case MetonaStreamEventType.ERROR:
if (event.error) {
throw new Error(event.error.message);
}
break;
}
}
// 处理缓冲区中的工具调用(TOOL_CALL_DELTA 拼接)
if (toolCallsBuffer.size > 0 && (!step.toolCalls || step.toolCalls.length === 0)) {
step.toolCalls = [];
for (const [index, buf] of toolCallsBuffer) {
try {
step.toolCalls.push({
id: `tc_${nanoid(8)}`,
name: buf.name,
args: buf.argsBuffer ? JSON.parse(buf.argsBuffer) : {},
iteration: this.currentIteration,
timestamp: Date.now(),
});
} catch {
// JSON 解析失败,跳过
}
}
}
// 记录 Thought
if (fullContent || reasoningContent) {
step.thought = {
id: `thought-${this.currentIteration}`,
content: fullContent,
reasoningContent,
timestamp: Date.now(),
iteration: this.currentIteration,
};
}
// 记录 Token 使用
if (tokenUsage) {
step.tokenUsage = tokenUsage;
this.accumulateTokens(tokenUsage);
}
// === EXECUTING: 执行工具调用 ===
if (step.toolCalls && step.toolCalls.length > 0) {
await this.transitionTo(AgentLoopState.EXECUTING);
this.emit('stateChange', {
sessionId,
iteration: this.currentIteration,
state: 'EXECUTING',
});
step.toolResults = [];
for (const tc of step.toolCalls) {
const result = await this.executeToolSafely(tc);
step.toolResults.push(result);
// 转发工具结果到渲染进程
this.emit('streamEvent', {
type: 'tool_result',
requestId: request.meta.requestId,
sessionId,
iteration: this.currentIteration,
seq: 0,
timestamp: Date.now(),
toolResult: result,
});
}
}
// === OBSERVING ===
await this.transitionTo(AgentLoopState.OBSERVING);
step.completedAt = Date.now();
step.state = AgentLoopState.OBSERVING;
return step;
} catch (error) {
step.completedAt = Date.now();
step.state = AgentLoopState.TERMINATED;
throw error;
}
}
/**
* 安全执行工具调用(经过 Hook 管道)
*/
private async executeToolSafely(toolCall: MetonaToolCall): Promise<MetonaToolResult> {
const startTs = Date.now();
if (!this.toolRegistry) {
return {
toolCallId: toolCall.id, toolName: toolCall.name,
result: null, success: false,
error: `Tool '${toolCall.name}' not available: no ToolRegistry configured`,
durationMs: Date.now() - startTs, timestamp: Date.now(),
};
}
// 前置 Hook 管道
for (const hook of this.preToolHooks) {
const result = await hook.beforeExecute(toolCall, this.currentSessionId);
if (result.blocked) {
return {
toolCallId: toolCall.id, toolName: toolCall.name,
result: null, success: false, error: `Blocked: ${result.reason}`,
durationMs: Date.now() - startTs, timestamp: Date.now(),
};
}
}
// 执行工具
const toolResult = await this.toolRegistry.execute(toolCall, {
sessionId: this.currentSessionId ?? '',
workspacePath: this.workspacePath,
iteration: this.currentIteration,
requestId: '',
});
// 后置 Hook 管道
for (const hook of this.postToolHooks) {
await hook.afterExecute(toolCall, toolResult, this.currentSessionId);
}
return toolResult;
}
private async transitionTo(state: AgentLoopState): Promise<void> {
const previous = this.currentState;
this.currentState = state;
this.emit('stateChange', { previous, current: state });
}
private accumulateTokens(usage: TokenUsage): void {
this.totalTokens.promptTokens += usage.promptTokens;
this.totalTokens.completionTokens += usage.completionTokens;
this.totalTokens.totalTokens += usage.totalTokens;
}
private finish(
reason: TerminationReason,
answer?: string,
error?: Error,
): AgentLoopOutput {
this.currentState = AgentLoopState.TERMINATED;
return {
finalAnswer: answer ?? (error ? error.message : 'No answer produced'),
terminationReason: reason,
iterations: this.iterations,
totalTokenUsage: this.totalTokens,
durationMs: Date.now() - this.startTime,
metadata: { config: this.config, error: error?.message },
};
}
}
+10
View File
@@ -0,0 +1,10 @@
export { AgentLoopEngine } from './engine';
export {
AgentLoopState,
TerminationReason,
type AgentLoopConfig,
type AgentLoopOutput,
type IterationStep,
type Thought,
type TokenUsage,
} from './types';
+135
View File
@@ -0,0 +1,135 @@
/**
* Agent Loop — LLM 输出解析器
*
* 使用 Zod Schema 定义 ReAct 输出格式。
* 优先使用 Structured Output / Tool Calling,降级为正则解析。
*
* @see docs/生产级通用 AI Agent 智能体桌面应用:完整设计与构建指南.html — 第四章
*/
import { z } from 'zod';
import type { Thought } from './types';
import type { MetonaToolCall } from '../types';
/** 思考内容 Schema */
export const ThoughtSchema = z.object({
type: z.literal('thought'),
content: z.string().describe('当前的推理思考过程'),
needs_tool: z.boolean().describe('是否需要调用工具'),
});
/** 工具调用 Schema */
export const ToolCallSchema = z.object({
type: z.literal('tool_call'),
tool_name: z.string().describe('要调用的工具名称'),
args: z.record(z.unknown()).describe('工具参数'),
reasoning: z.string().describe('为什么选择这个工具'),
});
/** 最终答案 Schema */
export const FinalAnswerSchema = z.object({
type: z.literal('final_answer'),
answer: z.string().describe('最终答案内容'),
confidence: z.number().min(0).max(1).describe('答案置信度'),
summary: z.string().describe('简要总结'),
});
/** ReAct 步骤联合类型 */
export const ReActStepSchema = z.discriminatedUnion('type', [
ThoughtSchema,
ToolCallSchema,
FinalAnswerSchema,
]);
/**
* LLM 输出解析器
*/
export function parseLLMOutput(rawText: string, iteration: number): {
thought?: Thought;
toolCalls?: MetonaToolCall[];
} {
// 尝试 JSON 解析(Structured Output 模式)
try {
const jsonStart = rawText.indexOf('{');
const jsonEnd = rawText.lastIndexOf('}');
if (jsonStart !== -1 && jsonEnd !== -1) {
const jsonStr = rawText.slice(jsonStart, jsonEnd + 1);
const parsed = JSON.parse(jsonStr);
const validated = ReActStepSchema.safeParse(parsed);
if (validated.success) {
const data = validated.data;
switch (data.type) {
case 'thought':
return {
thought: {
id: `thought-${iteration}`,
content: data.content,
timestamp: Date.now(),
iteration,
},
};
case 'tool_call':
return {
toolCalls: [{
id: `call-${iteration}-${Date.now()}`,
name: data.tool_name,
args: data.args as Record<string, unknown>,
timestamp: Date.now(),
iteration,
}],
};
case 'final_answer':
return {
thought: {
id: `answer-${iteration}`,
content: `[FINAL ANSWER]\n${data.answer}\n\nConfidence: ${data.confidence}\nSummary: ${data.summary}`,
timestamp: Date.now(),
iteration,
},
};
}
}
}
} catch {
// JSON 解析失败,降级到正则提取
}
// 降级:正则提取
return parseWithRegex(rawText, iteration);
}
/** 正则降级解析 */
function parseWithRegex(text: string, iteration: number): {
thought?: Thought;
toolCalls?: MetonaToolCall[];
} {
const result: { thought?: Thought; toolCalls?: MetonaToolCall[] } = {};
const thoughtMatch = text.match(/Thought:\s*([\s\S]*?)(?=Action:|$)/i);
if (thoughtMatch) {
result.thought = {
id: `thought-${iteration}`,
content: thoughtMatch[1].trim(),
timestamp: Date.now(),
iteration,
};
}
const actionMatch = text.match(/Action:\s*(\w+)\s*\n\s*Action Input:\s*({[\s\S]*?})\s*(?=\n\n|Observation:|$)/i);
if (actionMatch) {
try {
result.toolCalls = [{
id: `call-${iteration}-${Date.now()}`,
name: actionMatch[1].trim(),
args: JSON.parse(actionMatch[2]),
timestamp: Date.now(),
iteration,
}];
} catch {
// 参数 JSON 解析失败
}
}
return result;
}
+77
View File
@@ -0,0 +1,77 @@
/**
* Agent Loop — 内部类型定义
*
* 用于 Agent Loop 引擎内部的状态管理和迭代记录。
*/
// ===== Agent Loop 状态机 =====
export enum AgentLoopState {
INIT = 'INIT',
THINKING = 'THINKING',
PARSING = 'PARSING',
EXECUTING = 'EXECUTING',
OBSERVING = 'OBSERVING',
REFLECTING = 'REFLECTING',
COMPRESSING = 'COMPRESSING',
TERMINATED = 'TERMINATED',
}
export enum TerminationReason {
COMPLETED = 'completed',
MAX_ITERATIONS = 'max_iterations',
TIMEOUT = 'timeout',
USER_INTERRUPT = 'user_interrupt',
ERROR = 'error',
}
// ===== 迭代步骤 =====
export interface Thought {
id: string;
content: string;
reasoningContent?: string;
timestamp: number;
iteration: number;
}
export interface IterationStep {
iteration: number;
state: AgentLoopState;
startedAt: number;
completedAt?: number;
thought?: Thought;
toolCalls?: import('@shared/index').MetonaToolCall[];
toolResults?: import('@shared/index').MetonaToolResult[];
tokenUsage?: TokenUsage;
}
export interface TokenUsage {
promptTokens: number;
completionTokens: number;
totalTokens: number;
}
// ===== Agent Loop 配置 =====
export interface AgentLoopConfig {
maxIterations: number;
timeoutMs: number;
totalTimeoutMs: number;
enableReflection: boolean;
compressionThreshold: number;
contextWindow: number;
retryCount: number;
temperature: number;
}
// ===== Agent Loop 输出 =====
export interface AgentLoopOutput {
finalAnswer: string;
terminationReason: TerminationReason;
iterations: IterationStep[];
totalTokenUsage: TokenUsage;
durationMs: number;
metadata: Record<string, unknown>;
}