Files
justice/www/html/Admin/api/ai-settings.php
T
2026-07-31 03:36:58 +00:00

122 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
/**
* ตั้งค่า AI แชท — อ่าน/เขียน Game/data/ai-settings.json
* ใช้ session Admin หลัก (ไม่ต้องรหัส game-ai-admin แยกเมื่อเปิดจากแผง Admin)
*
* GET -> { model, models[], hasKey, intent, rag_enabled, rag_context } (ไม่ส่ง api key ดิบ)
* PUT -> { openai_api_key?, model?, intent?, rag_enabled?, rag_context? }
*/
require __DIR__ . '/_common.php';
require_login();
require_tab('ai-admin');
define('AI_SETTINGS_FILE', dirname(__DIR__, 2) . '/Game/data/ai-settings.json');
function ai_models(): array
{
return [
'gpt-5.2', 'gpt-5.2-pro', 'gpt-5.1', 'gpt-5', 'gpt-5-pro', 'gpt-5-mini', 'gpt-5-nano',
'gpt-4.1', 'gpt-4.1-mini', 'gpt-4.1-nano',
'o3', 'o3-mini', 'o3-pro', 'o4-mini', 'o3-deep-research', 'o4-mini-deep-research',
'o1', 'o1-mini', 'o1-pro',
'gpt-4o', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4',
'gpt-3.5-turbo',
];
}
function ai_defaults(): array
{
return [
'openai_api_key' => '',
'model' => 'gpt-4o-mini',
'intent' => '',
'rag_enabled' => false,
'rag_context' => '',
];
}
function ai_read(): array
{
$base = ai_defaults();
if (!is_file(AI_SETTINGS_FILE)) {
return $base;
}
$raw = @file_get_contents(AI_SETTINGS_FILE);
$j = json_decode($raw ?: '{}', true);
if (!is_array($j)) {
return $base;
}
return array_replace($base, array_intersect_key($j, $base));
}
function ai_write(array $s): bool
{
$dir = dirname(AI_SETTINGS_FILE);
if (!is_dir($dir) && !@mkdir($dir, 0755, true)) {
return false;
}
$json = json_encode($s, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
if ($json === false) {
return false;
}
$tmp = AI_SETTINGS_FILE . '.tmp.' . bin2hex(random_bytes(4));
if (file_put_contents($tmp, $json, LOCK_EX) === false) {
return false;
}
if (!rename($tmp, AI_SETTINGS_FILE)) {
@unlink($tmp);
return false;
}
return true;
}
function ai_public_payload(array $s): array
{
$key = trim((string) ($s['openai_api_key'] ?? ''));
return [
'ok' => true,
'model' => (string) ($s['model'] ?? 'gpt-4o-mini') ?: 'gpt-4o-mini',
'models' => ai_models(),
'hasKey' => $key !== '',
'intent' => (string) ($s['intent'] ?? ''),
'rag_enabled' => !empty($s['rag_enabled']),
'rag_context' => (string) ($s['rag_context'] ?? ''),
];
}
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
if ($method === 'GET') {
json_response(ai_public_payload(ai_read()));
}
if ($method === 'PUT' || $method === 'POST') {
$body = require_json_body();
$s = ai_read();
if (array_key_exists('openai_api_key', $body)) {
$s['openai_api_key'] = trim((string) $body['openai_api_key']);
}
if (array_key_exists('model', $body)) {
$m = trim((string) $body['model']);
$s['model'] = $m !== '' ? $m : 'gpt-4o-mini';
}
if (array_key_exists('intent', $body)) {
$s['intent'] = (string) $body['intent'];
}
if (array_key_exists('rag_enabled', $body)) {
$s['rag_enabled'] = !empty($body['rag_enabled']);
}
if (array_key_exists('rag_context', $body)) {
$s['rag_context'] = (string) $body['rag_context'];
}
if (!ai_write($s)) {
json_response(['ok' => false, 'error' => 'บันทึก ai-settings.json ไม่สำเร็จ'], 500);
}
json_response(['ok' => true]);
}
json_response(['ok' => false, 'error' => 'Method not allowed'], 405);