@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
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { PluginCallError } from "./install.js";
|
|
2
|
+
class WireError extends Error {
|
|
3
|
+
}
|
|
4
|
+
function isRec(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
function record(value, path, keys) {
|
|
8
|
+
if (!isRec(value))
|
|
9
|
+
throw new WireError(`${path}: expected an object`);
|
|
10
|
+
for (const key of Object.keys(value)) {
|
|
11
|
+
if (keys !== undefined && !keys.includes(key)) {
|
|
12
|
+
throw new WireError(`${path}.${key}: unexpected field`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function text(value, path, nonEmpty) {
|
|
18
|
+
if (typeof value !== "string")
|
|
19
|
+
throw new WireError(`${path}: expected a string`);
|
|
20
|
+
if (nonEmpty && value.length === 0)
|
|
21
|
+
throw new WireError(`${path}: must not be empty`);
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function name(rec, key, path) {
|
|
25
|
+
return text(rec[key], `${path}.${key}`, true);
|
|
26
|
+
}
|
|
27
|
+
function optionalName(rec, key, path) {
|
|
28
|
+
return rec[key] === undefined ? undefined : name(rec, key, path);
|
|
29
|
+
}
|
|
30
|
+
function origin(value, path) {
|
|
31
|
+
const tag = record(value, path, ["_tag", "step", "seat", "role"])["_tag"];
|
|
32
|
+
if (tag === "Step") {
|
|
33
|
+
const rec = record(value, path, ["_tag", "step"]);
|
|
34
|
+
return { _tag: "Step", step: name(rec, "step", path) };
|
|
35
|
+
}
|
|
36
|
+
if (tag === "Seat") {
|
|
37
|
+
const rec = record(value, path, ["_tag", "seat", "role"]);
|
|
38
|
+
return { _tag: "Seat", seat: name(rec, "seat", path), role: name(rec, "role", path) };
|
|
39
|
+
}
|
|
40
|
+
throw new WireError(`${path}._tag: expected "Step" or "Seat"`);
|
|
41
|
+
}
|
|
42
|
+
function subject(value, path) {
|
|
43
|
+
const tag = record(value, path, ["_tag", "message", "messageKind", "origin", "shape", "variant"])["_tag"];
|
|
44
|
+
if (tag === "Message") {
|
|
45
|
+
const rec = record(value, path, ["_tag", "message", "messageKind", "origin"]);
|
|
46
|
+
return {
|
|
47
|
+
_tag: "Message",
|
|
48
|
+
message: name(rec, "message", path),
|
|
49
|
+
messageKind: name(rec, "messageKind", path),
|
|
50
|
+
...(rec["origin"] === undefined ? {} : { origin: origin(rec["origin"], `${path}.origin`) }),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (tag === "Draft") {
|
|
54
|
+
const rec = record(value, path, ["_tag", "shape", "variant"]);
|
|
55
|
+
const variant = optionalName(rec, "variant", path);
|
|
56
|
+
return {
|
|
57
|
+
_tag: "Draft",
|
|
58
|
+
shape: name(rec, "shape", path),
|
|
59
|
+
...(variant === undefined ? {} : { variant }),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
throw new WireError(`${path}._tag: expected "Message" or "Draft"`);
|
|
63
|
+
}
|
|
64
|
+
function labels(value, path) {
|
|
65
|
+
const rec = record(value, path);
|
|
66
|
+
const out = {};
|
|
67
|
+
for (const [key, label] of Object.entries(rec))
|
|
68
|
+
out[key] = text(label, `${path}.${key}`, false);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function decode(code, read) {
|
|
72
|
+
try {
|
|
73
|
+
return read();
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
if (err instanceof WireError)
|
|
77
|
+
throw new PluginCallError(code, err.message);
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Reads the args of a send hook call. Throws `PluginCallError("INVALID_INPUT")` naming the bad field. */
|
|
82
|
+
export function decodeSendHookArgs(raw) {
|
|
83
|
+
return decode("INVALID_INPUT", () => {
|
|
84
|
+
const rec = record(raw, "args", ["subject", "case", "toRole", "seat", "rendered"]);
|
|
85
|
+
const kase = record(rec["case"], "args.case", ["id", "protocol", "project", "scope"]);
|
|
86
|
+
const seat = record(rec["seat"], "args.seat", ["id", "role", "labels"]);
|
|
87
|
+
const project = optionalName(kase, "project", "args.case");
|
|
88
|
+
const scope = optionalName(kase, "scope", "args.case");
|
|
89
|
+
return {
|
|
90
|
+
subject: subject(rec["subject"], "args.subject"),
|
|
91
|
+
case: {
|
|
92
|
+
id: name(kase, "id", "args.case"),
|
|
93
|
+
protocol: name(kase, "protocol", "args.case"),
|
|
94
|
+
...(project === undefined ? {} : { project }),
|
|
95
|
+
...(scope === undefined ? {} : { scope }),
|
|
96
|
+
},
|
|
97
|
+
toRole: name(rec, "toRole", "args"),
|
|
98
|
+
seat: {
|
|
99
|
+
id: name(seat, "id", "args.seat"),
|
|
100
|
+
role: name(seat, "role", "args.seat"),
|
|
101
|
+
labels: labels(seat["labels"], "args.seat.labels"),
|
|
102
|
+
},
|
|
103
|
+
rendered: text(rec["rendered"], "args.rendered", false),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** Reads a send hook verdict as telar-core does. Throws `PluginCallError("INVALID_VERDICT")`. */
|
|
108
|
+
export function decodeSendVerdict(raw) {
|
|
109
|
+
return decode("INVALID_VERDICT", () => {
|
|
110
|
+
const allow = record(raw, "verdict", ["allow", "reason"])["allow"];
|
|
111
|
+
if (allow === true) {
|
|
112
|
+
record(raw, "verdict", ["allow"]);
|
|
113
|
+
return { allow: true };
|
|
114
|
+
}
|
|
115
|
+
if (allow === false) {
|
|
116
|
+
return {
|
|
117
|
+
allow: false,
|
|
118
|
+
reason: name(record(raw, "verdict", ["allow", "reason"]), "reason", "verdict"),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
throw new WireError("verdict.allow: expected a boolean");
|
|
122
|
+
});
|
|
123
|
+
}
|
package/dist/show.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** A case as `telar.show/1` renders it, and as `read` returns it. */
|
|
2
|
+
export interface ShowCase {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly protocol: string;
|
|
5
|
+
readonly parent: string | null;
|
|
6
|
+
readonly scope: string | null;
|
|
7
|
+
readonly state: string;
|
|
8
|
+
readonly phase: "open" | "final-pending-effects" | "closed" | "cancelled";
|
|
9
|
+
readonly data: Readonly<Record<string, unknown>>;
|
|
10
|
+
}
|
|
11
|
+
/** The key a seat is bound by, as `telar.show/1` renders it. */
|
|
12
|
+
export type ShowSeatBinding = {
|
|
13
|
+
readonly kind: "case";
|
|
14
|
+
readonly case: string;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: "all";
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "field";
|
|
19
|
+
readonly field: string;
|
|
20
|
+
readonly value: string;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "ancestor";
|
|
23
|
+
readonly protocol: string;
|
|
24
|
+
readonly case: string;
|
|
25
|
+
};
|
|
26
|
+
/** A seat as `telar.show/1` renders it, and as `read` returns it. `holder` is what its runner last reported. */
|
|
27
|
+
export interface ShowSeat {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly role: string;
|
|
30
|
+
readonly binding: ShowSeatBinding;
|
|
31
|
+
readonly labels: Readonly<Record<string, string>>;
|
|
32
|
+
readonly phase: "active" | "closed";
|
|
33
|
+
readonly holder?: Readonly<Record<string, string>> | undefined;
|
|
34
|
+
}
|
package/dist/show.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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 SourceId = `${string}/source/${string}/${number}`;
|
|
5
|
+
/** One schema per field of the event data. The host merges these fields into the opened case's data. */
|
|
6
|
+
export type SourceEventFields = Readonly<Record<string, StandardSchema>>;
|
|
7
|
+
export type SourceEventData<Fields extends SourceEventFields> = {
|
|
8
|
+
readonly [K in keyof Fields]: Fields[K] extends StandardSchema<infer O> ? O : never;
|
|
9
|
+
};
|
|
10
|
+
export interface SourceEvent<Data> {
|
|
11
|
+
readonly key: string;
|
|
12
|
+
readonly at: string;
|
|
13
|
+
readonly data: Data;
|
|
14
|
+
}
|
|
15
|
+
export interface SourceLast {
|
|
16
|
+
readonly key: string;
|
|
17
|
+
readonly at: string;
|
|
18
|
+
}
|
|
19
|
+
export interface SourcePollInput<Options> {
|
|
20
|
+
readonly now: string;
|
|
21
|
+
readonly last?: SourceLast;
|
|
22
|
+
readonly options: Options;
|
|
23
|
+
}
|
|
24
|
+
export interface DefineSourceInput<Id extends SourceId, Options, Fields extends SourceEventFields> {
|
|
25
|
+
readonly id: Id;
|
|
26
|
+
readonly options: StandardSchema<Options>;
|
|
27
|
+
readonly event: Fields;
|
|
28
|
+
readonly poll: (input: SourcePollInput<Options>, powers: WorldPowers) => readonly SourceEvent<SourceEventData<Fields>>[];
|
|
29
|
+
}
|
|
30
|
+
export interface SourceEventIssue {
|
|
31
|
+
readonly key: string;
|
|
32
|
+
readonly message: string;
|
|
33
|
+
}
|
|
34
|
+
export interface SourcePollReply {
|
|
35
|
+
readonly events: readonly SourceEvent<Readonly<Record<string, unknown>>>[];
|
|
36
|
+
readonly invalid: readonly SourceEventIssue[];
|
|
37
|
+
}
|
|
38
|
+
export interface AnySource {
|
|
39
|
+
readonly id: SourceId;
|
|
40
|
+
readonly eventFields: readonly string[];
|
|
41
|
+
readonly admitOptions: (raw: unknown) => RunnerOptionValidation<unknown>;
|
|
42
|
+
readonly dispatch: (args: unknown, powers: WorldPowers) => SourcePollReply;
|
|
43
|
+
}
|
|
44
|
+
export interface Source<Id extends SourceId = SourceId, Options = Readonly<Record<string, unknown>>, Fields extends SourceEventFields = SourceEventFields> extends AnySource {
|
|
45
|
+
readonly id: Id;
|
|
46
|
+
readonly options: StandardSchema<Options>;
|
|
47
|
+
readonly event: Fields;
|
|
48
|
+
readonly poll: DefineSourceInput<Id, Options, Fields>["poll"];
|
|
49
|
+
}
|
|
50
|
+
export type SourceIdOf<S> = S extends {
|
|
51
|
+
readonly id: infer Id extends SourceId;
|
|
52
|
+
} ? Id : never;
|
|
53
|
+
export type SourceOptionsOf<S> = S extends {
|
|
54
|
+
readonly options: StandardSchema<infer O>;
|
|
55
|
+
} ? O : never;
|
|
56
|
+
export type SourceFieldsOf<S> = S extends {
|
|
57
|
+
readonly event: infer F;
|
|
58
|
+
} ? keyof F & string : never;
|
|
59
|
+
export interface AnySourceChoice {
|
|
60
|
+
readonly source: string;
|
|
61
|
+
readonly options?: unknown;
|
|
62
|
+
readonly opens: {
|
|
63
|
+
readonly protocol: string;
|
|
64
|
+
readonly data?: Readonly<Record<string, unknown>>;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export type SourceChoice<Sources extends readonly AnySource[]> = AnySource extends Sources[number] ? AnySourceChoice : Sources[number] extends infer S ? S extends AnySource ? {
|
|
68
|
+
readonly source: SourceIdOf<S>;
|
|
69
|
+
readonly options?: SourceOptionsOf<S> | undefined;
|
|
70
|
+
readonly opens: {
|
|
71
|
+
readonly protocol: string;
|
|
72
|
+
readonly data?: (Readonly<Record<string, unknown>> & {
|
|
73
|
+
readonly [K in SourceFieldsOf<S>]?: never;
|
|
74
|
+
}) | undefined;
|
|
75
|
+
};
|
|
76
|
+
} : never : never;
|
|
77
|
+
/** The team config's source instances, by name, each a choice among the installation's sources. */
|
|
78
|
+
export type SourcesConfig<Sources extends readonly AnySource[] = readonly []> = Readonly<Record<string, SourceChoice<Sources>>>;
|
|
79
|
+
export declare class SourceError extends Error {
|
|
80
|
+
readonly name: string;
|
|
81
|
+
readonly code: string;
|
|
82
|
+
constructor(code: string, message: string);
|
|
83
|
+
}
|
|
84
|
+
export declare function isValidSourceId(id: string): boolean;
|
|
85
|
+
export declare function defineSource<const Id extends SourceId, Options, const Fields extends SourceEventFields>(input: DefineSourceInput<Id, Options, Fields>): Source<Id, Options, Fields>;
|
package/dist/source.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { formatIsoUtc, parseIsoUtc } from "./iso.js";
|
|
2
|
+
import { validateStandard } from "./runner.js";
|
|
3
|
+
export class SourceError extends Error {
|
|
4
|
+
name = "SourceError";
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const SOURCE_ID_RE = /^[a-z][a-z0-9.-]*\/source\/[a-z][a-z0-9-]*\/[1-9][0-9]*$/;
|
|
12
|
+
export function isValidSourceId(id) {
|
|
13
|
+
return SOURCE_ID_RE.test(id);
|
|
14
|
+
}
|
|
15
|
+
function isRec(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
function issueText(issues) {
|
|
19
|
+
const first = issues[0];
|
|
20
|
+
if (first === undefined)
|
|
21
|
+
return "invalid";
|
|
22
|
+
return first.path.length > 0 ? `${first.path.join(".")}: ${first.message}` : first.message;
|
|
23
|
+
}
|
|
24
|
+
function decodeData(id, fields, raw) {
|
|
25
|
+
if (!isRec(raw))
|
|
26
|
+
return "data must be an object";
|
|
27
|
+
const extra = Object.keys(raw).find((k) => !(k in fields));
|
|
28
|
+
if (extra !== undefined)
|
|
29
|
+
return `data.${extra} is not a declared event field`;
|
|
30
|
+
const out = {};
|
|
31
|
+
for (const [name, schema] of Object.entries(fields)) {
|
|
32
|
+
const checked = validateStandard(schema, raw[name]);
|
|
33
|
+
if (checked.ok === "async") {
|
|
34
|
+
throw new SourceError("INVALID_SOURCE", `source ${id} event.${name} must validate synchronously`);
|
|
35
|
+
}
|
|
36
|
+
if (!checked.ok)
|
|
37
|
+
return `data.${name}: ${issueText(checked.issues)}`;
|
|
38
|
+
out[name] = checked.value;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
function readLast(raw) {
|
|
43
|
+
if (!isRec(raw) || typeof raw["key"] !== "string" || typeof raw["at"] !== "string") {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return { key: raw["key"], at: raw["at"] };
|
|
47
|
+
}
|
|
48
|
+
function decodeEvents(id, fields, now, events) {
|
|
49
|
+
const nowMs = parseIsoUtc(now) ?? Number.NEGATIVE_INFINITY;
|
|
50
|
+
const valid = [];
|
|
51
|
+
const invalid = [];
|
|
52
|
+
for (const event of events) {
|
|
53
|
+
const key = typeof event.key === "string" ? event.key : "";
|
|
54
|
+
if (key.length === 0) {
|
|
55
|
+
invalid.push({ key: "", message: "an event needs a non-empty key" });
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const at = typeof event.at === "string" ? parseIsoUtc(event.at) : undefined;
|
|
59
|
+
if (at === undefined || at > nowMs) {
|
|
60
|
+
invalid.push({ key, message: `at must be an ISO-UTC instant no later than ${now}` });
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const data = decodeData(id, fields, event.data);
|
|
64
|
+
if (typeof data === "string")
|
|
65
|
+
invalid.push({ key, message: data });
|
|
66
|
+
else
|
|
67
|
+
valid.push({ key, at: formatIsoUtc(at), data });
|
|
68
|
+
}
|
|
69
|
+
return { events: valid, invalid };
|
|
70
|
+
}
|
|
71
|
+
export function defineSource(input) {
|
|
72
|
+
if (!isValidSourceId(input.id)) {
|
|
73
|
+
throw new SourceError("INVALID_SOURCE", `source id ${JSON.stringify(input.id)} must match <installation>/source/<name>/<v>`);
|
|
74
|
+
}
|
|
75
|
+
const admitOptions = (raw) => {
|
|
76
|
+
const checked = validateStandard(input.options, raw === undefined ? {} : raw);
|
|
77
|
+
if (checked.ok === "async") {
|
|
78
|
+
throw new SourceError("INVALID_SOURCE", `source ${input.id} options must validate synchronously`);
|
|
79
|
+
}
|
|
80
|
+
return checked;
|
|
81
|
+
};
|
|
82
|
+
const dispatch = (args, powers) => {
|
|
83
|
+
const now = isRec(args) ? args["now"] : undefined;
|
|
84
|
+
if (typeof now !== "string" || parseIsoUtc(now) === undefined) {
|
|
85
|
+
throw new SourceError("INVALID_INPUT", "source:poll requires now as an ISO-UTC instant");
|
|
86
|
+
}
|
|
87
|
+
const options = admitOptions(isRec(args) ? args["options"] : undefined);
|
|
88
|
+
if (!options.ok) {
|
|
89
|
+
throw new SourceError("INVALID_SOURCE", issueText(options.issues));
|
|
90
|
+
}
|
|
91
|
+
const last = readLast(isRec(args) ? args["last"] : undefined);
|
|
92
|
+
const events = input.poll({ now, ...(last !== undefined ? { last } : {}), options: options.value }, powers);
|
|
93
|
+
return decodeEvents(input.id, input.event, now, events);
|
|
94
|
+
};
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
id: input.id,
|
|
97
|
+
options: input.options,
|
|
98
|
+
event: input.event,
|
|
99
|
+
eventFields: Object.freeze(Object.keys(input.event)),
|
|
100
|
+
poll: input.poll,
|
|
101
|
+
admitOptions,
|
|
102
|
+
dispatch,
|
|
103
|
+
});
|
|
104
|
+
}
|
package/dist/spec.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { PluginCallError, defineInstallation, type AdmissionProblem, type ConfiguredInstallation, type DeclareFrame, type DefineInstallationInput, type DefinedInstallation, type InstallationCallReply, type InstallationConfigBase, type InstallationTest, type SeatingFor, type SourcesFor, type TrailProof, } from "./install.js";
|
|
2
|
+
export { SpecCompileError, compile, installation, type Installation, type InstallationInput, type RosterInput, } from "./compile.js";
|
|
3
|
+
export { guide, type Guide } from "./guide.js";
|
|
4
|
+
export { mergeSeating, type InstalledRunnerChoice, type RoleSeating, type RoleSeatingBase, type RunnerChoice, type RunnerOptions, type SeatKey, type SeatRunner, type Seating, } from "./seating.js";
|
|
5
|
+
export { RunnerError, defineRunner, isValidRunnerId, validateRunnerOptions, type AnyRunner, type DefineRunnerInput, type Runner, type RunnerEndInput, type RunnerId, type RunnerIdOf, type RunnerHolder, type RunnerIdentity, type RunnerOptionIssue, type RunnerOptionValidation, type RunnerOptionsOf, type RunnerPoint, type RunnerPollInput, type RunnerReport, type RunnerSeatRef, type RunnerState, type RunnerWakeInput, type RunnerWork, type StandardValidation, validateStandard, } from "./runner.js";
|
|
6
|
+
export type { StandardSchema, StandardSchemaIssue, StandardSchemaIssuePath, StandardSchemaResult, } from "./standard-schema.js";
|
|
7
|
+
export { evidenceProblems, workEvidenceProblems, type Evidence, type EvidenceRef, type Readings, type WorkEvidence, } from "./evidence.js";
|
|
8
|
+
export { formatIsoUtc, isCanonicalIsoUtc, parseIsoUtc, zoneOffset } from "./iso.js";
|
|
9
|
+
export { ObserverError, defineObserver, isValidObserverId, type AnyObserver, type DefineObserverInput, type ObserveInput, type Observed, type Observer, type ObserverId, } from "./observer.js";
|
|
10
|
+
export { PowerError, hostWorldPowers, type WorldHttpMethod, type WorldNetworkRequest, type WorldNetworkResponse, type WorldPowers, type WorldProcessRequest, type WorldProcessResult, } from "./powers.js";
|
|
11
|
+
export { fakeRead, type FakeReadSeat, type Read, type ReadSeatsFilter } from "./read.js";
|
|
12
|
+
export { SourceError, defineSource, isValidSourceId, type AnySource, type DefineSourceInput, type Source, type SourceEvent, type SourceEventData, type SourceEventFields, type SourceChoice, type SourceEventIssue, type SourceFieldsOf, type SourceIdOf, type SourceOptionsOf, type SourcesConfig, type SourceId, type SourceLast, type SourcePollInput, type SourcePollReply, } from "./source.js";
|
|
13
|
+
export { EvaluatorError, defineEvaluator, isValidEvaluatorId, type AnyEvaluator, type Evaluation, type EvaluatorArgs, type EvaluatorId, type Evaluator, type StdEvaluatorId, } from "./evaluator.js";
|
|
14
|
+
export { QueryError, bindQuery, decodeQueryRows, defineQuery, type NoQueryArgs, type Query, type QueryArg, type QueryArgInput, type QueryArgs, type QueryBinding, type QueryParam, type QueryRowIssue, type QueryRowIssuePath, type QueryRowResult, type QueryRowSchema, } from "./query.js";
|
|
15
|
+
export { ack, ActionError, bound, check, defineAction, defineClass, isValidActionId, message, msg, openOnMiss, redeliver, role, roleBrief, roleView, type AckDef, type Action, type ActionId, type AnyAction, type AnyAuthClass, type AnyCheck, type AnyMessageKind, type AnyNamedBound, type AnyRole, type ArgsForContract, type AuthClass, type BoundByFieldInput, type BoundSpecInput, type CallActionEffectNode, type Check, type CheckNeedNode, type DelegationInput, type InlineBound, type MessageArgRef, type MessageKind, type MissActionDef, type MissArgInput, type NamedBound, type ProtocolTarget, type Role, type RunnableAction, type RoleBriefOverride, type RoleViewOverride, type SeatLabelsDef, type StepBoundInput, } from "./pieces.js";
|
|
16
|
+
export { answer, anyOf, append, authorize, boundReached, casesEnded, childrenEnded, childrenEndedIn, childrenIn, emit, openChild, protocol, protocolRef, received, set, step, withGuide, type AnswerNeedOpts, type AnyProtocol, type AnyProtocolRef, type AnyStep, type ChildFilterInput, type Effect, type FieldHandlesOfProtocolData, type Need, type Protocol, type ProtocolAckOverride, type ProtocolDataInput, type ProtocolRef, type SeatLabelReq, type Step, } from "./protocol.js";
|
|
17
|
+
export { caseRef, contract, t, type AnnotationsInput, type AnyContract, type AnyFieldSchemaEntry, type AnyStructContract, type AnyStructType, type AnyTaggedStruct, type AnyTypeDef, type AnyUnionType, type ArrayOpts, type BooleanOpts, type Brand, type CaseRef, type Contract, type FieldHandlesOf, type FieldSchemaMap, type FlattenedUnionType, type Infer, type InferStruct, type NumberOpts, type OptionalType, type StringOpts, type StructContract, type StructGuideOpts, type StructOpts, type StructType, type TaggedGuideOpts, type TaggedOpts, type TaggedStructType, type TypeDef, type UnionMemberInput, type UnionOpts, type UnionType, type VariantsOf, } from "./schema.js";
|
|
18
|
+
export type { ActionDecl, ArgValue, Audience, BoundByFieldDecl, BoundDecl, BoundRefDecl, BriefPiece, CheckDecl, CheckObserveDecl, ChildFilterDecl, ClassDecl, DelegationDecl, EffectDecl, EffortLevel, EngineManagedFieldPolicy, FallbackSeatBindingDecl, FieldByFieldDecl, GitGrantDecl, GranteeGrantsDecl, HooksDecl, HttpMethod, JsonObject, JsonPrimitive, JsonValue, MessageDecl, MessageFieldName, MissActionDecl, MissArgDecl, NeedDecl, NetworkGrantDecl, NetworkScheme, NonEmpty, ProcessGrantDecl, ProtocolDecl, RecordMode, RootGrantDecl, RoleBindingDecl, RoleDecl, RosterDecl, SchemaAnnotations, SeatBindingDecl, SeatLabelNeedDecl, SeatLabelsDecl, SpecAckDecl, StepBoundDecl, StepDecl, StructFieldDecl, TelarSpecV1, TypeSchema, ViewKind, ViewPolicyDecl, } from "./types.js";
|
|
19
|
+
export { cause, engine, field, fields, fromAnswer, literal, seat, type AnswerRef, type AnyArgRef, type ArgInput, type ArgMapInput, type CauseRef, type EngineByFieldOpts, type EngineDigestOpts, type EngineFieldDef, type EngineFieldOpts, type FieldRef, type FieldTable, type LiteralRef, type ProtocolFieldEntry, type ProtocolFieldMap, type RecordInput, type SeatRef, } from "./values.js";
|
|
20
|
+
export { decodeSendHookArgs, decodeSendVerdict, type SendHookArgs, type SendHookCase, type SendHookSeat, type SendHookSubject, type SendOrigin, type SendVerdict, } from "./send-hook.js";
|
|
21
|
+
export type { ShowCase, ShowSeat, ShowSeatBinding } from "./show.js";
|
package/dist/spec.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { PluginCallError, defineInstallation, } from "./install.js";
|
|
2
|
+
export { SpecCompileError, compile, installation, } from "./compile.js";
|
|
3
|
+
export { guide } from "./guide.js";
|
|
4
|
+
export { mergeSeating, } from "./seating.js";
|
|
5
|
+
export { RunnerError, defineRunner, isValidRunnerId, validateRunnerOptions, validateStandard, } from "./runner.js";
|
|
6
|
+
export { evidenceProblems, workEvidenceProblems, } from "./evidence.js";
|
|
7
|
+
export { formatIsoUtc, isCanonicalIsoUtc, parseIsoUtc, zoneOffset } from "./iso.js";
|
|
8
|
+
export { ObserverError, defineObserver, isValidObserverId, } from "./observer.js";
|
|
9
|
+
export { PowerError, hostWorldPowers, } from "./powers.js";
|
|
10
|
+
export { fakeRead } from "./read.js";
|
|
11
|
+
export { SourceError, defineSource, isValidSourceId, } from "./source.js";
|
|
12
|
+
export { EvaluatorError, defineEvaluator, isValidEvaluatorId, } from "./evaluator.js";
|
|
13
|
+
export { QueryError, bindQuery, decodeQueryRows, defineQuery, } from "./query.js";
|
|
14
|
+
export { ack, ActionError, bound, check, defineAction, defineClass, isValidActionId, message, msg, openOnMiss, redeliver, role, roleBrief, roleView, } from "./pieces.js";
|
|
15
|
+
export { answer, anyOf, append, authorize, boundReached, casesEnded, childrenEnded, childrenEndedIn, childrenIn, emit, openChild, protocol, protocolRef, received, set, step, withGuide, } from "./protocol.js";
|
|
16
|
+
export { caseRef, contract, t, } from "./schema.js";
|
|
17
|
+
export { cause, engine, field, fields, fromAnswer, literal, seat, } from "./values.js";
|
|
18
|
+
export { decodeSendHookArgs, decodeSendVerdict, } from "./send-hook.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type StandardSchemaIssuePath = readonly (PropertyKey | {
|
|
2
|
+
readonly key: PropertyKey;
|
|
3
|
+
})[];
|
|
4
|
+
export interface StandardSchemaIssue {
|
|
5
|
+
readonly message: string;
|
|
6
|
+
readonly path?: StandardSchemaIssuePath | undefined;
|
|
7
|
+
}
|
|
8
|
+
export type StandardSchemaResult<Output> = {
|
|
9
|
+
readonly value: Output;
|
|
10
|
+
readonly issues?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
readonly issues: readonly StandardSchemaIssue[];
|
|
13
|
+
};
|
|
14
|
+
/** A Standard Schema (v1). An Effect schema becomes one through `Schema.toStandardSchemaV1`. */
|
|
15
|
+
export interface StandardSchema<Output = unknown> {
|
|
16
|
+
readonly "~standard": {
|
|
17
|
+
readonly version: 1;
|
|
18
|
+
readonly vendor: string;
|
|
19
|
+
readonly validate: (value: unknown) => StandardSchemaResult<Output> | PromiseLike<StandardSchemaResult<Output>>;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Query } from "../query.js";
|
|
2
|
+
export interface JudgmentAccuracyRow {
|
|
3
|
+
readonly question: string;
|
|
4
|
+
readonly questionVersion: string;
|
|
5
|
+
readonly rung: number;
|
|
6
|
+
readonly labelled: number;
|
|
7
|
+
readonly correct: number;
|
|
8
|
+
readonly unlabelled: number;
|
|
9
|
+
readonly accuracy: number | null;
|
|
10
|
+
}
|
|
11
|
+
/** Per question, version and rung: how many confident answers a later override labelled, and how many it confirmed. */
|
|
12
|
+
export declare const judgmentAccuracy: Query<JudgmentAccuracyRow>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { defineQuery } from "../query.js";
|
|
2
|
+
const COLUMNS = new Set([
|
|
3
|
+
"question",
|
|
4
|
+
"questionVersion",
|
|
5
|
+
"rung",
|
|
6
|
+
"labelled",
|
|
7
|
+
"correct",
|
|
8
|
+
"unlabelled",
|
|
9
|
+
"accuracy",
|
|
10
|
+
]);
|
|
11
|
+
function isRec(value) {
|
|
12
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
function nonEmpty(row, key, issues) {
|
|
15
|
+
const value = row[key];
|
|
16
|
+
if (typeof value === "string" && value.length > 0)
|
|
17
|
+
return value;
|
|
18
|
+
issues.push({ message: "expected a non-empty string", path: [key] });
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
function count(row, key, issues) {
|
|
22
|
+
const value = row[key];
|
|
23
|
+
if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0)
|
|
24
|
+
return value;
|
|
25
|
+
issues.push({ message: "expected a count", path: [key] });
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
function ratio(row, issues) {
|
|
29
|
+
const value = row["accuracy"];
|
|
30
|
+
if (value === null || (typeof value === "number" && value >= 0 && value <= 1))
|
|
31
|
+
return value;
|
|
32
|
+
issues.push({ message: "expected null or a number from 0 to 1", path: ["accuracy"] });
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const rowSchema = {
|
|
36
|
+
"~standard": {
|
|
37
|
+
version: 1,
|
|
38
|
+
vendor: "telar.std",
|
|
39
|
+
validate: (value) => {
|
|
40
|
+
if (!isRec(value))
|
|
41
|
+
return { issues: [{ message: "expected an object" }] };
|
|
42
|
+
const issues = Object.keys(value)
|
|
43
|
+
.filter((key) => !COLUMNS.has(key))
|
|
44
|
+
.map((key) => ({ message: "unexpected column", path: [key] }));
|
|
45
|
+
const row = {
|
|
46
|
+
question: nonEmpty(value, "question", issues),
|
|
47
|
+
questionVersion: nonEmpty(value, "questionVersion", issues),
|
|
48
|
+
rung: count(value, "rung", issues),
|
|
49
|
+
labelled: count(value, "labelled", issues),
|
|
50
|
+
correct: count(value, "correct", issues),
|
|
51
|
+
unlabelled: count(value, "unlabelled", issues),
|
|
52
|
+
accuracy: ratio(value, issues),
|
|
53
|
+
};
|
|
54
|
+
return issues.length > 0 ? { issues } : { value: row };
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const SQL = `
|
|
59
|
+
WITH answers AS (
|
|
60
|
+
SELECT f.case_id AS judgment,
|
|
61
|
+
json_extract(f.payload, '$.value.question') AS question,
|
|
62
|
+
json_extract(f.payload, '$.value.questionVersion') AS questionVersion,
|
|
63
|
+
json_extract(f.payload, '$.value.rung') AS rung,
|
|
64
|
+
json_extract(f.payload, '$.value.option') AS option,
|
|
65
|
+
json_extract(f.payload, '$.value.probability') AS probability
|
|
66
|
+
FROM facts f
|
|
67
|
+
WHERE f.kind = 'case-data-recorded'
|
|
68
|
+
AND json_extract(f.payload, '$.field') = 'answers'
|
|
69
|
+
AND json_extract(f.payload, '$.mode') = 'append'
|
|
70
|
+
AND json_extract(f.payload, '$.value.probability') IS NOT NULL
|
|
71
|
+
),
|
|
72
|
+
predictions AS (
|
|
73
|
+
SELECT a.* FROM answers a
|
|
74
|
+
WHERE a.probability >= (
|
|
75
|
+
SELECT json_extract(c.payload, '$.args.params.thresholds[' || a.rung || ']')
|
|
76
|
+
FROM facts c
|
|
77
|
+
WHERE c.kind = 'check-observed' AND c.case_id = a.judgment
|
|
78
|
+
AND json_extract(c.payload, '$.check') = a.question || '.policy'
|
|
79
|
+
LIMIT 1
|
|
80
|
+
)
|
|
81
|
+
),
|
|
82
|
+
overrides AS (
|
|
83
|
+
SELECT json_extract(o.payload, '$.data.judgment') AS judgment,
|
|
84
|
+
json_extract(o.payload, '$.protocol') AS protocol,
|
|
85
|
+
closed.seq AS closedSeq,
|
|
86
|
+
(SELECT json_extract(d.payload, '$.value') FROM facts d
|
|
87
|
+
WHERE d.case_id = o.case_id AND d.kind = 'case-data-recorded'
|
|
88
|
+
AND json_extract(d.payload, '$.field') = 'option'
|
|
89
|
+
ORDER BY d.seq DESC LIMIT 1) AS truth
|
|
90
|
+
FROM facts o
|
|
91
|
+
JOIN facts closed ON closed.case_id = o.case_id AND closed.kind = 'case-closed'
|
|
92
|
+
WHERE o.kind = 'case-opened' AND json_extract(o.payload, '$.data.judgment') IS NOT NULL
|
|
93
|
+
),
|
|
94
|
+
latest AS (
|
|
95
|
+
SELECT o.judgment, o.protocol, o.truth FROM overrides o
|
|
96
|
+
WHERE o.closedSeq = (
|
|
97
|
+
SELECT MAX(x.closedSeq) FROM overrides x
|
|
98
|
+
WHERE x.judgment = o.judgment AND x.protocol = o.protocol
|
|
99
|
+
)
|
|
100
|
+
),
|
|
101
|
+
labelled AS (
|
|
102
|
+
SELECT p.question, p.questionVersion, p.rung, p.option, l.truth
|
|
103
|
+
FROM predictions p
|
|
104
|
+
LEFT JOIN latest l ON l.judgment = p.judgment AND l.protocol = p.question || '-override'
|
|
105
|
+
)
|
|
106
|
+
SELECT question, questionVersion, rung,
|
|
107
|
+
SUM(truth IS NOT NULL) AS labelled,
|
|
108
|
+
SUM(truth IS NOT NULL AND truth = option) AS correct,
|
|
109
|
+
SUM(truth IS NULL) AS unlabelled,
|
|
110
|
+
CASE WHEN SUM(truth IS NOT NULL) = 0 THEN NULL
|
|
111
|
+
ELSE 1.0 * SUM(truth IS NOT NULL AND truth = option) / SUM(truth IS NOT NULL) END
|
|
112
|
+
AS accuracy
|
|
113
|
+
FROM labelled
|
|
114
|
+
GROUP BY question, questionVersion, rung
|
|
115
|
+
ORDER BY question, questionVersion, rung
|
|
116
|
+
`;
|
|
117
|
+
/** Per question, version and rung: how many confident answers a later override labelled, and how many it confirmed. */
|
|
118
|
+
export const judgmentAccuracy = defineQuery({
|
|
119
|
+
name: "judgment-accuracy",
|
|
120
|
+
sql: SQL,
|
|
121
|
+
row: rowSchema,
|
|
122
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AnyContract, type AnyRole, type Contract, type MissActionDef, type NonEmpty, type Protocol, type ProtocolTarget, type TaggedStructType, type TypeDef } from "../spec.js";
|
|
2
|
+
export declare const HOUR_MS: 3600000;
|
|
3
|
+
export declare function defaultMissPolicy(missedAckTarget: ProtocolTarget, responsible: AnyRole): readonly [MissActionDef, MissActionDef];
|
|
4
|
+
export declare const missedAckAnswer: Contract<"missed-ack-answer", TaggedStructType<"Handled", {
|
|
5
|
+
readonly note: TypeDef<string>;
|
|
6
|
+
}>, {
|
|
7
|
+
readonly _tag: "Handled";
|
|
8
|
+
readonly note: string;
|
|
9
|
+
}, "Handled">;
|
|
10
|
+
export declare function missedAckProtocol(def: {
|
|
11
|
+
readonly name?: string | undefined;
|
|
12
|
+
readonly responsible: AnyRole | NonEmpty<AnyRole>;
|
|
13
|
+
readonly answerShape?: AnyContract | undefined;
|
|
14
|
+
}): Protocol<string, "open" | "handling" | "closed", "closed">;
|
package/dist/std/ack.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { answer, contract, engine, fields, fromAnswer, msg, openOnMiss, protocol, redeliver, set, step, t, } from "../spec.js";
|
|
2
|
+
import { ENGINEERING_TEXT_MAX, engineeringText } from "./brief.js";
|
|
3
|
+
export const HOUR_MS = 3_600_000;
|
|
4
|
+
export function defaultMissPolicy(missedAckTarget, responsible) {
|
|
5
|
+
return Object.freeze([
|
|
6
|
+
redeliver(),
|
|
7
|
+
openOnMiss(missedAckTarget, {
|
|
8
|
+
for: responsible,
|
|
9
|
+
data: {
|
|
10
|
+
message: msg.id,
|
|
11
|
+
messageKind: msg.kind,
|
|
12
|
+
toRole: msg.toRole,
|
|
13
|
+
},
|
|
14
|
+
}),
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
export const missedAckAnswer = contract("missed-ack-answer", t.tagged("Handled", {
|
|
18
|
+
note: engineeringText(ENGINEERING_TEXT_MAX, "How the missed acknowledgement was handled"),
|
|
19
|
+
}));
|
|
20
|
+
export function missedAckProtocol(def) {
|
|
21
|
+
const nonEmpty = t.string({ nonEmpty: true });
|
|
22
|
+
const data = fields({
|
|
23
|
+
message: nonEmpty,
|
|
24
|
+
messageKind: nonEmpty,
|
|
25
|
+
toRole: nonEmpty,
|
|
26
|
+
note: engine(t.optional(engineeringText())),
|
|
27
|
+
});
|
|
28
|
+
return protocol(def.name ?? "missed-ack", {
|
|
29
|
+
states: ["open", "handling", "closed"],
|
|
30
|
+
start: "open",
|
|
31
|
+
final: ["closed"],
|
|
32
|
+
data,
|
|
33
|
+
steps: [
|
|
34
|
+
step("start", { from: "open", to: "handling" }),
|
|
35
|
+
step("handle", {
|
|
36
|
+
from: "handling",
|
|
37
|
+
to: "closed",
|
|
38
|
+
needs: [answer(def.responsible, def.answerShape ?? missedAckAnswer)],
|
|
39
|
+
then: [set(data.note, fromAnswer("note"))],
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
}
|