mirror of
https://github.com/Anil-matcha/Open-Generative-AI.git
synced 2026-05-07 01:17:18 +00:00
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
"use client";
|
|
|
|
import { CreateAgentPage } from "ai-agent";
|
|
import "ai-agent/dist/tailwind.css";
|
|
import { useCallback, useEffect, useRef } from "react";
|
|
import axios from "axios";
|
|
|
|
const STORAGE_KEY = "muapi_key";
|
|
|
|
export default function AgentCreateClient({ userData }) {
|
|
const interceptorRef = useRef(null);
|
|
|
|
useEffect(() => {
|
|
const getKey = () => {
|
|
if (typeof window === "undefined") return null;
|
|
const fromStorage = localStorage.getItem(STORAGE_KEY);
|
|
if (fromStorage) return fromStorage;
|
|
const match = document.cookie.match(/muapi_key=([^;]+)/);
|
|
return match ? match[1] : null;
|
|
};
|
|
|
|
const apiKey = getKey();
|
|
if (!apiKey) return;
|
|
|
|
interceptorRef.current = axios.interceptors.request.use((config) => {
|
|
const isRelative = config.url.startsWith("/") || !config.url.startsWith("http");
|
|
const isInternalProxy = config.url.includes('/api/app') || config.url.includes('/api/workflow') || config.url.includes('/api/agents') || config.url.includes('/api/api') || config.url.includes('/api/v1');
|
|
|
|
if (isRelative || isInternalProxy) {
|
|
config.headers["x-api-key"] = apiKey;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
return () => {
|
|
if (interceptorRef.current !== null) {
|
|
axios.interceptors.request.eject(interceptorRef.current);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
const useUser = useCallback(
|
|
() => ({
|
|
user: {
|
|
username: userData?.email?.split("@")[0] || "Studio User",
|
|
name: userData?.email?.split("@")[0] || "Studio User",
|
|
email: userData?.email || null,
|
|
profile_photo: null,
|
|
balance: userData?.balance || 0,
|
|
},
|
|
isAuthorized: !!userData,
|
|
}),
|
|
[userData]
|
|
);
|
|
|
|
return (
|
|
<CreateAgentPage
|
|
useUser={useUser}
|
|
usedIn="studio"
|
|
/>
|
|
);
|
|
}
|