diff --git a/core/components/modai/src/API/Prompt/Text.php b/core/components/modai/src/API/Prompt/Text.php index 268ceb8..602dec9 100644 --- a/core/components/modai/src/API/Prompt/Text.php +++ b/core/components/modai/src/API/Prompt/Text.php @@ -18,6 +18,8 @@ class Text extends API public function post(ServerRequestInterface $request): void { + $contextKey = null; + if (!$this->modx->hasPermission('modai_client_text')) { throw APIException::unauthorized(); } @@ -60,7 +62,7 @@ public function post(ServerRequestInterface $request): void if (!$resource) { throw new LexiconException('modai.error.no_resource_found'); } - + $contextKey = $resource->get('context_key'); $content = $resource->getContent(); if (empty($content)) { @@ -71,13 +73,13 @@ public function post(ServerRequestInterface $request): void $systemInstructions = []; $stream = intval(Settings::getTextSetting($this->modx, $field, 'stream', $namespace)) === 1; - $model = Settings::getTextSetting($this->modx, $field, 'model', $namespace); - $temperature = (float)Settings::getTextSetting($this->modx, $field, 'temperature', $namespace); - $maxTokens = (int)Settings::getTextSetting($this->modx, $field, 'max_tokens', $namespace); - $output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false); - $base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false); - $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace); - $customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false); + $model = Settings::getTextSetting($this->modx, $field, 'model', $namespace, true, $contextKey); + $temperature = (float)Settings::getTextSetting($this->modx, $field, 'temperature', $namespace , true, $contextKey); + $maxTokens = (int)Settings::getTextSetting($this->modx, $field, 'max_tokens', $namespace, true, $contextKey); + $output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false, $contextKey); + $base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false, $contextKey); + $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace, true, $contextKey); + $customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false, $contextKey); if (!empty($output)) { $systemInstructions[] = $output; diff --git a/core/components/modai/src/API/Prompt/Vision.php b/core/components/modai/src/API/Prompt/Vision.php index fc3b054..b7b4119 100644 --- a/core/components/modai/src/API/Prompt/Vision.php +++ b/core/components/modai/src/API/Prompt/Vision.php @@ -10,35 +10,46 @@ use modAI\Settings; use modAI\Utils; use Psr\Http\Message\ServerRequestInterface; +use MODX\Revolution\modResource; class Vision extends API { public function post(ServerRequestInterface $request): void { + $contextKey = null; if (!$this->modx->hasPermission('modai_client_vision')) { throw APIException::unauthorized(); } set_time_limit(0); - $data = $request->getParsedBody(); $field = Utils::getOption('field', $data); $namespace = Utils::getOption('namespace', $data, 'modai'); $image = Utils::getOption('image', $data); $prompt = Utils::getOption('prompt', $data); + $resourceId = Utils::getOption('resourceId', $data); if (empty($image)) { throw new LexiconException('modai.error.image_requried'); } - $stream = intval(Settings::getVisionSetting($this->modx, $field, 'stream', $namespace)) === 1; - $model = Settings::getVisionSetting($this->modx, $field, 'model', $namespace); + if (!empty($resourceId)) { + /** @var modResource $resource */ + $resource = $this->modx->getObject('modResource', $resourceId); + if (!$resource) { + throw new LexiconException('modai.error.no_resource_found'); + } + $contextKey = $resource->get('context_key'); + } + + $stream = intval(Settings::getVisionSetting($this->modx, $field, 'stream', $namespace, true, $contextKey)) === 1; + $model = Settings::getVisionSetting($this->modx, $field, 'model', $namespace, true, $contextKey); if (empty($prompt)) { - $prompt = Settings::getVisionSetting($this->modx, $field, 'prompt', $namespace); + $prompt = Settings::getVisionSetting($this->modx, $field, 'prompt', $namespace, true, $contextKey); } - $customOptions = Settings::getVisionSetting($this->modx, $field, 'custom_options', $namespace, false); - $maxTokens = (int)Settings::getVisionSetting($this->modx, $field, 'max_tokens', $namespace); + $customOptions = Settings::getVisionSetting($this->modx, $field, 'custom_options', $namespace, false, $contextKey); + $maxTokens = (int)Settings::getVisionSetting($this->modx, $field, 'max_tokens', $namespace, true, $contextKey); $aiService = AIServiceFactory::new($model, $this->modx); $result = $aiService->getVision( diff --git a/core/components/modai/src/Settings.php b/core/components/modai/src/Settings.php index 650bbc6..977be9c 100644 --- a/core/components/modai/src/Settings.php +++ b/core/components/modai/src/Settings.php @@ -25,28 +25,37 @@ class Settings * @param string $setting * @return string|null */ - private static function getOption(modX $modx, string $namespace, string $field, string $area, string $setting): ?string + private static function getOption(modX $modx, string $namespace, string $field, string $area, string $setting, ?string $contextKey = null): ?string { + $handler = $modx; + + if (!empty($contextKey)) { + $context = $modx->getContext($contextKey); + if ($context) { + $handler = $context; + } + } + if (!empty($field)) { - $value = $modx->getOption("#sys.$field.$area.$setting"); + $value = $handler->getOption("#sys.$field.$area.$setting"); if ($value !== null && $value !== '') { return $value; } } - $value = $modx->getOption("#sys.global.$area.$setting"); + $value = $handler->getOption("#sys.global.$area.$setting"); if ($value !== null && $value !== '') { return $value; } if (!empty($field)) { - $value = $modx->getOption("$namespace.$field.$area.$setting"); + $value = $handler->getOption("$namespace.$field.$area.$setting"); if ($value !== null && $value !== '') { return $value; } } - $value = $modx->getOption("$namespace.global.$area.$setting"); + $value = $handler->getOption("$namespace.global.$area.$setting"); if ($value !== null && $value !== '') { return $value; } @@ -56,13 +65,13 @@ private static function getOption(modX $modx, string $namespace, string $field, } if (!empty($field)) { - $value = $modx->getOption("modai.$field.$area.$setting"); + $value = $handler->getOption("modai.$field.$area.$setting"); if ($value !== null && $value !== '') { return $value; } } - $value = $modx->getOption("modai.global.$area.$setting"); + $value = $handler->getOption("modai.global.$area.$setting"); if ($value !== null && $value !== '') { return $value; } @@ -73,9 +82,9 @@ private static function getOption(modX $modx, string $namespace, string $field, /** * @throws RequiredSettingException */ - public static function getTextSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true): ?string + public static function getTextSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true, ?string $contextKey = null): ?string { - $value = self::getOption($modx, $namespace, $field, 'text', $setting); + $value = self::getOption($modx, $namespace, $field, 'text', $setting, $contextKey); if ($required && ($value === null || $value === '')) { throw new RequiredSettingException("modai.global.text.$setting"); @@ -101,9 +110,9 @@ public static function getImageSetting(modX $modx, string $field, string $settin /** * @throws RequiredSettingException */ - public static function getVisionSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true): ?string + public static function getVisionSetting(modX $modx, string $field, string $setting, string $namespace = 'modai', bool $required = true, ?string $contextKey = null): ?string { - $value = self::getOption($modx, $namespace, $field, 'vision', $setting); + $value = self::getOption($modx, $namespace, $field, 'vision', $setting, $contextKey); if ($required && ($value === null || $value === '')) { throw new RequiredSettingException("modai.global.vision.$setting");