Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""LangGraph Store implementation backed by OpenViking."""
from __future__ import annotations
import asyncio
import json
import logging
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Iterable
from urllib.parse import quote, unquote
from langchain_openviking._uri import UriClassification, classify_uri
from langchain_openviking.client import (
OpenVikingConnection,
OptionalDependencyError,
call_openviking,
ensure_client,
is_not_found_error,
item_value,
iter_result_items,
missing_dependency,
)
_LANGGRAPH_IMPORT_ERROR: OptionalDependencyError | None = None
if TYPE_CHECKING:
from langgraph.store.base import (
BaseStore,
GetOp,
Item,
ListNamespacesOp,
MatchCondition,
PutOp,
SearchItem,
SearchOp,
get_text_at_path,
)
else:
try:
from langgraph.store.base import (
BaseStore,
GetOp,
Item,
ListNamespacesOp,
MatchCondition,
PutOp,
SearchItem,
SearchOp,
get_text_at_path,
)
except ImportError: # pragma: no cover - exercised by optional import path
_LANGGRAPH_IMPORT_ERROR = missing_dependency("langgraph", "langgraph")
BaseStore = object
GetOp = PutOp = SearchOp = ListNamespacesOp = MatchCondition = object
Item = SearchItem = object
logger = logging.getLogger(__name__)
class OpenVikingStore(BaseStore):
"""LangGraph ``BaseStore`` persisted and indexed through OpenViking.
Values are stored as JSON records under ``<root_uri>/data``. A separate
markdown projection under ``<root_uri>/index`` gives OpenViking semantic
retrieval a compact document to index for query-based ``search`` calls.
Removal ignores missing targets and propagates other backend errors.
Args:
root_uri: Base URI for the store. Defaults to the ``viking://~`` home alias,
which the server expands to the authenticated caller's own user space, so
this requires an OpenViking server with ``viking://~`` support. Pass an
explicit ``viking://user/<uid>/...`` URI to target a specific user.
"""
def __init__(
self,
*,
client: Any = None,
url: str | None = None,
api_key: str | None = None,
account: str | None = None,
user: str | None = None,
user_id: str | None = None,
actor_peer_id: str | None = None,
root_uri: str = "viking://~/memories/langgraph_store",
index: bool | list[str] | None = None,
wait: bool = True,
search_fetch_limit: int = 50,
auto_initialize: bool = True,
):
if _LANGGRAPH_IMPORT_ERROR is not None:
raise _LANGGRAPH_IMPORT_ERROR
super().__init__()
self._connection: OpenVikingConnection = OpenVikingConnection(
client=client,
url=url,
api_key=api_key,
account=account,
user=user,
user_id=user_id,
actor_peer_id=actor_peer_id,
auto_initialize=auto_initialize,
)
self.root_uri: str = root_uri.rstrip("/")
self.index: bool | list[str] | None = index
self.wait: bool = wait
self.search_fetch_limit: int = search_fetch_limit
self._client_cache: Any = None
def batch(self, ops: Iterable[Any]) -> list[Any]:
results: list[Any] = []
for op in ops:
if isinstance(op, GetOp):
results.append(self.get(op.namespace, op.key))
elif isinstance(op, PutOp):
if op.value is None:
self.delete(op.namespace, op.key)
else:
self.put(op.namespace, op.key, op.value, index=op.index, ttl=op.ttl)
results.append(None)
elif isinstance(op, SearchOp):
results.append(
self.search(
op.namespace_prefix,
query=op.query,
filter=op.filter,
limit=op.limit,
offset=op.offset,
)
)
elif isinstance(op, ListNamespacesOp):
results.append(
self._list_namespaces(
op.match_conditions,
max_depth=op.max_depth,
limit=op.limit,
offset=op.offset,
)
)
else:
raise TypeError(f"Unsupported LangGraph store operation: {type(op)!r}")
return results
async def abatch(self, ops: Iterable[Any]) -> list[Any]:
return await asyncio.to_thread(lambda: self.batch(list(ops)))
def get(self, namespace: tuple[str, ...], key: str, *, refresh_ttl: Any = None) -> Any:
try:
record = self._read_record(self._data_uri(namespace, key))
except Exception as exc:
if not is_not_found_error(exc):
raise
return None
return Item(
namespace=tuple(record["namespace"]),
key=record["key"],
value=record["value"],
created_at=_parse_dt(record["created_at"]),
updated_at=_parse_dt(record["updated_at"]),
)
def put(
self,
namespace: tuple[str, ...],
key: str,
value: Mapping[str, Any] | None,
index: bool | list[str] | None = None,
*,
ttl: Any = None,
) -> None:
from langgraph.store.base import _validate_namespace
if ttl is not None:
raise NotImplementedError(
"TTL is not supported by OpenVikingStore. "
"OpenViking stores LangGraph values as durable content records."
)
namespace = tuple(namespace)
_validate_namespace(namespace)
if value is None:
self.delete(namespace, key)
return
now = datetime.now(timezone.utc)
data_uri = self._data_uri(namespace, key)
record = self._write_record(data_uri, namespace, key, dict(value), now)
effective_index = self.index if index is None else index
index_uri = self._index_uri(namespace, key)
if effective_index is False:
self._remove(index_uri)
return
self._write(index_uri, self._index_document(record, effective_index))
def delete(self, namespace: tuple[str, ...], key: str) -> None:
self._remove(self._data_uri(namespace, key))
self._remove(self._index_uri(namespace, key))
def search(
self,
namespace_prefix: tuple[str, ...],
*,
query: str | None = None,
filter: dict[str, Any] | None = None,
limit: int = 10,
offset: int = 0,
refresh_ttl: Any = None,
) -> list[Any]:
namespace_prefix = tuple(namespace_prefix)
if query:
return self._semantic_search(namespace_prefix, query, filter, limit, offset)
items = [
self._item_to_search_item(item, score=None)
for item in self._list_items(namespace_prefix)
if _matches_filter(item.value, filter)
]
items.sort(key=lambda item: (item.updated_at, item.namespace, item.key), reverse=True)
return items[offset : offset + limit]
def list_namespaces(
self,
*,
prefix: tuple[str, ...] | None = None,
suffix: tuple[str, ...] | None = None,
max_depth: int | None = None,
limit: int = 100,
offset: int = 0,
) -> list[tuple[str, ...]]:
conditions = []
if prefix:
conditions.append(MatchCondition("prefix", prefix))
if suffix:
conditions.append(MatchCondition("suffix", suffix))
return self._list_namespaces(conditions, max_depth=max_depth, limit=limit, offset=offset)
def _list_namespaces(
self,
match_conditions: Iterable[Any] | None,
*,
max_depth: int | None,
limit: int,
offset: int,
) -> list[tuple[str, ...]]:
conditions = tuple(match_conditions or ())
scan_prefix: tuple[str, ...] = ()
for condition in conditions:
if condition.match_type == "prefix":
path = tuple(condition.path)
literal_prefix = path[: path.index("*")] if "*" in path else path
if len(literal_prefix) > len(scan_prefix):
scan_prefix = literal_prefix
namespaces = set()
# Wildcards match namespace labels, never physical URI path segments.
for uri in self._all_data_uris(scan_prefix):
parsed = self._parse_data_uri(uri)
if parsed is None:
continue
namespace, _key = parsed
if not all(_namespace_matches(namespace, condition) for condition in conditions):
continue
if max_depth is not None and len(namespace) > max_depth:
namespace = namespace[:max_depth]
namespaces.add(namespace)
ordered = sorted(namespaces)
return ordered[offset : offset + limit]
def _semantic_search(
self,
namespace_prefix: tuple[str, ...],
query: str,
filter: dict[str, Any] | None,
limit: int,
offset: int,
) -> list[Any]:
result = call_openviking(
self._client(),
"find",
query=query,
target_uri=self._index_prefix_uri(namespace_prefix),
limit=max(limit + offset, self.search_fetch_limit),
)
items: list[Any] = []
for _context_type, result_item in iter_result_items(
result, ("memory", "resource", "skill")
):
uri = item_value(result_item, "uri", "")
parsed = self._parse_index_uri(uri)
if parsed is None:
continue
namespace, key = parsed
item = self.get(namespace, key)
if item is None or not _matches_filter(item.value, filter):
continue
items.append(self._item_to_search_item(item, score=item_value(result_item, "score")))
return items[offset : offset + limit]
def _list_items(self, namespace_prefix: tuple[str, ...]) -> list[Any]:
items = []
for uri in self._all_data_uris(namespace_prefix):
parsed = self._parse_data_uri(uri)
if parsed is None:
continue
namespace, key = parsed
item = self.get(namespace, key)
if item is not None:
items.append(item)
return items
def _all_data_uris(self, namespace_prefix: tuple[str, ...]) -> list[str]:
base_uri = self._data_prefix_uri(namespace_prefix)
uris: list[str] = []
seen: set[str] = set()
for pattern in ("*.json", "**/*.json"):
try:
result = call_openviking(self._client(), "glob", pattern=pattern, uri=base_uri)
except Exception:
continue
for uri in _extract_uris(result):
if uri not in seen:
seen.add(uri)
uris.append(uri)
return uris
def _client(self) -> Any:
if self._client_cache is None:
self._client_cache = ensure_client(self._connection)
return self._client_cache
def _read_record(self, uri: str) -> dict[str, Any]:
content = call_openviking(self._client(), "read", uri=uri)
if isinstance(content, bytes):
content = content.decode("utf-8")
return json.loads(str(content))
def _write_record(
self,
uri: str,
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
now: datetime,
) -> dict[str, Any]:
try:
existing = self._read_record(uri)
except Exception as exc:
if not is_not_found_error(exc):
raise
record = _store_record(namespace, key, value, created_at=now, updated_at=now)
self._write_create(uri, json.dumps(record, ensure_ascii=False, indent=2))
return record
created_at = _parse_dt(existing["created_at"])
record = _store_record(namespace, key, value, created_at=created_at, updated_at=now)
self._write_replace(uri, json.dumps(record, ensure_ascii=False, indent=2))
return record
def _write(self, uri: str, content: str) -> None:
try:
self._write_create(uri, content)
return
except Exception as create_exc:
try:
self._write_replace(uri, content)
return
except Exception:
raise create_exc
def _write_create(self, uri: str, content: str) -> None:
call_openviking(
self._client(),
"write",
uri=uri,
content=content,
mode="create",
wait=self.wait,
)
def _write_replace(self, uri: str, content: str) -> None:
call_openviking(
self._client(),
"write",
uri=uri,
content=content,
mode="replace",
wait=self.wait,
)
def _remove(self, uri: str) -> None:
try:
call_openviking(self._client(), "rm", uri=uri, recursive=False)
except Exception as exc:
if not is_not_found_error(exc):
raise
logger.debug("OpenVikingStore remove ignored missing URI", exc_info=True)
def _data_uri(self, namespace: tuple[str, ...], key: str) -> str:
return f"{self._data_prefix_uri(namespace)}/{_segment(key)}.json"
def _index_uri(self, namespace: tuple[str, ...], key: str) -> str:
return f"{self._index_prefix_uri(namespace)}/{_segment(key)}.md"
def _data_prefix_uri(self, namespace: tuple[str, ...]) -> str:
return _join_uri(self.root_uri, "data", *namespace)
def _index_prefix_uri(self, namespace: tuple[str, ...]) -> str:
return _join_uri(self.root_uri, "index", *namespace)
def _parse_data_uri(self, uri: str) -> tuple[tuple[str, ...], str] | None:
return self._parse_record_uri(uri, "data", ".json")
def _parse_index_uri(self, uri: str) -> tuple[tuple[str, ...], str] | None:
return self._parse_record_uri(uri, "index", ".md")
def _parse_record_uri(
self, uri: str, collection: str, suffix: str
) -> tuple[tuple[str, ...], str] | None:
prefix = _join_uri(self.root_uri, collection) + "/"
if not uri.startswith(prefix) or not uri.endswith(suffix):
return _parse_canonicalized_record_uri(
root_uri=self.root_uri,
uri=uri,
collection=collection,
suffix=suffix,
)
return _parse_record_parts(uri[len(prefix) : -len(suffix)].split("/"))
def _index_document(self, record: dict[str, Any], index: bool | list[str] | None) -> str:
value = record["value"]
projected = _project_value(value, index)
return "\n".join(
[
f"# {record['key']}",
"",
f"Namespace: {'/'.join(record['namespace'])}",
f"Key: {record['key']}",
"",
json.dumps(projected, ensure_ascii=False, indent=2),
]
)
def _item_to_search_item(self, item: Any, score: float | None) -> Any:
return SearchItem(
namespace=item.namespace,
key=item.key,
value=item.value,
created_at=item.created_at,
updated_at=item.updated_at,
score=score,
)
def _segment(value: str) -> str:
return quote(str(value), safe="")
def _join_uri(root: str, *segments: str) -> str:
suffix = "/".join(_segment(segment) for segment in segments if segment)
return root.rstrip("/") if not suffix else f"{root.rstrip('/')}/{suffix}"
def _parse_canonicalized_record_uri(
*,
root_uri: str,
uri: str,
collection: str,
suffix: str,
) -> tuple[tuple[str, ...], str] | None:
root = classify_uri(root_uri)
root_tail = _identity_relative_root_tail(root)
candidate = classify_uri(uri)
if root.parts[:2] == ("~", "peers") and root.content_index == 3:
if candidate.scope != "user" or candidate.content_index != 4:
return None
# Only the current-user alias changes; keep the peer ID in the match.
root_tail = root.parts[1:]
candidate_tail = candidate.parts[2:]
else:
if root_tail is None:
return None
if candidate.scope != root.scope or candidate.content_index is None:
return None
candidate_tail = candidate.parts[candidate.content_index :]
if len(candidate_tail) < len(root_tail) + 2:
return None
if candidate_tail[: len(root_tail)] != root_tail:
return None
collection_index = len(root_tail)
if candidate_tail[collection_index] != collection:
return None
rel_parts = list(candidate_tail[collection_index + 1 :])
if not rel_parts or not rel_parts[-1].endswith(suffix):
return None
rel_parts = [*rel_parts[:-1], rel_parts[-1][: -len(suffix)]]
return _parse_record_parts(rel_parts)
def _identity_relative_root_tail(classification: UriClassification) -> tuple[str, ...] | None:
if classification.scope != "user":
return None
if classification.content_index != 1:
return None
return classification.parts[classification.content_index :]
def _parse_record_parts(parts: list[str]) -> tuple[tuple[str, ...], str] | None:
if not parts or not parts[-1]:
return None
namespace = tuple(unquote(part) for part in parts[:-1])
key = unquote(parts[-1])
return namespace, key
def _parse_dt(value: str) -> datetime:
parsed = datetime.fromisoformat(value)
if parsed.tzinfo is None:
return parsed.replace(tzinfo=timezone.utc)
return parsed
def _store_record(
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
*,
created_at: datetime,
updated_at: datetime,
) -> dict[str, Any]:
return {
"namespace": list(namespace),
"key": key,
"value": value,
"created_at": created_at.isoformat(),
"updated_at": updated_at.isoformat(),
}
def _extract_uris(value: Any) -> list[str]:
if value is None:
return []
if isinstance(value, str):
return [value] if value.startswith("viking://") else []
if isinstance(value, dict):
if isinstance(value.get("uri"), str):
return [value["uri"]]
uris: list[str] = []
for key in ("matches", "result", "files", "items"):
uris.extend(_extract_uris(value.get(key)))
return uris
if isinstance(value, list):
nested_uris: list[str] = []
for item in value:
nested_uris.extend(_extract_uris(item))
return nested_uris
return []
def _project_value(value: dict[str, Any], index: bool | list[str] | None) -> dict[str, Any]:
if isinstance(index, list):
projected = {field: _nested_value(value, field) for field in index}
for field in index:
if projected[field] is None:
projected[field] = get_text_at_path(value, field) or None
return projected
return value
def _nested_value(value: dict[str, Any], path: str) -> Any:
current: Any = value
for part in path.split("."):
if not isinstance(current, dict):
return None
current = current.get(part)
return current
def _matches_filter(value: dict[str, Any], filter: dict[str, Any] | None) -> bool:
if not filter:
return True
for path, expected in filter.items():
actual = _nested_value(value, path)
if not _matches_value(actual, expected):
return False
return True
def _matches_value(actual: Any, expected: Any) -> bool:
if isinstance(expected, dict):
if any(
key.startswith("$") or key in {"eq", "ne", "gt", "gte", "lt", "lte", "in"}
for key in expected
):
return all(_compare(actual, op, target) for op, target in expected.items())
return isinstance(actual, dict) and all(
_matches_value(actual.get(key), target) for key, target in expected.items()
)
if isinstance(expected, (list, tuple)):
return (
isinstance(actual, (list, tuple))
and len(actual) == len(expected)
and all(
_matches_value(left, right) for left, right in zip(actual, expected, strict=False)
)
)
return actual == expected
def _compare(actual: Any, op: str, target: Any) -> bool:
if op in {"$eq", "eq"}:
return actual == target
if op in {"$ne", "ne"}:
return actual != target
if op in {"$gt", "gt"}:
return _safe_ordered_compare(actual, target, lambda left, right: left > right)
if op in {"$gte", "gte"}:
return _safe_ordered_compare(actual, target, lambda left, right: left >= right)
if op in {"$lt", "lt"}:
return _safe_ordered_compare(actual, target, lambda left, right: left < right)
if op in {"$lte", "lte"}:
return _safe_ordered_compare(actual, target, lambda left, right: left <= right)
if op in {"$in", "in"}:
try:
return actual in target
except TypeError:
return False
return actual == target
def _safe_ordered_compare(actual: Any, target: Any, compare) -> bool:
if actual is None:
return False
try:
return bool(compare(actual, target))
except TypeError:
return False
def _namespace_matches(value: tuple[str, ...], condition: Any) -> bool:
path = condition.path
if len(value) < len(path):
return False
if condition.match_type == "prefix":
selected = value[: len(path)]
elif condition.match_type == "suffix":
selected = value[-len(path) :] if path else ()
else:
raise ValueError(f"Unsupported match type: {condition.match_type}")
return all(
pattern == "*" or actual == pattern for actual, pattern in zip(selected, path, strict=False)
)