<?php
namespace App\AI\Contexts\Utils; class CurrencyExchange extends \App\AI\Contexts\ContextAbstract { protected function checkPermission_currency_exchange() : bool { return true; } protected function checkPermission_all_exchange_rates() : bool { return true; } protected function generateFunctionObject_currency_exchange(\App\AI\Api\ChatGpt\Responses\ToolFunction $funcObj) : \App\AI\Api\ChatGpt\Responses\ToolFunction { $amountObj = new \App\AI\Api\ChatGpt\Responses\Property(); $amountObj->type = "number"; $amountObj->description = "The amount of money to be exchanged."; $funcObj->parameters->addProperty("initialAmount", $amountObj); $fromCurrencyObj = new \App\AI\Api\ChatGpt\Responses\Property(); $fromCurrencyObj->type = "string"; $fromCurrencyObj->description = "The currency code to convert from (e.g., USD)."; $funcObj->parameters->addProperty("initialCurrency", $fromCurrencyObj); $toCurrencyObj = new \App\AI\Api\ChatGpt\Responses\Property(); $toCurrencyObj->type = "string"; $toCurrencyObj->description = "The currency code to convert to (e.g., EUR)."; $funcObj->parameters->addProperty("finalCurrency", $toCurrencyObj); return $funcObj; } protected function generateFunctionObject_all_exchange_rates(\App\AI\Api\ChatGpt\Responses\ToolFunction $funcObj) : \App\AI\Api\ChatGpt\Responses\ToolFunction { $paramsObj = new \App\AI\Api\ChatGpt\Responses\Parameters(); $funcObj->setParameters($paramsObj); $refCurrObj = new \App\AI\Api\ChatGpt\Responses\Property(); $refCurrObj->type = "string"; $refCurrObj->description = "Exchange rate date"; $paramsObj->addProperty("date", $refCurrObj); return $funcObj; } protected function executeFunction_currency_exchange(string $arguments) : array { $args = json_decode($arguments, 1); $response = [ 'initialAmount' => $args['initialAmount'], 'initialCurrency' => $args['initialCurrency'], 'finalCurrency' => $args['finalCurrency'], 'finalAmount' => \App\CurrencyExchange\Rates::getInstance()->convert($args['initialAmount'], $args['initialCurrency'], $args['finalCurrency']) ]; return $response; } protected function executeFunction_all_exchange_rates(string $arguments) : array { $args = json_decode($arguments, 1); global $DBTRd_currencyRates, $DBTRd_currencyReferences; $db = \DB::getInstance(); $rates = []; if ($args['date']) { $rates['date'] = $args['date']; $sql = "SELECT currency
                    FROM {$DBTRd_currencyReferences}
                    WHERE date = ?"; $rates['referenceCurrency'] = $db->GetOne($sql, [$args['date']]); $sql = "SELECT type, currency, value
                    FROM {$DBTRd_currencyRates}
                    WHERE date = ?"; $rates['rates'] = $db->GetAll($sql, [$args['date']]); } return $rates; } } 