Files
writing-agent/src/server/stats-handlers.ts
2026-07-10 08:31:27 +08:00

38 lines
1.1 KiB
TypeScript

import type { IncomingMessage, ServerResponse } from "node:http";
import {
readRecords,
summarizeTokenUsage,
type TokenStatsQuery,
} from "../stats/token-store.js";
function json(res: ServerResponse, status: number, data: unknown): void {
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify(data));
}
export async function handleStatsApi(
_req: IncomingMessage,
res: ServerResponse,
pathname: string,
searchParams: URLSearchParams,
): Promise<boolean> {
if (pathname !== "/api/stats/tokens") return false;
const query: TokenStatsQuery = {
bookId: searchParams.get("bookId") ?? undefined,
orchestratorId: searchParams.get("orchestratorId") ?? undefined,
sessionId: searchParams.get("sessionId") ?? undefined,
from: searchParams.get("from") ?? undefined,
to: searchParams.get("to") ?? undefined,
limit: searchParams.get("limit")
? Number(searchParams.get("limit"))
: undefined,
};
json(res, 200, {
summary: summarizeTokenUsage(query),
records: readRecords({ ...query, limit: query.limit ?? 100 }),
});
return true;
}