Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3
4import { type ChatAttachment, type ModelSelection, TextGenerationError } from "@t3tools/contracts";
5import { formatGeneratedBranchName, sanitizeFeatureBranchName } from "@t3tools/shared/git";
6import { extractJsonObject } from "@t3tools/shared/schemaJson";
7
8import type { ProviderTextGeneration } from "./textGeneration.ts";
9import {
10 buildBranchNamePrompt,
11 buildCommitMessagePrompt,
12 buildPrContentPrompt,
13 buildReviewGuidePrompt,
14 buildThreadTitlePrompt,
15} from "./textGenerationPrompts.ts";
16import {
17 sanitizeCommitSubject,
18 sanitizePrTitle,
19 sanitizeThreadTitle,
20} from "./textGenerationUtils.ts";
21
22export type Operation = keyof ProviderTextGeneration;
23
24export interface Request<S extends Schema.Top> {
25 readonly operation: Operation;
26 readonly cwd: string;
27 readonly prompt: string;
28 readonly outputSchema: S;
29 readonly modelSelection: ModelSelection;
30 readonly attachments?: ReadonlyArray<ChatAttachment> | undefined;
31 /** Investigate cwd with read-only tools across turns; fail if the runner cannot guarantee read-only access. */
32 readonly investigate?: { readonly timeoutMs: number } | undefined;
33}
34
35const INVESTIGATION_TIMEOUT_MS = 15 * 60_000;
36
37export const investigationUnsupported = (operation: Operation, provider: string) =>
38 new TextGenerationError({
39 operation,
40 detail: `${provider} cannot investigate a checkout read-only. Choose another review guide model, or source control writer model when none is set.`,
41 });
42
43export type Runner = <S extends Schema.Top>(
44 request: Request<S>,
45) => Effect.Effect<S["Type"], TextGenerationError, S["DecodingServices"]>;
46
47export const withoutInvestigation =
48 (provider: string, run: Runner): Runner =>
49 (request) =>
50 request.investigate
51 ? Effect.fail(investigationUnsupported(request.operation, provider))
52 : run(request);
53
54export const decodeJsonReply = <S extends Schema.Top>(
55 request: Pick<Request<S>, "operation" | "outputSchema">,
56 provider: string,
57 text: string,
58) => {
59 const decodeOutput = Schema.decodeEffect(Schema.fromJsonString(request.outputSchema));
60 return decodeOutput(extractJsonObject(text)).pipe(
61 Effect.mapError(
62 (cause) =>
63 new TextGenerationError({
64 operation: request.operation,
65 detail: `${provider} returned invalid structured output.`,
66 cause,
67 }),
68 ),
69 );
70};
71
72export function fromRunner(name: string, run: Runner): ProviderTextGeneration {
73 const generateCommitMessage: ProviderTextGeneration["generateCommitMessage"] = Effect.fn(
74 `${name}.generateCommitMessage`,
75 )(function* (input) {
76 const generated = yield* run({
77 operation: "generateCommitMessage",
78 cwd: input.cwd,
79 modelSelection: input.modelSelection,
80 ...buildCommitMessagePrompt({
81 branch: input.branch,
82 stagedSummary: input.stagedSummary,
83 stagedPatch: input.stagedPatch,
84 includeBranch: input.includeBranch === true,
85 policy: input.policy,
86 }),
87 });
88 return {
89 subject: sanitizeCommitSubject(generated.subject),
90 body: generated.body.trim(),
91 ...("branch" in generated && typeof generated.branch === "string"
92 ? { branch: sanitizeFeatureBranchName(generated.branch) }
93 : {}),
94 };
95 });
96
97 const generatePrContent: ProviderTextGeneration["generatePrContent"] = Effect.fn(
98 `${name}.generatePrContent`,
99 )(function* (input) {
100 const generated = yield* run({
101 operation: "generatePrContent",
102 cwd: input.cwd,
103 modelSelection: input.modelSelection,
104 ...buildPrContentPrompt({
105 baseBranch: input.baseBranch,
106 headBranch: input.headBranch,
107 commitSummary: input.commitSummary,
108 diffSummary: input.diffSummary,
109 diffPatch: input.diffPatch,
110 policy: input.policy,
111 changeRequestTemplate: input.changeRequestTemplate,
112 threadContext: input.threadContext,
113 }),
114 });
115 return { title: sanitizePrTitle(generated.title), body: generated.body.trim() };
116 });
117
118 const generateBranchName: ProviderTextGeneration["generateBranchName"] = Effect.fn(
119 `${name}.generateBranchName`,
120 )(function* (input) {
121 const generated = yield* run({
122 operation: "generateBranchName",
123 cwd: input.cwd,
124 modelSelection: input.modelSelection,
125 attachments: input.attachments,
126 ...buildBranchNamePrompt({
127 message: input.message,
128 attachments: input.attachments,
129 naming: input.naming,
130 }),
131 });
132 return { branch: formatGeneratedBranchName(generated.branch, input.naming) };
133 });
134
135 const generateThreadTitle: ProviderTextGeneration["generateThreadTitle"] = Effect.fn(
136 `${name}.generateThreadTitle`,
137 )(function* (input) {
138 const generated = yield* run({
139 operation: "generateThreadTitle",
140 cwd: input.cwd,
141 modelSelection: input.modelSelection,
142 attachments: input.attachments,
143 ...buildThreadTitlePrompt({
144 message: input.message,
145 previousTitle: input.previousTitle,
146 forkSource: input.forkSource,
147 linkedContext: input.linkedContext,
148 attachments: input.attachments,
149 instructions: input.instructions,
150 }),
151 });
152 return {
153 title: sanitizeThreadTitle(generated.title),
154 ...(generated.needsRefinement ? { needsRefinement: true } : {}),
155 };
156 });
157
158 const generateReviewGuide: ProviderTextGeneration["generateReviewGuide"] = Effect.fn(
159 `${name}.generateReviewGuide`,
160 )(function* (input) {
161 return yield* run({
162 operation: "generateReviewGuide",
163 cwd: input.cwd,
164 modelSelection: input.modelSelection,
165 investigate: { timeoutMs: INVESTIGATION_TIMEOUT_MS },
166 ...buildReviewGuidePrompt(input),
167 });
168 });
169
170 return {
171 generateCommitMessage,
172 generatePrContent,
173 generateBranchName,
174 generateThreadTitle,
175 generateReviewGuide,
176 } satisfies ProviderTextGeneration;
177}