The best way to control Claude Code, Codex and more from any computer or phone

main
ts 302 lines 8.05 KB
Raw
1 import type { Effect } from "effect/Effect";
2 import * as S from "effect/Schema";
3 import * as AST from "effect/SchemaAST";
4 import type { Sink } from "effect/Sink";
5 import type { Stream } from "effect/Stream";
6
7 export * from "effect/Schema";
8
9 export const isNullishSchema = (schema: S.Schema<any>) =>
10 isNullSchema(schema) || isUndefinedSchema(schema);
11 export const isNullSchema = (schema: S.Schema<any>) => AST.isNull(schema.ast);
12 export const isUndefinedSchema = (schema: S.Schema<any>) => AST.isUndefined(schema.ast);
13 export const isBooleanSchema = (schema: S.Schema<any>) => AST.isBoolean(schema.ast);
14 export const isStringSchema = (schema: S.Schema<any>) => AST.isString(schema.ast);
15 export const isNumberSchema = (schema: S.Schema<any>) => AST.isNumber(schema.ast);
16
17 export const isRecordLikeSchema = (schema: S.Schema<any>) =>
18 isMapSchema(schema) ||
19 isRecordSchema(schema) ||
20 isStructSchema(schema) ||
21 isClassSchema(schema) ||
22 false;
23
24 export const isMapSchema = (schema: S.Schema<any>): boolean =>
25 getDeclarationRepresentationId(schema.ast) === "effect/schema/ReadonlyMap";
26
27 export const isClassSchema = (schema: S.Schema<any>) => {
28 const ast = schema.ast;
29 if (AST.isDeclaration(ast)) {
30 return AST.isObjects(ast.typeParameters[0]);
31 }
32 return false;
33 };
34
35 export const isStructSchema = (schema: S.Schema<any>) => AST.isObjects(schema.ast);
36
37 export const isRecordSchema = (schema: S.Schema<any>) => {
38 const ast = schema.ast;
39 return AST.isObjects(ast) && ast.indexSignatures?.length > 0;
40 };
41
42 export const isListSchema = (schema: S.Schema<any>) => AST.isArrays(schema.ast);
43
44 export const isSetSchema = (schema: S.Schema<any>): boolean =>
45 getDeclarationRepresentationId(schema.ast) === "effect/schema/ReadonlySet";
46
47 /**
48 * Effect's built-in declaration schemas (ReadonlyMap, ReadonlySet, ...) carry a
49 * `representation` annotation identifying the underlying type constructor.
50 */
51 const getDeclarationRepresentationId = (ast: AST.AST): string | undefined => {
52 if (AST.isDeclaration(ast)) {
53 return (ast.annotations as any)?.representation?.id;
54 }
55 return undefined;
56 };
57
58 export const getSetValueAST = (schema: S.Schema<any>): AST.AST | undefined => {
59 const ast = schema.ast;
60 if (AST.isDeclaration(ast) && isSetSchema(schema)) {
61 return ast.typeParameters[0];
62 }
63 return undefined;
64 };
65
66 /** A Schema representing a Schema */
67 export type Field = S.Top;
68 export const Field = S.suspend((): [Field] extends [any] ? S.Schema<Field> : never => S.Any);
69
70 type FunctionType = (...args: any[]) => any;
71 export type Function<F extends FunctionType = FunctionType> = S.Schema<F>;
72 export const Function: <F extends FunctionType>() => Function<F> = S.suspend(
73 (): S.Schema<FunctionType> => S.Any,
74 ) as any;
75
76 export type CreatedAt = Date;
77 export const CreatedAt = S.Date.annotate({
78 description: "The timestamp of when this record was created",
79 });
80
81 export type UpdatedAt = Date;
82 export const UpdatedAt = S.Date.annotate({
83 description: "The timestamp of when this record was last updated",
84 });
85
86 export type AnyClassSchema<
87 Self = any,
88 Fields extends S.Top & { fields: S.Struct.Fields } = S.Top & {
89 fields: S.Struct.Fields;
90 },
91 > = S.Class<Self, Fields, any>;
92
93 export type AnyClass = new (...args: any[]) => any;
94
95 export type AnyErrorSchema = S.Class<any, any, any>;
96
97 export type SchemaWithTemplate<
98 Schema extends S.Schema<any>,
99 References extends any[] = any[],
100 > = Schema & {
101 template: TemplateStringsArray;
102 references: References;
103 };
104
105 export type SchemaExt = FunctionSchema | EffectSchema | StreamSchema | SinkSchema;
106
107 export interface SchemaExtBase<A> extends S.Schema<A> {
108 <References extends any[]>(
109 template: TemplateStringsArray,
110 ...references: References
111 ): SchemaWithTemplate<this, References>;
112 }
113
114 export interface FunctionSchema<
115 Input extends S.Top | undefined = S.Top | undefined,
116 Output extends S.Top = S.Top,
117 > extends SchemaExtBase<
118 (
119 ...args: Input extends undefined ? [] : [input: S.Schema.Type<Exclude<Input, undefined>>]
120 ) => S.Schema.Type<Output>
121 > {
122 input: Input;
123 output: Output;
124 }
125
126 export interface EffectSchema<
127 A extends S.Top = S.Top,
128 Err extends S.Top = S.Top,
129 Req extends S.Top = S.Top,
130 > extends SchemaExtBase<Effect<S.Schema.Type<A>, S.Schema.Type<Err>, S.Schema.Type<Req>>> {
131 A: A;
132 Err: Err;
133 Req: Req;
134 }
135
136 export interface StreamSchema<
137 A extends S.Top = S.Top,
138 Err extends S.Top = S.Top,
139 Req extends S.Top = S.Top,
140 > extends SchemaExtBase<Stream<S.Schema.Type<A>, S.Schema.Type<Err>, S.Schema.Type<Req>>> {
141 A: A;
142 Err: Err;
143 Req: Req;
144 }
145
146 export interface SinkSchema<
147 A extends S.Top = S.Top,
148 In extends S.Top = S.Top,
149 L extends S.Top = S.Top,
150 Err extends S.Top = S.Top,
151 Req extends S.Top = S.Top,
152 > extends SchemaExtBase<
153 Sink<
154 S.Schema.Type<A>,
155 S.Schema.Type<In>,
156 S.Schema.Type<L>,
157 S.Schema.Type<Err>,
158 S.Schema.Type<Req>
159 >
160 > {
161 A: A;
162 In: In;
163 L: L;
164 Err: Err;
165 Req: Req;
166 }
167
168 export const makeExtSchema = <Schema extends SchemaExt>(schema: Schema): SchemaExt => {
169 const s = S.Any.annotate({
170 aspect: schema,
171 });
172 return new Proxy(() => {}, {
173 get: (_target, prop) => s[prop as keyof typeof s],
174 apply: (_target, _thisArg, [template, ...references]) =>
175 S.annotate({
176 aspect: {
177 ...schema,
178 template,
179 references,
180 },
181 }),
182 }) as any as SchemaExt;
183 };
184
185 // export type def = typeof def;
186
187 export interface func<
188 Input extends undefined | S.Top | S.Top[],
189 Output extends S.Top,
190 > extends S.Schema<
191 Input extends undefined
192 ? () => S.Schema.Type<Output>
193 : Input extends S.Top[]
194 ? (...args: TypeArray<Input>) => S.Schema.Type<Output>
195 : (input: S.Schema.Type<Extract<Input, S.Top>>) => S.Schema.Type<Output>
196 > {}
197
198 type TypeArray<T extends S.Top[]> = T extends [infer Head, ...infer Tail extends S.Top[]]
199 ? Head extends S.Top
200 ? [S.Schema.Type<Head>, ...TypeArray<Tail>]
201 : never
202 : [];
203
204 export const func: {
205 <Output extends S.Schema<any>>(
206 output: Output,
207 ): func<undefined, Output> & {
208 <R extends any[]>(
209 template: TemplateStringsArray,
210 ...references: R
211 ): SchemaWithTemplate<func<undefined, Output>, R>;
212 };
213 <Input extends S.Schema<any>, Output extends S.Schema<any>>(
214 input: Input,
215 output: Output,
216 ): func<Input, Output> & {
217 <R extends any[]>(
218 template: TemplateStringsArray,
219 ...references: R
220 ): SchemaWithTemplate<func<Input, Output>, R>;
221 };
222 <const Args extends S.Schema<any>[], Output extends S.Schema<any>>(
223 args: Args,
224 output: Output,
225 ): func<Args, Output> & {
226 <R extends any[]>(
227 template: TemplateStringsArray,
228 ...references: R
229 ): SchemaWithTemplate<func<Args, Output>, R>;
230 };
231 } = ((input: any, output: any) =>
232 S.Any.annotate({
233 aspect: {
234 type: "fn",
235 input: output ? input : undefined,
236 output: output ?? input,
237 },
238 })) as any;
239
240 export interface effect<A extends S.Top, Err extends S.Top, Req extends S.Top> extends S.Schema<
241 Effect<S.Schema.Type<A>, S.Schema.Type<Err>, S.Schema.Type<Req>>
242 > {}
243
244 export const effect = <
245 A extends S.Schema<any>,
246 Err extends S.Schema<any> | S.Never = S.Never,
247 Req extends S.Schema<any> | S.Any = S.Any,
248 >(
249 a: A,
250 err: Err = S.Never as any,
251 req: Req = S.Never as any,
252 ): effect<A, Err, Req> =>
253 S.Any.annotate({
254 aspect: {
255 type: "effect",
256 a: a,
257 err: err,
258 req: req,
259 },
260 });
261
262 export const stream = <
263 A extends S.Schema<any>,
264 Err extends S.Schema<any> | S.Never = S.Never,
265 Req extends S.Schema<any> | S.Never = S.Never,
266 >(
267 a: A,
268 err: Err = S.Never as any,
269 req: Req = S.Never as any,
270 ) =>
271 S.Any.annotate({
272 aspect: {
273 type: "stream",
274 a: a,
275 err: err,
276 req: req,
277 },
278 });
279
280 export const sink = <
281 A extends S.Schema<any>,
282 In extends S.Schema<any>,
283 L extends S.Schema<any>,
284 Err extends S.Schema<any> | S.Never = S.Never,
285 Req extends S.Schema<any> | S.Never = S.Never,
286 >(
287 a: A,
288 _in: In = S.Never as any,
289 l: L = S.Never as any,
290 err: Err = S.Never as any,
291 req: Req = S.Never as any,
292 ) =>
293 S.Any.annotate({
294 aspect: {
295 type: "sink",
296 a: a,
297 in: _in,
298 l: l,
299 err: err,
300 req: req,
301 },
302 });