@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.
- package/LICENSE +202 -0
- package/README.md +1059 -2
- package/_dist/account-forward.d.ts +12 -0
- package/_dist/account-forward.d.ts.map +1 -0
- package/_dist/accounts.d.ts +23 -0
- package/_dist/accounts.d.ts.map +1 -0
- package/_dist/answer.d.ts +88 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/apply.d.ts +17 -0
- package/_dist/apply.d.ts.map +1 -0
- package/_dist/args.d.ts +86 -0
- package/_dist/args.d.ts.map +1 -0
- package/_dist/commands.d.ts +15 -0
- package/_dist/commands.d.ts.map +1 -0
- package/_dist/complete.d.ts +15 -0
- package/_dist/complete.d.ts.map +1 -0
- package/_dist/config.d.ts +37 -0
- package/_dist/config.d.ts.map +1 -0
- package/_dist/dependencies.d.ts +6 -0
- package/_dist/dependencies.d.ts.map +1 -0
- package/_dist/facets.d.ts +71 -0
- package/_dist/facets.d.ts.map +1 -0
- package/_dist/grammar.d.ts +5 -0
- package/_dist/grammar.d.ts.map +1 -0
- package/_dist/host.d.ts +164 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/install.d.ts +43 -0
- package/_dist/install.d.ts.map +1 -0
- package/_dist/main.d.ts +16 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/memo.d.ts +60 -0
- package/_dist/memo.d.ts.map +1 -0
- package/_dist/mod.d.ts +46 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/page.d.ts +73 -0
- package/_dist/page.d.ts.map +1 -0
- package/_dist/platform.d.ts +27 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/reader.d.ts +10 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/release.d.ts +48 -0
- package/_dist/release.d.ts.map +1 -0
- package/_dist/remote.d.ts +13 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/reported.d.ts +11 -0
- package/_dist/reported.d.ts.map +1 -0
- package/_dist/roles.d.ts +2 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/roster.d.ts +41 -0
- package/_dist/roster.d.ts.map +1 -0
- package/_dist/rpc.d.ts +29 -0
- package/_dist/rpc.d.ts.map +1 -0
- package/_dist/run.d.ts +164 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/runtime.d.ts +4 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/searching.d.ts +7 -0
- package/_dist/searching.d.ts.map +1 -0
- package/_dist/show.d.ts +22 -0
- package/_dist/show.d.ts.map +1 -0
- package/_dist/store.d.ts +25 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/terminal.d.ts +17 -0
- package/_dist/terminal.d.ts.map +1 -0
- package/_dist/tool.d.ts +55 -0
- package/_dist/tool.d.ts.map +1 -0
- package/_dist/tty.d.ts +3 -0
- package/_dist/tty.d.ts.map +1 -0
- package/_dist/vault.d.ts +9 -0
- package/_dist/vault.d.ts.map +1 -0
- package/_dist/words.d.ts +11 -0
- package/_dist/words.d.ts.map +1 -0
- package/_dist/yak.d.ts +10 -0
- package/_dist/yak.d.ts.map +1 -0
- package/account-forward.js +40 -0
- package/account-forward.js.map +1 -0
- package/account-forward.ts +50 -0
- package/accounts.js +162 -0
- package/accounts.js.map +1 -0
- package/accounts.ts +228 -0
- package/answer.js +394 -0
- package/answer.js.map +1 -0
- package/answer.ts +569 -0
- package/apply.js +49 -0
- package/apply.js.map +1 -0
- package/apply.ts +54 -0
- package/args.js +420 -0
- package/args.js.map +1 -0
- package/args.ts +494 -0
- package/code.js +209 -0
- package/code.js.map +1 -0
- package/code.ts +227 -0
- package/commands.js +131 -0
- package/commands.js.map +1 -0
- package/commands.ts +142 -0
- package/complete.js +104 -0
- package/complete.js.map +1 -0
- package/complete.ts +152 -0
- package/config.js +125 -0
- package/config.js.map +1 -0
- package/config.ts +180 -0
- package/declarations.js +67 -0
- package/declarations.js.map +1 -0
- package/declarations.ts +84 -0
- package/deno.json +1 -0
- package/dependencies.js +126 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +141 -0
- package/derive.js +102 -0
- package/derive.js.map +1 -0
- package/derive.ts +125 -0
- package/drain.js +83 -0
- package/drain.js.map +1 -0
- package/drain.ts +115 -0
- package/duties.ts +17 -0
- package/exports.json +1486 -0
- package/external.js +373 -0
- package/external.js.map +1 -0
- package/external.ts +435 -0
- package/facets.js +214 -0
- package/facets.js.map +1 -0
- package/facets.ts +232 -0
- package/feedback.js +54 -0
- package/feedback.js.map +1 -0
- package/feedback.ts +57 -0
- package/files.js +84 -0
- package/files.js.map +1 -0
- package/files.ts +112 -0
- package/get_starts.js +31 -0
- package/get_starts.js.map +1 -0
- package/get_starts.ts +34 -0
- package/git-credential.js +79 -0
- package/git-credential.js.map +1 -0
- package/git-credential.ts +77 -0
- package/grammar.js +4 -0
- package/grammar.js.map +1 -0
- package/grammar.ts +5 -0
- package/held.js +51 -0
- package/held.js.map +1 -0
- package/held.ts +66 -0
- package/host.js +1174 -0
- package/host.js.map +1 -0
- package/host.ts +1343 -0
- package/init.js +138 -0
- package/init.js.map +1 -0
- package/init.ts +139 -0
- package/install.js +216 -0
- package/install.js.map +1 -0
- package/install.ts +223 -0
- package/lifetime.js +56 -0
- package/lifetime.js.map +1 -0
- package/lifetime.ts +66 -0
- package/local-page.ts +114 -0
- package/local.js +417 -0
- package/local.js.map +1 -0
- package/local.ts +537 -0
- package/lockfile.js +20 -0
- package/lockfile.js.map +1 -0
- package/lockfile.ts +32 -0
- package/machines.js +33 -0
- package/machines.js.map +1 -0
- package/machines.ts +51 -0
- package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
- package/main.js +534 -0
- package/main.js.map +1 -0
- package/main.ts +547 -0
- package/mcp.js +108 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +113 -0
- package/member.js +103 -0
- package/member.js.map +1 -0
- package/member.ts +133 -0
- package/memo.js +503 -0
- package/memo.js.map +1 -0
- package/memo.ts +634 -0
- package/mod.js +46 -0
- package/mod.js.map +1 -0
- package/mod.ts +99 -0
- package/operations.js +53 -0
- package/operations.js.map +1 -0
- package/operations.ts +68 -0
- package/package.json +115 -4
- package/page.js +211 -0
- package/page.js.map +1 -0
- package/page.ts +308 -0
- package/parser.js +45 -0
- package/parser.js.map +1 -0
- package/parser.ts +77 -0
- package/people-search.js +112 -0
- package/people-search.js.map +1 -0
- package/people-search.ts +162 -0
- package/platform.js +184 -0
- package/platform.js.map +1 -0
- package/platform.ts +224 -0
- package/platform_effects.js +5 -0
- package/platform_effects.js.map +1 -0
- package/platform_effects.ts +13 -0
- package/prepare.js +40 -0
- package/prepare.js.map +1 -0
- package/prepare.ts +46 -0
- package/read_activity.js +54 -0
- package/read_activity.js.map +1 -0
- package/read_activity.ts +61 -0
- package/read_thread.js +387 -0
- package/read_thread.js.map +1 -0
- package/read_thread.ts +506 -0
- package/read_worker.ts +164 -0
- package/reader.js +48 -0
- package/reader.js.map +1 -0
- package/reader.ts +57 -0
- package/release.js +82 -0
- package/release.js.map +1 -0
- package/release.json +127 -0
- package/release.ts +98 -0
- package/remote-page.ts +70 -0
- package/remote.js +96 -0
- package/remote.js.map +1 -0
- package/remote.ts +153 -0
- package/report.js +83 -0
- package/report.js.map +1 -0
- package/report.ts +101 -0
- package/reported.js +32 -0
- package/reported.js.map +1 -0
- package/reported.ts +38 -0
- package/restart.js +349 -0
- package/restart.js.map +1 -0
- package/restart.ts +478 -0
- package/restore.js +32 -0
- package/restore.js.map +1 -0
- package/restore.ts +38 -0
- package/roles.js +32 -0
- package/roles.js.map +1 -0
- package/roles.ts +49 -0
- package/roster.js +65 -0
- package/roster.js.map +1 -0
- package/roster.ts +84 -0
- package/rpc.js +113 -0
- package/rpc.js.map +1 -0
- package/rpc.ts +159 -0
- package/run.js +533 -0
- package/run.js.map +1 -0
- package/run.ts +741 -0
- package/runtime.js +381 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +433 -0
- package/sandboxes.js +26 -0
- package/sandboxes.js.map +1 -0
- package/sandboxes.ts +30 -0
- package/searching.js +33 -0
- package/searching.js.map +1 -0
- package/searching.ts +50 -0
- package/show.js +109 -0
- package/show.js.map +1 -0
- package/show.ts +144 -0
- package/shown.js +13 -0
- package/shown.js.map +1 -0
- package/shown.ts +44 -0
- package/stall.js +90 -0
- package/stall.js.map +1 -0
- package/stall.ts +156 -0
- package/stall_profile.js +113 -0
- package/stall_profile.js.map +1 -0
- package/stall_profile.ts +142 -0
- package/stall_wait.js +183 -0
- package/stall_wait.js.map +1 -0
- package/stall_wait.ts +249 -0
- package/stall_wait_worker.ts +11 -0
- package/start.js +349 -0
- package/start.js.map +1 -0
- package/start.ts +343 -0
- package/starvation.js +147 -0
- package/starvation.js.map +1 -0
- package/starvation.ts +224 -0
- package/store.js +118 -0
- package/store.js.map +1 -0
- package/store.ts +193 -0
- package/subcommands.js +62 -0
- package/subcommands.js.map +1 -0
- package/subcommands.ts +77 -0
- package/terminal.js +93 -0
- package/terminal.js.map +1 -0
- package/terminal.ts +128 -0
- package/text.js +20 -0
- package/text.js.map +1 -0
- package/text.ts +37 -0
- package/tool.js +76 -0
- package/tool.js.map +1 -0
- package/tool.ts +157 -0
- package/trace_control.js +231 -0
- package/trace_control.js.map +1 -0
- package/trace_control.ts +233 -0
- package/traces.js +138 -0
- package/traces.js.map +1 -0
- package/traces.ts +160 -0
- package/tracker.js +22 -0
- package/tracker.js.map +1 -0
- package/tracker.ts +27 -0
- package/tty.js +82 -0
- package/tty.js.map +1 -0
- package/tty.ts +142 -0
- package/unit_failure.js +178 -0
- package/unit_failure.js.map +1 -0
- package/unit_failure.ts +191 -0
- package/upkeep_thread.js +32 -0
- package/upkeep_thread.js.map +1 -0
- package/upkeep_thread.ts +35 -0
- package/upkeep_worker.ts +27 -0
- package/vault.js +186 -0
- package/vault.js.map +1 -0
- package/vault.ts +220 -0
- package/vocab-ids.js +63 -0
- package/vocab-ids.js.map +1 -0
- package/vocab-ids.ts +62 -0
- package/vocab.json +35 -0
- package/warm.js +138 -0
- package/warm.js.map +1 -0
- package/warm.ts +171 -0
- package/watch_revision.js +25 -0
- package/watch_revision.js.map +1 -0
- package/watch_revision.ts +30 -0
- package/words.js +29 -0
- package/words.js.map +1 -0
- package/words.ts +41 -0
- package/workspaces.js +40 -0
- package/workspaces.js.map +1 -0
- package/workspaces.ts +58 -0
- package/yak-unit-failure.conf +6 -0
- package/yak-unit-handoff.conf +2 -0
- package/yak.js +123 -0
- package/yak.js.map +1 -0
- 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
|
+
}
|