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

TextGeneration.ts

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