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 RunId = makeEntityId("RunId");
84export type RunId = typeof RunId.Type;
85export const RunAttemptId = makeEntityId("RunAttemptId");
86export type RunAttemptId = typeof RunAttemptId.Type;
87export const NodeId = makeEntityId("NodeId");
88export type NodeId = typeof NodeId.Type;
89export const AuthSessionId = makeEntityId("AuthSessionId");
90export type AuthSessionId = typeof AuthSessionId.Type;
91export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
92export type RpcClientId = typeof RpcClientId.Type;
93
94/**
95 * Which client surface a connection or command comes from. Unlike
96 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
97 * desktop are both "desktop"), this names the actual product surface.
98 * Optional everywhere it appears: old clients never send it.
99 */
100export const ClientSurface = Schema.Literals(["web", "desktop", "mobile", "cli"]);
101export type ClientSurface = typeof ClientSurface.Type;
102
103export const ClientOs = Schema.Literals([
104 "macOS",
105 "Windows",
106 "Linux",
107 "iOS",
108 "Android",
109 "ChromeOS",
110 "other",
111 "unknown",
112]);
113export type ClientOs = typeof ClientOs.Type;
114
115export const ClientDeviceType = Schema.Literals(["desktop", "phone", "tablet", "unknown"]);
116export type ClientDeviceType = typeof ClientDeviceType.Type;
117
118export const ClientWebDeployment = Schema.Literals(["hosted", "server"]);
119export type ClientWebDeployment = typeof ClientWebDeployment.Type;
120
121export const ClientConnectionMethod = Schema.Literals(["direct", "ssh", "relay", "unknown"]);
122export type ClientConnectionMethod = typeof ClientConnectionMethod.Type;
123
124export const ProviderItemId = makeEntityId("ProviderItemId");
125export type ProviderItemId = typeof ProviderItemId.Type;
126export const ProviderSessionId = makeEntityId("ProviderSessionId");
127export type ProviderSessionId = typeof ProviderSessionId.Type;
128export const ProviderThreadId = makeEntityId("ProviderThreadId");
129export type ProviderThreadId = typeof ProviderThreadId.Type;
130export const ProviderTurnId = makeEntityId("ProviderTurnId");
131export type ProviderTurnId = typeof ProviderTurnId.Type;
132export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
133export type RuntimeSessionId = typeof RuntimeSessionId.Type;
134export const RuntimeItemId = makeEntityId("RuntimeItemId");
135export type RuntimeItemId = typeof RuntimeItemId.Type;
136export const TurnItemId = makeEntityId("TurnItemId");
137export type TurnItemId = typeof TurnItemId.Type;
138export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
139export type RuntimeRequestId = typeof RuntimeRequestId.Type;
140export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
141export type RuntimeTaskId = typeof RuntimeTaskId.Type;
142export const ScheduledTaskId = makeEntityId("ScheduledTaskId");
143export type ScheduledTaskId = typeof ScheduledTaskId.Type;
144export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
145export type ApprovalRequestId = typeof ApprovalRequestId.Type;
146export const CheckpointRef = makeEntityId("CheckpointRef");
147export type CheckpointRef = typeof CheckpointRef.Type;
148export const CheckpointId = makeEntityId("CheckpointId");
149export type CheckpointId = typeof CheckpointId.Type;
150export const CheckpointScopeId = makeEntityId("CheckpointScopeId");
151export type CheckpointScopeId = typeof CheckpointScopeId.Type;
152export const ContextHandoffId = makeEntityId("ContextHandoffId");
153export type ContextHandoffId = typeof ContextHandoffId.Type;
154export const ContextTransferId = makeEntityId("ContextTransferId");
155export type ContextTransferId = typeof ContextTransferId.Type;
156export const RawEventId = makeEntityId("RawEventId");
157export type RawEventId = typeof RawEventId.Type;
158export const PlanId = makeEntityId("PlanId");
159export type PlanId = typeof PlanId.Type;