An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(search): project results onto their declared shape for the model (#1032)
Yoshiki Miura30e809b789d1
diff --git a/lib/tools/__tests__/search-to-model-output.test.ts b/lib/tools/__tests__/search-to-model-output.test.tsindex 82fc1f5e..9e84c840 100644--- a/lib/tools/__tests__/search-to-model-output.test.ts+++ b/lib/tools/__tests__/search-to-model-output.test.ts@@ -69,19 +69,69 @@ describe('search tool toModelOutput', () => {expect(value).not.toHaveProperty('toolCallId')})+ it('keeps only model-facing fields on each result', async () => {+ const result = {+ title: 'A',+ url: 'https://a.test',+ content: 'alpha',+ label: 'S1',+ id: 'provider-id',+ score: 0.37,+ raw_content: null,+ images: []+ }+ const value = await getModelValue({+ ...fullOutput,+ results: [result]+ })++ expect(value.results).toEqual([+ {+ title: result.title,+ url: result.url,+ content: result.content,+ label: result.label+ }+ ])+ expect(value.images).toBe(fullOutput.images)+ })++ it('does not add a label to an unlabelled result', async () => {+ const value = await getModelValue({+ ...fullOutput,+ results: [{ title: 'A', url: 'https://a.test', content: 'alpha' }]+ })+ const projectedResult = (value.results as Array<Record<string, unknown>>)[0]++ expect(projectedResult).not.toHaveProperty('label')+ })+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 }]+ results: [+ {+ ...fullOutput.results[0],+ content,+ id: 'provider-id',+ score: 0.37,+ raw_content: null,+ images: []+ }+ ]})- const projectedContent = (value.results as Array<{ content: string }>)[0]- .content+ const projectedResult = (value.results as Array<Record<string, unknown>>)[0]+ const projectedContent = projectedResult.content as stringexpect(projectedContent).toBe(`${content.slice(0, SEARCH_MODEL_CONTENT_MAX_CHARACTERS - 1)}…`)expect(projectedContent).toHaveLength(SEARCH_MODEL_CONTENT_MAX_CHARACTERS)+ expect(projectedResult).not.toHaveProperty('id')+ expect(projectedResult).not.toHaveProperty('score')+ expect(projectedResult).not.toHaveProperty('raw_content')+ expect(projectedResult).not.toHaveProperty('images')})it('does not split a surrogate pair at the boundary', async () => {diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex 6bf111fa..f58a60b5 100644--- a/lib/tools/search.ts+++ b/lib/tools/search.ts@@ -26,6 +26,10 @@ import {// ellipsis is counted inside the bound, as it is for historical messages.export const SEARCH_MODEL_CONTENT_MAX_CHARACTERS = 600+// The declared SearchResultItem shape. Everything else a provider returns is+// kept out of the model-facing projection.+const MODEL_FACING_RESULT_FIELDS = ['url', 'title', 'content', 'label'] as const+function getOptimizedSearchProviderType(): SearchProviderType {return (process.env.SEARCH_API as SearchProviderType) || DEFAULT_PROVIDER}@@ -254,18 +258,14 @@ export function createSearchTool(...(fallback ? { fallback } : {})}},- // Trim the model-facing tool result: citationMap fully duplicates- // `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. 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.+ // Trim the model-facing tool result: the deleted keys are duplicates or+ // diagnostics the model cannot cite. Results are narrowed by allowlist+ // rather than by a delete list, because a provider's own result id would+ // otherwise sit beside `label` as a competing citation target that+ // resolves to nothing. Content is capped but no result is dropped, since+ // provider order is not relevance order. `execute` still yields the full+ // result, so the UI and persisted citations are unaffected. images MUST+ // stay: getImageSpecPrompt has the model copy URLs verbatim from it.toModelOutput: ({ output }) => {if (!output || typeof output !== 'object') {return { type: 'json', value: (output ?? null) as JSONValue }@@ -284,8 +284,12 @@ export function createSearchTool(return result}- const projectedResult = {- ...(result as Record<string, unknown>)+ const sourceResult = result as Record<string, unknown>+ const projectedResult: Record<string, unknown> = {}+ for (const field of MODEL_FACING_RESULT_FIELDS) {+ if (Object.hasOwn(sourceResult, field)) {+ projectedResult[field] = sourceResult[field]+ }}if (typeof projectedResult.content === 'string' &&