Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Read only the opening of a long Google Drive file, not the whole download (#595)
kevin9da63c14fd88
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9122c45f..47f7f71f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,14 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.## Unreleased+### Reading a large file from Google Drive no longer downloads all of it++`read_file_content` shows a Bot at most the first 20,000 characters of a file, but it downloaded the+whole file and held it in memory before cutting it. A 200 MB text file raised the server's memory by+more than 600 MB for one call, on the process that serves everybody else. The connector now stops+reading once it has more than it can show and cancels the rest of the download. What the Bot is shown+is unchanged, except that the note on a cut file no longer gives the file's full length, which is no+longer known.### A Bot reads the text an MCP server returns as an embedded resourceA tool result can carry an embedded resource, and a text resource holds content, such as a file thediff --git a/server/src/plugins/google-drive-rest.ts b/server/src/plugins/google-drive-rest.tsindex 069443bc..e48e94ff 100644--- a/server/src/plugins/google-drive-rest.ts+++ b/server/src/plugins/google-drive-rest.ts@@ -307,8 +307,11 @@ const driveQuery = (query: string) => {* transports. The empty case is the one that matters: a search that matched nothing has to SAY so,* because an empty string reads to a model as "the tool had nothing to say" and gets filled in from* memory — which for a knowledge connector is the exact failure the lane exists to prevent.+ *+ * `cut` is true when reading stopped before the file ended (see {@link readOpening}), so the file's+ * full length is not known and is not claimed.*/-function asResult(text: string): McpCallResult {+function asResult(text: string, cut = false): McpCallResult {const joined = text.trim();if (joined === "") {return {@@ -317,16 +320,46 @@ function asResult(text: string): McpCallResult {truncated: false,};}- if (joined.length <= MAX_RESULT_CHARS) {+ if (joined.length <= MAX_RESULT_CHARS && !cut) {return { text: joined, isError: false, truncated: false };}return {- text: `${cutAtCodeUnits(joined, MAX_RESULT_CHARS)}\n\n[truncated: the tool returned ${joined.length} characters]`,+ text: `${cutAtCodeUnits(joined, MAX_RESULT_CHARS)}\n\n[truncated: ${cut ? "the file is longer than this" : `the tool returned ${joined.length} characters`}]`,isError: false,truncated: true,};}+/**+ * As much of a file's text as a result can show, and whether there was more.+ *+ * `response.text()` held the whole download as one string before all but its opening was dropped,+ * so a 200 MB log raised the process's memory by more than 600 MB to return 20,000 characters, on+ * the same process that serves everybody else. Reading stops once there is more than a result can+ * carry, and the rest of the body is cancelled rather than downloaded.+ *+ * Decoded the way `response.text()` decodes: UTF-8, with a byte order mark dropped and anything+ * malformed replaced.+ */+async function readOpening(+ response: Response,+): Promise<{ text: string; cut: boolean }> {+ if (!response.body) return { text: "", cut: false };+ const reader = response.body.getReader();+ const decoder = new TextDecoder();+ let text = "";+ try {+ for (;;) {+ const { done, value } = await reader.read();+ if (done) return { text: text + decoder.decode(), cut: false };+ text += decoder.decode(value, { stream: true });+ if (text.length > MAX_RESULT_CHARS) return { text, cut: true };+ }+ } finally {+ await reader.cancel().catch(() => {});+ }+}+const failure = (message: string): McpCallResult => ({text: message,isError: true,@@ -447,9 +480,9 @@ export async function callTool(});if (!content.ok) return failure(content.message);- const text = await content.response.text();+ const { text, cut } = await readOpening(content.response);// Named, because a model handed only the body cannot cite what it read.- return asResult(`${file.name ?? fileId}\n\n${text}`);+ return asResult(`${file.name ?? fileId}\n\n${text}`, cut);}return failure(diff --git a/server/tests/google-drive-rest.test.ts b/server/tests/google-drive-rest.test.tsindex 4053f723..80752a14 100644--- a/server/tests/google-drive-rest.test.ts+++ b/server/tests/google-drive-rest.test.ts@@ -398,6 +398,86 @@ describe("reading a file asks Drive what it is first", () => {expect(result.truncated).toBe(true);expect(result.text.split("\n\n[truncated")[0]).toBe(`${heading}${filler}`);});++ /*+ * Only the opening of a long file is shown, so only the opening is read.+ *+ * The whole download used to be held as one string before all but the first MAX_RESULT_CHARS+ * characters were dropped: a 200 MB log took the process up by more than 600 MB to return 20,000+ * characters. The body here counts what is pulled from it, so reading it to the end fails.+ */+ test("a file far longer than one result is not downloaded whole", async () => {+ const line = new TextEncoder().encode(`${"x".repeat(1023)}\n`);+ const size = 16 * 1024 * 1024;+ let pulled = 0;+ let served = 0;+ globalThis.fetch = (async () => {+ served += 1;+ if (served === 1) {+ return new Response(+ JSON.stringify({+ id: "log1",+ name: "big.log",+ mimeType: "text/plain",+ }),+ { headers: { "content-type": "application/json" } },+ );+ }+ const body = new ReadableStream<Uint8Array>({+ pull(controller) {+ if (pulled >= size) {+ controller.close();+ return;+ }+ pulled += line.length;+ controller.enqueue(line);+ },+ });+ return new Response(body, { headers: { "content-type": "text/plain" } });+ }) as unknown as typeof fetch;++ const result = await callTool(connection, "read_file_content", {+ fileId: "log1",+ });++ expect(pulled).toBeLessThan(size / 16);+ expect(result.isError).toBe(false);+ expect(result.truncated).toBe(true);+ expect(result.text.startsWith(`big.log\n\n${"x".repeat(1023)}\n`)).toBe(+ true,+ );+ // The file's length is not known, so none is claimed.+ expect(+ result.text.endsWith("\n\n[truncated: the file is longer than this]"),+ ).toBe(true);+ });++ test("a download with no body at all reads as an empty file", async () => {+ let served = 0;+ globalThis.fetch = (async () => {+ served += 1;+ return served === 1+ ? new Response(+ JSON.stringify({+ id: "empty1",+ name: "empty.txt",+ mimeType: "text/plain",+ }),+ { headers: { "content-type": "application/json" } },+ )+ : new Response(null);+ }) as unknown as typeof fetch;++ const result = await callTool(connection, "read_file_content", {+ fileId: "empty1",+ });++ expect(result).toEqual({+ text: "empty.txt",+ isError: false,+ truncated: false,+ });+ });});/*