77 llmProviderStore ,
88 analyticsSettingsStore ,
99 chatHistoryStore ,
10+ Actors ,
1011} from '@extension/storage' ;
1112import { t } from '@extension/i18n' ;
1213import BrowserContext from './browser/context' ;
@@ -116,26 +117,38 @@ const onTuyaCommand = async (command: string, commandId: string) => {
116117
117118 const taskId = `tuya_${ commandId } ` ;
118119
119- // Ensure session exists FIRST
120+ // Ensure session exists FIRST and SEED User Message
120121 try {
121122 const session = await chatHistoryStore . getSession ( taskId ) ;
122123 if ( ! session ) {
123- logger . info ( '[Tuya Bridge] Creating new session for task :' , taskId ) ;
124+ logger . info ( '[Tuya Bridge] Creating new session and seeding message :' , taskId ) ;
124125 await chatHistoryStore . createSession ( `Tuya Task: ${ command . substring ( 0 , 30 ) } ...` , taskId ) ;
126+
127+ // Seed the User message directly to DB
128+ await chatHistoryStore . addMessage ( taskId , {
129+ actor : Actors . USER ,
130+ content : command ,
131+ timestamp : Date . now ( )
132+ } ) ;
125133 }
126134 } catch ( e ) {
127135 logger . warning ( '[Tuya Bridge] Session check failed, trying to create:' , e ) ;
128136 try {
129137 await chatHistoryStore . createSession ( `Tuya Task: ${ command . substring ( 0 , 30 ) } ...` , taskId ) ;
138+ await chatHistoryStore . addMessage ( taskId , {
139+ actor : Actors . USER ,
140+ content : command ,
141+ timestamp : Date . now ( )
142+ } ) ;
130143 } catch ( createErr ) {
131- logger . error ( '[Tuya Bridge] Could not create session:' , createErr ) ;
144+ logger . error ( '[Tuya Bridge] Could not create session/message :' , createErr ) ;
132145 }
133146 }
134147
135148 if ( currentPort ) {
136149 logger . info ( '[Tuya Bridge] Side Panel connected, syncing UI...' ) ;
137150
138- // 4. Send "Init Session"
151+ // 4. Send "Init Session" (Loads history -> Sees Seeded Message)
139152 currentPort . postMessage ( {
140153 type : 'init_session' ,
141154 sessionId : taskId
@@ -150,14 +163,7 @@ const onTuyaCommand = async (command: string, commandId: string) => {
150163 // Wait for the "typing" and "auto-send" animation/delay
151164 await new Promise ( r => setTimeout ( r , 1200 ) ) ;
152165
153- // 6. Send "User Message" (The task start)
154- currentPort . postMessage ( {
155- type : 'execution' ,
156- actor : 'user' ,
157- state : 'task_start' ,
158- data : { details : command } ,
159- timestamp : Date . now ( )
160- } ) ;
166+ // Note: We do NOT send "task_start" USER message here anymore.
161167 } else {
162168 logger . warning ( '[Tuya Bridge] Side Panel did not connect in time, running headless...' ) ;
163169 }
0 commit comments