mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-27 05:41:23 +00:00
feat(opencode): show round usage and tokens per second (#1668)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { convertAgentMessage } from './messageConverter';
|
||||
|
||||
describe('convertAgentMessage round summary', () => {
|
||||
it('converts the generic carrier into the HAPI-owned round-summary wire contract', () => {
|
||||
const converted = convertAgentMessage({
|
||||
type: 'round_summary',
|
||||
summary: {
|
||||
usage: { inputTokens: 120, outputTokens: 30, cacheReadInputTokens: 90, cacheCreationInputTokens: 10 },
|
||||
modelUsage: {
|
||||
'openai/gpt-5.4': { inputTokens: 120, outputTokens: 30, cacheReadInputTokens: 90, cacheCreationInputTokens: 10 }
|
||||
},
|
||||
totalCostUsd: 0.034,
|
||||
numTurns: 2,
|
||||
durationMs: 1_250,
|
||||
generationMs: 500,
|
||||
generationOutputTokens: 25
|
||||
}
|
||||
} as never);
|
||||
|
||||
expect(converted).toEqual({
|
||||
type: 'round-summary',
|
||||
summary: {
|
||||
usage: { input_tokens: 120, output_tokens: 30, cache_read_input_tokens: 90, cache_creation_input_tokens: 10 },
|
||||
modelUsage: {
|
||||
'openai/gpt-5.4': { inputTokens: 120, outputTokens: 30, cacheReadInputTokens: 90, cacheCreationInputTokens: 10 }
|
||||
},
|
||||
total_cost_usd: 0.034,
|
||||
num_turns: 2,
|
||||
duration_ms: 1_250,
|
||||
generation_ms: 500,
|
||||
generation_output_tokens: 25
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -42,6 +42,18 @@ export type CodexMessage =
|
||||
}
|
||||
| { type: 'plan'; entries: PlanItem[] }
|
||||
| { type: 'error'; message: string }
|
||||
| {
|
||||
type: 'round-summary';
|
||||
summary: {
|
||||
usage: { input_tokens: number; output_tokens: number; cache_read_input_tokens: number; cache_creation_input_tokens: number };
|
||||
modelUsage: Record<string, { inputTokens: number; outputTokens: number; cacheReadInputTokens: number; cacheCreationInputTokens: number }>;
|
||||
total_cost_usd?: number;
|
||||
num_turns: number;
|
||||
duration_ms: number;
|
||||
generation_ms?: number;
|
||||
generation_output_tokens?: number;
|
||||
};
|
||||
}
|
||||
| {
|
||||
type: 'generated-image';
|
||||
imageId: string;
|
||||
@@ -121,6 +133,24 @@ export function convertAgentMessage(message: AgentMessage, model?: string | null
|
||||
type: 'plan',
|
||||
entries: message.items
|
||||
};
|
||||
case 'round_summary':
|
||||
return {
|
||||
type: 'round-summary',
|
||||
summary: {
|
||||
usage: {
|
||||
input_tokens: message.summary.usage.inputTokens,
|
||||
output_tokens: message.summary.usage.outputTokens,
|
||||
cache_read_input_tokens: message.summary.usage.cacheReadInputTokens,
|
||||
cache_creation_input_tokens: message.summary.usage.cacheCreationInputTokens
|
||||
},
|
||||
modelUsage: message.summary.modelUsage,
|
||||
...(message.summary.totalCostUsd !== undefined ? { total_cost_usd: message.summary.totalCostUsd } : {}),
|
||||
num_turns: message.summary.numTurns,
|
||||
duration_ms: message.summary.durationMs,
|
||||
...(message.summary.generationMs !== undefined && message.summary.generationOutputTokens !== undefined
|
||||
? { generation_ms: message.summary.generationMs, generation_output_tokens: message.summary.generationOutputTokens } : {})
|
||||
}
|
||||
};
|
||||
case 'generated_image':
|
||||
return {
|
||||
type: 'generated-image',
|
||||
|
||||
@@ -29,6 +29,24 @@ export type PlanItem = {
|
||||
status: 'pending' | 'in_progress' | 'completed';
|
||||
};
|
||||
|
||||
export type AgentRoundModelUsage = {
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
cacheReadInputTokens: number;
|
||||
cacheCreationInputTokens: number;
|
||||
};
|
||||
|
||||
export type AgentRoundSummary = {
|
||||
usage: AgentRoundModelUsage;
|
||||
modelUsage: Record<string, AgentRoundModelUsage>;
|
||||
totalCostUsd?: number;
|
||||
numTurns: number;
|
||||
durationMs: number;
|
||||
/** Assistant output + reasoning and provider-active time, excluding tools/compaction. */
|
||||
generationMs?: number;
|
||||
generationOutputTokens?: number;
|
||||
};
|
||||
|
||||
export type AgentMessage =
|
||||
| { type: 'text'; text: string; id?: string; live?: boolean; streamSnapshot?: boolean }
|
||||
| { type: 'reasoning'; text: string; id?: string; live?: boolean }
|
||||
@@ -57,6 +75,7 @@ export type AgentMessage =
|
||||
| { type: 'plan'; items: PlanItem[] }
|
||||
| { type: 'generated_image'; imageId: string; fileName: string; mimeType: string; source?: InlineMediaSource }
|
||||
| { type: 'turn_complete'; stopReason: string }
|
||||
| { type: 'round_summary'; summary: AgentRoundSummary }
|
||||
| { type: 'error'; message: string };
|
||||
|
||||
export type PermissionOption = {
|
||||
|
||||
Reference in New Issue
Block a user