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

baseSchemas.ts

198 lines8.2 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.transformOrFail({
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​
85export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
86 const decodeElement = Schema.decodeUnknownOption(element as never);
87 return Schema.Array(Schema.Unknown).pipe(
88 Schema.decodeTo(
89 Schema.Array(element),
90 SchemaTransformation.transform<ReadonlyArray<Element["Encoded"]>, ReadonlyArray<unknown>>({
91 decode: (values) =>
92 values.filter((value) => Option.isSome(decodeElement(value))) as ReadonlyArray<
93 Element["Encoded"]
94 >,
95 encode: (values) => values,
96 }),
97 ),
98 );
99};
100​
101/**
102 * Construct a branded identifier. Enforces non-empty trimmed strings
103 */
104const makeEntityId = <Brand extends string>(brand: Brand) => {
105 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
106};
107​
108export const ThreadId = makeEntityId("ThreadId");
109export type ThreadId = typeof ThreadId.Type;
110export const ProjectId = makeEntityId("ProjectId");
111export type ProjectId = typeof ProjectId.Type;
112export const EnvironmentId = makeEntityId("EnvironmentId");
113export type EnvironmentId = typeof EnvironmentId.Type;
114export const CommandId = makeEntityId("CommandId");
115export type CommandId = typeof CommandId.Type;
116export const EventId = makeEntityId("EventId");
117export type EventId = typeof EventId.Type;
118export const MessageId = makeEntityId("MessageId");
119export type MessageId = typeof MessageId.Type;
120export const TurnId = makeEntityId("TurnId");
121export type TurnId = typeof TurnId.Type;
122export const RunId = makeEntityId("RunId");
123export type RunId = typeof RunId.Type;
124export const RunAttemptId = makeEntityId("RunAttemptId");
125export type RunAttemptId = typeof RunAttemptId.Type;
126export const NodeId = makeEntityId("NodeId");
127export type NodeId = typeof NodeId.Type;
128export const AuthSessionId = makeEntityId("AuthSessionId");
129export type AuthSessionId = typeof AuthSessionId.Type;
130export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
131export type RpcClientId = typeof RpcClientId.Type;
132​
133/**
134 * Which client surface a connection or command comes from. Unlike
135 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
136 * desktop are both "desktop"), this names the actual product surface.
137 * Optional everywhere it appears: old clients never send it.
138 */
139export const ClientSurface = Schema.Literals(["web", "desktop", "mobile", "cli"]);
140export type ClientSurface = typeof ClientSurface.Type;
141​
142export const ClientOs = Schema.Literals([
143 "macOS",
144 "Windows",
145 "Linux",
146 "iOS",
147 "Android",
148 "ChromeOS",
149 "other",
150 "unknown",
151]);
152export type ClientOs = typeof ClientOs.Type;
153​
154export const ClientDeviceType = Schema.Literals(["desktop", "phone", "tablet", "unknown"]);
155export type ClientDeviceType = typeof ClientDeviceType.Type;
156​
157export const ClientWebDeployment = Schema.Literals(["hosted", "server"]);
158export type ClientWebDeployment = typeof ClientWebDeployment.Type;
159​
160export const ClientConnectionMethod = Schema.Literals(["direct", "ssh", "relay", "unknown"]);
161export type ClientConnectionMethod = typeof ClientConnectionMethod.Type;
162​
163export const ProviderItemId = makeEntityId("ProviderItemId");
164export type ProviderItemId = typeof ProviderItemId.Type;
165export const ProviderSessionId = makeEntityId("ProviderSessionId");
166export type ProviderSessionId = typeof ProviderSessionId.Type;
167export const ProviderThreadId = makeEntityId("ProviderThreadId");
168export type ProviderThreadId = typeof ProviderThreadId.Type;
169export const ProviderTurnId = makeEntityId("ProviderTurnId");
170export type ProviderTurnId = typeof ProviderTurnId.Type;
171export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
172export type RuntimeSessionId = typeof RuntimeSessionId.Type;
173export const RuntimeItemId = makeEntityId("RuntimeItemId");
174export type RuntimeItemId = typeof RuntimeItemId.Type;
175export const TurnItemId = makeEntityId("TurnItemId");
176export type TurnItemId = typeof TurnItemId.Type;
177export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
178export type RuntimeRequestId = typeof RuntimeRequestId.Type;
179export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
180export type RuntimeTaskId = typeof RuntimeTaskId.Type;
181export const ScheduledTaskId = makeEntityId("ScheduledTaskId");
182export type ScheduledTaskId = typeof ScheduledTaskId.Type;
183export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
184export type ApprovalRequestId = typeof ApprovalRequestId.Type;
185export const CheckpointRef = makeEntityId("CheckpointRef");
186export type CheckpointRef = typeof CheckpointRef.Type;
187export const CheckpointId = makeEntityId("CheckpointId");
188export type CheckpointId = typeof CheckpointId.Type;
189export const CheckpointScopeId = makeEntityId("CheckpointScopeId");
190export type CheckpointScopeId = typeof CheckpointScopeId.Type;
191export const ContextHandoffId = makeEntityId("ContextHandoffId");
192export type ContextHandoffId = typeof ContextHandoffId.Type;
193export const ContextTransferId = makeEntityId("ContextTransferId");
194export type ContextTransferId = typeof ContextTransferId.Type;
195export const RawEventId = makeEntityId("RawEventId");
196export type RawEventId = typeof RawEventId.Type;
197export const PlanId = makeEntityId("PlanId");
198export type PlanId = typeof PlanId.Type;