harnessed_agent/wwwroot/api/agent_config_save.dspy

92 lines
4.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Save agent configuration for current user"""
import json, uuid, time
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}}
try:
dbname = get_module_dbname('harnessed_agent')
user_id = await get_user()
now = time.strftime('%Y-%m-%d %H:%M:%S')
work_dir = params_kw.get('work_dir', './hermes_work').strip()
skills_path = params_kw.get('skills_path', '~/.hermes/skills').strip()
max_memory_tokens = int(params_kw.get('max_memory_tokens', 2000))
default_priority = int(params_kw.get('default_priority', 50))
high_priority_threshold = int(params_kw.get('high_priority_threshold', 70))
low_priority_threshold = int(params_kw.get('low_priority_threshold', 30))
auto_cleanup_enabled = '1' if params_kw.get('auto_cleanup_enabled') == '1' else '0'
min_retention_days = int(params_kw.get('min_retention_days', 30))
default_model = params_kw.get('default_model', 'qwen3-max').strip()
default_temperature = float(params_kw.get('default_temperature', 0.7))
enable_streaming = '1' if params_kw.get('enable_streaming') == '1' else '0'
async with DBPools().sqlorContext(dbname) as sor:
rows = await sor.sqlExe("SELECT id FROM harnessed_agent_config WHERE user_id = ${user_id}$", {'user_id': user_id})
if rows and len(rows) > 0:
config_id = rows[0]['id']
await sor.sqlExe("""UPDATE harnessed_agent_config SET
work_dir = ${work_dir}$,
skills_path = ${skills_path}$,
max_memory_tokens = ${max_memory_tokens}$,
default_priority = ${default_priority}$,
high_priority_threshold = ${high_priority_threshold}$,
low_priority_threshold = ${low_priority_threshold}$,
auto_cleanup_enabled = ${auto_cleanup_enabled}$,
min_retention_days = ${min_retention_days}$,
default_model = ${default_model}$,
default_temperature = ${default_temperature}$,
enable_streaming = ${enable_streaming}$,
updated_at = ${updated_at}$
WHERE id = ${id}$""", {
'id': config_id,
'work_dir': work_dir,
'skills_path': skills_path,
'max_memory_tokens': max_memory_tokens,
'default_priority': default_priority,
'high_priority_threshold': high_priority_threshold,
'low_priority_threshold': low_priority_threshold,
'auto_cleanup_enabled': auto_cleanup_enabled,
'min_retention_days': min_retention_days,
'default_model': default_model,
'default_temperature': default_temperature,
'enable_streaming': enable_streaming,
'updated_at': now
})
else:
config_id = str(uuid.uuid4()).replace('-', '')[:32]
await sor.sqlExe("""INSERT INTO harnessed_agent_config
(id, user_id, work_dir, skills_path, max_memory_tokens,
default_priority, high_priority_threshold, low_priority_threshold,
auto_cleanup_enabled, min_retention_days, default_model,
default_temperature, enable_streaming, created_at, updated_at)
VALUES (${id}$, ${user_id}$, ${work_dir}$, ${skills_path}$, ${max_memory_tokens}$,
${default_priority}$, ${high_priority_threshold}$, ${low_priority_threshold}$,
${auto_cleanup_enabled}$, ${min_retention_days}$, ${default_model}$,
${default_temperature}$, ${enable_streaming}$, ${created_at}$, ${updated_at}$)""", {
'id': config_id,
'user_id': user_id,
'work_dir': work_dir,
'skills_path': skills_path,
'max_memory_tokens': max_memory_tokens,
'default_priority': default_priority,
'high_priority_threshold': high_priority_threshold,
'low_priority_threshold': low_priority_threshold,
'auto_cleanup_enabled': auto_cleanup_enabled,
'min_retention_days': min_retention_days,
'default_model': default_model,
'default_temperature': default_temperature,
'enable_streaming': enable_streaming,
'created_at': now,
'updated_at': now
})
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '配置保存成功', 'type': 'success'}}
except Exception as e:
result['options'] = {'title': 'Error', 'message': '保存失败: ' + str(e), 'type': 'error'}
return json.dumps(result, ensure_ascii=False)