function-call.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Value object representing a function call request from an AI model |
| 5 | */ |
| 6 | class Meow_MWAI_Data_FunctionCall { |
| 7 | public string $id; |
| 8 | public string $name; |
| 9 | public string $arguments; |
| 10 | |
| 11 | public function __construct( string $id, string $name, string $arguments ) { |
| 12 | $this->id = $id; |
| 13 | $this->name = $name; |
| 14 | $this->arguments = $arguments; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Create from OpenAI tool call format |
| 19 | */ |
| 20 | public static function from_tool_call( array $toolCall ): self { |
| 21 | return new self( |
| 22 | $toolCall['id'], |
| 23 | $toolCall['function']['name'], |
| 24 | $toolCall['function']['arguments'] |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Create from Anthropic tool use format |
| 30 | */ |
| 31 | public static function from_tool_use( array $toolUse ): self { |
| 32 | return new self( |
| 33 | $toolUse['id'], |
| 34 | $toolUse['name'], |
| 35 | json_encode( $toolUse['input'] ) |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get arguments as JSON string |
| 41 | */ |
| 42 | public function get_arguments_json(): string { |
| 43 | return $this->arguments; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get arguments as array |
| 48 | */ |
| 49 | public function get_arguments_array(): array { |
| 50 | return json_decode( $this->arguments, true ) ?? []; |
| 51 | } |
| 52 | } |
| 53 |