1import type { ThreadId } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4
5import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
6import * as ProjectService from "./ProjectService.ts";
7
8/** The directory a thread works in, or undefined when the thread or its project is gone. */
9export const threadWorkspaceRoot = Effect.fn("threadWorkspaceRoot")(
10 function* (threadId: ThreadId) {
11 const threads = yield* ThreadManagementService.ThreadManagementService;
12 const projects = yield* ProjectService.ProjectService;
13 const shell = yield* threads.getThreadShell(threadId);
14 if (shell === null) return undefined;
15 if (shell.worktreePath) return shell.worktreePath;
16 return Option.getOrUndefined(yield* projects.getById(shell.projectId))?.workspaceRoot;
17 },
18 Effect.orElseSucceed(() => undefined),
19);