From aa8becce81b3371d067a8d60e54839dfc5a93abf Mon Sep 17 00:00:00 2001 From: Hylke Date: Mon, 27 Oct 2025 13:14:38 +0100 Subject: [PATCH 1/2] Able to use settings as context settings --- core/components/modai/src/API/Prompt/Text.php | 8 ++++-- core/components/modai/src/Settings.php | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/components/modai/src/API/Prompt/Text.php b/core/components/modai/src/API/Prompt/Text.php index 268ceb8..05801d0 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 = ''; + 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)) { @@ -74,9 +76,9 @@ public function post(ServerRequestInterface $request): void $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); + $output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false, $contextKey); $base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false); - $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace); + $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace, false); $customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false); if (!empty($output)) { diff --git a/core/components/modai/src/Settings.php b/core/components/modai/src/Settings.php index 650bbc6..4d94291 100644 --- a/core/components/modai/src/Settings.php +++ b/core/components/modai/src/Settings.php @@ -25,28 +25,38 @@ 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 = 'web'): ?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 +66,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 +83,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 = ''): ?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"); From cfa97c70e3a876e4c316b29b53d8d0001b3c6253 Mon Sep 17 00:00:00 2001 From: Hylke Date: Mon, 27 Oct 2025 14:02:23 +0100 Subject: [PATCH 2/2] Send contextkey with the function --- core/components/modai/src/API/Prompt/Text.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/components/modai/src/API/Prompt/Text.php b/core/components/modai/src/API/Prompt/Text.php index 05801d0..d76ac94 100644 --- a/core/components/modai/src/API/Prompt/Text.php +++ b/core/components/modai/src/API/Prompt/Text.php @@ -73,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); + $model = Settings::getTextSetting($this->modx, $field, 'model', $namespace, 'openai/gpt-4o-mini', $contextKey); + $temperature = (float)Settings::getTextSetting($this->modx, $field, 'temperature', $namespace , 0, $contextKey); + $maxTokens = (int)Settings::getTextSetting($this->modx, $field, 'max_tokens', $namespace, 0, $contextKey); $output = Settings::getTextSetting($this->modx, $field, 'base_output', $namespace, false, $contextKey); - $base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false); - $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace, false); - $customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false); + $base = Settings::getTextSetting($this->modx, $field, 'base_prompt', $namespace, false, $contextKey); + $fieldPrompt = Settings::getTextSetting($this->modx, $field, 'prompt', $namespace, false, $contextKey); + $customOptions = Settings::getTextSetting($this->modx, $field, 'custom_options', $namespace, false, $contextKey); if (!empty($output)) { $systemInstructions[] = $output;