"use client"; import { useState, useEffect } from "react"; import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components"; import Image from "next/image"; import BaseUrlSelect from "./BaseUrlSelect"; import ApiKeySelect from "./ApiKeySelect"; import { matchKnownEndpoint } from "./cliEndpointMatch"; export default function KiloToolCard({ tool, isExpanded, onToggle, baseUrl, apiKeys, activeProviders, cloudEnabled, initialStatus, tunnelEnabled, tunnelPublicUrl, tailscaleEnabled, tailscaleUrl }) { const [status, setStatus] = useState(initialStatus || null); const [checking, setChecking] = useState(false); const [applying, setApplying] = useState(false); const [restoring, setRestoring] = useState(false); const [message, setMessage] = useState(null); const [showInstallGuide, setShowInstallGuide] = useState(false); const [selectedApiKey, setSelectedApiKey] = useState(""); const [selectedModel, setSelectedModel] = useState(""); const [modalOpen, setModalOpen] = useState(false); const [modelAliases, setModelAliases] = useState({}); const [showManualConfigModal, setShowManualConfigModal] = useState(false); const [customBaseUrl, setCustomBaseUrl] = useState(""); useEffect(() => { if (apiKeys?.length > 0 && !selectedApiKey) setSelectedApiKey(apiKeys[0].key); }, [apiKeys, selectedApiKey]); useEffect(() => { if (initialStatus) setStatus(initialStatus); }, [initialStatus]); useEffect(() => { if (isExpanded && !status) { checkStatus(); fetchModelAliases(); } if (isExpanded) fetchModelAliases(); }, [isExpanded]); const fetchModelAliases = async () => { try { const res = await fetch("/api/models/alias"); const data = await res.json(); if (res.ok) setModelAliases(data.aliases || {}); } catch (error) { console.log("Error fetching model aliases:", error); } }; const getConfigStatus = () => { if (!status?.installed) return null; return status.has9Router ? "configured" : "not_configured"; }; const configStatus = getConfigStatus(); const getEffectiveBaseUrl = () => { const url = customBaseUrl || `${baseUrl}/v1`; return url.endsWith("/v1") ? url : `${url}/v1`; }; const getDisplayUrl = () => customBaseUrl || `${baseUrl}/v1`; const checkStatus = async () => { setChecking(true); try { const res = await fetch("/api/cli-tools/kilo-settings"); const data = await res.json(); setStatus(data); } catch (error) { setStatus({ installed: false, error: error.message }); } finally { setChecking(false); } }; const handleApply = async () => { setApplying(true); setMessage(null); try { const keyToUse = (selectedApiKey && selectedApiKey.trim()) ? selectedApiKey : (!cloudEnabled ? "sk_9router" : selectedApiKey); const res = await fetch("/api/cli-tools/kilo-settings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ baseUrl: getEffectiveBaseUrl(), apiKey: keyToUse, model: selectedModel }), }); const data = await res.json(); if (res.ok) { setMessage({ type: "success", text: "Settings applied successfully!" }); checkStatus(); } else { setMessage({ type: "error", text: data.error || "Failed to apply settings" }); } } catch (error) { setMessage({ type: "error", text: error.message }); } finally { setApplying(false); } }; const handleReset = async () => { setRestoring(true); setMessage(null); try { const res = await fetch("/api/cli-tools/kilo-settings", { method: "DELETE" }); const data = await res.json(); if (res.ok) { setMessage({ type: "success", text: "Settings reset successfully!" }); setSelectedModel(""); checkStatus(); } else { setMessage({ type: "error", text: data.error || "Failed to reset settings" }); } } catch (error) { setMessage({ type: "error", text: error.message }); } finally { setRestoring(false); } }; const getManualConfigs = () => { const keyToUse = (selectedApiKey && selectedApiKey.trim()) ? selectedApiKey : (!cloudEnabled ? "sk_9router" : ""); return [{ filename: "~/.local/share/kilo/auth.json", content: JSON.stringify({ "openai-compatible": { type: "api-key", apiKey: keyToUse, baseUrl: getEffectiveBaseUrl(), model: selectedModel || "provider/model-id", }, }, null, 2), }]; }; return (
{tool.name} { e.target.style.display = "none"; }} />

{tool.name}

{configStatus === "configured" && Connected} {configStatus === "not_configured" && Not configured}

{tool.description}

expand_more
{isExpanded && (
{checking && (
progress_activity Checking Kilo Code...
)} {!checking && status && !status.installed && (
warning

Kilo Code not detected locally

Manual configuration is still available if 9router is deployed on a remote server.

{showInstallGuide && (

Installation Guide

Install Kilo Code from kilocode.ai or VS Code extension marketplace.

)}
)} {!checking && status?.installed && ( <>
Select Endpoint arrow_forward
API Key arrow_forward
Model arrow_forward
setSelectedModel(e.target.value)} placeholder="provider/model-id" className="w-full min-w-0 pl-2 pr-7 py-2 bg-surface rounded border border-border text-xs focus:outline-none focus:ring-1 focus:ring-primary/50 sm:py-1.5" /> {selectedModel && }
{message && (
{message.type === "success" ? "check_circle" : "error"} {message.text}
)}
)}
)} setModalOpen(false)} onSelect={(model) => { setSelectedModel(model.value); setModalOpen(false); }} selectedModel={selectedModel} activeProviders={activeProviders} modelAliases={modelAliases} title="Select Model for Kilo Code" /> setShowManualConfigModal(false)} title="Kilo Code - Manual Configuration" configs={getManualConfigs()} />
); }