2026-02-04 14:58:14 +05:30
|
|
|
"use client";
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
import { useEffect, useState } from "react";
|
2026-02-04 14:58:14 +05:30
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
|
|
|
|
|
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
|
|
|
|
|
|
|
|
|
import { DashboardSidebar } from "@/components/organisms/DashboardSidebar";
|
|
|
|
|
import { DashboardShell } from "@/components/templates/DashboardShell";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
2026-02-04 20:21:33 +05:30
|
|
|
import { getApiBaseUrl } from "@/lib/api-base";
|
2026-02-04 16:04:52 +05:30
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
2026-02-04 14:58:14 +05:30
|
|
|
|
2026-02-04 20:21:33 +05:30
|
|
|
const apiBase = getApiBaseUrl();
|
2026-02-04 14:58:14 +05:30
|
|
|
|
|
|
|
|
type Agent = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
type Board = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
slug: string;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-04 14:58:14 +05:30
|
|
|
export default function NewAgentPage() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const { getToken, isSignedIn } = useAuth();
|
|
|
|
|
|
|
|
|
|
const [name, setName] = useState("");
|
2026-02-04 16:04:52 +05:30
|
|
|
const [boards, setBoards] = useState<Board[]>([]);
|
|
|
|
|
const [boardId, setBoardId] = useState<string>("");
|
2026-02-04 17:05:58 +05:30
|
|
|
const [heartbeatEvery, setHeartbeatEvery] = useState("10m");
|
|
|
|
|
const [heartbeatTarget, setHeartbeatTarget] = useState("none");
|
2026-02-04 14:58:14 +05:30
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
const loadBoards = async () => {
|
|
|
|
|
if (!isSignedIn) return;
|
|
|
|
|
try {
|
|
|
|
|
const token = await getToken();
|
|
|
|
|
const response = await fetch(`${apiBase}/api/v1/boards`, {
|
|
|
|
|
headers: { Authorization: token ? `Bearer ${token}` : "" },
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Unable to load boards.");
|
|
|
|
|
}
|
|
|
|
|
const data = (await response.json()) as Board[];
|
|
|
|
|
setBoards(data);
|
|
|
|
|
if (!boardId && data.length > 0) {
|
|
|
|
|
setBoardId(data[0].id);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err instanceof Error ? err.message : "Something went wrong.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
loadBoards();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [isSignedIn]);
|
|
|
|
|
|
2026-02-04 14:58:14 +05:30
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (!isSignedIn) return;
|
|
|
|
|
const trimmed = name.trim();
|
|
|
|
|
if (!trimmed) {
|
|
|
|
|
setError("Agent name is required.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-04 16:04:52 +05:30
|
|
|
if (!boardId) {
|
|
|
|
|
setError("Select a board before creating an agent.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-04 14:58:14 +05:30
|
|
|
setIsLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
|
|
|
|
const token = await getToken();
|
|
|
|
|
const response = await fetch(`${apiBase}/api/v1/agents`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
|
|
|
},
|
2026-02-04 17:05:58 +05:30
|
|
|
body: JSON.stringify({
|
|
|
|
|
name: trimmed,
|
|
|
|
|
board_id: boardId,
|
|
|
|
|
heartbeat_config: {
|
|
|
|
|
every: heartbeatEvery.trim() || "10m",
|
|
|
|
|
target: heartbeatTarget,
|
|
|
|
|
},
|
|
|
|
|
}),
|
2026-02-04 14:58:14 +05:30
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Unable to create agent.");
|
|
|
|
|
}
|
|
|
|
|
const created = (await response.json()) as Agent;
|
|
|
|
|
router.push(`/agents/${created.id}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err instanceof Error ? err.message : "Something went wrong.");
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DashboardShell>
|
|
|
|
|
<SignedOut>
|
2026-02-04 21:44:21 +05:30
|
|
|
<div className="col-span-2 flex min-h-[calc(100vh-64px)] items-center justify-center bg-slate-50 p-10 text-center">
|
|
|
|
|
<div className="rounded-xl border border-slate-200 bg-white px-8 py-6 shadow-sm">
|
|
|
|
|
<p className="text-sm text-slate-600">Sign in to create an agent.</p>
|
|
|
|
|
<SignInButton
|
|
|
|
|
mode="modal"
|
|
|
|
|
forceRedirectUrl="/agents/new"
|
|
|
|
|
signUpForceRedirectUrl="/agents/new"
|
|
|
|
|
>
|
|
|
|
|
<Button className="mt-4">Sign in</Button>
|
|
|
|
|
</SignInButton>
|
|
|
|
|
</div>
|
2026-02-04 14:58:14 +05:30
|
|
|
</div>
|
|
|
|
|
</SignedOut>
|
|
|
|
|
<SignedIn>
|
|
|
|
|
<DashboardSidebar />
|
2026-02-04 21:44:21 +05:30
|
|
|
<main className="flex-1 overflow-y-auto bg-slate-50">
|
|
|
|
|
<div className="border-b border-slate-200 bg-white px-8 py-6">
|
|
|
|
|
<div>
|
|
|
|
|
<h1 className="font-heading text-2xl font-semibold text-slate-900 tracking-tight">
|
|
|
|
|
Create agent
|
|
|
|
|
</h1>
|
|
|
|
|
<p className="mt-1 text-sm text-slate-500">
|
|
|
|
|
Agents start in provisioning until they check in.
|
|
|
|
|
</p>
|
2026-02-04 14:58:14 +05:30
|
|
|
</div>
|
2026-02-04 21:44:21 +05:30
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="p-8">
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="rounded-xl border border-slate-200 bg-white p-6 shadow-sm space-y-6"
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-xs font-semibold uppercase tracking-wider text-slate-500">
|
|
|
|
|
Agent identity
|
2026-02-04 16:04:52 +05:30
|
|
|
</p>
|
2026-02-04 21:44:21 +05:30
|
|
|
<div className="mt-4 grid gap-6 md:grid-cols-2">
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<label className="text-sm font-medium text-slate-900">
|
|
|
|
|
Agent name
|
|
|
|
|
</label>
|
|
|
|
|
<Input
|
|
|
|
|
value={name}
|
|
|
|
|
onChange={(event) => setName(event.target.value)}
|
|
|
|
|
placeholder="e.g. Deploy bot"
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<label className="text-sm font-medium text-slate-900">
|
|
|
|
|
Board
|
|
|
|
|
</label>
|
|
|
|
|
<Select
|
|
|
|
|
value={boardId}
|
|
|
|
|
onValueChange={(value) => setBoardId(value)}
|
|
|
|
|
disabled={boards.length === 0}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder="Select board" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{boards.map((board) => (
|
|
|
|
|
<SelectItem key={board.id} value={board.id}>
|
|
|
|
|
{board.name}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
{boards.length === 0 ? (
|
|
|
|
|
<p className="text-xs text-slate-500">
|
|
|
|
|
Create a board before adding agents.
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-xs font-semibold uppercase tracking-wider text-slate-500">
|
|
|
|
|
Heartbeat settings
|
|
|
|
|
</p>
|
|
|
|
|
<div className="mt-4 grid gap-6 md:grid-cols-2">
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<label className="text-sm font-medium text-slate-900">
|
|
|
|
|
Interval
|
|
|
|
|
</label>
|
|
|
|
|
<Input
|
|
|
|
|
value={heartbeatEvery}
|
|
|
|
|
onChange={(event) => setHeartbeatEvery(event.target.value)}
|
|
|
|
|
placeholder="e.g. 10m"
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
<p className="text-xs text-slate-500">
|
|
|
|
|
How often this agent runs HEARTBEAT.md (10m, 30m, 2h).
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<label className="text-sm font-medium text-slate-900">
|
|
|
|
|
Target
|
|
|
|
|
</label>
|
|
|
|
|
<Select
|
|
|
|
|
value={heartbeatTarget}
|
|
|
|
|
onValueChange={(value) => setHeartbeatTarget(value)}
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder="Select target" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
<SelectItem value="none">
|
|
|
|
|
None (no outbound message)
|
|
|
|
|
</SelectItem>
|
|
|
|
|
<SelectItem value="last">Last channel</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error ? (
|
|
|
|
|
<div className="rounded-lg border border-slate-200 bg-white p-3 text-sm text-slate-600 shadow-sm">
|
|
|
|
|
{error}
|
|
|
|
|
</div>
|
2026-02-04 16:04:52 +05:30
|
|
|
) : null}
|
2026-02-04 21:44:21 +05:30
|
|
|
|
|
|
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
|
|
|
<Button type="submit" disabled={isLoading}>
|
|
|
|
|
{isLoading ? "Creating…" : "Create agent"}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => router.push("/agents")}
|
|
|
|
|
>
|
|
|
|
|
Back to agents
|
|
|
|
|
</Button>
|
2026-02-04 14:58:14 +05:30
|
|
|
</div>
|
2026-02-04 21:44:21 +05:30
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
2026-02-04 14:58:14 +05:30
|
|
|
</SignedIn>
|
|
|
|
|
</DashboardShell>
|
|
|
|
|
);
|
|
|
|
|
}
|