diff --git a/frontend/app/aipanel/aipanel.tsx b/frontend/app/aipanel/aipanel.tsx index 46780455c2..dded015f85 100644 --- a/frontend/app/aipanel/aipanel.tsx +++ b/frontend/app/aipanel/aipanel.tsx @@ -306,7 +306,7 @@ const AIPanelComponentInner = memo(() => { }; useEffect(() => { - globalStore.set(model.isAIStreaming, status == "streaming"); + globalStore.set(model.isAIStreaming, status === "streaming" || status === "submitted"); }, [status]); useEffect(() => { diff --git a/package-lock.json b/package-lock.json index 269181d32a..f11d429435 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "waveterm", - "version": "0.14.1-beta.1", + "version": "0.14.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "waveterm", - "version": "0.14.1-beta.1", + "version": "0.14.1", "hasInstallScript": true, "license": "Apache-2.0", "workspaces": [ diff --git a/pkg/aiusechat/anthropic/anthropic-backend.go b/pkg/aiusechat/anthropic/anthropic-backend.go index b52b4a6797..02070b1bf8 100644 --- a/pkg/aiusechat/anthropic/anthropic-backend.go +++ b/pkg/aiusechat/anthropic/anthropic-backend.go @@ -10,8 +10,9 @@ import ( "errors" "fmt" "io" - "log" "net/http" + "net/url" + "sort" "strings" "time" @@ -20,6 +21,7 @@ import ( "github.com/wavetermdev/waveterm/pkg/aiusechat/aiutil" "github.com/wavetermdev/waveterm/pkg/aiusechat/chatstore" "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes" + "github.com/wavetermdev/waveterm/pkg/util/logutil" "github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/web/sse" ) @@ -56,10 +58,11 @@ func (m *anthropicChatMessage) GetUsage() *uctypes.AIUsage { } return &uctypes.AIUsage{ - APIType: uctypes.APIType_AnthropicMessages, - Model: m.Usage.Model, - InputTokens: m.Usage.InputTokens, - OutputTokens: m.Usage.OutputTokens, + APIType: uctypes.APIType_AnthropicMessages, + Model: m.Usage.Model, + InputTokens: m.Usage.InputTokens, + OutputTokens: m.Usage.OutputTokens, + NativeWebSearchCount: m.Usage.NativeWebSearchCount, } } @@ -95,8 +98,9 @@ type anthropicMessageContentBlock struct { Name string `json:"name,omitempty"` Input interface{} `json:"input,omitempty"` - ToolUseDisplayName string `json:"toolusedisplayname,omitempty"` // internal field (cannot marshal to API, must be stripped) - ToolUseShortDescription string `json:"tooluseshortdescription,omitempty"` // internal field (cannot marshal to API, must be stripped) + ToolUseDisplayName string `json:"toolusedisplayname,omitempty"` // internal field (cannot marshal to API, must be stripped) + ToolUseShortDescription string `json:"tooluseshortdescription,omitempty"` // internal field (cannot marshal to API, must be stripped) + ToolUseData *uctypes.UIMessageDataToolUse `json:"toolusedata,omitempty"` // internal field (cannot marshal to API, must be stripped) // Tool result content ToolUseID string `json:"tool_use_id,omitempty"` @@ -154,6 +158,7 @@ func (b *anthropicMessageContentBlock) Clean() *anthropicMessageContentBlock { rtn.SourcePreviewUrl = "" rtn.ToolUseDisplayName = "" rtn.ToolUseShortDescription = "" + rtn.ToolUseData = nil if rtn.Source != nil { rtn.Source = rtn.Source.Clean() } @@ -177,10 +182,15 @@ type anthropicStreamRequest struct { Stream bool `json:"stream"` System []anthropicMessageContentBlock `json:"system,omitempty"` ToolChoice any `json:"tool_choice,omitempty"` - Tools []uctypes.ToolDefinition `json:"tools,omitempty"` + Tools []any `json:"tools,omitempty"` // *uctypes.ToolDefinition or *anthropicWebSearchTool Thinking *anthropicThinkingOpts `json:"thinking,omitempty"` } +type anthropicWebSearchTool struct { + Type string `json:"type"` // "web_search_20250305" + Name string `json:"name"` // "web_search" +} + type anthropicCacheControl struct { Type string `json:"type"` // "ephemeral" TTL string `json:"ttl"` // "5m" or "1h" @@ -228,8 +238,9 @@ type anthropicUsageType struct { CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"` CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"` - // internal field for Wave use (not sent to API) - Model string `json:"model,omitempty"` + // internal fields for Wave use (not sent to API) + Model string `json:"model,omitempty"` + NativeWebSearchCount int `json:"nativewebsearchcount,omitempty"` // for reference, but we dont keep thsese up to date or track them CacheCreation *anthropicCacheCreationType `json:"cache_creation,omitempty"` // breakdown of cached tokens by TTL @@ -290,14 +301,16 @@ type partialJSON struct { } type streamingState struct { - blockMap map[int]*blockState - toolCalls []uctypes.WaveToolCall - stopFromDelta string - msgID string - model string - stepStarted bool - rtnMessage *anthropicChatMessage - usage *anthropicUsageType + blockMap map[int]*blockState + toolCalls []uctypes.WaveToolCall + stopFromDelta string + msgID string + model string + stepStarted bool + rtnMessage *anthropicChatMessage + usage *anthropicUsageType + chatOpts uctypes.WaveChatOpts + webSearchCount int } func (p *partialJSON) Write(s string) { @@ -330,6 +343,20 @@ func (p *partialJSON) FinalObject() (json.RawMessage, error) { } } +// sanitizeHostnameInError removes the Wave cloud hostname from error messages +func sanitizeHostnameInError(err error) error { + if err == nil { + return nil + } + errStr := err.Error() + parsedURL, parseErr := url.Parse(uctypes.DefaultAIEndpoint) + if parseErr == nil && parsedURL.Host != "" && strings.Contains(errStr, parsedURL.Host) { + errStr = strings.ReplaceAll(errStr, uctypes.DefaultAIEndpoint, "AI service") + errStr = strings.ReplaceAll(errStr, parsedURL.Host, "host") + } + return fmt.Errorf("%s", errStr) +} + // makeThinkingOpts creates thinking options based on level and max tokens func makeThinkingOpts(thinkingLevel string, maxTokens int) *anthropicThinkingOpts { if thinkingLevel != uctypes.ThinkingLevelMedium && thinkingLevel != uctypes.ThinkingLevelHigh { @@ -373,13 +400,13 @@ func parseAnthropicHTTPError(resp *http.Response) error { // Try to parse as Anthropic error format first var eresp anthropicHTTPErrorResponse if err := json.Unmarshal(slurp, &eresp); err == nil && eresp.Error.Message != "" { - return fmt.Errorf("anthropic %s: %s", resp.Status, eresp.Error.Message) + return sanitizeHostnameInError(fmt.Errorf("anthropic %s: %s", resp.Status, eresp.Error.Message)) } // Try to parse as proxy error format var proxyErr uctypes.ProxyErrorResponse if err := json.Unmarshal(slurp, &proxyErr); err == nil && !proxyErr.Success && proxyErr.Error != "" { - return fmt.Errorf("anthropic %s: %s", resp.Status, proxyErr.Error) + return sanitizeHostnameInError(fmt.Errorf("anthropic %s: %s", resp.Status, proxyErr.Error)) } // Fall back to truncated raw response @@ -387,7 +414,7 @@ func parseAnthropicHTTPError(resp *http.Response) error { if msg == "" { msg = "unknown error" } - return fmt.Errorf("anthropic %s: %s", resp.Status, msg) + return sanitizeHostnameInError(fmt.Errorf("anthropic %s: %s", resp.Status, msg)) } func RunAnthropicChatStep( @@ -426,7 +453,7 @@ func RunAnthropicChatStep( // Validate continuation if provided if cont != nil { - if chatOpts.Config.Model != cont.Model { + if !uctypes.AreModelsCompatible(chat.APIType, chatOpts.Config.Model, cont.Model) { return nil, nil, nil, fmt.Errorf("cannot continue with a different model, model:%q, cont-model:%q", chatOpts.Config.Model, cont.Model) } } @@ -461,7 +488,7 @@ func RunAnthropicChatStep( resp, err := httpClient.Do(req) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, sanitizeHostnameInError(err) } defer resp.Body.Close() @@ -499,7 +526,7 @@ func RunAnthropicChatStep( // Use eventsource decoder for proper SSE parsing decoder := eventsource.NewDecoder(resp.Body) - stopReason, rtnMessage := handleAnthropicStreamingResp(ctx, sse, decoder, cont) + stopReason, rtnMessage := handleAnthropicStreamingResp(ctx, sse, decoder, cont, chatOpts) return stopReason, rtnMessage, rateLimitInfo, nil } @@ -509,6 +536,7 @@ func handleAnthropicStreamingResp( sse *sse.SSEHandlerCh, decoder *eventsource.Decoder, cont *uctypes.WaveContinueResponse, + chatOpts uctypes.WaveChatOpts, ) (*uctypes.WaveStopReason, *anthropicChatMessage) { // Per-response state state := &streamingState{ @@ -518,6 +546,7 @@ func handleAnthropicStreamingResp( Role: "assistant", Content: []anthropicMessageContentBlock{}, }, + chatOpts: chatOpts, } var rtnStopReason *uctypes.WaveStopReason @@ -526,8 +555,10 @@ func handleAnthropicStreamingResp( defer func() { // Set usage in the returned message if state.usage != nil { - // Set model in usage for internal use state.usage.Model = state.model + if state.webSearchCount > 0 { + state.usage.NativeWebSearchCount = state.webSearchCount + } state.rtnMessage.Usage = state.usage } @@ -558,6 +589,13 @@ func handleAnthropicStreamingResp( // Normal end of stream break } + if sse.Err() != nil { + return &uctypes.WaveStopReason{ + Kind: uctypes.StopKindCanceled, + ErrorType: "client_disconnect", + ErrorText: "client disconnected", + }, extractPartialTextFromState(state) + } // transport error mid-stream _ = sse.AiMsgError(err.Error()) return &uctypes.WaveStopReason{ @@ -587,6 +625,37 @@ func handleAnthropicStreamingResp( return rtnStopReason, state.rtnMessage } +func extractPartialTextFromState(state *streamingState) *anthropicChatMessage { + var content []anthropicMessageContentBlock + for _, block := range state.rtnMessage.Content { + if block.Type == "text" && block.Text != "" { + content = append(content, block) + } + } + var partialIdx []int + for idx, st := range state.blockMap { + if st.kind == blockText && st.contentBlock != nil && st.contentBlock.Text != "" { + partialIdx = append(partialIdx, idx) + } + } + sort.Ints(partialIdx) + for _, idx := range partialIdx { + st := state.blockMap[idx] + if st.kind == blockText && st.contentBlock != nil && st.contentBlock.Text != "" { + content = append(content, *st.contentBlock) + } + } + if len(content) == 0 { + return nil + } + return &anthropicChatMessage{ + MessageId: state.rtnMessage.MessageId, + Role: "assistant", + Content: content, + Usage: state.rtnMessage.Usage, + } +} + // handleAnthropicEvent processes one SSE event block. It may emit SSE parts // and/or return a StopReason when the stream is complete. // @@ -601,6 +670,13 @@ func handleAnthropicEvent( state *streamingState, cont *uctypes.WaveContinueResponse, ) (stopFromDelta *string, final *uctypes.WaveStopReason) { + if err := sse.Err(); err != nil { + return nil, &uctypes.WaveStopReason{ + Kind: uctypes.StopKindCanceled, + ErrorType: "client_disconnect", + ErrorText: "client disconnected", + } + } eventName := event.Event() data := event.Data() switch eventName { @@ -693,6 +769,10 @@ func handleAnthropicEvent( } state.blockMap[idx] = st _ = sse.AiMsgToolInputStart(tcID, tName) + case "server_tool_use": + if ev.ContentBlock.Name == "web_search" { + state.webSearchCount++ + } default: // ignore other block types gracefully per Anthropic guidance :contentReference[oaicite:18]{index=18} } @@ -732,6 +812,7 @@ func handleAnthropicEvent( if st.kind == blockToolUse { st.accumJSON.Write(ev.Delta.PartialJSON) _ = sse.AiMsgToolInputDelta(st.toolCallID, ev.Delta.PartialJSON) + aiutil.SendToolProgress(st.toolCallID, st.toolName, st.accumJSON.Bytes(), state.chatOpts, sse, true) } case "signature_delta": // Accumulate signature for thinking blocks @@ -784,6 +865,7 @@ func handleAnthropicEvent( } } _ = sse.AiMsgToolInputAvailable(st.toolCallID, st.toolName, raw) + aiutil.SendToolProgress(st.toolCallID, st.toolName, raw, state.chatOpts, sse, false) state.toolCalls = append(state.toolCalls, uctypes.WaveToolCall{ ID: st.toolCallID, Name: st.toolName, @@ -798,6 +880,9 @@ func handleAnthropicEvent( } state.rtnMessage.Content = append(state.rtnMessage.Content, toolUseBlock) } + // extractPartialTextFromState reads blockMap for still-in-flight content, so remove completed blocks + // once they have been appended to rtnMessage.Content to avoid duplicate text on disconnect. + delete(state.blockMap, *ev.Index) return nil, nil case "message_delta": @@ -868,7 +953,7 @@ func handleAnthropicEvent( } default: - log.Printf("unknown anthropic event type: %s", eventName) + logutil.DevPrintf("unknown anthropic event type: %s", eventName) return nil, nil } } diff --git a/pkg/aiusechat/anthropic/anthropic-backend_test.go b/pkg/aiusechat/anthropic/anthropic-backend_test.go index 8d9acb78e2..71e89bfb2f 100644 --- a/pkg/aiusechat/anthropic/anthropic-backend_test.go +++ b/pkg/aiusechat/anthropic/anthropic-backend_test.go @@ -6,6 +6,7 @@ package anthropic import ( "testing" + "github.com/wavetermdev/waveterm/pkg/aiusechat/chatstore" "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes" ) @@ -69,3 +70,97 @@ func TestConvertPartsToAnthropicBlocks_SkipsUnknownTypes(t *testing.T) { t.Errorf("expected second text 'Another valid text', got %v", block2.Text) } } + +func TestGetFunctionCallInputByToolCallId(t *testing.T) { + toolData := &uctypes.UIMessageDataToolUse{ToolCallId: "call-1", ToolName: "read_file", Status: uctypes.ToolUseStatusPending} + chat := uctypes.AIChat{ + NativeMessages: []uctypes.GenAIMessage{ + &anthropicChatMessage{ + MessageId: "m1", + Role: "assistant", + Content: []anthropicMessageContentBlock{ + {Type: "tool_use", ID: "call-1", Name: "read_file", Input: map[string]interface{}{"path": "/tmp/a"}, ToolUseData: toolData}, + }, + }, + }, + } + fnCall := GetFunctionCallInputByToolCallId(chat, "call-1") + if fnCall == nil { + t.Fatalf("expected function call input") + } + if fnCall.CallId != "call-1" || fnCall.Name != "read_file" { + t.Fatalf("unexpected function call input: %#v", fnCall) + } + if fnCall.Arguments != "{\"path\":\"/tmp/a\"}" { + t.Fatalf("unexpected arguments: %s", fnCall.Arguments) + } + if fnCall.ToolUseData == nil || fnCall.ToolUseData.ToolCallId != "call-1" { + t.Fatalf("expected tool use data") + } +} + +func TestUpdateAndRemoveToolUseCall(t *testing.T) { + chatID := "anthropic-test-tooluse" + chatstore.DefaultChatStore.Delete(chatID) + defer chatstore.DefaultChatStore.Delete(chatID) + + aiOpts := &uctypes.AIOptsType{ + APIType: uctypes.APIType_AnthropicMessages, + Model: "claude-sonnet-4-5", + APIVersion: AnthropicDefaultAPIVersion, + } + msg := &anthropicChatMessage{ + MessageId: "m1", + Role: "assistant", + Content: []anthropicMessageContentBlock{ + {Type: "text", Text: "start"}, + {Type: "tool_use", ID: "call-1", Name: "read_file", Input: map[string]interface{}{"path": "/tmp/a"}}, + }, + } + if err := chatstore.DefaultChatStore.PostMessage(chatID, aiOpts, msg); err != nil { + t.Fatalf("failed to seed chat: %v", err) + } + + newData := uctypes.UIMessageDataToolUse{ToolCallId: "call-1", ToolName: "read_file", Status: uctypes.ToolUseStatusCompleted} + if err := UpdateToolUseData(chatID, "call-1", newData); err != nil { + t.Fatalf("update failed: %v", err) + } + + chat := chatstore.DefaultChatStore.Get(chatID) + updated := chat.NativeMessages[0].(*anthropicChatMessage) + if updated.Content[1].ToolUseData == nil || updated.Content[1].ToolUseData.Status != uctypes.ToolUseStatusCompleted { + t.Fatalf("tool use data not updated") + } + + if err := RemoveToolUseCall(chatID, "call-1"); err != nil { + t.Fatalf("remove failed: %v", err) + } + chat = chatstore.DefaultChatStore.Get(chatID) + updated = chat.NativeMessages[0].(*anthropicChatMessage) + if len(updated.Content) != 1 || updated.Content[0].Type != "text" { + t.Fatalf("expected tool_use block removed, got %#v", updated.Content) + } +} + +func TestConvertToUIMessageIncludesToolUseData(t *testing.T) { + msg := &anthropicChatMessage{ + MessageId: "m1", + Role: "assistant", + Content: []anthropicMessageContentBlock{ + { + Type: "tool_use", + ID: "call-1", + Name: "read_file", + Input: map[string]interface{}{"path": "/tmp/a"}, + ToolUseData: &uctypes.UIMessageDataToolUse{ToolCallId: "call-1", ToolName: "read_file", Status: uctypes.ToolUseStatusPending}, + }, + }, + } + ui := msg.ConvertToUIMessage() + if ui == nil || len(ui.Parts) != 2 { + t.Fatalf("expected tool and data-tooluse parts, got %#v", ui) + } + if ui.Parts[0].Type != "tool-read_file" || ui.Parts[1].Type != "data-tooluse" { + t.Fatalf("unexpected part types: %#v", ui.Parts) + } +} diff --git a/pkg/aiusechat/anthropic/anthropic-convertmessage.go b/pkg/aiusechat/anthropic/anthropic-convertmessage.go index 7fec54b1ab..552cc8080c 100644 --- a/pkg/aiusechat/anthropic/anthropic-convertmessage.go +++ b/pkg/aiusechat/anthropic/anthropic-convertmessage.go @@ -13,10 +13,13 @@ import ( "log" "net/http" "regexp" + "slices" "strings" "github.com/google/uuid" + "github.com/wavetermdev/waveterm/pkg/aiusechat/chatstore" "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes" + "github.com/wavetermdev/waveterm/pkg/util/logutil" "github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/wavebase" ) @@ -119,24 +122,23 @@ func buildAnthropicHTTPRequest(ctx context.Context, msgs []anthropicInputMessage reqBody.System = systemBlocks } - if len(chatOpts.Tools) > 0 { - cleanedTools := make([]uctypes.ToolDefinition, len(chatOpts.Tools)) - for i, tool := range chatOpts.Tools { - cleanedTools[i] = *tool.Clean() - } - reqBody.Tools = cleanedTools + for _, tool := range chatOpts.Tools { + cleanedTool := tool.Clean() + reqBody.Tools = append(reqBody.Tools, cleanedTool) } for _, tool := range chatOpts.TabTools { - cleanedTool := *tool.Clean() + cleanedTool := tool.Clean() reqBody.Tools = append(reqBody.Tools, cleanedTool) } + if chatOpts.AllowNativeWebSearch { + reqBody.Tools = append(reqBody.Tools, &anthropicWebSearchTool{Type: "web_search_20250305", Name: "web_search"}) + } // Enable extended thinking based on level reqBody.Thinking = makeThinkingOpts(opts.ThinkingLevel, maxTokens) // pretty print json of anthropicMsgs if jsonStr, err := utilfn.MarshalIndentNoHTMLString(convertedMsgs, "", " "); err == nil { - log.Printf("system-prompt: %v\n", chatOpts.SystemPrompt) var toolNames []string for _, tool := range chatOpts.Tools { toolNames = append(toolNames, tool.Name) @@ -144,9 +146,12 @@ func buildAnthropicHTTPRequest(ctx context.Context, msgs []anthropicInputMessage for _, tool := range chatOpts.TabTools { toolNames = append(toolNames, tool.Name) } - log.Printf("tools: %s\n", strings.Join(toolNames, ", ")) - log.Printf("anthropicMsgs JSON:\n%s", jsonStr) - log.Printf("has-api-key: %v\n", opts.APIToken != "") + if chatOpts.AllowNativeWebSearch { + toolNames = append(toolNames, "web_search[server]") + } + logutil.DevPrintf("tools: %s\n", strings.Join(toolNames, ", ")) + logutil.DevPrintf("anthropicMsgs JSON:\n%s", jsonStr) + logutil.DevPrintf("has-api-key: %v\n", opts.APIToken != "") } var buf bytes.Buffer @@ -698,6 +703,13 @@ func (m *anthropicChatMessage) ConvertToUIMessage() *uctypes.UIMessage { ToolCallID: block.ID, Input: block.Input, }) + if block.ToolUseData != nil { + parts = append(parts, uctypes.UIMessagePart{ + Type: "data-tooluse", + ID: block.ID, + Data: *block.ToolUseData, + }) + } } default: // For now, skip all other types (will implement later) @@ -827,3 +839,102 @@ func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) { Messages: uiMessages, }, nil } + +func GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput { + for _, genMsg := range aiChat.NativeMessages { + chatMsg, ok := genMsg.(*anthropicChatMessage) + if !ok { + continue + } + for _, block := range chatMsg.Content { + if block.Type != "tool_use" || block.ID != toolCallId { + continue + } + argsInput := block.Input + if argsInput == nil { + argsInput = map[string]interface{}{} + } + argsBytes, err := json.Marshal(argsInput) + if err != nil { + continue + } + return &uctypes.AIFunctionCallInput{ + CallId: block.ID, + Name: block.Name, + Arguments: string(argsBytes), + ToolUseData: block.ToolUseData, + } + } + } + return nil +} + +func UpdateToolUseData(chatId string, toolCallId string, toolUseData uctypes.UIMessageDataToolUse) error { + chat := chatstore.DefaultChatStore.Get(chatId) + if chat == nil { + return fmt.Errorf("chat not found: %s", chatId) + } + for _, genMsg := range chat.NativeMessages { + chatMsg, ok := genMsg.(*anthropicChatMessage) + if !ok { + continue + } + for i, block := range chatMsg.Content { + if block.Type != "tool_use" || block.ID != toolCallId { + continue + } + updatedMsg := &anthropicChatMessage{ + MessageId: chatMsg.MessageId, + Usage: chatMsg.Usage, + Role: chatMsg.Role, + Content: slices.Clone(chatMsg.Content), + } + updatedMsg.Content[i].ToolUseData = &toolUseData + aiOpts := &uctypes.AIOptsType{ + APIType: chat.APIType, + Model: chat.Model, + APIVersion: chat.APIVersion, + } + return chatstore.DefaultChatStore.PostMessage(chatId, aiOpts, updatedMsg) + } + } + return fmt.Errorf("tool call with ID %s not found in chat %s", toolCallId, chatId) +} + +func RemoveToolUseCall(chatId string, toolCallId string) error { + chat := chatstore.DefaultChatStore.Get(chatId) + if chat == nil { + return fmt.Errorf("chat not found: %s", chatId) + } + for _, genMsg := range chat.NativeMessages { + chatMsg, ok := genMsg.(*anthropicChatMessage) + if !ok { + continue + } + for i, block := range chatMsg.Content { + if block.Type != "tool_use" || block.ID != toolCallId { + continue + } + updatedMsg := &anthropicChatMessage{ + MessageId: chatMsg.MessageId, + Usage: chatMsg.Usage, + Role: chatMsg.Role, + Content: slices.Delete(slices.Clone(chatMsg.Content), i, i+1), + } + if len(updatedMsg.Content) == 0 { + chatstore.DefaultChatStore.RemoveMessage(chatId, chatMsg.MessageId) + } else { + aiOpts := &uctypes.AIOptsType{ + APIType: chat.APIType, + Model: chat.Model, + APIVersion: chat.APIVersion, + } + if err := chatstore.DefaultChatStore.PostMessage(chatId, aiOpts, updatedMsg); err != nil { + return err + } + } + return nil + } + } + return nil +} diff --git a/pkg/aiusechat/usechat-backend.go b/pkg/aiusechat/usechat-backend.go index cb380a457c..37e2f432ec 100644 --- a/pkg/aiusechat/usechat-backend.go +++ b/pkg/aiusechat/usechat-backend.go @@ -186,15 +186,18 @@ func (b *anthropicBackend) RunChatStep( cont *uctypes.WaveContinueResponse, ) (*uctypes.WaveStopReason, []uctypes.GenAIMessage, *uctypes.RateLimitInfo, error) { 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 } func (b *anthropicBackend) UpdateToolUseData(chatId string, toolCallId string, toolUseData uctypes.UIMessageDataToolUse) error { - return fmt.Errorf("UpdateToolUseData not implemented for anthropic backend") + return anthropic.UpdateToolUseData(chatId, toolCallId, toolUseData) } func (b *anthropicBackend) RemoveToolUseCall(chatId string, toolCallId string) error { - return fmt.Errorf("RemoveToolUseCall not implemented for anthropic backend") + return anthropic.RemoveToolUseCall(chatId, toolCallId) } func (b *anthropicBackend) ConvertToolResultsToNativeChatMessage(toolResults []uctypes.AIToolResult) ([]uctypes.GenAIMessage, error) { @@ -210,7 +213,7 @@ func (b *anthropicBackend) ConvertAIMessageToNativeChatMessage(message uctypes.A } func (b *anthropicBackend) GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput { - return nil + return anthropic.GetFunctionCallInputByToolCallId(aiChat, toolCallId) } func (b *anthropicBackend) ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {