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
21export const IsoDateTime = Schema.String;
22export type IsoDateTime = typeof IsoDateTime.Type;
23
24/**
25 * Wire codec for server→client arrays whose element unions grow over time
26 * (new literal members, new struct variants). Decoding drops elements the
27 * current build cannot decode instead of failing the whole payload — a client
28 * has to keep decoding configs sent by servers newer than itself, and
29 * rejecting the payload would take down the connection over data the client
30 * couldn't act on anyway. Encoding is the plain array encoding.
31 */
32export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
33 const decodeElement = Schema.decodeUnknownOption(element as never);
34 return Schema.Array(Schema.Unknown).pipe(
35 Schema.decodeTo(
36 Schema.Array(element),
37 SchemaTransformation.transform<ReadonlyArray<Element["Encoded"]>, ReadonlyArray<unknown>>({
38 decode: (values) =>
39 values.filter((value) => Option.isSome(decodeElement(value))) as ReadonlyArray<
40 Element["Encoded"]
41 >,
42 encode: (values) => values,
43 }),
44 ),
45 );
46};
47
48/**
49 * Construct a branded identifier. Enforces non-empty trimmed strings
50 */
51const makeEntityId = <Brand extends string>(brand: Brand) => {
52 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
53};
54
55export const ThreadId = makeEntityId("ThreadId");
56export type ThreadId = typeof ThreadId.Type;
57export const ProjectId = makeEntityId("ProjectId");
58export type ProjectId = typeof ProjectId.Type;
59export const EnvironmentId = makeEntityId("EnvironmentId");
60export type EnvironmentId = typeof EnvironmentId.Type;
61export const CommandId = makeEntityId("CommandId");
62export type CommandId = typeof CommandId.Type;
63export const EventId = makeEntityId("EventId");
64export type EventId = typeof EventId.Type;
65export const MessageId = makeEntityId("MessageId");
66export type MessageId = typeof MessageId.Type;
67export const TurnId = makeEntityId("TurnId");
68export type TurnId = typeof TurnId.Type;
69export const AuthSessionId = makeEntityId("AuthSessionId");
70export type AuthSessionId = typeof AuthSessionId.Type;
71export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
72export type RpcClientId = typeof RpcClientId.Type;
73
74export const ProviderItemId = makeEntityId("ProviderItemId");
75export type ProviderItemId = typeof ProviderItemId.Type;
76export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
77export type RuntimeSessionId = typeof RuntimeSessionId.Type;
78export const RuntimeItemId = makeEntityId("RuntimeItemId");
79export type RuntimeItemId = typeof RuntimeItemId.Type;
80export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
81export type RuntimeRequestId = typeof RuntimeRequestId.Type;
82export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
83export type RuntimeTaskId = typeof RuntimeTaskId.Type;
84export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
85export type ApprovalRequestId = typeof ApprovalRequestId.Type;
86export const CheckpointRef = makeEntityId("CheckpointRef");
87export type CheckpointRef = typeof CheckpointRef.Type;