|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import type { AgentViewProps } from "../../features/agents/service"; |
| 3 | +import { ActivityTranscript } from "../../features/agents/ActivityTranscript"; |
| 4 | +import { Button } from "../../shared/design-system/ui/Button"; |
| 5 | +import { Select } from "../../shared/design-system/ui/Select"; |
| 6 | +import { meshAgentConfig, type MeshAgentConfig } from "./meshAgent"; |
| 7 | +import type { |
| 8 | + GooseTranscript, |
| 9 | + MeshHistory, |
| 10 | + MeshHistoryRow, |
| 11 | +} from "./meshTranscript"; |
| 12 | + |
| 13 | +/** The Agents/profile host owns framing and tabs; this body only reads history. */ |
| 14 | +export function createMeshTranscriptTab(history: MeshHistory) { |
| 15 | + return function MeshTranscriptTab({ |
| 16 | + agent, |
| 17 | + conversation, |
| 18 | + }: AgentViewProps<MeshAgentConfig>) { |
| 19 | + const workspace = meshAgentConfig(agent.config).workspace; |
| 20 | + const [rows, setRows] = useState<MeshHistoryRow[]>(); |
| 21 | + const [selected, setSelected] = useState(""); |
| 22 | + const [transcript, setTranscript] = useState<GooseTranscript>(); |
| 23 | + const [error, setError] = useState<string>(); |
| 24 | + const [loading, setLoading] = useState(true); |
| 25 | + const [attempt, setAttempt] = useState(0); |
| 26 | + // biome-ignore lint/correctness/useExhaustiveDependencies: attempt is the explicit refresh. |
| 27 | + useEffect(() => { |
| 28 | + const abort = new AbortController(); |
| 29 | + setRows(undefined); |
| 30 | + setSelected(""); |
| 31 | + setTranscript(undefined); |
| 32 | + setError(undefined); |
| 33 | + setLoading(true); |
| 34 | + void history.recent(agent.pubkey, workspace, abort.signal).then( |
| 35 | + (found) => { |
| 36 | + if (abort.signal.aborted) return; |
| 37 | + setRows(found); |
| 38 | + const nearby = found.find( |
| 39 | + (row) => |
| 40 | + row.channelId === conversation?.channelId && |
| 41 | + (!row.root || row.root === conversation?.rootId), |
| 42 | + ); |
| 43 | + setSelected((nearby ?? found[0])?.sessionId ?? ""); |
| 44 | + setLoading(false); |
| 45 | + }, |
| 46 | + (reason: unknown) => { |
| 47 | + if (abort.signal.aborted) return; |
| 48 | + setError(String(reason)); |
| 49 | + setLoading(false); |
| 50 | + }, |
| 51 | + ); |
| 52 | + return () => abort.abort(); |
| 53 | + }, [ |
| 54 | + agent.pubkey, |
| 55 | + workspace, |
| 56 | + conversation?.channelId, |
| 57 | + conversation?.rootId, |
| 58 | + attempt, |
| 59 | + ]); |
| 60 | + useEffect(() => { |
| 61 | + const row = rows?.find((item) => item.sessionId === selected); |
| 62 | + if (!row) return; |
| 63 | + const abort = new AbortController(); |
| 64 | + setLoading(true); |
| 65 | + setTranscript(undefined); |
| 66 | + setError(undefined); |
| 67 | + void history.transcript(agent.pubkey, workspace, row, abort.signal).then( |
| 68 | + (found) => { |
| 69 | + if (abort.signal.aborted) return; |
| 70 | + setTranscript(found); |
| 71 | + setLoading(false); |
| 72 | + }, |
| 73 | + (reason: unknown) => { |
| 74 | + if (abort.signal.aborted) return; |
| 75 | + setError(String(reason)); |
| 76 | + setLoading(false); |
| 77 | + }, |
| 78 | + ); |
| 79 | + return () => abort.abort(); |
| 80 | + }, [rows, selected, agent.pubkey, workspace]); |
| 81 | + return ( |
| 82 | + <div className="grid gap-4"> |
| 83 | + <div> |
| 84 | + <Button |
| 85 | + variant="ghost" |
| 86 | + loading={loading} |
| 87 | + onClick={() => setAttempt((value) => value + 1)} |
| 88 | + > |
| 89 | + Refresh |
| 90 | + </Button> |
| 91 | + </div> |
| 92 | + {rows && rows.length > 0 && ( |
| 93 | + <Select |
| 94 | + label="Session" |
| 95 | + variant="field" |
| 96 | + value={selected} |
| 97 | + onValueChange={setSelected} |
| 98 | + groups={[ |
| 99 | + { |
| 100 | + label: "Recent sessions", |
| 101 | + options: rows.map((row) => ({ |
| 102 | + value: row.sessionId, |
| 103 | + label: `${new Date(row.at).toLocaleString()} · ${row.root ? "Thread" : "Channel"} ${row.channelId.slice(0, 8)}`, |
| 104 | + })), |
| 105 | + }, |
| 106 | + ]} |
| 107 | + /> |
| 108 | + )} |
| 109 | + {error ? ( |
| 110 | + <p role="alert" className="m-0 text-body-sm"> |
| 111 | + Could not read the transcript: {error}. Try Refresh. |
| 112 | + </p> |
| 113 | + ) : loading ? ( |
| 114 | + <p role="status" className="m-0 text-body-sm text-subtle"> |
| 115 | + Reading saved Mesh sessions… |
| 116 | + </p> |
| 117 | + ) : rows?.length === 0 ? ( |
| 118 | + <p className="m-0 text-body-sm text-subtle"> |
| 119 | + No matching Mesh sessions in recent history for this workspace. Chat |
| 120 | + mode has no tool transcript. Sessions from older builds may not be |
| 121 | + listed. |
| 122 | + </p> |
| 123 | + ) : ( |
| 124 | + transcript && ( |
| 125 | + <ActivityTranscript |
| 126 | + transcript={transcript} |
| 127 | + agentName={agent.name} |
| 128 | + state={() => "unknown"} |
| 129 | + before={ |
| 130 | + <p className="m-0 text-body-sm text-subtle"> |
| 131 | + Saved snapshot; Refresh to see newer activity. |
| 132 | + {transcript.more ? " Showing the latest 50 turns." : ""} |
| 133 | + </p> |
| 134 | + } |
| 135 | + /> |
| 136 | + ) |
| 137 | + )} |
| 138 | + </div> |
| 139 | + ); |
| 140 | + }; |
| 141 | +} |
0 commit comments