Skip to content
Closed
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
28 changes: 19 additions & 9 deletions packages/agent/src/session/session-additions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ type Queue = {
* 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.
* text a skill command expanded) goes back to its own queue unchanged. Each queue is restored in
* the order clearQueue returned it, and the switched input joins its new queue after the inputs
* already there.
*/
export async function requeue(
additions: SessionAdditions,
Expand All @@ -88,21 +90,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[] = [];
// A tracked entry keeps its native slot; an unmatched entry stays where it was; the
// switched entry joins its new queue last.
const lists = {
steer: cleared.steering.map((text) => ({ text })),
followUp: cleared.followUp.map((text) => ({ text })),
};
let switched = false;
let moved: Input | undefined;
for (const [key, input] of additions.queued) {
const at = left[input.mode].indexOf(input.text);
const list = lists[input.mode];
const at = list.findIndex((entry) => entry.text === input.text);
if (at < 0) continue;
left[input.mode].splice(at, 1);
if (key === id) {
list.splice(at, 1);
input.mode = mode;
switched = true;
moved = input;
} else {
list[at] = input;
}
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);
if (moved) lists[mode].push(moved);
for (const input of lists.steer) await session.steer(input.text, input.images);
for (const input of lists.followUp) await session.followUp(input.text, input.images);
return switched;
}
68 changes: 68 additions & 0 deletions packages/agent/test/session-additions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { ImageContent } from "@earendil-works/pi-ai";
import { describe, expect, it } from "vitest";
import { requeue, SessionAdditions } from "../src/session/session-additions.ts";

type Call = { mode: "steer" | "followUp"; text: string; images?: ImageContent[] };

type TrackedInput = { text: string; mode: "steer" | "followUp"; images?: ImageContent[] };

function fakeQueue(steering: string[], followUp: string[]) {
const calls: Call[] = [];
return {
calls,
session: {
clearQueue: () => ({ steering: [...steering], followUp: [...followUp] }),
steer: async (text: string, images?: ImageContent[]) => {
calls.push({ mode: "steer", text, ...(images ? { images } : {}) });
},
followUp: async (text: string, images?: ImageContent[]) => {
calls.push({ mode: "followUp", text, ...(images ? { images } : {}) });
},
},
};
}

function tracked(entries: [string, TrackedInput][]) {
const additions = new SessionAdditions();
for (const [id, input] of entries) additions.queued.set(id, input);
return additions;
}

function order(calls: Call[]) {
return calls.map((call) => `${call.mode}:${call.text}`);
}

describe("requeue", () => {
it("keeps each queue in native order and puts the switched input last", async () => {
const { calls, session } = fakeQueue(["first", "sdk-only", "second"], ["later"]);
const images = [{} as ImageContent];
const additions = tracked([
["second-id", { text: "second", mode: "steer" }],
["first-id", { text: "first", mode: "steer", images }],
["later-id", { text: "later", mode: "followUp" }],
]);
expect(await requeue(additions, session, "later-id", "steer")).toBe(true);
expect(order(calls)).toEqual(["steer:first", "steer:sdk-only", "steer:second", "steer:later"]);
expect(calls.find((call) => call.text === "first")?.images).toBe(images);
});

it("leaves a queue in native order when nothing switches", async () => {
const { calls, session } = fakeQueue(["b", "a"], []);
const additions = tracked([
["a-id", { text: "a", mode: "steer" }],
["b-id", { text: "b", mode: "steer" }],
]);
expect(await requeue(additions, session, "missing-id", "followUp")).toBe(false);
expect(order(calls)).toEqual(["steer:b", "steer:a"]);
});

it("drops an input pi already took and reports switching it as false", async () => {
const { calls, session } = fakeQueue(["kept"], []);
const additions = tracked([
["kept-id", { text: "kept", mode: "steer" }],
["gone-id", { text: "gone", mode: "steer" }],
]);
expect(await requeue(additions, session, "gone-id", "followUp")).toBe(false);
expect(order(calls)).toEqual(["steer:kept"]);
});
});
Loading