/** * Bot templates: where a new bot starts. A bot is an ordinary long-lived chat * with a name, description and avatar. Starting from a template opens a new * chat with the template's setup instructions as its first message, ready to * read and change before sending; the agent then sets itself up with its * ordinary tools. Templates can be exported as a file and imported into * another workspace. A template is Only me or shared with the workspace, like * a chat; anyone who starts chats here can change or delete a shared one. */ import { useRef, useState } from "react"; import { BotIcon, DownloadIcon, PlusIcon, UploadIcon } from "lucide-react"; import type { WorkspaceBotTemplate } from "@opengeni/contracts"; import { apiErrorDetails, userErrorText } from "../../../ui/api-error"; import { Button } from "../../../ui/button"; import { ConfirmDialog } from "../../../ui/confirm-dialog"; import { DetailPage, DetailPageHeader } from "../../../ui/detail-page"; import { DetailSection } from "../../../ui/detail-sheet"; import { DropdownMenuItem } from "../../../ui/dropdown-menu"; import { EmptyState } from "../../../ui/empty-state"; import { ErrorMessage } from "../../../ui/error-message"; import { ListRow, ListRowSkeleton, RowList } from "../../../ui/list-row"; import { LogoTile } from "../../../ui/logo-tile"; import { MetaChip } from "../../../ui/meta-chip"; import { Notice } from "../../../ui/notice"; import { useAppContext } from "../../app-context"; import { downloadBotTemplate, parseBotTemplateFile, useBotTemplates, } from "../../lib/bot-templates"; import { useNavigate } from "../../lib/navigation"; import { toast } from "../../lib/toast"; import { SessionAvatar, sessionAvatarFallbackHue, sessionAvatarInitials, } from "../session/session-avatar"; import { BotTemplateDialog, type BotTemplateValues } from "./bot-template-dialog"; const EMPTY_TEMPLATE: BotTemplateValues = { name: "", description: "", avatar: null, setupInstructions: "", visibility: "workspace", }; export function BotTemplatesPage({ workspaceId, onUseInSlack, }: { workspaceId: string; /** * Start a Slack agent from a template: a one-time copy of its name, * description, avatar and setup instructions. Hosts pass it only to people * who can create Slack agents. */ onUseInSlack?: ((template: WorkspaceBotTemplate) => void) | undefined; }) { const { client } = useAppContext(); const navigate = useNavigate(); const saved = useBotTemplates(workspaceId); const [creating, setCreating] = useState(false); const [editing, setEditing] = useState(null); const [deleting, setDeleting] = useState(null); const fileInput = useRef(null); const start = (template: WorkspaceBotTemplate) => void navigate({ to: "/workspaces/$workspaceId/sessions", params: { workspaceId }, search: { botTemplateId: template.id }, }); const importFile = async (file: File) => { const parsed = parseBotTemplateFile(await file.text()); if (!parsed.ok) { toast.error("Couldn't import the template", { description: parsed.error, }); return; } try { const created = await client.createBotTemplate(workspaceId, parsed.request); saved.replace((current) => [created, ...current]); toast.success("Template imported", { description: created.name }); } catch (error) { toast.error("Couldn't import the template", { description: userErrorText(error), }); } }; const actions = ( <> { const file = event.target.files?.[0]; event.target.value = ""; if (file) void importFile(file); }} /> ); return ( } />} title="Bot templates" meta={["Start a bot from one: a chat that sets itself up and stays around"]} actions={saved.supported ? actions : undefined} /> {!saved.supported ? ( This server doesn't keep bot templates yet. ) : saved.templates === undefined ? ( ) : saved.error && saved.templates.length === 0 ? ( Try again } > Check your connection, then try again. ) : saved.templates.length === 0 ? ( } title="No bot templates yet" description="Save one from a chat's menu, ask a chat's agent to save itself as one, or write one here." action={ } /> ) : ( {saved.templates.map((template) => ( ) : ( ) } title={template.name} titleAddon={ template.visibility === "private" ? ( Only me ) : null } description={template.description || undefined} control={ } menuLabel={`More actions for the ${template.name} template`} menu={ <> downloadBotTemplate(template)}> Export {onUseInSlack ? ( onUseInSlack(template)}> Use in Slack ) : null} {template.viewerAccess === "manage" ? ( <> setEditing(template)}> Edit setDeleting(template)} > Delete ) : null} } onOpen={() => start(template)} /> ))} )} { const created = await client.createBotTemplate(workspaceId, values); saved.replace((current) => [created, ...current]); toast.success("Template saved", { description: created.name }); }} /> { if (!open) setEditing(null); }} workspaceId={workspaceId} seed={editing?.id ?? "bot-template"} mode="edit" canMakePrivate={editing?.viewerCanMakePrivate ?? true} initial={ editing ? { name: editing.name, description: editing.description, avatar: editing.avatar, setupInstructions: editing.setupInstructions, visibility: editing.visibility, } : EMPTY_TEMPLATE } onSubmit={async (values) => { if (!editing) return; const updated = await client.updateBotTemplate(workspaceId, editing.id, values); saved.replace((current) => current.map((entry) => (entry.id === updated.id ? updated : entry)), ); }} /> { if (!open) setDeleting(null); }} title={`Delete the “${deleting?.name ?? ""}” template?`} description="Bots started from it keep working." confirmLabel="Delete template" pendingLabel="Deleting…" onConfirm={async () => { if (!deleting) return true; try { await client.deleteBotTemplate(workspaceId, deleting.id); saved.replace((current) => current.filter((entry) => entry.id !== deleting.id)); toast.success("Template deleted", { description: deleting.name }); return true; } catch (error) { toast.error("Couldn't delete the template", { description: userErrorText(error), }); return false; } }} /> ); }