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

ThreadMetadataMcpService.ts

230 lines7.0 KB

1import {
2 CommandId,
3 OrchestratorMcpFailure,
4 type OrchestrationV2AppThread,
5 type OrchestrationV2Command,
6 type ThreadMetadataMcpAction,
7 type ThreadMetadataMcpUpdateInput,
8 type ThreadMetadataMcpUpdateResult,
9 ThreadId,
10} from "@t3tools/contracts";
11import * as Context from "effect/Context";
12import * as Crypto from "effect/Crypto";
13import * as DateTime from "effect/DateTime";
14import * as Effect from "effect/Effect";
15import * as Layer from "effect/Layer";
16​
17import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
18import type { McpInvocationScope } from "./McpInvocationContext.ts";
19​
20export class ThreadMetadataMcpService extends Context.Service<
21 ThreadMetadataMcpService,
22 {
23 readonly update: (
24 scope: McpInvocationScope,
25 input: ThreadMetadataMcpUpdateInput,
26 ) => Effect.Effect<ThreadMetadataMcpUpdateResult, OrchestratorMcpFailure>;
27 }
28>()("t3/mcp/ThreadMetadataMcpService") {}
29​
30function failure(code: OrchestratorMcpFailure["code"], message: string): OrchestratorMcpFailure {
31 return new OrchestratorMcpFailure({ code, message });
33​
34function errorMessage(error: unknown): string {
35 return error instanceof Error ? error.message : String(error);
37​
38function threadLookupFailure(
39 error: ThreadManagementService.ThreadManagementError,
40): OrchestratorMcpFailure {
41 return error._tag === "ThreadManagementThreadNotFoundError"
42 ? failure("thread_not_found", error.message)
43 : failure("orchestration_error", error.message);
45​
46function stablePart(value: string): string {
47 return encodeURIComponent(value);
49​
50function commandId(input: {
51 readonly scope: McpInvocationScope;
52 readonly threadId: ThreadId;
53 readonly action: ThreadMetadataMcpAction;
54 readonly requestKey: string;
55}): CommandId {
56 return CommandId.make(
57 [
58 "command",
59 "mcp",
60 stablePart(input.scope.providerSessionId),
61 "thread-update",
62 stablePart(input.threadId),
63 stablePart(input.action),
64 stablePart(input.requestKey),
65 ].join(":"),
66 );
68​
69function metadataCommand(input: {
70 readonly commandId: CommandId;
71 readonly threadId: ThreadId;
72 readonly projectId: OrchestrationV2AppThread["projectId"];
73 readonly update: ThreadMetadataMcpUpdateInput;
74}): Extract<OrchestrationV2Command, { readonly type: "thread.metadata.update" }> {
75 switch (input.update.action) {
76 case "rename":
77 return {
78 type: "thread.metadata.update",
79 commandId: input.commandId,
80 threadId: input.threadId,
81 title: input.update.title!,
82 };
83 case "regenerate_title":
84 return {
85 type: "thread.metadata.update",
86 commandId: input.commandId,
87 threadId: input.threadId,
88 regenerateTitle: true,
89 };
90 case "link_pull_request":
91 return {
92 type: "thread.metadata.update",
93 commandId: input.commandId,
94 threadId: input.threadId,
95 linkedPullRequest: {
96 projectId: input.projectId,
97 ...input.update.pullRequest!,
98 },
99 };
100 case "unlink_pull_request":
101 return {
102 type: "thread.metadata.update",
103 commandId: input.commandId,
104 threadId: input.threadId,
105 linkedPullRequest: null,
106 };
107 }
109​
110function resultFromThread(input: {
111 readonly action: ThreadMetadataMcpAction;
112 readonly commandId: CommandId;
113 readonly sequence: number;
114 readonly thread: OrchestrationV2AppThread;
115}): ThreadMetadataMcpUpdateResult {
116 return {
117 threadId: input.thread.id,
118 action: input.action,
119 commandId: input.commandId,
120 sequence: input.sequence,
121 title: input.thread.title,
122 titleRegeneration:
123 input.thread.titleRegeneration === undefined || input.thread.titleRegeneration === null
124 ? null
125 : {
126 requestId: input.thread.titleRegeneration.requestId,
127 startedAt: DateTime.formatIso(input.thread.titleRegeneration.startedAt),
128 },
129 linkedPullRequest: input.thread.linkedPullRequest ?? null,
130 updatedAt: DateTime.formatIso(input.thread.updatedAt),
131 };
133​
134const make = Effect.gen(function* () {
135 const crypto = yield* Crypto.Crypto;
136 const threadManagement = yield* ThreadManagementService.ThreadManagementService;
137​
138 const update = Effect.fn("ThreadMetadataMcpService.update")(function* (
139 scope: McpInvocationScope,
140 input: ThreadMetadataMcpUpdateInput,
141 ) {
142 if (!scope.capabilities.has("orchestration")) {
143 return yield* failure(
144 "capability_denied",
145 "This MCP credential does not grant orchestration capabilities.",
146 );
147 }
148​
149 const parentShell = yield* threadManagement
150 .getThreadShell(scope.threadId)
151 .pipe(
152 Effect.mapError((error) =>
153 failure(
154 "orchestration_error",
155 `Unable to locate calling thread ${scope.threadId}: ${errorMessage(error)}`,
156 ),
157 ),
158 );
159 if (parentShell === null) {
160 return yield* failure("thread_not_found", `Calling thread ${scope.threadId} was not found.`);
161 }
162 const parent = yield* threadManagement
163 .getThreadRecords(scope.threadId, [])
164 .pipe(
165 Effect.mapError((error) =>
166 failure(
167 "orchestration_error",
168 `Unable to read calling thread ${scope.threadId}: ${errorMessage(error)}`,
169 ),
170 ),
171 );
172 const threadId = input.threadId ?? scope.threadId;
173 const target =
174 threadId === scope.threadId
175 ? parent
176 : yield* threadManagement
177 .getProjectThreadRecords({ projectId: parent.thread.projectId, threadId }, [])
178 .pipe(Effect.mapError(threadLookupFailure));
179 const requestKey =
180 input.clientRequestId === undefined
181 ? yield* crypto.randomUUIDv4.pipe(Effect.orDie)
182 : input.clientRequestId;
183 const updateCommandId = commandId({
184 scope,
185 threadId,
186 action: input.action,
187 requestKey,
188 });
189 const dispatched = yield* threadManagement
190 .dispatch(
191 metadataCommand({
192 commandId: updateCommandId,
193 threadId,
194 projectId: target.thread.projectId,
195 update: input,
196 }),
197 )
198 .pipe(
199 Effect.mapError((error) =>
200 failure(
201 "orchestration_error",
202 `Unable to ${input.action} for thread ${threadId}: ${errorMessage(error)}`,
203 ),
204 ),
205 );
206 const metadataEvent = dispatched.storedEvents.find(
207 (stored) => stored.event.type === "thread.metadata-updated",
208 );
209 if (metadataEvent === undefined || metadataEvent.event.type !== "thread.metadata-updated") {
210 return yield* failure(
211 "orchestration_error",
212 `Thread ${threadId} metadata update completed without a resultant state.`,
213 );
214 }
215 return resultFromThread({
216 action: input.action,
217 commandId: updateCommandId,
218 sequence: dispatched.sequence,
219 thread: metadataEvent.event.payload,
220 });
221 });
222​
223 return ThreadMetadataMcpService.of({ update });
224});
225​
226export const layer: Layer.Layer<
227 ThreadMetadataMcpService,
228 never,
229 Crypto.Crypto | ThreadManagementService.ThreadManagementService
230> = Layer.effect(ThreadMetadataMcpService, make);