Files
ChatLab/electron/main/ai/agent/content-parser.ts
T
digua e6849af698 refactor(agent): split monolithic Agent into modular architecture
- Delete monolithic agent.ts (−1464 lines)
- Add agent/index.ts: Agent orchestrator class with runAgent/runAgentStream
- Add agent/types.ts: AgentConfig, AgentStreamChunk, AgentResult, PromptConfig
- Add agent/prompt-builder.ts: system prompt construction with i18n
- Add agent/content-parser.ts: thinking tag and tool call tag parsing
- Add agent/event-handler.ts: runtime state, event mapping, token estimation
2026-02-26 21:05:50 +08:00

39 lines
1.1 KiB
TypeScript

/**
* Agent 内容解析工具
* 处理思考标签提取和工具调用标签清理
*/
const THINK_TAGS = ['think', 'analysis', 'reasoning', 'reflection', 'thought', 'thinking']
/**
* 从文本内容中提取思考类标签内容
*/
export function extractThinkingContent(content: string): { thinking: string; cleanContent: string } {
if (!content) {
return { thinking: '', cleanContent: '' }
}
const tagPattern = THINK_TAGS.join('|')
const thinkRegex = new RegExp(`<(${tagPattern})>([\\s\\S]*?)<\\/\\1>`, 'gi')
const thinkingParts: string[] = []
let cleanContent = content
const matches = content.matchAll(thinkRegex)
for (const match of matches) {
const thinkText = match[2].trim()
if (thinkText) {
thinkingParts.push(thinkText)
}
cleanContent = cleanContent.replace(match[0], '')
}
return { thinking: thinkingParts.join('\n').trim(), cleanContent: cleanContent.trim() }
}
/**
* 清理 <tool_call> 标签内容,避免将工具调用文本展示给用户
*/
export function stripToolCallTags(content: string): string {
return content.replace(/<tool_call>[\s\S]*?<\/tool_call>/gi, '').trim()
}