diff --git a/packages/agent/src/session/session-additions.ts b/packages/agent/src/session/session-additions.ts index 71c6e3a..e561500 100644 --- a/packages/agent/src/session/session-additions.ts +++ b/packages/agent/src/session/session-additions.ts @@ -77,9 +77,10 @@ type Queue = { /** * Switches queued input `id` between "add to the current task" and "after the current task". pi * keeps no per-item removal, so its queue is taken out and put back in order: only inputs still in - * it go back; one pi already took (in flight, not yet announced) is in neither list, is not added - * twice, and switching it reports false. Anything pi held that is not matched (another source, or - * text a skill command expanded) goes back to its own queue unchanged. + * it go back, and the switched input joins the end of its destination queue. One pi already took + * (in flight, not yet announced) is in neither list, is not added twice, and switching it reports + * false. Anything pi held that is not matched (another source, or text a skill command expanded) + * goes back to its own queue unchanged. */ export async function requeue( additions: SessionAdditions, @@ -88,21 +89,29 @@ export async function requeue( mode: "steer" | "followUp", ): Promise { const cleared = session.clearQueue(); - const left = { steer: [...cleared.steering], followUp: [...cleared.followUp] }; - const back: Input[] = []; - let switched = false; + const tracked = new Map(); for (const [key, input] of additions.queued) { - const at = left[input.mode].indexOf(input.text); - if (at < 0) continue; - left[input.mode].splice(at, 1); - if (key === id) { - input.mode = mode; - switched = true; + const lookup = `${input.mode}:${input.text}`; + const matches = tracked.get(lookup); + if (matches) matches.push({ key, input }); + else tracked.set(lookup, [{ key, input }]); + } + let moved: Input | undefined; + for (const [queue, texts] of [ + ["steer", cleared.steering], + ["followUp", cleared.followUp], + ] as const) { + for (const text of texts) { + const match = tracked.get(`${queue}:${text}`)?.shift(); + if (match?.key === id) { + moved = match.input; + continue; + } + await session[queue](text, match?.input.images); } - back.push(input); } - for (const text of left.steer) await session.steer(text); - for (const text of left.followUp) await session.followUp(text); - for (const input of back) await session[input.mode](input.text, input.images); - return switched; + if (!moved) return false; + await session[mode](moved.text, moved.images); + moved.mode = mode; + return true; } diff --git a/packages/agent/test/session-additions.test.ts b/packages/agent/test/session-additions.test.ts new file mode 100644 index 0000000..72193b6 --- /dev/null +++ b/packages/agent/test/session-additions.test.ts @@ -0,0 +1,60 @@ +import type { ImageContent } from "@earendil-works/pi-ai"; +import { expect, it, vi } from "vitest"; +import { requeue, SessionAdditions } from "../src/session/session-additions.ts"; + +it("keeps the SDK delivery order when moving one queued input", async () => { + const additions = new SessionAdditions(); + const images: ImageContent[] = [{ type: "image", data: "AA==", mimeType: "image/png" }]; + additions.queued.set("second", { text: "second", mode: "steer" }); + additions.queued.set("moved", { text: "move me", mode: "followUp", images }); + additions.queued.set("first", { text: "first", mode: "steer" }); + additions.queued.set("in-flight", { text: "already consumed", mode: "followUp" }); + const sdk = { + clearQueue: vi.fn(() => ({ + steering: ["first", "sdk input", "second"], + followUp: ["stay", "move me", "sdk follow-up"], + })), + steer: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + followUp: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + }; + + expect(await requeue(additions, sdk, "moved", "steer")).toBe(true); + expect(sdk.steer.mock.calls.map(([text]) => text)).toEqual([ + "first", + "sdk input", + "second", + "move me", + ]); + expect(sdk.steer).toHaveBeenLastCalledWith("move me", images); + expect(sdk.followUp.mock.calls.map(([text]) => text)).toEqual(["stay", "sdk follow-up"]); + expect(additions.queued.get("moved")?.mode).toBe("steer"); +}); + +it("matches duplicate queued text in order and keeps each input's images", async () => { + const additions = new SessionAdditions(); + const firstImages: ImageContent[] = [{ type: "image", data: "AA==", mimeType: "image/png" }]; + const secondImages: ImageContent[] = [{ type: "image", data: "AQ==", mimeType: "image/png" }]; + additions.queued.set("first", { text: "continue", mode: "steer", images: firstImages }); + additions.queued.set("second", { text: "continue", mode: "steer", images: secondImages }); + const sdk = { + clearQueue: vi.fn(() => ({ + steering: ["continue", "other", "continue", "continue"], + followUp: ["continue"], + })), + steer: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + followUp: vi.fn(async (_text: string, _images?: ImageContent[]) => undefined), + }; + + expect(await requeue(additions, sdk, "first", "followUp")).toBe(true); + expect(sdk.steer.mock.calls).toEqual([ + ["other", undefined], + ["continue", secondImages], + ["continue", undefined], + ]); + expect(sdk.followUp.mock.calls).toEqual([ + ["continue", undefined], + ["continue", firstImages], + ]); + expect(additions.queued.get("first")?.mode).toBe("followUp"); + expect(additions.queued.get("second")?.mode).toBe("steer"); +});