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

TextGeneration.ts

230 lines9.0 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type { ModelSelection, ProjectId, ProviderInstanceId } from "@t3tools/contracts";
5import {
6 isProviderAvailable,
7 isProviderTextGenerationCapable,
8 resolveProjectProviderInstanceEnabled,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11import { exhaustedUsageWindow } from "@t3tools/shared/usageLimits";
12import {
13 defaultTextGenerationModel,
14 fallbackTextGenerationModelSelection,
15} from "@t3tools/shared/serverSettings";
16import * as ServerSettings from "../serverSettings.ts";
17import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration";
18import { TextGenerationError } from "@t3tools/contracts";
19​
20import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
21import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
22import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
23import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
24import * as ThreadTitleLinks from "./ThreadTitleLinks.ts";
25import * as TextGenerators from "./TextGenerators.ts";
26import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations";
27​
28export type {
29 BranchNameGenerationInput,
30 BranchNameGenerationResult,
31 CommitMessageGenerationInput,
32 CommitMessageGenerationResult,
33 PrContentGenerationInput,
34 PrContentGenerationResult,
35 ReviewGuideGenerationInput,
36 ReviewGuideGenerationResult,
37 ThreadTitleGenerationInput,
38 ThreadTitleGenerationResult,
39 TranscriptionPostProcessingInput,
40 TranscriptionPostProcessingResult,
41} from "@t3tools/provider-core/server/textGeneration";
42​
43export class TextGeneration extends Context.Service<TextGeneration, ProviderTextGeneration>()(
44 "t3/textGeneration/TextGeneration",
45) {}
46​
47type TextGenerationOp =
48 | "generateCommitMessage"
49 | "generatePrContent"
50 | "generateBranchName"
51 | "generateThreadTitle"
52 | "generateReviewGuide"
53 | "generateTranscriptionPostProcessing";
54​
55const resolveInstance = (
56 registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
57 operation: TextGenerationOp,
58 instanceId: ProviderInstanceId,
59): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
60 registry.getInstance(instanceId).pipe(
61 Effect.flatMap((instance) =>
62 instance
63 ? Effect.succeed(instance.textGeneration)
64 : Effect.fail(
65 new TextGenerationError({
66 operation,
67 detail: `No provider instance registered for id '${instanceId}'.`,
68 }),
69 ),
70 ),
71 );
72​
73// Review guides investigate the checkout read-only; the other drivers reject that request.
74const INVESTIGATING_DRIVERS = new Set(["codex", "claudeAgent"]);
75​
76/** @public */
77export const make = Effect.gen(function* () {
78 const registry = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
79 const sourceControl = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
80 const serverSettings = yield* ServerSettings.ServerSettingsService;
81 const installationGate = yield* ProviderInstallationGate.ProviderInstallationGate;
82 const textGenerators = yield* TextGenerators.TextGenerators;
83 const generate = Effect.fn("TextGeneration.generate")(function* <A>(
84 operation: TextGenerationOp,
85 selection: ModelSelection,
86 projectId: ProjectId | null | undefined,
87 invoke: (
88 service: ProviderTextGeneration,
89 modelSelection: ModelSelection,
90 ) => Effect.Effect<A, TextGenerationError>,
91 ) {
92 const textGenerator = yield* textGenerators.runner(selection.instanceId);
93 // A review guide reaches a text generator only through a fallback; it needs an agent instead.
94 const textGeneratorId = selection.instanceId;
95 const textGeneratorCannotRun =
96 textGenerator !== undefined && operation === "generateReviewGuide";
97 if (textGenerator !== undefined && !textGeneratorCannotRun) {
98 return yield* invoke(
99 TextGenerationOperations.fromRunner("TextGenerator", textGenerator),
100 selection,
101 );
102 }
103 const snapshots = yield* registry.listInstances.pipe(
104 Effect.flatMap((instances) =>
105 Effect.forEach(instances, (instance) => instance.snapshot.getSnapshot),
106 ),
107 );
108 const now = DateTime.toEpochMillis(yield* DateTime.now);
109 const selected = snapshots.find((provider) => provider.instanceId === selection.instanceId);
110 // Only a failed probe counts: a pending one is also not installed yet.
111 const unusable =
112 textGeneratorCannotRun ||
113 (selected?.enabled === true &&
114 selected.status === "error" &&
115 (!selected.installed || selected.auth.status === "unauthenticated"));
116 if (unusable || exhaustedUsageWindow(selected, now, selection.model)) {
117 const settings = yield* serverSettings.getSettings.pipe(
118 Effect.mapError(
119 (cause) =>
120 new TextGenerationError({
121 operation,
122 detail: "Could not read fallback model settings.",
123 cause,
124 }),
125 ),
126 );
127 selection =
128 fallbackTextGenerationModelSelection(settings.providerInstances, (instanceId, driver) => {
129 const provider = snapshots.find((candidate) => candidate.instanceId === instanceId);
130 return (
131 instanceId !== selection.instanceId &&
132 provider?.enabled === true &&
133 resolveProjectProviderInstanceEnabled(settings, projectId ?? null, instanceId) &&
134 provider.installed &&
135 provider.auth.status !== "unauthenticated" &&
136 provider.status !== "error" &&
137 isProviderAvailable(provider) &&
138 isProviderTextGenerationCapable(provider) &&
139 (operation !== "generateReviewGuide" || INVESTIGATING_DRIVERS.has(driver)) &&
140 !exhaustedUsageWindow(provider, now, defaultTextGenerationModel(driver))
141 );
142 }) ?? selection;
143 }
144 if (textGeneratorCannotRun && selection.instanceId === textGeneratorId) {
145 return yield* new TextGenerationError({
146 operation,
147 detail:
148 "Review guides need a coding agent that can read the checkout. None is ready. Pick Codex or Claude as the review guide model in Settings → Source control.",
149 });
150 }
151 return yield* installationGate.execute(
152 selection.instanceId,
153 resolveInstance(registry, operation, selection.instanceId).pipe(
154 Effect.flatMap((service) => invoke(service, selection)),
155 ),
156 );
157 });
158 return TextGeneration.of({
159 generateCommitMessage: Effect.fn("TextGeneration.generateCommitMessage")(function (input) {
160 return generate(
161 "generateCommitMessage",
162 input.modelSelection,
163 input.projectId,
164 (service, modelSelection) => service.generateCommitMessage({ ...input, modelSelection }),
165 );
166 }),
167 generatePrContent: Effect.fn("TextGeneration.generatePrContent")(function (input) {
168 return generate(
169 "generatePrContent",
170 input.modelSelection,
171 input.projectId,
172 (service, modelSelection) => service.generatePrContent({ ...input, modelSelection }),
173 );
174 }),
175 generateBranchName: Effect.fn("TextGeneration.generateBranchName")(function (input) {
176 return generate(
177 "generateBranchName",
178 input.modelSelection,
179 input.projectId,
180 (service, modelSelection) => service.generateBranchName({ ...input, modelSelection }),
181 );
182 }),
183 generateReviewGuide: Effect.fn("TextGeneration.generateReviewGuide")(function (input) {
184 return generate(
185 "generateReviewGuide",
186 input.modelSelection,
187 input.projectId,
188 (service, modelSelection) => service.generateReviewGuide({ ...input, modelSelection }),
189 );
190 }),
191 generateTranscriptionPostProcessing: Effect.fn(
192 "TextGeneration.generateTranscriptionPostProcessing",
193 )(function (input) {
194 return generate(
195 "generateTranscriptionPostProcessing",
196 input.modelSelection,
197 null,
198 (service, modelSelection) =>
199 service.generateTranscriptionPostProcessing({ ...input, modelSelection }),
200 );
201 }),
202 generateThreadTitle: Effect.fn("TextGeneration.generateThreadTitle")(function (input) {
203 return generate(
204 "generateThreadTitle",
205 input.modelSelection,
206 input.projectId,
207 (textGeneration, modelSelection) =>
208 Effect.gen(function* () {
209 const linkedContext =
210 input.linkedContext ??
211 (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe(
212 Effect.provideService(
213 SourceControlProviderRegistry.SourceControlProviderRegistry,
214 sourceControl,
215 ),
216 ));
217 return yield* textGeneration.generateThreadTitle({
218 ...input,
219 linkedContext,
220 modelSelection,
221 });
222 }),
223 );
224 }),
225 });
226});
227​
228export const layer = Layer.effect(TextGeneration, make).pipe(
229 Layer.provideMerge(TextGenerators.layer),
230);