| Sean McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 1 | import { SketchTimeline } from "./web-components/sketch-timeline"; |
| 2 | import { aggregateAgentMessages } from "./web-components/aggregateAgentMessages"; |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 3 | import { State } from "./types"; |
| Sean McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 4 | |
| 5 | // Ensure this dependency ends up in the bundle (the "as SketchTimeline" reference below |
| 6 | // is insufficient for the bundler to include it). |
| 7 | SketchTimeline; |
| 8 | |
| 9 | export 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 McCullough | d183284 | 2025-06-20 22:42:06 -0400 | [diff] [blame] | 20 | timelineEl.scrollContainer = { value: window.document.body }; |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 21 | |
| 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 McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 37 | container.replaceWith(timelineEl); |
| 38 | } |
| 39 | |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 40 | // Helper function to extract git username from session data |
| 41 | function 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 McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 52 | window.globalThis.renderMessagesViewer = renderMessagesViewer; |