(
listener: (event: AgentSessionChatEvent) => void,
mode?: OnChatMode
)
| 1852 | } |
| 1853 | |
| 1854 | private async emitHistoricalEvents( |
| 1855 | listener: (event: AgentSessionChatEvent) => void, |
| 1856 | mode?: OnChatMode |
| 1857 | ): Promise<void> { |
| 1858 | let replayMode: "full" | "since" | "live" = "full"; |
| 1859 | let hasOlderHistory: boolean | undefined; |
| 1860 | let serverCursor: OnChatCursor | undefined; |
| 1861 | let emittedReplayMessages = false; |
| 1862 | |
| 1863 | const emitReplayMessage = (message: WorkspaceChatMessage): void => { |
| 1864 | emittedReplayMessages = true; |
| 1865 | listener({ workspaceId: this.workspaceId, message }); |
| 1866 | }; |
| 1867 | |
| 1868 | let replayedTerminalStreamError = false; |
| 1869 | let replayedStreamLifecycle: StreamLifecycleSnapshot | null = null; |
| 1870 | let replayedRuntimeStatus: RuntimeStatusEvent | null = null; |
| 1871 | const emitReplayStatusMessage = (message: WorkspaceChatMessage): void => { |
| 1872 | listener({ workspaceId: this.workspaceId, message }); |
| 1873 | }; |
| 1874 | const emitCurrentReplayTerminalState = (): void => { |
| 1875 | if (!replayedTerminalStreamError && this.terminalStreamError) { |
| 1876 | replayedTerminalStreamError = true; |
| 1877 | emitReplayStatusMessage({ |
| 1878 | ...this.terminalStreamError, |
| 1879 | replay: true, |
| 1880 | }); |
| 1881 | } |
| 1882 | |
| 1883 | const lifecycle = this.getCurrentStreamLifecycleSnapshot(); |
| 1884 | if (!this.hasSameStreamLifecycle(replayedStreamLifecycle, lifecycle)) { |
| 1885 | replayedStreamLifecycle = copyStreamLifecycleSnapshot(lifecycle); |
| 1886 | emitReplayStatusMessage({ |
| 1887 | type: "stream-lifecycle", |
| 1888 | workspaceId: this.workspaceId, |
| 1889 | ...lifecycle, |
| 1890 | }); |
| 1891 | } |
| 1892 | |
| 1893 | const runtimeStatus = this.preparingRuntimeStatus; |
| 1894 | if (runtimeStatus && !this.hasSameRuntimeStatus(replayedRuntimeStatus, runtimeStatus)) { |
| 1895 | replayedRuntimeStatus = { ...runtimeStatus }; |
| 1896 | emitReplayStatusMessage(runtimeStatus); |
| 1897 | } |
| 1898 | }; |
| 1899 | |
| 1900 | let emittedReplayStreamEvents = false; |
| 1901 | const replayStreamEventTracker = (event: AgentSessionChatEvent) => { |
| 1902 | if (event.workspaceId !== this.workspaceId) { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | const message = event.message; |
| 1907 | if (typeof message !== "object" || message === null) { |
| 1908 | return; |
| 1909 | } |
| 1910 | |
| 1911 | if (!("replay" in message) || message.replay !== true) { |
no test coverage detected