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

baseSchemas.ts

223 lines9.1 KB

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 RunId = makeEntityId("RunId");
148export type RunId = typeof RunId.Type;
149export const RunAttemptId = makeEntityId("RunAttemptId");
150export type RunAttemptId = typeof RunAttemptId.Type;
151export const NodeId = makeEntityId("NodeId");
152export type NodeId = typeof NodeId.Type;
153export const AuthSessionId = makeEntityId("AuthSessionId");
154export type AuthSessionId = typeof AuthSessionId.Type;
155export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
156export type RpcClientId = typeof RpcClientId.Type;
157​
158/**
159 * Which client surface a connection or command comes from. Unlike
160 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
161 * desktop are both "desktop"), this names the actual product surface.
162 * Optional everywhere it appears: old clients never send it.
163 */
164export const ClientSurface = Schema.Literals(["web", "desktop", "mobile", "cli"]);
165export type ClientSurface = typeof ClientSurface.Type;
166​
167export const ClientOs = Schema.Literals([
168 "macOS",
169 "Windows",
170 "Linux",
171 "iOS",
172 "Android",
173 "ChromeOS",
174 "other",
175 "unknown",
176]);
177export type ClientOs = typeof ClientOs.Type;
178​
179export const ClientDeviceType = Schema.Literals(["desktop", "phone", "tablet", "unknown"]);
180export type ClientDeviceType = typeof ClientDeviceType.Type;
181​
182export const ClientWebDeployment = Schema.Literals(["hosted", "server"]);
183export type ClientWebDeployment = typeof ClientWebDeployment.Type;
184​
185export const ClientConnectionMethod = Schema.Literals(["direct", "ssh", "relay", "unknown"]);
186export type ClientConnectionMethod = typeof ClientConnectionMethod.Type;
187​
188export const ProviderItemId = makeEntityId("ProviderItemId");
189export type ProviderItemId = typeof ProviderItemId.Type;
190export const ProviderSessionId = makeEntityId("ProviderSessionId");
191export type ProviderSessionId = typeof ProviderSessionId.Type;
192export const ProviderThreadId = makeEntityId("ProviderThreadId");
193export type ProviderThreadId = typeof ProviderThreadId.Type;
194export const ProviderTurnId = makeEntityId("ProviderTurnId");
195export type ProviderTurnId = typeof ProviderTurnId.Type;
196export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
197export type RuntimeSessionId = typeof RuntimeSessionId.Type;
198export const RuntimeItemId = makeEntityId("RuntimeItemId");
199export type RuntimeItemId = typeof RuntimeItemId.Type;
200export const TurnItemId = makeEntityId("TurnItemId");
201export type TurnItemId = typeof TurnItemId.Type;
202export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
203export type RuntimeRequestId = typeof RuntimeRequestId.Type;
204export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
205export type RuntimeTaskId = typeof RuntimeTaskId.Type;
206export const ScheduledTaskId = makeEntityId("ScheduledTaskId");
207export type ScheduledTaskId = typeof ScheduledTaskId.Type;
208export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
209export type ApprovalRequestId = typeof ApprovalRequestId.Type;
210export const CheckpointRef = makeEntityId("CheckpointRef");
211export type CheckpointRef = typeof CheckpointRef.Type;
212export const CheckpointId = makeEntityId("CheckpointId");
213export type CheckpointId = typeof CheckpointId.Type;
214export const CheckpointScopeId = makeEntityId("CheckpointScopeId");
215export type CheckpointScopeId = typeof CheckpointScopeId.Type;
216export const ContextHandoffId = makeEntityId("ContextHandoffId");
217export type ContextHandoffId = typeof ContextHandoffId.Type;
218export const ContextTransferId = makeEntityId("ContextTransferId");
219export type ContextTransferId = typeof ContextTransferId.Type;
220export const RawEventId = makeEntityId("RawEventId");
221export type RawEventId = typeof RawEventId.Type;
222export const PlanId = makeEntityId("PlanId");
223export type PlanId = typeof PlanId.Type;