@acryldev/agent-panel 0.0.0-stage → 0.1.0
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 +21 -0
- package/client/describe.ts +101 -0
- package/client/driver-pick.ts +22 -0
- package/client/frame.ts +51 -0
- package/client/main.ts +707 -0
- package/client/markdown.ts +219 -0
- package/headless.ts +220 -0
- package/index.ts +523 -0
- package/launch.ts +67 -0
- package/lib/client/describe.ts +101 -0
- package/lib/client/driver-pick.ts +22 -0
- package/lib/client/frame.ts +51 -0
- package/lib/client/main.ts +707 -0
- package/lib/client/markdown.ts +219 -0
- package/lib/headless-B_RGxZFU.js +252 -0
- package/lib/headless-B_RGxZFU.js.map +1 -0
- package/lib/headless.js +2 -0
- package/lib/index.js +562 -0
- package/lib/index.js.map +1 -0
- package/lib/mount.js +25 -0
- package/lib/mount.js.map +1 -0
- package/lib/registry.js +13 -0
- package/lib/registry.js.map +1 -0
- package/lib/types/headless.d.ts +22 -0
- package/lib/types/index.d.ts +22 -0
- package/lib/types/launch.d.ts +18 -0
- package/lib/types/mount.d.ts +12 -0
- package/lib/types/registry.d.ts +10 -0
- package/lib/types/tsdown.config.d.ts +2 -0
- package/lib/types/visitors.d.ts +82 -0
- package/mount.ts +27 -0
- package/package.json +62 -3
- package/plugin.json +28 -0
- package/registry.ts +17 -0
- package/scripts/copy-assets.mjs +5 -0
- package/test/client-guards.test.ts +13 -0
- package/test/describe.test.ts +105 -0
- package/test/driver-pick.test.ts +24 -0
- package/test/frame.test.ts +35 -0
- package/test/launch.test.ts +90 -0
- package/test/markdown.test.ts +79 -0
- package/test/panel-client.test.ts +150 -0
- package/test/panel.test.ts +534 -0
- package/test/socket.test.ts +209 -0
- package/test/visitors.test.ts +112 -0
- package/test/why.test.ts +61 -0
- package/tsconfig.json +21 -0
- package/tsdown.config.ts +23 -0
- package/visitors.ts +231 -0
- package/README.md +0 -3
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { afterEach, test } from 'node:test'
|
|
6
|
+
import { agent, memory, model, scripted, tools } from '@acryldev/agent'
|
|
7
|
+
import { builder } from '@acryldev/builder'
|
|
8
|
+
import { extensions } from '@acryldev/extensions'
|
|
9
|
+
import { instance, pinnedInstance } from '@acryldev/instance'
|
|
10
|
+
import * as router from '@acryldev/router'
|
|
11
|
+
import * as state from '@acryldev/state'
|
|
12
|
+
import { Context } from '@deepseek-ai/cordis'
|
|
13
|
+
import * as panel from '../index.ts'
|
|
14
|
+
|
|
15
|
+
const cleanup: Array<() => Promise<void> | void> = []
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
for (const step of cleanup.splice(0)) await step()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const OWN = { 'sec-fetch-site': 'same-origin' }
|
|
21
|
+
|
|
22
|
+
/** An app with the agent family, the panel mounted BEFORE a page of the app's own. `panel` and `pagePrefix` mirror the frame host: the app mounted at a prefix. */
|
|
23
|
+
async function boot(
|
|
24
|
+
env: Record<string, string | undefined> = {},
|
|
25
|
+
options: { panel?: panel.PanelConfig; pagePrefix?: string } = {},
|
|
26
|
+
) {
|
|
27
|
+
const saved = { NODE_ENV: process.env.NODE_ENV, ACRYL_DOOR: process.env.ACRYL_DOOR }
|
|
28
|
+
process.env.NODE_ENV = env.NODE_ENV ?? 'test'
|
|
29
|
+
if (env.ACRYL_DOOR === undefined) delete process.env.ACRYL_DOOR
|
|
30
|
+
else process.env.ACRYL_DOOR = env.ACRYL_DOOR
|
|
31
|
+
const root = mkdtempSync(join(tmpdir(), 'panel-root-'))
|
|
32
|
+
const app = new Context()
|
|
33
|
+
const fibers: Array<ReturnType<Context['plugin']>> = []
|
|
34
|
+
cleanup.push(async () => {
|
|
35
|
+
for (const fiber of [...fibers].reverse()) await fiber.dispose()
|
|
36
|
+
rmSync(root, { recursive: true, force: true })
|
|
37
|
+
for (const [key, value] of Object.entries(saved)) {
|
|
38
|
+
if (value === undefined) delete process.env[key]
|
|
39
|
+
else process.env[key] = value
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
// One after another, as `blend.yaml` does (compose): the panel's middleware is mounted before the page it wraps.
|
|
43
|
+
const mount = async (fiber: ReturnType<Context['plugin']>) => {
|
|
44
|
+
fibers.push(fiber)
|
|
45
|
+
await fiber.await()
|
|
46
|
+
}
|
|
47
|
+
for (const start of [
|
|
48
|
+
() => app.plugin(instance, pinnedInstance(join(root, '.acrylblends'), { root })),
|
|
49
|
+
() => app.plugin(router),
|
|
50
|
+
() => app.plugin(state),
|
|
51
|
+
() => app.plugin(extensions, { sources: [{ dir: join(root, 'plugins'), trust: 'install' }] }),
|
|
52
|
+
() => app.plugin(model),
|
|
53
|
+
() => app.plugin(tools),
|
|
54
|
+
() => app.plugin(memory),
|
|
55
|
+
() => app.plugin(agent),
|
|
56
|
+
() => app.plugin(builder, {}),
|
|
57
|
+
() => app.plugin(panel, { drivers: [], ...options.panel }),
|
|
58
|
+
() =>
|
|
59
|
+
app.plugin({
|
|
60
|
+
name: 'page',
|
|
61
|
+
inject: ['router'],
|
|
62
|
+
apply(ctx: Context) {
|
|
63
|
+
ctx.router.route(
|
|
64
|
+
options.pagePrefix ?? '/',
|
|
65
|
+
new router.Acryl()
|
|
66
|
+
.get('/', (c) => c.html('<!doctype html><html><body><h1>Home</h1></body></html>'))
|
|
67
|
+
.get('/data', (c) => c.json({ ok: true })),
|
|
68
|
+
)
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
])
|
|
72
|
+
await mount(start())
|
|
73
|
+
return { app, root }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The events of a finished server-sent stream. */
|
|
77
|
+
async function eventsOf(
|
|
78
|
+
response: Response,
|
|
79
|
+
): Promise<Array<{ event: string; data: Record<string, unknown> }>> {
|
|
80
|
+
const text = await response.text()
|
|
81
|
+
return text
|
|
82
|
+
.split('\n\n')
|
|
83
|
+
.filter(Boolean)
|
|
84
|
+
.map((block) => ({
|
|
85
|
+
event: /^event: (.*)$/mu.exec(block)?.[1] ?? 'message',
|
|
86
|
+
data: JSON.parse(/^data: (.*)$/mu.exec(block)?.[1] ?? '{}') as Record<string, unknown>,
|
|
87
|
+
}))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
test('the script tag is added to HTML pages and to nothing else', async () => {
|
|
91
|
+
const { app } = await boot()
|
|
92
|
+
const page = await (await app.router.request('/')).text()
|
|
93
|
+
assert.match(
|
|
94
|
+
page,
|
|
95
|
+
/<script type="module" src="\/api\/agent-panel\/panel\.js"><\/script><\/body>/u,
|
|
96
|
+
)
|
|
97
|
+
assert.equal(await (await app.router.request('/data')).text(), '{"ok":true}')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test("a hosted app keeps the panel under its mount: the root's /api path is not the panel's, and the page carries the mounted tag", async () => {
|
|
101
|
+
const { app } = await boot({}, { panel: { mount: '/app' }, pagePrefix: '/app' })
|
|
102
|
+
const page = await (await app.router.request('/app')).text()
|
|
103
|
+
assert.match(
|
|
104
|
+
page,
|
|
105
|
+
/<script type="module" src="\/app\/api\/agent-panel\/panel\.js"><\/script><\/body>/u,
|
|
106
|
+
)
|
|
107
|
+
assert.equal((await app.router.request('/api/agent-panel/session', { headers: OWN })).status, 404)
|
|
108
|
+
const session = await app.router.request('/app/api/agent-panel/session', { headers: OWN })
|
|
109
|
+
assert.equal(session.status, 200)
|
|
110
|
+
const loader = await (await app.router.request('/app/api/agent-panel/panel.js')).text()
|
|
111
|
+
assert.match(loader, /start\("\/app\/api\/agent-panel"\)/u)
|
|
112
|
+
const main = await app.router.request('/app/api/agent-panel/client/main.ts')
|
|
113
|
+
assert.equal(main.status, 200)
|
|
114
|
+
assert.match(main.headers.get('content-type') ?? '', /javascript/u)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('the mount must be empty or an absolute path without a trailing slash', async () => {
|
|
118
|
+
for (const mount of ['app', '/app/', 'https://example.com'])
|
|
119
|
+
await assert.rejects(
|
|
120
|
+
async () => boot({}, { panel: { mount } }),
|
|
121
|
+
/absolute path such as "\/app"/u,
|
|
122
|
+
)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test("only the app's own page gets a session, and the token is needed to ask", async () => {
|
|
126
|
+
const { app } = await boot()
|
|
127
|
+
assert.equal((await app.router.request('/api/agent-panel/session')).status, 403)
|
|
128
|
+
assert.equal(
|
|
129
|
+
(
|
|
130
|
+
await app.router.request('/api/agent-panel/session', {
|
|
131
|
+
headers: { 'sec-fetch-site': 'cross-site' },
|
|
132
|
+
})
|
|
133
|
+
).status,
|
|
134
|
+
403,
|
|
135
|
+
)
|
|
136
|
+
const session = (await (
|
|
137
|
+
await app.router.request('/api/agent-panel/session', { headers: OWN })
|
|
138
|
+
).json()) as { token: string; drivers: Array<{ name: string; kind: string }> }
|
|
139
|
+
assert.deepEqual(session.drivers, [{ name: 'agent', kind: 'chat' }])
|
|
140
|
+
assert.match(session.token, /^[\w-]{32}$/u)
|
|
141
|
+
const ask = (headers: Record<string, string>, body: unknown) =>
|
|
142
|
+
app.router.request('/api/agent-panel/ask', {
|
|
143
|
+
method: 'POST',
|
|
144
|
+
headers: { 'content-type': 'application/json', ...headers },
|
|
145
|
+
body: JSON.stringify(body),
|
|
146
|
+
})
|
|
147
|
+
assert.equal((await ask({}, { message: 'hi' })).status, 403)
|
|
148
|
+
assert.equal((await ask(OWN, { message: 'hi' })).status, 403)
|
|
149
|
+
assert.equal(
|
|
150
|
+
(await ask({ ...OWN, 'x-agent-panel-token': 'wrong' }, { message: 'hi' })).status,
|
|
151
|
+
403,
|
|
152
|
+
)
|
|
153
|
+
const good = { ...OWN, 'x-agent-panel-token': session.token }
|
|
154
|
+
assert.equal((await ask(good, { message: '' })).status, 400)
|
|
155
|
+
assert.equal((await ask(good, { message: 'hi', driver: 'claude' })).status, 400)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('a message makes the agent build a feature live: tool events, a change event, and the page of the running app serves it', async () => {
|
|
159
|
+
const { app, root } = await boot()
|
|
160
|
+
const session = (await (
|
|
161
|
+
await app.router.request('/api/agent-panel/session', { headers: OWN })
|
|
162
|
+
).json()) as { token: string }
|
|
163
|
+
assert.equal((await app.router.request('/hello')).status, 404)
|
|
164
|
+
const response = await app.router.request('/api/agent-panel/ask', {
|
|
165
|
+
method: 'POST',
|
|
166
|
+
headers: { 'content-type': 'application/json', ...OWN, 'x-agent-panel-token': session.token },
|
|
167
|
+
body: JSON.stringify({ message: 'create route hello /hello Hello' }),
|
|
168
|
+
})
|
|
169
|
+
assert.equal(response.status, 200)
|
|
170
|
+
const events = await eventsOf(response)
|
|
171
|
+
const names = events.map((e) => e.event)
|
|
172
|
+
assert.ok(names.includes('tool'), names.join())
|
|
173
|
+
assert.ok(names.includes('change'), names.join())
|
|
174
|
+
assert.deepEqual(events.find((e) => e.event === 'change')?.data, {
|
|
175
|
+
extension: 'hello',
|
|
176
|
+
action: 'installed',
|
|
177
|
+
})
|
|
178
|
+
assert.equal(names.at(-1), 'done')
|
|
179
|
+
assert.equal(events.at(-1)?.data.changed, true)
|
|
180
|
+
assert.match(
|
|
181
|
+
String(events.find((e) => e.event === 'text')?.data.text),
|
|
182
|
+
/hello: installed, active/u,
|
|
183
|
+
)
|
|
184
|
+
// No restart: the same app answers the new route now.
|
|
185
|
+
assert.deepEqual(await (await app.router.request('/hello')).json(), { text: 'Hello' })
|
|
186
|
+
// Every ask is in the audit log, without its text.
|
|
187
|
+
const audit = readFileSync(join(root, '.acryl', 'audit', 'agent-panel.jsonl'), 'utf8')
|
|
188
|
+
assert.match(audit, /"ask":"start","driver":"agent"/u)
|
|
189
|
+
assert.match(audit, /"ask":"end","driver":"agent","changed":true/u)
|
|
190
|
+
assert.doesNotMatch(audit, /create route hello/u)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test("the panel's browser code is served and compiled for the browser", async () => {
|
|
194
|
+
const { app } = await boot()
|
|
195
|
+
const loader = await app.router.request('/api/agent-panel/panel.js')
|
|
196
|
+
assert.equal(loader.status, 200)
|
|
197
|
+
assert.match(await loader.text(), /agent-panel\/client\/main\.ts/u)
|
|
198
|
+
const main = await app.router.request('/api/agent-panel/client/main.ts')
|
|
199
|
+
assert.equal(main.status, 200)
|
|
200
|
+
assert.match(main.headers.get('content-type') ?? '', /javascript/u)
|
|
201
|
+
assert.doesNotMatch(await main.text(), /: Session|interface /u)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('it is a development tool: with NODE_ENV=production or ACRYL_DOOR=off it adds nothing', async () => {
|
|
205
|
+
for (const env of [{ NODE_ENV: 'production' }, { ACRYL_DOOR: 'off' }]) {
|
|
206
|
+
const { app, root } = await boot(env)
|
|
207
|
+
assert.doesNotMatch(await (await app.router.request('/')).text(), /agent-panel/u)
|
|
208
|
+
assert.equal(
|
|
209
|
+
(await app.router.request('/api/agent-panel/session', { headers: OWN })).status,
|
|
210
|
+
404,
|
|
211
|
+
)
|
|
212
|
+
assert.equal(existsSync(join(root, '.acryl')), false)
|
|
213
|
+
await cleanup.pop()?.()
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('another plugin adds a chat driver to the registry; the panel lists it, runs it, and drops it when the plugin goes', async () => {
|
|
218
|
+
const { app } = await boot()
|
|
219
|
+
const session = async () =>
|
|
220
|
+
(await (await app.router.request('/api/agent-panel/session', { headers: OWN })).json()) as {
|
|
221
|
+
token: string
|
|
222
|
+
drivers: Array<{ name: string; kind: string }>
|
|
223
|
+
}
|
|
224
|
+
const fiber = app.plugin({
|
|
225
|
+
name: 'echo-driver',
|
|
226
|
+
inject: ['drivers'],
|
|
227
|
+
apply(ctx: Context) {
|
|
228
|
+
ctx.effect(() =>
|
|
229
|
+
ctx.drivers.register({
|
|
230
|
+
kind: 'chat',
|
|
231
|
+
name: 'echo',
|
|
232
|
+
async *start({ message }) {
|
|
233
|
+
yield { event: 'text', data: { text: `echo: ${message}` } }
|
|
234
|
+
},
|
|
235
|
+
}),
|
|
236
|
+
)
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
await fiber.await()
|
|
240
|
+
const listed = await session()
|
|
241
|
+
assert.deepEqual(
|
|
242
|
+
listed.drivers.map((d) => d.name),
|
|
243
|
+
['agent', 'echo'],
|
|
244
|
+
)
|
|
245
|
+
const response = await app.router.request('/api/agent-panel/ask', {
|
|
246
|
+
method: 'POST',
|
|
247
|
+
headers: { 'content-type': 'application/json', ...OWN, 'x-agent-panel-token': listed.token },
|
|
248
|
+
body: JSON.stringify({ message: 'hi', driver: 'echo' }),
|
|
249
|
+
})
|
|
250
|
+
const events = await eventsOf(response)
|
|
251
|
+
assert.deepEqual(
|
|
252
|
+
events.map((e) => [e.event, e.data.text ?? e.data.changed]),
|
|
253
|
+
[
|
|
254
|
+
['text', 'echo: hi'],
|
|
255
|
+
['done', false],
|
|
256
|
+
],
|
|
257
|
+
)
|
|
258
|
+
await fiber.dispose()
|
|
259
|
+
assert.deepEqual(
|
|
260
|
+
(await session()).drivers.map((d) => d.name),
|
|
261
|
+
['agent'],
|
|
262
|
+
)
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
test('a plugin that injects drivers is pending while no panel is mounted, and starts when the panel arrives', async () => {
|
|
266
|
+
const app = new Context()
|
|
267
|
+
let registered = false
|
|
268
|
+
const fiber = app.plugin({
|
|
269
|
+
name: 'late-driver',
|
|
270
|
+
inject: ['drivers'],
|
|
271
|
+
apply(ctx: Context) {
|
|
272
|
+
ctx.effect(() => {
|
|
273
|
+
registered = true
|
|
274
|
+
return () => {}
|
|
275
|
+
})
|
|
276
|
+
},
|
|
277
|
+
})
|
|
278
|
+
await fiber.await().catch(() => {})
|
|
279
|
+
assert.equal(registered, false)
|
|
280
|
+
const { createDrivers } = await import('@acryldev/drivers')
|
|
281
|
+
app.provide('drivers', createDrivers())
|
|
282
|
+
await new Promise((r) => setTimeout(r, 20))
|
|
283
|
+
assert.equal(registered, true)
|
|
284
|
+
await fiber.dispose()
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
test('pi is offered when it has a DeepSeek key, from the environment or from its own stored login', async () => {
|
|
288
|
+
const { piHasDeepseekKey } = await import('../headless.ts')
|
|
289
|
+
const { mkdirSync, writeFileSync } = await import('node:fs')
|
|
290
|
+
const home = mkdtempSync(join(tmpdir(), 'pi-home-'))
|
|
291
|
+
cleanup.push(() => rmSync(home, { recursive: true, force: true }))
|
|
292
|
+
assert.equal(piHasDeepseekKey({}, home), false, 'no key and no login')
|
|
293
|
+
assert.equal(
|
|
294
|
+
piHasDeepseekKey({ DEEPSEEK_API_KEY: 'k' }, home),
|
|
295
|
+
true,
|
|
296
|
+
'the key in the environment',
|
|
297
|
+
)
|
|
298
|
+
mkdirSync(join(home, '.pi', 'agent'), { recursive: true })
|
|
299
|
+
writeFileSync(join(home, '.pi', 'agent', 'auth.json'), JSON.stringify({ openai: { key: 'x' } }))
|
|
300
|
+
assert.equal(piHasDeepseekKey({}, home), false, 'a login for another provider does not count')
|
|
301
|
+
writeFileSync(
|
|
302
|
+
join(home, '.pi', 'agent', 'auth.json'),
|
|
303
|
+
JSON.stringify({ deepseek: { type: 'api_key', key: 'k' } }),
|
|
304
|
+
)
|
|
305
|
+
assert.equal(piHasDeepseekKey({}, home), true, 'the DeepSeek key pi stored')
|
|
306
|
+
writeFileSync(join(home, '.pi', 'agent', 'auth.json'), 'not json')
|
|
307
|
+
assert.equal(piHasDeepseekKey({}, home), false, 'an unreadable file is no login')
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
test('a run belongs to the app: it goes on when the page that asked goes away, a page that loads next replays and follows it, and only Stop cancels it', async () => {
|
|
311
|
+
const { app } = await boot()
|
|
312
|
+
let release!: () => void
|
|
313
|
+
const gate = new Promise<void>((resolve) => {
|
|
314
|
+
release = resolve
|
|
315
|
+
})
|
|
316
|
+
let cancelled = false
|
|
317
|
+
const fiber = app.plugin({
|
|
318
|
+
name: 'slow-driver',
|
|
319
|
+
inject: ['drivers'],
|
|
320
|
+
apply(ctx: Context) {
|
|
321
|
+
ctx.effect(() =>
|
|
322
|
+
ctx.drivers.register({
|
|
323
|
+
kind: 'chat',
|
|
324
|
+
name: 'slow',
|
|
325
|
+
async *start({ message, signal }) {
|
|
326
|
+
signal.addEventListener('abort', () => {
|
|
327
|
+
cancelled = true
|
|
328
|
+
})
|
|
329
|
+
yield { event: 'text', data: { text: `start: ${message}` } }
|
|
330
|
+
await Promise.race([
|
|
331
|
+
gate,
|
|
332
|
+
new Promise<void>((resolve) => signal.addEventListener('abort', () => resolve())),
|
|
333
|
+
])
|
|
334
|
+
yield { event: 'text', data: { text: 'finish' } }
|
|
335
|
+
},
|
|
336
|
+
}),
|
|
337
|
+
)
|
|
338
|
+
},
|
|
339
|
+
})
|
|
340
|
+
await fiber.await()
|
|
341
|
+
const { token } = (await (
|
|
342
|
+
await app.router.request('/api/agent-panel/session', { headers: OWN })
|
|
343
|
+
).json()) as { token: string }
|
|
344
|
+
const headers = { 'content-type': 'application/json', ...OWN, 'x-agent-panel-token': token }
|
|
345
|
+
assert.equal(
|
|
346
|
+
(await app.router.request('/api/agent-panel/run', { headers })).headers
|
|
347
|
+
.get('content-type')
|
|
348
|
+
?.includes('json'),
|
|
349
|
+
true,
|
|
350
|
+
'no run yet',
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
// The page that asked reads the first event and goes away.
|
|
354
|
+
const asked = await app.router.request('/api/agent-panel/ask', {
|
|
355
|
+
method: 'POST',
|
|
356
|
+
headers,
|
|
357
|
+
body: JSON.stringify({ message: 'build', driver: 'slow' }),
|
|
358
|
+
})
|
|
359
|
+
const reader = asked.body!.getReader()
|
|
360
|
+
const first = new TextDecoder().decode((await reader.read()).value)
|
|
361
|
+
assert.match(first, /start: build/)
|
|
362
|
+
await reader.cancel()
|
|
363
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
364
|
+
assert.equal(cancelled, false, 'the run went on')
|
|
365
|
+
assert.equal(
|
|
366
|
+
(
|
|
367
|
+
await app.router.request('/api/agent-panel/ask', {
|
|
368
|
+
method: 'POST',
|
|
369
|
+
headers,
|
|
370
|
+
body: JSON.stringify({ message: 'again', driver: 'slow' }),
|
|
371
|
+
})
|
|
372
|
+
).status,
|
|
373
|
+
409,
|
|
374
|
+
'one run at a time',
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
// The next page: the question, the events so far, and the rest.
|
|
378
|
+
const followed = app.router.request('/api/agent-panel/run', { headers })
|
|
379
|
+
const events = eventsOf(await followed)
|
|
380
|
+
release()
|
|
381
|
+
const seen = await events
|
|
382
|
+
assert.deepEqual(
|
|
383
|
+
seen.map((e) => [e.event, e.data.message ?? e.data.text ?? e.data.changed]),
|
|
384
|
+
[
|
|
385
|
+
['run', 'build'],
|
|
386
|
+
['text', 'start: build'],
|
|
387
|
+
['text', 'finish'],
|
|
388
|
+
['done', false],
|
|
389
|
+
],
|
|
390
|
+
)
|
|
391
|
+
assert.equal(seen[0]!.data.ended, false)
|
|
392
|
+
// After it ended, a page that loads gets it again, marked ended, and it does not restart.
|
|
393
|
+
const later = await eventsOf(await app.router.request('/api/agent-panel/run', { headers }))
|
|
394
|
+
assert.equal(later[0]!.data.ended, true)
|
|
395
|
+
assert.deepEqual(
|
|
396
|
+
later.slice(1).map((e) => e.event),
|
|
397
|
+
['text', 'text', 'done'],
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
// Stop cancels.
|
|
401
|
+
const second = await app.router.request('/api/agent-panel/ask', {
|
|
402
|
+
method: 'POST',
|
|
403
|
+
headers,
|
|
404
|
+
body: JSON.stringify({ message: 'stop me', driver: 'slow' }),
|
|
405
|
+
})
|
|
406
|
+
const secondReader = second.body!.getReader()
|
|
407
|
+
await secondReader.read()
|
|
408
|
+
assert.equal(
|
|
409
|
+
(await app.router.request('/api/agent-panel/stop', { method: 'POST', headers })).status,
|
|
410
|
+
200,
|
|
411
|
+
)
|
|
412
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
413
|
+
assert.equal(cancelled, true, 'Stop cancelled the run')
|
|
414
|
+
await secondReader.cancel()
|
|
415
|
+
assert.equal(
|
|
416
|
+
(
|
|
417
|
+
await app.router.request('/api/agent-panel/stop', {
|
|
418
|
+
method: 'POST',
|
|
419
|
+
headers: { 'content-type': 'application/json' },
|
|
420
|
+
})
|
|
421
|
+
).status,
|
|
422
|
+
403,
|
|
423
|
+
)
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
test("as the Assistant every visitor is their own: their own cookie, token, run, Stop and replay, and only the app's agent", async () => {
|
|
427
|
+
const { app } = await boot({}, { panel: { assistant: true } })
|
|
428
|
+
// A tool that holds a run until it is released or stopped, so two runs can go at once.
|
|
429
|
+
const released = new Set<string>()
|
|
430
|
+
const stopped = new Set<string>()
|
|
431
|
+
app.tools.register('hold', {
|
|
432
|
+
description: 'holds the run',
|
|
433
|
+
run: async (input, options) => {
|
|
434
|
+
const who = String((input as { who?: string }).who)
|
|
435
|
+
while (!released.has(who)) {
|
|
436
|
+
if (options?.signal?.aborted) {
|
|
437
|
+
stopped.add(who)
|
|
438
|
+
throw options.signal.reason
|
|
439
|
+
}
|
|
440
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
441
|
+
}
|
|
442
|
+
return `released ${who}`
|
|
443
|
+
},
|
|
444
|
+
})
|
|
445
|
+
scripted(app.model)?.teach(({ question, step, toolResult }) =>
|
|
446
|
+
step === 0
|
|
447
|
+
? { tool: 'hold', input: { who: question } }
|
|
448
|
+
: { text: `answer to ${question}: ${String(toolResult)}` },
|
|
449
|
+
)
|
|
450
|
+
// A plugin's driver is never offered to a visitor, and its endpoints refuse them.
|
|
451
|
+
app.drivers.register({
|
|
452
|
+
kind: 'chat',
|
|
453
|
+
name: 'coder',
|
|
454
|
+
async *start() {
|
|
455
|
+
yield { event: 'text', data: { text: 'coding' } }
|
|
456
|
+
},
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
const visit = async () => {
|
|
460
|
+
const response = await app.router.request('/api/agent-panel/session', { headers: OWN })
|
|
461
|
+
const cookie = (response.headers.get('set-cookie') ?? '').split(';')[0] ?? ''
|
|
462
|
+
const session = (await response.json()) as { token: string; drivers: Array<{ name: string }> }
|
|
463
|
+
const headers = {
|
|
464
|
+
'content-type': 'application/json',
|
|
465
|
+
...OWN,
|
|
466
|
+
cookie,
|
|
467
|
+
'x-agent-panel-token': session.token,
|
|
468
|
+
}
|
|
469
|
+
return { cookie, token: session.token, drivers: session.drivers, headers }
|
|
470
|
+
}
|
|
471
|
+
const a = await visit()
|
|
472
|
+
const b = await visit()
|
|
473
|
+
assert.match(a.cookie, /^acryl-visitor=[\w-]+$/u)
|
|
474
|
+
assert.notEqual(a.cookie, b.cookie)
|
|
475
|
+
assert.notEqual(a.token, b.token)
|
|
476
|
+
assert.deepEqual(
|
|
477
|
+
a.drivers.map((driver) => driver.name),
|
|
478
|
+
['agent'],
|
|
479
|
+
)
|
|
480
|
+
assert.equal(app.drivers.authorized(new Headers(a.headers)), false)
|
|
481
|
+
const ask = (who: typeof a, message: string, driver = 'agent') =>
|
|
482
|
+
app.router.request('/api/agent-panel/ask', {
|
|
483
|
+
method: 'POST',
|
|
484
|
+
headers: who.headers,
|
|
485
|
+
body: JSON.stringify({ message, driver }),
|
|
486
|
+
})
|
|
487
|
+
assert.equal((await ask(a, 'code something', 'coder')).status, 400)
|
|
488
|
+
|
|
489
|
+
// A asks; B asks while A's run goes, and is not refused for it.
|
|
490
|
+
const askedA = await ask(a, 'A')
|
|
491
|
+
assert.equal(askedA.status, 200)
|
|
492
|
+
const askedB = await ask(b, 'B')
|
|
493
|
+
assert.equal(askedB.status, 200)
|
|
494
|
+
// A token works with its own cookie only.
|
|
495
|
+
assert.equal(
|
|
496
|
+
(
|
|
497
|
+
await app.router.request('/api/agent-panel/run', {
|
|
498
|
+
headers: { ...b.headers, 'x-agent-panel-token': a.token },
|
|
499
|
+
})
|
|
500
|
+
).status,
|
|
501
|
+
403,
|
|
502
|
+
)
|
|
503
|
+
// B's Stop stops B's run, not A's.
|
|
504
|
+
assert.equal(
|
|
505
|
+
(await app.router.request('/api/agent-panel/stop', { method: 'POST', headers: b.headers }))
|
|
506
|
+
.status,
|
|
507
|
+
200,
|
|
508
|
+
)
|
|
509
|
+
const eventsB = await eventsOf(askedB)
|
|
510
|
+
// The run ends cancelled at once; the held tool sees its signal at its next check.
|
|
511
|
+
for (let tries = 0; tries < 100 && !stopped.has('B'); tries++)
|
|
512
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
513
|
+
assert.equal(stopped.has('B'), true)
|
|
514
|
+
assert.equal(stopped.has('A'), false)
|
|
515
|
+
assert.equal(
|
|
516
|
+
eventsB.some((e) => String(e.data.text ?? '').includes('answer to')),
|
|
517
|
+
false,
|
|
518
|
+
)
|
|
519
|
+
released.add('A')
|
|
520
|
+
const eventsA = await eventsOf(askedA)
|
|
521
|
+
assert.equal(
|
|
522
|
+
eventsA.some((e) => e.data.text === 'answer to A: released A'),
|
|
523
|
+
true,
|
|
524
|
+
)
|
|
525
|
+
// Each replay is the visitor's own latest run.
|
|
526
|
+
const replayA = await eventsOf(
|
|
527
|
+
await app.router.request('/api/agent-panel/run', { headers: a.headers }),
|
|
528
|
+
)
|
|
529
|
+
const replayB = await eventsOf(
|
|
530
|
+
await app.router.request('/api/agent-panel/run', { headers: b.headers }),
|
|
531
|
+
)
|
|
532
|
+
assert.equal(replayA[0]?.data.message, 'A')
|
|
533
|
+
assert.equal(replayB[0]?.data.message, 'B')
|
|
534
|
+
})
|