|
| 1 | +import { OrchestratorMcpFailure, ProjectId } from "@t3tools/contracts"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Option from "effect/Option"; |
| 4 | +import * as Project from "../../../project/ProjectService.ts"; |
| 5 | +import * as Repositories from "../../../sourceControl/SourceControlRepositoryService.ts"; |
| 6 | +import { newCommandId, readCaller, readMutationCaller, unavailable } from "../../threadAccess.ts"; |
| 7 | +import { ProjectToolkit } from "./tools.ts"; |
| 8 | + |
| 9 | +function projectFailure(error: Project.ProjectServiceError) { |
| 10 | + if (error._tag === "ProjectOperationError") return unavailable(); |
| 11 | + const message = |
| 12 | + error._tag === "ProjectNotFoundError" |
| 13 | + ? "The project was not found." |
| 14 | + : error._tag === "ProjectConflictError" |
| 15 | + ? "The workspace is already registered to a project." |
| 16 | + : "The project is not empty; force=true is required to delete it."; |
| 17 | + return new OrchestratorMcpFailure({ code: "invalid_request", message }); |
| 18 | +} |
| 19 | + |
| 20 | +const access = Effect.gen(function* () { |
| 21 | + yield* readCaller(); |
| 22 | + return yield* Project.ProjectService; |
| 23 | +}); |
| 24 | +const mutation = Effect.gen(function* () { |
| 25 | + const { caller } = yield* readMutationCaller(); |
| 26 | + if ( |
| 27 | + caller.archivedAt !== null || |
| 28 | + caller.runtimeMode !== "full-access" || |
| 29 | + caller.interactionMode !== "default" |
| 30 | + ) |
| 31 | + return yield* new OrchestratorMcpFailure({ |
| 32 | + code: "capability_denied", |
| 33 | + message: "Project changes require a live full-access/default calling thread.", |
| 34 | + }); |
| 35 | + return yield* Project.ProjectService; |
| 36 | +}); |
| 37 | +export const ProjectHandlersLive = ProjectToolkit.toLayer({ |
| 38 | + t3_project_list: (input) => |
| 39 | + Effect.gen(function* () { |
| 40 | + const projects = yield* access; |
| 41 | + const snapshot = yield* projects.snapshot.pipe(Effect.mapError(unavailable)); |
| 42 | + const rows = snapshot.projects.filter((project) => project.deletedAt === null); |
| 43 | + const start = input.cursor ?? 0, |
| 44 | + end = start + (input.limit ?? 20); |
| 45 | + return { projects: rows.slice(start, end), nextCursor: end < rows.length ? end : null }; |
| 46 | + }), |
| 47 | + t3_project_read: (input) => |
| 48 | + Effect.gen(function* () { |
| 49 | + const projects = yield* access; |
| 50 | + const result = yield* projects.getById(input.projectId).pipe(Effect.mapError(unavailable)); |
| 51 | + if (Option.isNone(result)) |
| 52 | + return yield* new OrchestratorMcpFailure({ |
| 53 | + code: "invalid_request", |
| 54 | + message: "The project was not found.", |
| 55 | + }); |
| 56 | + return result.value; |
| 57 | + }), |
| 58 | + t3_project_create: (input) => |
| 59 | + Effect.gen(function* () { |
| 60 | + const projects = yield* mutation; |
| 61 | + const commandId = yield* newCommandId(); |
| 62 | + return yield* projects |
| 63 | + .create({ ...input, commandId, projectId: ProjectId.make(commandId) }) |
| 64 | + .pipe(Effect.mapError(projectFailure)); |
| 65 | + }), |
| 66 | + t3_project_update: (input) => |
| 67 | + Effect.gen(function* () { |
| 68 | + const projects = yield* mutation; |
| 69 | + return yield* projects |
| 70 | + .update({ ...input, commandId: yield* newCommandId() }) |
| 71 | + .pipe(Effect.mapError(projectFailure)); |
| 72 | + }), |
| 73 | + t3_project_delete: (input) => |
| 74 | + Effect.gen(function* () { |
| 75 | + const projects = yield* mutation; |
| 76 | + return yield* projects |
| 77 | + .delete({ ...input, commandId: yield* newCommandId() }) |
| 78 | + .pipe(Effect.mapError(projectFailure)); |
| 79 | + }), |
| 80 | + t3_project_clone: (input) => |
| 81 | + Effect.gen(function* () { |
| 82 | + yield* mutation; |
| 83 | + const repositories = yield* Repositories.SourceControlRepositoryService; |
| 84 | + return yield* repositories.cloneRepository(input).pipe( |
| 85 | + Effect.mapError( |
| 86 | + (error) => |
| 87 | + new OrchestratorMcpFailure({ |
| 88 | + code: "orchestration_error", |
| 89 | + message: error.detail, |
| 90 | + }), |
| 91 | + ), |
| 92 | + ); |
| 93 | + }), |
| 94 | +}); |
0 commit comments