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 } from "react"; | |
// import "./App.scss"; // اگر فایل App.scss جداگانه دارید | |
import { LiveAPIProvider } from "./contexts/LiveAPIContext"; | |
// SidePanel و Altair دیگر مستقیما اینجا استفاده نمیشوند، مگر اینکه بخواهید آنها را در جای دیگری از UI جدید قرار دهید. | |
// 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 cn from "classnames"; | |
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 (میتوانید اینها را به فایلهای جداگانه منتقل کنید و ایمپورت کنید اگر تمایل دارید) | |
const SvgHumanIcon = () => ( | |
<svg width="70" height="70" 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> | |
); | |
function App() { | |
const videoRef = useRef<HTMLVideoElement>(null); | |
const [videoStream, setVideoStream] = useState<MediaStream | null>(null); // این توسط ControlTray مدیریت میشود | |
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 notificationPopoverRef = useRef<HTMLDivElement>(null); | |
const notificationButtonRef = useRef<HTMLButtonElement>(null); | |
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]; | |
console.log("Parent Origin:", parentOrigin); | |
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 { | |
// اگر مستقیماً بارگذاری شده و نه در یک iframe از دامنه مجاز، آن را مسدود کنید | |
// مگر اینکه بخواهید برای تست محلی به آن اجازه دهید | |
if (process.env.NODE_ENV === 'development' && window.location.hostname === 'localhost') { | |
console.warn("App loaded directly in development. Allowed for local testing."); | |
setIsAllowedOrigin(true); | |
} else { | |
console.warn("App loaded directly, not in an iframe from allowed origin. Blocking."); | |
setIsAllowedOrigin(false); | |
} | |
} | |
} catch (e) { | |
console.error("Cross-origin access error, cannot verify parent. Blocking.", e); | |
setIsAllowedOrigin(false); | |
} | |
}, []); | |
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]); | |
if (isAllowedOrigin === null) { | |
return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>; | |
} | |
if (isAllowedOrigin === false) { | |
return <div style={{ padding: '20px', textAlign: 'center', color: 'red' }}>دسترسی غیرمجاز! اگر چت صوتی و تصویری برای شما باز نمیشود این لینک رو با مرورگر کروم باز کنید و همچنین مرورگر کروم رو به عنوان مرورگر پیشفرض گوشی خود قرار دهید تا هر بار زدن روی دکمه شروع داخل برنامه لینک با مرورگر کروم باز بشه، برای پیشفرض قرار دادن مرورگر کروم وارد تنظیمات گوشی خود شوید قسمت برنامه ها ، مدیریت برنامه ها رو کلیک کنید بالای صفحه روی سه نقطه بزنید و تنظیمات بیشتر رو انتخاب کنید بعدا وارد قسمت برنامه های پیش فرض شوید و مرورگر کروم رو به عنوان مرورگر پیشفرض خود قرار دهید، اگر مشکلی بود حتماً به پشتیبانی برنامه پیام بفرستید</div>; | |
} | |
// تابع برای ساخت لوگو (ساده شده) | |
const createLogoHTML = (isMini: boolean, isActive: boolean, type: 'human' | 'ai' = 'human') => { | |
if (!isActive) 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 } | |
: { ping: 40, outer: 0, mid: 20, inner: 50, icon: 65 }; | |
const bgColorBase = type === 'human' ? 'blue' : 'green'; | |
return ( | |
<div className="relative" style={{ width: `${size}px`, height: `${size}px` }}> | |
<div className={`absolute rounded-full opacity-50 animate-ping bg-${bgColorBase}-200`} style={{ inset: `${insetBase.ping}px` }}></div> | |
<div className={`absolute inset-0 rounded-full opacity-50 bg-${bgColorBase}-200`} style={{ inset: `${insetBase.outer}px` }}></div> | |
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-300`} style={{ inset: `${insetBase.mid}px` }}></div> | |
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-400`} style={{ inset: `${insetBase.inner}px` }}></div> | |
<div className="z-10 absolute" style={{ inset: `${insetBase.icon}px`, display: 'flex', alignItems: 'center', justifyContent: 'center' }}> | |
{type === 'human' && <SvgHumanIcon />} | |
{/* Add AI icon if needed */} | |
</div> | |
</div> | |
); | |
}; | |
return ( | |
<> | |
{/* Global Styles from your HTML example */} | |
<style jsx global>{` | |
:root { | |
--radius: 0.625rem; /* 10px */ | |
--radius-md: 0.5rem; /* 8px */ | |
--background: oklch(1 0 0); | |
--foreground: oklch(0.145 0 0); | |
--popover: oklch(1 0 0); | |
--popover-foreground: oklch(0.145 0 0); | |
--border: oklch(0.922 0 0); | |
} | |
.dark { | |
--background: oklch(0.145 0 0); | |
--foreground: oklch(0.985 0 0); | |
--popover: oklch(0.205 0 0); | |
--popover-foreground: oklch(0.985 0 0); | |
--border: oklch(1 0 0 / 10%); | |
} | |
/* Tailwind base layer directives are handled by Tailwind itself if setup via CDN/build */ | |
/* Notification Popover */ | |
.notification-popover-wrapper { | |
position: fixed; top: 1rem; left: 50%; | |
transform: translateX(-50%); z-index: 100; | |
width: calc(100% - 2rem); max-width: 28rem; | |
display: flex; justify-content: center; pointer-events: none; | |
} | |
.popover-content { | |
width: 100%; border-radius: var(--radius-md, 0.5rem); | |
border-width: 1px; border-color: var(--border); | |
background-color: var(--popover); color: var(--popover-foreground); | |
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1); | |
outline: none; transition: opacity 0.3s ease-out, transform 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55); | |
opacity: 0; transform: translateY(-100%) scale(0.9); pointer-events: none; | |
} | |
.popover-content.open { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; } | |
.notification-popover-text-content { | |
background-color: #eff6ff; /* bg-blue-50 */ | |
font-size: 0.875rem; line-height: 1.5rem; direction: rtl; | |
padding: 1rem; border-radius: var(--radius-md, 0.5rem); | |
color: oklch(0.145 0 0); | |
} | |
.dark .notification-popover-text-content { | |
background-color: oklch(0.25 0.05 230); color: oklch(0.95 0.01 230); | |
} | |
/* Header controls */ | |
.header-controls { | |
display: flex; padding: 1rem; justify-content: space-between; | |
align-items: center; width: 100%; position: absolute; top: 0; left: 0; z-index: 10; | |
} | |
.header-button { | |
display: flex; align-items: center; justify-content: center; | |
padding: 0.5rem; border-radius: var(--radius-lg, 0.625rem); | |
background-color: #e5e7eb; /* bg-gray-200 */ | |
cursor: pointer; transition: background-color 0.2s; | |
} | |
.header-button:hover { background-color: #d1d5db; /* bg-gray-300 */ } | |
.header-button svg { opacity: 0.7; stroke: #374151 /* gray-700 */; } | |
.dark .header-button { background-color: oklch(0.28 0 0); } | |
.dark .header-button:hover { background-color: oklch(0.35 0 0); } | |
.dark .header-button svg { opacity: 0.8; stroke: oklch(0.85 0 0); } | |
/* Footer and Control Buttons */ | |
.footer-controls { | |
width: 100%; display: flex; gap: 1rem; position: absolute; | |
bottom: 0; padding: 2rem 3rem; align-items: center; | |
} | |
.footer-controls.layout-default { justify-content: space-between; } | |
.footer-controls.layout-with-small-logo { justify-content: space-around; } | |
.control-button { | |
height: 80px; width: 80px; border-radius: 9999px; /* rounded-full */ | |
padding: 0; display: flex; align-items: center; justify-content: center; | |
border-width: 1px; border-color: var(--border); | |
box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06); | |
cursor: pointer; transition: transform 0.2s ease-out, box-shadow 0.2s ease-out; | |
} | |
.control-button:hover { | |
transform: scale(1.05); | |
box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05); | |
} | |
.cam-button-color { background-color: #E0ECFF; } | |
.mic-button-color { background-color: #fecdd3; } | |
.dark .cam-button-color { background-color: #223355; } | |
.dark .mic-button-color { background-color: #5C2129; } | |
/* Switch Camera Button */ | |
.switch-camera-button-container { | |
position: absolute; bottom: calc(100% + 0.65rem); left: 50%; | |
z-index: 5; opacity: 0; | |
transform: translateY(15px) scale(0.7) translateX(-50%); | |
pointer-events: none; | |
transition: opacity 0.35s cubic-bezier(0.68, -0.55, 0.27, 1.55), transform 0.35s cubic-bezier(0.68, -0.55, 0.27, 1.55); | |
transform-origin: center bottom; | |
} | |
.switch-camera-button-container.visible { | |
opacity: 1; transform: translateY(0) scale(1) translateX(-50%); pointer-events: auto; | |
} | |
.switch-camera-button-content { | |
width: 48px; height: 48px; background-color: var(--background); | |
border: 1px solid var(--border); border-radius: 9999px; | |
display: flex; align-items: center; justify-content: center; | |
box-shadow: 0 5px 10px rgba(0,0,0,0.12), 0 2px 4px rgba(0,0,0,0.08); | |
cursor: pointer; transform-origin: center; | |
transition: transform 0.2s ease-out, box-shadow 0.2s ease-out; | |
} | |
.switch-camera-button-content:hover { | |
transform: scale(1.12) rotate(-6deg); | |
box-shadow: 0 7px 15px rgba(0,0,0,0.18), 0 3px 6px rgba(0,0,0,0.12); | |
} | |
.switch-camera-button-content:active { transform: scale(1.03) rotate(0deg); } | |
.switch-camera-button-content svg { | |
width: 22px; height: 22px; stroke: var(--foreground); | |
transition: transform 0.3s ease-in-out; | |
} | |
.switch-camera-button-content:hover svg { transform: rotate(360deg); } | |
/* Keyframes for popover (Tailwind keyframes should be in tailwind.config.js or added via <style>) */ | |
@keyframes popover-drop-in { | |
'0%': { opacity: '0', transform: 'translateY(-100%) scale(0.9)' }, | |
'70%': { opacity: '1', transform: 'translateY(5px) scale(1.02)' }, | |
'100%': { opacity: '1', transform: 'translateY(0) scale(1)' }, | |
} | |
@keyframes popover-lift-out { | |
'0%': { opacity: '1', transform: 'translateY(0) scale(1)' }, | |
'100%': { opacity: '0', transform: 'translateY(-100%) scale(0.9)' }, | |
} | |
.animate-popover-open-top-center { animation: popover-drop-in 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55) forwards; } | |
.animate-popover-close-top-center { animation: popover-lift-out 0.3s ease-in forwards; } | |
/* Forcing Tailwind specific classes for logo bg as an example if not working via template literals in JS */ | |
.bg-blue-200 { background-color: #bfdbfe !important; } | |
.bg-blue-300 { background-color: #93c5fd !important; } | |
.bg-blue-400 { background-color: #60a5fa !important; } | |
.bg-green-200 { background-color: #bbf7d0 !important; } | |
.bg-green-300 { background-color: #86efac !important; } | |
.bg-green-400 { background-color: #4ade80 !important; } | |
`}</style> | |
<LiveAPIProvider initialConfig={initialAppConfig}> | |
<div className="w-full flex flex-col items-center justify-center min-h-[90dvh] md:min-h-screen bg-background text-foreground antialiased"> | |
<div className="max-w-3xl w-full flex flex-col items-center justify-center h-full relative"> | |
{/* Header */} | |
<div className="header-controls"> | |
<button | |
ref={notificationButtonRef} | |
id="notification-button" | |
aria-label="Notifications" | |
className="header-button" | |
onClick={() => 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 className="header-button" onClick={() => alert('Back clicked (implement navigation)')}> | |
<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> | |
</div> | |
</div> | |
{/* Notification Popover */} | |
<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> | |
{/* Main Media Area */} | |
<div className="w-full flex flex-col items-center justify-center h-[90dvh] bg-background top-0 left-0 relative"> | |
{/* Video Feed (controlled by ControlTray's activeVideoStream) */} | |
<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, // isCamActive از ControlTray بروزرسانی میشود | |
})} | |
/> | |
{/* Large Logo Container */} | |
<div id="large-logo-container" className={cn("items-center justify-center w-full h-full absolute top-0 left-0", { | |
"flex": !isCamActive && isMicActive, // Show when mic is on and cam is off | |
"hidden": isCamActive || !isMicActive | |
})}> | |
{createLogoHTML(false, !isCamActive && isMicActive)} | |
</div> | |
{/* Altair Chat component - شما تصمیم میگیرید کجا و چگونه نمایش داده شود */} | |
{/* <div className="absolute bottom-24 left-4 z-20 w-1/3 max-w-md"> <Altair /> </div> */} | |
{/* <SidePanel /> */} | |
{/* Footer Controls - Passed to ControlTray */} | |
<ControlTray | |
videoRef={videoRef} | |
supportsVideo={true} // شما این را true گذاشته بودید | |
onVideoStreamChange={setVideoStream} // ControlTray این را برای بروزرسانی استریم ویدیو استفاده میکند | |
// Props جدید برای کنترل ظاهر از App.tsx | |
isAppMicActive={isMicActive} | |
onAppMicToggle={setIsMicActive} | |
isAppCamActive={isCamActive} | |
onAppCamToggle={setIsCamActive} | |
createLogoFunction={createLogoHTML} // تابع ساخت لوگو به ControlTray پاس داده میشود | |
/> | |
</div> | |
</div> | |
</div> | |
<IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} /> | |
</LiveAPIProvider> | |
</> | |
); | |
} | |
export default App; |