Add live agent views and improve Codex monitoring

This commit is contained in:
William Valentin
2026-03-20 13:59:51 -07:00
parent a87bbc6983
commit 687a7aa79d
10 changed files with 1408 additions and 184 deletions
+101 -41
View File
@@ -52,27 +52,52 @@ function safeJSONStringify(value) {
}
function getSessionKey(input) {
return pickString(
input.id,
input.session,
input.sessionId,
input.session_id,
input.threadId,
input.thread_id,
input.chatId,
input.chat_id,
input.conversationId,
input.conversation_id
);
}
function getUsage(input) {
const usage = isRecord(input.usage) ? input.usage : isRecord(input.llm) ? input.llm : isRecord(input.tokens) ? input.tokens : void 0;
const usage = isRecord(input.usage) ? input.usage : isRecord(input.llm) ? input.llm : isRecord(input.tokens) ? input.tokens : isRecord(input.llm_usage) ? input.llm_usage : void 0;
if (!usage)
return void 0;
const result = {};
if (usage.input_tokens !== void 0)
result.input_tokens = usage.input_tokens;
if (usage.prompt_tokens !== void 0)
result.input_tokens = usage.prompt_tokens;
if (usage.input !== void 0)
result.input_tokens = usage.input;
if (usage.output_tokens !== void 0)
result.output_tokens = usage.output_tokens;
if (usage.completion_tokens !== void 0)
result.output_tokens = usage.completion_tokens;
if (usage.output !== void 0)
result.output_tokens = usage.output;
if (usage.total_tokens !== void 0)
result.total_tokens = usage.total_tokens;
if (usage.total !== void 0)
result.total_tokens = usage.total;
if (usage.cost !== void 0)
result.total_cost = usage.cost;
if (usage.total_cost !== void 0)
result.total_cost = usage.total_cost;
return Object.keys(result).length > 0 ? result : void 0;
}
function getModel(input) {
return pickString(
input.model,
isRecord(input.llm) ? input.llm.model : void 0,
isRecord(input.usage) ? input.usage.model : void 0
);
}
function buildEnvelope(type, sessionKey, opts = {}) {
const correlation = {};
if (sessionKey) {
@@ -126,6 +151,52 @@ function enqueue(event) {
scheduleFlush();
}
}
function enqueueMetricSnapshot(sessionKey, runId, usage, input) {
if (!usage) {
return;
}
const metrics = { usage };
const model = getModel(input);
if (model) {
metrics.model = model;
}
enqueue(buildEnvelope("metric.snapshot", sessionKey, {
runId,
payload: { metrics }
}));
}
function startRun(sessionKey, input) {
const runId = randomUUID();
if (sessionKey) {
activeRuns.set(sessionKey, runId);
}
enqueue(buildEnvelope("run.start", sessionKey, {
runId,
attributes: {
trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type)
},
payload: {
prompt_preview: truncate(pickString(input.prompt, input.message, input.text), 200)
}
}));
return runId;
}
function endRun(sessionKey, runId, input, duration) {
if (!runId) {
return;
}
const usage = getUsage(input);
enqueue(buildEnvelope("run.end", sessionKey, {
runId,
payload: {
status: "success",
duration_ms: duration,
model: getModel(input),
...usage && { usage }
}
}));
enqueueMetricSnapshot(sessionKey, runId, usage, input);
}
async function postBatch(batch) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
@@ -167,18 +238,8 @@ async function flush() {
}
async function handleSessionStart(input) {
const sessionKey = getSessionKey(input) || randomUUID();
const runId = randomUUID();
activeRuns.set(sessionKey, runId);
enqueue(buildEnvelope("session.start", sessionKey));
enqueue(buildEnvelope("run.start", sessionKey, {
runId,
attributes: {
trigger: pickString(input.trigger_type, input.trigger)
},
payload: {
prompt_preview: truncate(input.prompt, 200)
}
}));
startRun(sessionKey, input);
await flush();
}
async function handleSessionEnd(input) {
@@ -186,46 +247,41 @@ async function handleSessionEnd(input) {
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0;
const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms, input.duration);
if (runId) {
enqueue(buildEnvelope("run.end", sessionKey, {
runId,
payload: {
status: "success",
duration_ms: duration,
...usage && { usage }
}
}));
}
endRun(sessionKey, runId, input, duration);
enqueue(buildEnvelope("session.end", sessionKey, {
payload: usage ? { usage } : void 0
payload: {
model: getModel(input),
...usage && { usage }
}
}));
enqueueMetricSnapshot(sessionKey, runId, usage, input);
activeRuns.delete(sessionKey || "");
await flush();
}
async function handlePromptSubmit(input) {
const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0;
const duration = pickNumber(input.elapsed_ms, input.duration_ms, input.duration);
const prompt = pickString(input.prompt, input.text, input.message);
if (runId && prompt) {
endRun(sessionKey, runId, input, duration);
}
startRun(sessionKey, input);
await flush();
}
async function handleNotification(input) {
const sessionKey = getSessionKey(input);
const notificationType = pickString(input.type, input.notification_type);
const notificationType = pickString(input.type, input.notification_type, input.event, input.event_type);
const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms);
if (notificationType === "agent-turn-complete" || notificationType === "Done") {
if (notificationType === "agent-turn-complete" || notificationType === "Done" || notificationType === "turn.complete") {
const runId = sessionKey ? activeRuns.get(sessionKey) : void 0;
if (runId) {
enqueue(buildEnvelope("run.end", sessionKey, {
runId,
payload: {
status: "success",
duration_ms: duration,
...usage && { usage }
}
}));
endRun(sessionKey, runId, input, duration);
if (pickString(input.prompt, input.message, input.text)) {
startRun(sessionKey, input);
}
const newRunId = randomUUID();
if (sessionKey) {
activeRuns.set(sessionKey, newRunId);
}
enqueue(buildEnvelope("run.start", sessionKey, {
runId: newRunId
}));
} else if (usage) {
enqueueMetricSnapshot(sessionKey, sessionKey ? activeRuns.get(sessionKey) : void 0, usage, input);
}
await flush();
}
@@ -249,6 +305,10 @@ const handler = async () => {
case "stop":
await handleSessionEnd(input);
break;
case "prompt":
case "prompt-submit":
await handlePromptSubmit(input);
break;
case "notification":
await handleNotification(input);
break;
+109 -44
View File
@@ -65,8 +65,14 @@ function safeJSONStringify(value: unknown): string {
function getSessionKey(input: Dict): string | undefined {
return pickString(
input.id,
input.session,
input.sessionId,
input.session_id,
input.threadId,
input.thread_id,
input.chatId,
input.chat_id,
input.conversationId,
input.conversation_id,
);
@@ -75,18 +81,33 @@ function getSessionKey(input: Dict): string | undefined {
function getUsage(input: Dict): Dict | undefined {
const usage = isRecord(input.usage) ? input.usage :
isRecord(input.llm) ? input.llm :
isRecord(input.tokens) ? input.tokens : undefined;
isRecord(input.tokens) ? input.tokens :
isRecord(input.llm_usage) ? input.llm_usage : undefined;
if (!usage) return undefined;
const result: Dict = {};
if (usage.input_tokens !== undefined) result.input_tokens = usage.input_tokens;
if (usage.prompt_tokens !== undefined) result.input_tokens = usage.prompt_tokens;
if (usage.input !== undefined) result.input_tokens = usage.input;
if (usage.output_tokens !== undefined) result.output_tokens = usage.output_tokens;
if (usage.completion_tokens !== undefined) result.output_tokens = usage.completion_tokens;
if (usage.output !== undefined) result.output_tokens = usage.output;
if (usage.total_tokens !== undefined) result.total_tokens = usage.total_tokens;
if (usage.total !== undefined) result.total_tokens = usage.total;
if (usage.cost !== undefined) result.total_cost = usage.cost;
if (usage.total_cost !== undefined) result.total_cost = usage.total_cost;
return Object.keys(result).length > 0 ? result : undefined;
}
function getModel(input: Dict): string | undefined {
return pickString(
input.model,
isRecord(input.llm) ? input.llm.model : undefined,
isRecord(input.usage) ? input.usage.model : undefined,
);
}
function buildEnvelope(
type: string,
sessionKey?: string,
@@ -156,6 +177,60 @@ function enqueue(event: Dict) {
}
}
function enqueueMetricSnapshot(sessionKey: string | undefined, runId: string | undefined, usage: Dict | undefined, input: Dict) {
if (!usage) {
return;
}
const metrics: Dict = { usage };
const model = getModel(input);
if (model) {
metrics.model = model;
}
enqueue(buildEnvelope('metric.snapshot', sessionKey, {
runId,
payload: { metrics },
}));
}
function startRun(sessionKey: string | undefined, input: Dict): string {
const runId = randomUUID();
if (sessionKey) {
activeRuns.set(sessionKey, runId);
}
enqueue(buildEnvelope('run.start', sessionKey, {
runId,
attributes: {
trigger: pickString(input.trigger_type, input.trigger, input.event, input.event_type),
},
payload: {
prompt_preview: truncate(pickString(input.prompt, input.message, input.text), 200),
},
}));
return runId;
}
function endRun(sessionKey: string | undefined, runId: string | undefined, input: Dict, duration?: number) {
if (!runId) {
return;
}
const usage = getUsage(input);
enqueue(buildEnvelope('run.end', sessionKey, {
runId,
payload: {
status: 'success',
duration_ms: duration,
model: getModel(input),
...(usage && { usage }),
},
}));
enqueueMetricSnapshot(sessionKey, runId, usage, input);
}
async function postBatch(batch: Dict[]) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
@@ -202,20 +277,9 @@ async function flush() {
async function handleSessionStart(input: Dict) {
const sessionKey = getSessionKey(input) || randomUUID();
const runId = randomUUID();
activeRuns.set(sessionKey, runId);
enqueue(buildEnvelope('session.start', sessionKey));
enqueue(buildEnvelope('run.start', sessionKey, {
runId,
attributes: {
trigger: pickString(input.trigger_type, input.trigger),
},
payload: {
prompt_preview: truncate(input.prompt, 200),
},
}));
startRun(sessionKey, input);
await flush();
}
@@ -226,53 +290,50 @@ async function handleSessionEnd(input: Dict) {
const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms, input.duration);
if (runId) {
enqueue(buildEnvelope('run.end', sessionKey, {
runId,
payload: {
status: 'success',
duration_ms: duration,
...(usage && { usage }),
},
}));
}
endRun(sessionKey, runId, input, duration);
enqueue(buildEnvelope('session.end', sessionKey, {
payload: usage ? { usage } : undefined,
payload: {
model: getModel(input),
...(usage && { usage }),
},
}));
enqueueMetricSnapshot(sessionKey, runId, usage, input);
activeRuns.delete(sessionKey || '');
await flush();
}
async function handlePromptSubmit(input: Dict) {
const sessionKey = getSessionKey(input);
const runId = sessionKey ? activeRuns.get(sessionKey) : undefined;
const duration = pickNumber(input.elapsed_ms, input.duration_ms, input.duration);
const prompt = pickString(input.prompt, input.text, input.message);
if (runId && prompt) {
endRun(sessionKey, runId, input, duration);
}
startRun(sessionKey, input);
await flush();
}
async function handleNotification(input: Dict) {
const sessionKey = getSessionKey(input);
const notificationType = pickString(input.type, input.notification_type);
const notificationType = pickString(input.type, input.notification_type, input.event, input.event_type);
const usage = getUsage(input);
const duration = pickNumber(input.duration_ms, input.elapsed_ms);
if (notificationType === 'agent-turn-complete' || notificationType === 'Done') {
if (notificationType === 'agent-turn-complete' || notificationType === 'Done' || notificationType === 'turn.complete') {
const runId = sessionKey ? activeRuns.get(sessionKey) : undefined;
endRun(sessionKey, runId, input, duration);
if (runId) {
enqueue(buildEnvelope('run.end', sessionKey, {
runId,
payload: {
status: 'success',
duration_ms: duration,
...(usage && { usage }),
},
}));
if (pickString(input.prompt, input.message, input.text)) {
startRun(sessionKey, input);
}
const newRunId = randomUUID();
if (sessionKey) {
activeRuns.set(sessionKey, newRunId);
}
enqueue(buildEnvelope('run.start', sessionKey, {
runId: newRunId,
}));
} else if (usage) {
enqueueMetricSnapshot(sessionKey, sessionKey ? activeRuns.get(sessionKey) : undefined, usage, input);
}
await flush();
@@ -300,6 +361,10 @@ const handler = async () => {
case 'stop':
await handleSessionEnd(input);
break;
case 'prompt':
case 'prompt-submit':
await handlePromptSubmit(input);
break;
case 'notification':
await handleNotification(input);
break;
+18
View File
@@ -17,6 +17,24 @@
"type": "command",
"command": "~/.local/bin/agentmon-codex-handler notification"
}
],
"userpromptsubmit": [
{
"type": "command",
"command": "~/.local/bin/agentmon-codex-handler prompt"
}
],
"userPromptSubmit": [
{
"type": "command",
"command": "~/.local/bin/agentmon-codex-handler prompt"
}
],
"userPromptSubmitted": [
{
"type": "command",
"command": "~/.local/bin/agentmon-codex-handler prompt"
}
]
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
"main": "handler.js",
"type": "module",
"bin": {
"agentmon-handler": "./handler.js"
"agentmon-codex-handler": "./handler.js"
},
"scripts": {
"build": "npx esbuild handler.ts --platform=node --format=esm --outfile=handler.js"