feat: 知识图谱工具支持 + 智能体事件处理

- dialogService: 添加智能体 SSE 事件处理
- toolExecutor: 添加 knowledge_save/knowledge_load 工具
- messageArea: 添加智能体消息渲染支持
- 添加 CLAUDE.md 项目配置
This commit is contained in:
XiaoFeng
2025-12-30 09:40:04 +08:00
parent d4c726ea9c
commit 44bbcde5cf
14 changed files with 423 additions and 16 deletions

View File

@ -13,7 +13,7 @@ import type { DialogRequest, ToolCallRequest, AskUserEvent } from '../types/api'
* 消息段落类型
*/
export interface MessageSegment {
type: 'text' | 'tool' | 'question';
type: 'text' | 'tool' | 'question' | 'agent';
content?: string;
toolName?: string;
toolStatus?: 'running' | 'success' | 'error';
@ -21,6 +21,22 @@ export interface MessageSegment {
askId?: string;
question?: string;
options?: string[];
// 智能体相关字段
agentId?: string;
agentName?: string;
agentStatus?: 'running' | 'completed' | 'error';
agentSteps?: AgentStep[];
}
/**
* 智能体执行步骤
*/
export interface AgentStep {
step: number;
toolName: string;
toolInput?: unknown;
toolResult?: string;
status: 'running' | 'completed' | 'error';
}
/**
@ -164,28 +180,42 @@ export class DialogSession {
onToolCall: async (data: ToolCallRequest) => {
const toolName = data.params.name;
console.log('[DialogSession] onToolCall:', toolName);
// 检查是否已经有相同的工具段落(可能由 onToolStart 添加)
const lastToolSegment = this.segments.filter(s => s.type === 'tool').pop();
if (lastToolSegment && lastToolSegment.toolName === toolName && lastToolSegment.toolStatus === 'running') {
console.log('[DialogSession] onToolCall: 跳过重复的工具段落:', toolName);
// 检查是否有活跃的智能体(如果有,工具执行会显示在智能体卡片内,不需要单独显示)
const hasActiveAgent = this.segments.some(
s => s.type === 'agent' && s.agentStatus === 'running'
);
if (hasActiveAgent) {
console.log('[DialogSession] onToolCall: 智能体执行中,跳过工具段落:', toolName);
} else {
this.addToolSegment(toolName, 'running');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
// 检查是否已经有相同的工具段落(可能由 onToolStart 添加)
const lastToolSegment = this.segments.filter(s => s.type === 'tool').pop();
if (lastToolSegment && lastToolSegment.toolName === toolName && lastToolSegment.toolStatus === 'running') {
console.log('[DialogSession] onToolCall: 跳过重复的工具段落:', toolName);
} else {
this.addToolSegment(toolName, 'running');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
}
// 注意:不在这里调用 callbacks.onToolStart避免与 onToolStart 事件重复
try {
await executeToolCall(data, this.toolContext);
this.updateToolSegment(toolName, 'success', '执行完成');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
if (!hasActiveAgent) {
this.updateToolSegment(toolName, 'success', '执行完成');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
// 也不调用 callbacks.onToolComplete避免重复
} catch (error) {
const errorMsg = error instanceof Error ? error.message : '未知错误';
this.updateToolSegment(toolName, 'error', errorMsg);
if (!hasActiveAgent) {
this.updateToolSegment(toolName, 'error', errorMsg);
callbacks.onSegmentUpdate?.(this.segments);
}
callbacks.onToolError?.(toolName, errorMsg);
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
},
@ -257,6 +287,69 @@ export class DialogSession {
callbacks.onNotification?.(data.message);
},
// 智能体事件处理
onAgentStart: (data) => {
console.log('[DialogSession] onAgentStart:', data.agentId);
this.finalizeTextSegment();
this.segments.push({
type: 'agent',
agentId: data.agentId,
agentName: data.agentName,
content: data.instruction,
agentStatus: 'running',
agentSteps: []
});
callbacks.onSegmentUpdate?.(this.segments);
},
onAgentProgress: (data) => {
console.log('[DialogSession] onAgentProgress:', data.agentId, data.step, data.status);
const agentSegment = this.segments.find(
s => s.type === 'agent' && s.agentId === data.agentId
);
if (agentSegment && agentSegment.agentSteps) {
if (data.status === 'running') {
agentSegment.agentSteps.push({
step: data.step,
toolName: data.toolName,
toolInput: data.toolInput,
status: 'running'
});
} else {
const step = agentSegment.agentSteps.find(s => s.step === data.step);
if (step) {
step.status = data.status;
step.toolResult = data.toolResult;
}
}
callbacks.onSegmentUpdate?.(this.segments);
}
},
onAgentComplete: (data) => {
console.log('[DialogSession] onAgentComplete:', data.agentId);
const agentSegment = this.segments.find(
s => s.type === 'agent' && s.agentId === data.agentId
);
if (agentSegment) {
agentSegment.agentStatus = 'completed';
agentSegment.content = data.summary;
callbacks.onSegmentUpdate?.(this.segments);
}
},
onAgentError: (data) => {
console.log('[DialogSession] onAgentError:', data.agentId, data.error);
const agentSegment = this.segments.find(
s => s.type === 'agent' && s.agentId === data.agentId
);
if (agentSegment) {
agentSegment.agentStatus = 'error';
agentSegment.content = data.error;
callbacks.onSegmentUpdate?.(this.segments);
}
},
onOpen: () => {
console.log('[DialogSession] SSE 连接已建立');
},