()
| 15 | let conn: KimiEventConnection | null = null; |
| 16 | |
| 17 | function ensureConnection(): KimiEventConnection | null { |
| 18 | if (conn !== null) return conn; |
| 19 | if (typeof WebSocket === 'undefined') return null; |
| 20 | conn = getKimiWebApi().connectEvents({ |
| 21 | onEvent: () => {}, |
| 22 | onResync: () => {}, |
| 23 | onError: (_code, msg) => { |
| 24 | error.value = msg; |
| 25 | }, |
| 26 | onConnectionChange: (state) => { |
| 27 | connected.value = state; |
| 28 | }, |
| 29 | onTerminalOutput: (sid, terminalId, data, seq) => { |
| 30 | if (sid !== sessionId.value || terminal.value?.id !== terminalId) return; |
| 31 | lastSeq.value = Math.max(lastSeq.value, seq); |
| 32 | for (const handler of outputHandlers) handler(data); |
| 33 | }, |
| 34 | onTerminalExit: (sid, terminalId, exitCode) => { |
| 35 | if (sid !== sessionId.value || terminal.value?.id !== terminalId) return; |
| 36 | readOnly.value = true; |
| 37 | terminal.value = terminal.value |
| 38 | ? { ...terminal.value, status: 'exited', exitCode } |
| 39 | : terminal.value; |
| 40 | for (const handler of exitHandlers) handler(exitCode); |
| 41 | }, |
| 42 | }); |
| 43 | return conn; |
| 44 | } |
| 45 | |
| 46 | async function start(size?: { cols?: number; rows?: number }): Promise<void> { |
| 47 | const sid = sessionId.value; |
no test coverage detected