feat: fix a tool calling issue with claude and update the oauth version (could change with newer claude code versions)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
55d5e89246
commit
08e5ca9eb1
2 changed files with 25 additions and 7 deletions
|
|
@ -486,8 +486,10 @@ func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.M
|
||||||
for _, tc := range response.ToolCalls {
|
for _, tc := range response.ToolCalls {
|
||||||
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
||||||
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
|
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
|
||||||
ID: tc.ID,
|
ID: tc.ID,
|
||||||
Type: "function",
|
Type: "function",
|
||||||
|
Name: tc.Name,
|
||||||
|
Arguments: tc.Arguments,
|
||||||
Function: &providers.FunctionCall{
|
Function: &providers.FunctionCall{
|
||||||
Name: tc.Name,
|
Name: tc.Name,
|
||||||
Arguments: string(argumentsJSON),
|
Arguments: string(argumentsJSON),
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,38 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/anthropics/anthropic-sdk-go"
|
"github.com/anthropics/anthropic-sdk-go"
|
||||||
"github.com/anthropics/anthropic-sdk-go/option"
|
"github.com/anthropics/anthropic-sdk-go/option"
|
||||||
"github.com/sipeed/picoclaw/pkg/auth"
|
"github.com/sipeed/picoclaw/pkg/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Would change with newer versions
|
||||||
|
const oauthBetaHeader = "oauth-2025-04-20"
|
||||||
|
|
||||||
type ClaudeProvider struct {
|
type ClaudeProvider struct {
|
||||||
client *anthropic.Client
|
client *anthropic.Client
|
||||||
tokenSource func() (string, error)
|
tokenSource func() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClaudeProvider(token string) *ClaudeProvider {
|
func claudeClientOptions(token string) []option.RequestOption {
|
||||||
client := anthropic.NewClient(
|
opts := []option.RequestOption{
|
||||||
option.WithAuthToken(token),
|
|
||||||
option.WithBaseURL("https://api.anthropic.com"),
|
option.WithBaseURL("https://api.anthropic.com"),
|
||||||
)
|
}
|
||||||
|
if strings.HasPrefix(strings.TrimSpace(token), "sk-ant-oat") {
|
||||||
|
opts = append(opts,
|
||||||
|
option.WithAuthToken(token),
|
||||||
|
option.WithHeader("anthropic-beta", oauthBetaHeader),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
opts = append(opts, option.WithAPIKey(token))
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClaudeProvider(token string) *ClaudeProvider {
|
||||||
|
client := anthropic.NewClient(claudeClientOptions(token)...)
|
||||||
return &ClaudeProvider{client: &client}
|
return &ClaudeProvider{client: &client}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,7 +52,7 @@ func (p *ClaudeProvider) Chat(ctx context.Context, messages []Message, tools []T
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("refreshing token: %w", err)
|
return nil, fmt.Errorf("refreshing token: %w", err)
|
||||||
}
|
}
|
||||||
opts = append(opts, option.WithAuthToken(tok))
|
opts = append(opts, claudeClientOptions(tok)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
params, err := buildClaudeParams(messages, tools, model, options)
|
params, err := buildClaudeParams(messages, tools, model, options)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue