2025-04-16 04:45:46 +08:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import React, { useState, Suspense } from 'react';
|
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
import { ChatInput } from '@/components/thread/chat-input';
|
2025-04-17 21:19:52 +08:00
|
|
|
import { createProject, addUserMessage, startAgent, createThread } from "@/lib/api";
|
|
|
|
import { generateThreadName } from "@/lib/actions/threads";
|
2025-04-16 04:45:46 +08:00
|
|
|
|
|
|
|
function DashboardContent() {
|
|
|
|
const [inputValue, setInputValue] = useState("");
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const router = useRouter();
|
|
|
|
|
2025-04-18 13:42:57 +08:00
|
|
|
const handleSubmit = async (message: string, options?: { model_name?: string; enable_thinking?: boolean }) => {
|
2025-04-16 04:45:46 +08:00
|
|
|
if (!message.trim() || isSubmitting) return;
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
|
|
|
try {
|
2025-04-16 17:54:52 +08:00
|
|
|
// Generate a name for the project using GPT
|
|
|
|
const projectName = await generateThreadName(message);
|
|
|
|
|
|
|
|
// 1. Create a new project with the GPT-generated name
|
2025-04-16 04:45:46 +08:00
|
|
|
const newAgent = await createProject({
|
2025-04-16 17:54:52 +08:00
|
|
|
name: projectName,
|
2025-04-16 04:45:46 +08:00
|
|
|
description: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
// 2. Create a new thread for this project
|
|
|
|
const thread = await createThread(newAgent.id);
|
|
|
|
|
|
|
|
// 3. Add the user message to the thread
|
|
|
|
await addUserMessage(thread.thread_id, message.trim());
|
|
|
|
|
|
|
|
// 4. Start the agent with the thread ID
|
2025-04-18 13:42:57 +08:00
|
|
|
const agentRun = await startAgent(thread.thread_id, {
|
|
|
|
model_name: options?.model_name,
|
|
|
|
enable_thinking: options?.enable_thinking,
|
|
|
|
stream: true
|
|
|
|
});
|
2025-04-16 04:45:46 +08:00
|
|
|
|
|
|
|
// 5. Navigate to the new agent's thread page
|
|
|
|
router.push(`/dashboard/agents/${thread.thread_id}`);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error creating agent:", error);
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-04-16 01:20:15 +08:00
|
|
|
return (
|
2025-04-16 04:45:46 +08:00
|
|
|
<div className="flex flex-col items-center justify-center h-full w-full">
|
|
|
|
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-[560px] max-w-[90%]">
|
|
|
|
<div className="text-center mb-10">
|
2025-04-16 08:04:04 +08:00
|
|
|
<h1 className="text-4xl font-medium text-foreground mb-2">Hey </h1>
|
|
|
|
<h2 className="text-2xl text-muted-foreground">What would you like Suna to do today?</h2>
|
2025-04-16 04:45:46 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<ChatInput
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
loading={isSubmitting}
|
|
|
|
placeholder="Describe what you need help with..."
|
|
|
|
value={inputValue}
|
|
|
|
onChange={setInputValue}
|
|
|
|
/>
|
2025-04-16 01:20:15 +08:00
|
|
|
</div>
|
2025-04-16 04:45:46 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function DashboardPage() {
|
|
|
|
return (
|
|
|
|
<Suspense fallback={
|
|
|
|
<div className="flex flex-col items-center justify-center h-full w-full">
|
|
|
|
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-[560px] max-w-[90%]">
|
|
|
|
<div className="flex flex-col items-center text-center mb-10">
|
|
|
|
<Skeleton className="h-10 w-40 mb-2" />
|
|
|
|
<Skeleton className="h-7 w-56" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Skeleton className="w-full h-[100px] rounded-xl" />
|
|
|
|
<div className="flex justify-center mt-3">
|
|
|
|
<Skeleton className="h-5 w-16" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}>
|
|
|
|
<DashboardContent />
|
|
|
|
</Suspense>
|
|
|
|
);
|
2025-04-16 01:20:15 +08:00
|
|
|
}
|