@aitoroses/telar 0.0.0 → 0.2.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 +457 -3
- package/dist/compile.d.ts +48 -0
- package/dist/compile.js +1240 -0
- package/dist/contract.d.ts +109 -0
- package/dist/contract.js +491 -0
- package/dist/evaluator.d.ts +23 -0
- package/dist/evaluator.js +40 -0
- package/dist/evidence.d.ts +23 -0
- package/dist/evidence.js +74 -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 +10 -0
- package/dist/iso.js +73 -0
- package/dist/node/build.d.ts +2 -0
- package/dist/node/build.js +56 -0
- package/dist/node/telar-module.d.ts +2 -0
- package/dist/node/telar-module.js +28 -0
- package/dist/observer.d.ts +38 -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 +220 -0
- package/dist/protocol.js +243 -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 +113 -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 +432 -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 +221 -0
- package/dist/schema.js +227 -0
- package/dist/seating.d.ts +47 -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 +21 -0
- package/dist/spec.js +18 -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 +105 -0
- package/dist/std/actions.js +153 -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 +113 -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 +24 -0
- package/dist/std/observe-github-checks.js +289 -0
- package/dist/std/observe-http-status.d.ts +27 -0
- package/dist/std/observe-http-status.js +193 -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 +370 -0
- package/dist/types.js +1 -0
- package/dist/values.d.ts +81 -0
- package/dist/values.js +129 -0
- package/package.json +62 -5
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 reference ("env:NAME"), resolved in Rust after the matching (method, path) grant entry passes (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
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type { Guide } from "./guide.js";
|
|
2
|
+
import type { AckDef, AnyAuthClass, AnyMessageKind, AnyRole, ArgsForContract, CallActionEffectNode, CheckNeedNode, MessageKind, ProtocolTarget, Role, RoleBriefOverride, RoleViewOverride, SeatLabelsDef, StepBoundInput } from "./pieces.js";
|
|
3
|
+
import type { AnyContract, AnyStructContract, Brand, CaseRef, FieldHandlesOf, FieldSchemaMap, Infer, OptionalType, StructContract, StructFieldsOfContract, VariantsOf } from "./schema.js";
|
|
4
|
+
import type { BriefPiece, NonEmpty, Rec, RecordMode } from "./types.js";
|
|
5
|
+
import { type FieldRef, type FieldTable, type RecordInput } from "./values.js";
|
|
6
|
+
type LabelsOfRole<R> = R extends Role<string, infer L> ? L : never;
|
|
7
|
+
type LabelsOfRoles<R> = R extends readonly (infer Item)[] ? LabelsOfRole<Item> : LabelsOfRole<R>;
|
|
8
|
+
export type SeatLabelReq<L extends SeatLabelsDef> = {
|
|
9
|
+
readonly [K in keyof L & string]: {
|
|
10
|
+
readonly key: K;
|
|
11
|
+
readonly values: NonEmpty<L[K][number]>;
|
|
12
|
+
};
|
|
13
|
+
}[keyof L & string];
|
|
14
|
+
export interface ChildFilterInput {
|
|
15
|
+
readonly childField: string | FieldRef;
|
|
16
|
+
readonly parentField: string | FieldRef;
|
|
17
|
+
}
|
|
18
|
+
export type Need = CheckNeedNode | {
|
|
19
|
+
readonly kind: "answer-need";
|
|
20
|
+
readonly roles: NonEmpty<AnyRole>;
|
|
21
|
+
readonly shape: AnyContract;
|
|
22
|
+
readonly variants?: NonEmpty<string>;
|
|
23
|
+
readonly seatLabel?: {
|
|
24
|
+
readonly key: string;
|
|
25
|
+
readonly values: NonEmpty<string>;
|
|
26
|
+
};
|
|
27
|
+
readonly guide?: string;
|
|
28
|
+
} | {
|
|
29
|
+
readonly kind: "auth-need";
|
|
30
|
+
readonly cls: AnyAuthClass;
|
|
31
|
+
readonly guide?: string;
|
|
32
|
+
} | {
|
|
33
|
+
readonly kind: "auth-from-field-need";
|
|
34
|
+
readonly field: string;
|
|
35
|
+
readonly allowed: NonEmpty<AnyAuthClass>;
|
|
36
|
+
readonly guide?: string;
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: "children-need";
|
|
39
|
+
readonly protocol?: ProtocolTarget;
|
|
40
|
+
readonly condition: "ended" | "state" | "ended-in";
|
|
41
|
+
readonly state?: string;
|
|
42
|
+
readonly where?: ChildFilterInput;
|
|
43
|
+
readonly allowEmpty: boolean;
|
|
44
|
+
readonly guide?: string;
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: "message-need";
|
|
47
|
+
readonly message: AnyMessageKind;
|
|
48
|
+
readonly from: NonEmpty<AnyRole>;
|
|
49
|
+
readonly guide?: string;
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: "bound-reached-need";
|
|
52
|
+
readonly step: string;
|
|
53
|
+
readonly guide?: string;
|
|
54
|
+
} | {
|
|
55
|
+
readonly kind: "cases-ended-need";
|
|
56
|
+
readonly field: string;
|
|
57
|
+
readonly guide?: string;
|
|
58
|
+
} | {
|
|
59
|
+
readonly kind: "any-need";
|
|
60
|
+
readonly of: readonly [Need, Need, ...Need[]];
|
|
61
|
+
readonly guide?: string;
|
|
62
|
+
};
|
|
63
|
+
export type Effect = CallActionEffectNode | {
|
|
64
|
+
readonly kind: "record-data-effect";
|
|
65
|
+
readonly mode: RecordMode;
|
|
66
|
+
readonly targetField: string;
|
|
67
|
+
readonly source: unknown;
|
|
68
|
+
} | {
|
|
69
|
+
readonly kind: "open-child-effect";
|
|
70
|
+
readonly protocol: ProtocolTarget;
|
|
71
|
+
readonly forRole: AnyRole;
|
|
72
|
+
readonly seatScopeFrom?: string;
|
|
73
|
+
readonly data: Rec<unknown>;
|
|
74
|
+
} | {
|
|
75
|
+
readonly kind: "emit-message-effect";
|
|
76
|
+
readonly message: AnyMessageKind;
|
|
77
|
+
readonly toRole: AnyRole;
|
|
78
|
+
readonly payload?: Rec<unknown>;
|
|
79
|
+
};
|
|
80
|
+
export interface Step<Name extends string = string, From extends string = string, To extends string = string> {
|
|
81
|
+
readonly kind: "step";
|
|
82
|
+
readonly name: Name;
|
|
83
|
+
readonly from: From;
|
|
84
|
+
readonly to: To;
|
|
85
|
+
readonly needs: readonly Need[];
|
|
86
|
+
readonly then: readonly Effect[];
|
|
87
|
+
readonly bound?: StepBoundInput;
|
|
88
|
+
readonly optional?: true;
|
|
89
|
+
readonly guide?: string;
|
|
90
|
+
}
|
|
91
|
+
export type AnyStep = Step;
|
|
92
|
+
export interface ProtocolRef<Name extends string = string, S extends string = string, F extends string = S> {
|
|
93
|
+
readonly kind: "protocol-ref";
|
|
94
|
+
readonly name: Name;
|
|
95
|
+
readonly states?: readonly S[];
|
|
96
|
+
readonly final?: readonly F[];
|
|
97
|
+
}
|
|
98
|
+
export type AnyProtocolRef = ProtocolRef;
|
|
99
|
+
export interface ProtocolAckOverride {
|
|
100
|
+
readonly message: AnyMessageKind;
|
|
101
|
+
readonly ack: AckDef;
|
|
102
|
+
}
|
|
103
|
+
export type ProtocolDataInput = FieldTable | AnyContract | FieldSchemaMap;
|
|
104
|
+
export type FieldHandlesOfProtocolData<D extends ProtocolDataInput> = D extends FieldTable<infer M> ? FieldTable<M> : D extends StructContract<string, infer F> ? FieldHandlesOf<F> : D extends FieldSchemaMap ? FieldHandlesOf<D> : Readonly<Record<string, never>>;
|
|
105
|
+
type IsBindingValue<T> = unknown extends T ? true : [T] extends [string] ? string extends T ? true : [T] extends [Brand<string, string>] ? true : false : false;
|
|
106
|
+
type RequiredStringFields<F extends FieldSchemaMap> = {
|
|
107
|
+
readonly [K in keyof F & string]-?: F[K] extends OptionalType ? never : IsBindingValue<Infer<F[K]>> extends true ? K : never;
|
|
108
|
+
}[keyof F & string];
|
|
109
|
+
type BindingFieldOfStruct<F> = [F] extends [never] ? string : F extends FieldSchemaMap ? RequiredStringFields<F> : string;
|
|
110
|
+
type BindingFieldOfData<D> = D extends FieldTable<infer _M> ? string : D extends AnyContract ? BindingFieldOfStruct<StructFieldsOfContract<D>> : D extends FieldSchemaMap ? RequiredStringFields<D> : string;
|
|
111
|
+
export type BindingFieldOf<P extends AnyProtocol> = BindingFieldOfData<P["data"]>;
|
|
112
|
+
export interface Protocol<Name extends string = string, S extends string = string, F extends string = S, D extends ProtocolDataInput = ProtocolDataInput> {
|
|
113
|
+
readonly kind: "protocol";
|
|
114
|
+
readonly name: Name;
|
|
115
|
+
readonly states: readonly [S, S, ...S[]];
|
|
116
|
+
readonly start: S;
|
|
117
|
+
readonly final: NonEmpty<F>;
|
|
118
|
+
readonly data: D;
|
|
119
|
+
readonly dataName?: string;
|
|
120
|
+
readonly openDataName?: string;
|
|
121
|
+
readonly predecessor?: {
|
|
122
|
+
readonly field: string;
|
|
123
|
+
readonly protocol?: ProtocolTarget;
|
|
124
|
+
};
|
|
125
|
+
readonly roleViews: readonly RoleViewOverride[];
|
|
126
|
+
readonly brief?: readonly BriefPiece[];
|
|
127
|
+
readonly roleBriefs: readonly RoleBriefOverride[];
|
|
128
|
+
readonly guide?: string;
|
|
129
|
+
readonly children: readonly ProtocolTarget[];
|
|
130
|
+
readonly acks: readonly ProtocolAckOverride[];
|
|
131
|
+
readonly steps: NonEmpty<AnyStep>;
|
|
132
|
+
}
|
|
133
|
+
export type AnyProtocol = Protocol;
|
|
134
|
+
export interface AnswerNeedOpts<V extends string, L extends SeatLabelsDef> {
|
|
135
|
+
readonly variants?: NonEmpty<V> | undefined;
|
|
136
|
+
readonly seatLabel?: SeatLabelReq<L> | undefined;
|
|
137
|
+
readonly guide?: Guide | undefined;
|
|
138
|
+
}
|
|
139
|
+
export declare function answer<R extends AnyRole | NonEmpty<AnyRole>, C extends AnyContract>(roles: R, shape: C, variantsOrOpts?: NonEmpty<VariantsOf<C>> | AnswerNeedOpts<VariantsOf<C>, LabelsOfRoles<R>>): Need;
|
|
140
|
+
export declare const authorize: {
|
|
141
|
+
(cls: AnyAuthClass, opts?: {
|
|
142
|
+
readonly guide?: Guide | undefined;
|
|
143
|
+
}): Need;
|
|
144
|
+
readonly fromField: (fieldRef: FieldRef<string, string | undefined>, allowed: NonEmpty<AnyAuthClass>, opts?: {
|
|
145
|
+
readonly guide?: Guide | undefined;
|
|
146
|
+
}) => Need;
|
|
147
|
+
};
|
|
148
|
+
export declare function childrenEnded(opts: {
|
|
149
|
+
readonly protocol?: ProtocolTarget | undefined;
|
|
150
|
+
readonly where?: ChildFilterInput | undefined;
|
|
151
|
+
readonly allowEmpty: boolean;
|
|
152
|
+
readonly guide?: Guide | undefined;
|
|
153
|
+
}): Need;
|
|
154
|
+
export declare function childrenIn<S extends string>(target: Protocol<string, S, string> | ProtocolRef<string, S, string>, state: NoInfer<S>, opts: {
|
|
155
|
+
readonly where?: ChildFilterInput | undefined;
|
|
156
|
+
readonly allowEmpty: boolean;
|
|
157
|
+
readonly guide?: Guide | undefined;
|
|
158
|
+
}): Need;
|
|
159
|
+
export declare function childrenEndedIn<F extends string>(target: Protocol<string, string, F> | ProtocolRef<string, string, F>, state: NoInfer<F>, opts: {
|
|
160
|
+
readonly where?: ChildFilterInput | undefined;
|
|
161
|
+
readonly allowEmpty: boolean;
|
|
162
|
+
readonly guide?: Guide | undefined;
|
|
163
|
+
}): Need;
|
|
164
|
+
export declare function casesEnded(opts: {
|
|
165
|
+
readonly field: FieldRef<string, readonly CaseRef[] | undefined>;
|
|
166
|
+
readonly guide?: Guide | undefined;
|
|
167
|
+
}): Need;
|
|
168
|
+
export declare function received(msgKind: AnyMessageKind, opts: {
|
|
169
|
+
readonly from: AnyRole | NonEmpty<AnyRole>;
|
|
170
|
+
readonly guide?: Guide | undefined;
|
|
171
|
+
}): Need;
|
|
172
|
+
export declare function boundReached(stepName: string, opts?: {
|
|
173
|
+
readonly guide?: Guide | undefined;
|
|
174
|
+
}): Need;
|
|
175
|
+
export declare function anyOf(first: Need, second: Need, ...rest: readonly Need[]): Need;
|
|
176
|
+
export declare function withGuide(need: Need, g: Guide): Need;
|
|
177
|
+
export declare function set<T>(target: FieldRef<string, T>, value: RecordInput<T>): Effect;
|
|
178
|
+
export declare function append<E>(target: FieldRef<string, readonly E[]>, item: RecordInput<E>): Effect;
|
|
179
|
+
export declare function openChild(target: ProtocolTarget, opts: {
|
|
180
|
+
readonly for: AnyRole;
|
|
181
|
+
readonly seatScopeFrom?: string | FieldRef | undefined;
|
|
182
|
+
readonly data: Rec<unknown>;
|
|
183
|
+
}): Effect;
|
|
184
|
+
export declare function emit<P extends AnyStructContract>(msgKind: MessageKind<string, P>, opts: {
|
|
185
|
+
readonly to: AnyRole;
|
|
186
|
+
readonly payload?: ArgsForContract<P> | undefined;
|
|
187
|
+
}): Effect;
|
|
188
|
+
export declare function step<const Name extends string, const From extends string, const To extends string>(name: Name, def: {
|
|
189
|
+
readonly from: From;
|
|
190
|
+
readonly to: To;
|
|
191
|
+
readonly needs?: readonly Need[] | undefined;
|
|
192
|
+
readonly then?: readonly Effect[] | undefined;
|
|
193
|
+
readonly bound?: StepBoundInput | undefined;
|
|
194
|
+
readonly optional?: true | undefined;
|
|
195
|
+
readonly guide?: Guide | undefined;
|
|
196
|
+
}): Step<Name, From, To>;
|
|
197
|
+
export declare function protocolRef<const Name extends string, const S extends string = string, const F extends NoInfer<S> = S>(name: Name, def?: {
|
|
198
|
+
readonly states?: readonly S[] | undefined;
|
|
199
|
+
readonly final?: readonly F[] | undefined;
|
|
200
|
+
}): ProtocolRef<Name, S, F>;
|
|
201
|
+
export declare function protocol<const Name extends string, const S extends string, const F extends NoInfer<S>, D extends ProtocolDataInput = ProtocolDataInput>(name: Name, def: {
|
|
202
|
+
readonly states: readonly [S, S, ...S[]];
|
|
203
|
+
readonly start: NoInfer<S>;
|
|
204
|
+
readonly final: NonEmpty<F>;
|
|
205
|
+
readonly data: D;
|
|
206
|
+
readonly dataName?: string | undefined;
|
|
207
|
+
readonly openDataName?: string | undefined;
|
|
208
|
+
readonly predecessor?: {
|
|
209
|
+
readonly field: FieldRef<string, string | undefined>;
|
|
210
|
+
readonly protocol?: ProtocolTarget | undefined;
|
|
211
|
+
} | undefined;
|
|
212
|
+
readonly roleViews?: readonly RoleViewOverride[] | undefined;
|
|
213
|
+
readonly brief?: readonly BriefPiece[] | undefined;
|
|
214
|
+
readonly roleBriefs?: readonly RoleBriefOverride[] | undefined;
|
|
215
|
+
readonly guide?: Guide | ((data: FieldHandlesOfProtocolData<D>) => Guide) | undefined;
|
|
216
|
+
readonly children?: readonly ProtocolTarget[] | undefined;
|
|
217
|
+
readonly acks?: readonly ProtocolAckOverride[] | undefined;
|
|
218
|
+
readonly steps: NonEmpty<Step<string, NoInfer<S>, NoInfer<S>>>;
|
|
219
|
+
}): Protocol<Name, S, F, D>;
|
|
220
|
+
export {};
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { readFieldTableMeta } from "./values.js";
|
|
2
|
+
const THEN = "then";
|
|
3
|
+
function freezeNonEmpty(items) {
|
|
4
|
+
const [first, ...rest] = items;
|
|
5
|
+
return Object.freeze([first, ...rest]);
|
|
6
|
+
}
|
|
7
|
+
function isRoleList(roles) {
|
|
8
|
+
return Array.isArray(roles);
|
|
9
|
+
}
|
|
10
|
+
function normalizeRoles(roles) {
|
|
11
|
+
if (isRoleList(roles)) {
|
|
12
|
+
return freezeNonEmpty(roles);
|
|
13
|
+
}
|
|
14
|
+
return Object.freeze([roles]);
|
|
15
|
+
}
|
|
16
|
+
function isVariantList(value) {
|
|
17
|
+
return Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
export function answer(roles, shape, variantsOrOpts) {
|
|
20
|
+
const roleList = normalizeRoles(roles);
|
|
21
|
+
if (variantsOrOpts === undefined) {
|
|
22
|
+
return Object.freeze({ kind: "answer-need", roles: roleList, shape });
|
|
23
|
+
}
|
|
24
|
+
if (isVariantList(variantsOrOpts)) {
|
|
25
|
+
return Object.freeze({
|
|
26
|
+
kind: "answer-need",
|
|
27
|
+
roles: roleList,
|
|
28
|
+
shape,
|
|
29
|
+
variants: freezeNonEmpty(variantsOrOpts),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return Object.freeze({
|
|
33
|
+
kind: "answer-need",
|
|
34
|
+
roles: roleList,
|
|
35
|
+
shape,
|
|
36
|
+
...(variantsOrOpts.variants !== undefined
|
|
37
|
+
? { variants: freezeNonEmpty(variantsOrOpts.variants) }
|
|
38
|
+
: {}),
|
|
39
|
+
...(variantsOrOpts.seatLabel !== undefined
|
|
40
|
+
? {
|
|
41
|
+
seatLabel: Object.freeze({
|
|
42
|
+
key: variantsOrOpts.seatLabel.key,
|
|
43
|
+
values: freezeNonEmpty(variantsOrOpts.seatLabel.values),
|
|
44
|
+
}),
|
|
45
|
+
}
|
|
46
|
+
: {}),
|
|
47
|
+
...(variantsOrOpts.guide !== undefined ? { guide: variantsOrOpts.guide.template } : {}),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function makeAuthNeed(cls, opts) {
|
|
51
|
+
return Object.freeze({
|
|
52
|
+
kind: "auth-need",
|
|
53
|
+
cls,
|
|
54
|
+
...(opts?.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
export const authorize = Object.freeze(Object.assign(makeAuthNeed, {
|
|
58
|
+
fromField(fieldRef, allowed, opts) {
|
|
59
|
+
return Object.freeze({
|
|
60
|
+
kind: "auth-from-field-need",
|
|
61
|
+
field: fieldRef.name,
|
|
62
|
+
allowed: freezeNonEmpty(allowed),
|
|
63
|
+
...(opts?.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
}));
|
|
67
|
+
export function childrenEnded(opts) {
|
|
68
|
+
return Object.freeze({
|
|
69
|
+
kind: "children-need",
|
|
70
|
+
condition: "ended",
|
|
71
|
+
allowEmpty: opts.allowEmpty,
|
|
72
|
+
...(opts.protocol !== undefined ? { protocol: opts.protocol } : {}),
|
|
73
|
+
...(opts.where !== undefined ? { where: opts.where } : {}),
|
|
74
|
+
...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function childrenIn(target, state, opts) {
|
|
78
|
+
return Object.freeze({
|
|
79
|
+
kind: "children-need",
|
|
80
|
+
protocol: target,
|
|
81
|
+
condition: "state",
|
|
82
|
+
state,
|
|
83
|
+
allowEmpty: opts.allowEmpty,
|
|
84
|
+
...(opts.where !== undefined ? { where: opts.where } : {}),
|
|
85
|
+
...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function childrenEndedIn(target, state, opts) {
|
|
89
|
+
return Object.freeze({
|
|
90
|
+
kind: "children-need",
|
|
91
|
+
protocol: target,
|
|
92
|
+
condition: "ended-in",
|
|
93
|
+
state,
|
|
94
|
+
allowEmpty: opts.allowEmpty,
|
|
95
|
+
...(opts.where !== undefined ? { where: opts.where } : {}),
|
|
96
|
+
...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
export function casesEnded(opts) {
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
kind: "cases-ended-need",
|
|
102
|
+
field: opts.field.name,
|
|
103
|
+
...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function received(msgKind, opts) {
|
|
107
|
+
return Object.freeze({
|
|
108
|
+
kind: "message-need",
|
|
109
|
+
message: msgKind,
|
|
110
|
+
from: normalizeRoles(opts.from),
|
|
111
|
+
...(opts.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
export function boundReached(stepName, opts) {
|
|
115
|
+
return Object.freeze({
|
|
116
|
+
kind: "bound-reached-need",
|
|
117
|
+
step: stepName,
|
|
118
|
+
...(opts?.guide !== undefined ? { guide: opts.guide.template } : {}),
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
export function anyOf(first, second, ...rest) {
|
|
122
|
+
const of = Object.freeze([first, second, ...rest]);
|
|
123
|
+
return Object.freeze({ kind: "any-need", of });
|
|
124
|
+
}
|
|
125
|
+
export function withGuide(need, g) {
|
|
126
|
+
return Object.freeze({ ...need, guide: g.template });
|
|
127
|
+
}
|
|
128
|
+
export function set(target, value) {
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
kind: "record-data-effect",
|
|
131
|
+
mode: "set",
|
|
132
|
+
targetField: target.name,
|
|
133
|
+
source: value,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
export function append(target, item) {
|
|
137
|
+
return Object.freeze({
|
|
138
|
+
kind: "record-data-effect",
|
|
139
|
+
mode: "append",
|
|
140
|
+
targetField: target.name,
|
|
141
|
+
source: item,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
export function openChild(target, opts) {
|
|
145
|
+
const scopeField = opts.seatScopeFrom === undefined
|
|
146
|
+
? undefined
|
|
147
|
+
: typeof opts.seatScopeFrom === "string"
|
|
148
|
+
? opts.seatScopeFrom
|
|
149
|
+
: opts.seatScopeFrom.name;
|
|
150
|
+
return Object.freeze({
|
|
151
|
+
kind: "open-child-effect",
|
|
152
|
+
protocol: target,
|
|
153
|
+
forRole: opts.for,
|
|
154
|
+
...(scopeField !== undefined ? { seatScopeFrom: scopeField } : {}),
|
|
155
|
+
data: opts.data,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
export function emit(msgKind, opts) {
|
|
159
|
+
return Object.freeze({
|
|
160
|
+
kind: "emit-message-effect",
|
|
161
|
+
message: msgKind,
|
|
162
|
+
toRole: opts.to,
|
|
163
|
+
...(opts.payload !== undefined ? { payload: opts.payload } : {}),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
export function step(name, def) {
|
|
167
|
+
return Object.freeze({
|
|
168
|
+
kind: "step",
|
|
169
|
+
name,
|
|
170
|
+
from: def.from,
|
|
171
|
+
to: def.to,
|
|
172
|
+
needs: Object.freeze([...(def.needs ?? [])]),
|
|
173
|
+
[THEN]: Object.freeze([...(def.then ?? [])]),
|
|
174
|
+
...(def.bound !== undefined ? { bound: def.bound } : {}),
|
|
175
|
+
...(def.optional !== undefined ? { optional: def.optional } : {}),
|
|
176
|
+
...(def.guide !== undefined ? { guide: def.guide.template } : {}),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
export function protocolRef(name, def) {
|
|
180
|
+
return Object.freeze({
|
|
181
|
+
kind: "protocol-ref",
|
|
182
|
+
name,
|
|
183
|
+
...(def?.states !== undefined ? { states: Object.freeze([...def.states]) } : {}),
|
|
184
|
+
...(def?.final !== undefined ? { final: Object.freeze([...def.final]) } : {}),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function isStructContract(data) {
|
|
188
|
+
return "kind" in data && data.kind === "contract" && "fields" in data;
|
|
189
|
+
}
|
|
190
|
+
function protocolFieldHandles(data) {
|
|
191
|
+
const meta = readFieldTableMeta(data);
|
|
192
|
+
if (meta !== undefined) {
|
|
193
|
+
const handles = {};
|
|
194
|
+
for (const key of Object.keys(meta.entries)) {
|
|
195
|
+
handles[key] = Object.freeze({ kind: "field", name: key });
|
|
196
|
+
}
|
|
197
|
+
return Object.freeze(handles);
|
|
198
|
+
}
|
|
199
|
+
if (isStructContract(data)) {
|
|
200
|
+
return data.fields;
|
|
201
|
+
}
|
|
202
|
+
const handles = {};
|
|
203
|
+
for (const key of Object.keys(data)) {
|
|
204
|
+
handles[key] = Object.freeze({ kind: "field", name: key });
|
|
205
|
+
}
|
|
206
|
+
return Object.freeze(handles);
|
|
207
|
+
}
|
|
208
|
+
export function protocol(name, def) {
|
|
209
|
+
const [s0, s1, ...sRest] = def.states;
|
|
210
|
+
const states = Object.freeze([s0, s1, ...sRest]);
|
|
211
|
+
const resolvedGuide = def.guide === undefined
|
|
212
|
+
? undefined
|
|
213
|
+
: typeof def.guide === "function"
|
|
214
|
+
? def.guide(protocolFieldHandles(def.data)).template
|
|
215
|
+
: def.guide.template;
|
|
216
|
+
return Object.freeze({
|
|
217
|
+
kind: "protocol",
|
|
218
|
+
name,
|
|
219
|
+
states,
|
|
220
|
+
start: def.start,
|
|
221
|
+
final: freezeNonEmpty(def.final),
|
|
222
|
+
data: def.data,
|
|
223
|
+
...(def.dataName !== undefined ? { dataName: def.dataName } : {}),
|
|
224
|
+
...(def.openDataName !== undefined ? { openDataName: def.openDataName } : {}),
|
|
225
|
+
...(def.predecessor !== undefined
|
|
226
|
+
? {
|
|
227
|
+
predecessor: Object.freeze({
|
|
228
|
+
field: def.predecessor.field.name,
|
|
229
|
+
...(def.predecessor.protocol !== undefined
|
|
230
|
+
? { protocol: def.predecessor.protocol }
|
|
231
|
+
: {}),
|
|
232
|
+
}),
|
|
233
|
+
}
|
|
234
|
+
: {}),
|
|
235
|
+
roleViews: Object.freeze([...(def.roleViews ?? [])]),
|
|
236
|
+
...(def.brief !== undefined ? { brief: Object.freeze([...def.brief]) } : {}),
|
|
237
|
+
roleBriefs: Object.freeze([...(def.roleBriefs ?? [])]),
|
|
238
|
+
...(resolvedGuide !== undefined ? { guide: resolvedGuide } : {}),
|
|
239
|
+
children: Object.freeze([...(def.children ?? [])]),
|
|
240
|
+
acks: Object.freeze([...(def.acks ?? [])]),
|
|
241
|
+
steps: freezeNonEmpty(def.steps),
|
|
242
|
+
});
|
|
243
|
+
}
|