@@ -6,6 +6,8 @@ import type { RootState } from './store'
66import type { CodingCliProviderName } from '@/lib/coding-cli-types'
77import { nanoid } from 'nanoid'
88
9+ const CODING_CLI_CREATE_TIMEOUT_MS = 30_000
10+
911export const createCodingCliTab = createAsyncThunk (
1012 'codingCli/createTab' ,
1113 async (
@@ -42,12 +44,16 @@ export const createCodingCliTab = createAsyncThunk(
4244 throw err
4345 }
4446
45- return new Promise < string > ( ( resolve , reject ) => {
46- const unsub = ws . onMessage ( ( msg ) => {
47+ let unsub : ( ( ) => void ) | undefined
48+ let timeoutId : ReturnType < typeof setTimeout > | undefined
49+
50+ const mainPromise = new Promise < string > ( ( resolve , reject ) => {
51+ unsub = ws . onMessage ( ( msg ) => {
4752 if ( msg . type === 'codingcli.created' && msg . requestId === requestId ) {
53+ clearTimeout ( timeoutId )
4854 const canceled = ( getState ( ) as RootState ) . codingCli . pendingRequests [ requestId ] ?. canceled
4955 dispatch ( resolveCodingCliRequest ( { requestId } ) )
50- unsub ( )
56+ unsub ?. ( )
5157 if ( canceled ) {
5258 ws . send ( { type : 'codingcli.kill' , sessionId : msg . sessionId } )
5359 reject ( new Error ( 'Canceled' ) )
@@ -76,9 +82,10 @@ export const createCodingCliTab = createAsyncThunk(
7682 resolve ( msg . sessionId )
7783 }
7884 if ( msg . type === 'error' && msg . requestId === requestId ) {
85+ clearTimeout ( timeoutId )
7986 const canceled = ( getState ( ) as RootState ) . codingCli . pendingRequests [ requestId ] ?. canceled
8087 dispatch ( resolveCodingCliRequest ( { requestId } ) )
81- unsub ( )
88+ unsub ?. ( )
8289 if ( ! canceled && createdTabId ) {
8390 dispatch (
8491 updateTab ( {
@@ -99,5 +106,18 @@ export const createCodingCliTab = createAsyncThunk(
99106 cwd,
100107 } )
101108 } )
109+
110+ const timeoutPromise = new Promise < never > ( ( _ , reject ) => {
111+ timeoutId = setTimeout ( ( ) => {
112+ dispatch ( resolveCodingCliRequest ( { requestId } ) )
113+ unsub ?.( )
114+ if ( createdTabId ) {
115+ dispatch ( updateTab ( { id : createdTabId , updates : { status : 'error' } } ) )
116+ }
117+ reject ( new Error ( 'Coding CLI creation timed out after 30 seconds' ) )
118+ } , CODING_CLI_CREATE_TIMEOUT_MS )
119+ } )
120+
121+ return Promise . race ( [ mainPromise , timeoutPromise ] )
102122 }
103123)
0 commit comments