import { getWebMcpTool } from './webMcpService'; export interface WebMcpChatTemplate { toolName?: string; label: string; pattern: RegExp; examples: string[]; requiresConfirmation: boolean; parse: (match: RegExpMatchArray) => Record; } const template = ( toolName: string | undefined, label: string, pattern: RegExp, examples: string[], parse: (match: RegExpMatchArray) => Record ): WebMcpChatTemplate => ({ toolName, label, pattern, examples, parse, requiresConfirmation: toolName ? Boolean(getWebMcpTool(toolName)?.requiresConfirmation) : false, }); export const webMcpChatTemplates: ReadonlyArray = [ template('list_documents', '列出我的文档', /^列出(?:我的|当前用户的)?文档$/, ['列出我的文档'], () => ({})), template('open_document', '打开文档', /^(?:打开|查看)\s+(.+?)\s+文档$/, ['查看 doc-abc123 文档'], (match) => ({ documentId: match[1] })), template('get_document', '读取文档', /^读取文档\s+(\S+)$/, ['读取文档 doc-abc123'], (match) => ({ documentId: match[1] })), template('search_document', '搜索文档', /^搜索文档\s+(\S+)\s+(.+)$/, ['搜索文档 doc-abc123 WebMCP'], (match) => ({ documentId: match[1], query: match[2] })), template('get_block', '读取文档块', /^读取文档块\s+(\S+)\s+(\S+)$/, ['读取文档块 doc-abc123 block-p-50'], (match) => ({ documentId: match[1], blockId: match[2] })), template('get_document_toc', '查看文档目录', /^查看文档目录\s+(\S+)$/, ['查看文档目录 doc-abc123'], (match) => ({ documentId: match[1] })), template('get_document_stats', '查看文档统计', /^查看文档统计\s+(\S+)$/, ['查看文档统计 doc-abc123'], (match) => ({ documentId: match[1] })), template('list_export_records', '查看我的导出记录', /^查看我的导出记录$/, ['查看我的导出记录'], () => ({})), template('insert_block', '向文档插入段落', /^向文档\s+(\S+)\s+插入段落:(.+)$/, ['向文档 doc-abc123 插入段落:新增内容'], (match) => ({ documentId: match[1], type: 'paragraph', content: match[2] })), template('update_block', '更新文档块', /^更新文档块\s+(\S+)\s+(\S+)\s+为:(.+)$/, ['更新文档块 doc-abc123 block-p-50 为:新内容'], (match) => ({ documentId: match[1], blockId: match[2], content: match[3] })), template('delete_block', '删除文档块', /^删除文档块\s+(\S+)\s+(\S+)$/, ['删除文档块 doc-abc123 block-p-50'], (match) => ({ documentId: match[1], blockId: match[2] })), template('export_document', '导出文档', /^导出文档\s+(\S+)\s+为\s*Word$/i, ['导出文档 doc-abc123 为 Word'], (match) => ({ documentId: match[1] })), template('download_export_record', '下载导出记录', /^下载导出记录\s+(\S+)$/, ['下载导出记录 export-abc123'], (match) => ({ recordId: match[1] })), ]; export const webMcpTemplateHelp = (): string => webMcpChatTemplates .map((item) => `${item.label}:${item.examples.join(';')}${item.requiresConfirmation ? '(需要确认)' : ''}`) .join('\n'); export const matchWebMcpChatTemplate = (content: string): { template: WebMcpChatTemplate; input: Record } | null => { const normalized = content.trim().replace(/^调用\s*WebMCP\s*/i, ''); for (const item of webMcpChatTemplates) { const match = normalized.match(item.pattern); if (match) return { template: item, input: item.parse(match) }; } return null; }; export const getWebMcpChatTemplates = (): ReadonlyArray => webMcpChatTemplates;