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

@ -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())