| 1 | |
| 2 | export type AgentKind = "claude" | "codex" | "opencode" | "shell"; |
| 3 | export type TermMode = "persistent" | "ephemeral"; |
| 4 | |
| 5 | export interface TermDir { |
| 6 | label: string; |
| 7 | path: string; |
| 8 | } |
| 9 | |
| 10 | export interface TermTarget { |
| 11 | key: string; |
| 12 | ip: string; |
| 13 | label: string; |
| 14 | agents: AgentKind[]; |
| 15 | default_agent: AgentKind; |
| 16 | default_cwd: string; |
| 17 | dirs: TermDir[]; |
| 18 | } |
| 19 | |
| 20 | export interface TargetsResponse { |
| 21 | targets: TermTarget[]; |
| 22 | agent_cmds: Record<string, string>; |
| 23 | common_dirs: TermDir[]; |
| 24 | } |
| 25 | |
| 26 | export interface LiveSession { |
| 27 | target: string; |
| 28 | label: string; |
| 29 | session: string; |
| 30 | agent: string; |
| 31 | created: string; |
| 32 | attached: boolean; |
| 33 | } |
| 34 | |
| 35 | export interface TermCreds { |
| 36 | ok: boolean; |
| 37 | token: string; |
| 38 | host: string; |
| 39 | error?: string; |
| 40 | } |
| 41 | |
| 42 | export const AGENT_LABEL: Record<AgentKind, string> = { |
| 43 | claude: "CLAUDE", |
| 44 | codex: "CODEX", |
| 45 | opencode: "GLM", |
| 46 | shell: "SHELL", |
| 47 | }; |
| 48 | |
| 49 | export async function fetchTermCreds(): Promise<TermCreds> { |
| 50 | const res = await fetch("/api/term", { cache: "no-store" }); |
| 51 | return res.json(); |
| 52 | } |
| 53 | |
| 54 | export async function fetchTargets(_creds: TermCreds): Promise<TargetsResponse> { |
| 55 | const res = await fetch(`/api/term/targets`, { cache: "no-store" }); |
| 56 | return res.json(); |
| 57 | } |
| 58 | |
| 59 | export async function fetchSessions(_creds: TermCreds): Promise<LiveSession[]> { |
| 60 | const res = await fetch(`/api/term/sessions`, { cache: "no-store" }); |
| 61 | const data = await res.json(); |
| 62 | return data.sessions ?? []; |
| 63 | } |
| 64 | |
| 65 | export async function killSession( |
| 66 | _creds: TermCreds, |
| 67 | target: string, |
| 68 | session: string |
| 69 | ): Promise<void> { |
| 70 | const p = new URLSearchParams({ target, session }); |
| 71 | await fetch(`/api/term/kill?${p.toString()}`, { method: "POST" }); |
| 72 | } |
| 73 | |
| 74 | export interface LaunchSpec { |
| 75 | target: string; |
| 76 | agent: AgentKind; |
| 77 | mode: TermMode; |
| 78 | cwd: string; |
| 79 | session?: string; |
| 80 | } |
| 81 | |
| 82 | export function buildWsUrl(creds: TermCreds, spec: LaunchSpec): string { |
| 83 | const p = new URLSearchParams(); |
| 84 | p.set("target", spec.target); |
| 85 | p.set("agent", spec.agent); |
| 86 | p.set("mode", spec.mode); |
| 87 | if (spec.cwd) p.set("cwd", spec.cwd); |
| 88 | if (spec.session) p.set("session", spec.session); |
| 89 | p.set("token", creds.token); |
| 90 | return `ws://${creds.host}/ws?${p.toString()}`; |
| 91 | } |