fix(ai): require OpenAI Responses terminal events

This commit is contained in:
Mario Zechner
2026-06-23 16:35:45 +02:00
parent 2285f87964
commit cd95c2749f
12 changed files with 378 additions and 39 deletions
@@ -139,12 +139,17 @@ export function calculateContextTokens(usage: Usage): number {
/**
* Get usage from an assistant message if available.
* Skips aborted and error messages as they don't have valid usage data.
* Skips aborted, error, and all-zero usage messages as they don't have valid usage data.
*/
function getAssistantUsage(msg: AgentMessage): Usage | undefined {
if (msg.role === "assistant" && "usage" in msg) {
const assistantMsg = msg as AssistantMessage;
if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
if (
assistantMsg.stopReason !== "aborted" &&
assistantMsg.stopReason !== "error" &&
assistantMsg.usage &&
calculateContextTokens(assistantMsg.usage) > 0
) {
return assistantMsg.usage;
}
}
@@ -152,7 +157,7 @@ function getAssistantUsage(msg: AgentMessage): Usage | undefined {
}
/**
* Find the last non-aborted assistant message usage from session entries.
* Find the last valid assistant message usage from session entries.
*/
export function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined {
for (let i = entries.length - 1; i >= 0; i--) {