# DO NOT EDIT THIS FILE!
#
# This file is generated from the CDP specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# CDP domain: WebMCP (experimental)
from __future__ import annotations
from .util import event_class, T_JSON_DICT
from dataclasses import dataclass
import enum
import typing
from . import dom
from . import page
from . import runtime


@dataclass
class Annotation:
    '''
    Tool annotations
    '''
    #: A hint indicating that the tool does not modify any state.
    read_only: typing.Optional[bool] = None

    #: If the declarative tool was declared with the autosubmit attribute.
    autosubmit: typing.Optional[bool] = None

    def to_json(self):
        json = dict()
        if self.read_only is not None:
            json['readOnly'] = self.read_only
        if self.autosubmit is not None:
            json['autosubmit'] = self.autosubmit
        return json

    @classmethod
    def from_json(cls, json):
        return cls(
            read_only=bool(json['readOnly']) if 'readOnly' in json else None,
            autosubmit=bool(json['autosubmit']) if 'autosubmit' in json else None,
        )


@dataclass
class Tool:
    '''
    Definition of a tool that can be invoked.
    '''
    #: Tool name.
    name: str

    #: Tool description.
    description: str

    #: Frame identifier associated with the tool registration.
    frame_id: page.FrameId

    #: Schema for the tool's input parameters.
    input_schema: typing.Optional[dict] = None

    #: Optional annotations for the tool.
    annotations: typing.Optional[Annotation] = None

    #: Optional node ID for declarative tools.
    backend_node_id: typing.Optional[dom.BackendNodeId] = None

    #: The stack trace at the time of the registration.
    stack_trace: typing.Optional[runtime.StackTrace] = None

    def to_json(self):
        json = dict()
        json['name'] = self.name
        json['description'] = self.description
        json['frameId'] = self.frame_id.to_json()
        if self.input_schema is not None:
            json['inputSchema'] = self.input_schema
        if self.annotations is not None:
            json['annotations'] = self.annotations.to_json()
        if self.backend_node_id is not None:
            json['backendNodeId'] = self.backend_node_id.to_json()
        if self.stack_trace is not None:
            json['stackTrace'] = self.stack_trace.to_json()
        return json

    @classmethod
    def from_json(cls, json):
        return cls(
            name=str(json['name']),
            description=str(json['description']),
            frame_id=page.FrameId.from_json(json['frameId']),
            input_schema=dict(json['inputSchema']) if 'inputSchema' in json else None,
            annotations=Annotation.from_json(json['annotations']) if 'annotations' in json else None,
            backend_node_id=dom.BackendNodeId.from_json(json['backendNodeId']) if 'backendNodeId' in json else None,
            stack_trace=runtime.StackTrace.from_json(json['stackTrace']) if 'stackTrace' in json else None,
        )


def enable() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Enables the WebMCP domain, allowing events to be sent. Enabling the domain will trigger a toolsAdded event for
    all currently registered tools.
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'WebMCP.enable',
    }
    json = yield cmd_dict


@event_class('WebMCP.toolsAdded')
@dataclass
class ToolsAdded:
    '''
    Event fired when new tools are added.
    '''
    #: Array of tools that were added.
    tools: typing.List[Tool]

    @classmethod
    def from_json(cls, json: T_JSON_DICT) -> ToolsAdded:
        return cls(
            tools=[Tool.from_json(i) for i in json['tools']]
        )


@event_class('WebMCP.toolsRemoved')
@dataclass
class ToolsRemoved:
    '''
    Event fired when tools are removed.
    '''
    #: Array of tools that were removed.
    tools: typing.List[Tool]

    @classmethod
    def from_json(cls, json: T_JSON_DICT) -> ToolsRemoved:
        return cls(
            tools=[Tool.from_json(i) for i in json['tools']]
        )
