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.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 */
46export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
47 const decodeElement = Schema.decodeUnknownOption(element as never);
48 return Schema.Array(Schema.Unknown).pipe(
49 Schema.decodeTo(
50 Schema.Array(element),
51 SchemaTransformation.transform<ReadonlyArray<Element["Encoded"]>, ReadonlyArray<unknown>>({
52 decode: (values) =>
53 values.filter((value) => Option.isSome(decodeElement(value))) as ReadonlyArray<
54 Element["Encoded"]
55 >,
56 encode: (values) => values,
57 }),
58 ),
59 );
60};
61
62/**
63 * Construct a branded identifier. Enforces non-empty trimmed strings
64 */
65const makeEntityId = <Brand extends string>(brand: Brand) => {
66 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
67};
68
69export const ThreadId = makeEntityId("ThreadId");
70export type ThreadId = typeof ThreadId.Type;
71export const ProjectId = makeEntityId("ProjectId");
72export type ProjectId = typeof ProjectId.Type;
73export const EnvironmentId = makeEntityId("EnvironmentId");
74export type EnvironmentId = typeof EnvironmentId.Type;
75export const CommandId = makeEntityId("CommandId");
76export type CommandId = typeof CommandId.Type;
77export const EventId = makeEntityId("EventId");
78export type EventId = typeof EventId.Type;
79export const MessageId = makeEntityId("MessageId");
80export type MessageId = typeof MessageId.Type;
81export const TurnId = makeEntityId("TurnId");
82export type TurnId = typeof TurnId.Type;
83export const AuthSessionId = makeEntityId("AuthSessionId");
84export type AuthSessionId = typeof AuthSessionId.Type;
85export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
86export type RpcClientId = typeof RpcClientId.Type;
87
88/**
89 * Which client surface a connection or command comes from. Unlike
90 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
91 * desktop are both "desktop"), this names the actual product surface.
92 * Optional everywhere it appears: old clients never send it.
93 */
94export const ClientSurface = Schema.Literals(["web", "desktop", "mobile", "cli"]);
95export type ClientSurface = typeof ClientSurface.Type;
96
97export const ClientOs = Schema.Literals([
98 "macOS",
99 "Windows",
100 "Linux",
101 "iOS",
102 "Android",
103 "ChromeOS",
104 "other",
105 "unknown",
106]);
107export type ClientOs = typeof ClientOs.Type;
108
109export const ClientDeviceType = Schema.Literals(["desktop", "phone", "tablet", "unknown"]);
110export type ClientDeviceType = typeof ClientDeviceType.Type;
111
112export const ClientWebDeployment = Schema.Literals(["hosted", "server"]);
113export type ClientWebDeployment = typeof ClientWebDeployment.Type;
114
115export const ClientConnectionMethod = Schema.Literals(["direct", "ssh", "relay", "unknown"]);
116export type ClientConnectionMethod = typeof ClientConnectionMethod.Type;
117
118export const ProviderItemId = makeEntityId("ProviderItemId");
119export type ProviderItemId = typeof ProviderItemId.Type;
120export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
121export type RuntimeSessionId = typeof RuntimeSessionId.Type;
122export const RuntimeItemId = makeEntityId("RuntimeItemId");
123export type RuntimeItemId = typeof RuntimeItemId.Type;
124export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
125export type RuntimeRequestId = typeof RuntimeRequestId.Type;
126export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
127export type RuntimeTaskId = typeof RuntimeTaskId.Type;
128export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
129export type ApprovalRequestId = typeof ApprovalRequestId.Type;
130export const CheckpointRef = makeEntityId("CheckpointRef");
131export type CheckpointRef = typeof CheckpointRef.Type;