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