fix: cut the middle of a capped hub result instead of repeating its tail

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-08-25 12:28:13 +02:00
parent aa51731ad1
commit 03d06db7a2
2 changed files with 11 additions and 9 deletions
@@ -1433,7 +1433,10 @@ describe('createSearchHubScriptsTool', () => {
toolCallbacks: { setToolStatus: vi.fn() }
} as any)
expect(JSON.parse(raw).results.map((r: any) => r.integration)).toContain('salesforce')
const integrations = JSON.parse(raw).results.map((r: any) => r.integration)
expect(integrations).toContain('salesforce')
// reserving room for the tail must not re-emit it when nothing was cut
expect(new Set(integrations).size).toBe(integrations.length)
})
// Ranking buries an integration the query names when other integrations' scripts
@@ -1519,14 +1519,13 @@ export const createSearchHubScriptsTool = (withContent: boolean = false) => ({
}
// Each result costs a content fetch, so cap the fan-out when content is wanted.
// Keep the tail: hits appended for a named integration sit there, and dropping
// them would undo the reason they were fetched.
const matches = withContent
? [
...scripts.slice(0, MAX_FETCHED_HUB_SCRIPTS - mentionedHits),
...scripts.slice(scripts.length - mentionedHits)
]
: scripts
// Cut from the middle: hits appended for a named integration sit at the tail,
// and dropping those would undo the reason they were fetched.
let matches = scripts
if (withContent && scripts.length > MAX_FETCHED_HUB_SCRIPTS) {
const mentioned = scripts.slice(scripts.length - mentionedHits)
matches = [...scripts.slice(0, MAX_FETCHED_HUB_SCRIPTS - mentioned.length), ...mentioned]
}
toolCallbacks.setToolStatus(toolId, {
content: `Found ${matches.length} hub script${matches.length === 1 ? '' : 's'} for ${subject}`
})