Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Add a /new composer command that opens a thread.
Shreerama9e7d05507df75
diff --git a/src/components/Composer.slash.test.ts b/src/components/Composer.slash.test.tsindex dbbfce08f9..b92254b001 100644--- a/src/components/Composer.slash.test.ts+++ b/src/components/Composer.slash.test.ts@@ -212,6 +212,48 @@ describe("the / menu in the composer", () => {expect(input.value).toBe("use /standup later");});+ it("offers /new once a conversation has started, and opens a blank thread", () => {+ const started: Bot = { ...bot, messages: [{ id: "visible-leaf", role: "user", kind: "text", text: "Earlier", at: 1 }] };+ flushSync(() => root.render(createElement(Composer, { bot: started })));+ fill("/");+ expect(labels()).toEqual(["/goal", "/learn", "/setup", "/burst", "/fork", "/new", "/archive", "/delete", "/compact", "/context", "/fork-release", "/standup"]);+ const input = fill("/new");+ press(input, "Enter");+ expect(dispatch).toHaveBeenCalledWith({ type: "newTask", botId: bot.id });+ expect(dispatch.mock.calls.some(([action]) => action.type === "send")).toBe(false);+ expect(input.value).toBe("");+ });++ it("starts that thread with the words after /new", () => {+ const started: Bot = { ...bot, messages: [{ id: "visible-leaf", role: "user", kind: "text", text: "Earlier", at: 1 }] };+ flushSync(() => root.render(createElement(Composer, { bot: started })));+ const input = fill("/new ship the notes");+ press(input, "Enter");+ expect(dispatch).toHaveBeenNthCalledWith(1, { type: "newTask", botId: bot.id });+ expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({+ type: "send", botId: bot.id, threadId: newChatThreadId(bot.id), text: "ship the notes", mode: "chat",+ }));+ expect(input.value).toBe("");+ });++ it("hides /new on a blank thread, and a bare /new leaves it", () => {+ fill("/");+ expect(labels()).not.toContain("/new");+ const input = fill("/new");+ press(input, "Enter");+ expect(dispatch).not.toHaveBeenCalled();+ expect(input.value).toBe("");+ });++ it("sends the words after /new into the blank thread already open", () => {+ const input = fill("/new ship the notes");+ press(input, "Enter");+ expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({+ type: "send", botId: bot.id, threadId: bot.threadId, text: "ship the notes", mode: "chat",+ }));+ expect(dispatch.mock.calls.some(([action]) => action.type === "newTask")).toBe(false);+ });+it("archives the conversation on /archive, with Undo", () => {const input = fill("/archive");press(input, "Enter");diff --git a/src/components/Composer.tsx b/src/components/Composer.tsxindex a35909194e..fd1c5fbe85 100644--- a/src/components/Composer.tsx+++ b/src/components/Composer.tsx@@ -306,8 +306,9 @@ export function Composer({const effectiveText = typedGoalText ?? text;// A room's threads are managed from the room, and a blank New thread has no conversation to act on yet.const threadCommandAvailable = !group && Boolean(composerTask);+ const startedThread = Boolean(bot) && threadCommandAvailable && !isNewChatThread(threadId) && visibleMessages(bot).length > 0;const { command: threadCommand, run: runThreadCommand, dialog: threadCommandDialog, contextMenu } = useComposerThreadCommand({- text, bot: threadCommandAvailable ? bot : undefined, threadId, mainThreadId: profile?.mainThreadId, threads: profile?.tasks,+ text, bot: !group ? bot : undefined, threadId, mainThreadId: profile?.mainThreadId, threads: profile?.tasks, threadActions: threadCommandAvailable, started: startedThread,title: composerTask?.title, busy, dispatch, returnFocusRef: inputRef,clearText: () => { stopDictation(); editText(""); picks.clear(); setCaret(0); },onCleared: () => { editText(""); setAttachments([]); picks.clear(); setCaret(0); setDismissedSlashAt(null); onConsumeReply?.(); setChannelMode("chat"); },@@ -345,11 +346,11 @@ export function Composer({description: t("composer.command.setupDesc"),}, { kind: "command", id: "burst", label: "/burst", description: t("composer.command.burstDesc") });if (builtIns && forkOwnerId) available.push({ kind: "command", id: "fork", label: "/fork", description: t("composer.command.forkDesc") });- if (builtIns && threadCommandAvailable && bot) available.push(...conversationCommands());+ if (builtIns && threadCommandAvailable && bot) available.push(...conversationCommands(startedThread));available.push(...skillSlashCommands(slashSkills).filter((command) => !available.some((builtIn) => builtIn.id === command.id)));return available.filter((command) => slashCommandMatches(command, slash.query)).sort((a, b) => Number(b.id === slash.query.toLowerCase()) - Number(a.id === slash.query.toLowerCase()));- }, [slash, dismissedSlashAt, group, bot, threadCommandAvailable, canLearn, state.instances, locale, slashSkills, forkOwnerId]);+ }, [slash, dismissedSlashAt, group, bot, threadCommandAvailable, startedThread, canLearn, state.instances, locale, slashSkills, forkOwnerId]);const commandPickerOpen = commandCandidates.length > 0;const mention = mentionQueryAt(text, caret); const teammates = useTeammates(); const viewer = useViewerEmail();diff --git a/src/components/use-composer-thread-command.tsx b/src/components/use-composer-thread-command.tsxindex cfe69016bb..8fe296510c 100644--- a/src/components/use-composer-thread-command.tsx+++ b/src/components/use-composer-thread-command.tsx@@ -2,14 +2,16 @@ import { useRef, useState, type RefObject } from "react";import { t } from "@/lib/i18n";import { canCompactConversation } from "@/lib/context-warning";-import { threadCommandFromComposer, type ComposerSlashCommand } from "@/lib/composer-commands";+import { newThreadPrompt, threadCommandFromComposer, type ComposerSlashCommand } from "@/lib/composer-commands";+import { newChatThreadId } from "@/state/new-chat";import { visibleMessages, type Action, type Bot } from "@/state/store";import { announceArchiveNotice } from "./bot-archive";import { ConfirmDialog } from "./ConfirmDialog";import { useContextAction } from "./ContextWarning";-export function conversationCommands(): ComposerSlashCommand[] {+export function conversationCommands(started: boolean): ComposerSlashCommand[] {return [+ ...(started ? [{ kind: "command" as const, id: "new" as const, label: "/new" as const, description: t("composer.command.newDesc") }] : []),{ kind: "command", id: "archive", label: "/archive", description: t("composer.command.archiveDesc") },{ kind: "command", id: "delete", label: "/delete", description: t("composer.command.deleteDesc") },{ kind: "command", id: "compact", label: "/compact", description: t("contextMenu.compact") },@@ -17,10 +19,14 @@ export function conversationCommands(): ComposerSlashCommand[] {];}-export function useComposerThreadCommand({ text, bot, threadId, mainThreadId, threads, title, busy, dispatch, returnFocusRef, clearText, onCleared }: {+export function useComposerThreadCommand({ text, bot, threadId, mainThreadId, threads, title, busy, dispatch, returnFocusRef, clearText, onCleared, threadActions, started }: {text: string;bot: Bot | undefined;threadId: string;+ /** Archive, delete, compact, and context need a real conversation. `/new` does not. */+ threadActions: boolean;+ /** False on a blank thread, where `/new` is hidden and a bare `/new` does nothing. */+ started: boolean;/** The conversation the person set as main, opened after this one leaves the list when it is not archived. */mainThreadId: string | undefined;/** The bot's conversations, for the next active one when there is no main to open. */@@ -33,7 +39,8 @@ export function useComposerThreadCommand({ text, bot, threadId, mainThreadId, thonCleared: () => void;}) {const botId = bot?.id;- const command = botId ? threadCommandFromComposer(text) : null;+ const parsed = botId ? threadCommandFromComposer(text) : null;+ const command = parsed === "new" || threadActions ? parsed : null;const [confirming, setConfirming] = useState(false);const contextMenu = useRef<{ open: () => void }>(null);const compact = useContextAction();@@ -48,6 +55,17 @@ export function useComposerThreadCommand({ text, bot, threadId, mainThreadId, th};const run = () => {if (!command || !bot || !botId) return;+ if (command === "new") {+ const prompt = newThreadPrompt(text);+ onCleared();+ if (!started) {+ if (prompt) dispatch({ type: "send", botId, threadId, text: prompt, mode: "chat" });+ return;+ }+ dispatch({ type: "newTask", botId });+ if (prompt) dispatch({ type: "send", botId, threadId: newChatThreadId(botId), text: prompt, mode: "chat" });+ return;+ }if (command === "context") {clearText();contextMenu.current?.open();diff --git a/src/lib/composer-commands.test.ts b/src/lib/composer-commands.test.tsindex 547a60305f..d80cee3090 100644--- a/src/lib/composer-commands.test.ts+++ b/src/lib/composer-commands.test.ts@@ -6,6 +6,7 @@ import {replaceComposerSlashTrigger,skillSlashCommands,slashCommandMatches,+ newThreadPrompt,threadCommandFromComposer,type ComposerSlashCommandId, builtInCommandText } from "./composer-commands";import { enabledSlashSkills } from "./use-slash-skills";@@ -81,6 +82,13 @@ describe("composer slash commands", () => {expect(threadCommandFromComposer("/archive this")).toBeNull();expect(threadCommandFromComposer("please /delete")).toBeNull();expect(threadCommandFromComposer("/deleted")).toBeNull();+ expect(threadCommandFromComposer("/new")).toBe("new");+ expect(threadCommandFromComposer("/NEW ")).toBe("new");+ expect(threadCommandFromComposer("/new ship the notes")).toBe("new");+ expect(newThreadPrompt("/new ship the notes")).toBe("ship the notes");+ expect(newThreadPrompt("/new")).toBe("");+ expect(threadCommandFromComposer("/newer")).toBeNull();+ expect(threadCommandFromComposer("please /new")).toBeNull();});it("offers setup as a slash command id and keeps the typed token", () => {@@ -134,6 +142,7 @@ describe("burst", () => {expect(builtInCommandText("setup")).toBe("/setup ");expect(builtInCommandText("archive")).toBe("/archive ");expect(builtInCommandText("delete")).toBe("/delete ");+ expect(builtInCommandText("new")).toBe("/new ");expect(builtInCommandText("goal")).toBe("");});});diff --git a/src/lib/composer-commands.ts b/src/lib/composer-commands.tsindex 94f7b738a7..dc719cd68c 100644--- a/src/lib/composer-commands.ts+++ b/src/lib/composer-commands.ts@@ -1,7 +1,7 @@import { blankCode, offersSkillCommand } from "../../shared/skill-command";/** A command that acts on the conversation itself instead of sending a message. */-export type ComposerThreadCommand = "archive" | "delete" | "compact" | "context";+export type ComposerThreadCommand = "archive" | "delete" | "compact" | "context" | "new";export type ComposerSlashCommandId = "goal" | "learn" | "setup" | "burst" | "fork" | ComposerThreadCommand;@@ -49,7 +49,13 @@ export function goalTextFromComposer(text: string): string | null {export function threadCommandFromComposer(text: string): ComposerThreadCommand | null {const match = /^\/(archive|delete|compact|context)\s*$/i.exec(text);- return match ? (match[1]!.toLowerCase() as ComposerThreadCommand) : null;+ if (match) return match[1]!.toLowerCase() as ComposerThreadCommand;+ return /^\/new(?:\s|$)/i.test(text.trim()) ? "new" : null;+}++/** The first message after `/new`, or empty when the command only opens a blank thread. */+export function newThreadPrompt(text: string): string {+ return (/^\/new(?:\s+([\s\S]*))?$/i.exec(text.trim())?.[1] ?? "").trim();}/** The text a picked built-in leaves in the composer; /goal only flips the composer's mode. */diff --git a/src/locales/en.json b/src/locales/en.jsonindex 987acbb6a1..f5d0cac7e3 100644--- a/src/locales/en.json+++ b/src/locales/en.json@@ -3137,6 +3137,7 @@"composer.command.setupDesc": "Have this bot interview you and set itself up","composer.command.burstDesc": "Run many parts of a job at once, then merge the results","composer.command.forkDesc": "Fork from the latest text message",+ "composer.command.newDesc": "Start a new conversation","composer.command.archiveDesc": "Archive this conversation","composer.command.deleteDesc": "Delete this conversation","composer.command.archived": "Conversation archived",