Unrestricted Open-source alternative to AI video platforms — Free AI image & video generation studio with 600+ models (Flux, Midjourney, Kling, Sora, Veo). No content filters. Self-hosted, MIT licensed.
JavaScript ai-art-generatorai-image-generationai-video-generationfal-ai-alternativegenerative-aiimage-to-videoinvideo-alternativekling-ailipsynclitellm-alternativemidjourney-alternativemuapiopen-sourceopenrouter-alternativeseedance2sora-alternativetext-to-imagetext-to-videouncensoredwan-video
Fix white-label deep links for Agents and Workflows tabs
Anil-matcha01f0a76a313d
diff --git a/packages/studio/src/components/AgentStudio.jsx b/packages/studio/src/components/AgentStudio.jsxindex 67684ebd..d7673222 100644--- a/packages/studio/src/components/AgentStudio.jsx+++ b/packages/studio/src/components/AgentStudio.jsx@@ -1,11 +1,18 @@"use client";import { useState, useEffect, useCallback } from "react";-import { useRouter } from "next/navigation";+import { useParams, useRouter } from "next/navigation";+import ReactMarkdown from "react-markdown";+import remarkGfm from "remark-gfm";import {getTemplateAgents,getUserAgents,getUserConversations,+ getAgentBySlug,+ getAgentConversation,+ sendAgentChatMessage,+ pollAgentChatResult,+ createAgent,} from "../muapi.js";// ─── Helpers ────────────────────────────────────────────────────────────────@@ -114,11 +121,47 @@ function ConversationCard({ conv, onClick }) {);}+// Conversation history entries can carry `content` as a plain string or as a+// list of structured blocks (matches the shape agent_router.py's own last-message+// preview extraction already assumes for the "My Chats" list).+function textFromContent(content) {+ if (typeof content === "string") return content;+ if (Array.isArray(content)) {+ return content.map((b) => (typeof b === "string" ? b : b?.text || "")).join("\n");+ }+ return "";+}++function ChatBubble({ message }) {+ const isUser = message.role === "user";+ const text = textFromContent(message.content);+ return (+ <div className={`flex ${isUser ? "justify-end" : "justify-start"}`}>+ <div+ className={`max-w-[75%] rounded-2xl px-4 py-3 text-sm leading-relaxed ${+ isUser+ ? "bg-[#22d3ee] text-black font-medium"+ : "bg-white/[0.04] border border-white/5 text-white/90"+ }`}+ >+ {isUser ? (+ <span className="whitespace-pre-wrap">{text}</span>+ ) : (+ <div className="prose prose-invert prose-sm max-w-none prose-p:my-2 prose-pre:bg-black/40">+ <ReactMarkdown remarkPlugins={[remarkGfm]}>{text || "…"}</ReactMarkdown>+ </div>+ )}+ </div>+ </div>+ );+}+// ─── Main Component ──────────────────────────────────────────────────────────const TABS = ["templates", "my-agents", "my-chats"];export default function AgentStudio({ apiKey }) {const router = useRouter();+ const params = useParams();const [activeMainTab, setActiveMainTab] = useState("templates");const [agents, setAgents] = useState([]);@@ -126,7 +169,32 @@ export default function AgentStudio({ apiKey }) {const [loading, setLoading] = useState(true);const [error, setError] = useState(null);+ // Deep-link routing. `router.push('/agents/...')` below is a real full navigation+ // away from the studio shell on muapi.ai (BYOK/dashboard) — AiAgent's own standalone+ // pages handle it there, unaffected. On a white-label custom domain the same push is+ // transparently rewritten by middleware.js back to this same catch-all shell route, so+ // reading it back out via useParams() here drives an inline view instead of navigating+ // anywhere — see docs/whitelabel_plan.md for the full routing writeup.+ const tabSegments = Array.isArray(params?.tab) ? params.tab : [];+ const agentsIdx = tabSegments.indexOf("agents");+ const urlAgentSlug = agentsIdx === -1 ? null : tabSegments[agentsIdx + 1] || null;+ const urlConversationId = agentsIdx === -1 ? null : tabSegments[agentsIdx + 2] || null;++ const [view, setView] = useState("list"); // 'list' | 'chat' | 'create'+ const [activeAgent, setActiveAgent] = useState(null);+ const [conversationId, setConversationId] = useState(null);+ const [chatMessages, setChatMessages] = useState([]);+ const [chatInput, setChatInput] = useState("");+ const [chatLoading, setChatLoading] = useState(false);+ const [sending, setSending] = useState(false);+ const [chatError, setChatError] = useState(null);++ const [createForm, setCreateForm] = useState({ name: "", description: "", system_prompt: "", welcome_message: "" });+ const [creating, setCreating] = useState(false);+ const [createError, setCreateError] = useState(null);+// Navigate to the standalone /agents page — AiAgent handles its own routing there+ // (on a custom domain, this instead drives the inline view below via urlAgentSlug)const handleSelectAgent = useCallback((agent) => {const id = agent.agent_id || agent.id;@@ -154,8 +222,97 @@ export default function AgentStudio({ apiKey }) {[router]);+ // Resolve the inline view from the URL. 'edit' isn't built inline yet (only used+ // from the standalone dashboard today) — treat it as "back to list" rather than+ // trying to load an agent named "edit" and showing a confusing error.useEffect(() => {if (!apiKey) return;+ if (!urlAgentSlug || urlAgentSlug === "edit") {+ setView("list");+ return;+ }+ if (urlAgentSlug === "create") {+ setView("create");+ return;+ }++ let cancelled = false;+ async function openFromUrl() {+ setChatLoading(true);+ setChatError(null);+ try {+ const agent = await getAgentBySlug(apiKey, urlAgentSlug);+ if (cancelled) return;+ setActiveAgent(agent);+ if (urlConversationId) {+ const conv = await getAgentConversation(apiKey, urlAgentSlug, urlConversationId);+ if (cancelled) return;+ setConversationId(conv.id);+ setChatMessages(conv.history || []);+ } else {+ setConversationId(null);+ setChatMessages([]);+ }+ setView("chat");+ } catch (err) {+ if (!cancelled) setChatError(err.message || "Failed to load agent");+ } finally {+ if (!cancelled) setChatLoading(false);+ }+ }+ openFromUrl();+ return () => { cancelled = true; };+ }, [apiKey, urlAgentSlug, urlConversationId]);++ const handleSendMessage = useCallback(async () => {+ if (!chatInput.trim() || sending || !activeAgent) return;+ const text = chatInput.trim();+ setChatInput("");+ setChatMessages((prev) => [...prev, { role: "user", content: text }]);+ setSending(true);+ setChatError(null);+ try {+ const agentSlug = activeAgent.agent_id;+ const { request_id } = await sendAgentChatMessage(apiKey, agentSlug, {+ message: text,+ conversationId,+ });+ const result = await pollAgentChatResult(apiKey, request_id);+ setConversationId(result.conversation_id);+ setChatMessages(result.messages || []);+ } catch (err) {+ setChatError(err.message || "Failed to send message");+ } finally {+ setSending(false);+ }+ }, [apiKey, activeAgent, conversationId, chatInput, sending]);++ const handleCreateSubmit = useCallback(+ async (e) => {+ e.preventDefault();+ if (!createForm.name.trim() || !createForm.system_prompt.trim() || creating) return;+ setCreating(true);+ setCreateError(null);+ try {+ const created = await createAgent(apiKey, {+ name: createForm.name.trim(),+ description: createForm.description.trim() || null,+ system_prompt: createForm.system_prompt.trim(),+ welcome_message: createForm.welcome_message.trim() || null,+ skill_ids: [],+ });+ router.push(`/agents/${created.agent_id}`);+ } catch (err) {+ setCreateError(err.message || "Failed to create agent");+ } finally {+ setCreating(false);+ }+ },+ [apiKey, createForm, creating, router]+ );++ useEffect(() => {+ if (!apiKey || view !== "list") return;let cancelled = false;async function load() {@@ -184,9 +341,182 @@ export default function AgentStudio({ apiKey }) {load();return () => { cancelled = true; };- }, [apiKey, activeMainTab]);+ }, [apiKey, activeMainTab, view]);++ // ── Render: Create ───────────────────────────────────────────────────────────+ if (view === "create") {+ return (+ <div className="h-full flex flex-col bg-[#030303] text-white overflow-y-auto custom-scrollbar">+ <div className="flex-shrink-0 h-16 border-b border-white/5 flex items-center gap-6 px-8 bg-black/40">+ <button+ onClick={() => router.push("/agents")}+ className="flex items-center gap-2 text-xs font-bold text-white/50 hover:text-white transition-colors"+ type="button"+ >+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">+ <path d="M19 12H5M12 19l-7-7 7-7" />+ </svg>+ Agents+ </button>+ <h2 className="text-sm font-black uppercase tracking-[0.2em] text-[#22d3ee]">Create Agent</h2>+ </div>++ <form onSubmit={handleCreateSubmit} className="max-w-2xl w-full mx-auto p-8 space-y-6">+ <div className="space-y-2">+ <label className="block text-[10px] font-black text-white/40 uppercase tracking-widest">Name</label>+ <input+ type="text"+ required+ value={createForm.name}+ onChange={(e) => setCreateForm((f) => ({ ...f, name: e.target.value }))}+ placeholder="e.g. Caption Crafter Pro"+ className="w-full bg-white/5 border border-white/10 rounded-lg p-3 text-sm text-white focus:outline-none focus:border-[#22d3ee]/50 transition-colors"+ />+ </div>++ <div className="space-y-2">+ <label className="block text-[10px] font-black text-white/40 uppercase tracking-widest">Description</label>+ <input+ type="text"+ value={createForm.description}+ onChange={(e) => setCreateForm((f) => ({ ...f, description: e.target.value }))}+ placeholder="What does this agent help with?"+ className="w-full bg-white/5 border border-white/10 rounded-lg p-3 text-sm text-white focus:outline-none focus:border-[#22d3ee]/50 transition-colors"+ />+ </div>++ <div className="space-y-2">+ <label className="block text-[10px] font-black text-white/40 uppercase tracking-widest">System Prompt</label>+ <textarea+ required+ value={createForm.system_prompt}+ onChange={(e) => setCreateForm((f) => ({ ...f, system_prompt: e.target.value }))}+ placeholder="You are a helpful assistant that..."+ className="w-full bg-white/5 border border-white/10 rounded-lg p-3 text-sm text-white focus:outline-none focus:border-[#22d3ee]/50 transition-colors min-h-[140px] resize-none"+ />+ </div>++ <div className="space-y-2">+ <label className="block text-[10px] font-black text-white/40 uppercase tracking-widest">Welcome Message (optional)</label>+ <input+ type="text"+ value={createForm.welcome_message}+ onChange={(e) => setCreateForm((f) => ({ ...f, welcome_message: e.target.value }))}+ placeholder="Hi! How can I help you today?"+ className="w-full bg-white/5 border border-white/10 rounded-lg p-3 text-sm text-white focus:outline-none focus:border-[#22d3ee]/50 transition-colors"+ />+ </div>++ {createError && (+ <p className="text-xs font-bold text-red-400">{createError}</p>+ )}++ <button+ type="submit"+ disabled={creating || !createForm.name.trim() || !createForm.system_prompt.trim()}+ className="w-full py-4 bg-[#22d3ee] text-black text-xs font-black uppercase tracking-[0.2em] rounded-xl hover:bg-white transition-all disabled:opacity-40 flex items-center justify-center gap-3"+ >+ {creating ? (+ <>+ <div className="w-4 h-4 border-2 border-black/20 border-t-black rounded-full animate-spin" />+ <span>Creating...</span>+ </>+ ) : (+ <span>Create Agent</span>+ )}+ </button>+ </form>+ </div>+ );+ }++ // ── Render: Chat ─────────────────────────────────────────────────────────────+ if (view === "chat") {+ return (+ <div className="h-full flex flex-col bg-[#030303] text-white">+ <div className="flex-shrink-0 h-16 border-b border-white/5 flex items-center gap-4 px-8 bg-black/40">+ <button+ onClick={() => router.push("/agents")}+ className="flex items-center gap-2 text-xs font-bold text-white/50 hover:text-white transition-colors"+ type="button"+ >+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">+ <path d="M19 12H5M12 19l-7-7 7-7" />+ </svg>+ Agents+ </button>+ <div className="h-4 w-[1px] bg-white/10" />+ {activeAgent && (+ <div className="flex items-center gap-3">+ <div className="w-8 h-8 rounded-lg overflow-hidden bg-white/5 border border-white/5 shrink-0">+ {activeAgent.icon_url ? (+ <img src={activeAgent.icon_url} alt={activeAgent.name} className="w-full h-full object-cover" />+ ) : null}+ </div>+ <span className="text-sm font-bold text-white">{activeAgent.name}</span>+ </div>+ )}+ </div>++ <div className="flex-1 overflow-y-auto custom-scrollbar p-8 space-y-4 max-w-3xl w-full mx-auto">+ {chatLoading ? (+ <div className="h-full flex items-center justify-center">+ <div className="w-10 h-10 border-2 border-white/5 border-t-[#22d3ee] rounded-full animate-spin" />+ </div>+ ) : (+ <>+ {chatMessages.length === 0 && activeAgent?.welcome_message && (+ <ChatBubble message={{ role: "assistant", content: activeAgent.welcome_message }} />+ )}+ {chatMessages.map((msg, i) => (+ <ChatBubble key={i} message={msg} />+ ))}+ {sending && (+ <div className="flex justify-start">+ <div className="bg-white/[0.04] border border-white/5 rounded-2xl px-4 py-3">+ <div className="w-4 h-4 border-2 border-white/10 border-t-[#22d3ee] rounded-full animate-spin" />+ </div>+ </div>+ )}+ {chatError && (+ <p className="text-xs font-bold text-red-400">{chatError}</p>+ )}+ </>+ )}+ </div>++ <div className="flex-shrink-0 border-t border-white/5 p-6 bg-black/40">+ <form+ onSubmit={(e) => { e.preventDefault(); handleSendMessage(); }}+ className="max-w-3xl w-full mx-auto flex items-end gap-3"+ >+ <textarea+ value={chatInput}+ onChange={(e) => setChatInput(e.target.value)}+ onKeyDown={(e) => {+ if (e.key === "Enter" && !e.shiftKey) {+ e.preventDefault();+ handleSendMessage();+ }+ }}+ placeholder="Message this agent..."+ rows={1}+ className="flex-1 bg-white/5 border border-white/10 rounded-xl p-3 text-sm text-white focus:outline-none focus:border-[#22d3ee]/50 transition-colors resize-none max-h-40"+ />+ <button+ type="submit"+ disabled={!chatInput.trim() || sending}+ className="px-5 py-3 bg-[#22d3ee] text-black text-xs font-black uppercase tracking-widest rounded-xl hover:bg-white transition-all disabled:opacity-40"+ >+ Send+ </button>+ </form>+ </div>+ </div>+ );+ }- // ── Render ──────────────────────────────────────────────────────────────────+ // ── Render: List ──────────────────────────────────────────────────────────────return (<div className="h-full flex flex-col bg-[#030303] text-white">{/* Header */}diff --git a/packages/studio/src/components/WorkflowStudio.jsx b/packages/studio/src/components/WorkflowStudio.jsxindex 82917fb7..ac294ef6 100644--- a/packages/studio/src/components/WorkflowStudio.jsx+++ b/packages/studio/src/components/WorkflowStudio.jsx@@ -136,24 +136,29 @@ export default function WorkflowStudio({}) {const params = useParams();const router = useRouter();- const slug = params?.slug || [];const idFromParams = params?.id; // exists on /workflow/[id]/[tab] route- const tabFromParams = params?.tab; // exists on /workflow/[id]/[tab] route-+ const tabFromParams = params?.tab; // string on /workflow/[id]/[tab]; array on the [[...tab]] catch-all+ // Catch-all routes (/studio/[brandSlug]/[[...tab]], /open-generative-ai/[[...tab]]) expose the+ // whole remaining path as params.tab (an array) — NOT params.slug, which doesn't exist on either+ // route and previously made this whole fallback branch permanently dead.+ const catchAllSegments = Array.isArray(tabFromParams) ? tabFromParams : [];+// Robustly extract ID and Tab from either route structureconst getWorkflowInfo = useCallback(() => {- // Priority 1: Dedicated /workflow/[id]/[tab] route+ // Priority 1: Dedicated /workflow/[id]/[tab] routeif (idFromParams) {return { id: idFromParams, tab: tabFromParams || null };}- // Priority 2: Catch-all /studio/[[...slug]] route- const wfIndex = slug.findIndex(s => s === 'workflows' || s === 'workflow');+ // Priority 2: Catch-all studio shell route — also recognizes the singular+ // "workflow" segment used by the standalone /workflow/[id]/[tab] URL scheme,+ // since a white-label custom domain's middleware rewrite lands here too.+ const wfIndex = catchAllSegments.findIndex(s => s === 'workflows' || s === 'workflow');if (wfIndex === -1) return { id: null, tab: null };return {- id: slug[wfIndex + 1] || null,- tab: slug[wfIndex + 2] || null+ id: catchAllSegments[wfIndex + 1] || null,+ tab: catchAllSegments[wfIndex + 2] || null};- }, [slug, idFromParams, tabFromParams]);+ }, [catchAllSegments, idFromParams, tabFromParams]);const { id: urlWorkflowId, tab: urlTab } = getWorkflowInfo();diff --git a/packages/studio/src/muapi.js b/packages/studio/src/muapi.jsindex 59b3e64f..fb4d30aa 100644--- a/packages/studio/src/muapi.js+++ b/packages/studio/src/muapi.js@@ -397,6 +397,109 @@ export async function getUserConversations(apiKey) {return Array.isArray(data) ? data : [];};+// GET /agents/by-slug/{slug} — public agent details (works unauthenticated for+// published/template agents; x-api-key is sent for consistency but not required).+export async function getAgentBySlug(apiKey, slug) {+ const response = await fetch(`${BASE_URL}/agents/by-slug/${slug}`, {+ headers: {+ 'Content-Type': 'application/json',+ 'x-api-key': apiKey+ }+ });+ if (!response.ok) {+ const errText = await response.text();+ throw new Error(`Failed to fetch agent: ${response.status} - ${errText.slice(0, 100)}`);+ }+ return await response.json();+}++// GET /agents/by-slug/{slug}/{conversationId} — chat history for one conversation.+export async function getAgentConversation(apiKey, agentSlug, conversationId) {+ const response = await fetch(`${BASE_URL}/agents/by-slug/${agentSlug}/${conversationId}`, {+ headers: {+ 'Content-Type': 'application/json',+ 'x-api-key': apiKey+ }+ });+ if (!response.ok) {+ const errText = await response.text();+ throw new Error(`Failed to fetch conversation: ${response.status} - ${errText.slice(0, 100)}`);+ }+ return await response.json();+}++// POST /agents/by-slug/{slug}/chat — send a message, returns {request_id, status}+// to poll via pollAgentChatResult.+export async function sendAgentChatMessage(apiKey, agentSlug, { message, conversationId, attachments } = {}) {+ const response = await fetch(`${BASE_URL}/agents/by-slug/${agentSlug}/chat`, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'x-api-key': apiKey+ },+ body: JSON.stringify({+ message,+ conversation_id: conversationId || null,+ attachments: attachments || null,+ stream: false+ })+ });+ if (!response.ok) {+ const errText = await response.text();+ throw new Error(`Failed to send message: ${response.status} - ${errText.slice(0, 100)}`);+ }+ return await response.json();+}++// Polls /api/v1/predictions/{requestId}/result until the agent turn completes.+// Unlike submitAndPoll's generic media polling, a completed agent-chat result is+// the full {conversation_id, messages, is_complete, suggestions} envelope, not a+// media URL — while processing, the endpoint doesn't surface intermediate status+// text (get_result_url_from_output only returns output_data once COMPLETED), so+// this just waits until is_complete rather than showing incremental progress.+export async function pollAgentChatResult(apiKey, requestId, { maxAttempts = 150, interval = 2000 } = {}) {+ const url = `${BASE_URL}/api/v1/predictions/${requestId}/result`;+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {+ await new Promise(resolve => setTimeout(resolve, interval));+ const response = await fetch(url, {+ headers: {+ 'Content-Type': 'application/json',+ 'x-api-key': apiKey+ }+ });+ if (response.status === 400) {+ const errBody = await response.json().catch(() => ({}));+ throw new Error(errBody?.detail?.error || 'Agent failed to respond');+ }+ if (!response.ok) {+ if (attempt === maxAttempts) throw new Error(`Poll failed: ${response.status}`);+ continue;+ }+ const data = await response.json();+ if (data.is_complete) return data;+ }+ throw new Error('Agent response timed out.');+}++// POST /agents — create a new persona agent (no skill picker in this minimal+// embedded form; skill_ids defaults to [] server-side, so the agent is created+// as a plain system-prompt-driven assistant with no extra tool skills attached).+export async function createAgent(apiKey, payload) {+ const response = await fetch(`${BASE_URL}/agents`, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'x-api-key': apiKey+ },+ body: JSON.stringify(payload)+ });+ if (!response.ok) {+ const errText = await response.text();+ throw new Error(`Failed to create agent: ${response.status} - ${errText.slice(0, 100)}`);+ }+ return await response.json();+}+export async function createWorkflow(apiKey, payload) {const response = await fetch(`${BASE_URL}/workflow/create`, {method: 'POST',