Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

TextGeneration.ts

173 lines5.4 KB

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