Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

2.6 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""HTTP backend collection adapter."""

from __future__ import annotations

from typing import Any, Dict

from openviking.storage.vectordb.collection.collection import Collection
from openviking.storage.vectordb.collection.http_collection import (
    HttpCollection,
    get_or_create_http_collection,
    list_vikingdb_collections,
)

from .base import (
    VIKINGDB_TEXT_FIELD_BYTE_LIMIT,
    CollectionAdapter,
    _normalize_collection_names,
    _parse_url,
)


class HttpCollectionAdapter(CollectionAdapter):
    """Adapter for remote HTTP vectordb project."""

    _DATA_BATCH_SIZE = 100
    _TEXT_FIELD_BYTE_LIMIT = VIKINGDB_TEXT_FIELD_BYTE_LIMIT

    def __init__(
        self,
        host: str,
        port: int,
        project_name: str,
        collection_name: str,
        index_name: str,
    ):
        super().__init__(collection_name=collection_name, index_name=index_name)
        self.mode = "http"
        self._host = host
        self._port = port
        self._project_name = project_name

    @classmethod
    def from_config(cls, config: Any):
        if not config.url:
            raise ValueError("HTTP backend requires a valid URL")
        host, port = _parse_url(config.url)
        return cls(
            host=host,
            port=port,
            project_name=config.project_name or "default",
            collection_name=config.name or "context",
            index_name=config.index_name or "default",
        )

    def _meta(self) -> Dict[str, Any]:
        return {
            "ProjectName": self._project_name,
            "CollectionName": self._collection_name,
        }

    def _remote_has_collection(self) -> bool:
        raw = list_vikingdb_collections(
            host=self._host,
            port=self._port,
            project_name=self._project_name,
        )
        return self._collection_name in _normalize_collection_names(raw)

    def _load_existing_collection_if_needed(self) -> None:
        if self._collection is not None:
            return
        if not self._remote_has_collection():
            return
        self._collection = Collection(
            HttpCollection(
                ip=self._host,
                port=self._port,
                meta_data=self._meta(),
            )
        )

    def _create_backend_collection(self, meta: Dict[str, Any]) -> Collection:
        payload = dict(meta)
        payload.update(self._meta())
        return get_or_create_http_collection(
            host=self._host,
            port=self._port,
            meta_data=payload,
        )