@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/mcp.ts ADDED
@@ -0,0 +1,113 @@
1
+ // A local MCP client over stdio. File references expand here, before an HTTP
2
+ // request leaves the process that owns the files.
3
+ import { CallError } from '@yaks/tools/errors'
4
+ import { fileArgs, FILES, fileSchema, type ReadFile } from './files.js'
5
+ import { Refused, type Rpc } from './rpc.js'
6
+
7
+ type Object = Record<string, unknown>
8
+ type Tool = { name: string; inputSchema: Object }
9
+ type Request = { id?: string | number | null; method: string; params?: Object }
10
+
11
+ /** One local client's requests, with its own tool schemas and file reader. */
12
+ export let bridge = (
13
+ ask: Rpc,
14
+ read: ReadFile,
15
+ ): (request: Request) => Promise<Object | undefined> => {
16
+ let schemas = new Map<string, Object>()
17
+ let list = async (params?: Object): Promise<Object> => {
18
+ let out = await ask('tools/list', params)
19
+ return {
20
+ ...out,
21
+ tools: (out.tools as Tool[]).map((tool) => {
22
+ schemas.set(tool.name, tool.inputSchema)
23
+ return { ...tool, inputSchema: fileSchema(tool.inputSchema) }
24
+ }),
25
+ }
26
+ }
27
+ let call = async (params: Object) => {
28
+ let name = params.name as string
29
+ if (!schemas.has(name)) await list()
30
+ return await ask('tools/call', {
31
+ ...params,
32
+ arguments: await fileArgs(
33
+ (params.arguments ?? {}) as Object,
34
+ schemas.get(name) ?? {},
35
+ read,
36
+ ),
37
+ })
38
+ }
39
+ return async ({ id, method, params }) => {
40
+ if (id === undefined) return
41
+ try {
42
+ let result = method == 'tools/list'
43
+ ? await list(params)
44
+ : method == 'tools/call'
45
+ ? await call(params ?? {})
46
+ : await ask(method, params)
47
+ if (method == 'initialize') {
48
+ result = {
49
+ ...result,
50
+ instructions: [result.instructions, FILES].filter(Boolean).join(
51
+ '\n\n',
52
+ ),
53
+ }
54
+ }
55
+ return { jsonrpc: '2.0', id, result }
56
+ } catch (error) {
57
+ let message = error instanceof Error ? error.message : String(error)
58
+ return error instanceof CallError && method == 'tools/call'
59
+ ? {
60
+ jsonrpc: '2.0',
61
+ id,
62
+ result: { isError: true, content: [{ type: 'text', text: message }] },
63
+ }
64
+ : {
65
+ jsonrpc: '2.0',
66
+ id,
67
+ error: {
68
+ code: error instanceof Refused ? error.code ?? -32603 : -32603,
69
+ message,
70
+ },
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ /** Newline-delimited JSON-RPC, with independent requests answered as they end. */
77
+ export let stdio = async (
78
+ input: AsyncIterable<string>,
79
+ write: (line: string) => void,
80
+ answer: ReturnType<typeof bridge>,
81
+ ): Promise<void> => {
82
+ let pending = new Set<Promise<void>>()
83
+ for await (let line of input) {
84
+ let request: Request
85
+ try {
86
+ request = JSON.parse(line)
87
+ } catch {
88
+ write(JSON.stringify({
89
+ jsonrpc: '2.0',
90
+ id: null,
91
+ error: { code: -32700, message: 'Invalid JSON' },
92
+ }))
93
+ continue
94
+ }
95
+ if (
96
+ !request || typeof request != 'object' ||
97
+ typeof request.method != 'string'
98
+ ) {
99
+ write(JSON.stringify({
100
+ jsonrpc: '2.0',
101
+ id: null,
102
+ error: { code: -32600, message: 'Invalid request' },
103
+ }))
104
+ continue
105
+ }
106
+ let work = answer(request).then((reply) => {
107
+ if (reply) write(JSON.stringify(reply))
108
+ })
109
+ pending.add(work)
110
+ void work.finally(() => pending.delete(work))
111
+ }
112
+ await Promise.all(pending)
113
+ }
package/member.js ADDED
@@ -0,0 +1,103 @@
1
+ // A host as a member of a reach (@yaks/reach): a graph this program reaches
2
+ // over its `/mcp` door rather than opens. Its reads are `graph_query`, its
3
+ // writes `graph_apply` and its words `graph_schema`, the generic tier every
4
+ // graph's door lists (@yaks/graph/tools), so a member is the same value
5
+ // whether the host is another box or yaks.app answering across its own
6
+ // stores.
7
+ //
8
+ // Every host is asked as the person it knows: its door carries the token of
9
+ // that host's own connection row (accounts.ts). The command line's own say
10
+ // about who is asking — `$YAKS_TOKEN`, `--as` — is about the one host the
11
+ // line aims at (run.ts `aimed`), so only that host's door hears it.
12
+ import * as system from "@yaks/process/system";
13
+ import { CallError } from "@yaks/tools/errors";
14
+ import { doorUrl, rpc } from "./rpc.js";
15
+ import { saidBy } from "./roster.js";
16
+ import { accountToken } from "./accounts.js";
17
+ /** One tool called on a door, its answer as the value it carries: the
18
+ * bundles, or what a reduction or a schema said. A tool's refusal throws. */ export let called = async (ask, name, args)=>{
19
+ let reply = await ask('tools/call', {
20
+ name,
21
+ arguments: args
22
+ });
23
+ if (reply.isError) throw new CallError('remote', saidBy(reply).text);
24
+ let said = reply.structuredContent;
25
+ return said && 'result' in said ? said.result : said;
26
+ };
27
+ /** A host's vocabulary in full: `graph_schema` with no arguments is the
28
+ * index of every word, and asked for each by name it answers them as
29
+ * declared. A host whose index names nothing has no words to give. */ export let schemaOf = async (ask)=>{
30
+ let index = await called(ask, 'graph_schema', {});
31
+ let names = Object.keys(index?.$defs ?? {});
32
+ if (!names.length) return null;
33
+ return await called(ask, 'graph_schema', {
34
+ component: names
35
+ });
36
+ };
37
+ // A reduction comes back as one `$said` row carrying its value (`{count}`),
38
+ // which is what a member answers for it.
39
+ let reduction = (rows)=>{
40
+ let [one] = rows;
41
+ return rows.length == 1 && one.entity?.eid == '$said' ? one.output?.value : undefined;
42
+ };
43
+ /**
44
+ * The host at `at`, asked through `ask`, as a reach member. A read is one
45
+ * `graph_query` of the line, including the gather by eid reach sends
46
+ * (`.entity.eid=a,b`): a host holding none of them answers nothing rather
47
+ * than refusing, which graph_show would. The words are asked once.
48
+ *
49
+ * ```ts ignore
50
+ * let member = hostMember('yaks.app', hostDoor('yaks.app', { aimed: 'yaks.app' }))
51
+ * await reach({ core }).read([box, member], '.book&?loan')
52
+ * ```
53
+ */ export let hostMember = (at, ask)=>{
54
+ let words;
55
+ return {
56
+ at,
57
+ read: async (line)=>{
58
+ let rows = await called(ask, 'graph_query', {
59
+ q: line
60
+ });
61
+ if (!Array.isArray(rows)) return rows;
62
+ return reduction(rows) ?? rows;
63
+ },
64
+ apply: async (bundles, check)=>await called(ask, 'graph_apply', {
65
+ bundles,
66
+ ...check ? {
67
+ check
68
+ } : {}
69
+ }),
70
+ vocab: ()=>{
71
+ words ??= schemaOf(ask).then((doc)=>doc ?? {
72
+ $defs: {}
73
+ });
74
+ // A host that could not say its words is asked again next time.
75
+ words.catch(()=>words = undefined);
76
+ return words;
77
+ }
78
+ };
79
+ };
80
+ // The environment a host the line did not aim at reads: the same, without the
81
+ // token the line gave for the host it aimed at.
82
+ let unaimed = (env)=>(name)=>name == 'YAKS_TOKEN' ? undefined : env(name);
83
+ /** The door to `host`, as the person its own connection row signs in. The
84
+ * aimed host's door also hears `$YAKS_TOKEN` and `--as`; every other host's
85
+ * hears only its row. The token is read at each call. */ export let hostDoor = (host, aim)=>{
86
+ let env = aim.env ?? ((name)=>system.env.get(name));
87
+ let mine = host == aim.aimed;
88
+ let token = aim.token ?? accountToken;
89
+ return rpc({
90
+ url: doorUrl(host),
91
+ via: aim.via,
92
+ fetch: aim.fetch,
93
+ token: ()=>token(host, aim.state, {
94
+ config: aim.config,
95
+ env: mine ? env : unaimed(env),
96
+ agent: !!aim.via,
97
+ ...mine && aim.as ? {
98
+ as: aim.as
99
+ } : {}
100
+ })
101
+ });
102
+ };
103
+ //# sourceMappingURL=member.js.map
package/member.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"member.js","sources":["./member.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,2EAA2E;AAC3E,4EAA4E;AAC5E,wEAAwE;AACxE,uEAAuE;AACvE,UAAU;AACV,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAC1E,oEAAoE;AAEpE,YAAY,wCAA6C;AAGzD,SAAS,SAAS,kCAAuC;AAEzD,SAAoB,OAAO,EAAY,GAAG,mBAAkB;AAC5D,SAAsB,MAAM,sBAAqB;AACjD,SAA8B,YAAY,wBAAuB;AAGjE;2EAC2E,GAC3E,OAAO,IAAI,SAAS,OAClB,KACA,MACA;EAEA,IAAI,QAAQ,MAAM,IAAI,cAAc;IAAE;IAAM,WAAW;EAAK;EAC5D,IAAI,MAAM,OAAO,EAAE,MAAM,IAAI,UAAU,UAAU,OAAO,OAAO,IAAI;EACnE,IAAI,OAAO,MAAM,iBAAiB;EAClC,OAAO,QAAQ,YAAY,OAAO,KAAK,MAAM,GAAG;AAClD,EAAC;AAED;;oEAEoE,GACpE,OAAO,IAAI,WAAW,OAAO;EAC3B,IAAI,QAAQ,MAAM,OAAO,KAAK,gBAAgB,CAAC;EAC/C,IAAI,QAAQ,OAAO,IAAI,CAAC,OAAO,SAAS,CAAC;EACzC,IAAI,CAAC,MAAM,MAAM,EAAE,OAAO;EAC1B,OAAO,MAAM,OAAO,KAAK,gBAAgB;IAAE,WAAW;EAAM;AAG9D,EAAC;AAED,4EAA4E;AAC5E,yCAAyC;AACzC,IAAI,YAAY,CAAC;EACf,IAAI,CAAC,IAAI,GAAG;EACZ,OAAO,KAAK,MAAM,IAAI,KAAK,IAAI,MAAM,EAAE,OAAO,UACzC,IAAI,MAAM,EAAsC,QACjD;AACN;AAEA;;;;;;;;;;CAUC,GACD,OAAO,IAAI,aAAa,CAAC,IAAY;EACnC,IAAI;EACJ,OAAO;IACL;IACA,MAAM,OAAO;MACX,IAAI,OAAO,MAAM,OAAO,KAAK,eAAe;QAAE,GAAG;MAAK;MACtD,IAAI,CAAC,MAAM,OAAO,CAAC,OAAO,OAAO;MACjC,OAAO,UAAU,SAAS;IAC5B;IACA,OAAO,OAAO,SAAS,QACrB,MAAM,OAAO,KAAK,eAAe;QAC/B;QACA,GAAI,QAAQ;UAAE;QAAM,IAAI,CAAC,CAAC;MAC5B;IACF,OAAO;MACL,UAAU,SAAS,KAAK,IAAI,CAAC,CAAC,MAAQ,OAAO;UAAE,OAAO,CAAC;QAAE;MACzD,gEAAgE;MAChE,MAAM,KAAK,CAAC,IAAM,QAAQ;MAC1B,OAAO;IACT;EACF;AACF,EAAC;AAoBD,8EAA8E;AAC9E,gDAAgD;AAChD,IAAI,UAAU,CAAC,MAAkB,CAAC,OAChC,QAAQ,eAAe,YAAY,IAAI;AAEzC;;uDAEuD,GACvD,OAAO,IAAI,WAAW,CAAC,MAAc;EACnC,IAAI,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,OAAiB,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK;EAC5D,IAAI,OAAO,QAAQ,IAAI,KAAK;EAC5B,IAAI,QAAQ,IAAI,KAAK,IAAI;EACzB,OAAO,IAAI;IACT,KAAK,QAAQ;IACb,KAAK,IAAI,GAAG;IACZ,OAAO,IAAI,KAAK;IAChB,OAAO,IACL,MAAM,MAAM,IAAI,KAAK,EAAE;QACrB,QAAQ,IAAI,MAAM;QAClB,KAAK,OAAO,MAAM,QAAQ;QAC1B,OAAO,CAAC,CAAC,IAAI,GAAG;QAChB,GAAI,QAAQ,IAAI,EAAE,GAAG;UAAE,IAAI,IAAI,EAAE;QAAC,IAAI,CAAC,CAAC;MAC1C;EACJ;AACF,EAAC"}
package/member.ts ADDED
@@ -0,0 +1,133 @@
1
+ // A host as a member of a reach (@yaks/reach): a graph this program reaches
2
+ // over its `/mcp` door rather than opens. Its reads are `graph_query`, its
3
+ // writes `graph_apply` and its words `graph_schema`, the generic tier every
4
+ // graph's door lists (@yaks/graph/tools), so a member is the same value
5
+ // whether the host is another box or yaks.app answering across its own
6
+ // stores.
7
+ //
8
+ // Every host is asked as the person it knows: its door carries the token of
9
+ // that host's own connection row (accounts.ts). The command line's own say
10
+ // about who is asking — `$YAKS_TOKEN`, `--as` — is about the one host the
11
+ // line aims at (run.ts `aimed`), so only that host's door hears it.
12
+
13
+ import * as system from '@yaks/process/system'
14
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
15
+ import type { Member } from 'jsr:@yaks/reach@0.2.36'
16
+ import { CallError } from '@yaks/tools/errors'
17
+ import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
18
+ import { type Door, doorUrl, type Rpc, rpc } from './rpc.js'
19
+ import { type Result, saidBy } from './roster.js'
20
+ import { type AccountOptions, accountToken } from './accounts.js'
21
+ import type { Env } from './store.ts'
22
+
23
+ /** One tool called on a door, its answer as the value it carries: the
24
+ * bundles, or what a reduction or a schema said. A tool's refusal throws. */
25
+ export let called = async (
26
+ ask: Rpc,
27
+ name: string,
28
+ args: Record<string, unknown>,
29
+ ): Promise<unknown> => {
30
+ let reply = await ask('tools/call', { name, arguments: args }) as Result
31
+ if (reply.isError) throw new CallError('remote', saidBy(reply).text)
32
+ let said = reply.structuredContent as { result?: unknown } | undefined
33
+ return said && 'result' in said ? said.result : said
34
+ }
35
+
36
+ /** A host's vocabulary in full: `graph_schema` with no arguments is the
37
+ * index of every word, and asked for each by name it answers them as
38
+ * declared. A host whose index names nothing has no words to give. */
39
+ export let schemaOf = async (ask: Rpc): Promise<VocabDoc | null> => {
40
+ let index = await called(ask, 'graph_schema', {}) as VocabDoc | undefined
41
+ let names = Object.keys(index?.$defs ?? {})
42
+ if (!names.length) return null
43
+ return await called(ask, 'graph_schema', { component: names }) as
44
+ | VocabDoc
45
+ | null
46
+ }
47
+
48
+ // A reduction comes back as one `$said` row carrying its value (`{count}`),
49
+ // which is what a member answers for it.
50
+ let reduction = (rows: Bundle[]): unknown => {
51
+ let [one] = rows
52
+ return rows.length == 1 && one.entity?.eid == '$said'
53
+ ? (one.output as { value?: unknown } | undefined)?.value
54
+ : undefined
55
+ }
56
+
57
+ /**
58
+ * The host at `at`, asked through `ask`, as a reach member. A read is one
59
+ * `graph_query` of the line, including the gather by eid reach sends
60
+ * (`.entity.eid=a,b`): a host holding none of them answers nothing rather
61
+ * than refusing, which graph_show would. The words are asked once.
62
+ *
63
+ * ```ts ignore
64
+ * let member = hostMember('yaks.app', hostDoor('yaks.app', { aimed: 'yaks.app' }))
65
+ * await reach({ core }).read([box, member], '.book&?loan')
66
+ * ```
67
+ */
68
+ export let hostMember = (at: string, ask: Rpc): Member => {
69
+ let words: Promise<VocabDoc> | undefined
70
+ return {
71
+ at,
72
+ read: async (line) => {
73
+ let rows = await called(ask, 'graph_query', { q: line })
74
+ if (!Array.isArray(rows)) return rows
75
+ return reduction(rows) ?? rows
76
+ },
77
+ apply: async (bundles, check) =>
78
+ await called(ask, 'graph_apply', {
79
+ bundles,
80
+ ...(check ? { check } : {}),
81
+ }) as Bundle[],
82
+ vocab: () => {
83
+ words ??= schemaOf(ask).then((doc) => doc ?? { $defs: {} })
84
+ // A host that could not say its words is asked again next time.
85
+ words.catch(() => words = undefined)
86
+ return words
87
+ },
88
+ }
89
+ }
90
+
91
+ /** Where a command line stands: the host it aims at, and its own say about
92
+ * who is asking, which reaches that host alone. */
93
+ export type Aim = {
94
+ aimed: string
95
+ state?: string
96
+ config?: string
97
+ as?: string
98
+ env?: Env
99
+ via?: string
100
+ fetch?: Door['fetch']
101
+ /** The bearer for a host; the selected connection's by default. */
102
+ token?: (
103
+ host: string,
104
+ state?: string,
105
+ opts?: AccountOptions,
106
+ ) => Promise<string | null>
107
+ }
108
+
109
+ // The environment a host the line did not aim at reads: the same, without the
110
+ // token the line gave for the host it aimed at.
111
+ let unaimed = (env: Env): Env => (name) =>
112
+ name == 'YAKS_TOKEN' ? undefined : env(name)
113
+
114
+ /** The door to `host`, as the person its own connection row signs in. The
115
+ * aimed host's door also hears `$YAKS_TOKEN` and `--as`; every other host's
116
+ * hears only its row. The token is read at each call. */
117
+ export let hostDoor = (host: string, aim: Aim): Rpc => {
118
+ let env = aim.env ?? ((name: string) => system.env.get(name))
119
+ let mine = host == aim.aimed
120
+ let token = aim.token ?? accountToken
121
+ return rpc({
122
+ url: doorUrl(host),
123
+ via: aim.via,
124
+ fetch: aim.fetch,
125
+ token: () =>
126
+ token(host, aim.state, {
127
+ config: aim.config,
128
+ env: mine ? env : unaimed(env),
129
+ agent: !!aim.via,
130
+ ...(mine && aim.as ? { as: aim.as } : {}),
131
+ }),
132
+ })
133
+ }