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

TextGeneration.ts

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