waveterm/pkg
Copilot edc20f7ec0
Bring Anthropic usechat backend to OpenAI-level tool-use parity and stream robustness (#2971)
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

```go
// 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
}
```

<!-- START COPILOT CODING AGENT TIPS -->
---

 Let Copilot coding agent [set things up for
you](https://github.com/wavetermdev/waveterm/issues/new?title=+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Co-authored-by: sawka <mike@commandline.dev>
2026-03-04 16:20:50 -08:00
..
aiusechat Bring Anthropic usechat backend to OpenAI-level tool-use parity and stream robustness (#2971) 2026-03-04 16:20:50 -08:00
authkey Happy new year! (#1684) 2025-01-04 20:56:57 -08:00
baseds very large refactor of wshrouter (#2732) 2026-01-01 17:44:00 -08:00
blockcontroller Fix for Claude Code Scroll to Top Bug (#2956) 2026-02-27 15:34:35 -08:00
blocklogger conn updates 3 (#1711) 2025-01-10 14:09:32 -08:00
buildercontroller more misc builder improvements (#2592) 2025-11-24 13:37:24 -08:00
eventbus waveapps builder window (scaffolding, restructure AI panel to work in both builder and tab windows) (#2482) 2025-10-26 17:48:01 -07:00
faviconcache web bookmarks (#1930) 2025-02-07 16:11:40 -08:00
filebackup Add Write File Tools to WaveAI (#2492) 2025-10-31 14:40:03 -07:00
filestore Fix windows path for wsh url (#1798) 2025-01-22 17:28:59 -08:00
genconn WSL Updates for New Architecture (#1756) 2025-01-16 15:54:58 -08:00
gogen Add native 2+ arg RPC support and wire a concrete TestMultiArgCommand through server, generated clients, and CLI (#2963) 2026-03-02 12:29:04 -08:00
ijson Happy new year! (#1684) 2025-01-04 20:56:57 -08:00
jobcontroller fix windows compilation error (#2862) 2026-02-11 14:57:27 -08:00
jobmanager update logging for jobmanager. (#2854) 2026-02-10 18:43:54 -08:00
panichandler new tevents analytics framework (#1894) 2025-02-03 15:32:44 -08:00
remote conn error sub-codes for better classification (#2889) 2026-02-19 11:18:52 -08:00
schema settings schema.json (#1874) 2025-02-03 14:20:50 -08:00
secretstore minor v0.13 fixes (#2649) 2025-12-08 21:58:54 -08:00
service Persistent Terminal Sessions (+ improvements and bug fixes) (#2806) 2026-01-28 13:30:48 -08:00
shellexec fix windows compilation error (#2862) 2026-02-11 14:57:27 -08:00
streamclient stress test for the new RPC streaming primitives (+ bug fixes) (#2828) 2026-02-05 14:48:12 -08:00
suggestion Do not allow large/recursive file transfers (for now), remove s3 and wavefile fs implementations (#2808) 2026-01-28 14:28:31 -08:00
telemetry quick updates to get apptype (#2969) 2026-03-03 15:19:23 -08:00
trimquotes Add license identifiers to missing files (#1806) 2025-01-22 20:06:36 -08:00
tsgen Add native 2+ arg RPC support and wire a concrete TestMultiArgCommand through server, generated clients, and CLI (#2963) 2026-03-02 12:29:04 -08:00
tsunamiutil builder secrets, builder config/data tab hooked up (#2581) 2025-11-21 10:36:51 -08:00
userinput Working on Test Harness for Remote Connections (#2829) 2026-02-09 21:50:34 -08:00
util Make Wave home config writes atomic and serialized to avoid watcher partial reads (#2945) 2026-02-27 15:43:37 -08:00
utilds conn error sub-codes for better classification (#2889) 2026-02-19 11:18:52 -08:00
vdom Happy new year! (#1684) 2025-01-04 20:56:57 -08:00
waveai update to gpt-5.1 (#2552) 2025-11-13 21:10:28 -08:00
waveapp Persistent Terminal Sessions (+ improvements and bug fixes) (#2806) 2026-01-28 13:30:48 -08:00
waveappstore show apps icon in the widgets bar which shows a tsunami app launcher (#2554) 2025-11-14 09:34:14 -08:00
waveapputil v0.12.4 release notes (#2590) 2025-11-21 16:03:15 -08:00
wavebase quick updates to get apptype (#2969) 2026-03-03 15:19:23 -08:00
wavejwt Persistent Terminal Sessions (+ improvements and bug fixes) (#2806) 2026-01-28 13:30:48 -08:00
waveobj Add terminal cursor style/blink config with block-level overrides (#2933) 2026-02-25 12:40:20 -08:00
wcloud very large refactor of wshrouter (#2732) 2026-01-01 17:44:00 -08:00
wconfig Centralize proxy HTTP client creation in aiutil and remove redundant backend tests (#2961) 2026-03-02 11:16:50 -08:00
wcore add a preview component for tabs, update contextmenu model w/ getInstance method (#2927) 2026-02-25 12:39:07 -08:00
web Do not allow large/recursive file transfers (for now), remove s3 and wavefile fs implementations (#2808) 2026-01-28 14:28:31 -08:00
wps Generate WaveEvent as a typed discriminated union with explicit null payloads for no-data events (#2899) 2026-02-20 17:04:03 -08:00
wshrpc Add native 2+ arg RPC support and wire a concrete TestMultiArgCommand through server, generated clients, and CLI (#2963) 2026-03-02 12:29:04 -08:00
wshutil Add native 2+ arg RPC support and wire a concrete TestMultiArgCommand through server, generated clients, and CLI (#2963) 2026-03-02 12:29:04 -08:00
wsl More Durable Shell Bug Fixes (#2822) 2026-02-04 11:10:21 -08:00
wslconn increase initial command timeout to 30s from 5s (slow WSL start) (#2853) 2026-02-10 15:33:08 -08:00
wstore Persistent Terminal Sessions (+ improvements and bug fixes) (#2806) 2026-01-28 13:30:48 -08:00