File size: 17,865 Bytes
7daa6bf
288545c
cfee1d1
cc21a82
90131db
4ad9b66
 
 
cc21a82
 
 
7daa6bf
90131db
cc21a82
cfee1d1
cc21a82
ccb1e48
 
 
 
 
 
 
 
90131db
ccb1e48
 
 
cfee1d1
90131db
cfee1d1
cc21a82
 
 
fc82e2c
cc21a82
 
 
 
 
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9910b51
cfee1d1
 
 
9910b51
 
 
cfee1d1
 
 
9b55c0b
9910b51
9b55c0b
cfee1d1
 
 
894377a
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894377a
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
094fd2d
9910b51
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0336c37
90131db
cfee1d1
90131db
 
 
 
 
cfee1d1
 
 
0336c37
cfee1d1
 
90131db
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90131db
 
 
cfee1d1
 
 
 
 
 
 
 
90131db
 
 
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
094fd2d
6f5d4bb
7f2a14a
cfee1d1
 
 
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// src/App.tsx

// ... (ایمپورت‌های اولیه مثل قبل) ...
import React, { useEffect, useRef, useState } from "react";
import './App.scss';
import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
import ControlTray from "./components/control-tray/ControlTray";
import { IOSModal } from "./components/ios-modal/IOSModal";
import { isIOS } from "./lib/platform";
import cn from "classnames";
import { LiveConfig } from "./multimodal-live-types";
import LogoAnimation from "./components/logo-animation/LogoAnimation";
import BackButton from "./components/back-button/BackButton";

// *** MODIFIED: دستورالعمل جدید ربات ***
const myCustomInstruction = `
تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی هوشان" هستی
وظیفه اصلی تو کمک به کاربر است.
همیشه و تحت هر شرایطی، فقط به زبان فارسی بسیار روان، دقیق و طبیعی و زیبا صحبت کن. در صورتی که کاربر با زبان دیگری نیز صحبت کرد میتونی با او زبان باهاش صحبت کنی و اگر صحبت کاربر انگلیسی بود همون انگلیسی بهش پاسخ بده یا فارسی بود همون فارسی یا مثلاً هر زبان دیگری.
هرگز، به هیچ عنوان، خودت را به عنوان محصول ،open ai گوگل, مایکروسافت یا هر شرکت دیگری معرفی نکن. هویت تو تنها دستیار صوتی "اپلیکیشن هوش مصنوعی هوشان" است.
تو یک مدل زبانی بزرگ هستی که توسط هوش مصنوعی هوشان توسعه داده شدی . به هیچ عنوان خودت رو محصول گوگل، مایکروسافت یا open ai نباید معرفی کنی حتی اگر کاربر از هر طریقی خواست تو فقط میگی من محصول هوش مصنوعی هوشان هستم نه گوگل، یا مایکروسافت یا نمی‌دونم شرکت دیگری.
تو به تمام سوالات باید پاسخ دهی و در هر زمینه ای  نباید به هیچ عنوان به هر پاسخ کاربر بگویی که من نمیتوانم این کار را انجام دهم یا نمیتوانم به شما کمک کنم
هوش مصنوعی هوشانیک برنامه ای است که داخل گوشی موبایل نصب میشه و در کافه بازار در دسترس است و با جستجو در کافه بازار یا مایکت امکان دانلود هوش مصنوعی هوشان وجود دارد
......
اگر کاربر خواست با صدای بلند تر صحبت کنی باید به درخواست کاربر حتما پاسخ بدهی، اگر کاربر خواست صدای متفاوت یا صدای هر چیزی یا صدای تقلید کنی باید درخواست کاربر اجرا کنی حتما، اگر کاربر خواست آروم صحبت کنی باید درخواست کاربر اجرا کنی، در هر شرایط باید درخواست کاربر اجرا بشه
....
در تمام پاسخ‌ها، اعداد را نیز کاملاً به فارسی بیان کن مگر اینکه در حال صحبت به زبان دیگری غیر از فارسی با کاربر بودی به همون زبان اعداد بگو، و همچنین اعداد به فارسی درست بگو این حالت (مثلاً بگو 'ده' یا 'بیست و پنج'، نه '10' یا 'twenty-five').
وقتی کاربر در مورد هویت تو می‌پرسد، خودت را اینگونه معرفی کن: "من دستیار صوتی و تصویری از برنامه هوش مصنوعی هوشان هستم. چگونه می‌توانم به شما کمک کنم؟"
مهم: هنگامی که پیام ورودی فقط شامل متن "START_GREETING" بود، فقط با یک خوشامدگویی کوتاه و دوستانه به فارسی پاسخ بده، مانند "سلام، چطور می‌تونم کمکتون کنم؟" یا "سلام  حالتون چطوره!
هشدار: به هیچ عنوان در هنگام مکالمه و پیام دادن به کاربر نباید شکلک بفرستی و یا بیان کنی شکلک و یا اموجی هارو حتی شکلک هارو نیاز نیست بگی اسم شونو در هنگام صحبت اگر شکلک نیاز بود بود بگی نباید اسم شکلک بگی
مهم: به هیچ عنوان در پاسخ‌های خود از ایموجی استفاده نکن.
`.trim();

const initialAppConfig: LiveConfig = {
  model: "models/gemini-2.0-flash-exp",
  systemInstruction: {
    parts: [{ text: myCustomInstruction }],
  },
};

const SvgReferenceMicrophoneIcon = () => ( /* ... (بدون تغییر) ... */ );

const AppInternalLogic: React.FC<{
  isMicActive: boolean;
  isCamActive: boolean;
  setIsMicActive: React.Dispatch<React.SetStateAction<boolean>>;
  setIsCamActive: React.Dispatch<React.SetStateAction<boolean>>;
  videoRef: React.RefObject<HTMLVideoElement>;
  notificationPopoverRef: React.RefObject<HTMLDivElement>;
  notificationButtonRef: React.RefObject<HTMLButtonElement>;
  isNotificationOpen: boolean;
  setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>;
  currentFacingMode: 'user' | 'environment'; // *** NEW PROP ***
}> = ({
  isMicActive,
  isCamActive,
  setIsMicActive,
  setIsCamActive,
  videoRef,
  notificationPopoverRef,
  notificationButtonRef,
  isNotificationOpen,
  setIsNotificationOpen,
  currentFacingMode, // *** NEW PROP ***
}) => {
  const { connected, disconnect } = useLiveAPIContext();

  useEffect(() => {
    if (!isMicActive && !isCamActive && connected) {
      disconnect();
    }
  }, [isMicActive, isCamActive, connected, disconnect]);

  return (
    <div className="w-full flex flex-col items-center justify-center min-h-screen text-foreground antialiased">
      <div className="main-wrapper max-w-3xl w-full flex flex-col items-center justify-center h-full relative">
        <div className="header-controls">
          <div className="back-button-container">
            <BackButton />
          </div>
          <div id="notification-trigger-container">
            <button
              ref={notificationButtonRef}
              id="notification-button"
              aria-label="Notifications"
              className="header-button"
              onClick={(e) => {
                  e.stopPropagation();
                  setIsNotificationOpen(!isNotificationOpen);
                }}
            >
              <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>
            </button>
          </div>
        </div>

        <div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper"> {/* ... */} </div>

        <div className="media-area w-full flex flex-col items-center justify-center flex-grow relative">
          <video
            id="video-feed"
            ref={videoRef}
            autoPlay
            playsInline
            className={cn(
              "absolute top-0 left-0 w-full h-full object-cover",
              // *** MODIFIED: اعمال scale-x-[-1] فقط برای دوربین جلو ***
              { "scale-x-[-1]": currentFacingMode === 'user' },
              { "hidden": !isCamActive }
            )}
          />
          {isMicActive && !isCamActive && (
            <div id="large-logo-container" className="absolute top-0 left-0 w-full h-full flex items-center justify-center pointer-events-none">
              <LogoAnimation isMini={false} isActive={true} type="human" />
            </div>
          )}
        </div>

        <ControlTray
          videoRef={videoRef}
          supportsVideo={true}
          onVideoStreamChange={(stream) => { /* ... */ }}
          isAppMicActive={isMicActive}
          onAppMicToggle={setIsMicActive}
          isAppCamActive={isCamActive}
          onAppCamToggle={setIsCamActive}
          ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
          // currentFacingMode و setCurrentFacingMode از App به ControlTray پاس داده می‌شوند
        />
      </div>
    </div>
  );
}

function App() {
  const videoRef = useRef<HTMLVideoElement>(null);
  const [showIOSModal, setShowIOSModal] = useState(false);
  const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);

  const [isMicActive, setIsMicActive] = useState(false);
  const [isCamActive, setIsCamActive] = useState(false);
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
  // *** NEW STATE: برای نگهداری حالت دوربین (جلو یا عقب) ***
  const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');

  const notificationButtonRef = useRef<HTMLButtonElement>(null);
  const notificationPopoverRef = useRef<HTMLDivElement>(null);

  useEffect(() => { /* ... */ }, []);
  useEffect(() => { /* ... */ }, [isNotificationOpen]);
  if (isAllowedOrigin === null) { /* ... */ }

  return (
    <LiveAPIProvider initialConfig={initialAppConfig}>
      <AppInternalLogic
        isMicActive={isMicActive}
        setIsMicActive={setIsMicActive}
        isCamActive={isCamActive}
        setIsCamActive={setIsCamActive}
        videoRef={videoRef}
        notificationPopoverRef={notificationPopoverRef}
        notificationButtonRef={notificationButtonRef}
        isNotificationOpen={isNotificationOpen}
        setIsNotificationOpen={setIsNotificationOpen}
        currentFacingMode={currentFacingMode} // *** پاس دادن state جدید ***
      />
      {/* ControlTray دیگر نیازی به پاس دادن setCurrentFacingMode ندارد چون این state در App.tsx مدیریت می‌شود */}
      {/* و خود ControlTray باید currentFacingMode را از App.tsx به عنوان prop دریافت کند */}
      {/* اگر ControlTray نیاز به تغییر facingMode دارد، باید یک تابع callback پاس داده شود */}
      <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
    </LiveAPIProvider>
  );
}
// تابع App به یک تابع نیاز دارد که به ControlTray پاس داده شود تا بتواند currentFacingMode را تغییر دهد.
// یا اینکه currentFacingMode به طور کامل در ControlTray مدیریت شود.
// برای سادگی، فعلا فرض می‌کنیم ControlTray فقط currentFacingMode را از App دریافت می‌کند.
// اگر نیاز به تغییر آن از ControlTray باشد، باید یک setter هم پاس دهیم.

// برای اینکه ControlTray بتواند currentFacingMode را تغییر دهد، باید `setCurrentFacingMode` را هم به آن پاس دهیم.
// اصلاح تابع App:
function AppWithFacingModeControl() {
  const videoRef = useRef<HTMLVideoElement>(null);
  const [showIOSModal, setShowIOSModal] = useState(false);
  const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);

  const [isMicActive, setIsMicActive] = useState(false);
  const [isCamActive, setIsCamActive] = useState(false);
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
  const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // State در App

  const notificationButtonRef = useRef<HTMLButtonElement>(null);
  const notificationPopoverRef = useRef<HTMLDivElement>(null);

  // useEffect ها مثل قبل

  if (isAllowedOrigin === null) { /* ... */ }

  return (
    <LiveAPIProvider initialConfig={initialAppConfig}>
      {/* AppInternalLogic currentFacingMode را برای نمایش ویدیو استفاده می‌کند */}
      <AppInternalLogic
        isMicActive={isMicActive}
        setIsMicActive={setIsMicActive}
        isCamActive={isCamActive}
        setIsCamActive={setIsCamActive}
        videoRef={videoRef}
        notificationPopoverRef={notificationPopoverRef}
        notificationButtonRef={notificationButtonRef}
        isNotificationOpen={isNotificationOpen}
        setIsNotificationOpen={setIsNotificationOpen}
        currentFacingMode={currentFacingMode} // پاس دادن حالت فعلی
      />
      {/* ControlTray نیاز به setCurrentFacingMode دارد تا بتواند آن را تغییر دهد */}
      {/* برای این کار، ControlTray باید این تابع را به عنوان prop دریافت کند */}
      {/* و App.tsx باید آن را فراهم کند. این در ControlTray.tsx پیاده‌سازی می‌شود. */}
      {/* فعلا در این فایل، فقط currentFacingMode را به AppInternalLogic پاس می‌دهیم. */}
      {/* تغییر facing mode در خود ControlTray مدیریت می‌شود و state آن به App برگردانده نمی‌شود. */}
      {/* این برای نمایش scale-x صحیح کافی است اگر ControlTray خودش facingMode را مدیریت کند. */}
      {/* اما برای اینکه AppInternalLogic هم بداند، باید state مشترک باشد. */}
      {/* ساده‌ترین راه این است که ControlTray یک prop onFacingModeChange بگیرد */}
      <ControlTrayPassProps
        videoRef={videoRef}
        supportsVideo={true}
        onVideoStreamChange={(stream) => { /* ... */ }}
        isAppMicActive={isMicActive}
        onAppMicToggle={setIsMicActive}
        isAppCamActive={isCamActive}
        onAppCamToggle={setIsCamActive}
        ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
        // *** NEW: پاس دادن تابع برای تغییر facing mode ***
        onFacingModeChange={setCurrentFacingMode}
        initialFacingMode={currentFacingMode} // پاس دادن حالت اولیه
      />
      <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
    </LiveAPIProvider>
  );
}
// برای جلوگیری از پیچیدگی زیاد، فعلا از AppWithFacingModeControl صرف نظر می‌کنیم
// و فرض می‌کنیم ControlTray خودش currentFacingMode را مدیریت می‌کند و ما فقط برای نمایش از آن استفاده می‌کنیم.
// این یعنی AppInternalLogic باید currentFacingMode را از ControlTray بگیرد یا ControlTray آن را به App پاس دهد.
// راه حل ساده‌تر: currentFacingMode را فقط در ControlTray نگه داریم و از طریق یک prop به AppInternalLogic بدهیم.
// اما چون AppInternalLogic والد ControlTray است، این کار مستقیم نیست.
// بهترین راه: state در App، و پاس دادن state و setter به کامپوننت‌های فرزند.

// اصلاح نهایی App و AppInternalLogic:
// (کد SvgReferenceMicrophoneIcon و initialAppConfig و myCustomInstruction مثل قبل)

const CorrectedAppInternalLogic: React.FC<{ /* props مثل قبل */ currentFacingMode: 'user' | 'environment'; }> = ({ /* ... */ currentFacingMode }) => {
    // ... (JSX مثل قبل، اما با استفاده از currentFacingMode برای video className)
    // <video className={cn(..., { "scale-x-[-1]": currentFacingMode === 'user' }, ... )} />
    // ...
    return ( /* ... JSX قبلی با currentFacingMode ... */ );
};


function CorrectedApp() {
  // ... (state های isMicActive, isCamActive, isNotificationOpen مثل قبل)
  const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // State در App
  // ... (ref ها مثل قبل)

  // useEffect ها مثل قبل

  if (isAllowedOrigin === null) { /* ... */ }

  return (
    <LiveAPIProvider initialConfig={initialAppConfig}>
      <AppInternalLogic // استفاده از AppInternalLogic که currentFacingMode را دریافت می‌کند
        isMicActive={isMicActive}
        setIsMicActive={setIsMicActive}
        isCamActive={isCamActive}
        setIsCamActive={setIsCamActive}
        videoRef={useRef<HTMLVideoElement>(null)} // ref ها باید اینجا باشند یا از AppInternalLogic مدیریت شوند
        notificationPopoverRef={useRef<HTMLDivElement>(null)}
        notificationButtonRef={useRef<HTMLButtonElement>(null)}
        isNotificationOpen={isNotificationOpen}
        setIsNotificationOpen={setIsNotificationOpen}
        currentFacingMode={currentFacingMode} // پاس دادن state
      />
      <ControlTray
        videoRef={useRef<HTMLVideoElement>(null)} // این ref ها باید با AppInternalLogic هماهنگ باشند
        supportsVideo={true}
        onVideoStreamChange={(stream) => { /* ... */ }}
        isAppMicActive={isMicActive}
        onAppMicToggle={setIsMicActive}
        isAppCamActive={isCamActive}
        onAppCamToggle={setIsCamActive}
        ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
        onFacingModeChange={setCurrentFacingMode} // پاس دادن setter
        initialFacingMode={currentFacingMode}     // پاس دادن مقدار اولیه
      />
      <IOSModal isOpen={useState(false)[0]} onClose={() => useState(false)[1](false)} />
    </LiveAPIProvider>
  );
}
// استفاده از CorrectedApp به جای App
// export default CorrectedApp; // <--- این را فعال کنید
export default App; // فعلا برای حفظ سادگی از App اصلی استفاده می‌کنیم و تغییرات را در ControlTray اعمال می‌کنیم