Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Rooms: a bot the first answer names keeps its skill turn on any path; unused shares are withdrawn
AdityaUmale0a758e63a56b
diff --git a/server/agents-turn.test.ts b/server/agents-turn.test.tsindex a97c73691b..5b1a87ed9d 100644--- a/server/agents-turn.test.ts+++ b/server/agents-turn.test.ts@@ -152,25 +152,43 @@ describe("what a turn that may not use a listed tool is told", () => {const claim: RoomSkillsClaim = { claimed: false };takeRoomSkills(claim, 0, "writer", true);expect(roomTurnMayWriteSkills(claim, 1, "video")).toBe(false);- handRoomSkills(claim, "video");+ handRoomSkills(claim, ["video"]);expect(roomTurnMayWriteSkills(claim, 1, "video")).toBe(true);expect(roomTurnMayWriteSkills(claim, 1, "editor")).toBe(false);takeRoomSkills(claim, 1, "video", true);expect(roomTurnMayWriteSkills(claim, 1, "video")).toBe(false);});- it("never lets a bot mentioned further down the chain change skills", () => {+ it("lets a bot that first answer @mentions use its share even when another mentioned bot reaches it first", () => {const claim: RoomSkillsClaim = { claimed: false };takeRoomSkills(claim, 0, "writer", true);- handRoomSkills(claim, "video");- expect(roomTurnMayWriteSkills(claim, 2, "video")).toBe(false);+ handRoomSkills(claim, ["video", "editor"]);+ expect(roomTurnMayWriteSkills(claim, 2, "editor")).toBe(true);+ takeRoomSkills(claim, 2, "editor", true);+ expect(roomTurnMayWriteSkills(claim, 1, "editor")).toBe(false);+ });++ it("never lets a bot only a later answer mentions change skills", () => {+ const claim: RoomSkillsClaim = { claimed: false };+ takeRoomSkills(claim, 0, "writer", true);+ handRoomSkills(claim, ["video"]);expect(roomTurnMayWriteSkills(claim, 2, "editor")).toBe(false);});+ it("withdraws the shares still unused once the first answer's mentions are done", () => {+ const claim: RoomSkillsClaim = { claimed: false };+ takeRoomSkills(claim, 0, "writer", true);+ const withdraw = handRoomSkills(claim, ["video", "editor"]);+ takeRoomSkills(claim, 1, "video", true);+ withdraw();+ expect(roomTurnMayWriteSkills(claim, 1, "editor")).toBe(false);+ expect(roomTurnMayWriteSkills(claim, 1, "video")).toBe(false);+ });+it("gives the share back to a turn that never ran", () => {const claim: RoomSkillsClaim = { claimed: false };takeRoomSkills(claim, 0, "writer", true);- handRoomSkills(claim, "video");+ handRoomSkills(claim, ["video"]);takeRoomSkills(claim, 1, "video", true);takeRoomSkills(claim, 1, "video", false);expect(roomTurnMayWriteSkills(claim, 1, "video")).toBe(true);diff --git a/server/agents-turn.ts b/server/agents-turn.tsindex 47610fcb87..c7460aa470 100644--- a/server/agents-turn.ts+++ b/server/agents-turn.ts@@ -101,24 +101,26 @@ export function roomSkillsRefusal(handoff: boolean): string {}/** Who may still list and write skills on one person's message in a room: the first bot answering it (hop 0), once, and each- * bot that answer @mentions (hop 1), once each. A bot those mention (hop 2 and on) never may. */+ * bot that answer @mentions, once each, on its first turn however the chain reaches it. A bot only a later answer mentions never may. */export interface RoomSkillsClaim {claimed: boolean;mentioned?: Set<string>;}export function roomTurnMayWriteSkills(claim: RoomSkillsClaim, hop: number, botId: string): boolean {- return hop === 0 ? !claim.claimed : hop === 1 && claim.mentioned?.has(botId) === true;+ return hop === 0 ? !claim.claimed : claim.mentioned?.has(botId) === true;}-/** The first answer, holding the claim, @mentions `botId`: that bot may change its own skills once on this message. */-export function handRoomSkills(claim: RoomSkillsClaim, botId: string): void {- (claim.mentioned ??= new Set()).add(botId);+/** The first answer, holding the claim, @mentions `botIds`: each may change its own skills once on this message. Returns what+ * withdraws the shares still unused, for when that answer's mentions are done, so no later answer's mention can use them. */+export function handRoomSkills(claim: RoomSkillsClaim, botIds: readonly string[]): () => void {+ for (const botId of botIds) (claim.mentioned ??= new Set()).add(botId);+ return () => { for (const botId of botIds) claim.mentioned?.delete(botId); };}/** A turn that may write skills takes its share once its setup succeeded (`taken`), or gives it back when it never ran. */export function takeRoomSkills(claim: RoomSkillsClaim, hop: number, botId: string, taken: boolean): void {if (hop === 0) claim.claimed = taken;else if (taken) claim.mentioned?.delete(botId);- else handRoomSkills(claim, botId);+ else handRoomSkills(claim, [botId]);}diff --git a/server/index.ts b/server/index.tsindex f6712e9c69..73ce32ad4e 100644--- a/server/index.ts+++ b/server/index.ts@@ -10375,7 +10375,8 @@ async function runClaimedGroupMemberTurn(}// A bot that spoke earlier answers a mention by its own name (not @everyone) again, unless it took a turn since this reply.const spokeBeforeReply = new Set(spoken), answeredSince = turnsSince(spoken), named = new Set(mentionedBots(replyText, members).map((member) => member.id));- for (const next of roomResponders(replyText, members, { kind: "mentions" })) {+ const responders = roomResponders(replyText, members, { kind: "mentions" }), withdrawSkills = skillAuthoring && hop === 0 ? handRoomSkills(skillAuthoringClaim, responders.map((next) => next.id)) : undefined;+ try { for (const next of responders) {if (isCancelled?.()) return false;if (spokeBeforeReply.has(next.id) ? answeredSince(next.id) > 0 || !named.has(next.id) : spoken.has(next.id)) continue;if (operation) {@@ -10384,7 +10385,6 @@ async function runClaimedGroupMemberTurn(if (verdict === "skip") { noteGroupTurn(spoken, next.id); continue; }}if (providerTurnId) usageTrace.expect(threadId, { parent: { threadId, turnId: providerTurnId } });- if (skillAuthoring && hop === 0) handRoomSkills(skillAuthoringClaim, next.id);if (!(await runGroupMemberTurn(groupId,threadId,@@ -10402,7 +10402,7 @@ async function runClaimedGroupMemberTurn())) {return false;}- }+ } } finally { withdrawSkills?.(); }}return true;} catch (error) {