All files / src/components/opc AgentPanel.svelte

0% Statements 0/117
0% Branches 0/1
0% Functions 0/1
0% Lines 0/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                                                                                                                                                                                                                                                                                                                                                                                                                   
<script>
  import { onMount } from "svelte";
  import { getAgents, getExecutions } from "../../lib/opc-api.js";
 
  let agents = $state([]);
  let executions = $state([]);
  let loading = $state(true);
  let selectedAgent = $state(null);
 
  async function loadData() {
    loading = true;
    try {
      const [agentsRes, executionsRes] = await Promise.all([
        getAgents(),
        getExecutions({ limit: 20 })
      ]);
      agents = agentsRes.items || [];
      executions = executionsRes.items || [];
    } catch (e) {
      console.error("Failed to load agent data:", e);
    } finally {
      loading = false;
    }
  }
 
  function getAgentIcon(agentId) {
    const icons = {
      pm: "📋",
      cto: "💻",
      coo: "⚙️",
      ceo: "🎯",
      marketing: "📢",
      social_media: "📱"
    };
    return icons[agentId] || "🤖";
  }
 
  function getStatusColor(status) {
    const colors = {
      pending: "bg-slate-100 text-slate-700",
      running: "bg-blue-100 text-blue-700",
      completed: "bg-emerald-100 text-emerald-700",
      failed: "bg-rose-100 text-rose-700",
      pending_approval: "bg-amber-100 text-amber-700"
    };
    return colors[status] || colors.pending;
  }
 
  function getAgentExecutions(agentId) {
    return executions.filter(e => e.agent_id === agentId);
  }
 
  function formatTimestamp(timestamp) {
    if (!timestamp) return "N/A";
    return new Date(timestamp).toLocaleString();
  }
 
  onMount(() => {
    loadData();
    // Refresh every 10 seconds
    const interval = setInterval(loadData, 10000);
    return () => clearInterval(interval);
  });
</script>
 
<div class="space-y-6">
  <h2 class="text-xl font-semibold text-surface-900 dark:text-surface-100">
    AI Agents
  </h2>
 
  {#if loading}
    <div class="text-surface-600 dark:text-surface-400">Loading agents...</div>
  {:else}
    <!-- Agent Cards -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
      {#each agents as agent}
        {@const agentExecs = getAgentExecutions(agent.id)}
        {@const runningExecs = agentExecs.filter(e => e.status === "running")}
        {@const completedExecs = agentExecs.filter(e => e.status === "completed")}
 
        <button
          onclick={() => selectedAgent = selectedAgent?.id === agent.id ? null : agent}
          class="bg-white dark:bg-surface-800 rounded-lg p-4 border-2 transition-all text-left hover:shadow-lg {selectedAgent?.id === agent.id ? 'border-primary-500' : 'border-surface-200 dark:border-surface-700'}"
        >
          <!-- Agent Header -->
          <div class="flex items-start justify-between mb-3">
            <div class="flex items-center gap-2">
              <span class="text-3xl">{getAgentIcon(agent.id)}</span>
              <div>
                <h3 class="font-semibold text-surface-900 dark:text-surface-100">
                  {agent.name}
                </h3>
                <p class="text-xs text-surface-600 dark:text-surface-400">
                  {agent.role}
                </p>
              </div>
            </div>
            {#if runningExecs.length > 0}
              <span class="flex items-center gap-1 text-xs text-blue-600 dark:text-blue-400">
                <span class="animate-pulse">●</span>
                Working
              </span>
            {:else}
              <span class="text-xs text-surface-500">Idle</span>
            {/if}
          </div>
 
          <!-- Agent Stats -->
          <div class="grid grid-cols-2 gap-2 text-sm">
            <div class="bg-surface-50 dark:bg-surface-900 rounded p-2">
              <div class="text-xs text-surface-600 dark:text-surface-400">Total Tasks</div>
              <div class="text-lg font-semibold text-surface-900 dark:text-surface-100">
                {agentExecs.length}
              </div>
            </div>
            <div class="bg-surface-50 dark:bg-surface-900 rounded p-2">
              <div class="text-xs text-surface-600 dark:text-surface-400">Completed</div>
              <div class="text-lg font-semibold text-emerald-600 dark:text-emerald-400">
                {completedExecs.length}
              </div>
            </div>
          </div>
 
          <!-- Capabilities -->
          <div class="mt-3">
            <div class="text-xs text-surface-600 dark:text-surface-400 mb-1">Capabilities:</div>
            <div class="flex flex-wrap gap-1">
              {#each agent.capabilities.slice(0, 3) as capability}
                <span class="text-xs px-2 py-0.5 rounded bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-300">
                  {capability.replace(/_/g, ' ')}
                </span>
              {/each}
              {#if agent.capabilities.length > 3}
                <span class="text-xs text-surface-500">+{agent.capabilities.length - 3}</span>
              {/if}
            </div>
          </div>
        </button>
      {/each}
    </div>
 
    <!-- Execution Log -->
    <div class="bg-white dark:bg-surface-800 rounded-lg border border-surface-200 dark:border-surface-700">
      <div class="p-4 border-b border-surface-200 dark:border-surface-700">
        <h3 class="font-semibold text-surface-900 dark:text-surface-100">
          {selectedAgent ? `${selectedAgent.name} Execution Log` : "Recent Executions"}
        </h3>
      </div>
 
      <div class="divide-y divide-surface-200 dark:divide-surface-700 max-h-96 overflow-y-auto">
        {#each (selectedAgent ? getAgentExecutions(selectedAgent.id) : executions) as execution}
          <div class="p-4 hover:bg-surface-50 dark:hover:bg-surface-900/50">
            <div class="flex items-start justify-between mb-2">
              <div class="flex items-center gap-2">
                <span class="text-xl">{getAgentIcon(execution.agent_id)}</span>
                <div>
                  <div class="font-medium text-surface-900 dark:text-surface-100">
                    Task #{execution.task_id}
                  </div>
                  <div class="text-xs text-surface-600 dark:text-surface-400">
                    {formatTimestamp(execution.started_at)}
                  </div>
                </div>
              </div>
              <span class="text-xs px-2 py-1 rounded {getStatusColor(execution.status)}">
                {execution.status.replace(/_/g, ' ')}
              </span>
            </div>
 
            {#if execution.output_result?.reasoning}
              <p class="text-sm text-surface-700 dark:text-surface-300 mb-2">
                {execution.output_result.reasoning}
              </p>
            {/if}
 
            {#if execution.actions_proposed && execution.actions_proposed.length > 0}
              <div class="text-xs text-surface-600 dark:text-surface-400">
                Actions: {execution.actions_proposed.length}
                {#each execution.actions_proposed.slice(0, 2) as action}
                  <span class="ml-2 px-1.5 py-0.5 rounded bg-surface-100 dark:bg-surface-700">
                    {action.type}
                  </span>
                {/each}
              </div>
            {/if}
 
            {#if execution.error_message}
              <div class="mt-2 text-xs text-rose-600 dark:text-rose-400">
                Error: {execution.error_message}
              </div>
            {/if}
          </div>
        {:else}
          <div class="p-8 text-center text-surface-500">
            No executions yet
          </div>
        {/each}
      </div>
    </div>
  {/if}
</div>