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
fix(skill): Fix PR bug, delete invalid config in doc, fix deepseek API for bot (#1794)
DuTaoe87751c7c6be
diff --git a/README.md b/README.mdindex 6c06255d0..a43dc7111 100644--- a/README.md+++ b/README.md@@ -511,8 +511,7 @@ Example: ovcli.conf for visiting localhost server```json{"url": "http://localhost:1933",- "timeout": 60.0,- "output": "table"+ "timeout": 60.0}```diff --git a/README_JA.md b/README_JA.mdindex 38fc0d8ef..394d44a8f 100644--- a/README_JA.md+++ b/README_JA.md@@ -310,8 +310,7 @@ set "OPENVIKING_CONFIG_FILE=%USERPROFILE%\.openviking\ov.conf"```json{"url": "http://localhost:1933",- "timeout": 60.0,- "output": "table"+ "timeout": 60.0}```diff --git a/bot/tests/test_chat_functionality.py b/bot/tests/test_chat_functionality.pyindex 556a0e4bd..b6eef3065 100644--- a/bot/tests/test_chat_functionality.py+++ b/bot/tests/test_chat_functionality.py@@ -11,6 +11,7 @@ from vikingbot.bus.queue import MessageBusfrom vikingbot.channels.chat import ChatChannel, ChatChannelConfigfrom vikingbot.channels.single_turn import SingleTurnChannel, SingleTurnChannelConfigfrom vikingbot.config.schema import SessionKey+from vikingbot.session.manager import Session@pytest.fixture@@ -77,8 +78,47 @@ class TestSingleTurnChannel:assert channel._response_received.is_set()+class TestSessionHistoryProviderSpecificFields:+ """Tests provider-specific history reconstruction from persisted session messages."""++ def test_deepseek_history_includes_reasoning_content(self):+ session = Session(key=SessionKey(type="cli", channel_id="default", chat_id="test-session"))+ session.add_message("user", "hello")+ session.add_message(+ "assistant",+ "hi",+ reasoning_content="internal reasoning",+ )++ history = session.get_history(provider_name="deepseek")++ assert history == [+ {"role": "user", "content": "hello"},+ {+ "role": "assistant",+ "content": "hi",+ "reasoning_content": "internal reasoning",+ },+ ]++ def test_non_deepseek_history_omits_reasoning_content(self):+ session = Session(key=SessionKey(type="cli", channel_id="default", chat_id="test-session"))+ session.add_message("user", "hello")+ session.add_message(+ "assistant",+ "hi",+ reasoning_content="internal reasoning",+ )++ history = session.get_history(provider_name="openai")++ assert history == [+ {"role": "user", "content": "hello"},+ {"role": "assistant", "content": "hi"},+ ]++class TestChatChannel:- """Tests for ChatChannel (interactive vikingbot chat)."""def test_chat_channel_initialization(self, message_bus, temp_workspace):"""Test that ChatChannel can be initialized correctly."""diff --git a/bot/vikingbot/agent/loop.py b/bot/vikingbot/agent/loop.pyindex d6e507377..fbef7a38d 100644--- a/bot/vikingbot/agent/loop.py+++ b/bot/vikingbot/agent/loop.py@@ -263,7 +263,7 @@ class AgentLoop:publish_events: bool = True,sender_id: str | None = None,ov_tools_enable: bool = True,- ) -> tuple[str | None, list[dict], dict[str, int], int]:+ ) -> tuple[str | None, str | None, list[dict], dict[str, int], int]:"""Run the core agent loop: call LLM, execute tools, repeat until done.@@ -274,10 +274,11 @@ class AgentLoop:ov_tools_enable: Whether to enable OpenViking tools for this sessionReturns:- tuple of (final_content, tools_used)+ tuple of (final_content, final_reasoning_content, tools_used, token_usage, iteration)"""iteration = 0final_content = None+ final_reasoning_content = Nonetools_used: list[dict] = []token_usage = {"prompt_tokens": 0,@@ -319,6 +320,7 @@ class AgentLoop:)if response.has_tool_calls:+ final_reasoning_content = response.reasoning_contentargs_list = [tc.arguments for tc in response.tool_calls]tool_call_dicts = [{@@ -402,6 +404,7 @@ class AgentLoop:)else:final_content = response.content+ final_reasoning_content = response.reasoning_contentbreakif final_content is None or (isinstance(final_content, str) and not final_content.strip()):@@ -410,7 +413,7 @@ class AgentLoop:else:final_content = "I've completed processing but have no response to give."- return final_content, tools_used, token_usage, iteration+ return final_content, final_reasoning_content, tools_used, token_usage, iteration@trace(name="process_message",@@ -595,8 +598,9 @@ class AgentLoop:)# Build initial messages (use get_history for LLM-formatted messages)+ provider_name = self.config.get_provider_name(self.model) if self.config else Nonemessages = await message_context.build_messages(- history=session.get_history(),+ history=session.get_history(provider_name=provider_name),current_message=msg.content,media=msg.media if msg.media else None,session_key=msg.session_key,@@ -608,7 +612,7 @@ class AgentLoop:# logger.info(f"New messages: {json.dumps(messages, indent=4)}")# Run agent loop- final_content, tools_used, token_usage, iteration = await self._run_agent_loop(+ final_content, final_reasoning_content, tools_used, token_usage, iteration = await self._run_agent_loop(messages=messages,session_key=session_key,publish_events=True,@@ -629,6 +633,7 @@ class AgentLoop:tools_used=tools_used if tools_used else None,token_usage=token_usage,sender_id=msg.sender_id,+ reasoning_content=final_reasoning_content,)await self.sessions.save(session)@@ -699,8 +704,9 @@ class AgentLoop:profile_user_list = getattr(channel_config, "profile_user_list", [])# Build messages with the announce content+ provider_name = self.config.get_provider_name(self.model) if self.config else Nonemessages = await self.context.build_messages(- history=session.get_history(),+ history=session.get_history(provider_name=provider_name),current_message=msg.content,session_key=msg.session_key,ov_tools_enable=ov_tools_enable,@@ -708,7 +714,7 @@ class AgentLoop:)# Run agent loop (no events published)- final_content, tools_used, token_usage, iteration = await self._run_agent_loop(+ final_content, final_reasoning_content, tools_used, token_usage, iteration = await self._run_agent_loop(messages=messages,session_key=msg.session_key,publish_events=False,@@ -721,7 +727,10 @@ class AgentLoop:# Save to session (mark as system message in history)session.add_message("user", f"[System: {msg.sender_id}] {msg.content}")session.add_message(- "assistant", final_content, tools_used=tools_used if tools_used else None+ "assistant",+ final_content,+ tools_used=tools_used if tools_used else None,+ reasoning_content=final_reasoning_content,)await self.sessions.save(session)diff --git a/bot/vikingbot/session/manager.py b/bot/vikingbot/session/manager.pyindex 27c28f413..1fa466c0b 100644--- a/bot/vikingbot/session/manager.py+++ b/bot/vikingbot/session/manager.py@@ -10,6 +10,7 @@ from typing import Anyfrom loguru import loggerfrom vikingbot.config.schema import SessionKey+from vikingbot.providers.registry import find_by_namefrom vikingbot.sandbox.manager import SandboxManagerfrom vikingbot.utils.helpers import ensure_dir, ensure_non_empty_assistant_content@@ -40,12 +41,15 @@ class Session:self.messages.append(msg)self.updated_at = datetime.now()- def get_history(self, max_messages: int = 50) -> list[dict[str, Any]]:+ def get_history(+ self, max_messages: int = 50, provider_name: str | None = None+ ) -> list[dict[str, Any]]:"""Get message history for LLM context.Args:max_messages: Maximum messages to return.+ provider_name: Optional provider name for provider-specific history fields.Returns:List of messages in LLM format.@@ -55,14 +59,22 @@ class Session:self.messages[-max_messages:] if len(self.messages) > max_messages else self.messages)- # Convert to LLM format (just role and content)+ provider_spec = find_by_name(provider_name) if provider_name else None+ include_reasoning_content = bool(provider_spec and provider_spec.name == "deepseek")+out: list[dict[str, Any]] = []for m in recent:role = m["role"]content: Any = m.get("content", "")if role == "assistant" and isinstance(content, str):content = ensure_non_empty_assistant_content(content)- out.append({"role": role, "content": content})++ msg = {"role": role, "content": content}++ if role == "assistant" and include_reasoning_content and m.get("reasoning_content"):+ msg["reasoning_content"] = m["reasoning_content"]++ out.append(msg)return outdef clear(self) -> None:diff --git a/docs/en/concepts/13-privacy.md b/docs/en/concepts/13-privacy.mdindex 6604fd106..125bf989a 100644--- a/docs/en/concepts/13-privacy.md+++ b/docs/en/concepts/13-privacy.md@@ -70,7 +70,7 @@ add_skill1. **Extraction source**: model returns JSON; `values` is used as privacy key-value pairs.2. **Content replacement**: matched plaintext is replaced by placeholders:- - <code v-pre>{{ov_privacy:skill:{skill_name}:{field_name}}}</code>+ - ``{{ov_privacy:skill:{skill_name}:{field_name}}}``3. **Block mapping captured**:- `original_content_blocks`- `replacement_content_blocks`diff --git a/docs/en/guides/01-configuration.md b/docs/en/guides/01-configuration.mdindex 7a101a2a1..b32655318 100644--- a/docs/en/guides/01-configuration.md+++ b/docs/en/guides/01-configuration.md@@ -975,7 +975,6 @@ Config file for the HTTP client (`SyncHTTPClient` / `AsyncHTTPClient`) and CLI t"account": "acme","user": "alice","agent_id": "my-agent",- "output": "table","upload": {"ignore_dirs": "node_modules,.cache,.nx","include": "*.md,*.pdf",@@ -991,7 +990,6 @@ Config file for the HTTP client (`SyncHTTPClient` / `AsyncHTTPClient`) and CLI t| `account` | Default account sent as `X-OpenViking-Account` | `null` || `user` | Default user sent as `X-OpenViking-User` | `null` || `agent_id` | Agent identifier for agent space isolation | `null` |-| `output` | Default output format: `"table"` or `"json"` | `"table"` || `upload.ignore_dirs` | Default directory ignore list for `add-resource` (CSV) | `null` || `upload.include` | Default include patterns for `add-resource` (CSV) | `null` || `upload.exclude` | Default exclude patterns for `add-resource` (CSV) | `null` |diff --git a/docs/zh/concepts/13-privacy.md b/docs/zh/concepts/13-privacy.mdindex 2e31e2b82..5ada89244 100644--- a/docs/zh/concepts/13-privacy.md+++ b/docs/zh/concepts/13-privacy.md@@ -70,7 +70,7 @@ add_skill1. **抽取结果来源**:模型返回 JSON,读取 `values` 字段作为隐私键值。2. **内容替换**:原文中命中的敏感片段会替换为占位符:- - <code v-pre>{{ov_privacy:skill:{skill_name}:{field_name}}}</code>+ - ``{{ov_privacy:skill:{skill_name}:{field_name}}}``3. **保留块映射**:会同时记录:- `original_content_blocks`- `replacement_content_blocks`diff --git a/docs/zh/guides/01-configuration.md b/docs/zh/guides/01-configuration.mdindex e9ce9fd59..ff57f588c 100644--- a/docs/zh/guides/01-configuration.md+++ b/docs/zh/guides/01-configuration.md@@ -949,7 +949,6 @@ HTTP 客户端(`SyncHTTPClient` / `AsyncHTTPClient`)和 CLI 工具连接远"account": "acme","user": "alice","agent_id": "my-agent",- "output": "table","upload": {"ignore_dirs": "node_modules,.cache,.nx","include": "*.md,*.pdf",@@ -965,7 +964,6 @@ HTTP 客户端(`SyncHTTPClient` / `AsyncHTTPClient`)和 CLI 工具连接远| `account` | 默认发送为 `X-OpenViking-Account` 的租户标识 | `null` || `user` | 默认发送为 `X-OpenViking-User` 的用户标识 | `null` || `agent_id` | Agent 标识,用于 agent space 隔离 | `null` |-| `output` | 默认输出格式:`"table"` 或 `"json"` | `"table"` || `upload.ignore_dirs` | `add-resource` 默认忽略目录列表(CSV) | `null` || `upload.include` | `add-resource` 默认包含模式(CSV) | `null` || `upload.exclude` | `add-resource` 默认排除模式(CSV) | `null` |diff --git a/openviking/privacy/service.py b/openviking/privacy/service.pyindex 592a37341..95fcaefe1 100644--- a/openviking/privacy/service.py+++ b/openviking/privacy/service.py@@ -7,6 +7,7 @@ from typing import Any, Optionalfrom openviking_cli.exceptions import NotFoundError+from openviking.core.namespace import canonical_user_rootfrom openviking.privacy.helpers import (canonicalize_values,config_root_uri,@@ -28,8 +29,11 @@ class UserPrivacyConfigService:def __init__(self, viking_fs: VikingFS):self._viking_fs = viking_fs+ def _user_root(self, ctx: RequestContext) -> str:+ return canonical_user_root(ctx)+def _user_space(self, ctx: RequestContext) -> str:- return ctx.user.user_space_name()+ return self._user_root(ctx)[len("viking://user/") :]def get_config_root(self, ctx: RequestContext, category: str, target_key: str) -> str:return config_root_uri(self._user_space(ctx), category, target_key)@@ -90,7 +94,7 @@ class UserPrivacyConfigService:return UserPrivacyConfigVersion.from_dict(json.loads(content))async def list_categories(self, ctx: RequestContext) -> list[str]:- uri = f"viking://user/{self._user_space(ctx)}/privacy"+ uri = f"{self._user_root(ctx)}/privacy"try:entries = await self._viking_fs.ls(uri, ctx=ctx)except Exception:@@ -98,7 +102,7 @@ class UserPrivacyConfigService:return sorted(entry["name"] for entry in entries if entry.get("name"))async def list_targets(self, ctx: RequestContext, category: str) -> list[str]:- uri = f"viking://user/{self._user_space(ctx)}/privacy/{category}"+ uri = f"{self._user_root(ctx)}/privacy/{category}"try:entries = await self._viking_fs.ls(uri, ctx=ctx)except Exception:diff --git a/openviking/privacy/skill_extractor.py b/openviking/privacy/skill_extractor.pyindex af2c4b8aa..fe04647a6 100644--- a/openviking/privacy/skill_extractor.py+++ b/openviking/privacy/skill_extractor.py@@ -48,7 +48,7 @@ async def extract_skill_privacy_values(placeholder_result = placeholderize_skill_content_with_blocks(content, skill_name, values)return SkillPrivacyExtractionResult(- values=values,+ values=placeholder_result.replaced_values,original_content=content,sanitized_content=placeholder_result.sanitized_content,original_content_blocks=placeholder_result.original_content_blocks,diff --git a/openviking/privacy/skill_placeholder.py b/openviking/privacy/skill_placeholder.pyindex 293346266..accc33467 100644--- a/openviking/privacy/skill_placeholder.py+++ b/openviking/privacy/skill_placeholder.py@@ -10,18 +10,46 @@ class SkillPrivacyPlaceholderizationResult:sanitized_content: stroriginal_content_blocks: list[str] = field(default_factory=list)replacement_content_blocks: list[str] = field(default_factory=list)+ replaced_values: dict[str, str] = field(default_factory=dict)def build_placeholder(skill_name: str, field_name: str) -> str:return f"{{{{ov_privacy:skill:{skill_name}:{field_name}}}}}"+def _replace_structured_value(content: str, raw_value: str, placeholder: str) -> tuple[str, bool]:+ replacements = (+ (f'"{raw_value}"', f'"{placeholder}"'),+ (f"'{raw_value}'", f"'{placeholder}'"),+ (f": {raw_value}\n", f": {placeholder}\n"),+ (f": {raw_value}\r\n", f": {placeholder}\r\n"),+ (f":{raw_value}\n", f":{placeholder}\n"),+ (f":{raw_value}\r\n", f":{placeholder}\r\n"),+ (f": {raw_value}", f": {placeholder}"),+ (f":{raw_value}", f":{placeholder}"),+ (f"= {raw_value}\n", f"= {placeholder}\n"),+ (f"= {raw_value}\r\n", f"= {placeholder}\r\n"),+ (f"={raw_value}\n", f"={placeholder}\n"),+ (f"={raw_value}\r\n", f"={placeholder}\r\n"),+ (f"= {raw_value}", f"= {placeholder}"),+ (f"={raw_value}", f"={placeholder}"),+ )++ replaced = False+ for old, new in replacements:+ if old in content:+ content = content.replace(old, new)+ replaced = True+ return content, replaced++def placeholderize_skill_content_with_blocks(content: str, skill_name: str, values: dict[str, str]) -> SkillPrivacyPlaceholderizationResult:sanitized = contentoriginal_content_blocks: list[str] = []replacement_content_blocks: list[str] = []+ replaced_values: dict[str, str] = {}replacements = sorted(values.items(), key=lambda item: len(str(item[1])), reverse=True)for field_name, raw_value in replacements:@@ -29,15 +57,17 @@ def placeholderize_skill_content_with_blocks(continueraw_value_str = str(raw_value)placeholder = build_placeholder(skill_name, field_name)- if raw_value_str in sanitized:+ sanitized, replaced = _replace_structured_value(sanitized, raw_value_str, placeholder)+ if replaced:original_content_blocks.append(raw_value_str)replacement_content_blocks.append(placeholder)- sanitized = sanitized.replace(raw_value_str, placeholder)+ replaced_values[field_name] = raw_value_strreturn SkillPrivacyPlaceholderizationResult(sanitized_content=sanitized,original_content_blocks=original_content_blocks,replacement_content_blocks=replacement_content_blocks,+ replaced_values=replaced_values,)diff --git a/tests/server/test_privacy_config_service.py b/tests/server/test_privacy_config_service.pyindex 19d45a888..fb553477f 100644--- a/tests/server/test_privacy_config_service.py+++ b/tests/server/test_privacy_config_service.py@@ -4,7 +4,8 @@import pytestfrom openviking.privacy.skill_extractor import extract_skill_privacy_values-from openviking.server.identity import RequestContext, Role+from openviking.privacy.skill_placeholder import placeholderize_skill_content_with_blocks+from openviking.server.identity import AccountNamespacePolicy, RequestContext, Rolefrom openviking_cli.session.user_id import UserIdentifier@@ -181,3 +182,93 @@ async def test_skill_read_appends_notice_for_extra_configured_keys(service):assert 'api_key: "secret-xyz"' in restoredassert "[Privacy Config Notice]" in restoredassert "Configured but not referenced in content: region=cn" in restored+++async def test_privacy_config_service_uses_policy_aware_user_root(service):+ ctx = RequestContext(+ user=UserIdentifier("acme", "privacy_user_policy", "demo-agent"),+ role=Role.USER,+ namespace_policy=AccountNamespacePolicy(isolate_user_scope_by_agent=True),+ )+ await service.initialize_user_directories(ctx)++ privacy = service.privacy_configs+ await privacy.upsert(+ ctx=ctx,+ category="skill",+ target_key="policy-skill",+ values={"api_key": "secret-1"},+ updated_by=ctx.user.user_id,+ )++ current = await privacy.get_current(ctx, "skill", "policy-skill")+ stored = await service.viking_fs.read_file(+ "viking://user/privacy_user_policy/agent/demo-agent/privacy/skill/policy-skill/current.json",+ ctx=ctx,+ )++ assert current is not None+++async def test_placeholderization_only_replaces_structured_values(monkeypatch):+ content = """region: cn\nnotes: region is cn in docs\ndefault_env=prod\ntext: prod should stay here\n"""+ result = placeholderize_skill_content_with_blocks(+ content,+ "structured-skill",+ {"region": "cn", "env": "prod"},+ )++ assert result.replaced_values == {"region": "cn", "env": "prod"}+ assert result.sanitized_content == (+ "region: {{ov_privacy:skill:structured-skill:region}}\n"+ "notes: region is cn in docs\n"+ "default_env={{ov_privacy:skill:structured-skill:env}}\n"+ "text: prod should stay here\n"+ )++ async def fake_completion_async(_prompt):+ return '{"values": {"api_key": "different-secret"}}'++ monkeypatch.setattr(+ "openviking.privacy.skill_extractor.get_openviking_config",+ lambda: type("Cfg", (), {"vlm": type("VLM", (), {"get_completion_async": staticmethod(fake_completion_async)})()})(),+ )++ content = 'api_key: "secret-xyz"\n'+ result = await extract_skill_privacy_values(+ skill_name="unmatched-skill",+ skill_description="skill with secret",+ content=content,+ )++ assert result.values == {}+ assert result.sanitized_content == content+ assert result.original_content_blocks == []+ assert result.replacement_content_blocks == []+++def test_placeholderization_replaces_end_of_file_values_without_newline():+ result = placeholderize_skill_content_with_blocks(+ "api_key=secret-xyz",+ "eof-skill",+ {"api_key": "secret-xyz"},+ )++ assert result.replaced_values == {"api_key": "secret-xyz"}+ assert result.sanitized_content == "api_key={{ov_privacy:skill:eof-skill:api_key}}"+++def test_placeholderization_replaces_all_structured_occurrences_for_same_value():+ result = placeholderize_skill_content_with_blocks(+ 'api_key: "secret"\nbackup=secret\n',+ "multi-hit-skill",+ {"api_key": "secret"},+ )++ assert result.replaced_values == {"api_key": "secret"}+ assert result.sanitized_content == (+ 'api_key: "{{ov_privacy:skill:multi-hit-skill:api_key}}"\n'+ 'backup={{ov_privacy:skill:multi-hit-skill:api_key}}\n'+ )