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