Агенты: учёт токенов (agent_usage, буфер в памяти + пачки в БД), globals с лимитами аккаунта, доллары убраны
Расчётная стоимость из ответов claude не является списанием (тариф Team, 0 из 10 USD), поэтому результаты, API и записи больше её не ведут. Токены каждого вызова модели - в agent_usage; проценты 5-часового и недельного окон читаются у claude в globals (одна строка, check), предупреждение ИТ на 80% и 95%, сводка GET /agent/usage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import type { Pool } from "khb_common";
|
||||
import { registerAgentRoutes } from "./agents-api.js";
|
||||
import { usageSummary, type UsageLog } from "./usage.js";
|
||||
import type { PrepJob, PrepResult } from "./jobs.js";
|
||||
|
||||
export interface ChatApi {
|
||||
@@ -18,6 +19,7 @@ export interface PrepApiDeps {
|
||||
token: string;
|
||||
run(job: PrepJob, opts: { projectId?: string; dryRun?: boolean }): Promise<PrepResult>;
|
||||
chat?: ChatApi;
|
||||
usageLog?: UsageLog;
|
||||
}
|
||||
|
||||
interface RunState {
|
||||
@@ -96,7 +98,9 @@ export function buildApi(deps: PrepApiDeps): FastifyInstance {
|
||||
return reply.code(202).send({ chatRunId: startChat(() => deps.chat!.apply({ managerId: b.managerId!, text: b.text!, publish: b.publish })) });
|
||||
});
|
||||
app.get<{ Params: { id: string } }>("/chat/runs/:id", async (req, reply) => chatRuns.get(req.params.id) ?? reply.code(404).send({ error: "not found" }));
|
||||
app.get("/agent/runs", async () => (await deps.pool.query("select id, manager_person_id, step, attempt, resumed, status, started_at, duration_ms, cost_usd, error, changes from agent_run order by started_at desc limit 20")).rows);
|
||||
app.get("/agent/runs", async () => (await deps.pool.query("select id, manager_person_id, step, attempt, resumed, status, started_at, duration_ms, error, changes from agent_run order by started_at desc limit 20")).rows);
|
||||
/** Токены агентов: сводка за N часов (по умолчанию 24) по операциям и менеджерам + лимиты из globals. Денег здесь нет намеренно. */
|
||||
app.get<{ Querystring: { hours?: string } }>("/agent/usage", async (req) => usageSummary(deps.pool, Number(req.query.hours) || 24, deps.usageLog));
|
||||
|
||||
app.get<{ Params: { id: string } }>("/runs/:id", async (req, reply) => runs.get(req.params.id) ?? reply.code(404).send({ error: "not found" }));
|
||||
return app;
|
||||
|
||||
Reference in New Issue
Block a user