|
7 | 7 |
|
8 | 8 | "github.com/usetero/cli/internal/api/gen" |
9 | 9 | "github.com/usetero/cli/internal/domain" |
10 | | - "github.com/usetero/cli/internal/domain/tool" |
11 | 10 | "github.com/usetero/cli/internal/log" |
12 | 11 | ) |
13 | 12 |
|
@@ -119,21 +118,21 @@ func toContentBlockInput(block domain.Block) (gen.ContentBlockInput, error) { |
119 | 118 | }, nil |
120 | 119 |
|
121 | 120 | case domain.BlockTypeToolUse: |
122 | | - input, err := toToolInput(block.ToolUse) |
| 121 | + input, err := rawJSONToMap(block.ToolUse.Input) |
123 | 122 | if err != nil { |
124 | 123 | return gen.ContentBlockInput{}, fmt.Errorf("tool_use block: %w", err) |
125 | 124 | } |
126 | 125 | return gen.ContentBlockInput{ |
127 | 126 | Type: gen.ContentBlockTypeToolUse, |
128 | 127 | ToolUse: &gen.ToolUseInput{ |
129 | 128 | Id: block.ToolUse.ID, |
130 | | - Name: string(block.ToolUse.Name), |
| 129 | + Name: block.ToolUse.Name, |
131 | 130 | Input: input, |
132 | 131 | }, |
133 | 132 | }, nil |
134 | 133 |
|
135 | 134 | case domain.BlockTypeToolResult: |
136 | | - content, err := toToolResultContent(block.ToolResult) |
| 135 | + content, err := rawJSONToMapPtr(block.ToolResult.Content) |
137 | 136 | if err != nil { |
138 | 137 | return gen.ContentBlockInput{}, fmt.Errorf("tool_result block: %w", err) |
139 | 138 | } |
@@ -162,102 +161,24 @@ func toContentBlockInput(block domain.Block) (gen.ContentBlockInput, error) { |
162 | 161 | return gen.ContentBlockInput{}, fmt.Errorf("unknown block type: %s", block.Type) |
163 | 162 | } |
164 | 163 |
|
165 | | -func toToolInput(use *tool.Use) (map[string]any, error) { |
166 | | - switch use.Name { |
167 | | - case tool.AddContext: |
168 | | - if use.AddContext == nil { |
169 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
170 | | - } |
171 | | - return structToMap(use.AddContext) |
172 | | - case tool.RemoveContext: |
173 | | - if use.RemoveContext == nil { |
174 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
175 | | - } |
176 | | - return structToMap(use.RemoveContext) |
177 | | - case tool.Query: |
178 | | - if use.Query == nil { |
179 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
180 | | - } |
181 | | - return structToMap(use.Query) |
182 | | - case tool.ShowMetric: |
183 | | - if use.ShowMetric == nil { |
184 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
185 | | - } |
186 | | - return structToMap(use.ShowMetric) |
187 | | - case tool.ShowSeries: |
188 | | - if use.ShowSeries == nil { |
189 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
190 | | - } |
191 | | - return structToMap(use.ShowSeries) |
192 | | - case tool.ShowTimeSeries: |
193 | | - if use.ShowTimeSeries == nil { |
194 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
195 | | - } |
196 | | - return structToMap(use.ShowTimeSeries) |
197 | | - case tool.ShowTable: |
198 | | - if use.ShowTable == nil { |
199 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
200 | | - } |
201 | | - return structToMap(use.ShowTable) |
202 | | - case tool.StartJourney: |
203 | | - if use.StartJourney == nil { |
204 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
205 | | - } |
206 | | - return structToMap(use.StartJourney) |
207 | | - case tool.EndJourney: |
208 | | - if use.EndJourney == nil { |
209 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
210 | | - } |
211 | | - return structToMap(use.EndJourney) |
212 | | - case tool.ApprovePolicy: |
213 | | - if use.ApprovePolicy == nil { |
214 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
215 | | - } |
216 | | - return structToMap(use.ApprovePolicy) |
217 | | - case tool.DismissPolicy: |
218 | | - if use.DismissPolicy == nil { |
219 | | - return nil, fmt.Errorf("tool %s: missing typed input", use.Name) |
220 | | - } |
221 | | - return structToMap(use.DismissPolicy) |
222 | | - } |
223 | | - |
224 | | - return nil, fmt.Errorf("unknown tool: %s", use.Name) |
225 | | -} |
226 | | - |
227 | | -func toToolResultContent(result *tool.Result) (*map[string]any, error) { |
228 | | - if result.AddContext != nil { |
229 | | - m, err := structToMap(result.AddContext) |
230 | | - return &m, err |
231 | | - } |
232 | | - if result.RemoveContext != nil { |
233 | | - m, err := structToMap(result.RemoveContext) |
234 | | - return &m, err |
235 | | - } |
236 | | - if result.Query != nil { |
237 | | - m, err := structToMap(result.Query) |
238 | | - return &m, err |
239 | | - } |
240 | | - if result.ApprovePolicy != nil { |
241 | | - m, err := structToMap(result.ApprovePolicy) |
242 | | - return &m, err |
| 164 | +func rawJSONToMap(data json.RawMessage) (map[string]any, error) { |
| 165 | + if len(data) == 0 { |
| 166 | + return nil, nil |
243 | 167 | } |
244 | | - if result.DismissPolicy != nil { |
245 | | - m, err := structToMap(result.DismissPolicy) |
246 | | - return &m, err |
| 168 | + var m map[string]any |
| 169 | + if err := json.Unmarshal(data, &m); err != nil { |
| 170 | + return nil, err |
247 | 171 | } |
248 | | - |
249 | | - // No typed result - this is OK for client-executed tools |
250 | | - return nil, nil |
| 172 | + return m, nil |
251 | 173 | } |
252 | 174 |
|
253 | | -func structToMap(v any) (map[string]any, error) { |
254 | | - data, err := json.Marshal(v) |
255 | | - if err != nil { |
256 | | - return nil, fmt.Errorf("marshal struct: %w", err) |
| 175 | +func rawJSONToMapPtr(data json.RawMessage) (*map[string]any, error) { |
| 176 | + if len(data) == 0 { |
| 177 | + return nil, nil |
257 | 178 | } |
258 | 179 | var m map[string]any |
259 | 180 | if err := json.Unmarshal(data, &m); err != nil { |
260 | | - return nil, fmt.Errorf("unmarshal to map: %w", err) |
| 181 | + return nil, err |
261 | 182 | } |
262 | | - return m, nil |
| 183 | + return &m, nil |
263 | 184 | } |
0 commit comments