feat(sync): 将桌面人设绑定到 Vault 记录
This commit is contained in:
@@ -1620,12 +1620,12 @@ async def cancel_export(job_id: str) -> OperationResponse:
|
||||
|
||||
|
||||
@router.get("/settings/persona", response_model=PersonaSettings, tags=["Settings"])
|
||||
async def get_global_persona():
|
||||
def get_global_persona():
|
||||
return load_persona()
|
||||
|
||||
|
||||
@router.put("/settings/persona", response_model=PersonaSettings, tags=["Settings"])
|
||||
async def put_global_persona(request: PersonaSettings):
|
||||
def put_global_persona(request: PersonaSettings):
|
||||
return save_persona(request)
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ class DialoguePair(BaseModel):
|
||||
|
||||
class PersonaSettings(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
version: int = Field(default=0, ge=0)
|
||||
version: int = Field(default=0, ge=0, le=9007199254740991)
|
||||
revision: str = Field(default="", pattern=r"^(?:[0-9a-f]{64})?$")
|
||||
name: str = Field(default="", max_length=128)
|
||||
system_prompt: str = Field(default="", max_length=16000)
|
||||
dialogue_pairs: list[DialoguePair] = Field(default_factory=list, max_length=20)
|
||||
@@ -24,13 +25,42 @@ def connection():
|
||||
return conn
|
||||
|
||||
|
||||
def _desktop():
|
||||
from app.config import get_settings
|
||||
return get_settings().environment == 'desktop'
|
||||
|
||||
|
||||
def load_persona():
|
||||
if _desktop():
|
||||
from app.services.desktop_notes import call
|
||||
document = call('persona.get', id='default')
|
||||
if document is None:
|
||||
return PersonaSettings()
|
||||
return PersonaSettings.model_validate({**document['record']['data'], 'revision': document['hash']})
|
||||
with closing(connection()) as conn:
|
||||
row = conn.execute("SELECT data FROM global_persona WHERE id=1").fetchone()
|
||||
return PersonaSettings.model_validate_json(row[0]) if row else PersonaSettings()
|
||||
|
||||
|
||||
def save_persona(settings):
|
||||
if _desktop():
|
||||
from uuid import uuid4
|
||||
from app import host_bridge
|
||||
from app.services.desktop_notes import call
|
||||
from app.errors import ApiError
|
||||
if settings.version >= 9007199254740991:
|
||||
raise ApiError(409, 'PERSONA_VERSION_EXHAUSTED', '人设版本已达到上限。')
|
||||
data = settings.model_dump(exclude={'revision'})
|
||||
data['version'] += 1
|
||||
operation = host_bridge.operation_id.get() or str(uuid4())
|
||||
try:
|
||||
receipt = call('persona.write', record={'schema': 1, 'kind': 'persona', 'id': 'default', 'data': data},
|
||||
expected=settings.revision, operation_id=operation)
|
||||
except ApiError as error:
|
||||
if error.code == 'REVISION_CONFLICT':
|
||||
raise ApiError(409, 'PERSONA_VERSION_CONFLICT', '当前工作区人设已被修改,请重新打开表单后保存。') from None
|
||||
raise
|
||||
return PersonaSettings.model_validate({**receipt['record']['data'], 'revision': receipt['hash']})
|
||||
from app.errors import ApiError
|
||||
with closing(connection()) as conn:
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
|
||||
Reference in New Issue
Block a user