完成世界书、骰子、apiconfig页面处理
This commit is contained in:
11
backend/services/__init__.py
Normal file
11
backend/services/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
业务服务层
|
||||
|
||||
包含项目的核心业务逻辑,协调 Models、Utils 和 LLM 组件。
|
||||
"""
|
||||
from .prompt_assembler import PromptAssembler, PromptConfig
|
||||
|
||||
__all__ = [
|
||||
'PromptAssembler',
|
||||
'PromptConfig',
|
||||
]
|
||||
173
backend/services/comfyui_workflow_manager.py
Normal file
173
backend/services/comfyui_workflow_manager.py
Normal file
@@ -0,0 +1,173 @@
|
||||
"""
|
||||
ComfyUI Workflow Manager
|
||||
管理工作流 JSON 文件的上传、删除和加载
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Optional
|
||||
from fastapi import UploadFile, HTTPException
|
||||
import shutil
|
||||
from core.config import settings
|
||||
|
||||
# 工作流目录 - 使用统一的数据目录
|
||||
WORKFLOW_DIR = settings.COMFYUI_WORKFLOWS_PATH
|
||||
WORKFLOW_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
class WorkflowManager:
|
||||
"""ComfyUI 工作流管理器"""
|
||||
|
||||
@staticmethod
|
||||
def list_workflows() -> List[Dict[str, str]]:
|
||||
"""列出所有可用的工作流"""
|
||||
workflows = []
|
||||
|
||||
for json_file in WORKFLOW_DIR.glob("*.json"):
|
||||
try:
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
workflow_data = json.load(f)
|
||||
|
||||
workflows.append({
|
||||
"filename": json_file.name,
|
||||
"name": json_file.stem,
|
||||
"nodes_count": len(workflow_data),
|
||||
"size": json_file.stat().st_size
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"Error loading workflow {json_file.name}: {e}")
|
||||
continue
|
||||
|
||||
return workflows
|
||||
|
||||
@staticmethod
|
||||
def load_workflow(filename: str) -> Dict:
|
||||
"""加载指定工作流"""
|
||||
filepath = WORKFLOW_DIR / filename
|
||||
|
||||
if not filepath.exists():
|
||||
raise HTTPException(status_code=404, detail=f"Workflow '{filename}' not found")
|
||||
|
||||
if not filepath.suffix == '.json':
|
||||
raise HTTPException(status_code=400, detail="Invalid file type")
|
||||
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
raise HTTPException(status_code=500, detail=f"Invalid JSON: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
async def upload_workflow(file: UploadFile) -> Dict[str, str]:
|
||||
"""上传工作流文件"""
|
||||
# 验证文件名
|
||||
if not file.filename or not file.filename.endswith('.json'):
|
||||
raise HTTPException(status_code=400, detail="File must be a JSON file")
|
||||
|
||||
# 安全检查:防止路径遍历攻击
|
||||
safe_filename = os.path.basename(file.filename)
|
||||
if not safe_filename:
|
||||
raise HTTPException(status_code=400, detail="Invalid filename")
|
||||
|
||||
filepath = WORKFLOW_DIR / safe_filename
|
||||
|
||||
# 如果文件已存在,先备份
|
||||
if filepath.exists():
|
||||
backup_path = WORKFLOW_DIR / f"{safe_filename}.bak"
|
||||
shutil.copy2(filepath, backup_path)
|
||||
|
||||
# 保存文件
|
||||
try:
|
||||
content = await file.read()
|
||||
|
||||
# 验证 JSON 格式
|
||||
try:
|
||||
workflow_data = json.loads(content)
|
||||
|
||||
# 基本验证:检查是否是 ComfyUI 工作流
|
||||
if not isinstance(workflow_data, dict):
|
||||
raise ValueError("Workflow must be a JSON object")
|
||||
|
||||
# 检查是否包含必要的节点类型
|
||||
has_sampler = any(
|
||||
node.get("class_type") == "KSampler"
|
||||
for node in workflow_data.values()
|
||||
if isinstance(node, dict)
|
||||
)
|
||||
|
||||
if not has_sampler:
|
||||
raise ValueError("Invalid ComfyUI workflow: missing KSampler node")
|
||||
|
||||
except json.JSONDecodeError:
|
||||
raise HTTPException(status_code=400, detail="Invalid JSON format")
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
# 写入文件
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write(content.decode('utf-8'))
|
||||
|
||||
return {
|
||||
"message": "Workflow uploaded successfully",
|
||||
"filename": safe_filename,
|
||||
"size": len(content)
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
# 如果出错,恢复备份
|
||||
backup_path = WORKFLOW_DIR / f"{safe_filename}.bak"
|
||||
if backup_path.exists():
|
||||
shutil.move(backup_path, filepath)
|
||||
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def delete_workflow(filename: str) -> Dict[str, str]:
|
||||
"""删除工作流文件"""
|
||||
# 安全检查
|
||||
safe_filename = os.path.basename(filename)
|
||||
if not safe_filename or not safe_filename.endswith('.json'):
|
||||
raise HTTPException(status_code=400, detail="Invalid filename")
|
||||
|
||||
filepath = WORKFLOW_DIR / safe_filename
|
||||
|
||||
if not filepath.exists():
|
||||
raise HTTPException(status_code=404, detail=f"Workflow '{filename}' not found")
|
||||
|
||||
# 不允许删除默认工作流
|
||||
if safe_filename == "default_txt2img.json":
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Cannot delete default workflow"
|
||||
)
|
||||
|
||||
try:
|
||||
filepath.unlink()
|
||||
return {"message": f"Workflow '{safe_filename}' deleted successfully"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Delete failed: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def replace_prompt_in_workflow(workflow: Dict, prompt: str) -> Dict:
|
||||
"""
|
||||
在工作流中替换提示词
|
||||
找到第一个 CLIPTextEncode 节点,替换其 text 字段
|
||||
"""
|
||||
import copy
|
||||
workflow_copy = copy.deepcopy(workflow)
|
||||
|
||||
# 查找 CLIPTextEncode 节点(通常是正向提示词)
|
||||
for node_id, node in workflow_copy.items():
|
||||
if isinstance(node, dict) and node.get("class_type") == "CLIPTextEncode":
|
||||
if "text" in node.get("inputs", {}):
|
||||
# 替换提示词
|
||||
node["inputs"]["text"] = prompt
|
||||
return workflow_copy
|
||||
|
||||
# 如果没有找到 CLIPTextEncode 节点,抛出错误
|
||||
raise ValueError("No CLIPTextEncode node found in workflow")
|
||||
|
||||
|
||||
# 全局实例
|
||||
workflow_manager = WorkflowManager()
|
||||
172
backend/services/llm_model_service.py
Normal file
172
backend/services/llm_model_service.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
LLM 模型管理服务
|
||||
|
||||
提供获取不同 LLM 提供商可用模型列表的功能
|
||||
"""
|
||||
from typing import List, Dict, Any, Optional
|
||||
import requests
|
||||
|
||||
|
||||
class LLMModelService:
|
||||
"""LLM 模型管理服务"""
|
||||
|
||||
@staticmethod
|
||||
def get_openai_models(api_key: str, base_url: Optional[str] = None) -> List[str]:
|
||||
"""
|
||||
获取 OpenAI 兼容 API 的模型列表
|
||||
|
||||
Args:
|
||||
api_key: API Key
|
||||
base_url: API 基础 URL,默认为 OpenAI 官方 API
|
||||
|
||||
Returns:
|
||||
模型名称列表
|
||||
"""
|
||||
try:
|
||||
# 默认使用 OpenAI 官方 API
|
||||
if not base_url:
|
||||
base_url = "https://api.openai.com/v1"
|
||||
|
||||
# 确保 base_url 以 /v1 结尾
|
||||
if not base_url.endswith('/v1'):
|
||||
base_url = base_url.rstrip('/') + '/v1'
|
||||
|
||||
response = requests.get(
|
||||
f"{base_url}/models",
|
||||
headers={
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"HTTP {response.status_code}: {response.text}")
|
||||
|
||||
data = response.json()
|
||||
models = [model['id'] for model in data.get('data', [])]
|
||||
|
||||
# 过滤出聊天模型(可选)
|
||||
chat_models = [
|
||||
m for m in models
|
||||
if any(keyword in m.lower() for keyword in ['gpt', 'chat'])
|
||||
]
|
||||
|
||||
# 如果没有找到聊天模型,返回所有模型
|
||||
return chat_models if chat_models else models
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"获取 OpenAI 模型列表失败: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def get_anthropic_models(api_key: str) -> List[str]:
|
||||
"""
|
||||
获取 Anthropic Claude 模型列表
|
||||
|
||||
Args:
|
||||
api_key: API Key
|
||||
|
||||
Returns:
|
||||
模型名称列表
|
||||
"""
|
||||
try:
|
||||
# Anthropic 没有公开的模型列表 API,返回已知模型
|
||||
return [
|
||||
"claude-3-5-sonnet-20241022",
|
||||
"claude-3-5-haiku-20241022",
|
||||
"claude-3-opus-20240229",
|
||||
"claude-3-sonnet-20240229",
|
||||
"claude-3-haiku-20240307",
|
||||
"claude-2.1",
|
||||
"claude-2.0",
|
||||
"claude-instant-1.2"
|
||||
]
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"获取 Anthropic 模型列表失败: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def get_ollama_models(base_url: str = "http://localhost:11434") -> List[str]:
|
||||
"""
|
||||
获取 Ollama 本地模型列表
|
||||
|
||||
Args:
|
||||
base_url: Ollama API 地址
|
||||
|
||||
Returns:
|
||||
模型名称列表
|
||||
"""
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{base_url}/api/tags",
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"HTTP {response.status_code}: {response.text}")
|
||||
|
||||
data = response.json()
|
||||
models = [model['name'] for model in data.get('models', [])]
|
||||
|
||||
return models
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"获取 Ollama 模型列表失败: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def detect_provider(api_url: str) -> str:
|
||||
"""
|
||||
根据 API URL 检测提供商类型
|
||||
|
||||
Args:
|
||||
api_url: API 地址
|
||||
|
||||
Returns:
|
||||
提供商类型: 'openai', 'anthropic', 'ollama', 'unknown'
|
||||
"""
|
||||
api_url_lower = api_url.lower()
|
||||
|
||||
if 'openai' in api_url_lower or 'api.openai.com' in api_url_lower:
|
||||
return 'openai'
|
||||
elif 'anthropic' in api_url_lower or 'api.anthropic.com' in api_url_lower:
|
||||
return 'anthropic'
|
||||
elif 'ollama' in api_url_lower or 'localhost:11434' in api_url_lower or '127.0.0.1:11434' in api_url_lower:
|
||||
return 'ollama'
|
||||
elif 'siliconflow' in api_url_lower or 'silicon.cloud' in api_url_lower:
|
||||
# SiliconFlow 等兼容 OpenAI API 的服务
|
||||
return 'openai'
|
||||
elif 'deepseek' in api_url_lower:
|
||||
# DeepSeek 等兼容 OpenAI API 的服务
|
||||
return 'openai'
|
||||
else:
|
||||
# 默认尝试 OpenAI 兼容 API
|
||||
return 'openai'
|
||||
|
||||
@staticmethod
|
||||
def get_models_by_provider(
|
||||
provider: str,
|
||||
api_key: str,
|
||||
api_url: Optional[str] = None
|
||||
) -> List[str]:
|
||||
"""
|
||||
根据提供商类型获取模型列表
|
||||
|
||||
Args:
|
||||
provider: 提供商类型 ('openai', 'anthropic', 'ollama')
|
||||
api_key: API Key
|
||||
api_url: API 地址(可选)
|
||||
|
||||
Returns:
|
||||
模型名称列表
|
||||
"""
|
||||
if provider == 'openai':
|
||||
return LLMModelService.get_openai_models(api_key, api_url)
|
||||
elif provider == 'anthropic':
|
||||
return LLMModelService.get_anthropic_models(api_key)
|
||||
elif provider == 'ollama':
|
||||
base_url = api_url or "http://localhost:11434"
|
||||
# 移除 /v1 后缀(如果有)
|
||||
base_url = base_url.replace('/v1', '').replace('/v1/', '')
|
||||
return LLMModelService.get_ollama_models(base_url)
|
||||
else:
|
||||
raise Exception(f"不支持的提供商: {provider}")
|
||||
221
backend/services/prompt_assembler.py
Normal file
221
backend/services/prompt_assembler.py
Normal file
@@ -0,0 +1,221 @@
|
||||
"""
|
||||
提示词组装器 (Prompt Assembler)
|
||||
|
||||
负责根据 SillyTavern 规范将角色卡、世界书、聊天历史等组件
|
||||
拼装成最终的 LLM 消息列表。
|
||||
"""
|
||||
import re
|
||||
from typing import List, Dict, Optional
|
||||
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage, BaseMessage
|
||||
|
||||
from models.internal import CharacterCard, ChatMessage, WorldInfoEntry
|
||||
|
||||
|
||||
class PromptConfig:
|
||||
"""提示词组装配置"""
|
||||
def __init__(
|
||||
self,
|
||||
an_position: str = "after_history", # "before_history" or "after_history"
|
||||
an_depth: int = 4,
|
||||
post_history_instructions: Optional[str] = None
|
||||
):
|
||||
self.an_position = an_position
|
||||
self.an_depth = an_depth
|
||||
self.post_history_instructions = post_history_instructions
|
||||
|
||||
|
||||
class PromptAssembler:
|
||||
"""
|
||||
轻量级提示词组装核心
|
||||
|
||||
不依赖复杂的框架,只负责纯粹的文本拼接和位置插入。
|
||||
"""
|
||||
|
||||
# SillyTavern 的位置枚举映射
|
||||
POS_WI_BEFORE = 0
|
||||
POS_WI_AFTER = 1
|
||||
POS_EXAMPLES_BEFORE = 2
|
||||
POS_EXAMPLES_AFTER = 3
|
||||
POS_AN_TOP = 4
|
||||
POS_AN_BOTTOM = 5
|
||||
POS_DEPTH = 6
|
||||
POS_OUTLET = 7
|
||||
|
||||
def assemble(
|
||||
self,
|
||||
character: CharacterCard,
|
||||
chat_history: List[ChatMessage],
|
||||
user_input: str,
|
||||
active_entries: List[WorldInfoEntry],
|
||||
config: PromptConfig = PromptConfig()
|
||||
) -> List[BaseMessage]:
|
||||
"""
|
||||
执行完整的提示词组装流程
|
||||
|
||||
Returns:
|
||||
List[BaseMessage]: 准备好发送给 LLM 的消息列表
|
||||
"""
|
||||
# 1. 按位置分组世界书条目
|
||||
grouped_entries = self._group_entries_by_position(active_entries)
|
||||
|
||||
# 2. 组装 Story String (包含 Pos 0-3)
|
||||
story_string = self._build_story_string(character, grouped_entries)
|
||||
|
||||
# 3. 组装 Author's Note (包含 Pos 4-5)
|
||||
authors_note_content = self._build_authors_note(grouped_entries, config.an_depth)
|
||||
|
||||
# 4. 处理 Chat History 并注入 Depth 条目 (Pos 6)
|
||||
processed_history = self._inject_depth_entries(chat_history, grouped_entries.get(self.POS_DEPTH, []))
|
||||
|
||||
# 5. 准备 Outlet 替换字典 (Pos 7)
|
||||
outlet_map = {entry.uid: entry.content for entry in grouped_entries.get(self.POS_OUTLET, [])}
|
||||
|
||||
# 6. 最终封装为 Messages
|
||||
return self._wrap_to_messages(
|
||||
story_string,
|
||||
authors_note_content,
|
||||
processed_history,
|
||||
user_input,
|
||||
outlet_map,
|
||||
config
|
||||
)
|
||||
|
||||
def _group_entries_by_position(self, entries: List[WorldInfoEntry]) -> Dict[int, List[WorldInfoEntry]]:
|
||||
"""将激活的条目按 position 分组"""
|
||||
grouped = {}
|
||||
for entry in entries:
|
||||
# 这里假设 entry.position 存储的是我们定义的 0-7 整数
|
||||
pos = entry.position if isinstance(entry.position, int) else 1 # 默认为 wiAfter
|
||||
if pos not in grouped:
|
||||
grouped[pos] = []
|
||||
grouped[pos].append(entry)
|
||||
|
||||
# 对每个组内的条目按 order 排序
|
||||
for pos in grouped:
|
||||
grouped[pos].sort(key=lambda x: x.order)
|
||||
return grouped
|
||||
|
||||
def _build_story_string(self, character: CharacterCard, grouped: Dict) -> str:
|
||||
"""组装故事字符串 (Story String)"""
|
||||
parts = []
|
||||
|
||||
# Pos 0: wiBefore
|
||||
for entry in grouped.get(self.POS_WI_BEFORE, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
# 角色核心信息
|
||||
parts.append(f"[Character('{character.name}')]\n{character.description}\n")
|
||||
parts.append(f"Personality: {character.personality}\n")
|
||||
parts.append(f"Scenario: {character.scenario}\n")
|
||||
|
||||
# Pos 1: wiAfter
|
||||
for entry in grouped.get(self.POS_WI_AFTER, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
# Pos 2: Examples Before
|
||||
for entry in grouped.get(self.POS_EXAMPLES_BEFORE, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
# 示例对话
|
||||
if character.mes_example:
|
||||
parts.append(f"<START>\n{character.mes_example}")
|
||||
|
||||
# Pos 3: Examples After
|
||||
for entry in grouped.get(self.POS_EXAMPLES_AFTER, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
def _build_authors_note(self, grouped: Dict, depth: int) -> str:
|
||||
"""组装作者笔记 (Author's Note)"""
|
||||
parts = []
|
||||
|
||||
# Pos 4: AN Top
|
||||
for entry in grouped.get(self.POS_AN_TOP, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
# AN 核心内容 (这里简化为一个占位,实际应从角色卡或设置获取)
|
||||
parts.append(f"[Author's note at depth {depth}]")
|
||||
|
||||
# Pos 5: AN Bottom
|
||||
for entry in grouped.get(self.POS_AN_BOTTOM, []):
|
||||
parts.append(entry.content)
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
def _inject_depth_entries(self, history: List[ChatMessage], depth_entries: List[WorldInfoEntry]) -> List[Dict]:
|
||||
"""
|
||||
在聊天历史的指定深度插入条目 (Pos 6)
|
||||
返回一个包含 role 和 content 的字典列表,方便后续转换
|
||||
"""
|
||||
# 先将历史转换为中间格式
|
||||
msg_list = []
|
||||
for msg in history:
|
||||
msg_list.append({"role": "user" if msg.is_user else "assistant", "content": msg.mes})
|
||||
|
||||
# 按 depth 分组插入
|
||||
# d0 通常指最新用户输入之前,即列表末尾
|
||||
for entry in depth_entries:
|
||||
depth = entry.depth if entry.depth is not None else 0
|
||||
# 计算插入索引 (从后往前数)
|
||||
insert_index = max(0, len(msg_list) - depth)
|
||||
|
||||
# 确定角色
|
||||
role_map = {"system": "system", "user": "user", "assistant": "assistant"}
|
||||
role = role_map.get(str(entry.position).split('_')[-1] if '_' in str(entry.position) else "system", "system")
|
||||
|
||||
msg_list.insert(insert_index, {"role": "system", "content": entry.content})
|
||||
|
||||
return msg_list
|
||||
|
||||
def _replace_outlets(self, text: str, outlet_map: Dict[str, str]) -> str:
|
||||
"""执行 Outlet 宏替换 (Pos 7)"""
|
||||
def replacer(match):
|
||||
uid = match.group(1)
|
||||
return outlet_map.get(uid, "")
|
||||
|
||||
# 匹配 {{outlet::UID}}
|
||||
return re.sub(r"\{\{outlet::([^}]+)\}\}", replacer, text)
|
||||
|
||||
def _wrap_to_messages(
|
||||
self,
|
||||
story_string: str,
|
||||
an_content: str,
|
||||
history: List[Dict],
|
||||
user_input: str,
|
||||
outlet_map: Dict[str, str],
|
||||
config: PromptConfig
|
||||
) -> List[BaseMessage]:
|
||||
"""将组装好的文本块封装为 LangChain Messages"""
|
||||
messages = []
|
||||
|
||||
# 1. System Message (Story String + Outlet 替换)
|
||||
final_story = self._replace_outlets(story_string, outlet_map)
|
||||
if final_story:
|
||||
messages.append(SystemMessage(content=final_story))
|
||||
|
||||
# 2. Author's Note (根据配置位置插入)
|
||||
if an_content and config.an_position == "before_history":
|
||||
messages.append(SystemMessage(content=self._replace_outlets(an_content, outlet_map)))
|
||||
|
||||
# 3. Chat History
|
||||
for msg_data in history:
|
||||
if msg_data["role"] == "user":
|
||||
messages.append(HumanMessage(content=msg_data["content"]))
|
||||
elif msg_data["role"] == "assistant":
|
||||
messages.append(AIMessage(content=msg_data["content"]))
|
||||
else:
|
||||
messages.append(SystemMessage(content=msg_data["content"]))
|
||||
|
||||
# 4. Author's Note (如果在 History 之后)
|
||||
if an_content and config.an_position == "after_history":
|
||||
messages.append(SystemMessage(content=self._replace_outlets(an_content, outlet_map)))
|
||||
|
||||
# 5. Post-History Instructions & User Input
|
||||
final_input = user_input
|
||||
if config.post_history_instructions:
|
||||
final_input = f"{config.post_history_instructions}\n\n{user_input}"
|
||||
|
||||
messages.append(HumanMessage(content=final_input))
|
||||
|
||||
return messages
|
||||
381
backend/services/worldbook_service.py
Normal file
381
backend/services/worldbook_service.py
Normal file
@@ -0,0 +1,381 @@
|
||||
"""
|
||||
World Book Service
|
||||
世界书服务层 - 处理世界书及条目的 CRUD 操作
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from models.internal import WorldInfo, WorldInfoEntry, ActivationType
|
||||
from models.converters import WorldBookConverter
|
||||
from core.config import settings
|
||||
|
||||
|
||||
class WorldBookService:
|
||||
"""世界书服务类"""
|
||||
|
||||
@staticmethod
|
||||
def _get_worldbook_path(name: str) -> Path:
|
||||
"""获取世界书文件路径"""
|
||||
return settings.WORLDBOOKS_PATH / f"{name}.json"
|
||||
|
||||
@staticmethod
|
||||
def _load_worldbook(name: str) -> Optional[Dict[str, Any]]:
|
||||
"""加载世界书 JSON 文件"""
|
||||
path = WorldBookService._get_worldbook_path(name)
|
||||
if not path.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to load worldbook '{name}': {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def _save_worldbook(name: str, data: Dict[str, Any]):
|
||||
"""保存世界书到 JSON 文件"""
|
||||
path = WorldBookService._get_worldbook_path(name)
|
||||
try:
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to save worldbook '{name}': {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def list_worldbooks() -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取所有世界书的列表(仅基本信息)
|
||||
|
||||
Returns:
|
||||
世界书列表,每个包含 name, description, entries_count 等
|
||||
"""
|
||||
worldbooks = []
|
||||
|
||||
for json_file in settings.WORLDBOOKS_PATH.glob("*.json"):
|
||||
try:
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
worldbooks.append({
|
||||
"name": data.get("name", json_file.stem),
|
||||
"description": data.get("description", ""),
|
||||
"entries_count": len(data.get("entries", [])),
|
||||
"createdAt": data.get("createdAt", 0),
|
||||
"updatedAt": data.get("updatedAt", 0)
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"Error loading worldbook {json_file.name}: {e}")
|
||||
continue
|
||||
|
||||
# 按更新时间排序
|
||||
worldbooks.sort(key=lambda x: x.get("updatedAt", 0), reverse=True)
|
||||
return worldbooks
|
||||
|
||||
@staticmethod
|
||||
def get_worldbook(name: str) -> Dict[str, Any]:
|
||||
"""
|
||||
获取指定世界书的完整数据
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
|
||||
Returns:
|
||||
世界书完整数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def create_worldbook(name: str, description: str = "") -> Dict[str, Any]:
|
||||
"""
|
||||
创建新世界书
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
description: 世界书描述
|
||||
|
||||
Returns:
|
||||
创建的世界书数据
|
||||
"""
|
||||
# 检查是否已存在
|
||||
if WorldBookService._get_worldbook_path(name).exists():
|
||||
raise ValueError(f"Worldbook '{name}' already exists")
|
||||
|
||||
now = int(datetime.now().timestamp())
|
||||
worldbook_data = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"name": name,
|
||||
"description": description,
|
||||
"entries": [],
|
||||
"createdAt": now,
|
||||
"updatedAt": now,
|
||||
"version": 1
|
||||
}
|
||||
|
||||
WorldBookService._save_worldbook(name, worldbook_data)
|
||||
return worldbook_data
|
||||
|
||||
@staticmethod
|
||||
def update_worldbook(name: str, description: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
更新世界书基本信息
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
description: 新的描述(可选)
|
||||
|
||||
Returns:
|
||||
更新后的世界书数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
if description is not None:
|
||||
data["description"] = description
|
||||
|
||||
data["updatedAt"] = int(datetime.now().timestamp())
|
||||
WorldBookService._save_worldbook(name, data)
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def delete_worldbook(name: str) -> bool:
|
||||
"""
|
||||
删除世界书
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
|
||||
Returns:
|
||||
是否删除成功
|
||||
"""
|
||||
path = WorldBookService._get_worldbook_path(name)
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
path.unlink()
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def list_entries(name: str) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
获取世界书的所有条目
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
|
||||
Returns:
|
||||
条目列表
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
return data.get("entries", [])
|
||||
|
||||
@staticmethod
|
||||
def get_entry(name: str, uid: str) -> Dict[str, Any]:
|
||||
"""
|
||||
获取世界书的指定条目
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
uid: 条目 UID
|
||||
|
||||
Returns:
|
||||
条目数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
for entry in data.get("entries", []):
|
||||
if entry.get("uid") == uid:
|
||||
return entry
|
||||
|
||||
raise FileNotFoundError(f"Entry '{uid}' not found in worldbook '{name}'")
|
||||
|
||||
@staticmethod
|
||||
def create_entry(name: str, entry_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
在世界书中创建新条目
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
entry_data: 条目数据(不包含 uid, createdAt, updatedAt)
|
||||
|
||||
Returns:
|
||||
创建的条目数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
# 生成 UID 和时间戳
|
||||
now = int(datetime.now().timestamp())
|
||||
new_entry = {
|
||||
"uid": str(uuid.uuid4()),
|
||||
"key": entry_data.get("key", []),
|
||||
"keysecondary": entry_data.get("keysecondary", []),
|
||||
"content": entry_data.get("content", ""),
|
||||
"activationType": entry_data.get("activationType", ActivationType.KEYWORD.value),
|
||||
"logicExpression": entry_data.get("logicExpression"),
|
||||
"ragConfig": entry_data.get("ragConfig"),
|
||||
"order": entry_data.get("order", 0),
|
||||
"position": entry_data.get("position", "after_char"),
|
||||
"depth": entry_data.get("depth"),
|
||||
"probability": entry_data.get("probability", 100),
|
||||
"group": entry_data.get("group", []),
|
||||
"disable": entry_data.get("disable", False),
|
||||
"createdAt": now,
|
||||
"updatedAt": now
|
||||
}
|
||||
|
||||
data["entries"].append(new_entry)
|
||||
data["updatedAt"] = now
|
||||
WorldBookService._save_worldbook(name, data)
|
||||
|
||||
return new_entry
|
||||
|
||||
@staticmethod
|
||||
def update_entry(name: str, uid: str, entry_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
更新世界书的指定条目
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
uid: 条目 UID
|
||||
entry_data: 更新的字段
|
||||
|
||||
Returns:
|
||||
更新后的条目数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
for i, entry in enumerate(data.get("entries", [])):
|
||||
if entry.get("uid") == uid:
|
||||
# 更新字段
|
||||
for key, value in entry_data.items():
|
||||
if key not in ["uid", "createdAt"]: # 不修改 UID 和创建时间
|
||||
entry[key] = value
|
||||
|
||||
# 更新时间戳
|
||||
entry["updatedAt"] = int(datetime.now().timestamp())
|
||||
data["entries"][i] = entry
|
||||
data["updatedAt"] = entry["updatedAt"]
|
||||
|
||||
WorldBookService._save_worldbook(name, data)
|
||||
return entry
|
||||
|
||||
raise FileNotFoundError(f"Entry '{uid}' not found in worldbook '{name}'")
|
||||
|
||||
@staticmethod
|
||||
def delete_entry(name: str, uid: str) -> bool:
|
||||
"""
|
||||
删除世界书的指定条目
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
uid: 条目 UID
|
||||
|
||||
Returns:
|
||||
是否删除成功
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
original_length = len(data.get("entries", []))
|
||||
data["entries"] = [e for e in data.get("entries", []) if e.get("uid") != uid]
|
||||
|
||||
if len(data["entries"]) == original_length:
|
||||
raise FileNotFoundError(f"Entry '{uid}' not found in worldbook '{name}'")
|
||||
|
||||
data["updatedAt"] = int(datetime.now().timestamp())
|
||||
WorldBookService._save_worldbook(name, data)
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def import_from_sillytavern(name: str, st_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
从 SillyTavern 格式导入世界书
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
st_data: SillyTavern 格式的世界书数据
|
||||
|
||||
Returns:
|
||||
转换后的内部格式世界书数据
|
||||
"""
|
||||
# 使用转换器进行转换
|
||||
worldbook_data = WorldBookConverter.st_to_internal(st_data, name)
|
||||
|
||||
# 保存到文件
|
||||
WorldBookService._save_worldbook(name, worldbook_data)
|
||||
|
||||
return worldbook_data
|
||||
|
||||
@staticmethod
|
||||
def import_internal_format(name: str, internal_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
直接导入内部格式的世界书(无需转换)
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
internal_data: 内部格式的世界书数据
|
||||
|
||||
Returns:
|
||||
内部格式世界书数据
|
||||
"""
|
||||
# 确保包含必要的字段
|
||||
if "name" not in internal_data:
|
||||
internal_data["name"] = name
|
||||
|
||||
# 规范化所有条目,确保有 trigger_config
|
||||
if "entries" in internal_data and isinstance(internal_data["entries"], list):
|
||||
normalized_entries = []
|
||||
for entry in internal_data["entries"]:
|
||||
if isinstance(entry, dict):
|
||||
normalized_entry = WorldBookConverter.normalize_entry(entry)
|
||||
normalized_entries.append(normalized_entry)
|
||||
internal_data["entries"] = normalized_entries
|
||||
|
||||
# 保存文件
|
||||
WorldBookService._save_worldbook(name, internal_data)
|
||||
|
||||
return internal_data
|
||||
|
||||
@staticmethod
|
||||
def export_to_sillytavern(name: str) -> Dict[str, Any]:
|
||||
"""
|
||||
导出为 SillyTavern 格式
|
||||
|
||||
Args:
|
||||
name: 世界书名称
|
||||
|
||||
Returns:
|
||||
SillyTavern 格式的世界书数据
|
||||
"""
|
||||
data = WorldBookService._load_worldbook(name)
|
||||
if not data:
|
||||
raise FileNotFoundError(f"Worldbook '{name}' not found")
|
||||
|
||||
# 使用转换器进行转换
|
||||
st_data = WorldBookConverter.internal_to_st(data)
|
||||
|
||||
return st_data
|
||||
|
||||
|
||||
# 全局实例
|
||||
worldbook_service = WorldBookService()
|
||||
Reference in New Issue
Block a user