| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| Sean McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 2 | import { SketchTimeline } from "./web-components/sketch-timeline"; |
| 3 | import { aggregateAgentMessages } from "./web-components/aggregateAgentMessages"; |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 4 | import { State } from "./types"; |
| Sean McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 5 | |
| 6 | // Ensure this dependency ends up in the bundle (the "as SketchTimeline" reference below |
| 7 | // is insufficient for the bundler to include it). |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 8 | // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| Sean McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 9 | SketchTimeline; |
| 10 | |
| 11 | export 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 McCullough | d183284 | 2025-06-20 22:42:06 -0400 | [diff] [blame] | 22 | timelineEl.scrollContainer = { value: window.document.body }; |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 23 | |
| 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 McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 39 | container.replaceWith(timelineEl); |
| 40 | } |
| 41 | |
| banksean | cad67b0 | 2025-06-27 21:57:05 +0000 | [diff] [blame] | 42 | // Helper function to extract git username from session data |
| 43 | function 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 McCullough | 021231a | 2025-06-12 09:35:24 -0700 | [diff] [blame] | 54 | window.globalThis.renderMessagesViewer = renderMessagesViewer; |