1import * as Schema from "effect/Schema";
2import { OrchestrationV2ThreadShell } from "./orchestrationV2.ts";
3import { AssetResource } from "./assets.ts";
4import { OrchestrationProjectShell } from "./orchestrationProject.ts";
5import { EnvironmentId, ThreadId } from "./baseSchemas.ts";
6
7export const ArchiveObjectHash = Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/u));
8export const ArchiveObjectReceipt = Schema.Struct({
9 hash: ArchiveObjectHash,
10 size: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
11});
12export const ArchiveManagementRequest = Schema.Union([
13 Schema.Struct({ type: Schema.Literal("archive.status") }),
14 Schema.Struct({ type: Schema.Literal("archive.retry"), threadId: Schema.optionalKey(ThreadId) }),
15]);
16export type ArchiveManagementRequest = typeof ArchiveManagementRequest.Type;
17export const ArchiveStatus = Schema.Struct({
18 enabled: Schema.Boolean,
19 offloaded: Schema.Int,
20 pending: Schema.Int,
21 queue: Schema.Array(Schema.Struct({ threadId: ThreadId, nextAttemptAt: Schema.Number })),
22 storage: Schema.Struct({
23 allocatedDatabaseBytes: Schema.Number,
24 attachmentsBytes: Schema.Number,
25 nativeProviderLogBytes: Schema.Number,
26 }),
27 lastReclamation: Schema.NullOr(
28 Schema.Struct({
29 beforeAllocatedBytes: Schema.Number,
30 afterAllocatedBytes: Schema.Number,
31 }),
32 ),
33 failures: Schema.Array(
34 Schema.Struct({ threadId: ThreadId, attempts: Schema.Int, reason: Schema.String }),
35 ),
36});
37export type ArchiveStatus = typeof ArchiveStatus.Type;
38
39export const ArchiveAssetCreateUrlInput = Schema.Struct({
40 environmentId: EnvironmentId,
41 resource: AssetResource,
42});
43
44export const ArchivePublication = Schema.Struct({
45 environmentId: EnvironmentId,
46 manifestHash: ArchiveObjectHash,
47 project: OrchestrationProjectShell,
48});
49export type ArchivePublication = typeof ArchivePublication.Type;
50export const ArchiveCatalogEntry = Schema.Struct({
51 environmentId: EnvironmentId,
52 manifestHash: ArchiveObjectHash,
53 throughSequence: Schema.Number,
54 thread: OrchestrationV2ThreadShell,
55 project: OrchestrationProjectShell,
56});
57export type ArchiveCatalogEntry = typeof ArchiveCatalogEntry.Type;
58export const ArchiveCatalogResult = Schema.Struct({ entries: Schema.Array(ArchiveCatalogEntry) });
59
60export class ArchiveError extends Schema.TaggedError<ArchiveError>()("ArchiveError", {
61 reason: Schema.Literals(["unavailable", "integrity", "changed", "storage"]),
62 cause: Schema.optionalKey(Schema.Redacted(Schema.Defect())),
63}) {
64 override get message() {
65 switch (this.reason) {
66 case "unavailable":
67 return "The transcript archive is unavailable. Reconnect to its server and retry.";
68 case "integrity":
69 return "The archived transcript failed verification. Retry or check the archive storage.";
70 case "changed":
71 return "The chat changed while it was being archived. Its local copy was retained.";
72 case "storage":
73 return "The transcript archive could not be saved. Check its mounted storage and retry.";
74 }
75 }
76}