Skip to content

Commit 84747b3

Browse files
authored
Format Anthropic web-search results and include encrypted_index in citation data (#1778)
1 parent 4062247 commit 84747b3

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/extension/byok/vscode-node/anthropicProvider.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -402,24 +402,21 @@ export class AnthropicLMProvider implements BYOKModelProvider<LanguageModelChatI
402402
}
403403

404404
const results = resultBlock.content.map((result: Anthropic.Messages.WebSearchResultBlock) => ({
405+
type: 'web_search_result',
405406
url: result.url,
406407
title: result.title,
407408
page_age: result.page_age,
408409
encrypted_content: result.encrypted_content
409410
}));
410411

411-
let query: string | undefined;
412-
try {
413-
query = pendingServerToolCall.jsonInput ? JSON.parse(pendingServerToolCall.jsonInput).query : undefined;
414-
} catch (e) {
415-
this._logService.error('Failed to parse server tool call JSON for query:', e);
416-
query = undefined;
417-
}
412+
// Format according to Anthropic's web_search_tool_result specification
413+
const toolResult = {
414+
type: 'web_search_tool_result',
415+
tool_use_id: pendingServerToolCall.toolId,
416+
content: results
417+
};
418418

419-
const searchResults = JSON.stringify({
420-
query: query,
421-
results: results
422-
}, null, 2);
419+
const searchResults = JSON.stringify(toolResult, null, 2);
423420

424421
// TODO: @bhavyaus - instead of just pushing text, create a specialized WebSearchResult part
425422
progress.report(new LanguageModelToolResultPart(
@@ -440,24 +437,25 @@ export class AnthropicLMProvider implements BYOKModelProvider<LanguageModelChatI
440437
// TODO: @bhavyaus - instead of just pushing text, create a specialized Citation part
441438
const citation = chunk.delta.citation as Anthropic.Messages.CitationsWebSearchResultLocation;
442439
if (citation.type === 'web_search_result_location') {
443-
// Format as: Reference: [Title](URL) with cited text in italics
444-
const referenceText = `${localize('anthropic.citation.reference', 'Reference')}: [${citation.title}](${citation.url})\n*${citation.cited_text}*`;
445-
446-
// Store the full citation data including encrypted_index for multi-turn conversations
447-
const citationData = JSON.stringify({
448-
cited_text: citation.cited_text,
449-
title: citation.title,
440+
// Format citation according to Anthropic specification
441+
const citationData = {
442+
type: 'web_search_result_location',
450443
url: citation.url,
451-
encrypted_index: citation.encrypted_index
452-
}, null, 2);
444+
title: citation.title,
445+
encrypted_index: citation.encrypted_index,
446+
cited_text: citation.cited_text
447+
};
448+
449+
// Format citation as readable blockquote with source link
450+
const referenceText = `\n> "${citation.cited_text}" — [${localize('anthropic.citation.source', 'Source')}](${citation.url})\n\n`;
453451

454452
// Report formatted reference text to user
455-
progress.report(new LanguageModelTextPart(`\n${referenceText}\n`));
453+
progress.report(new LanguageModelTextPart(referenceText));
456454

457-
// Also store the full citation data for conversation context
455+
// Store the citation data in the correct format for multi-turn conversations
458456
progress.report(new LanguageModelToolResultPart(
459457
'citation',
460-
[new LanguageModelTextPart(citationData)]
458+
[new LanguageModelTextPart(JSON.stringify(citationData, null, 2))]
461459
));
462460
}
463461
}

0 commit comments

Comments
 (0)