Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type { ChatAttachment, ModelSelection, ProviderInstanceId } from "@t3tools/contracts";
5import { TextGenerationError } from "@t3tools/contracts";
6
7import * as ProviderInstanceRegistry from "../provider/Services/ProviderInstanceRegistry.ts";
8import type { ProviderInstance } from "../provider/ProviderDriver.ts";
9import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
10import * as ThreadTitleLinks from "./ThreadTitleLinks.ts";
11import type { TextGenerationPolicy } from "./TextGenerationPolicy.ts";
12
13export type TextGenerationProvider =
14 | "codex"
15 | "claudeAgent"
16 | "cursor"
17 | "grok"
18 | "pi"
19 | "muse"
20 | "opencode";
21
22export interface CommitMessageGenerationInput {
23 cwd: string;
24 branch: string | null;
25 stagedSummary: string;
26 stagedPatch: string;
27 /** When true, the model also returns a semantic branch name for the change. */
28 includeBranch?: boolean;
29 policy?: TextGenerationPolicy | undefined;
30 /** What model and provider to use for generation. */
31 modelSelection: ModelSelection;
32}
33
34export interface CommitMessageGenerationResult {
35 subject: string;
36 body: string;
37 /** Only present when `includeBranch` was set on the input. */
38 branch?: string | undefined;
39}
40
41export interface PrContentGenerationInput {
42 cwd: string;
43 baseBranch: string;
44 headBranch: string;
45 commitSummary: string;
46 diffSummary: string;
47 diffPatch: string;
48 changeRequestTemplate?: string | undefined;
49 policy?: TextGenerationPolicy | undefined;
50 /** What model and provider to use for generation. */
51 modelSelection: ModelSelection;
52}
53
54export interface PrContentGenerationResult {
55 title: string;
56 body: string;
57}
58
59export interface BranchNameGenerationInput {
60 cwd: string;
61 message: string;
62 attachments?: ReadonlyArray<ChatAttachment> | undefined;
63 /** What model and provider to use for generation. */
64 modelSelection: ModelSelection;
65}
66
67export interface BranchNameGenerationResult {
68 branch: string;
69}
70
71export interface ThreadTitleGenerationInput {
72 linkedContext?: string | undefined;
73 cwd: string;
74 message: string;
75 /** Present when replacing an existing title from the current thread history. */
76 previousTitle?: string | undefined;
77 attachments?: ReadonlyArray<ChatAttachment> | undefined;
78 /** What model and provider to use for generation. */
79 modelSelection: ModelSelection;
80}
81
82export interface ThreadTitleGenerationResult {
83 title: string;
84 needsRefinement?: boolean | undefined;
85}
86
87/**
88 * TextGeneration - Service tag for commit and change request text generation.
89 */
90export class TextGeneration extends Context.Service<
91 TextGeneration,
92 {
93 /**
94 * Generate a commit message from staged change context.
95 */
96 readonly generateCommitMessage: (
97 input: CommitMessageGenerationInput,
98 ) => Effect.Effect<CommitMessageGenerationResult, TextGenerationError>;
99
100 /**
101 * Generate change request title/body from branch and diff context.
102 */
103 readonly generatePrContent: (
104 input: PrContentGenerationInput,
105 ) => Effect.Effect<PrContentGenerationResult, TextGenerationError>;
106
107 /**
108 * Generate a concise branch name from a user message.
109 */
110 readonly generateBranchName: (
111 input: BranchNameGenerationInput,
112 ) => Effect.Effect<BranchNameGenerationResult, TextGenerationError>;
113
114 /** Generate a concise thread title from a first message or thread history. */
115 readonly generateThreadTitle: (
116 input: ThreadTitleGenerationInput,
117 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
118 }
119>()("t3/textGeneration/TextGeneration") {}
120
121type TextGenerationOp =
122 | "generateCommitMessage"
123 | "generatePrContent"
124 | "generateBranchName"
125 | "generateThreadTitle";
126
127const resolveInstance = (
128 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
129 operation: TextGenerationOp,
130 instanceId: ProviderInstanceId,
131): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
132 registry.getInstance(instanceId).pipe(
133 Effect.flatMap((instance) =>
134 instance
135 ? Effect.succeed(instance.textGeneration)
136 : Effect.fail(
137 new TextGenerationError({
138 operation,
139 detail: `No provider instance registered for id '${instanceId}'.`,
140 }),
141 ),
142 ),
143 );
144
145/** @public Service construction is part of the canonical Effect module API. */
146export const make = Effect.gen(function* () {
147 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
148 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
149 return TextGeneration.of({
150 generateCommitMessage: (input) =>
151 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
152 Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
153 ),
154 generatePrContent: (input) =>
155 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
156 Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
157 ),
158 generateBranchName: (input) =>
159 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
160 Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
161 ),
162 generateThreadTitle: (input) =>
163 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
164 Effect.flatMap((textGeneration) =>
165 Effect.gen(function* () {
166 const linkedContext =
167 input.linkedContext ??
168 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
169 Effect.provideService(
170 SourceControlProviderRegistry.SourceControlProviderRegistry,
171 sourceControl,
172 ),
173 ));
174 return yield* textGeneration.generateThreadTitle({ ...input, linkedContext });
175 }),
176 ),
177 ),
178 });
179});
180
181export const layer = Layer.effect(TextGeneration, make);