import { d as defineEventHandler, a as useOrchestrator, g as getRouterParam, c as createError, w as workspacePath } 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 _identifier__get = defineEventHandler((event) => {
  var _a, _b, _c, _d, _e, _f;
  const orchestrator = useOrchestrator();
  const identifier = (_a = getRouterParam(event, "identifier")) != null ? _a : "";
  if (!identifier) {
    throw createError({ statusCode: 400, statusMessage: "Missing identifier" });
  }
  const state = orchestrator.getState();
  const config = orchestrator.getConfig();
  const wsPath = workspacePath(config, identifier);
  const running = findRunningByIdentifier(state, identifier);
  const retry = findRetryByIdentifier(state, identifier);
  if (!running && !retry) {
    throw createError({ statusCode: 404, statusMessage: `Issue not found: ${identifier}` });
  }
  const issueId = (_c = (_b = running == null ? void 0 : running.issue.id) != null ? _b : retry == null ? void 0 : retry.issue_id) != null ? _c : "";
  const status = running ? "running" : "retrying";
  return {
    issue_identifier: identifier,
    issue_id: issueId,
    status,
    workspace: { path: wsPath },
    attempts: {
      restart_count: Math.max(((_d = retry == null ? void 0 : retry.attempt) != null ? _d : 0) - 1, 0),
      current_retry_attempt: (_e = retry == null ? void 0 : retry.attempt) != null ? _e : 0
    },
    running: running ? runningIssuePayload(running) : null,
    retry: retry ? retryIssuePayload(retry) : null,
    logs: { codex_session_logs: [] },
    recent_events: running ? recentEventsPayload(running) : [],
    last_error: (_f = retry == null ? void 0 : retry.error) != null ? _f : null,
    tracked: {}
  };
});
function runningIssuePayload(r) {
  var _a, _b;
  return {
    session_id: r.session_id,
    turn_count: r.turn_count,
    state: r.issue.state,
    started_at: r.started_at.toISOString(),
    last_event: r.last_agent_event,
    last_message: r.last_agent_message,
    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 retryIssuePayload(r) {
  return {
    attempt: r.attempt,
    due_at: new Date(r.due_at_ms).toISOString(),
    error: r.error
  };
}
function recentEventsPayload(r) {
  if (!r.last_agent_timestamp)
    return [];
  return [{
    at: r.last_agent_timestamp.toISOString(),
    event: r.last_agent_event,
    message: r.last_agent_message
  }];
}
function findRunningByIdentifier(state, identifier) {
  for (const entry of state.running.values()) {
    if (entry.identifier === identifier)
      return entry;
  }
  return void 0;
}
function findRetryByIdentifier(state, identifier) {
  for (const entry of state.retry_attempts.values()) {
    if (entry.identifier === identifier)
      return entry;
  }
  return void 0;
}

export { _identifier__get as default };
//# sourceMappingURL=_identifier_.get.mjs.map
