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

TextGeneration.ts

208 lines6.9 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import {
5 ProviderDriverKind,
6 type ChatAttachment,
7 type ModelSelection,
8 type ProviderInstanceId,
9} from "@t3tools/contracts";
10import { TextGenerationError } from "@t3tools/contracts";
11​
12import * as ProviderInstanceRegistry from "../provider/Services/ProviderInstanceRegistry.ts";
13import type { ProviderInstance } from "../provider/ProviderDriver.ts";
14import type { TextGenerationPolicy } from "./TextGenerationPolicy.ts";
15​
16const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
17​
18export function findAvailableCodexInstance(
19 instances: ReadonlyArray<ProviderInstance>,
20): ProviderInstance | undefined {
21 return instances.find(
22 (instance) => instance.enabled && instance.driverKind === CODEX_DRIVER_KIND,
23 );
25​
26export type TextGenerationProvider = "codex" | "claudeAgent" | "cursor" | "grok" | "opencode";
27​
28export interface CommitMessageGenerationInput {
29 cwd: string;
30 branch: string | null;
31 stagedSummary: string;
32 stagedPatch: string;
33 /** When true, the model also returns a semantic branch name for the change. */
34 includeBranch?: boolean;
35 policy?: TextGenerationPolicy | undefined;
36 /** What model and provider to use for generation. */
37 modelSelection: ModelSelection;
39​
40export interface CommitMessageGenerationResult {
41 subject: string;
42 body: string;
43 /** Only present when `includeBranch` was set on the input. */
44 branch?: string | undefined;
46​
47export interface PrContentGenerationInput {
48 cwd: string;
49 baseBranch: string;
50 headBranch: string;
51 commitSummary: string;
52 diffSummary: string;
53 diffPatch: string;
54 changeRequestTemplate?: string | undefined;
55 policy?: TextGenerationPolicy | undefined;
56 /** What model and provider to use for generation. */
57 modelSelection: ModelSelection;
59​
60export interface PrContentGenerationResult {
61 title: string;
62 body: string;
64​
65export interface BranchNameGenerationInput {
66 cwd: string;
67 message: string;
68 attachments?: ReadonlyArray<ChatAttachment> | undefined;
69 /** What model and provider to use for generation. */
70 modelSelection: ModelSelection;
72​
73export interface BranchNameGenerationResult {
74 branch: string;
76​
77export interface ThreadTitleGenerationInput {
78 cwd: string;
79 message: string;
80 /** Present when replacing an existing title from the current thread history. */
81 previousTitle?: string | undefined;
82 attachments?: ReadonlyArray<ChatAttachment> | undefined;
83 /** What model and provider to use for generation. */
84 modelSelection: ModelSelection;
86​
87export interface ThreadTitleGenerationResult {
88 title: string;
90​
91export interface HandoverGenerationInput {
92 cwd: string;
93 threadContents: string;
94 /** The handover action supplies GPT-5.6 Luna with high reasoning explicitly. */
95 modelSelection: ModelSelection;
97​
98export interface HandoverGenerationResult {
99 handover: string;
101​
102/**
103 * TextGeneration - Service tag for commit and change request text generation.
104 */
105export class TextGeneration extends Context.Service<
106 TextGeneration,
107 {
108 /**
109 * Generate a commit message from staged change context.
110 */
111 readonly generateCommitMessage: (
112 input: CommitMessageGenerationInput,
113 ) => Effect.Effect<CommitMessageGenerationResult, TextGenerationError>;
114​
115 /**
116 * Generate change request title/body from branch and diff context.
117 */
118 readonly generatePrContent: (
119 input: PrContentGenerationInput,
120 ) => Effect.Effect<PrContentGenerationResult, TextGenerationError>;
121​
122 /**
123 * Generate a concise branch name from a user message.
124 */
125 readonly generateBranchName: (
126 input: BranchNameGenerationInput,
127 ) => Effect.Effect<BranchNameGenerationResult, TextGenerationError>;
128​
129 /** Generate a concise thread title from a first message or thread history. */
130 readonly generateThreadTitle: (
131 input: ThreadTitleGenerationInput,
132 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
133​
134 /** Generate the compact continuation document for an oversized thread. */
135 readonly generateHandover: (
136 input: HandoverGenerationInput,
137 ) => Effect.Effect<HandoverGenerationResult, TextGenerationError>;
138 }
139>()("t3/textGeneration/TextGeneration") {}
140​
141type TextGenerationOp =
142 | "generateCommitMessage"
143 | "generatePrContent"
144 | "generateBranchName"
145 | "generateThreadTitle"
146 | "generateHandover";
147​
148const resolveInstance = (
149 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
150 operation: TextGenerationOp,
151 instanceId: ProviderInstanceId,
152): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
153 registry.getInstance(instanceId).pipe(
154 Effect.flatMap((instance) =>
155 instance
156 ? Effect.succeed(instance.textGeneration)
157 : Effect.fail(
158 new TextGenerationError({
159 operation,
160 detail: `No provider instance registered for id '${instanceId}'.`,
161 }),
162 ),
163 ),
164 );
165​
166export const makeTextGenerationFromRegistry = (
167 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
168): TextGeneration["Service"] =>
169 TextGeneration.of({
170 generateCommitMessage: (input) =>
171 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
172 Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
173 ),
174 generatePrContent: (input) =>
175 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
176 Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
177 ),
178 generateBranchName: (input) =>
179 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
180 Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
181 ),
182 generateThreadTitle: (input) =>
183 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
184 Effect.flatMap((textGeneration) => textGeneration.generateThreadTitle(input)),
185 ),
186 generateHandover: (input) =>
187 resolveInstance(registry, "generateHandover", input.modelSelection.instanceId).pipe(
188 Effect.flatMap((textGeneration) => textGeneration.generateHandover(input)),
189 ),
190 });
191​
192export const unsupportedHandoverGeneration = (
193 provider: TextGenerationProvider | "antigravity",
194): TextGeneration["Service"]["generateHandover"] =>
195 Effect.fn(`${provider}.generateHandover.unsupported`)(function* () {
196 return yield* new TextGenerationError({
197 operation: "generateHandover",
198 detail: `${provider} does not support handover generation.`,
199 });
200 });
201​
202/** @public Service construction is part of the canonical Effect module API. */
203export const make = Effect.gen(function* () {
204 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
205 return makeTextGenerationFromRegistry(registry);
206});
207​
208export const layer = Layer.effect(TextGeneration, make);