import { NextResponse } from "next/server";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { llmProviderRegistry } from "@open-mercato/shared/lib/ai/llm-provider-registry";
import { isAgentTaskPlanEnabled, listAgents, loadAgentRegistry } from "../../../lib/agent-registry.js";
import { hasRequiredFeatures } from "../../../lib/auth.js";
import { toolRegistry } from "../../../lib/tool-registry.js";
const openApi = {
  tag: "AI Assistant",
  summary: "List AI agents the caller can invoke",
  methods: {
    GET: {
      operationId: "aiAssistantListAgents",
      summary: "List registered AI agents, filtered by the caller's features.",
      description: "Returns `{ agents: [...] }` \u2014 the subset of agents from `ai-agents.generated.ts` that the authenticated caller can invoke based on each agent's `requiredFeatures`. Mirrors the `meta.list_agents` tool handler so backoffice pages (e.g. the playground) can render an agent picker without going through the MCP tool transport.",
      responses: [
        {
          status: 200,
          description: "Accessible agent summaries.",
          mediaType: "application/json"
        }
      ],
      errors: [
        { status: 401, description: "Unauthenticated caller." },
        { status: 500, description: "Internal failure while loading the agent registry." }
      ]
    }
  }
};
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["ai_assistant.view"] }
};
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth) {
    return NextResponse.json({ error: "Unauthorized", code: "unauthenticated" }, { status: 401 });
  }
  try {
    const container = await createRequestContainer();
    const rbacService = container.resolve("rbacService");
    const acl = await rbacService.loadAcl(auth.sub, {
      tenantId: auth.tenantId,
      organizationId: auth.orgId
    });
    const aiConfigured = llmProviderRegistry.resolveFirstConfigured() != null;
    await loadAgentRegistry();
    const all = listAgents();
    const accessible = all.filter(
      (agent) => hasRequiredFeatures(agent.requiredFeatures, acl.features, acl.isSuperAdmin, rbacService)
    );
    const agents = accessible.map((agent) => {
      const tools = agent.allowedTools.map((toolName) => {
        const tool = toolRegistry.getTool(toolName);
        return {
          name: toolName,
          displayName: tool?.displayName ?? toolName,
          isMutation: Boolean(tool?.isMutation),
          registered: Boolean(tool)
        };
      });
      return {
        id: agent.id,
        moduleId: agent.moduleId,
        label: agent.label,
        description: agent.description,
        systemPrompt: agent.systemPrompt,
        executionMode: agent.executionMode ?? "chat",
        defaultProvider: agent.defaultProvider ?? null,
        defaultModel: agent.defaultModel ?? null,
        defaultBaseUrl: agent.defaultBaseUrl ?? null,
        allowRuntimeModelOverride: agent.allowRuntimeModelOverride !== false,
        mutationPolicy: agent.mutationPolicy ?? "read-only",
        readOnly: Boolean(agent.readOnly),
        maxSteps: agent.maxSteps ?? null,
        taskPlan: { enabled: isAgentTaskPlanEnabled(agent) },
        allowedTools: agent.allowedTools,
        tools,
        requiredFeatures: agent.requiredFeatures ?? [],
        acceptedMediaTypes: agent.acceptedMediaTypes ?? [],
        keywords: agent.keywords ?? [],
        suggestions: agent.suggestions ?? [],
        hasOutputSchema: Boolean(agent.output)
      };
    });
    return NextResponse.json({ agents, total: agents.length, aiConfigured });
  } catch (error) {
    console.error("[AI Agents] Failed to list agents:", error);
    return NextResponse.json(
      { error: "Failed to list agents", code: "internal_error" },
      { status: 500 }
    );
  }
}
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
