969b06253c
Part B: Model Pricing, Hook Configuration and CLAUDE_HOME move from Settings → Dashboard to the Claude Code tab's AgentHub sub-tab (ClaudeAgentHubSettings.tsx); CODEX_HOME is editable in Codex → AgentHub. A home that is or contains the OS home is refused (EBADHOME). Settings persistence: dashboard-saved settings go to ~/.config/agenthub/settings.env (AGENTHUB_SETTINGS_FILE), outside the plugin cache an update wipes. Load order: real env > the checkout's .env > that file. The first CLAUDE_HOME change pins DASHBOARD_DATA_DIR so the DB stays put. C1: codex-usage.js reads Codex rollout journals into sessions (new column tool) and token_usage, tracked per file in codex_rollouts; forks deduped by total vector, sub-agent threads folded into the root; pricing-sync also prices OpenAI models; PricingSettings with a vendor filter. C2: codex-hooks.js installs AgentHub's hooks into $CODEX_HOME/hooks.json on a click, through a stable forwarder; Codex trust is never written. The hook route creates tool='codex' sessions and folds sub-agent hooks into the root. D: antigravity-usage.js reads agy's per-conversation SQLite files (tokens from ModelUsageStats) into tool='antigravity' sessions; antigravity-hooks.js installs the named hook `agenthub` (PreInvocation, PostToolUse, Stop — never PreToolUse) into ~/.gemini/config/hooks.json; Settings → Antigravity tab; Gemini pricing. Whole-change review fixes: sameOriginGuard on both home PUTs, routers.missing on a CLAUDE_HOME change, reset-pricing scope, GET /api/sessions?tool=, clear-data covering the new tables, Codex child-thread events moving no status, hooks owning status only while current, EFOREIGN for a forwarder that is not ours, agy quiet window and turn rules, AGENTHUB_SETTINGS_FILE honoured from .env. Docs, OpenAPI and the CLAUDE.md marker updated. Server 2141, client 779.
119 lines
4.3 KiB
JavaScript
119 lines
4.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Claude Code (and Codex, and Antigravity) hook handler.
|
|
* Receives hook event JSON on stdin and forwards it to every live Agent
|
|
* Dashboard server. Designed to fail silently so it never blocks Claude
|
|
* Code, and to fan out across multiple dashboards that use **different**
|
|
* SQLite data directories (e.g. the macOS desktop app alongside `npm run dev`
|
|
* when each has its own DB). Servers sharing one database receive hooks through
|
|
* a single ingest port so events are never duplicated.
|
|
*
|
|
* Delivery is fire-and-forget: we exit as soon as the request body is on the
|
|
* wire, WITHOUT waiting for the dashboard's HTTP response. The hook only needs
|
|
* to *deliver* the event — on loopback the local server reads the buffered
|
|
* request and processes it even after this short-lived process exits. Waiting
|
|
* for the response is what made Claude Code sit at "running hooks" for seconds
|
|
* whenever a dashboard was busy, slow, or wedged.
|
|
*
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const http = require("http");
|
|
|
|
const hookType = process.argv[2] || "unknown";
|
|
// Codex runs this through `<CODEX_HOME>/agenthub-hook.sh <Event>`, which adds
|
|
// `codex` (server/lib/codex-hooks.js), Antigravity through
|
|
// `~/.gemini/config/agenthub-hook.sh`, which adds `antigravity`
|
|
// (server/lib/antigravity-hooks.js); Claude Code passes no second argument.
|
|
const tool = ["codex", "antigravity"].includes(process.argv[3]) ? process.argv[3] : undefined;
|
|
|
|
/**
|
|
* Resolve every live dashboard server's port via the discovery file. Falls
|
|
* back to the `CLAUDE_DASHBOARD_PORT` override or the conventional 4820 if
|
|
* the discovery module can't load for any reason. Never throws.
|
|
*/
|
|
function resolvePorts() {
|
|
try {
|
|
return require("../server/lib/server-info").resolveHookIngestPorts();
|
|
} catch {
|
|
const envPort = parseInt(process.env.CLAUDE_DASHBOARD_PORT || "", 10);
|
|
return [Number.isInteger(envPort) && envPort > 0 ? envPort : 4820];
|
|
}
|
|
}
|
|
|
|
const ports = resolvePorts();
|
|
|
|
let input = "";
|
|
|
|
process.stdin.setEncoding("utf8");
|
|
process.stdin.on("data", (chunk) => (input += chunk));
|
|
process.stdin.on("end", () => {
|
|
let parsedData;
|
|
try {
|
|
parsedData = JSON.parse(input);
|
|
} catch {
|
|
parsedData = { raw: input };
|
|
}
|
|
|
|
const payload = JSON.stringify({
|
|
hook_type: hookType,
|
|
data: parsedData,
|
|
...(tool && { tool }),
|
|
});
|
|
const contentLength = Buffer.byteLength(payload);
|
|
|
|
// Fan out one POST per live server. Each per-target promise resolves the
|
|
// moment the request body has been flushed — NOT when the dashboard replies
|
|
// — so a busy, slow, or wedged dashboard can't stall the hook. Each promise
|
|
// always resolves (never rejects), so one dead listener can't starve the
|
|
// others and Promise.all can't be left hanging by a single failure.
|
|
const sends = ports.map(
|
|
(port) =>
|
|
new Promise((resolve) => {
|
|
let settled = false;
|
|
const done = () => {
|
|
if (settled) return;
|
|
settled = true;
|
|
resolve();
|
|
};
|
|
|
|
const req = http.request(
|
|
{
|
|
hostname: "127.0.0.1",
|
|
port,
|
|
path: "/api/hooks/event",
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Content-Length": contentLength,
|
|
},
|
|
timeout: 2000,
|
|
},
|
|
// Drain any response so the socket closes cleanly if the server does
|
|
// reply before we exit. We never block on it.
|
|
(res) => res.resume()
|
|
);
|
|
|
|
req.on("error", done); // dead listener (ECONNREFUSED) — nothing to deliver
|
|
req.on("timeout", () => {
|
|
req.destroy();
|
|
done();
|
|
});
|
|
req.write(payload);
|
|
// The 'end' callback fires once the body is on the wire: delivery is
|
|
// done and the local server will process it on its own schedule.
|
|
req.end(done);
|
|
})
|
|
);
|
|
|
|
// Give the kernel one tick to hand the buffered request bytes to the local
|
|
// server before our sockets close, then exit. The hook returns in ms.
|
|
Promise.all(sends).finally(() => setImmediate(() => process.exit(0)));
|
|
});
|
|
|
|
// Safety net — guarantees the hook never blocks Claude Code even if a send
|
|
// somehow never settles. Shorter than the old 5s wait because we no longer
|
|
// block on the dashboard's response, only on the request flush.
|
|
setTimeout(() => process.exit(0), 2500);
|