fix: stream tool summaries early and tool output
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
createToolDebouncer,
|
||||
formatToolAggregate,
|
||||
formatToolPrefix,
|
||||
shortenMeta,
|
||||
@@ -48,37 +47,3 @@ describe("tool meta formatting", () => {
|
||||
expect(formatToolPrefix("x", "/Users/test/a.txt")).toBe("🧩 x: ~/a.txt");
|
||||
});
|
||||
});
|
||||
|
||||
describe("tool meta debouncer", () => {
|
||||
it("flushes on timer and when tool changes", () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const calls: Array<{ tool: string | undefined; metas: string[] }> = [];
|
||||
const d = createToolDebouncer((tool, metas) => {
|
||||
calls.push({ tool, metas });
|
||||
}, 50);
|
||||
|
||||
d.push("a", "/tmp/1");
|
||||
d.push("a", "/tmp/2");
|
||||
expect(calls).toHaveLength(0);
|
||||
|
||||
vi.advanceTimersByTime(60);
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]).toMatchObject({
|
||||
tool: "a",
|
||||
metas: ["/tmp/1", "/tmp/2"],
|
||||
});
|
||||
|
||||
d.push("a", "x");
|
||||
d.push("b", "y"); // tool change flushes immediately
|
||||
expect(calls).toHaveLength(2);
|
||||
expect(calls[1]).toMatchObject({ tool: "a", metas: ["x"] });
|
||||
|
||||
vi.advanceTimersByTime(60);
|
||||
expect(calls).toHaveLength(3);
|
||||
expect(calls[2]).toMatchObject({ tool: "b", metas: ["y"] });
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,9 +4,6 @@ import {
|
||||
} from "../agents/tool-display.js";
|
||||
import { shortenHomeInString, shortenHomePath } from "../utils.js";
|
||||
|
||||
export const TOOL_RESULT_DEBOUNCE_MS = 500;
|
||||
export const TOOL_RESULT_FLUSH_COUNT = 5;
|
||||
|
||||
export function shortenPath(p: string): string {
|
||||
return shortenHomePath(p);
|
||||
}
|
||||
@@ -77,33 +74,3 @@ function isPathLike(value: string): boolean {
|
||||
if (value.includes("&&") || value.includes("||")) return false;
|
||||
return /^~?(\/[^\s]+)+$/.test(value);
|
||||
}
|
||||
|
||||
export function createToolDebouncer(
|
||||
onFlush: (toolName: string | undefined, metas: string[]) => void,
|
||||
windowMs = TOOL_RESULT_DEBOUNCE_MS,
|
||||
) {
|
||||
let pendingTool: string | undefined;
|
||||
let pendingMetas: string[] = [];
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
|
||||
const flush = () => {
|
||||
if (!pendingTool && pendingMetas.length === 0) return;
|
||||
onFlush(pendingTool, pendingMetas);
|
||||
pendingTool = undefined;
|
||||
pendingMetas = [];
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const push = (toolName?: string, meta?: string) => {
|
||||
if (pendingTool && toolName && pendingTool !== toolName) flush();
|
||||
if (!pendingTool) pendingTool = toolName;
|
||||
if (meta) pendingMetas.push(meta);
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(flush, windowMs);
|
||||
};
|
||||
|
||||
return { push, flush };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user