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

TextGeneration.ts

172 lines5.9 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 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 linkedContext?: string | undefined;
64 cwd: string;
65 message: string;
66 /** Present when replacing an existing title from the current thread history. */
67 previousTitle?: string | undefined;
68 attachments?: ReadonlyArray<ChatAttachment> | undefined;
69 /** What model and provider to use for generation. */
70 modelSelection: ModelSelection;
72​
73export interface ThreadTitleGenerationResult {
74 title: string;
75 needsRefinement?: boolean | undefined;
77​
78/**
79 * TextGeneration - Service tag for commit and change request text generation.
80 */
81export class TextGeneration extends Context.Service<
82 TextGeneration,
83 {
84 /**
85 * Generate a commit message from staged change context.
86 */
87 readonly generateCommitMessage: (
88 input: CommitMessageGenerationInput,
89 ) => Effect.Effect<CommitMessageGenerationResult, TextGenerationError>;
90​
91 /**
92 * Generate change request title/body from branch and diff context.
93 */
94 readonly generatePrContent: (
95 input: PrContentGenerationInput,
96 ) => Effect.Effect<PrContentGenerationResult, TextGenerationError>;
97​
98 /**
99 * Generate a concise branch name from a user message.
100 */
101 readonly generateBranchName: (
102 input: BranchNameGenerationInput,
103 ) => Effect.Effect<BranchNameGenerationResult, TextGenerationError>;
104​
105 /** Generate a concise thread title from a first message or thread history. */
106 readonly generateThreadTitle: (
107 input: ThreadTitleGenerationInput,
108 ) => Effect.Effect<ThreadTitleGenerationResult, TextGenerationError>;
109 }
110>()("t3/textGeneration/TextGeneration") {}
111​
112type TextGenerationOp =
113 | "generateCommitMessage"
114 | "generatePrContent"
115 | "generateBranchName"
116 | "generateThreadTitle";
117​
118const resolveInstance = (
119 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
120 operation: TextGenerationOp,
121 instanceId: ProviderInstanceId,
122): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
123 registry.getInstance(instanceId).pipe(
124 Effect.flatMap((instance) =>
125 instance
126 ? Effect.succeed(instance.textGeneration)
127 : Effect.fail(
128 new TextGenerationError({
129 operation,
130 detail: `No provider instance registered for id '${instanceId}'.`,
131 }),
132 ),
133 ),
134 );
135​
136/** @public Service construction is part of the canonical Effect module API. */
137export const make = Effect.gen(function* () {
138 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
139 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
140 return TextGeneration.of({
141 generateCommitMessage: (input) =>
142 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
143 Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
144 ),
145 generatePrContent: (input) =>
146 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
147 Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
148 ),
149 generateBranchName: (input) =>
150 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
151 Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
152 ),
153 generateThreadTitle: (input) =>
154 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
155 Effect.flatMap((textGeneration) =>
156 Effect.gen(function* () {
157 const linkedContext =
158 input.linkedContext ??
159 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
160 Effect.provideService(
161 SourceControlProviderRegistry.SourceControlProviderRegistry,
162 sourceControl,
163 ),
164 ));
165 return yield* textGeneration.generateThreadTitle({ ...input, linkedContext });
166 }),
167 ),
168 ),
169 });
170});
171​
172export const layer = Layer.effect(TextGeneration, make);