File size: 10,358 Bytes
53ea588
 
 
 
 
 
 
 
e821a83
53ea588
 
 
 
 
 
 
 
 
 
 
 
e821a83
88326b7
 
53ea588
 
 
e821a83
53ea588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e821a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53ea588
 
 
 
 
 
 
 
 
 
 
 
88326b7
 
 
 
 
 
53ea588
 
88326b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53ea588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: BSD 2-Clause License

import { toast } from "sonner";
import { useEffect, useState } from "react";
import { AudioStream } from "./AudioStream";
import { AudioWaveForm } from "./AudioWaveForm";
import { Toaster } from "./components/ui/sonner";
import { RTC_OFFER_URL, DYNAMIC_PROMPT, POLL_PROMPT_URL, ASSISTANTS_URL } from "./config";
import usePipecatWebRTC from "./hooks/use-pipecat-webrtc";
import { Transcripts } from "./Transcripts";
import WebRTCButton from "./WebRTCButton";
import MicrophoneButton from "./MicrophoneButton";
import { PromptInput } from "./PromptInput";

function App() {
  const [showPromptInput, setShowPromptInput] = useState<boolean>(false); // Control PromptInput visibility
  const [currentPrompt, setCurrentPrompt] = useState<string>(""); // Store current prompt value
  const [assistants, setAssistants] = useState<Array<{ assistant_id: string; name?: string | null; graph_id?: string | null; display_name?: string | null }>>([]);
  const [selectedAssistant, setSelectedAssistant] = useState<string | null>(null);
  const [selectedAssistantName, setSelectedAssistantName] = useState<string>("Speech to Speech Demo");
  const [rtcConfig, setRtcConfig] = useState<ConstructorParameters<typeof RTCPeerConnection>[0]>({});
  const [showDisclaimer, setShowDisclaimer] = useState<boolean>(true);
  const [disclaimerAgreed, setDisclaimerAgreed] = useState<boolean>(false);
  
  const webRTC = usePipecatWebRTC({
    url: RTC_OFFER_URL,
    rtcConfig,
    onError: (e) => toast.error(e.message),
    assistant: selectedAssistant,
  });

  // Fetch and set the latest prompt when page loads - only if DYNAMIC_PROMPT is true
  useEffect(() => {
    if (DYNAMIC_PROMPT) {
      const fetchPrompt = async () => {
        try {
          console.log("Fetching latest prompt from API... (DYNAMIC_PROMPT mode)");
          const response = await fetch(POLL_PROMPT_URL);
          
          if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
          }
          
          const data = await response.json();
          console.log("Latest Prompt:", data);
          // Set the fetched prompt as current value
          setCurrentPrompt(data.prompt); // Initialize currentPrompt with API data
          console.log("Current prompt updated in PromptInput component");
        } catch (error) {
          console.error("Error fetching prompt:", error);
          toast.error("Failed to fetch latest prompt");
          // Keep the fallback default value on error
        }
      };

      fetchPrompt();
    } else {
      console.log("DYNAMIC_PROMPT is false - skipping API call");
    }
  }, []); // Empty dependency array - runs only on component mount (page reload)

  // Fetch assistants on mount and pick first as default
  useEffect(() => {
    const fetchAssistants = async () => {
      try {
        const res = await fetch(ASSISTANTS_URL);
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const data = await res.json();
        const list = Array.isArray(data) ? data : [];
        setAssistants(list);
        if (list.length > 0) {
          setSelectedAssistant(list[0].assistant_id || null);
          setSelectedAssistantName(list[0].display_name || list[0].name || list[0].graph_id || list[0].assistant_id || "Speech to Speech Demo");
        }
      } catch (e) {
        console.warn("Failed to fetch assistants", e);
      }
    };
    fetchAssistants();
  }, []);

  // Fetch ICE configuration for RTC
  useEffect(() => {
    const fetchRtcConfig = async () => {
      try {
        const res = await fetch("/rtc-config");
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const data = await res.json();
        setRtcConfig(data || {});
      } catch (e) {
        console.warn("Failed to fetch rtc-config", e);
        // Fallback STUN if server not available
        setRtcConfig({ iceServers: [{ urls: "stun:stun.l.google.com:19302" }] });
      }
    };
    fetchRtcConfig();
  }, []);

  // Send current prompt IMMEDIATELY when WebRTC connection is established
  useEffect(() => {
    if (webRTC.status === "connected" && currentPrompt.trim()) {
      console.log("WebRTC connected! Sending prompt IMMEDIATELY:", currentPrompt);
      // Send without any delay to beat the LLM initialization
      webRTC.websocket.send(JSON.stringify({
        type: "context_reset",
        message: currentPrompt.trim(),
      }));
    }
  }, [webRTC.status]); // Triggers immediately when status becomes "connected"

  const handleDisclaimerContinue = () => {
    if (disclaimerAgreed) {
      setShowDisclaimer(false);
    }
  };

  return (
    <div className="h-screen flex flex-col">
      {/* Disclaimer Modal */}
      {showDisclaimer && (
        <div className="fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center z-50 p-4">
          <div className="bg-white rounded-lg shadow-2xl max-w-2xl w-full max-h-[90vh] overflow-y-auto">
            <div className="p-6 md:p-8">
              <h2 className="text-2xl font-bold text-gray-900 mb-4">
                Healthcare Agent – Demo & Disclaimer
              </h2>
              <div className="text-gray-700 space-y-4 mb-6">
                <p>
                  This is a <strong>demo for educational purposes only</strong>. It is not medical advice, 
                  a diagnosis, or a treatment tool, and it is not a medical device. The agent's responses 
                  are generated by AI and may be incorrect, incomplete, harmful, or offensive. Do not share 
                  personal or protected health information. No patient–provider relationship is created.
                </p>
                <p>
                  You are responsible for how you use the demo. We make no warranties and accept no liability 
                  for actions taken based on the agent's responses. For medical concerns, consult a qualified 
                  clinician. In an emergency, call 911 (or your local equivalent).
                </p>
                <p>
                  By continuing, you confirm you are 18+ and agree to our Terms and this Disclaimer.
                </p>
              </div>
              <div className="mb-6">
                <label className="flex items-start cursor-pointer">
                  <input
                    type="checkbox"
                    checked={disclaimerAgreed}
                    onChange={(e) => setDisclaimerAgreed(e.target.checked)}
                    className="mt-1 mr-3 h-5 w-5 text-nvidia border-gray-300 rounded focus:ring-nvidia"
                  />
                  <span className="text-gray-900 font-medium">
                    I understand and agree
                  </span>
                </label>
              </div>
              <button
                onClick={handleDisclaimerContinue}
                disabled={!disclaimerAgreed}
                className={`w-full py-3 px-6 rounded-lg font-semibold transition-colors ${
                  disclaimerAgreed
                    ? "bg-nvidia text-black hover:bg-nvidia/90 cursor-pointer"
                    : "bg-gray-300 text-gray-500 cursor-not-allowed"
                }`}
              >
                Continue
              </button>
            </div>
          </div>
        </div>
      )}
      
      <header className="bg-black p-6 flex items-center">
        <img src="logo_mm.png" alt="NVIDIA ACE Logo" className="h-16 mr-8" />
        <div className="flex-1 flex justify-center">
          <div className="bg-nvidia px-6 py-3 rounded text-black">
            <h1 className="text-2xl font-semibold">{selectedAssistantName}</h1>
          </div>
        </div>
      </header>
      <section className="flex-1 flex">
        <div className="flex-1 p-5">
          <AudioStream
            streamOrTrack={webRTC.status === "connected" ? webRTC.stream : null}
          />
          <Transcripts
            websocket={webRTC.status === "connected" ? webRTC.websocket : null}
          />
        </div>
        <div className="p-5 border-l-1 border-gray-200 flex flex-col">
          <div className="flex-1 mb-4">
            <AudioWaveForm
              streamOrTrack={webRTC.status === "connected" ? webRTC.stream : null}
            />
          </div>
          {showPromptInput && (
            <div className="flex-7">
              <PromptInput
                defaultValue={currentPrompt}
                onChange={(prompt) => setCurrentPrompt(prompt)}
                disabled={webRTC.status === "connected"}
              />
            </div>
          )}
        </div>
      </section>
      <footer className="bg-black p-6 flex items-center justify-between text-white">
        <div className="flex items-center">
          {/* Assistant selector */}
          <select
            className="mr-3 border border-nvidia rounded px-2 py-1 bg-black text-white"
            value={selectedAssistant || ""}
            onChange={(e) => {
              const id = e.target.value || null;
              setSelectedAssistant(id);
              const found = assistants.find((a) => a.assistant_id === id);
              if (found) {
                setSelectedAssistantName(found.display_name || found.name || found.graph_id || found.assistant_id || "Speech to Speech Demo");
              }
            }}
            disabled={webRTC.status !== "init"}
          >
            {assistants.map((a) => (
              <option key={a.assistant_id} value={a.assistant_id}>
                {a.display_name || a.name || a.graph_id || a.assistant_id}
              </option>
            ))}
          </select>
          <WebRTCButton {...webRTC} />
          {webRTC.status === "connected" && (
            <MicrophoneButton stream={webRTC.micStream} />
          )}
        </div>
        {DYNAMIC_PROMPT && (
          <button
            type="button"
            className="bg-nvidia px-4 py-2 rounded-lg text-black"
            onClick={() => {
              setShowPromptInput(!showPromptInput);
            }}
          >
            {showPromptInput ? "Hide Prompt" : "Show Prompt"}
          </button>
        )}
      </footer>
      <Toaster />
    </div>
  );
}

export default App;