@yaks/browse 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 (271) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +324 -2
  3. package/_dist/app.d.ts +8 -0
  4. package/_dist/app.d.ts.map +1 -0
  5. package/_dist/cli.d.ts +16 -0
  6. package/_dist/cli.d.ts.map +1 -0
  7. package/_dist/hosting.d.ts +24 -0
  8. package/_dist/hosting.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +7 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/types.d.ts +5 -0
  12. package/_dist/types.d.ts.map +1 -0
  13. package/_dist/url.d.ts +2 -0
  14. package/_dist/url.d.ts.map +1 -0
  15. package/app.js +10 -0
  16. package/app.js.map +1 -0
  17. package/app.ts +10 -0
  18. package/browser-links.js +22 -0
  19. package/browser-links.js.map +1 -0
  20. package/browser-links.ts +28 -0
  21. package/cli.js +172 -0
  22. package/cli.js.map +1 -0
  23. package/cli.ts +168 -0
  24. package/client.js +748 -0
  25. package/client.js.map +1 -0
  26. package/client.ts +734 -0
  27. package/clipboard.js +18 -0
  28. package/clipboard.js.map +1 -0
  29. package/clipboard.ts +17 -0
  30. package/commands.js +1157 -0
  31. package/commands.js.map +1 -0
  32. package/commands.ts +1108 -0
  33. package/components/Ansi.tsx +160 -0
  34. package/components/App.tsx +446 -0
  35. package/components/Canvas.tsx +625 -0
  36. package/components/Card.tsx +276 -0
  37. package/components/Chat.tsx +205 -0
  38. package/components/Configuration.tsx +218 -0
  39. package/components/Decision.tsx +49 -0
  40. package/components/Dot.tsx +52 -0
  41. package/components/Entity.tsx +609 -0
  42. package/components/Fault.tsx +43 -0
  43. package/components/Filter.tsx +103 -0
  44. package/components/Keybindings.tsx +100 -0
  45. package/components/Keys.tsx +67 -0
  46. package/components/Markdown.tsx +90 -0
  47. package/components/Navigation.tsx +161 -0
  48. package/components/New.tsx +490 -0
  49. package/components/Peek.tsx +168 -0
  50. package/components/Prio.tsx +20 -0
  51. package/components/Run.tsx +263 -0
  52. package/components/Search.tsx +280 -0
  53. package/components/Stamp.tsx +44 -0
  54. package/components/Status.tsx +514 -0
  55. package/components/Sync.tsx +173 -0
  56. package/components/Tile.tsx +50 -0
  57. package/components/answered.js +23 -0
  58. package/components/answered.js.map +1 -0
  59. package/components/answered.ts +25 -0
  60. package/components/comp.js +11 -0
  61. package/components/comp.js.map +1 -0
  62. package/components/comp.ts +10 -0
  63. package/components/drag.js +173 -0
  64. package/components/drag.js.map +1 -0
  65. package/components/drag.ts +198 -0
  66. package/components/fields.tsx +90 -0
  67. package/components/fixtures/README.md +10 -0
  68. package/components/fixtures/archetypes.json +3646 -0
  69. package/components/hits.js +63 -0
  70. package/components/hits.js.map +1 -0
  71. package/components/hits.ts +84 -0
  72. package/components/icons.tsx +177 -0
  73. package/components/inspect.tsx +331 -0
  74. package/components/mount.ts +79 -0
  75. package/components/nav.tsx +479 -0
  76. package/components/offers.js +26 -0
  77. package/components/offers.js.map +1 -0
  78. package/components/offers.ts +54 -0
  79. package/components/page.js +21 -0
  80. package/components/page.js.map +1 -0
  81. package/components/page.ts +18 -0
  82. package/components/read.js +5 -0
  83. package/components/read.js.map +1 -0
  84. package/components/read.ts +7 -0
  85. package/components/registry.js +285 -0
  86. package/components/registry.js.map +1 -0
  87. package/components/registry.ts +317 -0
  88. package/components/session_status.tsx +115 -0
  89. package/components/subscriptions.js +149 -0
  90. package/components/subscriptions.js.map +1 -0
  91. package/components/subscriptions.ts +161 -0
  92. package/components/title.tsx +21 -0
  93. package/components/useQuery.js +166 -0
  94. package/components/useQuery.js.map +1 -0
  95. package/components/useQuery.ts +211 -0
  96. package/components/views/Board.tsx +450 -0
  97. package/components/views/BoardTile.tsx +65 -0
  98. package/components/views/Dashboard.tsx +148 -0
  99. package/components/views/Debug.tsx +445 -0
  100. package/components/views/Dependency.tsx +51 -0
  101. package/components/views/Entry.tsx +500 -0
  102. package/components/views/Gist.tsx +28 -0
  103. package/components/views/Inline.tsx +54 -0
  104. package/components/views/Json.tsx +19 -0
  105. package/components/views/Layout.tsx +211 -0
  106. package/components/views/List.tsx +238 -0
  107. package/components/views/Md.tsx +43 -0
  108. package/components/views/Media.tsx +48 -0
  109. package/components/views/Memory.tsx +32 -0
  110. package/components/views/Relate.tsx +205 -0
  111. package/components/views/Search.tsx +42 -0
  112. package/components/views/Session.tsx +850 -0
  113. package/components/views/SessionManage.tsx +127 -0
  114. package/components/views/Show.tsx +758 -0
  115. package/components/views/Shown.tsx +72 -0
  116. package/components/views/Split.tsx +58 -0
  117. package/components/views/TaskTile.tsx +37 -0
  118. package/components/views/Title.tsx +105 -0
  119. package/components/views/Wake.tsx +77 -0
  120. package/components/views/Web.tsx +49 -0
  121. package/components/wells.js +30 -0
  122. package/components/wells.js.map +1 -0
  123. package/components/wells.ts +33 -0
  124. package/configuration.js +38 -0
  125. package/configuration.js.map +1 -0
  126. package/configuration.ts +41 -0
  127. package/decision_sync.js +88 -0
  128. package/decision_sync.js.map +1 -0
  129. package/decision_sync.ts +93 -0
  130. package/deno.json +1 -0
  131. package/domain-host.tsx +108 -0
  132. package/edge.js +193 -0
  133. package/edge.js.map +1 -0
  134. package/edge.ts +196 -0
  135. package/entry_log.js +241 -0
  136. package/entry_log.js.map +1 -0
  137. package/entry_log.ts +214 -0
  138. package/failure.js +22 -0
  139. package/failure.js.map +1 -0
  140. package/failure.ts +34 -0
  141. package/front.json +61 -0
  142. package/highlight.js +143 -0
  143. package/highlight.js.map +1 -0
  144. package/highlight.ts +149 -0
  145. package/history.js +68 -0
  146. package/history.js.map +1 -0
  147. package/history.ts +72 -0
  148. package/host_commands.js +107 -0
  149. package/host_commands.js.map +1 -0
  150. package/host_commands.ts +38 -0
  151. package/hosting.js +53 -0
  152. package/hosting.js.map +1 -0
  153. package/hosting.ts +72 -0
  154. package/idb.js +14 -0
  155. package/idb.js.map +1 -0
  156. package/idb.ts +15 -0
  157. package/index.js +232 -0
  158. package/index.js.map +1 -0
  159. package/index.ts +252 -0
  160. package/keybindings.js +136 -0
  161. package/keybindings.js.map +1 -0
  162. package/keybindings.ts +37 -0
  163. package/kit.js +103 -0
  164. package/kit.js.map +1 -0
  165. package/kit.ts +119 -0
  166. package/layout.js +254 -0
  167. package/layout.js.map +1 -0
  168. package/layout.ts +210 -0
  169. package/legacy_outbox.js +53 -0
  170. package/legacy_outbox.js.map +1 -0
  171. package/legacy_outbox.ts +63 -0
  172. package/live.js +2883 -0
  173. package/live.js.map +1 -0
  174. package/live.ts +3334 -0
  175. package/live_archetypes.js +92 -0
  176. package/live_archetypes.js.map +1 -0
  177. package/live_archetypes.ts +113 -0
  178. package/live_client.js +145 -0
  179. package/live_client.js.map +1 -0
  180. package/live_client.ts +120 -0
  181. package/main.tsx +5 -0
  182. package/mod.js +7 -0
  183. package/mod.js.map +1 -0
  184. package/mod.ts +7 -0
  185. package/mount.tsx +50 -0
  186. package/navigation.js +120 -0
  187. package/navigation.js.map +1 -0
  188. package/navigation.ts +152 -0
  189. package/near.js +83 -0
  190. package/near.js.map +1 -0
  191. package/near.ts +83 -0
  192. package/new.js +295 -0
  193. package/new.js.map +1 -0
  194. package/new.ts +361 -0
  195. package/outbox.js +112 -0
  196. package/outbox.js.map +1 -0
  197. package/outbox.ts +129 -0
  198. package/owner.js +39 -0
  199. package/owner.js.map +1 -0
  200. package/owner.ts +37 -0
  201. package/package.json +81 -4
  202. package/paste.js +157 -0
  203. package/paste.js.map +1 -0
  204. package/paste.ts +122 -0
  205. package/props.js +303 -0
  206. package/props.js.map +1 -0
  207. package/props.ts +358 -0
  208. package/providers.js +136 -0
  209. package/providers.js.map +1 -0
  210. package/providers.ts +194 -0
  211. package/query.js +987 -0
  212. package/query.js.map +1 -0
  213. package/query.ts +1113 -0
  214. package/remote.js +37 -0
  215. package/remote.js.map +1 -0
  216. package/remote.ts +46 -0
  217. package/resolver.js +149 -0
  218. package/resolver.js.map +1 -0
  219. package/resolver.ts +213 -0
  220. package/rfc2047.js +37 -0
  221. package/rfc2047.js.map +1 -0
  222. package/rfc2047.ts +40 -0
  223. package/row_holds.js +107 -0
  224. package/row_holds.js.map +1 -0
  225. package/row_holds.ts +93 -0
  226. package/served.ts +42 -0
  227. package/session_worktree.js +7 -0
  228. package/session_worktree.js.map +1 -0
  229. package/session_worktree.ts +16 -0
  230. package/sessions.js +57 -0
  231. package/sessions.js.map +1 -0
  232. package/sessions.ts +53 -0
  233. package/sha.js +161 -0
  234. package/sha.js.map +1 -0
  235. package/sha.ts +164 -0
  236. package/styles.css +2823 -0
  237. package/subs.js +36 -0
  238. package/subs.js.map +1 -0
  239. package/subs.ts +40 -0
  240. package/terminal-route.js +13 -0
  241. package/terminal-route.js.map +1 -0
  242. package/terminal-route.ts +17 -0
  243. package/terminal.js +251 -0
  244. package/terminal.js.map +1 -0
  245. package/terminal.ts +186 -0
  246. package/time.js +142 -0
  247. package/time.js.map +1 -0
  248. package/time.ts +133 -0
  249. package/title.js +25 -0
  250. package/title.js.map +1 -0
  251. package/title.ts +50 -0
  252. package/tui/doc.ts +6 -0
  253. package/tui/testing.ts +19 -0
  254. package/twin.js +11 -0
  255. package/twin.js.map +1 -0
  256. package/twin.ts +13 -0
  257. package/types.js +456 -0
  258. package/types.js.map +1 -0
  259. package/types.ts +1370 -0
  260. package/url.js +77 -0
  261. package/url.js.map +1 -0
  262. package/url.ts +85 -0
  263. package/verb.js +82 -0
  264. package/verb.js.map +1 -0
  265. package/verb.ts +137 -0
  266. package/warmth.js +41 -0
  267. package/warmth.js.map +1 -0
  268. package/warmth.ts +54 -0
  269. package/wire.js +86 -0
  270. package/wire.js.map +1 -0
  271. package/wire.ts +63 -0
@@ -0,0 +1,93 @@
1
+ import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
2
+ import { draftEid } from '@yaks/draft'
3
+ import { line } from '@yaks/query'
4
+ import { DECISION_SETTLED } from '@yaks/task'
5
+ import { answerPlace } from '@yaks/task/form'
6
+
7
+ /** The form sends an answer alone or alongside a spend of its own draft. */
8
+ export let decisionWrite = ([answer, spend, ...rest]: Bundle[]) => {
9
+ let eid = answer?.entity.eid
10
+ let decided = answer?.decided as Comp | undefined
11
+ let choice = decided?.choice
12
+ if (!eid || typeof choice != 'string' || rest.length) return
13
+ if (Object.keys(decided!).length != 1) return
14
+ if (Object.keys(answer).some((k) => k != 'entity' && k != 'decided')) return
15
+ if (!spend) return { eid }
16
+ let draft = spend.draft as Comp | undefined
17
+ let over = (spend.typed as Comp | undefined)?.over
18
+ let place = answerPlace(eid)
19
+ if (
20
+ draft?.place != place || draft.text !== '' || typeof draft.by != 'string' ||
21
+ spend.entity.eid != draftEid(draft.by, place) ||
22
+ Object.keys(spend).some((k) => !['entity', 'draft', 'typed'].includes(k))
23
+ ) return
24
+ // Older pages trimmed the answer in the write. Its saved base can still
25
+ // carry the original whitespace.
26
+ let text = typeof over == 'string' && over.trim() == choice ? over : choice
27
+ return {
28
+ eid,
29
+ recovery: {
30
+ entity: spend.entity,
31
+ draft: { ...draft, text },
32
+ typed: { over: '' },
33
+ },
34
+ }
35
+ }
36
+
37
+ type Send = (request: Request) => Promise<Response>
38
+
39
+ /** Reconcile a stale answer with the server's decision. The original batch
40
+ * was atomic, so the draft clear never ran. Save the attempted text via the
41
+ * ordinary draft merge, under a stable key for retries after a lost receipt. */
42
+ export let deliverAnswer = async (
43
+ request: Request,
44
+ answer: NonNullable<ReturnType<typeof decisionWrite>>,
45
+ send: Send,
46
+ refused: boolean,
47
+ ): Promise<Response> => {
48
+ if (!refused) {
49
+ let response = await send(request)
50
+ if (response.status != 400) return response
51
+ let why = await response.clone().json().catch(() => null)
52
+ if (
53
+ why?.error != 'Refused' || why.message != DECISION_SETTLED ||
54
+ (why.eid != null && why.eid != answer.eid)
55
+ ) return response
56
+ }
57
+ let url = new URL(request.url)
58
+ url.pathname = url.pathname.replace(/\/apply$/, '/query')
59
+ url.searchParams.set(
60
+ 'q',
61
+ line`.entity.eid=${answer.eid} .fields=decided,cancelled,completed`,
62
+ )
63
+ let current = await send(new Request(url))
64
+ if (!current.ok) return current
65
+ let [row] = await current.json() as Bundle[]
66
+ if (!row?.decided && !row?.cancelled) {
67
+ return Response.json({
68
+ error: 'NotFound',
69
+ message: 'The settled decision could not be read.',
70
+ }, { status: 404 })
71
+ }
72
+ let saved: Bundle[] = []
73
+ if (answer.recovery) {
74
+ let headers = new Headers(request.headers)
75
+ let key = headers.get('idempotency-key')
76
+ if (key) headers.set('idempotency-key', `${key}:draft`)
77
+ let result = await send(
78
+ new Request(request.url, {
79
+ method: 'POST',
80
+ headers,
81
+ body: JSON.stringify([answer.recovery]),
82
+ }),
83
+ )
84
+ if (!result.ok || result.status == 202) return result
85
+ saved = await result.json()
86
+ }
87
+ return Response.json([{
88
+ entity: row.entity,
89
+ decided: row.decided ?? null,
90
+ cancelled: row.cancelled ?? null,
91
+ completed: row.completed ?? null,
92
+ }, ...saved])
93
+ }
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts","**/*_test.tsx","**/*_bench.ts","./testing.ts","./host_testing.ts"]},"name":"@yaks/browse","version":"0.2.36","license":"Apache-2.0","description":"The graph browsing app: domain views, editing and navigation, painted through @yaks/ui in browsers and terminals.","exports":{".":"./mod.ts","./main":"./main.tsx","./hosting":"./hosting.ts","./app":"./app.ts","./web":"./app.ts","./cli":"./cli.ts"},"imports":{"@yaks/browse":"./mod.ts","@yaks/browse/main":"./main.tsx","@yaks/browse/hosting":"./hosting.ts","@yaks/browse/app":"./app.ts","@yaks/browse/web":"./app.ts","@yaks/browse/cli":"./cli.ts"}}
@@ -0,0 +1,108 @@
1
+ /** @jsxRuntime automatic *//** @jsxImportSourceTypes npm:react@19.3.0 *//** @jsxImportSource npm:react@19.3.0 */
2
+ // Bind portable domain components to Browse's replica and page graph. Domain
3
+ // packages know these interfaces, never the app that supplies them.
4
+ import { computed, effect } from 'npm:@preact/signals-core@1.14.5'
5
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
6
+ import { configureDrafts } from 'jsr:@yaks/draft@0.2.36/input'
7
+ import { configureComments } from 'jsr:@yaks/kernel@0.2.36/comment-host'
8
+ import {
9
+ apply,
10
+ capable,
11
+ config,
12
+ ent,
13
+ findEid,
14
+ myActor,
15
+ pending,
16
+ queryEids,
17
+ row,
18
+ } from './live.ts'
19
+ import { scopedStorage } from './hosting.ts'
20
+ import { parseQuery, resolveRefs } from './query.ts'
21
+ import { type Ent, idOf, nick, vocab } from './types.ts'
22
+ import { commands, orderIn, suggest } from './commands.ts'
23
+ import { slotsOf } from './verb.ts'
24
+ import { people } from './components/hits.ts'
25
+ import { usePage } from './components/page.ts'
26
+ import { useModel, useRepoUrl } from './components/subscriptions.ts'
27
+ import { useQuery, useQueryResult } from './components/useQuery.ts'
28
+ import { linkProps } from './components/nav.tsx'
29
+ import { Markdown } from './components/Markdown.tsx'
30
+ import { ago } from './components/Stamp.tsx'
31
+ import { pretty } from './time.ts'
32
+ import { renderView } from './components/registry.ts'
33
+
34
+ export let domainBundle = (e: Ent): Bundle => {
35
+ let {
36
+ eid,
37
+ num,
38
+ entity: _entity,
39
+ kind: _kind,
40
+ refs: _refs,
41
+ kids: _kids,
42
+ ...comps
43
+ } = e
44
+ return { entity: { eid, num }, ...comps } as Bundle
45
+ }
46
+ let entity = (b: Bundle) => ent(b.entity.eid)
47
+ configureDrafts({
48
+ by: () => capable('draft') ? myActor() : undefined,
49
+ people: () => people(2),
50
+ stash: () => {
51
+ if (!config.host) return undefined
52
+ try {
53
+ return scopedStorage(globalThis.localStorage)
54
+ } catch {
55
+ return undefined
56
+ }
57
+ },
58
+ client: {
59
+ mutate: apply,
60
+ // What the mounted app holds (useDrafts), read: a watch opens nothing.
61
+ watch: (query) => {
62
+ let preds = resolveRefs(parseQuery(query), findEid)
63
+ let held = computed(() =>
64
+ queryEids(preds).value.flatMap((eid): Bundle[] => {
65
+ let draft = row(eid).value?.draft
66
+ return draft ? [{ entity: { eid }, draft }] : []
67
+ })
68
+ )
69
+ return {
70
+ get value() {
71
+ return held.value
72
+ },
73
+ subscribe: (fn) => effect(() => fn(held.value)),
74
+ }
75
+ },
76
+ },
77
+ })
78
+ // The person's drafts, held by the app while it is mounted: every box that
79
+ // types one and every view that reads one (a board's open quick-create, a
80
+ // comment's composer) is inside it, and the desk's watch reads this hold.
81
+ export let useDrafts = () => {
82
+ let by = capable('draft') ? myActor() : undefined
83
+ useQueryResult(by ? `.draft.by=${by}` : '', !!by)
84
+ }
85
+
86
+ configureComments({
87
+ get: (eid) => domainBundle(ent(eid)),
88
+ id: (b) => idOf(entity(b)),
89
+ useRows: (query) => useQuery(query).map(domainBundle),
90
+ useModel: (eid) => useModel(ent(eid)).name,
91
+ useRepo: (b) => useRepoUrl(entity(b)),
92
+ usePage: (name, place) => usePage(name, place),
93
+ render: (b, view) => renderView(entity(b), view),
94
+ link: (b) => linkProps(entity(b)),
95
+ markdown: (text, repo) => <Markdown text={text} repo={repo} />,
96
+ pending: (b) => pending(entity(b)),
97
+ has: (comp) => !!vocab.comp(comp),
98
+ modelName: (model) => nick(model) ?? undefined,
99
+ when: ago,
100
+ timestamp: pretty,
101
+ command: (line) => !!orderIn(line),
102
+ hints: (line) =>
103
+ suggest(line, commands).map(([name, c]) => ({
104
+ name,
105
+ args: slotsOf(c.args),
106
+ about: c.about,
107
+ })),
108
+ })
package/edge.js ADDED
@@ -0,0 +1,193 @@
1
+ // Translate a browser relation into the edge bundle's component and eid.
2
+ import { sha } from "./sha.js";
3
+ import { edges, vocab } from "./types.js";
4
+ import { edgeEid as derived, relationIn } from "@yaks/edge";
5
+ // The browser's camera and cursor rows use derived UUIDs of their own.
6
+ let sentenceEid = (sentence)=>{
7
+ let h = sha(sentence).slice(0, 32);
8
+ let variant = (parseInt(h[16], 16) & 0x3 | 0x8).toString(16);
9
+ let s = `${h.slice(0, 12)}8${h.slice(13, 16)}${variant}${h.slice(17)}`;
10
+ return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20)}`;
11
+ };
12
+ export let edgeEid = (from, nature, to)=>derived(from, relationIn(vocab, nature), to);
13
+ // Client singletons are sentences too. Their identity must not depend on a
14
+ // cache warmed by an async subscription: a cold-cache mint patches the same
15
+ // row. JSON tuples domain-separate these from edges and from one another.
16
+ export let cameraEid = (client, canvas)=>sentenceEid(JSON.stringify([
17
+ 'camera',
18
+ client,
19
+ canvas
20
+ ]));
21
+ export let cursorEid = (client)=>sentenceEid(JSON.stringify([
22
+ 'cursor',
23
+ client
24
+ ]));
25
+ // A relation has two spellings and they are not interchangeable: the TYPE is
26
+ // what every read and every query says (`referenced`), the NATURE is the comp
27
+ // the edge entity wears (`references`, present tense for a live relationship).
28
+ // `recalled` keeps its past tense because it is the one nature that is an
29
+ // EVENT: the edge wears `recalled{at}` — the case D-23820 names, a relation
30
+ // with a time carried by the sentence rather than forced onto either end
31
+ // (T-32471).
32
+ // The tables are read off the edge types the vocabulary has taught so far
33
+ // (`edges` is replaced by learn()), and rebuilt when it teaches again.
34
+ let tables = (()=>{
35
+ let seen;
36
+ let built;
37
+ return ()=>{
38
+ if (seen != edges) {
39
+ seen = edges;
40
+ let nature = Object.fromEntries(edges.map((t)=>[
41
+ t,
42
+ t == 'referenced' ? 'references' : t
43
+ ]));
44
+ built = {
45
+ nature,
46
+ type: Object.fromEntries(Object.entries(nature).map(([t, n])=>[
47
+ n,
48
+ t
49
+ ]))
50
+ };
51
+ }
52
+ return built;
53
+ };
54
+ })();
55
+ export let natureOf = (type)=>tables().nature[type];
56
+ export let typeOf = (nature)=>tables().type[nature];
57
+ // The eid a BUNDLE derives from what it says. A `$alias` means the door
58
+ // chooses the eid; for a content-addressed entity choosing IS deriving, so an
59
+ // edge bundle — `edge{from, to}` beside its nature tag — resolves to the
60
+ // sentence's own eid, and the alias map reports it (T-33622). `ends` answers
61
+ // what an endpoint names, since a bundle may spell one as another alias, a
62
+ // nested bundle, or a human id. An incomplete sentence derives nothing and
63
+ // falls back to a minted eid, where apply() says exactly what it lacks.
64
+ //
65
+ // A blob and a commit are content-addressed too, and neither can be derived
66
+ // here: their content never enters the graph (`blob{bytes}` is a LENGTH, the
67
+ // bytes live beside the db), so those writers name the hash they already hold
68
+ // and the bundle door mints at it (client.ts `coined`).
69
+ export let saidEid = (comps, ends)=>{
70
+ let said = comps.edge;
71
+ if (!said) return undefined;
72
+ let nature = Object.keys(comps).find((n)=>comps[n] && typeOf(n));
73
+ if (!nature || said.from == null || said.to == null) return undefined;
74
+ return edgeEid(ends(said.from), nature, ends(said.to));
75
+ };
76
+ let verbOf = (type)=>{
77
+ let nature = natureOf(type);
78
+ if (!nature) throw new Error(`unknown edge type: ${type}`);
79
+ return nature;
80
+ };
81
+ // SAYING a sentence: the edge entity's two ends, and the nature tag that is its
82
+ // verb. This is the only way to write an edge — there is no edge row and no
83
+ // component that names a triple, so a writer says the entity the sentence
84
+ // derives. `ord` is PATCH-shaped: naming it sets the listing order, omitting it
85
+ // leaves the stored one alone.
86
+ export let link = (from, type, to, ord)=>{
87
+ let nature = verbOf(type);
88
+ let eid = edgeEid(from, nature, to);
89
+ return [
90
+ {
91
+ eid,
92
+ name: 'edge',
93
+ comp: ord === undefined ? {
94
+ from,
95
+ to
96
+ } : {
97
+ from,
98
+ to,
99
+ ord
100
+ }
101
+ },
102
+ {
103
+ eid,
104
+ name: nature,
105
+ // Every nature is a bare tag but `recalled`, which is an event: the edge
106
+ // carries the recall's clock (D-23820, T-32471). Now IS the recalling
107
+ // entry's birth — recall.ts mints the entry and its links in one batch.
108
+ comp: nature == 'recalled' ? {
109
+ at: new Date().toISOString()
110
+ } : {}
111
+ }
112
+ ];
113
+ };
114
+ // Unlinking is not a DEATH. The sentence is no longer said, and the same
115
+ // sentence may be said again tomorrow — so its COMPS go and the spine stays.
116
+ // An entity wearing nothing is invisible to every reader, and saying the
117
+ // sentence again fills it back in.
118
+ // An endpoint's death still reaps the whole entity through edge's own cascades:
119
+ // that sentence cannot become true again, because one of its ends is gone.
120
+ export let unlink = (from, type, to)=>{
121
+ let nature = verbOf(type);
122
+ let eid = edgeEid(from, nature, to);
123
+ return [
124
+ {
125
+ eid,
126
+ name: nature,
127
+ comp: null
128
+ },
129
+ {
130
+ eid,
131
+ name: 'edge',
132
+ comp: null
133
+ }
134
+ ];
135
+ };
136
+ export let moves = (changes, held = ()=>undefined)=>{
137
+ // One record per edge entity, folded IN ORDER: the last thing a change says
138
+ // about a sentence is what the batch says. A catch-up stream that mints an
139
+ // edge and then reaps it must read as a loss, not as the link it opened with.
140
+ let told = new Map();
141
+ let order = [];
142
+ let at = (eid)=>{
143
+ let t = told.get(eid);
144
+ if (!t) {
145
+ told.set(eid, t = {
146
+ gone: false
147
+ });
148
+ order.push(eid);
149
+ }
150
+ return t;
151
+ };
152
+ for (let { eid, name, comp } of changes){
153
+ let nature = typeOf(name);
154
+ if (name == 'edge') {
155
+ let t = at(eid);
156
+ if (comp == null) t.gone = true;
157
+ else if (comp.from != null && comp.to != null) {
158
+ t.gone = false;
159
+ t.from = String(comp.from);
160
+ t.to = String(comp.to);
161
+ if (comp.ord != null) t.ord = Number(comp.ord);
162
+ }
163
+ } else if (nature) {
164
+ let t = at(eid);
165
+ t.gone = comp == null;
166
+ if (comp != null) t.type = nature;
167
+ } else if (name == 'entity' && comp == null) at(eid).gone = true;
168
+ }
169
+ let out = [];
170
+ for (let eid of order){
171
+ // What the batch did not name, whoever HELD the sentence answers for.
172
+ let was = held(eid);
173
+ let t = told.get(eid);
174
+ let parent = t.from ?? was?.parent;
175
+ let child = t.to ?? was?.child;
176
+ let type = t.type ?? was?.type;
177
+ if (!parent || !child || !type) continue;
178
+ let ord = t.ord ?? was?.ord;
179
+ out.push({
180
+ dep: {
181
+ parent,
182
+ type,
183
+ child,
184
+ ...ord == null ? {} : {
185
+ ord
186
+ }
187
+ },
188
+ gone: t.gone
189
+ });
190
+ }
191
+ return out;
192
+ };
193
+ //# sourceMappingURL=edge.js.map
package/edge.js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C,EAAE,IAAI,GAAG;QACT,EAAE,IAAI,GAAG,OAAO,KAAK,IAAI;QACzB,EAAE,EAAE,GAAG,OAAO,KAAK,EAAE;QACrB,IAAI,KAAK,GAAG,IAAI,MAAM,EAAE,GAAG,GAAG,OAAO,KAAK,GAAG;MAC/C;IACF,OAAO,IAAI,QAAQ;MACjB,IAAI,IAAI,GAAG;MACX,EAAE,IAAI,GAAG,QAAQ;MACjB,IAAI,QAAQ,MAAM,EAAE,IAAI,GAAG;IAC7B,OAAO,IAAI,QAAQ,YAAY,QAAQ,MAAM,GAAG,KAAK,IAAI,GAAG;EAC9D;EACA,IAAI,MAAc,EAAE;EACpB,KAAK,IAAI,OAAO,MAAO;IACrB,sEAAsE;IACtE,IAAI,MAAM,KAAK;IACf,IAAI,IAAI,KAAK,GAAG,CAAC;IACjB,IAAI,SAAS,EAAE,IAAI,IAAI,KAAK;IAC5B,IAAI,QAAQ,EAAE,EAAE,IAAI,KAAK;IACzB,IAAI,OAAO,EAAE,IAAI,IAAI,KAAK;IAC1B,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM;IAChC,IAAI,MAAM,EAAE,GAAG,IAAI,KAAK;IACxB,IAAI,IAAI,CAAC;MACP,KAAK;QAAE;QAAQ;QAAM;QAAO,GAAI,OAAO,OAAO,CAAC,IAAI;UAAE;QAAI,CAAC;MAAE;MAC5D,MAAM,EAAE,IAAI;IACd;EACF;EACA,OAAO;AACT,EAAC"}
package/edge.ts ADDED
@@ -0,0 +1,196 @@
1
+ // Translate a browser relation into the edge bundle's component and eid.
2
+ import { sha } from './sha.js'
3
+ import type { Change, Dep, Edge } from './types.js'
4
+ import { edges, vocab } from './types.js'
5
+ import { edgeEid as derived, relationIn } from '@yaks/edge'
6
+
7
+ // The browser's camera and cursor rows use derived UUIDs of their own.
8
+ let sentenceEid = (sentence: string): string => {
9
+ let h = sha(sentence).slice(0, 32)
10
+ let variant = ((parseInt(h[16], 16) & 0x3) | 0x8).toString(16)
11
+ let s = `${h.slice(0, 12)}8${h.slice(13, 16)}${variant}${h.slice(17)}`
12
+ return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${
13
+ s.slice(16, 20)
14
+ }-${s.slice(20)}`
15
+ }
16
+
17
+ export let edgeEid = (from: string, nature: string, to: string): string =>
18
+ derived(from, relationIn(vocab, nature), to)
19
+
20
+ // Client singletons are sentences too. Their identity must not depend on a
21
+ // cache warmed by an async subscription: a cold-cache mint patches the same
22
+ // row. JSON tuples domain-separate these from edges and from one another.
23
+ export let cameraEid = (client: string, canvas: string): string =>
24
+ sentenceEid(JSON.stringify(['camera', client, canvas]))
25
+ export let cursorEid = (client: string): string =>
26
+ sentenceEid(JSON.stringify(['cursor', client]))
27
+
28
+ // A relation has two spellings and they are not interchangeable: the TYPE is
29
+ // what every read and every query says (`referenced`), the NATURE is the comp
30
+ // the edge entity wears (`references`, present tense for a live relationship).
31
+ // `recalled` keeps its past tense because it is the one nature that is an
32
+ // EVENT: the edge wears `recalled{at}` — the case D-23820 names, a relation
33
+ // with a time carried by the sentence rather than forced onto either end
34
+ // (T-32471).
35
+ // The tables are read off the edge types the vocabulary has taught so far
36
+ // (`edges` is replaced by learn()), and rebuilt when it teaches again.
37
+ let tables = (() => {
38
+ let seen: string[] | undefined
39
+ let built: { nature: Record<string, string>; type: Record<string, string> }
40
+ return () => {
41
+ if (seen != edges) {
42
+ seen = edges
43
+ let nature = Object.fromEntries(
44
+ edges.map((t) => [t, t == 'referenced' ? 'references' : t]),
45
+ )
46
+ built = {
47
+ nature,
48
+ type: Object.fromEntries(
49
+ Object.entries(nature).map(([t, n]) => [n, t]),
50
+ ),
51
+ }
52
+ }
53
+ return built
54
+ }
55
+ })()
56
+ export let natureOf = (type: string): string | undefined =>
57
+ tables().nature[type]
58
+ export let typeOf = (nature: string): string | undefined =>
59
+ tables().type[nature]
60
+
61
+ // The eid a BUNDLE derives from what it says. A `$alias` means the door
62
+ // chooses the eid; for a content-addressed entity choosing IS deriving, so an
63
+ // edge bundle — `edge{from, to}` beside its nature tag — resolves to the
64
+ // sentence's own eid, and the alias map reports it (T-33622). `ends` answers
65
+ // what an endpoint names, since a bundle may spell one as another alias, a
66
+ // nested bundle, or a human id. An incomplete sentence derives nothing and
67
+ // falls back to a minted eid, where apply() says exactly what it lacks.
68
+ //
69
+ // A blob and a commit are content-addressed too, and neither can be derived
70
+ // here: their content never enters the graph (`blob{bytes}` is a LENGTH, the
71
+ // bytes live beside the db), so those writers name the hash they already hold
72
+ // and the bundle door mints at it (client.ts `coined`).
73
+ export let saidEid = (
74
+ comps: Record<string, Record<string, unknown> | null>,
75
+ ends: (target: unknown) => string,
76
+ ): string | undefined => {
77
+ let said = comps.edge
78
+ if (!said) return undefined
79
+ let nature = Object.keys(comps).find((n) => comps[n] && typeOf(n))
80
+ if (!nature || said.from == null || said.to == null) return undefined
81
+ return edgeEid(ends(said.from), nature, ends(said.to))
82
+ }
83
+
84
+ let verbOf = (type: string) => {
85
+ let nature = natureOf(type)
86
+ if (!nature) throw new Error(`unknown edge type: ${type}`)
87
+ return nature
88
+ }
89
+
90
+ // SAYING a sentence: the edge entity's two ends, and the nature tag that is its
91
+ // verb. This is the only way to write an edge — there is no edge row and no
92
+ // component that names a triple, so a writer says the entity the sentence
93
+ // derives. `ord` is PATCH-shaped: naming it sets the listing order, omitting it
94
+ // leaves the stored one alone.
95
+ export let link = (
96
+ from: string,
97
+ type: Edge | string,
98
+ to: string,
99
+ ord?: number,
100
+ ): Change[] => {
101
+ let nature = verbOf(type)
102
+ let eid = edgeEid(from, nature, to)
103
+ return [
104
+ {
105
+ eid,
106
+ name: 'edge',
107
+ comp: ord === undefined ? { from, to } : { from, to, ord },
108
+ },
109
+ {
110
+ eid,
111
+ name: nature,
112
+ // Every nature is a bare tag but `recalled`, which is an event: the edge
113
+ // carries the recall's clock (D-23820, T-32471). Now IS the recalling
114
+ // entry's birth — recall.ts mints the entry and its links in one batch.
115
+ comp: nature == 'recalled' ? { at: new Date().toISOString() } : {},
116
+ },
117
+ ]
118
+ }
119
+
120
+ // Unlinking is not a DEATH. The sentence is no longer said, and the same
121
+ // sentence may be said again tomorrow — so its COMPS go and the spine stays.
122
+ // An entity wearing nothing is invisible to every reader, and saying the
123
+ // sentence again fills it back in.
124
+ // An endpoint's death still reaps the whole entity through edge's own cascades:
125
+ // that sentence cannot become true again, because one of its ends is gone.
126
+ export let unlink = (
127
+ from: string,
128
+ type: Edge | string,
129
+ to: string,
130
+ ): Change[] => {
131
+ let nature = verbOf(type)
132
+ let eid = edgeEid(from, nature, to)
133
+ return [{ eid, name: nature, comp: null }, { eid, name: 'edge', comp: null }]
134
+ }
135
+
136
+ // The sentences a BATCH says or unsays, read back out of its edge writes — the
137
+ // one reader for every consumer that used to scan the batch for edge rows
138
+ // (live caches, the subscription riders, the comms bus).
139
+ //
140
+ // A link names its whole triple: `edge{from, to}` beside the nature tag. An
141
+ // unlink names only the edge's EID — its ends have left the batch and the graph
142
+ // — so whoever HELD the sentence answers for it, which every such consumer can
143
+ // do because holding it is what makes the loss news.
144
+ export type Move = { dep: Dep; gone: boolean }
145
+ type Told = { from?: string; to?: string; type?: Edge; ord?: number }
146
+ export let moves = (
147
+ changes: Change[],
148
+ held: (eid: string) => Dep | undefined = () => undefined,
149
+ ): Move[] => {
150
+ // One record per edge entity, folded IN ORDER: the last thing a change says
151
+ // about a sentence is what the batch says. A catch-up stream that mints an
152
+ // edge and then reaps it must read as a loss, not as the link it opened with.
153
+ let told = new Map<string, Told & { gone: boolean }>()
154
+ let order: string[] = []
155
+ let at = (eid: string) => {
156
+ let t = told.get(eid)
157
+ if (!t) {
158
+ told.set(eid, t = { gone: false })
159
+ order.push(eid)
160
+ }
161
+ return t
162
+ }
163
+ for (let { eid, name, comp } of changes) {
164
+ let nature = typeOf(name)
165
+ if (name == 'edge') {
166
+ let t = at(eid)
167
+ if (comp == null) t.gone = true
168
+ else if (comp.from != null && comp.to != null) {
169
+ t.gone = false
170
+ t.from = String(comp.from)
171
+ t.to = String(comp.to)
172
+ if (comp.ord != null) t.ord = Number(comp.ord)
173
+ }
174
+ } else if (nature) {
175
+ let t = at(eid)
176
+ t.gone = comp == null
177
+ if (comp != null) t.type = nature as Edge
178
+ } else if (name == 'entity' && comp == null) at(eid).gone = true
179
+ }
180
+ let out: Move[] = []
181
+ for (let eid of order) {
182
+ // What the batch did not name, whoever HELD the sentence answers for.
183
+ let was = held(eid)
184
+ let t = told.get(eid)!
185
+ let parent = t.from ?? was?.parent
186
+ let child = t.to ?? was?.child
187
+ let type = t.type ?? was?.type
188
+ if (!parent || !child || !type) continue
189
+ let ord = t.ord ?? was?.ord
190
+ out.push({
191
+ dep: { parent, type, child, ...(ord == null ? {} : { ord }) },
192
+ gone: t.gone,
193
+ })
194
+ }
195
+ return out
196
+ }