Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
feat(vibey): show hosted tool calls as Web cards and IM lines, and link provider citations (#2573)
Call White30d071bd357e
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.pyindex a8c3025a6..6c9b06c2b 100644--- a/core/agent_core/agent/loop.py+++ b/core/agent_core/agent/loop.py@@ -129,11 +129,11 @@ from core.agent_core.messages import (AssistantMessage,Message,Origin,- TextBlock,ToolCallBlock,ToolResultMessage,Usage,UserMessage,+ has_reply,message_to_dict,text,usage_to_dict,@@ -925,9 +925,7 @@ class Agent:cause=decided,)response, final = await self._commit_model_message(message, emit)- empty_reply = final and not any(- isinstance(block, TextBlock) and block.text and block.text.strip() for block in message.content- )+ empty_reply = final and not has_reply(message)if empty_reply:decided = self._outcome.primary("error")await self._error(diff --git a/core/agent_core/messages.py b/core/agent_core/messages.pyindex e3ffaca5a..f2b7fbd0c 100644--- a/core/agent_core/messages.py+++ b/core/agent_core/messages.py@@ -601,6 +601,21 @@ def text(value: str) -> TextBlock:return TextBlock(text=value)+def has_reply(message: AssistantMessage) -> bool:+ """Whether a response says something to the user: non-blank text, or an image a hosted call generated.++ The one rule for "the model replied", read by the loop's empty-reply test and+ by a final row's outcome. A generated image is a reply on its own: a model+ asked for a picture may answer with the picture alone.+ """+ for block in message.content:+ if isinstance(block, TextBlock) and block.text and block.text.strip():+ return True+ if isinstance(block, HostedToolBlock) and block.record.status == "completed" and block.record.images:+ return True+ return False++# --- persisted form ---------------------------------------------------------## Two owners, so no field can be checked differently from another:diff --git a/core/citations.py b/core/citations.pyindex b76eae7fa..a2785b0ac 100644--- a/core/citations.py+++ b/core/citations.py@@ -28,7 +28,7 @@ import reimport secretsfrom dataclasses import dataclassfrom html.entities import html5 as HTML5_ENTITIES-from typing import Any, Callable, Iterable, Mapping, Optional+from typing import Any, Callable, Iterable, Mapping, Optional, Protocolfrom urllib.parse import unquoteimport idna@@ -1065,6 +1065,61 @@ def register_citations()+class TextCitation(Protocol):+ """A citation a provider attached to a text block (C-1 ``Citation``)."""++ url: str+ title: Optional[str]+ start: Optional[int]+ end: Optional[int]+++def register_text_citations(+ blocks: Iterable[tuple[str, Iterable[TextCitation]]],+ *,+ unresolved_label: str,+ separator: str = "\n\n",+ transform: Callable[[str], str] = lambda text: text,+) -> tuple[str, Optional[CitationBundle]]:+ """Register the citations providers attach to text blocks, as ``register_citations`` does markers.++ The one builder from ``(text, TextBlock.citations)`` to the delivered shape+ (``vibey-hosted-tools.md`` §4.2): each block's text joined by ``separator``,+ with an opaque token where each citation is written out at delivery, and the+ bundle that writes Markdown links plus the Web's badge sidecar.++ A citation with a range is attributed where its cited span ends (OpenAI and+ xAI ``url_citation``); one without covers its whole block, so it is+ attributed at the block's end (Anthropic). ``transform`` runs on the joined+ text before registration - the display rules, such as dropping silent+ blocks - and a citation whose place it removed, or that falls in code, is+ left out rather than written as a stray marker.+ """+ sources: dict[str, CitationSource] = {}+ parts: list[str] = []+ for block_index, (text, citations) in enumerate(blocks):+ at: dict[int, list[str]] = {}+ # A block the display rules remove entirely shows nothing to cite; its+ # end-of-block citation would otherwise survive past the removed text.+ if not transform(text).strip():+ citations = ()+ for citation_index, citation in enumerate(citations or ()):+ ref = f"t{block_index}c{citation_index}"+ sources[ref] = CitationSource(ref_id=ref, title=citation.title or "", url=citation.url)+ end = len(text) if citation.end is None else min(max(citation.end, 0), len(text))+ at.setdefault(end, []).append(ref)+ marked = text+ for offset in sorted(at, reverse=True):+ marker = f"{_START}cite{_SEP}{_SEP.join(at[offset])}{_END}"+ marked = marked[:offset] + marker + marked[offset:]+ parts.append(marked)+ joined = transform(separator.join(part for part in parts if part))+ if not sources:+ return joined, None+ registered, bundle = register_citations(joined, sources, unresolved_label=unresolved_label)+ return CITATION_MARKER_RE.sub("", registered or ""), bundle++def materialize_citations(text: Optional[str],bundle: Optional[CitationBundle],diff --git a/core/controller.py b/core/controller.pyindex 51f6179d6..6b7da394d 100644--- a/core/controller.py+++ b/core/controller.py@@ -1945,6 +1945,7 @@ class Controller:terminal_error: Optional[str] = None,delivery: Any = None,citations: Optional[CitationBundle] = None,+ hosted: Optional[dict] = None,):"""Backward-compatible entrypoint; delegated to message dispatcher."""result = await self.message_dispatcher.emit_agent_message(@@ -1965,6 +1966,7 @@ class Controller:# signature of every stand-in. The citation sidecar rides the same way.**({"delivery": delivery} if delivery is not None else {}),**({"citations": citations} if citations else {}),+ **({"hosted": hosted} if hosted else {}),)manager = getattr(self, "session_turns", None)complete = getattr(manager, "on_terminal_delivery_complete", None)diff --git a/core/hosted_activity.py b/core/hosted_activity.pynew file mode 100644index 000000000..799d10abb--- /dev/null+++ b/core/hosted_activity.py@@ -0,0 +1,315 @@+"""How a hosted tool call is shown: the Web card's data and the IM line (``vibey-hosted-tools.md`` §6.4).++One projection from the canonical C-1 records, so the card, the IM line and any+later surface render the same facts for a native and a proxied call alike:++- a native call is a ``HostedToolBlock`` in an ``AssistantMessage``; the+ response-scoped ``hosted_summary`` (cost, cited posts) belongs to the whole+ response, so it is shown once, on the response's last hosted call;+- a proxied call is a ``ToolCallBlock`` whose ``ToolResultMessage`` carries+ ``hosted``; its record and executor are its own.++Nothing here prices, routes or replays anything: it reads what the producers+recorded. ``HostedActivity.to_payload`` is the wire shape a ``toolcall`` row+carries as ``content.hosted`` (version 1), read by the Web activity group.+"""++from __future__ import annotations++from dataclasses import dataclass+from typing import Any, Callable, Literal, Optional++from core.agent_core.messages import (+ AssistantMessage,+ HostedAction,+ HostedCost,+ HostedImage,+ HostedResult,+ HostedSource,+ HostedToolBlock,+ ToolCallBlock,+)+from vibe.i18n import t as i18n_t++HOSTED_ACTIVITY_VERSION = 1++# The longest query or prompt an IM line quotes; the Web card shows it whole.+LINE_SUBJECT_MAX_CHARS = 60++SourceNames = Callable[[str], Optional[str]]+++def _no_names(_source_id: str) -> Optional[str]:+ return None+++@dataclass(frozen=True)+class HostedActivity:+ """One hosted call as the user sees it."""++ call_id: str+ tool: str+ mode: Literal["native", "proxy"]+ status: str+ error: Optional[str]+ # What the call was about: the query, prompt, page or thread it named.+ subject: Optional[str]+ # The provider-neutral action type for a native call; a proxied call has none.+ action: Optional[str]+ extra_queries: tuple[str, ...]+ # The vendor that ran it (the response's for native, the executor's for proxy).+ provider: Optional[str]+ source_id: Optional[str]+ source_name: Optional[str]+ model: Optional[str]+ sources: tuple[HostedSource, ...]+ images: tuple[HostedImage, ...]+ size: Optional[str]+ quality: Optional[str]+ # The prompt the image model actually rendered, when the provider reported it.+ revised_prompt: Optional[str]+ cost: Optional[HostedCost]+ # How many calls ``cost`` covers: a native response's figure is for all its calls.+ cost_calls: int+ duration_ms: Optional[int] = None+ # The committed response row a native call came in. Its card is ordered just+ # before that row, as the call happened before the text that reads its result.+ response_id: Optional[str] = None++ def to_payload(self) -> dict[str, Any]:+ payload: dict[str, Any] = {+ "version": HOSTED_ACTIVITY_VERSION,+ "call_id": self.call_id,+ "tool": self.tool,+ "mode": self.mode,+ "status": self.status,+ "sources": [_source_payload(source) for source in self.sources],+ "images": [_image_payload(image) for image in self.images],+ }+ optional = {+ "error": self.error,+ "subject": self.subject,+ "action": self.action,+ "provider": self.provider,+ "source_id": self.source_id,+ "source_name": self.source_name,+ "model": self.model,+ "size": self.size,+ "quality": self.quality,+ "revised_prompt": self.revised_prompt,+ "duration_ms": self.duration_ms,+ "response_id": self.response_id,+ }+ payload.update({key: value for key, value in optional.items() if value is not None})+ if self.extra_queries:+ payload["extra_queries"] = list(self.extra_queries)+ if self.cost is not None:+ cost: dict[str, Any] = {"basis": self.cost.basis, "calls": self.cost_calls}+ if self.cost.usd is not None:+ cost["usd"] = self.cost.usd+ payload["cost"] = cost+ return payload+++def _source_payload(source: HostedSource) -> dict[str, Any]:+ out: dict[str, Any] = {"url": source.url}+ for name in ("title", "snippet", "page_age"):+ value = getattr(source, name)+ if value:+ out[name] = value+ return out+++def _image_payload(image: HostedImage) -> dict[str, Any]:+ out: dict[str, Any] = {"path": image.path, "mime": image.mime}+ if image.width is not None:+ out["width"] = image.width+ if image.height is not None:+ out["height"] = image.height+ return out+++def _subject(action: HostedAction) -> tuple[Optional[str], tuple[str, ...]]:+ if action.type == "search" and action.queries:+ return action.queries[0], tuple(action.queries[1:])+ for name in ("query", "prompt", "pattern", "url", "ref", "name"):+ value = getattr(action, name)+ if value:+ return value, ()+ return None, ()+++def native_activities(+ message: AssistantMessage,+ *,+ source_names: SourceNames = _no_names,+ response_id: Optional[str] = None,+) -> list[HostedActivity]:+ """Every native hosted call in a response, in content order.++ The response's summary is shown once: its cost on the last hosted call, and+ its cited posts on the last X search that has no sources of its own (xAI+ reports X-search results only as citations on the answer).+ """++ blocks = [block for block in message.content if isinstance(block, HostedToolBlock)]+ if not blocks:+ return []+ summary = message.hosted_summary+ cited_owner = next(+ (+ index+ for index in range(len(blocks) - 1, -1, -1)+ if blocks[index].tool == "x_search" and not blocks[index].record.sources+ ),+ None,+ )+ activities = []+ for index, block in enumerate(blocks):+ subject, extra = _subject(block.action)+ sources = block.record.sources+ if index == cited_owner and summary is not None:+ sources = summary.cited_sources+ last = index == len(blocks) - 1+ activities.append(+ HostedActivity(+ call_id=block.id,+ tool=block.tool,+ mode="native",+ status=block.record.status,+ error=block.record.error,+ subject=subject,+ action=block.action.type,+ extra_queries=extra,+ provider=message.origin.provider,+ source_id=block.native.source_id,+ source_name=source_names(block.native.source_id),+ model=message.origin.model,+ sources=tuple(sources),+ images=tuple(block.record.images or ()),+ size=block.action.size,+ quality=block.action.quality,+ # A native generate action's prompt is the provider's revised prompt.+ revised_prompt=block.action.prompt if block.action.type == "generate" else None,+ cost=summary.cost if last and summary is not None else None,+ cost_calls=len(blocks),+ response_id=response_id,+ )+ )+ return activities+++def proxy_activity(+ call: ToolCallBlock,+ result: HostedResult,+ *,+ source_names: SourceNames = _no_names,+ duration_ms: Optional[int] = None,+) -> HostedActivity:+ """A proxied hosted call: the model's own call, and the record its executor returned."""++ arguments = call.arguments if isinstance(call.arguments, dict) else {}+ subject = next(+ (value for name in ("query", "prompt") if isinstance(value := arguments.get(name), str) and value),+ None,+ )+ size = arguments.get("size") if isinstance(arguments.get("size"), str) else None+ execution = result.execution+ return HostedActivity(+ call_id=call.id,+ tool=call.name,+ mode="proxy",+ status=result.record.status,+ error=result.record.error,+ subject=subject,+ action=None,+ extra_queries=(),+ provider=execution.executor_origin.provider if execution is not None else None,+ source_id=execution.executor_source_id if execution is not None else None,+ source_name=source_names(execution.executor_source_id) if execution is not None else None,+ model=execution.executor_origin.model if execution is not None else None,+ sources=tuple(result.record.sources),+ images=tuple(result.record.images or ()),+ size=size,+ quality=None,+ revised_prompt=None,+ cost=execution.cost if execution is not None else None,+ cost_calls=1,+ duration_ms=duration_ms,+ )+++def _vendor_label(provider: Optional[str]) -> Optional[str]:+ if not provider:+ return None+ from vibe.model_hub_runtime.api_key_vendors import catalog_api_key_vendor_label++ return catalog_api_key_vendor_label(provider) or provider+++def _usd(value: float) -> str:+ if value >= 0.1:+ return f"{value:.2f}"+ # Small per-call figures keep their first significant digits: $0.006, not $0.01.+ return f"{value:.3f}".rstrip("0").rstrip(".") if value >= 0.001 else f"{value:.4f}".rstrip("0").rstrip(".")+++def _clip(text: str, limit: int = LINE_SUBJECT_MAX_CHARS) -> str:+ flat = " ".join(text.split())+ return flat if len(flat) <= limit else flat[: limit - 1].rstrip() + "…"+++def _count(activity: HostedActivity, lang: str) -> Optional[str]:+ if activity.tool == "image_generation":+ count, unit = len(activity.images), "image"+ elif activity.tool == "x_search":+ count, unit = len(activity.sources), "post"+ else:+ count, unit = len(activity.sources), "source"+ if count == 0:+ return None+ key = f"hostedTools.count.{unit}One" if count == 1 else f"hostedTools.count.{unit}s"+ return i18n_t(key, lang, count=count)+++def _cost(activity: HostedActivity, lang: str) -> Optional[str]:+ cost = activity.cost+ if cost is None:+ return None+ if cost.basis == "included":+ text = i18n_t("hostedTools.cost.included", lang)+ elif cost.usd is None:+ return None+ else:+ key = "hostedTools.cost.list" if cost.basis == "list" else "hostedTools.cost.reported"+ text = i18n_t(key, lang, usd=_usd(cost.usd))+ if activity.cost_calls > 1:+ text = i18n_t("hostedTools.cost.calls", lang, cost=text, calls=activity.cost_calls)+ return text+++def hosted_line(activity: HostedActivity, lang: str, *, escape: Callable[[str], str] = lambda text: text) -> str:+ """The one-line form IM shows per hosted call.++ ``🔎 Web search "query" · native (xAI) · 6 sources · $0.01``. A failed call+ says why instead of counting results it never had. ``escape`` makes the+ quoted subject safe for the destination's markup.+ """++ emoji = "🖼" if activity.tool == "image_generation" else "🔎"+ parts = [f"{emoji} {i18n_t(f'hostedTools.tool.{activity.tool}', lang)}"]+ if activity.subject:+ parts[0] += " " + i18n_t("hostedTools.quoted", lang, subject=escape(_clip(activity.subject)))+ if activity.status in ("failed", "incomplete"):+ parts.append(i18n_t(f"hostedTools.status.{activity.status}", lang, error=escape(activity.error or "")))+ return " · ".join(parts)+ vendor = _vendor_label(activity.provider)+ if vendor:+ parts.append(i18n_t(f"hostedTools.mode.{activity.mode}", lang, provider=vendor))+ if activity.status == "pending":+ parts.append(i18n_t("hostedTools.status.pending", lang))+ return " · ".join(parts)+ for part in (_count(activity, lang), _cost(activity, lang)):+ if part:+ parts.append(part)+ return " · ".join(parts)diff --git a/core/message_dispatcher.py b/core/message_dispatcher.pyindex 05083f17c..52aa8d48e 100644--- a/core/message_dispatcher.py+++ b/core/message_dispatcher.py@@ -2590,6 +2590,7 @@ class ConsolidatedMessageDispatcher:terminal_error: Optional[str] = None,delivery: DeliveryEvidence | None = None,citations: Optional[CitationBundle] = None,+ hosted: Optional[dict] = None,) -> Optional[str]:"""Centralized dispatch for agent messages.@@ -2633,6 +2634,10 @@ class ConsolidatedMessageDispatcher:outputs remain in the current Turn, while detached outputs are deliveredto the Session/IM surface without touching a newer Turn or its stream.+ ``hosted`` is a ``toolcall``'s hosted-tool card (``core.hosted_activity``,+ ``HostedActivity.to_payload``). It rides on the persisted row for the Web+ activity group; every IM surface delivers ``text``, its one-line form.+``citations`` is the registered source bundle for a message whose textstill carries an opaque token where each cited marker was (see``core.citations``). Every copy this method produces writes those tokens@@ -3678,6 +3683,7 @@ class ConsolidatedMessageDispatcher:citations=citations,metadata={TRANSCRIPT_COPY_KEY: "interim"} if interim_row else None,existing_row_id=existing_row_id,+ hosted=hosted if canonical_type == "toolcall" else None,)# The row above is the only copy of an intermediate message that carries adiff --git a/core/message_mirror.py b/core/message_mirror.pyindex 76996f29f..c8a25df88 100644--- a/core/message_mirror.py+++ b/core/message_mirror.py@@ -23,8 +23,10 @@ the live IM reply path.from __future__ import annotationsimport logging+import osimport timefrom datetime import datetime, timezone+from pathlib import Pathfrom typing import Any, Optionalfrom sqlalchemy.exc import IntegrityError@@ -220,6 +222,53 @@ def _publish_activity_event(event_row: Optional[dict]) -> None:logger.debug("message_mirror: activity tool_call publish failed", exc_info=True)+def _hosted_card(conn, context: MessageContext, hosted: dict, *, scope_id, session_id) -> dict:+ """A hosted-tool card as the row stores it: on the Workbench, each image gains its media URL.++ The card is a trace row, which never passes the transcript's media rewrite,+ so its images are registered here, in the row's own transaction. The media+ store reuses one token per (session, file), so the answer that links the+ same file resolves to the same URL and the Web can join the two.+ """+ images = hosted.get("images") if isinstance(hosted.get("images"), list) else []+ if context.platform != "avibe" or not session_id or not images:+ return hosted+ from core.workbench_media import register_agent_reply_media+ from storage import media_service++ registered = []+ for image in images:+ path = image.get("path") if isinstance(image, dict) else None+ if not isinstance(path, str) or not os.path.isabs(path):+ registered.append(image)+ continue+ try:+ # Canonical, as the transcript's rewrite registers it, so both share one token.+ canonical = str(Path(path).resolve())+ token = register_agent_reply_media(+ conn,+ scope_id=scope_id,+ session_id=session_id,+ kind="image",+ local_path=canonical,+ file_name=os.path.basename(canonical),+ )+ row = media_service.get_by_token(conn, token) or {}+ except Exception:+ logger.exception("persist_agent_message: hosted image registration failed")+ registered.append(image)+ continue+ url = f"/api/media/{token}"+ width, height = row.get("width_px"), row.get("height_px")+ if width and height:+ url = f"{url}?w={width}&h={height}"+ extra = {"media_url": url}+ if row.get("size_bytes") is not None:+ extra["bytes"] = row["size_bytes"]+ registered.append({**image, **extra})+ return {**hosted, "images": registered}++def _session_row(conn, session_id: str) -> Optional[dict]:"""Resolve scope/provenance from an agent session."""from sqlalchemy import select@@ -299,6 +348,7 @@ def persist_agent_message(native_message_id: Optional[str] = None,error_sink: Optional[list] = None,existing_row_id: Optional[str] = None,+ hosted: Optional[dict] = None,) -> Optional[dict]:"""Persist one agent output into the workbench ``messages`` store.@@ -380,8 +430,24 @@ def persist_agent_message(# A trace row carries text and no sidecar, so its copy is# written here rather than finalized with spans.text=materialize_citations(text, citations) or text,- content={"kind": canonical_type or "tool_call"},- metadata={"canonical_type": canonical_type or "tool_call"},+ content={+ "kind": canonical_type or "tool_call",+ **(+ {"hosted": _hosted_card(conn, context, hosted, scope_id=scope_id, session_id=row_session_id)}+ if hosted+ else {}+ ),+ },+ metadata={+ "canonical_type": canonical_type or "tool_call",+ # A native hosted call happened before the text of the response that+ # carried it, though it is emitted after that row commits.+ **(+ {"precedes_message_id": hosted["response_id"]}+ if hosted and isinstance(hosted.get("response_id"), str)+ else {}+ ),+ },agent_name=agent_name,backend=backend,turn_id=turn_id,diff --git a/modules/agents/vibey/agent.py b/modules/agents/vibey/agent.pyindex 92188a75d..83591b60f 100644--- a/modules/agents/vibey/agent.py+++ b/modules/agents/vibey/agent.py@@ -23,6 +23,7 @@ One Turn is one ``Agent.run`` (plan section 4). The adapterfrom __future__ import annotationsimport asyncio+import jsonimport loggingimport osimport secrets@@ -46,6 +47,7 @@ from core.agent_core.agent.events import (CompactionFinished,MessageCommitted,RunEnded,+ ToolFinished,ToolStarted,)from core.agent_core.agent.hooks import AgentInput@@ -58,10 +60,15 @@ from core.agent_core.harness.store import ContextEntryfrom core.agent_core.tools.base import CallInstancefrom core.agent_core.tools.jobs import call_instancefrom core.agent_core.messages import (+ HOSTED_TOOLS,IMAGE_MIME_TYPES,AssistantMessage,+ TextBlock,ToolCallBlock,+ ToolResultMessage,UserMessage,+ has_reply,+ message_from_dict,text as text_block,)from core.agent_input import AgentInputMetadata@@ -74,6 +81,8 @@ from core.services.agent_steering import (result as steer_result,)from core.backend_failure import emit_backend_failure+from core.citations import CitationBundle, register_text_citations+from core.hosted_activity import HostedActivity, hosted_line, native_activities, proxy_activityfrom core.message_output import MessageOutput, stop_output_for, terminal_output_forfrom core.native_dispatch_phase import mark_backend_dispatch_attemptedfrom core.processing_indicator import STOPPED_REACTION_EMOJI@@ -105,6 +114,7 @@ from storage.agent_transcript import ()from storage.db import get_cached_sqlite_enginefrom storage.models import agent_events, agent_sessions, message_deliveries, messages, session_turns+from vibe.i18n import t as i18n_tlogger = logging.getLogger(__name__)@@ -184,6 +194,10 @@ class _Run:# Steer attempts ``Agent.steer`` accepted into this run: reconcile's in-process evidence.accepted_steers: set[str] = field(default_factory=set)stop_requested: bool = False+ # When each tool call started (monotonic), for a proxied hosted call's duration.+ tool_started: dict[str, float] = field(default_factory=dict)+ # Images the run's hosted calls generated, as (path, label), attached to its final reply.+ hosted_images: list[tuple[str, str]] = field(default_factory=list)@propertydef native_turn_id(self) -> str:@@ -616,13 +630,18 @@ class VibeyAgent(BaseAgent):async def _on_event(self, run: _Run, event: AgentEvent) -> None:if isinstance(event, MessageCommitted):+ # A response's native hosted calls show before the text that reads their results.+ await self._emit_native_hosted(run, event.message_id)if event.final:# Delivered at the end of the run, which decides how the Turn settles.run.final_row = event.message_idelse:await self._emit_narration(run, event.message_id)elif isinstance(event, ToolStarted):+ run.tool_started[event.tool_call_id] = time.monotonic()await self._emit_tool_started(run, event)+ elif isinstance(event, ToolFinished):+ await self._emit_proxy_hosted(run, event)elif isinstance(event, AgentError):run.errors.append(event)elif isinstance(event, RunEnded):@@ -660,7 +679,8 @@ class VibeyAgent(BaseAgent):failed = final_outcome(final) == "failed" or run_failed_after_commitif failed:self._log_failure(run, reason, kind, origin, diagnostic)- body = self._display_source(final, final=True)+ body, citations = self._display_with_citations(final, final=True)+ body = self._with_hosted_images(run, body)if failed and not body.strip():# A silent final whose run failed after the commit: like a final without text# of its own (``_display_source``), its row carries the explanation.@@ -678,6 +698,7 @@ class VibeyAgent(BaseAgent):started_at=request.started_at,request=request,output=replace(terminal_output_for(request), persisted_row_id=run.final_row),+ citations=citations,)else:# A reply the model chose to keep silent.@@ -685,6 +706,9 @@ class VibeyAgent(BaseAgent):context, "result", "", level="silent", output=terminal_output_for(request))return+ # Generated images are paid for and already announced: they reach the user+ # whether or not a final reply follows.+ await self._emit_orphan_images(run)if run.stop_requested and reason == "aborted":# The stop receipt IM shows, as for the other backends.await self._remove_ack_reaction(request, terminal_emoji=STOPPED_REACTION_EMOJI)@@ -1037,7 +1061,9 @@ class VibeyAgent(BaseAgent):never shows nothing for a failed Turn (loop-control.md section 2)."""value = render_text(message)- if final and not value.strip():+ # A final whose reply is a generated image has no text to show; its image+ # is attached on delivery (``_with_hosted_images``), so there is nothing to explain.+ if final and not value.strip() and not has_reply(message):kind = message.stop_reason if message.stop_reason in _EXPLAINED_STOPS else "empty_response"return error_text(kind, self._language())return strip_silent_blocks(value)@@ -1160,7 +1186,7 @@ class VibeyAgent(BaseAgent):async def _emit_narration(self, run: _Run, row_id: str) -> None:"""A non-final response through the same process path as the other backends' narration."""message = run.responses.get(row_id)- body = self._display_source(message, final=False) if message is not None else ""+ body, citations = self._display_with_citations(message, final=False) if message is not None else ("", None)if not body.strip():returntry:@@ -1169,24 +1195,156 @@ class VibeyAgent(BaseAgent):"assistant",body,output=MessageOutput(completes_turn=False, completes_run=False, persisted_row_id=row_id),+ citations=citations,)except Exception:logger.exception("Vibey could not show narration %s", row_id)+ def _display_with_citations(+ self, message: AssistantMessage, *, final: bool+ ) -> tuple[str, Optional[CitationBundle]]:+ """The display text and, when the provider cited sources, the bundle that links them.++ The same text ``_display_source`` gives, with each cited span carrying a+ token that delivery writes out as a Markdown link (IM) and a badge (Web),+ through ``core.citations`` like every other backend's citations.+ """+ blocks = [(block.text, block.citations or ()) for block in message.content if isinstance(block, TextBlock) and block.text]+ if not any(citations for _, citations in blocks):+ return self._display_source(message, final=final), None+ body, bundle = register_text_citations(+ blocks,+ unresolved_label=i18n_t("message.citationUnresolved", self._language()),+ transform=strip_silent_blocks,+ )+ if final and not body.strip():+ return self._display_source(message, final=True), None+ return body, bundle++ def _with_hosted_images(self, run: _Run, body: str) -> str:+ """The final reply with the run's generated images attached, as file links.++ A file link is how every surface delivers a local file: IM uploads it as an+ attachment, and the Web shows it inline from the same media token its card+ registered. An image the reply already links is not attached twice.+ """+ links = self._hosted_image_links(run, body)+ if not links:+ return body+ # A reply that is only images still says what it carries, as the Codex backend's does.+ lead = body.rstrip() or i18n_t("info.generatedImagesFallback", self._language())+ return f"{lead}\n\n{links}"++ @staticmethod+ def _hosted_image_links(run: _Run, body: str) -> str:+ """File links for the run's images that ``body`` does not already link.++ Only a link counts: the model is given each image's path as text, and a+ reply that mentions it in prose has still not delivered the file. A path+ that is not absolute names no file a surface could send, so it is skipped.+ """+ links = []+ for path, label in run.hosted_images:+ if not os.path.isabs(path):+ logger.warning("Vibey skipped a hosted image with a relative path: %s", path)+ continue+ uri = Path(path).as_uri()+ if f"]({uri})" in body:+ continue+ safe = label.replace("[", "(").replace("]", ")")+ links.append(f"")+ return "\n\n".join(links)++ async def _emit_orphan_images(self, run: _Run) -> None:+ """A run's generated images when it ends with no final reply to carry them."""+ links = self._hosted_image_links(run, "")+ if not links:+ return+ try:+ await self.controller.emit_agent_message(+ run.request.context,+ "output",+ f"{i18n_t('info.generatedImagesFallback', self._language())}\n\n{links}",+ output=MessageOutput(completes_turn=False, completes_run=False, records_run_output=False),+ )+ except Exception:+ logger.exception("Vibey could not deliver %d generated image(s)", len(run.hosted_images))++ def _source_name(self, source_id: str) -> Optional[str]:+ hub = getattr(self.config, "model_hub", None)+ for source in getattr(hub, "sources", None) or ():+ if getattr(source, "id", None) == source_id:+ return getattr(source, "display_name", None) or None+ return None++ async def _emit_hosted(self, run: _Run, activity: HostedActivity) -> None:+ """One hosted call: its line for IM and its card for the Web (``core.hosted_activity``)."""+ run.hosted_images.extend((image.path, Path(image.path).name) for image in activity.images)+ formatter = self._get_formatter(run.request.context)+ line = hosted_line(activity, self._language(), escape=formatter.escape_special_chars)+ try:+ await self.controller.emit_agent_message(+ run.request.context, "toolcall", line, status_label=line, hosted=activity.to_payload()+ )+ except Exception:+ logger.exception("Vibey could not show hosted call %s", activity.call_id)++ async def _emit_native_hosted(self, run: _Run, row_id: str) -> None:+ message = run.responses.get(row_id)+ if message is None:+ return+ for activity in native_activities(message, source_names=self._source_name, response_id=row_id):+ await self._emit_hosted(run, activity)++ async def _emit_proxy_hosted(self, run: _Run, event: ToolFinished) -> None:+ """A proxied hosted call, once its result is committed: the record its executor returned."""+ call = run.tool_calls.get(event.tool_call_id)+ started = run.tool_started.pop(event.tool_call_id, None)+ if call is None or call.name not in HOSTED_TOOLS:+ return+ try:+ result = await asyncio.to_thread(self._committed_tool_result, event.event_id)+ except Exception:+ logger.exception("Vibey could not read hosted result %s", event.event_id)+ result = None+ if result is None or result.hosted is None:+ # A hosted-named call that ended without a record (a plain error result):+ # it still shows, as the tool line its start would have drawn.+ await self._emit_tool_line(run, call.name, dict(call.arguments))+ return+ duration = int((time.monotonic() - started) * 1000) if started is not None else None+ await self._emit_hosted(+ run, proxy_activity(call, result.hosted, source_names=self._source_name, duration_ms=duration)+ )++ def _committed_tool_result(self, event_id: str) -> Optional[ToolResultMessage]:+ """The tool result a ``ToolFinished`` names: one row, by primary key."""+ with self._engine.connect() as conn:+ raw = conn.execute(select(agent_events.c.content_json).where(agent_events.c.id == event_id)).scalar()+ if not raw:+ return None+ message = message_from_dict((json.loads(raw) or {}).get("message") or {})+ return message if isinstance(message, ToolResultMessage) else None+async def _emit_tool_started(self, run: _Run, event: ToolStarted) -> None:+ if event.name in HOSTED_TOOLS:+ # A proxied hosted call shows once, as its card, when its result lands.+ returncall = run.tool_calls.get(event.tool_call_id)- arguments = dict(call.arguments) if call is not None else {}+ await self._emit_tool_line(run, event.name, dict(call.arguments) if call is not None else {})++ async def _emit_tool_line(self, run: _Run, name: str, arguments: dict) -> None:formatter = self._get_formatter(run.request.context)relative = _relative_to(run.cwd)try:await self.controller.emit_agent_message(run.request.context,"toolcall",- formatter.format_toolcall(event.name, arguments, get_relative_path=relative),- status_label=formatter.format_toolcall_label(event.name, arguments, get_relative_path=relative),+ formatter.format_toolcall(name, arguments, get_relative_path=relative),+ status_label=formatter.format_toolcall_label(name, arguments, get_relative_path=relative),)except Exception:- logger.exception("Vibey could not show tool %s", event.name)+ logger.exception("Vibey could not show tool %s", name)def _on_response(self, session_id: str, row_id: str, message: AssistantMessage) -> None:"""A response committed for the Session's run: keep what its delivery and tool events need."""diff --git a/storage/agent_activity_service.py b/storage/agent_activity_service.pyindex 35faeceec..48531f3d3 100644--- a/storage/agent_activity_service.py+++ b/storage/agent_activity_service.py@@ -204,8 +204,16 @@ def _emit_micros(row_id: Optional[str], ts: datetime) -> int:return int(ts.timestamp() * 1_000_000)-def _event_emit_micros(event: dict[str, Any], ts: datetime) -> int:+def _event_anchor_micros(event: dict[str, Any], ts: datetime) -> int:+ """The clock that places an event in the scanned window: its own emission, or the row it belongs to.++ A native hosted call is written after the response row that carried it, so it+ names that row (``precedes_message_id``) and belongs wherever that row does.+ """metadata = event.get("metadata") or {}+ precedes = metadata.get("precedes_message_id")+ if isinstance(precedes, str) and precedes:+ return _emit_micros(precedes, ts)legacy_message_id = metadata.get("legacy_message_id")return _emit_micros(str(legacy_message_id) if legacy_message_id else event.get("id"),@@ -213,6 +221,14 @@ def _event_emit_micros(event: dict[str, Any], ts: datetime) -> int:)+def _event_emit_micros(event: dict[str, Any], ts: datetime) -> int:+ """The event's place in the timeline: a hosted call happened before the text of+ its response, so it sorts just ahead of that row."""+ anchor = _event_anchor_micros(event, ts)+ precedes = (event.get("metadata") or {}).get("precedes_message_id")+ return anchor - 1 if isinstance(precedes, str) and precedes else anchor++def _timeline(conn, session_id: str, *, include_text: bool) -> list[dict[str, Any]]:"""Merge the recent tail of relevant messages + tool-call events into onechronologically-ordered list of classified items."""@@ -361,7 +377,9 @@ def _timeline(conn, session_id: str, *, include_text: bool) -> list[dict[str, Anfor event in events:event_ts = _parse_ts(event.get("created_at"))event_sort = _event_emit_micros(event, event_ts)- if oldest_msg_sort is not None and event_sort < oldest_msg_sort:+ # Window membership by the row the event belongs to, never by its sort key:+ # a card sorted just ahead of the oldest retained response is still its card.+ if oldest_msg_sort is not None and _event_anchor_micros(event, event_ts) < oldest_msg_sort:continueif event.get("event_type") == "silent_terminal":terminal_outcome = (event.get("metadata") or {}).get(@@ -400,6 +418,9 @@ def _timeline(conn, session_id: str, *, include_text: bool) -> list[dict[str, An"turn_id": str(event.get("turn_id") or "").strip() or None,"row_kind": "tool_call","text": event.get("text") if include_text else None,+ # A hosted-tool call carries its card (``core.hosted_activity``); every+ # other tool call is its text alone.+ "hosted": ((event.get("content") or {}).get("hosted") if include_text else None),})for turn in conn.execute(@@ -488,6 +509,7 @@ def _make_group(if isinstance(item.get("citations"), list) and item["citations"]else {}),+ **({"hosted": item["hosted"]} if isinstance(item.get("hosted"), dict) else {}),}for item in pending]diff --git a/storage/agent_transcript.py b/storage/agent_transcript.pyindex 1f1029054..6a5cb7722 100644--- a/storage/agent_transcript.py+++ b/storage/agent_transcript.py@@ -73,6 +73,7 @@ from core.agent_core.messages import (AssistantMessage,TextBlock,ToolResultMessage,+ has_reply,UserMessage,message_from_dict,message_to_dict,@@ -123,8 +124,7 @@ def final_outcome(message: AssistantMessage) -> FinalOutcome:empty-reply test (loop-control.md section 2). Failures only a live run canobserve after the commit are applied by the live caller."""- has_text = any(isinstance(block, TextBlock) and block.text and block.text.strip() for block in message.content)- return "completed" if has_text else "failed"+ return "completed" if has_reply(message) else "failed"def _response_type(message: AssistantMessage, *, final: bool, display_text: str) -> str:diff --git a/tests/agent_core/agent/test_loop.py b/tests/agent_core/agent/test_loop.pyindex 7b8769a27..14831ae48 100644--- a/tests/agent_core/agent/test_loop.py+++ b/tests/agent_core/agent/test_loop.py@@ -48,7 +48,22 @@ from core.agent_core.ai.provider import (from core.agent_core.agent.models import ModelSelectionfrom core.agent_core.harness.context import ContextConfig, budgetfrom core.agent_core.harness.projection import project-from core.agent_core.messages import AssistantMessage, ThinkingBlock, ToolCallBlock, ToolResultMessage, Usage, UserMessage, text+from core.agent_core.messages import (+ AssistantMessage,+ HostedAction,+ HostedImage,+ HostedNative,+ HostedRecord,+ HostedSource,+ HostedSummary,+ HostedToolBlock,+ ThinkingBlock,+ ToolCallBlock,+ ToolResultMessage,+ Usage,+ UserMessage,+ text,+)from core.agent_core.tools.base import CallInstance, ToolResultfrom tests.agent_core.fakes import (FakeJobHost,@@ -1191,18 +1206,41 @@ async def test_reasoning_effort_must_be_declared_by_the_route():@pytest.mark.parametrize("stop_reason", ["stop", "length", "refusal", "safety"])[email protected]("shape", ["empty", "whitespace", "thinking", "visible"])[email protected]("shape", ["empty", "whitespace", "thinking", "visible", "image", "search"])@pytest.mark.parametrize("continuation", ["final", "steer", "follow_up"])async def test_tool_free_final_reply_is_committed_and_empty_success_is_an_error(stop_reason, shape, continuation):# The old refusal/safety-only table missed empty successful finals.# Pending-input responses are non-final and must still be allowed to continue.+ # A generated image is a reply on its own; a search with no answer is not.+ native = HostedNative(source_id="src_testsrc1", payload=({"type": "provider_item"},))content = {"empty": (),"whitespace": (text(" \n"),),"thinking": (ThinkingBlock("private reasoning"),),"visible": (ThinkingBlock("private reasoning"), text("visible reply")),+ "image": (+ HostedToolBlock(+ id="ig_1",+ tool="image_generation",+ action=HostedAction(type="generate", prompt="a laptop at night"),+ record=HostedRecord(status="completed", images=(HostedImage(path="/media/ig_1.png", mime="image/png"),)),+ native=native,+ ),+ ),+ "search": (+ HostedToolBlock(+ id="ws_1",+ tool="web_search",+ action=HostedAction(type="search", queries=("avibe",)),+ record=HostedRecord(status="completed", sources=(HostedSource(url="https://avibe.bot"),)),+ native=native,+ ),+ ),}[shape]- message = replace(assistant(stop_reason=stop_reason), content=content)+ hosted = shape in ("image", "search")+ message = replace(+ assistant(stop_reason=stop_reason), content=content, hosted_summary=HostedSummary() if hosted else None+ )async def first(request, cancel):if continuation != "final":@@ -1225,7 +1263,7 @@ async def test_tool_free_final_reply_is_committed_and_empty_success_is_an_error(["input"] if continuation == "final" else ["input", "pending"])errors = [event for event in events if isinstance(event, AgentError)]- if shape == "visible" or continuation != "final":+ if shape in ("visible", "image") or continuation != "final":assert errors == []assert events[-1].reason == "completed"else:diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yamlindex 96c0fbc7a..6355bf5b2 100644--- a/tests/scenarios/INDEX.yaml+++ b/tests/scenarios/INDEX.yaml@@ -53,6 +53,7 @@ capabilities:- tests/test_model_hub_hosted_tools_config.py- tests/test_model_hub_capability_evidence.py- tests/test_provider_overlay.py+ - tests/test_hosted_activity.py- tests/agent_core/ai/test_hosted_requests.py- tests/agent_core/ai/test_hosted_parsing.py- tests/agent_core/ai/test_hosted_transform.py@@ -109,6 +110,7 @@ capabilities:- ui/src/components/settings/models/RouteChainDialog.test.tsx- ui/src/components/settings/models/BackendModelCatalogDialog.test.tsx- ui/src/components/settings/models/AgentCard.test.tsx+ - ui/src/components/workbench/HostedToolRow.test.tsx- ui/src/components/settings/models/MigrationDialog.test.tsx- ui/src/components/settings/models/migrationScan.test.ts- ui/src/components/onboarding/providers/providerStage.test.tsdiff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yamlindex 572ca9cb2..599e820dd 100644--- a/tests/scenarios/model_hub/catalog.yaml+++ b/tests/scenarios/model_hub/catalog.yaml@@ -10,6 +10,8 @@ capability:- tests/test_model_hub_hosted_tools_config.py- tests/test_model_hub_capability_evidence.py- tests/test_provider_overlay.py+ - tests/test_hosted_activity.py+ - ui/src/components/workbench/HostedToolRow.test.tsx- tests/agent_core/ai/test_hosted_requests.py- tests/agent_core/ai/test_hosted_parsing.py- tests/agent_core/ai/test_hosted_transform.py@@ -197,6 +199,47 @@ scenarios:related_tests:- tests/test_provider_overlay.py::test_bundled_providers_section_satisfies_its_schema- tests/test_provider_overlay.py::test_an_invalid_vendor_entry_disables_only_that_vendor+ - id: MH-HOSTED-UI-008+ name: The execution-process group draws a native and a proxied hosted call as one card, with how it ran, its known cost and its sources or image+ status: covered+ kind: observation+ layer: unit+ test: ui/src/components/workbench/HostedToolRow.test.tsx::MH-HOSTED-UI-008+ - id: MH-HOSTED-UI-009+ name: A hosted call's card payload is projected from the canonical C-1 records in the shape the Web renders+ status: covered+ kind: contract+ layer: contract+ test: tests/test_hosted_activity.py::test_payloads_match_the_shape_the_web_card_renders+ related_tests:+ - tests/test_hosted_activity.py::test_a_response_scoped_summary_is_shown_once+ - tests/test_agent_activity_service.py::test_a_hosted_tool_row_carries_its_card+ - tests/test_message_mirror.py::test_a_hosted_card_rides_on_its_toolcall_row_and_its_image_joins_the_answer+ - id: MH-HOSTED-UI-010+ name: IM shows one line per hosted call with tool, query, mode, count and a cost only when known+ status: covered+ kind: observation+ layer: unit+ test: tests/test_hosted_activity.py::test_the_im_line_states_tool_query_mode_count_and_known_cost+ related_tests:+ - tests/test_hosted_activity.py::test_the_im_line_names_a_subscription_and_a_shared_response_cost+ - id: MH-HOSTED-UI-012+ name: A final whose only content is a generated image is a reply, not an empty one, and reaches the user as an attachment+ status: covered+ kind: contract+ layer: contract+ test: tests/test_vibey_agent.py::test_an_image_only_final_is_a_reply_and_reaches_the_user+ related_tests:+ - tests/agent_core/agent/test_loop.py::test_tool_free_final_reply_is_committed_and_empty_success_is_an_error+ - id: MH-HOSTED-UI-011+ name: Vibey delivers a native hosted call as its IM line and Web card, its answer with provider citations as links, and its generated image as an attachment+ status: covered+ kind: contract+ layer: contract+ test: tests/test_vibey_agent.py::test_a_native_hosted_call_reaches_im_as_one_line_with_its_cited_answer_and_image+ related_tests:+ - tests/test_vibey_agent.py::test_a_native_hosted_call_reaches_the_workbench_as_a_card_and_cited_answer+ - tests/test_vibey_agent.py::test_a_proxied_hosted_result_shows_once_its_result_is_committed- id: MH-HOSTED-VIBEY-001name: Vibey declares a hop's native hosted tool verbatim with its include values and pins that request to the hop's Source; the probe builds the same requeststatus: covereddiff --git a/tests/test_agent_activity_service.py b/tests/test_agent_activity_service.pyindex b15ea2ede..b045ad533 100644--- a/tests/test_agent_activity_service.py+++ b/tests/test_agent_activity_service.py@@ -277,6 +277,109 @@ def test_a_narration_row_carries_its_citation_sidecar(isolated_state):assert "citations" not in rows["m_plain"]+def test_a_hosted_tool_row_carries_its_card(isolated_state):+ """A hosted call's card (``core.hosted_activity``) is what the Web renders for it.++ Its row text is the IM line, which the card does not parse, so the row+ contract has to carry the card or the call degrades to a plain tool line. An+ ordinary tool call keeps the wire shape it always had.+ """+ engine = create_sqlite_engine()+ sid = "ses_hosted_card"+ card = {+ "version": 1, "call_id": "call_x1", "tool": "x_search", "mode": "proxy", "status": "completed",+ "subject": "from:OpenAI", "sources": [{"url": "https://x.com/OpenAI/status/1"}], "images": [],+ }+ with engine.begin() as conn:+ scope = _seed_session(conn, session_id=sid)+ _msg(conn, scope, sid, mid="m_u", mtype="user", author="user", created_at="2026-06-01T10:00:00.000000+00:00", text="q", source="user")+ conn.execute(+ agent_events.insert().values(+ id="e_hosted", scope_id=scope, session_id=sid, platform="avibe", event_type="tool_call",+ visibility="trace", content_text="🔎 X search “from:OpenAI” · via xAI proxy · 1 post",+ content_json=json.dumps({"kind": "toolcall", "hosted": card}), metadata_json="{}", source="agent",+ created_at="2026-06-01T10:00:01.000000+00:00", updated_at="2026-06-01T10:00:01.000000+00:00",+ )+ )+ _evt(conn, scope, sid, eid="e_plain", created_at="2026-06-01T10:00:02.000000+00:00", text="🔧 `Read`")+ _msg(conn, scope, sid, mid="m_r", mtype="result", author="agent", created_at="2026-06-01T10:00:03.000000+00:00", text="done")++ with engine.connect() as conn:+ [group] = agent_activity_service.list_turn_groups(conn, session_id=sid)["groups"]+ detail = agent_activity_service.get_turn_group(conn, session_id=sid, group_id=group["id"])++ assert detail is not None+ rows = {row["id"]: row for row in detail["rows"]}+ assert rows["e_hosted"]["hosted"] == card+ assert "hosted" not in rows["e_plain"]+++def test_a_native_hosted_card_sorts_before_the_response_that_carried_it(isolated_state):+ """The card is written after its response row commits, but the call came first.++ Without the ordering key the final reply would close the group before the+ card, leaving it after the answer; narration would read before the search+ it summarizes.+ """+ engine = create_sqlite_engine()+ sid = "ses_hosted_order"+ with engine.begin() as conn:+ scope = _seed_session(conn, session_id=sid)+ _msg(conn, scope, sid, mid="msg_00063f0000000000aaaaaaaa", mtype="user", author="user", created_at="2026-06-01T10:00:00.000000+00:00", text="q", source="user")+ _msg(conn, scope, sid, mid="msg_00063f0000000100bbbbbbbb", mtype="result", author="agent", created_at="2026-06-01T10:00:02.000000+00:00", text="Domain filters shipped.")+ conn.execute(+ agent_events.insert().values(+ id="evt_00063f0000000200cccccccc", scope_id=scope, session_id=sid, platform="avibe", event_type="tool_call",+ visibility="trace", content_text="🔎 Web search",+ content_json=json.dumps({"kind": "toolcall", "hosted": {"version": 1, "call_id": "srvtoolu_1"}}),+ metadata_json=json.dumps({"canonical_type": "toolcall", "precedes_message_id": "msg_00063f0000000100bbbbbbbb"}),+ source="agent", created_at="2026-06-01T10:00:03.000000+00:00", updated_at="2026-06-01T10:00:03.000000+00:00",+ )+ )++ with engine.connect() as conn:+ groups = agent_activity_service.list_turn_groups(conn, session_id=sid)["groups"]+ detail = agent_activity_service.get_turn_group(conn, session_id=sid, group_id=groups[-1]["id"])++ assert detail is not None and detail["status"] == "done"+ assert detail["anchor_message_id"] == "msg_00063f0000000100bbbbbbbb"+ assert [row["id"] for row in detail["rows"]] == ["evt_00063f0000000200cccccccc"]+++def test_a_native_hosted_card_stays_with_its_response_at_the_scan_window_edge(isolated_state, monkeypatch):+ """The card sorts just ahead of its response, but belongs to the window that response is in.++ When the response is the oldest message the bounded scan keeps, a window cut by+ the card's sort key would drop the card while its answer stays visible.+ """+ monkeypatch.setattr(agent_activity_service, "MESSAGE_SCAN_LIMIT", 1)+ engine = create_sqlite_engine()+ sid = "ses_hosted_edge"+ with engine.begin() as conn:+ scope = _seed_session(conn, session_id=sid)+ _msg(conn, scope, sid, mid="msg_00063f0000000000aaaaaaaa", mtype="user", author="user", created_at="2026-06-01T10:00:00.000000+00:00", text="q", source="user")+ _msg(conn, scope, sid, mid="msg_00063f0000000100bbbbbbbb", mtype="result", author="agent", created_at="2026-06-01T10:00:02.000000+00:00", text="Domain filters shipped.")+ conn.execute(+ agent_events.insert().values(+ id="evt_00063f0000000200cccccccc", scope_id=scope, session_id=sid, platform="avibe", event_type="tool_call",+ visibility="trace", content_text="🔎 Web search",+ content_json=json.dumps({"kind": "toolcall", "hosted": {"version": 1, "call_id": "srvtoolu_1"}}),+ metadata_json=json.dumps({"canonical_type": "toolcall", "precedes_message_id": "msg_00063f0000000100bbbbbbbb"}),+ source="agent", created_at="2026-06-01T10:00:03.000000+00:00", updated_at="2026-06-01T10:00:03.000000+00:00",+ )+ )++ with engine.connect() as conn:+ groups = agent_activity_service.list_turn_groups(conn, session_id=sid)["groups"]+ rows = [+ row["id"]+ for group in groups+ for row in (agent_activity_service.get_turn_group(conn, session_id=sid, group_id=group["id"]) or {}).get("rows", [])+ ]++ assert rows == ["evt_00063f0000000200cccccccc"]++def test_narration_the_transcript_draws_is_not_repeated_in_activity(isolated_state):"""A narration with an ``interim`` transcript copy appears once, as the bubble."""engine = create_sqlite_engine()diff --git a/tests/test_citations.py b/tests/test_citations.pyindex 7e96d5d7e..27a120abf 100644--- a/tests/test_citations.py+++ b/tests/test_citations.py@@ -40,6 +40,7 @@ from core.citations import (has_citation_markers,materialize_citations,register_citations,+ register_text_citations,resolve_citations,safe_url,source_label,@@ -1522,3 +1523,80 @@ class TestAuthorityBracketsAcrossTheBoundary:def test_the_accepted_spelling_is_still_a_fixed_point(self, case):"""A restored bracket must not be spelled away by the next pass."""assert safe_url(case["safe_url"]) == case["safe_url"]+++class TestTextBlockCitations:+ """Citations a provider attached to text blocks, through the same delivered shape (§4.2)."""++ FILTERS = "https://platform.openai.com/docs/changelog"+ WINDOWS = "https://x.com/OpenAI/status/4"++ @staticmethod+ def cite(url, title=None, start=None, end=None):+ from core.agent_core.messages import Citation++ return Citation(url=url, title=title, start=start, end=end)++ def test_a_ranged_citation_is_attributed_where_its_span_ends(self):+ text = "Domain filters shipped. Codex is on Windows."+ registered, bundle = register_text_citations(+ [(text, [self.cite(self.FILTERS, "Changelog", 0, len("Domain filters shipped."))])],+ unresolved_label=UNRESOLVED,+ )+ body, citations = finalize_citations(registered, bundle)++ assert [row["url"] for row in citations] == [self.FILTERS]+ [span] = citations[0]["spans"]+ link = utf16_slice(body, *span)+ # Written after the sentence it supports, and before the one it does not.+ assert body.index("Domain filters shipped.") < body.index(link) < body.index("Codex is on Windows.")+ assert link.endswith(f"({self.FILTERS})")++ def test_an_unranged_citation_covers_its_whole_block(self):+ """Anthropic cites a block, not a span: the link closes that block, in the joined text."""+ registered, bundle = register_text_citations(+ [+ ("The posts this week:", []),+ ("Codex is on Windows.", [self.cite(self.WINDOWS, "The Codex app is now on Windows")]),+ ],+ unresolved_label=UNRESOLVED,+ )+ body, citations = finalize_citations(registered, bundle)++ first, second = body.split("\n\n")+ assert first == "The posts this week:"+ assert second.startswith("Codex is on Windows.") and second.endswith(f"({self.WINDOWS})")+ assert [row["index"] for row in citations] == [1]++ def test_a_citation_the_display_rules_removed_leaves_no_marker_behind(self):+ """A citation in a silent block or in code cannot be shown, so nothing of it may be."""+ silent = "Visible.<silent>internal note</silent>"+ code = "Run `vibe status` now."+ registered, bundle = register_text_citations(+ [+ (silent, [self.cite(self.FILTERS, start=len("Visible.<silent>internal"), end=len("Visible.<silent>internal"))]),+ (code, [self.cite(self.WINDOWS, start=len("Run `vibe"), end=len("Run `vibe"))]),+ ],+ unresolved_label=UNRESOLVED,+ transform=strip_silent_blocks,+ )+ body, citations = finalize_citations(registered, bundle)++ assert body == "Visible.\n\nRun `vibe status` now."+ assert citations == []++ def test_a_block_the_display_rules_remove_takes_its_citations_with_it(self):+ """An unranged citation sits after its block; when the whole block is silent, nothing is left to cite."""+ registered, bundle = register_text_citations(+ [("<silent>internal note</silent>", [self.cite(self.WINDOWS, "The Codex app is now on Windows")])],+ unresolved_label=UNRESOLVED,+ transform=strip_silent_blocks,+ )+ assert (registered, bundle) == ("", None)++ def test_text_without_citations_is_the_plain_display(self):+ registered, bundle = register_text_citations(+ [("One.", []), ("Two.", [])], unresolved_label=UNRESOLVED+ )+ assert (registered, bundle) == ("One.\n\nTwo.", None)+diff --git a/tests/test_hosted_activity.py b/tests/test_hosted_activity.pynew file mode 100644index 000000000..60c23b558--- /dev/null+++ b/tests/test_hosted_activity.py@@ -0,0 +1,244 @@+"""How a hosted tool call is shown: the card payload and the IM line (``vibey-hosted-tools.md`` §6.4).++The payloads are pinned to ``ui/src/lib/hostedActivity.fixture.json``, the+fixture the Web card tests render, so producer and consumer read one shape.+"""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from core.agent_core.messages import (+ AssistantMessage,+ HostedAction,+ HostedCost,+ HostedImage,+ HostedNative,+ HostedRecord,+ HostedResult,+ HostedSource,+ HostedSummary,+ HostedToolBlock,+ Origin,+ ProxyExecution,+ TextBlock,+ ToolCallBlock,+)+from core.hosted_activity import hosted_line, native_activities, proxy_activity++FIXTURE = json.loads(+ (Path(__file__).resolve().parents[1] / "ui/src/lib/hostedActivity.fixture.json").read_text(encoding="utf-8")+)+NAMES = {"src_relayclaude": "relay-claude", "src_grok0001": "Grok", "src_relaygpt1": "relay-gpt"}.get++DEVDAY = HostedSource(url="https://openai.com/index/devday-2026", title="DevDay 2026 recap", page_age="2 days ago")+CHANGELOG = HostedSource(url="https://platform.openai.com/docs/changelog", title="Responses API changelog")+POSTS = (+ HostedSource(+ url="https://x.com/OpenAI/status/1",+ title="DevDay 2026 recap: everything we shipped this week",+ snippet="Recordings and the launch list in one post.",+ page_age="2 days ago",+ ),+ HostedSource(url="https://x.com/OpenAIDevs/status/2", title="Web search in the Responses API now supports domain filters"),+)+PROMPT = "A wide photo of an open laptop on a desk at night, empty dark space in the upper right"+XAI = ProxyExecution(+ executor_origin=Origin(provider="xai", api="openai_responses", model="grok-4.6"),+ executor_source_id="src_grok0001",+ executor_billing="metered",+ cost=HostedCost(basis="reported", usd=0.11),+)+++def _native(source_id: str) -> HostedNative:+ return HostedNative(source_id=source_id, payload=({"type": "provider_item"},))+++def _search(call_id: str, *queries: str, sources=(), source_id="src_relayclaude") -> HostedToolBlock:+ return HostedToolBlock(+ id=call_id,+ tool="web_search",+ action=HostedAction(type="search", queries=tuple(queries)),+ record=HostedRecord(status="completed", sources=tuple(sources)),+ native=_native(source_id),+ )+++def _message(*content, summary: HostedSummary, provider="anthropic", api="anthropic", model="claude-opus-5-5"):+ return AssistantMessage(+ content=tuple(content),+ origin=Origin(provider=provider, api=api, model=model),+ stop_reason="stop",+ hosted_summary=summary,+ )+++def _x_call(call_id: str, query: str) -> ToolCallBlock:+ return ToolCallBlock(id=call_id, name="x_search", arguments={"query": query})+++def native_web_search():+ [activity] = native_activities(+ _message(+ TextBlock(text="Here is what I found."),+ _search("srvtoolu_01", "OpenAI announcements October 2026", sources=(DEVDAY, CHANGELOG)),+ summary=HostedSummary(counts={"web_search_requests": 1}, cost=HostedCost(basis="list", usd=0.01)),+ ),+ source_names=NAMES,+ )+ return activity+++def proxy_x_search():+ return proxy_activity(+ _x_call("call_x1", "from:OpenAI within_time:7d"),+ HostedResult(record=HostedRecord(status="completed", sources=POSTS), execution=XAI),+ source_names=NAMES,+ duration_ms=31000,+ )+++def native_image():+ [activity] = native_activities(+ _message(+ HostedToolBlock(+ id="ig_01",+ tool="image_generation",+ action=HostedAction(type="generate", prompt=PROMPT, size="1536x1024", quality="medium"),+ record=HostedRecord(+ status="completed",+ images=(HostedImage(path="/media/3f2a.png", mime="image/png", width=1536, height=1024),),+ ),+ native=_native("src_relaygpt1"),+ ),+ # Phase 1 knows no image price, so the response carries no cost at all.+ summary=HostedSummary(),+ provider="openai",+ api="openai_responses",+ model="gpt-6.1-sol",+ ),+ source_names=NAMES,+ )+ return activity+++def proxy_failed():+ return proxy_activity(+ _x_call("call_x2", "from:OpenAI"),+ HostedResult(record=HostedRecord(status="failed", error="no Source is verified for X search")),+ source_names=NAMES,+ )++[email protected]("build", [native_web_search, proxy_x_search, native_image, proxy_failed])+def test_payloads_match_the_shape_the_web_card_renders(build):+ """MH-HOSTED-UI-009: every mode and outcome the card distinguishes, from the canonical records each producer writes.++ A call that never reached an executor names no vendor or Source it never used.+ """++ assert build().to_payload() == FIXTURE[build.__name__]+++def test_a_response_scoped_summary_is_shown_once():+ """The response's cost covers all its calls, and xAI's cited posts belong to its X search."""++ x_search = HostedToolBlock(+ id="xs_1",+ tool="x_search",+ action=HostedAction(type="x_search", variant="keyword", query="from:xai", limit=10, mode="Latest"),+ record=HostedRecord(status="completed"),+ native=_native("src_grok0001"),+ )+ activities = native_activities(+ _message(+ _search("ws_1", "grok 5 release", "grok 5 benchmarks", sources=(DEVDAY,), source_id="src_grok0001"),+ x_search,+ _search("ws_2", "xai api pricing", source_id="src_grok0001"),+ summary=HostedSummary(+ counts={"web_search_calls": 2, "x_search_calls": 1},+ cited_sources=POSTS,+ cost=HostedCost(basis="reported", usd=0.06),+ ),+ provider="xai",+ api="openai_responses",+ model="grok-4.6",+ ),+ )++ assert [(a.call_id, a.cost, a.cost_calls) for a in activities] == [+ ("ws_1", None, 3),+ ("xs_1", None, 3),+ ("ws_2", HostedCost(basis="reported", usd=0.06), 3),+ ]+ # Only the X search without per-call results shows the response's cited posts.+ assert [a.sources for a in activities] == [(DEVDAY,), POSTS, ()]+ assert activities[0].extra_queries == ("grok 5 benchmarks",)+ assert activities[1].subject == "from:xai"+++ ("build", "lang", "line"),+ [+ (+ native_web_search,+ "en",+ "🔎 Web search “OpenAI announcements October 2026” · native (Anthropic) · 2 sources · ≈ $0.01",+ ),+ (+ native_web_search,+ "zh",+ "🔎 网页搜索 “OpenAI announcements October 2026” · 原生(Anthropic) · 2 个来源 · ≈ $0.01",+ ),+ (proxy_x_search, "zh", "🔎 X 搜索 “from:OpenAI within_time:7d” · 由 xAI 代理 · 2 条帖子 · $0.11"),+ (+ native_image,+ "en",+ "🖼 Image generation “A wide photo of an open laptop on a desk at night, empty da…”"+ " · native (OpenAI) · 1 image",+ ),+ (proxy_failed, "zh", "🔎 X 搜索 “from:OpenAI” · 失败:no Source is verified for X search"),+ ],+)+def test_the_im_line_states_tool_query_mode_count_and_known_cost(build, lang, line):+ """MH-HOSTED-UI-010: one line per hosted call, the same for either mode, with a cost only when one is known."""+ assert hosted_line(build(), lang) == line+++def test_the_im_line_names_a_subscription_and_a_shared_response_cost():+ included = proxy_activity(+ _x_call("call_x3", "from:xai within_time:3d"),+ HostedResult(+ record=HostedRecord(status="completed", sources=POSTS[:1]),+ execution=ProxyExecution(+ executor_origin=Origin(provider="openai", api="openai_responses", model="gpt-6.1-sol"),+ executor_source_id="src_chatgpt01",+ executor_billing="monthly",+ cost=HostedCost(basis="included"),+ ),+ ),+ )+ assert hosted_line(included, "zh") == "🔎 X 搜索 “from:xai within_time:3d” · 由 OpenAI 代理 · 1 条帖子 · 订阅内含"++ shared = native_activities(+ _message(+ _search("ws_1", "a"),+ _search("ws_2", "b", sources=(DEVDAY,)),+ summary=HostedSummary(cost=HostedCost(basis="list", usd=0.02)),+ )+ )[-1]+ assert hosted_line(shared, "en") == "🔎 Web search “b” · native (Anthropic) · 1 source · ≈ $0.02 for 2 calls"+++def test_the_im_line_escapes_the_quoted_subject_for_its_destination():+ activity = proxy_activity(+ _x_call("call_x4", "*bold* claim"),+ HostedResult(record=HostedRecord(status="completed"), execution=XAI),+ )+ assert hosted_line(activity, "en", escape=lambda text: text.replace("*", "\\*")).startswith(+ "🔎 X search “\\*bold\\* claim”"+ )diff --git a/tests/test_message_mirror.py b/tests/test_message_mirror.pyindex b72c794b2..22af05bc7 100644--- a/tests/test_message_mirror.py+++ b/tests/test_message_mirror.py@@ -1625,3 +1625,88 @@ def test_im_agent_output_ranks_the_row_without_publishing(isolated_state, monkey.one())assert row["last_active_at"] == RANK_NOW+++def _hosted_card(image_path: str) -> dict:+ return {+ "version": 1,+ "call_id": "ig_01",+ "tool": "image_generation",+ "mode": "native",+ "status": "completed",+ "subject": "a laptop at night",+ "sources": [],+ "images": [{"path": image_path, "mime": "image/png", "width": 2, "height": 1}],+ }+++def test_a_hosted_card_rides_on_its_toolcall_row_and_its_image_joins_the_answer(isolated_state, tmp_path):+ """The Web card reads ``content.hosted``; its image shares the answer's media URL.++ A toolcall row is trace data and never passes the transcript's media rewrite,+ so without its own registration the card could not show the image at all,+ and the answer that links the same file could not be matched to it.+ """+ image = tmp_path / "image-3f2a.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nimage")+ engine = create_sqlite_engine()+ now = "2026-10-14T00:00:00Z"+ with engine.begin() as conn:+ scope_id = upsert_scope(conn, platform="avibe", scope_type="project", native_id="proj_hosted", now=now)+ conn.execute(+ agent_sessions.insert().values(+ id="ses_hosted",+ scope_id=scope_id,+ agent_name="vibey",+ agent_backend="vibey",+ agent_variant="default",+ session_anchor="anchor_ses_hosted",+ native_session_id="",+ status="active",+ metadata_json="{}",+ created_at=now,+ updated_at=now,+ last_active_at=now,+ )+ )+ ctx = MessageContext(+ user_id="workbench",+ channel_id="ses_hosted",+ platform="avibe",+ platform_specific={"agent_session_id": "ses_hosted"},+ )++ persist_agent_message(+ ctx, "toolcall", "🖼 Image generation · 1 image", hosted={**_hosted_card(str(image)), "response_id": "msg_response"}+ )+ persist_agent_message(ctx, "result", f"Done.\n\n})")++ with engine.connect() as conn:+ event = conn.execute(select(agent_events).where(agent_events.c.session_id == "ses_hosted")).mappings().one()+ answer = conn.execute(select(messages).where(messages.c.session_id == "ses_hosted")).mappings().one()+ card = json.loads(event["content_json"])["hosted"]+ [stored] = card["images"]+ assert {key: card[key] for key in ("call_id", "tool", "mode", "status", "subject")} == {+ "call_id": "ig_01", "tool": "image_generation", "mode": "native", "status": "completed",+ "subject": "a laptop at night",+ }+ assert stored["path"] == str(image) and stored["bytes"] == image.stat().st_size+ token = stored["media_url"].split("?")[0]+ assert token.startswith("/api/media/")+ assert token in answer["content_text"]+ assert event["content_text"] == "🖼 Image generation · 1 image"+ # Ordered before the response that carried it (``agent_activity_service``).+ assert json.loads(event["metadata_json"])["precedes_message_id"] == "msg_response"+++def test_an_im_hosted_card_keeps_its_file_paths(isolated_state, tmp_path):+ """Only the Workbench serves media URLs; an IM row stores the card as given."""+ ctx = _slack_ctx()+ mirror_inbound(ctx, "draw something")+ card = _hosted_card(str(tmp_path / "image.png"))+ persist_agent_message(ctx, "toolcall", "🖼 Image generation", hosted=card)++ engine = create_sqlite_engine()+ with engine.connect() as conn:+ event = conn.execute(select(agent_events)).mappings().one()+ assert json.loads(event["content_json"])["hosted"] == carddiff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.pyindex 0c79e63f9..8bd714067 100644--- a/tests/test_vibey_agent.py+++ b/tests/test_vibey_agent.py@@ -33,14 +33,25 @@ from typing import Any, Optionalimport pytestfrom sqlalchemy import select, update-from core.agent_core.agent.events import NAMED_ERROR_KINDS+from core.agent_core.agent.events import NAMED_ERROR_KINDS, ToolFinishedfrom core.agent_core.agent.recovery import UNRECORDED_EFFECTfrom core.agent_core.ai.provider import Donefrom core.agent_core.harness.projection import projectfrom core.agent_core.messages import (AssistantMessage,+ Citation,+ HostedAction,+ HostedCost,+ HostedImage,+ HostedNative,+ HostedRecord,+ HostedResult,+ HostedSource,+ HostedSummary,HostedToolBlock,ImageBlock,+ Origin,+ ProxyExecution,TextBlock,ThinkingBlock,ToolCallBlock,@@ -100,6 +111,9 @@ class _Formatter:def format_result_footer(self, subtype: str, *_args: Any, **_kwargs: Any) -> str:return "❌ failed" if subtype == "error" else "✅ done"+ def escape_special_chars(self, text: str) -> str:+ return text.replace("*", "\\*")+class _IMClient:"""Records every send; ``fail_sends`` names 1-based send attempts the platform rejects.@@ -2154,14 +2168,33 @@ async def test_im_narration_is_live_best_effort_output(engine, session, tmp_pathassert "Third look." not in hidden.controller.im_client.sent[email protected]("final, row_type", [("answer", "result"), ("empty", "error"), ("refusal", "error")])+ "final, row_type", [("answer", "result"), ("empty", "error"), ("refusal", "error"), ("image", "result")]+)async def test_a_final_is_stored_with_its_outcome_semantics(engine, session, tmp_path, published, final, row_type) -> None:+ image = tmp_path / "image-final.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nimage")message = {"answer": assistant("here it is"),"empty": assistant(""),"refusal": AssistantMessage((), ORIGIN, "refusal"),+ # A generated image is a reply by itself (``has_reply``).+ "image": AssistantMessage(+ (+ HostedToolBlock(+ id="ig_1",+ tool="image_generation",+ action=HostedAction(type="generate", prompt="a laptop at night"),+ record=HostedRecord(status="completed", images=(HostedImage(path=str(image), mime="image/png"),)),+ native=HostedNative(source_id="src_testsrc1", payload=({"type": "image_generation_call"},)),+ ),+ ),+ ORIGIN,+ "stop",+ hosted_summary=HostedSummary(),+ ),}[final]harness = _Harness(engine, tmp_path, "avibe", [[Done(message)]])@@ -3283,3 +3316,233 @@ async def test_a_running_service_prunes_settled_jobs_after_its_runs(engine, sessawait harness.agent.handle_message(harness.request("anything else?"))await _until(lambda: not job_dir.exists(), "a settled job outlived its retention while the service ran")+++CHANGELOG = "https://platform.openai.com/docs/changelog"+++def _cited_search_turn(image: Optional[Path] = None) -> list:+ """One response with a native web search, an answer that cites it, and optionally a generated image."""+ blocks: list = [+ HostedToolBlock(+ id="srvtoolu_1",+ tool="web_search",+ action=HostedAction(type="search", queries=("responses api *domain* filters",)),+ record=HostedRecord(status="completed", sources=(HostedSource(url=CHANGELOG, title="Changelog"),)),+ native=HostedNative(source_id="src_testsrc1", payload=({"type": "server_tool_use"},)),+ )+ ]+ if image is not None:+ blocks.append(+ HostedToolBlock(+ id="ig_1",+ tool="image_generation",+ action=HostedAction(type="generate", prompt="a laptop at night", size="1536x1024"),+ record=HostedRecord(status="completed", images=(HostedImage(path=str(image), mime="image/png"),)),+ native=HostedNative(source_id="src_testsrc1", payload=({"type": "image_generation_call"},)),+ )+ )+ answer = "Domain filters shipped."+ blocks.append(TextBlock(text=answer, citations=(Citation(url=CHANGELOG, title="Changelog", start=0, end=len(answer)),)))+ message = AssistantMessage(+ tuple(blocks), ORIGIN, "stop", hosted_summary=HostedSummary(cost=HostedCost(basis="list", usd=0.01))+ )+ return [[Done(message)]]+++async def test_a_native_hosted_call_reaches_im_as_one_line_with_its_cited_answer_and_image(+ engine, session, tmp_path, published+) -> None:+ """MH-HOSTED-UI-011: the line, the cited answer and the generated image, as an IM channel gets them."""+ image = tmp_path / "image-3f2a.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nimage")+ harness = _Harness(engine, tmp_path, "telegram", _cited_search_turn(image))++ await harness.agent.handle_message(harness.request("what shipped?"))++ search = "🔎 Web search “responses api \\*domain\\* filters” · native (test-provider) · 1 source"+ image_line = "🖼 Image generation “a laptop at night” · native (test-provider) · 1 image · ≈ $0.01 for 2 calls"+ with engine.connect() as conn:+ lines = [+ row["content_text"]+ for row in conn.execute(+ select(agent_events)+ .where(agent_events.c.session_id == SESSION, agent_events.c.event_type == "tool_call")+ .order_by(agent_events.c.created_at, agent_events.c.id)+ ).mappings()+ ]+ # One line per hosted call, escaped for the destination; the response's cost is+ # shown once, on its last call. The process log opens with the first of them.+ assert lines == [search, image_line]+ sent = harness.controller.im_client.sent+ assert sent[0] == search+ [answer] = [item for item in sent if item.startswith("Domain filters shipped.")]+ # The citation is a Markdown link where its span ends; the image is an upload, not a link.+ assert f"]({CHANGELOG})" in answer+ assert "file://" not in answer+ assert harness.controller.im_client.uploads == [str(image.resolve())]+++async def test_a_native_hosted_call_reaches_the_workbench_as_a_card_and_cited_answer(+ engine, session, tmp_path, published+) -> None:+ harness = _Harness(engine, tmp_path, "avibe", _cited_search_turn())++ await harness.agent.handle_message(harness.request("what shipped?"))++ with engine.connect() as conn:+ [event] = conn.execute(+ select(agent_events).where(agent_events.c.session_id == SESSION, agent_events.c.event_type == "tool_call")+ ).mappings().all()+ card = json.loads(event["content_json"])["hosted"]+ assert (card["call_id"], card["mode"], card["subject"], card["cost"]) == (+ "srvtoolu_1", "native", "responses api *domain* filters", {"basis": "list", "usd": 0.01, "calls": 1}+ )+ [result] = harness.rows("result")+ [citation] = json.loads(result["content_json"])["citations"]+ assert citation["url"] == CHANGELOG+ assert result["content_text"].startswith("Domain filters shipped.") and f"]({CHANGELOG})" in result["content_text"]+++async def test_a_proxied_hosted_result_shows_once_its_result_is_committed(engine, session, tmp_path, published) -> None:+ """The record the executor returned, read back from the committed tool-result row."""+ from modules.agents.vibey.agent import _Run++ harness = _Harness(engine, tmp_path, "telegram", [[Done(assistant("ok"))]])+ # A tool result answers a Turn, so the Session has consumed an input first.+ await harness.agent.handle_message(harness.request("hello"))+ request = harness.request("what did OpenAI post?")+ call = ToolCallBlock(id="call_x1", name="x_search", arguments={"query": "from:OpenAI"})+ result = ToolResultMessage(+ tool_call_id="call_x1",+ tool_name="x_search",+ content=(text("1. DevDay 2026 recap"),),+ hosted=HostedResult(+ record=HostedRecord(status="completed", sources=(HostedSource(url="https://x.com/OpenAI/status/1"),)),+ execution=ProxyExecution(+ executor_origin=Origin(provider="xai", api="openai_responses", model="grok-4.6"),+ executor_source_id="src_grok0001",+ executor_billing="metered",+ cost=HostedCost(basis="reported", usd=0.11),+ ),+ ),+ )+ entry = await harness.agent.store.append_tool_result(SESSION, result, details={})+ run = _Run(request=request, session_id=SESSION, turn_id="turn", instance="test")+ run.tool_calls[call.id] = call++ await harness.agent._on_event(run, ToolFinished("turn", 1, call.id, entry.row_id, False))++ assert harness.controller.im_client.sent[-1:] == ["🔎 X search “from:OpenAI” · via xAI proxy · 1 post · $0.11"]+++async def test_generated_images_reach_the_user_when_no_final_reply_follows(engine, session, tmp_path, published) -> None:+ """A paid image is delivered even when the run fails before a final reply could carry it."""+ from core.agent_core.ai.provider import ProviderError++ image = tmp_path / "image-orphan.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nimage")+ generated = AssistantMessage(+ (+ HostedToolBlock(+ id="ig_1",+ tool="image_generation",+ action=HostedAction(type="generate", prompt="a laptop at night"),+ record=HostedRecord(status="completed", images=(HostedImage(path=str(image), mime="image/png"),)),+ native=HostedNative(source_id="src_testsrc1", payload=({"type": "image_generation_call"},)),+ ),+ ToolCallBlock(id="call_1", name="echo", arguments={}),+ ),+ ORIGIN,+ "tool_use",+ hosted_summary=HostedSummary(),+ )+ harness = _Harness(+ engine, tmp_path, "telegram",+ [[Done(generated)], [ProviderError("invalid_request", "the served response broke its protocol", False)]],+ )++ await harness.agent.handle_message(harness.request("draw it, then summarize"))++ assert harness.controller.im_client.uploads == [str(image.resolve())]+ assert harness.controller.terminals[-1]["is_error"] is True+++def test_an_image_is_attached_unless_the_reply_already_links_it(tmp_path) -> None:+ """A prose mention of the path is not a delivered file; a relative path names no file to send."""+ from modules.agents.vibey.agent import _Run, VibeyAgent++ image = tmp_path / "image-3f2a.png"+ run = _Run(request=None, session_id=SESSION, turn_id="turn", instance="test")+ run.hosted_images = [("relative.png", "relative.png"), (str(image), image.name)]++ assert VibeyAgent._hosted_image_links(run, f"Saved it to {image}.") == f"})"+ assert VibeyAgent._hosted_image_links(run, f"Here: })") == ""+++async def test_a_hosted_named_call_without_a_record_still_shows_its_tool_line(engine, session, tmp_path, published) -> None:+ """A hosted tool that failed before producing a record (a plain error result) is not silently dropped."""+ from modules.agents.vibey.agent import _Run++ harness = _Harness(engine, tmp_path, "telegram", [[Done(assistant("ok"))]])+ # A tool result answers a Turn, so the Session has consumed an input first.+ await harness.agent.handle_message(harness.request("hello"))+ request = harness.request("search X")+ call = ToolCallBlock(id="call_x1", name="x_search", arguments={"query": "from:OpenAI"})+ entry = await harness.agent.store.append_tool_result(+ SESSION,+ ToolResultMessage(tool_call_id="call_x1", tool_name="x_search", content=(text("not available"),), is_error=True),+ details={},+ )+ run = _Run(request=request, session_id=SESSION, turn_id="turn", instance="test")+ run.tool_calls[call.id] = call++ await harness.agent._on_event(run, ToolFinished("turn", 1, call.id, entry.row_id, True))++ assert harness.controller.im_client.sent[-1:] == ['🔧 x_search {"query": "from:OpenAI"}']+++async def test_a_final_whose_cited_text_is_all_silent_stays_silent(engine, session, tmp_path, published) -> None:+ """No citation outlives the text it cited: the reply is the model's silence, not a bare source link."""+ silent = AssistantMessage(+ (TextBlock(text="<silent>internal note</silent>", citations=(Citation(url=CHANGELOG, title="Changelog"),)),),+ ORIGIN,+ "stop",+ )+ harness = _Harness(engine, tmp_path, "telegram", [[Done(silent)]])++ await harness.agent.handle_message(harness.request("anything new?"))++ assert not any(CHANGELOG in item for item in harness.controller.im_client.sent)+ assert not any(CHANGELOG in (row["content_text"] or "") for row in harness.rows("result") + harness.rows("assistant"))+++async def test_an_image_only_final_is_a_reply_and_reaches_the_user(engine, session, tmp_path, published) -> None:+ """MH-HOSTED-UI-012: a model asked for a picture may answer with the picture alone; that is not an empty reply."""+ image = tmp_path / "image-only.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nimage")+ final = AssistantMessage(+ (+ HostedToolBlock(+ id="ig_1",+ tool="image_generation",+ action=HostedAction(type="generate", prompt="a laptop at night"),+ record=HostedRecord(status="completed", images=(HostedImage(path=str(image), mime="image/png"),)),+ native=HostedNative(source_id="src_testsrc1", payload=({"type": "image_generation_call"},)),+ ),+ ),+ ORIGIN,+ "stop",+ hosted_summary=HostedSummary(),+ )+ harness = _Harness(engine, tmp_path, "telegram", [[Done(final)]])++ await harness.agent.handle_message(harness.request("draw a laptop at night"))++ assert harness.controller.terminals[-1]["is_error"] is False+ assert harness.controller.im_client.uploads == [str(image.resolve())]+ # The reply is the image under its heading, never the empty-reply error.+ [result] = harness.rows("result")+ assert result["content_text"].startswith(i18n_t("info.generatedImagesFallback", "en"))+ assert not any(i18n_t("vibeyAgent.error.emptyResponse", "en") in item for item in harness.controller.im_client.sent)+diff --git a/ui/src/components/workbench/AgentActivityGroup.tsx b/ui/src/components/workbench/AgentActivityGroup.tsxindex d9ecf7f3f..97279d9fe 100644--- a/ui/src/components/workbench/AgentActivityGroup.tsx+++ b/ui/src/components/workbench/AgentActivityGroup.tsx@@ -27,6 +27,7 @@ import type { LucideIcon } from 'lucide-react';import clsx from 'clsx';import { Markdown } from '../ui/markdown';+import { HostedToolRow } from './HostedToolRow';import { Dialog, DialogContent } from '../ui/dialog';import { copyTextToClipboard } from '../../lib/utils';import { bindCitations } from '../../lib/citations';@@ -352,7 +353,9 @@ const ActivityAssistantRow: React.FC<{ row: ActivityRow }> = ({ row }) => {};const ActivityRowItem = memo(function ActivityRowItem({ row }: { row: ActivityRow }) {- return row.kind === 'tool_call' ? <ActivityToolRow row={row} /> : <ActivityAssistantRow row={row} />;+ if (row.kind !== 'tool_call') return <ActivityAssistantRow row={row} />;+ // A hosted call's text is its IM line; the card renders it from the record instead.+ return row.hosted ? <HostedToolRow card={row.hosted} /> : <ActivityToolRow row={row} />;});// ``variant`` picks the all-filtered placeholder: the compact LIVE card shows a singlediff --git a/ui/src/components/workbench/HostedToolRow.test.tsx b/ui/src/components/workbench/HostedToolRow.test.tsxnew file mode 100644index 000000000..2c3eafc97--- /dev/null+++ b/ui/src/components/workbench/HostedToolRow.test.tsx@@ -0,0 +1,122 @@+/** @vitest-environment jsdom */+// What the execution-process group shows for a hosted call (§6.4), rendered from+// the payloads `tests/test_hosted_activity.py` pins the producer to.+import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';+import { createInstance } from 'i18next';+import { I18nextProvider, initReactI18next } from 'react-i18next';+import { afterEach, describe, expect, it } from 'vitest';++import en from '../../i18n/en.json';+import zh from '../../i18n/zh.json';+import fixture from '../../lib/hostedActivity.fixture.json';+import { groupFromWire } from '../../lib/agentActivity';+import { ActivityChip } from './AgentActivityGroup';++const translator = (lng: 'en' | 'zh') => {+ const instance = createInstance();+ void instance.use(initReactI18next).init({+ lng,+ fallbackLng: 'en',+ resources: { en: { translation: en }, zh: { translation: zh } },+ interpolation: { escapeValue: false },+ });+ return instance;+};++type Payload = keyof Omit<typeof fixture, '_comment'>;++/** The stored rows, through the same wire mapping the /activity endpoint feeds. */+const renderGroup = (payloads: (Payload | Record<string, unknown>)[], lng: 'en' | 'zh' = 'en') => {+ const group = groupFromWire({+ id: 'turn', anchor_message_id: 'reply', anchor_position: 'before', open: false, status: 'done',+ steps: payloads.length, duration_ms: 38000,+ rows: payloads.map((payload, index) => ({+ id: `evt_${index}`, kind: 'tool_call' as const, text: 'the IM line', created_at: '2026-10-13T14:02:00Z',+ hosted: typeof payload === 'string' ? fixture[payload] : payload,+ })),+ });+ render(+ <I18nextProvider i18n={translator(lng)}>+ <ActivityChip group={group} expanded loading={false} onToggle={() => {}} showToolCalls onToggleTools={() => {}} />+ </I18nextProvider>,+ );+};++const row = (callId: string) => document.querySelector(`[data-hosted-call="${callId}"]`) as HTMLElement;++afterEach(() => cleanup());++describe('hosted tool cards', () => {+ it('MH-HOSTED-UI-008: shows a native and a proxied call the same way, each with how it ran and its known cost', () => {+ renderGroup(['native_web_search', 'proxy_x_search']);++ const native = row('srvtoolu_01');+ expect(native.textContent).not.toContain('the IM line');+ within(native).getByText('web_search');+ within(native).getByText('OpenAI announcements October 2026');+ within(native).getByText('Native · Anthropic');+ within(native).getByText('≈ $0.01');+ within(native).getByText('2 sources');++ const proxy = row('call_x1');+ within(proxy).getByText('Via xAI proxy');+ within(proxy).getByText('$0.11');+ within(proxy).getByText('2 posts');++ // Expanded: who ran it, and the numbered sources with their excerpts.+ fireEvent.click(within(proxy).getByRole('button'));+ within(proxy).getByText('Run by proxy executor Grok · grok-4.6 · took 31 s · $0.11');+ const first = within(proxy).getByRole('link', { name: 'DevDay 2026 recap: everything we shipped this week' });+ expect(first.getAttribute('href')).toBe('https://x.com/OpenAI/status/1');+ within(proxy).getByText('Recordings and the launch list in one post.');+ within(proxy).getByText('x.com · 2 days ago');+ });++ it('says why a call failed instead of counting results it never had', () => {+ renderGroup(['proxy_failed']);+ const failed = row('call_x2');+ within(failed).getByText('Failed');+ expect(failed.textContent).not.toMatch(/\$|post/);+ fireEvent.click(within(failed).getByRole('button'));+ within(failed).getByText('no Source is verified for X search');+ });++ it('shows a generated image with its prompt and file, and no cost when none is known', () => {+ renderGroup([{+ ...fixture.native_image,+ images: [{ ...fixture.native_image.images[0], media_url: '/api/media/tok_3f2a?w=1536&h=1024', bytes: 1_993_000 }],+ }]);+ const image = row('ig_01');+ within(image).getByText('1 image · 1536×1024');+ expect(image.textContent).not.toMatch(/\$/);++ fireEvent.click(within(image).getByRole('button'));+ const figure = image.querySelector('[data-hosted-image="ig_01"]') as HTMLElement;+ expect((figure.querySelector('img') as HTMLImageElement).getAttribute('src')).toBe('/api/media/tok_3f2a?w=1536&h=1024');+ within(figure).getByRole('link', { name: '3f2a.png' });+ within(figure).getByText('1.9 MB');+ within(figure).getByText('Revised prompt');+ within(figure).getByText(fixture.native_image.revised_prompt);+ });++ it('shows a call cut off with its turn as cut off, never as having run', () => {+ renderGroup([{ ...fixture.native_web_search, status: 'pending', cost: undefined }]);+ const pending = row('srvtoolu_01');+ within(pending).getByText('Cut off');+ expect(within(pending).queryByText('Native · Anthropic')).toBeNull();+ });++ it('keeps the plain tool line for a row whose card this client cannot read', () => {+ renderGroup([{ ...fixture.native_web_search, version: 2 }]);+ expect(document.querySelector('[data-hosted-call]')).toBeNull();+ // The generic tool row, drawn from the row's text as for any other tool call.+ screen.getByRole('button', { name: /IM line/ });+ });++ it('reads in Chinese with the same facts', () => {+ renderGroup(['proxy_x_search'], 'zh');+ const proxy = row('call_x1');+ within(proxy).getByText('由 xAI 代理');+ within(proxy).getByText('2 条帖子');+ });+});diff --git a/ui/src/components/workbench/HostedToolRow.tsx b/ui/src/components/workbench/HostedToolRow.tsxnew file mode 100644index 000000000..a76222920--- /dev/null+++ b/ui/src/components/workbench/HostedToolRow.tsx@@ -0,0 +1,218 @@+// A hosted tool call in the execution-process group (`vibey-hosted-tools.md` §6.4,+// design.pen frames DA8aJ and t2P7VH): the query, how it ran (natively, or by which+// executor), what it cost when known, and the sources or image it produced. A+// native and a proxied call draw the same card from the same record.+import * as React from 'react';+import { useTranslation } from 'react-i18next';+import { AtSign, Check, ChevronRight, CircleSlash, Download, FileImage, Globe, Image as ImageIcon, TriangleAlert, Waypoints, WandSparkles } from 'lucide-react';+import clsx from 'clsx';++import { ChatImage } from '../ui/chat-image';+import { apiKeyVendorPreset } from '../settings/models/apiKeyVendors';+import type { HostedActivity, HostedActivityImage, HostedToolName } from '../../lib/hostedActivity';++const TOOL_ICON: Record<HostedToolName, React.ComponentType<{ className?: string }>> = {+ web_search: Globe,+ x_search: AtSign,+ image_generation: ImageIcon,+};++const vendorLabel = (provider: string | undefined): string | null =>+ provider ? apiKeyVendorPreset(provider)?.label ?? provider : null;++// Small per-call figures keep their first significant digits: $0.006, not $0.01.+const formatUsd = (usd: number): string => {+ if (usd >= 0.1) return usd.toFixed(2);+ const digits = usd >= 0.001 ? 3 : 4;+ return usd.toFixed(digits).replace(/0+$/, '').replace(/\.$/, '');+};++const domainOf = (url: string): string => {+ try {+ return new URL(url).hostname.replace(/^www\./, '');+ } catch {+ return url;+ }+};++const fileName = (path: string): string => path.split(/[\\/]/).pop() || path;++const formatBytes = (bytes: number): string =>+ bytes >= 1024 * 1024 ? `${(bytes / (1024 * 1024)).toFixed(1)} MB` : `${Math.max(1, Math.round(bytes / 1024))} KB`;++/** What a call cost, only when it is known; unknown is never shown as zero. */+const useHostedCost = (card: HostedActivity): string | null => {+ const { t } = useTranslation();+ const cost = card.cost;+ if (!cost) return null;+ const figure = cost.basis === 'included'+ ? t('chat.agentActivity.hosted.cost.included')+ : cost.usd === undefined+ ? null+ : t(cost.basis === 'list' ? 'chat.agentActivity.hosted.cost.list' : 'chat.agentActivity.hosted.cost.reported', { usd: formatUsd(cost.usd) });+ if (figure === null) return null;+ return cost.calls > 1 ? t('chat.agentActivity.hosted.cost.calls', { cost: figure, count: cost.calls }) : figure;+};++/** 「原生 · Anthropic」 or 「由 xAI 代理」, and the failure state when it failed. */+export const HostedModeChip: React.FC<{ card: HostedActivity }> = ({ card }) => {+ const { t } = useTranslation();+ if (card.status === 'failed' || card.status === 'incomplete') {+ return (+ <span className="inline-flex shrink-0 items-center gap-1 rounded-full border border-destructive/40 bg-destructive/10 px-2 py-px text-[11px] font-medium text-destructive-ink">+ <TriangleAlert className="size-3" aria-hidden="true" />+ {t(`chat.agentActivity.hosted.status.${card.status}`)}+ </span>+ );+ }+ if (card.status === 'pending') {+ // Cut off with its turn: it never finished, so it claims no way of having run.+ return (+ <span className="inline-flex shrink-0 items-center gap-1 rounded-full border border-gold/40 bg-gold/10 px-2 py-px text-[11px] font-medium text-gold-ink">+ <CircleSlash className="size-3" aria-hidden="true" />+ {t('chat.agentActivity.hosted.status.pending')}+ </span>+ );+ }+ const vendor = vendorLabel(card.provider);+ if (!vendor) return null;+ return card.mode === 'native' ? (+ <span className="inline-flex shrink-0 items-center gap-1 rounded-full border border-mint/40 bg-mint-soft px-2 py-px text-[11px] font-medium text-mint-ink">+ <Check className="size-3" aria-hidden="true" />+ {t('chat.agentActivity.hosted.mode.native', { provider: vendor })}+ </span>+ ) : (+ <span className="inline-flex shrink-0 items-center gap-1 rounded-full border border-cyan/40 bg-cyan-soft px-2 py-px text-[11px] font-medium text-cyan-ink">+ <Waypoints className="size-3" aria-hidden="true" />+ {t('chat.agentActivity.hosted.mode.proxy', { provider: vendor })}+ </span>+ );+};++/** What the row counts ("1 image · 1536×1024", "2 posts"), when there is anything to count. */+const useCountLabel = (card: HostedActivity): string | null => {+ const { t } = useTranslation();+ if (card.tool === 'image_generation') {+ if (card.images.length === 0) return null;+ const [first] = card.images;+ const count = t('chat.agentActivity.hosted.count.images', { count: card.images.length });+ return first.width && first.height ? `${count} · ${first.width}×${first.height}` : count;+ }+ if (card.sources.length === 0) return null;+ return t(card.tool === 'x_search' ? 'chat.agentActivity.hosted.count.posts' : 'chat.agentActivity.hosted.count.sources', { count: card.sources.length });+};++/** A generated image as the answer and the card show it: the file, how it ran, and the prompt it rendered. */+export const HostedImageFigure: React.FC<{ card: HostedActivity; image: HostedActivityImage }> = ({ card, image }) => {+ const { t } = useTranslation();+ const cost = useHostedCost(card);+ const name = fileName(image.path);+ return (+ <figure className="overflow-hidden rounded-lg border border-border bg-background" data-hosted-image={card.call_id}>+ {image.media_url+ ? <ChatImage src={image.media_url} alt={card.revised_prompt ?? card.subject ?? name} width={image.width} height={image.height} />+ : null}+ <figcaption className="flex flex-col">+ <span className="flex flex-wrap items-center gap-x-2 gap-y-1 px-3 py-2 text-[11px] text-muted">+ <HostedModeChip card={card} />+ {cost && <span>{cost}</span>}+ <span className="ml-auto inline-flex min-w-0 items-center gap-1.5">+ <FileImage className="size-3.5 shrink-0 text-cyan-ink" aria-hidden="true" />+ {image.media_url+ ? <a href={image.media_url} download={name} className="truncate font-medium text-cyan-ink hover:underline" title={name}>{name}</a>+ : <span className="truncate font-medium text-foreground" title={image.path}>{name}</span>}+ {image.bytes !== undefined && <span className="shrink-0">{formatBytes(image.bytes)}</span>}+ {image.media_url && <a href={image.media_url} download={name} aria-label={t('chat.agentActivity.hosted.image.download', { name })} className="shrink-0 text-muted hover:text-foreground"><Download className="size-3.5" aria-hidden="true" /></a>}+ </span>+ </span>+ {card.revised_prompt && (+ <span className="flex flex-col gap-1 border-t border-border px-3 py-2">+ <span className="inline-flex items-center gap-1.5 text-[11px] font-medium text-muted">+ <WandSparkles className="size-3" aria-hidden="true" />+ {t('chat.agentActivity.hosted.image.revisedPrompt')}+ </span>+ <span className="text-[12px] leading-relaxed text-foreground/90">{card.revised_prompt}</span>+ </span>+ )}+ </figcaption>+ </figure>+ );+};++const HostedDetail: React.FC<{ card: HostedActivity }> = ({ card }) => {+ const { t } = useTranslation();+ const cost = useHostedCost(card);+ const where = [card.source_name ?? vendorLabel(card.provider), card.model].filter(Boolean).join(' · ');+ const facts = [+ where && t(card.mode === 'proxy' ? 'chat.agentActivity.hosted.ranProxy' : 'chat.agentActivity.hosted.ranNative', { where }),+ card.duration_ms !== undefined && t('chat.agentActivity.hosted.duration', { seconds: Math.max(1, Math.round(card.duration_ms / 1000)) }),+ cost,+ ].filter(Boolean);+ return (+ <div className="mx-1.5 mb-1 mt-0.5 flex flex-col overflow-hidden rounded-md border border-border bg-foreground/[0.03]">+ {facts.length > 0 && (+ <div className="flex items-center gap-1.5 border-b border-border px-2.5 py-1.5 text-[11px] text-muted">+ {card.mode === 'proxy' ? <Waypoints className="size-3 shrink-0 text-cyan-ink" aria-hidden="true" /> : <Check className="size-3 shrink-0 text-mint-ink" aria-hidden="true" />}+ <span className="min-w-0">{facts.join(' · ')}</span>+ </div>+ )}+ {(card.status === 'failed' || card.status === 'incomplete') && card.error && (+ <p className="px-2.5 py-2 text-[12px] text-destructive-ink">{card.error}</p>+ )}+ {card.extra_queries && card.extra_queries.length > 0 && (+ <p className="border-b border-border px-2.5 py-1.5 text-[11px] text-muted">+ {t('chat.agentActivity.hosted.alsoSearched', { queries: card.extra_queries.join(' · ') })}+ </p>+ )}+ {card.sources.length > 0 && (+ <ol className="flex flex-col">+ {card.sources.map((source, index) => (+ <li key={`${source.url}:${index}`} className="flex gap-2.5 border-b border-border px-2.5 py-2 last:border-b-0">+ <span className="mt-px inline-flex size-[18px] shrink-0 items-center justify-center rounded border border-mint/30 bg-mint/[0.08] text-[10px] font-semibold text-mint-ink">{index + 1}</span>+ <span className="flex min-w-0 flex-col gap-0.5">+ <span className="flex min-w-0 flex-wrap items-baseline gap-x-2">+ <a href={source.url} target="_blank" rel="noopener noreferrer" className="min-w-0 truncate text-[12px] font-medium text-foreground hover:underline" title={source.title ?? source.url}>{source.title || domainOf(source.url)}</a>+ <span className="shrink-0 text-[11px] text-muted">{[domainOf(source.url), source.page_age].filter(Boolean).join(' · ')}</span>+ </span>+ {source.snippet && <span className="text-[11px] leading-relaxed text-muted">{source.snippet}</span>}+ </span>+ </li>+ ))}+ </ol>+ )}+ {card.images.map((image) => (+ <div key={image.path} className="p-2"><HostedImageFigure card={card} image={image} /></div>+ ))}+ </div>+ );+};++export const HostedToolRow: React.FC<{ card: HostedActivity }> = ({ card }) => {+ const { t } = useTranslation();+ const [open, setOpen] = React.useState(false);+ const Icon = TOOL_ICON[card.tool];+ const cost = useHostedCost(card);+ const count = useCountLabel(card);+ return (+ <div className="flex flex-col" data-hosted-call={card.call_id}>+ <button+ type="button"+ onClick={() => setOpen((value) => !value)}+ aria-expanded={open}+ title={open ? t('chat.agentActivity.hideCall') : t('chat.agentActivity.showCall')}+ className="group/act flex w-full items-center gap-2 rounded-md px-1.5 py-1 text-left transition-colors hover:bg-foreground/[0.04]"+ >+ <Icon className="size-3.5 shrink-0 text-cyan-ink" aria-hidden="true" />+ <span className="shrink-0 font-mono text-[11px] font-medium text-foreground">{card.tool}</span>+ {card.subject+ ? <span className="min-w-0 flex-1 truncate"><span className="rounded-md border border-border bg-foreground/[0.03] px-1.5 py-px text-[12px] text-foreground/90" title={card.subject}>{card.subject}</span></span>+ : <span className="min-w-0 flex-1" />}+ <HostedModeChip card={card} />+ {cost && card.status === 'completed' && <span className="shrink-0 text-[11px] text-muted">{cost}</span>}+ {count && <span className="hidden shrink-0 text-[11px] text-muted sm:inline">{count}</span>}+ <ChevronRight className={clsx('size-3.5 shrink-0 text-muted transition-transform', open && 'rotate-90')} aria-hidden="true" />+ </button>+ {open && <HostedDetail card={card} />}+ </div>+ );+};diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.jsonindex 0cf32fda2..ad88c7759 100644--- a/ui/src/i18n/en.json+++ b/ui/src/i18n/en.json@@ -4766,6 +4766,40 @@}},"agentActivity": {+ "hosted": {+ "mode": {+ "native": "Native · {{provider}}",+ "proxy": "Via {{provider}} proxy"+ },+ "status": {+ "failed": "Failed",+ "incomplete": "Incomplete",+ "pending": "Cut off"+ },+ "cost": {+ "included": "Included in subscription",+ "list": "≈ ${{usd}}",+ "reported": "${{usd}}",+ "calls_one": "{{cost}} for {{count}} call",+ "calls_other": "{{cost}} for {{count}} calls"+ },+ "count": {+ "sources_one": "{{count}} source",+ "sources_other": "{{count}} sources",+ "posts_one": "{{count}} post",+ "posts_other": "{{count}} posts",+ "images_one": "{{count}} image",+ "images_other": "{{count}} images"+ },+ "ranNative": "Ran natively on {{where}}",+ "ranProxy": "Run by proxy executor {{where}}",+ "duration": "took {{seconds}} s",+ "alsoSearched": "Also searched: {{queries}}",+ "image": {+ "revisedPrompt": "Revised prompt",+ "download": "Download {{name}}"+ }+ },"running": "Running","step": "{{count}} step","steps": "{{count}} steps",diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.jsonindex 42fc77f7c..44a188b85 100644--- a/ui/src/i18n/zh.json+++ b/ui/src/i18n/zh.json@@ -4766,6 +4766,40 @@}},"agentActivity": {+ "hosted": {+ "mode": {+ "native": "原生 · {{provider}}",+ "proxy": "由 {{provider}} 代理"+ },+ "status": {+ "failed": "失败",+ "incomplete": "未完成",+ "pending": "已中断"+ },+ "cost": {+ "included": "订阅内含",+ "list": "≈ ${{usd}}",+ "reported": "${{usd}}",+ "calls_one": "{{cost}}({{count}} 次调用)",+ "calls_other": "{{cost}}({{count}} 次调用)"+ },+ "count": {+ "sources_one": "{{count}} 个来源",+ "sources_other": "{{count}} 个来源",+ "posts_one": "{{count}} 条帖子",+ "posts_other": "{{count}} 条帖子",+ "images_one": "{{count}} 张",+ "images_other": "{{count}} 张"+ },+ "ranNative": "原生执行:{{where}}",+ "ranProxy": "代理执行:{{where}}",+ "duration": "用时 {{seconds}} 秒",+ "alsoSearched": "还搜索了:{{queries}}",+ "image": {+ "revisedPrompt": "修订后的提示词",+ "download": "下载 {{name}}"+ }+ },"running": "正在执行","step": "{{count}} 步","steps": "{{count}} 步",diff --git a/ui/src/lib/agentActivity.ts b/ui/src/lib/agentActivity.tsindex 99d85a12c..b9f452d59 100644--- a/ui/src/lib/agentActivity.ts+++ b/ui/src/lib/agentActivity.ts@@ -1,5 +1,6 @@import type { WorkbenchMessage } from '../context/ApiContext';import type { CitationSource } from './citations';+import { hostedActivityFrom, type HostedActivity } from './hostedActivity';import { specFor } from './messageTypes';import { timestampOrderTimeMs } from './transcriptOrder';@@ -16,6 +17,8 @@ export type ActivityRow = {// Source sidecar for an agent narration row whose text carries citation links// (see lib/citations). Absent on every other row, live or durable.citations?: CitationSource[];+ // A hosted tool call's card (lib/hostedActivity); its ``text`` is then the IM line.+ hosted?: HostedActivity;};// Storage owns persisted order, including clocks recovered from migration metadata.@@ -94,6 +97,7 @@ export type TurnActivityGroupWire = {created_at: string;order_micros?: number;citations?: CitationSource[];+ hosted?: unknown;}>;};@@ -109,6 +113,7 @@ export const groupFromWire = (wire: TurnActivityGroupWire): ActivityGroup => ({rows: wire.rows?.map((r) => ({id: r.id, kind: r.kind, text: r.text, created_at: r.created_at, order_micros: r.order_micros,citations: r.citations,+ hosted: hostedActivityFrom(r.hosted),})),});@@ -120,6 +125,7 @@ export const activityRowFromMessage = (msg: WorkbenchMessage): ActivityRow => ({text: msg.text ?? '',created_at: msg.created_at,citations: (msg.content as { citations?: CitationSource[] } | null | undefined)?.citations,+ hosted: hostedActivityFrom((msg.content as { hosted?: unknown } | null | undefined)?.hosted),});// ===== Live running-card buffer: a pure state machine (state, not timing) =====diff --git a/ui/src/lib/hostedActivity.fixture.json b/ui/src/lib/hostedActivity.fixture.jsonnew file mode 100644index 000000000..d54ed141e--- /dev/null+++ b/ui/src/lib/hostedActivity.fixture.json@@ -0,0 +1,75 @@+{+ "_comment": "The hosted-tool card wire shape (core/hosted_activity.py, content.hosted on a toolcall row, version 1). tests/test_hosted_activity.py builds these exact payloads from canonical C-1 records; the Web card tests render them.",+ "native_web_search": {+ "version": 1,+ "call_id": "srvtoolu_01",+ "tool": "web_search",+ "mode": "native",+ "status": "completed",+ "subject": "OpenAI announcements October 2026",+ "action": "search",+ "provider": "anthropic",+ "source_id": "src_relayclaude",+ "source_name": "relay-claude",+ "model": "claude-opus-5-5",+ "sources": [+ { "url": "https://openai.com/index/devday-2026", "title": "DevDay 2026 recap", "page_age": "2 days ago" },+ { "url": "https://platform.openai.com/docs/changelog", "title": "Responses API changelog" }+ ],+ "images": [],+ "cost": { "basis": "list", "usd": 0.01, "calls": 1 }+ },+ "proxy_x_search": {+ "version": 1,+ "call_id": "call_x1",+ "tool": "x_search",+ "mode": "proxy",+ "status": "completed",+ "subject": "from:OpenAI within_time:7d",+ "provider": "xai",+ "source_id": "src_grok0001",+ "source_name": "Grok",+ "model": "grok-4.6",+ "duration_ms": 31000,+ "sources": [+ {+ "url": "https://x.com/OpenAI/status/1",+ "title": "DevDay 2026 recap: everything we shipped this week",+ "snippet": "Recordings and the launch list in one post.",+ "page_age": "2 days ago"+ },+ { "url": "https://x.com/OpenAIDevs/status/2", "title": "Web search in the Responses API now supports domain filters" }+ ],+ "images": [],+ "cost": { "basis": "reported", "usd": 0.11, "calls": 1 }+ },+ "native_image": {+ "version": 1,+ "call_id": "ig_01",+ "tool": "image_generation",+ "mode": "native",+ "status": "completed",+ "subject": "A wide photo of an open laptop on a desk at night, empty dark space in the upper right",+ "revised_prompt": "A wide photo of an open laptop on a desk at night, empty dark space in the upper right",+ "action": "generate",+ "provider": "openai",+ "source_id": "src_relaygpt1",+ "source_name": "relay-gpt",+ "model": "gpt-6.1-sol",+ "size": "1536x1024",+ "quality": "medium",+ "sources": [],+ "images": [{ "path": "/media/3f2a.png", "mime": "image/png", "width": 1536, "height": 1024 }]+ },+ "proxy_failed": {+ "version": 1,+ "call_id": "call_x2",+ "tool": "x_search",+ "mode": "proxy",+ "status": "failed",+ "error": "no Source is verified for X search",+ "subject": "from:OpenAI",+ "sources": [],+ "images": []+ }+}diff --git a/ui/src/lib/hostedActivity.ts b/ui/src/lib/hostedActivity.tsnew file mode 100644index 000000000..c80cea210--- /dev/null+++ b/ui/src/lib/hostedActivity.ts@@ -0,0 +1,74 @@+// A hosted tool call's card, as a ``toolcall`` row carries it in ``content.hosted``+// (``core/hosted_activity.py``, version 1; the shared fixture is+// ``hostedActivity.fixture.json``). The row's ``text`` is the IM line; the Web+// renders the card from these fields instead.++export type HostedToolName = 'web_search' | 'x_search' | 'image_generation';++export type HostedActivitySource = {+ url: string;+ title?: string;+ snippet?: string;+ page_age?: string;+};++export type HostedActivityImage = {+ path: string;+ mime: string;+ width?: number;+ height?: number;+ /** Workbench only: the media-proxy URL the row registered for the file. */+ media_url?: string;+ bytes?: number;+};++export type HostedActivity = {+ version: 1;+ call_id: string;+ tool: HostedToolName;+ mode: 'native' | 'proxy';+ status: 'completed' | 'failed' | 'incomplete' | 'pending';+ error?: string;+ /** The query, prompt, page or thread the call named. */+ subject?: string;+ action?: string;+ extra_queries?: string[];+ /** Vendor id of whoever ran it: the response's for native, the executor's for proxy. */+ provider?: string;+ source_id?: string;+ source_name?: string;+ model?: string;+ sources: HostedActivitySource[];+ images: HostedActivityImage[];+ size?: string;+ quality?: string;+ revised_prompt?: string;+ duration_ms?: number;+ /** Absent when no price is known: unknown is never shown as zero. ``calls`` is how many calls it covers. */+ cost?: { basis: 'reported' | 'list' | 'included'; usd?: number; calls: number };+};++const TOOLS: readonly string[] = ['web_search', 'x_search', 'image_generation'];+const STATUSES: readonly string[] = ['completed', 'failed', 'incomplete', 'pending'];++/** The card a row carries, or undefined for anything that is not one this client reads. */+export const hostedActivityFrom = (raw: unknown): HostedActivity | undefined => {+ if (!raw || typeof raw !== 'object') return undefined;+ const card = raw as Record<string, unknown>;+ if (card.version !== 1 || typeof card.call_id !== 'string') return undefined;+ if (typeof card.tool !== 'string' || !TOOLS.includes(card.tool)) return undefined;+ if (card.mode !== 'native' && card.mode !== 'proxy') return undefined;+ if (typeof card.status !== 'string' || !STATUSES.includes(card.status)) return undefined;+ return {+ ...(card as unknown as HostedActivity),+ sources: Array.isArray(card.sources) ? (card.sources as HostedActivitySource[]).filter((s) => typeof s?.url === 'string') : [],+ images: Array.isArray(card.images) ? (card.images as HostedActivityImage[]).filter((i) => typeof i?.path === 'string') : [],+ };+};++/** The media token of a proxy URL (``/api/media/<token>?w=&h=``), which joins a card's image to the answer's. */+export const mediaToken = (url: string | undefined): string | null => {+ if (!url) return null;+ const match = /\/api\/media\/([^/?#]+)/.exec(url);+ return match ? match[1] : null;+};diff --git a/vibe/i18n/en.json b/vibe/i18n/en.jsonindex a80b0a457..64ed10bd5 100644--- a/vibe/i18n/en.json+++ b/vibe/i18n/en.json@@ -1732,5 +1732,36 @@"sessionTitle": "Vibey · Desktop mini","noChatProject": "Create a project in Avibe first, so the desktop mini has a place for its conversation.","builtinAgentUnavailable": "The built-in Agent is not available, so the desktop mini has no one to talk to."+ },+ "hostedTools": {+ "tool": {+ "web_search": "Web search",+ "x_search": "X search",+ "image_generation": "Image generation"+ },+ "quoted": "“{subject}”",+ "mode": {+ "native": "native ({provider})",+ "proxy": "via {provider} proxy"+ },+ "count": {+ "sourceOne": "1 source",+ "sources": "{count} sources",+ "postOne": "1 post",+ "posts": "{count} posts",+ "imageOne": "1 image",+ "images": "{count} images"+ },+ "cost": {+ "included": "included in subscription",+ "list": "≈ ${usd}",+ "reported": "${usd}",+ "calls": "{cost} for {calls} calls"+ },+ "status": {+ "failed": "failed: {error}",+ "incomplete": "incomplete: {error}",+ "pending": "cut off"+ }}}diff --git a/vibe/i18n/zh.json b/vibe/i18n/zh.jsonindex 86b86b032..5fb8e1e90 100644--- a/vibe/i18n/zh.json+++ b/vibe/i18n/zh.json@@ -1732,5 +1732,36 @@"sessionTitle": "歪比 · 桌宠","noChatProject": "先在 Avibe 里建一个项目,桌宠的对话需要放在项目里。","builtinAgentUnavailable": "内置 Agent 当前不可用,桌宠暂时没有对话对象。"+ },+ "hostedTools": {+ "tool": {+ "web_search": "网页搜索",+ "x_search": "X 搜索",+ "image_generation": "图片生成"+ },+ "quoted": "“{subject}”",+ "mode": {+ "native": "原生({provider})",+ "proxy": "由 {provider} 代理"+ },+ "count": {+ "sourceOne": "1 个来源",+ "sources": "{count} 个来源",+ "postOne": "1 条帖子",+ "posts": "{count} 条帖子",+ "imageOne": "1 张",+ "images": "{count} 张"+ },+ "cost": {+ "included": "订阅内含",+ "list": "≈ ${usd}",+ "reported": "${usd}",+ "calls": "{cost}({calls} 次调用)"+ },+ "status": {+ "failed": "失败:{error}",+ "incomplete": "未完成:{error}",+ "pending": "已中断"+ }}}