Open History to see the last commit
1/**
2 * Shared prompt builders for text generation providers.
3 *
4 * Extracts the prompt construction logic that is identical across
5 * Codex, Claude, and any future CLI-based text generation backends.
6 *
7 * @module textGenerationPrompts
8 */
9import * as Schema from "effect/Schema";
10import type { ChatAttachment } from "@v12/contracts";
11
12import { limitSection } from "./TextGenerationUtils.ts";
13import type { TextGenerationPolicy } from "./TextGenerationPolicy.ts";
14
15function policyInstruction(instruction: string | undefined): ReadonlyArray<string> {
16 const trimmed = instruction?.trim();
17 return trimmed ? ["", "Additional instructions:", limitSection(trimmed, 4_000)] : [];
18}
19
20// ---------------------------------------------------------------------------
21// Commit message
22// ---------------------------------------------------------------------------
23
24export interface CommitMessagePromptInput {
25 branch: string | null;
26 stagedSummary: string;
27 stagedPatch: string;
28 includeBranch: boolean;
29 policy?: TextGenerationPolicy | undefined;
30}
31
32export function buildCommitMessagePrompt(input: CommitMessagePromptInput) {
33 const wantsBranch = input.includeBranch;
34
35 const prompt = [
36 "You write concise git commit messages.",
37 wantsBranch
38 ? "Return a JSON object with keys: subject, body, branch."
39 : "Return a JSON object with keys: subject, body.",
40 "Rules:",
41 "- subject must be imperative, <= 72 chars, and no trailing period",
42 "- body can be empty string or short bullet points",
43 ...(wantsBranch
44 ? ["- branch must be a short semantic git branch fragment for this change"]
45 : []),
46 "- capture the primary user-visible or developer-visible change",
47 ...policyInstruction(input.policy?.commitInstructions),
48 "",
49 `Branch: ${input.branch ?? "(detached)"}`,
50 "",
51 "Staged files:",
52 limitSection(input.stagedSummary, 6_000),
53 "",
54 "Staged patch:",
55 limitSection(input.stagedPatch, 40_000),
56 ].join("\n");
57
58 if (wantsBranch) {
59 return {
60 prompt,
61 outputSchema: Schema.Struct({
62 subject: Schema.String,
63 body: Schema.String,
64 branch: Schema.String,
65 }),
66 };
67 }
68
69 return {
70 prompt,
71 outputSchema: Schema.Struct({
72 subject: Schema.String,
73 body: Schema.String,
74 }),
75 };
76}
77
78// ---------------------------------------------------------------------------
79// PR content
80// ---------------------------------------------------------------------------
81
82export interface PrContentPromptInput {
83 baseBranch: string;
84 headBranch: string;
85 commitSummary: string;
86 diffSummary: string;
87 diffPatch: string;
88 policy?: TextGenerationPolicy | undefined;
89}
90
91export function buildPrContentPrompt(input: PrContentPromptInput) {
92 const prompt = [
93 "You write GitHub pull request content.",
94 "Return a JSON object with keys: title, body.",
95 "Rules:",
96 "- title should be concise and specific",
97 "- body must be markdown and include headings '## Summary' and '## Testing'",
98 "- under Summary, provide short bullet points",
99 "- under Testing, include bullet points with concrete checks or 'Not run' where appropriate",
100 ...policyInstruction(input.policy?.changeRequestInstructions),
101 "",
102 `Base branch: ${input.baseBranch}`,
103 `Head branch: ${input.headBranch}`,
104 "",
105 "Commits:",
106 limitSection(input.commitSummary, 12_000),
107 "",
108 "Diff stat:",
109 limitSection(input.diffSummary, 12_000),
110 "",
111 "Diff patch:",
112 limitSection(input.diffPatch, 40_000),
113 ].join("\n");
114
115 const outputSchema = Schema.Struct({
116 title: Schema.String,
117 body: Schema.String,
118 });
119
120 return { prompt, outputSchema };
121}
122
123// ---------------------------------------------------------------------------
124// Branch name
125// ---------------------------------------------------------------------------
126
127export interface BranchNamePromptInput {
128 message: string;
129 attachments?: ReadonlyArray<ChatAttachment> | undefined;
130 policy?: TextGenerationPolicy | undefined;
131}
132
133interface PromptFromMessageInput {
134 instruction: string;
135 responseShape: string;
136 rules: ReadonlyArray<string>;
137 message: string;
138 attachments?: ReadonlyArray<ChatAttachment> | undefined;
139 additionalInstructions?: string | undefined;
140}
141
142function buildPromptFromMessage(input: PromptFromMessageInput): string {
143 const attachmentLines = (input.attachments ?? []).map(
144 (attachment) => `- ${attachment.name} (${attachment.mimeType}, ${attachment.sizeBytes} bytes)`,
145 );
146
147 const promptSections = [
148 input.instruction,
149 input.responseShape,
150 "Rules:",
151 ...input.rules.map((rule) => `- ${rule}`),
152 "",
153 "User message:",
154 limitSection(input.message, 8_000),
155 ...policyInstruction(input.additionalInstructions),
156 ];
157 if (attachmentLines.length > 0) {
158 promptSections.push(
159 "",
160 "Attachment metadata:",
161 limitSection(attachmentLines.join("\n"), 4_000),
162 );
163 }
164
165 return promptSections.join("\n");
166}
167
168export function buildBranchNamePrompt(input: BranchNamePromptInput) {
169 const prompt = buildPromptFromMessage({
170 instruction: "You generate concise git branch names.",
171 responseShape: "Return a JSON object with key: branch.",
172 rules: [
173 "Branch should describe the requested work from the user message.",
174 "Keep it short and specific (2-6 words).",
175 "Use plain words only, no issue prefixes and no punctuation-heavy text.",
176 "If images are attached, use them as primary context for visual/UI issues.",
177 ],
178 message: input.message,
179 attachments: input.attachments,
180 additionalInstructions: input.policy?.branchInstructions,
181 });
182 const outputSchema = Schema.Struct({
183 branch: Schema.String,
184 });
185
186 return { prompt, outputSchema };
187}
188
189// ---------------------------------------------------------------------------
190// Thread title
191// ---------------------------------------------------------------------------
192
193export interface ThreadTitlePromptInput {
194 message: string;
195 attachments?: ReadonlyArray<ChatAttachment> | undefined;
196 policy?: TextGenerationPolicy | undefined;
197}
198
199export function buildThreadTitlePrompt(input: ThreadTitlePromptInput) {
200 const prompt = buildPromptFromMessage({
201 instruction: "You write concise thread titles for coding conversations.",
202 responseShape: "Return a JSON object with key: title.",
203 rules: [
204 "Title should summarize the user's request, not restate it verbatim.",
205 "Keep it short and specific (3-8 words).",
206 "Avoid quotes, filler, prefixes, and trailing punctuation.",
207 "If images are attached, use them as primary context for visual/UI issues.",
208 ],
209 message: input.message,
210 attachments: input.attachments,
211 additionalInstructions: input.policy?.threadTitleInstructions,
212 });
213 const outputSchema = Schema.Struct({
214 title: Schema.String,
215 });
216
217 return { prompt, outputSchema };
218}