Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Move the skill switch card code out of server/index.ts
AdityaUmaled59aade33dc0
diff --git a/server/host/skill-switch-host.ts b/server/host/skill-switch-host.tsnew file mode 100644index 0000000000..f9a871a321--- /dev/null+++ b/server/host/skill-switch-host.ts@@ -0,0 +1,136 @@+// A bot asks to turn one of its skills on or off, or to delete it. That always waits for the person on a card, even with+// Full Access, and approval applies only if the skill is still the one the card showed (its SKILL.md sha256).+import { randomUUID } from "node:crypto";+import type { SkillRequestCardData, SkillSwitchAction } from "../../shared/skill-request.ts";+import type { OptionCardData } from "../../shared/wire.ts";+import { appendDecision } from "../decision-log.ts";+import { setImportedSkillEnabled } from "../imported-skills.ts";+import { listSkills, readSkillFile, removeSkill, setSkillEnabled } from "../skills.ts";+import { skillSwitchCardCopy, skillSwitchStillMatches, skillSwitchTarget, type SkillSwitchTarget, type SwitchableSkill } from "../skill-switch.ts";+import type { BotRecord, Store } from "../store.ts";++export interface SkillSwitchHostDeps {+ store: Pick<Store, "bot" | "appendMessage" | "patchMessage">;+ dataDir: string;+ /** The open-card checks every skill card passes; a switch card opens even with Full Access. */+ persistence(botId: string, threadId: string): { ok: true } | { ok: false; status: number; error: string };+ listBotSkills(bot: BotRecord): Array<{ name: string; enabled: boolean; source: string; description: string }>;+ readBotSkillFile(bot: BotRecord, name: string): string | null;+ noteSkillChange(botId: string, threadId: string): void;+}++export interface SkillSwitchResolveArgs {+ botId: string;+ botName?: string;+ threadId: string;+ requestId: string;+ behavior: "allow" | "deny" | "answer";+ reviewedSha256?: string;+}++export type SkillSwitchResolution =+ | { claimed: true; status: number; error: string }+ | { claimed: true; outcome: "allowed-once" | "rejected"; alreadySettled?: true };++export function createSkillSwitchHost(deps: SkillSwitchHostDeps) {+ const { store } = deps;++ /** The skill a bot asked to turn on, turn off or delete, as it is now. */+ function switchableSkill(bot: BotRecord, name: string): SwitchableSkill | undefined {+ const own = listSkills(bot.id).find((skill) => skill.name === name);+ // Org skills an Admin pushed stay the person's to change, like library skills.+ if (own) return { kind: own.source.startsWith("org:") ? "fixed" : "own", enabled: own.enabled, text: readSkillFile(bot.id, name), source: own.source, description: own.description };+ const seen = deps.listBotSkills(bot).find((skill) => skill.name === name);+ if (!seen) return undefined;+ const imported = (seen as { origin?: string }).origin === "imported";+ return { kind: imported ? "imported" : "fixed", enabled: seen.enabled, text: imported ? deps.readBotSkillFile(bot, name) : null, source: seen.source, description: seen.description };+ }++ function appendCard(args: { botId: string; threadId: string; action: SkillSwitchAction; name: string; target: SkillSwitchTarget }): { requestId: string; summary: string } {+ const requestId = randomUUID();+ const copy = skillSwitchCardCopy(args.action, args.name, args.target.gist);+ const payload: SkillRequestCardData = {+ version: 1,+ requestId,+ botId: args.botId,+ threadId: args.threadId,+ // Nothing is staged: approval binds to the current SKILL.md through sha256.+ stagedId: `switch-${requestId}`,+ action: args.action,+ name: args.name,+ gist: args.target.gist,+ ...(args.target.source ? { source: args.target.source } : {}),+ preview: args.target.preview,+ sha256: args.target.sha256,+ warnings: [],+ createdAt: Date.now(),+ };+ const from = store.bot(args.botId);+ store.appendMessage(args.threadId, {+ role: "bot",+ kind: "options",+ from: from ? { botId: from.id, name: from.name, color: from.color } : undefined,+ card: { title: copy.title, subtitle: copy.subtitle, options: [copy.option, "Deny"], requestId, tool: "stage_skill", skillRequest: payload },+ });+ return { requestId, summary: copy.title };+ }++ function apply(botId: string, name: string, action: SkillSwitchAction, kind: SwitchableSkill["kind"] | undefined) {+ if (action === "delete") return removeSkill(botId, name);+ return (kind === "imported" ? setImportedSkillEnabled : setSkillEnabled)(botId, name, action === "enable");+ }++ /** Turning a skill on or off, or deleting it, always waits for the person, even with Full Access. */+ function stage(from: BotRecord, threadId: string, action: SkillSwitchAction, body: Record<string, unknown>): { status: number; body: Record<string, unknown> } {+ const name = typeof body.skill_name === "string" ? body.skill_name.trim() : "";+ if (!name) return { status: 400, body: { error: `skill_name is required when action is ${action}` } };+ const persistence = deps.persistence(from.id, threadId);+ if (!persistence.ok) return { status: persistence.status, body: { error: persistence.error } };+ const target = skillSwitchTarget(name, action, switchableSkill(from, name));+ if ("error" in target) return { status: 422, body: { error: target.error } };+ const card = appendCard({ botId: from.id, threadId, action, name, target });+ appendDecision(deps.dataDir, { threadId, requestId: card.requestId, botId: from.id, botName: from.name,+ tool: "stage_skill", summary: card.summary, decision: "card-shown", source: "skill" });+ return { status: 201, body: { state: "pending", name, action, gist: target.gist, warnings: [], summary: card.summary } };+ }++ function resolve(+ args: SkillSwitchResolveArgs,+ messageId: string,+ card: OptionCardData,+ request: SkillRequestCardData & { action: SkillSwitchAction },+ ): SkillSwitchResolution {+ if (card.answered || card.dismissed) {+ return { claimed: true, outcome: card.answered === "allow" ? "allowed-once" : "rejected", alreadySettled: true };+ }+ const decided = (decision: "user-approved" | "user-denied") => appendDecision(deps.dataDir, { threadId: args.threadId,+ requestId: args.requestId, botId: args.botId, botName: args.botName, tool: card.tool, summary: card.subtitle, decision, source: "user" });+ if (args.behavior !== "allow") {+ store.patchMessage(args.threadId, messageId, { card: { ...card, answered: "deny", dismissed: true, held: undefined } });+ decided("user-denied");+ return { claimed: true, outcome: "rejected" };+ }+ if (!request.sha256 || args.reviewedSha256 !== request.sha256) {+ return { claimed: true, status: 409, error: "reviewedSha256 must match the skill shown on the approval card" };+ }+ if (request.requestId !== args.requestId || request.threadId !== args.threadId) {+ return { claimed: true, status: 422, error: "this approval card no longer matches its skill" };+ }+ const bot = store.bot(args.botId);+ const current = bot ? switchableSkill(bot, request.name) : undefined;+ const still = skillSwitchStillMatches(request.name, request.action, current, request.sha256);+ const applied = "error" in still ? still : apply(args.botId, request.name, request.action, current?.kind);+ if ("error" in applied) {+ store.patchMessage(args.threadId, messageId, { card: { ...card, held: applied.error } });+ return { claimed: true, status: 422, error: applied.error };+ }+ store.patchMessage(args.threadId, messageId, {+ card: { ...card, answered: "allow", held: undefined, title: skillSwitchCardCopy(request.action, request.name, "").done },+ });+ deps.noteSkillChange(args.botId, args.threadId);+ decided("user-approved");+ return { claimed: true, outcome: "allowed-once" };+ }++ return { stage, resolve };+}diff --git a/server/index.ts b/server/index.tsindex d2289eeafe..1ad7bde913 100644--- a/server/index.ts+++ b/server/index.ts@@ -99,7 +99,7 @@ import { createDataRoutes } from "./data/routes.ts";import { DataSheetRegistry } from "./data/sheet.ts";import { DataFailure } from "./data/types.ts";import { TeamComputers, teamComputerOwner, type TeamComputerRecord } from "./team-computers.ts";-import { isEffortLevel, type CardAnswerer, type OptionCardData, type ResolvedSender, type SignInRequestCardData, type WireBot, type WireGroup, type WireTask } from "../shared/wire.ts";+import { isEffortLevel, type CardAnswerer, type ResolvedSender, type SignInRequestCardData, type WireBot, type WireGroup, type WireTask } from "../shared/wire.ts";import { isPersistentQuestionCard, shouldSettleRequestCard } from "../shared/ask-question.ts";import { parseToolScope } from "../shared/tool-scope.ts";import type { TeamComputersPayload } from "../shared/team-computer.ts";@@ -384,9 +384,7 @@ import { pickBotName } from "./names.ts";import { botSkillTemplateSchema, newBotDefaultsSchema } from "./new-bot-defaults.ts";import { expandLearnTurnText, learnSource } from "./skill-learn.ts";import { connectedAppsForSetup, expandSetupTurnText, setupModeActive, setupSystemPrompt, type SetupConnectedApps } from "./setup-mode.ts"; import { forgetSetupPresser, setupPresser, setupTurnText, takeSetupRequest } from "./setup-request.ts"; import { setupSuggestionsFor } from "./setup-suggestions.ts"; import { createSetupRequestRoutes } from "./routes/setup-request.ts";-import { isSkillSwitchAction, type SkillRequestCardData, type SkillSwitchAction } from "../shared/skill-request.ts";-import { setImportedSkillEnabled } from "./imported-skills.ts";-import { skillSwitchCardCopy, skillSwitchStillMatches, skillSwitchTarget, type SwitchableSkill, type SkillSwitchTarget } from "./skill-switch.ts";+import { isSkillSwitchAction, type SkillRequestCardData } from "../shared/skill-request.ts"; import { createSkillSwitchHost } from "./host/skill-switch-host.ts";import { checkSoulDrift, readSoulDrift, soulFile, writeSoulMirror } from "./bot-folder.ts";import {botPersona,@@ -9571,9 +9569,7 @@ function undoSkillCard(botId: string, threadId: string, messageId: string): Undoif (request.botId !== botId || request.threadId !== threadId) {return { claimed: true, state: "invalid", error: "this skill request belongs to a different bot", status: 403 };}- const action = request.action;- // A switch or delete always waited for the person, so it never has an undo.- if (isSkillSwitchAction(action)) return { claimed: true, state: "invalid", error: "This change was approved on its card and can't be undone here.", status: 409 };+ const action = request.action; if (isSkillSwitchAction(action)) return { claimed: true, state: "invalid", error: "This change was approved on its card and can't be undone here.", status: 409 };if (request.shared) return skillsHost.undoReceipt(botId, threadId, message, { ...request, action, shared: request.shared });const undone = undoSkillWrite(botId, {stagedId: request.stagedId,@@ -11455,103 +11451,7 @@ function appendSkillRequestCard(args: {};}-/** The skill a bot asked to turn on, turn off or delete, as it is now. */-function switchableSkill(bot: BotRecord, name: string): SwitchableSkill | undefined {- const own = listSkills(bot.id).find((skill) => skill.name === name);- // Org skills an Admin pushed stay the person's to change, like library skills.- if (own) return { kind: own.source.startsWith("org:") ? "fixed" : "own", enabled: own.enabled, text: readSkillFile(bot.id, name), source: own.source, description: own.description };- const seen = listBotSkills(bot).find((skill) => skill.name === name);- if (!seen) return undefined;- const imported = (seen as { origin?: string }).origin === "imported";- return { kind: imported ? "imported" : "fixed", enabled: seen.enabled, text: imported ? readBotSkillFile(bot, name) : null, source: seen.source, description: seen.description };-}--/** Turning a skill on or off, or deleting it, always waits for the person, even with Full Access. */-function stageSkillSwitch(res: ServerResponse, from: BotRecord, threadId: string, action: SkillSwitchAction, body: Record<string, unknown>) {- const name = typeof body.skill_name === "string" ? body.skill_name.trim() : "";- if (!name) return json(res, 400, { error: `skill_name is required when action is ${action}` });- const persistence = skillProposalPersistence(from.id, threadId, true, true);- if (!persistence.ok) return json(res, persistence.status, { error: persistence.error });- const target = skillSwitchTarget(name, action, switchableSkill(from, name));- if ("error" in target) return json(res, 422, { error: target.error });- const card = appendSkillSwitchCard({ botId: from.id, threadId, action, name, target });- appendDecision(DATA_DIR, { threadId, requestId: card.requestId, botId: from.id, botName: from.name,- tool: "stage_skill", summary: card.summary, decision: "card-shown", source: "skill" });- return json(res, 201, { state: "pending", name, action, gist: target.gist, warnings: [], summary: card.summary });-}--function appendSkillSwitchCard(args: { botId: string; threadId: string; action: SkillSwitchAction; name: string; target: SkillSwitchTarget }): { requestId: string; summary: string } {- const requestId = randomUUID();- const copy = skillSwitchCardCopy(args.action, args.name, args.target.gist);- const payload: SkillRequestCardData = {- version: 1,- requestId,- botId: args.botId,- threadId: args.threadId,- // Nothing is staged: approval binds to the current SKILL.md through sha256.- stagedId: `switch-${requestId}`,- action: args.action,- name: args.name,- gist: args.target.gist,- ...(args.target.source ? { source: args.target.source } : {}),- preview: args.target.preview,- sha256: args.target.sha256,- warnings: [],- createdAt: Date.now(),- };- const from = store.bot(args.botId);- store.appendMessage(args.threadId, {- role: "bot",- kind: "options",- from: from ? { botId: from.id, name: from.name, color: from.color } : undefined,- card: { title: copy.title, subtitle: copy.subtitle, options: [copy.option, "Deny"], requestId, tool: "stage_skill", skillRequest: payload },- });- return { requestId, summary: copy.title };-}--function applySkillSwitch(botId: string, name: string, action: SkillSwitchAction, kind: SwitchableSkill["kind"] | undefined) {- if (action === "delete") return removeSkill(botId, name);- return (kind === "imported" ? setImportedSkillEnabled : setSkillEnabled)(botId, name, action === "enable");-}--function resolveSkillSwitch(- args: Parameters<typeof resolveSkillRequest>[0],- messageId: string,- card: OptionCardData,- request: SkillRequestCardData & { action: SkillSwitchAction },-): ReturnType<typeof resolveSkillRequest> {- if (card.answered || card.dismissed) {- return { claimed: true, outcome: card.answered === "allow" ? "allowed-once" : "rejected", alreadySettled: true };- }- const decided = (decision: "user-approved" | "user-denied") => appendDecision(DATA_DIR, { threadId: args.threadId,- requestId: args.requestId, botId: args.botId, botName: args.botName, tool: card.tool, summary: card.subtitle, decision, source: "user" });- if (args.behavior !== "allow") {- store.patchMessage(args.threadId, messageId, { card: { ...card, answered: "deny", dismissed: true, held: undefined } });- decided("user-denied");- return { claimed: true, outcome: "rejected" };- }- if (!request.sha256 || args.reviewedSha256 !== request.sha256) {- return { claimed: true, status: 409, error: "reviewedSha256 must match the skill shown on the approval card" };- }- if (request.requestId !== args.requestId || request.threadId !== args.threadId) {- return { claimed: true, status: 422, error: "this approval card no longer matches its skill" };- }- const bot = store.bot(args.botId);- const current = bot ? switchableSkill(bot, request.name) : undefined;- const still = skillSwitchStillMatches(request.name, request.action, current, request.sha256);- const applied = "error" in still ? still : applySkillSwitch(args.botId, request.name, request.action, current?.kind);- if ("error" in applied) {- store.patchMessage(args.threadId, messageId, { card: { ...card, held: applied.error } });- return { claimed: true, status: 422, error: applied.error };- }- store.patchMessage(args.threadId, messageId, {- card: { ...card, answered: "allow", held: undefined, title: skillSwitchCardCopy(request.action, request.name, "").done },- });- skillReview.noteSkillChange(args.botId, args.threadId);- decided("user-approved");- return { claimed: true, outcome: "allowed-once" };-}-+const skillSwitch = createSkillSwitchHost({ store, dataDir: DATA_DIR, persistence: (botId, threadId) => skillProposalPersistence(botId, threadId, true, true), listBotSkills, readBotSkillFile, noteSkillChange: (botId, threadId) => skillReview.noteSkillChange(botId, threadId) });function resolveSkillRequest(args: {botId: string;botName?: string;@@ -11572,7 +11472,7 @@ function resolveSkillRequest(args: {if (request.botId !== args.botId) {return { claimed: true, status: 403, error: "this skill request belongs to a different bot" };}- if (isSkillSwitchAction(request.action)) return resolveSkillSwitch(args, message.id, card, { ...request, action: request.action });+ if (isSkillSwitchAction(request.action)) return skillSwitch.resolve(args, message.id, card, { ...request, action: request.action });if (card.answered || card.dismissed) {const cleanup = rejectStagedSkillWrite(args.botId, request.stagedId);if ("applied" in cleanup && cleanup.applied && card.answered !== "allow") {@@ -13925,7 +13825,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {if (!connectorThread(from.id, fromThreadId)) {return json(res, 403, { error: "source conversation does not belong to sender" });}- if (isSkillSwitchAction(body.action)) return stageSkillSwitch(res, from, fromThreadId, body.action, body);+ if (isSkillSwitchAction(body.action)) { const staged = skillSwitch.stage(from, fromThreadId, body.action, body); return json(res, staged.status, staged.body); }const direct = appliesDirectly(from.id, fromThreadId);const persistence = skillProposalPersistence(from.id, fromThreadId, !direct);if (!persistence.ok) return json(res, persistence.status, { error: persistence.error });