@zhin.js/core 1.0.34 → 1.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/lib/ai/bootstrap.d.ts +10 -10
- package/lib/ai/bootstrap.js +17 -17
- package/lib/ai/bootstrap.js.map +1 -1
- package/lib/ai/index.d.ts +8 -8
- package/lib/ai/index.d.ts.map +1 -1
- package/lib/ai/index.js +6 -6
- package/lib/ai/index.js.map +1 -1
- package/lib/ai/init.js +2 -2
- package/lib/ai/init.js.map +1 -1
- package/lib/ai/subagent.d.ts +1 -1
- package/lib/ai/subagent.js +1 -1
- package/lib/ai/{zhin-agent-builtin-tools.d.ts → zhin-agent/builtin-tools.d.ts} +7 -7
- package/lib/ai/zhin-agent/builtin-tools.d.ts.map +1 -0
- package/lib/ai/{zhin-agent-builtin-tools.js → zhin-agent/builtin-tools.js} +1 -1
- package/lib/ai/zhin-agent/builtin-tools.js.map +1 -0
- package/lib/ai/{zhin-agent-config.d.ts → zhin-agent/config.d.ts} +2 -2
- package/lib/ai/zhin-agent/config.d.ts.map +1 -0
- package/lib/ai/{zhin-agent-config.js → zhin-agent/config.js} +2 -2
- package/lib/ai/zhin-agent/config.js.map +1 -0
- package/lib/ai/{zhin-agent-exec-policy.d.ts → zhin-agent/exec-policy.d.ts} +3 -3
- package/lib/ai/zhin-agent/exec-policy.d.ts.map +1 -0
- package/lib/ai/{zhin-agent-exec-policy.js → zhin-agent/exec-policy.js} +2 -2
- package/lib/ai/zhin-agent/exec-policy.js.map +1 -0
- package/lib/ai/{zhin-agent.d.ts → zhin-agent/index.d.ts} +12 -12
- package/lib/ai/zhin-agent/index.d.ts.map +1 -0
- package/lib/ai/{zhin-agent.js → zhin-agent/index.js} +24 -18
- package/lib/ai/zhin-agent/index.js.map +1 -0
- package/lib/ai/{zhin-agent-prompt.d.ts → zhin-agent/prompt.d.ts} +7 -7
- package/lib/ai/zhin-agent/prompt.d.ts.map +1 -0
- package/lib/ai/zhin-agent/prompt.js +111 -0
- package/lib/ai/zhin-agent/prompt.js.map +1 -0
- package/lib/ai/{zhin-agent-tool-collector.d.ts → zhin-agent/tool-collector.d.ts} +5 -5
- package/lib/ai/zhin-agent/tool-collector.d.ts.map +1 -0
- package/lib/ai/{zhin-agent-tool-collector.js → zhin-agent/tool-collector.js} +3 -3
- package/lib/ai/zhin-agent/tool-collector.js.map +1 -0
- package/package.json +4 -4
- package/src/ai/bootstrap.ts +17 -17
- package/src/ai/index.ts +8 -8
- package/src/ai/init.ts +4 -4
- package/src/ai/subagent.ts +2 -2
- package/src/ai/{zhin-agent-builtin-tools.ts → zhin-agent/builtin-tools.ts} +6 -6
- package/src/ai/{zhin-agent-config.ts → zhin-agent/config.ts} +2 -2
- package/src/ai/{zhin-agent-exec-policy.ts → zhin-agent/exec-policy.ts} +3 -3
- package/src/ai/{zhin-agent.ts → zhin-agent/index.ts} +30 -24
- package/src/ai/zhin-agent/prompt.ts +131 -0
- package/src/ai/{zhin-agent-tool-collector.ts → zhin-agent/tool-collector.ts} +6 -6
- package/tests/ai/zhin-agent.test.ts +1 -1
- package/lib/ai/zhin-agent-builtin-tools.d.ts.map +0 -1
- package/lib/ai/zhin-agent-builtin-tools.js.map +0 -1
- package/lib/ai/zhin-agent-config.d.ts.map +0 -1
- package/lib/ai/zhin-agent-config.js.map +0 -1
- package/lib/ai/zhin-agent-exec-policy.d.ts.map +0 -1
- package/lib/ai/zhin-agent-exec-policy.js.map +0 -1
- package/lib/ai/zhin-agent-prompt.d.ts.map +0 -1
- package/lib/ai/zhin-agent-prompt.js +0 -116
- package/lib/ai/zhin-agent-prompt.js.map +0 -1
- package/lib/ai/zhin-agent-tool-collector.d.ts.map +0 -1
- package/lib/ai/zhin-agent-tool-collector.js.map +0 -1
- package/lib/ai/zhin-agent.d.ts.map +0 -1
- package/lib/ai/zhin-agent.js.map +0 -1
- package/src/ai/zhin-agent-prompt.ts +0 -136
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ZhinAgent System Prompt 构建 + 消息辅助函数
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import type { ContentPart } from './types.js';
|
|
7
|
-
import type { SkillFeature } from '../built/skill.js';
|
|
8
|
-
import type { ZhinAgentConfig } from './zhin-agent-config.js';
|
|
9
|
-
import type { ToolContext } from '../types.js';
|
|
10
|
-
import { SECTION_SEP, HISTORY_CONTEXT_MARKER, CURRENT_MESSAGE_MARKER } from './zhin-agent-config.js';
|
|
11
|
-
import type { ChatMessage } from './types.js';
|
|
12
|
-
import { getFileMemoryContext } from './bootstrap.js';
|
|
13
|
-
|
|
14
|
-
export function contentToText(c: string | ContentPart[]): string {
|
|
15
|
-
if (typeof c === 'string') return c;
|
|
16
|
-
return (c as ContentPart[]).map(p => (p.type === 'text' ? p.text : '')).join('');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function buildUserMessageWithHistory(history: ChatMessage[], currentContent: string): string {
|
|
20
|
-
if (history.length === 0) return currentContent;
|
|
21
|
-
const roleLabel = (role: string) => (role === 'user' ? '用户' : role === 'assistant' ? '助手' : '系统');
|
|
22
|
-
const lines = history
|
|
23
|
-
.filter(m => m.role === 'user' || m.role === 'assistant' || m.role === 'system')
|
|
24
|
-
.map(m => `${roleLabel(m.role)}: ${contentToText(m.content)}`);
|
|
25
|
-
const historyBlock = lines.join('\n');
|
|
26
|
-
return `${HISTORY_CONTEXT_MARKER}\n${historyBlock}\n\n${CURRENT_MESSAGE_MARKER}\n${currentContent}`;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function buildEnhancedPersona(
|
|
30
|
-
config: Required<ZhinAgentConfig>,
|
|
31
|
-
profileSummary: string,
|
|
32
|
-
toneHint: string,
|
|
33
|
-
): string {
|
|
34
|
-
let persona = config.persona;
|
|
35
|
-
if (profileSummary) {
|
|
36
|
-
persona += `\n\n${profileSummary}`;
|
|
37
|
-
}
|
|
38
|
-
if (toneHint) {
|
|
39
|
-
persona += `\n\n[语气提示] ${toneHint}`;
|
|
40
|
-
}
|
|
41
|
-
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
42
|
-
const timeStr = new Date().toLocaleString('zh-CN', { timeZone: tz });
|
|
43
|
-
persona += `\n\n当前时间: ${timeStr} (${tz})`;
|
|
44
|
-
return persona;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function buildContextHint(context: ToolContext, _content: string): string {
|
|
48
|
-
const parts: string[] = [];
|
|
49
|
-
if (context.platform) parts.push(`平台:${context.platform}`);
|
|
50
|
-
if (context.botId) parts.push(`Bot:${context.botId}`);
|
|
51
|
-
if (context.senderId) parts.push(`用户:${context.senderId}`);
|
|
52
|
-
if (context.scope) parts.push(`场景类型:${context.scope}`);
|
|
53
|
-
if (context.sceneId) parts.push(`场景ID:${context.sceneId}`);
|
|
54
|
-
if (parts.length === 0) return '';
|
|
55
|
-
return `\n上下文: ${parts.join(' | ')}`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface RichSystemPromptContext {
|
|
59
|
-
config: Required<ZhinAgentConfig>;
|
|
60
|
-
skillRegistry: SkillFeature | null;
|
|
61
|
-
skillsSummaryXML: string;
|
|
62
|
-
activeSkillsContext: string;
|
|
63
|
-
bootstrapContext: string;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function buildRichSystemPrompt(ctx: RichSystemPromptContext): string {
|
|
67
|
-
const { config, skillRegistry, skillsSummaryXML, activeSkillsContext, bootstrapContext } = ctx;
|
|
68
|
-
const parts: string[] = [];
|
|
69
|
-
const cwd = process.cwd();
|
|
70
|
-
const dataDir = path.join(cwd, 'data');
|
|
71
|
-
|
|
72
|
-
// §1 Identity
|
|
73
|
-
const now = new Date();
|
|
74
|
-
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
75
|
-
const timeStr = now.toLocaleString('zh-CN', { timeZone: tz });
|
|
76
|
-
const memoryDir = path.join(dataDir, 'memory');
|
|
77
|
-
const todayStr = new Date().toISOString().split('T')[0];
|
|
78
|
-
const identityLines: string[] = [
|
|
79
|
-
config.persona,
|
|
80
|
-
'',
|
|
81
|
-
`当前时间: ${timeStr} (${tz})`,
|
|
82
|
-
`工作区: ${cwd}`,
|
|
83
|
-
`数据目录: ${dataDir}(记忆、技能等)`,
|
|
84
|
-
`长期记忆: ${path.join(memoryDir, 'MEMORY.md')};今日笔记: ${path.join(memoryDir, todayStr + '.md')}。重要事项可用 write_file 写入 MEMORY.md。`,
|
|
85
|
-
];
|
|
86
|
-
parts.push(identityLines.join('\n'));
|
|
87
|
-
|
|
88
|
-
// §2 Rules
|
|
89
|
-
const ruleLines = [
|
|
90
|
-
'## 规则',
|
|
91
|
-
'1. 直接调用工具执行操作,不要描述步骤或解释意图',
|
|
92
|
-
'2. 时间/日期问题:直接用上方"当前时间"回答,不调工具',
|
|
93
|
-
'3. 修改文件必须调用 edit_file/write_file,禁止给手动教程',
|
|
94
|
-
'4. activate_skill 返回后,必须继续调用其中指导的工具,不要停',
|
|
95
|
-
'5. 所有回答必须基于工具返回的实际数据',
|
|
96
|
-
'6. 工具失败时尝试替代方案,不要直接把错误丢给用户',
|
|
97
|
-
'7. 只根据用户**最后一条**消息作答,前面的对话仅作背景',
|
|
98
|
-
'8. 耗时或复杂的独立任务可使用 spawn_task 交给后台处理,不要阻塞当前对话',
|
|
99
|
-
'9. 用户要求从 URL 安装/加入/学习技能时,使用 install_skill(url) 下载并安装,安装后自动 activate_skill 激活',
|
|
100
|
-
];
|
|
101
|
-
parts.push(ruleLines.join('\n'));
|
|
102
|
-
|
|
103
|
-
// §3 Skills
|
|
104
|
-
if (skillsSummaryXML) {
|
|
105
|
-
parts.push('## 可用技能\n\n以下技能可扩展你的能力。使用技能时用 read_file 读取对应 SKILL.md,或直接调用 activate_skill(name)。\n\n' + skillsSummaryXML + '\n\n用户提到技能名 → 调用 activate_skill(name) → 按返回的指导执行工具');
|
|
106
|
-
} else if (skillRegistry && skillRegistry.size > 0) {
|
|
107
|
-
const skills = skillRegistry.getAll();
|
|
108
|
-
const skillLines: string[] = ['## 可用技能'];
|
|
109
|
-
for (const skill of skills) {
|
|
110
|
-
skillLines.push(`- ${skill.name}: ${skill.description}`);
|
|
111
|
-
}
|
|
112
|
-
skillLines.push('用户提到技能名 → 调用 activate_skill(name) → 按返回的指导执行工具');
|
|
113
|
-
parts.push(skillLines.join('\n'));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// §4 Active skills
|
|
117
|
-
if (activeSkillsContext) {
|
|
118
|
-
parts.push('## Active Skills\n\n' + activeSkillsContext);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// §5 Memory
|
|
122
|
-
const fileMemory = getFileMemoryContext();
|
|
123
|
-
if (fileMemory) {
|
|
124
|
-
parts.push('## Memory\n\n' + fileMemory);
|
|
125
|
-
}
|
|
126
|
-
if (bootstrapContext && bootstrapContext.includes('AGENTS.md')) {
|
|
127
|
-
parts.push('## 记忆\n\n长期记忆与重要记录见下方引导文件 AGENTS.md。');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// §6 Bootstrap
|
|
131
|
-
if (bootstrapContext) {
|
|
132
|
-
parts.push(bootstrapContext);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return parts.filter(Boolean).join(SECTION_SEP);
|
|
136
|
-
}
|