Refactor assistant configuration and database seeding
- Update Makefile to include new database seed commands for assistants and credentials. - Refactor assistant model to use explicit fields instead of a config dictionary, improving data integrity and clarity. - Implement new seeding SQL script for assistants, ensuring dependencies on credentials are respected. - Modify backend routes and frontend components to accommodate the new assistant structure, including direct field access for prompt, API URL, and keys. - Enhance the AssistantPage component to handle the new data structure and streamline the save process for different assistant types.
This commit is contained in:
@@ -31,43 +31,17 @@ class CamelModel(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
# ---------- 各类型的 config 形态(JSON 内嵌,按 type 校验) ----------
|
||||
class PromptConfig(CamelModel):
|
||||
prompt: str = ""
|
||||
realtime_model: str = ""
|
||||
|
||||
|
||||
class WorkflowConfig(CamelModel):
|
||||
graph: dict[str, Any] = {} # {nodes, edges, viewport};节点 data 可带 *CredentialId 覆盖
|
||||
|
||||
|
||||
class DifyConfig(CamelModel):
|
||||
api_url: str = ""
|
||||
api_key: str = "" # 写时:占位符/空 → 保留旧
|
||||
|
||||
|
||||
class FastgptConfig(CamelModel):
|
||||
app_id: str = ""
|
||||
api_url: str = ""
|
||||
api_key: str = ""
|
||||
|
||||
|
||||
class OpencodeConfig(CamelModel):
|
||||
prompt: str = ""
|
||||
api_url: str = ""
|
||||
api_key: str = ""
|
||||
|
||||
|
||||
CONFIG_BY_TYPE: dict[str, type[CamelModel]] = {
|
||||
"prompt": PromptConfig,
|
||||
"workflow": WorkflowConfig,
|
||||
"dify": DifyConfig,
|
||||
"fastgpt": FastgptConfig,
|
||||
"opencode": OpencodeConfig,
|
||||
# 各 type 允许的瘦字段(其余字段写入时清零,防止跨类型脏数据)
|
||||
ALLOWED_FIELDS: dict[str, set[str]] = {
|
||||
"prompt": {"prompt"},
|
||||
"workflow": {"graph"},
|
||||
"dify": {"api_url", "api_key"},
|
||||
"fastgpt": {"app_id", "api_url", "api_key"},
|
||||
"opencode": {"prompt", "api_url", "api_key"},
|
||||
}
|
||||
|
||||
|
||||
# ---------- 助手(单表,无版本化;type 可变) ----------
|
||||
# ---------- 助手(单表 STI:瘦类型真列 + workflow 图 JSON 列) ----------
|
||||
class AssistantUpsert(CamelModel):
|
||||
name: str
|
||||
type: AssistantType = "prompt"
|
||||
@@ -82,14 +56,22 @@ class AssistantUpsert(CamelModel):
|
||||
realtime_credential_id: str | None = None
|
||||
knowledge_base_id: str | None = None
|
||||
|
||||
# 类型专属字段;校验后归一为 camelCase 存库
|
||||
config: dict[str, Any] = {}
|
||||
# 瘦类型专属(真列);按 type 取用,无关字段写入时清零
|
||||
prompt: str = ""
|
||||
api_url: str = ""
|
||||
api_key: str = "" # 写时:占位符/空 → 保留旧(哨兵)
|
||||
app_id: str = ""
|
||||
# workflow 专属:图
|
||||
graph: dict[str, Any] = {}
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _normalize_config(self):
|
||||
model = CONFIG_BY_TYPE[self.type]
|
||||
# 按当前 type 校验并裁掉无关字段,统一回 camelCase 落库
|
||||
self.config = model.model_validate(self.config).model_dump(by_alias=True)
|
||||
def _strip_irrelevant_fields(self):
|
||||
allowed = ALLOWED_FIELDS[self.type]
|
||||
for field in ("prompt", "api_url", "api_key", "app_id"):
|
||||
if field not in allowed:
|
||||
setattr(self, field, "")
|
||||
if "graph" not in allowed:
|
||||
self.graph = {}
|
||||
return self
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user