Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(routines): read Gmail in pages small enough to come back whole
AdityaUmaleb6a23fc4cec0
diff --git a/server/connected-reads.test.ts b/server/connected-reads.test.tsindex db2f2b3a41..2e6f51817c 100644--- a/server/connected-reads.test.ts+++ b/server/connected-reads.test.ts@@ -8,7 +8,7 @@ import {const fixture = (name: string): unknown => JSON.parse(readFileSync(new URL(`./testing/fixtures/${name}.json`, import.meta.url), "utf8"));/** One relay that answers like Composio's multi-execute tool, from `answers` by tool slug. */-function relay(answers: Record<string, (args: Record<string, unknown>) => { successful: boolean; data?: unknown; error?: string }>) {+function relay(answers: Record<string, (args: Record<string, unknown>) => { successful: boolean; data?: unknown; data_preview?: unknown; error?: string }>) {const frames: Array<{ method: string; session?: string; slug?: string; args?: Record<string, unknown>; item?: Record<string, unknown> }> = [];let sessions = 0;const deps = {@@ -96,6 +96,31 @@ describe("a check's reads", () => {expect(read.emails.map((email) => email.id)).toEqual(["m4", "m3", "m2"]);});+ it("reads mail in smaller pages when the connected-apps service sends a page back shortened", async () => {+ const messages = Array.from({ length: 30 }, (_, index) => ({ messageId: `m${index}`, threadId: `t${index}`, messageTimestamp: new Date(Date.UTC(2026, 9, 22, 0, index)).toISOString(), sender: "[email protected]", subject: "s" }));+ // Like the real service: an answer past its size limit comes back as a short preview with no data.+ const { deps, frames } = relay({+ GMAIL_FETCH_EMAILS: (args) => {+ const size = Number(args.max_results);+ const start = Number(args.page_token ?? 0);+ const page = messages.slice(start, start + size);+ if (size > 10) return { successful: true, data_preview: { messages: [...page.slice(0, 2), `...${page.length - 2} more items`] } };+ return { successful: true, data: { messages: page, ...(start + size < messages.length ? { nextPageToken: String(start + size) } : {}) } };+ },+ });+ const read = await createConnectedReads(deps).session().emails(0, 100);+ expect(read).toMatchObject({ more: false });+ expect(read.emails).toHaveLength(30);+ expect(frames.filter((frame) => frame.slug === "GMAIL_FETCH_EMAILS").every((frame) => Number(frame.args!.max_results) <= 50)).toBe(true);+ });++ it("a shortened answer is an error that says so, never an empty list", async () => {+ const { deps } = relay({ GMAIL_FETCH_EMAILS: () => ({ successful: true, data_preview: { messages: ["...50 more items"] } }) });+ const mail = await createConnectedReads(deps).session().emails(0, 5).catch((error: unknown) => error);+ expect(mail).toMatchObject({ app: "gmail", signedOut: false });+ expect((mail as Error).message).toMatch(/too large/);+ });+it("names the app that failed, and whether reconnecting fixes it", async () => {const { deps } = relay({GMAIL_FETCH_EMAILS: () => ({ successful: false, error: "Connected account is not ACTIVE: token expired" }),diff --git a/server/connected-reads.ts b/server/connected-reads.tsindex 2dcac3c36a..0951188286 100644--- a/server/connected-reads.ts+++ b/server/connected-reads.ts@@ -85,6 +85,7 @@ const PREVIEW_MAX = 300;const ADDRESS = /^[\w.+][\w.+-]*@[\w-]+(?:\.[\w-]+)+$/;const PRIMARY_CALENDAR = { id: "primary", name: "primary", primary: true, owned: true, read: true } as const;const GONE = Symbol("gone");+const SHORTENED = Symbol("shortened");const record = (value: unknown): Record<string, unknown> | undefined =>value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;@@ -95,6 +96,8 @@ export const isAddress = (value: string): boolean => ADDRESS.test(value);/** The tool itself answered that it failed, as opposed to the relay or Composio failing around it. */class ToolAnswerError extends Error {}+/** The connected-apps service sends an answer past its size limit as a short preview with no data: the read must ask for less. */+class ShortenedAnswerError extends Error {}/** Whether a Gmail tool's own error says the message asked for is missing, while naming no account, connection, token,* permission or tool. */@@ -139,7 +142,10 @@ export function toolData(slug: string, frame: Record<string, unknown> | null): uconst response = record(results.map(record).find((item) => item?.tool_slug === slug)?.response);if (!response) throw new Error(`${slug}: missing from the answer`);if (response.successful === false) throw new ToolAnswerError(String(response.error ?? `${slug} failed`).slice(0, 300));- if (!("data" in response)) throw new Error(`${slug}: the answer had no data`);+ if (!("data" in response)) {+ if ("data_preview" in response) throw new ShortenedAnswerError(`${slug}: the answer was too large and came back shortened; ask for fewer`);+ throw new Error(`${slug}: the answer had no data`);+ }return response.data;}if (!("data" in body)) throw new Error(`${slug}: the answer had no data`);@@ -424,7 +430,10 @@ export interface ConnectedReads {session(): ReadSession;}-const GMAIL_PAGE = 50;+// A 50-email page is past the connected-apps service's size limit and comes back shortened; 20 fits, and a page that+// still comes back shortened is asked again at half the size, down to 5.+const GMAIL_PAGE = 20;+const GMAIL_PAGE_MIN = 5;const GMAIL_PAGES = 6;const appOf = (slug: string): ReadApp | "outlook" => (slug.startsWith("GMAIL_") ? "gmail" : slug.startsWith("OUTLOOK_") ? "outlook" : "googlecalendar");@@ -447,9 +456,10 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedReareturn init.transportSessionId;}- /** One tool's data, or GONE when `gone` says the tool's own failure means the thing asked for is missing. */- async function call(slug: string, args: Record<string, unknown>, options: { signal?: AbortSignal; gone?: (toolError: string) => boolean } = {}): Promise<unknown> {- const { gone, signal = AbortSignal.timeout(readTimeoutMs) } = options;+ /** One tool's data, GONE when `gone` says the tool's own failure means the thing asked for is missing, or SHORTENED+ * when `shortened` is set and the answer was too large to come back whole. */+ async function call(slug: string, args: Record<string, unknown>, options: { signal?: AbortSignal; gone?: (toolError: string) => boolean; shortened?: boolean } = {}): Promise<unknown> {+ const { gone, shortened, signal = AbortSignal.timeout(readTimeoutMs) } = options;const app = appOf(slug);let account: string | undefined;try {@@ -478,6 +488,7 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedRea} catch (error) {if (error instanceof SourceError) throw error;if (gone && error instanceof ToolAnswerError && gone(error.message)) return GONE;+ if (shortened && error instanceof ShortenedAnswerError) return SHORTENED;if (signal.aborted) throw new SourceError(app, `${slug} did not answer within ${Math.round(readTimeoutMs / 1000)} seconds`);// Only the tool's own refusal points at the named account; a relay hiccup (HTTP 5xx) is a later try.throw new SourceError(app, errorText(error), account !== undefined && error instanceof ToolAnswerError);@@ -536,11 +547,16 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedRearequireFinite("gmail", sinceMs);const found: EmailHeader[] = [];let page: string | undefined;+ let size = GMAIL_PAGE;for (let index = 0; index < GMAIL_PAGES; index++) {const data = await call(GMAIL_FETCH_TOOL, {user_id: "me", query: `in:inbox after:${seconds(sinceMs)} -category:promotions -category:social`,- max_results: GMAIL_PAGE, include_payload: false, verbose: false, ...(page ? { page_token: page } : {}),- });+ max_results: size, include_payload: false, verbose: false, ...(page ? { page_token: page } : {}),+ }, { shortened: size > GMAIL_PAGE_MIN });+ if (data === SHORTENED) {+ size = Math.max(GMAIL_PAGE_MIN, Math.floor(size / 2));+ continue;+ }found.push(...parsed("gmail", () => emailsFromData(data)));page = nextPageOf(data);if (!page) break;