Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Ask for the accounts again after a failed lookup instead of keeping the fallback for the session
AdityaUmale97833a224b8d
diff --git a/server/connected-reads.test.ts b/server/connected-reads.test.tsindex 98ea391198..fc8dc0e72f 100644--- a/server/connected-reads.test.ts+++ b/server/connected-reads.test.ts@@ -190,6 +190,26 @@ describe("a check's reads", () => {expect(error).toMatchObject({ app: "gmail", severalAccounts: false });});+ it("names the fallback account while the lookup fails, and still asks again on the next read", async () => {+ const { deps, frames } = relay({+ GMAIL_GET_PROFILE: () => ({ successful: true, data: { emailAddress: "[email protected]" } }),+ GOOGLECALENDAR_LIST_CALENDARS: () => ({ successful: true, data: { items: [] } }),+ GOOGLECALENDAR_EVENTS_LIST: () => ({ successful: true, data: { items: [] } }),+ });+ let asked = 0;+ const accounts = async () => {+ asked += 1;+ if (asked === 1) throw new Error("inventory down");+ return { gmail: "ca_older", googlecalendar: "ca_cal_newest" };+ };+ const session = createConnectedReads({ ...deps, accounts, fallbackAccounts: () => ({ gmail: "ca_older" }) }).session("bot-1");+ expect(await session.address()).toBe("[email protected]");+ await session.events(0, 1);+ expect(asked).toBe(2);+ expect(frames.filter((one) => one.slug === "GMAIL_GET_PROFILE").map((one) => one.item!.account)).toEqual(["ca_older"]);+ for (const frame of frames.filter((one) => one.slug?.startsWith("GOOGLECALENDAR_"))) expect(frame.item!.account).toBe("ca_cal_newest");+ });+it("gives up on an account lookup that outlasts the read's time limit, and asks again on the next read", async () => {const { deps, frames } = relay({ GMAIL_GET_PROFILE: () => ({ successful: true, data: { emailAddress: "[email protected]" } }) });let asked = 0;diff --git a/server/connected-reads.ts b/server/connected-reads.tsindex 7fb203bd18..786639c9f2 100644--- a/server/connected-reads.ts+++ b/server/connected-reads.ts@@ -400,6 +400,8 @@ export interface ConnectedReadDeps {/** The account to read for each app with several connected (Composio refuses a read that names none then); an app* with one is left out, and its reads name no account. Asked once per session, with the bot the session reads for. */accounts?(botId?: string): Promise<AppAccounts>;+ /** The accounts a read names while that lookup is failing, for a bot limited to some accounts. */+ fallbackAccounts?(botId?: string): AppAccounts;relay(payload: unknown, transportSessionId: string | undefined, signal: AbortSignal): Promise<{ status: number; bytes: Uint8Array; transportSessionId?: string }>;readTimeoutMs?: number;}@@ -470,10 +472,11 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedReathrow error;});const transport = await opened;- // A failed lookup reads as no account named and is asked again on the next read, not kept for the session.+ // A failed lookup reads as the fallback (no account named, unless the bot is limited to some) and is asked again+ // on the next read, not kept for the session.accounts ??= deps.accounts?.(botId).catch((): AppAccounts => {accounts = undefined;- return {};+ return deps.fallbackAccounts?.(botId) ?? {};}) ?? Promise.resolve({});// The lookup counts toward this read's time limit; one that runs out is dropped so the next read asks again.const lookup = accounts;diff --git a/server/host/proactive.test.ts b/server/host/proactive.test.tsindex bd24069287..bbf6f3c23c 100644--- a/server/host/proactive.test.ts+++ b/server/host/proactive.test.ts@@ -150,6 +150,23 @@ describe("the watch's checks", () => {expect(new Set(gmail)).toEqual(new Set(["ca_older"]));});+ it("ask for the accounts again after a failed lookup instead of keeping its fallback for the session", async () => {+ let calls = 0;+ const s = setup({ connectedServices: async () => {+ if (++calls === 2) throw new Error("composio down");+ return {+ gmail: { connected: true, accounts: [{ id: "ca_newest", status: "ACTIVE" }, { id: "ca_older", status: "ACTIVE" }] },+ googlecalendar: { connected: true, accounts: [{ id: "ca_cal_newest", status: "ACTIVE" }, { id: "ca_cal_older", status: "ACTIVE" }] },+ };+ } });+ const limited: ConnectorScopes = { apps: { gmail: "read", googlecalendar: "read" }, accounts: { gmail: ["ca_older"] } };+ s.store.patchBot(s.chief.id, { connectorScopes: limited });+ await s.proactive.checks.check(s.proactive.ensureWatch()!, { manual: true });+ const named = (prefix: string) => s.reads.flatMap((slug, index) => (slug.startsWith(prefix) ? [s.accounts[index]] : []));+ expect(new Set(named("GMAIL_"))).toEqual(new Set(["ca_older"]));+ expect(named("GOOGLECALENDAR_")).toContain("ca_cal_newest");+ });+it("belong to the main agent only: a briefing routine of another bot reads nothing", async () => {const s = setup();const other = s.store.createBot({ name: "Scout" });diff --git a/server/host/proactive.ts b/server/host/proactive.tsindex 97b3612f0b..a60bf1a85d 100644--- a/server/host/proactive.ts+++ b/server/host/proactive.ts@@ -100,13 +100,10 @@ export function createProactive(deps: ProactiveDeps) {},// Several accounts of one app: read the newest active one, as Composio refuses a read that names none. A bot limited// to some accounts reads one of those, named even when it is the app's only one; with none of them active the read- // names the first anyway, so it is refused rather than run as another account, also when the lookup fails.+ // names the first anyway, so it is refused rather than run as another account, also while the lookup fails.accounts: async (botId) => {const scopes = botId ? store.bot(botId)?.connectorScopes : undefined;- const services = await composio.connectedServices(cfg).catch((error: unknown) => {- if (!READ_APPS.some((app) => accountLimit(scopes, app))) throw error;- return {} as Awaited<ReturnType<typeof composio.connectedServices>>;- });+ const services = await composio.connectedServices(cfg);return Object.fromEntries(READ_APPS.flatMap((app) => {const active = (services[app]?.accounts ?? []).filter((one) => /^active$/i.test(one.status)).map((one) => one.id);const allowed = accountLimit(scopes, app);@@ -114,6 +111,13 @@ export function createProactive(deps: ProactiveDeps) {return active.length > 1 ? [[app, active[0]!]] : [];}));},+ fallbackAccounts: (botId) => {+ const scopes = botId ? store.bot(botId)?.connectorScopes : undefined;+ return Object.fromEntries(READ_APPS.flatMap((app) => {+ const allowed = accountLimit(scopes, app);+ return allowed ? [[app, allowed[0]!]] : [];+ }));+ },relay: (payload, session, signal) => composio.relayMcp(cfg, payload as never, session, undefined, signal),});