@aitoroses/telar 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +453 -3
  2. package/dist/compile.d.ts +48 -0
  3. package/dist/compile.js +1238 -0
  4. package/dist/contract.d.ts +109 -0
  5. package/dist/contract.js +497 -0
  6. package/dist/evaluator.d.ts +23 -0
  7. package/dist/evaluator.js +40 -0
  8. package/dist/guide.d.ts +8 -0
  9. package/dist/guide.js +16 -0
  10. package/dist/install.d.ts +104 -0
  11. package/dist/install.js +402 -0
  12. package/dist/iso.d.ts +8 -0
  13. package/dist/iso.js +68 -0
  14. package/dist/node/build.d.ts +1 -0
  15. package/dist/node/build.js +34 -0
  16. package/dist/node/telar-module.d.ts +2 -0
  17. package/dist/node/telar-module.js +28 -0
  18. package/dist/observer.d.ts +47 -0
  19. package/dist/observer.js +53 -0
  20. package/dist/pieces.d.ts +230 -0
  21. package/dist/pieces.js +178 -0
  22. package/dist/powers.d.ts +46 -0
  23. package/dist/powers.js +126 -0
  24. package/dist/protocol.d.ts +212 -0
  25. package/dist/protocol.js +236 -0
  26. package/dist/query.d.ts +67 -0
  27. package/dist/query.js +102 -0
  28. package/dist/read.d.ts +37 -0
  29. package/dist/read.js +27 -0
  30. package/dist/runner.d.ts +112 -0
  31. package/dist/runner.js +134 -0
  32. package/dist/scenario/builder.d.ts +25 -0
  33. package/dist/scenario/builder.js +603 -0
  34. package/dist/scenario/templates.d.ts +35 -0
  35. package/dist/scenario/templates.js +64 -0
  36. package/dist/scenario/types.d.ts +441 -0
  37. package/dist/scenario/types.js +10 -0
  38. package/dist/scenario/validate.d.ts +7 -0
  39. package/dist/scenario/validate.js +19 -0
  40. package/dist/scenario.d.ts +4 -0
  41. package/dist/scenario.js +4 -0
  42. package/dist/schema.d.ts +219 -0
  43. package/dist/schema.js +221 -0
  44. package/dist/seating.d.ts +49 -0
  45. package/dist/seating.js +26 -0
  46. package/dist/send-hook.d.ts +46 -0
  47. package/dist/send-hook.js +123 -0
  48. package/dist/show.d.ts +34 -0
  49. package/dist/show.js +1 -0
  50. package/dist/source.d.ts +85 -0
  51. package/dist/source.js +104 -0
  52. package/dist/spec.d.ts +20 -0
  53. package/dist/spec.js +17 -0
  54. package/dist/standard-schema.d.ts +21 -0
  55. package/dist/standard-schema.js +1 -0
  56. package/dist/std/accuracy.d.ts +12 -0
  57. package/dist/std/accuracy.js +122 -0
  58. package/dist/std/ack.d.ts +14 -0
  59. package/dist/std/ack.js +43 -0
  60. package/dist/std/actions.d.ts +106 -0
  61. package/dist/std/actions.js +154 -0
  62. package/dist/std/brief.d.ts +36 -0
  63. package/dist/std/brief.js +78 -0
  64. package/dist/std/claude-code.d.ts +9 -0
  65. package/dist/std/claude-code.js +106 -0
  66. package/dist/std/compare.d.ts +29 -0
  67. package/dist/std/compare.js +36 -0
  68. package/dist/std/git-workspace.d.ts +44 -0
  69. package/dist/std/git-workspace.js +184 -0
  70. package/dist/std/judgment.d.ts +85 -0
  71. package/dist/std/judgment.js +462 -0
  72. package/dist/std/observe-command.d.ts +23 -0
  73. package/dist/std/observe-command.js +151 -0
  74. package/dist/std/observe-github-checks.d.ts +26 -0
  75. package/dist/std/observe-github-checks.js +299 -0
  76. package/dist/std/observe-http-status.d.ts +27 -0
  77. package/dist/std/observe-http-status.js +181 -0
  78. package/dist/std/plan.d.ts +7 -0
  79. package/dist/std/plan.js +28 -0
  80. package/dist/std/queries.d.ts +31 -0
  81. package/dist/std/queries.js +318 -0
  82. package/dist/std/reading.d.ts +17 -0
  83. package/dist/std/reading.js +88 -0
  84. package/dist/std/schedule.d.ts +19 -0
  85. package/dist/std/schedule.js +122 -0
  86. package/dist/std.d.ts +15 -0
  87. package/dist/std.js +15 -0
  88. package/dist/types.d.ts +366 -0
  89. package/dist/types.js +1 -0
  90. package/dist/values.d.ts +81 -0
  91. package/dist/values.js +129 -0
  92. package/package.json +62 -5
@@ -0,0 +1,219 @@
1
+ import type { Guide } from "./guide.js";
2
+ import type { Audience, JsonValue, NonEmpty, SchemaAnnotations, TypeSchema } from "./types.js";
3
+ declare const brandSymbol: unique symbol;
4
+ declare const phantomType: unique symbol;
5
+ declare const variantSymbol: unique symbol;
6
+ export type Brand<T, B extends string> = T & {
7
+ readonly [brandSymbol]: B;
8
+ };
9
+ export interface TypedFieldHandle<Name extends string = string, T = unknown> {
10
+ readonly kind: "field";
11
+ readonly name: Name;
12
+ readonly [phantomType]?: T;
13
+ }
14
+ export interface AnnotationsInput {
15
+ readonly description?: string | undefined;
16
+ readonly examples?: readonly JsonValue[] | undefined;
17
+ readonly audience?: Audience | undefined;
18
+ readonly guide?: Guide | undefined;
19
+ }
20
+ export interface StringOpts<B extends string = never> extends AnnotationsInput {
21
+ readonly brand?: B | undefined;
22
+ readonly nonEmpty?: boolean | undefined;
23
+ readonly maxLength?: number | undefined;
24
+ readonly validator?: string | undefined;
25
+ }
26
+ export interface NumberOpts<B extends string = never> extends AnnotationsInput {
27
+ readonly brand?: B | undefined;
28
+ readonly integer?: boolean | undefined;
29
+ readonly min?: number | undefined;
30
+ readonly max?: number | undefined;
31
+ readonly exclusiveMin?: number | undefined;
32
+ readonly exclusiveMax?: number | undefined;
33
+ }
34
+ export interface BooleanOpts<B extends string = never> extends AnnotationsInput {
35
+ readonly brand?: B | undefined;
36
+ }
37
+ export interface ArrayOpts<NE extends boolean = false> extends AnnotationsInput {
38
+ readonly nonEmpty?: NE | undefined;
39
+ }
40
+ export interface StructOpts extends AnnotationsInput {
41
+ readonly validator?: string | undefined;
42
+ }
43
+ export interface StructGuideOpts<F extends FieldSchemaMap> extends Omit<StructOpts, "guide"> {
44
+ readonly guide?: Guide | ((fields: FieldHandlesOf<F>) => Guide) | undefined;
45
+ }
46
+ export interface TaggedOpts extends StructOpts {
47
+ readonly tag?: string | undefined;
48
+ }
49
+ export interface TaggedGuideOpts<F extends FieldSchemaMap, TagKey extends string = "_tag"> extends Omit<TaggedOpts, "guide" | "tag"> {
50
+ readonly tag?: TagKey | undefined;
51
+ readonly guide?: Guide | ((fields: FieldHandlesOf<F>) => Guide) | undefined;
52
+ }
53
+ export interface UnionOpts extends AnnotationsInput {
54
+ readonly tag?: string | undefined;
55
+ }
56
+ export type SchemaNode = {
57
+ readonly tag: "primitive";
58
+ readonly wire: TypeSchema;
59
+ } | {
60
+ readonly tag: "array";
61
+ readonly item: AnyTypeDef;
62
+ readonly nonEmpty?: boolean;
63
+ readonly annotations: SchemaAnnotations;
64
+ } | {
65
+ readonly tag: "struct";
66
+ readonly fields: Readonly<Record<string, AnyFieldSchemaEntry>>;
67
+ readonly validator?: string;
68
+ readonly annotations: SchemaAnnotations;
69
+ } | {
70
+ readonly tag: "tagged";
71
+ readonly tagKey: string;
72
+ readonly tagValue: string;
73
+ readonly fields: Readonly<Record<string, AnyFieldSchemaEntry>>;
74
+ readonly validator?: string;
75
+ readonly annotations: SchemaAnnotations;
76
+ } | {
77
+ readonly tag: "union";
78
+ readonly tagKey: string;
79
+ readonly variants: Readonly<Record<string, AnyStructType>>;
80
+ readonly annotations: SchemaAnnotations;
81
+ };
82
+ export interface TypeDef<T = unknown> {
83
+ readonly kind: "type";
84
+ readonly node: SchemaNode;
85
+ readonly [phantomType]?: T;
86
+ }
87
+ export interface OptionalType<S extends AnyTypeDef = AnyTypeDef> {
88
+ readonly kind: "optional";
89
+ readonly inner: S;
90
+ }
91
+ export type AnyTypeDef = {
92
+ readonly kind: "type";
93
+ readonly node: SchemaNode;
94
+ readonly [phantomType]?: unknown;
95
+ } | AnyContract;
96
+ export interface AnyContract {
97
+ readonly kind: "contract";
98
+ readonly name: string;
99
+ readonly schema: AnyTypeDef;
100
+ readonly [phantomType]?: unknown;
101
+ readonly [variantSymbol]?: unknown;
102
+ }
103
+ export type AnyFieldSchemaEntry = AnyTypeDef | OptionalType;
104
+ export type FieldSchemaMap = Readonly<Record<string, AnyFieldSchemaEntry>>;
105
+ type RequiredKeys<F extends FieldSchemaMap> = {
106
+ readonly [K in keyof F]: F[K] extends OptionalType ? never : K;
107
+ }[keyof F];
108
+ type OptionalKeys<F extends FieldSchemaMap> = {
109
+ readonly [K in keyof F]: F[K] extends OptionalType ? K : never;
110
+ }[keyof F];
111
+ type ValueOf<S> = S extends {
112
+ readonly [phantomType]?: infer T;
113
+ } ? T : never;
114
+ export type Infer<S> = S extends OptionalType<infer Inner> ? ValueOf<Inner> | undefined : ValueOf<S>;
115
+ export type InferStruct<F extends FieldSchemaMap> = {
116
+ readonly [K in RequiredKeys<F>]: ValueOf<F[K]>;
117
+ } & {
118
+ readonly [K in OptionalKeys<F>]?: F[K] extends OptionalType<infer Inner> ? ValueOf<Inner> : never;
119
+ };
120
+ export interface StructType<F extends FieldSchemaMap = FieldSchemaMap> extends TypeDef<InferStruct<F>> {
121
+ readonly shapeKind: "struct";
122
+ readonly fields: F;
123
+ }
124
+ export interface TaggedStructType<Tag extends string = string, F extends FieldSchemaMap = FieldSchemaMap, TagKey extends string = "_tag"> extends TypeDef<InferStruct<F> & {
125
+ readonly [K in TagKey]: Tag;
126
+ }> {
127
+ readonly shapeKind: "tagged";
128
+ readonly tagKey: TagKey;
129
+ readonly tagValue: Tag;
130
+ readonly fields: F;
131
+ }
132
+ type VariantMapToUnion<V extends Readonly<Record<string, FieldSchemaMap | AnyStructType>>, TagKey extends string> = {
133
+ readonly [K in keyof V & string]: {
134
+ readonly [TK in TagKey]: K;
135
+ } & (V[K] extends StructType<infer F> ? InferStruct<F> : V[K] extends FieldSchemaMap ? InferStruct<V[K]> : never);
136
+ }[keyof V & string];
137
+ export interface UnionType<V extends Readonly<Record<string, FieldSchemaMap | AnyStructType>> = Readonly<Record<string, FieldSchemaMap | AnyStructType>>, TagKey extends string = "_tag"> extends TypeDef<VariantMapToUnion<V, TagKey>> {
138
+ readonly shapeKind: "union";
139
+ readonly tagKey: TagKey;
140
+ readonly variants: Readonly<Record<string, AnyStructType>>;
141
+ readonly [variantSymbol]?: keyof V & string;
142
+ }
143
+ export interface AnyStructType {
144
+ readonly kind: "type";
145
+ readonly shapeKind: "struct";
146
+ readonly fields: FieldSchemaMap;
147
+ readonly node: SchemaNode;
148
+ readonly [phantomType]?: unknown;
149
+ }
150
+ export interface AnyTaggedStruct {
151
+ readonly kind: "type";
152
+ readonly shapeKind: "tagged";
153
+ readonly tagKey: string;
154
+ readonly tagValue: string;
155
+ readonly fields: FieldSchemaMap;
156
+ readonly node: SchemaNode;
157
+ readonly [phantomType]?: unknown;
158
+ }
159
+ export interface AnyUnionType {
160
+ readonly kind: "type";
161
+ readonly shapeKind: "union";
162
+ readonly tagKey: string;
163
+ readonly variants: Readonly<Record<string, AnyStructType>>;
164
+ readonly node: SchemaNode;
165
+ readonly [phantomType]?: unknown;
166
+ readonly [variantSymbol]?: unknown;
167
+ }
168
+ export type UnionMemberInput = AnyTaggedStruct | AnyUnionType | Contract<string, AnyTaggedStruct, unknown, unknown> | Contract<string, AnyUnionType, unknown, unknown>;
169
+ type MemberVariants<M> = M extends Contract<string, AnyTaggedStruct, unknown, infer V> ? V & string : M extends Contract<string, AnyUnionType, unknown, infer V> ? V & string : M extends TaggedStructType<infer Tag, FieldSchemaMap, string> ? Tag : M extends {
170
+ readonly [variantSymbol]?: infer V;
171
+ } ? V & string : never;
172
+ export interface FlattenedUnionType<Variants extends string = string, Value = unknown> extends TypeDef<Value> {
173
+ readonly shapeKind: "union";
174
+ readonly tagKey: string;
175
+ readonly variants: Readonly<Record<string, AnyStructType>>;
176
+ readonly [variantSymbol]?: Variants;
177
+ }
178
+ export type FieldHandlesOf<F extends FieldSchemaMap> = {
179
+ readonly [K in keyof F & string]: TypedFieldHandle<K, F[K] extends OptionalType<infer Inner> ? ValueOf<Inner> | undefined : ValueOf<F[K]>>;
180
+ };
181
+ export interface Contract<Name extends string = string, S extends AnyTypeDef = AnyTypeDef, T = unknown, V = never> {
182
+ readonly kind: "contract";
183
+ readonly name: Name;
184
+ readonly schema: S;
185
+ readonly [phantomType]?: T;
186
+ readonly [variantSymbol]?: V;
187
+ }
188
+ export interface StructContract<Name extends string = string, F extends FieldSchemaMap = FieldSchemaMap> extends Contract<Name, StructType<F>, InferStruct<F>> {
189
+ readonly fields: FieldHandlesOf<F>;
190
+ field<K extends keyof F & string>(name: K): FieldHandlesOf<F>[K];
191
+ }
192
+ export type AnyStructContract = StructContract;
193
+ export type VariantsOf<C> = C extends {
194
+ readonly [variantSymbol]?: infer V;
195
+ } ? V & string : never;
196
+ export type StructFieldsOfContract<C> = C extends StructContract<string, infer F> ? F : C extends Contract<string, StructType<infer F>, unknown, unknown> ? F : never;
197
+ export declare const t: {
198
+ readonly string: <const B extends string = never>(opts?: StringOpts<B>) => TypeDef<[B] extends [never] ? string : Brand<string, B>>;
199
+ readonly number: <const B extends string = never>(opts?: NumberOpts<B>) => TypeDef<[B] extends [never] ? number : Brand<number, B>>;
200
+ readonly int: <const B extends string = never>(opts?: Omit<NumberOpts<B>, "integer">) => TypeDef<[B] extends [never] ? number : Brand<number, B>>;
201
+ readonly boolean: <const B extends string = never>(opts?: BooleanOpts<B>) => TypeDef<[B] extends [never] ? boolean : Brand<boolean, B>>;
202
+ readonly literal: <const V extends string | number | boolean>(value: V, opts?: AnnotationsInput) => TypeDef<V>;
203
+ readonly enum: <const V extends string>(values: NonEmpty<V>, opts?: AnnotationsInput) => TypeDef<V>;
204
+ readonly array: <S extends AnyTypeDef, const NE extends boolean = false>(item: S, opts?: ArrayOpts<NE>) => TypeDef<NE extends true ? NonEmpty<Infer<S>> : readonly Infer<S>[]>;
205
+ readonly struct: <F extends FieldSchemaMap>(fields: F, opts?: StructGuideOpts<F>) => StructType<F>;
206
+ readonly tagged: <const Tag extends string, F extends FieldSchemaMap, const TagKey extends string = "_tag">(tagValue: Tag, fields: F, opts?: TaggedGuideOpts<F, TagKey>) => TaggedStructType<Tag, F, TagKey>;
207
+ readonly union: <V extends Readonly<Record<string, FieldSchemaMap | AnyStructType>>, const TagKey extends string = "_tag">(variants: V, opts?: UnionOpts & {
208
+ readonly tag?: TagKey | undefined;
209
+ }) => UnionType<V, TagKey>;
210
+ readonly unionOf: <const M extends NonEmpty<UnionMemberInput>>(members: M, opts?: UnionOpts) => FlattenedUnionType<MemberVariants<M[number]>, ValueOf<M[number]>>;
211
+ readonly optional: <S extends AnyTypeDef>(schema: S) => OptionalType<S>;
212
+ readonly ref: <T = unknown>(schema: string, opts?: AnnotationsInput) => TypeDef<T>;
213
+ };
214
+ type VariantOfTypeDef<S> = S extends TaggedStructType<infer Tag, FieldSchemaMap, string> ? Tag : S extends {
215
+ readonly [variantSymbol]?: infer V;
216
+ } ? V : never;
217
+ export declare function contract<const Name extends string, F extends FieldSchemaMap>(name: Name, fields: F, opts?: StructGuideOpts<F>): StructContract<Name, F>;
218
+ export declare function contract<const Name extends string, S extends AnyTypeDef>(name: Name, schema: S): Contract<Name, S, ValueOf<S>, VariantOfTypeDef<S>>;
219
+ export {};
package/dist/schema.js ADDED
@@ -0,0 +1,221 @@
1
+ function pickAnnotations(opts) {
2
+ if (opts === undefined)
3
+ return {};
4
+ return {
5
+ ...(opts.description !== undefined ? { description: opts.description } : {}),
6
+ ...(opts.examples !== undefined ? { examples: opts.examples } : {}),
7
+ ...(opts.audience !== undefined ? { audience: opts.audience } : {}),
8
+ ...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
9
+ };
10
+ }
11
+ function resolveStructOpts(fields, opts) {
12
+ if (opts === undefined)
13
+ return undefined;
14
+ const g = opts.guide;
15
+ const resolved = typeof g === "function" ? g(makeFieldHandles(fields)) : g;
16
+ return {
17
+ ...opts,
18
+ ...(resolved !== undefined ? { guide: resolved } : { guide: undefined }),
19
+ };
20
+ }
21
+ function isStructType(value) {
22
+ return ("kind" in value && value.kind === "type" && "shapeKind" in value && value.shapeKind === "struct");
23
+ }
24
+ function isAnyTypeDef(value) {
25
+ return "kind" in value && (value.kind === "type" || value.kind === "contract");
26
+ }
27
+ function makeStructTypeFromAnnotations(fields, annotations, validator) {
28
+ const node = {
29
+ tag: "struct",
30
+ fields,
31
+ ...(validator !== undefined ? { validator } : {}),
32
+ annotations,
33
+ };
34
+ return Object.freeze({ kind: "type", shapeKind: "struct", fields, node });
35
+ }
36
+ function makeStructType(fields, opts) {
37
+ const resolvedOpts = resolveStructOpts(fields, opts);
38
+ const annotations = pickAnnotations(resolvedOpts);
39
+ return makeStructTypeFromAnnotations(fields, annotations, resolvedOpts?.validator);
40
+ }
41
+ function makeFieldHandles(fields) {
42
+ const handles = {};
43
+ for (const key of Object.keys(fields)) {
44
+ handles[key] = Object.freeze({ kind: "field", name: key });
45
+ }
46
+ return Object.freeze(handles);
47
+ }
48
+ function makeTagged(tagValue, fields, opts) {
49
+ const tagKey = opts?.tag ?? "_tag";
50
+ const resolvedOpts = resolveStructOpts(fields, opts);
51
+ const node = {
52
+ tag: "tagged",
53
+ tagKey,
54
+ tagValue,
55
+ fields,
56
+ ...(resolvedOpts?.validator !== undefined ? { validator: resolvedOpts.validator } : {}),
57
+ annotations: pickAnnotations(resolvedOpts),
58
+ };
59
+ return Object.freeze({ kind: "type", shapeKind: "tagged", tagKey, tagValue, fields, node });
60
+ }
61
+ function makeUnion(variants, opts) {
62
+ const tagKey = opts?.tag ?? "_tag";
63
+ const normalized = {};
64
+ for (const [key, variant] of Object.entries(variants)) {
65
+ normalized[key] = isStructType(variant) ? variant : makeStructType(variant);
66
+ }
67
+ const frozenVariants = Object.freeze(normalized);
68
+ const node = {
69
+ tag: "union",
70
+ tagKey,
71
+ variants: frozenVariants,
72
+ annotations: pickAnnotations(opts),
73
+ };
74
+ return Object.freeze({
75
+ kind: "type",
76
+ shapeKind: "union",
77
+ tagKey,
78
+ variants: frozenVariants,
79
+ node,
80
+ });
81
+ }
82
+ function unwrapUnionMember(member) {
83
+ const target = member.kind === "contract" ? member.schema : member;
84
+ if (target.shapeKind === "tagged") {
85
+ const node = target.node;
86
+ const validator = node.tag === "tagged" ? node.validator : undefined;
87
+ const annotations = node.tag === "tagged" ? node.annotations : {};
88
+ const stripped = makeStructTypeFromAnnotations(target.fields, annotations, validator);
89
+ return { tagKey: target.tagKey, entries: [[target.tagValue, stripped]] };
90
+ }
91
+ return {
92
+ tagKey: target.tagKey,
93
+ entries: Object.entries(target.variants),
94
+ };
95
+ }
96
+ export const t = Object.freeze({
97
+ string(opts) {
98
+ const wire = {
99
+ kind: "string",
100
+ ...(opts?.brand !== undefined ? { brand: opts.brand } : {}),
101
+ ...(opts?.nonEmpty !== undefined ? { nonEmpty: opts.nonEmpty } : {}),
102
+ ...(opts?.maxLength !== undefined ? { maxLength: opts.maxLength } : {}),
103
+ ...(opts?.validator !== undefined ? { validator: opts.validator } : {}),
104
+ ...pickAnnotations(opts),
105
+ };
106
+ const node = { tag: "primitive", wire };
107
+ return Object.freeze({ kind: "type", node });
108
+ },
109
+ number(opts) {
110
+ const wire = {
111
+ kind: "number",
112
+ ...(opts?.brand !== undefined ? { brand: opts.brand } : {}),
113
+ ...(opts?.integer !== undefined ? { integer: opts.integer } : {}),
114
+ ...(opts?.min !== undefined ? { min: opts.min } : {}),
115
+ ...(opts?.max !== undefined ? { max: opts.max } : {}),
116
+ ...(opts?.exclusiveMin !== undefined ? { exclusiveMin: opts.exclusiveMin } : {}),
117
+ ...(opts?.exclusiveMax !== undefined ? { exclusiveMax: opts.exclusiveMax } : {}),
118
+ ...pickAnnotations(opts),
119
+ };
120
+ const node = { tag: "primitive", wire };
121
+ return Object.freeze({ kind: "type", node });
122
+ },
123
+ int(opts) {
124
+ return t.number({ ...opts, integer: true });
125
+ },
126
+ boolean(opts) {
127
+ const wire = {
128
+ kind: "boolean",
129
+ ...(opts?.brand !== undefined ? { brand: opts.brand } : {}),
130
+ ...pickAnnotations(opts),
131
+ };
132
+ const node = { tag: "primitive", wire };
133
+ return Object.freeze({ kind: "type", node });
134
+ },
135
+ literal(value, opts) {
136
+ const wire = { kind: "literal", value, ...pickAnnotations(opts) };
137
+ const node = { tag: "primitive", wire };
138
+ return Object.freeze({ kind: "type", node });
139
+ },
140
+ enum(values, opts) {
141
+ const wire = { kind: "enum", values: [...values], ...pickAnnotations(opts) };
142
+ const node = { tag: "primitive", wire };
143
+ return Object.freeze({ kind: "type", node });
144
+ },
145
+ array(item, opts) {
146
+ const node = {
147
+ tag: "array",
148
+ item,
149
+ ...(opts?.nonEmpty !== undefined ? { nonEmpty: opts.nonEmpty } : {}),
150
+ annotations: pickAnnotations(opts),
151
+ };
152
+ return Object.freeze({ kind: "type", node });
153
+ },
154
+ struct(fields, opts) {
155
+ return makeStructType(fields, opts);
156
+ },
157
+ tagged: makeTagged,
158
+ union: makeUnion,
159
+ unionOf(members, opts) {
160
+ const tagKey = opts?.tag ?? "_tag";
161
+ const variants = {};
162
+ for (const member of members) {
163
+ const unwrapped = unwrapUnionMember(member);
164
+ if (unwrapped.tagKey !== tagKey) {
165
+ throw new Error(`unionOf tag mismatch: expected ${JSON.stringify(tagKey)}, got ${JSON.stringify(unwrapped.tagKey)}`);
166
+ }
167
+ for (const [k, v] of unwrapped.entries) {
168
+ if (Object.hasOwn(variants, k)) {
169
+ throw new Error(`Duplicate union variant ${JSON.stringify(k)}`);
170
+ }
171
+ variants[k] = v;
172
+ }
173
+ }
174
+ const frozenVariants = Object.freeze(variants);
175
+ const node = {
176
+ tag: "union",
177
+ tagKey,
178
+ variants: frozenVariants,
179
+ annotations: pickAnnotations(opts),
180
+ };
181
+ return Object.freeze({
182
+ kind: "type",
183
+ shapeKind: "union",
184
+ tagKey,
185
+ variants: frozenVariants,
186
+ node,
187
+ });
188
+ },
189
+ optional(schema) {
190
+ return Object.freeze({ kind: "optional", inner: schema });
191
+ },
192
+ ref(schema, opts) {
193
+ const wire = { kind: "ref", schema, ...pickAnnotations(opts) };
194
+ const node = { tag: "primitive", wire };
195
+ return Object.freeze({ kind: "type", node });
196
+ },
197
+ });
198
+ export function contract(name, shape, opts) {
199
+ if (isAnyTypeDef(shape)) {
200
+ return Object.freeze({
201
+ kind: "contract",
202
+ name,
203
+ schema: shape,
204
+ });
205
+ }
206
+ const struct = makeStructType(shape, opts);
207
+ const handles = makeFieldHandles(shape);
208
+ return Object.freeze({
209
+ kind: "contract",
210
+ name,
211
+ schema: struct,
212
+ fields: handles,
213
+ field: (key) => {
214
+ const handle = handles[key];
215
+ if (handle === undefined) {
216
+ throw new Error(`Unknown field ${JSON.stringify(key)} on contract ${JSON.stringify(name)}`);
217
+ }
218
+ return handle;
219
+ },
220
+ });
221
+ }
@@ -0,0 +1,49 @@
1
+ import type { AnyRunner, RunnerIdOf, RunnerOptionsOf } from "./runner.js";
2
+ export type SeatBindingChoice = "all" | "scope" | "case";
3
+ /** The key a seat is bound by, as `seat-bound.binding` records it in `telar.trail/1`. */
4
+ export type SeatKey = {
5
+ readonly kind: "case";
6
+ readonly case_id: string;
7
+ } | {
8
+ readonly kind: "all";
9
+ } | {
10
+ readonly kind: "field";
11
+ readonly field: string;
12
+ readonly value: string;
13
+ } | {
14
+ readonly kind: "ancestor";
15
+ readonly protocol: string;
16
+ readonly case_id: string;
17
+ };
18
+ export type RunnerOptions = Readonly<Record<string, unknown>>;
19
+ export type InstalledRunnerChoice<Runners extends readonly AnyRunner[]> = Runners[number] extends infer R ? R extends AnyRunner ? {
20
+ readonly runner: RunnerIdOf<R>;
21
+ readonly options?: RunnerOptionsOf<R> | undefined;
22
+ } : never : never;
23
+ export type RunnerChoice<Runners extends readonly AnyRunner[] = readonly []> = InstalledRunnerChoice<Runners> | {
24
+ readonly runner?: undefined;
25
+ readonly options?: RunnerOptionsOf<Runners[number]> | undefined;
26
+ };
27
+ export type SeatRunner<Runners extends readonly AnyRunner[] = readonly []> = RunnerIdOf<Runners[number]>;
28
+ export interface RoleSeatingBase {
29
+ readonly binding?: SeatBindingChoice | undefined;
30
+ readonly manual?: boolean | undefined;
31
+ }
32
+ /** How `telar host` seats one role. Unset fields fall back to the seating's own. */
33
+ export type RoleSeating<Runners extends readonly AnyRunner[] = readonly []> = RoleSeatingBase & RunnerChoice<Runners>;
34
+ /** Which runner and typed options `telar host` uses for each role. */
35
+ export type Seating<Runners extends readonly AnyRunner[] = readonly []> = RunnerChoice<Runners> & {
36
+ readonly roles?: Readonly<Record<string, RoleSeating<Runners>>> | undefined;
37
+ };
38
+ interface RuntimeSeating {
39
+ readonly runner?: string | undefined;
40
+ readonly options?: unknown;
41
+ readonly roles?: Readonly<Record<string, RoleSeatingBase & {
42
+ readonly runner?: string | undefined;
43
+ readonly options?: unknown;
44
+ }>> | undefined;
45
+ }
46
+ /** The installation's defaults under the team's seating, role by role. */
47
+ export declare function mergeSeating<const Runners extends readonly AnyRunner[] = readonly []>(defaults: Seating<Runners> | undefined, team: Seating<Runners> | undefined): Seating<Runners> | undefined;
48
+ export declare function mergeSeating(defaults: RuntimeSeating | undefined, team: RuntimeSeating | undefined): RuntimeSeating | undefined;
49
+ export {};
@@ -0,0 +1,26 @@
1
+ function mergeOptions(defaults, team) {
2
+ if (team === undefined || defaults === undefined)
3
+ return team ?? defaults;
4
+ if (typeof defaults === "object" &&
5
+ defaults !== null &&
6
+ !Array.isArray(defaults) &&
7
+ typeof team === "object" &&
8
+ team !== null &&
9
+ !Array.isArray(team)) {
10
+ return { ...defaults, ...team };
11
+ }
12
+ return team;
13
+ }
14
+ export function mergeSeating(defaults, team) {
15
+ if (team === undefined || defaults === undefined)
16
+ return team ?? defaults;
17
+ const roles = {
18
+ ...defaults.roles,
19
+ };
20
+ for (const [role, entry] of Object.entries(team.roles ?? {})) {
21
+ const options = mergeOptions(roles[role]?.options, entry.options);
22
+ roles[role] = { ...roles[role], ...entry, ...(options !== undefined ? { options } : {}) };
23
+ }
24
+ const options = mergeOptions(defaults.options, team.options);
25
+ return { ...defaults, ...team, ...(options !== undefined ? { options } : {}), roles };
26
+ }
@@ -0,0 +1,46 @@
1
+ export type SendOrigin = {
2
+ readonly _tag: "Step";
3
+ readonly step: string;
4
+ } | {
5
+ readonly _tag: "Seat";
6
+ readonly seat: string;
7
+ readonly role: string;
8
+ };
9
+ export type SendHookSubject = {
10
+ readonly _tag: "Message";
11
+ readonly message: string;
12
+ readonly messageKind: string;
13
+ readonly origin?: SendOrigin;
14
+ } | {
15
+ readonly _tag: "Draft";
16
+ readonly shape: string;
17
+ readonly variant?: string;
18
+ };
19
+ export interface SendHookCase {
20
+ readonly id: string;
21
+ readonly protocol: string;
22
+ readonly project?: string;
23
+ readonly scope?: string;
24
+ }
25
+ export interface SendHookSeat {
26
+ readonly id: string;
27
+ readonly role: string;
28
+ readonly labels: Readonly<Record<string, string>>;
29
+ }
30
+ export interface SendHookArgs {
31
+ readonly subject: SendHookSubject;
32
+ readonly case: SendHookCase;
33
+ readonly toRole: string;
34
+ readonly seat: SendHookSeat;
35
+ readonly rendered: string;
36
+ }
37
+ export type SendVerdict = {
38
+ readonly allow: true;
39
+ } | {
40
+ readonly allow: false;
41
+ readonly reason: string;
42
+ };
43
+ /** Reads the args of a send hook call. Throws `PluginCallError("INVALID_INPUT")` naming the bad field. */
44
+ export declare function decodeSendHookArgs(raw: unknown): SendHookArgs;
45
+ /** Reads a send hook verdict as telar-core does. Throws `PluginCallError("INVALID_VERDICT")`. */
46
+ export declare function decodeSendVerdict(raw: unknown): SendVerdict;
@@ -0,0 +1,123 @@
1
+ import { PluginCallError } from "./install.js";
2
+ class WireError extends Error {
3
+ }
4
+ function isRec(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ }
7
+ function record(value, path, keys) {
8
+ if (!isRec(value))
9
+ throw new WireError(`${path}: expected an object`);
10
+ for (const key of Object.keys(value)) {
11
+ if (keys !== undefined && !keys.includes(key)) {
12
+ throw new WireError(`${path}.${key}: unexpected field`);
13
+ }
14
+ }
15
+ return value;
16
+ }
17
+ function text(value, path, nonEmpty) {
18
+ if (typeof value !== "string")
19
+ throw new WireError(`${path}: expected a string`);
20
+ if (nonEmpty && value.length === 0)
21
+ throw new WireError(`${path}: must not be empty`);
22
+ return value;
23
+ }
24
+ function name(rec, key, path) {
25
+ return text(rec[key], `${path}.${key}`, true);
26
+ }
27
+ function optionalName(rec, key, path) {
28
+ return rec[key] === undefined ? undefined : name(rec, key, path);
29
+ }
30
+ function origin(value, path) {
31
+ const tag = record(value, path, ["_tag", "step", "seat", "role"])["_tag"];
32
+ if (tag === "Step") {
33
+ const rec = record(value, path, ["_tag", "step"]);
34
+ return { _tag: "Step", step: name(rec, "step", path) };
35
+ }
36
+ if (tag === "Seat") {
37
+ const rec = record(value, path, ["_tag", "seat", "role"]);
38
+ return { _tag: "Seat", seat: name(rec, "seat", path), role: name(rec, "role", path) };
39
+ }
40
+ throw new WireError(`${path}._tag: expected "Step" or "Seat"`);
41
+ }
42
+ function subject(value, path) {
43
+ const tag = record(value, path, ["_tag", "message", "messageKind", "origin", "shape", "variant"])["_tag"];
44
+ if (tag === "Message") {
45
+ const rec = record(value, path, ["_tag", "message", "messageKind", "origin"]);
46
+ return {
47
+ _tag: "Message",
48
+ message: name(rec, "message", path),
49
+ messageKind: name(rec, "messageKind", path),
50
+ ...(rec["origin"] === undefined ? {} : { origin: origin(rec["origin"], `${path}.origin`) }),
51
+ };
52
+ }
53
+ if (tag === "Draft") {
54
+ const rec = record(value, path, ["_tag", "shape", "variant"]);
55
+ const variant = optionalName(rec, "variant", path);
56
+ return {
57
+ _tag: "Draft",
58
+ shape: name(rec, "shape", path),
59
+ ...(variant === undefined ? {} : { variant }),
60
+ };
61
+ }
62
+ throw new WireError(`${path}._tag: expected "Message" or "Draft"`);
63
+ }
64
+ function labels(value, path) {
65
+ const rec = record(value, path);
66
+ const out = {};
67
+ for (const [key, label] of Object.entries(rec))
68
+ out[key] = text(label, `${path}.${key}`, false);
69
+ return out;
70
+ }
71
+ function decode(code, read) {
72
+ try {
73
+ return read();
74
+ }
75
+ catch (err) {
76
+ if (err instanceof WireError)
77
+ throw new PluginCallError(code, err.message);
78
+ throw err;
79
+ }
80
+ }
81
+ /** Reads the args of a send hook call. Throws `PluginCallError("INVALID_INPUT")` naming the bad field. */
82
+ export function decodeSendHookArgs(raw) {
83
+ return decode("INVALID_INPUT", () => {
84
+ const rec = record(raw, "args", ["subject", "case", "toRole", "seat", "rendered"]);
85
+ const kase = record(rec["case"], "args.case", ["id", "protocol", "project", "scope"]);
86
+ const seat = record(rec["seat"], "args.seat", ["id", "role", "labels"]);
87
+ const project = optionalName(kase, "project", "args.case");
88
+ const scope = optionalName(kase, "scope", "args.case");
89
+ return {
90
+ subject: subject(rec["subject"], "args.subject"),
91
+ case: {
92
+ id: name(kase, "id", "args.case"),
93
+ protocol: name(kase, "protocol", "args.case"),
94
+ ...(project === undefined ? {} : { project }),
95
+ ...(scope === undefined ? {} : { scope }),
96
+ },
97
+ toRole: name(rec, "toRole", "args"),
98
+ seat: {
99
+ id: name(seat, "id", "args.seat"),
100
+ role: name(seat, "role", "args.seat"),
101
+ labels: labels(seat["labels"], "args.seat.labels"),
102
+ },
103
+ rendered: text(rec["rendered"], "args.rendered", false),
104
+ };
105
+ });
106
+ }
107
+ /** Reads a send hook verdict as telar-core does. Throws `PluginCallError("INVALID_VERDICT")`. */
108
+ export function decodeSendVerdict(raw) {
109
+ return decode("INVALID_VERDICT", () => {
110
+ const allow = record(raw, "verdict", ["allow", "reason"])["allow"];
111
+ if (allow === true) {
112
+ record(raw, "verdict", ["allow"]);
113
+ return { allow: true };
114
+ }
115
+ if (allow === false) {
116
+ return {
117
+ allow: false,
118
+ reason: name(record(raw, "verdict", ["allow", "reason"]), "reason", "verdict"),
119
+ };
120
+ }
121
+ throw new WireError("verdict.allow: expected a boolean");
122
+ });
123
+ }