Spaces:
Running
Running
File size: 17,979 Bytes
a9bf795 eafdfe6 3a18fb8 7f2a14a eafdfe6 7f2a14a d8e9074 a9bf795 d8e9074 dc3cd2d d8e9074 d66b261 eafdfe6 ee03d5a 75120d3 ee03d5a 75120d3 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a eafdfe6 ee03d5a 1a9bc32 eafdfe6 dc3cd2d eafdfe6 dc3cd2d eafdfe6 dc3cd2d eafdfe6 dc3cd2d eafdfe6 3a18fb8 eafdfe6 3a18fb8 eafdfe6 3a18fb8 eafdfe6 dc3cd2d eafdfe6 7f2a14a d66b261 |
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
/**
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, useCallback } from "react";
import "./App.scss"; // Updated SCSS file
import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
// SidePanel and Altair are part of the original Gemini UI.
// You need to decide if/how they fit into the new UI.
// For now, we'll include them but they might need styling or to be conditionally rendered.
// import SidePanel from "./components/side-panel/SidePanel"; // اگر استفاده نمیکنید کامنت کنید
import { Altair } from "./components/altair/Altair";
import ControlTray from "./components/control-tray/ControlTray"; // This will be our new footer
import { IOSModal } from "./components/ios-modal/IOSModal";
import { isIOS } from "./lib/platform";
import { LiveConfig } from "./multimodal-live-types";
// --- 👇 دامنه مجاز خودتان را اینجا قرار دهید (با https یا http) 👇 ---
const ALLOWED_ORIGIN = "https://www.aisada.ir"; // یا "http://www.aisada.ir"; // یا "https://aisada.ir"; // یا "http://aisada.ir" اگر سایتتان http است
// --- 👆 ---
// --- 👇 دستورالعمل شخصیسازی شما (بدون تغییر) 👇 ---
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 }]
}
};
// --- 👆 ---
// SVG Icons (as React components or functions returning JSX)
const NotificationIcon = () => (
<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>
);
const BackIcon = () => (
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m15 18-6-6 6-6"></path></svg>
);
const HumanIconSVG = ({ width = 70, height = 70 }: { width?: number, height?: number }) => (
<svg width={width} height={height} viewBox="0 0 88 89" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M75.1481 81.6361H12.9259C9.66667 81.6361 7 78.9721 7 75.7161V58.5112C7 57.5862 7 57.1052 7.44444 56.2172C8.85185 52.9612 13 50.2232 19.4815 47.8922C24.1111 56.6982 33.3704 62.6921 44 62.6921C54.6296 62.6921 63.9259 56.6982 68.5185 47.8922C75 50.1862 79.1852 52.9982 80.5556 56.2172C81 56.6612 81 57.6232 81 58.5112V75.7161C81 78.9721 78.3333 81.6361 75.0741 81.6361H75.1481Z" stroke="#FCFCFC" strokeWidth="6.42146" strokeLinecap="round" strokeLinejoin="round" /><path d="M44.0371 50.1862C33.8519 50.1862 25.5186 41.8612 25.5186 31.6863V26.1363C25.5186 15.9613 33.8519 7.63635 44.0371 7.63635C54.2223 7.63635 62.5556 15.9613 62.5556 26.1363V31.6863C62.5556 41.8612 54.2223 50.1862 44.0371 50.1862Z" stroke="#FCFCFC" strokeWidth="6.42146" strokeLinecap="round" strokeLinejoin="round" /></svg>
);
const LogoDisplay = ({ isMini, isHumanActive }: { isMini: boolean, isHumanActive: boolean }) => {
if (!isHumanActive) return null; // Simplified: only show human logo
const size = isMini ? 80 : 200;
const iconSize = isMini ? 35 : 70;
const insetBase = isMini
? { ping: 10, outer: 0, mid: 5, inner: 12, icon: 22.5 } // Centered icon
: { ping: 40, outer: 0, mid: 20, inner: 50, icon: 65 }; // Centered icon
const bgColorBase = 'blue'; // Simplified to blue for human
return (
<div className="relative" style={{ width: `${size}px`, height: `${size}px` }}>
{/* These divs are for Tailwind JIT, not strictly needed if classes are used directly */}
{/* <div className="hidden bg-blue-200 bg-blue-300 bg-blue-400"></div> */}
<div className={`absolute rounded-full opacity-50 animate-ping bg-${bgColorBase}-200 dark:bg-${bgColorBase}-700`} style={{ inset: `${insetBase.ping}px` }}></div>
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-200 dark:bg-${bgColorBase}-700`} style={{ inset: `${insetBase.outer}px` }}></div>
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-300 dark:bg-${bgColorBase}-600`} style={{ inset: `${insetBase.mid}px` }}></div>
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-400 dark:bg-${bgColorBase}-500`} style={{ inset: `${insetBase.inner}px` }}></div>
<div className="z-10 absolute" style={{ top: `${insetBase.icon}px`, left: `${insetBase.icon}px` }}>
<HumanIconSVG width={iconSize} height={iconSize} />
</div>
</div>
);
};
function AppContent() {
const videoRef = useRef<HTMLVideoElement>(null);
const [videoStream, setVideoStream] = useState<MediaStream | null>(null);
const [showIOSModal, setShowIOSModal] = useState(false);
const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
const [isNotificationOpen, setIsNotificationOpen] = useState(false); // <--- اصلاح در این خط
const notificationPopoverRef = useRef<HTMLDivElement>(null);
const notificationButtonRef = useRef<HTMLButtonElement>(null);
// States for new UI, to be controlled by ControlTray or LiveAPIContext
const [isMicActive, setIsMicActive] = useState(false); // Is microphone capturing
const [isCamActive, setIsCamActive] = useState(false); // Is camera capturing
const { connected, volume } = useLiveAPIContext(); // Get connection status and volume
useEffect(() => {
if (isIOS()) {
setShowIOSModal(true);
}
try {
if (window.self !== window.top) {
if (window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0) {
const parentOrigin = window.location.ancestorOrigins[0];
if (parentOrigin === ALLOWED_ORIGIN) {
setIsAllowedOrigin(true);
} else {
console.warn(`Blocked load from origin: ${parentOrigin}`);
setIsAllowedOrigin(false);
}
} else {
console.warn("Cannot verify parent origin (ancestorOrigins not available/empty). Blocking.");
setIsAllowedOrigin(false);
}
} else {
console.warn("App loaded directly, not in an iframe. Blocking.");
setIsAllowedOrigin(false); // For security, must be in iframe from allowed origin
}
} catch (e) {
console.error("Cross-origin access error, cannot verify parent. Blocking.", e);
setIsAllowedOrigin(false);
}
// Dark mode toggle for testing (can be removed or integrated with a theme switcher)
// document.documentElement.classList.add('dark');
// Click outside listener for notification popover
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('click', handleClickOutside);
return () => {
document.removeEventListener('click', handleClickOutside);
};
}, [isNotificationOpen]);
const handleVideoStreamChange = useCallback((stream: MediaStream | null) => {
setVideoStream(stream);
setIsCamActive(!!stream); // Update cam active state based on stream
if (videoRef.current) {
videoRef.current.srcObject = stream;
if (stream) {
videoRef.current.play().catch(e => console.warn("Video play failed:", e));
}
}
}, []);
useEffect(() => {
// This useEffect was intended for CSS --volume for mic pulse,
// but ControlTray.tsx now handles its own inVolume for that.
// This 'volume' from useLiveAPIContext is usually output volume.
// If you need to visualize output volume elsewhere, you can use it.
}, [volume]);
if (isAllowedOrigin === null) {
return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>;
}
if (isAllowedOrigin === false) {
return <div style={{ padding: '20px', textAlign: 'center', color: 'red' }}>دسترسی غیرمجاز! اگر چت صوتی و تصویری برای شما باز نمیشود این لینک رو با مرورگر کروم باز کنید و همچنین مرورگر کروم رو به عنوان مرورگر پیشفرض گوشی خود قرار دهید تا هر بار زدن روی دکمه شروع داخل برنامه لینک با مرورگر کروم باز بشه، برای پیشفرض قرار دادن مرورگر کروم وارد تنظیمات گوشی خود شوید قسمت برنامه ها ، مدیریت برنامه ها رو کلیک کنید بالای صفحه روی سه نقطه بزنید و تنظیمات بیشتر رو انتخاب کنید بعدا وارد قسمت برنامه های پیش فرض شوید و مرورگر کروم رو به عنوان مرورگر پیشفرض خود قرار دهید، اگر مشکلی بود حتماً به پشتیبانی برنامه پیام بفرستید</div>;
}
return (
<div className="App"> {/* Main container from original Gemini App */}
{/* <SidePanel /> // اگر استفاده نمیکنید کامنت کنید یا حذف کنید */}
<main> {/* Original main, Altair will go here */}
<div className="main-app-area"> {/* For Altair and video */}
{/* Altair is the chat bubbles. Position it as needed. */}
<Altair />
{/* Header */}
<div className="header-controls">
<button
id="notification-button"
ref={notificationButtonRef}
aria-label="Notifications"
className="header-button"
onClick={(e) => {
e.stopPropagation();
setIsNotificationOpen(!isNotificationOpen);
}}
>
<NotificationIcon />
</button>
<button className="header-button" onClick={() => alert('Back clicked (TODO: implement)')}>
<BackIcon />
</button>
</div>
{/* Notification Popover */}
<div id="notification-popover-wrapper" className="notification-popover-wrapper">
<div
id="notification-popover"
ref={notificationPopoverRef}
className={`popover-content ${isNotificationOpen ? 'open' : ''}`}
>
<div className="notification-popover-text-content">
مدلهای هوش مصنوعی میتوانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از وارد کردن اطلاعات حساس بپرهیزید.
</div>
</div>
</div>
{/* Media Area (Video feed and Logos) */}
<div className="media-toggle-area">
<video
id="video-feed"
ref={videoRef}
autoPlay
playsInline
className={`video-feed ${!isCamActive ? 'hidden' : ''}`}
/>
{!isCamActive && isMicActive && ( // Show large logo if mic is on and cam is off
<div id="large-logo-container" className="large-logo-container">
<LogoDisplay isMini={false} isHumanActive={true} />
</div>
)}
{/* The small logo is part of the footer logic in ControlTray */}
</div>
</div>
{/* Footer Controls */}
<ControlTray
videoRef={videoRef} // Pass videoRef for video operations
onVideoStreamChange={handleVideoStreamChange}
// Pass setters for App to know mic/cam state for UI logic (e.g., logos)
setIsMicActive={setIsMicActive}
setIsCamActiveExternal={setIsCamActive} // Renamed to avoid conflict if ControlTray has its own isCamActive
isCamActiveApp={isCamActive} // Pass down App's view of cam state
/>
</main>
<IOSModal
isOpen={showIOSModal}
onClose={() => setShowIOSModal(false)}
/>
</div>
);
}
function App() {
return (
<LiveAPIProvider initialConfig={initialAppConfig}>
<AppContent />
</LiveAPIProvider>
);
}
export default App; |