@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.
Files changed (94) hide show
  1. package/README.md +457 -3
  2. package/dist/compile.d.ts +48 -0
  3. package/dist/compile.js +1240 -0
  4. package/dist/contract.d.ts +109 -0
  5. package/dist/contract.js +491 -0
  6. package/dist/evaluator.d.ts +23 -0
  7. package/dist/evaluator.js +40 -0
  8. package/dist/evidence.d.ts +23 -0
  9. package/dist/evidence.js +74 -0
  10. package/dist/guide.d.ts +8 -0
  11. package/dist/guide.js +16 -0
  12. package/dist/install.d.ts +104 -0
  13. package/dist/install.js +402 -0
  14. package/dist/iso.d.ts +10 -0
  15. package/dist/iso.js +73 -0
  16. package/dist/node/build.d.ts +2 -0
  17. package/dist/node/build.js +56 -0
  18. package/dist/node/telar-module.d.ts +2 -0
  19. package/dist/node/telar-module.js +28 -0
  20. package/dist/observer.d.ts +38 -0
  21. package/dist/observer.js +53 -0
  22. package/dist/pieces.d.ts +230 -0
  23. package/dist/pieces.js +178 -0
  24. package/dist/powers.d.ts +46 -0
  25. package/dist/powers.js +126 -0
  26. package/dist/protocol.d.ts +220 -0
  27. package/dist/protocol.js +243 -0
  28. package/dist/query.d.ts +67 -0
  29. package/dist/query.js +102 -0
  30. package/dist/read.d.ts +37 -0
  31. package/dist/read.js +27 -0
  32. package/dist/runner.d.ts +113 -0
  33. package/dist/runner.js +134 -0
  34. package/dist/scenario/builder.d.ts +25 -0
  35. package/dist/scenario/builder.js +603 -0
  36. package/dist/scenario/templates.d.ts +35 -0
  37. package/dist/scenario/templates.js +64 -0
  38. package/dist/scenario/types.d.ts +432 -0
  39. package/dist/scenario/types.js +10 -0
  40. package/dist/scenario/validate.d.ts +7 -0
  41. package/dist/scenario/validate.js +19 -0
  42. package/dist/scenario.d.ts +4 -0
  43. package/dist/scenario.js +4 -0
  44. package/dist/schema.d.ts +221 -0
  45. package/dist/schema.js +227 -0
  46. package/dist/seating.d.ts +47 -0
  47. package/dist/seating.js +26 -0
  48. package/dist/send-hook.d.ts +46 -0
  49. package/dist/send-hook.js +123 -0
  50. package/dist/show.d.ts +34 -0
  51. package/dist/show.js +1 -0
  52. package/dist/source.d.ts +85 -0
  53. package/dist/source.js +104 -0
  54. package/dist/spec.d.ts +21 -0
  55. package/dist/spec.js +18 -0
  56. package/dist/standard-schema.d.ts +21 -0
  57. package/dist/standard-schema.js +1 -0
  58. package/dist/std/accuracy.d.ts +12 -0
  59. package/dist/std/accuracy.js +122 -0
  60. package/dist/std/ack.d.ts +14 -0
  61. package/dist/std/ack.js +43 -0
  62. package/dist/std/actions.d.ts +105 -0
  63. package/dist/std/actions.js +153 -0
  64. package/dist/std/brief.d.ts +36 -0
  65. package/dist/std/brief.js +78 -0
  66. package/dist/std/claude-code.d.ts +9 -0
  67. package/dist/std/claude-code.js +113 -0
  68. package/dist/std/compare.d.ts +29 -0
  69. package/dist/std/compare.js +36 -0
  70. package/dist/std/git-workspace.d.ts +44 -0
  71. package/dist/std/git-workspace.js +184 -0
  72. package/dist/std/judgment.d.ts +85 -0
  73. package/dist/std/judgment.js +462 -0
  74. package/dist/std/observe-command.d.ts +23 -0
  75. package/dist/std/observe-command.js +151 -0
  76. package/dist/std/observe-github-checks.d.ts +24 -0
  77. package/dist/std/observe-github-checks.js +289 -0
  78. package/dist/std/observe-http-status.d.ts +27 -0
  79. package/dist/std/observe-http-status.js +193 -0
  80. package/dist/std/plan.d.ts +7 -0
  81. package/dist/std/plan.js +28 -0
  82. package/dist/std/queries.d.ts +31 -0
  83. package/dist/std/queries.js +318 -0
  84. package/dist/std/reading.d.ts +17 -0
  85. package/dist/std/reading.js +88 -0
  86. package/dist/std/schedule.d.ts +19 -0
  87. package/dist/std/schedule.js +122 -0
  88. package/dist/std.d.ts +15 -0
  89. package/dist/std.js +15 -0
  90. package/dist/types.d.ts +370 -0
  91. package/dist/types.js +1 -0
  92. package/dist/values.d.ts +81 -0
  93. package/dist/values.js +129 -0
  94. package/package.json +62 -5
@@ -0,0 +1,289 @@
1
+ import { parseIsoUtc } from "../iso.js";
2
+ import { defineObserver, ObserverError, } from "../observer.js";
3
+ export const GITHUB_CHECKS_OBSERVER_ID = "telar.std/observe/github-checks/1";
4
+ const SLUG_RE = /^[^\s/?#]+$/;
5
+ const HEX_SHA_RE = /^[0-9a-fA-F]+$/;
6
+ const isRec = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
7
+ const nonEmpty = (value) => typeof value === "string" && value.length > 0 ? value : undefined;
8
+ function optStr(raw, key, path) {
9
+ const value = raw[key];
10
+ if (value === undefined)
11
+ return { value: {} };
12
+ return typeof value === "string"
13
+ ? { value: { [key]: value } }
14
+ : { issues: [{ path: [...path, key], message: `${key} must be a string` }] };
15
+ }
16
+ function optScheme(raw, path) {
17
+ const value = raw["apiScheme"];
18
+ if (value === undefined)
19
+ return { value: {} };
20
+ if (value === "http" || value === "https")
21
+ return { value: { apiScheme: value } };
22
+ return {
23
+ issues: [{ path: [...path, "apiScheme"], message: 'apiScheme must be "http" or "https"' }],
24
+ };
25
+ }
26
+ function parseEntry(raw, path) {
27
+ if (!isRec(raw))
28
+ return { issues: [{ path, message: "a github-checks entry must be an object" }] };
29
+ const { owner, name, required } = raw;
30
+ if (typeof owner !== "string" || !SLUG_RE.test(owner)) {
31
+ return { issues: [{ path: [...path, "owner"], message: "owner must be a non-empty slug" }] };
32
+ }
33
+ if (typeof name !== "string" || !SLUG_RE.test(name)) {
34
+ return { issues: [{ path: [...path, "name"], message: "name must be a non-empty slug" }] };
35
+ }
36
+ const reqList = Array.isArray(required)
37
+ ? required.filter((item) => typeof item === "string" && item.length > 0)
38
+ : [];
39
+ if (!Array.isArray(required) || required.length === 0 || reqList.length !== required.length) {
40
+ return {
41
+ issues: [
42
+ { path: [...path, "required"], message: "required must be a non-empty list of strings" },
43
+ ],
44
+ };
45
+ }
46
+ const head = optStr(raw, "head", path);
47
+ const scheme = optScheme(raw, path);
48
+ const authority = optStr(raw, "apiAuthority", path);
49
+ if ("issues" in head)
50
+ return head;
51
+ if ("issues" in scheme)
52
+ return scheme;
53
+ if ("issues" in authority)
54
+ return authority;
55
+ return {
56
+ value: {
57
+ owner,
58
+ name,
59
+ required: reqList,
60
+ ...head.value,
61
+ ...scheme.value,
62
+ ...authority.value,
63
+ },
64
+ };
65
+ }
66
+ function parseParams(raw) {
67
+ if (!isRec(raw))
68
+ return { issues: [{ path: [], message: "params must be an object" }] };
69
+ const head = optStr(raw, "head", []);
70
+ const scheme = optScheme(raw, []);
71
+ const authority = optStr(raw, "apiAuthority", []);
72
+ if ("issues" in head)
73
+ return head;
74
+ if ("issues" in scheme)
75
+ return scheme;
76
+ if ("issues" in authority)
77
+ return authority;
78
+ if ("byRepo" in raw) {
79
+ const rawTable = raw["byRepo"];
80
+ if (!isRec(rawTable)) {
81
+ return {
82
+ issues: [
83
+ { path: ["byRepo"], message: "byRepo must be an object of github-checks entries" },
84
+ ],
85
+ };
86
+ }
87
+ const table = {};
88
+ for (const [key, entry] of Object.entries(rawTable)) {
89
+ const parsed = parseEntry(entry, ["byRepo", key]);
90
+ if ("issues" in parsed)
91
+ return parsed;
92
+ table[key] = parsed.value;
93
+ }
94
+ const repo = optStr(raw, "repo", []);
95
+ if ("issues" in repo)
96
+ return repo;
97
+ return {
98
+ value: {
99
+ byRepo: table,
100
+ ...repo.value,
101
+ ...head.value,
102
+ ...scheme.value,
103
+ ...authority.value,
104
+ },
105
+ };
106
+ }
107
+ return parseEntry(raw, []);
108
+ }
109
+ const params = {
110
+ "~standard": { version: 1, vendor: "telar.std", validate: parseParams },
111
+ };
112
+ function selectEntry(p, args) {
113
+ if (!("byRepo" in p))
114
+ return p;
115
+ const repo = p.repo ?? args["repo"];
116
+ if (typeof repo !== "string") {
117
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", 'missing string "repo" for byRepo lookup');
118
+ }
119
+ const entry = p.byRepo[repo];
120
+ if (entry === undefined) {
121
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", `no observer entry in byRepo for "${repo}"`);
122
+ }
123
+ return entry;
124
+ }
125
+ function getGithubJson(powers, url) {
126
+ const resp = powers.network({
127
+ url,
128
+ method: "GET",
129
+ headers: { Accept: "application/vnd.github+json", "User-Agent": "telar-std" },
130
+ });
131
+ if (resp.status < 200 || resp.status >= 300) {
132
+ throw new ObserverError("GITHUB_API_ERROR", `GitHub API error (status ${String(resp.status)}): ${resp.body}`);
133
+ }
134
+ let parsed;
135
+ try {
136
+ parsed = JSON.parse(resp.body);
137
+ }
138
+ catch (err) {
139
+ throw new ObserverError("INVALID_OBSERVER_RESPONSE", `invalid GitHub JSON: ${String(err)}`);
140
+ }
141
+ if (!isRec(parsed)) {
142
+ throw new ObserverError("INVALID_OBSERVER_RESPONSE", "GitHub response must be a JSON object");
143
+ }
144
+ return parsed;
145
+ }
146
+ function mergeState(states, name, next) {
147
+ const prev = states.get(name);
148
+ const combined = prev === "failed" || next === "failed"
149
+ ? "failed"
150
+ : prev === "pending" || next === "pending"
151
+ ? "pending"
152
+ : "green";
153
+ states.set(name, combined);
154
+ }
155
+ function jsonId(value) {
156
+ if (typeof value === "number" && Number.isFinite(value))
157
+ return String(value);
158
+ return nonEmpty(value);
159
+ }
160
+ function parseIsoMs(value) {
161
+ return typeof value === "string" ? parseIsoUtc(value) : undefined;
162
+ }
163
+ function collectCheckRuns(doc, states, runs) {
164
+ const list = doc["check_runs"];
165
+ if (!Array.isArray(list))
166
+ return;
167
+ for (const item of list) {
168
+ if (!isRec(item))
169
+ continue;
170
+ const name = item["name"];
171
+ if (typeof name !== "string")
172
+ continue;
173
+ const status = item["status"];
174
+ const conclusion = item["conclusion"];
175
+ const state = status !== "completed"
176
+ ? "pending"
177
+ : conclusion === "success" || conclusion === "neutral" || conclusion === "skipped"
178
+ ? "green"
179
+ : "failed";
180
+ mergeState(states, name, state);
181
+ const runId = jsonId(item["id"]);
182
+ const url = nonEmpty(item["html_url"]) ?? nonEmpty(item["details_url"]);
183
+ const startedAtMs = parseIsoMs(item["started_at"]);
184
+ const completedAtMs = parseIsoMs(item["completed_at"]);
185
+ runs.push({
186
+ name,
187
+ ...(runId !== undefined ? { runId } : {}),
188
+ ...(url !== undefined ? { url } : {}),
189
+ ...(startedAtMs !== undefined ? { startedAtMs } : {}),
190
+ ...(completedAtMs !== undefined ? { completedAtMs } : {}),
191
+ });
192
+ }
193
+ }
194
+ function collectStatuses(doc, states, runs) {
195
+ const list = doc["statuses"];
196
+ if (!Array.isArray(list))
197
+ return;
198
+ for (const item of list) {
199
+ if (!isRec(item))
200
+ continue;
201
+ const ctx = item["context"];
202
+ if (typeof ctx !== "string")
203
+ continue;
204
+ const rawState = item["state"];
205
+ const state = rawState === "success" ? "green" : rawState === "pending" ? "pending" : "failed";
206
+ mergeState(states, ctx, state);
207
+ const runId = jsonId(item["id"]);
208
+ const url = nonEmpty(item["target_url"]);
209
+ runs.push({
210
+ name: ctx,
211
+ ...(runId !== undefined ? { runId } : {}),
212
+ ...(url !== undefined ? { url } : {}),
213
+ });
214
+ }
215
+ }
216
+ function buildEvidence(required, runs) {
217
+ const wanted = new Set(required);
218
+ const refs = [];
219
+ const seen = new Set();
220
+ let minStart;
221
+ let maxEnd;
222
+ for (const run of runs) {
223
+ if (!wanted.has(run.name))
224
+ continue;
225
+ const id = run.runId ?? nonEmpty(run.name);
226
+ const key = `${id ?? ""}\0${run.url ?? ""}`;
227
+ if (!seen.has(key)) {
228
+ seen.add(key);
229
+ refs.push({
230
+ kind: "check-run",
231
+ ...(id !== undefined ? { id } : {}),
232
+ ...(run.url !== undefined ? { url: run.url } : {}),
233
+ });
234
+ }
235
+ if (run.startedAtMs !== undefined &&
236
+ run.completedAtMs !== undefined &&
237
+ run.completedAtMs >= run.startedAtMs) {
238
+ minStart = minStart === undefined ? run.startedAtMs : Math.min(minStart, run.startedAtMs);
239
+ maxEnd = maxEnd === undefined ? run.completedAtMs : Math.max(maxEnd, run.completedAtMs);
240
+ }
241
+ }
242
+ const durationMs = minStart !== undefined && maxEnd !== undefined && maxEnd >= minStart
243
+ ? maxEnd - minStart
244
+ : undefined;
245
+ if (refs.length === 0 && durationMs === undefined)
246
+ return undefined;
247
+ return {
248
+ ...(refs.length > 0 ? { refs } : {}),
249
+ ...(durationMs !== undefined ? { durationMs } : {}),
250
+ };
251
+ }
252
+ /**
253
+ * std's github-checks observer, written on the public SDK over `powers.network`.
254
+ * Reads GitHub check-runs and commit statuses for a commit SHA and evaluates `required`.
255
+ */
256
+ export const githubChecksObserver = defineObserver({
257
+ id: GITHUB_CHECKS_OBSERVER_ID,
258
+ params,
259
+ observe: ({ args, params: p }, powers) => {
260
+ const entry = selectEntry(p, args);
261
+ const head = nonEmpty(entry.head) ?? nonEmpty(p.head) ?? nonEmpty(args["head"]);
262
+ if (head === undefined || !HEX_SHA_RE.test(head)) {
263
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", head === undefined
264
+ ? 'missing non-empty string field "head"'
265
+ : `invalid commit sha "${head}"`);
266
+ }
267
+ const scheme = entry.apiScheme ?? p.apiScheme ?? "https";
268
+ const authority = nonEmpty(entry.apiAuthority) ?? nonEmpty(p.apiAuthority) ?? "api.github.com";
269
+ const base = `${scheme}://${authority}/repos/${entry.owner}/${entry.name}/commits/${head}`;
270
+ const states = new Map();
271
+ const runs = [];
272
+ collectCheckRuns(getGithubJson(powers, `${base}/check-runs`), states, runs);
273
+ collectStatuses(getGithubJson(powers, `${base}/status`), states, runs);
274
+ let allGreen = true;
275
+ for (const reqName of entry.required) {
276
+ const st = states.get(reqName);
277
+ if (st === "failed") {
278
+ const evidence = buildEvidence(entry.required, runs);
279
+ return evidence === undefined ? { ok: false } : { ok: false, evidence };
280
+ }
281
+ if (st !== "green")
282
+ allGreen = false;
283
+ }
284
+ if (!allGreen)
285
+ return { ok: null };
286
+ const evidence = buildEvidence(entry.required, runs);
287
+ return evidence === undefined ? { ok: true } : { ok: true, evidence };
288
+ },
289
+ });
@@ -0,0 +1,27 @@
1
+ import { type Observer } from "../observer.js";
2
+ /** One HTTP endpoint check: URL, expected status, optional JSON version field and bearer secret. */
3
+ export interface HttpStatusEntry {
4
+ readonly url: string;
5
+ readonly expectStatus: number;
6
+ readonly versionField?: string;
7
+ readonly head?: string;
8
+ readonly secretRef?: string;
9
+ }
10
+ /** Direct endpoint config or a table keyed by the check's `project` or `repo`. */
11
+ export type HttpStatusParams = HttpStatusEntry | {
12
+ readonly byProject: Readonly<Record<string, HttpStatusEntry>>;
13
+ readonly project?: string;
14
+ readonly head?: string;
15
+ readonly secretRef?: string;
16
+ } | {
17
+ readonly byRepo: Readonly<Record<string, HttpStatusEntry>>;
18
+ readonly repo?: string;
19
+ readonly head?: string;
20
+ readonly secretRef?: string;
21
+ };
22
+ export declare const HTTP_STATUS_OBSERVER_ID = "telar.std/observe/http-status/1";
23
+ /**
24
+ * std's http-status observer, written on the public SDK over `powers.network`.
25
+ * Performs a GET request, checks `expectStatus`, and optionally matches `versionField` to `head`.
26
+ */
27
+ export declare const httpStatusObserver: Observer<typeof HTTP_STATUS_OBSERVER_ID, HttpStatusParams>;
@@ -0,0 +1,193 @@
1
+ import { defineObserver, ObserverError, } from "../observer.js";
2
+ export const HTTP_STATUS_OBSERVER_ID = "telar.std/observe/http-status/1";
3
+ const isRec = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
+ const nonEmpty = (value) => typeof value === "string" && value.length > 0 ? value : undefined;
5
+ function validHttpUrl(raw) {
6
+ if (typeof raw !== "string")
7
+ return false;
8
+ const rest = raw.startsWith("https://")
9
+ ? raw.slice(8)
10
+ : raw.startsWith("http://")
11
+ ? raw.slice(7)
12
+ : undefined;
13
+ if (rest === undefined || rest.length === 0)
14
+ return false;
15
+ const slash = rest.indexOf("/");
16
+ const query = rest.indexOf("?");
17
+ const end = slash === -1
18
+ ? query === -1
19
+ ? rest.length
20
+ : query
21
+ : query === -1
22
+ ? slash
23
+ : Math.min(slash, query);
24
+ return end > 0;
25
+ }
26
+ function optStr(raw, key, path) {
27
+ const value = raw[key];
28
+ if (value === undefined)
29
+ return { value: {} };
30
+ return typeof value === "string"
31
+ ? { value: { [key]: value } }
32
+ : { issues: [{ path: [...path, key], message: `${key} must be a string` }] };
33
+ }
34
+ const ENV_SECRET_REF_RE = /^env:[A-Za-z_][A-Za-z0-9_]*$/;
35
+ function optSecretRef(raw, path) {
36
+ const value = raw["secretRef"];
37
+ if (value === undefined)
38
+ return { value: {} };
39
+ if (typeof value === "string" && ENV_SECRET_REF_RE.test(value)) {
40
+ return { value: { secretRef: value } };
41
+ }
42
+ return {
43
+ issues: [{ path: [...path, "secretRef"], message: 'secretRef must have the form "env:NAME"' }],
44
+ };
45
+ }
46
+ function parseEntry(raw, path) {
47
+ if (!isRec(raw))
48
+ return { issues: [{ path, message: "an http-status entry must be an object" }] };
49
+ const { url, expectStatus } = raw;
50
+ if (!validHttpUrl(url)) {
51
+ return {
52
+ issues: [{ path: [...path, "url"], message: "url must start with http:// or https://" }],
53
+ };
54
+ }
55
+ if (typeof expectStatus !== "number" ||
56
+ !Number.isInteger(expectStatus) ||
57
+ expectStatus < 100 ||
58
+ expectStatus > 599) {
59
+ return {
60
+ issues: [
61
+ { path: [...path, "expectStatus"], message: "expectStatus must be an integer 100..=599" },
62
+ ],
63
+ };
64
+ }
65
+ const vf = optStr(raw, "versionField", path);
66
+ const head = optStr(raw, "head", path);
67
+ const sec = optSecretRef(raw, path);
68
+ if ("issues" in vf)
69
+ return vf;
70
+ if ("issues" in head)
71
+ return head;
72
+ if ("issues" in sec)
73
+ return sec;
74
+ return { value: { url, expectStatus, ...vf.value, ...head.value, ...sec.value } };
75
+ }
76
+ function parseTable(raw, key) {
77
+ if (!isRec(raw))
78
+ return {
79
+ issues: [{ path: [key], message: `${key} must be an object of http-status entries` }],
80
+ };
81
+ const table = {};
82
+ for (const [name, entry] of Object.entries(raw)) {
83
+ const parsed = parseEntry(entry, [key, name]);
84
+ if ("issues" in parsed)
85
+ return parsed;
86
+ table[name] = parsed.value;
87
+ }
88
+ return { value: table };
89
+ }
90
+ function parseParams(raw) {
91
+ if (!isRec(raw))
92
+ return { issues: [{ path: [], message: "params must be an object" }] };
93
+ const head = optStr(raw, "head", []);
94
+ const sec = optSecretRef(raw, []);
95
+ if ("issues" in head)
96
+ return head;
97
+ if ("issues" in sec)
98
+ return sec;
99
+ if ("byProject" in raw) {
100
+ const table = parseTable(raw["byProject"], "byProject");
101
+ const project = optStr(raw, "project", []);
102
+ if ("issues" in table)
103
+ return table;
104
+ if ("issues" in project)
105
+ return project;
106
+ return { value: { byProject: table.value, ...project.value, ...head.value, ...sec.value } };
107
+ }
108
+ if ("byRepo" in raw) {
109
+ const table = parseTable(raw["byRepo"], "byRepo");
110
+ const repo = optStr(raw, "repo", []);
111
+ if ("issues" in table)
112
+ return table;
113
+ if ("issues" in repo)
114
+ return repo;
115
+ return { value: { byRepo: table.value, ...repo.value, ...head.value, ...sec.value } };
116
+ }
117
+ return parseEntry(raw, []);
118
+ }
119
+ const params = {
120
+ "~standard": { version: 1, vendor: "telar.std", validate: parseParams },
121
+ };
122
+ function lookup(table, key, given, args) {
123
+ const name = given ?? args[key];
124
+ const tableKey = key === "project" ? "byProject" : "byRepo";
125
+ if (typeof name !== "string") {
126
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", `missing string "${key}" for ${tableKey} lookup`);
127
+ }
128
+ const entry = table[name];
129
+ if (entry === undefined) {
130
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", `no observer entry in ${tableKey} for "${name}"`);
131
+ }
132
+ return entry;
133
+ }
134
+ function selectEntry(p, args) {
135
+ if ("byProject" in p)
136
+ return lookup(p.byProject, "project", p.project, args);
137
+ if ("byRepo" in p)
138
+ return lookup(p.byRepo, "repo", p.repo, args);
139
+ return p;
140
+ }
141
+ function checkVersionField(body, field, expectedHead) {
142
+ let doc;
143
+ try {
144
+ doc = JSON.parse(body);
145
+ }
146
+ catch (err) {
147
+ throw new ObserverError("INVALID_OBSERVER_RESPONSE", `expected JSON body for versionField "${field}": ${String(err)}`);
148
+ }
149
+ let cur = doc;
150
+ for (const segment of field.split(".")) {
151
+ if (!isRec(cur) || !(segment in cur))
152
+ return null;
153
+ cur = cur[segment];
154
+ }
155
+ return cur === expectedHead ? true : null;
156
+ }
157
+ /**
158
+ * std's http-status observer, written on the public SDK over `powers.network`.
159
+ * Performs a GET request, checks `expectStatus`, and optionally matches `versionField` to `head`.
160
+ */
161
+ export const httpStatusObserver = defineObserver({
162
+ id: HTTP_STATUS_OBSERVER_ID,
163
+ params,
164
+ observe: ({ args, params: p }, powers) => {
165
+ const entry = selectEntry(p, args);
166
+ const versionField = nonEmpty(entry.versionField);
167
+ let expectedHead;
168
+ if (versionField !== undefined) {
169
+ expectedHead = nonEmpty(entry.head) ?? nonEmpty(p.head) ?? nonEmpty(args["head"]);
170
+ if (expectedHead === undefined) {
171
+ throw new ObserverError("INVALID_OBSERVER_PARAMS", 'versionField requires a non-empty "head" value');
172
+ }
173
+ }
174
+ const secretRef = nonEmpty(entry.secretRef) ?? nonEmpty(p.secretRef);
175
+ const resp = powers.network({
176
+ url: entry.url,
177
+ method: "GET",
178
+ headers: { "User-Agent": "telar-std" },
179
+ ...(secretRef !== undefined ? { bearerSecret: secretRef } : {}),
180
+ });
181
+ const evidence = {
182
+ refs: [{ kind: "http", url: entry.url }],
183
+ status: resp.status,
184
+ };
185
+ if (resp.status !== entry.expectStatus)
186
+ return { ok: false, evidence };
187
+ if (versionField !== undefined && expectedHead !== undefined) {
188
+ const matched = checkVersionField(resp.body, versionField, expectedHead);
189
+ return matched === true ? { ok: true, evidence } : { ok: null };
190
+ }
191
+ return { ok: true, evidence };
192
+ },
193
+ });
@@ -0,0 +1,7 @@
1
+ import { type AnyContract, type AnyRole, type NonEmpty, type Protocol, type ProtocolTarget } from "../spec.js";
2
+ export declare function planProtocol<const Name extends string>(def: {
3
+ readonly name: Name;
4
+ readonly planner: AnyRole | NonEmpty<AnyRole>;
5
+ readonly itemProtocol: ProtocolTarget;
6
+ readonly approveShape?: AnyContract | undefined;
7
+ }): Protocol<Name, "planning" | "running" | "done", "done">;
@@ -0,0 +1,28 @@
1
+ import { answer, childrenEnded, contract, fields, protocol, step, t, } from "../spec.js";
2
+ export function planProtocol(def) {
3
+ const nonEmpty = t.string({ nonEmpty: true });
4
+ const approve = def.approveShape ?? contract(`${def.name}-approve`, t.tagged("Approve", { summary: nonEmpty }));
5
+ const planData = fields({
6
+ title: nonEmpty,
7
+ doneWhen: nonEmpty,
8
+ });
9
+ return protocol(def.name, {
10
+ states: ["planning", "running", "done"],
11
+ start: "planning",
12
+ final: ["done"],
13
+ data: planData,
14
+ children: [def.itemProtocol],
15
+ steps: [
16
+ step("approve", {
17
+ from: "planning",
18
+ to: "running",
19
+ needs: [answer(def.planner, approve)],
20
+ }),
21
+ step("finish", {
22
+ from: "running",
23
+ to: "done",
24
+ needs: [childrenEnded({ protocol: def.itemProtocol, allowEmpty: false })],
25
+ }),
26
+ ],
27
+ });
28
+ }
@@ -0,0 +1,31 @@
1
+ import { type Query, type QueryArg, type QueryRowSchema } from "../query.js";
2
+ import type { CasePhase } from "../scenario/types.js";
3
+ export interface CasesRow {
4
+ readonly case: string;
5
+ readonly protocol: string;
6
+ readonly state: string;
7
+ readonly phase: CasePhase;
8
+ readonly parent: string | null;
9
+ readonly enteredAt: number;
10
+ readonly timeInStateMs: number;
11
+ readonly waitingOn: readonly string[];
12
+ }
13
+ export interface CaseStoryRow {
14
+ readonly seq: number;
15
+ readonly id: string;
16
+ readonly when: number;
17
+ readonly kind: string;
18
+ readonly who: string;
19
+ readonly what: string;
20
+ readonly why: string;
21
+ }
22
+ /** Decodes one row of `cases`: `waitingOn` arrives as JSON text. */
23
+ export declare const CasesRow: QueryRowSchema<CasesRow>;
24
+ /** Every case on the trail with its state, phase, time in state and the roles it waits on. */
25
+ export declare const cases: Query<CasesRow>;
26
+ /** Decodes one row of `case-story`: composes who, what and why from the fact's kind, provenance and payload. */
27
+ export declare const CaseStoryRow: QueryRowSchema<CaseStoryRow>;
28
+ /** One case's facts in append order, each told as who did what and why. */
29
+ export declare const caseStory: Query<CaseStoryRow, {
30
+ readonly case: QueryArg<string>;
31
+ }>;