-- ============================================================
-- 56_kb_select_ai_router_team.sql
--
-- KB Select AI profile을 호출하는 최소 Agent / Tool / Task / Team 체인.
-- Tool -> Task -> Router Agent -> Team
-- Execute as POC_2 using SQLcl.
-- ============================================================
WHENEVER SQLERROR EXIT SQL.SQLCODE
SET DEFINE OFF
SET FEEDBACK ON

PROMPT === Resetting only this Select AI router chain ===
BEGIN
  DBMS_CLOUD_AI_AGENT.DROP_TEAM('KB_SELECT_AI_ROUTER_TEAM', force => TRUE);
  DBMS_CLOUD_AI_AGENT.DROP_TASK('KB_SELECT_AI_ROUTER_TASK', force => TRUE);
  DBMS_CLOUD_AI_AGENT.DROP_AGENT('KB_SELECT_AI_ROUTER_AGENT', force => TRUE);
  DBMS_CLOUD_AI_AGENT.DROP_TOOL('KB_SELECT_AI_ROUTER_TOOL', force => TRUE);
END;
/

PROMPT === Creating Select AI SQL tool ===
BEGIN
  DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
    tool_name => 'KB_SELECT_AI_ROUTER_TOOL',
    attributes => q'~{
      "tool_type": "SQL",
      "tool_params": {
        "profile_name": "KB_AIDP_SELECTAI_GPT55_OCI_PROFILE_V2"
      },
      "instruction": "Use the configured Select AI profile for structured KB insurance questions. For this demo, generate SHOWSQL only: create a read-only SELECT or WITH statement, do not execute it, and never generate DDL, DML, transaction control, package calls, or data changes."
    }~',
    status => 'ENABLED',
    description => 'Calls KB_AIDP_SELECTAI_GPT55_OCI_PROFILE_V2 to generate read-only KB SQL.'
  );
END;
/

PROMPT === Creating router agent ===
BEGIN
  DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
    agent_name => 'KB_SELECT_AI_ROUTER_AGENT',
    attributes => q'~{
      "profile_name": "KB_AIDP_AGENT_GPT55_OCI_PROFILE_V2",
      "role": "You are the KB Select AI routing agent. Classify every user request. If it asks for KB structured insurance data, metrics, counts, contracts, claims, customers, products, coverage, holdings, or stakeholders, call the assigned KB Select AI SQL tool. Return a concise Korean answer and the generated SQL. Do not invent data, do not expose internal reasoning, and do not use any unassigned tool.",
      "enable_human_tool": false
    }~',
    status => 'ENABLED',
    description => 'Routes KB structured-data questions to the Select AI SQL tool.'
  );
END;
/

PROMPT === Registering tool task ===
BEGIN
  DBMS_CLOUD_AI_AGENT.CREATE_TASK(
    task_name => 'KB_SELECT_AI_ROUTER_TASK',
    attributes => q'~{
      "instruction": "Route this user request to the KB Select AI SQL tool when it requires structured insurance data: {query}. Use SHOWSQL only. Explain briefly in Korean what the generated SQL will retrieve. If the request is outside the KB structured-data scope, state that this team only handles KB structured-data SQL generation.",
      "tools": ["KB_SELECT_AI_ROUTER_TOOL"],
      "enable_human_tool": false
    }~',
    status => 'ENABLED',
    description => 'Single Select AI SQL generation task for KB structured-data requests.'
  );
END;
/

PROMPT === Creating agent task team ===
BEGIN
  DBMS_CLOUD_AI_AGENT.CREATE_TEAM(
    team_name => 'KB_SELECT_AI_ROUTER_TEAM',
    attributes => q'~{
      "agents": [
        {
          "name": "KB_SELECT_AI_ROUTER_AGENT",
          "task": "KB_SELECT_AI_ROUTER_TASK"
        }
      ],
      "process": "sequential"
    }~',
    status => 'ENABLED',
    description => 'KB Select AI router Team: SQL tool, task, agent, and team.'
  );
END;
/

PROMPT === Registered chain ===
SELECT tool_name, status
FROM USER_AI_AGENT_TOOLS
WHERE tool_name = 'KB_SELECT_AI_ROUTER_TOOL';

SELECT agent_name, status
FROM USER_AI_AGENTS
WHERE agent_name = 'KB_SELECT_AI_ROUTER_AGENT';

SELECT task_name, status
FROM USER_AI_AGENT_TASKS
WHERE task_name = 'KB_SELECT_AI_ROUTER_TASK';

SELECT agent_team_name, status
FROM USER_AI_AGENT_TEAMS
WHERE agent_team_name = 'KB_SELECT_AI_ROUTER_TEAM';

EXIT
