Skip to content

Commit dec1878

Browse files
committed
fix: replay Anthropic web search tool results (VCL_Anthropic demo)
1 parent 28001de commit dec1878

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

demos/VCL/pythia-anthropic/Demo.Anthropic.Context.pas

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface
7474
- TTextEditorCodeExecutionToolResultBlockParam (text-editor result echo)
7575
- TBashCodeExecutionToolResultBlockParam (bash result echo)
7676
- TMCPToolResultBlockParam (MCP result echo)
77+
- TWebSearchToolResultBlockParam (web search result echo)
7778
7879
Note on tool_use / tool_result pairing
7980
--------------------------------------
@@ -174,7 +175,8 @@ interface
174175
bskToolResult,
175176
bskTextEditorCodeExecutionToolResult,
176177
bskBashCodeExecutionToolResult,
177-
bskMCPToolResult
178+
bskMCPToolResult,
179+
bskWebSearchToolResult
178180
);
179181

180182
TBlockSnapshot = record
@@ -260,6 +262,9 @@ TAnthropicContext = class(TInterfacedObject, IContext)
260262
function BuildMCPToolResultBlock(
261263
const ASnapshot: TBlockSnapshot): TContentBlockParam;
262264

265+
function BuildWebSearchToolResultBlock(
266+
const ASnapshot: TBlockSnapshot): TContentBlockParam;
267+
263268
procedure AppendTurn(
264269
var AMessages: TMessages;
265270
const ATurn: TChatTurn);
@@ -341,6 +346,9 @@ class function TJsonResponseParser.MapKind(
341346
if SameText(ABlockType, 'mcp_tool_result') then
342347
Exit(bskMCPToolResult);
343348

349+
if SameText(ABlockType, 'web_search_tool_result') then
350+
Exit(bskWebSearchToolResult);
351+
344352
Result := bskUnknown;
345353
end;
346354

@@ -736,6 +744,38 @@ function TAnthropicContext.BuildMCPToolResultBlock(
736744
Result := Block;
737745
end;
738746

747+
function TAnthropicContext.BuildWebSearchToolResultBlock(
748+
const ASnapshot: TBlockSnapshot): TContentBlockParam;
749+
begin
750+
(*--- Server-executed web search result. Pairs with a previously emitted
751+
web_search server_tool_use:
752+
753+
messages.N: `web_search` tool use with id ... was found without
754+
a corresponding `web_search_tool_result` block.
755+
756+
TWebSearchToolResultBlockParam exposes typed Content() overloads
757+
only (TArray<TWebSearchToolResultBlockItem> for the success path,
758+
TWebSearchToolRequestError for the error path) � no string
759+
fallback. Same shortcut as the text-editor / bash builders:
760+
capture the raw `content_block.content` JSON in ASnapshot.Content,
761+
parse it back as a TJSONValue (the wire shape is normally an
762+
array of search-result items) and inject it through the
763+
inherited TJSONParam.Add('content', ...) entry point so the
764+
original structure is replayed verbatim.
765+
*)
766+
var Block := TWebSearchToolResultBlockParam.New
767+
.ToolUseId(ASnapshot.ToolUseId);
768+
769+
if not ASnapshot.Content.Trim.IsEmpty then
770+
begin
771+
var Inner := TJSONObject.ParseJSONValue(ASnapshot.Content);
772+
if Assigned(Inner) then
773+
Block.Add('content', Inner);
774+
end;
775+
776+
Result := Block;
777+
end;
778+
739779
function TAnthropicContext.BuildAssistantContent(
740780
const ATurn: TChatTurn): TArray<TContentBlockParam>;
741781
begin
@@ -772,6 +812,9 @@ function TAnthropicContext.BuildAssistantContent(
772812

773813
bskMCPToolResult:
774814
Result := Result + [BuildMCPToolResultBlock(S)];
815+
816+
bskWebSearchToolResult:
817+
Result := Result + [BuildWebSearchToolResultBlock(S)];
775818
end;
776819
end;
777820

0 commit comments

Comments
 (0)