Add external resource registration

This commit is contained in:
2026-06-22 13:19:18 +08:00
parent e5cd87789f
commit 12c767cd68
4 changed files with 496 additions and 11 deletions

View File

@ -440,6 +440,61 @@ async def test_upload_resource_creates_attachment_mapping(
assert attachments[0]["source"] == "resource_upload"
@pytest.mark.asyncio
async def test_register_external_resource_does_not_copy_file_and_uses_ingest_uri(
config: GatewayConfig,
repo: MemoryRepository,
) -> None:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources/external",
json={
"user_id": "u_123",
"user_key": user_key,
"app_id": "default",
"project_id": "default",
"filename": "chart.png",
"mime_type": "image/png",
"content_type": "image",
"size_bytes": 9,
"sha256": "sha-chart",
"source_uri": "minio://beaver-user-files/users/u_123/outputs/chart.png",
"ingest_uri": "http://minio.local/presigned/chart.png",
"title": "Chart",
"description": "Generated chart",
},
)
assert response.status_code == 200, response.text
body = response.json()
resource_id = body["resource_id"]
assert body["session_id"] == f"resource:u_123:{resource_id}"
assert body["uri"] == f"resource://u_123/{resource_id}"
assert body["status"] == "extracted"
assert not config.storage_dir.exists()
resource = repo.get_resource(resource_id)
assert resource is not None
assert resource["uri"] == "minio://beaver-user-files/users/u_123/outputs/chart.png"
assert resource["uri_public"] == 0
assert resource["sha256"] == "sha-chart"
assert resource["size_bytes"] == 9
content = backend.add_calls[0]["messages"][0]["content"][0]
assert content["type"] == "image"
assert content["uri"] == "http://minio.local/presigned/chart.png"
assert content["name"] == "chart.png"
assert content["extras"] == {"resource_id": resource_id, "source": "external_resource"}
assert "base64" not in content
attachments = repo.list_attachments_for_session("u_123", body["session_id"])
assert len(attachments) == 1
assert attachments[0]["internal_uri"] == "minio://beaver-user-files/users/u_123/outputs/chart.png"
assert attachments[0]["source"] == "external_resource"
@pytest.mark.asyncio
async def test_upload_resource_uses_current_timestamp(
config: GatewayConfig,
@ -704,6 +759,59 @@ async def test_add_memory_forwards_multimodal_payload_to_backend(
]
@pytest.mark.asyncio
async def test_add_memory_propagates_backend_http_error(
config: GatewayConfig,
) -> None:
backend = FakeBackendClient()
request = httpx.Request("POST", "http://backend.test/api/v1/memory/add")
response = httpx.Response(
415,
request=request,
json={"error": {"message": "LibreOffice conversion failed"}},
)
async def fail_add_memory(payload: dict[str, Any]) -> dict[str, Any]:
backend.add_calls.append(payload)
raise httpx.HTTPStatusError(
"Client error '415 Unsupported Media Type'",
request=request,
response=response,
)
backend.add_memory = fail_add_memory # type: ignore[method-assign]
async with app_client(config, backend) as client:
user_key = await create_user(client)
gateway_response = await client.post(
"/memories/add",
json={
"user_id": "u_123",
"user_key": user_key,
"session_id": "chat:c_backend_415",
"messages": [
{
"sender_id": "u_123",
"role": "user",
"timestamp": 1234567890123,
"content": [
{
"type": "doc",
"uri": "file:///tmp/bad.doc",
"ext": "doc",
"name": "bad.doc",
}
],
}
],
},
)
assert gateway_response.status_code == 415
assert gateway_response.json()["detail"] == {
"error": {"message": "LibreOffice conversion failed"}
}
@pytest.mark.asyncio
async def test_add_memory_creates_uri_attachment_mapping(
config: GatewayConfig,