fix: persist usage from rpc
This commit is contained in:
@@ -57,4 +57,15 @@ describe("pi agent helpers", () => {
|
||||
expect(tool?.toolName).toBe("bash");
|
||||
expect(tool?.meta).toBe("ls -la");
|
||||
});
|
||||
|
||||
it("keeps usage meta even when assistant message has no text", () => {
|
||||
const stdout = [
|
||||
'{"type":"message_start","message":{"role":"assistant"}}',
|
||||
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"thinking","thinking":"hmm"}],"usage":{"input":10,"output":5},"model":"pi-1","provider":"inflection","stopReason":"end"}}',
|
||||
].join("\n");
|
||||
const parsed = piSpec.parseOutput(stdout);
|
||||
expect(parsed.texts?.length ?? 0).toBe(0);
|
||||
expect((parsed.meta?.usage as { input?: number })?.input).toBe(10);
|
||||
expect(parsed.meta?.model).toBe("pi-1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -95,10 +95,11 @@ function parsePiJson(raw: string): AgentParseResult {
|
||||
.trim();
|
||||
|
||||
if (isAssistantMessage) {
|
||||
// Always remember the last assistant message so we keep usage/model meta even when no text.
|
||||
lastAssistant = msg;
|
||||
if (msgText && msgText !== lastPushed) {
|
||||
texts.push(msgText);
|
||||
lastPushed = msgText;
|
||||
lastAssistant = msg;
|
||||
}
|
||||
} else if (isToolResult && msg.content) {
|
||||
const toolText = msg.content
|
||||
@@ -119,15 +120,14 @@ function parsePiJson(raw: string): AgentParseResult {
|
||||
}
|
||||
}
|
||||
|
||||
const meta: AgentMeta | undefined =
|
||||
lastAssistant && texts.length
|
||||
? {
|
||||
model: lastAssistant.model,
|
||||
provider: lastAssistant.provider,
|
||||
stopReason: lastAssistant.stopReason,
|
||||
usage: lastAssistant.usage,
|
||||
}
|
||||
: undefined;
|
||||
const meta: AgentMeta | undefined = lastAssistant
|
||||
? {
|
||||
model: lastAssistant.model,
|
||||
provider: lastAssistant.provider,
|
||||
stopReason: lastAssistant.stopReason,
|
||||
usage: lastAssistant.usage,
|
||||
}
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
texts,
|
||||
|
||||
Reference in New Issue
Block a user