Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Start new threads on the last pick only while its engine can run
milind-soni580ed23e2bbb
diff --git a/server/host/own-threads.ts b/server/host/own-threads.tsindex 8a3bed319d..a8a15b80d4 100644--- a/server/host/own-threads.ts+++ b/server/host/own-threads.ts@@ -71,6 +71,13 @@ export function createOwnThreads(deps: Pick<SpawnLevelDeps, "sourceLevel" | "mayconst approvalMode = levelFor(bot, modelSelection, { botId, threadId: parentThreadId });return { ...(modelSelection ? { modelSelection } : {}), ...(approvalMode ? { approvalMode } : {}) };},+ /** A thread a person opens on `bot`: on its owner's latest thread pick, at a level that engine carries, while it can run. */+ startsOn(bot: BotRecord): { modelSelection?: ModelSelection; approvalMode?: NewThreadLevel } {+ const picked = bot.newThreadModel;+ if (!picked || deps.givesWay(picked, bot.modelSelection)) return {};+ const approvalMode = levelFor(bot, picked);+ return { modelSelection: picked, ...(approvalMode ? { approvalMode } : {}) };+ },name(requester: Omit<Placed, "threadId">, child: Placed): string | undefined {return own(requester, child) ? `#${store.taskByThread(child.botId, child.threadId)?.title ?? "Thread"}` : undefined;},diff --git a/server/index.ts b/server/index.tsindex 70a6985afc..800c382d07 100644--- a/server/index.ts+++ b/server/index.ts@@ -12088,7 +12088,7 @@ const taskRoutes = createTaskRoutes({cloudThreadRefusal,sharedWorkspaceFullAccessEnabled,supportsApprovalMode,- threadBusy, broadcast, notificationListed, newThreadLevel: (bot, selection) => ownThreads.levelFor(bot, selection),+ threadBusy, broadcast, notificationListed, newThreadStart: ownThreads.startsOn,publicBot,wireBot,wireTask,diff --git a/server/routes/tasks.ts b/server/routes/tasks.tsindex a36a3e12ad..794d600832 100644--- a/server/routes/tasks.ts+++ b/server/routes/tasks.ts@@ -77,8 +77,8 @@ export interface TaskRouteDeps {rejectDeletedThreadSkillStages(cleanups: Array<{ botId: string; stagedId: string }>): void;/** A person opened this conversation (a Team Cloud's Needs you lines for them there are done). */threadRead?(auth: RequestAuth, threadId: string): void;- /** "ask" when a new thread of `bot` on `selection` must start at Ask: its engine does not carry the bot's level. */- newThreadLevel(bot: BotRecord, selection: ModelSelection): NewThreadLevel | undefined;+ /** The model, and level where it is not the bot's, of a thread a person opens on `bot` (host/own-threads.ts). */+ newThreadStart(bot: BotRecord): { modelSelection?: ModelSelection; approvalMode?: NewThreadLevel };/** A conversation's model changed (host/no-terminal.ts); `routinesFollow`: the bot's own model changed with it. */modelSwitched?(botId: string, threadId: string, before: ModelSelection, after: ModelSelection, routinesFollow: boolean): void;}@@ -218,8 +218,8 @@ export function createTaskRoutes(deps: TaskRouteDeps): RouteHandler & { deleteTaif (body.projectId !== undefined && (typeof body.projectId !== "string" || !store.project(bot.id, body.projectId))) {return json(res, 400, { error: "projectId must belong to this bot" });}- const startsOn = bot.newThreadModel && checkedModelSelection(bot.newThreadModel).ok ? bot.newThreadModel : undefined;- const level = startsOn && deps.newThreadLevel(bot, startsOn) === "ask" ? "ask" : body.approvalMode;+ const start = deps.newThreadStart(bot), startsOn = start.modelSelection && checkedModelSelection(start.modelSelection).ok ? start.modelSelection : undefined;+ const level = startsOn && start.approvalMode === "ask" ? "ask" : body.approvalMode;// Who opened it decides who may answer its cards on a shared workspace, and on a team who sees it at all.const task = store.createTask(bot.id, typeof body.title === "string" ? body.title : undefined, body.select !== false, body.projectId, undefined, level, startsOn, cwd,auth.kind === "session" ? (created) => threadStarters.set(created.threadId, actorKey(auth)) : undefined);diff --git a/server/thread-only-model-pick.e2e.test.ts b/server/thread-only-model-pick.e2e.test.tsindex 08cec25f10..29183d8137 100644--- a/server/thread-only-model-pick.e2e.test.ts+++ b/server/thread-only-model-pick.e2e.test.ts@@ -7,7 +7,7 @@ const haiku = { instanceId: "claude", model: "claude-haiku-4-5" };let session: VerificationServer;beforeAll(async () => {- session = await launchVerificationServer(process.env, undefined, undefined, undefined, undefined, undefined, ["kimi"]);+ session = await launchVerificationServer(process.env, undefined, undefined, undefined, undefined, undefined, ["codex"]);}, 60_000);afterAll(async () => { await session?.close(); });@@ -80,6 +80,20 @@ it("changes the bot's model, and every thread that follows it, only from the botawait ok("PATCH", `/api/bots/${botId}`, { modelSelection: opus });expect((await profile(botId)).newThreadModel).toBeUndefined();expect(await thread(botId, await newThread(botId, "After settings"))).toMatchObject({ modelSelection: opus, followsBotModel: true });++ await ok("PATCH", `/api/bots/${botId}/tasks/${pinned}`, { modelSelection: haiku });+ await ok("PATCH", `/api/bots/${botId}/model`, sonnet);+ expect(await profile(botId)).toMatchObject({ modelSelection: sonnet });+ expect((await profile(botId)).newThreadModel).toBeUndefined();+}, 60_000);++it("starts a new thread on the bot's model while the last pick's engine cannot run", async () => {+ const botId = await newBot("Gone engine");+ const threadId = (await profile(botId)).threadId as string;+ const gone = { instanceId: "no-such-engine", model: "old-model" };+ await ok("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: gone, resetApprovalToAsk: true });+ expect((await profile(botId)).newThreadModel).toEqual(gone);+ expect(await thread(botId, await newThread(botId, "Still works"))).toMatchObject({ modelSelection: sonnet, followsBotModel: true });}, 60_000);it("never lets a chat-only session move where new threads start", async () => {@@ -99,10 +113,10 @@ it("starts a new thread on a picked engine its bot's level does not carry to atconst threadId = (await profile(botId)).threadId as string;await ok("PATCH", `/api/bots/${botId}`, { approvalMode: "edits" });expect((await profile(botId)).approvalMode).toBe("edits");- const kimi = { instanceId: "kimi", model: (await ok("GET", "/api/instances")).instances.find((entry: any) => entry.instanceId === "kimi").models.default };- await ok("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: kimi, resetApprovalToAsk: true });- expect((await profile(botId)).newThreadModel).toEqual(kimi);- const started = await thread(botId, await newThread(botId, "On Kimi"));- expect(started).toMatchObject({ modelSelection: kimi, followsBotModel: false, approvalMode: "ask" });+ const codex = { instanceId: "codex", model: (await ok("GET", "/api/instances")).instances.find((entry: any) => entry.instanceId === "codex").models.default };+ await ok("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: codex, resetApprovalToAsk: true });+ expect((await profile(botId)).newThreadModel).toEqual(codex);+ const started = await thread(botId, await newThread(botId, "On Codex"));+ expect(started).toMatchObject({ modelSelection: codex, followsBotModel: false, approvalMode: "ask" });expect((await profile(botId)).approvalMode).toBe("edits");}, 60_000);diff --git a/src/state/store.test.ts b/src/state/store.test.tsindex 3b3ca89019..acaed6bd32 100644--- a/src/state/store.test.ts+++ b/src/state/store.test.ts@@ -208,6 +208,14 @@ describe("independent bot threads", () => {expect(open.bots[0]?.onlyPeopleAdded).toBeUndefined();});+ it("a frame without newThreadModel clears it, so a blank chat is back on the bot's model", () => {+ const picked = reducer(start(), { type: "botPatched", bot: { ...bot, newThreadModel: { instanceId: "claude", model: "opus" } } });+ expect(picked.bots[0]?.newThreadModel).toEqual({ instanceId: "claude", model: "opus" });+ expect(reducer(picked, { type: "botPatched", bot: { ...bot } }).bots[0]?.newThreadModel).toBeUndefined();+ const full = { ...bot, threadId: "second", activeLeafId: null, tasks: bot.tasks!.slice(1), messages: [] };+ expect(reducer(picked, { type: "botPatched", bot: full }).bots[0]?.newThreadModel).toBeUndefined();+ });+it("a frame without liveVoice clears the bot's own call voice", () => {const voiced = reducer(start(), { type: "botPatched", bot: { ...bot, liveVoice: "marin" } });expect(voiced.bots[0]?.liveVoice).toBe("marin");diff --git a/src/state/store.tsx b/src/state/store.tsxindex b005d5c8ca..03d693fe72 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -1144,7 +1144,7 @@ function reduceApp(state: AppState, action: Action): AppState {if ((switchedThread || (before.awaitingThreadSnapshot && action.bot.threadId === before.threadId)) &&Array.isArray(action.bot.messages)) {// The slim deletion broadcast can beat the full snapshot; later duplicate snapshots must not overwrite newer messages.- return reducer(switching, { type: "taskSwitched", bot: { ...before, ...action.bot, computer: action.bot.computer, section: action.bot.section, toolScope: action.bot.toolScope, managedSections: action.bot.managedSections, messages: action.bot.messages, browserProfile: action.bot.browserProfile, vpsAlias: action.bot.vpsAlias, phoneApps: action.bot.phoneApps, onlyPeopleAdded: action.bot.onlyPeopleAdded, liveVoice: action.bot.liveVoice } });+ return reducer(switching, { type: "taskSwitched", bot: { ...before, ...action.bot, computer: action.bot.computer, section: action.bot.section, toolScope: action.bot.toolScope, managedSections: action.bot.managedSections, messages: action.bot.messages, browserProfile: action.bot.browserProfile, vpsAlias: action.bot.vpsAlias, phoneApps: action.bot.phoneApps, onlyPeopleAdded: action.bot.onlyPeopleAdded, liveVoice: action.bot.liveVoice, newThreadModel: action.bot.newThreadModel } });}const patched = updateBot(switching, action.bot.id, (b) => ({...b,@@ -1157,7 +1157,7 @@ function reduceApp(state: AppState, action: Action): AppState {computer: action.bot.computer,toolScope: action.bot.toolScope,section: action.bot.section, managedSections: action.bot.managedSections,- vpsAlias: action.bot.vpsAlias, phoneApps: action.bot.phoneApps, onlyPeopleAdded: action.bot.onlyPeopleAdded, liveVoice: action.bot.liveVoice, liveOverride: action.bot.liveOverride,+ vpsAlias: action.bot.vpsAlias, phoneApps: action.bot.phoneApps, onlyPeopleAdded: action.bot.onlyPeopleAdded, liveVoice: action.bot.liveVoice, liveOverride: action.bot.liveOverride, newThreadModel: action.bot.newThreadModel,tasks: action.bot.tasks ? mergeTaskStamps(b.tasks, action.bot.tasks) : b.tasks,// Old approvals must never be sent to the replacement thread while its transcript loads.messages: switchedThread ? [] : b.messages,@@ -1703,7 +1703,7 @@ function reduceApp(state: AppState, action: Action): AppState {...action.bot,computer: action.bot.computer,toolScope: action.bot.toolScope, managedSections: action.bot.managedSections,- vpsAlias: action.bot.vpsAlias, liveVoice: action.bot.liveVoice, liveOverride: action.bot.liveOverride,+ vpsAlias: action.bot.vpsAlias, liveVoice: action.bot.liveVoice, liveOverride: action.bot.liveOverride, newThreadModel: action.bot.newThreadModel,messages: action.bot.messages ?? [],hasMore: action.bot.hasMore,awaitingThreadSnapshot: false,