Skip to content

An AI-powered search engine with a generative UI

perf(search): tighten the model-facing search result projection (#1015)

Yoshiki Miura6b8916716ced
diff --git a/lib/tools/__tests__/search-to-model-output.test.ts b/lib/tools/__tests__/search-to-model-output.test.ts
index a09fcf7a..82fc1f5e 100644
--- a/lib/tools/__tests__/search-to-model-output.test.ts
+++ b/lib/tools/__tests__/search-to-model-output.test.ts
@@ -1,10 +1,13 @@
import { describe, expect, it } from 'vitest'
-import { createSearchTool } from '@/lib/tools/search'
+import {
+ createSearchTool,
+ SEARCH_MODEL_CONTENT_MAX_CHARACTERS
+} from '@/lib/tools/search'
// The search tool's toModelOutput strips UI-only fields (citationMap
// duplicates results; state is a streaming marker) from what the model sees.
-// images MUST reach the model — getImageSpecPrompt instructs it to embed URLs
+// images MUST reach the model. getImageSpecPrompt instructs it to embed URLs
// verbatim from that array. All fields must survive in the streamed/persisted
// output for the UI.
describe('search tool toModelOutput', () => {
@@ -66,10 +69,109 @@ describe('search tool toModelOutput', () => {
expect(value).not.toHaveProperty('toolCallId')
})
- it('keeps images so the model can embed inline image specs', async () => {
- const value = await getModelValue(fullOutput)
+ it('truncates long result content and appends a marker inside the bound', async () => {
+ const content = 'a'.repeat(SEARCH_MODEL_CONTENT_MAX_CHARACTERS + 10)
+ const value = await getModelValue({
+ ...fullOutput,
+ results: [{ ...fullOutput.results[0], content }]
+ })
+ const projectedContent = (value.results as Array<{ content: string }>)[0]
+ .content
+
+ expect(projectedContent).toBe(
+ `${content.slice(0, SEARCH_MODEL_CONTENT_MAX_CHARACTERS - 1)}…`
+ )
+ expect(projectedContent).toHaveLength(SEARCH_MODEL_CONTENT_MAX_CHARACTERS)
+ })
+
+ it('does not split a surrogate pair at the boundary', async () => {
+ // The astral character straddles the cut, so a naive slice would leave a
+ // lone high surrogate for the model to read.
+ const content = `${'a'.repeat(SEARCH_MODEL_CONTENT_MAX_CHARACTERS - 2)}😀tail`
+ const value = await getModelValue({
+ ...fullOutput,
+ results: [{ ...fullOutput.results[0], content }]
+ })
+ const projectedContent = (value.results as Array<{ content: string }>)[0]
+ .content
+
+ expect(projectedContent).toBe(
+ `${'a'.repeat(SEARCH_MODEL_CONTENT_MAX_CHARACTERS - 2)}…`
+ )
+ expect(projectedContent).not.toMatch(/[\uD800-\uDBFF]$/)
+ expect(projectedContent).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/)
+ })
+
+ it('preserves result content at or under the limit exactly', async () => {
+ const contents = [
+ 'a'.repeat(SEARCH_MODEL_CONTENT_MAX_CHARACTERS),
+ 'short content'
+ ]
+ const value = await getModelValue({
+ ...fullOutput,
+ results: fullOutput.results.map((result, index) => ({
+ ...result,
+ content: contents[index]
+ }))
+ })
+
+ expect(
+ (value.results as Array<{ content: string }>).map(
+ result => result.content
+ )
+ ).toEqual(contents)
+ })
+
+ // Providers do not all order by relevance: the firecrawl adapter appends news
+ // results after web results, so dropping a tail would silently remove a whole
+ // category from the model's view.
+ it('keeps every result, in order, however many the provider returned', async () => {
+ const results = Array.from({ length: 24 }, (_, index) => ({
+ title: `Result ${index + 1}`,
+ url: `https://${index + 1}.test`,
+ content: `content ${index + 1}`,
+ label: `S${index + 1}`
+ }))
+ const value = await getModelValue({ ...fullOutput, results })
+ const projectedResults = value.results as typeof results
+
+ expect(projectedResults).toHaveLength(results.length)
+ expect(projectedResults.map(result => result.label)).toEqual(
+ results.map(result => result.label)
+ )
+ })
+
+ it('keeps the complete images array so the model can embed inline image specs', async () => {
+ const images = Array.from({ length: 11 }, (_, index) => ({
+ url: `https://images.test/${index}.png`,
+ description: `image ${index}`
+ }))
+ const value = await getModelValue({ ...fullOutput, images })
+
+ expect(value.images).toBe(images)
+ expect(value.images).toHaveLength(images.length)
+ })
+
+ it('passes through non-string and missing result content', async () => {
+ const results = [
+ {
+ title: 'Number',
+ url: 'https://number.test',
+ content: 123,
+ label: 'S1'
+ },
+ { title: 'Missing', url: 'https://missing.test', label: 'S2' }
+ ]
+ const value = await getModelValue({ ...fullOutput, results })
+
+ expect(value.results).toEqual(results)
+ })
- expect(value.images).toEqual(fullOutput.images)
+ it('passes through a non-array results value', async () => {
+ const results = { malformed: true }
+ const value = await getModelValue({ ...fullOutput, results })
+
+ expect(value.results).toBe(results)
})
it('does not mutate the original output (UI/persistence keep all fields)', async () => {
@@ -86,6 +188,19 @@ describe('search tool toModelOutput', () => {
expect(fullOutput.results.map(result => result.label)).toEqual(['S1', 'S2'])
})
+ it('does not truncate content on the original output object', async () => {
+ const content = 'a'.repeat(SEARCH_MODEL_CONTENT_MAX_CHARACTERS + 10)
+ const output = {
+ ...fullOutput,
+ results: [{ ...fullOutput.results[0], content }]
+ }
+
+ await getModelValue(output)
+
+ expect(output.results[0].content).toBe(content)
+ expect(output.results[0].content).toHaveLength(content.length)
+ })
+
it('returns identical persisted labels across repeated conversion', async () => {
const first = await getModelValue(fullOutput)
const second = await getModelValue(fullOutput)
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 0681b4d0..6bf111fa 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -2,6 +2,7 @@ import { type JSONValue, tool, UIToolInvocation } from 'ai'
import { ToolFailureError } from '@/lib/errors/tool-error'
import { getSearchSchemaForModel } from '@/lib/schema/search'
+import { sliceWithoutSplittingSurrogatePair } from '@/lib/streaming/helpers/slice-without-splitting-surrogate-pair'
import { SearchResults } from '@/lib/types'
import { assignCitationLabels } from '@/lib/utils/citation'
import {
@@ -21,6 +22,10 @@ import {
RecoverableSearchFailure
} from './search/providers/recoverable-error'
+// Bounds each result's content in the model-facing projection only. The
+// ellipsis is counted inside the bound, as it is for historical messages.
+export const SEARCH_MODEL_CONTENT_MAX_CHARACTERS = 600
+
function getOptimizedSearchProviderType(): SearchProviderType {
return (process.env.SEARCH_API as SearchProviderType) || DEFAULT_PROVIDER
}
@@ -253,7 +258,12 @@ export function createSearchTool(
// `results` (dropped defensively for older persisted output), state is a
// streaming marker, and provider/fallback are trace diagnostics.
// toolCallId is dropped too: citations address a result's own `label`, so
- // the opaque id no longer belongs in the model's view. images MUST stay -
+ // the opaque id no longer belongs in the model's view. Result content is
+ // capped, but every result is kept: dropping results would decide relevance
+ // from provider order, and providers do not all order by relevance (the
+ // firecrawl adapter appends news after web). `execute` still yields the
+ // untruncated content, so the UI and the persisted citation targets are
+ // unaffected. images MUST stay -
// getImageSpecPrompt instructs the model to embed URLs verbatim from that
// array. Labels are assigned in `execute` and only passed through here.
toModelOutput: ({ output }) => {
@@ -268,6 +278,27 @@ export function createSearchTool(
delete modelView.provider
delete modelView.fallback
delete modelView.toolCallId
+ if (Array.isArray(modelView.results)) {
+ modelView.results = modelView.results.map(result => {
+ if (!result || typeof result !== 'object' || Array.isArray(result)) {
+ return result
+ }
+
+ const projectedResult = {
+ ...(result as Record<string, unknown>)
+ }
+ if (
+ typeof projectedResult.content === 'string' &&
+ projectedResult.content.length > SEARCH_MODEL_CONTENT_MAX_CHARACTERS
+ ) {
+ projectedResult.content = `${sliceWithoutSplittingSurrogatePair(
+ projectedResult.content,
+ SEARCH_MODEL_CONTENT_MAX_CHARACTERS - 1
+ )}…`
+ }
+ return projectedResult
+ })
+ }
return { type: 'json', value: modelView as JSONValue }
}
})