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

TextGeneration.ts

174 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 = "codex" | "claudeAgent" | "cursor" | "grok" | "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 policy?: TextGenerationPolicy | undefined;
23 /** What model and provider to use for generation. */
24 modelSelection: ModelSelection;
26​
27export interface CommitMessageGenerationResult {
28 subject: string;
29 body: string;
30 /** Only present when `includeBranch` was set on the input. */
31 branch?: string | undefined;
33​
34export interface PrContentGenerationInput {
35 cwd: string;
36 baseBranch: string;
37 headBranch: string;
38 commitSummary: string;
39 diffSummary: string;
40 diffPatch: string;
41 changeRequestTemplate?: string | undefined;
42 policy?: TextGenerationPolicy | undefined;
43 /** What model and provider to use for generation. */
44 modelSelection: ModelSelection;
46​
47export interface PrContentGenerationResult {
48 title: string;
49 body: string;
51​
52export interface BranchNameGenerationInput {
53 cwd: string;
54 message: string;
55 attachments?: ReadonlyArray<ChatAttachment> | undefined;
56 /** What model and provider to use for generation. */
57 modelSelection: ModelSelection;
59​
60export interface BranchNameGenerationResult {
61 branch: string;
63​
64export interface ThreadTitleGenerationInput {
65 linkedContext?: string | undefined;
66 cwd: string;
67 message: string;
68 /** Present when replacing an existing title from the current thread history. */
69 previousTitle?: string | undefined;
70 attachments?: ReadonlyArray<ChatAttachment> | undefined;
71 /** What model and provider to use for generation. */
72 modelSelection: ModelSelection;
74​
75export interface ThreadTitleGenerationResult {
76 title: string;
77 needsRefinement?: boolean | undefined;
79​
80/**
81 * TextGeneration - Service tag for commit and change request text generation.
82 */
83export class TextGeneration extends Context.Service<
84 TextGeneration,
85 {
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 change 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 /** Generate a concise thread title from a first message or thread history. */
108 readonly generateThreadTitle: (
109 input: ThreadTitleGenerationInput,
110 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
111 }
112>()("t3/textGeneration/TextGeneration") {}
113​
114type TextGenerationOp =
115 | "generateCommitMessage"
116 | "generatePrContent"
117 | "generateBranchName"
118 | "generateThreadTitle";
119​
120const resolveInstance = (
121 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
122 operation: TextGenerationOp,
123 instanceId: ProviderInstanceId,
124): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
125 registry.getInstance(instanceId).pipe(
126 Effect.flatMap((instance) =>
127 instance
128 ? Effect.succeed(instance.textGeneration)
129 : Effect.fail(
130 new TextGenerationError({
131 operation,
132 detail: `No provider instance registered for id '${instanceId}'.`,
133 }),
134 ),
135 ),
136 );
137​
138/** @public Service construction is part of the canonical Effect module API. */
139export const make = Effect.gen(function* () {
140 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
141 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
142 return TextGeneration.of({
143 generateCommitMessage: (input) =>
144 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
145 Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
146 ),
147 generatePrContent: (input) =>
148 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
149 Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
150 ),
151 generateBranchName: (input) =>
152 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
153 Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
154 ),
155 generateThreadTitle: (input) =>
156 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
157 Effect.flatMap((textGeneration) =>
158 Effect.gen(function* () {
159 const linkedContext =
160 input.linkedContext ??
161 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
162 Effect.provideService(
163 SourceControlProviderRegistry.SourceControlProviderRegistry,
164 sourceControl,
165 ),
166 ));
167 return yield* textGeneration.generateThreadTitle({ ...input, linkedContext });
168 }),
169 ),
170 ),
171 });
172});
173​
174export const layer = Layer.effect(TextGeneration, make);