2025-02-15 12:30:20 +08:00
|
|
|
import logging
|
|
|
|
|
from aiohttp import web
|
2025-02-15 19:38:32 +08:00
|
|
|
from config.settings import update_config
|
|
|
|
|
from ruamel.yaml.scalarstring import PreservedScalarString
|
2025-02-15 12:30:20 +08:00
|
|
|
from manager.api.auth import verify_token
|
2025-02-15 16:17:08 +08:00
|
|
|
from manager.api.response import response_unauthorized, response_success, response_error
|
2025-02-15 12:30:20 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 16:17:08 +08:00
|
|
|
class PromptApi:
|
2025-02-15 12:30:20 +08:00
|
|
|
def __init__(self, config):
|
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
|
|
async def get_prompt(self, request):
|
|
|
|
|
if not await verify_token(self.config, request):
|
2025-02-15 16:17:08 +08:00
|
|
|
return response_unauthorized()
|
2025-02-15 12:30:20 +08:00
|
|
|
|
|
|
|
|
return web.json_response({
|
|
|
|
|
'prompt': self.config['prompt'],
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async def update_prompt(self, request):
|
|
|
|
|
if not await verify_token(self.config, request):
|
2025-02-15 16:17:08 +08:00
|
|
|
return response_unauthorized()
|
2025-02-15 12:30:20 +08:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
data = await request.json()
|
|
|
|
|
if 'prompt' not in data:
|
2025-02-15 16:17:08 +08:00
|
|
|
return response_success()
|
2025-02-15 12:30:20 +08:00
|
|
|
|
2025-02-15 19:38:32 +08:00
|
|
|
# 使用PreservedScalarString保留多行文本格式
|
|
|
|
|
self.config['prompt'] = PreservedScalarString(data['prompt'])
|
2025-02-15 12:30:20 +08:00
|
|
|
|
2025-02-15 19:38:32 +08:00
|
|
|
# 保存到配置文件
|
|
|
|
|
update_config(self.config)
|
|
|
|
|
|
|
|
|
|
return response_success()
|
2025-02-15 12:30:20 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to update prompt: {e}")
|
2025-02-15 16:17:08 +08:00
|
|
|
return response_error(str(e))
|