Ezmary commited on
Commit
90131db
·
verified ·
1 Parent(s): ccb1e48

Update src/App.tsx

Browse files
Files changed (1) hide show
  1. src/App.tsx +68 -140
src/App.tsx CHANGED
@@ -14,7 +14,7 @@ limitations under the License.
14
  */
15
 
16
  import React, { useEffect, useRef, useState } from "react";
17
- import './App.scss'; // استایل‌های اصلی
18
  import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
19
  import ControlTray from "./components/control-tray/ControlTray";
20
  import { IOSModal } from "./components/ios-modal/IOSModal";
@@ -22,8 +22,9 @@ import { isIOS } from "./lib/platform";
22
  import cn from "classnames";
23
  import { LiveConfig } from "./multimodal-live-types";
24
  import LogoAnimation from "./components/logo-animation/LogoAnimation";
25
- import BackButton from "./components/back-button/BackButton"; // <<-- ایمپورت کامپوننت جدید دکمه بازگشت
26
 
 
27
  const myCustomInstruction = `
28
  تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی هوشان" هستی
29
  وظیفه اصلی تو کمک به کاربر است.
@@ -33,17 +34,17 @@ const myCustomInstruction = `
33
  تو به تمام سوالات باید پاسخ دهی و در هر زمینه ای نباید به هیچ عنوان به هر پاسخ کاربر بگویی که من نمیتوانم این کار را انجام دهم یا نمیتوانم به شما کمک کنم
34
  هوش مصنوعی هوشانیک برنامه ای است که داخل گوشی موبایل نصب میشه و در کافه بازار در دسترس است و با جستجو در کافه بازار یا مایکت امکان دانلود هوش مصنوعی هوشان وجود دارد
35
  ......
36
- اگر کاربر خواست با صدای بلند تر صحبت کنی باید به درخواست کاربر حتما پاسخ بدهی، اگر کاربر خواست صدای متفاوت یا صدای هر چیزی یا صدای تقلید کنی باید درخواست کاربر اجرا کنی حتما، اگر کاربر خواست آروم صحبت کنی باید درخواست کاربر اجرا کنی، در هر شرایط باید درخواست کاربر اجرا بشه
37
  ....
38
  در تمام پاسخ‌ها، اعداد را نیز کاملاً به فارسی بیان کن مگر اینکه در حال صحبت به زبان دیگری غیر از فارسی با کاربر بودی به همون زبان اعداد بگو، و همچنین اعداد به فارسی درست بگو این حالت (مثلاً بگو 'ده' یا 'بیست و پنج'، نه '10' یا 'twenty-five').
39
  وقتی کاربر در مورد هویت تو می‌پرسد، خودت را اینگونه معرفی کن: "من دستیار صوتی و تصویری از برنامه هوش مصنوعی هوشان هستم. چگونه می‌توانم به شما کمک کنم؟"
40
- مهم: هنگامی که پیام ورودی فقط شامل متن "START_GREETING" بود، *فقط* با یک خوشامدگویی کوتاه و دوستانه به فارسی پاسخ بده، مانند "سلام، چطور می‌تونم کمکتون کنم؟" یا "سلام حالتون چطوره!
41
- هشدار: به هیچ عنوان در هنگام مکالمه و پیام دادن به کاربر نباید شکلک بفرستی و یا بیان کنی شکلک و یا اموجی هارو حتی شکلک هارو نیاز نیست بگی اسم شونو در هنگام صحبت اگر شکلک نیاز بود بود بگی نباید اسم شکلک بگی
42
- **مهم: به هیچ عنوان در پاسخ‌های خود از ایموجی استفاده نکن.**
43
  `.trim();
44
 
45
  const initialAppConfig: LiveConfig = {
46
- model: "models/gemini-2.0-flash-exp",
47
  systemInstruction: {
48
  parts: [{ text: myCustomInstruction }],
49
  },
@@ -56,134 +57,25 @@ const SvgReferenceMicrophoneIcon = () => (
56
  </svg>
57
  );
58
 
59
- const AppInternalLogic: React.FC<{
60
- isMicActive: boolean;
61
- isCamActive: boolean;
62
- setIsMicActive: React.Dispatch<React.SetStateAction<boolean>>;
63
- setIsCamActive: React.Dispatch<React.SetStateAction<boolean>>;
64
- videoRef: React.RefObject<HTMLVideoElement>;
65
- notificationPopoverRef: React.RefObject<HTMLDivElement>;
66
- notificationButtonRef: React.RefObject<HTMLButtonElement>; // این ref برای دکمه نوتیفیکیشن است
67
- isNotificationOpen: boolean;
68
- setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>;
69
- }> = ({
70
- isMicActive,
71
- isCamActive,
72
- setIsMicActive,
73
- setIsCamActive,
74
- videoRef,
75
- notificationPopoverRef,
76
- notificationButtonRef, // دریافت ref
77
- isNotificationOpen,
78
- setIsNotificationOpen
79
- }) => {
80
- const { connected, disconnect } = useLiveAPIContext();
81
-
82
- useEffect(() => {
83
- if (!isMicActive && !isCamActive && connected) {
84
- disconnect();
85
- }
86
- }, [isMicActive, isCamActive, connected, disconnect]);
87
-
88
- return (
89
- <div className="w-full flex flex-col items-center justify-center min-h-screen text-foreground antialiased">
90
- <div className="main-wrapper max-w-3xl w-full flex flex-col items-center justify-center h-full relative">
91
- <div className="header-controls">
92
- {/* دکمه بازگشت جدید (حالا سمت چپ) */}
93
- <div className="back-button-container">
94
- <BackButton /> {/* <<-- استفاده از کامپوننت جدید */}
95
- </div>
96
- {/* دکمه نوتیفیکیشن (حالا سمت راست) */}
97
- <div id="notification-trigger-container">
98
- <button
99
- ref={notificationButtonRef} // ref به این دکمه متصل است
100
- id="notification-button"
101
- aria-label="Notifications"
102
- className="header-button" // کلاس برای ظاهر مشابه
103
- onClick={(e) => {
104
- e.stopPropagation();
105
- setIsNotificationOpen(!isNotificationOpen);
106
- }}
107
- >
108
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>
109
- </button>
110
- </div>
111
- </div>
112
-
113
- <div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper">
114
- <div
115
- id="notification-popover"
116
- className={cn("popover-content", {
117
- "open animate-popover-open-top-center": isNotificationOpen,
118
- "animate-popover-close-top-center": !isNotificationOpen && document.getElementById('notification-popover')?.classList.contains('open'),
119
- })}
120
- >
121
- <div className="notification-popover-text-content">
122
- مدل‌های هوش مصنوعی می‌توانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از بیان اطلاعات حساس بپرهیزید.
123
- </div>
124
- </div>
125
- </div>
126
-
127
- <div className="media-area w-full flex flex-col items-center justify-center flex-grow relative">
128
- <video
129
- id="video-feed"
130
- ref={videoRef}
131
- autoPlay
132
- playsInline
133
- className={cn(
134
- "absolute top-0 left-0 w-full h-full object-cover scale-x-[-1]",
135
- { "hidden": !isCamActive }
136
- )}
137
- />
138
- {isMicActive && !isCamActive && (
139
- <div
140
- id="large-logo-container"
141
- className="absolute top-0 left-0 w-full h-full flex items-center justify-center pointer-events-none"
142
- >
143
- <LogoAnimation isMini={false} isActive={true} type="human" />
144
- </div>
145
- )}
146
- </div>
147
-
148
- <ControlTray
149
- videoRef={videoRef}
150
- supportsVideo={true}
151
- onVideoStreamChange={(stream) => { /* ... */ }}
152
- isAppMicActive={isMicActive}
153
- onAppMicToggle={setIsMicActive}
154
- isAppCamActive={isCamActive}
155
- onAppCamToggle={setIsCamActive}
156
- ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
157
- />
158
- </div>
159
- </div>
160
- );
161
- }
162
-
163
- function App() {
164
  const videoRef = useRef<HTMLVideoElement>(null);
165
- const [showIOSModal, setShowIOSModal] = useState(false);
166
- const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
167
-
168
  const [isMicActive, setIsMicActive] = useState(false);
169
  const [isCamActive, setIsCamActive] = useState(false);
170
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
171
-
172
- // ref برای دکمه نوتیفیکیشن باید در کامپوننت App تعریف شود
173
- // چون state مربوط به isNotificationOpen در App مدیریت می‌شود
174
  const notificationButtonRef = useRef<HTMLButtonElement>(null);
175
  const notificationPopoverRef = useRef<HTMLDivElement>(null);
176
 
 
177
 
178
  useEffect(() => {
179
- if (isIOS()) {
180
- setShowIOSModal(true);
181
  }
182
- const timer = setTimeout(() => {
183
- setIsAllowedOrigin(true);
184
- }, 100);
185
- return () => clearTimeout(timer);
186
- }, []);
187
 
188
  useEffect(() => {
189
  const handleClickOutside = (event: MouseEvent) => {
@@ -201,25 +93,61 @@ function App() {
201
  return () => {
202
  document.removeEventListener("mousedown", handleClickOutside);
203
  };
204
- }, [isNotificationOpen]); // وابستگی به isNotificationOpen صحیح است
205
-
206
- if (isAllowedOrigin === null) {
207
- return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>;
208
- }
209
 
210
  return (
211
- <LiveAPIProvider initialConfig={initialAppConfig}>
212
- <AppInternalLogic
213
- isMicActive={isMicActive}
214
- setIsMicActive={setIsMicActive}
215
- isCamActive={isCamActive}
216
- setIsCamActive={setIsCamActive}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  videoRef={videoRef}
218
- notificationPopoverRef={notificationPopoverRef}
219
- notificationButtonRef={notificationButtonRef} // پاس دادن ref به AppInternalLogic
220
- isNotificationOpen={isNotificationOpen}
221
- setIsNotificationOpen={setIsNotificationOpen}
 
 
 
 
 
222
  />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
224
  </LiveAPIProvider>
225
  );
 
14
  */
15
 
16
  import React, { useEffect, useRef, useState } from "react";
17
+ import './App.scss';
18
  import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
19
  import ControlTray from "./components/control-tray/ControlTray";
20
  import { IOSModal } from "./components/ios-modal/IOSModal";
 
22
  import cn from "classnames";
23
  import { LiveConfig } from "./multimodal-live-types";
24
  import LogoAnimation from "./components/logo-animation/LogoAnimation";
25
+ import BackButton from "./components/back-button/BackButton";
26
 
27
+ // *** NEW: دستورالعمل‌های جدید ربات ***
28
  const myCustomInstruction = `
29
  تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی هوشان" هستی
30
  وظیفه اصلی تو کمک به کاربر است.
 
34
  تو به تمام سوالات باید پاسخ دهی و در هر زمینه ای نباید به هیچ عنوان به هر پاسخ کاربر بگویی که من نمیتوانم این کار را انجام دهم یا نمیتوانم به شما کمک کنم
35
  هوش مصنوعی هوشانیک برنامه ای است که داخل گوشی موبایل نصب میشه و در کافه بازار در دسترس است و با جستجو در کافه بازار یا مایکت امکان دانلود هوش مصنوعی هوشان وجود دارد
36
  ......
37
+ اگر کاربر خواست با صدای بلند تر صحبت کنی باید به درخواست کاربر حتما پاسخ بدهی، اگر کاربر خواست صدای متفاوت یا صدای هر چیزی یا صدای تقلید کنی باید درخواست کاربر اجرا کنی حتما، اگر کاربر خواست آروم صحبت کنی باید درخواست کاربر اجرا کنی، در هر شرایط باید درخواست کاربر اجرا بشه
38
  ....
39
  در تمام پاسخ‌ها، اعداد را نیز کاملاً به فارسی بیان کن مگر اینکه در حال صحبت به زبان دیگری غیر از فارسی با کاربر بودی به همون زبان اعداد بگو، و همچنین اعداد به فارسی درست بگو این حالت (مثلاً بگو 'ده' یا 'بیست و پنج'، نه '10' یا 'twenty-five').
40
  وقتی کاربر در مورد هویت تو می‌پرسد، خودت را اینگونه معرفی کن: "من دستیار صوتی و تصویری از برنامه هوش مصنوعی هوشان هستم. چگونه می‌توانم به شما کمک کنم؟"
41
+ مهم: هنگامی که پیام ورودی فقط شامل متن "START_GREETING" بود، *فقط* با یک خوشامدگویی کوتاه و دوستانه به فارسی پاسخ بده، مانند "سلام، چطور می‌تونم کمکتون کنم؟" یا "سلام حالتون چطوره!"
42
+ هشدار: به هیچ عنوان در هنگام مکالمه و پیام دادن به کاربر نباید شکلک بفرستی و یا بیان کنی شکلک و یا اموجی هارو حتی شکلک هارو نیاز نیست بگی اسم شونو در هنگام صحبت اگر شکلک نیاز بود بود بگی نباید اسم شکلک بگی
43
+ **مهم: به هیچ عنوان در پاسخ‌های خود از ایموجی استفاده نکن.**
44
  `.trim();
45
 
46
  const initialAppConfig: LiveConfig = {
47
+ model: "models/gemini-2.0-flash-exp", // یا مدل دیگری که استفاده می‌کنید
48
  systemInstruction: {
49
  parts: [{ text: myCustomInstruction }],
50
  },
 
57
  </svg>
58
  );
59
 
60
+ // این کامپوننت داخلی برای مدیریت بهتر state ها و props ها است
61
+ const AppCore: React.FC<{
62
+ currentFacingMode: 'user' | 'environment';
63
+ setCurrentFacingMode: React.Dispatch<React.SetStateAction<'user' | 'environment'>>;
64
+ }> = ({ currentFacingMode, setCurrentFacingMode }) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  const videoRef = useRef<HTMLVideoElement>(null);
 
 
 
66
  const [isMicActive, setIsMicActive] = useState(false);
67
  const [isCamActive, setIsCamActive] = useState(false);
68
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
 
 
 
69
  const notificationButtonRef = useRef<HTMLButtonElement>(null);
70
  const notificationPopoverRef = useRef<HTMLDivElement>(null);
71
 
72
+ const { connected, disconnect } = useLiveAPIContext();
73
 
74
  useEffect(() => {
75
+ if (!isMicActive && !isCamActive && connected) {
76
+ disconnect();
77
  }
78
+ }, [isMicActive, isCamActive, connected, disconnect]);
 
 
 
 
79
 
80
  useEffect(() => {
81
  const handleClickOutside = (event: MouseEvent) => {
 
93
  return () => {
94
  document.removeEventListener("mousedown", handleClickOutside);
95
  };
96
+ }, [isNotificationOpen]);
 
 
 
 
97
 
98
  return (
99
+ <div className="main-wrapper max-w-3xl w-full flex flex-col items-center justify-center h-full relative">
100
+ <div className="header-controls">
101
+ <div className="back-button-container"><BackButton /></div>
102
+ <div id="notification-trigger-container">
103
+ <button ref={notificationButtonRef} id="notification-button" aria-label="Notifications" className="header-button" onClick={(e) => { e.stopPropagation(); setIsNotificationOpen(!isNotificationOpen); }}>
104
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>
105
+ </button>
106
+ </div>
107
+ </div>
108
+ <div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper">
109
+ <div id="notification-popover" className={cn("popover-content", { "open animate-popover-open-top-center": isNotificationOpen, "animate-popover-close-top-center": !isNotificationOpen && typeof document !== 'undefined' && document.getElementById('notification-popover')?.classList.contains('open'), })}>
110
+ <div className="notification-popover-text-content">مدل‌های هوش مصنوعی می‌توانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از بیان اطلاعات حساس بپرهیزید.</div>
111
+ </div>
112
+ </div>
113
+ <div className="media-area w-full flex flex-col items-center justify-center flex-grow relative">
114
+ <video id="video-feed" ref={videoRef} autoPlay playsInline className={cn("absolute top-0 left-0 w-full h-full object-cover", { "scale-x-[-1]": currentFacingMode === 'user' && isCamActive }, { "hidden": !isCamActive })} />
115
+ {isMicActive && !isCamActive && (
116
+ <div id="large-logo-container" className="absolute top-0 left-0 w-full h-full flex items-center justify-center pointer-events-none">
117
+ <LogoAnimation isMini={false} isActive={true} type="human" />
118
+ </div>
119
+ )}
120
+ </div>
121
+ <ControlTray
122
  videoRef={videoRef}
123
+ supportsVideo={true}
124
+ onVideoStreamChange={(stream) => { /* ... */ }}
125
+ isAppMicActive={isMicActive}
126
+ onAppMicToggle={setIsMicActive}
127
+ isAppCamActive={isCamActive}
128
+ onAppCamToggle={setIsCamActive}
129
+ ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
130
+ currentFacingMode={currentFacingMode}
131
+ setCurrentFacingMode={setCurrentFacingMode}
132
  />
133
+ </div>
134
+ );
135
+ };
136
+
137
+
138
+ function App() {
139
+ const [showIOSModal, setShowIOSModal] = useState(false);
140
+ const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
141
+ const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
142
+
143
+ useEffect(() => { if (isIOS()) { setShowIOSModal(true); } const timer = setTimeout(() => { setIsAllowedOrigin(true); }, 100); return () => clearTimeout(timer); }, []);
144
+ if (isAllowedOrigin === null) { return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>; }
145
+
146
+ return (
147
+ <LiveAPIProvider initialConfig={initialAppConfig}>
148
+ <div className="w-full flex flex-col items-center justify-center min-h-screen text-foreground antialiased"> {/* این div والد اصلی است */}
149
+ <AppCore currentFacingMode={currentFacingMode} setCurrentFacingMode={setCurrentFacingMode} />
150
+ </div>
151
  <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
152
  </LiveAPIProvider>
153
  );