Ops-dashboard/lib/agent-client.ts
Scrum4Me Agent 90eacc963d feat(docker): agent-client helper, Docker container list page
- lib/agent-client.ts: server-side execAgent() that calls the ops-agent
  directly via OPS_AGENT_URL/OPS_AGENT_SECRET and streams SSE output
- lib/parse-docker.ts: pure parser for fixed-width docker ps table output
- app/docker/page.tsx: server component that fetches initial container list
  and passes it to the client component
- app/docker/_components/docker-table.tsx: client component with 5s
  auto-refresh via useEffect, status badge, and Link to /docker/[name]
- app/docker/[name]/page.tsx: placeholder detail page (logs in Story 3)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-13 17:27:35 +02:00

58 lines
1.5 KiB
TypeScript

import 'server-only'
const AGENT_URL = process.env.OPS_AGENT_URL ?? 'http://127.0.0.1:3099'
const AGENT_SECRET = process.env.OPS_AGENT_SECRET ?? ''
export async function execAgent(
commandKey: string,
args: string[] = [],
onChunk?: (chunk: string) => void,
): Promise<string> {
const response = await fetch(`${AGENT_URL}/agent/v1/exec`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${AGENT_SECRET}`,
},
body: JSON.stringify({ command_key: commandKey, args }),
cache: 'no-store',
})
if (!response.ok) {
const text = await response.text()
throw new Error(`agent error ${response.status}: ${text}`)
}
const reader = response.body?.getReader()
if (!reader) throw new Error('no response body')
const decoder = new TextDecoder()
let buffer = ''
let output = ''
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n')
buffer = lines.pop() ?? ''
for (const line of lines) {
if (line.startsWith('data:')) {
const jsonStr = line.slice(5).trim()
try {
const parsed = JSON.parse(jsonStr) as { data?: string }
if (parsed.data !== undefined) {
output += parsed.data
onChunk?.(parsed.data)
}
} catch {
// skip malformed SSE data
}
}
}
}
return output
}