|
| 1 | +package anthropic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + keep "github.com/majorcontext/keep" |
| 8 | + "github.com/majorcontext/keep/internal/gateway/config" |
| 9 | +) |
| 10 | + |
| 11 | +// DecomposeRequest breaks an Anthropic Messages API request into flat Keep calls. |
| 12 | +// Returns one llm.request summary + one call per content block (based on decompose config). |
| 13 | +func DecomposeRequest(req *MessagesRequest, scope string, cfg config.DecomposeConfig) []keep.Call { |
| 14 | + var calls []keep.Call |
| 15 | + |
| 16 | + // Build a map from tool_use_id to tool name for resolving tool_result references. |
| 17 | + toolNameMap := buildToolNameMap(req.Messages) |
| 18 | + |
| 19 | + // 1. Summary call. |
| 20 | + if cfg.RequestSummaryEnabled() { |
| 21 | + calls = append(calls, keep.Call{ |
| 22 | + Operation: "llm.request", |
| 23 | + Params: map[string]any{ |
| 24 | + "model": req.Model, |
| 25 | + "system": systemToString(req.System), |
| 26 | + "token_estimate": estimateTokens(req), |
| 27 | + "tool_result_count": countToolResults(req), |
| 28 | + "message_count": len(req.Messages), |
| 29 | + }, |
| 30 | + Context: keep.CallContext{Scope: scope, Direction: "request"}, |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + // 2. Walk messages for content blocks. |
| 35 | + for _, msg := range req.Messages { |
| 36 | + blocks := msg.ContentBlocks() |
| 37 | + for _, block := range blocks { |
| 38 | + switch block.Type { |
| 39 | + case "tool_result": |
| 40 | + if cfg.ToolResultEnabled() { |
| 41 | + toolName := toolNameMap[block.ToolUseID] |
| 42 | + calls = append(calls, keep.Call{ |
| 43 | + Operation: "llm.tool_result", |
| 44 | + Params: map[string]any{ |
| 45 | + "tool_name": toolName, |
| 46 | + "tool_use_id": block.ToolUseID, |
| 47 | + "content": block.ToolResultContent(), |
| 48 | + }, |
| 49 | + Context: keep.CallContext{Scope: scope, Direction: "request"}, |
| 50 | + }) |
| 51 | + } |
| 52 | + case "text": |
| 53 | + if cfg.TextEnabled() { |
| 54 | + calls = append(calls, keep.Call{ |
| 55 | + Operation: "llm.text", |
| 56 | + Params: map[string]any{ |
| 57 | + "text": block.Text, |
| 58 | + "role": msg.Role, |
| 59 | + }, |
| 60 | + Context: keep.CallContext{Scope: scope, Direction: "request"}, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return calls |
| 68 | +} |
| 69 | + |
| 70 | +// DecomposeResponse breaks an Anthropic Messages API response into flat Keep calls. |
| 71 | +func DecomposeResponse(resp *MessagesResponse, scope string, cfg config.DecomposeConfig) []keep.Call { |
| 72 | + var calls []keep.Call |
| 73 | + |
| 74 | + // 1. Summary call. |
| 75 | + if cfg.ResponseSummaryEnabled() { |
| 76 | + calls = append(calls, keep.Call{ |
| 77 | + Operation: "llm.response", |
| 78 | + Params: map[string]any{ |
| 79 | + "stop_reason": resp.StopReason, |
| 80 | + "tool_use_count": countToolUses(resp), |
| 81 | + }, |
| 82 | + Context: keep.CallContext{Scope: scope, Direction: "response"}, |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + // 2. Walk response content blocks. |
| 87 | + for _, block := range resp.Content { |
| 88 | + switch block.Type { |
| 89 | + case "tool_use": |
| 90 | + if cfg.ToolUseEnabled() { |
| 91 | + calls = append(calls, keep.Call{ |
| 92 | + Operation: "llm.tool_use", |
| 93 | + Params: map[string]any{ |
| 94 | + "name": block.Name, |
| 95 | + "input": block.Input, |
| 96 | + }, |
| 97 | + Context: keep.CallContext{Scope: scope, Direction: "response"}, |
| 98 | + }) |
| 99 | + } |
| 100 | + case "text": |
| 101 | + if cfg.TextEnabled() { |
| 102 | + calls = append(calls, keep.Call{ |
| 103 | + Operation: "llm.text", |
| 104 | + Params: map[string]any{ |
| 105 | + "text": block.Text, |
| 106 | + "role": resp.Role, |
| 107 | + }, |
| 108 | + Context: keep.CallContext{Scope: scope, Direction: "response"}, |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return calls |
| 115 | +} |
| 116 | + |
| 117 | +// systemToString converts the system field (string or []ContentBlock) to a string. |
| 118 | +func systemToString(system any) string { |
| 119 | + if system == nil { |
| 120 | + return "" |
| 121 | + } |
| 122 | + switch v := system.(type) { |
| 123 | + case string: |
| 124 | + return v |
| 125 | + default: |
| 126 | + blocks := toContentBlocks(system) |
| 127 | + var parts []string |
| 128 | + for _, b := range blocks { |
| 129 | + if b.Type == "text" && b.Text != "" { |
| 130 | + parts = append(parts, b.Text) |
| 131 | + } |
| 132 | + } |
| 133 | + return strings.Join(parts, "\n") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// estimateTokens gives a rough token count for the request. |
| 138 | +// Uses a simple heuristic of ~4 characters per token. |
| 139 | +func estimateTokens(req *MessagesRequest) int { |
| 140 | + total := len(systemToString(req.System)) |
| 141 | + for _, msg := range req.Messages { |
| 142 | + blocks := msg.ContentBlocks() |
| 143 | + for _, b := range blocks { |
| 144 | + switch b.Type { |
| 145 | + case "text": |
| 146 | + total += len(b.Text) |
| 147 | + case "tool_result": |
| 148 | + total += len(b.ToolResultContent()) |
| 149 | + case "tool_use": |
| 150 | + total += len(fmt.Sprintf("%v", b.Input)) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + return total / 4 |
| 155 | +} |
| 156 | + |
| 157 | +// countToolResults counts tool_result blocks in the request. |
| 158 | +func countToolResults(req *MessagesRequest) int { |
| 159 | + count := 0 |
| 160 | + for _, msg := range req.Messages { |
| 161 | + for _, b := range msg.ContentBlocks() { |
| 162 | + if b.Type == "tool_result" { |
| 163 | + count++ |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + return count |
| 168 | +} |
| 169 | + |
| 170 | +// countToolUses counts tool_use blocks in the response. |
| 171 | +func countToolUses(resp *MessagesResponse) int { |
| 172 | + count := 0 |
| 173 | + for _, b := range resp.Content { |
| 174 | + if b.Type == "tool_use" { |
| 175 | + count++ |
| 176 | + } |
| 177 | + } |
| 178 | + return count |
| 179 | +} |
| 180 | + |
| 181 | +// buildToolNameMap creates a map from tool_use_id to tool name |
| 182 | +// by walking all messages and finding tool_use blocks. |
| 183 | +func buildToolNameMap(messages []Message) map[string]string { |
| 184 | + m := make(map[string]string) |
| 185 | + for _, msg := range messages { |
| 186 | + for _, b := range msg.ContentBlocks() { |
| 187 | + if b.Type == "tool_use" && b.ID != "" { |
| 188 | + m[b.ID] = b.Name |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + return m |
| 193 | +} |