forked from EduCraft/curriculum-project-hub
feat: add agent cost reporting
This commit is contained in:
@@ -73,6 +73,8 @@ export interface RunResult {
|
||||
readonly status: RunStatus;
|
||||
readonly text: string;
|
||||
readonly usage: { inputTokens: number; outputTokens: number };
|
||||
/** Provider- or gateway-reported cost for this run, in USD. */
|
||||
readonly costUsd?: number | undefined;
|
||||
readonly numTurns: number;
|
||||
readonly sdkSessionId?: string | undefined;
|
||||
readonly error?: string;
|
||||
@@ -123,6 +125,7 @@ export async function runAgent(req: RunRequest): Promise<RunResult> {
|
||||
|
||||
let fullText = "";
|
||||
let usage = { inputTokens: 0, outputTokens: 0 };
|
||||
let costUsd: number | undefined;
|
||||
let numTurns = 0;
|
||||
let sdkSessionId: string | undefined;
|
||||
let error: string | undefined;
|
||||
@@ -248,6 +251,7 @@ export async function runAgent(req: RunRequest): Promise<RunResult> {
|
||||
case "result": {
|
||||
const result = message as SDKResultMessage;
|
||||
sdkSessionId = result.session_id;
|
||||
costUsd = Number.isFinite(result.total_cost_usd) ? result.total_cost_usd : undefined;
|
||||
if (result.subtype !== "success") {
|
||||
error = `result_${result.subtype}`;
|
||||
}
|
||||
@@ -261,6 +265,7 @@ export async function runAgent(req: RunRequest): Promise<RunResult> {
|
||||
status: error !== undefined ? "failed" : "completed",
|
||||
text: fullText,
|
||||
usage,
|
||||
...(costUsd !== undefined ? { costUsd } : {}),
|
||||
numTurns,
|
||||
sdkSessionId,
|
||||
...(error !== undefined ? { error } : {}),
|
||||
@@ -271,6 +276,7 @@ export async function runAgent(req: RunRequest): Promise<RunResult> {
|
||||
status: aborted ? "interrupted" : "failed",
|
||||
text: fullText,
|
||||
usage,
|
||||
...(costUsd !== undefined ? { costUsd } : {}),
|
||||
numTurns,
|
||||
sdkSessionId,
|
||||
...(aborted ? {} : { error: e instanceof Error ? e.message : String(e) }),
|
||||
|
||||
Reference in New Issue
Block a user