fix(session): replay user images from stored attachments (#6720)
This commit is contained in:
1 parent
488b657ee1
commit
3025a01cf0
29 files changed
+456
-281
No files matched your search
@@ -12,6 +12,7 @@ from nanobot.providers.base import (
|
||||
)
|
||||
from nanobot.providers.factory import make_provider, provider_signature
|
||||
from nanobot.providers.fallback_provider import FallbackProvider
|
||||
from nanobot.providers.image_input import REPLAYED_IMAGE_META
|
||||
|
||||
_IMAGE = {"type": "image_url", "image_url": {"url": "data:image/png;base64,c3ludGhldGlj"}}
|
||||
|
||||
@@ -53,6 +54,39 @@ async def test_text_only_preset_rejects_images_before_request(stream, native):
|
||||
assert len(provider.calls) == 1
|
||||
|
||||
|
||||
async def test_text_only_preset_omits_history_but_still_rejects_fresh_images():
|
||||
provider = ImageProvider(False)
|
||||
history = {**_IMAGE, "_meta": {REPLAYED_IMAGE_META: True}}
|
||||
prompt = {"type": "text", "text": "Continue"}
|
||||
messages = [{"role": "user", "content": [history, prompt]}]
|
||||
|
||||
response = await provider.chat_with_retry(messages=messages)
|
||||
assert response.content == "accepted"
|
||||
forwarded = provider.calls[0]["messages"][0]["content"]
|
||||
assert forwarded == [
|
||||
{"type": "text", "text": "[Previous image omitted: model does not support images]"},
|
||||
prompt,
|
||||
]
|
||||
assert messages[0]["content"] == [history, prompt]
|
||||
|
||||
response = await provider.chat_with_retry(messages=[{"role": "user", "content": [history, _IMAGE]}])
|
||||
assert response.error_code == "unsupported_image"
|
||||
assert len(provider.calls) == 1
|
||||
|
||||
|
||||
async def test_text_fallback_accepts_history_after_vision_provider_fails():
|
||||
vision = ImageProvider(True, LLMResponse("unavailable", finish_reason="error", error_status_code=503))
|
||||
text = ImageProvider(False)
|
||||
chain = FallbackProvider(vision, [ModelPresetConfig(model="text", supports_images=False)], lambda _: text)
|
||||
messages = [{"role": "user", "content": [{**_IMAGE, "_meta": {REPLAYED_IMAGE_META: True}}]}]
|
||||
|
||||
assert (await chain.chat(messages=messages)).content == "accepted"
|
||||
assert vision.calls[0]["messages"] == messages
|
||||
assert text.calls[0]["messages"][0]["content"] == [
|
||||
{"type": "text", "text": "[Previous image omitted: model does not support images]"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stream", [False, True])
|
||||
@pytest.mark.parametrize("primary_supports", [False, True])
|
||||
async def test_image_fallback_skips_text_only_candidates_without_counting_capability_as_outage(stream, primary_supports):
|
||||
|
||||
@@ -7,10 +7,10 @@ import threading
|
||||
from collections.abc import Callable, Iterator
|
||||
from contextlib import closing
|
||||
from dataclasses import asdict
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.config.schema import ModelPresetConfig
|
||||
from nanobot.llm_usage.context import current_llm_usage_source, llm_usage_source
|
||||
from nanobot.llm_usage.models import LLMCallRecord
|
||||
from nanobot.llm_usage.service import record_llm_call
|
||||
@@ -473,13 +473,7 @@ async def test_fallback_provider_propagates_observer_to_every_leaf() -> None:
|
||||
]
|
||||
)
|
||||
)
|
||||
preset = SimpleNamespace(
|
||||
model="fallback-model",
|
||||
max_tokens=256,
|
||||
temperature=0.2,
|
||||
reasoning_effort=None,
|
||||
context_window_tokens=4_096,
|
||||
)
|
||||
preset = ModelPresetConfig(model="fallback-model")
|
||||
provider = FallbackProvider(primary, [preset], lambda _preset: fallback)
|
||||
events: list[LLMCallRecord] = []
|
||||
provider.set_llm_call_observer(events.append)
|
||||
|
||||
@@ -239,9 +239,8 @@ async def test_codex_exports_langfuse_usage(codex_peer, reject_upgrade):
|
||||
async def test_gateway_continues_image_then_pdf_attachment_turn(codex_peer, tmp_path):
|
||||
provider, server, _ = codex_peer
|
||||
image = tmp_path / "image.png"
|
||||
image.write_bytes(base64.b64decode(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aP1kAAAAASUVORK5CYII="
|
||||
))
|
||||
png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aP1kAAAAASUVORK5CYII="
|
||||
image.write_bytes(base64.b64decode(png))
|
||||
pdf = tmp_path / "report.pdf"
|
||||
writer = PdfWriter()
|
||||
writer.add_blank_page(width=100, height=100)
|
||||
@@ -263,11 +262,11 @@ async def test_gateway_continues_image_then_pdf_attachment_turn(codex_peer, tmp_
|
||||
assert len(server.peers) == 1
|
||||
assert len(server.requests) == 2
|
||||
initial, continued = [body for _, body in server.requests]
|
||||
assert "input_image" in json.dumps(initial["input"])
|
||||
assert any(
|
||||
block.get("type") == "input_text" and str(image) in block["text"]
|
||||
for item in initial["input"] for block in item.get("content", [])
|
||||
)
|
||||
content = next(item["content"] for item in initial["input"] if item.get("role") == "user")
|
||||
assert [b["image_url"] for b in content if b["type"] == "input_image"] == [f"data:image/png;base64,{png}"]
|
||||
texts = [b["text"] for b in content if b["type"] == "input_text"]
|
||||
assert "Describe this image." in texts
|
||||
assert all(str(image) not in text for text in texts)
|
||||
assert continued["previous_response_id"] == "resp_1"
|
||||
assert "input_image" not in json.dumps(continued["input"])
|
||||
assert f"[Attachment: {pdf}]" in continued["input"][0]["content"][0]["text"]
|
||||
|
||||
Reference in new issue
Block a user