@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/query.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { StandardSchema } from "./standard-schema.js";
|
|
2
|
+
export type QueryRowIssuePath = readonly (PropertyKey | {
|
|
3
|
+
readonly key: PropertyKey;
|
|
4
|
+
})[];
|
|
5
|
+
export interface QueryRowIssue {
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly path?: QueryRowIssuePath | undefined;
|
|
8
|
+
}
|
|
9
|
+
export type QueryRowResult<Row> = {
|
|
10
|
+
readonly value: Row;
|
|
11
|
+
readonly issues?: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
readonly issues: readonly QueryRowIssue[];
|
|
14
|
+
};
|
|
15
|
+
/** A Standard Schema (v1) for one row. An Effect schema becomes one through `Schema.toStandardSchemaV1`. */
|
|
16
|
+
export interface QueryRowSchema<Row> {
|
|
17
|
+
readonly "~standard": {
|
|
18
|
+
readonly version: 1;
|
|
19
|
+
readonly vendor: string;
|
|
20
|
+
readonly validate: (value: unknown) => QueryRowResult<Row> | PromiseLike<QueryRowResult<Row>>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export type QueryParam = string | number | null;
|
|
24
|
+
/** One argument: its schema decodes the string a caller gives into the value the SQL binds as `:<name>`. */
|
|
25
|
+
export interface QueryArg<Out extends QueryParam = QueryParam> {
|
|
26
|
+
readonly schema: StandardSchema<Out>;
|
|
27
|
+
readonly description?: string;
|
|
28
|
+
}
|
|
29
|
+
export type QueryArgs = Readonly<Record<string, QueryArg>>;
|
|
30
|
+
export type NoQueryArgs = Readonly<Record<string, never>>;
|
|
31
|
+
export type QueryArgInput<A extends QueryArgs> = {
|
|
32
|
+
readonly [K in keyof A]: string;
|
|
33
|
+
};
|
|
34
|
+
export interface Query<Row = unknown, A extends QueryArgs = QueryArgs> {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly args: A;
|
|
37
|
+
readonly sql: string;
|
|
38
|
+
readonly row: QueryRowSchema<Row>;
|
|
39
|
+
}
|
|
40
|
+
export type QueryBinding = {
|
|
41
|
+
readonly ok: true;
|
|
42
|
+
readonly sql: string;
|
|
43
|
+
readonly params: Readonly<Record<string, QueryParam>>;
|
|
44
|
+
} | {
|
|
45
|
+
readonly ok: false;
|
|
46
|
+
readonly issues: readonly QueryRowIssue[];
|
|
47
|
+
};
|
|
48
|
+
export declare class QueryError extends Error {
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly code: string;
|
|
51
|
+
constructor(code: string, message: string);
|
|
52
|
+
}
|
|
53
|
+
interface QueryDeclaration<Row> {
|
|
54
|
+
readonly name: string;
|
|
55
|
+
readonly sql: string;
|
|
56
|
+
readonly row: QueryRowSchema<Row>;
|
|
57
|
+
}
|
|
58
|
+
export declare function defineQuery<Row>(query: QueryDeclaration<Row>): Query<Row, NoQueryArgs>;
|
|
59
|
+
export declare function defineQuery<Row, A extends QueryArgs>(query: QueryDeclaration<Row> & {
|
|
60
|
+
readonly args: A;
|
|
61
|
+
}): Query<Row, A>;
|
|
62
|
+
/** Decodes a caller's arguments with the query's schemas into the named parameters its SQL binds. */
|
|
63
|
+
export declare function bindQuery<A extends QueryArgs>(query: Query<unknown, A>, input: QueryArgInput<A>): QueryBinding;
|
|
64
|
+
export declare function queryUsage(query: Query): string;
|
|
65
|
+
export declare function issueText(issue: QueryRowIssue): string;
|
|
66
|
+
export declare function decodeQueryRows<Row>(query: Query<Row>, rows: unknown): readonly Row[];
|
|
67
|
+
export {};
|
package/dist/query.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export class QueryError extends Error {
|
|
2
|
+
name = "QueryError";
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const QUERY_NAME = /^[a-z][a-z0-9-]*$/;
|
|
10
|
+
const ARG_NAME = /^[a-z][a-zA-Z0-9]*$/;
|
|
11
|
+
export function defineQuery(query) {
|
|
12
|
+
if (!QUERY_NAME.test(query.name)) {
|
|
13
|
+
throw new QueryError("INVALID_QUERY", `query name ${JSON.stringify(query.name)} must match ${String(QUERY_NAME)}`);
|
|
14
|
+
}
|
|
15
|
+
const args = query.args ?? {};
|
|
16
|
+
for (const arg of Object.keys(args)) {
|
|
17
|
+
if (!ARG_NAME.test(arg)) {
|
|
18
|
+
throw new QueryError("INVALID_QUERY", `query ${query.name} argument ${JSON.stringify(arg)} must match ${String(ARG_NAME)}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const sql = query.sql.trim();
|
|
22
|
+
if (sql.length === 0) {
|
|
23
|
+
throw new QueryError("INVALID_QUERY", `query ${query.name} has empty SQL`);
|
|
24
|
+
}
|
|
25
|
+
return Object.freeze({ name: query.name, args, sql, row: query.row });
|
|
26
|
+
}
|
|
27
|
+
function isQueryParam(value) {
|
|
28
|
+
return (value === null ||
|
|
29
|
+
typeof value === "string" ||
|
|
30
|
+
(typeof value === "number" && Number.isFinite(value)));
|
|
31
|
+
}
|
|
32
|
+
function decodeArg(name, arg, raw) {
|
|
33
|
+
const refuse = (message) => ({
|
|
34
|
+
ok: false,
|
|
35
|
+
issues: [{ message, path: [name] }],
|
|
36
|
+
});
|
|
37
|
+
if (typeof raw !== "string")
|
|
38
|
+
return refuse("is missing");
|
|
39
|
+
const result = arg.schema["~standard"].validate(raw);
|
|
40
|
+
if (isPromiseLike(result))
|
|
41
|
+
return refuse("must decode synchronously");
|
|
42
|
+
if (result.issues !== undefined) {
|
|
43
|
+
const issues = result.issues.map((issue) => ({
|
|
44
|
+
message: issue.message,
|
|
45
|
+
path: [name, ...(issue.path ?? [])],
|
|
46
|
+
}));
|
|
47
|
+
return { ok: false, issues };
|
|
48
|
+
}
|
|
49
|
+
if (!isQueryParam(result.value))
|
|
50
|
+
return refuse("must decode to a string, a finite number or null");
|
|
51
|
+
return { ok: true, value: result.value };
|
|
52
|
+
}
|
|
53
|
+
/** Decodes a caller's arguments with the query's schemas into the named parameters its SQL binds. */
|
|
54
|
+
export function bindQuery(query, input) {
|
|
55
|
+
const issues = Object.keys(input)
|
|
56
|
+
.filter((key) => !Object.hasOwn(query.args, key))
|
|
57
|
+
.map((key) => ({ message: `is not an argument of ${query.name}`, path: [key] }));
|
|
58
|
+
const params = {};
|
|
59
|
+
for (const [name, arg] of Object.entries(query.args)) {
|
|
60
|
+
const decoded = decodeArg(name, arg, Reflect.get(input, name));
|
|
61
|
+
if (decoded.ok)
|
|
62
|
+
params[name] = decoded.value;
|
|
63
|
+
else
|
|
64
|
+
issues.push(...decoded.issues);
|
|
65
|
+
}
|
|
66
|
+
return issues.length > 0 ? { ok: false, issues } : { ok: true, sql: query.sql, params };
|
|
67
|
+
}
|
|
68
|
+
export function queryUsage(query) {
|
|
69
|
+
const names = Object.keys(query.args);
|
|
70
|
+
const call = ["telar query", query.name, ...names.map((name) => `<${name}>`)].join(" ");
|
|
71
|
+
const described = Object.entries(query.args)
|
|
72
|
+
.filter(([, arg]) => arg.description !== undefined)
|
|
73
|
+
.map(([name, arg]) => `<${name}>: ${arg.description ?? ""}`);
|
|
74
|
+
return described.length > 0 ? `${call} (${described.join("; ")})` : call;
|
|
75
|
+
}
|
|
76
|
+
export function issueText(issue) {
|
|
77
|
+
const path = (issue.path ?? [])
|
|
78
|
+
.map((part) => String(typeof part === "object" ? part.key : part))
|
|
79
|
+
.join(".");
|
|
80
|
+
return path.length > 0 ? `${path}: ${issue.message}` : issue.message;
|
|
81
|
+
}
|
|
82
|
+
function isPromiseLike(value) {
|
|
83
|
+
return (typeof value === "object" &&
|
|
84
|
+
value !== null &&
|
|
85
|
+
typeof value.then === "function");
|
|
86
|
+
}
|
|
87
|
+
export function decodeQueryRows(query, rows) {
|
|
88
|
+
if (!Array.isArray(rows)) {
|
|
89
|
+
throw new QueryError("ROW_DECODE_ERROR", `query ${query.name} expected an array of rows`);
|
|
90
|
+
}
|
|
91
|
+
return rows.map((raw, index) => {
|
|
92
|
+
const result = query.row["~standard"].validate(raw);
|
|
93
|
+
if (isPromiseLike(result)) {
|
|
94
|
+
throw new QueryError("ROW_DECODE_ERROR", `query ${query.name} row schema must decode synchronously`);
|
|
95
|
+
}
|
|
96
|
+
if (result.issues !== undefined) {
|
|
97
|
+
const detail = result.issues.map(issueText).join("; ");
|
|
98
|
+
throw new QueryError("ROW_DECODE_ERROR", `query ${query.name} row ${String(index)} does not decode: ${detail}`);
|
|
99
|
+
}
|
|
100
|
+
return result.value;
|
|
101
|
+
});
|
|
102
|
+
}
|
package/dist/read.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type Query, type QueryArgInput, type QueryArgs } from "./query.js";
|
|
2
|
+
import type { ShowCase, ShowSeat } from "./show.js";
|
|
3
|
+
/** Which seats `read.seats` returns: by role, and eligible for one case. */
|
|
4
|
+
export interface ReadSeatsFilter {
|
|
5
|
+
readonly role?: string | undefined;
|
|
6
|
+
readonly eligibleFor?: string | undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The read-only view of the engine an extension gets, limited to its principal's view (ADR 0050).
|
|
10
|
+
* A case out of view reads the same as a case that does not exist.
|
|
11
|
+
*/
|
|
12
|
+
export interface Read {
|
|
13
|
+
readonly case: (id: string) => ShowCase | undefined;
|
|
14
|
+
/** The case, then its parent, up to the first case out of view. */
|
|
15
|
+
readonly lineage: (id: string) => readonly ShowCase[];
|
|
16
|
+
/** Active seats eligible for a visible case, with their `holder`. */
|
|
17
|
+
readonly seats: (filter?: ReadSeatsFilter) => readonly ShowSeat[];
|
|
18
|
+
/**
|
|
19
|
+
* A declared query's rows, run on this principal's view of the trail: the read-model tables and
|
|
20
|
+
* `facts` hold only what the view shows (ADR 0050 D2). The rows are decoded with the query's `row`.
|
|
21
|
+
*/
|
|
22
|
+
readonly query: <Row, A extends QueryArgs>(query: Query<Row, A>, args: QueryArgInput<A>) => readonly Row[];
|
|
23
|
+
}
|
|
24
|
+
/** A seat for `fakeRead`, with the cases it is eligible for. */
|
|
25
|
+
export interface FakeReadSeat extends ShowSeat {
|
|
26
|
+
readonly eligibleFor: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A `Read` over fixed data, for an installation's own unit tests. It enforces no view:
|
|
30
|
+
* it returns what the test gives. telar's contract tests hold the view.
|
|
31
|
+
*/
|
|
32
|
+
export declare function fakeRead(input?: {
|
|
33
|
+
readonly cases?: readonly ShowCase[];
|
|
34
|
+
readonly seats?: readonly FakeReadSeat[];
|
|
35
|
+
/** Raw rows per query name; `read.query` decodes them with the query's `row`. */
|
|
36
|
+
readonly rows?: Readonly<Record<string, readonly unknown[]>>;
|
|
37
|
+
}): Read;
|
package/dist/read.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { decodeQueryRows } from "./query.js";
|
|
2
|
+
/**
|
|
3
|
+
* A `Read` over fixed data, for an installation's own unit tests. It enforces no view:
|
|
4
|
+
* it returns what the test gives. telar's contract tests hold the view.
|
|
5
|
+
*/
|
|
6
|
+
export function fakeRead(input = {}) {
|
|
7
|
+
const cases = new Map((input.cases ?? []).map((c) => [c.id, c]));
|
|
8
|
+
const seats = input.seats ?? [];
|
|
9
|
+
const lineage = (id) => {
|
|
10
|
+
const chain = [];
|
|
11
|
+
let next = cases.get(id);
|
|
12
|
+
while (next !== undefined) {
|
|
13
|
+
chain.push(next);
|
|
14
|
+
next = next.parent === null ? undefined : cases.get(next.parent);
|
|
15
|
+
}
|
|
16
|
+
return chain;
|
|
17
|
+
};
|
|
18
|
+
return Object.freeze({
|
|
19
|
+
case: (id) => cases.get(id),
|
|
20
|
+
lineage,
|
|
21
|
+
seats: (filter = {}) => seats
|
|
22
|
+
.filter((s) => filter.role === undefined || s.role === filter.role)
|
|
23
|
+
.filter((s) => filter.eligibleFor === undefined || s.eligibleFor.includes(filter.eligibleFor))
|
|
24
|
+
.map(({ eligibleFor: _eligibleFor, ...seat }) => seat),
|
|
25
|
+
query: (query, _args) => decodeQueryRows(query, input.rows?.[query.name] ?? []),
|
|
26
|
+
});
|
|
27
|
+
}
|
package/dist/runner.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { WorkEvidence } from "./evidence.js";
|
|
2
|
+
import type { WorldPowers } from "./powers.js";
|
|
3
|
+
import type { SeatKey } from "./seating.js";
|
|
4
|
+
import type { StandardSchema } from "./standard-schema.js";
|
|
5
|
+
export type RunnerId = `${string}/runner/${string}/${number}`;
|
|
6
|
+
export interface RunnerSeatRef {
|
|
7
|
+
readonly installation: string;
|
|
8
|
+
readonly seat: string;
|
|
9
|
+
readonly role: string;
|
|
10
|
+
readonly binding: SeatKey;
|
|
11
|
+
}
|
|
12
|
+
export type RunnerState = "working" | "idle" | "ended";
|
|
13
|
+
export interface RunnerHolder {
|
|
14
|
+
readonly runner?: string | undefined;
|
|
15
|
+
readonly model?: string | undefined;
|
|
16
|
+
readonly effort?: string | undefined;
|
|
17
|
+
readonly account?: string | undefined;
|
|
18
|
+
readonly runtime?: string | undefined;
|
|
19
|
+
readonly thread?: string | undefined;
|
|
20
|
+
readonly ref?: never;
|
|
21
|
+
readonly [key: string]: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface RunnerWork {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly startedAt: string;
|
|
26
|
+
readonly endedAt: string;
|
|
27
|
+
readonly evidence?: WorkEvidence | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** How a command in the runner's thread finds this seat without TELAR_SEAT (ADR 0051 §6). */
|
|
30
|
+
export interface RunnerIdentity {
|
|
31
|
+
/** An environment variable name the thread's commands can read, such as `BB_THREAD_ID`. */
|
|
32
|
+
readonly env: string;
|
|
33
|
+
/** Its value for this seat. */
|
|
34
|
+
readonly value: string;
|
|
35
|
+
}
|
|
36
|
+
export interface RunnerReport {
|
|
37
|
+
readonly ref: string;
|
|
38
|
+
readonly state: RunnerState;
|
|
39
|
+
readonly holder?: RunnerHolder;
|
|
40
|
+
readonly identity?: RunnerIdentity;
|
|
41
|
+
readonly work?: readonly RunnerWork[];
|
|
42
|
+
}
|
|
43
|
+
export interface RunnerWakeInput<Options = Readonly<Record<string, unknown>>> {
|
|
44
|
+
readonly seat: RunnerSeatRef;
|
|
45
|
+
readonly prompt: string;
|
|
46
|
+
readonly ref?: string;
|
|
47
|
+
readonly options: Options;
|
|
48
|
+
}
|
|
49
|
+
export interface RunnerPollInput<Options = Readonly<Record<string, unknown>>> {
|
|
50
|
+
readonly ref: string;
|
|
51
|
+
readonly options: Options;
|
|
52
|
+
}
|
|
53
|
+
export interface RunnerEndInput<Options = Readonly<Record<string, unknown>>> {
|
|
54
|
+
readonly seat: RunnerSeatRef;
|
|
55
|
+
readonly ref: string;
|
|
56
|
+
readonly options: Options;
|
|
57
|
+
}
|
|
58
|
+
export interface DefineRunnerInput<Id extends RunnerId = RunnerId, Options = Readonly<Record<string, unknown>>> {
|
|
59
|
+
readonly id: Id;
|
|
60
|
+
readonly options: StandardSchema<Options>;
|
|
61
|
+
readonly wake: (input: RunnerWakeInput<Options>, powers: WorldPowers) => RunnerReport;
|
|
62
|
+
readonly poll: (input: RunnerPollInput<Options>, powers: WorldPowers) => RunnerReport;
|
|
63
|
+
readonly end: (input: RunnerEndInput<Options>, powers: WorldPowers) => void;
|
|
64
|
+
}
|
|
65
|
+
export type RunnerPoint = "runner:wake" | "runner:poll" | "runner:end";
|
|
66
|
+
export interface RunnerOptionIssue {
|
|
67
|
+
readonly path: readonly string[];
|
|
68
|
+
readonly message: string;
|
|
69
|
+
}
|
|
70
|
+
export type RunnerOptionValidation<Options> = {
|
|
71
|
+
readonly ok: true;
|
|
72
|
+
readonly value: Options;
|
|
73
|
+
} | {
|
|
74
|
+
readonly ok: false;
|
|
75
|
+
readonly issues: readonly RunnerOptionIssue[];
|
|
76
|
+
};
|
|
77
|
+
export interface AnyRunner {
|
|
78
|
+
readonly id: RunnerId;
|
|
79
|
+
readonly options: StandardSchema;
|
|
80
|
+
readonly admitOptions: (raw: unknown) => RunnerOptionValidation<unknown>;
|
|
81
|
+
readonly dispatch: (point: RunnerPoint, args: unknown, powers: WorldPowers) => unknown;
|
|
82
|
+
}
|
|
83
|
+
export type RunnerIdOf<R> = R extends {
|
|
84
|
+
readonly id: infer Id extends RunnerId;
|
|
85
|
+
} ? Id : never;
|
|
86
|
+
export type RunnerOptionsOf<R> = R extends {
|
|
87
|
+
readonly options: StandardSchema<infer O>;
|
|
88
|
+
} ? O : never;
|
|
89
|
+
export interface Runner<Id extends RunnerId = RunnerId, Options = Readonly<Record<string, unknown>>> extends AnyRunner {
|
|
90
|
+
readonly id: Id;
|
|
91
|
+
readonly options: StandardSchema<Options>;
|
|
92
|
+
readonly wake: (input: RunnerWakeInput<Options>, powers: WorldPowers) => RunnerReport;
|
|
93
|
+
readonly poll: (input: RunnerPollInput<Options>, powers: WorldPowers) => RunnerReport;
|
|
94
|
+
readonly end: (input: RunnerEndInput<Options>, powers: WorldPowers) => void;
|
|
95
|
+
}
|
|
96
|
+
export declare class RunnerError extends Error {
|
|
97
|
+
readonly name: string;
|
|
98
|
+
readonly code: string;
|
|
99
|
+
constructor(code: string, message: string);
|
|
100
|
+
}
|
|
101
|
+
export declare function isValidRunnerId(id: string): boolean;
|
|
102
|
+
export type StandardValidation<Output> = {
|
|
103
|
+
readonly ok: true;
|
|
104
|
+
readonly value: Output;
|
|
105
|
+
} | {
|
|
106
|
+
readonly ok: false;
|
|
107
|
+
readonly issues: readonly RunnerOptionIssue[];
|
|
108
|
+
} | {
|
|
109
|
+
readonly ok: "async";
|
|
110
|
+
};
|
|
111
|
+
export declare function validateStandard<Output>(schema: StandardSchema<Output>, raw: unknown): StandardValidation<Output>;
|
|
112
|
+
export declare function validateRunnerOptions<Options>(runner: Pick<DefineRunnerInput<RunnerId, Options>, "id" | "options">, raw: unknown): RunnerOptionValidation<Options>;
|
|
113
|
+
export declare function defineRunner<const Id extends RunnerId, Options>(input: DefineRunnerInput<Id, Options>): Runner<Id, Options>;
|
package/dist/runner.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export class RunnerError extends Error {
|
|
2
|
+
name = "RunnerError";
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const RUNNER_ID_RE = /^[a-z][a-z0-9.-]*\/runner\/[a-z][a-z0-9-]*\/[1-9][0-9]*$/;
|
|
10
|
+
export function isValidRunnerId(id) {
|
|
11
|
+
return RUNNER_ID_RE.test(id);
|
|
12
|
+
}
|
|
13
|
+
function isPromiseLike(value) {
|
|
14
|
+
return (typeof value === "object" && value !== null && typeof Reflect.get(value, "then") === "function");
|
|
15
|
+
}
|
|
16
|
+
function issuePath(issue) {
|
|
17
|
+
return (issue.path ?? []).map((part) => String(typeof part === "object" ? part.key : part));
|
|
18
|
+
}
|
|
19
|
+
function isRec(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
function reqNonEmpty(obj, key) {
|
|
23
|
+
const val = obj[key];
|
|
24
|
+
if (typeof val !== "string" || val.length === 0) {
|
|
25
|
+
throw new RunnerError("INVALID_INPUT", `runner:wake seat.binding.${key} must be a non-empty string`);
|
|
26
|
+
}
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
function readSeatKey(raw) {
|
|
30
|
+
if (!isRec(raw) || typeof raw["kind"] !== "string") {
|
|
31
|
+
throw new RunnerError("INVALID_INPUT", "runner:wake requires seat.binding SeatKey");
|
|
32
|
+
}
|
|
33
|
+
const kind = raw["kind"];
|
|
34
|
+
if (kind === "all")
|
|
35
|
+
return { kind: "all" };
|
|
36
|
+
if (kind === "case")
|
|
37
|
+
return { kind: "case", case_id: reqNonEmpty(raw, "case_id") };
|
|
38
|
+
if (kind === "field") {
|
|
39
|
+
return { kind: "field", field: reqNonEmpty(raw, "field"), value: reqNonEmpty(raw, "value") };
|
|
40
|
+
}
|
|
41
|
+
if (kind === "ancestor") {
|
|
42
|
+
return {
|
|
43
|
+
kind: "ancestor",
|
|
44
|
+
protocol: reqNonEmpty(raw, "protocol"),
|
|
45
|
+
case_id: reqNonEmpty(raw, "case_id"),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
throw new RunnerError("INVALID_INPUT", `unknown seat.binding kind ${JSON.stringify(kind)}`);
|
|
49
|
+
}
|
|
50
|
+
function readSeatRef(raw) {
|
|
51
|
+
if (!isRec(raw) ||
|
|
52
|
+
typeof raw["installation"] !== "string" ||
|
|
53
|
+
typeof raw["seat"] !== "string" ||
|
|
54
|
+
typeof raw["role"] !== "string") {
|
|
55
|
+
throw new RunnerError("INVALID_INPUT", "runner:wake and runner:end require seat {installation, seat, role, binding}");
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
installation: raw["installation"],
|
|
59
|
+
seat: raw["seat"],
|
|
60
|
+
role: raw["role"],
|
|
61
|
+
binding: readSeatKey(raw["binding"]),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function validateStandard(schema, raw) {
|
|
65
|
+
const result = schema["~standard"].validate(raw);
|
|
66
|
+
if (isPromiseLike(result)) {
|
|
67
|
+
return { ok: "async" };
|
|
68
|
+
}
|
|
69
|
+
if (result.issues !== undefined) {
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
issues: result.issues.map((issue) => ({
|
|
73
|
+
path: issuePath(issue),
|
|
74
|
+
message: issue.message,
|
|
75
|
+
})),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return { ok: true, value: result.value };
|
|
79
|
+
}
|
|
80
|
+
export function validateRunnerOptions(runner, raw) {
|
|
81
|
+
const checked = validateStandard(runner.options, raw === undefined ? {} : raw);
|
|
82
|
+
if (checked.ok === "async") {
|
|
83
|
+
throw new RunnerError("INVALID_RUNNER", `runner ${runner.id} options schema must validate synchronously`);
|
|
84
|
+
}
|
|
85
|
+
return checked;
|
|
86
|
+
}
|
|
87
|
+
export function defineRunner(input) {
|
|
88
|
+
if (!isValidRunnerId(input.id)) {
|
|
89
|
+
throw new RunnerError("INVALID_RUNNER", `runner id ${JSON.stringify(input.id)} must match <installation>/runner/<name>/<v>`);
|
|
90
|
+
}
|
|
91
|
+
const std = input.options["~standard"];
|
|
92
|
+
if (std.version !== 1 || typeof std.validate !== "function") {
|
|
93
|
+
throw new RunnerError("INVALID_RUNNER", `runner ${input.id} options must be a Standard Schema v1`);
|
|
94
|
+
}
|
|
95
|
+
const admitOptions = (raw) => validateRunnerOptions(input, raw);
|
|
96
|
+
const dispatch = (point, args, powers) => {
|
|
97
|
+
const rawOptions = isRec(args) ? args["options"] : undefined;
|
|
98
|
+
const validated = admitOptions(rawOptions);
|
|
99
|
+
if (!validated.ok) {
|
|
100
|
+
const first = validated.issues[0];
|
|
101
|
+
const path = first !== undefined && first.path.length > 0 ? `${first.path.join(".")}: ` : "";
|
|
102
|
+
throw new RunnerError("INVALID_SEATING", `${path}${first?.message ?? "invalid options"}`);
|
|
103
|
+
}
|
|
104
|
+
if (point === "runner:wake") {
|
|
105
|
+
if (!isRec(args) || typeof args["prompt"] !== "string") {
|
|
106
|
+
throw new RunnerError("INVALID_INPUT", "runner:wake requires a string prompt");
|
|
107
|
+
}
|
|
108
|
+
const ref = typeof args["ref"] === "string" ? args["ref"] : undefined;
|
|
109
|
+
return input.wake({
|
|
110
|
+
seat: readSeatRef(args["seat"]),
|
|
111
|
+
prompt: args["prompt"],
|
|
112
|
+
...(ref !== undefined ? { ref } : {}),
|
|
113
|
+
options: validated.value,
|
|
114
|
+
}, powers);
|
|
115
|
+
}
|
|
116
|
+
if (!isRec(args) || typeof args["ref"] !== "string" || args["ref"].length === 0) {
|
|
117
|
+
throw new RunnerError("INVALID_INPUT", `${point} requires a non-empty string ref`);
|
|
118
|
+
}
|
|
119
|
+
if (point === "runner:poll") {
|
|
120
|
+
return input.poll({ ref: args["ref"], options: validated.value }, powers);
|
|
121
|
+
}
|
|
122
|
+
input.end({ seat: readSeatRef(args["seat"]), ref: args["ref"], options: validated.value }, powers);
|
|
123
|
+
return {};
|
|
124
|
+
};
|
|
125
|
+
return Object.freeze({
|
|
126
|
+
id: input.id,
|
|
127
|
+
options: input.options,
|
|
128
|
+
wake: input.wake,
|
|
129
|
+
poll: input.poll,
|
|
130
|
+
end: input.end,
|
|
131
|
+
admitOptions,
|
|
132
|
+
dispatch,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type CaseBindingDescriptor, type ConfigInput, type EmptyBindings, type MessageBindingDescriptor, type MessageKindInput, type ProtocolInput, type RoleInput, type ScenarioBuilder, type ScenarioInit, type SeatBindingDescriptor, type SourceNames } from "./types.js";
|
|
2
|
+
export declare const absent: {
|
|
3
|
+
readonly $absent: true;
|
|
4
|
+
};
|
|
5
|
+
export declare function contains(substring: string): {
|
|
6
|
+
readonly $contains: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function bindCase<const P extends string, const Par extends string = never>(protocol: ProtocolInput<P>, opts?: {
|
|
9
|
+
readonly parent?: Par | undefined;
|
|
10
|
+
readonly forRole?: RoleInput | undefined;
|
|
11
|
+
readonly scope?: string | undefined;
|
|
12
|
+
readonly index?: number | undefined;
|
|
13
|
+
}): CaseBindingDescriptor<Record<Par, string>, P>;
|
|
14
|
+
export declare function bindSeatRef<const R extends string, const C extends string = never>(role: RoleInput<R>, opts?: {
|
|
15
|
+
readonly case?: C | undefined;
|
|
16
|
+
readonly scope?: string | undefined;
|
|
17
|
+
readonly index?: number | undefined;
|
|
18
|
+
}): SeatBindingDescriptor<Record<C, string>, R>;
|
|
19
|
+
export declare function bindMessage<const MK extends string, const C extends string = never>(messageKind: MessageKindInput<MK>, opts?: {
|
|
20
|
+
readonly case?: C | undefined;
|
|
21
|
+
readonly index?: number | undefined;
|
|
22
|
+
}): MessageBindingDescriptor<Record<C, string>, MK>;
|
|
23
|
+
export declare function scenario<const Cfg extends ConfigInput | undefined = undefined>(init: ScenarioInit & {
|
|
24
|
+
readonly config?: Cfg;
|
|
25
|
+
}): ScenarioBuilder<EmptyBindings, EmptyBindings, EmptyBindings, SourceNames<Cfg>>;
|