Repository navigation
Expand file tree
/
Copy pathprompts.ts
More file actions
127 lines (115 loc) · 5.81 KB
/
Copy pathprompts.ts
File metadata and controls
127 lines (115 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Thin facades over the codegen'd render handles. The prose that used to live
// in this file (per-wizard system text, verdict scaffold, source formatting)
// now lives in data/templates/**/*.mustache, with the templates compiled into
// src/render/generated/templates.ts at build time. This module preserves the
// historical positional signatures so call sites and the byte-equivalence
// snapshot test do not need to change.
import { renderVerdictSystem, renderVerdictUser } from "../render/generated/prompts";
// Payload types are the value objects' own generated types (ADR-0056, 1.0.5).
import type { WizardSystemPayload } from "../db/generated/WizardSystemPayload";
import type { WizardUserPayload } from "../db/generated/WizardUserPayload";
import type { VerdictSystemPayload } from "../db/generated/VerdictSystemPayload";
import type { VerdictUserPayload } from "../db/generated/VerdictUserPayload";
import { templateProvider } from "../render/generated/templates";
import { WIZARD_REGISTRY } from "../personas/generated/registry";
import type { Wizard } from "../db/generated/Wizard";
import type { WizardOutput } from "../db/generated/WizardOutput";
import type { TurnLens } from "../db/generated/TurnLens";
import { turnLensFieldsFromWizardOutput } from "../db/generated/TurnLens.lineage";
import type { SourceLens } from "../db/generated/SourceLens";
import type { AnthropicMessage } from "../llm/anthropic";
import type { SearchResult } from "../llm/web-search";
export interface PriorTurn {
persona: Wizard;
take: WizardOutput;
}
export function fullName(p: Wizard): string {
return `${p.name} ${p.title}`.trim();
}
// Projection-boundary helpers: PriorTurn/SearchResult (internal shapes) → the
// codegen'd TurnLens/SourceLens the user templates consume. Shared by the
// orchestrator and the message builders so the two projections can't drift
// (the snippet truncation in particular must stay in lock-step).
/** Project one prior wizard turn into the TurnLens shape shown to later wizards + the Clerk. */
export function toTurnLens(t: PriorTurn): TurnLens {
return { wizardId: t.persona.id, wizardName: fullName(t.persona), ...turnLensFieldsFromWizardOutput(t.take) };
}
/** Project a search hit into SourceLens. Snippet is truncated to 300 chars to
* preserve the pre-Phase-F prompt byte-equivalence (SearXNG snippets run >1k). */
export function toSourceLens(s: SearchResult): SourceLens {
return { title: s.title, url: s.url, snippet: s.content.slice(0, 300) };
}
function entryFor(wizard: Wizard) {
const e = WIZARD_REGISTRY.find((x) => x.wizard.id === wizard.id);
if (!e) throw new Error(`No registry entry for wizard ${wizard.id}`);
return e;
}
function rivalNamesOf(wizard: Wizard, all: readonly Wizard[]): string[] {
const bickersWith = (wizard.bickersWith ?? []) as readonly string[];
return bickersWith
.map((id) => all.find((w) => w.id === id)?.name)
.filter((n): n is string => Boolean(n));
}
export function buildWizardSystem(wizard: Wizard, all: readonly Wizard[]): string {
const turnNumber = all.findIndex((w) => w.id === wizard.id) + 1;
const rivalNames = rivalNamesOf(wizard, all);
// Project the rival list into RivalLens rows (name + a `last` flag). The
// system template does its own ", " joining via {{^last}}, {{/last}} in the
// shared wizards/bicker partial, so the separator prose lives in the template
// (visible to `meta verify`), never in a code-side .join(", ").
const payload: WizardSystemPayload = {
hasRivals: rivalNames.length > 0,
rivals: rivalNames.map((name, i) => ({ name, last: i === rivalNames.length - 1 })),
turnNumber,
};
// Mustache may produce trailing whitespace from section iteration; today's
// hand-rolled join produced none. trimEnd() guarantees byte-equivalence.
return entryFor(wizard).renderSystem(payload, templateProvider).trimEnd();
}
/** The payload every wizard's user prompt renders from. The orchestrator and
* buildWizardMessages both build it here, so the snapshot test covers the
* payload production sends. */
export function buildWizardUserPayload(
question: string,
prior: readonly PriorTurn[],
sources: readonly SearchResult[] = [],
): WizardUserPayload {
return {
question,
prior: prior.map(toTurnLens),
hasPrior: prior.length > 0,
sources: sources.map(toSourceLens),
hasSources: sources.length > 0,
};
}
/** @internal — kept for byte-equivalence snapshot test (`test/prompts-snapshot.test.ts`). */
export function buildWizardMessages(
question: string,
prior: PriorTurn[],
sources: readonly SearchResult[] = [],
): AnthropicMessage[] {
// All wizard user templates are byte-identical (the per-wizard
// variation is system-side); any renderer produces the same output.
// Pick the first registry entry by convention.
const payload = buildWizardUserPayload(question, prior, sources);
const content = WIZARD_REGISTRY[0].renderUser(payload, templateProvider).trimEnd();
return [{ role: "user", content }];
}
export function buildVerdictSystem(all: readonly Wizard[]): string {
// Project the guild into RosterLens rows (fullName + a `last` flag); the
// verdict system template joins them with ", " itself via {{^last}}, {{/last}},
// so the separator prose lives in the template, not a code-side .join(", ").
const payload: VerdictSystemPayload = {
roster: all.map((p, i) => ({ fullName: fullName(p), last: i === all.length - 1 })),
};
return renderVerdictSystem(payload, templateProvider).trimEnd();
}
/** `prior` is expected non-empty: the orchestrator calls this only after every wizard has spoken. */
export function buildVerdictMessages(question: string, prior: PriorTurn[]): AnthropicMessage[] {
const payload: VerdictUserPayload = {
question,
prior: prior.map(toTurnLens),
};
const content = renderVerdictUser(payload, templateProvider).trimEnd();
return [{ role: "user", content }];
}