mirror of https://github.com/openclaw/openclaw.git
Agents: normalize WS usage aliases
This commit is contained in:
parent
9367379771
commit
d166f2648e
|
|
@ -10,6 +10,7 @@ import type {
|
|||
} from "./openai-ws-connection.js";
|
||||
import { normalizeToolParameterSchema } from "./pi-tools.schema.js";
|
||||
import { buildAssistantMessage, buildUsageWithNoCost } from "./stream-message-shared.js";
|
||||
import { normalizeUsage } from "./usage.js";
|
||||
|
||||
type AnyMessage = Message & { role: string; content: unknown };
|
||||
type AssistantMessageWithPhase = AssistantMessage & { phase?: OpenAIResponsesAssistantPhase };
|
||||
|
|
@ -538,15 +539,16 @@ export function buildAssistantMessageFromResponse(
|
|||
|
||||
const hasToolCalls = content.some((part) => part.type === "toolCall");
|
||||
const stopReason: StopReason = hasToolCalls ? "toolUse" : "stop";
|
||||
const normalizedUsage = normalizeUsage(response.usage);
|
||||
|
||||
const message = buildAssistantMessage({
|
||||
model: modelInfo,
|
||||
content,
|
||||
stopReason,
|
||||
usage: buildUsageWithNoCost({
|
||||
input: response.usage?.input_tokens ?? 0,
|
||||
output: response.usage?.output_tokens ?? 0,
|
||||
totalTokens: response.usage?.total_tokens ?? 0,
|
||||
input: normalizedUsage?.input ?? 0,
|
||||
output: normalizedUsage?.output ?? 0,
|
||||
totalTokens: normalizedUsage?.total ?? response.usage?.total_tokens ?? 0,
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -860,6 +860,20 @@ describe("buildAssistantMessageFromResponse", () => {
|
|||
expect(msg.usage.totalTokens).toBe(150);
|
||||
});
|
||||
|
||||
it("maps prompt_tokens and completion_tokens usage aliases", () => {
|
||||
const response = makeResponseObject("resp_5b", "Hello");
|
||||
(response as unknown as { usage?: Record<string, number> }).usage = {
|
||||
prompt_tokens: 44,
|
||||
completion_tokens: 11,
|
||||
total_tokens: 55,
|
||||
};
|
||||
|
||||
const msg = buildAssistantMessageFromResponse(response, modelInfo);
|
||||
expect(msg.usage.input).toBe(44);
|
||||
expect(msg.usage.output).toBe(11);
|
||||
expect(msg.usage.totalTokens).toBe(55);
|
||||
});
|
||||
|
||||
it("sets model/provider/api from modelInfo", () => {
|
||||
const response = makeResponseObject("resp_6", "Hi");
|
||||
const msg = buildAssistantMessageFromResponse(response, modelInfo);
|
||||
|
|
|
|||
Loading…
Reference in New Issue