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 "@t3tools/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 === true;
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// Change request content
80// ---------------------------------------------------------------------------
81
82export interface PrContentPromptInput {
83 baseBranch: string;
84 headBranch: string;
85 commitSummary: string;
86 diffSummary: string;
87 diffPatch: string;
88 changeRequestTemplate?: string | undefined;
89 policy?: TextGenerationPolicy | undefined;
90}
91
92export function buildPrContentPrompt(input: PrContentPromptInput) {
93 const changeRequestTemplate = input.changeRequestTemplate?.trim();
94 const bodyRules = changeRequestTemplate
95 ? [
96 "- body must be markdown and follow the repository change request template structure",
97 "- fill in the template sections appropriately for this change",
98 "- drop HTML comments from the template in the generated body",
99 "- keep the template's markdown structure",
100 ]
101 : [
102 "- body must be markdown and include headings '## Summary' and '## Testing'",
103 "- under Summary, provide short bullet points",
104 "- under Testing, include bullet points with concrete checks or 'Not run' where appropriate",
105 ];
106 const prompt = [
107 "You write source control change request content.",
108 "Return a JSON object with keys: title, body.",
109 "Rules:",
110 "- title should be concise and specific",
111 ...bodyRules,
112 ...policyInstruction(input.policy?.changeRequestInstructions),
113 ...(changeRequestTemplate
114 ? ["", "Repository change request template:", limitSection(changeRequestTemplate, 8_000)]
115 : []),
116 "",
117 `Base branch: ${input.baseBranch}`,
118 `Head branch: ${input.headBranch}`,
119 "",
120 "Commits:",
121 limitSection(input.commitSummary, 12_000),
122 "",
123 "Diff stat:",
124 limitSection(input.diffSummary, 12_000),
125 "",
126 "Diff patch:",
127 limitSection(input.diffPatch, 40_000),
128 ].join("\n");
129
130 const outputSchema = Schema.Struct({
131 title: Schema.String,
132 body: Schema.String,
133 });
134
135 return { prompt, outputSchema };
136}
137
138// ---------------------------------------------------------------------------
139// Branch name
140// ---------------------------------------------------------------------------
141
142export interface BranchNamePromptInput {
143 message: string;
144 attachments?: ReadonlyArray<ChatAttachment> | undefined;
145 policy?: TextGenerationPolicy | undefined;
146}
147
148interface PromptFromMessageInput {
149 instruction: string;
150 responseShape: string;
151 rules: ReadonlyArray<string>;
152 message: string;
153 attachments?: ReadonlyArray<ChatAttachment> | undefined;
154 additionalInstructions?: string | undefined;
155}
156
157function buildPromptFromMessage(input: PromptFromMessageInput): string {
158 const attachmentLines = (input.attachments ?? []).map(
159 (attachment) => `- ${attachment.name} (${attachment.mimeType}, ${attachment.sizeBytes} bytes)`,
160 );
161
162 const promptSections = [
163 input.instruction,
164 input.responseShape,
165 "Rules:",
166 ...input.rules.map((rule) => `- ${rule}`),
167 "",
168 "User message:",
169 limitSection(input.message, 8_000),
170 ...policyInstruction(input.additionalInstructions),
171 ];
172 if (attachmentLines.length > 0) {
173 promptSections.push(
174 "",
175 "Attachment metadata:",
176 limitSection(attachmentLines.join("\n"), 4_000),
177 );
178 }
179
180 return promptSections.join("\n");
181}
182
183export function buildBranchNamePrompt(input: BranchNamePromptInput) {
184 const prompt = buildPromptFromMessage({
185 instruction: "You generate concise git branch names.",
186 responseShape: "Return a JSON object with key: branch.",
187 rules: [
188 "Branch should describe the requested work from the user message.",
189 "Keep it short and specific (2-6 words).",
190 "Use plain words only, no issue prefixes and no punctuation-heavy text.",
191 "If images are attached, use them as primary context for visual/UI issues.",
192 ],
193 message: input.message,
194 attachments: input.attachments,
195 additionalInstructions: input.policy?.branchInstructions,
196 });
197 const outputSchema = Schema.Struct({
198 branch: Schema.String,
199 });
200
201 return { prompt, outputSchema };
202}
203
204// ---------------------------------------------------------------------------
205// Thread title
206// ---------------------------------------------------------------------------
207
208export interface ThreadTitlePromptInput {
209 message: string;
210 attachments?: ReadonlyArray<ChatAttachment> | undefined;
211 policy?: TextGenerationPolicy | undefined;
212}
213
214export function buildThreadTitlePrompt(input: ThreadTitlePromptInput) {
215 const prompt = buildPromptFromMessage({
216 instruction: "You write concise thread titles for coding conversations.",
217 responseShape: "Return a JSON object with key: title.",
218 rules: [
219 "Title should summarize the user's request, not restate it verbatim.",
220 "Keep it short and specific (3-8 words).",
221 "Avoid quotes, filler, prefixes, and trailing punctuation.",
222 "If images are attached, use them as primary context for visual/UI issues.",
223 ],
224 message: input.message,
225 attachments: input.attachments,
226 additionalInstructions: input.policy?.threadTitleInstructions,
227 });
228 const outputSchema = Schema.Struct({
229 title: Schema.String,
230 });
231
232 return { prompt, outputSchema };
233}