import type { AgentRunView, HostedAgent, HostedAgents, ModelProvider } from "@refs/backend/contract"; import { AgentAvatar, Ago, Badge, Banner, Button, Empty, Input, InputArea, LayerCard, LinkButton, Select } from "@refs/design-system"; import { CheckCircleIcon, CircleNotchIcon, PlusIcon, RobotIcon, WarningCircleIcon } from "@refs/design-system/icons"; import { AppLink } from "./link"; import { useState } from "react"; import { Form, redirect, useNavigation } from "react-router"; import { failed, formText } from "../lib/session"; const PROVIDERS: Record<"" | ModelProvider, { label: string; example: string }> = { "": { label: "Workers AI (default)", example: "" }, anthropic: { label: "Anthropic", example: "claude-sonnet-5-5" }, openai: { label: "OpenAI", example: "gpt-5.5" }, openrouter: { label: "OpenRouter", example: "anthropic/claude-sonnet-5-5" }, }; const SCOPE_LABEL: Record = { builtin: "Built in", namespace: "Shared", repo: "This repository" }; export type Level = "namespace" | "repo"; /** Handles the save and remove forms of an agents page, then goes back to the list. */ export async function agentsAction(agents: HostedAgents, request: Request, back: string) { const form = await request.formData(); const intent = formText(form, "intent"); const outcome = intent === "save" ? await agents.save({ handle: formText(form, "handle"), description: formText(form, "description"), instructions: String(form.get("instructions") ?? ""), provider: formText(form, "provider"), model: formText(form, "model"), key: String(form.get("key") ?? ""), }) : intent === "remove" ? await agents.remove(formText(form, "handle")) : null; if (!outcome) return failed({ code: "invalid", message: "Unknown action." }, {}); return outcome.ok ? redirect(back) : failed(outcome.problem, {}); } /** The agent being edited: `?edit=new` for a new one, `?edit=` for one in the list. */ export function editingFrom(request: Request, list: readonly HostedAgent[], level: Level): { agent: HostedAgent | null; fresh: boolean } | null { const edit = new URL(request.url).searchParams.get("edit"); if (!edit) return null; if (edit === "new") return { agent: null, fresh: true }; const agent = list.find((entry) => entry.handle === edit && (entry.scope === level || !entry.replaced)) ?? null; return agent ? { agent, fresh: agent.scope !== level } : null; } export function HostedAgentsPage({ list, canEdit, level, editing, error }: { list: readonly HostedAgent[]; canEdit: boolean; level: Level; editing: ReturnType; error: string | null }) { return (
{error && } {canEdit && editing ? ( ) : ( canEdit && (
}> Add agent
) )} {list.map((agent) => ( ))}
); } function modelLabel(agent: HostedAgent): string { return agent.model ? `${PROVIDERS[agent.model.provider].label} · ${agent.model.model} · key ending in ${agent.model.keyHint}` : "GLM-5.3 on Workers AI"; } function AgentRow({ agent, canEdit, level }: { agent: HostedAgent; canEdit: boolean; level: Level }) { const own = agent.scope === level; return (
@{agent.handle} {SCOPE_LABEL[agent.scope]} {agent.replaced && Replaced here} {agent.description && {agent.description}} {modelLabel(agent)}
{canEdit && own && (
Edit
)} {canEdit && !own && !agent.replaced && ( Customize )}
); } function AgentEditor({ agent, customizing, level }: { agent: HostedAgent | null; customizing: boolean; level: Level }) { const navigation = useNavigation(); const saving = navigation.state === "submitting" && navigation.formData?.get("intent") === "save"; const saved = agent && !customizing ? agent.model : null; const [provider, setProvider] = useState<"" | ModelProvider>(saved?.provider ?? ""); const title = !agent ? "Add agent" : customizing ? `Customize @${agent.handle} for this ${level === "repo" ? "repository" : "namespace"}` : `Edit @${agent.handle}`; return (

{title}

{customizing &&

Your copy replaces @{agent!.handle} here. Remove it to go back to the original.

}
)} {provider ? ( ) : (

The agent runs GLM-5.3 on Workers AI, and refs pays for it.

)}
Cancel
); } const RUN_TEXT: Record = { working: "is working", answered: "answered", failed: "did not finish" }; /** The recent runs of the repository's agents. A run that still works shows each step as it lands. */ export function AgentActivity({ runs, repo, open, now }: { runs: readonly AgentRunView[]; repo: string; open: string | null; now: number }) { return (

Activity

{runs.length === 0 ? ( } title="No agent runs yet" description="Mention an agent on a pull request, or add an agent job to .refs/ci.yml." /> ) : ( {runs.map((run) => (
@{run.handle} {RUN_TEXT[run.state]} {run.pull !== null && ( on #{run.pull} )} {run.via === "ci" && CI} for {run.person} ·
{run.summary && run.state !== "working" &&

{run.summary}

} {run.steps ? ( ) : null} {run.state !== "working" && ( {open === run.session ? "Hide thinking" : "Show thinking"} )}
))}
)}
); } function RunSteps({ steps, working }: { steps: NonNullable; working: boolean }) { if (!steps.length) return

{working ? "Reading the pull request…" : "This run recorded no steps."}

; return (
    {steps.map((step) => (
  1. Step {step.step} {step.tools.length ? ` · ${step.tools.join(" · ")}` : " · writing the answer"} {step.thinking &&

    {step.thinking}

    }
  2. ))}
); } function RunIcon({ state }: { state: AgentRunView["state"] }) { if (state === "answered") return ; if (state === "failed") return ; return ; }