Add resource upload APIs

This commit is contained in:
2026-05-29 11:47:51 +08:00
parent c173fa45a7
commit 0ab2a35e16
8 changed files with 514 additions and 18 deletions

View File

@ -61,14 +61,21 @@ class FakeAsyncClient:
async def __aexit__(self, exc_type, exc, tb):
return False
async def post(self, path: str, json: dict | None = None) -> FakeResponse:
self.calls.append(("post", self.api_key, self.headers, path, json))
async def post(self, path: str, json: dict | None = None, files: dict | None = None) -> FakeResponse:
if files and "file" in files:
uploaded = files["file"]
files = {"file": uploaded[0] if isinstance(uploaded, tuple) else uploaded}
self.calls.append(("post", self.api_key, self.headers, path, json, files))
return self.responses.pop(0)
async def get(self, path: str) -> FakeResponse:
self.calls.append(("get", self.api_key, self.headers, path, None))
return self.responses.pop(0)
async def delete(self, path: str, params: dict | None = None) -> FakeResponse:
self.calls.append(("delete", self.api_key, self.headers, path, params))
return self.responses.pop(0)
def test_openviking_rejects_unknown_user_credentials():
store = FakeStore()
@ -126,7 +133,7 @@ def test_openviking_create_user_creates_isolated_admin_account():
},
),
]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -152,6 +159,7 @@ def test_openviking_create_user_creates_isolated_admin_account():
{},
"/api/v1/admin/accounts",
{"account_id": "userA_account", "admin_user_id": "userA"},
None,
),
]
@ -175,7 +183,7 @@ def test_openviking_create_user_creates_account_even_when_admin_workspace_exists
},
)
]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -201,6 +209,7 @@ def test_openviking_create_user_creates_account_even_when_admin_workspace_exists
{},
"/api/v1/admin/accounts",
{"account_id": "userB_account", "admin_user_id": "userB"},
None,
)
]
@ -210,7 +219,7 @@ def test_openviking_user_key_auth_is_used_for_session_create():
client.root_key = "root-key"
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"session_id": "sess-2"}})]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -229,6 +238,7 @@ def test_openviking_user_key_auth_is_used_for_session_create():
{},
"/api/v1/sessions",
{"session_id": "sess-2"},
None,
)
]
@ -237,7 +247,7 @@ def test_openviking_find_uses_current_identity_memory_scope():
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -255,6 +265,7 @@ def test_openviking_find_uses_current_identity_memory_scope():
{},
"/api/v1/search/find",
{"query": "咖啡", "target_uri": "viking://user/tom/memories/", "limit": 5},
None,
)
]
@ -263,7 +274,7 @@ def test_openviking_search_uses_fixed_user_memory_target_with_level_and_score_th
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -287,6 +298,7 @@ def test_openviking_search_uses_fixed_user_memory_target_with_level_and_score_th
"level": 3,
"score_threshold": 0.7,
},
None,
)
]
@ -295,7 +307,7 @@ def test_openviking_search_accepts_custom_target_uri():
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -326,6 +338,7 @@ def test_openviking_search_accepts_custom_target_uri():
"level": 3,
"score_threshold": 0.7,
},
None,
)
]
@ -334,7 +347,7 @@ def test_openviking_profile_search_uses_user_memory_target_and_level():
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -357,6 +370,7 @@ def test_openviking_profile_search_uses_user_memory_target_and_level():
"level": 2,
"target_uri": "viking://user/memories",
},
None,
)
]
@ -376,7 +390,7 @@ def test_openviking_get_session_context_uses_user_key_auth():
},
)
]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -420,7 +434,7 @@ def test_openviking_commit_keeps_no_recent_live_messages():
},
)
]
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
@ -446,6 +460,115 @@ def test_openviking_commit_keeps_no_recent_live_messages():
{},
"/api/v1/sessions/sess-1/commit",
{"keep_recent_count": 0},
None,
)
]
def test_openviking_upload_temp_file_posts_multipart(tmp_path):
path = tmp_path / "report.pdf"
path.write_bytes(b"pdf-bytes")
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [
FakeResponse(
200,
{"status": "ok", "result": {"temp_file_id": "upload_report.pdf"}},
)
]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
extra_headers or {},
)
credential = client.user_credential("tom-key", "tom")
result = asyncio.run(client.upload_temp_file(credential, path))
assert result == {"status": "ok", "result": {"temp_file_id": "upload_report.pdf"}}
assert calls == [
(
"post",
"tom-key",
{},
"/api/v1/resources/temp_upload",
None,
{"file": "report.pdf"},
)
]
def test_openviking_add_resource_posts_url_payload():
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"uri": "viking://resources/tom/images/photo.png"}})]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
extra_headers or {},
)
credential = client.user_credential("tom-key", "tom")
result = asyncio.run(
client.add_resource(
credential,
path="https://example.com/photo.png",
to="viking://resources/tom/images/photo.png",
reason="上传远程图片",
wait=True,
directly_upload_media=True,
)
)
assert result == {"status": "ok", "result": {"uri": "viking://resources/tom/images/photo.png"}}
assert calls == [
(
"post",
"tom-key",
{},
"/api/v1/resources",
{
"path": "https://example.com/photo.png",
"to": "viking://resources/tom/images/photo.png",
"reason": "上传远程图片",
"wait": True,
"directly_upload_media": True,
},
None,
)
]
def test_openviking_delete_resource_sends_uri_and_recursive_flag():
client = OpenVikingMemorySystemClient(store=FakeStore())
calls = []
responses = [FakeResponse(200, {"status": "ok", "result": {"estimated_deleted_count": 4}})]
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
calls,
responses,
api_key,
extra_headers or {},
)
credential = client.user_credential("tom-key", "tom")
result = asyncio.run(
client.delete_resource(
credential,
uri="viking://resources/tom/files/report.pdf",
recursive=True,
)
)
assert result == {"status": "ok", "result": {"estimated_deleted_count": 4}}
assert calls == [
(
"delete",
"tom-key",
{},
"/api/v1/fs",
{"uri": "viking://resources/tom/files/report.pdf", "recursive": "true"},
)
]

View File

@ -13,6 +13,7 @@ def test_memory_system_server_exposes_routes():
}
assert {"GET", "POST"} <= context_methods
assert "/memory-system/search" in paths
assert "/memory-system/resources" in paths
assert "/memory-system/users/{user_id}/profile" in paths
task_methods = {
method
@ -28,6 +29,13 @@ def test_memory_system_server_exposes_routes():
}
assert {"GET", "POST"} <= task_methods
assert {"GET", "POST"} <= profile_methods
resource_methods = {
method
for route in app.routes
if getattr(route, "path", "") == "/memory-system/resources"
for method in getattr(route, "methods", set())
}
assert {"DELETE", "POST"} <= resource_methods
def test_memory_system_messages_does_not_require_account_key_header():

View File

@ -1,6 +1,6 @@
import asyncio
from memory_system_api.schemas import MessageIngestRequest, SearchRequest, SessionContextRequest
from memory_system_api.schemas import MessageIngestRequest, ResourceUploadRequest, SearchRequest, SessionContextRequest
from memory_system_api.service import MemorySystemService
@ -89,6 +89,37 @@ class FakeOpenViking:
self.calls.append(("commit_session", user_key, session_id))
return {"status": "ok", "result": {"task_id": "task-1", "archive_uri": "archive-1"}}
async def upload_temp_file(self, user_key: str, path) -> dict:
self.calls.append(("upload_temp_file", user_key, str(path)))
return {"status": "ok", "result": {"temp_file_id": "upload_report.pdf"}}
async def add_resource(
self,
user_key: str,
*,
to: str,
reason: str | None,
wait: bool,
directly_upload_media: bool,
path: str | None = None,
temp_file_id: str | None = None,
) -> dict:
self.calls.append((
"add_resource",
user_key,
path,
temp_file_id,
to,
reason,
wait,
directly_upload_media,
))
return {"status": "ok", "result": {"uri": to}}
async def delete_resource(self, user_key: str, uri: str, recursive: bool = True) -> dict:
self.calls.append(("delete_resource", user_key, uri, recursive))
return {"status": "ok", "result": {"uri": uri, "estimated_deleted_count": 4}}
class FakeEverOS:
def __init__(self, fail_on_append: bool = False):
@ -218,6 +249,91 @@ def test_create_user_delegates_to_openviking_only():
assert everos.calls == []
def test_upload_resource_with_url_delegates_directly_to_openviking_add_resource():
openviking = FakeOpenViking()
service = MemorySystemService(openviking=openviking, everos=FakeEverOS())
response = asyncio.run(service.upload_resource(ResourceUploadRequest(
user_id="tom",
user_key="tom-key",
path="https://example.com/images/photo.png",
to="viking://resources/tom/images/photo.png",
reason="上传远程图片",
)))
assert response.status == "success"
assert response.resource == {"status": "ok", "result": {"uri": "viking://resources/tom/images/photo.png"}}
assert openviking.calls == [
("credential_for_user", "tom", "tom-key", None),
(
"add_resource",
"key-tom",
"https://example.com/images/photo.png",
None,
"viking://resources/tom/images/photo.png",
"上传远程图片",
True,
True,
),
]
def test_upload_resource_with_local_path_uploads_temp_file_first(tmp_path):
path = tmp_path / "report.pdf"
path.write_bytes(b"pdf-bytes")
openviking = FakeOpenViking()
service = MemorySystemService(openviking=openviking, everos=FakeEverOS())
response = asyncio.run(service.upload_resource(ResourceUploadRequest(
user_id="tom",
user_key="tom-key",
path=str(path),
to="viking://resources/tom/files/report.pdf",
reason="上传本地文件",
)))
assert response.status == "success"
assert response.resource == {"status": "ok", "result": {"uri": "viking://resources/tom/files/report.pdf"}}
assert openviking.calls == [
("credential_for_user", "tom", "tom-key", None),
("upload_temp_file", "key-tom", str(path)),
(
"add_resource",
"key-tom",
None,
"upload_report.pdf",
"viking://resources/tom/files/report.pdf",
"上传本地文件",
True,
True,
),
]
def test_delete_resource_delegates_to_openviking_only():
openviking = FakeOpenViking()
everos = FakeEverOS()
service = MemorySystemService(openviking=openviking, everos=everos)
response = asyncio.run(service.delete_resource(
user_id="tom",
user_key="tom-key",
uri="viking://resources/tom/files/report.pdf",
recursive=True,
))
assert response.status == "success"
assert response.resource == {
"status": "ok",
"result": {"uri": "viking://resources/tom/files/report.pdf", "estimated_deleted_count": 4},
}
assert openviking.calls == [
("credential_for_user", "tom", "tom-key", None),
("delete_resource", "key-tom", "viking://resources/tom/files/report.pdf", True),
]
assert everos.calls == []
def test_search_removes_vectors_from_items_and_backend_results():
service = MemorySystemService(openviking=FakeOpenViking(), everos=FakeEverOSWithVector())