11import { useOptionalAIFormContext } from '../AIFormProvider' ;
22import { executeAIProviders } from '../aiProviders' ;
33import type { AIProvider , AIProviderType } from '../types' ;
4- import { useMemo } from 'react' ;
4+ import { useMemo , useRef , useEffect } from 'react' ;
55
66type AutofillData = Record < string , string > ;
77
@@ -64,6 +64,15 @@ export function useAIAssistant({
6464} : AIAssistantOptions = { } ) {
6565 const providerContext = useOptionalAIFormContext ( ) ;
6666
67+ // CRITICAL FIX: Use a ref to store the latest formContext
68+ const formContextRef = useRef ( formContext ) ;
69+
70+ // Update ref whenever formContext changes
71+ useEffect ( ( ) => {
72+ formContextRef . current = formContext ;
73+ console . log ( '🔄 AI Assistant context updated:' , formContext ) ;
74+ } , [ formContext ] ) ;
75+
6776 // Merge context with local overrides (local takes precedence)
6877 const effectiveConfig = useMemo ( ( ) => {
6978 return {
@@ -79,13 +88,17 @@ export function useAIAssistant({
7988 async function suggestValue ( name : string , value : string ) : Promise < string | null > {
8089 if ( ! enabled ) return null ;
8190
91+ // CRITICAL FIX: Use the latest context from ref
92+ const currentContext = formContextRef . current ;
93+ console . log ( '💡 Suggesting value for' , name , 'with context:' , currentContext ) ;
94+
8295 if ( effectiveConfig . providers && effectiveConfig . executionOrder ) {
8396 const { result } = await executeAIProviders (
8497 effectiveConfig . providers ,
8598 effectiveConfig . executionOrder ,
8699 effectiveConfig . fallbackOnError ,
87100 async ( provider ) => {
88- const response = await provider . suggestValue ( name , value , formContext ) ;
101+ const response = await provider . suggestValue ( name , value , currentContext ) ;
89102 return response ;
90103 }
91104 ) ;
@@ -96,7 +109,7 @@ export function useAIAssistant({
96109 }
97110 } else {
98111 // Legacy fallback: Chrome AI -> Server
99- const legacyResult = await legacySuggestValue ( name , value , formContext , apiUrl ) ;
112+ const legacyResult = await legacySuggestValue ( name , value , currentContext , apiUrl ) ;
100113 return legacyResult ;
101114 }
102115
@@ -115,13 +128,17 @@ export function useAIAssistant({
115128 return Object . fromEntries ( fields . map ( ( f ) => [ f , 'AI disabled' ] ) ) as AutofillData ;
116129 }
117130
131+ // CRITICAL FIX: Use the latest context from ref
132+ const currentContext = formContextRef . current ;
133+ console . log ( '🤖 Auto-filling with context:' , currentContext ) ;
134+
118135 if ( effectiveConfig . providers && effectiveConfig . executionOrder ) {
119136 const { result } = await executeAIProviders (
120137 effectiveConfig . providers ,
121138 effectiveConfig . executionOrder ,
122139 effectiveConfig . fallbackOnError ,
123140 async ( provider ) => {
124- const data = await provider . autofill ( fields , formContext , options ?. onDownloadProgress ) ;
141+ const data = await provider . autofill ( fields , currentContext , options ?. onDownloadProgress ) ;
125142 return data ;
126143 }
127144 ) ;
@@ -131,7 +148,7 @@ export function useAIAssistant({
131148 }
132149 } else {
133150 // Legacy fallback
134- const legacyResult = await legacyAutofill ( fields , formContext , apiUrl , options ) ;
151+ const legacyResult = await legacyAutofill ( fields , currentContext , apiUrl , options ) ;
135152 return legacyResult ;
136153 }
137154
0 commit comments