@yaks/harness 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 (230) hide show
  1. package/FRONTEND.md +170 -0
  2. package/LICENSE +202 -0
  3. package/MCPAuthPanel.js +166 -0
  4. package/MCPAuthPanel.js.map +1 -0
  5. package/MCPAuthPanel.ts +184 -0
  6. package/ModelPanel.js +124 -0
  7. package/ModelPanel.js.map +1 -0
  8. package/ModelPanel.ts +143 -0
  9. package/PersonaPanel.js +106 -0
  10. package/PersonaPanel.js.map +1 -0
  11. package/PersonaPanel.ts +110 -0
  12. package/README.md +1570 -2
  13. package/RuntimePanel.js +180 -0
  14. package/RuntimePanel.js.map +1 -0
  15. package/RuntimePanel.ts +205 -0
  16. package/STREAMING.md +101 -0
  17. package/WINDOWS.md +78 -0
  18. package/WORKER.md +221 -0
  19. package/_dist/agent.d.ts +197 -0
  20. package/_dist/agent.d.ts.map +1 -0
  21. package/_dist/cli.d.ts +10 -0
  22. package/_dist/cli.d.ts.map +1 -0
  23. package/_dist/conversations.d.ts +45 -0
  24. package/_dist/conversations.d.ts.map +1 -0
  25. package/_dist/default.d.ts +32 -0
  26. package/_dist/default.d.ts.map +1 -0
  27. package/_dist/detail.d.ts +16 -0
  28. package/_dist/detail.d.ts.map +1 -0
  29. package/_dist/diagnostics.d.ts +5 -0
  30. package/_dist/diagnostics.d.ts.map +1 -0
  31. package/_dist/effects.d.ts +35 -0
  32. package/_dist/effects.d.ts.map +1 -0
  33. package/_dist/http.d.ts +12 -0
  34. package/_dist/http.d.ts.map +1 -0
  35. package/_dist/isolated.d.ts +6 -0
  36. package/_dist/isolated.d.ts.map +1 -0
  37. package/_dist/local.d.ts +74 -0
  38. package/_dist/local.d.ts.map +1 -0
  39. package/_dist/machine.d.ts +3 -0
  40. package/_dist/machine.d.ts.map +1 -0
  41. package/_dist/mcp_auth.d.ts +8 -0
  42. package/_dist/mcp_auth.d.ts.map +1 -0
  43. package/_dist/mod.d.ts +43 -0
  44. package/_dist/mod.d.ts.map +1 -0
  45. package/_dist/model_selection.d.ts +8 -0
  46. package/_dist/model_selection.d.ts.map +1 -0
  47. package/_dist/routes.d.ts +11 -0
  48. package/_dist/routes.d.ts.map +1 -0
  49. package/_dist/runs.d.ts +7 -0
  50. package/_dist/runs.d.ts.map +1 -0
  51. package/_dist/runtime.d.ts +2 -0
  52. package/_dist/runtime.d.ts.map +1 -0
  53. package/_dist/session_machines.d.ts +18 -0
  54. package/_dist/session_machines.d.ts.map +1 -0
  55. package/_dist/skills.d.ts +11 -0
  56. package/_dist/skills.d.ts.map +1 -0
  57. package/_dist/store.d.ts +38 -0
  58. package/_dist/store.d.ts.map +1 -0
  59. package/_dist/tools.d.ts +28 -0
  60. package/_dist/tools.d.ts.map +1 -0
  61. package/_dist/view.d.ts +5 -0
  62. package/_dist/view.d.ts.map +1 -0
  63. package/_dist/vocab.d.ts +7 -0
  64. package/_dist/vocab.d.ts.map +1 -0
  65. package/_dist/worn.d.ts +52 -0
  66. package/_dist/worn.d.ts.map +1 -0
  67. package/agent.js +773 -0
  68. package/agent.js.map +1 -0
  69. package/agent.ts +959 -0
  70. package/app.js +811 -0
  71. package/app.js.map +1 -0
  72. package/app.ts +868 -0
  73. package/artifact_tools.js +186 -0
  74. package/artifact_tools.js.map +1 -0
  75. package/artifact_tools.ts +190 -0
  76. package/backend_worker.ts +286 -0
  77. package/cli.js +43 -0
  78. package/cli.js.map +1 -0
  79. package/cli.ts +48 -0
  80. package/conversations.js +526 -0
  81. package/conversations.js.map +1 -0
  82. package/conversations.ts +576 -0
  83. package/conversations_testing.ts +162 -0
  84. package/default.js +162 -0
  85. package/default.js.map +1 -0
  86. package/default.ts +168 -0
  87. package/deferred_model.ts +1 -0
  88. package/deno.json +1 -0
  89. package/detail.js +50 -0
  90. package/detail.js.map +1 -0
  91. package/detail.ts +75 -0
  92. package/diagnostics.js +132 -0
  93. package/diagnostics.js.map +1 -0
  94. package/diagnostics.ts +150 -0
  95. package/draft_vault.js +149 -0
  96. package/draft_vault.js.map +1 -0
  97. package/draft_vault.ts +176 -0
  98. package/effects.js +233 -0
  99. package/effects.js.map +1 -0
  100. package/effects.ts +250 -0
  101. package/frontend/vocab.json +376 -0
  102. package/frontend.js +511 -0
  103. package/frontend.js.map +1 -0
  104. package/frontend.ts +389 -0
  105. package/http.js +107 -0
  106. package/http.js.map +1 -0
  107. package/http.ts +105 -0
  108. package/input_text.js +91 -0
  109. package/input_text.js.map +1 -0
  110. package/input_text.ts +99 -0
  111. package/instructions.js +111 -0
  112. package/instructions.js.map +1 -0
  113. package/instructions.ts +145 -0
  114. package/isolated.js +512 -0
  115. package/isolated.js.map +1 -0
  116. package/isolated.ts +502 -0
  117. package/keyboard.js +544 -0
  118. package/keyboard.js.map +1 -0
  119. package/keyboard.ts +403 -0
  120. package/legacy_instructions.js +3 -0
  121. package/legacy_instructions.js.map +1 -0
  122. package/legacy_instructions.ts +13 -0
  123. package/local.js +181 -0
  124. package/local.js.map +1 -0
  125. package/local.ts +282 -0
  126. package/machine.js +3 -0
  127. package/machine.js.map +1 -0
  128. package/machine.ts +8 -0
  129. package/mcp.js +123 -0
  130. package/mcp.js.map +1 -0
  131. package/mcp.ts +141 -0
  132. package/mcp_auth.js +63 -0
  133. package/mcp_auth.js.map +1 -0
  134. package/mcp_auth.ts +91 -0
  135. package/mcp_authorize.js +17 -0
  136. package/mcp_authorize.js.map +1 -0
  137. package/mcp_authorize.ts +18 -0
  138. package/mcp_registry.js +178 -0
  139. package/mcp_registry.js.map +1 -0
  140. package/mcp_registry.ts +193 -0
  141. package/mod.js +43 -0
  142. package/mod.js.map +1 -0
  143. package/mod.ts +45 -0
  144. package/model_selection.js +72 -0
  145. package/model_selection.js.map +1 -0
  146. package/model_selection.ts +86 -0
  147. package/numbers.js +6 -0
  148. package/numbers.js.map +1 -0
  149. package/numbers.ts +8 -0
  150. package/package.json +106 -4
  151. package/panels.js +119 -0
  152. package/panels.js.map +1 -0
  153. package/panels.ts +259 -0
  154. package/patch.js +329 -0
  155. package/patch.js.map +1 -0
  156. package/patch.ts +325 -0
  157. package/perf.ts +65 -0
  158. package/providers.js +36 -0
  159. package/providers.js.map +1 -0
  160. package/providers.ts +53 -0
  161. package/remote.js +441 -0
  162. package/remote.js.map +1 -0
  163. package/remote.ts +446 -0
  164. package/results.js +49 -0
  165. package/results.js.map +1 -0
  166. package/results.ts +39 -0
  167. package/routes.js +64 -0
  168. package/routes.js.map +1 -0
  169. package/routes.ts +87 -0
  170. package/runs.js +478 -0
  171. package/runs.js.map +1 -0
  172. package/runs.ts +479 -0
  173. package/runtime/vocab.json +36 -0
  174. package/runtime.js +105 -0
  175. package/runtime.js.map +1 -0
  176. package/runtime.ts +112 -0
  177. package/session_machines.js +203 -0
  178. package/session_machines.js.map +1 -0
  179. package/session_machines.ts +216 -0
  180. package/sheet.js +55 -0
  181. package/sheet.js.map +1 -0
  182. package/sheet.ts +31 -0
  183. package/shutdown.js +7 -0
  184. package/shutdown.js.map +1 -0
  185. package/shutdown.ts +7 -0
  186. package/skills.js +116 -0
  187. package/skills.js.map +1 -0
  188. package/skills.ts +114 -0
  189. package/startup_bench.ts +140 -0
  190. package/status.js +82 -0
  191. package/status.js.map +1 -0
  192. package/status.ts +97 -0
  193. package/steps.baseline.json +28 -0
  194. package/steps_bench.ts +155 -0
  195. package/store.js +31 -0
  196. package/store.js.map +1 -0
  197. package/store.ts +82 -0
  198. package/streaming.js +3 -0
  199. package/streaming.js.map +1 -0
  200. package/streaming.ts +8 -0
  201. package/streaming_bench.ts +76 -0
  202. package/switch_bench.ts +97 -0
  203. package/testing.ts +177 -0
  204. package/tools.js +287 -0
  205. package/tools.js.map +1 -0
  206. package/tools.ts +313 -0
  207. package/transcript.js +149 -0
  208. package/transcript.js.map +1 -0
  209. package/transcript.ts +187 -0
  210. package/tree.js +97 -0
  211. package/tree.js.map +1 -0
  212. package/tree.ts +114 -0
  213. package/unfinished.js +186 -0
  214. package/unfinished.js.map +1 -0
  215. package/unfinished.ts +198 -0
  216. package/view.js +92 -0
  217. package/view.js.map +1 -0
  218. package/view.ts +102 -0
  219. package/vocab.js +23 -0
  220. package/vocab.js.map +1 -0
  221. package/vocab.json +494 -0
  222. package/vocab.ts +22 -0
  223. package/window_bench.ts +114 -0
  224. package/worker_bench.ts +72 -0
  225. package/workspace.js +21 -0
  226. package/workspace.js.map +1 -0
  227. package/workspace.ts +17 -0
  228. package/worn.js +194 -0
  229. package/worn.js.map +1 -0
  230. package/worn.ts +246 -0
@@ -0,0 +1,72 @@
1
+ /** Isolated worker pilot measurements; never opens the default database.
2
+ * deno run -A packages/harness/worker_bench.ts
3
+ */
4
+
5
+ import * as fs from 'jsr:@yaks/file@0.2.36/io'
6
+ import { local } from './local.ts'
7
+ import { remote } from './remote.ts'
8
+ import { at, harness } from './testing.ts'
9
+ import { type Samples, stopwatch } from 'jsr:@yaks/benchmark@0.2.36'
10
+ import { standaloneMain } from '../../bench/standalone-main.ts'
11
+
12
+ export async function measure(
13
+ _args: string[] = [],
14
+ ): Promise<Record<string, Samples>> {
15
+ const cwd = await fs.makeTempDir()
16
+ let results: Record<string, Samples> = {}
17
+ try {
18
+ for (let mode of ['inline', 'worker']) {
19
+ let took = stopwatch()
20
+ let inline = mode == 'inline'
21
+ ? await local({
22
+ h: await harness(),
23
+ cwd,
24
+ name: 'fake',
25
+ model: () =>
26
+ Promise.resolve({
27
+ id: 'r',
28
+ model: 'fake',
29
+ items: [{ kind: 'assistant', text: 'ok' }],
30
+ }),
31
+ })
32
+ : undefined
33
+ let worker = mode == 'worker'
34
+ ? await remote({ config: at(':memory:'), cwd, fake: true })
35
+ : undefined
36
+ let a = inline ?? worker!.agent
37
+ let startup = took()
38
+ let last = performance.now(), maxDelay = 0, ticks = 0
39
+ let timer = setInterval(() => {
40
+ let now = performance.now()
41
+ maxDelay = Math.max(maxDelay, now - last - 2)
42
+ last = now
43
+ ticks++
44
+ }, 2)
45
+ took = stopwatch()
46
+ let id = await a.start('x'.repeat(100_000))
47
+ for (let i = 0; i < 10; i++) {
48
+ await a.send(id, 'tool-like output ' + i + '\n' + 'x'.repeat(100_000))
49
+ }
50
+ if (inline) await inline.idle(id)
51
+ else await worker!.idle(id)
52
+ let entries = await a.transcript(id)
53
+ await new Promise((resolve) => setTimeout(resolve, 0))
54
+ let elapsed = took()
55
+ clearInterval(timer)
56
+ let counts = { ticks, entries: entries.length }
57
+ results[mode + '/startup'] = { ...startup, counts }
58
+ results[mode + '/work'] = {
59
+ ...elapsed,
60
+ counts,
61
+ details: { traffic: worker?.traffic, timerDelay: maxDelay },
62
+ }
63
+ if (worker) await worker.close()
64
+ await inline?.close()
65
+ }
66
+ return results
67
+ } finally {
68
+ await fs.remove(cwd, { recursive: true })
69
+ }
70
+ }
71
+
72
+ if (import.meta.main) await standaloneMain('harness-worker')
package/workspace.js ADDED
@@ -0,0 +1,21 @@
1
+ // A local root opening selects the checkout where its caller is standing.
2
+ // This is an explicit CLI/filesystem boundary, never a remote host fallback.
3
+ import * as fs from "@yaks/file/io";
4
+ import { checkoutAt } from "@yaks/git/host";
5
+ /** Observe the local checkout, keeping its root separate from the command
6
+ * directory. Outside Git there is no checkout default to lend. */ export let homeAt = async (g, cwd)=>{
7
+ if (![
8
+ 'workspace',
9
+ 'worktree',
10
+ 'repository'
11
+ ].every((c)=>g.vocab.comp(c))) {
12
+ return {};
13
+ }
14
+ let directory = await fs.realPath(cwd);
15
+ let checkout = await checkoutAt(g, directory);
16
+ return checkout ? {
17
+ workspace: checkout.entity.eid,
18
+ cwd: directory
19
+ } : {};
20
+ };
21
+ //# sourceMappingURL=workspace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workspace.js","sources":["./workspace.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAE7E,YAAY,6BAAkC;AAE9C,SAAS,UAAU,8BAAmC;AAEtD;gEACgE,GAChE,OAAO,IAAI,SAAS,OAAO,GAAU;EACnC,IAAI,CAAC;IAAC;IAAa;IAAY;GAAa,CAAC,KAAK,CAAC,CAAC,IAAM,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK;IAC1E,OAAO,CAAC;EACV;EACA,IAAI,YAAY,MAAM,GAAG,QAAQ,CAAC;EAClC,IAAI,WAAW,MAAM,WAAW,GAAG;EACnC,OAAO,WAAW;IAAE,WAAW,SAAS,MAAM,CAAC,GAAG;IAAE,KAAK;EAAU,IAAI,CAAC;AAC1E,EAAC"}
package/workspace.ts ADDED
@@ -0,0 +1,17 @@
1
+ // A local root opening selects the checkout where its caller is standing.
2
+ // This is an explicit CLI/filesystem boundary, never a remote host fallback.
3
+
4
+ import * as fs from '@yaks/file/io'
5
+ import type { Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
6
+ import { checkoutAt } from '@yaks/git/host'
7
+
8
+ /** Observe the local checkout, keeping its root separate from the command
9
+ * directory. Outside Git there is no checkout default to lend. */
10
+ export let homeAt = async (g: Graph, cwd: string): Promise<Comp> => {
11
+ if (!['workspace', 'worktree', 'repository'].every((c) => g.vocab.comp(c))) {
12
+ return {}
13
+ }
14
+ let directory = await fs.realPath(cwd)
15
+ let checkout = await checkoutAt(g, directory)
16
+ return checkout ? { workspace: checkout.entity.eid, cwd: directory } : {}
17
+ }
package/worn.js ADDED
@@ -0,0 +1,194 @@
1
+ // The persona a transcript wears and the topic it is opened on, said afresh
2
+ // for each ask (agent.ts `wear`): read through @yaks/persona and @yaks/topic
3
+ // alone, so a host that runs no machine (the yaks.app Worker) loads nothing
4
+ // of the instruction files beside it.
5
+ import { argsOf, who } from "@yaks/graph/call";
6
+ import { Refused } from "@yaks/graph/errors";
7
+ import { human, short } from "@yaks/id";
8
+ import { line } from "@yaks/query";
9
+ import { beside, fold, offer, PART, voice, wear, wearing, wearLayers } from "@yaks/persona";
10
+ import { compile, gather } from "@yaks/topic";
11
+ // How far up its starters what a transcript is opened as is looked for: a
12
+ // fork of a helper of a conversation is three.
13
+ let STARTERS = 16;
14
+ /** What a transcript is opened as: its own `session{persona, topic}`, else
15
+ * those of the session that started it, since a fork or a helper works as the
16
+ * one that started it does. */ export let openedAs = async (g, session)=>{
17
+ // Only what this graph declares: one made without personas, topics or
18
+ // projects opens its transcripts as nothing.
19
+ let fields = [
20
+ [
21
+ 'session',
22
+ 'persona'
23
+ ],
24
+ [
25
+ 'session',
26
+ 'topic'
27
+ ],
28
+ [
29
+ 'spawned',
30
+ 'parent'
31
+ ],
32
+ [
33
+ 'filed',
34
+ 'project'
35
+ ],
36
+ [
37
+ 'home',
38
+ 'cwd'
39
+ ]
40
+ ].filter(([comp, prop])=>g.vocab.prop(comp, prop)).map((f)=>f.join('.'));
41
+ if (!fields.length) return {};
42
+ let asked = (id)=>line`.entity.eid=${id}&.fields=${fields}`;
43
+ let at = session;
44
+ for(let i = 0; at && i < STARTERS; i++){
45
+ let [row] = await g.read(asked(at));
46
+ let s = row?.session;
47
+ if (s?.persona || s?.topic) {
48
+ let project = row.filed?.project;
49
+ let cwd = row.home?.cwd;
50
+ return {
51
+ ...s.persona ? {
52
+ persona: String(s.persona)
53
+ } : {},
54
+ ...s.topic ? {
55
+ topic: String(s.topic)
56
+ } : {},
57
+ ...project ? {
58
+ project: String(project)
59
+ } : {},
60
+ ...cwd ? {
61
+ cwd: String(cwd)
62
+ } : {}
63
+ };
64
+ }
65
+ let parent = row?.spawned?.parent;
66
+ at = parent ? String(parent) : undefined;
67
+ }
68
+ return {};
69
+ };
70
+ /** The persona a transcript wears ({@link openedAs}). */ export let personaOf = async (g, session)=>(await openedAs(g, session)).persona;
71
+ // The persona as its session opened wearing it (@yaks/persona `wearing`): one
72
+ // that is the common persona of the session's work or checkout is heard with
73
+ // each common persona above it, and any other on its base.
74
+ let worn = async (g, as, bases)=>{
75
+ if (!as.persona) return;
76
+ let common = as.project || as.cwd ? await wearing(g, as.cwd ?? '', {
77
+ work: as.project
78
+ }) : undefined;
79
+ return common?.persona.entity.eid == as.persona ? common : await wear(g.storage, g.vocab, bases)(as.persona);
80
+ };
81
+ /** What a transcript wears, as text: its persona ({@link openedAs}), its
82
+ * bases read from `how.bases` (@yaks/persona `wear`), and where `how.topic`
83
+ * asks, the tree of the topic it is opened on after it (@yaks/topic
84
+ * `compile`). Undefined where it wears no persona and is opened on no topic.
85
+ * Read on each call, so an edit to the persona, a base or the topic reaches a
86
+ * transcript on its next ask. */ export let wornBy = async (g, session, how = {})=>{
87
+ let as = await openedAs(g, session);
88
+ let [persona, topic] = await Promise.all([
89
+ worn(g, as, how.bases),
90
+ how.topic && as.topic ? gather(g)(as.topic) : undefined
91
+ ]);
92
+ return [
93
+ persona && voice(g.vocab)(persona).trimEnd(),
94
+ topic && compile(g.vocab)(topic)
95
+ ].filter(Boolean).join(PART) || undefined;
96
+ };
97
+ /** The shared base and what this session wears over it, in prompt order.
98
+ * Only the configured base can start the shared prefix. */ export let wornLayers = async (g, session, how, shared)=>{
99
+ let { persona } = await openedAs(g, session);
100
+ if (!persona) return {};
101
+ let layers = await wearLayers(g.storage, g.vocab, how.bases)(persona);
102
+ let render = voice(g.vocab);
103
+ let [head, ...tail] = layers;
104
+ if (!head) return {};
105
+ if (head.persona.entity.eid != shared) {
106
+ return {
107
+ local: render(fold(head, ...tail)).trimEnd()
108
+ };
109
+ }
110
+ let [own, ...more] = tail;
111
+ return {
112
+ shared: render(head).trimEnd(),
113
+ local: own ? render(beside(fold(own, ...more), head)).trimEnd() : undefined
114
+ };
115
+ };
116
+ /** The names of the tools a transcript is offered by the persona it wears
117
+ * ({@link openedAs}, @yaks/persona `offer`): the registered tools it contains,
118
+ * itself or through a persona it includes or is said with. Undefined where it
119
+ * wears none or its persona contains none, which leaves the runner's own set.
120
+ * Read on each call, so a tool the persona gains reaches a transcript on its
121
+ * next ask. */ export let offeredTo = async (g, session, how = {})=>offer(await worn(g, await openedAs(g, session), how.bases));
122
+ // What a Read is called by: its id as the persona's text draws it, its short
123
+ // handle, its eid, or its title.
124
+ let callsOf = (g, b)=>[
125
+ human(g.vocab)(b),
126
+ short(b.entity.eid),
127
+ b.entity.eid,
128
+ String(b.doc?.title ?? '')
129
+ ].map((s)=>s.trim().toLowerCase()).filter(Boolean);
130
+ /** The Reads a transcript's persona names ({@link openedAs}): the documents
131
+ * it, or a base under it, links by `reads`, each read where it is kept. */ export let readsOf = async (g, session, how = {})=>(await worn(g, await openedAs(g, session), how.bases))?.names ?? [];
132
+ /** The Read among `names` that `asked` names: by its id or title, or,
133
+ * `loosely`, by its id and more words or the start of its title. */ export let readAt = (g, names, asked, loosely = true)=>{
134
+ let said = asked.trim().toLowerCase();
135
+ let title = (b)=>callsOf(g, b).at(-1) ?? '';
136
+ return names.find((b)=>callsOf(g, b).includes(said)) ?? (loosely ? names.find((b)=>callsOf(g, b).some((c)=>said.startsWith(c + ' '))) ?? names.find((b)=>said.length > 3 && title(b).startsWith(said)) : undefined);
137
+ };
138
+ /** A Read as one text: its id and title as a heading, then its body. */ export let readText = (g, b)=>{
139
+ let doc = b.doc ?? b.content ?? {};
140
+ return `# ${human(g.vocab)(b)} ${doc.title ?? ''}`.trim() + '\n\n' + String(doc.body ?? '');
141
+ };
142
+ /**
143
+ * The tool that opens one of the Reads a transcript's persona names (its
144
+ * `reads` documents, @yaks/persona `voice`'s `## Reads`) in full, by the id
145
+ * the text gives it or its title, read where it is kept: the transcript's own
146
+ * graph or `how.bases`. Only the persona's Reads open, so it reaches nothing
147
+ * else of where the bases are kept.
148
+ */ export let readOpen = (how = {})=>({
149
+ name: 'read_open',
150
+ title: 'Read instructions',
151
+ step: {
152
+ doing: 'Reading the instructions for this…',
153
+ done: 'Read the instructions for this'
154
+ },
155
+ description: 'Open one of the Reads your instructions list under "## Reads", in full, ' + 'by its id there (or its title). Open one before work it covers.',
156
+ inputSchema: {
157
+ type: 'object',
158
+ properties: {
159
+ read: {
160
+ type: 'string',
161
+ description: 'its id from the Reads list'
162
+ }
163
+ },
164
+ required: [
165
+ 'read'
166
+ ],
167
+ additionalProperties: false
168
+ },
169
+ readOnly: true,
170
+ run: async (call, g)=>{
171
+ let session = who(call)?.via;
172
+ if (!session) throw new Refused('read_open needs a session');
173
+ let asked = String(argsOf(call).read ?? '').trim();
174
+ let names = await readsOf(g, session, how);
175
+ let found = readAt(g, names, asked);
176
+ if (!found) {
177
+ throw new Refused(`no Read called ${asked.toLowerCase() || '(nothing)'}; your Reads are ${names.map((b)=>human(g.vocab)(b)).join(', ') || 'none'}`);
178
+ }
179
+ return [
180
+ {
181
+ entity: {
182
+ eid: '$read'
183
+ },
184
+ content: {
185
+ body: readText(g, found)
186
+ },
187
+ output: {
188
+ source: call.entity.eid
189
+ }
190
+ }
191
+ ];
192
+ }
193
+ });
194
+ //# sourceMappingURL=worn.js.map
package/worn.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worn.js","sources":["./worn.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E;AAC5E,sCAAsC;AAEtC,SAAS,MAAM,EAAE,GAAG,gCAAqC;AACzD,SAAS,OAAO,kCAAuC;AACvD,SAAS,KAAK,EAAE,KAAK,wBAA6B;AAClD,SAAqB,IAAI,2BAAgC;AACzD,SACE,MAAM,EACN,IAAI,EACJ,KAAK,EACL,IAAI,EAEJ,KAAK,EACL,IAAI,EACJ,OAAO,EACP,UAAU,6BACqB;AACjC,SAAS,OAAO,EAAE,MAAM,2BAAgC;AAExD,0EAA0E;AAC1E,+CAA+C;AAC/C,IAAI,WAAW;AAYf;;6BAE6B,GAC7B,OAAO,IAAI,WAAW,OAAO,GAAU;EACrC,sEAAsE;EACtE,6CAA6C;EAC7C,IAAI,SAAS;IACX;MAAC;MAAW;KAAU;IACtB;MAAC;MAAW;KAAQ;IACpB;MAAC;MAAW;KAAS;IACrB;MAAC;MAAS;KAAU;IACpB;MAAC;MAAQ;KAAM;GAChB,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,KAAK,GAAK,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,OAAO,GAAG,CAAC,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AAE,IAAI,KAAK,IAAI,IAAI,CAAC,IAAI,KAAK,SAC1D,OAAO,IAAI,IAAI,IAAI;AACvB,EAAC;AAED;;;;;;CAMC,GACD,OAAO,IAAI,WAAW,CAAC,MAAY,CAAC,CAAC,GAAgB,CAAC;IACpD,MAAM;IACN,OAAO;IACP,MAAM;MACJ,OAAO;MACP,MAAM;IACR;IACA,aACE,6EACA;IACF,aAAa;MACX,MAAM;MACN,YAAY;QACV,MAAM;UAAE,MAAM;UAAU,aAAa;QAA6B;MACpE;MACA,UAAU;QAAC;OAAO;MAClB,sBAAsB;IACxB;IACA,UAAU;IACV,KAAK,OAAO,MAAM;MAChB,IAAI,UAAU,IAAI,OAAO;MACzB,IAAI,CAAC,SAAS,MAAM,IAAI,QAAQ;MAChC,IAAI,QAAQ,OAAO,OAAO,MAAM,IAAI,IAAI,IAAI,IAAI;MAChD,IAAI,QAAQ,MAAM,QAAQ,GAAG,SAAS;MACtC,IAAI,QAAQ,OAAO,GAAG,OAAO;MAC7B,IAAI,CAAC,OAAO;QACV,MAAM,IAAI,QACR,CAAC,eAAe,EAAE,MAAM,WAAW,MAAM,YAAY,iBAAiB,EACpE,MAAM,GAAG,CAAC,CAAC,IAAM,MAAM,EAAE,KAAK,EAAE,IAAI,IAAI,CAAC,SAAS,QAClD;MAEN;MACA,OAAO;QAAC;UACN,QAAQ;YAAE,KAAK;UAAQ;UACvB,SAAS;YAAE,MAAM,SAAS,GAAG;UAAO;UACpC,QAAQ;YAAE,QAAQ,KAAK,MAAM,CAAC,GAAG;UAAC;QACpC;OAAE;IACJ;EACF,CAAC,EAAC"}
package/worn.ts ADDED
@@ -0,0 +1,246 @@
1
+ // The persona a transcript wears and the topic it is opened on, said afresh
2
+ // for each ask (agent.ts `wear`): read through @yaks/persona and @yaks/topic
3
+ // alone, so a host that runs no machine (the yaks.app Worker) loads nothing
4
+ // of the instruction files beside it.
5
+ import type { Bundle, Comp, Eid, Graph, NamedTool } from 'jsr:@yaks/graph@0.2.36'
6
+ import { argsOf, who } from '@yaks/graph/call'
7
+ import { Refused } from '@yaks/graph/errors'
8
+ import { human, short } from '@yaks/id'
9
+ import { type Built, line } from '@yaks/query'
10
+ import {
11
+ beside,
12
+ fold,
13
+ offer,
14
+ PART,
15
+ type Reads,
16
+ voice,
17
+ wear,
18
+ wearing,
19
+ wearLayers,
20
+ } from '@yaks/persona'
21
+ import { compile, gather } from '@yaks/topic'
22
+
23
+ // How far up its starters what a transcript is opened as is looked for: a
24
+ // fork of a helper of a conversation is three.
25
+ let STARTERS = 16
26
+
27
+ /** What a transcript is opened as: the persona it wears and the topic it is
28
+ * opened on, with the project it is filed under and the directory it works
29
+ * in where this graph keeps those. */
30
+ export type Opened = {
31
+ persona?: Eid
32
+ topic?: Eid
33
+ project?: Eid
34
+ cwd?: string
35
+ }
36
+
37
+ /** What a transcript is opened as: its own `session{persona, topic}`, else
38
+ * those of the session that started it, since a fork or a helper works as the
39
+ * one that started it does. */
40
+ export let openedAs = async (g: Graph, session: Eid): Promise<Opened> => {
41
+ // Only what this graph declares: one made without personas, topics or
42
+ // projects opens its transcripts as nothing.
43
+ let fields = [
44
+ ['session', 'persona'],
45
+ ['session', 'topic'],
46
+ ['spawned', 'parent'],
47
+ ['filed', 'project'],
48
+ ['home', 'cwd'],
49
+ ].filter(([comp, prop]) => g.vocab.prop(comp, prop)).map((f) => f.join('.'))
50
+ if (!fields.length) return {}
51
+ let asked = (id: Eid): Built => line`.entity.eid=${id}&.fields=${fields}`
52
+ let at: Eid | undefined = session
53
+ for (let i = 0; at && i < STARTERS; i++) {
54
+ let [row]: Bundle[] = await g.read(asked(at))
55
+ let s = row?.session as Comp | undefined
56
+ if (s?.persona || s?.topic) {
57
+ let project = (row.filed as Comp | undefined)?.project
58
+ let cwd = (row.home as Comp | undefined)?.cwd
59
+ return {
60
+ ...s.persona ? { persona: String(s.persona) } : {},
61
+ ...s.topic ? { topic: String(s.topic) } : {},
62
+ ...project ? { project: String(project) } : {},
63
+ ...cwd ? { cwd: String(cwd) } : {},
64
+ }
65
+ }
66
+ let parent: unknown = (row?.spawned as Comp | undefined)?.parent
67
+ at = parent ? String(parent) : undefined
68
+ }
69
+ return {}
70
+ }
71
+
72
+ /** The persona a transcript wears ({@link openedAs}). */
73
+ export let personaOf = async (
74
+ g: Graph,
75
+ session: Eid,
76
+ ): Promise<Eid | undefined> => (await openedAs(g, session)).persona
77
+
78
+ /** How a transcript's persona is worn: where its bases (`persona.base`) are
79
+ * kept when this graph does not keep them, whether the tree of the topic it
80
+ * is opened on follows the persona's text, and whether the tools the persona
81
+ * contains are all the transcript is offered. */
82
+ export type Wear = { bases?: Reads; topic?: boolean; tools?: boolean }
83
+
84
+ // The persona as its session opened wearing it (@yaks/persona `wearing`): one
85
+ // that is the common persona of the session's work or checkout is heard with
86
+ // each common persona above it, and any other on its base.
87
+ let worn = async (g: Graph, as: Opened, bases?: Reads) => {
88
+ if (!as.persona) return
89
+ let common = as.project || as.cwd
90
+ ? await wearing(g, as.cwd ?? '', { work: as.project })
91
+ : undefined
92
+ return common?.persona.entity.eid == as.persona
93
+ ? common
94
+ : await wear(g.storage, g.vocab, bases)(as.persona)
95
+ }
96
+
97
+ /** What a transcript wears, as text: its persona ({@link openedAs}), its
98
+ * bases read from `how.bases` (@yaks/persona `wear`), and where `how.topic`
99
+ * asks, the tree of the topic it is opened on after it (@yaks/topic
100
+ * `compile`). Undefined where it wears no persona and is opened on no topic.
101
+ * Read on each call, so an edit to the persona, a base or the topic reaches a
102
+ * transcript on its next ask. */
103
+ export let wornBy = async (
104
+ g: Graph,
105
+ session: Eid,
106
+ how: Wear = {},
107
+ ): Promise<string | undefined> => {
108
+ let as = await openedAs(g, session)
109
+ let [persona, topic] = await Promise.all([
110
+ worn(g, as, how.bases),
111
+ how.topic && as.topic ? gather(g)(as.topic) : undefined,
112
+ ])
113
+ return [
114
+ persona && voice(g.vocab)(persona).trimEnd(),
115
+ topic && compile(g.vocab)(topic),
116
+ ].filter(Boolean).join(PART) || undefined
117
+ }
118
+
119
+ /** The shared base and what this session wears over it, in prompt order.
120
+ * Only the configured base can start the shared prefix. */
121
+ export let wornLayers = async (
122
+ g: Graph,
123
+ session: Eid,
124
+ how: Wear,
125
+ shared: Eid,
126
+ ): Promise<{ shared?: string; local?: string }> => {
127
+ let { persona } = await openedAs(g, session)
128
+ if (!persona) return {}
129
+ let layers = await wearLayers(g.storage, g.vocab, how.bases)(persona)
130
+ let render = voice(g.vocab)
131
+ let [head, ...tail] = layers
132
+ if (!head) return {}
133
+ if (head.persona.entity.eid != shared) {
134
+ return { local: render(fold(head, ...tail)).trimEnd() }
135
+ }
136
+ let [own, ...more] = tail
137
+ return {
138
+ shared: render(head).trimEnd(),
139
+ local: own ? render(beside(fold(own, ...more), head)).trimEnd() : undefined,
140
+ }
141
+ }
142
+
143
+ /** The names of the tools a transcript is offered by the persona it wears
144
+ * ({@link openedAs}, @yaks/persona `offer`): the registered tools it contains,
145
+ * itself or through a persona it includes or is said with. Undefined where it
146
+ * wears none or its persona contains none, which leaves the runner's own set.
147
+ * Read on each call, so a tool the persona gains reaches a transcript on its
148
+ * next ask. */
149
+ export let offeredTo = async (
150
+ g: Graph,
151
+ session: Eid,
152
+ how: Wear = {},
153
+ ): Promise<string[] | undefined> =>
154
+ offer(await worn(g, await openedAs(g, session), how.bases))
155
+
156
+ // What a Read is called by: its id as the persona's text draws it, its short
157
+ // handle, its eid, or its title.
158
+ let callsOf = (g: Graph, b: Bundle): string[] =>
159
+ [
160
+ human(g.vocab)(b),
161
+ short(b.entity.eid),
162
+ b.entity.eid,
163
+ String((b.doc as Comp | undefined)?.title ?? ''),
164
+ ].map((s) => s.trim().toLowerCase()).filter(Boolean)
165
+
166
+ /** The Reads a transcript's persona names ({@link openedAs}): the documents
167
+ * it, or a base under it, links by `reads`, each read where it is kept. */
168
+ export let readsOf = async (
169
+ g: Graph,
170
+ session: Eid,
171
+ how: Wear = {},
172
+ ): Promise<Bundle[]> =>
173
+ (await worn(g, await openedAs(g, session), how.bases))?.names ?? []
174
+
175
+ /** The Read among `names` that `asked` names: by its id or title, or,
176
+ * `loosely`, by its id and more words or the start of its title. */
177
+ export let readAt = (
178
+ g: Graph,
179
+ names: Bundle[],
180
+ asked: string,
181
+ loosely = true,
182
+ ): Bundle | undefined => {
183
+ let said = asked.trim().toLowerCase()
184
+ let title = (b: Bundle) => callsOf(g, b).at(-1) ?? ''
185
+ return names.find((b) => callsOf(g, b).includes(said)) ??
186
+ (loosely
187
+ ? names.find((b) =>
188
+ callsOf(g, b).some((c) => said.startsWith(c + ' '))
189
+ ) ??
190
+ names.find((b) => said.length > 3 && title(b).startsWith(said))
191
+ : undefined)
192
+ }
193
+
194
+ /** A Read as one text: its id and title as a heading, then its body. */
195
+ export let readText = (g: Graph, b: Bundle): string => {
196
+ let doc = (b.doc ?? b.content ?? {}) as Comp
197
+ return `# ${human(g.vocab)(b)} ${doc.title ?? ''}`.trim() + '\n\n' +
198
+ String(doc.body ?? '')
199
+ }
200
+
201
+ /**
202
+ * The tool that opens one of the Reads a transcript's persona names (its
203
+ * `reads` documents, @yaks/persona `voice`'s `## Reads`) in full, by the id
204
+ * the text gives it or its title, read where it is kept: the transcript's own
205
+ * graph or `how.bases`. Only the persona's Reads open, so it reaches nothing
206
+ * else of where the bases are kept.
207
+ */
208
+ export let readOpen = (how: Wear = {}): NamedTool => ({
209
+ name: 'read_open',
210
+ title: 'Read instructions',
211
+ step: {
212
+ doing: 'Reading the instructions for this…',
213
+ done: 'Read the instructions for this',
214
+ },
215
+ description:
216
+ 'Open one of the Reads your instructions list under "## Reads", in full, ' +
217
+ 'by its id there (or its title). Open one before work it covers.',
218
+ inputSchema: {
219
+ type: 'object',
220
+ properties: {
221
+ read: { type: 'string', description: 'its id from the Reads list' },
222
+ },
223
+ required: ['read'],
224
+ additionalProperties: false,
225
+ },
226
+ readOnly: true,
227
+ run: async (call, g) => {
228
+ let session = who(call)?.via
229
+ if (!session) throw new Refused('read_open needs a session')
230
+ let asked = String(argsOf(call).read ?? '').trim()
231
+ let names = await readsOf(g, session, how)
232
+ let found = readAt(g, names, asked)
233
+ if (!found) {
234
+ throw new Refused(
235
+ `no Read called ${asked.toLowerCase() || '(nothing)'}; your Reads are ${
236
+ names.map((b) => human(g.vocab)(b)).join(', ') || 'none'
237
+ }`,
238
+ )
239
+ }
240
+ return [{
241
+ entity: { eid: '$read' },
242
+ content: { body: readText(g, found) },
243
+ output: { source: call.entity.eid },
244
+ }]
245
+ },
246
+ })