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