2fd3868401
* add usage info to branch summary entries * add usage to compaction entries * allow custom tools to report llm usage in tool results * allow observing and patching usage in tool_result hooks * agent-harness: save usage in entries for compaction, branch summaries and tool results
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { createAssistantMessageEventStream } from "@earendil-works/pi-ai";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { assistantMsg, userMsg } from "../../utilities.ts";
|
|
import { createHarness, type Harness } from "../harness.ts";
|
|
|
|
describe("issue #6324 branch summary ambient auth", () => {
|
|
const harnesses: Harness[] = [];
|
|
|
|
afterEach(() => {
|
|
while (harnesses.length > 0) {
|
|
harnesses.pop()?.cleanup();
|
|
}
|
|
});
|
|
|
|
it("summarizes tree branches when request auth has no API key", async () => {
|
|
const harness = await createHarness({ withConfiguredAuth: false });
|
|
harnesses.push(harness);
|
|
|
|
let streamCallCount = 0;
|
|
harness.session.agent.streamFn = (model, _context, options) => {
|
|
streamCallCount++;
|
|
expect(options?.apiKey).toBeUndefined();
|
|
|
|
const stream = createAssistantMessageEventStream();
|
|
stream.push({
|
|
type: "done",
|
|
reason: "stop",
|
|
message: {
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "branch summary text" }],
|
|
api: model.api,
|
|
provider: model.provider,
|
|
model: model.id,
|
|
usage: {
|
|
input: 1,
|
|
output: 1,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
totalTokens: 2,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0.25 },
|
|
},
|
|
stopReason: "stop",
|
|
timestamp: Date.now(),
|
|
},
|
|
});
|
|
return stream;
|
|
};
|
|
|
|
const targetId = harness.sessionManager.appendMessage(userMsg("first branch"));
|
|
harness.sessionManager.appendMessage(assistantMsg("first reply"));
|
|
harness.sessionManager.appendMessage(userMsg("abandoned branch work"));
|
|
harness.sessionManager.appendMessage(assistantMsg("abandoned reply"));
|
|
|
|
const result = await harness.session.navigateTree(targetId, { summarize: true });
|
|
|
|
expect(result.cancelled).toBe(false);
|
|
expect(streamCallCount).toBe(1);
|
|
expect(result.summaryEntry?.type).toBe("branch_summary");
|
|
expect(result.summaryEntry?.summary).toContain("branch summary text");
|
|
expect(result.summaryEntry?.usage?.cost.total).toBe(0.25);
|
|
});
|
|
});
|