Skip to content

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

feat(bot): support exact add-resource targets (#4015)

Onefly39f5f4ae46c0
diff --git a/bot/tests/test_openviking_add_resource.py b/bot/tests/test_openviking_add_resource.py
new file mode 100644
index 000000000..94f88199a
--- /dev/null
+++ b/bot/tests/test_openviking_add_resource.py
@@ -0,0 +1,72 @@
+from unittest.mock import AsyncMock
+
+import pytest
+from vikingbot.agent.tools.base import ToolContext
+from vikingbot.agent.tools.ov_file import VikingAddResourceTool
+from vikingbot.openviking_mount.ov_server import VikingClient
+
+
+class _FakeClient:
+ def __init__(self, root_uri):
+ self.root_uri = root_uri
+ self.calls = []
+
+ async def add_resource(self, path, description, to=None):
+ self.calls.append((path, description, to))
+ return {"root_uri": self.root_uri}
+
+
+def test_add_resource_schema_exposes_optional_target_uri():
+ tool = VikingAddResourceTool()
+
+ assert tool.parameters["properties"]["to"] == {
+ "type": "string",
+ "description": "Optional exact target URI under viking://resources/. When omitted, OpenViking chooses the resource URI.",
+ }
+ assert "to" not in tool.parameters["required"]
+ assert tool.validate_params({"path": "https://example.com/doc", "description": "doc"}) == []
+
+
+ ("target_uri", "root_uri"),
+ [
+ (None, "viking://resources/doc"),
+ ("viking://resources/feishu/doc/config", "viking://resources/feishu/doc/config"),
+ ],
+)
+async def test_add_resource_tool_forwards_optional_target(monkeypatch, target_uri, root_uri):
+ tool = VikingAddResourceTool()
+ client = _FakeClient(root_uri)
+
+ async def _fake_get_client(_tool_context):
+ return client
+
+ monkeypatch.setattr(tool, "_get_client", _fake_get_client)
+
+ kwargs = {"path": "https://example.com/doc", "description": "conversation export"}
+ if target_uri is not None:
+ kwargs["to"] = target_uri
+ result = await tool.execute(ToolContext(), **kwargs)
+
+ assert client.calls == [("https://example.com/doc", "conversation export", target_uri)]
+ assert result == f"Successfully added resource: {root_uri}"
+
+
[email protected]("target_uri", [None, "viking://resources/feishu/doc/config"])
+async def test_viking_client_add_resource_forwards_optional_target(target_uri):
+ expected_uri = target_uri or "viking://resources/doc"
+ sdk_client = AsyncMock()
+ sdk_client.add_resource.return_value = {"root_uri": expected_uri}
+ client = object.__new__(VikingClient)
+ client.client = sdk_client
+
+ result = await client.add_resource("/tmp/doc.md", "conversation export", to=target_uri)
+
+ sdk_client.add_resource.assert_awaited_once_with(
+ path="/tmp/doc.md",
+ to=target_uri,
+ reason="conversation export",
+ )
+ assert result["root_uri"] == expected_uri
diff --git a/bot/vikingbot/agent/tools/ov_file.py b/bot/vikingbot/agent/tools/ov_file.py
index 07b0999ba..8acda7762 100644
--- a/bot/vikingbot/agent/tools/ov_file.py
+++ b/bot/vikingbot/agent/tools/ov_file.py
@@ -514,6 +514,10 @@ class VikingAddResourceTool(OVFileTool):
"properties": {
"path": {"type": "string", "description": "Url or local file path"},
"description": {"type": "string", "description": "Description of the resource"},
+ "to": {
+ "type": "string",
+ "description": "Optional exact target URI under viking://resources/. When omitted, OpenViking chooses the resource URI.",
+ },
},
"required": ["path", "description"],
}
@@ -523,6 +527,7 @@ class VikingAddResourceTool(OVFileTool):
tool_context: "ToolContext",
path: str,
description: str,
+ to: Optional[str] = None,
**kwargs: Any,
) -> str:
client = None
@@ -535,7 +540,7 @@ class VikingAddResourceTool(OVFileTool):
return f"Error: Not a file: {path}"
client = await self._get_client(tool_context)
- result = await client.add_resource(path, description)
+ result = await client.add_resource(path, description, to=to)
if result:
root_uri = result.get("root_uri", "")
diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.py
index 148737be2..d31292c48 100644
--- a/bot/vikingbot/openviking_mount/ov_server.py
+++ b/bot/vikingbot/openviking_mount/ov_server.py
@@ -581,9 +581,14 @@ class VikingClient:
return await self.client.find(query, target_uri=target_uri, **kwargs)
return await self.client.find(query, **kwargs)
- async def add_resource(self, local_path: str, desc: str) -> Optional[Dict[str, Any]]:
+ async def add_resource(
+ self,
+ local_path: str,
+ desc: str,
+ to: Optional[str] = None,
+ ) -> Optional[Dict[str, Any]]:
"""添加资源到 Viking"""
- result = await self.client.add_resource(path=local_path, reason=desc)
+ result = await self.client.add_resource(path=local_path, to=to, reason=desc)
return result
async def list_resources(