blob: e23de4941e737e296e588696f3a669073196d71b [file] [log] [blame]
Sean McCullough021231a2025-06-12 09:35:24 -07001import { SketchTimeline } from "./web-components/sketch-timeline";
2import { aggregateAgentMessages } from "./web-components/aggregateAgentMessages";
bankseancad67b02025-06-27 21:57:05 +00003import { State } from "./types";
Sean McCullough021231a2025-06-12 09:35:24 -07004
5// Ensure this dependency ends up in the bundle (the "as SketchTimeline" reference below
6// is insufficient for the bundler to include it).
7SketchTimeline;
8
9export function renderMessagesViewer(viewData: any, container: HTMLDivElement) {
10 const timelineEl = document.createElement(
11 "sketch-timeline",
12 ) as SketchTimeline;
13 // Filter out hidden messages at the display level (matches sketch behavior)
14 const visibleMessages = viewData.Messages.filter(
15 (msg: any) => !msg.hide_output,
16 );
17 const messages = aggregateAgentMessages(visibleMessages, []);
18 timelineEl.messages = messages;
19 timelineEl.toolCalls = viewData.ToolResults;
Sean McCulloughd1832842025-06-20 22:42:06 -040020 timelineEl.scrollContainer = { value: window.document.body };
bankseancad67b02025-06-27 21:57:05 +000021
22 // Create a state object for the timeline component
23 // This ensures user attribution works in archived messages
24 const sessionWithData = viewData.SessionWithData;
25 const state: Partial<State> = {
26 session_id: sessionWithData?.session_id || "",
27 // Use git_username from session state if available, fallback to UserName field, then extract from session info
28 git_username:
29 sessionWithData?.session_state?.git_username ||
30 sessionWithData?.user_name ||
31 extractGitUsername(sessionWithData),
32 // Include other relevant state fields that might be available
33 git_origin: sessionWithData?.session_state?.git_origin,
34 };
35
36 timelineEl.state = state as State;
Sean McCullough021231a2025-06-12 09:35:24 -070037 container.replaceWith(timelineEl);
38}
39
bankseancad67b02025-06-27 21:57:05 +000040// Helper function to extract git username from session data
41function extractGitUsername(sessionWithData: any): string | undefined {
42 // Try to extract from session state first
43 if (sessionWithData?.session_state?.git_username) {
44 return sessionWithData.session_state.git_username;
45 }
46
47 // For older sessions, we might not have git_username stored
48 // We could try to extract it from other sources, but for now return undefined
49 return undefined;
50}
51
Sean McCullough021231a2025-06-12 09:35:24 -070052window.globalThis.renderMessagesViewer = renderMessagesViewer;