Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions packages/agent/src/session/session-additions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -88,21 +89,29 @@ export async function requeue(
mode: "steer" | "followUp",
): Promise<boolean> {
const cleared = session.clearQueue();
const left = { steer: [...cleared.steering], followUp: [...cleared.followUp] };
const back: Input[] = [];
let switched = false;
const tracked = new Map<string, { key: string; input: Input }[]>();
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;
}
60 changes: 60 additions & 0 deletions packages/agent/test/session-additions.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
Loading