Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(bot): Tool-call ordering, thinking mode, and image uploads (#2944)
DuTaod689a285da48
diff --git a/bot/tests/test_channel_delivery_metadata.py b/bot/tests/test_channel_delivery_metadata.pyindex 2facc37d1..5d643564f 100644--- a/bot/tests/test_channel_delivery_metadata.py+++ b/bot/tests/test_channel_delivery_metadata.py@@ -115,3 +115,98 @@ async def test_feishu_send_skips_normal_message_without_reply_to():content="hello",))+++async def test_feishu_upload_image_uses_detected_jpeg_format(monkeypatch):+ channel = FeishuChannel(FeishuChannelConfig(app_id="cli_app"), MessageBus())+ jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01fake-jpeg"+ captured = {}++ async def fake_token():+ return "tenant-token"++ class FakeResponse:+ is_error = False+ status_code = 200+ text = ""++ def raise_for_status(self):+ return None++ def json(self):+ return {"code": 0, "data": {"image_key": "img_key"}}++ class FakeClient:+ def __init__(self, timeout):+ self.timeout = timeout++ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ async def post(self, url, headers, data, files):+ captured.update(url=url, headers=headers, data=data, files=files)+ return FakeResponse()++ monkeypatch.setattr(channel, "_get_tenant_access_token", fake_token)+ monkeypatch.setattr("vikingbot.channels.feishu.httpx.AsyncClient", FakeClient)++ image_key = await channel._upload_image_to_feishu(jpeg_bytes)++ filename, file_obj, mime_type = captured["files"]["image"]+ assert image_key == "img_key"+ assert filename == "image.jpg"+ assert mime_type == "image/jpeg"+ assert file_obj.read() == jpeg_bytes+++async def test_feishu_upload_retries_with_normalized_image_after_bad_request(monkeypatch):+ channel = FeishuChannel(FeishuChannelConfig(app_id="cli_app"), MessageBus())+ original_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01original-with-metadata"+ normalized_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01normalized"+ uploads = []++ async def fake_token():+ return "tenant-token"++ class FakeResponse:+ def __init__(self, status_code, body):+ self.status_code = status_code+ self.text = body+ self.is_error = status_code >= 400++ def json(self):+ return {"code": 0, "data": {"image_key": "img_key"}}++ class FakeClient:+ def __init__(self, timeout):+ self.timeout = timeout++ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ async def post(self, url, headers, data, files):+ filename, file_obj, mime_type = files["image"]+ uploads.append((filename, mime_type, file_obj.read()))+ if len(uploads) == 1:+ return FakeResponse(400, '{"code":234011,"msg":"can not recognize image"}')+ return FakeResponse(200, '{"code":0}')++ monkeypatch.setattr(channel, "_get_tenant_access_token", fake_token)+ monkeypatch.setattr(channel, "_normalize_image_for_feishu", lambda data: normalized_bytes)+ monkeypatch.setattr("vikingbot.channels.feishu.httpx.AsyncClient", FakeClient)++ image_key = await channel._upload_image_to_feishu(original_bytes)++ assert image_key == "img_key"+ assert uploads == [+ ("image.jpg", "image/jpeg", original_bytes),+ ("image.jpg", "image/jpeg", normalized_bytes),+ ]diff --git a/bot/tests/test_image_format.py b/bot/tests/test_image_format.pynew file mode 100644index 000000000..4dfc27efb--- /dev/null+++ b/bot/tests/test_image_format.py@@ -0,0 +1,24 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Regression tests for image format detection."""++import sys+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parents[1]))++from vikingbot.utils.image_format import detect_image_format # noqa: E402+++def test_detect_image_format_identifies_jpeg():+ image_format = detect_image_format(b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01")++ assert image_format.extension == "jpg"+ assert image_format.mime_type == "image/jpeg"+++def test_detect_image_format_uses_mime_hint_for_unknown_bytes():+ image_format = detect_image_format(b"unknown", fallback_mime="image/webp; charset=binary")++ assert image_format.extension == "webp"+ assert image_format.mime_type == "image/webp"diff --git a/bot/tests/test_image_tool_sandbox.py b/bot/tests/test_image_tool_sandbox.pyindex e1656d98b..bdbf6e331 100644--- a/bot/tests/test_image_tool_sandbox.py+++ b/bot/tests/test_image_tool_sandbox.py@@ -37,6 +37,7 @@ def _load_image_module(monkeypatch):"""Load the image tool module directly so this regression stays isolated."""repo_root = Path(__file__).resolve().parents[2]image_path = repo_root / "bot" / "vikingbot" / "agent" / "tools" / "image.py"+ image_format_path = repo_root / "bot" / "vikingbot" / "utils" / "image_format.py"base_mod = types.ModuleType("vikingbot.agent.tools.base")@@ -59,6 +60,13 @@ def _load_image_module(monkeypatch):utils_mod = types.ModuleType("vikingbot.utils")utils_mod.get_data_path = lambda: repo_root+ image_format_spec = importlib.util.spec_from_file_location(+ "image_format_under_test", image_format_path+ )+ image_format_module = importlib.util.module_from_spec(image_format_spec)+ assert image_format_spec.loader is not None+ image_format_spec.loader.exec_module(image_format_module)+ utils_mod.detect_image_format = image_format_module.detect_image_formatmonkeypatch.setitem(sys.modules, "vikingbot", types.ModuleType("vikingbot"))monkeypatch.setitem(sys.modules, "vikingbot.agent", types.ModuleType("vikingbot.agent"))@@ -193,3 +201,25 @@ async def test_edit_mode_reads_base_image_and_mask_from_sandbox(monkeypatch, tmp)assert base64.b64decode(captured_kwargs["mask"].split(",", 1)[1]) == b"SANDBOX_MASK_IMAGE_BYTES"assert result.startswith("生成图片:")+++async def test_image_tool_saves_generated_jpeg_with_jpg_extension(monkeypatch, tmp_path):+ module = _load_image_module(monkeypatch)+ monkeypatch.setattr(module, "get_data_path", lambda: tmp_path)+ jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01fake-jpeg"++ async def fake_image_generation(**kwargs):+ return SimpleNamespace(data=[SimpleNamespace(b64_json=base64.b64encode(jpeg_bytes).decode())])++ monkeypatch.setattr(module.litellm, "aimage_generation", fake_image_generation)+ context = SimpleNamespace(session_key="session", channel_metadata={})+ tool = module.ImageGenerationTool()++ result = await tool.execute(context, mode="generate", prompt="make a jpeg", send_to_user=False)++ saved_files = list((tmp_path / "images").iterdir())+ assert len(saved_files) == 1+ assert saved_files[0].suffix == ".jpg"+ assert saved_files[0].read_bytes() == jpeg_bytes+ assert f"send://{saved_files[0].name}" in resultdiff --git a/bot/vikingbot/agent/tools/image.py b/bot/vikingbot/agent/tools/image.pyindex 1757810d4..031eb3fc2 100644--- a/bot/vikingbot/agent/tools/image.py+++ b/bot/vikingbot/agent/tools/image.py@@ -12,7 +12,7 @@ import litellmfrom vikingbot.agent.tools.base import Tool, ToolContextfrom vikingbot.bus.events import OutboundMessage-from vikingbot.utils import get_data_path+from vikingbot.utils import detect_image_format, get_data_pathclass ImageGenerationTool(Tool):@@ -128,11 +128,22 @@ class ImageGenerationTool(Tool):return data_uri, "data"async def _url_to_base64(self, url: str) -> str:- """Download image from URL and convert to base64."""+ """Download image from URL and convert to a data URI."""async with httpx.AsyncClient(timeout=60.0) as client:response = await client.get(url)response.raise_for_status()- return base64.b64encode(response.content).decode("utf-8")+ mime_type = response.headers.get("content-type", "application/octet-stream")+ encoded = base64.b64encode(response.content).decode("utf-8")+ return f"data:{mime_type};base64,{encoded}"++ @staticmethod+ def _decode_image_payload(image_payload: str) -> tuple[bytes, str | None]:+ """Decode a b64_json or data URI image payload."""+ fallback_mime = None+ if image_payload.startswith("data:"):+ header, image_payload = image_payload.split(",", 1)+ fallback_mime = header[5:].split(";", 1)[0] or None+ return base64.b64decode(image_payload), fallback_mimedef _build_common_kwargs(self,@@ -283,11 +294,10 @@ class ImageGenerationTool(Tool):saved_filenames = []for img in images:- random_filename = f"{uuid.uuid4().hex}.png"+ image_bytes, fallback_mime = self._decode_image_payload(img)+ image_format = detect_image_format(image_bytes, fallback_mime)+ random_filename = f"{uuid.uuid4().hex}.{image_format.extension}"image_path = images_dir / random_filename- if img.startswith("data:"):- _, img = img.split(",", 1)- image_bytes = base64.b64decode(img)with open(image_path, "wb") as f:f.write(image_bytes)saved_paths.append(f"send://{random_filename}")diff --git a/bot/vikingbot/channels/feishu.py b/bot/vikingbot/channels/feishu.pyindex 231e629f6..02a24ceb6 100644--- a/bot/vikingbot/channels/feishu.py+++ b/bot/vikingbot/channels/feishu.py@@ -14,7 +14,7 @@ import httpxfrom loguru import loggerfrom vikingbot.config import load_config-from vikingbot.utils import get_data_path+from vikingbot.utils import detect_image_format, get_data_path# Optional HTML processing librariestry:@@ -153,24 +153,86 @@ class FeishuChannel(BaseChannel):Upload image to Feishu media library and get image_key."""+ if not image_data:+ raise ValueError("Feishu image upload requires a non-empty image")+token = await self._get_tenant_access_token()url = "https://open.feishu.cn/open-apis/im/v1/images"headers = {"Authorization": f"Bearer {token}"}-- # Use io.BytesIO properly- files = {"image": ("image.png", io.BytesIO(image_data), "image/png")}data = {"image_type": "message"}+ async def post_image(client: httpx.AsyncClient, upload_data: bytes) -> httpx.Response:+ image_format = detect_image_format(upload_data)+ files = {+ "image": (+ f"image.{image_format.extension}",+ io.BytesIO(upload_data),+ image_format.mime_type,+ )+ }+ return await client.post(url, headers=headers, data=data, files=files)+async with httpx.AsyncClient(timeout=60.0) as client:- resp = await client.post(url, headers=headers, data=data, files=files)- # logger.debug(f"Upload response status: {resp.status_code}")- resp.raise_for_status()+ upload_data = image_data+ resp = await post_image(client, upload_data)++ if resp.status_code == 400:+ normalized = self._normalize_image_for_feishu(image_data)+ if normalized != image_data:+ logger.warning(+ "Retrying Feishu image upload after re-encoding image "+ f"({len(image_data)} -> {len(normalized)} bytes)"+ )+ upload_data = normalized+ resp = await post_image(client, upload_data)++ if resp.is_error:+ raise Exception(+ "Failed to upload image to Feishu: "+ f"status={resp.status_code}, body={resp.text}, "+ f"image_size={len(upload_data)}"+ )+result = resp.json()if result.get("code") != 0:- raise Exception(f"Failed to upload image: {result}")+ raise Exception(+ f"Failed to upload image to Feishu: response={result}, "+ f"image_size={len(upload_data)}"+ )return result["data"]["image_key"]+ @staticmethod+ def _normalize_image_for_feishu(image_data: bytes) -> bytes:+ """Re-encode an image to strip metadata that Feishu may reject."""+ try:+ from PIL import Image, ImageOps+ except ImportError:+ logger.warning("Pillow is not installed; cannot normalize image for Feishu upload")+ return image_data++ try:+ with Image.open(io.BytesIO(image_data)) as source:+ image = ImageOps.exif_transpose(source)+ if image.mode in ("RGBA", "LA") or (+ image.mode == "P" and "transparency" in image.info+ ):+ rgba = image.convert("RGBA")+ background = Image.new("RGBA", rgba.size, (255, 255, 255, 255))+ image = Image.alpha_composite(background, rgba).convert("RGB")+ elif image.mode not in ("RGB", "L"):+ image = image.convert("RGB")+ else:+ image = image.copy()++ output = io.BytesIO()+ image.save(output, format="JPEG", quality=92, optimize=True)+ normalized = output.getvalue()+ return normalized if normalized else image_data+ except Exception as exc:+ logger.warning(f"Failed to normalize image for Feishu upload: {exc}")+ return image_data+async def _download_feishu_image(self, image_key: str, message_id: str | None = None) -> bytes:"""Download an image from Feishu using image_key. If message_id is provided,diff --git a/bot/vikingbot/cli/commands.py b/bot/vikingbot/cli/commands.pyindex 2b6568fac..1d83496eb 100644--- a/bot/vikingbot/cli/commands.py+++ b/bot/vikingbot/cli/commands.py@@ -271,6 +271,7 @@ def _make_provider(config, langfuse_client: None = None):p = config.agentsmodel = p.model if p else Nonetemperature = p.temperature if p else 0.7+ thinking = p.thinking if p else Trueapi_key = p.api_key if p else Noneapi_base = p.api_base if p else Noneprovider_name = p.provider if p else None@@ -292,6 +293,7 @@ def _make_provider(config, langfuse_client: None = None):"provider": provider_name,"model": model,"temperature": temperature,+ "thinking": thinking,}if timeout is not None:vlm_config["timeout"] = timeout@@ -321,6 +323,7 @@ def _make_provider(config, langfuse_client: None = None):extra_headers=extra_headers,provider_name=provider_name,timeout=timeout,+ thinking=thinking,langfuse_client=langfuse_client,)diff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.pyindex 578fe1fc3..7a6b129ed 100644--- a/bot/vikingbot/config/schema.py+++ b/bot/vikingbot/config/schema.py@@ -439,6 +439,13 @@ class AgentsConfig(BaseModel):le=2.0,description="Sampling temperature for LLM requests.",)+ thinking: bool = Field(+ default=True,+ description=(+ "Enable provider reasoning/thinking mode for bot LLM requests when the "+ "selected provider protocol supports an explicit thinking parameter."+ ),+ )timeout: Optional[float] = Field(default=None,gt=0.0,diff --git a/bot/vikingbot/providers/base.py b/bot/vikingbot/providers/base.pyindex c859e7343..a47432b39 100644--- a/bot/vikingbot/providers/base.py+++ b/bot/vikingbot/providers/base.py@@ -49,10 +49,11 @@ def stream_delta_value(delta: Any, name: str) -> str:def merge_stream_tool_call_delta(raw_tool_calls: dict[int, dict[str, Any]],delta_tool_call: Any,+ fallback_index: int | None = None,) -> None:index = getattr(delta_tool_call, "index", None)if index is None:- index = len(raw_tool_calls)+ index = fallback_index if fallback_index is not None else len(raw_tool_calls)entry = raw_tool_calls.setdefault(int(index),{"id": "", "name": "", "arguments": ""},diff --git a/bot/vikingbot/providers/litellm_provider.py b/bot/vikingbot/providers/litellm_provider.pyindex b8d4925ca..e03cfb0a8 100644--- a/bot/vikingbot/providers/litellm_provider.py+++ b/bot/vikingbot/providers/litellm_provider.py@@ -23,6 +23,13 @@ from vikingbot.utils.helpers import cal_str_tokensfrom vikingbot.utils.tracing import get_current_response_id+_OPENAI_REASONING_MODEL_PREFIXES = ("gpt-5", "openai/gpt-5", "o1", "o3", "o4")+++def _is_openai_reasoning_model(model: str) -> bool:+ return model.lower().startswith(_OPENAI_REASONING_MODEL_PREFIXES)++class LiteLLMProvider(LLMProvider):"""LLM provider using LiteLLM for multi-provider support.@@ -40,12 +47,14 @@ class LiteLLMProvider(LLMProvider):extra_headers: dict[str, str] | None = None,provider_name: str | None = None,timeout: float | None = None,+ thinking: bool = True,langfuse_client: LangfuseClient | None = None,):super().__init__(api_key, api_base)self.default_model = default_modelself.extra_headers = extra_headers or {}self.timeout = timeout+ self.thinking = thinkingself.langfuse = langfuse_client or LangfuseClient.get_instance()# Detect gateway / local deployment.@@ -115,6 +124,34 @@ class LiteLLMProvider(LLMProvider):kwargs.update(overrides)return+ def _apply_thinking_overrides(self, model: str, kwargs: dict[str, Any]) -> None:+ """Attach explicit thinking params only for providers that support them."""+ if not self.thinking:+ return++ spec = self._gateway or find_by_model(model)+ thinking_param = spec.thinking_param if spec else ""+ model_lower = model.lower()++ if thinking_param == "volcengine_thinking" or model_lower.startswith("volcengine/"):+ kwargs["thinking"] = {"type": "enabled"}+ return++ if thinking_param == "dashscope_enable_thinking" or model_lower.startswith(+ "dashscope/"+ ):+ extra_body = dict(kwargs.get("extra_body") or {})+ extra_body["enable_thinking"] = True+ kwargs["extra_body"] = extra_body+ return++ if (+ (thinking_param == "openai_reasoning_effort" or not spec)+ and _is_openai_reasoning_model(model)+ and "reasoning_effort" not in kwargs+ ):+ kwargs["reasoning_effort"] = "low"+def _handle_system_message(self, model: str, messages: list[dict[str, Any]]) -> list[dict[str, Any]]:@@ -210,6 +247,7 @@ class LiteLLMProvider(LLMProvider):# Apply model-specific overrides (e.g. kimi-k2.5 temperature)self._apply_model_overrides(model, kwargs)+ self._apply_thinking_overrides(model, kwargs)# Pass api_key directly — more reliable than env vars aloneif self.api_key:@@ -373,6 +411,7 @@ class LiteLLMProvider(LLMProvider):"stream": True,}self._apply_model_overrides(model, kwargs)+ self._apply_thinking_overrides(model, kwargs)if self.api_key:kwargs["api_key"] = self.api_key@@ -419,8 +458,14 @@ class LiteLLMProvider(LLMProvider):content_parts.append(content_delta)yield LLMStreamEvent(type="content_delta", content=content_delta)- for delta_tool_call in getattr(delta, "tool_calls", None) or []:- merge_stream_tool_call_delta(tool_calls, delta_tool_call)+ for fallback_index, delta_tool_call in enumerate(+ getattr(delta, "tool_calls", None) or []+ ):+ merge_stream_tool_call_delta(+ tool_calls,+ delta_tool_call,+ fallback_index=fallback_index,+ )response_obj = build_stream_response(content="".join(content_parts),diff --git a/bot/vikingbot/providers/registry.py b/bot/vikingbot/providers/registry.pyindex 22f3cb735..a0f5bb2db 100644--- a/bot/vikingbot/providers/registry.py+++ b/bot/vikingbot/providers/registry.py@@ -51,6 +51,9 @@ class ProviderSpec:# per-model param overrides, e.g. (("kimi-k2.5", {"temperature": 1.0}),)model_overrides: tuple[tuple[str, dict[str, Any]], ...] = ()+ # provider-specific way to request reasoning/thinking, if supported by the protocol+ thinking_param: str = "" # volcengine_thinking | dashscope_enable_thinking | openai_reasoning_effort+@propertydef label(self) -> str:return self.display_name or self.name.title()@@ -133,6 +136,7 @@ PROVIDERS: tuple[ProviderSpec, ...] = (default_api_base="",strip_model_prefix=False,model_overrides=(),+ thinking_param="openai_reasoning_effort",),# DeepSeek: needs "deepseek/" prefix for LiteLLM routing.ProviderSpec(@@ -167,6 +171,7 @@ PROVIDERS: tuple[ProviderSpec, ...] = (default_api_base="https://ark.cn-beijing.volces.com/api/v3",strip_model_prefix=False,model_overrides=(),+ thinking_param="volcengine_thinking",),# Gemini: needs "gemini/" prefix for LiteLLM.ProviderSpec(@@ -220,6 +225,7 @@ PROVIDERS: tuple[ProviderSpec, ...] = (default_api_base="",strip_model_prefix=False,model_overrides=(),+ thinking_param="dashscope_enable_thinking",),# Moonshot: Kimi models, needs "moonshot/" prefix.# LiteLLM requires MOONSHOT_API_BASE env var to find the endpoint.diff --git a/bot/vikingbot/providers/vlm_adapter.py b/bot/vikingbot/providers/vlm_adapter.pyindex 07ccdb69f..afeb3005c 100644--- a/bot/vikingbot/providers/vlm_adapter.py+++ b/bot/vikingbot/providers/vlm_adapter.py@@ -81,6 +81,7 @@ class VLMProviderAdapter(LLMProvider):# --- Call VLM backend ---result = await self._vlm.get_completion_async(messages=messages,+ thinking=getattr(self._vlm, "thinking", None),tools=tools,tool_choice="auto" if tools else None,)@@ -192,8 +193,14 @@ class VLMProviderAdapter(LLMProvider):content_parts.append(content_delta)yield LLMStreamEvent(type="content_delta", content=content_delta)- for delta_tool_call in getattr(delta, "tool_calls", None) or []:- merge_stream_tool_call_delta(tool_calls, delta_tool_call)+ for fallback_index, delta_tool_call in enumerate(+ getattr(delta, "tool_calls", None) or []+ ):+ merge_stream_tool_call_delta(+ tool_calls,+ delta_tool_call,+ fallback_index=fallback_index,+ )if usage:self._record_vlm_usage(usage, time.perf_counter() - start_time)diff --git a/bot/vikingbot/tests/unit/test_bot_provider_thinking.py b/bot/vikingbot/tests/unit/test_bot_provider_thinking.pynew file mode 100644index 000000000..bf477ec66--- /dev/null+++ b/bot/vikingbot/tests/unit/test_bot_provider_thinking.py@@ -0,0 +1,276 @@+import contextlib+import sys+from types import SimpleNamespace++import pytest++from vikingbot.config.schema import AgentsConfig+from vikingbot.providers.litellm_provider import LiteLLMProvider+from vikingbot.providers.vlm_adapter import VLMProviderAdapter+++def test_agents_config_defaults_thinking_enabled():+ assert AgentsConfig().thinking is True+ assert AgentsConfig(thinking=False).thinking is False+++def test_make_provider_passes_default_thinking_to_vlm_adapter(monkeypatch):+ monkeypatch.setitem(+ sys.modules,+ "prompt_toolkit",+ SimpleNamespace(PromptSession=object),+ )+ monkeypatch.setitem(+ sys.modules,+ "prompt_toolkit.formatted_text",+ SimpleNamespace(HTML=lambda value: value),+ )+ monkeypatch.setitem(+ sys.modules,+ "prompt_toolkit.history",+ SimpleNamespace(FileHistory=object),+ )+ monkeypatch.setitem(+ sys.modules,+ "prompt_toolkit.patch_stdout",+ SimpleNamespace(patch_stdout=lambda: contextlib.nullcontext()),+ )++ from vikingbot.cli.commands import _make_provider++ captured = {}++ def fake_create(config):+ captured.update(config)+ return SimpleNamespace(+ provider=config["provider"],+ model=config["model"],+ thinking=config["thinking"],+ )++ monkeypatch.setattr("openviking.models.vlm.base.VLMFactory.create", fake_create)++ provider = _make_provider(+ SimpleNamespace(+ agents=SimpleNamespace(+ model="ep-test",+ temperature=0.0,+ thinking=True,+ api_key="ak-test",+ api_base="https://example.invalid",+ provider="volcengine",+ extra_headers={},+ timeout=None,+ )+ )+ )++ assert captured["thinking"] is True+ assert provider._vlm.thinking is True+++async def test_litellm_bot_provider_enables_volcengine_thinking(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="ak-test",+ default_model="volcengine/ep-test",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert captured["thinking"] == {"type": "enabled"}+++async def test_litellm_bot_provider_enables_dashscope_thinking(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="sk-test",+ default_model="qwen-plus",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert captured["extra_body"] == {"enable_thinking": True}+++async def test_vlm_adapter_preserves_dashscope_thinking(monkeypatch):+ from openviking.models.vlm.backends.litellm_vlm import LiteLLMVLMProvider++ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr(+ "openviking.models.vlm.backends.litellm_vlm.acompletion",+ fake_acompletion,+ )++ vlm = LiteLLMVLMProvider(+ {+ "provider": "dashscope",+ "model": "qwen-plus",+ "api_key": "sk-test",+ "thinking": True,+ }+ )+ provider = VLMProviderAdapter(vlm, default_model="qwen-plus")++ response = await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert response.content == "ok"+ assert captured["model"] == "dashscope/qwen-plus"+ assert captured["extra_body"] == {"enable_thinking": True}+++async def test_litellm_bot_provider_does_not_send_thinking_to_generic_openai(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="sk-test",+ default_model="gpt-4o",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert "thinking" not in captured+ assert "extra_body" not in captured+++async def test_litellm_bot_provider_enables_openai_reasoning_model(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="sk-test",+ default_model="gpt-5",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert captured["reasoning_effort"] == "low"+++async def test_litellm_bot_provider_does_not_send_dashscope_param_to_gemini(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="sk-test",+ default_model="gemini/gemini-2.5-pro",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert "thinking" not in captured+ assert "extra_body" not in captured+ assert "reasoning_effort" not in captured+++async def test_litellm_bot_provider_does_not_send_dashscope_param_to_zhipu(monkeypatch):+ captured = {}++ async def fake_acompletion(**kwargs):+ captured.update(kwargs)+ return SimpleNamespace(+ choices=[+ SimpleNamespace(+ message=SimpleNamespace(content="ok", tool_calls=None),+ finish_reason="stop",+ )+ ],+ usage=None,+ )++ monkeypatch.setattr("vikingbot.providers.litellm_provider.acompletion", fake_acompletion)++ provider = LiteLLMProvider(+ api_key="sk-test",+ default_model="glm-4",+ )+ await provider.chat(messages=[{"role": "user", "content": "hi"}])++ assert "thinking" not in captured+ assert "extra_body" not in captured+ assert "reasoning_effort" not in captureddiff --git a/bot/vikingbot/tests/unit/test_stream_tool_calls.py b/bot/vikingbot/tests/unit/test_stream_tool_calls.pynew file mode 100644index 000000000..abb7b1486--- /dev/null+++ b/bot/vikingbot/tests/unit/test_stream_tool_calls.py@@ -0,0 +1,73 @@+from types import SimpleNamespace++from vikingbot.providers.base import build_stream_response, merge_stream_tool_call_delta+++def _tool_call(*, index=None, call_id=None, name=None, arguments=None):+ return SimpleNamespace(+ index=index,+ id=call_id,+ function=SimpleNamespace(name=name, arguments=arguments),+ )+++def test_missing_stream_tool_call_index_uses_chunk_local_order():+ raw_tool_calls = {}+ chunks = [+ [+ _tool_call(call_id="call_search", name="search", arguments='{"query"'),+ _tool_call(call_id="call_read", name="read_file", arguments='{"path"'),+ ],+ [+ _tool_call(arguments=':"openviking"}'),+ _tool_call(arguments=':"bot.py"}'),+ ],+ ]++ for chunk in chunks:+ for fallback_index, delta_tool_call in enumerate(chunk):+ merge_stream_tool_call_delta(+ raw_tool_calls,+ delta_tool_call,+ fallback_index=fallback_index,+ )++ response = build_stream_response(+ content="",+ reasoning_content="",+ raw_tool_calls=raw_tool_calls,+ finish_reason="tool_calls",+ )++ assert [(tc.id, tc.name, tc.arguments) for tc in response.tool_calls] == [+ ("call_search", "search", {"query": "openviking"}),+ ("call_read", "read_file", {"path": "bot.py"}),+ ]+++def test_explicit_stream_tool_call_index_wins_over_chunk_local_order():+ raw_tool_calls = {}++ for fallback_index, delta_tool_call in enumerate(+ [+ _tool_call(index=1, call_id="call_second", name="second", arguments="{}"),+ _tool_call(index=0, call_id="call_first", name="first", arguments="{}"),+ ]+ ):+ merge_stream_tool_call_delta(+ raw_tool_calls,+ delta_tool_call,+ fallback_index=fallback_index,+ )++ response = build_stream_response(+ content="",+ reasoning_content="",+ raw_tool_calls=raw_tool_calls,+ finish_reason="tool_calls",+ )++ assert [(tc.id, tc.name) for tc in response.tool_calls] == [+ ("call_first", "first"),+ ("call_second", "second"),+ ]diff --git a/bot/vikingbot/utils/__init__.py b/bot/vikingbot/utils/__init__.pyindex 265714801..5b3a3d08b 100644--- a/bot/vikingbot/utils/__init__.py+++ b/bot/vikingbot/utils/__init__.py@@ -15,8 +15,15 @@ from vikingbot.utils.helpers import (get_mochat_path,get_mounts_path,)+from vikingbot.utils.image_format import (+ ImageFormat,+ detect_image_format,+ image_format_from_mime,+)__all__ = [+ "ImageFormat",+ "detect_image_format","ensure_dir","get_workspace_path","get_data_path",@@ -30,4 +37,5 @@ __all__ = ["get_received_path","get_mochat_path","get_mounts_path",+ "image_format_from_mime",]diff --git a/bot/vikingbot/utils/image_format.py b/bot/vikingbot/utils/image_format.pynew file mode 100644index 000000000..8ec7bfff5--- /dev/null+++ b/bot/vikingbot/utils/image_format.py@@ -0,0 +1,43 @@+"""Image format detection helpers."""++from typing import NamedTuple+++class ImageFormat(NamedTuple):+ extension: str+ mime_type: str+++PNG_FORMAT = ImageFormat("png", "image/png")++_MIME_FORMATS = {+ "image/png": PNG_FORMAT,+ "image/jpeg": ImageFormat("jpg", "image/jpeg"),+ "image/jpg": ImageFormat("jpg", "image/jpeg"),+ "image/gif": ImageFormat("gif", "image/gif"),+ "image/webp": ImageFormat("webp", "image/webp"),+ "image/bmp": ImageFormat("bmp", "image/bmp"),+}+++def image_format_from_mime(mime_type: str | None) -> ImageFormat | None:+ """Return a supported image format for a MIME type."""+ if not mime_type:+ return None+ return _MIME_FORMATS.get(mime_type.lower().split(";", 1)[0].strip())+++def detect_image_format(image_data: bytes, fallback_mime: str | None = None) -> ImageFormat:+ """Detect common image formats from magic bytes, falling back to a MIME hint."""+ if image_data.startswith(b"\x89PNG\r\n\x1a\n"):+ return PNG_FORMAT+ if image_data.startswith(b"\xff\xd8\xff"):+ return ImageFormat("jpg", "image/jpeg")+ if image_data.startswith((b"GIF87a", b"GIF89a")):+ return ImageFormat("gif", "image/gif")+ if image_data.startswith(b"BM"):+ return ImageFormat("bmp", "image/bmp")+ if len(image_data) >= 12 and image_data[:4] == b"RIFF" and image_data[8:12] == b"WEBP":+ return ImageFormat("webp", "image/webp")++ return image_format_from_mime(fallback_mime) or PNG_FORMATdiff --git a/openviking/models/vlm/backends/litellm_vlm.py b/openviking/models/vlm/backends/litellm_vlm.pyindex 43bca2ae6..6d413f5f2 100644--- a/openviking/models/vlm/backends/litellm_vlm.py+++ b/openviking/models/vlm/backends/litellm_vlm.py@@ -262,7 +262,7 @@ class LiteLLMVLMProvider(VLMBase):messages: list,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,- thinking: bool = False,+ thinking: Optional[bool] = None,) -> dict[str, Any]:"""Build kwargs for LiteLLM call."""kwargs: dict[str, Any] = {@@ -292,7 +292,7 @@ class LiteLLMVLMProvider(VLMBase):provider = self._detected_provider or detect_provider_by_model(model)if provider == "dashscope":extra = kwargs.get("extra_body", {})- extra["enable_thinking"] = thinking+ extra["enable_thinking"] = self._effective_thinking(thinking)kwargs["extra_body"] = extra# Workaround for LiteLLM bug where Gemini context-caching path emits@@ -311,6 +311,9 @@ class LiteLLMVLMProvider(VLMBase):return kwargs+ def _effective_thinking(self, thinking: Optional[bool]) -> bool:+ return bool(self._thinking if thinking is None else thinking)+def _parse_tool_calls(self, message) -> List[ToolCall]:"""Parse tool calls from LiteLLM response message."""tool_calls = []@@ -351,7 +354,7 @@ class LiteLLMVLMProvider(VLMBase):def _build_text_kwargs(self,prompt: str = "",- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,@@ -364,7 +367,7 @@ class LiteLLMVLMProvider(VLMBase):self,prompt: str = "",images: Optional[List[Union[str, Path, bytes]]] = None,- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,@@ -384,7 +387,7 @@ class LiteLLMVLMProvider(VLMBase):def get_completion(self,prompt: str = "",- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,@@ -413,7 +416,7 @@ class LiteLLMVLMProvider(VLMBase):async def get_completion_async(self,prompt: str = "",- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,@@ -444,7 +447,7 @@ class LiteLLMVLMProvider(VLMBase):self,prompt: str = "",images: Optional[List[Union[str, Path, bytes]]] = None,- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,@@ -472,7 +475,7 @@ class LiteLLMVLMProvider(VLMBase):self,prompt: str = "",images: Optional[List[Union[str, Path, bytes]]] = None,- thinking: bool = False,+ thinking: Optional[bool] = None,tools: Optional[List[Dict[str, Any]]] = None,tool_choice: Optional[str] = None,messages: Optional[List[Dict[str, Any]]] = None,diff --git a/tests/unit/test_vlm_thinking_param.py b/tests/unit/test_vlm_thinking_param.pyindex b5415d2e2..e790b9a77 100644--- a/tests/unit/test_vlm_thinking_param.py+++ b/tests/unit/test_vlm_thinking_param.py@@ -34,6 +34,17 @@ class TestLiteLLMThinkingParam:assert "extra_body" in kwargsassert kwargs["extra_body"]["enable_thinking"] is True+ def test_litellm_dashscope_uses_instance_thinking_by_default(self):+ """DashScope calls should inherit config thinking when no call override is passed."""+ vlm = self._make_provider("qwen-plus", thinking=True)+ model = vlm._resolve_model("qwen-plus")+ messages = [{"role": "user", "content": "hello"}]++ kwargs = vlm._build_kwargs(model, messages)++ assert "extra_body" in kwargs+ assert kwargs["extra_body"]["enable_thinking"] is True+def test_litellm_non_dashscope_no_thinking_field(self):"""Non-DashScope model should NOT have enable_thinking in extra_body."""vlm = self._make_provider("gpt-4o")