@aitoroses/telar 0.0.0-stage → 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.
- package/README.md +453 -2
- package/dist/compile.d.ts +48 -0
- package/dist/compile.js +1238 -0
- package/dist/contract.d.ts +109 -0
- package/dist/contract.js +497 -0
- package/dist/evaluator.d.ts +23 -0
- package/dist/evaluator.js +40 -0
- package/dist/guide.d.ts +8 -0
- package/dist/guide.js +16 -0
- package/dist/install.d.ts +104 -0
- package/dist/install.js +402 -0
- package/dist/iso.d.ts +8 -0
- package/dist/iso.js +68 -0
- package/dist/node/build.d.ts +1 -0
- package/dist/node/build.js +34 -0
- package/dist/node/telar-module.d.ts +2 -0
- package/dist/node/telar-module.js +28 -0
- package/dist/observer.d.ts +47 -0
- package/dist/observer.js +53 -0
- package/dist/pieces.d.ts +230 -0
- package/dist/pieces.js +178 -0
- package/dist/powers.d.ts +46 -0
- package/dist/powers.js +126 -0
- package/dist/protocol.d.ts +212 -0
- package/dist/protocol.js +236 -0
- package/dist/query.d.ts +67 -0
- package/dist/query.js +102 -0
- package/dist/read.d.ts +37 -0
- package/dist/read.js +27 -0
- package/dist/runner.d.ts +112 -0
- package/dist/runner.js +134 -0
- package/dist/scenario/builder.d.ts +25 -0
- package/dist/scenario/builder.js +603 -0
- package/dist/scenario/templates.d.ts +35 -0
- package/dist/scenario/templates.js +64 -0
- package/dist/scenario/types.d.ts +441 -0
- package/dist/scenario/types.js +10 -0
- package/dist/scenario/validate.d.ts +7 -0
- package/dist/scenario/validate.js +19 -0
- package/dist/scenario.d.ts +4 -0
- package/dist/scenario.js +4 -0
- package/dist/schema.d.ts +219 -0
- package/dist/schema.js +221 -0
- package/dist/seating.d.ts +49 -0
- package/dist/seating.js +26 -0
- package/dist/send-hook.d.ts +46 -0
- package/dist/send-hook.js +123 -0
- package/dist/show.d.ts +34 -0
- package/dist/show.js +1 -0
- package/dist/source.d.ts +85 -0
- package/dist/source.js +104 -0
- package/dist/spec.d.ts +20 -0
- package/dist/spec.js +17 -0
- package/dist/standard-schema.d.ts +21 -0
- package/dist/standard-schema.js +1 -0
- package/dist/std/accuracy.d.ts +12 -0
- package/dist/std/accuracy.js +122 -0
- package/dist/std/ack.d.ts +14 -0
- package/dist/std/ack.js +43 -0
- package/dist/std/actions.d.ts +106 -0
- package/dist/std/actions.js +154 -0
- package/dist/std/brief.d.ts +36 -0
- package/dist/std/brief.js +78 -0
- package/dist/std/claude-code.d.ts +9 -0
- package/dist/std/claude-code.js +106 -0
- package/dist/std/compare.d.ts +29 -0
- package/dist/std/compare.js +36 -0
- package/dist/std/git-workspace.d.ts +44 -0
- package/dist/std/git-workspace.js +184 -0
- package/dist/std/judgment.d.ts +85 -0
- package/dist/std/judgment.js +462 -0
- package/dist/std/observe-command.d.ts +23 -0
- package/dist/std/observe-command.js +151 -0
- package/dist/std/observe-github-checks.d.ts +26 -0
- package/dist/std/observe-github-checks.js +299 -0
- package/dist/std/observe-http-status.d.ts +27 -0
- package/dist/std/observe-http-status.js +181 -0
- package/dist/std/plan.d.ts +7 -0
- package/dist/std/plan.js +28 -0
- package/dist/std/queries.d.ts +31 -0
- package/dist/std/queries.js +318 -0
- package/dist/std/reading.d.ts +17 -0
- package/dist/std/reading.js +88 -0
- package/dist/std/schedule.d.ts +19 -0
- package/dist/std/schedule.js +122 -0
- package/dist/std.d.ts +15 -0
- package/dist/std.js +15 -0
- package/dist/types.d.ts +366 -0
- package/dist/types.js +1 -0
- package/dist/values.d.ts +81 -0
- package/dist/values.js +129 -0
- package/package.json +65 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { WorldPowers } from "./powers.js";
|
|
2
|
+
import { type RunnerOptionValidation } from "./runner.js";
|
|
3
|
+
import type { StandardSchema } from "./standard-schema.js";
|
|
4
|
+
export type ObserverId = `${string}/observe/${string}/${number}`;
|
|
5
|
+
export interface EvidenceRef {
|
|
6
|
+
readonly kind: string;
|
|
7
|
+
readonly id?: string;
|
|
8
|
+
readonly url?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Evidence {
|
|
11
|
+
readonly refs?: readonly EvidenceRef[];
|
|
12
|
+
readonly status?: number;
|
|
13
|
+
readonly durationMs?: number;
|
|
14
|
+
readonly readings?: Readonly<Record<string, number>>;
|
|
15
|
+
}
|
|
16
|
+
/** What the world says about a check: `ok: null` means "not yet" and writes nothing. */
|
|
17
|
+
export interface Observed {
|
|
18
|
+
readonly ok: boolean | null;
|
|
19
|
+
readonly evidence?: Evidence;
|
|
20
|
+
}
|
|
21
|
+
export interface ObserveInput<Params> {
|
|
22
|
+
readonly check: string;
|
|
23
|
+
readonly args: Readonly<Record<string, unknown>>;
|
|
24
|
+
readonly params: Params;
|
|
25
|
+
}
|
|
26
|
+
export interface DefineObserverInput<Id extends ObserverId, Params> {
|
|
27
|
+
readonly id: Id;
|
|
28
|
+
readonly params: StandardSchema<Params>;
|
|
29
|
+
readonly observe: (input: ObserveInput<Params>, powers: WorldPowers) => Observed;
|
|
30
|
+
}
|
|
31
|
+
export interface AnyObserver {
|
|
32
|
+
readonly id: ObserverId;
|
|
33
|
+
readonly admitParams: (raw: unknown) => RunnerOptionValidation<unknown>;
|
|
34
|
+
readonly dispatch: (args: unknown, powers: WorldPowers) => Observed;
|
|
35
|
+
}
|
|
36
|
+
export interface Observer<Id extends ObserverId = ObserverId, Params = unknown> extends AnyObserver {
|
|
37
|
+
readonly id: Id;
|
|
38
|
+
readonly params: StandardSchema<Params>;
|
|
39
|
+
readonly observe: DefineObserverInput<Id, Params>["observe"];
|
|
40
|
+
}
|
|
41
|
+
export declare class ObserverError extends Error {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly code: string;
|
|
44
|
+
constructor(code: string, message: string);
|
|
45
|
+
}
|
|
46
|
+
export declare function isValidObserverId(id: string): boolean;
|
|
47
|
+
export declare function defineObserver<const Id extends ObserverId, Params>(input: DefineObserverInput<Id, Params>): Observer<Id, Params>;
|
package/dist/observer.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { validateStandard } from "./runner.js";
|
|
2
|
+
export class ObserverError extends Error {
|
|
3
|
+
name = "ObserverError";
|
|
4
|
+
code;
|
|
5
|
+
constructor(code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const OBSERVER_ID_RE = /^[a-z][a-z0-9.-]*\/observe\/[a-z][a-z0-9-]*\/[1-9][0-9]*$/;
|
|
11
|
+
export function isValidObserverId(id) {
|
|
12
|
+
return OBSERVER_ID_RE.test(id);
|
|
13
|
+
}
|
|
14
|
+
function isRec(value) {
|
|
15
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
function issueText(issues) {
|
|
18
|
+
const first = issues[0];
|
|
19
|
+
if (first === undefined)
|
|
20
|
+
return "invalid";
|
|
21
|
+
return first.path.length > 0 ? `${first.path.join(".")}: ${first.message}` : first.message;
|
|
22
|
+
}
|
|
23
|
+
export function defineObserver(input) {
|
|
24
|
+
if (!isValidObserverId(input.id)) {
|
|
25
|
+
throw new ObserverError("INVALID_OBSERVER", `observer id ${JSON.stringify(input.id)} must match <namespace>/observe/<name>/<v>`);
|
|
26
|
+
}
|
|
27
|
+
const admitParams = (raw) => {
|
|
28
|
+
const checked = validateStandard(input.params, raw === undefined ? {} : raw);
|
|
29
|
+
if (checked.ok === "async") {
|
|
30
|
+
throw new ObserverError("INVALID_OBSERVER", `observer ${input.id} params must validate synchronously`);
|
|
31
|
+
}
|
|
32
|
+
return checked;
|
|
33
|
+
};
|
|
34
|
+
const dispatch = (args, powers) => {
|
|
35
|
+
const check = isRec(args) ? args["check"] : undefined;
|
|
36
|
+
const checkArgs = isRec(args) ? args["args"] : undefined;
|
|
37
|
+
if (typeof check !== "string" || !isRec(checkArgs)) {
|
|
38
|
+
throw new ObserverError("INVALID_INPUT", "observer:observe requires a check id and its args");
|
|
39
|
+
}
|
|
40
|
+
const params = admitParams(isRec(args) ? args["params"] : undefined);
|
|
41
|
+
if (!params.ok) {
|
|
42
|
+
throw new ObserverError("INVALID_OBSERVER_PARAMS", issueText(params.issues));
|
|
43
|
+
}
|
|
44
|
+
return input.observe({ check, args: checkArgs, params: params.value }, powers);
|
|
45
|
+
};
|
|
46
|
+
return Object.freeze({
|
|
47
|
+
id: input.id,
|
|
48
|
+
params: input.params,
|
|
49
|
+
observe: input.observe,
|
|
50
|
+
admitParams,
|
|
51
|
+
dispatch,
|
|
52
|
+
});
|
|
53
|
+
}
|
package/dist/pieces.d.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { EvaluatorId, StdEvaluatorId } from "./evaluator.js";
|
|
2
|
+
import type { Guide } from "./guide.js";
|
|
3
|
+
import type { WorldPowers } from "./powers.js";
|
|
4
|
+
import type { AnyContract, AnyStructContract, FieldHandlesOf, FieldSchemaMap, Infer, OptionalType, StructContract, StructFieldsOfContract } from "./schema.js";
|
|
5
|
+
import type { BriefPiece, CheckObserveDecl, JsonValue, MessageFieldName, NonEmpty, Rec, ViewKind } from "./types.js";
|
|
6
|
+
import type { ArgInput, FieldRef, LiteralRef } from "./values.js";
|
|
7
|
+
export type SeatLabelsDef = Readonly<Record<string, NonEmpty<string>>>;
|
|
8
|
+
export type NoSeatLabels = {
|
|
9
|
+
readonly [key: string]: never;
|
|
10
|
+
};
|
|
11
|
+
export interface Role<Name extends string = string, Labels extends SeatLabelsDef = SeatLabelsDef> {
|
|
12
|
+
readonly kind: "role";
|
|
13
|
+
readonly name: Name;
|
|
14
|
+
readonly view: ViewKind;
|
|
15
|
+
readonly includePredecessor?: boolean;
|
|
16
|
+
readonly seatLabels?: Labels;
|
|
17
|
+
readonly guide?: string;
|
|
18
|
+
readonly brief?: readonly BriefPiece[];
|
|
19
|
+
}
|
|
20
|
+
export type AnyRole = Role;
|
|
21
|
+
export interface RoleViewOverride {
|
|
22
|
+
readonly kind: "role-view";
|
|
23
|
+
readonly role: AnyRole;
|
|
24
|
+
readonly view: ViewKind;
|
|
25
|
+
readonly includePredecessor?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface RoleBriefOverride {
|
|
28
|
+
readonly kind: "role-brief";
|
|
29
|
+
readonly role: AnyRole;
|
|
30
|
+
readonly pieces: readonly BriefPiece[];
|
|
31
|
+
}
|
|
32
|
+
export interface DelegationInput {
|
|
33
|
+
readonly role: AnyRole;
|
|
34
|
+
readonly for: AnyRole;
|
|
35
|
+
}
|
|
36
|
+
export interface AuthClass<Name extends string = string> {
|
|
37
|
+
readonly kind: "class";
|
|
38
|
+
readonly name: Name;
|
|
39
|
+
readonly authorized: NonEmpty<AnyRole>;
|
|
40
|
+
readonly delegatedWhenAway?: readonly DelegationInput[];
|
|
41
|
+
readonly requires: readonly AnyCheck[];
|
|
42
|
+
readonly admitsImpossible: boolean;
|
|
43
|
+
readonly guide?: string;
|
|
44
|
+
}
|
|
45
|
+
export type AnyAuthClass = AuthClass;
|
|
46
|
+
export type ArgsForContract<C extends AnyStructContract> = C extends StructContract<string, infer F> ? {
|
|
47
|
+
readonly [K in keyof F as F[K] extends OptionalType ? never : K]: ArgInput<Infer<F[K]>>;
|
|
48
|
+
} & {
|
|
49
|
+
readonly [K in keyof F as F[K] extends OptionalType ? K : never]?: ArgInput<F[K] extends OptionalType<infer Inner> ? Infer<Inner> : never>;
|
|
50
|
+
} : never;
|
|
51
|
+
export interface CheckNeedNode {
|
|
52
|
+
readonly kind: "check-need";
|
|
53
|
+
readonly check: AnyCheck;
|
|
54
|
+
readonly args: Rec<unknown>;
|
|
55
|
+
readonly guide?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface Check<Id extends string = string, In extends AnyStructContract = AnyStructContract> {
|
|
58
|
+
readonly kind: "check";
|
|
59
|
+
readonly id: Id;
|
|
60
|
+
readonly input: In;
|
|
61
|
+
readonly evaluator?: string;
|
|
62
|
+
readonly observe?: CheckObserveDecl;
|
|
63
|
+
readonly guide?: string;
|
|
64
|
+
(args: ArgsForContract<In>, opts?: {
|
|
65
|
+
readonly guide?: Guide | ((input: FieldHandlesOf<StructFieldsOfContract<In>>) => Guide) | undefined;
|
|
66
|
+
}): CheckNeedNode;
|
|
67
|
+
}
|
|
68
|
+
export type AnyCheck = Check;
|
|
69
|
+
export interface CallActionEffectNode {
|
|
70
|
+
readonly kind: "call-action-effect";
|
|
71
|
+
readonly action: AnyAction;
|
|
72
|
+
readonly args: Rec<unknown>;
|
|
73
|
+
}
|
|
74
|
+
export interface Action<Id extends string = string, In extends AnyStructContract = AnyStructContract, Out extends AnyContract = AnyContract> {
|
|
75
|
+
readonly kind: "action";
|
|
76
|
+
readonly id: Id;
|
|
77
|
+
readonly input: In;
|
|
78
|
+
readonly output: Out;
|
|
79
|
+
readonly class?: AnyAuthClass;
|
|
80
|
+
readonly revert?: AnyAction;
|
|
81
|
+
readonly release?: AnyAction;
|
|
82
|
+
readonly reversible?: boolean;
|
|
83
|
+
readonly guide?: string;
|
|
84
|
+
run?(input: Infer<In>, powers: WorldPowers): Infer<Out>;
|
|
85
|
+
(args: ArgsForContract<In>): CallActionEffectNode;
|
|
86
|
+
}
|
|
87
|
+
export type AnyAction = Action;
|
|
88
|
+
export interface RunnableAction<Id extends string = string, In extends AnyStructContract = AnyStructContract, Out extends AnyContract = AnyContract> extends Action<Id, In, Out> {
|
|
89
|
+
run(input: Infer<In>, powers: WorldPowers): Infer<Out>;
|
|
90
|
+
}
|
|
91
|
+
export interface MessageArgRef {
|
|
92
|
+
readonly kind: "message-arg";
|
|
93
|
+
readonly field: MessageFieldName;
|
|
94
|
+
}
|
|
95
|
+
export type MissArgInput = MessageArgRef | LiteralRef | JsonValue;
|
|
96
|
+
export interface ProtocolTarget {
|
|
97
|
+
readonly kind: "protocol" | "protocol-ref";
|
|
98
|
+
readonly name: string;
|
|
99
|
+
}
|
|
100
|
+
export type MissActionDef = {
|
|
101
|
+
readonly kind: "redeliver";
|
|
102
|
+
} | {
|
|
103
|
+
readonly kind: "open-on-miss";
|
|
104
|
+
readonly protocol: ProtocolTarget;
|
|
105
|
+
readonly forRole: AnyRole;
|
|
106
|
+
readonly data: Rec<MissArgInput>;
|
|
107
|
+
};
|
|
108
|
+
export type AckDef = {
|
|
109
|
+
readonly kind: "none";
|
|
110
|
+
} | {
|
|
111
|
+
readonly kind: "read";
|
|
112
|
+
readonly within: number;
|
|
113
|
+
readonly onMiss: NonEmpty<MissActionDef>;
|
|
114
|
+
} | {
|
|
115
|
+
readonly kind: "answer";
|
|
116
|
+
readonly shape: "ack" | AnyContract;
|
|
117
|
+
readonly within: number;
|
|
118
|
+
readonly onMiss: NonEmpty<MissActionDef>;
|
|
119
|
+
};
|
|
120
|
+
export interface MessageKind<Name extends string = string, P extends AnyStructContract = AnyStructContract> {
|
|
121
|
+
readonly kind: "message-kind";
|
|
122
|
+
readonly name: Name;
|
|
123
|
+
readonly payload: P;
|
|
124
|
+
readonly template: Guide;
|
|
125
|
+
readonly ack: AckDef;
|
|
126
|
+
}
|
|
127
|
+
export type AnyMessageKind = MessageKind;
|
|
128
|
+
export interface BoundByFieldInput {
|
|
129
|
+
readonly field: string | FieldRef;
|
|
130
|
+
readonly values: Rec<number>;
|
|
131
|
+
}
|
|
132
|
+
export interface BoundSpecInput {
|
|
133
|
+
readonly max: number;
|
|
134
|
+
readonly byField?: BoundByFieldInput | undefined;
|
|
135
|
+
readonly onLimit: AnyRole;
|
|
136
|
+
readonly guide?: Guide | undefined;
|
|
137
|
+
}
|
|
138
|
+
export interface InlineBound {
|
|
139
|
+
readonly kind: "inline-bound";
|
|
140
|
+
readonly spec: BoundSpecInput;
|
|
141
|
+
}
|
|
142
|
+
export interface NamedBound<Name extends string = string> {
|
|
143
|
+
readonly kind: "named-bound";
|
|
144
|
+
readonly name: Name;
|
|
145
|
+
readonly spec: BoundSpecInput;
|
|
146
|
+
}
|
|
147
|
+
export type AnyNamedBound = NamedBound;
|
|
148
|
+
export type StepBoundInput = InlineBound | AnyNamedBound;
|
|
149
|
+
export declare function role<const Name extends string, const Labels extends SeatLabelsDef = NoSeatLabels>(name: Name, def: {
|
|
150
|
+
readonly view: ViewKind;
|
|
151
|
+
readonly includePredecessor?: boolean | undefined;
|
|
152
|
+
readonly seatLabels?: Labels | undefined;
|
|
153
|
+
readonly guide?: Guide | undefined;
|
|
154
|
+
readonly brief?: readonly BriefPiece[] | undefined;
|
|
155
|
+
}): Role<Name, Labels>;
|
|
156
|
+
export declare function roleView(target: AnyRole, view: ViewKind, opts?: {
|
|
157
|
+
readonly includePredecessor?: boolean | undefined;
|
|
158
|
+
}): RoleViewOverride;
|
|
159
|
+
export declare function roleBrief(target: AnyRole, pieces: readonly BriefPiece[]): RoleBriefOverride;
|
|
160
|
+
export declare function defineClass<const Name extends string>(name: Name, def: {
|
|
161
|
+
readonly authorized: NonEmpty<AnyRole>;
|
|
162
|
+
readonly delegatedWhenAway?: readonly DelegationInput[] | undefined;
|
|
163
|
+
readonly requires?: readonly AnyCheck[] | undefined;
|
|
164
|
+
readonly admitsImpossible?: boolean | undefined;
|
|
165
|
+
readonly guide?: Guide | undefined;
|
|
166
|
+
}): AuthClass<Name>;
|
|
167
|
+
export declare function check<const Id extends string, InName extends string, F extends FieldSchemaMap>(id: Id, def: {
|
|
168
|
+
readonly input: StructContract<InName, F>;
|
|
169
|
+
readonly evaluator?: EvaluatorId | StdEvaluatorId | undefined;
|
|
170
|
+
readonly observe?: CheckObserveDecl | undefined;
|
|
171
|
+
readonly guide?: Guide | ((input: FieldHandlesOf<F>) => Guide) | undefined;
|
|
172
|
+
}): Check<Id, StructContract<InName, F>>;
|
|
173
|
+
export type ActionId = `${string}/action/${string}/${number}`;
|
|
174
|
+
export declare class ActionError extends Error {
|
|
175
|
+
readonly name: string;
|
|
176
|
+
readonly code: string;
|
|
177
|
+
constructor(code: string, detail: string);
|
|
178
|
+
}
|
|
179
|
+
export declare function isValidActionId(id: string): boolean;
|
|
180
|
+
interface ActionShape<Id extends ActionId, InName extends string, F extends FieldSchemaMap, Out> {
|
|
181
|
+
readonly id: Id;
|
|
182
|
+
readonly input: StructContract<InName, F>;
|
|
183
|
+
readonly output: Out;
|
|
184
|
+
readonly class?: AnyAuthClass | undefined;
|
|
185
|
+
readonly revert?: AnyAction | undefined;
|
|
186
|
+
readonly release?: AnyAction | undefined;
|
|
187
|
+
readonly reversible?: boolean | undefined;
|
|
188
|
+
readonly guide?: Guide | ((input: FieldHandlesOf<F>) => Guide) | undefined;
|
|
189
|
+
}
|
|
190
|
+
/** The step handle of an action the host runs itself: the std actions still in Rust. */
|
|
191
|
+
export declare function declareAction<const Id extends ActionId, InName extends string, F extends FieldSchemaMap, Out extends AnyContract>(def: ActionShape<Id, InName, F, Out>): Action<Id, StructContract<InName, F>, Out>;
|
|
192
|
+
/**
|
|
193
|
+
* An action is its step handle and its implementation. List it in `defineInstallation({ actions })`;
|
|
194
|
+
* the host calls `run` with the requested input and records what it returns. Refuse with
|
|
195
|
+
* `throw new ActionError(code, message)`.
|
|
196
|
+
*/
|
|
197
|
+
export declare function defineAction<const Id extends ActionId, InName extends string, F extends FieldSchemaMap, Out extends AnyContract>(def: ActionShape<Id, InName, F, Out> & {
|
|
198
|
+
readonly run: (input: Infer<StructContract<InName, F>>, powers: WorldPowers) => Infer<Out>;
|
|
199
|
+
}): RunnableAction<Id, StructContract<InName, F>, Out>;
|
|
200
|
+
export declare const msg: {
|
|
201
|
+
readonly id: MessageArgRef;
|
|
202
|
+
readonly kind: MessageArgRef;
|
|
203
|
+
readonly toRole: MessageArgRef;
|
|
204
|
+
readonly case: MessageArgRef;
|
|
205
|
+
};
|
|
206
|
+
export declare function redeliver(): MissActionDef;
|
|
207
|
+
export declare function openOnMiss(target: ProtocolTarget, opts: {
|
|
208
|
+
readonly for: AnyRole;
|
|
209
|
+
readonly data: Rec<MissArgInput>;
|
|
210
|
+
}): MissActionDef;
|
|
211
|
+
export declare const ack: {
|
|
212
|
+
readonly none: () => AckDef;
|
|
213
|
+
readonly read: (opts: {
|
|
214
|
+
readonly within: number;
|
|
215
|
+
readonly onMiss: NonEmpty<MissActionDef>;
|
|
216
|
+
}) => AckDef;
|
|
217
|
+
readonly answer: (opts: {
|
|
218
|
+
readonly shape: "ack" | AnyContract;
|
|
219
|
+
readonly within: number;
|
|
220
|
+
readonly onMiss: NonEmpty<MissActionDef>;
|
|
221
|
+
}) => AckDef;
|
|
222
|
+
};
|
|
223
|
+
export declare function message<const Name extends string, F extends FieldSchemaMap>(name: Name, def: {
|
|
224
|
+
readonly payload: StructContract<string, F>;
|
|
225
|
+
readonly template: Guide | ((payload: FieldHandlesOf<F>) => Guide);
|
|
226
|
+
readonly ack: AckDef;
|
|
227
|
+
}): MessageKind<Name, StructContract<string, F>>;
|
|
228
|
+
export declare function bound(def: BoundSpecInput): InlineBound;
|
|
229
|
+
export declare function bound<const Name extends string>(name: Name, def: BoundSpecInput): NamedBound<Name>;
|
|
230
|
+
export {};
|
package/dist/pieces.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
export function role(name, def) {
|
|
2
|
+
return Object.freeze({
|
|
3
|
+
kind: "role",
|
|
4
|
+
name,
|
|
5
|
+
view: def.view,
|
|
6
|
+
...(def.includePredecessor !== undefined ? { includePredecessor: def.includePredecessor } : {}),
|
|
7
|
+
...(def.seatLabels !== undefined ? { seatLabels: def.seatLabels } : {}),
|
|
8
|
+
...(def.guide !== undefined ? { guide: def.guide.template } : {}),
|
|
9
|
+
...(def.brief !== undefined ? { brief: Object.freeze([...def.brief]) } : {}),
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export function roleView(target, view, opts) {
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
kind: "role-view",
|
|
15
|
+
role: target,
|
|
16
|
+
view,
|
|
17
|
+
...(opts?.includePredecessor !== undefined
|
|
18
|
+
? { includePredecessor: opts.includePredecessor }
|
|
19
|
+
: {}),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export function roleBrief(target, pieces) {
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
kind: "role-brief",
|
|
25
|
+
role: target,
|
|
26
|
+
pieces: Object.freeze([...pieces]),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function freezeNonEmpty(items) {
|
|
30
|
+
const [first, ...rest] = items;
|
|
31
|
+
return Object.freeze([first, ...rest]);
|
|
32
|
+
}
|
|
33
|
+
export function defineClass(name, def) {
|
|
34
|
+
return Object.freeze({
|
|
35
|
+
kind: "class",
|
|
36
|
+
name,
|
|
37
|
+
authorized: freezeNonEmpty(def.authorized),
|
|
38
|
+
...(def.delegatedWhenAway !== undefined
|
|
39
|
+
? { delegatedWhenAway: Object.freeze([...def.delegatedWhenAway]) }
|
|
40
|
+
: {}),
|
|
41
|
+
requires: Object.freeze([...(def.requires ?? [])]),
|
|
42
|
+
admitsImpossible: def.admitsImpossible ?? false,
|
|
43
|
+
...(def.guide !== undefined ? { guide: def.guide.template } : {}),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export function check(id, def) {
|
|
47
|
+
const resolveCheckGuide = (raw) => {
|
|
48
|
+
if (raw === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
if (typeof raw === "function") {
|
|
51
|
+
return raw(def.input.fields).template;
|
|
52
|
+
}
|
|
53
|
+
return raw.template;
|
|
54
|
+
};
|
|
55
|
+
const fn = (args, opts) => {
|
|
56
|
+
const g = resolveCheckGuide(opts?.guide);
|
|
57
|
+
return Object.freeze({
|
|
58
|
+
kind: "check-need",
|
|
59
|
+
check: callable,
|
|
60
|
+
args,
|
|
61
|
+
...(g !== undefined ? { guide: g } : {}),
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
const declGuide = resolveCheckGuide(def.guide);
|
|
65
|
+
const callable = Object.freeze(Object.assign(fn, {
|
|
66
|
+
kind: "check",
|
|
67
|
+
id,
|
|
68
|
+
input: def.input,
|
|
69
|
+
...(def.evaluator !== undefined ? { evaluator: def.evaluator } : {}),
|
|
70
|
+
...(def.observe !== undefined ? { observe: def.observe } : {}),
|
|
71
|
+
...(declGuide !== undefined ? { guide: declGuide } : {}),
|
|
72
|
+
}));
|
|
73
|
+
return callable;
|
|
74
|
+
}
|
|
75
|
+
export class ActionError extends Error {
|
|
76
|
+
name = "ActionError";
|
|
77
|
+
code;
|
|
78
|
+
constructor(code, detail) {
|
|
79
|
+
super(detail);
|
|
80
|
+
this.code = code;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const ACTION_ID_RE = /^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)*\/action\/[a-z][a-z0-9-]*\/[1-9][0-9]*$/;
|
|
84
|
+
export function isValidActionId(id) {
|
|
85
|
+
return ACTION_ID_RE.test(id);
|
|
86
|
+
}
|
|
87
|
+
function buildAction(def, extra) {
|
|
88
|
+
if (!isValidActionId(def.id)) {
|
|
89
|
+
throw new ActionError("INVALID_ACTION", `action id ${JSON.stringify(def.id)} must match <installation>/action/<name>/<version>`);
|
|
90
|
+
}
|
|
91
|
+
const declGuide = def.guide === undefined
|
|
92
|
+
? undefined
|
|
93
|
+
: typeof def.guide === "function"
|
|
94
|
+
? def.guide(def.input.fields).template
|
|
95
|
+
: def.guide.template;
|
|
96
|
+
const fn = (args) => Object.freeze({ kind: "call-action-effect", action: callable, args });
|
|
97
|
+
const callable = Object.freeze(Object.assign(fn, {
|
|
98
|
+
kind: "action",
|
|
99
|
+
id: def.id,
|
|
100
|
+
input: def.input,
|
|
101
|
+
output: def.output,
|
|
102
|
+
...(def.class !== undefined ? { class: def.class } : {}),
|
|
103
|
+
...(def.revert !== undefined ? { revert: def.revert } : {}),
|
|
104
|
+
...(def.release !== undefined ? { release: def.release } : {}),
|
|
105
|
+
...(def.reversible !== undefined ? { reversible: def.reversible } : {}),
|
|
106
|
+
...(declGuide !== undefined ? { guide: declGuide } : {}),
|
|
107
|
+
...extra,
|
|
108
|
+
}));
|
|
109
|
+
return callable;
|
|
110
|
+
}
|
|
111
|
+
/** The step handle of an action the host runs itself: the std actions still in Rust. */
|
|
112
|
+
export function declareAction(def) {
|
|
113
|
+
return buildAction(def, {});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* An action is its step handle and its implementation. List it in `defineInstallation({ actions })`;
|
|
117
|
+
* the host calls `run` with the requested input and records what it returns. Refuse with
|
|
118
|
+
* `throw new ActionError(code, message)`.
|
|
119
|
+
*/
|
|
120
|
+
export function defineAction(def) {
|
|
121
|
+
return buildAction(def, { run: def.run });
|
|
122
|
+
}
|
|
123
|
+
export const msg = Object.freeze({
|
|
124
|
+
id: Object.freeze({ kind: "message-arg", field: "id" }),
|
|
125
|
+
kind: Object.freeze({ kind: "message-arg", field: "kind" }),
|
|
126
|
+
toRole: Object.freeze({ kind: "message-arg", field: "toRole" }),
|
|
127
|
+
case: Object.freeze({ kind: "message-arg", field: "case" }),
|
|
128
|
+
});
|
|
129
|
+
export function redeliver() {
|
|
130
|
+
return Object.freeze({ kind: "redeliver" });
|
|
131
|
+
}
|
|
132
|
+
export function openOnMiss(target, opts) {
|
|
133
|
+
return Object.freeze({
|
|
134
|
+
kind: "open-on-miss",
|
|
135
|
+
protocol: target,
|
|
136
|
+
forRole: opts.for,
|
|
137
|
+
data: opts.data,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
export const ack = Object.freeze({
|
|
141
|
+
none() {
|
|
142
|
+
return Object.freeze({ kind: "none" });
|
|
143
|
+
},
|
|
144
|
+
read(opts) {
|
|
145
|
+
return Object.freeze({
|
|
146
|
+
kind: "read",
|
|
147
|
+
within: opts.within,
|
|
148
|
+
onMiss: freezeNonEmpty(opts.onMiss),
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
answer(opts) {
|
|
152
|
+
return Object.freeze({
|
|
153
|
+
kind: "answer",
|
|
154
|
+
shape: opts.shape,
|
|
155
|
+
within: opts.within,
|
|
156
|
+
onMiss: freezeNonEmpty(opts.onMiss),
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
export function message(name, def) {
|
|
161
|
+
const resolved = typeof def.template === "function" ? def.template(def.payload.fields) : def.template;
|
|
162
|
+
return Object.freeze({
|
|
163
|
+
kind: "message-kind",
|
|
164
|
+
name,
|
|
165
|
+
payload: def.payload,
|
|
166
|
+
template: resolved,
|
|
167
|
+
ack: def.ack,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
export function bound(nameOrDef, maybeDef) {
|
|
171
|
+
if (typeof nameOrDef === "string") {
|
|
172
|
+
if (maybeDef === undefined) {
|
|
173
|
+
throw new Error(`bound(${JSON.stringify(nameOrDef)}) requires a bound definition`);
|
|
174
|
+
}
|
|
175
|
+
return Object.freeze({ kind: "named-bound", name: nameOrDef, spec: maybeDef });
|
|
176
|
+
}
|
|
177
|
+
return Object.freeze({ kind: "inline-bound", spec: nameOrDef });
|
|
178
|
+
}
|
package/dist/powers.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Read } from "./read.js";
|
|
2
|
+
export interface WorldProcessRequest {
|
|
3
|
+
readonly executable: string;
|
|
4
|
+
readonly args?: readonly string[];
|
|
5
|
+
readonly stdin?: string;
|
|
6
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
7
|
+
readonly cwd?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface WorldProcessResult {
|
|
10
|
+
readonly exitCode: number;
|
|
11
|
+
readonly stdout: string;
|
|
12
|
+
readonly stderr: string;
|
|
13
|
+
readonly startedAt: string;
|
|
14
|
+
readonly endedAt: string;
|
|
15
|
+
}
|
|
16
|
+
export type WorldHttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
|
|
17
|
+
export interface WorldNetworkRequest {
|
|
18
|
+
readonly url: string;
|
|
19
|
+
readonly method?: WorldHttpMethod;
|
|
20
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
21
|
+
readonly body?: string;
|
|
22
|
+
/** Secret name in secrets.json, resolved in Rust after origin + origin-secret grant pass (HTTPS or loopback HTTP only). */
|
|
23
|
+
readonly bearerSecret?: string;
|
|
24
|
+
readonly timeoutMs?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface WorldNetworkResponse {
|
|
27
|
+
readonly status: number;
|
|
28
|
+
/** Lowercased header names; repeated headers (except set-cookie) are joined with ", ". */
|
|
29
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
30
|
+
readonly body: string;
|
|
31
|
+
readonly startedAt: string;
|
|
32
|
+
readonly endedAt: string;
|
|
33
|
+
}
|
|
34
|
+
export interface WorldPowers {
|
|
35
|
+
readonly process: (req: WorldProcessRequest) => WorldProcessResult;
|
|
36
|
+
readonly network: (req: WorldNetworkRequest) => WorldNetworkResponse;
|
|
37
|
+
readonly read: Read;
|
|
38
|
+
/** Absolute path of `config` and of each root granted under `roster.grants[<namespace>].roots`. */
|
|
39
|
+
readonly roots: Readonly<Record<string, string>>;
|
|
40
|
+
}
|
|
41
|
+
export declare class PowerError extends Error {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly code: string;
|
|
44
|
+
constructor(code: string, message: string);
|
|
45
|
+
}
|
|
46
|
+
export declare function hostWorldPowers(): WorldPowers;
|
package/dist/powers.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { bindQuery, decodeQueryRows, issueText, } from "./query.js";
|
|
2
|
+
export class PowerError extends Error {
|
|
3
|
+
name = "PowerError";
|
|
4
|
+
code;
|
|
5
|
+
constructor(code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function isRec(value) {
|
|
11
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
function callBridge(globalName, missingMessage, req) {
|
|
14
|
+
const bridge = Reflect.get(globalThis, globalName);
|
|
15
|
+
if (typeof bridge !== "function") {
|
|
16
|
+
throw new PowerError("GRANT_REFUSED", missingMessage);
|
|
17
|
+
}
|
|
18
|
+
const rawReply = Reflect.apply(bridge, undefined, [JSON.stringify(req)]);
|
|
19
|
+
if (typeof rawReply !== "string") {
|
|
20
|
+
throw new PowerError("POWER_ERROR", `host ${globalName} bridge returned a non-string reply`);
|
|
21
|
+
}
|
|
22
|
+
const parsed = JSON.parse(rawReply);
|
|
23
|
+
if (!isRec(parsed)) {
|
|
24
|
+
throw new PowerError("POWER_ERROR", `host ${globalName} bridge returned invalid JSON`);
|
|
25
|
+
}
|
|
26
|
+
if (parsed["ok"] !== true) {
|
|
27
|
+
const code = typeof parsed["code"] === "string" ? parsed["code"] : "POWER_ERROR";
|
|
28
|
+
const message = typeof parsed["message"] === "string" ? parsed["message"] : `${globalName} call failed`;
|
|
29
|
+
throw new PowerError(code, message);
|
|
30
|
+
}
|
|
31
|
+
return parsed;
|
|
32
|
+
}
|
|
33
|
+
function readTiming(parsed) {
|
|
34
|
+
const startedAt = parsed["startedAt"];
|
|
35
|
+
const endedAt = parsed["endedAt"];
|
|
36
|
+
if (typeof startedAt !== "string" || !startedAt || typeof endedAt !== "string" || !endedAt) {
|
|
37
|
+
throw new PowerError("POWER_ERROR", "host power reply requires non-empty startedAt/endedAt");
|
|
38
|
+
}
|
|
39
|
+
return { startedAt, endedAt };
|
|
40
|
+
}
|
|
41
|
+
function readHeaders(raw) {
|
|
42
|
+
if (!isRec(raw))
|
|
43
|
+
return {};
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
46
|
+
if (typeof v === "string")
|
|
47
|
+
out[k.toLowerCase()] = v;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
const hostRead = (request) => callBridge("__telar_read", 'read is not granted; declare capabilities: ["read"] and set roster.grants[<namespace>].read to true', request)["value"];
|
|
52
|
+
const hasStrings = (v, keys) => keys.every((k) => typeof v[k] === "string");
|
|
53
|
+
function isShowCase(v) {
|
|
54
|
+
return isRec(v) && hasStrings(v, ["id", "protocol", "state", "phase"]) && isRec(v["data"]);
|
|
55
|
+
}
|
|
56
|
+
function isShowSeat(v) {
|
|
57
|
+
return isRec(v) && hasStrings(v, ["id", "role", "phase"]) && isRec(v["binding"]);
|
|
58
|
+
}
|
|
59
|
+
function readReply(value, guard) {
|
|
60
|
+
if (Array.isArray(value) && value.every(guard))
|
|
61
|
+
return value;
|
|
62
|
+
throw new PowerError("POWER_ERROR", "host read bridge returned an unexpected value");
|
|
63
|
+
}
|
|
64
|
+
const HOST_READ = Object.freeze({
|
|
65
|
+
case: (id) => {
|
|
66
|
+
const value = hostRead({ op: "case", id });
|
|
67
|
+
return value === null ? undefined : readReply([value], isShowCase)[0];
|
|
68
|
+
},
|
|
69
|
+
lineage: (id) => readReply(hostRead({ op: "lineage", id }), isShowCase),
|
|
70
|
+
seats: (filter = {}) => readReply(hostRead({
|
|
71
|
+
op: "seats",
|
|
72
|
+
...(filter.role !== undefined ? { role: filter.role } : {}),
|
|
73
|
+
...(filter.eligibleFor !== undefined ? { eligibleFor: filter.eligibleFor } : {}),
|
|
74
|
+
}), isShowSeat),
|
|
75
|
+
query: (query, args) => {
|
|
76
|
+
const bound = bindQuery(query, args);
|
|
77
|
+
if (!bound.ok) {
|
|
78
|
+
throw new PowerError("INVALID_QUERY_ARGS", bound.issues.map(issueText).join("; "));
|
|
79
|
+
}
|
|
80
|
+
return decodeQueryRows(query, hostRead({ op: "query", sql: bound.sql, params: bound.params }));
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
/** The host grants each call under `roster.grants[<namespace>]` of the piece it is calling. */
|
|
84
|
+
function hostRoots() {
|
|
85
|
+
const bridge = Reflect.get(globalThis, "__telar_roots");
|
|
86
|
+
if (typeof bridge !== "function") {
|
|
87
|
+
return Object.freeze({});
|
|
88
|
+
}
|
|
89
|
+
const raw = Reflect.apply(bridge, undefined, []);
|
|
90
|
+
const parsed = typeof raw === "string" ? JSON.parse(raw) : undefined;
|
|
91
|
+
if (!isRec(parsed)) {
|
|
92
|
+
throw new PowerError("POWER_ERROR", "host roots bridge returned invalid JSON");
|
|
93
|
+
}
|
|
94
|
+
const roots = {};
|
|
95
|
+
for (const [name, path] of Object.entries(parsed)) {
|
|
96
|
+
if (typeof path !== "string") {
|
|
97
|
+
throw new PowerError("POWER_ERROR", `host root ${name} is not a path`);
|
|
98
|
+
}
|
|
99
|
+
roots[name] = path;
|
|
100
|
+
}
|
|
101
|
+
return Object.freeze(roots);
|
|
102
|
+
}
|
|
103
|
+
export function hostWorldPowers() {
|
|
104
|
+
return Object.freeze({
|
|
105
|
+
read: HOST_READ,
|
|
106
|
+
get roots() {
|
|
107
|
+
return hostRoots();
|
|
108
|
+
},
|
|
109
|
+
process(req) {
|
|
110
|
+
const parsed = callBridge("__telar_process", `process power for ${JSON.stringify(req.executable)} is not granted; declare capabilities: ["process"] and grant the executable in roster.grants`, req);
|
|
111
|
+
const exitCode = typeof parsed["exitCode"] === "number" ? parsed["exitCode"] : 1;
|
|
112
|
+
const stdout = typeof parsed["stdout"] === "string" ? parsed["stdout"] : "";
|
|
113
|
+
const stderr = typeof parsed["stderr"] === "string" ? parsed["stderr"] : "";
|
|
114
|
+
return { exitCode, stdout, stderr, ...readTiming(parsed) };
|
|
115
|
+
},
|
|
116
|
+
network(req) {
|
|
117
|
+
const parsed = callBridge("__telar_network", `network power for ${JSON.stringify(req.url)} is not granted; declare capabilities: ["network"] and grant the origin in roster.grants`, req);
|
|
118
|
+
const status = parsed["status"];
|
|
119
|
+
if (typeof status !== "number" || !Number.isInteger(status) || status < 100 || status > 599) {
|
|
120
|
+
throw new PowerError("POWER_ERROR", "network reply requires an HTTP status 100..=599");
|
|
121
|
+
}
|
|
122
|
+
const body = typeof parsed["body"] === "string" ? parsed["body"] : "";
|
|
123
|
+
return { status, headers: readHeaders(parsed["headers"]), body, ...readTiming(parsed) };
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|