mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-04-25 04:52:43 +08:00
feat: 聊天对话支持使用技能
This commit is contained in:
@@ -26,6 +26,8 @@ import { isEmbeddingEnabled } from '../rag'
|
||||
import { t as i18nT } from '../../i18n'
|
||||
import { preprocessMessages, type PreprocessableMessage } from '../preprocessor'
|
||||
import { formatMessageCompact } from './utils/format'
|
||||
import { getSkillConfig } from '../skills'
|
||||
import type { SkillDef } from '../skills/types'
|
||||
|
||||
// 导出类型
|
||||
export * from './types'
|
||||
@@ -226,3 +228,65 @@ export function getAllTools(context: ToolContext, allowedTools?: string[]): Agen
|
||||
|
||||
return tools.map(translateTool).map((t) => wrapWithPreprocessing(t, context))
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 activate_skill 元工具(AI 自选模式专用)
|
||||
* LLM 判断用户问题适合某个技能时调用此工具,获取技能的完整执行指导
|
||||
*/
|
||||
export function createActivateSkillTool(
|
||||
chatType: 'group' | 'private',
|
||||
allowedTools?: string[],
|
||||
locale: string = 'zh-CN'
|
||||
): AgentTool<any> {
|
||||
const isZh = locale.startsWith('zh')
|
||||
|
||||
return {
|
||||
name: 'activate_skill',
|
||||
description: isZh
|
||||
? '激活一个分析技能,获取该技能的详细执行指导'
|
||||
: 'Activate an analysis skill and get its detailed execution instructions',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
skill_id: {
|
||||
type: 'string',
|
||||
description: isZh ? '技能 ID' : 'Skill ID',
|
||||
},
|
||||
},
|
||||
required: ['skill_id'],
|
||||
},
|
||||
execute: async (_toolCallId: string, params: { skill_id: string }) => {
|
||||
const skill: SkillDef | null = getSkillConfig(params.skill_id)
|
||||
if (!skill) {
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: isZh ? '技能不存在' : 'Skill not found' }],
|
||||
}
|
||||
}
|
||||
|
||||
if (skill.chatScope !== 'all' && skill.chatScope !== chatType) {
|
||||
const scopeMsg = isZh
|
||||
? `该技能仅适用于${skill.chatScope === 'group' ? '群聊' : '私聊'}场景`
|
||||
: `This skill is only applicable to ${skill.chatScope === 'group' ? 'group chat' : 'private chat'} scenarios`
|
||||
return { content: [{ type: 'text' as const, text: scopeMsg }] }
|
||||
}
|
||||
|
||||
if (skill.tools.length > 0 && allowedTools && allowedTools.length > 0) {
|
||||
const missing = skill.tools.filter((t) => !allowedTools.includes(t))
|
||||
if (missing.length > 0) {
|
||||
const msg = isZh
|
||||
? `当前助手缺少该技能所需的工具:${missing.join(', ')}`
|
||||
: `Current assistant lacks tools required by this skill: ${missing.join(', ')}`
|
||||
return { content: [{ type: 'text' as const, text: msg }] }
|
||||
}
|
||||
}
|
||||
|
||||
const actionPrompt = isZh
|
||||
? '\n\n[System]: 你已成功加载该技能手册。现在,请立即、自动地开始执行步骤1,调用相关的基础数据工具,不要等待用户的进一步确认!'
|
||||
: '\n\n[System]: You have successfully loaded this skill manual. Now, immediately start executing step 1 by calling the relevant data tools. Do not wait for further user confirmation!'
|
||||
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: `${skill.prompt}${actionPrompt}` }],
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user