File size: 14,142 Bytes
7daa6bf
288545c
f9fe03e
 
 
 
 
 
 
 
 
 
 
 
 
cc21a82
f9fe03e
4ad9b66
 
 
cc21a82
 
 
7daa6bf
f9fe03e
cc21a82
 
f9fe03e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc21a82
 
 
fc82e2c
cc21a82
 
 
 
 
8f94a86
 
 
 
 
f9fe03e
cfee1d1
 
 
 
 
 
 
 
f9fe03e
cfee1d1
 
 
 
 
 
 
 
 
f9fe03e
cfee1d1
f9fe03e
cfee1d1
 
 
 
 
 
 
 
 
 
 
 
 
f9fe03e
cfee1d1
f9fe03e
cfee1d1
f9fe03e
cfee1d1
 
f9fe03e
cfee1d1
 
f9fe03e
cfee1d1
 
 
 
 
 
 
 
 
 
8f94a86
 
 
 
 
 
 
 
 
f9fe03e
8f94a86
 
 
cfee1d1
 
 
 
 
 
 
 
f9fe03e
cfee1d1
 
 
 
8f94a86
 
 
 
cfee1d1
 
 
 
f9fe03e
 
 
 
 
 
 
 
 
 
 
cfee1d1
 
 
 
 
 
f9fe03e
cfee1d1
 
 
 
 
 
 
f9fe03e
 
cfee1d1
 
 
f9fe03e
8f94a86
 
 
 
 
 
 
 
 
cfee1d1
8f94a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9fe03e
8f94a86
 
 
 
094fd2d
9910b51
cfee1d1
 
 
 
 
 
 
 
f9fe03e
cfee1d1
 
0336c37
cfee1d1
 
90131db
cfee1d1
90131db
8f94a86
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
// src/App.tsx

/**
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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"; // <<-- ایمپورت کامپوننت جدید دکمه بازگشت

const myCustomInstruction = `
ت1. هویت دستیار:

فقط خود را به عنوان "دستیار صوتی و تصویری اپلیکیشن زبانفلای" معرفی کن.

هرگز خود را محصول یا زیرمجموعه گوگل، اپل یا هیچ شرکت دیگری معرفی نکن.

در پاسخ به سؤال کاربر درباره هویت، دقیقاً بگو:
"من دستیار صوتی و تصویری اپلیکیشن زبانفلای هستم. چگونه می‌توانم به شما کمک کنم؟"

مدیریت زبان گفتگو:

به طور پیش‌فرض، مکالمه را به زبان انگلیسی آغاز کن.

اگر کاربر با زبانی غیر از انگلیسی صحبت کرد، گفتگو را به همان زبان ادامه بده.

زبان پاسخ را همیشه مطابق زبان کاربر تنظیم کن. تغییر زبان به صورت خودکار انجام شود.

بیان اعداد:

هنگام مکالمه به انگلیسی، اعداد را به صورت کامل به انگلیسی بیان کن (مثلاً: twenty-five).

هنگام مکالمه به زبانی دیگر، اعداد را به همان زبان بیان کن (مثلاً: بیست و پنج).

تنظیم صدا و لحن:

با صدای مایل به بلند و شفاف صحبت کن.

در صورت درخواست کاربر، صدای خود را بلندتر، آرام‌تر یا به تقلید از صدای خواسته‌شده تغییر بده.

بدون استثنا، هرگونه تغییر صدای درخواستی کاربر را اجرا کن.

تعهد به پاسخگویی:

تحت هیچ شرایطی به کاربر نگو که "نمی‌توانم پاسخ دهم" یا "نمی‌توانم کمک کنم".

همیشه در تمام زمینه‌ها تلاش کن بهترین پاسخ ممکن را به کاربر ارائه بدهی.

رفتار در شروع گفتگو:

اگر پیام ورودی "START_GREETING" دریافت شد، تنها با یکی از جملات کوتاه زیر پاسخ بده:

"Hello, how can I help you?"

یا "Hey, what's up?"

اطلاع‌رسانی درباره اپلیکیشن زبانفلای:

در صورت نیاز کاربر، توضیح بده که اپلیکیشن زبانفلای در گوگل پلی و کافه بازار قابل دانلود است.

راهنمایی کن که کاربران می‌توانند با جستجو در این فروشگاه‌ها، اپلیکیشن را نصب کنند.

پشتیبانی از یادگیری زبان:

هنگام پاسخگویی، در صورت مرتبط بودن، مثال‌های جمله‌سازی، کاربرد واژگان و اصطلاحات ارائه بده.

در صورت درخواست کاربر، معانی واژگان، هم‌معنی‌ها، متضادها، و نمونه جمله‌ها را هم ارائه بده.

تمرین شنیداری و گفتاری:

اگر کاربر بخواهد، جمله‌ای را بخوان و از او بخواه آن را تکرار کند (تمرین shadowing).

فرصت کامل برای تکرار بده و کاربر را با بازخورد مثبت تشویق کن.

شخصی‌سازی تجربه یادگیری:

در صورت درخواست کاربر، سرعت مکالمه را کند یا تند کن.

سطح سختی واژگان و جملات را بر اساس سطح کاربر (مبتدی، متوسط، پیشرفته) تنظیم کن.

**تعامل تصویری:**
- به تصویر زنده‌ای که از کاربر دریافت می‌کنی توجه کن.
- اگر در تصویر نکته قابل توجهی وجود دارد (مانند حالت چهره، اشیاء خاص، یا محیط اطراف کاربر)، می‌توانی به آن در مکالمه اشاره کنی، البته فقط اگر مرتبط با موضوع صحبت باشد یا کاربر از تو بخواهد.
- اگر کاربر سوالی در مورد چیزی که در تصویر می‌بیند پرسید، سعی کن بر اساس تصویر پاسخ دهی.
- هدف اصلی، کمک به یادگیری زبان است، پس تعامل تصویری باید در خدمت این هدف باشد.
`.trim();

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

const SvgReferenceMicrophoneIcon = () => (
    <svg className="reference-mic-svg" viewBox="0 0 69 68" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path opacity="0.4" d="M49.9479 27.1824C49.0803 27.1824 48.3907 27.872 48.3907 28.7396V32.2544C48.3907 40.1293 41.984 46.5361 34.109 46.5361C26.234 46.5361 19.8273 40.1293 19.8273 32.2544V28.7173C19.8273 27.8497 19.1377 27.1601 18.2701 27.1601C17.4025 27.1601 16.7129 27.8497 16.7129 28.7173V32.2321C16.7129 41.2861 23.6758 48.7384 32.5518 49.5393V54.2776C32.5518 55.1452 33.2414 55.8348 34.109 55.8348C34.9766 55.8348 35.6662 55.1452 35.6662 54.2776V49.5393C44.52 48.7607 51.5051 41.2861 51.5051 32.2321V28.7173C51.4829 27.872 50.7933 27.1824 49.9479 27.1824Z" fill="#BE123C"/>
        <path d="M34.1099 11.3434C28.682 11.3434 24.2773 15.7481 24.2773 21.176V32.5658C24.2773 37.9938 28.682 42.3984 34.1099 42.3984C39.5379 42.3984 43.9425 37.9938 43.9425 32.5658V21.176C43.9425 15.7481 39.5379 11.3434 34.1099 11.3434ZM37.0241 26.8042C36.8684 27.3826 36.3567 27.7608 35.7784 27.7608C35.6671 27.7608 35.5559 27.7385 35.4447 27.7163C34.5771 27.4716 33.665 27.4716 32.7974 27.7163C32.0856 27.9165 31.396 27.4938 31.218 26.8042C31.0178 26.1146 31.4404 25.4027 32.1301 25.2247C33.4426 24.8688 34.8218 24.8688 36.1343 25.2247C36.8017 25.4027 37.2021 26.1146 37.0241 26.8042ZM38.2031 22.4885C38.0029 23.0224 37.5135 23.3339 36.9796 23.3339C36.8239 23.3339 36.6904 23.3116 36.5347 23.2671C34.9775 22.6887 33.2423 22.6887 31.6852 23.2671C31.0178 23.5118 30.2614 23.1559 30.0167 22.4885C29.772 21.8212 30.128 21.0648 30.7953 20.8423C32.9309 20.0637 35.289 20.0637 37.4245 20.8423C38.0919 21.087 38.4478 21.8212 38.2031 22.4885Z" fill="#BE123C"/>
    </svg>
);

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>; // این ref برای دکمه نوتیفیکیشن است
  isNotificationOpen: boolean;
  setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>;
}> = ({
  isMicActive,
  isCamActive,
  setIsMicActive,
  setIsCamActive,
  videoRef,
  notificationPopoverRef,
  notificationButtonRef, // دریافت ref
  isNotificationOpen,
  setIsNotificationOpen
}) => {
  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} // ref به این دکمه متصل است
              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
            id="notification-popover"
            className={cn("popover-content", {
              "open animate-popover-open-top-center": isNotificationOpen,
              "animate-popover-close-top-center": !isNotificationOpen && document.getElementById('notification-popover')?.classList.contains('open'),
            })}
          >
            <div className="notification-popover-text-content">
              مدل‌های هوش مصنوعی می‌توانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از بیان اطلاعات حساس بپرهیزید.
            </div>
          </div>
        </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 scale-x-[-1]",
              { "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}
        />
      </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);

  // ref برای دکمه نوتیفیکیشن باید در کامپوننت App تعریف شود
  // چون state مربوط به isNotificationOpen در App مدیریت می‌شود
  const notificationButtonRef = useRef<HTMLButtonElement>(null);
  const notificationPopoverRef = useRef<HTMLDivElement>(null);


  useEffect(() => {
    if (isIOS()) {
      setShowIOSModal(true);
    }
    const timer = setTimeout(() => {
        setIsAllowedOrigin(true);
    }, 100);
    return () => clearTimeout(timer);
  }, []);

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (
        isNotificationOpen &&
        notificationPopoverRef.current &&
        !notificationPopoverRef.current.contains(event.target as Node) &&
        notificationButtonRef.current &&
        !notificationButtonRef.current.contains(event.target as Node)
      ) {
        setIsNotificationOpen(false);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [isNotificationOpen]); // وابستگی به isNotificationOpen صحیح است

  if (isAllowedOrigin === null) {
    return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>;
  }

  return (
    <LiveAPIProvider initialConfig={initialAppConfig}>
      <AppInternalLogic
        isMicActive={isMicActive}
        setIsMicActive={setIsMicActive}
        isCamActive={isCamActive}
        setIsCamActive={setIsCamActive}
        videoRef={videoRef}
        notificationPopoverRef={notificationPopoverRef}
        notificationButtonRef={notificationButtonRef} // پاس دادن ref به AppInternalLogic
        isNotificationOpen={isNotificationOpen}
        setIsNotificationOpen={setIsNotificationOpen}
      />
      <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
    </LiveAPIProvider>
  );
}

export default App;