| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 1 | // Package llm provides a unified interface for interacting with LLMs. |
| 2 | package llm |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "log/slog" |
| Josh Bleecher Snyder | 59bb27d | 2025-06-05 07:32:10 -0700 | [diff] [blame] | 9 | "net/http" |
| Josh Bleecher Snyder | 57afbca | 2025-07-23 13:29:59 -0700 | [diff] [blame] | 10 | "os" |
| 11 | "path/filepath" |
| Josh Bleecher Snyder | 59bb27d | 2025-06-05 07:32:10 -0700 | [diff] [blame] | 12 | "strconv" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 13 | "strings" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | type Service interface { |
| 18 | // Do sends a request to an LLM. |
| 19 | Do(context.Context, *Request) (*Response, error) |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 20 | // TokenContextWindow returns the maximum token context window size for this service |
| 21 | TokenContextWindow() int |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| Josh Bleecher Snyder | 994e984 | 2025-07-30 20:26:47 -0700 | [diff] [blame] | 24 | type SimplifiedPatcher interface { |
| 25 | // UseSimplifiedPatch reports whether the service should use the simplified patch input schema. |
| 26 | UseSimplifiedPatch() bool |
| 27 | } |
| 28 | |
| 29 | func UseSimplifiedPatch(svc Service) bool { |
| 30 | if sp, ok := svc.(SimplifiedPatcher); ok { |
| 31 | return sp.UseSimplifiedPatch() |
| 32 | } |
| 33 | return false |
| 34 | } |
| 35 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 36 | // MustSchema validates that schema is a valid JSON schema and returns it as a json.RawMessage. |
| 37 | // It panics if the schema is invalid. |
| Josh Bleecher Snyder | 2e967e5 | 2025-07-14 21:09:31 +0000 | [diff] [blame] | 38 | // The schema must have at least type="object" and a properties key. |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 39 | func MustSchema(schema string) json.RawMessage { |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 40 | schema = strings.TrimSpace(schema) |
| 41 | bytes := []byte(schema) |
| Josh Bleecher Snyder | 2e967e5 | 2025-07-14 21:09:31 +0000 | [diff] [blame] | 42 | var obj map[string]any |
| 43 | if err := json.Unmarshal(bytes, &obj); err != nil { |
| 44 | panic("failed to parse JSON schema: " + schema + ": " + err.Error()) |
| 45 | } |
| 46 | if typ, ok := obj["type"]; !ok || typ != "object" { |
| 47 | panic("JSON schema must have type='object': " + schema) |
| 48 | } |
| 49 | if _, ok := obj["properties"]; !ok { |
| 50 | panic("JSON schema must have 'properties' key: " + schema) |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 51 | } |
| 52 | return json.RawMessage(bytes) |
| 53 | } |
| 54 | |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 55 | func EmptySchema() json.RawMessage { |
| 56 | return MustSchema(`{"type": "object", "properties": {}}`) |
| 57 | } |
| 58 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 59 | type Request struct { |
| 60 | Messages []Message |
| 61 | ToolChoice *ToolChoice |
| 62 | Tools []*Tool |
| 63 | System []SystemContent |
| 64 | } |
| 65 | |
| 66 | // Message represents a message in the conversation. |
| 67 | type Message struct { |
| 68 | Role MessageRole |
| 69 | Content []Content |
| 70 | ToolUse *ToolUse // use to control whether/which tool to use |
| 71 | } |
| 72 | |
| 73 | // ToolUse represents a tool use in the message content. |
| 74 | type ToolUse struct { |
| 75 | ID string |
| 76 | Name string |
| 77 | } |
| 78 | |
| 79 | type ToolChoice struct { |
| 80 | Type ToolChoiceType |
| 81 | Name string |
| 82 | } |
| 83 | |
| 84 | type SystemContent struct { |
| 85 | Text string |
| 86 | Type string |
| 87 | Cache bool |
| 88 | } |
| 89 | |
| 90 | // Tool represents a tool available to an LLM. |
| 91 | type Tool struct { |
| 92 | Name string |
| 93 | // Type is used by the text editor tool; see |
| 94 | // https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool |
| 95 | Type string |
| 96 | Description string |
| 97 | InputSchema json.RawMessage |
| Sean McCullough | 021557a | 2025-05-05 23:20:53 +0000 | [diff] [blame] | 98 | // EndsTurn indicates that this tool should cause the model to end its turn when used |
| 99 | EndsTurn bool |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 100 | |
| 101 | // The Run function is automatically called when the tool is used. |
| 102 | // Run functions may be called concurrently with each other and themselves. |
| 103 | // The input to Run function is the input to the tool, as provided by Claude, in compliance with the input schema. |
| 104 | // The outputs from Run will be sent back to Claude. |
| 105 | // If you do not want to respond to the tool call request from Claude, return ErrDoNotRespond. |
| 106 | // ctx contains extra (rarely used) tool call information; retrieve it with ToolCallInfoFromContext. |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 107 | Run func(ctx context.Context, input json.RawMessage) ToolOut `json:"-"` |
| 108 | } |
| 109 | |
| 110 | // ToolOut represents the output of a tool run. |
| 111 | type ToolOut struct { |
| 112 | // LLMContent is the output of the tool to be sent back to the LLM. |
| 113 | // May be nil on error. |
| 114 | LLMContent []Content |
| Josh Bleecher Snyder | 3dd3e41 | 2025-07-22 20:32:03 -0700 | [diff] [blame] | 115 | // Display is content to be displayed to the user. |
| 116 | // The type of content is set by the tool and coordinated with the UIs. |
| 117 | // It should be JSON-serializable. |
| 118 | Display any |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 119 | // Error is the error (if any) that occurred during the tool run. |
| 120 | // The text contents of the error will be sent back to the LLM. |
| 121 | // If non-nil, LLMContent will be ignored. |
| 122 | Error error |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | type Content struct { |
| 126 | ID string |
| 127 | Type ContentType |
| 128 | Text string |
| 129 | |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 130 | // Media type for image content |
| 131 | MediaType string |
| 132 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 133 | // for thinking |
| 134 | Thinking string |
| 135 | Data string |
| 136 | Signature string |
| 137 | |
| 138 | // for tool_use |
| 139 | ToolName string |
| 140 | ToolInput json.RawMessage |
| 141 | |
| 142 | // for tool_result |
| 143 | ToolUseID string |
| 144 | ToolError bool |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 145 | ToolResult []Content |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 146 | |
| 147 | // timing information for tool_result; added externally; not sent to the LLM |
| 148 | ToolUseStartTime *time.Time |
| 149 | ToolUseEndTime *time.Time |
| 150 | |
| Josh Bleecher Snyder | 3dd3e41 | 2025-07-22 20:32:03 -0700 | [diff] [blame] | 151 | // Display is content to be displayed to the user, copied from ToolOut |
| 152 | Display any |
| 153 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 154 | Cache bool |
| 155 | } |
| 156 | |
| 157 | func StringContent(s string) Content { |
| 158 | return Content{Type: ContentTypeText, Text: s} |
| 159 | } |
| 160 | |
| 161 | // ContentsAttr returns contents as a slog.Attr. |
| 162 | // It is meant for logging. |
| 163 | func ContentsAttr(contents []Content) slog.Attr { |
| 164 | var contentAttrs []any // slog.Attr |
| 165 | for _, content := range contents { |
| 166 | var attrs []any // slog.Attr |
| 167 | switch content.Type { |
| 168 | case ContentTypeText: |
| 169 | attrs = append(attrs, slog.String("text", content.Text)) |
| 170 | case ContentTypeToolUse: |
| 171 | attrs = append(attrs, slog.String("tool_name", content.ToolName)) |
| 172 | attrs = append(attrs, slog.String("tool_input", string(content.ToolInput))) |
| 173 | case ContentTypeToolResult: |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 174 | attrs = append(attrs, slog.Any("tool_result", content.ToolResult)) |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 175 | attrs = append(attrs, slog.Bool("tool_error", content.ToolError)) |
| 176 | case ContentTypeThinking: |
| 177 | attrs = append(attrs, slog.String("thinking", content.Text)) |
| 178 | default: |
| 179 | attrs = append(attrs, slog.String("unknown_content_type", content.Type.String())) |
| 180 | attrs = append(attrs, slog.Any("text", content)) // just log it all raw, better to have too much than not enough |
| 181 | } |
| 182 | contentAttrs = append(contentAttrs, slog.Group(content.ID, attrs...)) |
| 183 | } |
| 184 | return slog.Group("contents", contentAttrs...) |
| 185 | } |
| 186 | |
| 187 | type ( |
| 188 | MessageRole int |
| 189 | ContentType int |
| 190 | ToolChoiceType int |
| 191 | StopReason int |
| 192 | ) |
| 193 | |
| 194 | //go:generate go tool golang.org/x/tools/cmd/stringer -type=MessageRole,ContentType,ToolChoiceType,StopReason -output=llm_string.go |
| 195 | |
| 196 | const ( |
| 197 | MessageRoleUser MessageRole = iota |
| 198 | MessageRoleAssistant |
| 199 | |
| 200 | ContentTypeText ContentType = iota |
| 201 | ContentTypeThinking |
| 202 | ContentTypeRedactedThinking |
| 203 | ContentTypeToolUse |
| 204 | ContentTypeToolResult |
| 205 | |
| 206 | ToolChoiceTypeAuto ToolChoiceType = iota // default |
| 207 | ToolChoiceTypeAny // any tool, but must use one |
| 208 | ToolChoiceTypeNone // no tools allowed |
| 209 | ToolChoiceTypeTool // must use the tool specified in the Name field |
| 210 | |
| 211 | StopReasonStopSequence StopReason = iota |
| 212 | StopReasonMaxTokens |
| 213 | StopReasonEndTurn |
| 214 | StopReasonToolUse |
| Josh Bleecher Snyder | 0e8073a | 2025-05-22 21:04:51 -0700 | [diff] [blame] | 215 | StopReasonRefusal |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 216 | ) |
| 217 | |
| 218 | type Response struct { |
| 219 | ID string |
| 220 | Type string |
| 221 | Role MessageRole |
| 222 | Model string |
| 223 | Content []Content |
| 224 | StopReason StopReason |
| 225 | StopSequence *string |
| 226 | Usage Usage |
| 227 | StartTime *time.Time |
| 228 | EndTime *time.Time |
| 229 | } |
| 230 | |
| 231 | func (m *Response) ToMessage() Message { |
| 232 | return Message{ |
| 233 | Role: m.Role, |
| 234 | Content: m.Content, |
| 235 | } |
| 236 | } |
| 237 | |
| Josh Bleecher Snyder | 59bb27d | 2025-06-05 07:32:10 -0700 | [diff] [blame] | 238 | func CostUSDFromResponse(headers http.Header) float64 { |
| 239 | h := headers.Get("Skaband-Cost-Microcents") |
| 240 | if h == "" { |
| 241 | return 0 |
| 242 | } |
| 243 | uc, err := strconv.ParseUint(h, 10, 64) |
| 244 | if err != nil { |
| 245 | slog.Warn("failed to parse cost header", "header", h) |
| 246 | return 0 |
| 247 | } |
| 248 | return float64(uc) / 100_000_000 |
| 249 | } |
| 250 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 251 | // Usage represents the billing and rate-limit usage. |
| 252 | // Most LLM structs do not have JSON tags, to avoid accidental direct use in specific providers. |
| 253 | // However, the front-end uses this struct, and it relies on its JSON serialization. |
| 254 | // Do NOT use this struct directly when implementing an llm.Service. |
| 255 | type Usage struct { |
| 256 | InputTokens uint64 `json:"input_tokens"` |
| 257 | CacheCreationInputTokens uint64 `json:"cache_creation_input_tokens"` |
| 258 | CacheReadInputTokens uint64 `json:"cache_read_input_tokens"` |
| 259 | OutputTokens uint64 `json:"output_tokens"` |
| 260 | CostUSD float64 `json:"cost_usd"` |
| 261 | } |
| 262 | |
| 263 | func (u *Usage) Add(other Usage) { |
| 264 | u.InputTokens += other.InputTokens |
| 265 | u.CacheCreationInputTokens += other.CacheCreationInputTokens |
| 266 | u.CacheReadInputTokens += other.CacheReadInputTokens |
| 267 | u.OutputTokens += other.OutputTokens |
| 268 | u.CostUSD += other.CostUSD |
| 269 | } |
| 270 | |
| 271 | func (u *Usage) String() string { |
| 272 | return fmt.Sprintf("in: %d, out: %d", u.InputTokens, u.OutputTokens) |
| 273 | } |
| 274 | |
| 275 | func (u *Usage) IsZero() bool { |
| 276 | return *u == Usage{} |
| 277 | } |
| 278 | |
| 279 | func (u *Usage) Attr() slog.Attr { |
| 280 | return slog.Group("usage", |
| 281 | slog.Uint64("input_tokens", u.InputTokens), |
| 282 | slog.Uint64("output_tokens", u.OutputTokens), |
| 283 | slog.Uint64("cache_creation_input_tokens", u.CacheCreationInputTokens), |
| 284 | slog.Uint64("cache_read_input_tokens", u.CacheReadInputTokens), |
| 285 | slog.Float64("cost_usd", u.CostUSD), |
| 286 | ) |
| 287 | } |
| 288 | |
| 289 | // UserStringMessage creates a user message with a single text content item. |
| 290 | func UserStringMessage(text string) Message { |
| 291 | return Message{ |
| 292 | Role: MessageRoleUser, |
| 293 | Content: []Content{StringContent(text)}, |
| 294 | } |
| 295 | } |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 296 | |
| 297 | // TextContent creates a simple text content for tool results. |
| 298 | // This is a helper function to create the most common type of tool result content. |
| 299 | func TextContent(text string) []Content { |
| 300 | return []Content{{ |
| 301 | Type: ContentTypeText, |
| 302 | Text: text, |
| 303 | }} |
| 304 | } |
| Josh Bleecher Snyder | 43b60b9 | 2025-07-21 14:57:10 -0700 | [diff] [blame] | 305 | |
| 306 | func ErrorToolOut(err error) ToolOut { |
| 307 | if err == nil { |
| 308 | panic("ErrorToolOut called with nil error") |
| 309 | } |
| 310 | return ToolOut{ |
| 311 | Error: err, |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | func ErrorfToolOut(format string, args ...any) ToolOut { |
| 316 | return ErrorToolOut(fmt.Errorf(format, args...)) |
| 317 | } |
| Josh Bleecher Snyder | 57afbca | 2025-07-23 13:29:59 -0700 | [diff] [blame] | 318 | |
| 319 | // DumpToFile writes LLM communication content to a timestamped file in ~/.cache/sketch/. |
| 320 | // For requests, it includes the URL followed by the content. For responses, it only includes the content. |
| 321 | // The typ parameter is used as a prefix in the filename ("request", "response"). |
| 322 | func DumpToFile(typ string, url string, content []byte) error { |
| 323 | homeDir, err := os.UserHomeDir() |
| 324 | if err != nil { |
| 325 | return err |
| 326 | } |
| 327 | cacheDir := filepath.Join(homeDir, ".cache", "sketch") |
| 328 | err = os.MkdirAll(cacheDir, 0o700) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 | now := time.Now() |
| 333 | filename := fmt.Sprintf("%s_%d.txt", typ, now.UnixMilli()) |
| 334 | filePath := filepath.Join(cacheDir, filename) |
| 335 | |
| 336 | // For requests, start with the URL; for responses, just write the content |
| 337 | data := []byte(url) |
| 338 | if url != "" { |
| 339 | data = append(data, "\n\n"...) |
| 340 | } |
| 341 | data = append(data, content...) |
| 342 | |
| 343 | return os.WriteFile(filePath, data, 0o600) |
| 344 | } |