@afncdelacru/brady-chat 0.5.0 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ interface ModeData {
27
27
  volumeMultiplier?: number;
28
28
  }
29
29
 
30
- interface BradyChatContextType {
30
+ export interface BradyChatContextType {
31
31
  workflowType: ChatWorkflowType;
32
32
  messages: ChatMessage[];
33
33
  startWorkflow: (workflow: ChatWorkflowType) => void;
@@ -55,6 +55,12 @@ interface BradyChatContextType {
55
55
  // Modal-aware Brady AI
56
56
  hideBradyForModal: () => void;
57
57
  restoreBradyAfterModal: () => void;
58
+
59
+ // Set user text and trigger send in EnhancedBradyChat
60
+ setUserText: (text: string) => void;
61
+ // Expose userTextToSend and setUserTextToSend for EnhancedBradyChat
62
+ userTextToSend?: string;
63
+ setUserTextToSend?: (text: string | undefined) => void;
58
64
  }
59
65
 
60
66
  const BradyChatContext = createContext<BradyChatContextType | undefined>(undefined);
@@ -67,6 +73,12 @@ const initialMessages: ChatMessage[] = [
67
73
  ];
68
74
 
69
75
  export function BradyChatProvider({ children }: { children: ReactNode }) {
76
+ // setUserText state for EnhancedBradyChat
77
+ const [userTextToSend, setUserTextToSend] = useState<string | undefined>(undefined);
78
+ // Expose setUserText to context consumers
79
+ const setUserText = (text: string) => {
80
+ setUserTextToSend(text);
81
+ };
70
82
  const [workflowType, setWorkflowType] = useState<ChatWorkflowType>('free');
71
83
  const [messages, setMessages] = useState<ChatMessage[]>(initialMessages);
72
84
  // Mode system state
@@ -205,6 +217,10 @@ export function BradyChatProvider({ children }: { children: ReactNode }) {
205
217
  setIsHidden,
206
218
  hideBradyForModal,
207
219
  restoreBradyAfterModal,
220
+ setUserText,
221
+ // Expose userTextToSend and setUserTextToSend for EnhancedBradyChat
222
+ userTextToSend,
223
+ setUserTextToSend,
208
224
  }}
209
225
  >
210
226
  {children}
@@ -26,10 +26,14 @@ export interface EnhancedBradyChatProps {
26
26
  * Typically you pass something like `/bradyIcon.png` from your app's public assets.
27
27
  */
28
28
  avatarSrc: string;
29
+
30
+ /**
31
+ * If set, will immediately set the input value and send it as a user message.
32
+ */
33
+ setUserText?: string;
29
34
  }
30
35
 
31
- export function EnhancedBradyChat({ modeVariant = 'loan-officer', avatarSrc }: EnhancedBradyChatProps) {
32
-
36
+ export function EnhancedBradyChat({ modeVariant = 'loan-officer', avatarSrc, setUserText }: EnhancedBradyChatProps) {
33
37
  const [bradyHealthy, setBradyHealthy] = useState(true);
34
38
  const {
35
39
  workflowType,
@@ -51,8 +55,43 @@ export function EnhancedBradyChat({ modeVariant = 'loan-officer', avatarSrc }: E
51
55
  setShowModePrompts,
52
56
  isHidden,
53
57
  setIsHidden,
58
+ // Add userTextToSend and setUserTextToSend from context
59
+ setUserText: contextSetUserText,
60
+ // @ts-ignore: context type not updated yet
61
+ userTextToSend,
62
+ // @ts-ignore: context type not updated yet
63
+ setUserTextToSend,
54
64
  } = useBradyChat();
55
65
  const [inputValue, setInputValue] = useState('');
66
+ const [setUserTextSent, setSetUserTextSent] = useState<string | undefined>(undefined);
67
+
68
+ // If userTextToSend from context changes, treat as setUserText
69
+ useEffect(() => {
70
+ if (userTextToSend && userTextToSend !== setUserTextSent) {
71
+ setInputValue(userTextToSend);
72
+ setSetUserTextSent(userTextToSend);
73
+ }
74
+ }, [userTextToSend, setUserTextSent]);
75
+
76
+ // Auto-send user text if setUserText prop changes
77
+ useEffect(() => {
78
+ if (setUserText && setUserText !== setUserTextSent) {
79
+ setInputValue(setUserText);
80
+ setSetUserTextSent(setUserText);
81
+ }
82
+ }, [setUserText, setUserTextSent]);
83
+
84
+ // When inputValue is set by setUserText or contextUserText, trigger send
85
+ useEffect(() => {
86
+ if (
87
+ ((setUserText && setUserTextSent === setUserText && inputValue === setUserText) ||
88
+ (userTextToSend && setUserTextSent === userTextToSend && inputValue === userTextToSend)) &&
89
+ inputValue.trim()
90
+ ) {
91
+ handleSend();
92
+ }
93
+ // eslint-disable-next-line react-hooks/exhaustive-deps
94
+ }, [inputValue, setUserText, setUserTextSent, userTextToSend]);
56
95
  const [showForm, setShowForm] = useState(false);
57
96
  const messagesEndRef = useRef<HTMLDivElement>(null);
58
97
  const [suggestionLinks, setSuggestionLinks] = useState<{ text: string; prompt: string }[] | null>(null);
@@ -293,9 +332,12 @@ export function EnhancedBradyChat({ modeVariant = 'loan-officer', avatarSrc }: E
293
332
  const handleSend = async () => {
294
333
  if (inputValue.trim() && !inputDisabled) {
295
334
  const userText = inputValue;
296
- addMessage({ type: 'user', text: userText });
297
- setInputValue('');
298
-
335
+ addMessage({ type: 'user', text: userText });
336
+ setInputValue('');
337
+ // If userTextToSend was used, clear it after send to avoid repeated triggers
338
+ if (userTextToSend && setUserTextSent === userTextToSend && setUserTextToSend) {
339
+ setUserTextToSend(undefined);
340
+ }
299
341
  try {
300
342
  const apiResponse = await sendBradyPrompt([{ role: 'user', content: userText }]);
301
343
  let mappedType: 'user' | 'brady' | 'form' = 'brady';