Skip to content
Open
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
27 changes: 20 additions & 7 deletions src/lib/agents/search/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,26 @@ class APISearchAgent {
type: 'researchComplete',
});

const finalContext =
searchResults?.searchFindings
.map(
(f, index) =>
`<result index=${index + 1} title=${f.metadata.title}>${f.content}</result>`,
)
.join('\n') || '';
// Cap each result and total context to stay within reasonable token budgets
const maxCharsPerResult = 24000;
const maxTotalChars = 80000;

let totalChars = 0;
const contextParts: string[] = [];

if (searchResults?.searchFindings) {
for (let i = 0; i < searchResults.searchFindings.length; i++) {
const f = searchResults.searchFindings[i];
const truncated = f.content.slice(0, maxCharsPerResult);
const part = `<result index=${i + 1} title=${f.metadata.title}>${truncated}</result>`;

if (totalChars + part.length > maxTotalChars) break;
totalChars += part.length;
contextParts.push(part);
}
}

const finalContext = contextParts.join('\n');

const widgetContext = widgetOutputs
.map((o) => {
Expand Down
27 changes: 20 additions & 7 deletions src/lib/agents/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ class SearchAgent {
type: 'researchComplete',
});

const finalContext =
searchResults?.searchFindings
.map(
(f, index) =>
`<result index=${index + 1} title=${f.metadata.title}>${f.content}</result>`,
)
.join('\n') || '';
// Cap each result and total context to stay within reasonable token budgets
const maxCharsPerResult = 24000;
const maxTotalChars = 80000;

let totalChars = 0;
const contextParts: string[] = [];

if (searchResults?.searchFindings) {
for (let i = 0; i < searchResults.searchFindings.length; i++) {
const f = searchResults.searchFindings[i];
const truncated = f.content.slice(0, maxCharsPerResult);
const part = `<result index=${i + 1} title=${f.metadata.title}>${truncated}</result>`;

if (totalChars + part.length > maxTotalChars) break;
totalChars += part.length;
contextParts.push(part);
}
}

const finalContext = contextParts.join('\n');

const widgetContext = widgetOutputs
.map((o) => {
Expand Down
18 changes: 16 additions & 2 deletions src/lib/agents/search/researcher/actions/scrapeURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ResearchAction } from '../../types';
import { Chunk, ReadingResearchBlock } from '@/lib/types';
import TurnDown from 'turndown';
import path from 'path';
import { splitText } from '@/lib/utils/splitText';

const turndownService = new TurnDown();

Expand Down Expand Up @@ -40,11 +41,18 @@ const scrapeURLAction: ResearchAction<typeof schema> = {
params.urls.map(async (url) => {
try {
const res = await fetch(url);
const text = await res.text();
let text = await res.text();

const title =
text.match(/<title>(.*?)<\/title>/i)?.[1] || `Content from ${url}`;

// Cap raw HTML before Turndown so we don't spend CPU converting
// megabytes of markup we'll mostly throw away after tokenization.
const maxHtmlChars = 200_000;
if (text.length > maxHtmlChars) {
text = text.slice(0, maxHtmlChars);
}

if (
!readingEmitted &&
researchBlock &&
Expand Down Expand Up @@ -110,8 +118,14 @@ const scrapeURLAction: ResearchAction<typeof schema> = {

const markdown = turndownService.turndown(text);

// Limit scraped content to avoid blowing up the context window.
// splitText chunks by token count — we only keep the first chunk.
const maxTokensPerPage = 6000;
const chunks = splitText(markdown, maxTokensPerPage, 0);
const content = chunks.length > 0 ? chunks[0] : markdown;

results.push({
content: markdown,
content,
metadata: {
url,
title: title,
Expand Down