Skip to content

Bring Anthropic usechat backend to OpenAI-level tool-use parity and stream robustness#2971

Merged
sawka merged 6 commits intomainfrom
copilot/update-anthropic-backend
Mar 5, 2026
Merged

Bring Anthropic usechat backend to OpenAI-level tool-use parity and stream robustness#2971
sawka merged 6 commits intomainfrom
copilot/update-anthropic-backend

Conversation

Copy link
Contributor

Copilot AI commented Mar 3, 2026

This updates pkg/aiusechat/anthropic from partial implementation to full backend parity for core tool-use orchestration and stream behavior. The main gaps were unimplemented tool lifecycle methods, missing persisted tool-use UI state, and weaker disconnect/error handling versus the OpenAI backend.

  • Tool-use lifecycle parity (critical path)

    • Implemented Anthropic backend support for:
      • UpdateToolUseData
      • RemoveToolUseCall
      • GetFunctionCallInputByToolCallId
    • Wired pkg/aiusechat/usechat-backend.go to call Anthropic implementations instead of stubs.
    • Added Anthropic run-step nil-message guard so nil responses are not wrapped into []GenAIMessage{nil}.
  • Persisted tool-use state in Anthropic native messages

    • Added internal ToolUseData storage on Anthropic tool_use blocks.
    • Ensured internal-only fields are stripped before API requests via Clean().
  • UI conversion parity for reloaded history

    • Extended ConvertToUIMessage() to emit data-tooluse parts when tool-use metadata exists, in addition to tool-{name} parts.
  • Streaming UX parity for tool argument deltas

    • Added aiutil.SendToolProgress(...) calls during:
      • input_json_delta (incremental updates)
      • content_block_stop for tool_use (final update)
  • Disconnect/stream robustness

    • Added sse.Err() checks in event handling and decode-error path.
    • Added partial-text extraction on client disconnect and deterministic ordering of partial blocks.
    • Cleans up completed blocks from in-flight state to avoid duplicate partial extraction.
  • Correctness + hygiene alignment

    • Continuation model checks now use AreModelsCompatible(...) (instead of strict string equality).
    • Added hostname sanitization in Anthropic error paths (HTTP error parsing and httpClient.Do failures).
    • Replaced unconditional Anthropic debug log.Printf calls with logutil.DevPrintf.
  • Targeted coverage additions

    • Added Anthropic tests for:
      • function-call lookup by tool call id
      • tool-use data update + removal
      • data-tooluse UI conversion behavior
// usechat-backend.go
func (b *anthropicBackend) RunChatStep(...) (..., []uctypes.GenAIMessage, ...) {
    stopReason, msg, rateLimitInfo, err := anthropic.RunAnthropicChatStep(ctx, sseHandler, chatOpts, cont)
    if msg == nil {
        return stopReason, nil, rateLimitInfo, err
    }
    return stopReason, []uctypes.GenAIMessage{msg}, rateLimitInfo, err
}

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits March 3, 2026 23:51
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Copilot AI changed the title [WIP] Update pkg/aiusechat/anthropic provider for full functionality Bring Anthropic usechat backend to OpenAI-level tool-use parity and stream robustness Mar 3, 2026
Copilot finished work on behalf of sawka March 3, 2026 23:59
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 4, 2026

Deploying waveterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: d629e4a
Status: ✅  Deploy successful!
Preview URL: https://bc2a2902.waveterm.pages.dev
Branch Preview URL: https://copilot-update-anthropic-bac.waveterm.pages.dev

View logs

@sawka sawka marked this pull request as ready for review March 4, 2026 18:46
@kilo-code-bot
Copy link
Contributor

kilo-code-bot bot commented Mar 4, 2026

Code Review Summary

Status: No Critical Issues Found | Recommendation: Merge with observations

Overview

This PR updates the Anthropic backend to support native web search, improves tool use data tracking, and adds better client disconnect handling. The changes are well-structured and include comprehensive test coverage.

Observations

Code Quality Improvements

  1. Better logging: Replaced log.Printf with logutil.DevPrintf for development-only logging (line 956 in anthropic-backend.go)
  2. Proper error handling: Added nil check for msg in RunChatStep (lines 189-191 in usechat-backend.go)
  3. Test coverage: Added comprehensive tests for new functionality (TestGetFunctionCallInputByToolCallId, TestUpdateAndRemoveToolUseCall, TestConvertToUIMessageIncludesToolUseData)
  4. Web search support: Added native web search tool integration with proper counting (lines 133-135, 772-775 in anthropic-backend.go)

Design Decisions Worth Noting

  1. Client disconnect check (lines 673-678 in anthropic-backend.go): The new check at the start of handleAnthropicEvent will check for client disconnect on every SSE event. This is good for responsiveness but adds overhead. The existing check at line 592 already handles this during the decode loop.

  2. BlockMap cleanup (line 883 in anthropic-backend.go): Deleting completed blocks from blockMap after appending to rtnMessage.Content prevents duplicate text extraction on disconnect. This is a good optimization, though it assumes block indices won't be reused (which appears to be the case based on Anthropic's API).

  3. Status check expansion (line 309 in aipanel.tsx): Added status === "submitted" to the streaming check. This makes sense if "submitted" represents a state where the AI is processing but not yet streaming.

  4. Tool definition type change (line 185 in anthropic-backend.go): Changed Tools from []uctypes.ToolDefinition to []any to support both regular tools and the new anthropicWebSearchTool. This is pragmatic but loses some type safety.

Files Reviewed (5 files)
  • frontend/app/aipanel/aipanel.tsx - Status check update
  • pkg/aiusechat/anthropic/anthropic-backend.go - Web search support, disconnect handling, tool progress
  • pkg/aiusechat/anthropic/anthropic-backend_test.go - New test coverage
  • pkg/aiusechat/anthropic/anthropic-convertmessage.go - Tool use data tracking, CRUD operations
  • pkg/aiusechat/usechat-backend.go - Backend interface implementation

The changes follow the project's coding standards and include appropriate test coverage. The new functionality is well-integrated with existing code patterns.

@sawka sawka merged commit edc20f7 into main Mar 5, 2026
10 checks passed
@sawka sawka deleted the copilot/update-anthropic-backend branch March 5, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants