|
| 1 | +import { useEffect, useMemo, useRef, useState } from "react"; |
1 | 2 | import { Fragment } from "react/jsx-runtime"; |
2 | 3 | import { useAgenticSelector } from "../../../../containers/Agentic/hooks"; |
3 | | -import { useGetIncidentAgentEventsQuery } from "../../../../redux/services/digma"; |
| 4 | +import { |
| 5 | + useGetIncidentAgentEventsQuery, |
| 6 | + useGetIncidentAgentsQuery |
| 7 | +} from "../../../../redux/services/digma"; |
| 8 | +import type { GetIncidentAgentEventsResponse } from "../../../../redux/services/types"; |
| 9 | +import { isBoolean } from "../../../../typeGuards/isBoolean"; |
| 10 | +import { isNumber } from "../../../../typeGuards/isNumber"; |
| 11 | +import { isUndefined } from "../../../../typeGuards/isUndefined"; |
4 | 12 | import { ThreeCirclesSpinner } from "../../../common/ThreeCirclesSpinner"; |
| 13 | +import { TypingMarkdown } from "../TypingMarkdown"; |
| 14 | +import { Accordion } from "./Accordion"; |
5 | 15 | import * as s from "./styles"; |
6 | 16 |
|
7 | 17 | const REFRESH_INTERVAL = 10 * 1000; // in milliseconds |
| 18 | +const AUTO_SCROLL_THRESHOLD = 10; // in pixels |
| 19 | +const TYPING_SPEED = 3; // in milliseconds per character |
8 | 20 |
|
9 | | -const getMessage = (message: string) => { |
| 21 | +const convertToMarkdown = (text: string) => { |
10 | 22 | try { |
11 | | - const parsedMessage: unknown = JSON.parse(message); |
12 | | - return JSON.stringify(parsedMessage, null, 2); |
| 23 | + // First try to parse as JSON |
| 24 | + const parsedJSON = JSON.parse(text) as unknown; |
| 25 | + const formattedJSON = JSON.stringify(parsedJSON, null, 2); |
| 26 | + return `\`\`\`json\n${formattedJSON}\n\`\`\``; |
13 | 27 | } catch { |
14 | | - return message; |
| 28 | + // If JSON parsing fails, check if it looks like structured data |
| 29 | + const trimmed = text.trim(); |
| 30 | + |
| 31 | + // Check for Python list/object representation patterns |
| 32 | + if ( |
| 33 | + (trimmed.startsWith("[") && trimmed.endsWith("]")) || |
| 34 | + (trimmed.startsWith("(") && trimmed.endsWith(")")) || |
| 35 | + (trimmed.includes("(") && trimmed.includes("=")) // Constructor-like syntax |
| 36 | + ) { |
| 37 | + return `\`\`\`python\n${text}\n\`\`\``; |
| 38 | + } |
| 39 | + |
| 40 | + return text; |
15 | 41 | } |
16 | 42 | }; |
17 | 43 |
|
18 | 44 | export const AgentEvents = () => { |
19 | 45 | const incidentId = useAgenticSelector((state) => state.incidents.incidentId); |
20 | 46 | const agentId = useAgenticSelector((state) => state.incidents.agentId); |
| 47 | + const [initialAgentRunning, setInitialAgentRunning] = useState<boolean>(); |
| 48 | + const [eventsVisibleCount, setEventsVisibleCount] = useState<number>(); |
| 49 | + const [data, setData] = useState<GetIncidentAgentEventsResponse>(); |
| 50 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 51 | + const [shouldAutoScroll, setShouldAutoScroll] = useState(true); |
| 52 | + const scrollHeightRef = useRef<number>(0); |
21 | 53 |
|
22 | | - const { data, isLoading } = useGetIncidentAgentEventsQuery( |
| 54 | + const { data: agentsData } = useGetIncidentAgentsQuery( |
| 55 | + { id: incidentId ?? "" }, |
| 56 | + { |
| 57 | + pollingInterval: REFRESH_INTERVAL, |
| 58 | + skip: !incidentId |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + const { data: agentEventsData } = useGetIncidentAgentEventsQuery( |
23 | 63 | { incidentId: incidentId ?? "", agentId: agentId ?? "" }, |
24 | 64 | { |
25 | 65 | pollingInterval: REFRESH_INTERVAL, |
26 | | - skip: !incidentId || !agentId |
| 66 | + skip: !incidentId || !agentId || !isBoolean(initialAgentRunning) |
27 | 67 | } |
28 | 68 | ); |
29 | 69 |
|
30 | | - if (isLoading) { |
31 | | - return <ThreeCirclesSpinner />; |
32 | | - } |
| 70 | + const handleMarkdownTypingComplete = (i: number) => () => { |
| 71 | + const events = data ?? []; |
| 72 | + const tokenEventsIndexes = events.reduce((acc, event, index) => { |
| 73 | + if (event.type === "token") { |
| 74 | + acc.push(index); |
| 75 | + } |
| 76 | + return acc; |
| 77 | + }, [] as number[]); |
| 78 | + |
| 79 | + const nextTokenEventIndex = tokenEventsIndexes.find((el) => el > i); |
| 80 | + |
| 81 | + if (isNumber(nextTokenEventIndex) && nextTokenEventIndex >= 0) { |
| 82 | + setEventsVisibleCount(nextTokenEventIndex + 1); |
| 83 | + } else { |
| 84 | + setEventsVisibleCount(events.length); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + const handleContainerScroll = () => { |
| 89 | + const isAtBottom = () => { |
| 90 | + if (!containerRef.current) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + const { scrollTop, scrollHeight, clientHeight } = containerRef.current; |
| 94 | + return scrollHeight - scrollTop <= clientHeight + AUTO_SCROLL_THRESHOLD; |
| 95 | + }; |
| 96 | + |
| 97 | + if (!containerRef.current) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + setShouldAutoScroll(isAtBottom()); |
| 102 | + }; |
| 103 | + |
| 104 | + const scrollToBottom = () => { |
| 105 | + if (containerRef.current) { |
| 106 | + containerRef.current.scrollTop = containerRef.current.scrollHeight; |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + // Handle scroll height changes and auto-scroll |
| 111 | + useEffect(() => { |
| 112 | + const element = containerRef.current; |
| 113 | + if (!element) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const checkScrollHeight = () => { |
| 118 | + const currentScrollHeight = element.scrollHeight; |
| 119 | + |
| 120 | + // Only auto-scroll if height has grown and auto-scroll is enabled |
| 121 | + if (currentScrollHeight > scrollHeightRef.current && shouldAutoScroll) { |
| 122 | + scrollToBottom(); |
| 123 | + } |
| 124 | + |
| 125 | + scrollHeightRef.current = currentScrollHeight; |
| 126 | + }; |
| 127 | + |
| 128 | + const mutationObserver = new MutationObserver(() => { |
| 129 | + // Use RAF to ensure DOM is updated before measuring |
| 130 | + requestAnimationFrame(checkScrollHeight); |
| 131 | + }); |
| 132 | + |
| 133 | + mutationObserver.observe(element, { |
| 134 | + childList: true, |
| 135 | + subtree: true, |
| 136 | + attributes: true, |
| 137 | + characterData: true |
| 138 | + }); |
| 139 | + |
| 140 | + // Initial setup |
| 141 | + scrollHeightRef.current = element.scrollHeight; |
| 142 | + scrollToBottom(); |
| 143 | + |
| 144 | + return () => { |
| 145 | + mutationObserver.disconnect(); |
| 146 | + }; |
| 147 | + }, [shouldAutoScroll]); |
| 148 | + |
| 149 | + useEffect(() => { |
| 150 | + setData(agentEventsData); |
| 151 | + }, [agentEventsData]); |
| 152 | + |
| 153 | + // Set agent initial running state |
| 154 | + useEffect(() => { |
| 155 | + if (!isBoolean(initialAgentRunning)) { |
| 156 | + const agent = agentsData?.agents.find((x) => x.name === agentId); |
| 157 | + setInitialAgentRunning(agent?.running); |
| 158 | + } |
| 159 | + }, [agentsData, agentId, initialAgentRunning]); |
| 160 | + |
| 161 | + // Set initial visible count based on agent initial running state |
| 162 | + useEffect(() => { |
| 163 | + if ( |
| 164 | + isBoolean(initialAgentRunning) && |
| 165 | + isUndefined(eventsVisibleCount) && |
| 166 | + data |
| 167 | + ) { |
| 168 | + const initialCount = initialAgentRunning ? 1 : data.length; |
| 169 | + setEventsVisibleCount(initialCount); |
| 170 | + } |
| 171 | + }, [initialAgentRunning, eventsVisibleCount, data]); |
| 172 | + |
| 173 | + const visibleEvents = useMemo( |
| 174 | + () => data?.slice(0, eventsVisibleCount) ?? [], |
| 175 | + [data, eventsVisibleCount] |
| 176 | + ); |
33 | 177 |
|
34 | 178 | return ( |
35 | | - <s.Container> |
36 | | - {data?.map((x, i) => ( |
37 | | - <Fragment key={i}> |
| 179 | + <s.Container ref={containerRef} onScroll={handleContainerScroll}> |
| 180 | + {visibleEvents.map((x, i) => ( |
| 181 | + <Fragment key={x.type + i}> |
38 | 182 | {x.type === "tool" ? ( |
39 | | - <details key={i}> |
40 | | - <summary> |
41 | | - {x.mcp_name} {x.tool_name} |
42 | | - </summary> |
43 | | - <s.Message>{getMessage(x.message)}</s.Message> |
44 | | - </details> |
| 183 | + <Accordion |
| 184 | + summary={`${x.tool_name} (${[x.mcp_name, "MCP tool"] |
| 185 | + .filter(Boolean) |
| 186 | + .join(" ")})`} |
| 187 | + content={<TypingMarkdown text={convertToMarkdown(x.message)} />} |
| 188 | + /> |
45 | 189 | ) : ( |
46 | | - <s.Message>{x.message}</s.Message> |
| 190 | + <TypingMarkdown |
| 191 | + text={x.message} |
| 192 | + onComplete={ |
| 193 | + initialAgentRunning |
| 194 | + ? handleMarkdownTypingComplete(i) |
| 195 | + : undefined |
| 196 | + } |
| 197 | + speed={initialAgentRunning ? TYPING_SPEED : undefined} |
| 198 | + /> |
47 | 199 | )} |
48 | 200 | </Fragment> |
49 | 201 | ))} |
| 202 | + {initialAgentRunning && <ThreeCirclesSpinner />} |
50 | 203 | </s.Container> |
51 | 204 | ); |
52 | 205 | }; |
0 commit comments