Open History to see the last commit
1/**
2 * RoutingTextGeneration – Dispatches text generation requests to either the
3 * Codex CLI or Claude CLI implementation based on the provider in each
4 * request input.
5 *
6 * When `modelSelection.provider` is `"claudeAgent"` the request is forwarded to
7 * the Claude layer; for any other value (including the default `undefined`) it
8 * falls through to the Codex layer.
9 *
10 * @module RoutingTextGeneration
11 */
12import { Effect, Layer, Context } from "effect";
13
14import {
15 TextGeneration,
16 type TextGenerationProvider,
17 type TextGenerationShape,
18} from "../Services/TextGeneration.ts";
19import { CodexTextGenerationLive } from "./CodexTextGeneration.ts";
20import { ClaudeTextGenerationLive } from "./ClaudeTextGeneration.ts";
21import { CursorTextGenerationLive } from "./CursorTextGeneration.ts";
22import { OpenCodeTextGenerationLive } from "./OpenCodeTextGeneration.ts";
23
24// ---------------------------------------------------------------------------
25// Internal service tags so both concrete layers can coexist.
26// ---------------------------------------------------------------------------
27
28class CodexTextGen extends Context.Service<CodexTextGen, TextGenerationShape>()(
29 "t3/git/Layers/RoutingTextGeneration/CodexTextGen",
30) {}
31
32class ClaudeTextGen extends Context.Service<ClaudeTextGen, TextGenerationShape>()(
33 "t3/git/Layers/RoutingTextGeneration/ClaudeTextGen",
34) {}
35
36class CursorTextGen extends Context.Service<CursorTextGen, TextGenerationShape>()(
37 "t3/git/Layers/RoutingTextGeneration/CursorTextGen",
38) {}
39
40class OpenCodeTextGen extends Context.Service<OpenCodeTextGen, TextGenerationShape>()(
41 "t3/git/Layers/RoutingTextGeneration/OpenCodeTextGen",
42) {}
43
44// ---------------------------------------------------------------------------
45// Routing implementation
46// ---------------------------------------------------------------------------
47
48const makeRoutingTextGeneration = Effect.gen(function* () {
49 const codex = yield* CodexTextGen;
50 const claude = yield* ClaudeTextGen;
51 const cursor = yield* CursorTextGen;
52 const openCode = yield* OpenCodeTextGen;
53
54 const route = (provider?: TextGenerationProvider): TextGenerationShape =>
55 provider === "claudeAgent"
56 ? claude
57 : provider === "opencode"
58 ? openCode
59 : provider === "cursor"
60 ? cursor
61 : codex;
62
63 return {
64 generateCommitMessage: (input) =>
65 route(input.modelSelection.provider).generateCommitMessage(input),
66 generatePrContent: (input) => route(input.modelSelection.provider).generatePrContent(input),
67 generateBranchName: (input) => route(input.modelSelection.provider).generateBranchName(input),
68 generateThreadTitle: (input) => route(input.modelSelection.provider).generateThreadTitle(input),
69 } satisfies TextGenerationShape;
70});
71
72const InternalCodexLayer = Layer.effect(
73 CodexTextGen,
74 Effect.gen(function* () {
75 const svc = yield* TextGeneration;
76 return svc;
77 }),
78).pipe(Layer.provide(CodexTextGenerationLive));
79
80const InternalClaudeLayer = Layer.effect(
81 ClaudeTextGen,
82 Effect.gen(function* () {
83 const svc = yield* TextGeneration;
84 return svc;
85 }),
86).pipe(Layer.provide(ClaudeTextGenerationLive));
87
88const InternalCursorLayer = Layer.effect(
89 CursorTextGen,
90 Effect.gen(function* () {
91 const svc = yield* TextGeneration;
92 return svc;
93 }),
94).pipe(Layer.provide(CursorTextGenerationLive));
95
96const InternalOpenCodeLayer = Layer.effect(
97 OpenCodeTextGen,
98 Effect.gen(function* () {
99 const svc = yield* TextGeneration;
100 return svc;
101 }),
102).pipe(Layer.provide(OpenCodeTextGenerationLive));
103
104export const RoutingTextGenerationLive = Layer.effect(
105 TextGeneration,
106 makeRoutingTextGeneration,
107).pipe(
108 Layer.provide(InternalCodexLayer),
109 Layer.provide(InternalClaudeLayer),
110 Layer.provide(InternalCursorLayer),
111 Layer.provide(InternalOpenCodeLayer),
112);