Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libs/deepagents/src/middleware/fs.eviction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { StructuredTool } from "langchain";
import {
createContentPreview,
createFilesystemMiddleware,
extractTextContent,
TOOLS_EXCLUDED_FROM_EVICTION,
NUM_CHARS_PER_TOKEN,
} from "./fs.js";
Expand Down Expand Up @@ -293,3 +294,41 @@ describe("read_file character-based truncation", () => {
expect(result.length).toBeGreaterThan(100000);
});
});

describe("extractTextContent", () => {
it("should return the string directly for string content", () => {
expect(extractTextContent("hello world")).toBe("hello world");
});

it("should join text blocks from array content", () => {
const content = [
{ type: "text", text: "hello " },
{ type: "text", text: "world" },
];
expect(extractTextContent(content)).toBe("hello world");
});

it("should ignore non-text blocks", () => {
const content = [
{ type: "text", text: "hello" },
{ type: "image_url", image_url: { url: "http://example.com/img.png" } },
{ type: "text", text: " world" },
];
expect(extractTextContent(content)).toBe("hello world");
});

it("should return null for array with no text blocks", () => {
const content = [
{ type: "image_url", image_url: { url: "http://example.com/img.png" } },
];
expect(extractTextContent(content)).toBeNull();
});

it("should return null for empty array", () => {
expect(extractTextContent([])).toBeNull();
});

it("should return empty string for string content that is empty", () => {
expect(extractTextContent("")).toBe("");
});
});
37 changes: 33 additions & 4 deletions libs/deepagents/src/middleware/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ export function createContentPreview(
return headSample + truncationNotice + tailSample;
}

/**
* Extract joined text from message content (string or array of content blocks).
*/
export function extractTextContent(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which would allow use to remove this function

content: string | Array<Record<string, unknown>>,
): string | null {
if (typeof content === "string") {
return content;
}
if (Array.isArray(content)) {
const textParts: string[] = [];
for (const block of content) {
if (
typeof block === "object" &&
block !== null &&
block.type === "text" &&
typeof block.text === "string"
) {
textParts.push(block.text);
}
}
if (textParts.length > 0) {
return textParts.join("");
}
}
return null;
}

/**
* required for type inference
*/
Expand Down Expand Up @@ -853,9 +881,10 @@ export function createFilesystemMiddleware(
msg: ToolMessage,
toolTokenLimitBeforeEvict: number,
) {
const textContent = extractTextContent(msg.content);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this suffice?

Suggested change
const textContent = extractTextContent(msg.content);
const textContent = msg.text;

if (
typeof msg.content === "string" &&
msg.content.length > toolTokenLimitBeforeEvict * NUM_CHARS_PER_TOKEN
textContent !== null &&
textContent.length > toolTokenLimitBeforeEvict * NUM_CHARS_PER_TOKEN
) {
// Build StateAndStore from request
const stateAndStore: StateAndStore = {
Expand All @@ -871,15 +900,15 @@ export function createFilesystemMiddleware(

const writeResult = await resolvedBackend.write(
evictPath,
msg.content,
textContent,
);

if (writeResult.error) {
return { message: msg, filesUpdate: null };
}

// Create preview showing head and tail of the result
const contentSample = createContentPreview(msg.content);
const contentSample = createContentPreview(textContent);
const replacementText = TOO_LARGE_TOOL_MSG.replace(
"{tool_call_id}",
msg.tool_call_id,
Expand Down
1 change: 1 addition & 0 deletions libs/deepagents/src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
TOOLS_EXCLUDED_FROM_EVICTION,
NUM_CHARS_PER_TOKEN,
createContentPreview,
extractTextContent,
} from "./fs.js";
export {
createSubAgentMiddleware,
Expand Down
Loading