diff --git a/docs/en/auto_resource.md b/docs/en/auto_resource.md index d3b5fd28..d62bf0f5 100644 --- a/docs/en/auto_resource.md +++ b/docs/en/auto_resource.md @@ -77,9 +77,10 @@ reprocessing the same image still updates the original card. The body should con description or transcription under `## Caption`, not an empty caption or a JSON response. Leave `status` to later processing steps and keep its existing value when updating the card. -Customize image instructions with `prompt_dict.resource_instructions` (`resource_instructions_zh` for Chinese), replacing -`user_message` / `user_message_zh`. If you also override the shared create or update template, keep its -`{resource_instructions}` placeholder. +Customize image instructions with `prompt_dict.resource_instructions` (`resource_instructions_zh` for Chinese). +Existing `user_message` / `user_message_zh` settings still work; the new name takes precedence when both are set for the +same language. Shared create/update templates insert these instructions at `{resource_instructions}`; older templates +without the placeholder receive them at the end. Set `include_images=false` on an `auto_resource` call or as a Job default to skip **all** image events, including deletions. Call-time values override Job defaults; when neither is set, image processing is enabled. For the watcher, use diff --git a/docs/zh/auto_resource.md b/docs/zh/auto_resource.md index aa707510..0134f865 100644 --- a/docs/zh/auto_resource.md +++ b/docs/zh/auto_resource.md @@ -69,8 +69,9 @@ Agent 写入一张 caption 卡片并链接原图。卡片正文以 `![[resource/ 卡片正文应包含原图引用和 `## Caption` 下的描述或文字转录,不能留空或直接写入 JSON。 `status` 留给后续流程填写,更新卡片时保留原值。 -自定义图片提示词使用 `prompt_dict.resource_instructions`,中文使用 `resource_instructions_zh`,替代原来的 -`user_message` / `user_message_zh`。如果同时覆盖公共创建或更新模板,需保留 `{resource_instructions}` 占位符。 +自定义图片提示词使用 `prompt_dict.resource_instructions`,中文使用 `resource_instructions_zh`;旧配置中的 +`user_message` / `user_message_zh` 仍可使用,同一语言同时配置新旧名称时以新名称为准。公共创建或更新模板通过 +`{resource_instructions}` 插入图片要求;旧模板没有该占位符时,图片要求会追加到末尾。 在 `auto_resource` 调用或 Job 默认值中设置 `include_images=false`,会跳过图片的**全部事件,包括删除**。 调用参数优先于 Job 默认值,两者都未设置时默认开启。监听任务可设置 `jobs.resource_watch_loop.include_images=false`, diff --git a/reme/steps/evolve/auto_image_resource.py b/reme/steps/evolve/auto_image_resource.py index d3082580..87714a74 100644 --- a/reme/steps/evolve/auto_image_resource.py +++ b/reme/steps/evolve/auto_image_resource.py @@ -214,6 +214,18 @@ class AutoImageResourceStep(BaseAutoResourceStep): {"agent_wrapper", "max_image_bytes", "max_image_pixels", "prompt_dict"}, ) + def __init__(self, *args, **kwargs): + prompt_dict = kwargs.get("prompt_dict") + if prompt_dict: + prompts = dict(prompt_dict) + for name, value in prompt_dict.items(): + if (name == "user_message" or name.startswith("user_message_")) and not name.startswith( + ("user_message_create", "user_message_update"), + ): + prompts.setdefault(name.replace("user_message", "resource_instructions", 1), value) + kwargs["prompt_dict"] = prompts + super().__init__(*args, **kwargs) + def _max_image_bytes(self) -> int: """Return the image read limit from Step or Job context.""" value = self.kwargs.get("max_image_bytes") @@ -350,6 +362,7 @@ class AutoImageResourceStep(BaseAutoResourceStep): ), note_metadata={"kind": "image", "media_type": payload["source_mime"]}, reply_kwargs={ + "scope_note_tools": True, "session_id": None, "resume": None, "builtin_tools": [], @@ -357,7 +370,6 @@ class AutoImageResourceStep(BaseAutoResourceStep): "toolkit": None, "output_schema": None, }, - scope_note_tools=True, ) if note_path is None: raise RuntimeError("Resource agent did not write a note") diff --git a/reme/steps/evolve/base_auto_resource.py b/reme/steps/evolve/base_auto_resource.py index 7d0ee25d..ff7a9275 100644 --- a/reme/steps/evolve/base_auto_resource.py +++ b/reme/steps/evolve/base_auto_resource.py @@ -378,12 +378,12 @@ class BaseAutoResourceStep(BaseStep): resource_instructions: str = "", note_metadata: dict | None = None, reply_kwargs: dict | None = None, - scope_note_tools: bool = False, ) -> str | None: """Run the same note-writing agent for text and native multimodal inputs.""" state = await self._prepare_resource_note(day, file_path, note_stem) + prompt_name = "user_message_create" if state.created else "user_message_update" prompt = self.prompt_format( - "user_message_create" if state.created else "user_message_update", + prompt_name, workspace_dir=str(self.workspace_path), note_path=state.path, note_stem=note_stem, @@ -393,6 +393,8 @@ class BaseAutoResourceStep(BaseStep): resource_instructions=f"\n\n{resource_instructions}" if resource_instructions else "", date=day, ) + if resource_instructions and "{resource_instructions}" not in self.get_prompt(prompt_name): + prompt = f"{prompt}\n\n{resource_instructions}" inputs = UserMsg(name="user", content=[*input_blocks, TextBlock(text=prompt)]) if input_blocks else prompt self.logger.info(f"[{self.name}] agent start file_path={file_path} note_path={state.path}") agent_kwargs = { @@ -402,7 +404,8 @@ class BaseAutoResourceStep(BaseStep): } if "session_id" not in agent_kwargs: agent_kwargs["session_id"] = _compute_agent_session_id(file_path) - if scope_note_tools: + # Consume the resource-only option before forwarding kwargs to the wrapper. + if agent_kwargs.pop("scope_note_tools", False): # Bind the final allocated path last, never a path supplied by the agent. agent_kwargs["injected_job_kwargs"] = { **(agent_kwargs.get("injected_job_kwargs") or {}), diff --git a/tests/unit/test_auto_image_steps.py b/tests/unit/test_auto_image_steps.py index 67f26f4a..2ff7d1e7 100644 --- a/tests/unit/test_auto_image_steps.py +++ b/tests/unit/test_auto_image_steps.py @@ -812,8 +812,9 @@ import reme.steps.evolve.auto_image_resource @pytest.mark.parametrize("language", ["en", "zh"]) @pytest.mark.parametrize("existing", [False, True], ids=["create", "update"]) +@pytest.mark.parametrize("legacy_prompts", [False, True], ids=["default-prompts", "legacy-prompts"]) @pytest.mark.asyncio -async def test_auto_image_prompt_merges_resource_instructions(language, existing, auto_resource_env): +async def test_auto_image_prompt_merges_resource_instructions(language, existing, legacy_prompts, auto_resource_env): """Shared create/update prompts include localized image requirements exactly once.""" env = auto_resource_env source_path = "resource/2026-01-01/cat-at-beach.png" @@ -822,6 +823,17 @@ async def test_auto_image_prompt_merges_resource_instructions(language, existing env.write_note("daily/2026-01-01/cat-at-beach.md", f"[[{source_path}]]") model = _FakeImageAgentWrapper(_caption_fields("red-square", "Red", "A red square.")) step = env.processor(model, language=language) + if legacy_prompts: + suffix = "_zh" if language == "zh" else "" + prompt_name = "user_message_update" if existing else "user_message_create" + step = env.processor( + model, + language=language, + prompt_dict={ + f"user_message{suffix}": "Legacy image instructions: {filename}", + f"{prompt_name}{suffix}": step.get_prompt(prompt_name).replace("{resource_instructions}", ""), + }, + ) response = await env.run(step, [{"change": "modified" if existing else "added", "path": str(source)}]) assert response.success is True @@ -836,12 +848,30 @@ async def test_auto_image_prompt_merges_resource_instructions(language, existing date="2026-01-01", ) assert prompt.count(instructions) == 1 - assert ("weak hints" if language == "en" else "弱提示") in prompt - assert ("trust the visible image content" if language == "en" else "以图像中的可见内容为准") in prompt + if legacy_prompts: + assert instructions == "Legacy image instructions: cat-at-beach.png" + assert prompt.endswith(instructions) + else: + assert ("weak hints" if language == "en" else "弱提示") in prompt + assert ("trust the visible image content" if language == "en" else "以图像中的可见内容为准") in prompt assert options["injected_job_kwargs"]["_allowed_paths"][0] in prompt assert ("read path=" in prompt) is existing +@pytest.mark.parametrize("language,suffix", [("en", ""), ("en", "_en"), ("zh", "_zh")]) +def test_image_prompt_aliases_preserve_explicit_new_keys_and_caller_config(language, suffix): + """Normalize only legacy image keys, leaving shared templates and the input dict intact.""" + prompts = {f"user_message{suffix}": "legacy", f"user_message_create{suffix}": "custom create"} + original = dict(prompts) + step = AutoImageResourceStep(language=language, prompt_dict=prompts) + assert step.get_prompt("resource_instructions") == "legacy" + assert step.get_prompt("user_message_create") == "custom create" + assert prompts == original + prompts[f"resource_instructions{suffix}"] = "new" + step = AutoImageResourceStep(language=language, prompt_dict=prompts) + assert step.get_prompt("resource_instructions") == "new" + + @pytest.mark.asyncio async def test_auto_image_reports_modified_when_index_refresh_fails_after_write(auto_resource_env): """A post-write failure keeps the actual on-disk modification state.""" diff --git a/tests/unit/test_auto_resource_agent_inputs.py b/tests/unit/test_auto_resource_agent_inputs.py index e5a1b62e..f1f07f04 100644 --- a/tests/unit/test_auto_resource_agent_inputs.py +++ b/tests/unit/test_auto_resource_agent_inputs.py @@ -82,6 +82,7 @@ async def test_native_image_input_preserves_context_formatter_and_scoped_tools(a assert options["output_schema"] is None assert options["resume"] is None assert options["session_id"] is None + assert "scope_note_tools" not in options assert options["injected_job_kwargs"]["_allowed_paths"] == ["daily/2026-01-01/brown-coat.md"] native = AsAgentWrapper(