Skip to content

armgit / fx0

Public Imported from GitHub
0

A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.

TypeScript

Send Stop to libfx even when no running turn is on record

Pranit97a388fe9d85
diff --git a/components/chat-app.tsx b/components/chat-app.tsx
index 2cdfaa5..036a2b9 100644
--- a/components/chat-app.tsx
+++ b/components/chat-app.tsx
@@ -1428,7 +1428,8 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod
try {
const response = await fetchUi(`/api/cancel?session=${encodeURIComponent(sessionId)}`, { method: 'POST' })
if (!response.ok) throw new Error(`Stop request failed with ${response.status}`)
- setInteractionNotice({ tone: 'status', text: 'Stop requested.' })
+ const { cancelled } = (await response.json()) as { cancelled?: boolean }
+ setInteractionNotice({ tone: 'status', text: cancelled ? 'Stop requested.' : 'There\'s no reply to stop.' })
} catch (error) {
if (error instanceof AuthRouteError) return
console.error('[fx0] stop failed', { sessionId, error })
diff --git a/lib/agent.ts b/lib/agent.ts
index 18fbc65..1f13cc9 100644
--- a/lib/agent.ts
+++ b/lib/agent.ts
@@ -275,11 +275,14 @@ export function followChat(host: SessionState, { idleMs = 20_000, signal }: { id
return follow
}
+// libfx knows which turn is open; fx0's running marker can lag it or outlive its
+// TTL. So a cancel always goes to libfx, which stops the turn open when it lands
+// and ignores it when none is. False only for a chat no prompt has reached libfx for.
export async function cancelCurrent(host: SessionState): Promise<boolean> {
const [active, stored] = await Promise.all([getActiveTurn(host.id), readSession(host.id)])
- if (!active || !stored?.libfxSessionId) return false
+ if (!stored?.libfxSessionId) return false
await agent().session(stored.libfxSessionId).cancel()
- log(host.id, 'turn:cancel', { turn: active.id })
+ log(host.id, 'turn:cancel', { turn: active?.id ?? null })
return true
}
diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjs
index 8670df0..a918476 100644
--- a/tests/guest-ui.test.mjs
+++ b/tests/guest-ui.test.mjs
@@ -247,6 +247,29 @@ test('an exhausted trial says so briefly and its Sign in opens the dialog', asyn
assert.equal(app.dialog().open, true)
})
+test('stop says when there was no reply to stop, rather than claiming a stop was requested', async (t) => {
+ t.mock.method(console, 'info', () => {})
+ let answer = { cancelled: false }
+ const ui = browser(t, {
+ '/api/events?session=trial-chat&after=2': () => new Response(''),
+ '/api/cancel?session=trial-chat': () => Response.json(answer),
+ })
+ const app = mount(t, ui, {
+ ...guestBoot, user: trialUser, sessionId: 'trial-chat', freePrompts: 4,
+ events: [{ type: 'user', seq: 1, id: 1, text: 'Earlier prompt' }, { type: 'text', seq: 2, id: 1, delta: 'Earlier answer' }],
+ })
+ const announced = async () => {
+ find(app.tree(), (node) => node.type === Composer).props.onStop()
+ await tick()
+ app.render()
+ return find(app.tree(), (node) => node.props?.role === 'status' && node.props['aria-live'] === 'polite').props.children
+ }
+ assert.equal(await announced(), 'There\'s no reply to stop.')
+ answer = { cancelled: true }
+ assert.equal(await announced(), 'Stop requested.')
+ assert.equal(ui.requests.filter(({ url, init }) => url === '/api/cancel?session=trial-chat' && init?.method === 'POST').length, 2)
+})
+
test('signed-in non-trial model settings explain team billing rather than claiming a free prompt', async (t) => {
const ui = browser(t)
const app = mount(t, ui, { ...guestBoot, user: { id: 'user-1', name: 'User', email: null, picture: null }, sessionId: 'draft', freePrompts: 5 })
diff --git a/tests/runtime.test.mjs b/tests/runtime.test.mjs
index 94b8d43..20826f8 100644
--- a/tests/runtime.test.mjs
+++ b/tests/runtime.test.mjs
@@ -466,13 +466,19 @@ test('ask_user_question waits for the open question\'s answer and rejects answer
assert.equal(f.events.at(-1).cancelled, true)
})
-test('cancel stops the running libfx turn, and does nothing when the chat is idle', async (t) => {
+test('cancel always reaches libfx, even when fx0 has no running turn on record', async (t) => {
const f = fixture(t, { libfxSessionId: 'wrun_9' })
const host = boundHost('cancel-chat')
- assert.equal(await runtime.cancelCurrent(host), false)
+ // No marker: its turn_start may not be projected yet, or the turn outlived the
+ // marker's TTL. libfx stops whatever turn is open, and ignores it if none is.
+ assert.equal(await runtime.cancelCurrent(host), true)
f.setActive({ id: 3 })
assert.equal(await runtime.cancelCurrent(host), true)
- assert.deepEqual(f.calls.filter((call) => call[0] === 'cancel'), [['cancel', 'wrun_9']])
+ assert.deepEqual(f.calls.filter((call) => call[0] === 'cancel'), [['cancel', 'wrun_9'], ['cancel', 'wrun_9']])
+ // A chat no prompt has reached libfx for has nothing to cancel.
+ f.stored = { ...f.stored, libfxSessionId: null }
+ assert.equal(await runtime.cancelCurrent(host), false)
+ assert.equal(f.calls.filter((call) => call[0] === 'cancel').length, 2)
})
test('sign-out drops only this login\'s chat credentials and cancels no turn', async (t) => {