| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | |
| 7 | "reasonix/internal/tool" |
| 8 | ) |
| 9 | |
| 10 | // parseToolResultWithSchema keeps output-schema validation advisory. Third-party |
| 11 | // servers sometimes publish a stale or incompatible output schema; Reasonix |
| 12 | // records that mismatch without discarding an otherwise usable tool result. |
| 13 | func parseToolResultWithSchema(res, outputSchema json.RawMessage) (string, []string, error) { |
| 14 | var envelope struct { |
| 15 | StructuredContent json.RawMessage `json:"structuredContent"` |
| 16 | } |
| 17 | if len(outputSchema) > 0 && json.Unmarshal(res, &envelope) == nil { |
| 18 | structured := bytes.TrimSpace(envelope.StructuredContent) |
| 19 | if len(structured) > 0 && !bytes.Equal(structured, []byte("null")) { |
| 20 | validation := tool.ValidateJSONSchemaValue(outputSchema, structured) |
| 21 | if validation.CompileErr != nil || len(validation.Violations) > 0 { |
| 22 | hostProtocol.outputSchemaMismatch.Add(1) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | return parseToolResult(res) |
| 27 | } |
| 28 |