Skip to content

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

feat: support local file to external-queue (#4412)

chenxiaobin-monkey5e0754f37150
diff --git a/openviking/parse/parser_router.py b/openviking/parse/parser_router.py
index 1f24ab1ef..21a8d50fe 100644
--- a/openviking/parse/parser_router.py
+++ b/openviking/parse/parser_router.py
@@ -155,6 +155,11 @@ class ParserRouter:
raise ValueError("source is not routed to UnderstandingAPI")
return await self._get_understanding_api().submit_url(str(source_path), **kwargs)
+ async def upload_file(self, source: Union[str, Path, LocalResource]) -> str:
+ """Upload a local source file and return only the external Files API file_id."""
+ source_path = self._extract_source_path(source)
+ return await self._get_understanding_api().upload_file(source_path)
+
def _extract_source_path(self, source: Union[str, Path, LocalResource]) -> Union[str, Path]:
"""Extract a filesystem path from the source."""
if hasattr(source, "path"):
diff --git a/openviking/parse/understanding_api.py b/openviking/parse/understanding_api.py
index 8e80a3a2f..bf656c6f8 100644
--- a/openviking/parse/understanding_api.py
+++ b/openviking/parse/understanding_api.py
@@ -43,6 +43,7 @@ from openviking_cli.utils.logger import get_logger
logger = get_logger(__name__)
PREPARED_RESPONSE_ID_ARG = "understanding_response_id"
+PREPARED_FILE_ID_ARG = "understanding_file_id"
class UnderstandingAPI(BaseParser):
@@ -97,13 +98,22 @@ class UnderstandingAPI(BaseParser):
if not isinstance(prepared_response_id, str) or not prepared_response_id.strip():
raise ValueError(f"{PREPARED_RESPONSE_ID_ARG} must be a non-empty string")
prepared_response_id = prepared_response_id.strip()
+ prepared_file_id = kwargs.get(PREPARED_FILE_ID_ARG)
+ if prepared_file_id is not None:
+ if not isinstance(prepared_file_id, str) or not prepared_file_id.strip():
+ raise ValueError(f"{PREPARED_FILE_ID_ARG} must be a non-empty string")
+ prepared_file_id = prepared_file_id.strip()
+ if prepared_response_id and prepared_file_id:
+ raise ValueError(
+ f"{PREPARED_RESPONSE_ID_ARG} and {PREPARED_FILE_ID_ARG} are mutually exclusive"
+ )
# Only a directly routed Feishu URL uses the Lark protocol. Accessor
# output is an existing local Markdown file and must stay local.
is_feishu_url = self._is_feishu_url(source_str)
lark_file = (
await self._resolve_lark_file(kwargs)
- if is_feishu_url and not prepared_response_id
+ if is_feishu_url and not prepared_response_id and not prepared_file_id
else None
)
@@ -118,6 +128,8 @@ class UnderstandingAPI(BaseParser):
("http://", "https://")
):
url = original_source
+ elif prepared_file_id:
+ local_path = None
else:
local_path = source_path
@@ -132,13 +144,15 @@ class UnderstandingAPI(BaseParser):
"base": "bitable",
}.get(path_parts[0], path_parts[0])
else:
- inferred_name = local_path.name if local_path is not None else ""
- if local_path is None or not local_path.is_file():
+ inferred_name = local_path.name if local_path is not None else Path(source_str).name
+ if not prepared_file_id and (local_path is None or not local_path.is_file()):
raise ValueError(
"UnderstandingAPI supports http(s) URLs or local files. "
"Got an invalid local file path."
)
- doc_type = resolved_extension or local_path.suffix.lower().lstrip(".")
+ doc_type = resolved_extension or (
+ local_path.suffix.lower().lstrip(".") if local_path is not None else ""
+ )
effective_name = str(display_name or inferred_name or "resource")
doc_name = Path(effective_name).stem or "resource"
@@ -148,6 +162,9 @@ class UnderstandingAPI(BaseParser):
if prepared_response_id:
response_id = prepared_response_id
+ elif prepared_file_id:
+ task_meta["file_id"] = prepared_file_id
+ response_obj = await self._create_response_for_file(file_id=prepared_file_id)
elif url is None and local_path is not None:
file_obj = await self._create_file(local_path=local_path)
file_id = file_obj.get("id")
@@ -237,17 +254,21 @@ class UnderstandingAPI(BaseParser):
logger.info("[UnderstandingAPI] done")
return result
- async def submit_file(self, source: Union[str, Path]) -> str:
- """Upload a local file and submit it without retaining the local artifact."""
+ async def upload_file(self, source: Union[str, Path]) -> str:
+ """Upload a local file and return a durable Files API file_id."""
local_path = Path(source)
if not local_path.is_file():
- raise ValueError("UnderstandingAPI file submission requires an existing local file")
+ raise ValueError("UnderstandingAPI file upload requires an existing local file")
file_obj = await self._create_file(local_path=local_path)
file_id = file_obj.get("id")
if not file_id:
raise RuntimeError(f"files api missing file_id: {self._safe_error_summary(file_obj)}")
+ return str(file_id)
+ async def submit_file(self, source: Union[str, Path]) -> str:
+ """Upload a local file and submit it without retaining the local artifact."""
+ file_id = await self.upload_file(source)
response_obj = await self._create_response_for_file(file_id=str(file_id))
response_id = response_obj.get("id")
if not response_id:
diff --git a/openviking/server/resource_ingest.py b/openviking/server/resource_ingest.py
index 08fb487ba..25937f817 100644
--- a/openviking/server/resource_ingest.py
+++ b/openviking/server/resource_ingest.py
@@ -58,6 +58,7 @@ async def ingest_temp_upload(
processing_mode=processing_mode,
allow_local_path_resolution=True,
enforce_public_remote_targets=True,
+ temp_file_id=temp_file_id,
args=ingest_args,
tags=tags,
tag_mode=tag_mode,
diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.py
index d00e31109..1ab5dd24f 100644
--- a/openviking/service/resource_service.py
+++ b/openviking/service/resource_service.py
@@ -127,6 +127,7 @@ _ADD_RESOURCE_ARGS_RESERVED_FIELDS = frozenset(
"telemetry",
"request_validator",
"understanding_response_id",
+ "understanding_file_id",
"parser_backend",
"resolved_extension",
"defer_post_processing",
@@ -150,6 +151,7 @@ _INTERNAL_INGESTION_FIELDS = frozenset(
"to_is_directory",
"watch_auth_state",
"understanding_response_id",
+ "understanding_file_id",
"parser_backend",
"resolved_extension",
"prepared_resource",
@@ -179,6 +181,7 @@ class _SourcePlan:
task_auth: Dict[str, Any] = field(repr=False)
staged_source: Optional["StagedSource"] = None
understanding_response_id: Optional[str] = None
+ understanding_file_id: Optional[str] = None
defer_unnamed_target: bool = False
@@ -235,6 +238,7 @@ class ResourceService:
"parser_backend",
"resolved_extension",
"understanding_response_id",
+ "understanding_file_id",
"temp_file_id",
}:
continue
@@ -614,7 +618,7 @@ class ResourceService:
legacy_backend = normalize_parser_backend(queued_args.pop("parser_backend", None))
parser_backend = legacy_backend or (
ParserBackend.UNDERSTANDING
- if msg.understanding_response_id is not None
+ if msg.understanding_response_id is not None or msg.understanding_file_id is not None
else ParserBackend.INTERNAL
)
internal_kwargs: Dict[str, Any] = {"parser_backend": parser_backend}
@@ -662,6 +666,10 @@ class ResourceService:
from openviking.parse.understanding_api import PREPARED_RESPONSE_ID_ARG
internal_kwargs[PREPARED_RESPONSE_ID_ARG] = msg.understanding_response_id
+ if msg.understanding_file_id is not None:
+ from openviking.parse.understanding_api import PREPARED_FILE_ID_ARG
+
+ internal_kwargs[PREPARED_FILE_ID_ARG] = msg.understanding_file_id
result = await self._execute_resource_ingestion(
path=msg.path,
ctx=ctx,
@@ -739,7 +747,7 @@ class ResourceService:
watch_auth_state = None
auth_kwargs = (
{FEISHU_ACCESS_TOKEN_ARG: token.strip()}
- if msg.understanding_response_id is None
+ if msg.understanding_response_id is None and msg.understanding_file_id is None
else {}
)
return auth_kwargs, watch_auth_state
@@ -794,6 +802,7 @@ class ResourceService:
task_auth: Dict[str, Any] = {}
staged_source = None
understanding_response_id = None
+ understanding_file_id = None
defer_unnamed_target = False
if git_source:
@@ -876,12 +885,17 @@ class ResourceService:
mode is ParseMode.DEFAULT
and self._resource_processor.should_use_understanding_api(prepared)
):
- understanding_response_id = (
- await self._resource_processor.submit_understanding(
- prepared,
- **processor_kwargs,
+ if processor_kwargs.get("temp_file_id"):
+ understanding_file_id = (
+ await self._resource_processor.upload_understanding_file(prepared)
+ )
+ else:
+ understanding_response_id = (
+ await self._resource_processor.submit_understanding(
+ prepared,
+ **processor_kwargs,
+ )
)
- )
else:
staged_source = await stage_source(
prepared,
@@ -898,6 +912,7 @@ class ResourceService:
task_auth=task_auth,
staged_source=staged_source,
understanding_response_id=understanding_response_id,
+ understanding_file_id=understanding_file_id,
defer_unnamed_target=defer_unnamed_target,
)
@@ -990,10 +1005,17 @@ class ResourceService:
defer_candidate_resolution=defer_candidate_resolution,
)
lock_handoff = await self._lock_to_handoff_payload(resource_lock)
+ message_path = plan.path
+ if processor_kwargs.get("temp_file_id") and plan.understanding_file_id is not None:
+ message_path = (
+ plan.source_identity.source_name
+ or plan.source_identity.source_path
+ or "uploaded-file"
+ )
msg = AddResourceMsg(
task_id=str(uuid4()),
job_phase=AddResourcePhase.SOURCE,
- path=plan.path,
+ path=message_path,
source_path=(plan.source_identity.source_name or "")
if processor_kwargs.get("temp_file_id")
else plan.source_identity.source_path or plan.path,
@@ -1036,6 +1058,7 @@ class ResourceService:
args=plan.processor_args,
defer_target_resolution=defer_target_resolution,
understanding_response_id=plan.understanding_response_id,
+ understanding_file_id=plan.understanding_file_id,
internal_task=internal_task,
)
@@ -1046,6 +1069,7 @@ class ResourceService:
queue_name = (
QueueManager.EXTERNAL_PARSE
if plan.understanding_response_id is not None
+ or plan.understanding_file_id is not None
else QueueManager.ADD_RESOURCE
)
enqueue_lock = resource_lock
diff --git a/openviking/storage/queuefs/add_resource_msg.py b/openviking/storage/queuefs/add_resource_msg.py
index ff6f1b564..f4b44b80f 100644
--- a/openviking/storage/queuefs/add_resource_msg.py
+++ b/openviking/storage/queuefs/add_resource_msg.py
@@ -51,6 +51,7 @@ class AddResourceMsg:
skip_watch_management: bool = True
defer_target_resolution: bool = False
understanding_response_id: Optional[str] = None
+ understanding_file_id: Optional[str] = None
processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE
parse_mode: str = "default"
tags: Optional[list[str]] = None
@@ -70,10 +71,20 @@ class AddResourceMsg:
if phase is AddResourcePhase.SOURCE and self.prepared is not None:
raise ValueError("source jobs cannot contain prepared post-process data")
if self.prepared is not None and (
- self.staged_source is not None or self.understanding_response_id is not None
+ self.staged_source is not None
+ or self.understanding_response_id is not None
+ or self.understanding_file_id is not None
):
raise ValueError("post_process jobs cannot contain source payloads")
- if self.staged_source is not None and self.understanding_response_id is not None:
+ source_payload_count = sum(
+ payload is not None
+ for payload in (
+ self.staged_source,
+ self.understanding_response_id,
+ self.understanding_file_id,
+ )
+ )
+ if source_payload_count > 1:
raise ValueError("source jobs cannot contain multiple source payloads")
self.job_phase = phase
@@ -171,6 +182,11 @@ class AddResourceMsg:
if isinstance(data.get("understanding_response_id"), str)
else None
),
+ understanding_file_id=(
+ data.get("understanding_file_id")
+ if isinstance(data.get("understanding_file_id"), str)
+ else None
+ ),
processing_mode=data.get("processing_mode", DEFAULT_PROCESSING_MODE),
parse_mode=str(data.get("parse_mode") or "default"),
tags=(list(data["tags"]) if isinstance(data.get("tags"), list) else None),
diff --git a/openviking/utils/media_processor.py b/openviking/utils/media_processor.py
index a70bdd48e..7d51e0e4a 100644
--- a/openviking/utils/media_processor.py
+++ b/openviking/utils/media_processor.py
@@ -158,6 +158,9 @@ class UnifiedResourceProcessor:
async def submit_understanding(self, source: str | Path | LocalResource, **kwargs) -> str:
return await self._get_parser_router().submit(source, **kwargs)
+ async def upload_understanding_file(self, source: str | Path | LocalResource) -> str:
+ return await self._get_parser_router().upload_file(source)
+
@staticmethod
def _set_resolved_identity(resource: LocalResource, source_name: Optional[str]) -> None:
meta = resource.meta
@@ -361,7 +364,12 @@ class UnifiedResourceProcessor:
@staticmethod
def _has_prepared_understanding_response(kwargs: dict) -> bool:
- from openviking.parse.understanding_api import PREPARED_RESPONSE_ID_ARG
+ from openviking.parse.understanding_api import (
+ PREPARED_FILE_ID_ARG,
+ PREPARED_RESPONSE_ID_ARG,
+ )
- value = kwargs.get(PREPARED_RESPONSE_ID_ARG)
- return isinstance(value, str) and bool(value.strip())
+ return any(
+ isinstance(kwargs.get(key), str) and bool(kwargs[key].strip())
+ for key in (PREPARED_RESPONSE_ID_ARG, PREPARED_FILE_ID_ARG)
+ )
diff --git a/openviking/utils/resource_processor.py b/openviking/utils/resource_processor.py
index 6f498eb16..2ed846fa6 100644
--- a/openviking/utils/resource_processor.py
+++ b/openviking/utils/resource_processor.py
@@ -138,6 +138,9 @@ class ResourceProcessor:
async def submit_understanding(self, source: Union[str, "LocalResource"], **kwargs) -> str:
return await self._get_media_processor().submit_understanding(source, **kwargs)
+ async def upload_understanding_file(self, source: Union[str, "LocalResource"]) -> str:
+ return await self._get_media_processor().upload_understanding_file(source)
+
async def build_index(
self, resource_uris: List[str], ctx: RequestContext, **kwargs
) -> Dict[str, Any]:
diff --git a/tests/parse/test_feishu_parser_api.py b/tests/parse/test_feishu_parser_api.py
index 118c29bc3..76b36fc94 100644
--- a/tests/parse/test_feishu_parser_api.py
+++ b/tests/parse/test_feishu_parser_api.py
@@ -8,7 +8,11 @@ from unittest.mock import AsyncMock, Mock
import pytest
from openviking.parse.accessors.base import LocalResource, SourceType
-from openviking.parse.understanding_api import PREPARED_RESPONSE_ID_ARG, UnderstandingAPI
+from openviking.parse.understanding_api import (
+ PREPARED_FILE_ID_ARG,
+ PREPARED_RESPONSE_ID_ARG,
+ UnderstandingAPI,
+)
from openviking.server.identity import RequestContext, Role
from openviking.service.resource_service import ResourceService
from openviking.service.task_tracker import TaskStatus
@@ -741,6 +745,7 @@ async def test_uat_producer_cancellation_respects_queue_ownership(
("field", "value"),
[
(PREPARED_RESPONSE_ID_ARG, "response-1"),
+ (PREPARED_FILE_ID_ARG, "file-1"),
("parser_backend", "understanding"),
("resolved_extension", ".pdf"),
],
@@ -793,6 +798,43 @@ async def test_add_resource_job_defers_target_and_expands_prepared_response():
assert result["root_uri"] == "viking://resources/真实文档标题"
+async def test_add_resource_job_expands_prepared_file_id():
+ service = ResourceService()
+ service._execute_resource_ingestion = AsyncMock(
+ return_value={
+ "status": "success",
+ "root_uri": "viking://resources/uploaded",
+ }
+ )
+ msg = AddResourceMsg(
+ task_id="task-1",
+ path="/tmp/upload_already_cleaned.pdf",
+ source_name="uploaded.pdf",
+ root_uri="viking://resources/uploaded",
+ account_id="account-1",
+ user_id="user-1",
+ role="user",
+ understanding_file_id="file-1",
+ )
+ ctx = RequestContext(
+ user=UserIdentifier("account-1", "user-1"),
+ role=Role.USER,
+ )
+
+ result = await service.execute_add_resource_job(
+ msg,
+ ctx=ctx,
+ resource_lock=None,
+ stage_callback=AsyncMock(),
+ )
+
+ call = service._execute_resource_ingestion.await_args
+ assert call.kwargs[PREPARED_FILE_ID_ARG] == "file-1"
+ assert call.kwargs["parser_backend"] == "understanding"
+ assert result["root_uri"] == "viking://resources/uploaded"
+
+
@pytest.mark.asyncio
async def test_add_resource_job_expands_parser_args():
service = ResourceService()
diff --git a/tests/parse/test_parser_router.py b/tests/parse/test_parser_router.py
index 48157f0f1..a068d6f60 100644
--- a/tests/parse/test_parser_router.py
+++ b/tests/parse/test_parser_router.py
@@ -7,7 +7,7 @@ from openviking.parse.accessors.base import LocalResource, SourceType
from openviking.parse.parser_router import ParserRouter
from openviking.parse.parsers.media.utils import MPEG_TS_PACKET_SIZE, MPEG_TS_PROBE_BYTES
from openviking.parse.registry import ParserRegistry
-from openviking.parse.understanding_api import PREPARED_RESPONSE_ID_ARG
+from openviking.parse.understanding_api import PREPARED_FILE_ID_ARG, PREPARED_RESPONSE_ID_ARG
from openviking.utils.media_processor import UnifiedResourceProcessor
@@ -331,3 +331,29 @@ async def test_prepared_understanding_response_bypasses_remote_redownload():
assert parser_router.parse.await_args.args == ("https://example.com/expiring-download",)
assert parser_router.parse.await_args.kwargs["parser_backend"] == "understanding"
assert parser_router.parse.await_args.kwargs[PREPARED_RESPONSE_ID_ARG] == "response-1"
+
+
+async def test_prepared_understanding_file_bypasses_local_reopen():
+ result = object()
+ parser_router = SimpleNamespace(parse=AsyncMock(return_value=result))
+ accessor = SimpleNamespace(
+ access=AsyncMock(side_effect=AssertionError("prepared file must not reopen source"))
+ )
+ processor = UnifiedResourceProcessor(vlm_processor=object())
+ processor._parser_router = parser_router
+ processor._accessor_registry = accessor
+
+ actual = await processor.process(
+ "/tmp/upload_already_cleaned.pdf",
+ source_name="uploaded.pdf",
+ resolved_extension=".pdf",
+ **{PREPARED_FILE_ID_ARG: "file-1"},
+ )
+
+ assert actual is result
+ accessor.access.assert_not_awaited()
+ parser_router.parse.assert_awaited_once()
+ assert parser_router.parse.await_args.args == ("/tmp/upload_already_cleaned.pdf",)
+ assert parser_router.parse.await_args.kwargs["parser_backend"] == "understanding"
+ assert parser_router.parse.await_args.kwargs[PREPARED_FILE_ID_ARG] == "file-1"
diff --git a/tests/parse/test_understanding_api.py b/tests/parse/test_understanding_api.py
index 9b92590c5..ef8a9389e 100644
--- a/tests/parse/test_understanding_api.py
+++ b/tests/parse/test_understanding_api.py
@@ -3,7 +3,7 @@ from unittest.mock import AsyncMock
import pytest
-from openviking.parse.understanding_api import UnderstandingAPI
+from openviking.parse.understanding_api import PREPARED_FILE_ID_ARG, UnderstandingAPI
from openviking_cli.exceptions import InvalidArgumentError
@@ -57,7 +57,7 @@ async def test_parse_uses_downloaded_file_and_resolved_extension(monkeypatch, tm
@pytest.mark.asyncio
-async def test_submit_file_validates_input_and_returns_response_id(tmp_path):
+async def test_upload_file_validates_input_and_returns_file_id(tmp_path):
empty_source = tmp_path / "empty.pdf"
empty_source.touch()
api = UnderstandingAPI.__new__(UnderstandingAPI)
@@ -66,20 +66,56 @@ async def test_submit_file_validates_input_and_returns_response_id(tmp_path):
InvalidArgumentError,
match="Understanding parser does not support empty files",
) as exc_info:
- await api.submit_file(empty_source)
+ await api.upload_file(empty_source)
assert exc_info.value.code == "INVALID_ARGUMENT"
source = tmp_path / "download.pdf"
source.write_bytes(b"%PDF-1.7")
api._create_file = AsyncMock(return_value={"id": "file-1"})
- api._create_response_for_file = AsyncMock(return_value={"id": "response-1"})
- response_id = await api.submit_file(source)
+ file_id = await api.upload_file(source)
- assert response_id == "response-1"
+ assert file_id == "file-1"
api._create_file.assert_awaited_once_with(local_path=source)
+
+
+async def test_parse_prepared_file_id_creates_response_and_polls(monkeypatch, tmp_path):
+ zip_path = tmp_path / "result.zip"
+ zip_path.write_bytes(b"zip")
+ api = UnderstandingAPI.__new__(UnderstandingAPI)
+ api._video_exts = {"mp4"}
+ api._audio_exts = {"mp3"}
+ api._image_exts = {"png"}
+ api._create_file = AsyncMock(side_effect=AssertionError("prepared file must not reupload"))
+ api._create_response_for_file = AsyncMock(return_value={"id": "response-1"})
+
+ async def poll_response(*, response_id):
+ assert response_id == "response-1"
+ return {"status": "completed"}
+
+ monkeypatch.setattr(api, "_poll_response", poll_response)
+ monkeypatch.setattr(api, "_extract_zip_url", lambda _: "https://example.com/result.zip")
+ monkeypatch.setattr(api, "_download_zip", lambda _: _return(zip_path))
+ monkeypatch.setattr(
+ api,
+ "_unpack_zip_to_temp_dir",
+ lambda **_: _return("viking://temp/result"),
+ )
+
+ result = await api.parse(
+ "/tmp/upload_already_cleaned.pdf",
+ source_name="uploaded.pdf",
+ resolved_extension=".pdf",
+ **{PREPARED_FILE_ID_ARG: "file-1"},
+ )
+
api._create_response_for_file.assert_awaited_once_with(file_id="file-1")
+ assert result.meta["file_id"] == "file-1"
+ assert result.meta["response_id"] == "response-1"
+ assert result.source_format == "pdf"
+ api._create_file.assert_not_awaited()
async def _return(value):
diff --git a/tests/service/test_resource_service_understanding_routing.py b/tests/service/test_resource_service_understanding_routing.py
index 21243ffc0..7e7282232 100644
--- a/tests/service/test_resource_service_understanding_routing.py
+++ b/tests/service/test_resource_service_understanding_routing.py
@@ -210,3 +210,106 @@ async def test_remote_mpeg_ts_url_queues_understanding_after_prepare(
assert not downloaded.exists()
agfs.pathlock_to_handoff.assert_awaited_once_with(lock)
agfs.pathlock_handoff.assert_awaited_once_with(lock)
+
+
[email protected]("temp_file_id", ["upload_audio.m4a", "shared_audio"])
+async def test_temp_uploaded_file_queues_external_parse_with_file_id(
+ monkeypatch,
+ tmp_path,
+ temp_file_id,
+):
+ """Upload snapshots keep only the external file_id before entering ExternalParse."""
+ ctx = RequestContext(
+ user=UserIdentifier("acct", "alice"),
+ role=Role.USER,
+ api_key="secret",
+ )
+ uploaded = tmp_path / "audio.m4a"
+ uploaded.write_bytes(b"audio bytes")
+ prepared = LocalResource(
+ path=uploaded,
+ source_type=SourceType.LOCAL,
+ original_source=str(uploaded),
+ meta={
+ "resolved_extension": ".m4a",
+ "original_filename": "audio.m4a",
+ },
+ is_temporary=True,
+ )
+ lock = {"lease_ref": "lock-1"}
+ agfs = SimpleNamespace(
+ pathlock_to_handoff=AsyncMock(return_value={"handle_id": "lock-1"}),
+ pathlock_handoff=AsyncMock(),
+ pathlock_release=AsyncMock(),
+ )
+ processor = SimpleNamespace(
+ should_use_understanding_directly=lambda _source, **_kwargs: False,
+ prepare_durable_source=AsyncMock(return_value=prepared),
+ should_use_understanding_api=lambda resource: resource is prepared,
+ upload_understanding_file=AsyncMock(return_value="file-1"),
+ submit_understanding=AsyncMock(side_effect=AssertionError("should not create response")),
+ tree_builder=SimpleNamespace(
+ resolve_target_uri=AsyncMock(
+ return_value=(
+ "viking://resources/audio",
+ "viking://resources/audio",
+ )
+ )
+ ),
+ reserve_unique_candidate=AsyncMock(return_value=("viking://resources/audio", lock)),
+ process_resource=AsyncMock(),
+ )
+ service = ResourceService(
+ vikingdb=object(),
+ viking_fs=SimpleNamespace(_async_agfs=agfs),
+ resource_processor=processor,
+ skill_processor=object(),
+ )
+ service._connector_delegate = SimpleNamespace(should_delegate=lambda *_args, **_kwargs: False)
+ tracker = SimpleNamespace(
+ create=AsyncMock(return_value=SimpleNamespace(task_id="task-1")),
+ update_stage=AsyncMock(),
+ fail=AsyncMock(),
+ )
+ queue_manager = SimpleNamespace(enqueue=AsyncMock())
+ monkeypatch.setattr(resource_service_module, "is_git_repo_url", lambda _path: False)
+ monkeypatch.setattr(
+ "openviking.service.task_tracker.get_task_tracker",
+ lambda: tracker,
+ )
+ monkeypatch.setattr(
+ "openviking.storage.queuefs.get_queue_manager",
+ lambda: queue_manager,
+ )
+
+ result = await service.add_resource(
+ path=str(uploaded),
+ ctx=ctx,
+ to="viking://resources/audio",
+ wait=False,
+ allow_local_path_resolution=True,
+ internal_task=True,
+ temp_file_id=temp_file_id,
+ )
+
+ assert result == {
+ "status": "success",
+ "root_uri": "viking://resources/audio",
+ "task_id": "task-1",
+ }
+ _, message = queue_manager.enqueue.await_args.args
+ assert queue_manager.enqueue.await_args.args[0] == QueueManager.EXTERNAL_PARSE
+ queued = AddResourceMsg.from_dict(message)
+ assert queued.understanding_file_id == "file-1"
+ assert queued.understanding_response_id is None
+ assert queued.staged_source is None
+ assert queued.internal_task is True
+ assert queued.path == "audio.m4a"
+ assert queued.source_name == "audio.m4a"
+ assert not uploaded.exists()
+ processor.upload_understanding_file.assert_awaited_once_with(prepared)
+ processor.submit_understanding.assert_not_called()
+ processor.process_resource.assert_not_awaited()
+ agfs.pathlock_to_handoff.assert_awaited_once_with(lock)
+ agfs.pathlock_handoff.assert_awaited_once_with(lock)
diff --git a/tests/utils/test_resource_processor_processing_mode.py b/tests/utils/test_resource_processor_processing_mode.py
index 572328f16..841b5e68d 100644
--- a/tests/utils/test_resource_processor_processing_mode.py
+++ b/tests/utils/test_resource_processor_processing_mode.py
@@ -40,6 +40,18 @@ class _RecordingVikingDB:
return [{"id": "recursive-child-detail"}]
+async def test_resource_processor_upload_understanding_file_delegates():
+ processor = ResourceProcessor(_FakeVikingDB())
+ media_processor = SimpleNamespace(upload_understanding_file=AsyncMock(return_value="file-1"))
+ processor._media_processor = media_processor
+
+ result = await processor.upload_understanding_file("/tmp/upload.pdf")
+
+ assert result == "file-1"
+ media_processor.upload_understanding_file.assert_awaited_once_with("/tmp/upload.pdf")
+
+
@pytest.fixture
def ctx() -> RequestContext:
return RequestContext(