Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(connector): name the usable accounts when a call names no account and the app has more than one

koeria640c87342320
diff --git a/server/connector-account-hint.test.ts b/server/connector-account-hint.test.ts
index 63fdc7bb8d..afc28d5106 100644
--- a/server/connector-account-hint.test.ts
+++ b/server/connector-account-hint.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { accountHint, accountsByApp, withAccountHint } from "./connector-account-hint.ts";
+import { accountHint, accountsByApp, missingAccountHint, withAccountHint } from "./connector-account-hint.ts";
const encode = (value: unknown) => new TextEncoder().encode(typeof value === "string" ? value : JSON.stringify(value));
const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes);
@@ -94,6 +94,47 @@ describe("connector account hint", () => {
expect(read.github?.[0]?.alias).toBe("personal");
});
+ it("names the usable accounts of a call refused for naming no account", async () => {
+ const noAccount = {
+ jsonrpc: "2.0",
+ id: 11,
+ result: { content: [{ type: "text", text: JSON.stringify({ successful: false, error: JSON.stringify({ message: "Multiple gmail accounts connected. Specify which to use via the 'account' field: work, personal" }) }) }], isError: true },
+ };
+ const out = JSON.parse(decode(await withAccountHint(encode(noAccount), "application/json", ["GMAIL_FETCH_EMAILS"], async () => ({
+ gmail: [
+ { id: "gmail_one", alias: "work", status: "ACTIVE" },
+ { id: "gmail_two", alias: "personal", status: "ACTIVE" },
+ ],
+ }))));
+ expect(out.result.content[1].text).toContain("Gmail refused this because the call named no account");
+ expect(out.result.content[1].text).toContain("2 connected accounts (work, personal)");
+ expect(out.result.content[1].text).toContain("Name one of them in the call's account field");
+ });
+
+ it("names the one usable account when the others cannot run a call", async () => {
+ const hint = missingAccountHint(["GMAIL_FETCH_EMAILS"], {
+ gmail: [
+ { id: "gmail_one", alias: "work", status: "ACTIVE" },
+ { id: "gmail_two", alias: "old", status: "EXPIRED" },
+ ],
+ });
+ expect(hint).toContain("one can run a call (work)");
+ expect(hint).toContain("Name that one in the call's account field");
+ });
+
+ it("leaves a no-account refusal alone when no account of the app can run a call", () => {
+ expect(missingAccountHint(["GMAIL_FETCH_EMAILS"], { gmail: [{ id: "gmail_one", alias: "old", status: "EXPIRED" }] })).toBe("");
+ });
+
+ it("leaves a no-account refusal alone when the batch mixes apps", () => {
+ expect(missingAccountHint(["GMAIL_GET_PROFILE", "GITHUB_CREATE_AN_ISSUE"], accounts)).toBe("");
+ });
+
+ it("leaves an answer that matches neither refusal untouched", async () => {
+ const multi = encode({ jsonrpc: "2.0", id: 12, result: { content: [{ type: "text", text: "Multiple gmail accounts connected. Specify which to use via the 'account' field: work" }], isError: true } });
+ expect(await withAccountHint(multi, "application/json", ["GMAIL_FETCH_EMAILS"], async () => ({}))).toBe(multi);
+ });
+
it("names the app the refusal names, not the first app of the batch", async () => {
const mixed = { github: accounts.github, gmail: [{ id: "gmail_one", alias: "work", status: "ACTIVE" }, { id: "gmail_two", alias: "other", status: "ACTIVE" }] };
const named = { jsonrpc: "2.0", id: 8, result: { content: [{ type: "text", text: JSON.stringify({ successful: false, tool_slug: "GITHUB_CREATE_AN_ISSUE", error: JSON.stringify({ message: "Resource not accessible by integration" }) }) }], isError: true } };
diff --git a/server/connector-account-hint.ts b/server/connector-account-hint.ts
index efd010c33d..c196f4cb3a 100644
--- a/server/connector-account-hint.ts
+++ b/server/connector-account-hint.ts
@@ -12,6 +12,10 @@ import { annotateFailedCall } from "./connector-scope-hint.ts";
/** GitHub's wording when the connected account's app installation cannot reach what the call asks for. */
const ACCOUNT_ERROR = /not accessible by integration/i;
+/** Composio's wording when a call names no account and the app has more than one connected account. The tool schema
+ * calls the account field optional, so a bot that has one account in mind leaves it out and the call fails wholesale. */
+const MISSING_ACCOUNT_ERROR = /multiple .* accounts connected/i;
+
/** Marks the note a frame carries, so a frame that must not get one can be told from one that does. */
const NOTE = "\u0000omb-account-hint:";
@@ -60,16 +64,36 @@ export function accountHint(slugs: readonly string[], accounts: ConnectedAccount
+ `before telling the person you have no access to ${app}.`;
}
+/** The note for a call refused because it named no account and the app has more than one connected account: the
+ * accounts the bot can use, by alias or id, so the model names one and retries instead of failing the task. */
+export function missingAccountHint(slugs: readonly string[], accounts: ConnectedAccounts): string {
+ const toolkit = oneToolkit(slugs);
+ if (!toolkit) return "";
+ const known = usable(accounts, toolkit);
+ if (!known.length) return "";
+ const app = appName(toolkit);
+ const names = known.map((account) => account.alias?.trim() || account.id).join(", ");
+ if (known.length === 1)
+ return `OpenMausBot note: ${app} refused this because the call named no account and ${app} has more than one `
+ + `connected account, of which one can run a call (${names}). Name that one in the call's account field `
+ + `(an alias or an id) and try the call again.`;
+ return `OpenMausBot note: ${app} refused this because the call named no account and ${app} has ${known.length} `
+ + `connected accounts (${names}). Name one of them in the call's account field (an alias or an id) and try the `
+ + `call again.`;
+}
+
+/** The refusals a note answers, and the note for each: an account the call used cannot reach it, or the call named no
+ * account at all and the app has more than one. A refusal both name answers with the first note. */
+const NOTES: readonly (readonly [RegExp, (slugs: readonly string[], accounts: ConnectedAccounts) => string])[] = [
+ [ACCOUNT_ERROR, accountHint],
+ [MISSING_ACCOUNT_ERROR, missingAccountHint],
+];
+
/** The accounts of every app, as the services inventory gives them. */
export function accountsByApp(services: Record<string, { accounts: readonly ConnectedAccount[] }>): ConnectedAccounts {
return Object.fromEntries(Object.entries(services).map(([slug, state]) => [slug, state.accounts]));
}
-/** The note a frame carries, marked so that a frame which earns none can be recognised afterwards. */
-function markedNote(failed: readonly string[], accounts: ConnectedAccounts): string {
- return `${NOTE}${accountHint(failed, accounts)}`;
-}
-
/** Takes the mark out of a frame: the note stays as it is, and a frame that earned none loses the mark again.
* Tells whether the frame carries a note. */
function unmarked(frame: unknown): boolean {
@@ -91,25 +115,27 @@ export async function withAccountHint(
slugs: readonly string[],
loadAccounts: () => Promise<ConnectedAccounts>,
): Promise<Uint8Array> {
- if (!ACCOUNT_ERROR.test(new TextDecoder().decode(bytes))) return bytes;
+ const text = new TextDecoder().decode(bytes);
+ const matched = NOTES.find(([pattern]) => pattern.test(text));
+ if (!matched) return bytes;
+ const [pattern, noteFor] = matched;
const accounts = await loadAccounts().catch(() => ({}) as ConnectedAccounts);
// The shared helper hands the hint the tools that frame names, so the note is the one of that frame's own app.
- const hint = (failed: readonly string[]) => markedNote(failed, accounts);
+ const hint = (failed: readonly string[]) => `${NOTE}${noteFor(failed, accounts)}`;
let noted = false;
try {
- const text = new TextDecoder().decode(bytes);
if (/text\/event-stream/i.test(contentType)) {
const lines = text.split("\n").map((line) => {
if (!line.startsWith("data:")) return line;
const frame = JSON.parse(line.slice(5));
- if (!annotateFailedCall(frame, ACCOUNT_ERROR, hint, slugs)) return line;
+ if (!annotateFailedCall(frame, pattern, hint, slugs)) return line;
noted = unmarked(frame) || noted;
return `data: ${JSON.stringify(frame)}`;
});
return noted ? new TextEncoder().encode(lines.join("\n")) : bytes;
}
const frame = JSON.parse(text);
- if (!annotateFailedCall(frame, ACCOUNT_ERROR, hint, slugs)) return bytes;
+ if (!annotateFailedCall(frame, pattern, hint, slugs)) return bytes;
if (!unmarked(frame)) return bytes;
return new TextEncoder().encode(JSON.stringify(frame));
} catch {