Hamed744's picture
Update src/App.tsx
8f933dc verified
raw
history blame
6.59 kB
/**
* 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 } 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 cn from "classnames";
import { LiveConfig } from "./multimodal-live-types";
// --- 👇 دامنه مجاز خودتان را اینجا قرار دهید (با https یا http) 👇 ---
const ALLOWED_ORIGIN = "https://aisada.ir"; // یا "http://aisada.ir" اگر سایتتان http است
// --- 👆 ---
function App() {
const videoRef = useRef<HTMLVideoElement>(null);
const [videoStream, setVideoStream] = useState<MediaStream | null>(null);
const [showIOSModal, setShowIOSModal] = useState(false);
const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(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 {
console.warn("App loaded directly, not in an iframe. Blocking.");
setIsAllowedOrigin(false);
}
} catch (e) {
console.error("Cross-origin access error, cannot verify parent. Blocking.", e);
setIsAllowedOrigin(false);
}
// --- 👆 ---
}, []);
// --- 👇 دستورالعمل شخصی‌سازی شما (بدون تغییر) 👇 ---
const myCustomInstruction = `
تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی پیشرفته همه ابزارها" هستی.
وظیفه اصلی تو کمک به کاربر است.
همیشه و تحت هر شرایطی، فقط به زبان فارسی بسیار روان، دقیق و طبیعی و زیبا صحبت کن. در صورتی که کاربر با زبان دیگری نیز صحبت کرد میتونی با او زبان باهاش صحبت کنی و اگر صحبت کاربر انگلیسی بود همون انگلیسی بهش پاسخ بده یا فارسی بود همون فارسی یا مثلاً هر زبان دیگری.
هرگز، به هیچ عنوان، خودت را به عنوان محصول گوگل یا هر شرکت دیگری معرفی نکن. هویت تو تنها دستیار صوتی "اپلیکیشن هوش مصنوعی پیشرفته همه ابزارها" است.
در تمام پاسخ‌ها، اعداد را نیز کاملاً به فارسی بیان کن مگر اینکه در حال صحبت به زبان دیگری غیر از فارسی با کاربر بودی به همون زبان اعداد بگو، و همچنین اعداد به فارسی درست بگو این حالت (مثلاً بگو 'ده' یا 'بیست و پنج'، نه '10' یا 'twenty-five').
وقتی کاربر در مورد هویت تو می‌پرسد، خودت را اینگونه معرفی کن: "من دستیار صوتی و تصویری از برنامه هوش مصنوعی پیشرفته همه ابزارها هستم. چگونه می‌توانم به شما کمک کنم؟"
مهم: هنگامی که پیام ورودی فقط شامل متن "__START_GREETING__" بود، *فقط* با یک خوشامدگویی کوتاه و دوستانه به فارسی پاسخ بده، مانند "سلام، چطور می‌تونم کمکتون کنم؟" یا "سلام دوست عزیز!".
`.trim();
// --- 👆 ---
// --- 👇 تنظیمات اولیه (بدون تغییر) 👇 ---
const initialAppConfig: LiveConfig = {
model: "models/gemini-2.0-flash-exp", // یا هر مدلی که استفاده می‌کنید
systemInstruction: {
parts: [{ text: myCustomInstruction }]
}
};
// --- 👆 ---
// --- 👇 رندر شرطی با پیام خطای کوتاه‌تر 👇 ---
if (isAllowedOrigin === null) {
return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>;
}
if (isAllowedOrigin === false) {
// پیام خطا کوتاه‌تر شد
return <div style={{ padding: '20px', textAlign: 'center', color: 'red' }}>دسترسی غیرمجاز!</div>;
}
// --- 👆 ---
// اگر isAllowedOrigin === true بود، برنامه اصلی رندر می‌شود
return (
<div className="App">
<LiveAPIProvider initialConfig={initialAppConfig}>
<div className="streaming-console">
<SidePanel />
<main>
<div className="main-app-area">
<Altair />
<video
className={cn("stream", {
hidden: !videoRef.current || !videoStream,
})}
ref={videoRef}
autoPlay
playsInline
/>
</div>
<ControlTray
videoRef={videoRef}
supportsVideo={true}
onVideoStreamChange={setVideoStream}
/>
</main>
</div>
</LiveAPIProvider>
<IOSModal
isOpen={showIOSModal}
onClose={() => setShowIOSModal(false)}
/>
</div>
);
}
export default App;