1import * as Schema from "effect/Schema";
2
3import {
4 AuthSessionId,
5 EnvironmentId,
6 RpcClientId,
7 ThreadId,
8 TrimmedNonEmptyString,
9} from "./baseSchemas.ts";
10import { ProviderInstanceId } from "./providerInstance.ts";
11
12export const BackgroundBooleanState = Schema.Literals(["true", "false", "unknown"]);
13export type BackgroundBooleanState = typeof BackgroundBooleanState.Type;
14
15export const HostPowerThermalState = Schema.Literals([
16 "unknown",
17 "nominal",
18 "fair",
19 "serious",
20 "critical",
21]);
22export type HostPowerThermalState = typeof HostPowerThermalState.Type;
23
24export const HostPowerSource = Schema.Literals([
25 "unknown",
26 "node-macos-shell",
27 "node-macos-native",
28 "node-linux",
29 "node-windows",
30 "electron-main",
31]);
32export type HostPowerSource = typeof HostPowerSource.Type;
33
34export const HostPowerSnapshot = Schema.Struct({
35 source: HostPowerSource,
36 idle: BackgroundBooleanState,
37 idleSeconds: Schema.NullOr(Schema.Number),
38 locked: BackgroundBooleanState,
39 suspended: Schema.Boolean,
40 onBattery: BackgroundBooleanState,
41 lowPowerMode: BackgroundBooleanState,
42 thermalState: HostPowerThermalState,
43 stale: Schema.Boolean,
44 updatedAt: Schema.DateTimeUtc,
45});
46export type HostPowerSnapshot = typeof HostPowerSnapshot.Type;
47
48export const BackgroundScope = Schema.Union([
49 Schema.Struct({ type: Schema.Literal("server-config") }),
50 Schema.Struct({
51 type: Schema.Literal("provider-status"),
52 instanceId: Schema.optionalKey(ProviderInstanceId),
53 }),
54 Schema.Struct({ type: Schema.Literal("vcs-status"), cwd: Schema.String }),
55 Schema.Struct({ type: Schema.Literal("git-refs"), cwd: Schema.String }),
56 Schema.Struct({ type: Schema.Literal("diagnostics") }),
57 Schema.Struct({ type: Schema.Literal("thread"), threadId: ThreadId }),
58]);
59export type BackgroundScope = typeof BackgroundScope.Type;
60
61export const ClientKind = Schema.Literals(["web", "desktop-renderer", "mobile", "unknown"]);
62export type ClientKind = typeof ClientKind.Type;
63
64export const ClientActivityClientId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
65export type ClientActivityClientId = typeof ClientActivityClientId.Type;
66
67export const ClientActivityReportInput = Schema.Struct({
68 environmentId: Schema.optionalKey(EnvironmentId),
69 clientId: ClientActivityClientId,
70 clientKind: ClientKind,
71 visible: Schema.Boolean,
72 focused: Schema.Boolean,
73 recentlyInteracted: Schema.Boolean,
74 appState: Schema.optionalKey(Schema.Literals(["active", "inactive", "background", "unknown"])),
75 lowPowerMode: Schema.optionalKey(BackgroundBooleanState),
76 batteryState: Schema.optionalKey(Schema.Literals(["unknown", "unplugged", "charging", "full"])),
77 networkType: Schema.optionalKey(Schema.String),
78 scopes: Schema.Array(BackgroundScope),
79 ttlMs: Schema.optionalKey(Schema.Number),
80 observedAt: Schema.DateTimeUtc,
81});
82export type ClientActivityReportInput = typeof ClientActivityReportInput.Type;
83
84export const ClientActivityLease = Schema.Struct({
85 sessionId: AuthSessionId,
86 rpcClientId: RpcClientId,
87 clientId: ClientActivityClientId,
88 clientKind: ClientKind,
89 visible: Schema.Boolean,
90 focused: Schema.Boolean,
91 recentlyInteracted: Schema.Boolean,
92 appState: Schema.optionalKey(Schema.Literals(["active", "inactive", "background", "unknown"])),
93 lowPowerMode: Schema.optionalKey(BackgroundBooleanState),
94 batteryState: Schema.optionalKey(Schema.Literals(["unknown", "unplugged", "charging", "full"])),
95 networkType: Schema.optionalKey(Schema.String),
96 scopes: Schema.Array(BackgroundScope),
97 updatedAt: Schema.DateTimeUtc,
98 expiresAt: Schema.DateTimeUtc,
99});
100export type ClientActivityLease = typeof ClientActivityLease.Type;
101
102export const BackgroundPolicySnapshot = Schema.Struct({
103 hostPower: HostPowerSnapshot,
104 leases: Schema.Array(ClientActivityLease),
105 activeForegroundLeaseCount: Schema.Number,
106 activeScopeKeys: Schema.Array(Schema.String),
107 shouldRunOpportunisticWork: Schema.Boolean,
108 updatedAt: Schema.DateTimeUtc,
109});
110export type BackgroundPolicySnapshot = typeof BackgroundPolicySnapshot.Type;