import { useEffect, useState } from "react"; import { post } from "../api"; import { useAgents, useFetch } from "../hooks"; import { useT } from "../i18n"; import { ModelSelect } from "./ModelSelect"; interface DirListing { path: string; parent: string | null; workspace: string; dirs: { name: string; path: string; git: boolean }[]; } const LAST_DIR = "codecraft.lastDir"; export function NewSession({ onMenu, onCreated }: { onMenu: () => void; onCreated: (id: string) => void }) { const t = useT(); const [path, setPath] = useState(localStorage.getItem(LAST_DIR) ?? ""); const { data: listing, error: dirError } = useFetch(`/fs/dirs${path ? `?path=${encodeURIComponent(path)}` : ""}`); const config = useFetch<{ default_model?: string }>("/config").data; const [model, setModel] = useState(""); const [agent, setAgent] = useState("build"); const [prompt, setPrompt] = useState(""); const [error, setError] = useState(); const [creating, setCreating] = useState(false); const agents = useAgents(listing?.path).filter((a) => a.mode === "primary"); useEffect(() => { if (!model && config?.default_model) setModel(config.default_model); }, [config, model]); // A remembered directory that vanished: fall back to the workspace root. useEffect(() => { if (dirError && path) setPath(""); }, [dirError, path]); const create = async () => { if (!listing) return; setCreating(true); setError(undefined); try { localStorage.setItem(LAST_DIR, listing.path); const s = await post<{ id: string }>("/sessions", { cwd: listing.path, model: model || undefined, agent }); if (prompt.trim()) await post(`/sessions/${s.id}/prompt`, { text: prompt.trim() }); onCreated(s.id); } catch (e: any) { setError(e.message); setCreating(false); } }; const rel = listing ? listing.path.slice(listing.workspace.length) || "/" : ""; return ( <>

{t("new.title")}

{t("new.directory")}

{rel} {listing?.parent && ( )}
{listing?.dirs.length === 0 &&
{t("new.noSubdirs")}
} {listing?.dirs.map((d) => ( ))}

{t("new.firstPrompt")}