| 188 | } |
| 189 | |
| 190 | function attachToWebsocket(internalState, state, commit, dispatch) { |
| 191 | if (!isNull(internalState.websocket)) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | const executionId = state.id; |
| 196 | |
| 197 | let websocket; |
| 198 | try { |
| 199 | websocket = new WebSocket(getWebsocketUrl('executions/io/' + executionId)); |
| 200 | } catch (e) { |
| 201 | console.log('Failed to open websocket') |
| 202 | } |
| 203 | |
| 204 | websocket.addEventListener('message', function (message) { |
| 205 | const event = JSON.parse(message.data); |
| 206 | |
| 207 | const eventType = event.event; |
| 208 | const data = event.data; |
| 209 | |
| 210 | if (eventType === 'output') { |
| 211 | commit('ADD_LOG_CHUNK', data); |
| 212 | |
| 213 | } else if (eventType === 'input') { |
| 214 | commit('SET_PROMPT_TEXT', data); |
| 215 | |
| 216 | } else if (eventType === 'file') { |
| 217 | commit('ADD_FILE', { |
| 218 | 'url': data.url, |
| 219 | 'filename': data.filename |
| 220 | }); |
| 221 | } else if (eventType === 'inline-image') { |
| 222 | commit('ADD_INLINE_IMAGE', { |
| 223 | 'output_path': data.output_path, |
| 224 | 'download_url': data.download_url |
| 225 | }); |
| 226 | } |
| 227 | }); |
| 228 | |
| 229 | websocket.addEventListener('close', function (event) { |
| 230 | let executionFinished = (event.code === 1000); |
| 231 | if (!executionFinished) { |
| 232 | axiosInstance.get('executions/status/' + executionId) |
| 233 | .then(({data: status}) => { |
| 234 | if (status === 'finished') { |
| 235 | dispatch('setStatus', STATUS_FINISHED); |
| 236 | } else { |
| 237 | dispatch('setStatus', STATUS_DISCONNECTED); |
| 238 | } |
| 239 | }) |
| 240 | .catch((error) => { |
| 241 | console.log('Failed to connect to the server: ' + error); |
| 242 | dispatch('setErrorStatus'); |
| 243 | }); |
| 244 | |
| 245 | } else { |
| 246 | dispatch('setStatus', STATUS_FINISHED); |
| 247 | } |