feat: register local agent project workspaces

This commit is contained in:
Hamza Ayed
2026-10-04 01:57:44 +03:00
parent d29275043a
commit 1feeb372a4
12 changed files with 806 additions and 45 deletions
+51
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import os
import hashlib
import sqlite3
import os
from uuid import UUID
from pathlib import Path
from contextlib import contextmanager
@@ -142,6 +143,15 @@ def initialize_database() -> None:
);
CREATE INDEX IF NOT EXISTS idx_agent_audit_created
ON agent_audit_events(created_at DESC);
CREATE TABLE IF NOT EXISTS user_workspace_roots (
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
path TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(user_id, path)
);
CREATE INDEX IF NOT EXISTS idx_workspace_roots_user
ON user_workspace_roots(user_id);
"""
)
message_columns = {
@@ -171,6 +181,47 @@ def ensure_user(user_id: str) -> None:
connection.execute("INSERT OR IGNORE INTO users(id) VALUES (?)", (user_id,))
def register_workspace_root(user_id: str, path: str) -> None:
with _connect() as connection:
connection.execute("INSERT OR IGNORE INTO users(id) VALUES (?)", (user_id,))
candidate = os.path.normcase(os.path.abspath(path))
existing_roots = connection.execute(
"SELECT path FROM user_workspace_roots WHERE user_id <> ?", (user_id,)
).fetchall()
for row in existing_roots:
existing = os.path.normcase(os.path.abspath(str(row["path"])))
try:
common = os.path.commonpath((candidate, existing))
except ValueError:
continue
if common in (candidate, existing):
raise ValueError(
"مجلد المشروع يتداخل مع مجلد مسجل لحساب آخر؛ اختر مجلدًا منفصلًا."
)
connection.execute(
"INSERT OR IGNORE INTO user_workspace_roots(user_id, path) VALUES (?, ?)",
(user_id, path),
)
def list_workspace_roots(user_id: str) -> list[str]:
with _connect() as connection:
rows = connection.execute(
"SELECT path FROM user_workspace_roots WHERE user_id = ? ORDER BY created_at, path",
(user_id,),
).fetchall()
return [str(row["path"]) for row in rows]
def unregister_workspace_root(user_id: str, path: str) -> bool:
with _connect() as connection:
cursor = connection.execute(
"DELETE FROM user_workspace_roots WHERE user_id = ? AND path = ?",
(user_id, path),
)
return cursor.rowcount > 0
def list_conversations(user_id: str) -> list[dict[str, Any]]:
with _connect() as connection:
rows = connection.execute(