Spaces:
Running
Running
/** | |
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"; | |
import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext"; | |
// import SidePanel from "./components/side-panel/SidePanel"; // اگر استفاده نمیکنید کامنت کنید | |
import { Altair } from "./components/altair/Altair"; | |
import ControlTray from "./components/control-tray/ControlTray"; | |
import { IOSModal } from "./components/ios-modal/IOSModal"; | |
import { isIOS } from "./lib/platform"; | |
import { LiveConfig } from "./multimodal-live-types"; | |
// --- 👇 دامنه **ممنوع** شما 👇 --- | |
const FORBIDDEN_ORIGIN = "https://www.aisada.ir"; // یا "http://www.aisada.ir"; | |
// --- 👆 --- | |
// --- 👇 دستورالعمل شخصیسازی شما (بدون تغییر) 👇 --- | |
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 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; | |
const size = isMini ? 80 : 200; | |
const iconSize = isMini ? 35 : 70; | |
const insetBase = isMini | |
? { ping: 10, outer: 0, mid: 5, inner: 12, icon: 22.5 } | |
: { ping: 40, outer: 0, mid: 20, inner: 50, icon: 65 }; | |
const bgColorBase = 'blue'; | |
return ( | |
<div className="relative" style={{ width: `${size}px`, height: `${size}px` }}> | |
<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 [showIOSModal, setShowIOSModal] = useState(false); | |
const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null); // true: allowed, false: blocked | |
const [isNotificationOpen, setIsNotificationOpen] = useState(false); | |
const notificationPopoverRef = useRef<HTMLDivElement>(null); | |
const notificationButtonRef = useRef<HTMLButtonElement>(null); | |
const [isMicActive, setIsMicActive] = useState(false); | |
const [isCamActive, setIsCamActive] = useState(false); | |
const { connected, volume } = useLiveAPIContext(); | |
useEffect(() => { | |
if (isIOS()) { | |
setShowIOSModal(true); | |
} | |
// --- 👇 منطق بررسی دامنه مجاز (معکوس شده) 👇 --- | |
try { | |
if (window.self !== window.top) { // App is in an iframe | |
if (window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0) { | |
const parentOrigin = window.location.ancestorOrigins[0]; | |
console.log("Parent Origin:", parentOrigin); | |
if (parentOrigin === FORBIDDEN_ORIGIN) { | |
console.warn(`Blocked load from forbidden origin: ${parentOrigin}`); | |
setIsAllowedOrigin(false); // Block if from forbidden origin | |
} else { | |
console.log(`Allowed load from origin: ${parentOrigin}`); | |
setIsAllowedOrigin(true); // Allow if from any other origin | |
} | |
} else { | |
// Cannot verify parent origin, but it's in an iframe. | |
// For "allow everywhere except specific site", we can assume allow here. | |
console.warn("Cannot verify parent origin (ancestorOrigins not available/empty), but in iframe. Allowing."); | |
setIsAllowedOrigin(true); | |
} | |
} else { // App loaded directly, not in an iframe | |
console.log("App loaded directly. Allowing."); | |
setIsAllowedOrigin(true); // Allow if loaded directly | |
} | |
} catch (e) { | |
// Cross-origin access error. This usually happens if the iframe has sandbox attributes | |
// that prevent accessing ancestorOrigins. In this "allow everywhere except" scenario, | |
// it's safer to allow if we can't determine, or block if you prefer higher security. | |
// Let's allow for wider compatibility, assuming it's not the forbidden one. | |
console.error("Cross-origin access error, cannot verify parent. Allowing by default.", e); | |
setIsAllowedOrigin(true); | |
} | |
// --- 👆 --- | |
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) => { | |
setIsCamActive(!!stream); | |
if (videoRef.current) { | |
videoRef.current.srcObject = stream; | |
if (stream) { | |
videoRef.current.play().catch(e => console.warn("Video play failed:", e)); | |
} | |
} | |
}, []); | |
useEffect(() => { | |
// This effect is for potential output volume visualization, not currently used for mic pulse | |
}, [volume]); | |
if (isAllowedOrigin === null) { | |
return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>; | |
} | |
// --- 👇 رندر شرطی با پیام خطای جدید 👇 --- | |
if (isAllowedOrigin === false) { | |
// این پیام زمانی نمایش داده میشود که برنامه در iframe سایت ممنوعه (aisada.ir) بارگذاری شود. | |
return ( | |
<div style={{ padding: '20px', textAlign: 'center', color: 'red', direction: 'rtl' }}> | |
دسترسی به این محتوا از دامنه شما ({FORBIDDEN_ORIGIN}) مجاز نمیباشد. | |
<br /> | |
لطفا این اسپیس را مستقیما یا از طریق دامنههای مجاز دیگر باز کنید. | |
</div> | |
); | |
} | |
// --- 👆 --- | |
// اگر isAllowedOrigin === true بود، برنامه اصلی رندر میشود | |
return ( | |
<div className="App"> | |
{/* <SidePanel /> */} | |
<main> | |
<div className="main-app-area"> | |
<Altair /> | |
<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> | |
<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> | |
<div className="media-toggle-area"> | |
<video | |
id="video-feed" | |
ref={videoRef} | |
autoPlay | |
playsInline | |
className={`video-feed ${!isCamActive ? 'hidden' : ''}`} | |
/> | |
{!isCamActive && isMicActive && ( | |
<div id="large-logo-container" className="large-logo-container"> | |
<LogoDisplay isMini={false} isHumanActive={true} /> | |
</div> | |
)} | |
</div> | |
</div> | |
<ControlTray | |
videoRef={videoRef} | |
onVideoStreamChange={handleVideoStreamChange} | |
setIsMicActive={setIsMicActive} | |
setIsCamActiveExternal={setIsCamActive} | |
isCamActiveApp={isCamActive} | |
/> | |
</main> | |
<IOSModal | |
isOpen={showIOSModal} | |
onClose={() => setShowIOSModal(false)} | |
/> | |
</div> | |
); | |
} | |
function App() { | |
return ( | |
<LiveAPIProvider initialConfig={initialAppConfig}> | |
<AppContent /> | |
</LiveAPIProvider> | |
); | |
} | |
export default App; |