The best way to control Claude Code, Codex and more from any computer or phone

main
ts 121 lines 4.06 KB
Raw
1 /**
2 * Issues linked to threads: the link and unlink commands the server's own surfaces (MCP) go
3 * through. A link records how the tracker named the issue when it was made; nothing refreshes
4 * it, and opening the issue reads its live state.
5 *
6 * @module orchestration-v2/ThreadIssueService
7 */
8 import {
9 CommandId,
10 threadIssueKeysEqual,
11 type IssueLinkedThreadsResult,
12 type IssueOperationError,
13 type IssueRef,
14 type ThreadId,
15 type ThreadIssueKey,
16 type ThreadIssueLinkSource,
17 } from "@t3tools/contracts";
18 import * as Context from "effect/Context";
19 import * as SecureRandom from "effect/crypto/SecureRandom";
20 import * as Effect from "effect/Effect";
21 import * as Layer from "effect/Layer";
22 import * as SqlClient from "effect/sql/SqlClient";
23
24 import { listLinkedIssueThreads } from "../issue/linkedThreads.ts";
25 import * as Orchestrator from "./Orchestrator.ts";
26
27 export interface LinkThreadIssueInput {
28 readonly threadId: ThreadId;
29 readonly ref: IssueRef & { readonly host: string };
30 readonly url: string;
31 readonly displayKey: string;
32 readonly title: string;
33 readonly source: ThreadIssueLinkSource;
34 }
35
36 export interface UnlinkThreadIssueInput {
37 readonly threadId: ThreadId;
38 readonly key: ThreadIssueKey;
39 }
40
41 export class ThreadIssueService extends Context.Service<
42 ThreadIssueService,
43 {
44 /** Linking a linked issue is a no-op. */
45 readonly link: (
46 input: LinkThreadIssueInput,
47 ) => Effect.Effect<{ readonly alreadyLinked: boolean }, Orchestrator.OrchestratorV2Error>;
48 readonly unlink: (
49 input: UnlinkThreadIssueInput,
50 ) => Effect.Effect<{ readonly wasLinked: boolean }, Orchestrator.OrchestratorV2Error>;
51 /** Every thread the issue is linked to, most recently active first. */
52 readonly linkedThreads: (
53 key: ThreadIssueKey,
54 ) => Effect.Effect<IssueLinkedThreadsResult, IssueOperationError>;
55 }
56 >()("t3/orchestration-v2/ThreadIssueService") {}
57
58 const make = Effect.gen(function* () {
59 const orchestrator = yield* Orchestrator.OrchestratorV2;
60 const random = yield* SecureRandom.SecureRandom;
61 const sql = yield* SqlClient.SqlClient;
62
63 const commandId = (tag: string, threadId: ThreadId) =>
64 random.uuidV4().pipe(
65 Effect.orDie,
66 Effect.map((uuid) => CommandId.make(`server:thread-issue-${tag}:${threadId}:${uuid}`)),
67 );
68
69 const isLinked = (threadId: ThreadId, key: ThreadIssueKey) =>
70 orchestrator
71 .getThreadShell(threadId)
72 .pipe(
73 Effect.map(
74 (thread) => thread?.issues?.some((link) => threadIssueKeysEqual(link, key)) === true,
75 ),
76 );
77
78 const link: ThreadIssueService["Service"]["link"] = Effect.fn("ThreadIssueService.link")(
79 function* (input) {
80 if (yield* isLinked(input.threadId, input.ref)) return { alreadyLinked: true };
81 yield* orchestrator.dispatch({
82 type: "thread.issue.link",
83 commandId: yield* commandId("link", input.threadId),
84 threadId: input.threadId,
85 tracker: input.ref.tracker,
86 host: input.ref.host,
87 scope: input.ref.scope,
88 key: input.ref.key,
89 projectId: input.ref.projectId,
90 url: input.url,
91 displayKey: input.displayKey,
92 title: input.title,
93 source: input.source,
94 });
95 return { alreadyLinked: false };
96 },
97 );
98
99 const unlink: ThreadIssueService["Service"]["unlink"] = Effect.fn("ThreadIssueService.unlink")(
100 function* (input) {
101 if (!(yield* isLinked(input.threadId, input.key))) return { wasLinked: false };
102 yield* orchestrator.dispatch({
103 type: "thread.issue.unlink",
104 commandId: yield* commandId("unlink", input.threadId),
105 threadId: input.threadId,
106 tracker: input.key.tracker,
107 host: input.key.host,
108 scope: input.key.scope,
109 key: input.key.key,
110 });
111 return { wasLinked: true };
112 },
113 );
114
115 const linkedThreads: ThreadIssueService["Service"]["linkedThreads"] = (key) =>
116 listLinkedIssueThreads(key).pipe(Effect.provideService(SqlClient.SqlClient, sql));
117
118 return ThreadIssueService.of({ link, unlink, linkedThreads });
119 });
120
121 export const layer = Layer.effect(ThreadIssueService, make);