+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
231 lines
7.0 KiB
TypeScript
231 lines
7.0 KiB
TypeScript
import {
|
|
CommandId,
|
|
OrchestratorMcpFailure,
|
|
type OrchestrationV2AppThread,
|
|
type OrchestrationV2Command,
|
|
type ThreadMetadataMcpAction,
|
|
type ThreadMetadataMcpUpdateInput,
|
|
type ThreadMetadataMcpUpdateResult,
|
|
ThreadId,
|
|
} from "@t3tools/contracts";
|
|
import * as Context from "effect/Context";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
|
|
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
|
|
import type { McpInvocationScope } from "./McpInvocationContext.ts";
|
|
|
|
export class ThreadMetadataMcpService extends Context.Service<
|
|
ThreadMetadataMcpService,
|
|
{
|
|
readonly update: (
|
|
scope: McpInvocationScope,
|
|
input: ThreadMetadataMcpUpdateInput,
|
|
) => Effect.Effect<ThreadMetadataMcpUpdateResult, OrchestratorMcpFailure>;
|
|
}
|
|
>()("t3/mcp/ThreadMetadataMcpService") {}
|
|
|
|
function failure(code: OrchestratorMcpFailure["code"], message: string): OrchestratorMcpFailure {
|
|
return new OrchestratorMcpFailure({ code, message });
|
|
}
|
|
|
|
function errorMessage(error: unknown): string {
|
|
return error instanceof Error ? error.message : String(error);
|
|
}
|
|
|
|
function threadLookupFailure(
|
|
error: ThreadManagementService.ThreadManagementError,
|
|
): OrchestratorMcpFailure {
|
|
return error._tag === "ThreadManagementThreadNotFoundError"
|
|
? failure("thread_not_found", error.message)
|
|
: failure("orchestration_error", error.message);
|
|
}
|
|
|
|
function stablePart(value: string): string {
|
|
return encodeURIComponent(value);
|
|
}
|
|
|
|
function commandId(input: {
|
|
readonly scope: McpInvocationScope;
|
|
readonly threadId: ThreadId;
|
|
readonly action: ThreadMetadataMcpAction;
|
|
readonly requestKey: string;
|
|
}): CommandId {
|
|
return CommandId.make(
|
|
[
|
|
"command",
|
|
"mcp",
|
|
stablePart(input.scope.providerSessionId),
|
|
"thread-update",
|
|
stablePart(input.threadId),
|
|
stablePart(input.action),
|
|
stablePart(input.requestKey),
|
|
].join(":"),
|
|
);
|
|
}
|
|
|
|
function metadataCommand(input: {
|
|
readonly commandId: CommandId;
|
|
readonly threadId: ThreadId;
|
|
readonly projectId: OrchestrationV2AppThread["projectId"];
|
|
readonly update: ThreadMetadataMcpUpdateInput;
|
|
}): Extract<OrchestrationV2Command, { readonly type: "thread.metadata.update" }> {
|
|
switch (input.update.action) {
|
|
case "rename":
|
|
return {
|
|
type: "thread.metadata.update",
|
|
commandId: input.commandId,
|
|
threadId: input.threadId,
|
|
title: input.update.title!,
|
|
};
|
|
case "regenerate_title":
|
|
return {
|
|
type: "thread.metadata.update",
|
|
commandId: input.commandId,
|
|
threadId: input.threadId,
|
|
regenerateTitle: true,
|
|
};
|
|
case "link_pull_request":
|
|
return {
|
|
type: "thread.metadata.update",
|
|
commandId: input.commandId,
|
|
threadId: input.threadId,
|
|
linkedPullRequest: {
|
|
projectId: input.projectId,
|
|
...input.update.pullRequest!,
|
|
},
|
|
};
|
|
case "unlink_pull_request":
|
|
return {
|
|
type: "thread.metadata.update",
|
|
commandId: input.commandId,
|
|
threadId: input.threadId,
|
|
linkedPullRequest: null,
|
|
};
|
|
}
|
|
}
|
|
|
|
function resultFromThread(input: {
|
|
readonly action: ThreadMetadataMcpAction;
|
|
readonly commandId: CommandId;
|
|
readonly sequence: number;
|
|
readonly thread: OrchestrationV2AppThread;
|
|
}): ThreadMetadataMcpUpdateResult {
|
|
return {
|
|
threadId: input.thread.id,
|
|
action: input.action,
|
|
commandId: input.commandId,
|
|
sequence: input.sequence,
|
|
title: input.thread.title,
|
|
titleRegeneration:
|
|
input.thread.titleRegeneration === undefined || input.thread.titleRegeneration === null
|
|
? null
|
|
: {
|
|
requestId: input.thread.titleRegeneration.requestId,
|
|
startedAt: DateTime.formatIso(input.thread.titleRegeneration.startedAt),
|
|
},
|
|
linkedPullRequest: input.thread.linkedPullRequest ?? null,
|
|
updatedAt: DateTime.formatIso(input.thread.updatedAt),
|
|
};
|
|
}
|
|
|
|
const make = Effect.gen(function* () {
|
|
const crypto = yield* Crypto.Crypto;
|
|
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
|
|
|
|
const update = Effect.fn("ThreadMetadataMcpService.update")(function* (
|
|
scope: McpInvocationScope,
|
|
input: ThreadMetadataMcpUpdateInput,
|
|
) {
|
|
if (!scope.capabilities.has("orchestration")) {
|
|
return yield* failure(
|
|
"capability_denied",
|
|
"This MCP credential does not grant orchestration capabilities.",
|
|
);
|
|
}
|
|
|
|
const parentShell = yield* threadManagement
|
|
.getThreadShell(scope.threadId)
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
failure(
|
|
"orchestration_error",
|
|
`Unable to locate calling thread ${scope.threadId}: ${errorMessage(error)}`,
|
|
),
|
|
),
|
|
);
|
|
if (parentShell === null) {
|
|
return yield* failure("thread_not_found", `Calling thread ${scope.threadId} was not found.`);
|
|
}
|
|
const parent = yield* threadManagement
|
|
.getThreadRecords(scope.threadId, [])
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
failure(
|
|
"orchestration_error",
|
|
`Unable to read calling thread ${scope.threadId}: ${errorMessage(error)}`,
|
|
),
|
|
),
|
|
);
|
|
const threadId = input.threadId ?? scope.threadId;
|
|
const target =
|
|
threadId === scope.threadId
|
|
? parent
|
|
: yield* threadManagement
|
|
.getProjectThreadRecords({ projectId: parent.thread.projectId, threadId }, [])
|
|
.pipe(Effect.mapError(threadLookupFailure));
|
|
const requestKey =
|
|
input.clientRequestId === undefined
|
|
? yield* crypto.randomUUIDv4.pipe(Effect.orDie)
|
|
: input.clientRequestId;
|
|
const updateCommandId = commandId({
|
|
scope,
|
|
threadId,
|
|
action: input.action,
|
|
requestKey,
|
|
});
|
|
const dispatched = yield* threadManagement
|
|
.dispatch(
|
|
metadataCommand({
|
|
commandId: updateCommandId,
|
|
threadId,
|
|
projectId: target.thread.projectId,
|
|
update: input,
|
|
}),
|
|
)
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
failure(
|
|
"orchestration_error",
|
|
`Unable to ${input.action} for thread ${threadId}: ${errorMessage(error)}`,
|
|
),
|
|
),
|
|
);
|
|
const metadataEvent = dispatched.storedEvents.find(
|
|
(stored) => stored.event.type === "thread.metadata-updated",
|
|
);
|
|
if (metadataEvent === undefined || metadataEvent.event.type !== "thread.metadata-updated") {
|
|
return yield* failure(
|
|
"orchestration_error",
|
|
`Thread ${threadId} metadata update completed without a resultant state.`,
|
|
);
|
|
}
|
|
return resultFromThread({
|
|
action: input.action,
|
|
commandId: updateCommandId,
|
|
sequence: dispatched.sequence,
|
|
thread: metadataEvent.event.payload,
|
|
});
|
|
});
|
|
|
|
return ThreadMetadataMcpService.of({ update });
|
|
});
|
|
|
|
export const layer: Layer.Layer<
|
|
ThreadMetadataMcpService,
|
|
never,
|
|
Crypto.Crypto | ThreadManagementService.ThreadManagementService
|
|
> = Layer.effect(ThreadMetadataMcpService, make);
|