AI-Studio/app/MindWork AI Studio/Provider/Anthropic/AnthropicToolCallingAdapter.cs
Thorsten Sommer 6ce7d856a3
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Count the tool conversation in the token count (#973)
2026-09-14 19:54:23 +02:00

109 lines
4.5 KiB
C#

using AIStudio.Tools.ToolCallingSystem;
using AIStudio.Tools.ToolCallingSystem.Harness;
namespace AIStudio.Provider.Anthropic;
/// <summary>
/// Speaks the Anthropic messages wire format for the tool calling loop.
/// </summary>
/// <remarks>
/// Anthropic works in content blocks rather than in separate message kinds: the model's turn is
/// one assistant message whose blocks may mix text, thinking, and tool uses, and the results go
/// back as tool result blocks inside a single user message. That difference is what made this
/// provider hard to support before the loop and the wire format were separated — it is now the
/// only thing this class is about.
/// </remarks>
public sealed class AnthropicToolCallingAdapter(Model chatModel, IList<IMessageBase> baseMessages, string systemPrompt, int maxTokens,
IDictionary<string, object> apiParameters, IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools,
Func<ChatRequest, CancellationToken, Task<AnthropicResponse?>> executeRequestAsync) : IToolCallingProviderAdapter
{
private readonly List<IMessageBase> internalMessages = [];
private readonly List<AnthropicToolResultContent> pendingToolResults = [];
private readonly List<string> recordedRequestTexts = [];
private readonly List<AnthropicTool> tools = runnableTools.Select(x => ProviderToolAdapters.ToAnthropicTool(x.Definition)).ToList();
private AnthropicResponse? lastResponse;
/// <inheritdoc />
public IReadOnlyList<string> RecordedRequestTexts => this.recordedRequestTexts;
/// <inheritdoc />
public async Task<ToolCallingRound?> ExecuteRoundAsync(string? finalResponseInstruction, bool includeTools, CancellationToken token = default)
{
//
// The results of the previous round are flushed here rather than when they were recorded:
// they all belong in one user message, and only now is it certain that no more are coming.
//
if (this.pendingToolResults.Count > 0)
{
this.internalMessages.Add(new AnthropicToolResultMessage([..this.pendingToolResults]));
this.pendingToolResults.Clear();
}
var response = await executeRequestAsync(new ChatRequest
{
Model = chatModel.Id,
Messages = [..baseMessages, ..this.internalMessages],
System = finalResponseInstruction is null
? systemPrompt
: $"{systemPrompt}{Environment.NewLine}{Environment.NewLine}{finalResponseInstruction}",
MaxTokens = maxTokens,
Stream = false,
Tools = includeTools && this.tools.Count > 0 ? this.tools : null,
AdditionalApiParameters = apiParameters,
}, token);
if (response is null)
return null;
this.lastResponse = response;
return new ToolCallingRound(
response.GetTextOutput(),
response.GetToolUses()
.Select(toolUse => new ToolCallingRequestedCall(
toolUse.Id,
toolUse.Name,
toolUse.Arguments,
ToolExecutor.IsValidArgumentsJson(toolUse.Arguments)))
.ToList(),
[]);
}
/// <inheritdoc />
public void RecordAssistantTurn()
{
if (this.lastResponse is null)
return;
//
// The blocks go back exactly as they arrived. Thinking blocks in particular have to be
// returned unchanged for the model to continue from them.
//
this.internalMessages.Add(new AnthropicMessage([..this.lastResponse.Content]));
//
// And they are counted exactly as they arrived, for the same reason: a thinking block is
// sent back whole, so what it costs is what it says, not what we could read out of it.
//
foreach (var contentBlock in this.lastResponse.Content)
this.recordedRequestTexts.Add(contentBlock.GetRawText());
}
/// <inheritdoc />
public void RecordToolResult(string callId, string content, bool isError = false)
{
this.pendingToolResults.Add(new AnthropicToolResultContent
{
ToolUseId = callId,
Content = content,
IsError = isError,
});
//
// Noted here rather than when the results are flushed into their message: the round they
// belong to is over, and whoever asks in the meantime has to see what it cost.
//
if (!string.IsNullOrWhiteSpace(content))
this.recordedRequestTexts.Add(content);
}
}