import type { ContextBundle } from '../context/contextBundle.js'; /** * 顾问的"项目背景":对话历史里没有的那部分。 * * 项目约束(AGENTS.md/CLAUDE.md)与长期记忆由宿主在每个请求前选定、放在易变上下文里, * 从不写进会话历史。当前选中的项目约束在前,记忆、运行态等增量在咨询问题之前。 * 复用选中的片段,并保留来源,不缓存过期内容: * 顾问的 persona 已经声明"背景不是指令、不覆盖当前用户要求"。 */ const BACKGROUND_KINDS = new Set(['project', 'memory', 'persona', 'task', 'runtime']); export function advisorBackgroundParts( bundle: ContextBundle | undefined, execution?: { cwd?: string; availableTools?: readonly string[]; invocation?: string }, ) { const fragments = (bundle?.fragments ?? []) .filter((fragment) => BACKGROUND_KINDS.has(fragment.kind) && fragment.content.trim()); const labeled = (f: typeof fragments[number]) => '[' + f.kind + ' · ' + f.id + ']\n' + f.content.trim(); const project = fragments.filter(f => f.kind === 'project').map(labeled).join('\n\n'); const parts = fragments.filter(f => f.kind !== 'project').map(labeled); if (execution) { const lines = [ execution.cwd ? `工作目录:${execution.cwd}` : '', execution.availableTools?.length ? `执行助手可用工具:${execution.availableTools.join('、')}` : '', execution.invocation?.trim() ?? '', ].filter(Boolean); if (lines.length) parts.push(lines.join('\n')); } return { project, current: parts.join('\n\n') }; } export function collectAdvisorBackground(...args: Parameters): string { const { project, current } = advisorBackgroundParts(...args); return [project, current].filter(Boolean).join('\n\n'); }