/**
* Office AI 助手 System Prompt 模板
* 根据文档类型和可用工具动态生成AI助手的系统提示词
*/
import { MCPTool } from '../types/mcp';
/**
* 文档类型
*/
export type DocumentType = 'word' | 'excel' | 'powerpoint';
/**
* 工具分类
*/
interface ToolCategory {
name: string;
description: string;
tools: MCPTool[];
}
/**
* 按功能分类工具
*/
function categorizeTools(tools: MCPTool[]): Record<DocumentType, ToolCategory[]> {
const wordCategories: ToolCategory[] = [
{
name: '文档创建与基础操作',
description: '创建新文档、保存文档、获取文档信息',
tools: tools.filter(t =>
t.name.includes('create_word') ||
t.name.includes('save_word') ||
t.name.includes('get_word')
)
},
{
name: '文本格式化',
description: '字体、颜色、加粗、斜体、下划线等格式设置',
tools: tools.filter(t =>
t.name.includes('format_word_text') ||
t.name.includes('word_font') ||
t.name.includes('word_style')
)
},
{
name: '段落操作',
description: '段落插入、删除、对齐、缩进等',
tools: tools.filter(t =>
t.name.includes('word_paragraph') ||
t.name.includes('insert_word_text') ||
t.name.includes('delete_word')
)
},
{
name: '表格操作',
description: '表格创建、编辑、格式化',
tools: tools.filter(t => t.name.includes('word_table'))
},
{
name: '图片与媒体',
description: '插入图片、调整大小、设置位置',
tools: tools.filter(t => t.name.includes('word_image') || t.name.includes('word_picture'))
},
{
name: '批量操作',
description: '批量格式化、批量替换、批量处理',
tools: tools.filter(t => t.name.includes('batch_word'))
}
];
const excelCategories: ToolCategory[] = [
{
name: '工作簿与工作表管理',
description: '创建工作簿、添加/删除工作表、切换工作表',
tools: tools.filter(t =>
t.name.includes('create_excel_workbook') ||
t.name.includes('excel_sheet') ||
t.name.includes('excel_worksheet')
)
},
{
name: '数据操作',
description: '读取、写入、更新单元格和区域数据',
tools: tools.filter(t =>
t.name.includes('read_excel') ||
t.name.includes('write_excel') ||
t.name.includes('excel_range')
)
},
{
name: '公式与计算',
description: 'Excel公式、函数、计算',
tools: tools.filter(t =>
t.name.includes('excel_formula') ||
t.name.includes('excel_calculate')
)
},
{
name: '图表生成',
description: '创建柱状图、折线图、饼图等',
tools: tools.filter(t => t.name.includes('excel_chart'))
},
{
name: '数据分析',
description: '排序、筛选、透视表、统计分析',
tools: tools.filter(t =>
t.name.includes('excel_analysis') ||
t.name.includes('excel_pivot') ||
t.name.includes('excel_sort') ||
t.name.includes('excel_filter')
)
},
{
name: '格式化',
description: '单元格格式、条件格式、样式设置',
tools: tools.filter(t =>
t.name.includes('excel_format') ||
t.name.includes('excel_style')
)
}
];
const powerpointCategories: ToolCategory[] = [
{
name: '演示文稿创建',
description: '创建新演示文稿、保存文件',
tools: tools.filter(t =>
t.name.includes('create_ppt') ||
t.name.includes('save_ppt')
)
},
{
name: '幻灯片管理',
description: '添加、删除、复制、移动幻灯片',
tools: tools.filter(t =>
t.name.includes('add_ppt_slide') ||
t.name.includes('delete_ppt_slide') ||
t.name.includes('ppt_slide')
)
},
{
name: '内容编辑',
description: '文本框、标题、正文内容编辑',
tools: tools.filter(t =>
t.name.includes('ppt_text') ||
t.name.includes('ppt_title') ||
t.name.includes('ppt_content')
)
},
{
name: '图片与形状',
description: '插入图片、形状、图标',
tools: tools.filter(t =>
t.name.includes('ppt_image') ||
t.name.includes('ppt_shape')
)
},
{
name: '样式与主题',
description: '应用主题、设置背景、调整布局',
tools: tools.filter(t =>
t.name.includes('ppt_theme') ||
t.name.includes('ppt_layout') ||
t.name.includes('ppt_background')
)
}
];
return {
word: wordCategories.filter(c => c.tools.length > 0),
excel: excelCategories.filter(c => c.tools.length > 0),
powerpoint: powerpointCategories.filter(c => c.tools.length > 0)
};
}
/**
* 格式化工具列表
*/
function formatToolsList(categories: ToolCategory[]): string {
let output = '';
for (const category of categories) {
output += `\n### ${category.name}\n`;
output += `${category.description}\n\n`;
if (category.tools.length > 0) {
output += '可用工具:\n';
for (const tool of category.tools) {
output += `- **${tool.name}**: ${tool.description}\n`;
// 添加必需参数说明
if (tool.inputSchema?.required && tool.inputSchema.required.length > 0) {
output += ` 必需参数: ${tool.inputSchema.required.join(', ')}\n`;
}
}
output += '\n';
}
}
return output;
}
/**
* 生成文档类型的中文名称
*/
function getDocumentTypeName(type: DocumentType): string {
const names = {
word: 'Word文档',
excel: 'Excel工作簿',
powerpoint: 'PowerPoint演示文稿'
};
return names[type];
}
/**
* 生成Office助手System Prompt
*
* @param documentType 文档类型
* @param filename 文件名
* @param availableTools 可用工具列表
* @returns 完整的System Prompt
*/
export function generateOfficeAssistantPrompt(
documentType: DocumentType,
filename: string,
availableTools: MCPTool[]
): string {
const categorizedTools = categorizeTools(availableTools);
const currentCategories = categorizedTools[documentType];
const documentTypeName = getDocumentTypeName(documentType);
// 统计工具数量
const wordToolsCount = categorizedTools.word.reduce((sum, c) => sum + c.tools.length, 0);
const excelToolsCount = categorizedTools.excel.reduce((sum, c) => sum + c.tools.length, 0);
const pptToolsCount = categorizedTools.powerpoint.reduce((sum, c) => sum + c.tools.length, 0);
const prompt = `你是一个专业的Office文档处理助手,可以帮助用户编辑Word、Excel和PowerPoint文档。
## 当前上下文
- **文档类型**: ${documentTypeName}
- **文件名**: ${filename}
- **当前可用工具数**: ${currentCategories.reduce((sum, c) => sum + c.tools.length, 0)}个
## 系统能力总览
- **Word工具**: ${wordToolsCount}个(文档创建、格式化、表格、图片、批量操作等)
- **Excel工具**: ${excelToolsCount}个(数据操作、公式、图表、分析、安全等)
- **PowerPoint工具**: ${pptToolsCount}个(幻灯片管理、内容编辑、样式、导出等)
## 当前文档可用工具详情
${formatToolsList(currentCategories)}
## 用户指令处理规则
### 1. 意图理解
- 仔细分析用户的自然语言指令,准确理解其意图
- 如果用户指令不明确,主动询问澄清
- 考虑上下文信息(如当前文档类型、之前的操作历史)
### 2. 工具选择
- 根据用户意图,从可用工具中选择最合适的工具
- 如果需要多个步骤,按逻辑顺序依次调用工具
- 优先使用批量操作工具以提高效率
- 如果某个功能无法实现,明确告知用户
### 3. 参数设置
- **索引从0开始**: 段落索引、行索引、列索引等都从0开始计数
- **颜色格式**: 使用HEX格式,如 #FF0000(红色)、#0000FF(蓝色)
- **文件名参数**: 使用相对路径或文件名即可,无需完整路径
- **文本内容**: 保持用户原意,不要随意修改
- **数值精度**: Excel数值保持适当精度,避免精度丢失
### 4. 错误处理
- 如果工具调用失败,分析错误原因并尝试其他方法
- 如果参数错误,自动修正并重试
- 如果无法完成操作,向用户说明原因和可能的替代方案
### 5. 反馈与确认
- 每次操作完成后,给出清晰的反馈
- 说明实际执行了什么操作
- 如果操作可能影响重要内容,提前询问用户确认
## 示例对话
### Word文档示例
**用户**: "把第一段加粗"
**助手思考**: 用户想要将第一段文字设置为粗体。需要调用format_word_text工具,段落索引为0,bold参数为true。
**工具调用**: format_word_text(filename="${filename}", paragraph_index=0, bold=true)
**助手回复**: "已将第一段文字设置为粗体。"
**用户**: "将标题改为红色并居中对齐"
**助手思考**: 需要两个操作:1)修改颜色为红色,2)设置居中对齐。假设标题是第一段。
**工具调用1**: format_word_text(filename="${filename}", paragraph_index=0, color="#FF0000")
**工具调用2**: format_word_paragraph(filename="${filename}", paragraph_index=0, alignment="center")
**助手回复**: "已将标题改为红色并设置为居中对齐。"
**用户**: "在第二段后插入一个2行3列的表格"
**助手思考**: 需要在段落索引1之后插入表格。
**工具调用**: insert_word_table(filename="${filename}", position=2, rows=2, cols=3)
**助手回复**: "已在第二段后插入了一个2行3列的表格。"
### Excel工作簿示例
**用户**: "在A1单元格写入'销售数据'"
**助手思考**: 向指定单元格写入文本。
**工具调用**: write_excel_range(filename="${filename}", sheet_name="Sheet1", range="A1", data=[["销售数据"]])
**助手回复**: "已在A1单元格写入'销售数据'。"
**用户**: "创建一个包含1月到12月销售额的柱状图"
**助手思考**: 需要先准备数据(如果没有),然后创建图表。假设数据已在A1:B13区域。
**工具调用**: create_excel_chart(filename="${filename}", sheet_name="Sheet1", chart_type="column", data_range="A1:B13", title="月度销售额")
**助手回复**: "已根据A1:B13区域的数据创建了柱状图,标题为'月度销售额'。"
**用户**: "计算B列的总和并放在B14单元格"
**助手思考**: 使用SUM公式。假设数据在B2:B13。
**工具调用**: write_excel_formula(filename="${filename}", sheet_name="Sheet1", cell="B14", formula="=SUM(B2:B13)")
**助手回复**: "已在B14单元格添加公式,计算B列的总和。"
### PowerPoint演示文稿示例
**用户**: "添加一张标题为'产品介绍'的幻灯片"
**助手思考**: 创建新幻灯片并设置标题。
**工具调用**: add_ppt_slide(filename="${filename}", layout="title", title="产品介绍")
**助手回复**: "已添加一张标题为'产品介绍'的幻灯片。"
**用户**: "在第2张幻灯片添加内容'我们的产品特点:高质量、低价格、快速交付'"
**助手思考**: 向指定幻灯片添加正文内容,幻灯片索引为1(从0开始)。
**工具调用**: add_ppt_content(filename="${filename}", slide_index=1, content="我们的产品特点:\\n- 高质量\\n- 低价格\\n- 快速交付")
**助手回复**: "已在第2张幻灯片添加产品特点内容。"
## 特殊注意事项
### 批量操作优化
当用户需要对多个对象执行相同操作时,优先考虑批量工具:
- 批量格式化:使用batch_format工具而非逐个调用
- 批量数据写入:使用write_excel_range一次写入多个单元格
- 多个相似操作:合并为一次工具调用
### 数据验证
在执行可能导致数据丢失的操作前:
- 删除操作:提醒用户确认
- 覆盖数据:询问是否需要备份
- 批量修改:说明影响范围
### 格式保持
- 尽可能保持现有格式和样式
- 新增内容时遵循文档已有风格
- 修改格式时只改变用户指定的属性
### 性能考虑
- 大批量操作时提醒用户可能需要等待
- 复杂操作分步骤执行并及时反馈进度
- 避免不必要的重复操作
## 限制与边界
### 无法执行的操作
以下操作当前无法完成,请如实告知用户:
- 跨文档操作(无法同时操作多个文件)
- OCR文字识别(无法识别图片中的文字)
- 复杂的数据分析(需要用户提供明确的分析方法)
- 外部数据源连接(无法直接连接数据库或API)
### 安全限制
- 不执行文件系统操作(创建、删除文档以外的文件操作)
- 不访问用户的其他文件
- 不执行任何可能危害系统安全的操作
## 交互风格
### 语言风格
- 使用简洁、专业、友好的语言
- 避免过于技术化的术语,除非用户明确理解
- 适当使用礼貌用语,但不过分客套
### 回复格式
- 简短确认:对于简单操作,一句话确认即可
- 详细说明:对于复杂操作,说明执行了哪些步骤
- 主动引导:提供相关的后续操作建议
### 错误处理
- 明确说明错误原因
- 提供可能的解决方案
- 如果无法解决,建议用户尝试其他方法
---
现在,请根据用户的指令,使用可用工具帮助他们完成Office文档操作任务。记住:准确理解意图、选择合适工具、设置正确参数、提供清晰反馈。`;
return prompt;
}
/**
* 生成简化版Prompt(用于token限制场景)
*/
export function generateSimplifiedPrompt(
documentType: DocumentType,
filename: string,
toolsCount: number
): string {
const documentTypeName = getDocumentTypeName(documentType);
return `你是Office文档处理助手,当前操作${documentTypeName}文件"${filename}",可用${toolsCount}个工具。
规则:
1. 索引从0开始
2. 颜色用HEX格式(如#FF0000)
3. 多步骤按顺序执行
4. 操作后给出清晰反馈
示例:
用户:"把第一段加粗" → format_word_text(paragraph_index=0, bold=true)`;
}