|
| 1 | +/** |
| 2 | + * Browser-safe OAR observability, branding, and runtime configuration form |
| 3 | + * adapter for LoopX Dashboard. |
| 4 | + * |
| 5 | + * Imports ONLY browser-safe `@botiverse/oar` subpaths (`@botiverse/oar/observe`, |
| 6 | + * `@botiverse/oar/brands`, `@botiverse/oar/forms`) with zero Node built-ins. |
| 7 | + */ |
| 8 | +import type { RawEvent, TurnOutcome } from "@botiverse/oar/browser"; |
| 9 | +import { |
| 10 | + runtimeBrandIcon, |
| 11 | + runtimeBrands, |
| 12 | + type RuntimeBrand, |
| 13 | +} from "@botiverse/oar/brands"; |
| 14 | +import { |
| 15 | + configForms, |
| 16 | + type RuntimeConfigForm, |
| 17 | +} from "@botiverse/oar/forms"; |
| 18 | +import { |
| 19 | + classifyTool, |
| 20 | + contextUsageOf, |
| 21 | + failureAdvice, |
| 22 | + failureText, |
| 23 | + groupToolActivity, |
| 24 | + initialConversation, |
| 25 | + initialSessionView, |
| 26 | + initialTaskState, |
| 27 | + noticeText, |
| 28 | + noticeTone, |
| 29 | + observeStalls, |
| 30 | + phaseLabel, |
| 31 | + reduceConversation, |
| 32 | + reduceSessionView, |
| 33 | + reduceTaskState, |
| 34 | + statusOf, |
| 35 | + taskStatusLabel, |
| 36 | + toolActionLabel, |
| 37 | + toolGroupSummary, |
| 38 | + usageOf, |
| 39 | + type AgentStatus, |
| 40 | + type ConversationState, |
| 41 | + type FailureAdvice, |
| 42 | + type SessionView, |
| 43 | + type StallInfo, |
| 44 | + type TaskState, |
| 45 | +} from "@botiverse/oar/observe"; |
| 46 | + |
| 47 | +export type OarThemeMode = "light" | "dark"; |
| 48 | + |
| 49 | +export interface OarRuntimeBrandSummary { |
| 50 | + readonly id: string; |
| 51 | + readonly name: string; |
| 52 | + readonly icon: string | null; |
| 53 | +} |
| 54 | + |
| 55 | +export interface OarReducedSessionSnapshot { |
| 56 | + readonly view: SessionView; |
| 57 | + readonly conversation: ConversationState; |
| 58 | + readonly tasks: TaskState; |
| 59 | + readonly status: AgentStatus; |
| 60 | + readonly phaseText: string; |
| 61 | + readonly usage: ReturnType<typeof usageOf>["value"]; |
| 62 | + readonly contextUsage: ReturnType<typeof contextUsageOf>["value"]; |
| 63 | +} |
| 64 | + |
| 65 | +const RUNTIME_BRAND_ALIASES: Readonly<Record<string, keyof typeof runtimeBrands>> = { |
| 66 | + codex: "codex", |
| 67 | + "codex-cli": "codex", |
| 68 | + codex_cli: "codex", |
| 69 | + claude: "claude", |
| 70 | + "claude-code": "claude", |
| 71 | + claude_code: "claude", |
| 72 | + dsh: "dsh", |
| 73 | + "deepseek-harness": "dsh", |
| 74 | + deepseek_harness: "dsh", |
| 75 | + deepseek_harness_segment: "dsh", |
| 76 | + pi: "pi", |
| 77 | + "pi-cli": "pi", |
| 78 | + "pi-durable": "pi-durable", |
| 79 | + pi_durable: "pi-durable", |
| 80 | + opencode: "opencode", |
| 81 | + opencode2: "opencode", |
| 82 | + antigravity: "antigravity", |
| 83 | + "antigravity-cli": "antigravity", |
| 84 | + "agy-cli": "antigravity", |
| 85 | + agy: "antigravity", |
| 86 | + kimi: "kimi", |
| 87 | + "kimi-cli": "kimi", |
| 88 | + qwen: "qwen", |
| 89 | + devin: "devin", |
| 90 | + factory: "factory", |
| 91 | + droid: "factory", |
| 92 | + grok: "grok", |
| 93 | + codebuddy: "codebuddy", |
| 94 | + qoder: "qoder", |
| 95 | + qodercli: "qoder", |
| 96 | + cursor: "cursor", |
| 97 | + "cursor-cli": "cursor", |
| 98 | + bottega: "bottega", |
| 99 | +}; |
| 100 | + |
| 101 | +/** Normalize a LoopX agent_id or adapter alias onto its canonical OAR brand key. */ |
| 102 | +export function canonicalOarBrandId(rawId: string): keyof typeof runtimeBrands | null { |
| 103 | + const token = rawId.trim().toLowerCase(); |
| 104 | + return RUNTIME_BRAND_ALIASES[token] ?? null; |
| 105 | +} |
| 106 | + |
| 107 | +/** Resolve offline SVG brand icon + display label for any OAR runtime or alias. */ |
| 108 | +export function resolveOarRuntimeBrand( |
| 109 | + runtimeOrAgentId: string, |
| 110 | + theme: OarThemeMode = "light", |
| 111 | +): OarRuntimeBrandSummary | null { |
| 112 | + const canonical = canonicalOarBrandId(runtimeOrAgentId); |
| 113 | + if (!canonical) return null; |
| 114 | + const brand = runtimeBrands[canonical] as RuntimeBrand; |
| 115 | + return { |
| 116 | + id: canonical, |
| 117 | + name: brand.name, |
| 118 | + icon: runtimeBrandIcon(brand, theme), |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +/** Return all 16 OAR built-in runtime brands resolved for the active theme. */ |
| 123 | +export function listOarRuntimeBrands(theme: OarThemeMode = "light"): readonly OarRuntimeBrandSummary[] { |
| 124 | + return (Object.keys(runtimeBrands) as (keyof typeof runtimeBrands)[]).map((id) => { |
| 125 | + const brand = runtimeBrands[id] as RuntimeBrand; |
| 126 | + return { |
| 127 | + id, |
| 128 | + name: brand.name, |
| 129 | + icon: runtimeBrandIcon(brand, theme), |
| 130 | + }; |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +/** Look up OAR's declarative configuration form schema for a runtime ID or alias. */ |
| 135 | +export function getOarRuntimeConfigForm(runtimeOrAgentId: string): RuntimeConfigForm | null { |
| 136 | + const canonical = canonicalOarBrandId(runtimeOrAgentId); |
| 137 | + if (!canonical) return null; |
| 138 | + return configForms[canonical] ?? null; |
| 139 | +} |
| 140 | + |
| 141 | +/** Return all declared OAR runtime configuration forms indexed by runtime ID. */ |
| 142 | +export function listOarRuntimeConfigForms(): Readonly<Record<string, RuntimeConfigForm>> { |
| 143 | + return configForms; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Pure fold from OAR `RawEvent[]` records into Dashboard-ready session view, |
| 148 | + * conversation transcript, task state, agent status, and usage projections. |
| 149 | + */ |
| 150 | +export function reduceOarRecordsToView( |
| 151 | + sessionId: string, |
| 152 | + records: readonly RawEvent[], |
| 153 | + runtimeId?: string, |
| 154 | +): OarReducedSessionSnapshot { |
| 155 | + let view = initialSessionView(sessionId); |
| 156 | + let conversation = initialConversation(); |
| 157 | + let tasks = initialTaskState(); |
| 158 | + for (const record of records) { |
| 159 | + view = reduceSessionView(view, record, sessionId); |
| 160 | + conversation = reduceConversation(conversation, record, sessionId); |
| 161 | + tasks = reduceTaskState(tasks, record); |
| 162 | + } |
| 163 | + const status = statusOf(records, sessionId).value; |
| 164 | + const phase = status.kind === "running" ? phaseLabel(status.phase, runtimeId) : status.kind; |
| 165 | + return { |
| 166 | + view, |
| 167 | + conversation, |
| 168 | + tasks, |
| 169 | + status, |
| 170 | + phaseText: phase, |
| 171 | + usage: usageOf(records, sessionId).value, |
| 172 | + contextUsage: contextUsageOf(records, sessionId).value, |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +/** Format a failed OAR `TurnOutcome` into user-safe message + typed recovery advice. */ |
| 177 | +export function formatOarTurnFailure( |
| 178 | + outcome: Extract<TurnOutcome, { readonly kind: "failed" }>, |
| 179 | + runtimeName = "Agent", |
| 180 | +): { |
| 181 | + readonly text: string; |
| 182 | + readonly advice: FailureAdvice; |
| 183 | +} { |
| 184 | + const credential = "credential" in outcome ? outcome.credential : undefined; |
| 185 | + return { |
| 186 | + text: failureText(outcome.failure, runtimeName, credential), |
| 187 | + advice: failureAdvice(outcome.failure), |
| 188 | + }; |
| 189 | +} |
| 190 | + |
| 191 | +export { |
| 192 | + classifyTool, |
| 193 | + groupToolActivity, |
| 194 | + noticeText, |
| 195 | + noticeTone, |
| 196 | + observeStalls, |
| 197 | + phaseLabel, |
| 198 | + taskStatusLabel, |
| 199 | + toolActionLabel, |
| 200 | + toolGroupSummary, |
| 201 | + type StallInfo, |
| 202 | +}; |
0 commit comments