import { d as defineEventHandler, a as useOrchestrator } from '../../../../index.mjs';
import '@anthropic-ai/claude-agent-sdk';
import '@octokit/webhooks';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:process';
import 'consola';
import '@octokit/graphql';
import 'zod';
import '@octokit/auth-app';
import 'node:os';
import '@libsql/client';
import 'liquidjs';
import 'js-yaml';
import 'node:child_process';
import '@chat-adapter/github';
import '@chat-adapter/slack';
import 'chat';
import 'chat-adapter-asana';
import 'better-auth';
import 'better-auth/plugins';
import 'bun:sqlite';
import 'better-auth/db/migration';
import 'node:url';
import '@iconify/utils';
import 'rou3';

const state_get = defineEventHandler((event) => {
  const orchestrator = useOrchestrator();
  const state = orchestrator.getState();
  const running = Array.from(state.running.values(), (r) => runningEntryPayload(r));
  const retrying = Array.from(state.retry_attempts.values(), (r) => retryEntryPayload(r));
  const liveSecondsRunning = computeLiveSecondsRunning(state);
  return {
    generated_at: (/* @__PURE__ */ new Date()).toISOString(),
    counts: {
      running: running.length,
      retrying: retrying.length
    },
    running,
    retrying,
    codex_totals: {
      input_tokens: state.agent_totals.input_tokens,
      output_tokens: state.agent_totals.output_tokens,
      total_tokens: state.agent_totals.total_tokens,
      seconds_running: state.agent_totals.seconds_running + liveSecondsRunning
    },
    rate_limits: state.agent_rate_limits
  };
});
function runningEntryPayload(r) {
  var _a, _b;
  return {
    issue_id: r.issue.id,
    issue_identifier: r.identifier,
    state: r.issue.state,
    session_id: r.session_id,
    turn_count: r.turn_count,
    last_event: r.last_agent_event,
    last_message: r.last_agent_message,
    started_at: r.started_at.toISOString(),
    last_event_at: (_b = (_a = r.last_agent_timestamp) == null ? void 0 : _a.toISOString()) != null ? _b : null,
    tokens: {
      input_tokens: r.agent_input_tokens,
      output_tokens: r.agent_output_tokens,
      total_tokens: r.agent_total_tokens
    }
  };
}
function retryEntryPayload(r) {
  return {
    issue_id: r.issue_id,
    issue_identifier: r.identifier,
    attempt: r.attempt,
    due_at: new Date(r.due_at_ms).toISOString(),
    error: r.error
  };
}
function computeLiveSecondsRunning(state) {
  const now = Date.now();
  let total = 0;
  for (const entry of state.running.values()) {
    total += (now - entry.started_at.getTime()) / 1e3;
  }
  return total;
}

export { state_get as default };
//# sourceMappingURL=state.get.mjs.map
