refactor: extract execution prompt assembly
This commit is contained in:
48
src/lib/execution/prompt-assembler.ts
Normal file
48
src/lib/execution/prompt-assembler.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { MasterAgentMemory } from "@/lib/boss-data";
|
||||
|
||||
type PromptMemory = Pick<MasterAgentMemory, "projectId" | "title" | "content" | "tags">;
|
||||
|
||||
function buildProjectMemorySection(memories: PromptMemory[]) {
|
||||
if (memories.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
"项目记忆:",
|
||||
...memories.map((memory) => `- [${memory.projectId ?? "unknown"}] ${memory.title}: ${memory.content}`),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function buildUserMemorySection(memories: PromptMemory[]) {
|
||||
if (memories.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
"用户通用记忆:",
|
||||
...memories.map((memory) => `- ${memory.title}: ${memory.content}`),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function buildExecutionPrompt(input: {
|
||||
globalPrompt?: string | null;
|
||||
userPrompt?: string | null;
|
||||
conversationPrompt?: string | null;
|
||||
projectMemories: PromptMemory[];
|
||||
userMemories: PromptMemory[];
|
||||
requestText: string;
|
||||
}) {
|
||||
return [
|
||||
input.globalPrompt?.trim() ? `管理员全局主提示词:\n${input.globalPrompt.trim()}` : null,
|
||||
input.userPrompt?.trim() ? `用户私有主提示词:\n${input.userPrompt.trim()}` : null,
|
||||
input.conversationPrompt?.trim() ? `当前对话附加提示词:\n${input.conversationPrompt.trim()}` : null,
|
||||
buildProjectMemorySection(input.projectMemories),
|
||||
buildUserMemorySection(input.userMemories),
|
||||
`当前消息:\n${input.requestText}`,
|
||||
]
|
||||
.filter((value): value is string => value !== null)
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
export const buildExecutionPromptForTesting = buildExecutionPrompt;
|
||||
export { resolveRelevantMemoriesForTesting } from "@/lib/execution/memory-resolver";
|
||||
Reference in New Issue
Block a user