Changeset 0.7.2 (#40)

This commit is contained in:
2026-02-21 19:03:19 +00:00
parent 494b1aa981
commit 416e5439ea
28 changed files with 3813 additions and 138 deletions

View File

@@ -35,21 +35,37 @@ func (p *AnthropicProvider) ChatCompletion(ctx context.Context, cfg ProviderConf
return nil, fmt.Errorf("decode response: %w", err)
}
// Extract text from content blocks
var content string
for _, block := range resp.Content {
if block.Type == "text" {
content += block.Text
}
}
return &CompletionResponse{
Content: content,
result := &CompletionResponse{
Model: resp.Model,
FinishReason: resp.StopReason,
InputTokens: resp.Usage.InputTokens,
OutputTokens: resp.Usage.OutputTokens,
}, nil
}
// Extract text and tool_use blocks
for _, block := range resp.Content {
switch block.Type {
case "text":
result.Content += block.Text
case "tool_use":
argsJSON, _ := json.Marshal(block.Input)
result.ToolCalls = append(result.ToolCalls, ToolCall{
ID: block.ID,
Type: "function",
Function: FunctionCall{
Name: block.Name,
Arguments: string(argsJSON),
},
})
}
}
// Normalize stop reason
if resp.StopReason == "tool_use" {
result.FinishReason = "tool_calls"
}
return result, nil
}
// ── Stream Completion ───────────────────────
@@ -68,6 +84,9 @@ func (p *AnthropicProvider) StreamCompletion(ctx context.Context, cfg ProviderCo
defer close(ch)
defer body.Close()
var toolCalls []ToolCall
var currentToolIdx int = -1
scanner := bufio.NewScanner(body)
for scanner.Scan() {
line := scanner.Text()
@@ -84,18 +103,42 @@ func (p *AnthropicProvider) StreamCompletion(ctx context.Context, cfg ProviderCo
}
switch event.Type {
case "content_block_start":
if event.ContentBlock != nil && event.ContentBlock.Type == "tool_use" {
currentToolIdx++
toolCalls = append(toolCalls, ToolCall{
ID: event.ContentBlock.ID,
Type: "function",
Function: FunctionCall{
Name: event.ContentBlock.Name,
Arguments: "",
},
})
}
case "content_block_delta":
if event.Delta.Type == "text_delta" {
ch <- StreamEvent{Delta: event.Delta.Text}
} else if event.Delta.Type == "input_json_delta" && currentToolIdx >= 0 {
toolCalls[currentToolIdx].Function.Arguments += event.Delta.PartialJSON
}
case "message_delta":
ch <- StreamEvent{
stopReason := event.Delta.StopReason
ev := StreamEvent{
Done: true,
FinishReason: event.Delta.StopReason,
FinishReason: stopReason,
}
if stopReason == "tool_use" {
ev.FinishReason = "tool_calls"
ev.ToolCalls = toolCalls
}
ch <- ev
case "message_stop":
ch <- StreamEvent{Done: true}
return
case "error":
ch <- StreamEvent{
Error: fmt.Errorf("anthropic stream error: %s", data),
@@ -115,8 +158,6 @@ func (p *AnthropicProvider) StreamCompletion(ctx context.Context, cfg ProviderCo
// ── List Models ─────────────────────────────
func (p *AnthropicProvider) ListModels(_ context.Context, _ ProviderConfig) ([]Model, error) {
// Anthropic doesn't have a list models endpoint.
// Return known models with authoritative capabilities from our table.
modelIDs := []struct{ id, name string }{
{"claude-opus-4-20250514", "Claude Opus 4"},
{"claude-sonnet-4-20250514", "Claude Sonnet 4"},
@@ -147,7 +188,7 @@ func (p *AnthropicProvider) doRequest(ctx context.Context, cfg ProviderConfig, r
}
endpoint = strings.TrimSuffix(endpoint, "/") + "/v1/messages"
// Separate system message from conversation
// Separate system message and convert to Anthropic format
var system string
messages := make([]anthropicMessage, 0, len(req.Messages))
for _, m := range req.Messages {
@@ -155,13 +196,55 @@ func (p *AnthropicProvider) doRequest(ctx context.Context, cfg ProviderConfig, r
system = m.Content
continue
}
messages = append(messages, anthropicMessage{
Role: m.Role,
Content: m.Content,
})
antMsg := anthropicMessage{Role: m.Role}
if m.Role == "assistant" && len(m.ToolCalls) > 0 {
// Assistant with tool calls → mixed content blocks
if m.Content != "" {
antMsg.Content = []anthropicContentBlock{
{Type: "text", Text: m.Content},
}
} else {
antMsg.Content = []anthropicContentBlock{}
}
for _, tc := range m.ToolCalls {
var input json.RawMessage
if tc.Function.Arguments != "" {
input = json.RawMessage(tc.Function.Arguments)
} else {
input = json.RawMessage("{}")
}
antMsg.Content = append(antMsg.Content, anthropicContentBlock{
Type: "tool_use",
ID: tc.ID,
Name: tc.Function.Name,
Input: input,
})
}
} else if m.Role == "tool" {
// Tool results → user message with tool_result block
antMsg.Role = "user"
antMsg.Content = []anthropicContentBlock{
{
Type: "tool_result",
ToolUseID: m.ToolCallID,
Content: m.Content,
},
}
} else {
// Regular text message
antMsg.Content = []anthropicContentBlock{
{Type: "text", Text: m.Content},
}
}
messages = append(messages, antMsg)
}
// Build Anthropic-format request
// Merge consecutive user messages (Anthropic requires alternating roles)
messages = mergeConsecutiveUserMessages(messages)
antReq := anthropicRequest{
Model: req.Model,
Messages: messages,
@@ -172,7 +255,6 @@ func (p *AnthropicProvider) doRequest(ctx context.Context, cfg ProviderConfig, r
antReq.System = system
}
if antReq.MaxTokens == 0 {
// Anthropic requires max_tokens. Resolve from known model table.
antReq.MaxTokens = ResolveMaxOutput(req.Model, ModelCapabilities{})
}
if req.Temperature != nil {
@@ -182,6 +264,15 @@ func (p *AnthropicProvider) doRequest(ctx context.Context, cfg ProviderConfig, r
antReq.TopP = req.TopP
}
// Add tools in Anthropic format
for _, t := range req.Tools {
antReq.Tools = append(antReq.Tools, anthropicToolDef{
Name: t.Function.Name,
Description: t.Function.Description,
InputSchema: t.Function.Parameters,
})
}
body, err := json.Marshal(antReq)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
@@ -209,11 +300,47 @@ func (p *AnthropicProvider) doRequest(ctx context.Context, cfg ProviderConfig, r
return resp.Body, nil
}
// mergeConsecutiveUserMessages combines consecutive user-role messages.
// Anthropic requires strictly alternating user/assistant roles.
func mergeConsecutiveUserMessages(msgs []anthropicMessage) []anthropicMessage {
if len(msgs) <= 1 {
return msgs
}
merged := make([]anthropicMessage, 0, len(msgs))
for _, m := range msgs {
if len(merged) > 0 && merged[len(merged)-1].Role == "user" && m.Role == "user" {
merged[len(merged)-1].Content = append(merged[len(merged)-1].Content, m.Content...)
} else {
merged = append(merged, m)
}
}
return merged
}
// ── Anthropic Wire Types ────────────────────
type anthropicContentBlock struct {
Type string `json:"type"`
// text
Text string `json:"text,omitempty"`
// tool_use
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
// tool_result
ToolUseID string `json:"tool_use_id,omitempty"`
Content string `json:"content,omitempty"`
}
type anthropicMessage struct {
Role string `json:"role"`
Content string `json:"content"`
Role string `json:"role"`
Content []anthropicContentBlock `json:"content"`
}
type anthropicToolDef struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"input_schema"`
}
type anthropicRequest struct {
@@ -224,14 +351,18 @@ type anthropicRequest struct {
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream"`
Tools []anthropicToolDef `json:"tools,omitempty"`
}
type anthropicResponse struct {
Model string `json:"model"`
StopReason string `json:"stop_reason"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
} `json:"content"`
Usage struct {
InputTokens int `json:"input_tokens"`
@@ -242,8 +373,14 @@ type anthropicResponse struct {
type anthropicStreamEvent struct {
Type string `json:"type"`
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
StopReason string `json:"stop_reason"`
Type string `json:"type"`
Text string `json:"text"`
StopReason string `json:"stop_reason"`
PartialJSON string `json:"partial_json"`
} `json:"delta"`
ContentBlock *struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
} `json:"content_block,omitempty"`
}

View File

@@ -36,13 +36,32 @@ func (p *OpenAIProvider) ChatCompletion(ctx context.Context, cfg ProviderConfig,
return nil, fmt.Errorf("no choices in response")
}
return &CompletionResponse{
result := &CompletionResponse{
Content: resp.Choices[0].Message.Content,
Model: resp.Model,
FinishReason: resp.Choices[0].FinishReason,
InputTokens: resp.Usage.PromptTokens,
OutputTokens: resp.Usage.CompletionTokens,
}, nil
}
// Extract tool calls if present
if len(resp.Choices[0].Message.ToolCalls) > 0 {
for _, tc := range resp.Choices[0].Message.ToolCalls {
result.ToolCalls = append(result.ToolCalls, ToolCall{
ID: tc.ID,
Type: tc.Type,
Function: FunctionCall{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
})
}
if result.FinishReason == "" {
result.FinishReason = "tool_calls"
}
}
return result, nil
}
// ── Stream Completion ───────────────────────
@@ -61,6 +80,9 @@ func (p *OpenAIProvider) StreamCompletion(ctx context.Context, cfg ProviderConfi
defer close(ch)
defer body.Close()
// Accumulate tool calls across stream chunks
toolCallMap := map[int]*ToolCall{} // index → accumulated call
scanner := bufio.NewScanner(body)
for scanner.Scan() {
line := scanner.Text()
@@ -84,13 +106,45 @@ func (p *OpenAIProvider) StreamCompletion(ctx context.Context, cfg ProviderConfi
continue
}
ev := StreamEvent{
Delta: chunk.Choices[0].Delta.Content,
Model: chunk.Model,
FinishReason: chunk.Choices[0].FinishReason,
choice := chunk.Choices[0]
// Accumulate tool call deltas
for _, tc := range choice.Delta.ToolCalls {
idx := 0
if tc.Index != nil {
idx = *tc.Index
}
if existing, ok := toolCallMap[idx]; ok {
// Append arguments fragment
existing.Function.Arguments += tc.Function.Arguments
} else {
// First chunk for this tool — has ID and name
toolCallMap[idx] = &ToolCall{
ID: tc.ID,
Type: "function",
Function: FunctionCall{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
}
}
}
ev := StreamEvent{
Delta: choice.Delta.Content,
Model: chunk.Model,
FinishReason: choice.FinishReason,
}
if ev.FinishReason != "" {
ev.Done = true
// Attach accumulated tool calls on final event
if ev.FinishReason == "tool_calls" && len(toolCallMap) > 0 {
for _, tc := range toolCallMap {
ev.ToolCalls = append(ev.ToolCalls, *tc)
}
}
}
ch <- ev
@@ -178,11 +232,46 @@ func (p *OpenAIProvider) doRequest(ctx context.Context, cfg ProviderConfig, req
oaiReq.TopP = req.TopP
}
// Convert tools
for _, t := range req.Tools {
oaiReq.Tools = append(oaiReq.Tools, openaiToolDef{
Type: t.Type,
Function: openaiToolDefFunction{
Name: t.Function.Name,
Description: t.Function.Description,
Parameters: t.Function.Parameters,
},
})
}
// Convert messages — handle all roles including tool
for _, m := range req.Messages {
oaiReq.Messages = append(oaiReq.Messages, openaiMessage{
oaiMsg := openaiMessage{
Role: m.Role,
Content: m.Content,
})
}
// Assistant messages with tool calls
if len(m.ToolCalls) > 0 {
for _, tc := range m.ToolCalls {
oaiMsg.ToolCalls = append(oaiMsg.ToolCalls, openaiToolCall{
ID: tc.ID,
Type: tc.Type,
Function: openaiToolCallFunction{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
})
}
}
// Tool result messages
if m.Role == "tool" {
oaiMsg.ToolCallID = m.ToolCallID
oaiMsg.Name = m.Name
}
oaiReq.Messages = append(oaiReq.Messages, oaiMsg)
}
body, err := json.Marshal(oaiReq)
@@ -220,8 +309,34 @@ func (p *OpenAIProvider) doRequest(ctx context.Context, cfg ProviderConfig, req
// ── OpenAI Wire Types ───────────────────────
type openaiMessage struct {
Role string `json:"role"`
Content string `json:"content"`
Role string `json:"role"`
Content string `json:"content,omitempty"`
ToolCalls []openaiToolCall `json:"tool_calls,omitempty"` // assistant requesting tools
ToolCallID string `json:"tool_call_id,omitempty"` // role=tool result
Name string `json:"name,omitempty"` // tool name for role=tool
}
type openaiToolCallFunction struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
type openaiToolCall struct {
Index *int `json:"index,omitempty"` // present in streaming deltas
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"` // "function"
Function openaiToolCallFunction `json:"function"`
}
type openaiToolDefFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters json.RawMessage `json:"parameters"`
}
type openaiToolDef struct {
Type string `json:"type"` // "function"
Function openaiToolDefFunction `json:"function"`
}
type openaiChatRequest struct {
@@ -231,6 +346,7 @@ type openaiChatRequest struct {
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream"`
Tools []openaiToolDef `json:"tools,omitempty"`
}
type openaiChatResponse struct {
@@ -249,7 +365,8 @@ type openaiStreamChunk struct {
Model string `json:"model"`
Choices []struct {
Delta struct {
Content string `json:"content"`
Content string `json:"content"`
ToolCalls []openaiToolCall `json:"tool_calls,omitempty"`
} `json:"delta"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`

View File

@@ -2,6 +2,7 @@ package providers
import (
"context"
"encoding/json"
)
// ── Provider Interface ──────────────────────
@@ -37,8 +38,39 @@ type ProviderConfig struct {
// Message represents a chat message in the conversation.
type Message struct {
Role string `json:"role"` // user, assistant, system
Role string `json:"role"` // user, assistant, system, tool
Content string `json:"content"`
// Tool calling fields (assistant → tool_calls, tool → tool_call_id)
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // set when assistant requests tools
ToolCallID string `json:"tool_call_id,omitempty"` // set for role="tool" result messages
Name string `json:"name,omitempty"` // tool name for role="tool"
}
// ToolCall represents a function call requested by the LLM.
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function FunctionCall `json:"function"`
}
// FunctionCall is the function name and JSON arguments.
type FunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
// ToolDef describes a tool available to the LLM.
type ToolDef struct {
Type string `json:"type"` // "function"
Function FunctionDef `json:"function"`
}
// FunctionDef is the schema for a tool function.
type FunctionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters json.RawMessage `json:"parameters"`
}
// CompletionRequest is the normalized request sent to any provider.
@@ -49,15 +81,17 @@ type CompletionRequest struct {
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
Tools []ToolDef `json:"tools,omitempty"` // available tools for function calling
}
// CompletionResponse is the normalized non-streaming response.
type CompletionResponse struct {
Content string `json:"content"`
Model string `json:"model"`
FinishReason string `json:"finish_reason"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
Content string `json:"content"`
Model string `json:"model"`
FinishReason string `json:"finish_reason"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // set when finish_reason is "tool_calls"
}
// StreamEvent is a single chunk from a streaming response.
@@ -69,8 +103,13 @@ type StreamEvent struct {
Done bool `json:"done,omitempty"`
// FinishReason is set on the final event.
// "stop" = normal, "tool_calls" = LLM wants to call tools.
FinishReason string `json:"finish_reason,omitempty"`
// ToolCalls accumulates tool call data during streaming.
// Fully populated on the final event when FinishReason is "tool_calls".
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// Model echoes back the model used.
Model string `json:"model,omitempty"`