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

TextGeneration.ts

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