File size: 10,117 Bytes
9792ea7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
"""Abstract knowledge base manager.

The manager is the **lifecycle owner** of knowledge bases:

- it creates / lists / deletes :class:`KnowledgeBaseRecord` rows in
  storage,
- it allocates / drops the matching vector store collections,
- it resolves an embedding model from the record's credential and
  hands a ready-to-use :class:`KnowledgeBase` runtime back to callers.

Different subclasses encode different *isolation strategies*: one
collection per knowledge base, a single shared collection scoped by
metadata, native VDB namespaces, etc.  All of them share the same
:class:`KnowledgeBaseManagerBase` interface so the rest of the
application can stay strategy-agnostic.

The manager is created once at application startup and stored on
``app.state.knowledge_base_manager``.  See
:func:`~agentscope.app.create_app` for the wiring.
"""
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Self

from ._dimension_policy import DimensionPolicy

if TYPE_CHECKING:
    from types import TracebackType

    from ...storage import (
        EmbeddingModelConfig,
        KnowledgeBaseRecord,
        StorageBase,
    )
    from ....rag import KnowledgeBase, VectorStoreBase


class KnowledgeBaseManagerBase(ABC):
    """Abstract base for knowledge base managers.

    Subclasses implement a specific isolation strategy by overriding
    :meth:`create_knowledge_base`, :meth:`delete_knowledge_base`, and
    :meth:`get_knowledge`.  The bookkeeping methods
    (:meth:`get_knowledge_base`, :meth:`list_knowledge_bases`) have
    default implementations that delegate to the bound storage.
    """

    def __init__(
        self,
        storage: "StorageBase",
        vector_store: "VectorStoreBase",
    ) -> None:
        """Initialize the manager.

        Args:
            storage (`StorageBase`):
                The application-wide storage backend used to persist
                :class:`KnowledgeBaseRecord` rows and resolve
                credentials.
            vector_store (`VectorStoreBase`):
                The application-wide vector store instance shared by
                every knowledge base allocated by this manager.
        """
        self._storage = storage
        self._vector_store = vector_store

    # ------------------------------------------------------------------
    # Lifecycle hooks
    # ------------------------------------------------------------------

    async def __aenter__(self) -> Self:
        """Enter the manager's lifetime.

        Enters the bound vector store's async context so a single
        ``create_app`` parameter (the manager) covers the vector store's
        lifecycle too. Subclasses that override this MUST call
        ``await super().__aenter__()`` first to keep the vector store
        ready before subclass-specific setup runs.
        """
        await self._vector_store.__aenter__()
        return self

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc: BaseException | None,
        tb: "TracebackType | None",
    ) -> None:
        """Exit the manager's lifetime, releasing the vector store.

        Args:
            exc_type (`type[BaseException] | None`):
                The exception type raised inside the with-block, if any.
            exc (`BaseException | None`):
                The exception instance raised inside the with-block,
                if any.
            tb (`TracebackType | None`):
                The traceback for the raised exception, if any.
        """
        await self._vector_store.__aexit__(exc_type, exc, tb)

    # ------------------------------------------------------------------
    # Capability discovery
    # ------------------------------------------------------------------

    @abstractmethod
    async def get_dimension_policy(self) -> DimensionPolicy:
        """Return the embedding-dimension policy this manager enforces.

        Surfaced over HTTP so the front-end can soft-filter
        incompatible models / dimensions before submission and show a
        helpful banner.

        Returns:
            `DimensionPolicy`:
                The current dimension policy.
        """

    # ------------------------------------------------------------------
    # CRUD
    # ------------------------------------------------------------------

    @abstractmethod
    async def create_knowledge_base(
        self,
        user_id: str,
        name: str,
        description: str,
        embedding_model_config: "EmbeddingModelConfig",
    ) -> "KnowledgeBaseRecord":
        """Create a new knowledge base for the given user.

        Implementations must:

        1. validate ``embedding_model_config.dimensions`` against
           :meth:`get_dimension_policy`;
        2. allocate the vector store collection (or namespace) the
           strategy uses;
        3. persist a :class:`KnowledgeBaseRecord` and return it.

        Args:
            user_id (`str`):
                The owner user id.
            name (`str`):
                Display name.
            description (`str`):
                Free-form description.
            embedding_model_config (`EmbeddingModelConfig`):
                Embedding model configuration; pinned to the record.

        Returns:
            `KnowledgeBaseRecord`:
                The newly persisted record.

        Raises:
            `DimensionPolicyError`:
                If the requested dimension violates the manager's
                dimension policy.
        """

    async def get_knowledge_base(
        self,
        user_id: str,
        knowledge_base_id: str,
    ) -> "KnowledgeBaseRecord | None":
        """Fetch a knowledge base record by id (delegates to storage).

        Args:
            user_id (`str`):
                The owner user id.
            knowledge_base_id (`str`):
                The knowledge base id.

        Returns:
            `KnowledgeBaseRecord | None`:
                The record, or ``None`` if not found / not owned by
                the user.
        """
        return await self._storage.get_knowledge_base(
            user_id,
            knowledge_base_id,
        )

    async def list_knowledge_bases(
        self,
        user_id: str,
    ) -> "list[KnowledgeBaseRecord]":
        """List all knowledge base records owned by the given user.

        Args:
            user_id (`str`):
                The owner user id.

        Returns:
            `list[KnowledgeBaseRecord]`:
                All knowledge base records belonging to the user.
        """
        return await self._storage.list_knowledge_bases(user_id)

    async def update_knowledge_base(
        self,
        user_id: str,
        knowledge_base_id: str,
        name: str | None = None,
        description: str | None = None,
    ) -> "KnowledgeBaseRecord | None":
        """Update mutable fields on an existing knowledge base record.

        Only ``name`` and ``description`` are mutable.  The embedding
        model configuration and the underlying collection are pinned
        for the lifetime of the record because changing either would
        invalidate every previously inserted vector.

        Args:
            user_id (`str`):
                The owner user id.
            knowledge_base_id (`str`):
                The knowledge base id.
            name (`str | None`, optional):
                New display name; ``None`` leaves the name unchanged.
            description (`str | None`, optional):
                New description; ``None`` leaves the description
                unchanged.

        Returns:
            `KnowledgeBaseRecord | None`:
                The updated record, or ``None`` if the record was not
                found / not owned by the user.
        """
        record = await self._storage.get_knowledge_base(
            user_id,
            knowledge_base_id,
        )
        if record is None:
            return None
        if name is not None:
            record.name = name
        if description is not None:
            record.description = description
        return await self._storage.upsert_knowledge_base(user_id, record)

    @abstractmethod
    async def delete_knowledge_base(
        self,
        user_id: str,
        knowledge_base_id: str,
    ) -> bool:
        """Delete a knowledge base record and its underlying storage.

        Implementations must:

        1. authorise the call by looking the record up first;
        2. drop the vector store collection (or scope) the strategy
           uses;
        3. remove the :class:`KnowledgeBaseRecord` from storage.

        Args:
            user_id (`str`):
                The owner user id.
            knowledge_base_id (`str`):
                The id of the knowledge base to delete.

        Returns:
            `bool`:
                ``True`` if the record existed and was deleted,
                ``False`` if it was not found.
        """

    # ------------------------------------------------------------------
    # KnowledgeBase runtime
    # ------------------------------------------------------------------

    @abstractmethod
    async def get_knowledge(
        self,
        user_id: str,
        knowledge_base_id: str,
    ) -> "KnowledgeBase":
        """Resolve a runtime :class:`KnowledgeBase` handle for one KB.

        Implementations are responsible for:

        - looking the record up in storage (authorisation);
        - resolving the embedding model from the record's credential;
        - constructing the :class:`KnowledgeBase` with the strategy's
          collection name and metadata filter.

        Args:
            user_id (`str`):
                The owner user id.
            knowledge_base_id (`str`):
                The knowledge base id.

        Returns:
            `KnowledgeBase`:
                A runtime handle bound to this knowledge base.

        Raises:
            `KnowledgeBaseNotFoundError`:
                If the record does not exist or does not belong to
                the authenticated user.
        """