/** * Create or change a bot template: the new bot's name, description and * avatar, the setup instructions that become its chat's first message, and * who can use it (Only me, or everyone in the workspace). */ import { useState } from "react"; import { BOT_TEMPLATE_SETUP_INSTRUCTIONS_MAX_LENGTH } from "@opengeni/contracts"; import type { BotTemplateVisibility, SessionAvatar } from "@opengeni/sdk"; import { Field, TextArea } from "../../../ui/field"; import { SegmentedControl } from "../../../ui/segmented-control"; import { SessionProfileDialog } from "../session/session-profile-dialog"; export type BotTemplateValues = { name: string; description: string; avatar: SessionAvatar | null; setupInstructions: string; visibility: BotTemplateVisibility; }; const VISIBILITY_OPTIONS = [ { value: "private", label: "Only me" }, { value: "workspace", label: "Everyone in the workspace" }, ] as const satisfies readonly { value: BotTemplateVisibility; label: string }[]; export function BotTemplateDialog({ open, onOpenChange, workspaceId, seed, mode, initial, canMakePrivate = true, onSubmit, }: { open: boolean; onOpenChange: (open: boolean) => void; workspaceId: string; seed: string; /** `save`: from a chat; `new`: from scratch; `edit`: a saved template. */ mode: "save" | "new" | "edit"; initial: BotTemplateValues; /** False when only whoever saved it, or an admin, could make it Only me. */ canMakePrivate?: boolean; onSubmit: (values: BotTemplateValues) => Promise; }) { const [setup, setSetup] = useState(initial.setupInstructions); const [setupError, setSetupError] = useState(null); const [visibility, setVisibility] = useState(initial.visibility); const [opened, setOpened] = useState(open); if (open !== opened) { setOpened(open); if (open) { setSetup(initial.setupInstructions); setSetupError(null); setVisibility(initial.visibility); } } return ( { const trimmed = setup.trim(); if (!trimmed) { setSetupError("Say what the new bot should do to set itself up."); return false; } await onSubmit({ name: values.title, description: values.description, avatar: values.avatar, setupInstructions: trimmed, visibility, }); }} >