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

TextGeneration.ts

178 lines6.0 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type {
5 BranchNamingOptions,
6 ChatAttachment,
7 ModelSelection,
8 ProviderInstanceId,
9} from "@t3tools/contracts";
10import { TextGenerationError } from "@t3tools/contracts";
11​
12import * as ProviderInstanceRegistry from "../provider/Services/ProviderInstanceRegistry.ts";
13import type { ProviderInstance } from "../provider/ProviderDriver.ts";
14import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
15import * as ThreadTitleLinks from "./ThreadTitleLinks.ts";
16import type { TextGenerationPolicy } from "./TextGenerationPolicy.ts";
17​
18export interface CommitMessageGenerationInput {
19 cwd: string;
20 branch: string | null;
21 stagedSummary: string;
22 stagedPatch: string;
23 /** When true, the model also returns a semantic branch name for the change. */
24 includeBranch?: boolean;
25 policy?: TextGenerationPolicy | undefined;
26 /** What model and provider to use for generation. */
27 modelSelection: ModelSelection;
29​
30export interface CommitMessageGenerationResult {
31 subject: string;
32 body: string;
33 /** Only present when `includeBranch` was set on the input. */
34 branch?: string | undefined;
36​
37export interface PrContentGenerationInput {
38 cwd: string;
39 baseBranch: string;
40 headBranch: string;
41 commitSummary: string;
42 diffSummary: string;
43 diffPatch: string;
44 changeRequestTemplate?: string | undefined;
45 policy?: TextGenerationPolicy | undefined;
46 /** What model and provider to use for generation. */
47 modelSelection: ModelSelection;
49​
50export interface PrContentGenerationResult {
51 title: string;
52 body: string;
54​
55export interface BranchNameGenerationInput {
56 naming?: BranchNamingOptions | undefined;
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 linkedContext?: string | undefined;
70 cwd: string;
71 message: string;
72 /** Present when replacing an existing title from the current thread history. */
73 previousTitle?: string | undefined;
74 attachments?: ReadonlyArray<ChatAttachment> | undefined;
75 /** What model and provider to use for generation. */
76 modelSelection: ModelSelection;
78​
79export interface ThreadTitleGenerationResult {
80 title: string;
81 needsRefinement?: boolean | undefined;
83​
84/**
85 * TextGeneration - Service tag for commit and change request text generation.
86 */
87export class TextGeneration extends Context.Service<
88 TextGeneration,
89 {
90 /**
91 * Generate a commit message from staged change context.
92 */
93 readonly generateCommitMessage: (
94 input: CommitMessageGenerationInput,
95 ) => Effect.Effect<CommitMessageGenerationResult, TextGenerationError>;
96​
97 /**
98 * Generate change request title/body from branch and diff context.
99 */
100 readonly generatePrContent: (
101 input: PrContentGenerationInput,
102 ) => Effect.Effect<PrContentGenerationResult, TextGenerationError>;
103​
104 /**
105 * Generate a concise branch name from a user message.
106 */
107 readonly generateBranchName: (
108 input: BranchNameGenerationInput,
109 ) => Effect.Effect<BranchNameGenerationResult, TextGenerationError>;
110​
111 /** Generate a concise thread title from a first message or thread history. */
112 readonly generateThreadTitle: (
113 input: ThreadTitleGenerationInput,
114 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
115 }
116>()("t3/textGeneration/TextGeneration") {}
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​
142/** @public Service construction is part of the canonical Effect module API. */
143export const make = Effect.gen(function* () {
144 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
145 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
146 return TextGeneration.of({
147 generateCommitMessage: (input) =>
148 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
149 Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
150 ),
151 generatePrContent: (input) =>
152 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
153 Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
154 ),
155 generateBranchName: (input) =>
156 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
157 Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
158 ),
159 generateThreadTitle: (input) =>
160 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
161 Effect.flatMap((textGeneration) =>
162 Effect.gen(function* () {
163 const linkedContext =
164 input.linkedContext ??
165 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
166 Effect.provideService(
167 SourceControlProviderRegistry.SourceControlProviderRegistry,
168 sourceControl,
169 ),
170 ));
171 return yield* textGeneration.generateThreadTitle({ ...input, linkedContext });
172 }),
173 ),
174 ),
175 });
176});
177​
178export const layer = Layer.effect(TextGeneration, make);