Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentSessions.ts

98 lines3.7 KB

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:");
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 */
40export const AgentSessionProjectCandidate = Schema.Struct({
41 path: TrimmedNonEmptyString,
42 title: TrimmedNonEmptyString,
43 projectId: Schema.optional(ProjectId),
44 sources: Schema.Array(AgentSessionSource),
45 threadCount: NonNegativeInt,
46 lastActiveAt: Schema.NullOr(IsoDateTime),
47 alreadyImported: Schema.Boolean,
48});
49export type AgentSessionProjectCandidate = typeof AgentSessionProjectCandidate.Type;
50​
51export const AgentSessionScanResult = Schema.Struct({
52 candidates: Schema.Array(AgentSessionProjectCandidate),
53 scannedAt: IsoDateTime,
54 truncated: Schema.optional(Schema.Boolean),
55});
56export type AgentSessionScanResult = typeof AgentSessionScanResult.Type;
57​
58export const AgentSessionImportInput = Schema.Struct({
59 projectId: ProjectId,
60 expectedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
61});
62export type AgentSessionImportInput = typeof AgentSessionImportInput.Type;
63​
64export class AgentSessionImportProjectNotFoundError extends Schema.TaggedErrorClass<AgentSessionImportProjectNotFoundError>()(
65 "AgentSessionImportProjectNotFoundError",
66 { projectId: ProjectId },
67) {
68 override get message(): string {
69 return `Project '${this.projectId}' does not exist.`;
70 }
72​
73export class AgentSessionImportProjectChangedError extends Schema.TaggedErrorClass<AgentSessionImportProjectChangedError>()(
74 "AgentSessionImportProjectChangedError",
75 { projectId: ProjectId },
76) {
77 override get message(): string {
78 return `Project '${this.projectId}' changed directories. Scan for projects again before importing history.`;
79 }
81​
82export const AgentSessionImportResult = Schema.Struct({
83 importedCount: NonNegativeInt,
84 skippedCount: NonNegativeInt,
85});
86export type AgentSessionImportResult = typeof AgentSessionImportResult.Type;
87​
88export class AgentSessionScanError extends Schema.TaggedErrorClass<AgentSessionScanError>()(
89 "AgentSessionScanError",
90 {
91 operation: Schema.Literals(["read-settings", "read-projects"]),
92 cause: Schema.Defect(),
93 },
94) {
95 override get message(): string {
96 return `Failed to scan agent sessions during ${this.operation}.`;
97 }