name = $name; $this->description = $description; $this->parameters = $parameters; $this->type = $type; $this->id = $id; } public function serializeForOpenAI() { // Initialize the base structure with name and description $json = [ 'name' => $this->name, 'description' => $this->description ]; // Check if parameters are set and not empty if ( !empty( $this->parameters ) ) { $properties = []; $required = []; // Loop through each parameter to construct the properties object foreach ( $this->parameters as $parameter ) { $properties[$parameter->name] = [ 'type' => $parameter->type, // Assuming each parameter has a 'type' attribute 'description' => $parameter->description, // Assuming each parameter has a 'description' attribute ]; // If an enum is set for the parameter, include it if ( isset($parameter->enum) ) { $properties[$parameter->name]['enum'] = $parameter->enum; } // If the parameter is required, add its name to the required array if ( $parameter->required ) { $required[] = $parameter->name; } } // Assemble the parameters part of the JSON $json['parameters'] = [ 'type' => 'object', 'properties' => $properties, 'required' => $required, ]; } return $json; } public function serializeForAnthropic() { $json = [ 'name' => $this->name, 'description' => $this->description, 'input_schema' => [ 'type' => 'object', 'properties' => new stdClass() ], ]; if ( !empty( $this->parameters ) ) { $properties = []; $required = []; foreach ( $this->parameters as $parameter ) { $properties[$parameter->name] = [ 'type' => $parameter->type, 'description' => $parameter->description, ]; if ( isset( $parameter->enum ) ) { $properties[$parameter->name]['enum'] = $parameter->enum; } if ( $parameter->required ) { $required[] = $parameter->name; } } $json['input_schema']['properties'] = empty( $properties ) ? new stdClass() : $properties; if ( !empty( $required ) ) { $json['input_schema']['required'] = $required; } } return $json; } }