Skip to content

Commit de11e28

Browse files
Merge pull request #3048 from stbenjam/websocket-wait
Wait for websocket before sending message
2 parents ce32510 + 0fdf180 commit de11e28

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

sippy-ng/src/chat/AskSippyButton.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import { AutoAwesome as AutoAwesomeIcon } from '@mui/icons-material'
22
import { Button, Tooltip } from '@mui/material'
33
import { CapabilitiesContext } from '../App'
44
import { makeStyles } from '@mui/styles'
5-
import {
6-
useDrawer,
7-
useSessionActions,
8-
useWebSocketActions,
9-
} from './store/useChatStore'
5+
import { useDrawer, useSessionActions } from './store/useChatStore'
106
import PropTypes from 'prop-types'
117
import React, { useContext } from 'react'
128

@@ -50,7 +46,6 @@ const useStyles = makeStyles((theme) => ({
5046
export default function AskSippyButton({ question, tooltip }) {
5147
const { openDrawer } = useDrawer()
5248
const { startNewSession } = useSessionActions()
53-
const { sendMessage } = useWebSocketActions()
5449
const capabilities = useContext(CapabilitiesContext)
5550
const classes = useStyles()
5651

@@ -59,11 +54,8 @@ export default function AskSippyButton({ question, tooltip }) {
5954
}
6055

6156
const handleClick = () => {
62-
startNewSession()
6357
openDrawer()
64-
setTimeout(() => {
65-
sendMessage(question)
66-
}, 100)
58+
startNewSession(question)
6759
}
6860

6961
const button = (

sippy-ng/src/chat/store/sessionSlice.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const createSessionSlice = (set, get) => ({
7676
},
7777

7878
// Start a new session (find empty or create new)
79-
startNewSession: () => {
79+
// If initialMessage is provided, waits for websocket connection and sends it
80+
startNewSession: (initialMessage = null) => {
8081
const {
8182
sessions,
8283
activeSessionId,
@@ -85,6 +86,8 @@ export const createSessionSlice = (set, get) => ({
8586
setCurrentThinking,
8687
setError,
8788
setIsTyping,
89+
connectionState,
90+
sendMessage,
8891
} = get()
8992

9093
// Try to find an existing empty session
@@ -106,6 +109,27 @@ export const createSessionSlice = (set, get) => ({
106109
setCurrentThinking(null)
107110
setError(null)
108111
setIsTyping(false)
112+
113+
// If an initial message is provided, wait for connection and send it
114+
if (initialMessage) {
115+
const maxAttempts = 50 // 5 seconds max (50 * 100ms)
116+
let attempts = 0
117+
118+
const waitForConnection = () => {
119+
const store = get()
120+
if (store.connectionState === 'connected') {
121+
sendMessage(initialMessage)
122+
} else if (attempts < maxAttempts) {
123+
attempts++
124+
setTimeout(waitForConnection, 100)
125+
} else {
126+
console.error('Failed to connect to websocket after 5 seconds')
127+
setError('Failed to connect to chat service')
128+
}
129+
}
130+
131+
waitForConnection()
132+
}
109133
},
110134

111135
// Delete a session

0 commit comments

Comments
 (0)