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