@yaks/tools 0.0.0-stage → 0.2.36

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 (138) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +870 -2
  3. package/_dist/access.d.ts +3 -0
  4. package/_dist/access.d.ts.map +1 -0
  5. package/_dist/app-view.d.ts +27 -0
  6. package/_dist/app-view.d.ts.map +1 -0
  7. package/_dist/args.d.ts +23 -0
  8. package/_dist/args.d.ts.map +1 -0
  9. package/_dist/check.d.ts +32 -0
  10. package/_dist/check.d.ts.map +1 -0
  11. package/_dist/declared.d.ts +66 -0
  12. package/_dist/declared.d.ts.map +1 -0
  13. package/_dist/display.d.ts +2 -0
  14. package/_dist/display.d.ts.map +1 -0
  15. package/_dist/errors.d.ts +46 -0
  16. package/_dist/errors.d.ts.map +1 -0
  17. package/_dist/fault.d.ts +4 -0
  18. package/_dist/fault.d.ts.map +1 -0
  19. package/_dist/invoke.d.ts +13 -0
  20. package/_dist/invoke.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +31 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/outlast.d.ts +89 -0
  24. package/_dist/outlast.d.ts.map +1 -0
  25. package/_dist/persist.d.ts +5 -0
  26. package/_dist/persist.d.ts.map +1 -0
  27. package/_dist/read.d.ts +4 -0
  28. package/_dist/read.d.ts.map +1 -0
  29. package/_dist/readable.d.ts +59 -0
  30. package/_dist/readable.d.ts.map +1 -0
  31. package/_dist/registry.d.ts +53 -0
  32. package/_dist/registry.d.ts.map +1 -0
  33. package/_dist/result.d.ts +154 -0
  34. package/_dist/result.d.ts.map +1 -0
  35. package/_dist/routes.d.ts +11 -0
  36. package/_dist/routes.d.ts.map +1 -0
  37. package/_dist/runner.d.ts +161 -0
  38. package/_dist/runner.d.ts.map +1 -0
  39. package/_dist/said.d.ts +4 -0
  40. package/_dist/said.d.ts.map +1 -0
  41. package/_dist/schema.d.ts +76 -0
  42. package/_dist/schema.d.ts.map +1 -0
  43. package/_dist/state.d.ts +9 -0
  44. package/_dist/state.d.ts.map +1 -0
  45. package/_dist/title.d.ts +20 -0
  46. package/_dist/title.d.ts.map +1 -0
  47. package/_dist/value.d.ts +43 -0
  48. package/_dist/value.d.ts.map +1 -0
  49. package/_dist/views.d.ts +5 -0
  50. package/_dist/views.d.ts.map +1 -0
  51. package/_dist/vocab.d.ts +9 -0
  52. package/_dist/vocab.d.ts.map +1 -0
  53. package/_dist/watch.d.ts +28 -0
  54. package/_dist/watch.d.ts.map +1 -0
  55. package/access.js +3 -0
  56. package/access.js.map +1 -0
  57. package/access.ts +11 -0
  58. package/app-view.js +26 -0
  59. package/app-view.js.map +1 -0
  60. package/app-view.ts +41 -0
  61. package/args.js +136 -0
  62. package/args.js.map +1 -0
  63. package/args.ts +187 -0
  64. package/body.js +14 -0
  65. package/body.js.map +1 -0
  66. package/body.ts +15 -0
  67. package/check.js +67 -0
  68. package/check.js.map +1 -0
  69. package/check.ts +92 -0
  70. package/declared.js +529 -0
  71. package/declared.js.map +1 -0
  72. package/declared.ts +719 -0
  73. package/deno.json +1 -0
  74. package/display.js +21 -0
  75. package/display.js.map +1 -0
  76. package/display.ts +39 -0
  77. package/errors.js +99 -0
  78. package/errors.js.map +1 -0
  79. package/errors.ts +127 -0
  80. package/fault.js +17 -0
  81. package/fault.js.map +1 -0
  82. package/fault.ts +24 -0
  83. package/invoke.js +207 -0
  84. package/invoke.js.map +1 -0
  85. package/invoke.ts +210 -0
  86. package/mod.js +31 -0
  87. package/mod.js.map +1 -0
  88. package/mod.ts +78 -0
  89. package/outlast.js +416 -0
  90. package/outlast.js.map +1 -0
  91. package/outlast.ts +551 -0
  92. package/package.json +109 -4
  93. package/persist.js +18 -0
  94. package/persist.js.map +1 -0
  95. package/persist.ts +26 -0
  96. package/read.js +50 -0
  97. package/read.js.map +1 -0
  98. package/read.ts +58 -0
  99. package/readable.js +118 -0
  100. package/readable.js.map +1 -0
  101. package/readable.ts +134 -0
  102. package/registry.js +162 -0
  103. package/registry.js.map +1 -0
  104. package/registry.ts +207 -0
  105. package/result.js +497 -0
  106. package/result.js.map +1 -0
  107. package/result.ts +540 -0
  108. package/routes.js +46 -0
  109. package/routes.js.map +1 -0
  110. package/routes.ts +50 -0
  111. package/runner.js +748 -0
  112. package/runner.js.map +1 -0
  113. package/runner.ts +910 -0
  114. package/said.js +15 -0
  115. package/said.js.map +1 -0
  116. package/said.ts +12 -0
  117. package/schema.js +137 -0
  118. package/schema.js.map +1 -0
  119. package/schema.ts +134 -0
  120. package/state.js +69 -0
  121. package/state.js.map +1 -0
  122. package/state.ts +108 -0
  123. package/title.js +222 -0
  124. package/title.js.map +1 -0
  125. package/title.ts +257 -0
  126. package/value.js +57 -0
  127. package/value.js.map +1 -0
  128. package/value.ts +74 -0
  129. package/views.js +131 -0
  130. package/views.js.map +1 -0
  131. package/views.ts +125 -0
  132. package/vocab.js +22 -0
  133. package/vocab.js.map +1 -0
  134. package/vocab.json +268 -0
  135. package/vocab.ts +22 -0
  136. package/watch.js +64 -0
  137. package/watch.js.map +1 -0
  138. package/watch.ts +74 -0
package/app-view.ts ADDED
@@ -0,0 +1,41 @@
1
+ // What an MCP Apps view is served as: the type a host renders it under and
2
+ // the metadata saying which origins it may reach.
3
+
4
+ /**
5
+ * The type a view is served under: text/html with the profile the MCP Apps
6
+ * spec requires, which is how a host tells a page it renders from a page it
7
+ * merely reads.
8
+ */
9
+ export let VIEW_MIME = 'text/html;profile=mcp-app'
10
+
11
+ /**
12
+ * The origins a view is allowed to reach, by what it reaches them for (MCP
13
+ * Apps §Resource _meta): `connectDomains` is fetch and websockets,
14
+ * `resourceDomains` is images, styles, scripts and fonts, `baseUriDomains` is
15
+ * what a `<base href>` may name. Anything not listed is refused by the host's
16
+ * own CSP, so an empty policy means a page that fetches nothing at all,
17
+ * which is a declaration, not a gap.
18
+ */
19
+ export type Csp = {
20
+ connectDomains?: string[]
21
+ resourceDomains?: string[]
22
+ baseUriDomains?: string[]
23
+ }
24
+
25
+ /**
26
+ * Resource metadata for both portable MCP Apps and ChatGPT, on resource
27
+ * listings and read contents. `ui.domain` is optional and host-specific:
28
+ * Claude expects a hashed hostname, while ChatGPT accepts a website origin,
29
+ * so the OpenAI value is never copied into the portable field. The CSP lists
30
+ * the network destinations; it does not set the sandbox origin.
31
+ * https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx
32
+ * https://developers.openai.com/plugins/reference
33
+ */
34
+ export let uiMeta = (domain: string, csp: Csp = {}) => ({
35
+ ui: { csp },
36
+ 'openai/widgetDomain': domain,
37
+ 'openai/widgetCSP': {
38
+ connect_domains: csp.connectDomains ?? [],
39
+ resource_domains: csp.resourceDomains ?? [],
40
+ },
41
+ })
package/args.js ADDED
@@ -0,0 +1,136 @@
1
+ // A call's arguments, from what was stored to what the tool is handed: an
2
+ // object, checked against the tool's schema, and every argument the schema
3
+ // declares a reference (`ref`) resolved to the eid it names. The last step is
4
+ // here, once, rather than in each tool: an argument is what a person types (a
5
+ // name, `T-7`, a transcript's own id), and a tool that writes must never be
6
+ // handed one that names nothing, or it mints an entity under it.
7
+ import { namedBy, referenced, takes } from "@yaks/graph/call";
8
+ import { Refused } from "@yaks/graph/errors";
9
+ import { TOMBSTONE } from "@yaks/graph/bundle";
10
+ import { validateToolInput } from "@yaks/vocab/tools";
11
+ import { CallError } from "./errors.js";
12
+ export { CallError } from "./errors.js";
13
+ /** A call's arguments as stored: an object, or none at all, which asks with
14
+ * none. */ export let parsed = (args)=>{
15
+ if (args == null) return {};
16
+ if (typeof args != 'object' || Array.isArray(args)) {
17
+ throw new CallError('arguments', 'Tool arguments must be an object');
18
+ }
19
+ return args;
20
+ };
21
+ /** A tool's arguments, validated against the JSON Schema on its declaration;
22
+ * a tool that declares none takes them as they came. */ export let validated = (tool, args)=>tool.inputSchema ? validateToolInput(tool, args, (message)=>new CallError('arguments', message)) : {
23
+ ...args
24
+ };
25
+ /**
26
+ * The arguments a schema declares references, each with the kind it names:
27
+ * `ref` on the property, or on an array's items.
28
+ *
29
+ * ```ts
30
+ * import { refs } from './args.ts'
31
+ *
32
+ * refs({ properties: { task: { type: 'string', ref: 'task' } } })
33
+ * // [['task', 'task']]
34
+ * ```
35
+ */ export let refs = (schema)=>Object.entries(schema?.properties ?? {}).flatMap(([key, p])=>{
36
+ let ref = p?.ref ?? p?.items?.ref;
37
+ return typeof ref == 'string' ? [
38
+ [
39
+ key,
40
+ ref
41
+ ]
42
+ ] : [];
43
+ });
44
+ /**
45
+ * `schema` with no argument marked a reference: what a runner is handed for a
46
+ * tool whose own run resolves them where it reads (@yaks/tools `invoke`), so
47
+ * an id is never resolved first against a store the tool does not read.
48
+ *
49
+ * ```ts
50
+ * import { unreferenced } from './args.ts'
51
+ *
52
+ * unreferenced({
53
+ * type: 'object',
54
+ * properties: {
55
+ * id: { type: 'string', ref: 'entity' },
56
+ * ids: { type: 'array', items: { type: 'string', ref: 'task' } },
57
+ * },
58
+ * })
59
+ * // { type: 'object', properties: { id: { type: 'string' }, ids: { type: 'array', items: { type: 'string' } } } }
60
+ * ```
61
+ */ export let unreferenced = (schema)=>{
62
+ let { ref: _, items, properties, ...rest } = schema;
63
+ return {
64
+ ...rest,
65
+ ...items ? {
66
+ items: unreferenced(items)
67
+ } : {},
68
+ ...properties ? {
69
+ properties: Object.fromEntries(Object.entries(properties).map(([k, p])=>[
70
+ k,
71
+ p && typeof p == 'object' ? unreferenced(p) : p
72
+ ]))
73
+ } : {}
74
+ };
75
+ };
76
+ // What an argument said, as the ids in it.
77
+ let ids = (v)=>(Array.isArray(v) ? v : [
78
+ v
79
+ ]).filter((x)=>typeof x == 'string' && x != '');
80
+ // A reference that names nothing, refused naming the argument that held it.
81
+ let missing = (key, kind, refusal)=>new CallError('arguments', takes(key, kind, refusal));
82
+ /**
83
+ * Tool references are resolved here once: ids and registered aliases first,
84
+ * then an exact document title of the declared kind. A name must identify one
85
+ * entity; a read may still ask about a missing or deleted durable eid.
86
+ */ export let resolved = async (tool, args, graph)=>{
87
+ if (tool.scope?.some((key)=>args[key] != null && args[key] !== '')) {
88
+ return args;
89
+ }
90
+ let resolve = async (schema, value, key = '')=>{
91
+ let kind = schema?.ref ?? schema?.items?.ref;
92
+ if (typeof kind != 'string') {
93
+ if (Array.isArray(value) && schema?.items) {
94
+ return await Promise.all(value.map((item)=>resolve(schema.items, item, key)));
95
+ }
96
+ if (value && typeof value == 'object' && !Array.isArray(value) && schema?.properties) {
97
+ let out = {
98
+ ...value
99
+ };
100
+ for (let [name, child] of Object.entries(schema.properties)){
101
+ if (Object.hasOwn(value, name)) {
102
+ let at = key ? `${key}.${name}` : name;
103
+ Reflect.set(out, name, await resolve(child, Reflect.get(value, name), at));
104
+ }
105
+ }
106
+ return out;
107
+ }
108
+ return value;
109
+ }
110
+ let said = ids(value);
111
+ if (!said.length) return value;
112
+ let eids;
113
+ try {
114
+ eids = await referenced(graph, said, kind);
115
+ } catch (error) {
116
+ if (error instanceof Refused) throw missing(key, kind, error.message);
117
+ throw error;
118
+ }
119
+ let held = new Map((await graph.get(eids, [
120
+ kind
121
+ ])).map((b)=>[
122
+ b.entity.eid,
123
+ b
124
+ ]));
125
+ for(let i = 0; i < said.length; i++){
126
+ let row = held.get(eids[i]);
127
+ if (!tool.readOnly && (!row || row[TOMBSTONE] != null || row[kind] == null)) {
128
+ throw missing(key, kind, kind == 'entity' ? `${said[i]} names nothing` : `${said[i]} names no ${kind}: ${row && row[TOMBSTONE] == null ? `it names an entity that is not a ${kind}` : `nothing here has that id`}; ${namedBy(graph.vocab, kind)}`);
129
+ }
130
+ }
131
+ let index = 0;
132
+ return Array.isArray(value) ? value.map((item)=>typeof item == 'string' && item != '' ? eids[index++] : item) : eids[0];
133
+ };
134
+ return await resolve(tool.inputSchema, args);
135
+ };
136
+ //# sourceMappingURL=args.js.map
package/args.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"args.js","sources":["./args.ts"],"names":[],"mappings":"AAAA,0EA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AAI;IACf,IAAI,CAAC,KAAK,MAAM,EAAE,OAAO;IACzB,IAAI;IACJ,IAAI;MACF,OAAO,MAAM,WAAW,OAAO,MAAM;IACvC,EAAE,OAAO,OAAO;MACd,IAAI,iBAAiB,SAAS,MAAM,QAAQ,KAAK,MAAM,MAAM,OAAO;MACpE,MAAM;IACR;IACA,IAAI,OAAO,IAAI,IACb,CAAC,MAAM,MAAM,GAAG,CAAC,MAAM;MAAC;KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,IAAM;QAAC,EAAE,MAAM,CAAC,GAAG;QAAE;OAAE;IAE9D,IAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,EAAE,IAAK;MACpC,IAAI,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,EAAE;MAC1B,IACE,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,OAAO,GAAG,CAAC,UAAU,IAAI,QAAQ,GAAG,CAAC,KAAK,IAAI,IAAI,GACtE;QACA,MAAM,QACJ,KACA,MACA,QAAQ,WACJ,GAAG,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,GAC1B,GAAG,IAAI,CAAC,EAAE,CAAC,UAAU,EAAE,KAAK,EAAE,EAC9B,OAAO,GAAG,CAAC,UAAU,IAAI,OACrB,CAAC,iCAAiC,EAAE,MAAM,GAC1C,CAAC,wBAAwB,CAAC,CAC/B,EAAE,EAAE,QAAQ,MAAM,KAAK,EAAE,OAAO;MAEvC;IACF;IACA,IAAI,QAAQ;IACZ,OAAO,MAAM,OAAO,CAAC,SACjB,MAAM,GAAG,CAAC,CAAC,OACX,OAAO,QAAQ,YAAY,QAAQ,KAAK,IAAI,CAAC,QAAQ,GAAG,QAExD,IAAI,CAAC,EAAE;EACb;EACA,OAAO,MAAM,QAAQ,KAAK,WAAW,EAAE;AACzC,EAAC"}
package/args.ts ADDED
@@ -0,0 +1,187 @@
1
+ // A call's arguments, from what was stored to what the tool is handed: an
2
+ // object, checked against the tool's schema, and every argument the schema
3
+ // declares a reference (`ref`) resolved to the eid it names. The last step is
4
+ // here, once, rather than in each tool: an argument is what a person types (a
5
+ // name, `T-7`, a transcript's own id), and a tool that writes must never be
6
+ // handed one that names nothing, or it mints an entity under it.
7
+
8
+ import type { Graph, NamedTool } from 'jsr:@yaks/graph@0.2.36'
9
+ import { namedBy, referenced, takes } from '@yaks/graph/call'
10
+ import { Refused } from '@yaks/graph/errors'
11
+ import { TOMBSTONE } from '@yaks/graph/bundle'
12
+ import { validateToolInput } from '@yaks/vocab/tools'
13
+ import { CallError } from './errors.js'
14
+ export { CallError } from './errors.js'
15
+
16
+ type Args = Record<string, unknown>
17
+
18
+ /** A call's arguments as stored: an object, or none at all, which asks with
19
+ * none. */
20
+ export let parsed = (args: unknown): Args => {
21
+ if (args == null) return {}
22
+ if (typeof args != 'object' || Array.isArray(args)) {
23
+ throw new CallError('arguments', 'Tool arguments must be an object')
24
+ }
25
+ return args as Args
26
+ }
27
+
28
+ /** A tool's arguments, validated against the JSON Schema on its declaration;
29
+ * a tool that declares none takes them as they came. */
30
+ export let validated = (tool: NamedTool, args: Args): Args =>
31
+ tool.inputSchema
32
+ ? validateToolInput(
33
+ tool,
34
+ args,
35
+ (message) => new CallError('arguments', message),
36
+ )
37
+ : { ...args }
38
+
39
+ type Prop = { ref?: unknown; items?: Prop; properties?: Record<string, Prop> }
40
+
41
+ /**
42
+ * The arguments a schema declares references, each with the kind it names:
43
+ * `ref` on the property, or on an array's items.
44
+ *
45
+ * ```ts
46
+ * import { refs } from './args.ts'
47
+ *
48
+ * refs({ properties: { task: { type: 'string', ref: 'task' } } })
49
+ * // [['task', 'task']]
50
+ * ```
51
+ */
52
+ export let refs = (schema?: Record<string, unknown>): [string, string][] =>
53
+ Object.entries((schema?.properties ?? {}) as Record<string, Prop>)
54
+ .flatMap(([key, p]) => {
55
+ let ref = p?.ref ?? p?.items?.ref
56
+ return typeof ref == 'string' ? [[key, ref] as [string, string]] : []
57
+ })
58
+
59
+ /**
60
+ * `schema` with no argument marked a reference: what a runner is handed for a
61
+ * tool whose own run resolves them where it reads (@yaks/tools `invoke`), so
62
+ * an id is never resolved first against a store the tool does not read.
63
+ *
64
+ * ```ts
65
+ * import { unreferenced } from './args.ts'
66
+ *
67
+ * unreferenced({
68
+ * type: 'object',
69
+ * properties: {
70
+ * id: { type: 'string', ref: 'entity' },
71
+ * ids: { type: 'array', items: { type: 'string', ref: 'task' } },
72
+ * },
73
+ * })
74
+ * // { type: 'object', properties: { id: { type: 'string' }, ids: { type: 'array', items: { type: 'string' } } } }
75
+ * ```
76
+ */
77
+ export let unreferenced = <S extends Args>(schema: S): S => {
78
+ let { ref: _, items, properties, ...rest } = schema as Args & Prop
79
+ return {
80
+ ...rest,
81
+ ...items ? { items: unreferenced(items as Args) } : {},
82
+ ...properties
83
+ ? {
84
+ properties: Object.fromEntries(
85
+ Object.entries(properties).map(([k, p]) => [
86
+ k,
87
+ p && typeof p == 'object' ? unreferenced(p as Args) : p,
88
+ ]),
89
+ ),
90
+ }
91
+ : {},
92
+ } as unknown as S
93
+ }
94
+
95
+ // What an argument said, as the ids in it.
96
+ let ids = (v: unknown): string[] =>
97
+ (Array.isArray(v) ? v : [v]).filter((x): x is string =>
98
+ typeof x == 'string' && x != ''
99
+ )
100
+
101
+ // A reference that names nothing, refused naming the argument that held it.
102
+ let missing = (key: string, kind: string, refusal: string): CallError =>
103
+ new CallError('arguments', takes(key, kind, refusal))
104
+
105
+ /**
106
+ * Tool references are resolved here once: ids and registered aliases first,
107
+ * then an exact document title of the declared kind. A name must identify one
108
+ * entity; a read may still ask about a missing or deleted durable eid.
109
+ */
110
+ export let resolved = async (
111
+ tool: NamedTool,
112
+ args: Args,
113
+ graph: Graph,
114
+ ): Promise<Args> => {
115
+ if (tool.scope?.some((key) => args[key] != null && args[key] !== '')) {
116
+ return args
117
+ }
118
+ let resolve = async (
119
+ schema: Prop | undefined,
120
+ value: unknown,
121
+ key = '',
122
+ ): Promise<unknown> => {
123
+ let kind = schema?.ref ?? schema?.items?.ref
124
+ if (typeof kind != 'string') {
125
+ if (Array.isArray(value) && schema?.items) {
126
+ return await Promise.all(
127
+ value.map((item) => resolve(schema.items, item, key)),
128
+ )
129
+ }
130
+ if (
131
+ value && typeof value == 'object' && !Array.isArray(value) &&
132
+ schema?.properties
133
+ ) {
134
+ let out = { ...value }
135
+ for (let [name, child] of Object.entries(schema.properties)) {
136
+ if (Object.hasOwn(value, name)) {
137
+ let at = key ? `${key}.${name}` : name
138
+ Reflect.set(
139
+ out,
140
+ name,
141
+ await resolve(child, Reflect.get(value, name), at),
142
+ )
143
+ }
144
+ }
145
+ return out
146
+ }
147
+ return value
148
+ }
149
+ let said = ids(value)
150
+ if (!said.length) return value
151
+ let eids: string[]
152
+ try {
153
+ eids = await referenced(graph, said, kind)
154
+ } catch (error) {
155
+ if (error instanceof Refused) throw missing(key, kind, error.message)
156
+ throw error
157
+ }
158
+ let held = new Map(
159
+ (await graph.get(eids, [kind])).map((b) => [b.entity.eid, b]),
160
+ )
161
+ for (let i = 0; i < said.length; i++) {
162
+ let row = held.get(eids[i])
163
+ if (
164
+ !tool.readOnly && (!row || row[TOMBSTONE] != null || row[kind] == null)
165
+ ) {
166
+ throw missing(
167
+ key,
168
+ kind,
169
+ kind == 'entity'
170
+ ? `${said[i]} names nothing`
171
+ : `${said[i]} names no ${kind}: ${
172
+ row && row[TOMBSTONE] == null
173
+ ? `it names an entity that is not a ${kind}`
174
+ : `nothing here has that id`
175
+ }; ${namedBy(graph.vocab, kind)}`,
176
+ )
177
+ }
178
+ }
179
+ let index = 0
180
+ return Array.isArray(value)
181
+ ? value.map((item) =>
182
+ typeof item == 'string' && item != '' ? eids[index++] : item
183
+ )
184
+ : eids[0]
185
+ }
186
+ return await resolve(tool.inputSchema, args) as Args
187
+ }
package/body.js ADDED
@@ -0,0 +1,14 @@
1
+ // What a tool does, whole: its description, lowered from Markdown to the
2
+ // caller's structural elements, the body of its page. Loaded only where one
3
+ // is drawn (./views.ts), so a reader that never draws one never loads the
4
+ // parser.
5
+ import { parse, render } from "@yaks/markdown/structural";
6
+ export let body = (b, h)=>{
7
+ let said = b.tool?.description;
8
+ return h('div', {
9
+ class: 'Md'
10
+ }, render(parse(typeof said == 'string' ? said : '', {
11
+ breaks: false
12
+ }), h));
13
+ };
14
+ //# sourceMappingURL=body.js.map
package/body.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.js","sources":["./body.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4EAA4E;AAC5E,0EAA0E;AAC1E,UAAU;AACV,SAAS,KAAK,EAAE,MAAM,yCAA8C;AAGpE,OAAO,IAAI,OAAkB,CAAC,GAAG;EAC/B,IAAI,OAAQ,EAAE,IAAI,EAA0C;EAC5D,OAAO,EACL,OACA;IAAE,OAAO;EAAK,GACd,OAAO,MAAM,OAAO,QAAQ,WAAW,OAAO,IAAI;IAAE,QAAQ;EAAM,IAAI;AAE1E,EAAC"}
package/body.ts ADDED
@@ -0,0 +1,15 @@
1
+ // What a tool does, whole: its description, lowered from Markdown to the
2
+ // caller's structural elements, the body of its page. Loaded only where one
3
+ // is drawn (./views.ts), so a reader that never draws one never loads the
4
+ // parser.
5
+ import { parse, render } from '@yaks/markdown/structural'
6
+ import type { Rendering } from 'jsr:@yaks/render@0.2.36'
7
+
8
+ export let body: Rendering = (b, h) => {
9
+ let said = (b.tool as Record<string, unknown> | undefined)?.description
10
+ return h(
11
+ 'div',
12
+ { class: 'Md' },
13
+ render(parse(typeof said == 'string' ? said : '', { breaks: false }), h),
14
+ )
15
+ }
package/check.js ADDED
@@ -0,0 +1,67 @@
1
+ // A health check is a tool declared with the verb `check`. There is no
2
+ // registry of checks: the set of them is every tool with that verb in the
3
+ // loaded vocabulary. Load a plugin and its checks come with it; drop the
4
+ // plugin and they go too. So no list of checks can fall out of date, and no
5
+ // package owns another package's invariants.
6
+ //
7
+ // A check returns an ordinary tool answer — text on `content{body}`, with
8
+ // `output{source}` naming the call it came from — plus one extra component:
9
+ // `finding{level}` when it found something, `fail` for a measured violation of a
10
+ // contract the package keeps, `warn` for a leak or for a verdict it could not
11
+ // establish. Reporting faults is not itself a failure (`faulted` in
12
+ // ./result.ts reads the call's own `execution.state`), so a check that finds a
13
+ // broken graph still succeeded; `ailing` reads the level, and the CLI exits 1
14
+ // for either.
15
+ //
16
+ // Two rules carried over from the fleet doctor this replaces: a check that
17
+ // finds nothing still returns an answer (silence is indistinguishable from a
18
+ // check that never ran), and a check that cannot run reports `warn` rather
19
+ // than passing quietly.
20
+ /** The verb that makes a tool a check. */ export let CHECK = 'check';
21
+ /** The checks among a list of tools — every one whose verb is `check`. It
22
+ * filters the tool declarations the caller already has (`@yaks/graph/tools`
23
+ * `loadTools` keeps `noun` and `verb` on each), so nothing has to be
24
+ * registered or tracked separately. */ export let checks = (tools)=>tools.filter((t)=>t.verb == CHECK);
25
+ // The worst level among the findings, or nothing when there are none. A check
26
+ // with a single `fail` among its warnings is failing.
27
+ let worst = (found)=>found.some((f)=>f.level == 'fail') ? 'fail' : found.length ? 'warn' : undefined;
28
+ /**
29
+ * What a check returns: one bundle, however much it found. `about` is the
30
+ * claim that holds when nothing is found ("no transcript has stalled"); the
31
+ * text leads with it either way, and `finding{level}` carries the level for a
32
+ * caller that has to act on it.
33
+ *
34
+ * One bundle rather than one per finding, deliberately: the runner writes what
35
+ * a tool returns, and a sweep that created an entity per stale lease would
36
+ * fill the graph with reports that the graph is untidy.
37
+ */ export let checked = (call, about, found)=>{
38
+ let level = worst(found);
39
+ return [
40
+ {
41
+ entity: {
42
+ eid: '$check'
43
+ },
44
+ ...level ? {
45
+ finding: {
46
+ level
47
+ }
48
+ } : {},
49
+ content: {
50
+ // `about` is what holds when the check finds nothing, so a check that
51
+ // found something says it against that claim rather than under it.
52
+ body: found.length ? [
53
+ `${found.length} finding(s) against: ${about}`,
54
+ ...found.map(said)
55
+ ].join('\n') : `${about} — nothing to report`
56
+ },
57
+ output: {
58
+ source: call
59
+ }
60
+ }
61
+ ];
62
+ };
63
+ let said = (f)=>`- ${f.level}: ${f.text}`;
64
+ /** Whether a tool is a check whose answer reports a measured violation — what
65
+ * the CLI turns into exit code 1. Only a check's own answer counts: a query
66
+ * that returns the answers checks left in the graph is not failing. */ export let ailing = (tool, answer)=>tool.verb == CHECK && answer.some((b)=>b.finding?.level == 'fail');
67
+ //# sourceMappingURL=check.js.map
package/check.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check.js","sources":["./check.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,0EAA0E;AAC1E,yEAAyE;AACzE,4EAA4E;AAC5E,6CAA6C;AAC7C,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,iFAAiF;AACjF,8EAA8E;AAC9E,oEAAoE;AACpE,+EAA+E;AAC/E,8EAA8E;AAC9E,cAAc;AACd,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,2EAA2E;AAC3E,wBAAwB;AAIxB,wCAAwC,GACxC,OAAO,IAAI,QAAQ,QAAO;AAW1B;;;qCAGqC,GACrC,OAAO,IAAI,SAAS,CAA8B,QAChD,MAAM,MAAM,CAAC,CAAC,IAAM,EAAE,IAAI,IAAI,OAAM;AAEtC,8EAA8E;AAC9E,sDAAsD;AACtD,IAAI,QAAQ,CAAC,QACX,MAAM,IAAI,CAAC,CAAC,IAAM,EAAE,KAAK,IAAI,UACzB,SACA,MAAM,MAAM,GACZ,SACA;AAEN;;;;;;;;;CASC,GACD,OAAO,IAAI,UAAU,CACnB,MACA,OACA;EAEA,IAAI,QAAQ,MAAM;EAClB,OAAO;IAAC;MACN,QAAQ;QAAE,KAAK;MAAS;MACxB,GAAG,QAAQ;QAAE,SAAS;UAAE;QAAM;MAAE,IAAI,CAAC,CAAC;MACtC,SAAS;QACP,sEAAsE;QACtE,mEAAmE;QACnE,MAAM,MAAM,MAAM,GACd;UAAC,GAAG,MAAM,MAAM,CAAC,qBAAqB,EAAE,OAAO;aAAK,MAAM,GAAG,CAAC;SAAM,CACnE,IAAI,CAAC,QACN,GAAG,MAAM,oBAAoB,CAAC;MACpC;MACA,QAAQ;QAAE,QAAQ;MAAK;IACzB;GAAE;AACJ,EAAC;AAED,IAAI,OAAO,CAAC,IAAuB,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE;AAE5D;;qEAEqE,GACrE,OAAO,IAAI,SAAS,CAClB,MACA,SAEA,KAAK,IAAI,IAAI,SACb,OAAO,IAAI,CAAC,CAAC,IAAM,AAAC,EAAE,OAAO,EAAuB,SAAS,QAAO"}
package/check.ts ADDED
@@ -0,0 +1,92 @@
1
+ // A health check is a tool declared with the verb `check`. There is no
2
+ // registry of checks: the set of them is every tool with that verb in the
3
+ // loaded vocabulary. Load a plugin and its checks come with it; drop the
4
+ // plugin and they go too. So no list of checks can fall out of date, and no
5
+ // package owns another package's invariants.
6
+ //
7
+ // A check returns an ordinary tool answer — text on `content{body}`, with
8
+ // `output{source}` naming the call it came from — plus one extra component:
9
+ // `finding{level}` when it found something, `fail` for a measured violation of a
10
+ // contract the package keeps, `warn` for a leak or for a verdict it could not
11
+ // establish. Reporting faults is not itself a failure (`faulted` in
12
+ // ./result.ts reads the call's own `execution.state`), so a check that finds a
13
+ // broken graph still succeeded; `ailing` reads the level, and the CLI exits 1
14
+ // for either.
15
+ //
16
+ // Two rules carried over from the fleet doctor this replaces: a check that
17
+ // finds nothing still returns an answer (silence is indistinguishable from a
18
+ // check that never ran), and a check that cannot run reports `warn` rather
19
+ // than passing quietly.
20
+
21
+ import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
22
+
23
+ /** The verb that makes a tool a check. */
24
+ export let CHECK = 'check'
25
+
26
+ /** How bad a finding is. `fail` means a contract the package keeps was
27
+ * measured to be broken; `warn` means a leak, or a verdict the check could not
28
+ * establish. */
29
+ export type Level = 'fail' | 'warn'
30
+
31
+ /** One thing a check found. `text` is written for a person to read: what is
32
+ * wrong, what it is about, and what it costs. */
33
+ export type Finding = { level: Level; text: string }
34
+
35
+ /** The checks among a list of tools — every one whose verb is `check`. It
36
+ * filters the tool declarations the caller already has (`@yaks/graph/tools`
37
+ * `loadTools` keeps `noun` and `verb` on each), so nothing has to be
38
+ * registered or tracked separately. */
39
+ export let checks = <T extends { verb?: string }>(tools: T[]): T[] =>
40
+ tools.filter((t) => t.verb == CHECK)
41
+
42
+ // The worst level among the findings, or nothing when there are none. A check
43
+ // with a single `fail` among its warnings is failing.
44
+ let worst = (found: Finding[]): Level | undefined =>
45
+ found.some((f) => f.level == 'fail')
46
+ ? 'fail'
47
+ : found.length
48
+ ? 'warn'
49
+ : undefined
50
+
51
+ /**
52
+ * What a check returns: one bundle, however much it found. `about` is the
53
+ * claim that holds when nothing is found ("no transcript has stalled"); the
54
+ * text leads with it either way, and `finding{level}` carries the level for a
55
+ * caller that has to act on it.
56
+ *
57
+ * One bundle rather than one per finding, deliberately: the runner writes what
58
+ * a tool returns, and a sweep that created an entity per stale lease would
59
+ * fill the graph with reports that the graph is untidy.
60
+ */
61
+ export let checked = (
62
+ call: Eid,
63
+ about: string,
64
+ found: Finding[],
65
+ ): Bundle[] => {
66
+ let level = worst(found)
67
+ return [{
68
+ entity: { eid: '$check' },
69
+ ...level ? { finding: { level } } : {},
70
+ content: {
71
+ // `about` is what holds when the check finds nothing, so a check that
72
+ // found something says it against that claim rather than under it.
73
+ body: found.length
74
+ ? [`${found.length} finding(s) against: ${about}`, ...found.map(said)]
75
+ .join('\n')
76
+ : `${about} — nothing to report`,
77
+ },
78
+ output: { source: call },
79
+ }]
80
+ }
81
+
82
+ let said = (f: Finding): string => `- ${f.level}: ${f.text}`
83
+
84
+ /** Whether a tool is a check whose answer reports a measured violation — what
85
+ * the CLI turns into exit code 1. Only a check's own answer counts: a query
86
+ * that returns the answers checks left in the graph is not failing. */
87
+ export let ailing = (
88
+ tool: { verb?: string },
89
+ answer: Bundle[],
90
+ ): boolean =>
91
+ tool.verb == CHECK &&
92
+ answer.some((b) => (b.finding as Comp | undefined)?.level == 'fail')