feat: add agent cost reporting

This commit is contained in:
2026-07-09 20:29:56 +08:00
parent fc5a5365d8
commit 716101eed0
9 changed files with 275 additions and 7 deletions
+18 -1
View File
@@ -21,11 +21,12 @@ function assistantMessage(text: string) {
};
}
function resultMessage(sessionId: string) {
function resultMessage(sessionId: string, costUsd?: number) {
return {
type: "result",
subtype: "success",
session_id: sessionId,
...(costUsd !== undefined ? { total_cost_usd: costUsd } : {}),
};
}
@@ -103,6 +104,22 @@ describe("runAgent", () => {
expect(call?.options).not.toHaveProperty("resume");
});
it("returns SDK-reported cost when present", async () => {
queryMock.mockReturnValue(messages(assistantMessage("ok"), resultMessage("sdk-session-1", 0.0042)));
const result = await runAgent({
prompt: "算一下成本",
model: undefined,
project: { projectId: "p", boundChatId: "c", workspaceDir: "/tmp/ws" },
systemPrompt: undefined,
runId: "run-1",
sessionId: "hub-session-1",
prisma: stubPrisma,
});
expect(result.costUsd).toBe(0.0042);
});
it("passes provider env per run without mutating process env", async () => {
delete process.env["CPH_TEST_PROVIDER_ENV_MUTATION"];
queryMock.mockReturnValue(messages(assistantMessage("ok"), resultMessage("sdk-session-1")));