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

TextGeneration.ts

180 lines6.0 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 * 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 | "opencode";
20​
21export interface CommitMessageGenerationInput {
22 cwd: string;
23 branch: string | null;
24 stagedSummary: string;
25 stagedPatch: string;
26 /** When true, the model also returns a semantic branch name for the change. */
27 includeBranch?: boolean;
28 policy?: TextGenerationPolicy | undefined;
29 /** What model and provider to use for generation. */
30 modelSelection: ModelSelection;
32​
33export interface CommitMessageGenerationResult {
34 subject: string;
35 body: string;
36 /** Only present when `includeBranch` was set on the input. */
37 branch?: string | undefined;
39​
40export interface PrContentGenerationInput {
41 cwd: string;
42 baseBranch: string;
43 headBranch: string;
44 commitSummary: string;
45 diffSummary: string;
46 diffPatch: string;
47 changeRequestTemplate?: string | undefined;
48 policy?: TextGenerationPolicy | undefined;
49 /** What model and provider to use for generation. */
50 modelSelection: ModelSelection;
52​
53export interface PrContentGenerationResult {
54 title: string;
55 body: string;
57​
58export interface BranchNameGenerationInput {
59 cwd: string;
60 message: string;
61 attachments?: ReadonlyArray<ChatAttachment> | undefined;
62 /** What model and provider to use for generation. */
63 modelSelection: ModelSelection;
65​
66export interface BranchNameGenerationResult {
67 branch: string;
69​
70export interface ThreadTitleGenerationInput {
71 linkedContext?: string | undefined;
72 cwd: string;
73 message: string;
74 /** Present when replacing an existing title from the current thread history. */
75 previousTitle?: string | undefined;
76 attachments?: ReadonlyArray<ChatAttachment> | undefined;
77 /** What model and provider to use for generation. */
78 modelSelection: ModelSelection;
80​
81export interface ThreadTitleGenerationResult {
82 title: string;
83 needsRefinement?: boolean | undefined;
85​
86/**
87 * TextGeneration - Service tag for commit and change request text generation.
88 */
89export class TextGeneration extends Context.Service<
90 TextGeneration,
91 {
92 /**
93 * Generate a commit message from staged change context.
94 */
95 readonly generateCommitMessage: (
96 input: CommitMessageGenerationInput,
97 ) => Effect.Effect<CommitMessageGenerationResult, TextGenerationError>;
98​
99 /**
100 * Generate change request title/body from branch and diff context.
101 */
102 readonly generatePrContent: (
103 input: PrContentGenerationInput,
104 ) => Effect.Effect<PrContentGenerationResult, TextGenerationError>;
105​
106 /**
107 * Generate a concise branch name from a user message.
108 */
109 readonly generateBranchName: (
110 input: BranchNameGenerationInput,
111 ) => Effect.Effect<BranchNameGenerationResult, TextGenerationError>;
112​
113 /** Generate a concise thread title from a first message or thread history. */
114 readonly generateThreadTitle: (
115 input: ThreadTitleGenerationInput,
116 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
117 }
118>()("t3/textGeneration/TextGeneration") {}
119​
120type TextGenerationOp =
121 | "generateCommitMessage"
122 | "generatePrContent"
123 | "generateBranchName"
124 | "generateThreadTitle";
125​
126const resolveInstance = (
127 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
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​
144/** @public Service construction is part of the canonical Effect module API. */
145export const make = Effect.gen(function* () {
146 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
147 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
148 return TextGeneration.of({
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) =>
164 Effect.gen(function* () {
165 const linkedContext =
166 input.linkedContext ??
167 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
168 Effect.provideService(
169 SourceControlProviderRegistry.SourceControlProviderRegistry,
170 sourceControl,
171 ),
172 ));
173 return yield* textGeneration.generateThreadTitle({ ...input, linkedContext });
174 }),
175 ),
176 ),
177 });
178});
179​
180export const layer = Layer.effect(TextGeneration, make);