WorkspaceService¶
WorkspaceService manages the lifecycle of workspaces for the current application session. Each workspace maps to a directory on disk containing a dedicated SQLite database file.
Session-Scoped Registry¶
The service maintains an in-memory registry of Workspace and SqliteGraphRepository objects for the current session. Workspaces created in previous sessions are not automatically reloaded — this will be addressed in a future persistence pass.
Default Storage Directory¶
If no base_directory is specified, workspaces are stored under ~/.knowledge_platform/:
from knowledge_platform.services.workspace_service import WorkspaceService
svc = WorkspaceService() # uses ~/.knowledge_platform/
svc = WorkspaceService(base_directory=Path("/data/workspaces")) # custom path
Creating and Accessing Workspaces¶
# Create
workspace = svc.create_workspace("My Research Notes")
print(workspace.db_path) # Path("~/.knowledge_platform/<uuid>.db")
# Retrieve
ws = svc.get_workspace(workspace.id)
# List all workspaces in the current session
all_ws = svc.list_workspaces()
Getting a Repository¶
Each workspace has exactly one SqliteGraphRepository. Retrieve it to pass to a GraphEngine:
from knowledge_platform.core.engine import GraphEngine
repo = svc.get_repository(workspace.id)
engine = GraphEngine(repo)
Cleanup¶
Call close() when the service is being torn down to release all SQLite connection pools:
In the standard application flow, WorkspaceService.close() is called at application exit. In tests, use a yield-based fixture:
@pytest.fixture()
def workspace_service(tmp_path):
svc = WorkspaceService(base_directory=tmp_path)
yield svc
svc.close()
API Reference¶
knowledge_platform.services.workspace_service.WorkspaceService ¶
Manages workspace creation, loading, and listing.
Workspaces are persisted as directories containing a SQLite database. The service keeps an in-memory registry of open workspaces for the current session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_directory
|
Path | None
|
Parent directory where workspace databases are stored.
Defaults to |
None
|
Source code in src/knowledge_platform/services/workspace_service.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Functions¶
close ¶
Dispose all open repository connection pools.
Call this when the service is being torn down (e.g. application exit or end of a test) to release SQLite file handles cleanly.
Source code in src/knowledge_platform/services/workspace_service.py
create_workspace ¶
Create and register a new workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Display name. |
required |
Returns:
| Type | Description |
|---|---|
Workspace
|
Newly created :class: |
Source code in src/knowledge_platform/services/workspace_service.py
get_repository ¶
Return the SQLite repository for a workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Target identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
SqliteGraphRepository
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If workspace not registered. |
Source code in src/knowledge_platform/services/workspace_service.py
get_workspace ¶
Return a registered workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Target identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
Workspace
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If not registered in the current session. |
Source code in src/knowledge_platform/services/workspace_service.py
list_workspaces ¶
Return all currently registered workspaces.
Returns:
| Type | Description |
|---|---|
list[Workspace]
|
List of :class: |
Source code in src/knowledge_platform/services/workspace_service.py
open_workspace ¶
Load an existing workspace from disk into the current session.