{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://ai-universe.com/schemas/agent.second-opinion.schema.json",
    "title": "Agent Second Opinion Response",
    "description": "Schema for agent.second_opinion MCP tool responses. Defines the contract for multi-model consultation results.",
    "type": "object",
    "required": ["primary", "conversationId"],
    "properties": {
        "conversationId": {
            "type": "string",
            "minLength": 1,
            "description": "ID of the conversation where this consultation was stored"
        },
        "conversationCreated": {
            "type": "boolean",
            "description": "Whether a new conversation was created for this consultation"
        },
        "conversationTitle": {
            "type": ["string", "null"],
            "description": "Title of the conversation"
        },
        "primary": {
            "$ref": "#/definitions/modelOpinion",
            "description": "Primary model response"
        },
        "secondaryOpinions": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/modelOpinion"
            },
            "description": "Additional model opinions for comparison"
        },
        "synthesis": {
            "oneOf": [
                { "$ref": "#/definitions/synthesis" },
                { "type": "null" }
            ],
            "description": "Synthesized response combining all model insights (null when maxOpinions=0)"
        },
        "userMessageId": {
            "type": ["string", "null"],
            "description": "ID of the user message in the conversation"
        },
        "assistantMessageId": {
            "type": ["string", "null"],
            "description": "ID of the assistant message in the conversation"
        },
        "persistenceStatus": {
            "$ref": "#/definitions/persistenceStatus",
            "description": "Status of conversation persistence operations"
        },
        "assistantMessage": {
            "$ref": "#/definitions/conversationMessage",
            "description": "Full assistant message object with metadata"
        },
        "metadata": {
            "type": "object",
            "required": ["secondOpinion"],
            "properties": {
                "secondOpinion": {
                    "type": "object",
                    "required": ["primary", "synthesis"],
                    "properties": {
                        "primary": {
                            "$ref": "#/definitions/modelOpinion"
                        },
                        "secondaryOpinions": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/modelOpinion"
                            }
                        },
                        "synthesis": {
                            "$ref": "#/definitions/synthesis"
                        },
                        "summary": {
                            "$ref": "#/definitions/summary"
                        }
                    }
                }
            },
            "description": "Metadata containing structured second opinion data"
        },
        "summary": {
            "$ref": "#/definitions/summary",
            "description": "Summary statistics for the consultation"
        }
    },
    "definitions": {
        "modelOpinion": {
            "type": "object",
            "required": ["model", "response"],
            "properties": {
                "model": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Model identifier (e.g., 'cerebras', 'gemini', 'grok')"
                },
                "modelId": {
                    "type": "string",
                    "description": "Full model ID with version (e.g., 'cerebras-llama-3.3-70b')"
                },
                "modelDisplayName": {
                    "type": "string",
                    "description": "Human-readable model name"
                },
                "response": {
                    "type": "string",
                    "description": "Model's response text"
                },
                "tokensUsed": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Number of tokens used"
                },
                "latencyMs": {
                    "type": "number",
                    "minimum": 0,
                    "description": "Response latency in milliseconds"
                },
                "cost": {
                    "type": "number",
                    "minimum": 0,
                    "description": "Cost in USD"
                },
                "citations": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "required": ["title", "url"],
                        "properties": {
                            "title": {
                                "type": "string"
                            },
                            "url": {
                                "type": "string",
                                "format": "uri"
                            },
                            "snippet": {
                                "type": "string"
                            }
                        }
                    },
                    "description": "Source citations from the model"
                },
                "error": {
                    "type": "boolean",
                    "description": "Whether this response contains an error"
                }
            },
            "additionalProperties": true
        },
        "synthesis": {
            "type": "object",
            "required": ["model", "response"],
            "properties": {
                "model": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Model used for synthesis"
                },
                "modelId": {
                    "type": "string",
                    "description": "Full model ID for synthesis"
                },
                "modelDisplayName": {
                    "type": "string",
                    "description": "Display name for synthesis model"
                },
                "response": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Synthesized response combining all insights"
                },
                "tokensUsed": {
                    "type": "integer",
                    "minimum": 0
                },
                "latencyMs": {
                    "type": "number",
                    "minimum": 0
                },
                "cost": {
                    "type": "number",
                    "minimum": 0
                },
                "sources": {
                    "type": "array",
                    "items": true,
                    "description": "Source references used in synthesis"
                },
                "error": {
                    "type": "boolean"
                }
            },
            "additionalProperties": true
        },
        "summary": {
            "type": "object",
            "properties": {
                "totalModels": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Total number of models consulted"
                },
                "totalTokens": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Total tokens used across all models"
                },
                "totalCost": {
                    "type": "number",
                    "minimum": 0,
                    "description": "Total cost in USD"
                },
                "totalLatencyMs": {
                    "type": "number",
                    "minimum": 0,
                    "description": "Total latency in milliseconds"
                }
            }
        },
        "persistenceStatus": {
            "type": "object",
            "required": ["userMessageSaved", "assistantMessageSaved", "userMessageId", "assistantMessageId"],
            "properties": {
                "userMessageSaved": {
                    "type": "boolean"
                },
                "assistantMessageSaved": {
                    "type": "boolean"
                },
                "userMessageId": {
                    "type": ["string", "null"]
                },
                "assistantMessageId": {
                    "type": ["string", "null"]
                },
                "error": {
                    "type": ["string", "null"]
                }
            },
            "additionalProperties": true
        },
        "conversationMessage": {
            "type": "object",
            "required": ["id", "content", "role", "sequence", "createdAt"],
            "properties": {
                "id": {
                    "type": "string",
                    "minLength": 1
                },
                "content": {
                    "type": "string"
                },
                "role": {
                    "type": "string",
                    "enum": ["user", "assistant", "system"]
                },
                "sequence": {
                    "type": "integer",
                    "minimum": 0
                },
                "createdAt": {
                    "type": "string",
                    "format": "date-time"
                },
                "metadata": {
                    "type": "object",
                    "additionalProperties": true
                }
            }
        }
    }
}
