Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4import * as SchemaTransformation from "effect/SchemaTransformation";
5
6export const TrimmedString = Schema.String.pipe(
7 Schema.decodeTo(
8 Schema.String,
9 SchemaTransformation.transformEffect({
10 decode: (value) => Effect.succeed(value.trim()),
11 encode: (value) => Effect.succeed(value.trim()),
12 }),
13 ),
14);
15export const TrimmedNonEmptyString = TrimmedString.check(Schema.isNonEmpty());
16
17export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
18export const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
19export const PortSchema = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }));
20
21/**
22 * Safe categories for a failed DPoP proof. These describe the class of failure
23 * without exposing proof contents or server-side authentication details.
24 */
25export const DpopFailureReason = Schema.Literals([
26 "time_window",
27 "key_mismatch",
28 "request_mismatch",
29 "token_mismatch",
30 "replay",
31 "invalid_proof",
32]);
33export type DpopFailureReason = typeof DpopFailureReason.Type;
34
35export const IsoDateTime = Schema.String;
36export type IsoDateTime = typeof IsoDateTime.Type;
37
38/**
39 * Wire codec for server→client arrays whose element unions grow over time
40 * (new literal members, new struct variants). Decoding drops elements the
41 * current build cannot decode instead of failing the whole payload — a client
42 * has to keep decoding configs sent by servers newer than itself, and
43 * rejecting the payload would take down the connection over data the client
44 * couldn't act on anyway. Encoding is the plain array encoding.
45 */
46/**
47 * Same idea for one optional value whose literal set grows over time: a
48 * member this build does not know decodes as absent rather than failing the
49 * enclosing struct. Encoding is the plain encoding.
50 */
51export const ForwardCompatibleOptional = <Value extends Schema.Top>(value: Value) => {
52 const decodeValue = Schema.decodeUnknownOption(value as never);
53 return Schema.optionalKey(
54 Schema.Unknown.pipe(
55 Schema.decodeTo(
56 Schema.UndefinedOr(value),
57 SchemaTransformation.transform<Value["Encoded"] | undefined, unknown>({
58 decode: (raw) =>
59 Option.isSome(decodeValue(raw)) ? (raw as Value["Encoded"]) : undefined,
60 encode: (raw) => raw,
61 }),
62 ),
63 ),
64 );
65};
66
67/**
68 * The nullable form, for a persisted setting whose literal set grows over
69 * time: a member this build does not know (or a missing key) decodes as null
70 * rather than failing the enclosing struct. Encoding is the plain encoding.
71 */
72export const ForwardCompatibleNullable = <Value extends Schema.Top>(value: Value) => {
73 const decodeValue = Schema.decodeUnknownOption(value as never);
74 return Schema.Unknown.pipe(
75 Schema.decodeTo(
76 Schema.NullOr(value),
77 SchemaTransformation.transform<Value["Encoded"] | null, unknown>({
78 decode: (raw) => (Option.isSome(decodeValue(raw)) ? (raw as Value["Encoded"]) : null),
79 encode: (raw) => raw,
80 }),
81 ),
82 );
83};
84
85/**
86 * A nullable setting whose null is "unset" and never crosses the wire: it
87 * decodes from a missing or unknown key and encodes back to a missing key.
88 * For a field that older clients decode as a required literal, so a null
89 * on the wire would fail their whole settings snapshot.
90 */
91export const OmittedWhenNull = <Value extends Schema.Top>(value: Value) => {
92 const decodeValue = Schema.decodeUnknownOption(value as never);
93 return Schema.optionalKey(Schema.Unknown).pipe(
94 Schema.decodeTo(
95 Schema.NullOr(value),
96 SchemaTransformation.transformOptional<Value["Encoded"] | null, unknown>({
97 decode: (raw) =>
98 Option.some(
99 Option.isSome(raw) && Option.isSome(decodeValue(raw.value))
100 ? (raw.value as Value["Encoded"])
101 : null,
102 ),
103 encode: (raw) =>
104 Option.isSome(raw) && raw.value !== null ? Option.some(raw.value) : Option.none(),
105 }),
106 ),
107 );
108};
109
110export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
111 const decodeElement = Schema.decodeUnknownOption(element as never);
112 return Schema.Array(Schema.Unknown).pipe(
113 Schema.decodeTo(
114 Schema.Array(element),
115 SchemaTransformation.transform<ReadonlyArray<Element["Encoded"]>, ReadonlyArray<unknown>>({
116 decode: (values) =>
117 values.filter((value) => Option.isSome(decodeElement(value))) as ReadonlyArray<
118 Element["Encoded"]
119 >,
120 encode: (values) => values,
121 }),
122 ),
123 );
124};
125
126/**
127 * Construct a branded identifier. Enforces non-empty trimmed strings
128 */
129const makeEntityId = <Brand extends string>(brand: Brand) => {
130 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
131};
132
133export const ThreadId = makeEntityId("ThreadId");
134export type ThreadId = typeof ThreadId.Type;
135export const ProjectId = makeEntityId("ProjectId");
136export type ProjectId = typeof ProjectId.Type;
137export const EnvironmentId = makeEntityId("EnvironmentId");
138export type EnvironmentId = typeof EnvironmentId.Type;
139export const CommandId = makeEntityId("CommandId");
140export type CommandId = typeof CommandId.Type;
141export const EventId = makeEntityId("EventId");
142export type EventId = typeof EventId.Type;
143export const MessageId = makeEntityId("MessageId");
144export type MessageId = typeof MessageId.Type;
145export const TurnId = makeEntityId("TurnId");
146export type TurnId = typeof TurnId.Type;
147export const AuthSessionId = makeEntityId("AuthSessionId");
148export type AuthSessionId = typeof AuthSessionId.Type;
149export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
150export type RpcClientId = typeof RpcClientId.Type;
151
152/**
153 * Which client surface a connection or command comes from. Unlike
154 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
155 * desktop are both "desktop"), this names the actual product surface.
156 * Optional everywhere it appears: old clients never send it.
157 */
158export const ClientSurface = Schema.Literals(["web", "desktop", "mobile", "cli"]);
159export type ClientSurface = typeof ClientSurface.Type;
160
161export const ClientOs = Schema.Literals([
162 "macOS",
163 "Windows",
164 "Linux",
165 "iOS",
166 "Android",
167 "ChromeOS",
168 "other",
169 "unknown",
170]);
171export type ClientOs = typeof ClientOs.Type;
172
173export const ClientDeviceType = Schema.Literals(["desktop", "phone", "tablet", "unknown"]);
174export type ClientDeviceType = typeof ClientDeviceType.Type;
175
176export const ClientWebDeployment = Schema.Literals(["hosted", "server"]);
177export type ClientWebDeployment = typeof ClientWebDeployment.Type;
178
179export const ClientConnectionMethod = Schema.Literals(["direct", "ssh", "relay", "unknown"]);
180export type ClientConnectionMethod = typeof ClientConnectionMethod.Type;
181
182export const ProviderItemId = makeEntityId("ProviderItemId");
183export type ProviderItemId = typeof ProviderItemId.Type;
184export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
185export type RuntimeSessionId = typeof RuntimeSessionId.Type;
186export const RuntimeItemId = makeEntityId("RuntimeItemId");
187export type RuntimeItemId = typeof RuntimeItemId.Type;
188export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
189export type RuntimeRequestId = typeof RuntimeRequestId.Type;
190export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
191export type RuntimeTaskId = typeof RuntimeTaskId.Type;
192export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
193export type ApprovalRequestId = typeof ApprovalRequestId.Type;
194export const CheckpointRef = makeEntityId("CheckpointRef");
195export type CheckpointRef = typeof CheckpointRef.Type;