@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.
- package/FRONTEND.md +170 -0
- package/LICENSE +202 -0
- package/MCPAuthPanel.js +166 -0
- package/MCPAuthPanel.js.map +1 -0
- package/MCPAuthPanel.ts +184 -0
- package/ModelPanel.js +124 -0
- package/ModelPanel.js.map +1 -0
- package/ModelPanel.ts +143 -0
- package/PersonaPanel.js +106 -0
- package/PersonaPanel.js.map +1 -0
- package/PersonaPanel.ts +110 -0
- package/README.md +1570 -2
- package/RuntimePanel.js +180 -0
- package/RuntimePanel.js.map +1 -0
- package/RuntimePanel.ts +205 -0
- package/STREAMING.md +101 -0
- package/WINDOWS.md +78 -0
- package/WORKER.md +221 -0
- package/_dist/agent.d.ts +197 -0
- package/_dist/agent.d.ts.map +1 -0
- package/_dist/cli.d.ts +10 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/conversations.d.ts +45 -0
- package/_dist/conversations.d.ts.map +1 -0
- package/_dist/default.d.ts +32 -0
- package/_dist/default.d.ts.map +1 -0
- package/_dist/detail.d.ts +16 -0
- package/_dist/detail.d.ts.map +1 -0
- package/_dist/diagnostics.d.ts +5 -0
- package/_dist/diagnostics.d.ts.map +1 -0
- package/_dist/effects.d.ts +35 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/http.d.ts +12 -0
- package/_dist/http.d.ts.map +1 -0
- package/_dist/isolated.d.ts +6 -0
- package/_dist/isolated.d.ts.map +1 -0
- package/_dist/local.d.ts +74 -0
- package/_dist/local.d.ts.map +1 -0
- package/_dist/machine.d.ts +3 -0
- package/_dist/machine.d.ts.map +1 -0
- package/_dist/mcp_auth.d.ts +8 -0
- package/_dist/mcp_auth.d.ts.map +1 -0
- package/_dist/mod.d.ts +43 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/model_selection.d.ts +8 -0
- package/_dist/model_selection.d.ts.map +1 -0
- package/_dist/routes.d.ts +11 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/runs.d.ts +7 -0
- package/_dist/runs.d.ts.map +1 -0
- package/_dist/runtime.d.ts +2 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/session_machines.d.ts +18 -0
- package/_dist/session_machines.d.ts.map +1 -0
- package/_dist/skills.d.ts +11 -0
- package/_dist/skills.d.ts.map +1 -0
- package/_dist/store.d.ts +38 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/tools.d.ts +28 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/view.d.ts +5 -0
- package/_dist/view.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/worn.d.ts +52 -0
- package/_dist/worn.d.ts.map +1 -0
- package/agent.js +773 -0
- package/agent.js.map +1 -0
- package/agent.ts +959 -0
- package/app.js +811 -0
- package/app.js.map +1 -0
- package/app.ts +868 -0
- package/artifact_tools.js +186 -0
- package/artifact_tools.js.map +1 -0
- package/artifact_tools.ts +190 -0
- package/backend_worker.ts +286 -0
- package/cli.js +43 -0
- package/cli.js.map +1 -0
- package/cli.ts +48 -0
- package/conversations.js +526 -0
- package/conversations.js.map +1 -0
- package/conversations.ts +576 -0
- package/conversations_testing.ts +162 -0
- package/default.js +162 -0
- package/default.js.map +1 -0
- package/default.ts +168 -0
- package/deferred_model.ts +1 -0
- package/deno.json +1 -0
- package/detail.js +50 -0
- package/detail.js.map +1 -0
- package/detail.ts +75 -0
- package/diagnostics.js +132 -0
- package/diagnostics.js.map +1 -0
- package/diagnostics.ts +150 -0
- package/draft_vault.js +149 -0
- package/draft_vault.js.map +1 -0
- package/draft_vault.ts +176 -0
- package/effects.js +233 -0
- package/effects.js.map +1 -0
- package/effects.ts +250 -0
- package/frontend/vocab.json +376 -0
- package/frontend.js +511 -0
- package/frontend.js.map +1 -0
- package/frontend.ts +389 -0
- package/http.js +107 -0
- package/http.js.map +1 -0
- package/http.ts +105 -0
- package/input_text.js +91 -0
- package/input_text.js.map +1 -0
- package/input_text.ts +99 -0
- package/instructions.js +111 -0
- package/instructions.js.map +1 -0
- package/instructions.ts +145 -0
- package/isolated.js +512 -0
- package/isolated.js.map +1 -0
- package/isolated.ts +502 -0
- package/keyboard.js +544 -0
- package/keyboard.js.map +1 -0
- package/keyboard.ts +403 -0
- package/legacy_instructions.js +3 -0
- package/legacy_instructions.js.map +1 -0
- package/legacy_instructions.ts +13 -0
- package/local.js +181 -0
- package/local.js.map +1 -0
- package/local.ts +282 -0
- package/machine.js +3 -0
- package/machine.js.map +1 -0
- package/machine.ts +8 -0
- package/mcp.js +123 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +141 -0
- package/mcp_auth.js +63 -0
- package/mcp_auth.js.map +1 -0
- package/mcp_auth.ts +91 -0
- package/mcp_authorize.js +17 -0
- package/mcp_authorize.js.map +1 -0
- package/mcp_authorize.ts +18 -0
- package/mcp_registry.js +178 -0
- package/mcp_registry.js.map +1 -0
- package/mcp_registry.ts +193 -0
- package/mod.js +43 -0
- package/mod.js.map +1 -0
- package/mod.ts +45 -0
- package/model_selection.js +72 -0
- package/model_selection.js.map +1 -0
- package/model_selection.ts +86 -0
- package/numbers.js +6 -0
- package/numbers.js.map +1 -0
- package/numbers.ts +8 -0
- package/package.json +106 -4
- package/panels.js +119 -0
- package/panels.js.map +1 -0
- package/panels.ts +259 -0
- package/patch.js +329 -0
- package/patch.js.map +1 -0
- package/patch.ts +325 -0
- package/perf.ts +65 -0
- package/providers.js +36 -0
- package/providers.js.map +1 -0
- package/providers.ts +53 -0
- package/remote.js +441 -0
- package/remote.js.map +1 -0
- package/remote.ts +446 -0
- package/results.js +49 -0
- package/results.js.map +1 -0
- package/results.ts +39 -0
- package/routes.js +64 -0
- package/routes.js.map +1 -0
- package/routes.ts +87 -0
- package/runs.js +478 -0
- package/runs.js.map +1 -0
- package/runs.ts +479 -0
- package/runtime/vocab.json +36 -0
- package/runtime.js +105 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +112 -0
- package/session_machines.js +203 -0
- package/session_machines.js.map +1 -0
- package/session_machines.ts +216 -0
- package/sheet.js +55 -0
- package/sheet.js.map +1 -0
- package/sheet.ts +31 -0
- package/shutdown.js +7 -0
- package/shutdown.js.map +1 -0
- package/shutdown.ts +7 -0
- package/skills.js +116 -0
- package/skills.js.map +1 -0
- package/skills.ts +114 -0
- package/startup_bench.ts +140 -0
- package/status.js +82 -0
- package/status.js.map +1 -0
- package/status.ts +97 -0
- package/steps.baseline.json +28 -0
- package/steps_bench.ts +155 -0
- package/store.js +31 -0
- package/store.js.map +1 -0
- package/store.ts +82 -0
- package/streaming.js +3 -0
- package/streaming.js.map +1 -0
- package/streaming.ts +8 -0
- package/streaming_bench.ts +76 -0
- package/switch_bench.ts +97 -0
- package/testing.ts +177 -0
- package/tools.js +287 -0
- package/tools.js.map +1 -0
- package/tools.ts +313 -0
- package/transcript.js +149 -0
- package/transcript.js.map +1 -0
- package/transcript.ts +187 -0
- package/tree.js +97 -0
- package/tree.js.map +1 -0
- package/tree.ts +114 -0
- package/unfinished.js +186 -0
- package/unfinished.js.map +1 -0
- package/unfinished.ts +198 -0
- package/view.js +92 -0
- package/view.js.map +1 -0
- package/view.ts +102 -0
- package/vocab.js +23 -0
- package/vocab.js.map +1 -0
- package/vocab.json +494 -0
- package/vocab.ts +22 -0
- package/window_bench.ts +114 -0
- package/worker_bench.ts +72 -0
- package/workspace.js +21 -0
- package/workspace.js.map +1 -0
- package/workspace.ts +17 -0
- package/worn.js +194 -0
- package/worn.js.map +1 -0
- package/worn.ts +246 -0
package/remote.ts
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import * as systemProcess from '@yaks/process/system'
|
|
2
|
+
import { ShuttingDown } from './shutdown.js'
|
|
3
|
+
import type { EntrySource } from './detail.ts'
|
|
4
|
+
import type {
|
|
5
|
+
TranscriptPage,
|
|
6
|
+
TranscriptPlan,
|
|
7
|
+
TranscriptWindow,
|
|
8
|
+
} from '@yaks/session'
|
|
9
|
+
import { streamingEnabled } from './streaming.js'
|
|
10
|
+
import { transient } from '@yaks/graph/read'
|
|
11
|
+
import type { ModelSelection } from './model_selection.ts'
|
|
12
|
+
/** Worker frontend. UI state remains in the frontend's private graph. */
|
|
13
|
+
import { client } from '@yaks/client'
|
|
14
|
+
import type { Frame } from 'jsr:@yaks/sync@0.2.36'
|
|
15
|
+
import { portLink } from '@yaks/sync/port'
|
|
16
|
+
import { type Clock, system } from '@yaks/fp'
|
|
17
|
+
import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
|
|
18
|
+
import { render as tree } from '@yaks/react'
|
|
19
|
+
import { render } from '@yaks/text'
|
|
20
|
+
import { sessionViews } from '@yaks/session'
|
|
21
|
+
import { type Config, words } from '@yaks/cli/host'
|
|
22
|
+
import { transcriptViews } from './transcript.js'
|
|
23
|
+
import type { UIAgent } from './panels.ts'
|
|
24
|
+
import { diagnostics } from './diagnostics.js'
|
|
25
|
+
|
|
26
|
+
/** How long a Worker being ended where it stands has to write its own ending
|
|
27
|
+
* before it is let go anyway (ms). */
|
|
28
|
+
let ENDING = 1_000
|
|
29
|
+
|
|
30
|
+
export let remote = async (
|
|
31
|
+
options: {
|
|
32
|
+
provider?: string
|
|
33
|
+
name?: string
|
|
34
|
+
/** the `yak` config naming the graph the worker composes */
|
|
35
|
+
config: Config
|
|
36
|
+
cwd?: string
|
|
37
|
+
streaming?: boolean
|
|
38
|
+
stream?: boolean
|
|
39
|
+
migrationPollMs?: number
|
|
40
|
+
/** how long a run the backend holds stands before another may take it
|
|
41
|
+
* over from a backend that died (ms) */
|
|
42
|
+
hold?: number
|
|
43
|
+
instructions?: string
|
|
44
|
+
fake?: boolean | 'stuck' | 'held' | {
|
|
45
|
+
deltas?: number
|
|
46
|
+
held?: boolean
|
|
47
|
+
}
|
|
48
|
+
/** where settings such as `HARNESS_STREAM` are read (default this
|
|
49
|
+
* process's environment); resolved here, never cloned to the worker */
|
|
50
|
+
env?: (name: string) => string | undefined
|
|
51
|
+
/** the backend Worker to serve this graph in, lent and left running
|
|
52
|
+
* (testing.ts `worker`); by default one of its own, ended with it */
|
|
53
|
+
worker?: Worker
|
|
54
|
+
/** the clock this frontend keeps its deadlines by: each request's, and a
|
|
55
|
+
* close's (default the runtime's); kept here, never cloned to the worker */
|
|
56
|
+
clock?: Clock
|
|
57
|
+
},
|
|
58
|
+
) => {
|
|
59
|
+
let { env = systemProcess.env.get, worker: lent, clock = system, ...config } =
|
|
60
|
+
options
|
|
61
|
+
let worker = lent ?? new Worker(
|
|
62
|
+
new URL('./backend_worker.ts', import.meta.url).href,
|
|
63
|
+
{ type: 'module' },
|
|
64
|
+
)
|
|
65
|
+
// Load the frontend's vocabulary while the backend imports its module graph.
|
|
66
|
+
// Neither depends on the other; doing them in sequence delays every startup.
|
|
67
|
+
let { vocab } = await words(options.config).catch((error) => {
|
|
68
|
+
if (!lent) worker.terminate()
|
|
69
|
+
throw error
|
|
70
|
+
})
|
|
71
|
+
// The backend is served on a port of its own (backend_worker.ts), so a
|
|
72
|
+
// Worker can hold one for its own frontend or one after another for a test
|
|
73
|
+
// process that lends it.
|
|
74
|
+
let { port1: port, port2 } = new MessageChannel()
|
|
75
|
+
worker.postMessage({ backend: port2 }, [port2])
|
|
76
|
+
let replica = client(vocab, [], { vault: false, retention: 256 })
|
|
77
|
+
let listeners = new Set<() => void>()
|
|
78
|
+
let members = new Map<string, string[]>()
|
|
79
|
+
// Memoize the asynchronous projection until its authoritative subscription
|
|
80
|
+
// changes. Selection changes do not change session titles or task summaries.
|
|
81
|
+
// Initial subscription data is returned to its awaiting caller. Publishing it
|
|
82
|
+
// as a new change would invalidate that same read and force a second refresh.
|
|
83
|
+
let initializing = new Set<string>()
|
|
84
|
+
let failure: Error | undefined
|
|
85
|
+
let windowRevision = 0
|
|
86
|
+
let queued = Promise.resolve()
|
|
87
|
+
let link = portLink(port, {
|
|
88
|
+
clock,
|
|
89
|
+
frame: (frame: Frame) => {
|
|
90
|
+
queued = queued.then(async () => {
|
|
91
|
+
if (frame.refused) throw new Error(frame.refused.message)
|
|
92
|
+
if (frame.id.startsWith('window:') && frame.gone?.length) {
|
|
93
|
+
windowRevision++
|
|
94
|
+
}
|
|
95
|
+
if (frame.id.startsWith('frontier:')) {
|
|
96
|
+
windowRevision++
|
|
97
|
+
if (!initializing.has(frame.id)) {
|
|
98
|
+
for (let notify of listeners) notify()
|
|
99
|
+
}
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
await replica.cache.land(frame)
|
|
103
|
+
let ids = new Set(members.get(frame.id) ?? [])
|
|
104
|
+
for (let b of frame.bundles ?? []) ids.add(b.entity.eid)
|
|
105
|
+
for (let id of frame.gone ?? []) ids.delete(id)
|
|
106
|
+
members.set(frame.id, [...ids])
|
|
107
|
+
if (!initializing.has(frame.id)) {
|
|
108
|
+
for (let notify of listeners) notify()
|
|
109
|
+
}
|
|
110
|
+
}).catch((error) => {
|
|
111
|
+
failure = error
|
|
112
|
+
diagnostics().report(error, { phase: 'worker-sync' })
|
|
113
|
+
for (let notify of listeners) notify()
|
|
114
|
+
})
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
let fatal = (event: Event) => {
|
|
118
|
+
failure = new Error(
|
|
119
|
+
(event as ErrorEvent).message || 'Worker transport failed',
|
|
120
|
+
)
|
|
121
|
+
link.close(failure)
|
|
122
|
+
diagnostics().report(failure, { phase: 'worker' })
|
|
123
|
+
for (let notify of listeners) notify()
|
|
124
|
+
}
|
|
125
|
+
worker.addEventListener('error', fatal)
|
|
126
|
+
worker.addEventListener('messageerror', fatal)
|
|
127
|
+
// Letting go of the backend: its port, and the Worker when it is this
|
|
128
|
+
// backend's own. A lent Worker runs on, and a backend let go of while it
|
|
129
|
+
// still works (`force`) finishes on it.
|
|
130
|
+
let release = () => {
|
|
131
|
+
link.close()
|
|
132
|
+
port.close()
|
|
133
|
+
worker.removeEventListener('error', fatal)
|
|
134
|
+
worker.removeEventListener('messageerror', fatal)
|
|
135
|
+
if (!lent) worker.terminate()
|
|
136
|
+
}
|
|
137
|
+
// A Worker of its own that did not drain is ended where it stands, and its
|
|
138
|
+
// pid is this process's, which goes on: nothing but an ending in the graph
|
|
139
|
+
// says it is over. So it is asked to write its own first (backend_worker.ts
|
|
140
|
+
// `end`), and nobody waits out what it held. Bounded, since a Worker wedged
|
|
141
|
+
// past answering can only be let go.
|
|
142
|
+
let ending: Promise<void> | undefined
|
|
143
|
+
let end = () =>
|
|
144
|
+
ending ??= (lent ? Promise.resolve() : link.request('end', [], {
|
|
145
|
+
timeout: ENDING,
|
|
146
|
+
}).then(() => {}, () => {})).finally(release)
|
|
147
|
+
let closing = false
|
|
148
|
+
let shutdown: Promise<{ drained: boolean }> | undefined
|
|
149
|
+
let forceExit!: () => void
|
|
150
|
+
let forced = new Promise<{ drained: boolean }>((resolve) => {
|
|
151
|
+
forceExit = () => resolve({ drained: false })
|
|
152
|
+
})
|
|
153
|
+
let request = async (method: string, args: unknown[] = []) => {
|
|
154
|
+
if (closing && method != 'close' && method != 'fakeRelease') {
|
|
155
|
+
throw new ShuttingDown()
|
|
156
|
+
}
|
|
157
|
+
if (failure) throw failure
|
|
158
|
+
return await link.request(
|
|
159
|
+
method,
|
|
160
|
+
args,
|
|
161
|
+
method == 'close'
|
|
162
|
+
? { timeout: null }
|
|
163
|
+
: method == 'authorizeMCP'
|
|
164
|
+
? { timeout: 120000 }
|
|
165
|
+
: undefined,
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
let init: { names: Record<string, string> }
|
|
169
|
+
try {
|
|
170
|
+
init = await request('init', [{
|
|
171
|
+
...config,
|
|
172
|
+
streaming: streamingEnabled(config, env),
|
|
173
|
+
}]) as typeof init
|
|
174
|
+
} catch (e) {
|
|
175
|
+
release()
|
|
176
|
+
replica.close()
|
|
177
|
+
throw e
|
|
178
|
+
}
|
|
179
|
+
// The backend owns the host reporter, so frontend diagnostics cross that
|
|
180
|
+
// door with their scrubbed thrown value and catch context.
|
|
181
|
+
let detachDiagnostics = diagnostics().attach(async (error, context) => {
|
|
182
|
+
let e = error as Error
|
|
183
|
+
await request('report', [{
|
|
184
|
+
name: e.name,
|
|
185
|
+
message: e.message,
|
|
186
|
+
stack: e.stack,
|
|
187
|
+
}, context])
|
|
188
|
+
})
|
|
189
|
+
let rows = (id: string) =>
|
|
190
|
+
(members.get(id) ?? []).flatMap((id) => {
|
|
191
|
+
let b = replica.ent(id)
|
|
192
|
+
return b ? transient(replica.graph).project([b]) : []
|
|
193
|
+
})
|
|
194
|
+
let listen = async (id: string, query: string) => {
|
|
195
|
+
initializing.add(id)
|
|
196
|
+
if (!id.startsWith('frontier:')) {
|
|
197
|
+
replica.cache.subscribe(id, query, { prime: false })
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
await request('subscribe', [id, query])
|
|
201
|
+
await queued
|
|
202
|
+
} finally {
|
|
203
|
+
initializing.delete(id)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
let selected: string | undefined,
|
|
207
|
+
plans: string[] = [],
|
|
208
|
+
serial: Promise<unknown> = Promise.resolve()
|
|
209
|
+
let selectedKey: string | undefined
|
|
210
|
+
let watchSelected = async (session: string) => {
|
|
211
|
+
let key = 'session:' + session
|
|
212
|
+
if (selectedKey == key) return
|
|
213
|
+
if (selectedKey) {
|
|
214
|
+
await request('unsubscribe', [selectedKey])
|
|
215
|
+
await queued
|
|
216
|
+
members.delete(selectedKey)
|
|
217
|
+
replica.cache.unsubscribe(selectedKey)
|
|
218
|
+
}
|
|
219
|
+
await listen(key, `.session&.entity.eid=${JSON.stringify(session)}&*`)
|
|
220
|
+
selectedKey = key
|
|
221
|
+
}
|
|
222
|
+
let snapshot = () =>
|
|
223
|
+
plans.flatMap((key) =>
|
|
224
|
+
rows(key).sort((a, b) =>
|
|
225
|
+
Number((a.entry as Comp).seq) - Number((b.entry as Comp).seq)
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
let select = (session: string) => {
|
|
229
|
+
let result = serial.catch(() => {}).then(async () => {
|
|
230
|
+
if (selected == session) {
|
|
231
|
+
await queued
|
|
232
|
+
return snapshot()
|
|
233
|
+
}
|
|
234
|
+
await watchSelected(session)
|
|
235
|
+
if (windowKeys.length || frontierKeys.length) await clearWindow()
|
|
236
|
+
for (let id of plans) {
|
|
237
|
+
await request('unsubscribe', [id])
|
|
238
|
+
await queued
|
|
239
|
+
members.delete(id)
|
|
240
|
+
replica.cache.unsubscribe(id)
|
|
241
|
+
}
|
|
242
|
+
plans = []
|
|
243
|
+
selected = undefined
|
|
244
|
+
let queries = await request('transcriptPlan', [session]) as string[]
|
|
245
|
+
for (let i = 0; i < queries.length; i++) {
|
|
246
|
+
let id = 'transcript:' + session + ':' + i
|
|
247
|
+
await listen(id, queries[i])
|
|
248
|
+
plans.push(id)
|
|
249
|
+
}
|
|
250
|
+
selected = session
|
|
251
|
+
return snapshot()
|
|
252
|
+
})
|
|
253
|
+
serial = result
|
|
254
|
+
return result
|
|
255
|
+
}
|
|
256
|
+
let windowSession: string | undefined,
|
|
257
|
+
windowQueries: string[] = [],
|
|
258
|
+
frontierKeys: string[] = []
|
|
259
|
+
let windowKeys: string[] = []
|
|
260
|
+
let lastWindow: {
|
|
261
|
+
key: string
|
|
262
|
+
revision: number
|
|
263
|
+
plan: TranscriptPlan & { frontiers: string[] }
|
|
264
|
+
} | undefined
|
|
265
|
+
let clearWindow = async () => {
|
|
266
|
+
for (let id of [...windowKeys, ...frontierKeys]) {
|
|
267
|
+
await request('unsubscribe', [id])
|
|
268
|
+
await queued
|
|
269
|
+
members.delete(id)
|
|
270
|
+
replica.cache.unsubscribe(id)
|
|
271
|
+
}
|
|
272
|
+
windowKeys = []
|
|
273
|
+
frontierKeys = []
|
|
274
|
+
windowQueries = []
|
|
275
|
+
lastWindow = undefined
|
|
276
|
+
}
|
|
277
|
+
let windowed = (
|
|
278
|
+
session: string,
|
|
279
|
+
options: TranscriptWindow = {},
|
|
280
|
+
): Promise<TranscriptPage> => {
|
|
281
|
+
let result = serial.catch(() => {}).then(async () => {
|
|
282
|
+
await watchSelected(session)
|
|
283
|
+
for (let id of plans) {
|
|
284
|
+
await request('unsubscribe', [id])
|
|
285
|
+
await queued
|
|
286
|
+
members.delete(id)
|
|
287
|
+
replica.cache.unsubscribe(id)
|
|
288
|
+
}
|
|
289
|
+
plans = []
|
|
290
|
+
selected = undefined
|
|
291
|
+
let key = JSON.stringify([session, options])
|
|
292
|
+
let revision = windowRevision
|
|
293
|
+
let plan = lastWindow?.key == key && lastWindow.revision == revision
|
|
294
|
+
? lastWindow.plan
|
|
295
|
+
: await request('transcriptWindowPlan', [session, options]) as
|
|
296
|
+
& TranscriptPlan
|
|
297
|
+
& { frontiers: string[] }
|
|
298
|
+
let replaced = windowSession != session ||
|
|
299
|
+
JSON.stringify(plan.queries) != JSON.stringify(windowQueries)
|
|
300
|
+
if (
|
|
301
|
+
windowSession != session ||
|
|
302
|
+
JSON.stringify(plan.queries) != JSON.stringify(windowQueries)
|
|
303
|
+
) {
|
|
304
|
+
await clearWindow()
|
|
305
|
+
windowSession = session
|
|
306
|
+
windowQueries = plan.queries
|
|
307
|
+
for (let [i, query] of plan.queries.entries()) {
|
|
308
|
+
let id = 'window:' + session + ':' + i
|
|
309
|
+
await listen(id, query)
|
|
310
|
+
windowKeys.push(id)
|
|
311
|
+
}
|
|
312
|
+
for (let [i, query] of plan.frontiers.entries()) {
|
|
313
|
+
let id = 'frontier:' + session + ':' + i
|
|
314
|
+
await listen(id, query)
|
|
315
|
+
frontierKeys.push(id)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
await queued
|
|
319
|
+
// Recheck once after installing frontier subscriptions. A write between
|
|
320
|
+
// planning and the initial frontier snapshot must not disappear.
|
|
321
|
+
lastWindow = { key, revision, plan }
|
|
322
|
+
if (replaced) {
|
|
323
|
+
queueMicrotask(() => {
|
|
324
|
+
for (let notify of listeners) notify()
|
|
325
|
+
})
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
entries: windowKeys.flatMap((key) =>
|
|
329
|
+
rows(key).sort((a, b) =>
|
|
330
|
+
Number((a.entry as Comp).seq) - Number((b.entry as Comp).seq)
|
|
331
|
+
)
|
|
332
|
+
),
|
|
333
|
+
before: plan.before,
|
|
334
|
+
after: plan.after,
|
|
335
|
+
total: plan.total,
|
|
336
|
+
offset: plan.offset,
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
serial = result
|
|
340
|
+
return result
|
|
341
|
+
}
|
|
342
|
+
let agent: UIAgent = {
|
|
343
|
+
start: async (text, options) =>
|
|
344
|
+
await request('start', [text, options]) as string,
|
|
345
|
+
models: async (session) =>
|
|
346
|
+
await request('models', [session]) as ModelSelection,
|
|
347
|
+
personas: async () => await request('personas') as Bundle[],
|
|
348
|
+
selectModel: async (session, model) => {
|
|
349
|
+
await request('selectModel', [session, model])
|
|
350
|
+
},
|
|
351
|
+
send: async (id, text) => await request('send', [id, text]) as string,
|
|
352
|
+
taskEntry: async (id, text) =>
|
|
353
|
+
await request('taskEntry', [id, text]) as { task: string; child: string },
|
|
354
|
+
archive: async (id, value) => {
|
|
355
|
+
await request('archive', [id, value])
|
|
356
|
+
},
|
|
357
|
+
authorizeMCP: async (action, name, callback) =>
|
|
358
|
+
await request('authorizeMCP', [
|
|
359
|
+
action,
|
|
360
|
+
name,
|
|
361
|
+
callback,
|
|
362
|
+
]) as import('./mcp_auth.ts').MCPAuthReply,
|
|
363
|
+
runtime: async (id) => await request('runtime', [id]) as Bundle[],
|
|
364
|
+
control: async (id, action) =>
|
|
365
|
+
await request('control', [id, action]) as string,
|
|
366
|
+
sessions: async (selected, page) =>
|
|
367
|
+
await request('sessions', [selected, page]) as Bundle[],
|
|
368
|
+
tasks: async (page) => await request('tasks', [page]) as Bundle[],
|
|
369
|
+
usage: async (session) => await request('usage', [session]) as Bundle[],
|
|
370
|
+
children: async (id) => await request('children', [id]) as Bundle[],
|
|
371
|
+
entrySource: async (session, eid, options) =>
|
|
372
|
+
await request('entrySource', [session, eid, options]) as EntrySource,
|
|
373
|
+
transcript: (id) => select(id),
|
|
374
|
+
transcriptWindow: windowed,
|
|
375
|
+
entry: (b) =>
|
|
376
|
+
tree(transcriptViews, b, 'Transcript', vocab, {
|
|
377
|
+
names: init.names,
|
|
378
|
+
inlineImages: env('HARNESS_GRAPHICS') == 'kitty',
|
|
379
|
+
image: (eid: string) => request('image', [eid]),
|
|
380
|
+
}),
|
|
381
|
+
line: (b, view = 'Line', ctx = {}) =>
|
|
382
|
+
render(
|
|
383
|
+
sessionViews,
|
|
384
|
+
b,
|
|
385
|
+
view,
|
|
386
|
+
vocab,
|
|
387
|
+
{ names: init.names, ...ctx },
|
|
388
|
+
'plain',
|
|
389
|
+
),
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
agent,
|
|
393
|
+
traffic: link.stats,
|
|
394
|
+
replica,
|
|
395
|
+
subscribe: (fn: () => void) => {
|
|
396
|
+
listeners.add(fn)
|
|
397
|
+
return () => {
|
|
398
|
+
listeners.delete(fn)
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
resume: () => request('resume'),
|
|
402
|
+
idle: (session: string) => request('idle', [session]),
|
|
403
|
+
/** Fake-provider barriers: observe dispatch and explicitly allow replies. */
|
|
404
|
+
testing: options.fake
|
|
405
|
+
? {
|
|
406
|
+
started: () => request('fakeStarted'),
|
|
407
|
+
release: () => request('fakeRelease'),
|
|
408
|
+
}
|
|
409
|
+
: undefined,
|
|
410
|
+
/** Force is explicit and also releases an outstanding graceful close. */
|
|
411
|
+
force: () => {
|
|
412
|
+
closing = true
|
|
413
|
+
forceExit()
|
|
414
|
+
detachDiagnostics()
|
|
415
|
+
void end()
|
|
416
|
+
},
|
|
417
|
+
close: (options: { timeout?: number | null } = {}) =>
|
|
418
|
+
shutdown ??= (async () => {
|
|
419
|
+
closing = true
|
|
420
|
+
listeners.clear()
|
|
421
|
+
let cancel = () => {}
|
|
422
|
+
let result = { drained: false }
|
|
423
|
+
try {
|
|
424
|
+
return result = await Promise.race([
|
|
425
|
+
request('close').then(() => ({ drained: true })),
|
|
426
|
+
forced,
|
|
427
|
+
new Promise<{ drained: boolean }>((resolve) => {
|
|
428
|
+
if (options.timeout === null) return
|
|
429
|
+
cancel = clock.after(
|
|
430
|
+
options.timeout ?? 2000,
|
|
431
|
+
() => resolve({ drained: false }),
|
|
432
|
+
)
|
|
433
|
+
}),
|
|
434
|
+
])
|
|
435
|
+
} finally {
|
|
436
|
+
cancel()
|
|
437
|
+
if (result.drained) release()
|
|
438
|
+
else await end()
|
|
439
|
+
await queued
|
|
440
|
+
replica.close()
|
|
441
|
+
detachDiagnostics()
|
|
442
|
+
listeners.clear()
|
|
443
|
+
}
|
|
444
|
+
})(),
|
|
445
|
+
}
|
|
446
|
+
}
|
package/results.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Narrow snapshots at the harness door; direct tool calls return complete data. */ import { outputValue } from "@yaks/context";
|
|
2
|
+
import { jqOver, jqRegex } from "@yaks/context/result";
|
|
3
|
+
import { ToolError } from "@yaks/session";
|
|
4
|
+
export let resultTool = (g)=>({
|
|
5
|
+
name: 'result_query',
|
|
6
|
+
title: 'Query kept result',
|
|
7
|
+
step: {
|
|
8
|
+
doing: 'Looking through an earlier answer…',
|
|
9
|
+
done: 'Looked through an earlier answer'
|
|
10
|
+
},
|
|
11
|
+
description: 'Run a jq filter against a complete tool result kept by id. The filter runs on the whole kept answer, not on its rows: a graph answer is {"result": [rows]}, so .result[:3] gives the first three rows, where .[:3] fails. Returns {values: [...]} for the jq output stream. Third-party MCP replies retain .structuredContent and .content. Plain text is a jq string.',
|
|
12
|
+
parameters: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
id: {
|
|
16
|
+
type: 'string'
|
|
17
|
+
},
|
|
18
|
+
query: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: jqRegex
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
required: [
|
|
24
|
+
'id',
|
|
25
|
+
'query'
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
readOnly: true,
|
|
29
|
+
run: async ({ id, query })=>{
|
|
30
|
+
let [row] = await g.get([
|
|
31
|
+
String(id)
|
|
32
|
+
], [
|
|
33
|
+
'context_output'
|
|
34
|
+
]);
|
|
35
|
+
let output = row?.context_output;
|
|
36
|
+
if (!output || output.reader) {
|
|
37
|
+
throw new ToolError('result', 'Kept result unavailable');
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
let values = jqOver(String(query), outputValue(String(output.body)));
|
|
41
|
+
return JSON.stringify({
|
|
42
|
+
values
|
|
43
|
+
});
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw new ToolError('query', error.message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
//# sourceMappingURL=results.js.map
|
package/results.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"results.js","sources":["./results.ts"],"names":[],"mappings":"AAAA,kFAAkF,GAElF,SAAS,WAAW,6BAAkC;AACtD,SAAS,MAAM,EAAE,OAAO,oCAAyC;AAEjE,SAAS,SAAS,6BAAkC;AAEpD,OAAO,IAAI,aAAa,CAAC,IAAmB,CAAC;IAC3C,MAAM;IACN,OAAO;IACP,MAAM;MACJ,OAAO;MACP,MAAM;IACR;IACA,aACE;IACF,YAAY;MACV,MAAM;MACN,YAAY;QACV,IAAI;UAAE,MAAM;QAAS;QACrB,OAAO;UAAE,MAAM;UAAU,aAAa;QAAQ;MAChD;MACA,UAAU;QAAC;QAAM;OAAQ;IAC3B;IACA,UAAU;IACV,KAAK,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE;MACvB,IAAI,CAAC,IAAI,GAAG,MAAM,EAAE,GAAG,CAAC;QAAC,OAAO;OAAI,EAAE;QAAC;OAAiB;MACxD,IAAI,SAAS,KAAK;MAClB,IAAI,CAAC,UAAU,OAAO,MAAM,EAAE;QAC5B,MAAM,IAAI,UAAU,UAAU;MAChC;MACA,IAAI;QACF,IAAI,SAAS,OAAO,OAAO,QAAQ,YAAY,OAAO,OAAO,IAAI;QACjE,OAAO,KAAK,SAAS,CAAC;UAAE;QAAO;MACjC,EAAE,OAAO,OAAO;QACd,MAAM,IAAI,UAAU,SAAS,AAAC,MAAgB,OAAO;MACvD;IACF;EACF,CAAC,EAAC"}
|
package/results.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Narrow snapshots at the harness door; direct tool calls return complete data. */
|
|
2
|
+
import type { Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
3
|
+
import { outputValue } from '@yaks/context'
|
|
4
|
+
import { jqOver, jqRegex } from '@yaks/context/result'
|
|
5
|
+
import type { Tool } from '@yaks/session'
|
|
6
|
+
import { ToolError } from '@yaks/session'
|
|
7
|
+
|
|
8
|
+
export let resultTool = (g: Graph): Tool => ({
|
|
9
|
+
name: 'result_query',
|
|
10
|
+
title: 'Query kept result',
|
|
11
|
+
step: {
|
|
12
|
+
doing: 'Looking through an earlier answer…',
|
|
13
|
+
done: 'Looked through an earlier answer',
|
|
14
|
+
},
|
|
15
|
+
description:
|
|
16
|
+
'Run a jq filter against a complete tool result kept by id. The filter runs on the whole kept answer, not on its rows: a graph answer is {"result": [rows]}, so .result[:3] gives the first three rows, where .[:3] fails. Returns {values: [...]} for the jq output stream. Third-party MCP replies retain .structuredContent and .content. Plain text is a jq string.',
|
|
17
|
+
parameters: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
id: { type: 'string' },
|
|
21
|
+
query: { type: 'string', description: jqRegex },
|
|
22
|
+
},
|
|
23
|
+
required: ['id', 'query'],
|
|
24
|
+
},
|
|
25
|
+
readOnly: true,
|
|
26
|
+
run: async ({ id, query }) => {
|
|
27
|
+
let [row] = await g.get([String(id)], ['context_output'])
|
|
28
|
+
let output = row?.context_output as Comp | undefined
|
|
29
|
+
if (!output || output.reader) {
|
|
30
|
+
throw new ToolError('result', 'Kept result unavailable')
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
let values = jqOver(String(query), outputValue(String(output.body)))
|
|
34
|
+
return JSON.stringify({ values })
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new ToolError('query', (error as Error).message)
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
})
|
package/routes.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The routes facet, exported as `@yaks/harness/routes`: where a `yak` host
|
|
2
|
+
// that lists the harness and serves `web` answers a model step another host
|
|
3
|
+
// forwards to it (@yaks/model/step), from this machine's own providers. That
|
|
4
|
+
// is the serving end of a provider that is a machine
|
|
5
|
+
// (`provider{name, transport: 'machine', machine}`): the other host sends a
|
|
6
|
+
// model request, and the credentials and command lines that answer it stay
|
|
7
|
+
// here.
|
|
8
|
+
//
|
|
9
|
+
// Which providers answer is the config's to say (`serve`, by `provider.name`),
|
|
10
|
+
// and naming none answers nothing: listing the harness lends no subscription.
|
|
11
|
+
// The route says nothing of who may ask. A machine opens it to another host
|
|
12
|
+
// the way it opens any path, such as @yaks/tunnel's `routes`.
|
|
13
|
+
import { signins } from "@yaks/connections";
|
|
14
|
+
import { serving } from "@yaks/model";
|
|
15
|
+
import { answering, PATH } from "@yaks/model/step";
|
|
16
|
+
import * as systemProcess from "@yaks/process/system";
|
|
17
|
+
import { pooledHttp } from "./http.js";
|
|
18
|
+
import { configured, lending } from "./providers.js";
|
|
19
|
+
import { hosted } from "./store.js";
|
|
20
|
+
// The ones of `all` the config names. A name no configured plugin serves is a
|
|
21
|
+
// mistake in the config, said at the first step rather than answered as a
|
|
22
|
+
// model nobody serves.
|
|
23
|
+
let named = (all, names)=>{
|
|
24
|
+
let missing = names.filter((name)=>!Object.hasOwn(all, name));
|
|
25
|
+
if (missing.length) {
|
|
26
|
+
throw new Error(`serve names ${missing.join(', ')}, which no plugin of this config ` + `serves: ${Object.keys(all).join(', ') || 'none'}`);
|
|
27
|
+
}
|
|
28
|
+
return Object.fromEntries(names.map((name)=>[
|
|
29
|
+
name,
|
|
30
|
+
all[name]
|
|
31
|
+
]));
|
|
32
|
+
};
|
|
33
|
+
/** `POST /model/step`: one model step, answered from the providers `serve`
|
|
34
|
+
* names and streamed as it arrives. `providers` are ones the caller already
|
|
35
|
+
* holds, beside the config's. */ export let routes = (host, options = {})=>{
|
|
36
|
+
let names = options.serve ?? [];
|
|
37
|
+
if (!names.length) return [];
|
|
38
|
+
let h = hosted(host);
|
|
39
|
+
// Loaded by the first step: a server that is never asked imports no
|
|
40
|
+
// provider.
|
|
41
|
+
let lent;
|
|
42
|
+
let providers = ()=>lent ??= (async ()=>{
|
|
43
|
+
let http = pooledHttp(options.http);
|
|
44
|
+
host.defer(http.close);
|
|
45
|
+
let all = await configured(h.plugins, lending(h, signins(h), http.fetch, options.env ?? systemProcess.env.get));
|
|
46
|
+
return named({
|
|
47
|
+
...all,
|
|
48
|
+
...options.providers
|
|
49
|
+
}, names);
|
|
50
|
+
})();
|
|
51
|
+
let serve = async (request)=>await serving(host.graph, await providers())(request);
|
|
52
|
+
return [
|
|
53
|
+
{
|
|
54
|
+
method: 'POST',
|
|
55
|
+
path: PATH,
|
|
56
|
+
handle: answering(serve, (error)=>host.report?.(error, {
|
|
57
|
+
during: {
|
|
58
|
+
request: PATH
|
|
59
|
+
}
|
|
60
|
+
}))
|
|
61
|
+
}
|
|
62
|
+
];
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=routes.js.map
|
package/routes.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sources":["./routes.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,6EAA6E;AAC7E,qDAAqD;AACrD,4EAA4E;AAC5E,2EAA2E;AAC3E,QAAQ;AACR,EAAE;AACF,+EAA+E;AAC/E,8EAA8E;AAC9E,4EAA4E;AAC5E,8DAA8D;AAG9D,SAAS,OAAO,iCAAsC;AAEtD,SAAqB,OAAO,2BAAgC;AAC5D,SAAS,SAAS,EAAE,IAAI,gCAAqC;AAC7D,YAAY,+CAAoD;AAChE,SAAS,UAAU,oBAAmB;AAEtC,SAAS,UAAU,EAAE,OAAO,yBAAwB;AACpD,SAAS,MAAM,qBAAoB;AASnC,8EAA8E;AAC9E,0EAA0E;AAC1E,uBAAuB;AACvB,IAAI,QAAQ,CACV,KACA;EAEA,IAAI,UAAU,MAAM,MAAM,CAAC,CAAC,OAAS,CAAC,OAAO,MAAM,CAAC,KAAK;EACzD,IAAI,QAAQ,MAAM,EAAE;IAClB,MAAM,IAAI,MACR,CAAC,YAAY,EAAE,QAAQ,IAAI,CAAC,MAAM,iCAAiC,CAAC,GAClE,CAAC,QAAQ,EAAE,OAAO,IAAI,CAAC,KAAK,IAAI,CAAC,SAAS,QAAQ;EAExD;EACA,OAAO,OAAO,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,OAAS;MAAC;MAAM,GAAG,CAAC,KAAK;KAAC;AACjE;AAEA;;+BAE+B,GAC/B,OAAO,IAAI,SAAS,CAClB,MACA,UAA8D,CAAC,CAAC;EAEhE,IAAI,QAAQ,QAAQ,KAAK,IAAI,EAAE;EAC/B,IAAI,CAAC,MAAM,MAAM,EAAE,OAAO,EAAE;EAC5B,IAAI,IAAI,OAAO;EACf,oEAAoE;EACpE,YAAY;EACZ,IAAI;EACJ,IAAI,YAAY,IACd,SAAS,CAAC;MACR,IAAI,OAAO,WAAW,QAAQ,IAAI;MAClC,KAAK,KAAK,CAAC,KAAK,KAAK;MACrB,IAAI,MAAM,MAAM,WACd,EAAE,OAAO,EACT,QACE,GACA,QAAQ,IACR,KAAK,KAAK,EACV,QAAQ,GAAG,IAAI,cAAc,GAAG,CAAC,GAAG;MAGxC,OAAO,MAAM;QAAE,GAAG,GAAG;QAAE,GAAG,QAAQ,SAAS;MAAC,GAAG;IACjD,CAAC;EACH,IAAI,QAAe,OAAO,UACxB,MAAM,QAAQ,KAAK,KAAK,EAAE,MAAM,aAAa;EAC/C,OAAO;IAAC;MACN,QAAQ;MACR,MAAM;MACN,QAAQ,UACN,OACA,CAAC,QAAU,KAAK,MAAM,GAAG,OAAO;UAAE,QAAQ;YAAE,SAAS;UAAK;QAAE;IAEhE;GAAE;AACJ,EAAC"}
|