Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { IsoDateTime, NonNegativeInt, ProjectId, TrimmedNonEmptyString } from "./baseSchemas.ts";
3import { ProviderInstanceId } from "./providerInstance.ts";
4
5/** Coding agent home directories the scanner knows how to read. */
6export const AgentSessionSource = Schema.Literals(["claudeAgent", "codex"]);
7export type AgentSessionSource = typeof AgentSessionSource.Type;
8
9/** File identity saved with an imported session so bounded retries can skip unchanged history. */
10export const AgentSessionImportSource = Schema.Struct({
11 provider: AgentSessionSource,
12 providerInstanceId: ProviderInstanceId,
13 providerSessionId: TrimmedNonEmptyString,
14 filePath: TrimmedNonEmptyString,
15 size: NonNegativeInt,
16 mtimeMs: Schema.NullOr(Schema.Number),
17 device: Schema.Number,
18 inode: Schema.NullOr(Schema.Number),
19 birthtimeMs: Schema.NullOr(Schema.Number),
20});
21export type AgentSessionImportSource = typeof AgentSessionImportSource.Type;
22
23/** Imported message ids retain their origin after event metadata is projected into SQLite. */
24export function isImportedAgentSessionMessageId(messageId: string): boolean {
25 return messageId.startsWith("import:");
26}
27
28/**
29 * Empty for now. Kept as a struct so future scan options (source filters,
30 * explicit roots) can be added without a new method.
31 */
32export const AgentSessionScanInput = Schema.Struct({});
33export type AgentSessionScanInput = typeof AgentSessionScanInput.Type;
34
35/**
36 * A directory that at least one agent CLI has run in, suitable for import as a
37 * T3 Code project. `alreadyImported` marks candidates that already have an
38 * active project rooted at the same path.
39 */
40/**
41 * Git identity of a candidate directory, read from `.git/config` without
42 * spawning git. `remoteKey` is the normalized origin URL, shared by every
43 * clone of the same repository so the client can group them. `repository`
44 * is the GitHub `owner/name` when the origin is on GitHub.
45 */
46export const AgentSessionProjectGit = Schema.Struct({
47 remoteKey: Schema.NullOr(Schema.String),
48 repository: Schema.NullOr(Schema.String),
49});
50export type AgentSessionProjectGit = typeof AgentSessionProjectGit.Type;
51
52export const AgentSessionProjectCandidate = Schema.Struct({
53 path: TrimmedNonEmptyString,
54 title: TrimmedNonEmptyString,
55 projectId: Schema.optional(ProjectId),
56 sources: Schema.Array(AgentSessionSource),
57 threadCount: NonNegativeInt,
58 lastActiveAt: Schema.NullOr(IsoDateTime),
59 alreadyImported: Schema.Boolean,
60 /**
61 * `null` when the directory is not the root of a git repository. Missing on
62 * servers that predate the git scan, where the client cannot tell repositories
63 * from plain folders and should treat every candidate as a standalone project.
64 */
65 git: Schema.optionalKey(Schema.NullOr(AgentSessionProjectGit)),
66});
67export type AgentSessionProjectCandidate = typeof AgentSessionProjectCandidate.Type;
68
69export const AgentSessionScanResult = Schema.Struct({
70 candidates: Schema.Array(AgentSessionProjectCandidate),
71 scannedAt: IsoDateTime,
72 truncated: Schema.optional(Schema.Boolean),
73});
74export type AgentSessionScanResult = typeof AgentSessionScanResult.Type;
75
76export const AgentSessionImportInput = Schema.Struct({
77 projectId: ProjectId,
78 expectedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
79});
80export type AgentSessionImportInput = typeof AgentSessionImportInput.Type;
81
82export class AgentSessionImportProjectNotFoundError extends Schema.TaggedError<AgentSessionImportProjectNotFoundError>()(
83 "AgentSessionImportProjectNotFoundError",
84 { projectId: ProjectId },
85) {
86 override get message(): string {
87 return `Project '${this.projectId}' does not exist.`;
88 }
89}
90
91export class AgentSessionImportProjectChangedError extends Schema.TaggedError<AgentSessionImportProjectChangedError>()(
92 "AgentSessionImportProjectChangedError",
93 { projectId: ProjectId },
94) {
95 override get message(): string {
96 return `Project '${this.projectId}' changed directories. Scan for projects again before importing history.`;
97 }
98}
99
100export const AgentSessionImportResult = Schema.Struct({
101 importedCount: NonNegativeInt,
102 skippedCount: NonNegativeInt,
103});
104export type AgentSessionImportResult = typeof AgentSessionImportResult.Type;
105
106export class AgentSessionScanError extends Schema.TaggedError<AgentSessionScanError>()(
107 "AgentSessionScanError",
108 {
109 operation: Schema.Literals(["read-settings", "read-projects"]),
110 cause: Schema.Defect(),
111 },
112) {
113 override get message(): string {
114 return `Failed to scan agent sessions during ${this.operation}.`;
115 }
116}