Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

15.6 KB
Raw Blame
"use client";

import { useState } from "react";
import { WorkflowLearningPanel } from "./workflow-learning-panel";
import { BrainCircuit, Code2, Copy, ExternalLink, FileCode2, GitCompareArrows, History, Save, TerminalSquare } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import type { SessionFlowAction, SessionFlowStep, SessionGraphView } from "@/lib/contracts/session-monitor";
import type { WorkflowGraphNode } from "@/lib/contracts/workflow-graph";
import { getSessionArtifactHistory, optimizeSessionWithJev, type JevSessionOptimization, type SessionArtifactHistoryResponse } from "../lib/api";

function value(config: Record<string, unknown>, key: string): string | undefined {
  const raw = config[key]; return typeof raw === "string" && raw ? raw : undefined;
}
function ArtifactRevisionInspector({ sessionId, action }: { sessionId: string; action: SessionFlowAction }) {
  const [result, setResult] = useState<SessionArtifactHistoryResponse | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  if (!action.artifact) return null;
  const inspect = async () => {
    setLoading(true); setError(null);
    try { setResult(await getSessionArtifactHistory(sessionId, action.ref)); }
    catch (cause) { setError(cause instanceof Error ? cause.message : "artifact history unavailable"); }
    finally { setLoading(false); }
  };
  const history = result?.history;
  return <div data-slot="session-artifact-history" className="space-y-2">
    <Button type="button" size="sm" variant="outline" className="h-7 text-[10px]" disabled={loading} onClick={() => void inspect()}>
      <History className="size-3.5"/>{loading ? "Checking revision…" : result ? "Refresh revision" : "Inspect revision"}
    </Button>
    {error ? <p className="text-[10px] text-destructive">{error}</p> : null}
    {history ? <div className="space-y-1.5 rounded-md border bg-muted/20 p-2 text-[10px]">
      <div className="flex flex-wrap items-center gap-1.5">
        <Badge variant="outline" className="h-5 px-1.5 text-[9px]">{history.capture.state === "legacy" ? "legacy · no proof" : history.capture.exactAtCapture ? "exact capture" : "capture proof"}</Badge>
        {history.capture.sha256 ? <code className="text-[9px] text-muted-foreground">sha256 {history.capture.sha256.slice(0, 12)}</code> : null}
        {history.capture.gitHead ? <code className="text-[9px] text-muted-foreground">git {history.capture.gitHead.slice(0, 12)}</code> : null}
      </div>
      <p className="text-muted-foreground">{history.capture.state === "legacy" ? "This older action predates artifact capture proof, so MSO will not guess a historical snapshot." : history.historical.available ? "Historical identity is proven. Source previews below are redacted before display." : "Capture metadata exists, but an exact historical body is no longer provable."}</p>
      {history.current.available ? <p className="text-muted-foreground">Current file · {history.current.matchesCapture ? "matches captured revision" : "changed since capture"} · {history.current.bytes ?? 0} bytes</p> : <p className="text-muted-foreground">Current file is unavailable.</p>}
      {history.historical.available ? <details data-slot="session-artifact-snapshot" className="rounded border bg-background/40">
        <summary className="cursor-pointer px-2 py-1.5 font-medium">Historical snapshot · exact · {history.historical.source}</summary>
        <div className="border-t p-2">{history.historical.truncated ? <p className="text-muted-foreground">Exact hash/size retained; preview omitted because the snapshot exceeds the preview limit.</p> : <pre className="max-h-56 overflow-auto whitespace-pre p-2 font-mono text-[10px] leading-4"><code>{history.historical.content ?? ""}</code></pre>}</div>
      </details> : null}
      {history.diff.available ? <details data-slot="session-artifact-diff" className="rounded border bg-background/40">
        <summary className="flex cursor-pointer items-center gap-1.5 px-2 py-1.5 font-medium"><GitCompareArrows className="size-3"/>Historical ↔ current · {history.diff.changed ? "changed" : "no changes"}</summary>
        <div className="border-t p-2"><pre className="max-h-64 overflow-auto whitespace-pre p-2 font-mono text-[10px] leading-4"><code>{history.diff.unifiedDiff || "No changes."}</code></pre></div>
      </details> : null}
    </div> : null}
  </div>;
}

function actionReceipt(action: SessionFlowAction, sessionId: string, onOpenTerminal: () => void, onOpenCode: (path: string) => void) {
  return <details key={action.ref} data-slot="session-action" data-action-ref={action.ref} className="group rounded-lg border bg-background/35">
    <summary data-slot="session-action-summary" aria-label={`Inspect action ${action.ref}: ${action.title}`} className="flex cursor-pointer list-none items-start gap-2 p-2.5 text-xs marker:hidden">
      <code className="shrink-0 rounded bg-muted px-1.5 py-0.5 font-mono text-[10px] font-semibold">{action.ref}</code>
      <div className="min-w-0 flex-1"><div className="break-words font-medium leading-4">{action.title}</div><div className="mt-0.5 text-[10px] text-muted-foreground">{action.tool || action.kind}{action.state ? ` · ${action.state}` : ""}</div></div>
    </summary>
    <div className="space-y-2 border-t p-2.5">
      {action.detail ? <div><div className="text-[10px] uppercase text-muted-foreground">Detail</div><p className="mt-1 whitespace-pre-wrap break-words text-[11px] [overflow-wrap:anywhere]">{action.detail}</p></div> : null}
      {action.code ? <div className="overflow-hidden rounded-md border bg-muted/30"><div className="flex items-center gap-2 border-b px-2 py-1 text-[10px] text-muted-foreground"><Code2 className="size-3"/><span>{action.code.kind} · {action.code.language}</span><Button type="button" variant="ghost" size="icon" className="ml-auto size-6" aria-label={`Copy ${action.ref} code`} onClick={() => void navigator.clipboard?.writeText(action.code!.content)}><Copy className="size-3"/></Button></div><pre className="max-h-56 overflow-auto whitespace-pre p-2.5 font-mono text-[10px] leading-4"><code>{action.code.content}</code></pre></div> : null}
      <div className="flex flex-wrap gap-1.5">
        {action.terminalContext ? <Button type="button" size="sm" variant="outline" className="h-7 text-[10px]" onClick={onOpenTerminal}><TerminalSquare className="size-3.5"/>Open terminal here<ExternalLink className="size-3"/></Button> : null}
        {action.artifact ? <Button type="button" size="sm" variant="outline" className="h-7 text-[10px]" onClick={() => onOpenCode(action.artifact!.path)}><FileCode2 className="size-3.5"/>Open {action.artifact.label} in Code<ExternalLink className="size-3"/></Button> : null}
      </div>
      {action.artifact ? <ArtifactRevisionInspector sessionId={sessionId} action={action}/> : null}
      <div className="flex flex-wrap gap-x-3 gap-y-1 text-[9px] text-muted-foreground"><span>{new Date(action.at).toLocaleString()}</span><span>{action.eventRef}</span>{action.artifact ? <span className="break-all">{action.artifact.relativePath} · revision {action.artifact.revisionRef}</span> : null}</div>
    </div>
  </details>;
}

function actionGroups(step: SessionFlowStep, sessionId: string, onOpenTerminal: () => void, onOpenCode: (path: string) => void) {
  return step.groups.map((group) => {
    const refs = new Set(group.actionRefs);
    const actions = step.actions.filter((action) => refs.has(action.ref));
    return <details key={group.ref} data-slot="session-action-group" data-action-group-ref={group.ref} className="rounded-lg border bg-muted/15">
      <summary className="flex cursor-pointer list-none items-center gap-2 px-2.5 py-2 text-xs marker:hidden">
        <span className="min-w-0 flex-1 truncate font-medium">{group.title}</span>
        <Badge variant="outline" className="h-5 shrink-0 px-1.5 text-[9px]">{group.count}</Badge>
      </summary>
      <div className="space-y-1.5 border-t p-2">{actions.map((action) => actionReceipt(action, sessionId, onOpenTerminal, onOpenCode))}</div>
    </details>;
  });
}

export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode, onSaveDraft, savingDraft = false }: { view: SessionGraphView; node: WorkflowGraphNode | null; onOpenTerminal: () => void; onOpenCode: (path: string) => void; onSaveDraft: (stepRef?: string) => void; savingDraft?: boolean }) {
  const ref = node ? value(node.config, "ref") : undefined;
  const step: SessionFlowStep | undefined = ref ? view.steps.find((item) => item.ref === ref) : undefined;
  const [jev, setJev] = useState<JevSessionOptimization | null>(null);
  const [jevBusy, setJevBusy] = useState(false);
  const [jevError, setJevError] = useState("");
  const runJev = async () => { setJevBusy(true); setJevError(""); try { setJev(await optimizeSessionWithJev(view.session.id)); } catch (cause) { setJevError(cause instanceof Error ? cause.message : "JEV unavailable"); } finally { setJevBusy(false); } };
  return <div className="flex h-full min-h-0 flex-col bg-card/20">
    <div className="border-b p-3">
      <div className="break-words text-sm font-semibold">{view.session.label}</div>
      <div className="mt-1 flex flex-wrap items-center gap-1.5 text-[10px] uppercase text-muted-foreground"><span>{view.session.source}</span><span>·</span><span>{view.session.status}</span><Badge variant="outline" className="h-5 px-1.5 text-[9px] normal-case">{view.steps.length} semantic steps</Badge></div>
      <p className="mt-2 text-xs text-muted-foreground">Canvas shows intent-level steps. Click a step to inspect its grouped actions, commands, code, artifacts, and raw execution receipt.</p>
      <div className="mt-2 flex flex-wrap items-center gap-2"><Button data-slot="use-jev-session" type="button" size="sm" variant="outline" className="h-7 text-[10px]" disabled={jevBusy} onClick={() => void runJev()}><BrainCircuit className="size-3.5"/>{jevBusy ? "Calculating…" : "Use JEV"}</Button><span className="text-[10px] text-muted-foreground">Review-first optimization packet for this session; no tools are executed.</span></div>
      {jevError ? <p className="mt-2 text-[10px] text-destructive">{jevError}</p> : null}
      {jev ? <div data-slot="jev-session-output" className="mt-2 rounded-lg border bg-muted/15 p-2.5 text-[10px]"><div className="flex items-center gap-2"><Badge variant="outline" className="h-5 text-[9px]">JEV DECISION</Badge><Badge variant="outline" className="h-5 text-[9px]">{jev.provider}</Badge><span>{jev.summary.acceptedCount} accepted · {jev.summary.rejectedCount} rejected / {jev.summary.candidateCount} candidates</span><Button type="button" size="sm" variant="ghost" className="ml-auto h-6 text-[9px]" onClick={() => void navigator.clipboard?.writeText(JSON.stringify(jev.llmContext,null,2))}><Copy className="size-3"/>Copy for LLM</Button></div><div data-slot="jev-decision-metadata" className="mt-2 grid gap-1 rounded border bg-background/40 p-2 text-[9px] text-muted-foreground sm:grid-cols-2"><span>Decision v{jev.decision.version} · threshold {Math.round(jev.decision.threshold*100)}%</span><span>{jev.decision.model ?? "~typesafe/jev-latest"}{jev.decision.latencyMs != null ? ` · ${jev.decision.latencyMs} ms` : ""}</span><span>Provider input {jev.decision.inputTokens != null ? `${jev.decision.inputTokens} tokens` : "not reported"}</span><span>Actual JEV cost {jev.decision.actualCostUsd != null ? `${jev.decision.actualCostUsd.toFixed(6)}` : "not reported"}</span></div><div data-slot="jev-capsule-summary" className="mt-2 rounded border bg-background/40 p-2 text-[9px] text-muted-foreground">Continuation capsule · {jev.valueSummary.semanticStepCount} semantic steps · {jev.valueSummary.toolCount} unique tools · {jev.valueSummary.artifactRefCount} artifact refs</div><div data-slot="jev-action-distinction" className="mt-2 flex flex-wrap gap-1.5"><Badge variant="outline" className="h-5 text-[9px]">MCP ACTION · not executed</Badge><Badge variant="outline" className="h-5 text-[9px]">VERIFIED RESULT · n/a</Badge></div>{jev.fallbackReason ? <p className="mt-1 text-muted-foreground">{jev.fallbackReason}</p> : null}<div className="mt-2 space-y-1">{jev.recommendations.slice(0,6).map(item=><div key={item.id} className="rounded border bg-background/40 p-2"><div className="font-medium">{Math.round(item.probability*100)}% · {item.title}</div><p className="mt-0.5 text-muted-foreground">{item.description}</p><div className="mt-1 font-mono text-[9px] text-muted-foreground">{item.actionRefs.slice(0,8).join(" · ")}</div></div>)}</div></div> : null}
      {view.omittedEvents ? <p className="mt-2 text-[10px] text-muted-foreground">Projection uses the latest {view.shownEvents} of {view.totalEvents} events to stay fast. Raw session history remains the source of truth.</p> : null}
    </div>
    <ScrollArea className="min-h-0 flex-1"><div className="space-y-3 p-3">
      {!node || node.id === "session-root" ? <><div><div className="text-[10px] uppercase text-muted-foreground">Flow</div><div className="mt-1 text-sm font-semibold">{view.session.title}</div><Button data-slot="save-session-workflow-draft" type="button" size="sm" variant="outline" className="mt-2 h-7 text-[10px]" disabled={savingDraft} onClick={() => onSaveDraft()}><Save className="size-3.5"/>Save session as workflow draft</Button><p className="mt-1.5 max-w-sm text-[10px] text-muted-foreground">Saved Session workflows can be reused by workflow ID in Subflow or Repeat Until nodes.</p></div><div className="space-y-1.5">{view.steps.map((item) => <div key={item.ref} className="rounded-lg border bg-background/35 p-2"><div className="flex items-center gap-2"><code className="rounded bg-muted px-1.5 py-0.5 text-[10px] font-semibold">{item.ref}</code><span className="text-xs font-medium">{item.title}</span><span className="ml-auto text-[10px] text-muted-foreground">{item.actions.length} actions</span></div><p className="mt-1 text-[10px] text-muted-foreground">{item.summary}</p></div>)}</div></> : step ? <>
        <div><div className="flex items-center gap-2"><code className="rounded bg-muted px-1.5 py-0.5 font-mono text-[10px] font-semibold">{step.ref}</code><Badge variant="outline" className="text-[9px] capitalize">{step.category}</Badge></div><div className="mt-2 break-words text-sm font-semibold">{step.title}</div><p className="mt-1 text-xs text-muted-foreground">{step.summary}</p><Button data-slot="save-step-workflow-draft" type="button" size="sm" variant="outline" className="mt-2 h-7 text-[10px]" disabled={savingDraft} onClick={() => onSaveDraft(step.ref)}><Save className="size-3.5"/>Save {step.ref} as workflow draft</Button></div>
        <div className="border-t pt-3"><div className="mb-2 flex items-center justify-between"><div className="text-[10px] font-semibold uppercase text-muted-foreground">Action groups</div><span className="text-[10px] text-muted-foreground">{step.groups.length} groups · {step.actions.length} actions</span></div><div className="space-y-1.5">{actionGroups(step, view.session.id, onOpenTerminal, onOpenCode)}</div></div>
      </> : <p className="text-xs text-muted-foreground">Select a semantic step to inspect its actions.</p>}
      <WorkflowLearningPanel key={view.session.id} sessionLabel={view.session.label}/>
      {view.session.cwd ? <div className="border-t pt-3"><div className="text-[10px] uppercase text-muted-foreground">Session context</div><p className="mt-1 break-all font-mono text-[10px] text-muted-foreground">{view.session.cwd}</p></div> : null}
    </div></ScrollArea>
    <div className="border-t p-3 text-[10px] text-muted-foreground">Use references such as <code>S3.A4</code> when discussing a specific action. MSO keeps internal session IDs hidden from this surface.</div>
  </div>;
}