Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Skills library: import several skill files at once
erywim6405fe65496c
diff --git a/src/components/SkillsSection.tsx b/src/components/SkillsSection.tsxindex cbbc5f0a32..ec65bd03fe 100644--- a/src/components/SkillsSection.tsx+++ b/src/components/SkillsSection.tsx@@ -9,6 +9,7 @@ import type { SkillsLibrarySkillWire } from "../../shared/wire";import { Layer } from "./Layer";import { Select } from "./Select";import { serverFetch } from "@/lib/relay-server";+import { SKILL_FILE_ACCEPT, skillFilesOf } from "@/lib/skill-files";export function filterLibrarySkills(skills: readonly SkillsLibrarySkillWire[],@@ -50,6 +51,7 @@ export function SkillsSection() {const [importMessage, setImportMessage] = useState("");const [assignBot, setAssignBot] = useState("");const busy = useRef(false);+ const fileInput = useRef<HTMLInputElement>(null);const bots = state.bots.filter((bot) => !bot.hidden);const tags = useMemo(@@ -180,6 +182,17 @@ export function SkillsSection() {}};+ const importOne = async (text: string): Promise<string> => {+ const response = await serverFetch("/api/skills-library", {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ text }),+ });+ const body = (await response.json()) as { skill?: { name: string }; error?: string };+ if (!response.ok) throw new Error(body.error ?? t("skills.error.import"));+ return body.skill?.name ?? "";+ };+const importSkill = async () => {const text = importText.trim();if (!text) return;@@ -187,14 +200,8 @@ export function SkillsSection() {setError("");setImportMessage("");try {- const response = await serverFetch("/api/skills-library", {- method: "POST",- headers: { "content-type": "application/json" },- body: JSON.stringify({ text }),- });- const body = (await response.json()) as { skill?: { name: string }; error?: string };- if (!response.ok) throw new Error(body.error ?? t("skills.error.import"));- setImportMessage(t("skills.library.imported", { name: body.skill?.name ?? "" }));+ const name = await importOne(text);+ setImportMessage(t("skills.library.imported", { name }));setImportText("");await refresh();} catch (cause) {@@ -204,6 +211,35 @@ export function SkillsSection() {}};+ /** Each chosen SKILL.md goes through the same local import as a paste: no URL is read, and the bytes are the file's. */+ const importFiles = async (list: FileList | null) => {+ const files = skillFilesOf(list);+ if (!files.length) return;+ setImporting(true);+ setError("");+ setImportMessage("");+ const failed: string[] = [];+ let imported = 0;+ try {+ for (const file of files) {+ try {+ await importOne(await file.text());+ imported++;+ } catch {+ failed.push(file.name);+ }+ }+ if (imported) {+ setImportMessage(t("skills.library.importFilesDone", { count: imported }));+ await refresh();+ }+ if (failed.length) setError(t("skills.library.importFilesFailed", { count: failed.length, names: failed.slice(0, 3).join(", ") + (failed.length > 3 ? "…" : "") }));+ } finally {+ setImporting(false);+ if (fileInput.current) fileInput.current.value = "";+ }+ };+const inputClass ="w-full rounded-lg border border-hairline/40 bg-inset px-3 py-2 text-[14px] text-ink placeholder:text-ink-secondary focus:outline-none";@@ -360,13 +396,33 @@ export function SkillsSection() {/><div className="flex items-center justify-between gap-2">{importMessage ? <div className="text-[12px] text-ink-secondary">{importMessage}</div> : <span />}- <button- type="submit"- disabled={importing || Boolean(working) || !importText.trim()}- className="shrink-0 rounded-lg bg-control px-3 py-2 text-[13px] text-ink hover:bg-raised-hover disabled:opacity-50"- >- {importing ? t("skills.library.importing") : t("skills.library.importButton")}- </button>+ <div className="flex shrink-0 items-center gap-2">+ <input+ ref={fileInput}+ type="file"+ accept={SKILL_FILE_ACCEPT}+ multiple+ className="hidden"+ aria-hidden="true"+ tabIndex={-1}+ onChange={(e) => void importFiles(e.target.files)}+ />+ <button+ type="button"+ onClick={() => fileInput.current?.click()}+ disabled={importing || Boolean(working)}+ className="rounded-lg border border-hairline/40 px-3 py-2 text-[13px] text-ink hover:bg-raised-hover disabled:opacity-50"+ >+ {t("skills.library.importFiles")}+ </button>+ <button+ type="submit"+ disabled={importing || Boolean(working) || !importText.trim()}+ className="rounded-lg bg-control px-3 py-2 text-[13px] text-ink hover:bg-raised-hover disabled:opacity-50"+ >+ {importing ? t("skills.library.importing") : t("skills.library.importButton")}+ </button>+ </div></div></form></Card>diff --git a/src/lib/skill-files.test.ts b/src/lib/skill-files.test.tsnew file mode 100644index 0000000000..717ea411a9--- /dev/null+++ b/src/lib/skill-files.test.ts@@ -0,0 +1,34 @@+import { expect, it } from "vitest";++import { SKILL_FILE_ACCEPT, skillFilesOf } from "./skill-files";++const named = (name: string, type = "") => new File([`# ${name}`], name, { type });++it("keeps markdown and plain text, whatever the extension case", () => {+ const files = [named("SKILL.md"), named("notes.markdown"), named("README.MD"), named("copy.txt")];+ expect(skillFilesOf(files).map((file) => file.name)).toEqual(["SKILL.md", "notes.markdown", "README.MD", "copy.txt"]);+});++it("trusts a text MIME type when the name has no telling extension", () => {+ const file = new File(["# skill"], "SKILL", { type: "text/markdown" });+ expect(skillFilesOf([file])).toEqual([file]);+});++it("leaves images and archives from the same picker alone", () => {+ const picked = [named("SKILL.md"), named("diagram.png", "image/png"), named("skills.zip", "application/zip"), named("app.js")];+ expect(skillFilesOf(picked).map((file) => file.name)).toEqual(["SKILL.md"]);+});++it("answers an empty list for nothing chosen", () => {+ expect(skillFilesOf(null)).toEqual([]);+ expect(skillFilesOf(undefined)).toEqual([]);+ expect(skillFilesOf([])).toEqual([]);+});++it("asks the picker for markdown and plain text only", () => {+ expect(SKILL_FILE_ACCEPT).toContain(".md");+ expect(SKILL_FILE_ACCEPT).toContain(".markdown");+ expect(SKILL_FILE_ACCEPT).toContain("text/markdown");+ expect(SKILL_FILE_ACCEPT).toContain("text/plain");+ expect(SKILL_FILE_ACCEPT).not.toContain("image");+});diff --git a/src/lib/skill-files.ts b/src/lib/skill-files.tsnew file mode 100644index 0000000000..b0d56d3d23--- /dev/null+++ b/src/lib/skill-files.ts@@ -0,0 +1,7 @@+// Security: a chosen file is read in the browser and posted as text, exactly like a paste. Nothing here fetches a URL.+export const SKILL_FILE_ACCEPT = ".md,.markdown,text/markdown,text/plain";++/** The chosen files that may hold a SKILL.md; an image or an archive from the same picker is left alone. */+export function skillFilesOf(list: FileList | readonly File[] | null | undefined): File[] {+ return Array.from(list ?? []).filter((file) => /\.(?:md|markdown|txt)$/i.test(file.name) || /^text\/(?:markdown|plain)$/i.test(file.type));+}diff --git a/src/locales/en.json b/src/locales/en.jsonindex cf38252006..dfb0826399 100644--- a/src/locales/en.json+++ b/src/locales/en.json@@ -6583,6 +6583,9 @@"skills.library.view": "View {skill}","skills.library.importTitle": "Import a skill","skills.library.importHint": "Paste a SKILL.md file exactly as it exists on disk. Local import only: no URL is fetched.",+ "skills.library.importFiles": "Choose files…",+ "skills.library.importFilesDone": "Imported {count} skills, disabled for review.",+ "skills.library.importFilesFailed": "{count} files could not be imported: {names}","skills.library.importPlaceholder": "---\nname: my-skill\ndescription: What it does\n---\n\nInstructions…","skills.library.importButton": "Import skill","skills.library.importing": "Importing…",diff --git a/src/locales/pt-br.json b/src/locales/pt-br.jsonindex 0dd3959498..f268a73471 100644--- a/src/locales/pt-br.json+++ b/src/locales/pt-br.json@@ -6212,6 +6212,9 @@"skills.library.view": "Ver {skill}","skills.library.importTitle": "Importar uma habilidade","skills.library.importHint": "Cole o conteúdo de um arquivo SKILL.md exatamente como está no disco. A importação é só local: nenhuma URL é acessada.",+ "skills.library.importFiles": "Escolher arquivos…",+ "skills.library.importFilesDone": "{count} habilidades importadas, desligadas para revisão.",+ "skills.library.importFilesFailed": "{count} arquivos não puderam ser importados: {names}","skills.library.importPlaceholder": "---\nname: minha-habilidade\ndescription: O que ela faz\n---\n\nInstruções…","skills.library.importButton": "Importar habilidade","skills.library.importing": "Importando…",