Open History to see the last commit
1/**
2 * TextGenerationLive — registry-backed implementation of the `TextGeneration`
3 * service tag.
4 *
5 * The `TextGeneration` tag is kept as a thin facade over
6 * `ProviderInstanceRegistry`. Every op pulls `modelSelection.instanceId`,
7 * looks up the matching `ProviderInstance`, and delegates to that instance's
8 * own `textGeneration` closure (built by its driver's `create()`).
9 *
10 * There is deliberately no per-driver dispatch here — the registry already
11 * knows which driver backs each instance, and each `ProviderInstance`
12 * carries the fully-bound `TextGenerationShape` produced by its driver.
13 * That means:
14 *
15 * - Multiple instances of the same driver (e.g. `codex_personal`,
16 * `codex_work`) each get their own text-generation closure bound to
17 * their own settings — the routing is by instance, not by driver.
18 * - Unknown or disabled instances surface a `TextGenerationError` with
19 * the missing `instanceId`, instead of silently falling back to a
20 * default.
21 *
22 * This replaces the old `RoutingTextGenerationLive`, which tried to route
23 * by driver-kind and misused `modelSelection.instanceId` as a driver-id
24 * literal.
25 *
26 * @module git/Layers/TextGenerationLive
27 */
28import { Effect, Layer } from "effect";
29
30import { TextGenerationError } from "@t3tools/contracts";
31import type { ProviderInstanceId } from "@t3tools/contracts";
32
33import {
34 ProviderInstanceRegistry,
35 type ProviderInstanceRegistryShape,
36} from "../../provider/Services/ProviderInstanceRegistry.ts";
37import type { ProviderInstance } from "../../provider/ProviderDriver.ts";
38import { TextGeneration, type TextGenerationShape } from "../Services/TextGeneration.ts";
39
40type TextGenerationOp =
41 | "generateCommitMessage"
42 | "generatePrContent"
43 | "generateBranchName"
44 | "generateThreadTitle";
45
46const resolveInstance = (
47 registry: ProviderInstanceRegistryShape,
48 operation: TextGenerationOp,
49 instanceId: ProviderInstanceId,
50): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
51 registry.getInstance(instanceId).pipe(
52 Effect.flatMap((instance) =>
53 instance
54 ? Effect.succeed(instance.textGeneration)
55 : Effect.fail(
56 new TextGenerationError({
57 operation,
58 detail: `No provider instance registered for id '${instanceId}'.`,
59 }),
60 ),
61 ),
62 );
63
64/**
65 * Build a `TextGenerationShape` that routes every call through the
66 * registry. Exposed separately from the Layer so tests can construct it
67 * against a stub registry without layering gymnastics.
68 */
69export const makeTextGenerationFromRegistry = (
70 registry: ProviderInstanceRegistryShape,
71): TextGenerationShape => ({
72 generateCommitMessage: (input) =>
73 resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
74 Effect.flatMap((tg) => tg.generateCommitMessage(input)),
75 ),
76 generatePrContent: (input) =>
77 resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
78 Effect.flatMap((tg) => tg.generatePrContent(input)),
79 ),
80 generateBranchName: (input) =>
81 resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
82 Effect.flatMap((tg) => tg.generateBranchName(input)),
83 ),
84 generateThreadTitle: (input) =>
85 resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
86 Effect.flatMap((tg) => tg.generateThreadTitle(input)),
87 ),
88});
89
90/**
91 * `TextGeneration` Layer wired to the `ProviderInstanceRegistry`. The rest
92 * of the server keeps using `yield* TextGeneration` — only the underlying
93 * wiring changed from kind-based routing to instance-based routing.
94 */
95export const TextGenerationLive = Layer.effect(
96 TextGeneration,
97 Effect.gen(function* () {
98 const registry = yield* ProviderInstanceRegistry;
99 return makeTextGenerationFromRegistry(registry);
100 }),
101);