File tree Expand file tree Collapse file tree 2 files changed +27
-11
lines changed Expand file tree Collapse file tree 2 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,7 @@ import { AutoAwesome as AutoAwesomeIcon } from '@mui/icons-material'
22import { Button , Tooltip } from '@mui/material'
33import { CapabilitiesContext } from '../App'
44import { makeStyles } from '@mui/styles'
5- import {
6- useDrawer ,
7- useSessionActions ,
8- useWebSocketActions ,
9- } from './store/useChatStore'
5+ import { useDrawer , useSessionActions } from './store/useChatStore'
106import PropTypes from 'prop-types'
117import React , { useContext } from 'react'
128
@@ -50,7 +46,6 @@ const useStyles = makeStyles((theme) => ({
5046export 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 = (
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments