115 lines
3.8 KiB
Python
115 lines
3.8 KiB
Python
"""
|
||||
|
|
多租户工作空间服务
|
|||
|
|
==================
|
|||
|
|
|
|||
|
|
【功能说明】
|
|||
|
|
实现工作空间隔离:项目/知识库/任务/记忆按空间隔离。
|
|||
|
|
对应功能 7:多团队共存。
|
|||
|
|
|
|||
|
|
【技术选型理由】
|
|||
|
|
1. 轻量方案:所有实体加 workspace_id 字段,查询时过滤;
|
|||
|
|
2. 空间数据独立,互不可见(数据级隔离);
|
|||
|
|
3. 支持默认空间(个人)+ 团队空间。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import logging
|
|||
|
|
import sqlite3
|
|||
|
|
import uuid
|
|||
|
|
from datetime import datetime
|
|||
|
|
from pathlib import Path
|
|||
|
|
from typing import Optional
|
|||
|
|
|
|||
|
|
from app.store.sqlite_util import open_connection
|
|||
|
|
|
|||
|
|
logger = logging.getLogger("agent.workspace")
|
|||
|
|
|
|||
|
|
|
|||
|
|
class WorkspaceStore:
|
|||
|
|
"""工作空间存储"""
|
|||
|
|
|
|||
|
|
def __init__(self, db_path: Path) -> None:
|
|||
|
|
self._db_path = Path(db_path)
|
|||
|
|
self._init_schema()
|
|||
|
|
self._seed_default()
|
|||
|
|
|
|||
|
|
def _conn(self):
|
|||
|
|
"""
|
|||
|
|
获取一个短连接(用完自动提交/回滚并关闭)。
|
|||
|
|
|
|||
|
|
【修复】原先只返回裸 sqlite3 连接、用完从不 close,连接泄漏到 GC
|
|||
|
|
(Windows 文件锁 / 句柄增长)。改为委托 app.store.sqlite_util.open_connection,
|
|||
|
|
提交/回滚语义不变,额外保证 close。
|
|||
|
|
"""
|
|||
|
|
return open_connection(self._db_path)
|
|||
|
|
|
|||
|
|
def _init_schema(self) -> None:
|
|||
|
|
ddl = """
|
|||
|
|
CREATE TABLE IF NOT EXISTS workspace (
|
|||
|
|
id TEXT PRIMARY KEY,
|
|||
|
|
name TEXT NOT NULL,
|
|||
|
|
description TEXT DEFAULT '',
|
|||
|
|
is_default INTEGER DEFAULT 0,
|
|||
|
|
created_at TEXT NOT NULL,
|
|||
|
|
updated_at TEXT NOT NULL
|
|||
|
|
);
|
|||
|
|
"""
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
conn.execute(ddl)
|
|||
|
|
conn.commit()
|
|||
|
|
|
|||
|
|
def _seed_default(self) -> None:
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
count = conn.execute("SELECT COUNT(*) FROM workspace").fetchone()[0]
|
|||
|
|
if count == 0:
|
|||
|
|
now = datetime.now().astimezone().isoformat(timespec="seconds")
|
|||
|
|
conn.execute(
|
|||
|
|
"INSERT INTO workspace (id, name, description, is_default, created_at, updated_at) VALUES (?,?,?,?,?,?)",
|
|||
|
|
("ws_default", "默认空间", "个人工作空间", 1, now, now),
|
|||
|
|
)
|
|||
|
|
conn.commit()
|
|||
|
|
|
|||
|
|
def list_all(self) -> list[dict]:
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
rows = conn.execute("SELECT * FROM workspace ORDER BY is_default DESC, created_at").fetchall()
|
|||
|
|
return [dict(r) for r in rows]
|
|||
|
|
|
|||
|
|
def get(self, ws_id: str) -> Optional[dict]:
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
row = conn.execute("SELECT * FROM workspace WHERE id = ?", (ws_id,)).fetchone()
|
|||
|
|
return dict(row) if row else None
|
|||
|
|
|
|||
|
|
def create(self, name: str, description: str = "") -> dict:
|
|||
|
|
ws_id = f"ws_{uuid.uuid4().hex[:8]}"
|
|||
|
|
now = datetime.now().astimezone().isoformat(timespec="seconds")
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
conn.execute(
|
|||
|
|
"INSERT INTO workspace (id, name, description, is_default, created_at, updated_at) VALUES (?,?,?,0,?,?)",
|
|||
|
|
(ws_id, name, description, now, now),
|
|||
|
|
)
|
|||
|
|
conn.commit()
|
|||
|
|
return self.get(ws_id)
|
|||
|
|
|
|||
|
|
def delete(self, ws_id: str) -> bool:
|
|||
|
|
if ws_id == "ws_default":
|
|||
|
|
return False # 禁止删除默认空间
|
|||
|
|
with self._conn() as conn:
|
|||
|
|
cursor = conn.execute("DELETE FROM workspace WHERE id = ?", (ws_id,))
|
|||
|
|
conn.commit()
|
|||
|
|
return cursor.rowcount > 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 全局单例
|
|||
|
|
_store: WorkspaceStore | None = None
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_workspace_store() -> WorkspaceStore:
|
|||
|
|
global _store
|
|||
|
|
if _store is None:
|
|||
|
|
from app.config.settings import load_settings
|
|||
|
|
|
|||
|
|
settings = load_settings()
|
|||
|
|
_store = WorkspaceStore(settings.sqlite_db_path)
|
|||
|
|
return _store
|