Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189// Messages the owner sends while a run goes: the web chat's /send with `queue: true` holds them until// the run's next model request, editable or removable until then, and hands back the ones a stopped// run never took. The agent, its records and a runner are real, on the dev stack. The model is scripted.
import assert from 'node:assert/strict'import crypto from 'node:crypto'import fs from 'node:fs'import net from 'node:net'import os from 'node:os'import path from 'node:path'import { test } from 'node:test'import { createModels, fauxAssistantMessage, fauxProvider, fauxText, fauxToolCall } from '@earendil-works/pi-ai'import { skipWithoutStack } from './stack.ts'
const pds = process.env.LANGUID_PDS ?? 'http://127.0.0.1:2583'const up = await fetch(`${pds}/xrpc/_health`).then((r) => r.ok).catch(() => false)
const freePort = () => new Promise<number>((resolve) => { const s = net.createServer().listen(0, '127.0.0.1', () => { const { port } = s.address() as net.AddressInfo s.close(() => resolve(port)) }) })
test('messages sent during a run wait for its next step, and can be edited or removed until then', { skip: skipWithoutStack(up) }, async (t) => { process.env.LANGUID_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'languid-queue-')) const { loadState } = await import('../src/state.ts') const { createAgent, ensureOwner, deleteAccount } = await import('../src/manager.ts') const { connect, register } = await import('../src/runner.ts') const { createService } = await import('../src/server.ts') const { LanguidClient } = await import('../src/client.ts')
const sfx = crypto.randomBytes(3).toString('hex') const state = loadState() await ensureOwner(state, `qu${sfx}.test`) const entry = await createAgent(state, `q${sfx}`) const alice = { handle: state.owner!.handle, did: state.owner!.did }
const handles = new Map<string, string>() const oauth = { metadata: () => ({ client_id: 'fake' }), authorize: async (handle: string, st: string) => { handles.set(st, handle) return new URL(`https://pds.invalid/oauth/authorize?state=${st}`) }, callback: async (params: URLSearchParams) => { const st = params.get('state') ?? '' if (handles.get(st) !== alice.handle) throw new Error('unknown state') return { did: alice.did, state: st } }, act: async () => { throw new Error('not used') }, forget: async () => {}, }
const faux = fauxProvider({ tokensPerSecond: 200, models: [{ id: 'faux-1' }] }) const models = createModels() models.setProvider(faux.provider) const port = await freePort() const url = `http://127.0.0.1:${port}` const token = crypto.randomBytes(16).toString('hex') const svc = createService({ host: '127.0.0.1', port, token, url, oauth, models: async () => models }) await svc.listen() const stopRunner = new AbortController() t.after(async () => { stopRunner.abort() await svc.close() for (const did of [entry.agent.did, state.owner!.did]) await deleteAccount(pds, did).catch(() => {}) fs.rmSync(process.env.LANGUID_HOME!, { recursive: true, force: true }) })
// A computer, so a bash call waits for the owner's answer: the run holds still while messages queue. const client = new LanguidClient(url, token, entry.name) await client.setModel(faux.provider.id, faux.getModel().id) const invite = await client.inviteComputer([entry.name], 5) const joined = await register(url, invite.code, 'box') void connect({ serviceUrl: url, token: joined.token, signal: stopRunner.signal, log: () => {} }) for (let i = 0; i < 50 && !(await client.listComputers()).find((m) => m.name === 'box')?.online; i++) await new Promise((r) => setTimeout(r, 50))
const started = await fetch(`${url}/oauth/web/start`, { method: 'POST', redirect: 'manual', headers: { 'content-type': 'application/x-www-form-urlencoded', origin: url }, body: new URLSearchParams({ handle: alice.handle }), }) const st = new URL(started.headers.get('location')!).searchParams.get('state')! const flow = started.headers.get('set-cookie')!.split(';')[0] const back = await fetch(`${url}/oauth/callback?state=${st}&code=x`, { redirect: 'manual', headers: { cookie: flow } }) const cookie = back.headers.getSetCookie().find((c) => c.startsWith('languid_session='))!.split(';')[0]
const base = `${url}/agents/${entry.agent.handle}` const post = (p: string, body: unknown) => fetch(`${base}${p}`, { method: 'POST', headers: { cookie, origin: url, 'content-type': 'application/json' }, body: JSON.stringify(body) }) const getRun = async (run: string) => { const r = await fetch(`${base}/runs/${run}?since=0`, { headers: { cookie } }) assert.equal(r.status, 200) return (await r.json()) as any } async function until(run: string, done: (st: any) => boolean) { for (let i = 0; i < 200; i++) { const s = await getRun(run) if (done(s)) return s await new Promise((r) => setTimeout(r, 50)) } throw new Error('timed out waiting for the run') }
// ---- queued, edited, removed, then delivered before the next model request ---- let secondRequest = '' faux.setResponses([ fauxAssistantMessage([fauxToolCall('bash', { call_intention: 'Test the tool', command: 'true' })], { stopReason: 'toolUse' }), (context) => { secondRequest = JSON.stringify(context) return fauxAssistantMessage([fauxText('Got it.')]) }, ]) const first = (await (await post('/send', { text: 'start', computer: 'box' })).json()) as any const { run, thread } = first const waiting = await until(run, (s) => s.events.some((e: any) => e.type === 'approval')) const ask = waiting.events.find((e: any) => e.type === 'approval')
const q1 = (await (await post('/send', { text: 'also this', thread, queue: true })).json()) as any assert.equal(q1.accepted, 'queued') assert.equal(q1.run, run) assert.ok(q1.queued, 'the queued message has an id to edit it by') const q2 = (await (await post('/send', { text: 'never mind', thread, queue: true })).json()) as any assert.equal(q2.accepted, 'queued') assert.notEqual(q2.queued, q1.queued) assert.deepEqual((await getRun(run)).queued, [ { id: q1.queued, text: 'also this', images: 0 }, { id: q2.queued, text: 'never mind', images: 0 }, ])
const edited = await post('/queued', { thread, id: q1.queued, text: ' also this, edited ' }) assert.equal(edited.status, 200) assert.deepEqual(await edited.json(), { id: q1.queued, text: 'also this, edited' }) const removed = await post('/queued', { thread, id: q2.queued, remove: true }) assert.equal(removed.status, 200) assert.deepEqual(await removed.json(), { removed: true }) assert.equal((await post('/queued', { thread, id: q2.queued, text: 'back' })).status, 409, 'a removed message is gone') assert.equal((await post('/queued', { thread })).status, 400) assert.deepEqual((await getRun(run)).queued, [{ id: q1.queued, text: 'also this, edited', images: 0 }])
assert.equal((await post(`/runs/${run}/approve`, { id: ask.id, approved: true })).status, 200) const done = await until(run, (s) => s.status !== 'running') assert.equal(done.status, 'done') const sentEvents = done.events.filter((e: any) => e.type === 'queued_sent') assert.equal(sentEvents.length, 1) assert.equal(sentEvents[0].id, q1.queued) assert.equal(sentEvents[0].text, 'also this, edited') assert.ok(sentEvents[0].message, 'the record it was written as') // Written before the model's next request, after the tool result. const toolDone = done.events.findIndex((e: any) => e.type === 'decision') assert.ok(done.events.indexOf(sentEvents[0]) > toolDone) assert.match(secondRequest, /also this, edited/) assert.doesNotMatch(secondRequest, /never mind/) assert.equal(done.queued, undefined, 'nothing waits once the run is done') assert.equal(done.unsent, undefined) assert.equal((await post('/queued', { thread, id: q1.queued, text: 'too late' })).status, 409, 'in the thread already')
// Not queued: with no run, `queue` changes nothing and the message starts one. faux.setResponses([fauxAssistantMessage([fauxText('Hello.')])]) const plain = (await (await post('/send', { text: 'hi', thread, queue: true })).json()) as any assert.equal(plain.accepted, 'started') assert.equal(plain.queued, undefined) await until(plain.run, (s) => s.status !== 'running')
// ---- Stop: what the run never took comes back as unsent ---- faux.setResponses([fauxAssistantMessage([fauxToolCall('bash', { call_intention: 'Test the tool', command: 'true' })], { stopReason: 'toolUse' }), fauxAssistantMessage([fauxText('never')])]) const third = (await (await post('/send', { text: 'one more', thread, computer: 'box' })).json()) as any await until(third.run, (s) => s.events.some((e: any) => e.type === 'approval')) const left = (await (await post('/send', { text: 'left behind', thread, queue: true })).json()) as any assert.equal(left.accepted, 'queued') assert.equal((await post(`/runs/${third.run}/stop`, {})).status, 200) const stopped = await until(third.run, (s) => s.status !== 'running') assert.equal(stopped.stopped, true) assert.deepEqual(stopped.unsent, [{ id: left.queued, text: 'left behind' }]) assert.equal(stopped.events.some((e: any) => e.type === 'queued_sent'), false) assert.equal(stopped.queued, undefined)})
test('the chat script, with its queue, parses', async () => { const { CHAT_JS } = await import('../src/web.ts') assert.doesNotThrow(() => new Function(CHAT_JS)) assert.match(CHAT_JS, /queue: true/)})