60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import type { ChatMessage } from "@llm/types";
|
|
import type { DerivedState } from "../state/onboarding-state.js";
|
|
import type { OnboardingSession, PromptContext } from "../types.js";
|
|
import {
|
|
contextLayer,
|
|
examplesLayer,
|
|
personaLayer,
|
|
policyLayer,
|
|
stateLayer,
|
|
} from "./layers.js";
|
|
import { buildToolPolicy } from "./tool-policy.js";
|
|
|
|
export class PromptBuilder {
|
|
private constructor(
|
|
private readonly derived: DerivedState,
|
|
private readonly session: OnboardingSession | null,
|
|
private readonly context: PromptContext,
|
|
) {}
|
|
|
|
static forDerived(
|
|
derived: DerivedState,
|
|
session: OnboardingSession | null,
|
|
context: PromptContext,
|
|
): PromptBuilder {
|
|
return new PromptBuilder(derived, session, context);
|
|
}
|
|
|
|
build(): ChatMessage[] {
|
|
const promptHistory = this.context.summary
|
|
? this.context.conversationHistory.slice(-5)
|
|
: this.context.conversationHistory;
|
|
const promptContext: PromptContext = {
|
|
...this.context,
|
|
conversationHistory: promptHistory,
|
|
};
|
|
|
|
const systemPrompt = [
|
|
personaLayer(),
|
|
policyLayer(this.derived.intent, {
|
|
toolPolicy:
|
|
this.derived.responseMode === "llm-with-tools"
|
|
? buildToolPolicy(this.derived.intent.kind, this.derived.phase.kind)
|
|
: undefined,
|
|
}),
|
|
stateLayer(this.session, this.derived),
|
|
examplesLayer(this.derived.intent),
|
|
contextLayer(promptContext),
|
|
].join("\n\n");
|
|
|
|
return [
|
|
{ role: "system", content: systemPrompt },
|
|
...promptHistory.map((message) => ({
|
|
role: message.role,
|
|
content: message.content,
|
|
})),
|
|
{ role: "user", content: this.context.userMessage },
|
|
];
|
|
}
|
|
}
|