|
| 1 | +import { ChatApi, type NotificationHandlerMap } from "@repo/shared"; |
| 2 | +import { buildNotificationRouter, sendCommand } from "@repo/webview-shared"; |
| 3 | + |
| 4 | +import "./index.css"; |
| 5 | + |
| 6 | +/** Chat shim: source-gated bridge between the iframe `{ type, payload }` protocol and `ChatApi`. */ |
| 7 | +export function main(): void { |
| 8 | + const shim = findShim(); |
| 9 | + if (!shim) { |
| 10 | + return; |
| 11 | + } |
| 12 | + revealIframeOnLoad(shim); |
| 13 | + listenForMessages(shim); |
| 14 | +} |
| 15 | + |
| 16 | +interface Shim { |
| 17 | + iframe: HTMLIFrameElement; |
| 18 | + status: HTMLDivElement; |
| 19 | + allowedOrigin: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface IframeMessage { |
| 23 | + type?: string; |
| 24 | + payload?: { url?: string }; |
| 25 | +} |
| 26 | + |
| 27 | +function findShim(): Shim | null { |
| 28 | + const iframe = document.getElementById("chat-frame"); |
| 29 | + const status = document.getElementById("status"); |
| 30 | + if ( |
| 31 | + !(iframe instanceof HTMLIFrameElement) || |
| 32 | + !(status instanceof HTMLDivElement) |
| 33 | + ) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + return { iframe, status, allowedOrigin: new URL(iframe.src).origin }; |
| 37 | +} |
| 38 | + |
| 39 | +function revealIframeOnLoad({ iframe, status }: Shim): void { |
| 40 | + iframe.addEventListener("load", () => { |
| 41 | + iframe.style.display = "block"; |
| 42 | + status.style.display = "none"; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +function listenForMessages(shim: Shim): void { |
| 47 | + const route = buildNotificationRouter( |
| 48 | + ChatApi, |
| 49 | + buildNotificationHandlers(shim), |
| 50 | + ); |
| 51 | + window.addEventListener("message", (event) => { |
| 52 | + if (event.source === shim.iframe.contentWindow) { |
| 53 | + if (typeof event.data === "object" && event.data !== null) { |
| 54 | + handleFromIframe(shim, event.data as IframeMessage); |
| 55 | + } |
| 56 | + return; |
| 57 | + } |
| 58 | + route(event.data); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +function handleFromIframe({ status }: Shim, msg: IframeMessage): void { |
| 63 | + switch (msg.type) { |
| 64 | + case "coder:vscode-ready": |
| 65 | + status.textContent = "Authenticating…"; |
| 66 | + sendCommand(ChatApi.vscodeReady); |
| 67 | + return; |
| 68 | + case "coder:chat-ready": |
| 69 | + sendCommand(ChatApi.chatReady); |
| 70 | + return; |
| 71 | + case "coder:navigate": |
| 72 | + if (msg.payload?.url) { |
| 73 | + sendCommand(ChatApi.navigate, { url: msg.payload.url }); |
| 74 | + } |
| 75 | + return; |
| 76 | + default: |
| 77 | + return; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Compile-checked: a new ChatApi notification without a handler fails the build. |
| 82 | +function buildNotificationHandlers( |
| 83 | + shim: Shim, |
| 84 | +): NotificationHandlerMap<typeof ChatApi> { |
| 85 | + return { |
| 86 | + setTheme: ({ theme }) => postToIframe(shim, "coder:set-theme", { theme }), |
| 87 | + authBootstrapToken: ({ token }) => { |
| 88 | + shim.status.textContent = "Signing in…"; |
| 89 | + postToIframe(shim, "coder:vscode-auth-bootstrap", { token }); |
| 90 | + }, |
| 91 | + authError: ({ error }) => showRetry(shim, error), |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +function postToIframe( |
| 96 | + { iframe, allowedOrigin }: Shim, |
| 97 | + type: string, |
| 98 | + payload: unknown, |
| 99 | +): void { |
| 100 | + iframe.contentWindow?.postMessage({ type, payload }, allowedOrigin); |
| 101 | +} |
| 102 | + |
| 103 | +function showRetry({ iframe, status }: Shim, error: string): void { |
| 104 | + status.textContent = ""; |
| 105 | + status.appendChild( |
| 106 | + document.createTextNode(error || "Authentication failed."), |
| 107 | + ); |
| 108 | + const btn = document.createElement("button"); |
| 109 | + btn.id = "retry-btn"; |
| 110 | + btn.textContent = "Retry"; |
| 111 | + btn.addEventListener("click", () => { |
| 112 | + status.textContent = "Authenticating…"; |
| 113 | + sendCommand(ChatApi.vscodeReady); |
| 114 | + }); |
| 115 | + status.appendChild(document.createElement("br")); |
| 116 | + status.appendChild(btn); |
| 117 | + status.style.display = "block"; |
| 118 | + iframe.style.display = "none"; |
| 119 | +} |
| 120 | + |
| 121 | +main(); |
0 commit comments