|
| 1 | +import { Badge } from "@posthog/ui/primitives/Badge"; |
| 2 | +import { Button } from "@posthog/ui/primitives/Button"; |
| 3 | +import { Dialog, Flex, Text } from "@radix-ui/themes"; |
| 4 | +import { useMemo, useState } from "react"; |
| 5 | +import { useImportAgentDraftBundle } from "../hooks/useImportAgentDraftBundle"; |
| 6 | +import { parseBundleInput } from "../utils/parseBundleInput"; |
| 7 | + |
| 8 | +const SAMPLE = `--- agent.md --- |
| 9 | +You are the growth review agent. … |
| 10 | +
|
| 11 | +--- skills/research/SKILL.md --- |
| 12 | +When asked to research, … |
| 13 | +
|
| 14 | +--- skills/draft-post/SKILL.md --- |
| 15 | +When asked to draft, … |
| 16 | +`; |
| 17 | + |
| 18 | +/** |
| 19 | + * Bulk-paste a markdown bundle into a draft revision. Designed for migrating |
| 20 | + * an existing multi-file agent in one paste — concatenate the source files |
| 21 | + * with a `--- path ---` header between each. Existing skill ids are |
| 22 | + * overwritten; new ids are added; skills not mentioned are left alone. |
| 23 | + */ |
| 24 | +export function AgentBundleImportDialog({ |
| 25 | + open, |
| 26 | + onOpenChange, |
| 27 | + idOrSlug, |
| 28 | + revisionId, |
| 29 | + existingSkillIds, |
| 30 | + onSuccess, |
| 31 | +}: { |
| 32 | + open: boolean; |
| 33 | + onOpenChange: (open: boolean) => void; |
| 34 | + idOrSlug: string; |
| 35 | + revisionId: string; |
| 36 | + existingSkillIds: string[]; |
| 37 | + onSuccess?: () => void; |
| 38 | +}) { |
| 39 | + const [input, setInput] = useState(""); |
| 40 | + const mutation = useImportAgentDraftBundle(idOrSlug, revisionId); |
| 41 | + |
| 42 | + const parsed = useMemo(() => { |
| 43 | + if (input.trim().length === 0) return null; |
| 44 | + return parseBundleInput(input); |
| 45 | + }, [input]); |
| 46 | + |
| 47 | + const value = parsed?.ok ? parsed.value : null; |
| 48 | + const existing = useMemo(() => new Set(existingSkillIds), [existingSkillIds]); |
| 49 | + |
| 50 | + const onConfirm = () => { |
| 51 | + if (!value) return; |
| 52 | + mutation.mutate(value, { |
| 53 | + onSuccess: () => { |
| 54 | + setInput(""); |
| 55 | + mutation.reset(); |
| 56 | + onOpenChange(false); |
| 57 | + onSuccess?.(); |
| 58 | + }, |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + const close = () => { |
| 63 | + if (mutation.isPending) return; |
| 64 | + setInput(""); |
| 65 | + mutation.reset(); |
| 66 | + onOpenChange(false); |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <Dialog.Root |
| 71 | + open={open} |
| 72 | + onOpenChange={(isOpen) => { |
| 73 | + if (!isOpen) close(); |
| 74 | + }} |
| 75 | + > |
| 76 | + <Dialog.Content maxWidth="640px"> |
| 77 | + <Dialog.Title className="text-base">Paste markdown bundle</Dialog.Title> |
| 78 | + <Dialog.Description size="2" className="text-gray-11"> |
| 79 | + Paste one or more <code>--- path ---</code> blocks. Accepts{" "} |
| 80 | + <code>agent.md</code> and <code>skills/[id]/SKILL.md</code>. Existing |
| 81 | + skills are overwritten by id; new ids are added. |
| 82 | + </Dialog.Description> |
| 83 | + <textarea |
| 84 | + aria-label="Markdown bundle" |
| 85 | + value={input} |
| 86 | + onChange={(e) => setInput(e.currentTarget.value)} |
| 87 | + placeholder={SAMPLE} |
| 88 | + disabled={mutation.isPending} |
| 89 | + spellCheck={false} |
| 90 | + className="mt-3 min-h-[280px] w-full resize-y rounded-(--radius-2) border border-border bg-(--color-panel-solid) p-3 text-[12.5px] text-gray-12 [font-family:var(--font-mono)] focus:border-(--accent-7) focus:outline-none" |
| 91 | + /> |
| 92 | + {parsed && !parsed.ok ? ( |
| 93 | + <Text className="mt-2 block text-(--red-11) text-[12px]"> |
| 94 | + {parsed.error} |
| 95 | + </Text> |
| 96 | + ) : null} |
| 97 | + {value ? ( |
| 98 | + <div className="mt-3 rounded-(--radius-2) border border-border bg-(--gray-2) px-3 py-2"> |
| 99 | + <Text className="block text-[11px] text-gray-10 uppercase tracking-wide"> |
| 100 | + Will write |
| 101 | + </Text> |
| 102 | + <Flex direction="column" gap="1" className="mt-1.5"> |
| 103 | + {value.agent_md !== undefined ? ( |
| 104 | + <Flex align="center" gap="2"> |
| 105 | + <code className="text-[12px] text-gray-12 [font-family:var(--font-mono)]"> |
| 106 | + agent.md |
| 107 | + </code> |
| 108 | + <Badge color="blue">update</Badge> |
| 109 | + </Flex> |
| 110 | + ) : null} |
| 111 | + {value.skills?.map((s) => ( |
| 112 | + <Flex key={s.id} align="center" gap="2"> |
| 113 | + <code className="text-[12px] text-gray-12 [font-family:var(--font-mono)]"> |
| 114 | + skills/{s.id}/SKILL.md |
| 115 | + </code> |
| 116 | + <Badge color={existing.has(s.id) ? "blue" : "green"}> |
| 117 | + {existing.has(s.id) ? "update" : "new"} |
| 118 | + </Badge> |
| 119 | + </Flex> |
| 120 | + ))} |
| 121 | + </Flex> |
| 122 | + </div> |
| 123 | + ) : null} |
| 124 | + {mutation.isError ? ( |
| 125 | + <Text className="mt-2 block text-(--red-11) text-[12px]"> |
| 126 | + {mutation.error?.message ?? "Import failed"} |
| 127 | + </Text> |
| 128 | + ) : null} |
| 129 | + <Flex justify="end" gap="2" mt="4"> |
| 130 | + <Button |
| 131 | + size="1" |
| 132 | + variant="soft" |
| 133 | + color="gray" |
| 134 | + disabled={mutation.isPending} |
| 135 | + onClick={close} |
| 136 | + > |
| 137 | + Cancel |
| 138 | + </Button> |
| 139 | + <Button |
| 140 | + size="1" |
| 141 | + loading={mutation.isPending} |
| 142 | + disabled={!value} |
| 143 | + onClick={onConfirm} |
| 144 | + > |
| 145 | + Import |
| 146 | + </Button> |
| 147 | + </Flex> |
| 148 | + </Dialog.Content> |
| 149 | + </Dialog.Root> |
| 150 | + ); |
| 151 | +} |
0 commit comments