xonecas.io
62159
Sy

symb / commit afb2a20

feat: deterministic tool schemas, kv-cache provider support

xonecas2026-02-14 afb2a201add6b7baadcbcc15ddbe0e2112a1571c parent 7fd7004

12 files changed

README.md+4−1
@@ -18,13 +18,16 @@ Symb pairs you with an LLM, providing an agentic UI with tools the LLM can use t
1818
1919 ## Features
2020
21-- **Dual-pane TUI**: Code editor on one side, agent conversation on the other
21+- **Dual-pane TUI**: Code viewer on one side, agent conversation on the other
2222 - **Syntax highlighting**: Via Chroma for Go and other languages
2323 - **Clean aesthetic**: Dark grayscale with minimal distractions
2424 - **ELM architecture**: Built with BubbleTea for solid state management
2525 - **LLM integration**: Ollama local + Opencode Zen support
2626 - **Hashline-assisted edits**: LLM edit operations use line hashes for precise, conflict-free file modifications
2727 - **Web search**: Exa AI integration with configurable SQLite cache and content-aware redundant search prevention
28+- **Git integration**: Diff viewing and change tracking for version control awareness
29+- **LSP diagnostics**: Closed-loop edit validation with language server feedback
30+- **Deterministic schemas**: KV-cache optimized tool definitions for reproducible LLM behavior
2831
2932 ## Quick Start
3033
exa_aiandwebsearch.md+0−62
@@ -1,62 +0,0 @@
1-# Web/Search Tools Feature
2-
3-## Current Architecture
4-
5-- **Credentials**: `~/.config/symb/credentials.json` with `providers.<name>.api_key` structure. Already supports arbitrary provider names — `exa_ai` fits naturally.
6-- **Tools**: Defined as `mcp.Tool` (name + description + JSON schema), registered on the `mcp.Proxy` via `RegisterTool(tool, handler)`. Handlers are `func(ctx, json.RawMessage) (*ToolResult, error)`.
7-- **Prompts**: 4 model-specific markdown files (`anthropic.md`, `gemini.md`, `qwen.md`, `gpt.md`), embedded via `//go:embed`. Each documents the available tools.
8-- **Registration**: All in `cmd/symb/main.go` — create tool definition + handler, register on proxy.
9-
10-## Exa AI API
11-
12-- **Auth**: `x-api-key: <key>` header
13-- **Key endpoint**: `POST https://api.exa.ai/search` — supports `type: "auto"`, inline `contents` extraction (text, highlights, summary), domain filtering, date filtering
14-- **Response**: Returns `results[]` with `url`, `title`, `text`, `publishedDate`, etc.
15-- Free tier: $10 credits, ~2000 searches
16-
17-## Design: Two New Tools
18-
19-### 1. `WebFetch` — HTTP fetch with HTML cleaning
20-
21-- **Purpose**: Fetch a URL and return cleaned text content (strip HTML tags, scripts, styles — preserve readable text)
22-- **Args**: `url` (required), `max_chars` (optional, default 10000)
23-- **Implementation**: Go's `net/http` GET, then HTML-to-text extraction (use `golang.org/x/net/html` tokenizer to strip tags)
24-- **Returns**: Cleaned text content, truncated to `max_chars`
25-
26-### 2. `WebSearch` — Exa AI search
27-
28-- **Purpose**: Search the web for information, documentation, etc.
29-- **Args**: `query` (required), `num_results` (optional, default 5), `type` (optional: "auto"/"fast"/"deep"), `include_domains` (optional), `include_text` (optional)
30-- **Implementation**: POST to `https://api.exa.ai/search` with `contents.text.maxCharacters` to keep tokens reasonable
31-- **Returns**: Formatted results (title, URL, snippet/text)
32-- **Requires**: Exa API key from `credentials.json` under `providers.exa_ai.api_key`
33-
34-### New file: `internal/mcp_tools/web.go`
35-
36-Both tools in one file. The Exa client is simple enough to not warrant a separate package — just HTTP POST with JSON.
37-
38-### Search cache
39-
40-In-memory cache shared by both `WebSearch` and `WebFetch`. Keyed by normalized string (lowercased + trimmed query for search, raw URL for fetch). Each entry stores the result and a timestamp. Entries older than 24 hours are considered stale and re-fetched. Simple `map` + `sync.RWMutex`, no persistence — cache resets on restart, which is fine since sessions are short-lived.
41-
42-### Credential plumbing
43-
44-`main.go` already loads credentials. Pass `creds.GetAPIKey("exa_ai")` to the WebSearch handler constructor. WebFetch needs no credentials.
45-
46-### Prompt updates
47-
48-Add `WebFetch` and `WebSearch` tool docs to all 4 prompt files. Add guidance like "Search before assuming when asked about external APIs, libraries, or current information."
49-
50-## Implementation Plan
51-
52-| Step | File(s) | Description |
53-|------|---------|-------------|
54-| 1 | `internal/mcp_tools/web.go` | `WebFetch` tool + handler (HTTP GET, HTML cleaning) |
55-| 2 | `internal/mcp_tools/web.go` | `WebSearch` tool + handler (Exa API client) |
56-| 3 | `internal/mcp_tools/web_test.go` | Tests for both tools |
57-| 4 | `cmd/symb/main.go` | Register both tools, pass Exa API key |
58-| 5 | `internal/llm/*.md` | Add tool docs + "search before assuming" guidance to all 4 prompts |
59-| 6 | `go.mod` | Add `golang.org/x/net` if needed for HTML parsing |
60-| 7 | `make lint && make test` | Verify |
61-
62-TODO: sqlite store?
internal/mcp_tools/edit.go+46−90
@@ -48,97 +48,11 @@ type CreateOp struct {
4848 Content string `json:"content"` // full file content
4949 }
5050
51+// anchor is the JSON schema fragment for a hashline anchor object.
52+const anchorSchema = `{"type": "object", "properties": {"line": {"type": "integer", "description": "1-indexed line number"}, "hash": {"type": "string", "description": "2-char hex hash from Read output"}}, "required": ["line", "hash"]}`
53+
5154 // NewEditTool creates the Edit tool definition.
5255 func NewEditTool() mcp.Tool {
53- schema := map[string]interface{}{
54- "type": "object",
55- "properties": map[string]interface{}{
56- "file": map[string]interface{}{
57- "type": "string",
58- "description": "Path to the file to edit",
59- },
60- "replace": map[string]interface{}{
61- "type": "object",
62- "description": "Replace lines from start to end (inclusive) with new content",
63- "properties": map[string]interface{}{
64- "start": map[string]interface{}{
65- "type": "object",
66- "description": "Anchor for first line to replace",
67- "properties": map[string]interface{}{
68- "line": map[string]interface{}{"type": "integer", "description": "1-indexed line number"},
69- "hash": map[string]interface{}{"type": "string", "description": "2-char hex hash from Read output"},
70- },
71- "required": []string{"line", "hash"},
72- },
73- "end": map[string]interface{}{
74- "type": "object",
75- "description": "Anchor for last line to replace",
76- "properties": map[string]interface{}{
77- "line": map[string]interface{}{"type": "integer", "description": "1-indexed line number"},
78- "hash": map[string]interface{}{"type": "string", "description": "2-char hex hash from Read output"},
79- },
80- "required": []string{"line", "hash"},
81- },
82- "content": map[string]interface{}{"type": "string", "description": "Replacement text (may be multiple lines)"},
83- },
84- "required": []string{"start", "end", "content"},
85- },
86- "insert": map[string]interface{}{
87- "type": "object",
88- "description": "Insert new lines after the anchored line",
89- "properties": map[string]interface{}{
90- "after": map[string]interface{}{
91- "type": "object",
92- "description": "Anchor for the line to insert after",
93- "properties": map[string]interface{}{
94- "line": map[string]interface{}{"type": "integer", "description": "1-indexed line number"},
95- "hash": map[string]interface{}{"type": "string", "description": "2-char hex hash from Read output"},
96- },
97- "required": []string{"line", "hash"},
98- },
99- "content": map[string]interface{}{"type": "string", "description": "Text to insert (may be multiple lines)"},
100- },
101- "required": []string{"after", "content"},
102- },
103- "delete": map[string]interface{}{
104- "type": "object",
105- "description": "Delete lines from start to end (inclusive)",
106- "properties": map[string]interface{}{
107- "start": map[string]interface{}{
108- "type": "object",
109- "description": "Anchor for first line to delete",
110- "properties": map[string]interface{}{
111- "line": map[string]interface{}{"type": "integer", "description": "1-indexed line number"},
112- "hash": map[string]interface{}{"type": "string", "description": "2-char hex hash from Read output"},
113- },
114- "required": []string{"line", "hash"},
115- },
116- "end": map[string]interface{}{
117- "type": "object",
118- "description": "Anchor for last line to delete",
119- "properties": map[string]interface{}{
120- "line": map[string]interface{}{"type": "integer", "description": "1-indexed line number"},
121- "hash": map[string]interface{}{"type": "string", "description": "2-char hex hash from Read output"},
122- },
123- "required": []string{"line", "hash"},
124- },
125- },
126- "required": []string{"start", "end"},
127- },
128- "create": map[string]interface{}{
129- "type": "object",
130- "description": "Create a new file (fails if file already exists)",
131- "properties": map[string]interface{}{
132- "content": map[string]interface{}{"type": "string", "description": "Full file content"},
133- },
134- "required": []string{"content"},
135- },
136- },
137- "required": []string{"file"},
138- }
139-
140- schemaJSON, _ := json.Marshal(schema)
141-
14256 return mcp.Tool{
14357 Name: "Edit",
14458 Description: `Edit a file using hash-anchored operations. You MUST Read the file first to get line hashes.
@@ -146,7 +60,49 @@ Each line from Read is tagged as "linenum:hash|content". Use the line number and
14660 Exactly one operation per call: replace, insert, delete, or create.
14761 If a hash does not match, the file changed since you read it — re-Read and retry.
14862 After each edit you receive fresh hashes — use those for subsequent edits, not the old ones.`,
149- InputSchema: schemaJSON,
63+ InputSchema: json.RawMessage(`{
64+ "type": "object",
65+ "properties": {
66+ "file": {"type": "string", "description": "Path to the file to edit"},
67+ "replace": {
68+ "type": "object",
69+ "description": "Replace lines from start to end (inclusive) with new content",
70+ "properties": {
71+ "start": ` + anchorSchema + `,
72+ "end": ` + anchorSchema + `,
73+ "content": {"type": "string", "description": "Replacement text (may be multiple lines)"}
74+ },
75+ "required": ["start", "end", "content"]
76+ },
77+ "insert": {
78+ "type": "object",
79+ "description": "Insert new lines after the anchored line",
80+ "properties": {
81+ "after": ` + anchorSchema + `,
82+ "content": {"type": "string", "description": "Text to insert (may be multiple lines)"}
83+ },
84+ "required": ["after", "content"]
85+ },
86+ "delete": {
87+ "type": "object",
88+ "description": "Delete lines from start to end (inclusive)",
89+ "properties": {
90+ "start": ` + anchorSchema + `,
91+ "end": ` + anchorSchema + `
92+ },
93+ "required": ["start", "end"]
94+ },
95+ "create": {
96+ "type": "object",
97+ "description": "Create a new file (fails if file already exists)",
98+ "properties": {
99+ "content": {"type": "string", "description": "Full file content"}
100+ },
101+ "required": ["content"]
102+ }
103+ },
104+ "required": ["file"]
105+ }`),
150106 }
151107 }
152108
internal/mcp_tools/git.go+13−30
@@ -24,47 +24,30 @@ type GitDiffArgs struct {
2424
2525 // NewGitStatusTool creates the GitStatus tool definition.
2626 func NewGitStatusTool() mcp.Tool {
27- schema := map[string]interface{}{
28- "type": "object",
29- "properties": map[string]interface{}{
30- "long": map[string]interface{}{
31- "type": "boolean",
32- "description": "Use long format output. Default: false (short format)",
33- },
34- },
35- }
36-
37- schemaJSON, _ := json.Marshal(schema)
38-
3927 return mcp.Tool{
4028 Name: "GitStatus",
4129 Description: "Show the working tree status. Returns modified, staged, and untracked files.",
42- InputSchema: schemaJSON,
30+ InputSchema: json.RawMessage(`{
31+ "type": "object",
32+ "properties": {
33+ "long": {"type": "boolean", "description": "Use long format output. Default: false (short format)"}
34+ }
35+ }`),
4336 }
4437 }
4538
4639 // NewGitDiffTool creates the GitDiff tool definition.
4740 func NewGitDiffTool() mcp.Tool {
48- schema := map[string]interface{}{
49- "type": "object",
50- "properties": map[string]interface{}{
51- "file": map[string]interface{}{
52- "type": "string",
53- "description": "Optional: specific file path to diff. If omitted, diffs all changed files.",
54- },
55- "staged": map[string]interface{}{
56- "type": "boolean",
57- "description": "If true, show staged (cached) changes. Default: false (unstaged changes)",
58- },
59- },
60- }
61-
62- schemaJSON, _ := json.Marshal(schema)
63-
6441 return mcp.Tool{
6542 Name: "GitDiff",
6643 Description: "Show changes between working tree and index (unstaged), or between index and HEAD (staged). Returns unified diff output.",
67- InputSchema: schemaJSON,
44+ InputSchema: json.RawMessage(`{
45+ "type": "object",
46+ "properties": {
47+ "file": {"type": "string", "description": "Optional: specific file path to diff. If omitted, diffs all changed files."},
48+ "staged": {"type": "boolean", "description": "If true, show staged (cached) changes. Default: false (unstaged changes)"}
49+ }
50+ }`),
6851 }
6952 }
7053
internal/mcp_tools/grep.go+10−26
@@ -22,35 +22,19 @@ type GrepArgs struct {
2222
2323 // NewGrepTool creates the grep tool definition.
2424 func NewGrepTool() mcp.Tool {
25- schema := map[string]interface{}{
26- "type": "object",
27- "properties": map[string]interface{}{
28- "pattern": map[string]interface{}{
29- "type": "string",
30- "description": "Pattern to search for (regex). For filenames: matches against basename or path. For content: matches line contents.",
31- },
32- "content_search": map[string]interface{}{
33- "type": "boolean",
34- "description": "If true, search file contents (grep); if false, search filenames (find). Default: false",
35- },
36- "max_results": map[string]interface{}{
37- "type": "integer",
38- "description": "Maximum number of results to return. Default: 100",
39- },
40- "case_sensitive": map[string]interface{}{
41- "type": "boolean",
42- "description": "Enable case-sensitive matching. Default: false (case-insensitive)",
43- },
44- },
45- "required": []string{"pattern"},
46- }
47-
48- schemaJSON, _ := json.Marshal(schema)
49-
5025 return mcp.Tool{
5126 Name: "Grep",
5227 Description: "Search for files by name (fuzzy) or search file contents (grep). Respects .gitignore. Use content_search=false for finding files, content_search=true for searching content.",
53- InputSchema: schemaJSON,
28+ InputSchema: json.RawMessage(`{
29+ "type": "object",
30+ "properties": {
31+ "pattern": {"type": "string", "description": "Pattern to search for (regex). For filenames: matches against basename or path. For content: matches line contents."},
32+ "content_search": {"type": "boolean", "description": "If true, search file contents (grep); if false, search filenames (find). Default: false"},
33+ "max_results": {"type": "integer", "description": "Maximum number of results to return. Default: 100"},
34+ "case_sensitive": {"type": "boolean", "description": "Enable case-sensitive matching. Default: false (case-insensitive)"}
35+ },
36+ "required": ["pattern"]
37+ }`),
5438 }
5539 }
5640
internal/mcp_tools/open.go+9−22
@@ -30,31 +30,18 @@ type ShowMsg struct {
3030
3131 // NewReadTool creates the Read tool definition.
3232 func NewReadTool() mcp.Tool {
33- schema := map[string]interface{}{
34- "type": "object",
35- "properties": map[string]interface{}{
36- "file": map[string]interface{}{
37- "type": "string",
38- "description": "Path to the file to read",
39- },
40- "start": map[string]interface{}{
41- "type": "integer",
42- "description": "Optional: starting line number (1-indexed, inclusive)",
43- },
44- "end": map[string]interface{}{
45- "type": "integer",
46- "description": "Optional: ending line number (1-indexed, inclusive)",
47- },
48- },
49- "required": []string{"file"},
50- }
51-
52- schemaJSON, _ := json.Marshal(schema)
53-
5433 return mcp.Tool{
5534 Name: "Read",
5635 Description: `Reads a file and returns hashline-tagged content. Each line is returned as "linenum:hash|content". You MUST Read a file before editing it with Edit. Use start/end for line ranges. Does NOT display in the editor — use Show for that.`,
57- InputSchema: schemaJSON,
36+ InputSchema: json.RawMessage(`{
37+ "type": "object",
38+ "properties": {
39+ "file": {"type": "string", "description": "Path to the file to read"},
40+ "start": {"type": "integer", "description": "Optional: starting line number (1-indexed, inclusive)"},
41+ "end": {"type": "integer", "description": "Optional: ending line number (1-indexed, inclusive)"}
42+ },
43+ "required": ["file"]
44+ }`),
5845 }
5946 }
6047
internal/mcp_tools/show.go+8−18
@@ -17,27 +17,17 @@ type ShowArgs struct {
1717
1818 // NewShowTool creates the Show tool definition.
1919 func NewShowTool() mcp.Tool {
20- schema := map[string]interface{}{
21- "type": "object",
22- "properties": map[string]interface{}{
23- "content": map[string]interface{}{
24- "type": "string",
25- "description": "The content to display in the editor pane",
26- },
27- "language": map[string]interface{}{
28- "type": "string",
29- "description": "Language for syntax highlighting (e.g. go, python, diff, markdown). Defaults to text.",
30- },
31- },
32- "required": []string{"content"},
33- }
34-
35- schemaJSON, _ := json.Marshal(schema)
36-
3720 return mcp.Tool{
3821 Name: "Show",
3922 Description: `Displays content in the user's editor pane with syntax highlighting. Use this to show code snippets, diffs, generated code, or any text the user should see. Does NOT read or write files.`,
40- InputSchema: schemaJSON,
23+ InputSchema: json.RawMessage(`{
24+ "type": "object",
25+ "properties": {
26+ "content": {"type": "string", "description": "The content to display in the editor pane"},
27+ "language": {"type": "string", "description": "Language for syntax highlighting (e.g. go, python, diff, markdown). Defaults to text."}
28+ },
29+ "required": ["content"]
30+ }`),
4131 }
4232 }
4333
internal/mcp_tools/web.go+18−46
@@ -29,27 +29,17 @@ type WebFetchArgs struct {
2929
3030 // NewWebFetchTool creates the WebFetch tool definition.
3131 func NewWebFetchTool() mcp.Tool {
32- schema := map[string]interface{}{
33- "type": "object",
34- "properties": map[string]interface{}{
35- "url": map[string]interface{}{
36- "type": "string",
37- "description": "The URL to fetch.",
38- },
39- "max_chars": map[string]interface{}{
40- "type": "integer",
41- "description": "Maximum characters to return. Default: 10000",
42- },
43- },
44- "required": []string{"url"},
45- }
46-
47- schemaJSON, _ := json.Marshal(schema)
48-
4932 return mcp.Tool{
5033 Name: "WebFetch",
5134 Description: "Fetch a URL and return its content as cleaned text (HTML tags, scripts, and styles stripped). Results are cached.",
52- InputSchema: schemaJSON,
35+ InputSchema: json.RawMessage(`{
36+ "type": "object",
37+ "properties": {
38+ "url": {"type": "string", "description": "The URL to fetch."},
39+ "max_chars": {"type": "integer", "description": "Maximum characters to return. Default: 10000"}
40+ },
41+ "required": ["url"]
42+ }`),
5343 }
5444 }
5545
@@ -153,37 +143,19 @@ type exaResult struct {
153143
154144 // NewWebSearchTool creates the WebSearch tool definition.
155145 func NewWebSearchTool() mcp.Tool {
156- schema := map[string]interface{}{
157- "type": "object",
158- "properties": map[string]interface{}{
159- "query": map[string]interface{}{
160- "type": "string",
161- "description": "Search query.",
162- },
163- "num_results": map[string]interface{}{
164- "type": "integer",
165- "description": "Number of results to return. Default: 5",
166- },
167- "type": map[string]interface{}{
168- "type": "string",
169- "description": "Search type: \"auto\" (default), \"fast\", or \"deep\".",
170- "enum": []string{"auto", "fast", "deep"},
171- },
172- "include_domains": map[string]interface{}{
173- "type": "array",
174- "items": map[string]interface{}{"type": "string"},
175- "description": "Only include results from these domains.",
176- },
177- },
178- "required": []string{"query"},
179- }
180-
181- schemaJSON, _ := json.Marshal(schema)
182-
183146 return mcp.Tool{
184147 Name: "WebSearch",
185148 Description: "Search the web using Exa AI. Use this to look up documentation, APIs, libraries, or current information. Results are cached.",
186- InputSchema: schemaJSON,
149+ InputSchema: json.RawMessage(`{
150+ "type": "object",
151+ "properties": {
152+ "query": {"type": "string", "description": "Search query."},
153+ "num_results": {"type": "integer", "description": "Number of results to return. Default: 5"},
154+ "type": {"type": "string", "description": "Search type: \"auto\" (default), \"fast\", or \"deep\".", "enum": ["auto", "fast", "deep"]},
155+ "include_domains": {"type": "array", "items": {"type": "string"}, "description": "Only include results from these domains."}
156+ },
157+ "required": ["query"]
158+ }`),
187159 }
188160 }
189161
internal/provider/anthropic.go+51−27
@@ -15,13 +15,25 @@ const roleSystem = "system"
1515 // Anthropic Messages API request types.
1616
1717 type anthropicRequest struct {
18- Model string `json:"model"`
19- Messages []anthropicMessage `json:"messages"`
20- System string `json:"system,omitempty"`
21- MaxTokens int `json:"max_tokens"`
22- Temperature float64 `json:"temperature,omitempty"`
23- Stream bool `json:"stream"`
24- Tools []anthropicTool `json:"tools,omitempty"`
18+ Model string `json:"model"`
19+ Messages []anthropicMessage `json:"messages"`
20+ System []anthropicCacheBlock `json:"system,omitempty"`
21+ MaxTokens int `json:"max_tokens"`
22+ Temperature float64 `json:"temperature,omitempty"`
23+ Stream bool `json:"stream"`
24+ Tools []anthropicTool `json:"tools,omitempty"`
25+}
26+
27+// anthropicCacheControl marks a block for prompt caching.
28+type anthropicCacheControl struct {
29+ Type string `json:"type"` // "ephemeral"
30+}
31+
32+// anthropicCacheBlock is a system prompt content block with optional cache_control.
33+type anthropicCacheBlock struct {
34+ Type string `json:"type"` // "text"
35+ Text string `json:"text"`
36+ CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
2537 }
2638
2739 type anthropicMessage struct {
@@ -51,9 +63,10 @@ type anthropicToolResultBlock struct {
5163 }
5264
5365 type anthropicTool struct {
54- Name string `json:"name"`
55- Description string `json:"description,omitempty"`
56- InputSchema interface{} `json:"input_schema"`
66+ Name string `json:"name"`
67+ Description string `json:"description,omitempty"`
68+ InputSchema json.RawMessage `json:"input_schema"`
69+ CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
5770 }
5871
5972 // Anthropic SSE streaming response types.
@@ -81,8 +94,9 @@ type anthropicContentBlockDelta struct {
8194 }
8295
8396 // toAnthropicMessages converts provider-agnostic messages to Anthropic Messages API format.
84-// Returns (system, messages) — system is extracted and hoisted out.
85-func toAnthropicMessages(messages []Message) (string, []anthropicMessage) {
97+// Returns (system blocks, messages) — system is extracted and hoisted out.
98+// The last system block gets cache_control for prompt caching.
99+func toAnthropicMessages(messages []Message) ([]anthropicCacheBlock, []anthropicMessage) {
86100 var systemParts []string
87101 var result []anthropicMessage
88102
@@ -142,27 +156,31 @@ func toAnthropicMessages(messages []Message) (string, []anthropicMessage) {
142156 })
143157 }
144158
145- return strings.Join(systemParts, "\n\n"), result
159+ var system []anthropicCacheBlock
160+ if len(systemParts) > 0 {
161+ system = make([]anthropicCacheBlock, len(systemParts))
162+ for i, part := range systemParts {
163+ system[i] = anthropicCacheBlock{Type: "text", Text: part}
164+ }
165+ // Mark last system block for prompt caching.
166+ system[len(system)-1].CacheControl = &anthropicCacheControl{Type: "ephemeral"}
167+ }
168+ return system, result
146169 }
147170
148171 // toAnthropicTools converts provider-agnostic tools to Anthropic tool format.
149-func toAnthropicTools(tools []Tool) ([]anthropicTool, error) {
172+// InputSchema is passed through as json.RawMessage to preserve deterministic
173+// serialization order (important for KV-cache hit rate).
174+func toAnthropicTools(tools []Tool) []anthropicTool {
150175 if tools == nil {
151- return nil, nil
176+ return nil
152177 }
178+ emptySchema := json.RawMessage(`{"type":"object","properties":{}}`)
153179 result := make([]anthropicTool, len(tools))
154180 for i, t := range tools {
155- var schema interface{}
156- if len(t.Parameters) > 0 {
157- if err := json.Unmarshal(t.Parameters, &schema); err != nil {
158- return nil, err
159- }
160- }
161- if schema == nil {
162- schema = map[string]interface{}{
163- "type": "object",
164- "properties": map[string]interface{}{},
165- }
181+ schema := t.Parameters
182+ if len(schema) == 0 {
183+ schema = emptySchema
166184 }
167185 result[i] = anthropicTool{
168186 Name: t.Name,
@@ -170,7 +188,13 @@ func toAnthropicTools(tools []Tool) ([]anthropicTool, error) {
170188 InputSchema: schema,
171189 }
172190 }
173- return result, nil
191+ // Mark last tool for prompt caching. Anthropic caches the prefix up to
192+ // and including blocks with cache_control, so tools + system form a
193+ // stable cached prefix across turns.
194+ if len(result) > 0 {
195+ result[len(result)-1].CacheControl = &anthropicCacheControl{Type: "ephemeral"}
196+ }
197+ return result
174198 }
175199
176200 // parseAnthropicSSEStream reads Anthropic Messages API SSE events and emits StreamEvents.
internal/provider/ollama.go+10−17
@@ -101,9 +101,9 @@ type ollamaReqTool struct {
101101 }
102102
103103 type ollamaReqFunction struct {
104- Name string `json:"name"`
105- Description string `json:"description"`
106- Parameters map[string]interface{} `json:"parameters"`
104+ Name string `json:"name"`
105+ Description string `json:"description"`
106+ Parameters json.RawMessage `json:"parameters"`
107107 }
108108
109109 type ollamaReqToolCall struct {
@@ -152,23 +152,16 @@ func toOllamaMessages(messages []Message) []ollamaReqMessage {
152152 return result
153153 }
154154
155+// toOllamaTools converts provider-agnostic tools to Ollama tool format.
156+// Parameters is passed through as json.RawMessage to preserve deterministic
157+// serialization order (important for KV-cache hit rate).
155158 func toOllamaTools(tools []Tool) []ollamaReqTool {
159+ emptyParams := json.RawMessage(`{"type":"object","properties":{}}`)
156160 result := make([]ollamaReqTool, len(tools))
157161 for i, t := range tools {
158- var params map[string]interface{}
159- if len(t.Parameters) > 0 {
160- if err := json.Unmarshal(t.Parameters, &params); err != nil {
161- params = map[string]interface{}{
162- "type": "object",
163- "properties": map[string]interface{}{},
164- }
165- }
166- }
167- if params == nil {
168- params = map[string]interface{}{
169- "type": "object",
170- "properties": map[string]interface{}{},
171- }
162+ params := t.Parameters
163+ if len(params) == 0 {
164+ params = emptyParams
172165 }
173166
174167 result[i] = ollamaReqTool{
internal/provider/openai_common.go+9−14
@@ -302,23 +302,18 @@ func mergeSystemMessagesOpenAI(messages []openai.ChatCompletionMessage) []openai
302302 }
303303
304304 // toOpenAITools converts provider-agnostic tools to OpenAI SDK tool format.
305-func toOpenAITools(tools []Tool) ([]openai.Tool, error) {
305+// Parameters is passed through as json.RawMessage to preserve deterministic
306+// serialization order (important for KV-cache hit rate).
307+func toOpenAITools(tools []Tool) []openai.Tool {
306308 if tools == nil {
307- return nil, nil
309+ return nil
308310 }
311+ emptyParams := json.RawMessage(`{"type":"object","properties":{}}`)
309312 result := make([]openai.Tool, len(tools))
310313 for i, t := range tools {
311- var params map[string]interface{}
312- if len(t.Parameters) > 0 {
313- if err := json.Unmarshal(t.Parameters, &params); err != nil {
314- return nil, err
315- }
316- }
317- if params == nil {
318- params = map[string]interface{}{
319- "type": "object",
320- "properties": map[string]interface{}{},
321- }
314+ params := t.Parameters
315+ if len(params) == 0 {
316+ params = emptyParams
322317 }
323318
324319 result[i] = openai.Tool{
@@ -330,5 +325,5 @@ func toOpenAITools(tools []Tool) ([]openai.Tool, error) {
330325 },
331326 }
332327 }
333- return result, nil
328+ return result
334329 }
internal/provider/opencode.go+3−8
@@ -82,10 +82,7 @@ func (p *OpenCodeProvider) ChatStream(ctx context.Context, messages []Message, t
8282
8383 // chatStreamOpenAI streams via the OpenAI-compatible /chat/completions endpoint.
8484 func (p *OpenCodeProvider) chatStreamOpenAI(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
85- openaiTools, err := toOpenAITools(tools)
86- if err != nil {
87- return nil, fmt.Errorf("invalid tool schema: %w", err)
88- }
85+ openaiTools := toOpenAITools(tools)
8986
9087 customReq := openCodeRequest{
9188 Model: p.model,
@@ -125,10 +122,7 @@ func (p *OpenCodeProvider) chatStreamOpenAI(ctx context.Context, messages []Mess
125122 func (p *OpenCodeProvider) chatStreamAnthropic(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
126123 system, anthropicMsgs := toAnthropicMessages(messages)
127124
128- anthropicTools, err := toAnthropicTools(tools)
129- if err != nil {
130- return nil, fmt.Errorf("invalid tool schema: %w", err)
131- }
125+ anthropicTools := toAnthropicTools(tools)
132126
133127 req := anthropicRequest{
134128 Model: p.model,
@@ -147,6 +141,7 @@ func (p *OpenCodeProvider) chatStreamAnthropic(ctx context.Context, messages []M
147141 headers := p.authHeaders()
148142 headers["x-api-key"] = p.apiKey
149143 headers["anthropic-version"] = "2023-06-01"
144+ headers["anthropic-beta"] = "prompt-caching-2024-07-31"
150145
151146 reader, err := httpDoSSE(ctx, httpRequestConfig{
152147 client: p.httpClient,