fix: 修正静态变量处理逻辑,确保其不出现在 system 消息中,仅用于模板展开

This commit is contained in:
carry
2026-06-08 15:54:42 +08:00
parent d31b42e4c2
commit 0869e5bf34
2 changed files with 11 additions and 15 deletions
+9 -10
View File
@@ -233,13 +233,12 @@ describe('exportToOpenAIFormat', () => {
], ],
} }
const { messages } = exportToOpenAIFormat(env) const { messages } = exportToOpenAIFormat(env)
// static_var 被提取到 system 消息中作为配置行 // 静态变量不应出现在 system 消息中,仅展开到对应位置
expect(messages[0].role).toBe('system') expect(messages[0].role).toBe('user')
expect(messages[0].content).toContain('user_name = 小明') expect(messages[0].content).toContain('小明')
// 用户消息中 static_var 展开为值 expect(messages[0].content).toContain('says hello')
expect(messages[1].role).toBe('user') // 不应生成 system 消息(没有其他结构性内容)
expect(messages[1].content).toContain('小明') expect(messages.every(m => m.role !== 'system')).toBe(true)
expect(messages[1].content).toContain('says hello')
}) })
it('expands static_vars in system_prompt template via {{var}} substitution', () => { it('expands static_vars in system_prompt template via {{var}} substitution', () => {
@@ -281,9 +280,9 @@ describe('exportToOpenAIFormat', () => {
expect(result.messages).toHaveLength(2) expect(result.messages).toHaveLength(2)
const sysMsg = result.messages[0] const sysMsg = result.messages[0]
expect(sysMsg.role).toBe('system') expect(sysMsg.role).toBe('system')
// {{var}} 模板应被展开为实际值 // {{var}} 模板应被展开为实际值,但静态变量定义本身不应出现在 system 消息中
expect(sysMsg.content).toContain('current_date = 2026年6月7日') expect(sysMsg.content).not.toContain('current_date =')
expect(sysMsg.content).toContain('language = 中文') expect(sysMsg.content).not.toContain('language =')
expect(sysMsg.content).toContain('今天是 2026年6月7日。') expect(sysMsg.content).toContain('今天是 2026年6月7日。')
expect(sysMsg.content).toContain('请用 中文 回复。') expect(sysMsg.content).toContain('请用 中文 回复。')
// 不应残留原始模板占位符 // 不应残留原始模板占位符
+2 -5
View File
@@ -85,8 +85,6 @@ export function segmentToText(seg: Segment): string | null {
/** Render a structural segment into text for the system message */ /** Render a structural segment into text for the system message */
function formatStructural(seg: Segment): string | null { function formatStructural(seg: Segment): string | null {
switch (seg.kind) { switch (seg.kind) {
case 'static_var':
return `${seg.name} = ${seg.value}`
case 'system_prompt': case 'system_prompt':
return `[System Prompt]\n${seg.content}` return `[System Prompt]\n${seg.content}`
case 'memory': case 'memory':
@@ -165,9 +163,7 @@ export function exportToOpenAIFormat(envelope: PromptEnvelope): OpenAIExport {
const summary = formatStructural(seg) const summary = formatStructural(seg)
if (summary) contentParts.push(summary) if (summary) contentParts.push(summary)
} else if (seg.kind === 'static_var') { } else if (seg.kind === 'static_var') {
// 静态变量:导出为配置行 // 静态变量不写入 system prompt,仅在上方 varMap 中参与模板展开
const s = formatStructural(seg)
if (s) contentParts.push(s)
} else if (seg.kind === 'system_prompt') { } else if (seg.kind === 'system_prompt') {
// 展开模板中的 {{var}} 占位符 // 展开模板中的 {{var}} 占位符
let expanded = seg.content let expanded = seg.content
@@ -198,6 +194,7 @@ export function exportToOpenAIFormat(envelope: PromptEnvelope): OpenAIExport {
if (seg.kind === 'tool_overview') { if (seg.kind === 'tool_overview') {
seg.items.forEach(item => tools.push(toolItemToOpenAI(item))) seg.items.forEach(item => tools.push(toolItemToOpenAI(item)))
} }
if (seg.kind === 'static_var') continue // 静态变量仅通过 segmentToText 展开到消息内容,不写入 system prompt
const s = formatStructural(seg) const s = formatStructural(seg)
if (s) systemParts.push(s) if (s) systemParts.push(s)
} }