Skip to content

Commit 08df21e

Browse files
committed
truncate documents when too long
1 parent dff27c6 commit 08df21e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

client/agents/policyai/utils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ export async function search({ query, maxResults = 100 }) {
8787
return allResults.slice(0, maxResults);
8888
}
8989

90+
/**
91+
* Truncates a string to a maximum length and appends a suffix
92+
* @param {string} str - The string to truncate
93+
* @param {number} maxLength - The maximum length of the string
94+
* @param {string} suffix - The suffix to append
95+
* @returns {string} - The truncated string
96+
*/
97+
export function truncate(str, maxLength = 10_000, suffix = "\n ... (truncated)") {
98+
return str.length > maxLength ? str.slice(0, maxLength) + suffix : str;
99+
}
100+
90101
/**
91102
* Returns the content of a website as text
92103
* @param {string} url
@@ -100,7 +111,8 @@ export async function browse({ url }) {
100111
return `Failed to read ${url}: ${response.status} ${response.statusText}\n${text}`;
101112
}
102113
const mimetype = response.headers.get("content-type");
103-
return await parseDocument(bytes, mimetype, url);
114+
const results = await parseDocument(bytes, mimetype, url);
115+
return truncate(results, 10_000);
104116
}
105117

106118
/**

0 commit comments

Comments
 (0)