@yaks/cli 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 (331) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1059 -2
  3. package/_dist/account-forward.d.ts +12 -0
  4. package/_dist/account-forward.d.ts.map +1 -0
  5. package/_dist/accounts.d.ts +23 -0
  6. package/_dist/accounts.d.ts.map +1 -0
  7. package/_dist/answer.d.ts +88 -0
  8. package/_dist/answer.d.ts.map +1 -0
  9. package/_dist/apply.d.ts +17 -0
  10. package/_dist/apply.d.ts.map +1 -0
  11. package/_dist/args.d.ts +86 -0
  12. package/_dist/args.d.ts.map +1 -0
  13. package/_dist/commands.d.ts +15 -0
  14. package/_dist/commands.d.ts.map +1 -0
  15. package/_dist/complete.d.ts +15 -0
  16. package/_dist/complete.d.ts.map +1 -0
  17. package/_dist/config.d.ts +37 -0
  18. package/_dist/config.d.ts.map +1 -0
  19. package/_dist/dependencies.d.ts +6 -0
  20. package/_dist/dependencies.d.ts.map +1 -0
  21. package/_dist/facets.d.ts +71 -0
  22. package/_dist/facets.d.ts.map +1 -0
  23. package/_dist/grammar.d.ts +5 -0
  24. package/_dist/grammar.d.ts.map +1 -0
  25. package/_dist/host.d.ts +164 -0
  26. package/_dist/host.d.ts.map +1 -0
  27. package/_dist/install.d.ts +43 -0
  28. package/_dist/install.d.ts.map +1 -0
  29. package/_dist/main.d.ts +16 -0
  30. package/_dist/main.d.ts.map +1 -0
  31. package/_dist/memo.d.ts +60 -0
  32. package/_dist/memo.d.ts.map +1 -0
  33. package/_dist/mod.d.ts +46 -0
  34. package/_dist/mod.d.ts.map +1 -0
  35. package/_dist/page.d.ts +73 -0
  36. package/_dist/page.d.ts.map +1 -0
  37. package/_dist/platform.d.ts +27 -0
  38. package/_dist/platform.d.ts.map +1 -0
  39. package/_dist/reader.d.ts +10 -0
  40. package/_dist/reader.d.ts.map +1 -0
  41. package/_dist/release.d.ts +48 -0
  42. package/_dist/release.d.ts.map +1 -0
  43. package/_dist/remote.d.ts +13 -0
  44. package/_dist/remote.d.ts.map +1 -0
  45. package/_dist/reported.d.ts +11 -0
  46. package/_dist/reported.d.ts.map +1 -0
  47. package/_dist/roles.d.ts +2 -0
  48. package/_dist/roles.d.ts.map +1 -0
  49. package/_dist/roster.d.ts +41 -0
  50. package/_dist/roster.d.ts.map +1 -0
  51. package/_dist/rpc.d.ts +29 -0
  52. package/_dist/rpc.d.ts.map +1 -0
  53. package/_dist/run.d.ts +164 -0
  54. package/_dist/run.d.ts.map +1 -0
  55. package/_dist/runtime.d.ts +4 -0
  56. package/_dist/runtime.d.ts.map +1 -0
  57. package/_dist/searching.d.ts +7 -0
  58. package/_dist/searching.d.ts.map +1 -0
  59. package/_dist/show.d.ts +22 -0
  60. package/_dist/show.d.ts.map +1 -0
  61. package/_dist/store.d.ts +25 -0
  62. package/_dist/store.d.ts.map +1 -0
  63. package/_dist/terminal.d.ts +17 -0
  64. package/_dist/terminal.d.ts.map +1 -0
  65. package/_dist/tool.d.ts +55 -0
  66. package/_dist/tool.d.ts.map +1 -0
  67. package/_dist/tty.d.ts +3 -0
  68. package/_dist/tty.d.ts.map +1 -0
  69. package/_dist/vault.d.ts +9 -0
  70. package/_dist/vault.d.ts.map +1 -0
  71. package/_dist/words.d.ts +11 -0
  72. package/_dist/words.d.ts.map +1 -0
  73. package/_dist/yak.d.ts +10 -0
  74. package/_dist/yak.d.ts.map +1 -0
  75. package/account-forward.js +40 -0
  76. package/account-forward.js.map +1 -0
  77. package/account-forward.ts +50 -0
  78. package/accounts.js +162 -0
  79. package/accounts.js.map +1 -0
  80. package/accounts.ts +228 -0
  81. package/answer.js +394 -0
  82. package/answer.js.map +1 -0
  83. package/answer.ts +569 -0
  84. package/apply.js +49 -0
  85. package/apply.js.map +1 -0
  86. package/apply.ts +54 -0
  87. package/args.js +420 -0
  88. package/args.js.map +1 -0
  89. package/args.ts +494 -0
  90. package/code.js +209 -0
  91. package/code.js.map +1 -0
  92. package/code.ts +227 -0
  93. package/commands.js +131 -0
  94. package/commands.js.map +1 -0
  95. package/commands.ts +142 -0
  96. package/complete.js +104 -0
  97. package/complete.js.map +1 -0
  98. package/complete.ts +152 -0
  99. package/config.js +125 -0
  100. package/config.js.map +1 -0
  101. package/config.ts +180 -0
  102. package/declarations.js +67 -0
  103. package/declarations.js.map +1 -0
  104. package/declarations.ts +84 -0
  105. package/deno.json +1 -0
  106. package/dependencies.js +126 -0
  107. package/dependencies.js.map +1 -0
  108. package/dependencies.ts +141 -0
  109. package/derive.js +102 -0
  110. package/derive.js.map +1 -0
  111. package/derive.ts +125 -0
  112. package/drain.js +83 -0
  113. package/drain.js.map +1 -0
  114. package/drain.ts +115 -0
  115. package/duties.ts +17 -0
  116. package/exports.json +1486 -0
  117. package/external.js +373 -0
  118. package/external.js.map +1 -0
  119. package/external.ts +435 -0
  120. package/facets.js +214 -0
  121. package/facets.js.map +1 -0
  122. package/facets.ts +232 -0
  123. package/feedback.js +54 -0
  124. package/feedback.js.map +1 -0
  125. package/feedback.ts +57 -0
  126. package/files.js +84 -0
  127. package/files.js.map +1 -0
  128. package/files.ts +112 -0
  129. package/get_starts.js +31 -0
  130. package/get_starts.js.map +1 -0
  131. package/get_starts.ts +34 -0
  132. package/git-credential.js +79 -0
  133. package/git-credential.js.map +1 -0
  134. package/git-credential.ts +77 -0
  135. package/grammar.js +4 -0
  136. package/grammar.js.map +1 -0
  137. package/grammar.ts +5 -0
  138. package/held.js +51 -0
  139. package/held.js.map +1 -0
  140. package/held.ts +66 -0
  141. package/host.js +1174 -0
  142. package/host.js.map +1 -0
  143. package/host.ts +1343 -0
  144. package/init.js +138 -0
  145. package/init.js.map +1 -0
  146. package/init.ts +139 -0
  147. package/install.js +216 -0
  148. package/install.js.map +1 -0
  149. package/install.ts +223 -0
  150. package/lifetime.js +56 -0
  151. package/lifetime.js.map +1 -0
  152. package/lifetime.ts +66 -0
  153. package/local-page.ts +114 -0
  154. package/local.js +417 -0
  155. package/local.js.map +1 -0
  156. package/local.ts +537 -0
  157. package/lockfile.js +20 -0
  158. package/lockfile.js.map +1 -0
  159. package/lockfile.ts +32 -0
  160. package/machines.js +33 -0
  161. package/machines.js.map +1 -0
  162. package/machines.ts +51 -0
  163. package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
  164. package/main.js +534 -0
  165. package/main.js.map +1 -0
  166. package/main.ts +547 -0
  167. package/mcp.js +108 -0
  168. package/mcp.js.map +1 -0
  169. package/mcp.ts +113 -0
  170. package/member.js +103 -0
  171. package/member.js.map +1 -0
  172. package/member.ts +133 -0
  173. package/memo.js +503 -0
  174. package/memo.js.map +1 -0
  175. package/memo.ts +634 -0
  176. package/mod.js +46 -0
  177. package/mod.js.map +1 -0
  178. package/mod.ts +99 -0
  179. package/operations.js +53 -0
  180. package/operations.js.map +1 -0
  181. package/operations.ts +68 -0
  182. package/package.json +115 -4
  183. package/page.js +211 -0
  184. package/page.js.map +1 -0
  185. package/page.ts +308 -0
  186. package/parser.js +45 -0
  187. package/parser.js.map +1 -0
  188. package/parser.ts +77 -0
  189. package/people-search.js +112 -0
  190. package/people-search.js.map +1 -0
  191. package/people-search.ts +162 -0
  192. package/platform.js +184 -0
  193. package/platform.js.map +1 -0
  194. package/platform.ts +224 -0
  195. package/platform_effects.js +5 -0
  196. package/platform_effects.js.map +1 -0
  197. package/platform_effects.ts +13 -0
  198. package/prepare.js +40 -0
  199. package/prepare.js.map +1 -0
  200. package/prepare.ts +46 -0
  201. package/read_activity.js +54 -0
  202. package/read_activity.js.map +1 -0
  203. package/read_activity.ts +61 -0
  204. package/read_thread.js +387 -0
  205. package/read_thread.js.map +1 -0
  206. package/read_thread.ts +506 -0
  207. package/read_worker.ts +164 -0
  208. package/reader.js +48 -0
  209. package/reader.js.map +1 -0
  210. package/reader.ts +57 -0
  211. package/release.js +82 -0
  212. package/release.js.map +1 -0
  213. package/release.json +127 -0
  214. package/release.ts +98 -0
  215. package/remote-page.ts +70 -0
  216. package/remote.js +96 -0
  217. package/remote.js.map +1 -0
  218. package/remote.ts +153 -0
  219. package/report.js +83 -0
  220. package/report.js.map +1 -0
  221. package/report.ts +101 -0
  222. package/reported.js +32 -0
  223. package/reported.js.map +1 -0
  224. package/reported.ts +38 -0
  225. package/restart.js +349 -0
  226. package/restart.js.map +1 -0
  227. package/restart.ts +478 -0
  228. package/restore.js +32 -0
  229. package/restore.js.map +1 -0
  230. package/restore.ts +38 -0
  231. package/roles.js +32 -0
  232. package/roles.js.map +1 -0
  233. package/roles.ts +49 -0
  234. package/roster.js +65 -0
  235. package/roster.js.map +1 -0
  236. package/roster.ts +84 -0
  237. package/rpc.js +113 -0
  238. package/rpc.js.map +1 -0
  239. package/rpc.ts +159 -0
  240. package/run.js +533 -0
  241. package/run.js.map +1 -0
  242. package/run.ts +741 -0
  243. package/runtime.js +381 -0
  244. package/runtime.js.map +1 -0
  245. package/runtime.ts +433 -0
  246. package/sandboxes.js +26 -0
  247. package/sandboxes.js.map +1 -0
  248. package/sandboxes.ts +30 -0
  249. package/searching.js +33 -0
  250. package/searching.js.map +1 -0
  251. package/searching.ts +50 -0
  252. package/show.js +109 -0
  253. package/show.js.map +1 -0
  254. package/show.ts +144 -0
  255. package/shown.js +13 -0
  256. package/shown.js.map +1 -0
  257. package/shown.ts +44 -0
  258. package/stall.js +90 -0
  259. package/stall.js.map +1 -0
  260. package/stall.ts +156 -0
  261. package/stall_profile.js +113 -0
  262. package/stall_profile.js.map +1 -0
  263. package/stall_profile.ts +142 -0
  264. package/stall_wait.js +183 -0
  265. package/stall_wait.js.map +1 -0
  266. package/stall_wait.ts +249 -0
  267. package/stall_wait_worker.ts +11 -0
  268. package/start.js +349 -0
  269. package/start.js.map +1 -0
  270. package/start.ts +343 -0
  271. package/starvation.js +147 -0
  272. package/starvation.js.map +1 -0
  273. package/starvation.ts +224 -0
  274. package/store.js +118 -0
  275. package/store.js.map +1 -0
  276. package/store.ts +193 -0
  277. package/subcommands.js +62 -0
  278. package/subcommands.js.map +1 -0
  279. package/subcommands.ts +77 -0
  280. package/terminal.js +93 -0
  281. package/terminal.js.map +1 -0
  282. package/terminal.ts +128 -0
  283. package/text.js +20 -0
  284. package/text.js.map +1 -0
  285. package/text.ts +37 -0
  286. package/tool.js +76 -0
  287. package/tool.js.map +1 -0
  288. package/tool.ts +157 -0
  289. package/trace_control.js +231 -0
  290. package/trace_control.js.map +1 -0
  291. package/trace_control.ts +233 -0
  292. package/traces.js +138 -0
  293. package/traces.js.map +1 -0
  294. package/traces.ts +160 -0
  295. package/tracker.js +22 -0
  296. package/tracker.js.map +1 -0
  297. package/tracker.ts +27 -0
  298. package/tty.js +82 -0
  299. package/tty.js.map +1 -0
  300. package/tty.ts +142 -0
  301. package/unit_failure.js +178 -0
  302. package/unit_failure.js.map +1 -0
  303. package/unit_failure.ts +191 -0
  304. package/upkeep_thread.js +32 -0
  305. package/upkeep_thread.js.map +1 -0
  306. package/upkeep_thread.ts +35 -0
  307. package/upkeep_worker.ts +27 -0
  308. package/vault.js +186 -0
  309. package/vault.js.map +1 -0
  310. package/vault.ts +220 -0
  311. package/vocab-ids.js +63 -0
  312. package/vocab-ids.js.map +1 -0
  313. package/vocab-ids.ts +62 -0
  314. package/vocab.json +35 -0
  315. package/warm.js +138 -0
  316. package/warm.js.map +1 -0
  317. package/warm.ts +171 -0
  318. package/watch_revision.js +25 -0
  319. package/watch_revision.js.map +1 -0
  320. package/watch_revision.ts +30 -0
  321. package/words.js +29 -0
  322. package/words.js.map +1 -0
  323. package/words.ts +41 -0
  324. package/workspaces.js +40 -0
  325. package/workspaces.js.map +1 -0
  326. package/workspaces.ts +58 -0
  327. package/yak-unit-failure.conf +6 -0
  328. package/yak-unit-handoff.conf +2 -0
  329. package/yak.js +123 -0
  330. package/yak.js.map +1 -0
  331. package/yak.ts +136 -0
package/answer.ts ADDED
@@ -0,0 +1,569 @@
1
+ // Portable answer rendering over the same views in a page or a terminal.
2
+ // Terminal painting and interactive presentation live in ./terminal.ts.
3
+ //
4
+ // The views are every configured plugin's `./views`, then @yaks/tools' (the
5
+ // host always carries its `content`), then the generic ones any entity has
6
+ // (@yaks/render/views). A server names no plugins, so its answers get the last
7
+ // two. An answer is a `Tile` per entity, one line apiece, whatever its length:
8
+ // a list of one is still a list, and a write says what it changed in a line.
9
+ // Only a tool that declares its answer a `page` (`graph show`, `mail show`)
10
+ // has a lone entity shown whole, as its `Page`, beside the links and comments
11
+ // the page asks for itself, since a tool answers only what it was asked.
12
+ //
13
+ // A terminal can hold more than a printout: a plugin's `./tui` exports views
14
+ // that are Preact components of their own — the harness's session, which is
15
+ // the whole interactive app — and those come first when the answer is held.
16
+ // They are never printed, because a component is not text.
17
+
18
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
19
+ import { type Built, joined as among, type Line, line, sub } from '@yaks/query'
20
+ import { human, idOf, short } from '@yaks/id'
21
+ import { homeOf } from '@yaks/reach'
22
+ import {
23
+ contributed,
24
+ define,
25
+ type H,
26
+ needsIn,
27
+ prepared,
28
+ type Registry,
29
+ type RenderContext,
30
+ type Renderer,
31
+ resolve,
32
+ type Selection,
33
+ } from '@yaks/render'
34
+ import type { ComponentRenderer } from 'jsr:@yaks/react@0.2.36'
35
+ import { names, reversed } from '@yaks/edge/vocab'
36
+ import { type Node, plain, safe, tree } from '@yaks/text'
37
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36/runtime'
38
+ import { type Related, type Shown, views as generic } from '@yaks/render/views'
39
+ import type { AnatomyObserver } from 'jsr:@yaks/code@0.2.36/anatomy'
40
+ import { relative } from '@yaks/ui/relative'
41
+
42
+ /** A terminal a held answer is drawn on: its width and how to write to it. */
43
+ export type Tty = { columns: number; write: (text: string) => void }
44
+
45
+ /** What one plugin's `./views` module offers a registry. */
46
+ export type ViewFacet = {
47
+ views?: Registry
48
+ inspectViews?: unknown
49
+ componentViews?:
50
+ | Selection<ComponentRenderer>
51
+ | (() =>
52
+ | Selection<ComponentRenderer>
53
+ | Promise<Selection<ComponentRenderer>>)
54
+ }
55
+ /** How one plugin's `./views` becomes a module: config.ts `pluginViews` for
56
+ * the plugins a config names, or modules a caller hands over inline. `warm`
57
+ * readies several at once before they are asked for one by one. */
58
+ export type Views = ((plugin: string) => Promise<ViewFacet | null>) & {
59
+ warm?: (plugins: string[]) => Promise<unknown>
60
+ }
61
+
62
+ type Hit = {
63
+ kind: string
64
+ title?: string
65
+ snippet: string
66
+ source: 'text' | 'meaning' | 'both'
67
+ session?: string
68
+ speaker?: 'input' | 'output'
69
+ /** the entity a call just made that this one may be a twin of */
70
+ near?: string
71
+ /** the entity a call just made that this one is a related resource of */
72
+ related?: string
73
+ /** the entity a call just made that this one was said or decided before
74
+ * on, and the name of who said it */
75
+ said?: string
76
+ by?: string
77
+ status?: string
78
+ }
79
+
80
+ let hitOf = (b: Bundle): Hit | null => {
81
+ let h = b.hit
82
+ if (
83
+ !h || typeof h != 'object' || !('kind' in h) ||
84
+ !('snippet' in h) || !('source' in h) ||
85
+ typeof h.kind != 'string' || typeof h.snippet != 'string' ||
86
+ !['text', 'meaning', 'both'].includes(String(h.source))
87
+ ) return null
88
+ return {
89
+ kind: h.kind,
90
+ title: 'title' in h && typeof h.title == 'string' ? h.title : undefined,
91
+ snippet: h.snippet,
92
+ source: h.source == 'text'
93
+ ? 'text'
94
+ : h.source == 'meaning'
95
+ ? 'meaning'
96
+ : 'both',
97
+ session: typeof h.session == 'string' ? h.session : undefined,
98
+ speaker: h.speaker == 'input'
99
+ ? 'input'
100
+ : h.speaker == 'output'
101
+ ? 'output'
102
+ : undefined,
103
+ near: 'near' in h && typeof h.near == 'string' ? h.near : undefined,
104
+ related: 'related' in h && typeof h.related == 'string'
105
+ ? h.related
106
+ : undefined,
107
+ said: 'said' in h && typeof h.said == 'string' ? h.said : undefined,
108
+ by: 'by' in h && typeof h.by == 'string' ? h.by : undefined,
109
+ status: 'status' in h && typeof h.status == 'string' ? h.status : undefined,
110
+ }
111
+ }
112
+
113
+ let excerpt = (text: string): string =>
114
+ safe(
115
+ text.replace(/\s+/g, ' ').replace(
116
+ // deno-lint-ignore no-control-regex -- FTS marks matches with these bytes.
117
+ /\x01([^\x02]*)\x02/g,
118
+ (_match, word: string) => `*${word}*`,
119
+ ),
120
+ ).trim()
121
+
122
+ let hit = <Node>(
123
+ b: Bundle,
124
+ h: H<Node>,
125
+ ctx: RenderContext<Node>,
126
+ ): Node => {
127
+ let found = hitOf(b)!
128
+ let s = ctx as Shown<Node>
129
+ let title = safe(found.title ?? '')
130
+ let kind = safe(found.kind)
131
+ let snippet = excerpt(found.snippet)
132
+ return h(
133
+ 'span',
134
+ { class: 'SearchHit' },
135
+ found.near
136
+ ? `near ${s.name(found.near)}: `
137
+ : found.related
138
+ ? `related to ${s.name(found.related)}: `
139
+ : found.said
140
+ ? `${safe(found.by ?? 'said')} earlier, on ${s.name(found.said)}: `
141
+ : '',
142
+ s.id(b),
143
+ title ? ` ${title}` : '',
144
+ ` · ${kind}`,
145
+ found.status ? ` ${safe(found.status)}` : '',
146
+ found.session && found.speaker
147
+ ? ` ${found.speaker} in session ${short(found.session)}`
148
+ : '',
149
+ found.source == 'meaning' ? ' (meaning)' : '',
150
+ snippet ? ` — ${snippet}` : '',
151
+ )
152
+ }
153
+
154
+ let search: Renderer = { view: 'Search.Tile', match: true, render: hit }
155
+
156
+ /** Every view the `yak` command draws with, most knowing first. */
157
+ export function registry(
158
+ plugins: string[],
159
+ load: Views,
160
+ observe: AnatomyObserver | undefined,
161
+ components: true,
162
+ ): Promise<Registry<Renderer | ComponentRenderer>>
163
+ export function registry(
164
+ plugins: string[],
165
+ load: Views,
166
+ observe?: AnatomyObserver,
167
+ ): Promise<Registry>
168
+ export async function registry(
169
+ plugins: string[],
170
+ load: Views,
171
+ observe?: AnatomyObserver,
172
+ components = false,
173
+ ): Promise<Registry<Renderer | ComponentRenderer>> {
174
+ let named = [...new Set([...plugins, '@yaks/tools'])]
175
+ await load.warm?.(named)
176
+ let found = await Promise.all(named.map(async (plugin) => {
177
+ let m = await load(plugin)
178
+ let faces = components
179
+ ? typeof m?.componentViews == 'function'
180
+ ? await m.componentViews()
181
+ : m?.componentViews
182
+ : undefined
183
+ let views = define(contributed(plugin, [
184
+ ...m?.views?.renderers ?? [],
185
+ ...faces?.renderers ?? [],
186
+ ]))
187
+ observe?.({
188
+ package: plugin,
189
+ facet: 'views',
190
+ loaded: m !== null,
191
+ bound: true,
192
+ value: faces ? { ...m, views } : m,
193
+ })
194
+ return views
195
+ }))
196
+ observe?.({
197
+ package: '@yaks/cli',
198
+ facet: 'views',
199
+ loaded: true,
200
+ bound: true,
201
+ value: { views: define([search]) },
202
+ })
203
+ observe?.({
204
+ package: '@yaks/render',
205
+ facet: 'views',
206
+ loaded: true,
207
+ bound: true,
208
+ value: { views: generic },
209
+ })
210
+ let all = define([
211
+ ...found.flatMap((m) => m.renderers),
212
+ search,
213
+ ...generic.renderers,
214
+ ])
215
+ return components ? prepared(all) : all
216
+ }
217
+
218
+ /** What a terminal draws with: portable views, and components of its own. */
219
+ export type Held = Selection<Renderer | ComponentRenderer>
220
+
221
+ /** How one plugin's `./tui` becomes a module. */
222
+ export type Tui = (plugin: string) => Promise<{ views?: Held } | null>
223
+
224
+ /** The views an answer held in the terminal draws with: each plugin's
225
+ * `./tui`, then {@link registry}'s. */
226
+ export let terminal = async (
227
+ plugins: string[],
228
+ load: Views,
229
+ held: Tui,
230
+ observe?: AnatomyObserver,
231
+ ): Promise<Held> => {
232
+ let own = await Promise.all(plugins.map(async (plugin) => {
233
+ let m = await held(plugin)
234
+ observe?.({
235
+ package: plugin,
236
+ facet: 'tui',
237
+ loaded: m !== null,
238
+ bound: true,
239
+ value: m,
240
+ })
241
+ return contributed(plugin, m?.views?.renderers ?? [])
242
+ }))
243
+ return prepared(define([
244
+ ...own.flat(),
245
+ ...(await registry(plugins, load, observe, true)).renderers,
246
+ ]))
247
+ }
248
+
249
+ /** What a drawing has read beyond its answer, and how it asks for more:
250
+ * `answers` holds the rows each query a view asked answered; `eid` and `query`
251
+ * are told of each entity and query a view asked for, so a printout can read
252
+ * them and draw again; `deferred` of a renderer the host must prepare. */
253
+ export type Wants = {
254
+ answers?: ReadonlyMap<string, Bundle[]>
255
+ eid?: (eid: string) => void
256
+ query?: (q: string) => void
257
+ deferred?: (renderer: Renderer) => void
258
+ }
259
+
260
+ // What a view cannot know from one bundle, for a terminal: an entity another
261
+ // names reads as its id where the answer or `named` holds it, a query a view
262
+ // asks answers what `want` holds, nothing links, and a moment reads as a
263
+ // browser says it, `5 minutes ago`.
264
+ let shown = <Node>(
265
+ vocab: Vocab,
266
+ answer: Bundle[],
267
+ draw: (b: Bundle, view: string, ctx: Shown<Node>) => Node | null,
268
+ named: Bundle[] = [],
269
+ links: Bundle[] = [],
270
+ want: Wants = {},
271
+ ): Shown<Node> => {
272
+ let demand = want.eid
273
+ // A number is its store's own: one another host gave is shown with that
274
+ // host (`T-12@yaks.app`), so it is never taken for this graph's T-12.
275
+ let homed = (b: Bundle, text: string) => {
276
+ let at = b.entity.num ? homeOf(b) : undefined
277
+ return at ? `${text}@${at}` : text
278
+ }
279
+ let humanOf = human(vocab)
280
+ let id = (b: Bundle) => homed(b, humanOf(b))
281
+ let idAs = idOf(vocab)
282
+ let held = new Map(
283
+ [...links, ...named, ...answer].map((b) => [b.entity.eid, b]),
284
+ )
285
+ let ctx: Shown<Node> & Pick<RenderContext<Node>, 'needed'> = {
286
+ needed: demand ? (eids) => eids.forEach(demand) : undefined,
287
+ id: (b) => {
288
+ let hit = hitOf(b)
289
+ return hit ? homed(b, idAs({ ...b.entity, kind: hit.kind })) : id(b)
290
+ },
291
+ kind: (b) => hitOf(b)?.kind ?? (vocab.kindOf(b) || 'entity'),
292
+ name: (eid) => {
293
+ demand?.(eid)
294
+ let b = held.get(eid)
295
+ return b ? id(b) : short(eid)
296
+ },
297
+ when: (at) => relative(at),
298
+ show: (b, view) => {
299
+ demand?.(b.entity.eid)
300
+ return draw(b, view, ctx)
301
+ },
302
+ relation: (b) => relationOf(vocab, b),
303
+ get: (eid) => {
304
+ demand?.(eid)
305
+ return held.get(eid)
306
+ },
307
+ query: (q) => {
308
+ let rows = want.answers?.get(q)
309
+ if (!rows) want.query?.(q)
310
+ return rows
311
+ },
312
+ related: (eid, relation) =>
313
+ links.flatMap((b) => {
314
+ let edge = b.edge as { from?: string; to?: string } | undefined
315
+ return b[relation] && edge?.to == eid && edge.from
316
+ ? [held.get(edge.from) ?? { entity: { eid: edge.from } }]
317
+ : []
318
+ }),
319
+ }
320
+ return ctx
321
+ }
322
+
323
+ /** What a tool declared that decides how its answer is drawn: one that is not
324
+ * `readOnly` wrote, so its answer is drawn as its entities now stand
325
+ * ({@link standing}); a `page` tool's lone entity is drawn whole. */
326
+ export type Drawn = { readOnly?: boolean; page?: boolean }
327
+
328
+ // The view each entity of an answer is drawn as: a `Page` for the lone entity
329
+ // of an answer drawn as a page, a `Tile` otherwise.
330
+ export let viewOf = (them: Bundle[], page: boolean): 'Page' | 'Tile' =>
331
+ page && them.length == 1 ? 'Page' : 'Tile'
332
+
333
+ // The relation an edge states: the component beside `edge` that the
334
+ // vocabulary declares one (@yaks/edge).
335
+ let relationOf = (vocab: Vocab, b: Bundle): string | undefined =>
336
+ Object.keys(b).find((c) => vocab.comp(c)?.keywords?.edge != null)
337
+
338
+ // Every reference one bundle makes, as `[comp, prop, eid]`.
339
+ let refsOf = (vocab: Vocab, b: Bundle): [string, string, string][] =>
340
+ Object.entries(b).flatMap(([comp, value]) =>
341
+ comp == 'entity' || !value || typeof value != 'object'
342
+ ? []
343
+ : Object.entries(value).flatMap(([prop, v]) =>
344
+ typeof v == 'string' && vocab.prop(comp, prop)?.category == 'ref'
345
+ ? [[comp, prop, v] as [string, string, string]]
346
+ : []
347
+ )
348
+ )
349
+
350
+ /** The entities an answer's references point at that the answer does not
351
+ * carry itself: what a printed reference needs looked up to read as `P-19`
352
+ * rather than as its handle. `page` is the eid of the entity drawn whole, if
353
+ * one is; every other is drawn as its `Tile`. */
354
+ export let referenced = (
355
+ vocab: Vocab,
356
+ answer: Bundle[],
357
+ views?: Held,
358
+ page?: string,
359
+ ): string[] => {
360
+ let held = new Set(answer.map((b) => b.entity.eid))
361
+ let out = new Set<string>()
362
+ for (let b of answer) {
363
+ let view = b.entity.eid == page ? 'Page' : 'Tile'
364
+ for (let [, , eid] of refsOf(vocab, b)) if (!held.has(eid)) out.add(eid)
365
+ let chosen = views && resolve(views, b, view, vocab)
366
+ for (let eid of chosen ? needsIn(chosen, b, vocab) : []) {
367
+ if (!held.has(eid)) out.add(eid)
368
+ }
369
+ }
370
+ return [...out]
371
+ }
372
+
373
+ // Links a page leaves out: bookkeeping written about an entity, not a link
374
+ // anybody made — the kernel's `references` (text that mentions it) and
375
+ // @yaks/dreaming's `recalled` (a session recalled it). `yak graph query
376
+ // '.refs=<id>&.references&*'` lists them.
377
+ let META = ['references', 'recalled']
378
+
379
+ // How many of one group a page lists before it only counts the rest, and how
380
+ // many links it asks for at all: an actor's page could name thousands.
381
+ let MOST = 10
382
+ let ENOUGH = 100
383
+
384
+ /** What a page draws beside an entity: the links it is in and the comments
385
+ * aimed at it. A tool answers only what it was asked for, so the page asks
386
+ * for these itself, in the one query {@link nearQuery} writes. */
387
+ export type Near = { links: Bundle[]; comments: Bundle[] }
388
+
389
+ /** The one query a page asks about the entity it draws: the links it is in,
390
+ * bar META, and the comments aimed at it — each only where this vocabulary
391
+ * has the component, and none at all where it has neither. */
392
+ export let nearQuery = (vocab: Vocab, eid: string): Built | null => {
393
+ let arms = [
394
+ ...vocab.comp('edge')
395
+ ? [line`.edge.from=${eid}`, line`.edge.to=${eid}`]
396
+ : [],
397
+ ...vocab.comp('comment') ? [line`.comment.target=${eid}`] : [],
398
+ ]
399
+ return arms.length
400
+ ? among([
401
+ line`(${sub(among(arms, '|'))})`,
402
+ ...META.filter((m) => vocab.comp(m)).map((m) => line`!${m}`),
403
+ line`.limit=${ENOUGH}`,
404
+ line`*`,
405
+ ], '&')
406
+ : null
407
+ }
408
+
409
+ // The answer to {@link nearQuery}, told apart: a comment aimed at the entity,
410
+ // or a link.
411
+ export let nearOf = (eid: string, found: Bundle[]): Near => ({
412
+ links: found.filter((b) => b.edge),
413
+ comments: found.filter((b) =>
414
+ (b.comment as { target?: string } | undefined)?.target == eid
415
+ ),
416
+ })
417
+
418
+ // What a page says about the entity it draws: each link as the entity at its
419
+ // other end, grouped by relation and direction — `requires` for what it
420
+ // requires, and the relation's `reversed` phrase (@yaks/edge), `required by`,
421
+ // for the entities that require it — and the comments aimed at it.
422
+ let around = (
423
+ vocab: Vocab,
424
+ it: Bundle,
425
+ near: Near,
426
+ held: Map<string, Bundle>,
427
+ ): { relations: Related[]; comments: Bundle[] } => {
428
+ let eid = it.entity.eid
429
+ let named = names(vocab)
430
+ let back = reversed(vocab)
431
+ let groups = new Map<string, Bundle[]>()
432
+ for (let b of near.links) {
433
+ let rel = relationOf(vocab, b)
434
+ let edge = b.edge as { from?: string; to?: string } | undefined
435
+ if (!rel || !edge) continue
436
+ let out = edge.from == eid
437
+ let other = (out ? edge.to : edge.from) ?? ''
438
+ let title = out ? rel : back[named[rel]] ?? `${rel} ←`
439
+ groups.set(title, [
440
+ ...groups.get(title) ?? [],
441
+ held.get(other) ?? { entity: { eid: other } },
442
+ ])
443
+ }
444
+ // Asked for at most ENOUGH, so a full answer counts only what it holds.
445
+ let more = near.links.length + near.comments.length >= ENOUGH ? '+' : ''
446
+ let relations = [...groups].map(([title, items]) => ({
447
+ title: items.length > MOST
448
+ ? `${title} (${MOST} of ${items.length}${more})`
449
+ : title,
450
+ items: items.slice(0, MOST),
451
+ }))
452
+ return { relations, comments: near.comments }
453
+ }
454
+
455
+ // An answer's entities, and apart from them its search hits: what a search
456
+ // found, or what a call's new entities were found near.
457
+ export let parted = (answer: Bundle[]): { them: Bundle[]; hits: Bundle[] } => ({
458
+ them: answer.filter((b) => !hitOf(b)),
459
+ hits: answer.filter(hitOf),
460
+ })
461
+
462
+ // What an answer draws, in whatever tree `draw` builds, as groups of lines: a
463
+ // lone entity drawn as a `page` as its `Page`, with what the page holds beside
464
+ // it, or else every entity as its `Tile`, a line apiece; then every hit, a
465
+ // line apiece. The one composition every lowering shares.
466
+ export let drawn = <Node>(
467
+ vocab: Vocab,
468
+ answer: Bundle[],
469
+ named: Bundle[],
470
+ page: Near | undefined,
471
+ draw: (b: Bundle, view: string, ctx: Shown<Node>) => Node | null,
472
+ want: Wants = {},
473
+ ): (Node | null)[][] => {
474
+ let near = page ?? nothing
475
+ let ctx = shown(
476
+ vocab,
477
+ answer,
478
+ draw,
479
+ named,
480
+ [...answer, ...near.links, ...near.comments],
481
+ want,
482
+ )
483
+ let { them, hits } = parted(answer)
484
+ let found = hits.map((b) => draw(b, 'Search.Tile', ctx))
485
+ let [lone] = them
486
+ let held = new Map([...named, ...answer].map((b) => [b.entity.eid, b]))
487
+ let drew = viewOf(them, !!page) == 'Page'
488
+ ? [draw(lone, 'Page', { ...ctx, ...around(vocab, lone, near, held) })]
489
+ : them.map((b) => draw(b, 'Tile', ctx))
490
+ return [drew, found].filter((g) => g.length)
491
+ }
492
+
493
+ // Groups of lines as one text: a line apiece within a group, a blank line
494
+ // between groups.
495
+ export let joined = (groups: string[][]): string =>
496
+ groups.map((g) => g.filter(Boolean).join('\n')).filter(Boolean).join('\n\n')
497
+
498
+ export let nothing: Near = { links: [], comments: [] }
499
+
500
+ // The token belongs to the body a page just showed, not to related entities
501
+ // or compact tiles that show no body.
502
+ export let freshness = (answer: Bundle[], page?: Near): string => {
503
+ let { them } = parted(answer)
504
+ let was = viewOf(them, !!page) == 'Page' ? them[0].$was?.doc?.body : undefined
505
+ return was === undefined ? '' : `was: ${was}`
506
+ }
507
+
508
+ /** An answer as the lines a terminal prints. `named` holds the entities its
509
+ * references point at, so each prints as its id; one not there prints as its
510
+ * handle. Each entity is a line, unless the answer is drawn as a `page`: then
511
+ * a lone entity is its `Page`, with what the page holds beside it. */
512
+ export let printed = (
513
+ views: Registry,
514
+ vocab: Vocab,
515
+ answer: Bundle[],
516
+ named: Bundle[] = [],
517
+ page?: Near,
518
+ want: Wants = {},
519
+ ): string => {
520
+ let groups = drawn<Node>(
521
+ vocab,
522
+ answer,
523
+ named,
524
+ page,
525
+ (b, v, c) => tree(views, b, v, vocab, { ...c, deferred: want.deferred }),
526
+ want,
527
+ )
528
+ return joined([
529
+ ...groups.map((g) => g.map((n) => plain(n))),
530
+ [freshness(answer, page)],
531
+ ])
532
+ }
533
+
534
+ /** Where a drawing asks for what it draws beyond the answer, from wherever
535
+ * the answer came from: `lookup` reads entities by eid, so a reference
536
+ * prints as the id a person types, and `query` answers a page's
537
+ * {@link nearQuery}. */
538
+ export type Source = {
539
+ lookup: (eids: string[]) => Bundle[] | Promise<Bundle[]>
540
+ query: (q: Line) => Bundle[] | Promise<Bundle[]>
541
+ }
542
+
543
+ // A call's words rather than a row it wrote: said under a name of its own, or
544
+ // aimed at the call (@yaks/tools result.ts says them as lines the same way).
545
+ let spoken = (b: Bundle): boolean =>
546
+ b.entity.eid[0] == '$' || b.$alias == '$said' || b.output != null
547
+
548
+ /** What a read that found nothing prints. */
549
+ export let NONE = 'none'
550
+
551
+ /** A write's answer as a person reads it: each entity it changed as that
552
+ * entity now stands, where it still does. The answer itself is the batch as
553
+ * applied — what `--json` prints — and names only what moved, so drawn as it
554
+ * is, `task update T-3 done` would be an entity with a `completed` mark and no
555
+ * title. One that no longer stands (a deletion's tombstone) is drawn as the
556
+ * answer has it, and so are the call's own words (`$said`, or `output` aimed
557
+ * at the call, under whatever eid a host minted for them): they are no row,
558
+ * and asking for one is a graph_show refused as naming nothing. */
559
+ export let standing = async (
560
+ answer: Bundle[],
561
+ from: Source,
562
+ ): Promise<Bundle[]> => {
563
+ let eids = parted(answer).them.filter((b) => !spoken(b))
564
+ .map((b) => b.entity.eid)
565
+ let now = new Map(
566
+ (eids.length ? await from.lookup(eids) : []).map((b) => [b.entity.eid, b]),
567
+ )
568
+ return answer.map((b) => hitOf(b) ? b : now.get(b.entity.eid) ?? b)
569
+ }
package/apply.js ADDED
@@ -0,0 +1,49 @@
1
+ // `yak apply`: `graph_apply` fed from a stream of bundles.
2
+ //
3
+ // One batch is applied in one transaction, and a file with fifty thousand
4
+ // bundles in it is not something anybody wants in one transaction — it is a
5
+ // bulk load. So NDJSON on stdin, one bundle per line, is sent in chunks: each
6
+ // chunk is its own transaction, applied or rejected whole, and a line that is
7
+ // not JSON is reported by its line number rather than swallowing the file.
8
+ //
9
+ // A JSON array is read the same way, because that is the shape the tool takes
10
+ // anyway and a person pasting one should not have to reformat it.
11
+ /** How many bundles are sent in one `graph_apply` call. */ export let CHUNK = 50;
12
+ /**
13
+ * The bundles in a body: one JSON value per line, or a single JSON array.
14
+ * Blank lines are ignored.
15
+ *
16
+ * ```ts
17
+ * bundlesIn('{"a":1}\n\n{"b":2}\n') // [{a: 1}, {b: 2}]
18
+ * ```
19
+ */ export let bundlesIn = (body)=>{
20
+ let text = body.trim();
21
+ if (!text) return [];
22
+ if (text.startsWith('[')) {
23
+ let whole = JSON.parse(text);
24
+ if (!Array.isArray(whole)) throw new Error('that JSON is not an array');
25
+ return whole;
26
+ }
27
+ return text.split('\n').map((line, i)=>({
28
+ line: line.trim(),
29
+ n: i + 1
30
+ })).filter(({ line })=>line).map(({ line, n })=>{
31
+ try {
32
+ return JSON.parse(line);
33
+ } catch {
34
+ throw new Error(`line ${n} is not JSON: ${line.slice(0, 80)}`);
35
+ }
36
+ });
37
+ };
38
+ /**
39
+ * A list split into fixed-size chunks.
40
+ *
41
+ * ```ts
42
+ * chunks([1, 2, 3], 2) // [[1, 2], [3]]
43
+ * ```
44
+ */ export let chunks = (xs, size = CHUNK)=>{
45
+ let out = [];
46
+ for(let i = 0; i < xs.length; i += size)out.push(xs.slice(i, i + size));
47
+ return out;
48
+ };
49
+ //# sourceMappingURL=apply.js.map
package/apply.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apply.js","sources":["./apply.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,8EAA8E;AAC9E,8EAA8E;AAC9E,2EAA2E;AAC3E,EAAE;AACF,8EAA8E;AAC9E,kEAAkE;AAElE,yDAAyD,GACzD,OAAO,IAAI,QAAQ,GAAE;AAErB;;;;;;;CAOC,GACD,OAAO,IAAI,YAAY,CAAC;EACtB,IAAI,OAAO,KAAK,IAAI;EACpB,IAAI,CAAC,MAAM,OAAO,EAAE;EACpB,IAAI,KAAK,UAAU,CAAC,MAAM;IACxB,IAAI,QAAQ,KAAK,KAAK,CAAC;IACvB,IAAI,CAAC,MAAM,OAAO,CAAC,QAAQ,MAAM,IAAI,MAAM;IAC3C,OAAO;EACT;EACA,OAAO,KAAK,KAAK,CAAC,MACf,GAAG,CAAC,CAAC,MAAM,IAAM,CAAC;MAAE,MAAM,KAAK,IAAI;MAAI,GAAG,IAAI;IAAE,CAAC,GACjD,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,GAAK,MACrB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE;IACf,IAAI;MACF,OAAO,KAAK,KAAK,CAAC;IACpB,EAAE,OAAM;MACN,MAAM,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,cAAc,EAAE,KAAK,KAAK,CAAC,GAAG,KAAK;IAC/D;EACF;AACJ,EAAC;AAED;;;;;;CAMC,GACD,OAAO,IAAI,SAAS,CAAI,IAAS,OAAO,KAAK;EAC3C,IAAI,MAAa,EAAE;EACnB,IAAK,IAAI,IAAI,GAAG,IAAI,GAAG,MAAM,EAAE,KAAK,KAAM,IAAI,IAAI,CAAC,GAAG,KAAK,CAAC,GAAG,IAAI;EACnE,OAAO;AACT,EAAC"}
package/apply.ts ADDED
@@ -0,0 +1,54 @@
1
+ // `yak apply`: `graph_apply` fed from a stream of bundles.
2
+ //
3
+ // One batch is applied in one transaction, and a file with fifty thousand
4
+ // bundles in it is not something anybody wants in one transaction — it is a
5
+ // bulk load. So NDJSON on stdin, one bundle per line, is sent in chunks: each
6
+ // chunk is its own transaction, applied or rejected whole, and a line that is
7
+ // not JSON is reported by its line number rather than swallowing the file.
8
+ //
9
+ // A JSON array is read the same way, because that is the shape the tool takes
10
+ // anyway and a person pasting one should not have to reformat it.
11
+
12
+ /** How many bundles are sent in one `graph_apply` call. */
13
+ export let CHUNK = 50
14
+
15
+ /**
16
+ * The bundles in a body: one JSON value per line, or a single JSON array.
17
+ * Blank lines are ignored.
18
+ *
19
+ * ```ts
20
+ * bundlesIn('{"a":1}\n\n{"b":2}\n') // [{a: 1}, {b: 2}]
21
+ * ```
22
+ */
23
+ export let bundlesIn = (body: string): unknown[] => {
24
+ let text = body.trim()
25
+ if (!text) return []
26
+ if (text.startsWith('[')) {
27
+ let whole = JSON.parse(text)
28
+ if (!Array.isArray(whole)) throw new Error('that JSON is not an array')
29
+ return whole
30
+ }
31
+ return text.split('\n')
32
+ .map((line, i) => ({ line: line.trim(), n: i + 1 }))
33
+ .filter(({ line }) => line)
34
+ .map(({ line, n }) => {
35
+ try {
36
+ return JSON.parse(line)
37
+ } catch {
38
+ throw new Error(`line ${n} is not JSON: ${line.slice(0, 80)}`)
39
+ }
40
+ })
41
+ }
42
+
43
+ /**
44
+ * A list split into fixed-size chunks.
45
+ *
46
+ * ```ts
47
+ * chunks([1, 2, 3], 2) // [[1, 2], [3]]
48
+ * ```
49
+ */
50
+ export let chunks = <T>(xs: T[], size = CHUNK): T[][] => {
51
+ let out: T[][] = []
52
+ for (let i = 0; i < xs.length; i += size) out.push(xs.slice(i, i + size))
53
+ return out
54
+ }