Open History to see the last commit
1/**
2 * Resolves the transcript directories the usage scan walks.
3 *
4 * Every configured provider instance contributes its own directory. A second
5 * Claude instance pointed at `~/.claude-work` writes transcripts there, not
6 * under `~/.claude`, so scanning only the default instance under-reports.
7 * Instances that resolve to the same directory collapse to one entry so a
8 * shared home is never counted twice.
9 *
10 * @module usageTranscriptDirs
11 */
12import {
13 ClaudeSettings,
14 CodexSettings,
15 ProviderDriverKind,
16 type ServerSettings,
17 type UsageProviderKind,
18} from "@t3tools/contracts";
19import * as Effect from "effect/Effect";
20import * as FileSystem from "effect/FileSystem";
21import * as Option from "effect/Option";
22import * as Path from "effect/Path";
23import * as Schema from "effect/Schema";
24
25import { resolveClaudeHomePath } from "../provider/Drivers/ClaudeHome.ts";
26import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
27import { deriveProviderInstanceConfigMap } from "../provider/Layers/ProviderInstanceRegistryHydration.ts";
28
29export interface UsageTranscriptDir {
30 readonly provider: UsageProviderKind;
31 readonly dir: string;
32}
33
34const CLAUDE_DRIVER_KIND = ProviderDriverKind.make("claudeAgent");
35const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
36
37const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);
38const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);
39
40/**
41 * Claude's config dir is the home itself when overridden, but a default
42 * install nests transcripts under `~/.claude/projects`. Probe both.
43 */
44const resolveClaudeTranscriptDir = Effect.fn("resolveClaudeTranscriptDir")(function* (
45 homePath: string,
46): Effect.fn.Return<string, never, Path.Path | FileSystem.FileSystem> {
47 const path = yield* Path.Path;
48 const fileSystem = yield* FileSystem.FileSystem;
49 const nested = path.join(homePath, ".claude", "projects");
50 const nestedExists = yield* fileSystem
51 .exists(nested)
52 .pipe(Effect.catchCause(() => Effect.succeed(false)));
53 return nestedExists ? nested : path.join(homePath, "projects");
54});
55
56/**
57 * One transcript directory per distinct provider home. Walks the same merged
58 * instance map the registry hydrates from, so explicit `providerInstances`
59 * entries and the legacy `providers.<kind>` mirrors are both covered.
60 * Instances whose config fails to decode are skipped; the registry already
61 * reports those as unavailable.
62 */
63export const resolveUsageTranscriptDirs = Effect.fn("resolveUsageTranscriptDirs")(function* (
64 settings: ServerSettings,
65): Effect.fn.Return<ReadonlyArray<UsageTranscriptDir>, never, Path.Path | FileSystem.FileSystem> {
66 const path = yield* Path.Path;
67 const dirs: UsageTranscriptDir[] = [];
68 const seen = new Set<string>();
69 const add = (provider: UsageProviderKind, dir: string) => {
70 const key = `${provider}\0${dir}`;
71 if (seen.has(key)) return;
72 seen.add(key);
73 dirs.push({ provider, dir });
74 };
75
76 for (const instance of Object.values(deriveProviderInstanceConfigMap(settings))) {
77 if (instance.driver === CLAUDE_DRIVER_KIND) {
78 const config = decodeClaudeSettings(instance.config ?? {});
79 if (Option.isNone(config)) continue;
80 const homePath = yield* resolveClaudeHomePath(config.value);
81 add("claude", yield* resolveClaudeTranscriptDir(homePath));
82 } else if (instance.driver === CODEX_DRIVER_KIND) {
83 const config = decodeCodexSettings(instance.config ?? {});
84 if (Option.isNone(config)) continue;
85 const layout = yield* resolveCodexHomeLayout(config.value);
86 add("codex", path.join(layout.sharedHomePath, "sessions"));
87 }
88 }
89
90 return dirs;
91});