Skip to content
Source
Sign inSign up

background.ts

106 lines3.7 KB

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 scopes: Schema.Array(BackgroundScope),
77 ttlMs: Schema.optionalKey(Schema.Number),
78 observedAt: Schema.DateTimeUtc,
79});
80export type ClientActivityReportInput = typeof ClientActivityReportInput.Type;
81​
82export const ClientActivityLease = Schema.Struct({
83 sessionId: AuthSessionId,
84 rpcClientId: RpcClientId,
85 clientId: ClientActivityClientId,
86 clientKind: ClientKind,
87 visible: Schema.Boolean,
88 focused: Schema.Boolean,
89 recentlyInteracted: Schema.Boolean,
90 appState: Schema.optionalKey(Schema.Literals(["active", "inactive", "background", "unknown"])),
91 lowPowerMode: Schema.optionalKey(BackgroundBooleanState),
92 scopes: Schema.Array(BackgroundScope),
93 updatedAt: Schema.DateTimeUtc,
94 expiresAt: Schema.DateTimeUtc,
95});
96export type ClientActivityLease = typeof ClientActivityLease.Type;
97​
98export const BackgroundPolicySnapshot = Schema.Struct({
99 hostPower: HostPowerSnapshot,
100 leases: Schema.Array(ClientActivityLease),
101 activeForegroundLeaseCount: Schema.Number,
102 activeScopeKeys: Schema.Array(Schema.String),
103 shouldRunOpportunisticWork: Schema.Boolean,
104 updatedAt: Schema.DateTimeUtc,
105});
106export type BackgroundPolicySnapshot = typeof BackgroundPolicySnapshot.Type;