// model-usage's tools — its own read-only query surface over the usage store (novox/hq ADR 0039: // the tools and the client they call live in the module), and its standing: what waits in the spool // (novox/hq issue 276). The store is opened from the environment; when it cannot be (no DATABASE_URL), // the query tool is not contributed rather than failing the whole tool runtime — the postgres // precedent — but the status tool is, since the spool is what matters most when the store is away. import { registerModuleTools, type ToolDefinition } from "@novox/mesh-sdk/tools"; import { UsageStore } from "../store.js"; import { Spool } from "../spool.js"; import { usageSpoolPath } from "../consume.js"; export function getUsageTools(store: UsageStore | undefined, env: NodeJS.ProcessEnv = process.env): ToolDefinition[] { const tools: ToolDefinition[] = [ { name: "model_usage_status", description: "The usage store's standing: the usage events that could not be written yet — how many wait in the spool, how many were taken from the bus and are held only there, the oldest, the last error, and whether the spool is over its bound (then the bus gives events up and max-deliveries is raised).", input: {}, // Launched as its own process beside the handler, so it reads the spool from disk. run: async () => ({ spool: (await Spool.read(usageSpoolPath(env))).standing() }), }, ]; if (store) { tools.push({ name: "model_usage_current", description: "The latest model-usage reading per (licence, consumer, period, metric), across both grains, optionally restricted to one licence.", input: { licence: { type: "string", description: "restrict to one licence (optional)" }, }, run: async (args) => { const licence = args.licence ? String(args.licence) : undefined; return { usage: await store.current(licence) }; }, }); } return tools; } registerModuleTools("model-usage", (env) => { let store: UsageStore | undefined; try { store = UsageStore.fromEnv(env); } catch { store = undefined; } return getUsageTools(store, env); });