fix(prompt): remove unreachable complete branches

This commit is contained in:
Yichen Jiang
2026-08-10 20:09:06 +08:00
parent 6dabf0ea99
commit eacd5e2167
6 changed files with 19 additions and 22 deletions

View File

@@ -478,23 +478,21 @@ export class SystemPrompt extends Service {
collected.push(...schemas)
for (const name of acceptedKnownNames) knownNames.add(name)
}
const completeSections = [...sectionByName.values()].filter(section => section.complete === true)
const sectionDefinitions = [...sectionByName.values()].sort((a, b) => a.order - b.order)
const completeSections = sectionDefinitions.filter(section => section.complete === true)
if (completeSections.length > 1) {
throw new Error(`multiple complete prompt sections are active: ${completeSections.map(section => JSON.stringify(section.name)).join(', ')}`)
}
const sections = [...sectionByName.values()]
.sort((a, b) => a.order - b.order)
.map(section => ({
name: section.name,
text: typeof section.text === 'function' ? section.text(context) : section.text,
}))
const completeName = completeSections[0]?.name
let completeSection: AssembledSection | undefined
if (completeName !== undefined) {
const assembled = sections.find(section => section.name === completeName)
if (assembled === undefined) throw new Error(`complete prompt section ${JSON.stringify(completeName)} did not assemble`)
completeSection = { ...assembled }
}
const sections = sectionDefinitions
.map((section) => {
const assembled = {
name: section.name,
text: typeof section.text === 'function' ? section.text(context) : section.text,
}
if (section.complete === true) completeSection = { ...assembled }
return assembled
})
const assembly: PromptAssembly = {
sections,
contexts: [...contextByName.values()]

View File

@@ -59,6 +59,6 @@ export function apply(ctx: Context, config: Config): void {
name: PERSONA_SECTION,
order: PERSONA_ORDER,
text: config.text,
complete: config.complete ?? false,
...(config.complete ? { complete: true } : {}),
}), 'persona.section()')
}