Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadIssueService.ts

109 lines3.5 KB

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 */
8import {
9 CommandId,
10 threadIssueKeysEqual,
11 type IssueRef,
12 type ThreadId,
13 type ThreadIssueKey,
14 type ThreadIssueLinkSource,
15} from "@t3tools/contracts";
16import * as Context from "effect/Context";
17import * as SecureRandom from "effect/crypto/SecureRandom";
18import * as Effect from "effect/Effect";
19import * as Layer from "effect/Layer";
20​
21import * as Orchestrator from "./Orchestrator.ts";
22​
23export interface LinkThreadIssueInput {
24 readonly threadId: ThreadId;
25 readonly ref: IssueRef & { readonly host: string };
26 readonly url: string;
27 readonly displayKey: string;
28 readonly title: string;
29 readonly source: ThreadIssueLinkSource;
31​
32export interface UnlinkThreadIssueInput {
33 readonly threadId: ThreadId;
34 readonly key: ThreadIssueKey;
36​
37export class ThreadIssueService extends Context.Service<
38 ThreadIssueService,
39 {
40 /** Linking a linked issue is a no-op. */
41 readonly link: (
42 input: LinkThreadIssueInput,
43 ) => Effect.Effect<{ readonly alreadyLinked: boolean }, Orchestrator.OrchestratorV2Error>;
44 readonly unlink: (
45 input: UnlinkThreadIssueInput,
46 ) => Effect.Effect<{ readonly wasLinked: boolean }, Orchestrator.OrchestratorV2Error>;
47 }
48>()("t3/orchestration-v2/ThreadIssueService") {}
49​
50const make = Effect.gen(function* () {
51 const orchestrator = yield* Orchestrator.OrchestratorV2;
52 const random = yield* SecureRandom.SecureRandom;
53​
54 const commandId = (tag: string, threadId: ThreadId) =>
55 random.uuidV4().pipe(
56 Effect.orDie,
57 Effect.map((uuid) => CommandId.make(`server:thread-issue-${tag}:${threadId}:${uuid}`)),
58 );
59​
60 const isLinked = (threadId: ThreadId, key: ThreadIssueKey) =>
61 orchestrator
62 .getThreadShell(threadId)
63 .pipe(
64 Effect.map(
65 (thread) => thread?.issues?.some((link) => threadIssueKeysEqual(link, key)) === true,
66 ),
67 );
68​
69 const link: ThreadIssueService["Service"]["link"] = Effect.fn("ThreadIssueService.link")(
70 function* (input) {
71 if (yield* isLinked(input.threadId, input.ref)) return { alreadyLinked: true };
72 yield* orchestrator.dispatch({
73 type: "thread.issue.link",
74 commandId: yield* commandId("link", input.threadId),
75 threadId: input.threadId,
76 tracker: input.ref.tracker,
77 host: input.ref.host,
78 scope: input.ref.scope,
79 key: input.ref.key,
80 projectId: input.ref.projectId,
81 url: input.url,
82 displayKey: input.displayKey,
83 title: input.title,
84 source: input.source,
85 });
86 return { alreadyLinked: false };
87 },
88 );
89​
90 const unlink: ThreadIssueService["Service"]["unlink"] = Effect.fn("ThreadIssueService.unlink")(
91 function* (input) {
92 if (!(yield* isLinked(input.threadId, input.key))) return { wasLinked: false };
93 yield* orchestrator.dispatch({
94 type: "thread.issue.unlink",
95 commandId: yield* commandId("unlink", input.threadId),
96 threadId: input.threadId,
97 tracker: input.key.tracker,
98 host: input.key.host,
99 scope: input.key.scope,
100 key: input.key.key,
101 });
102 return { wasLinked: true };
103 },
104 );
105​
106 return ThreadIssueService.of({ link, unlink });
107});
108​
109export const layer = Layer.effect(ThreadIssueService, make);