Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
feat(skills): call skills with /name anywhere in a message
Anil Kumar8b820bd5bdbb
diff --git a/server/imported-skills.ts b/server/imported-skills.tsindex 0c3a04dca4..38c46b535d 100644--- a/server/imported-skills.ts+++ b/server/imported-skills.ts@@ -14,7 +14,7 @@ import { homedir } from "node:os";import { dirname, isAbsolute, join, resolve } from "node:path";import { z } from "zod";-import { parseSkillCommand } from "../shared/skill-command.ts";+import { expandSkillCommands, mayCallSkill } from "../shared/skill-command.ts";import { isSkillName } from "../shared/skill-md.ts";import { writeFileAtomic } from "./atomic.ts";import { DATA_DIR } from "./config.ts";@@ -410,14 +410,8 @@ export function importedSkillsPrompt(skills: readonly ImportedSkillView[]): stri/** `/name` for an imported skill, the way a bot's own skill command reads. */export function expandImportedSkillCommand(text: string, skills: readonly ImportedSkillView[]): string {- if (!text.trimStart().startsWith("/")) return text;- const command = parseSkillCommand(text, skills.map((skill) => skill.name));- const skill = command && skills.find((candidate) => candidate.name === command.name);- if (!command || !skill) return text;- const lead = `The user ran your ${command.name} skill with /${command.name}. Read ${JSON.stringify(skill.file)} with your file tools and follow it for this turn.`;- return command.request- ? `${lead} Where the skill says {input}, use the request.\n\nRequest: ${command.request}`- : `${lead} There is no further request, so run it as the skill describes.`;+ if (!mayCallSkill(text)) return text;+ return expandSkillCommands(text, skills.map((skill) => skill.name), (name) => skills.find((skill) => skill.name === name)?.file ?? null);}const NATIVE_SKILL_DIRS = [".claude/skills", ".agents/skills", ".grok/skills"];diff --git a/server/skill-command.test.ts b/server/skill-command.test.tsindex 4150070621..bdcf372857 100644--- a/server/skill-command.test.ts+++ b/server/skill-command.test.ts@@ -3,7 +3,7 @@ import { join } from "node:path";import { enabledSkillFile, expandSkillCommandTurnText, installSkill, setSkillEnabled } from "./skills.ts";import { workspaceDir } from "./workspace.ts";-import { BUILT_IN_SLASH_COMMANDS, offersSkillCommand, parseSkillCommand } from "../shared/skill-command.ts";+import { BUILT_IN_SLASH_COMMANDS, expandSkillCommands, findSkillCommands, offersSkillCommand, parseSkillCommand } from "../shared/skill-command.ts";const SKILL = (name: string) =>`---\nname: ${name}\ndescription: Writes the weekly standup note.\n---\n\n# Standup\n\nSummarize {input} as three bullets.\n`;@@ -60,3 +60,55 @@ describe("expandSkillCommandTurnText", () => {expect(expandSkillCommandTurnText(on, "standup please")).toBe("standup please");});});++describe("findSkillCommands", () => {+ const skills = ["standup", "review", "weekly-2"];++ it("finds each /name that is its own word, anywhere in the message, once and in order", () => {+ expect(findSkillCommands("please /review this, then /standup.\n/review again", skills)).toEqual(["review", "standup"]);+ expect(findSkillCommands("/weekly-2 and (see /standup)", skills)).toEqual(["weekly-2", "standup"]);+ });++ it("skips paths, longer words, unknown names and built-ins", () => {+ expect(findSkillCommands("edit src/standup or /standupnow or /compact", skills)).toEqual([]);+ expect(findSkillCommands("/goal ship it /learn", [...skills, "goal", "learn"])).toEqual([]);+ });+});++describe("expandSkillCommands", () => {+ const files: Record<string, string> = { standup: "/s/standup/SKILL.md", review: "/s/review/SKILL.md" };+ const fileOf = (name: string) => files[name] ?? null;+ const skills = ["standup", "review", "off"];++ it("names every skill called later in a message and keeps the message as written", () => {+ const text = expandSkillCommands("look at the diff, /review it and write /standup notes", skills, fileOf);+ expect(text).toContain(`your review skill with /review (read ${JSON.stringify(files.review)})`);+ expect(text).toContain(`your standup skill with /standup (read ${JSON.stringify(files.standup)})`);+ expect(text.endsWith("Message: look at the diff, /review it and write /standup notes")).toBe(true);+ });++ it("keeps the single-skill wording for a message that starts with one, and adds the others it calls", () => {+ const text = expandSkillCommands("/standup the billing work, then /review it", skills, fileOf);+ expect(text).toContain(`The user ran your standup skill with /standup. Read ${JSON.stringify(files.standup)}`);+ expect(text).toContain("Request: the billing work, then /review it");+ expect(text).toContain(`The request also calls your review skill with /review (read ${JSON.stringify(files.review)})`);+ expect(expandSkillCommands("/standup the billing work", skills, fileOf)).not.toContain("also calls");+ });++ it("leaves a message alone when no called skill has a file", () => {+ expect(expandSkillCommands("please /off now", skills, fileOf)).toBe("please /off now");+ expect(expandSkillCommands("no skills here", skills, fileOf)).toBe("no skills here");+ });+});++describe("expandSkillCommandTurnText later in a message", () => {+ it("points the engine at an enabled skill called mid-sentence", () => {+ const bot = botWith("standup");+ const file = join(workspaceDir(bot), "skills", "standup", "SKILL.md");+ const text = expandSkillCommandTurnText(bot, "after lunch, /standup the billing work");+ expect(text).toContain(`read ${JSON.stringify(file)}`);+ expect(text).toContain("Message: after lunch, /standup the billing work");+ const off = botWith("standup", false);+ expect(expandSkillCommandTurnText(off, "after lunch, /standup")).toBe("after lunch, /standup");+ });+});diff --git a/server/skills.ts b/server/skills.tsindex b2a016e6bb..30ecb89e82 100644--- a/server/skills.ts+++ b/server/skills.ts@@ -33,7 +33,7 @@ import {} from "./skill-storage.ts";import { workspaceDir } from "./workspace.ts";import { isSkillName, parseSkillMd, scanSkillText, SKILL_FILE_MAX_BYTES, type ParsedSkill } from "../shared/skill-md.ts";-import { parseSkillCommand } from "../shared/skill-command.ts";+import { expandSkillCommands, mayCallSkill } from "../shared/skill-command.ts";export {DESCRIPTION_MAX,@@ -1306,16 +1306,9 @@ export function enabledSkillFile(botId: string, name: string, assignedLibrary?:}export function expandSkillCommandTurnText(botId: string, text: string, assignedLibrary?: readonly string[]): string {- if (!text.trimStart().startsWith("/")) return text;+ if (!mayCallSkill(text)) return text;const names = resolveBotSkills(botId, assignedLibrary).filter((skill) => skill.enabled).map((skill) => skill.name);- const command = parseSkillCommand(text, names);- if (!command) return text;- const file = enabledSkillFile(botId, command.name, assignedLibrary);- if (!file) return text;- const lead = `The user ran your ${command.name} skill with /${command.name}. Read ${JSON.stringify(file)} with your file tools and follow it for this turn.`;- return command.request- ? `${lead} Where the skill says {input}, use the request.\n\nRequest: ${command.request}`- : `${lead} There is no further request, so run it as the skill describes.`;+ return expandSkillCommands(text, names, (name) => enabledSkillFile(botId, name, assignedLibrary));}export function resolveBotSkills(botId: string, assignedLibrary: readonly string[] | undefined): SkillListing[] {diff --git a/shared/skill-command.ts b/shared/skill-command.tsindex c4cfea6580..b1acc227c4 100644--- a/shared/skill-command.ts+++ b/shared/skill-command.ts@@ -30,3 +30,48 @@ export function parseSkillCommand(text: string, skills: Iterable<string>): Skillexport function offersSkillCommand(name: string): boolean {return isSkillName(name) && !BUILT_IN_SLASH_COMMANDS.includes(name);}++/** Each `/name` in `text` that is one of `skills` and stands as its own word: at the start or after a space or+ * line break, and followed by a space, the end, or punctuation. In order, once each. A path such as `src/standup`+ * or a longer word such as `/standupnow` is not a call. */+export function findSkillCommands(text: string, skills: Iterable<string>): string[] {+ const known = new Set([...skills].filter(offersSkillCommand));+ const found: string[] = [];+ for (const match of text.matchAll(/(?<=^|\s)\/([a-z0-9-]+)(?=$|\s|[.,;:!?)])/g)) {+ const name = match[1]!;+ if (known.has(name) && !found.includes(name)) found.push(name);+ }+ return found;+}++/** Whether `text` has a `/` that could start a skill call, checked before the skills are listed. */+export function mayCallSkill(text: string): boolean {+ return /(?:^|\s)\//.test(text);+}++/**+ * The text the engine reads for a message that calls skills with `/name`. A message that starts with one keeps+ * the single-skill wording; skills called later in it, or anywhere in a message that starts with plain text, are+ * listed with their SKILL.md and the message follows as written. `fileOf` gives an enabled skill's SKILL.md, or+ * null. A message that calls no enabled skill comes back unchanged.+ */+export function expandSkillCommands(text: string, skills: readonly string[], fileOf: (name: string) => string | null): string {+ const command = parseSkillCommand(text, skills);+ const leadFile = command ? fileOf(command.name) : null;+ const called = findSkillCommands(command && leadFile ? command.request : text, skills)+ .filter((name) => name !== command?.name || !leadFile)+ .flatMap((name) => {+ const file = fileOf(name);+ return file ? [{ name, file }] : [];+ });+ const reads = called.map(({ name, file }) => `your ${name} skill with /${name} (read ${JSON.stringify(file)})`).join(", ");+ if (command && leadFile) {+ const lead = `The user ran your ${command.name} skill with /${command.name}. Read ${JSON.stringify(leadFile)} with your file tools and follow it for this turn.`;+ const also = called.length > 0 ? `\n\nThe request also calls ${reads}. Read each with your file tools and follow it where the request calls it.` : "";+ return command.request+ ? `${lead} Where the skill says {input}, use the request.\n\nRequest: ${command.request}${also}`+ : `${lead} There is no further request, so run it as the skill describes.`;+ }+ if (called.length === 0) return text;+ return `The user called ${reads} in their message. Read each with your file tools and follow it where the message calls it, for this turn. Where a skill says {input}, use what the message asks of it.\n\nMessage: ${text}`;+}diff --git a/src/components/Composer.tsx b/src/components/Composer.tsxindex a412785f8e..90e898007a 100644--- a/src/components/Composer.tsx+++ b/src/components/Composer.tsx@@ -302,13 +302,14 @@ export function Composer({const commandCandidates = useMemo(() => {if (!slash || slash.start === dismissedSlashAt) return [];const available: ComposerSlashCommand[] = [];- if (!group?.dm) available.push({+ // Later in a message only skills run: /goal, /learn and /setup are read at its start.+ if (slash.atStart && !group?.dm) available.push({kind: "command",id: "goal",label: "/goal",description: t(group ? "composer.command.goalDesc" : "composer.command.goalBotDesc"),});- if (canLearn) {+ if (slash.atStart && canLearn) {available.push({kind: "command",id: "learn",@@ -316,7 +317,7 @@ export function Composer({description: t("composer.command.learnDesc"),});}- if (!group && supportsAgents(state.instances, bot)) available.push({+ if (slash.atStart && !group && supportsAgents(state.instances, bot)) available.push({kind: "command",id: "setup",label: "/setup",diff --git a/src/lib/composer-commands.test.ts b/src/lib/composer-commands.test.tsindex 18f89686f1..04d66b9fab 100644--- a/src/lib/composer-commands.test.ts+++ b/src/lib/composer-commands.test.ts@@ -10,13 +10,25 @@ import {import { enabledSlashSkills } from "./use-slash-skills";describe("composer slash commands", () => {- it("opens command search only for the first unfinished token", () => {- expect(composerSlashTrigger("/", 1)).toEqual({ query: "", start: 0, end: 1 });- expect(composerSlashTrigger("/go", 3)).toEqual({ query: "go", start: 0, end: 3 });- expect(composerSlashTrigger("hello /go", 9)).toBeNull();+ it("opens command search for the unfinished token at the start", () => {+ expect(composerSlashTrigger("/", 1)).toEqual({ query: "", start: 0, end: 1, atStart: true });+ expect(composerSlashTrigger("/go", 3)).toEqual({ query: "go", start: 0, end: 3, atStart: true });+ expect(composerSlashTrigger(" /go", 5)).toEqual({ query: "go", start: 2, end: 5, atStart: true });expect(composerSlashTrigger("/goal write", 11)).toBeNull();});+ it("opens skill search for a token later in the message, after a space or a line break", () => {+ expect(composerSlashTrigger("hello /go", 9)).toEqual({ query: "go", start: 6, end: 9, atStart: false });+ expect(composerSlashTrigger("/standup billing\n/rev", 21)).toEqual({ query: "rev", start: 17, end: 21, atStart: false });+ expect(composerSlashTrigger("hello /go there", 9)).toEqual({ query: "go", start: 6, end: 9, atStart: false });+ });++ it("does not open inside a path, a link or a finished word", () => {+ expect(composerSlashTrigger("src/standup", 11)).toBeNull();+ expect(composerSlashTrigger("see https://example.com/a", 25)).toBeNull();+ expect(composerSlashTrigger("hello /go ", 10)).toBeNull();+ });+it("replaces the active token without losing text after the caret", () => {expect(replaceComposerSlashTrigger("/go later", { query: "go", start: 0, end: 3 }, ""),@@ -44,7 +56,7 @@ describe("composer slash commands", () => {});it("opens on a skill name with digits", () => {- expect(composerSlashTrigger("/weekly-2", 9)).toEqual({ query: "weekly-2", start: 0, end: 9 });+ expect(composerSlashTrigger("/weekly-2", 9)).toEqual({ query: "weekly-2", start: 0, end: 9, atStart: true });});it("lists enabled skills as rows after the built-ins, never shadowing one", () => {diff --git a/src/lib/composer-commands.ts b/src/lib/composer-commands.tsindex 33b8b14f81..f3afad8cbe 100644--- a/src/lib/composer-commands.ts+++ b/src/lib/composer-commands.ts@@ -27,14 +27,18 @@ export interface ComposerSlashTrigger {query: string;start: number;end: number;+ atStart: boolean;}+/** The `/word` the caret is in, at the start of the text or after a space or line break, so skills can be called+ * anywhere in a message. `atStart` is whether only spaces come before it: the built-in commands work only there. */export function composerSlashTrigger(text: string, caretInput: number): ComposerSlashTrigger | null {const caret = Math.max(0, Math.min(text.length, Math.floor(caretInput)));const prefix = text.slice(0, caret);- const match = /^\/([a-z0-9-]*)$/i.exec(prefix);+ const match = /(^|\s)\/([a-z0-9-]*)$/i.exec(prefix);if (!match) return null;- return { query: match[1] ?? "", start: 0, end: caret };+ const start = match.index + (match[1] ?? "").length;+ return { query: match[2] ?? "", start, end: caret, atStart: prefix.slice(0, start).trim() === "" };}export function goalTextFromComposer(text: string): string | null {@@ -44,7 +48,7 @@ export function goalTextFromComposer(text: string): string | null {export function replaceComposerSlashTrigger(text: string,- trigger: ComposerSlashTrigger,+ trigger: Omit<ComposerSlashTrigger, "atStart">,replacement: string,): { text: string; caret: number } {const next = `${text.slice(0, trigger.start)}${replacement}${text.slice(trigger.end)}`;