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