Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

merge: land hosted web search onto current main

vastsa9ec3d43119ef
diff --cc docs/adr/0297-provider-hosted-web-search-adapter-capability.md
index 15a21fc98,000000000..a3ccaecd6
mode 100644,000000..100644
--- a/docs/adr/0297-provider-hosted-web-search-adapter-capability.md
+++ b/docs/adr/0297-provider-hosted-web-search-adapter-capability.md
@@@ -1,109 -1,0 +1,109 @@@
- # ADR 0296: Provider-hosted web search as an adapter capability
++# ADR 0297: Provider-hosted web search as an adapter capability
+
- - Status: Proposed
++- Status: Accepted
+- Date: 2026-09-19
+
+## Context
+
+Some providers execute web search server-side: Anthropic Messages ships a
+`web_search_20250305` server tool, and OpenAI Responses (plus compatible
+gateways) ships a `web_search` tool. Unlike an MCP search server, the search
+runs inside the provider request, is billed by the provider per search, and
+returns structured blocks (`server_tool_use` + `web_search_tool_result`,
+`web_search_call`, `url_citation` annotations) that the client must keep in
+the conversation for multi-turn grounding.
+
+PR #570 previously added this as a composer toggle with runtime-level SSE
+re-parsing; it was reverted (#608) with the owner asking for a plugin-shaped
+follow-up. A capability review found the plugin SDK cannot reach the request
+payload, the response stream, the composer, the transcript, or message
+persistence, so "web search as a plugin" would require four new SDK extension
+points — larger than the feature itself. This ADR records the host-built
+shape that landed instead.
+
+Two upstream facts forced the design:
+
+- pi-ai 0.85.1 drops vendor search blocks in both adapters: the Anthropic
+ stream loop has no `server_tool_use` / `web_search_tool_result` /
+ `citations_delta` branches, and the Responses item loop has no
+ `web_search_call` slot or annotation handling. Extraction must therefore
+ live inside the adapters, not beside them.
+- Anthropic requires `server_tool_use` + `web_search_tool_result` blocks
+ (including `encrypted_content`) to be replayed verbatim on later turns.
+ A display-only normalization cannot satisfy that; replay needs the raw
+ wire blocks.
+
+## Decision
+
+1. **Capability is declared data, evaluated in one place.** The per-model
+ opt-in is `ModelBinding.nativeWebSearch` (settings UI: a checkbox in the
+ model's advanced sheet, disabled unless the provider's API style is
+ `responses` or `anthropic_messages`). models.dev publishes no hosted-tool
+ capability, so there is no catalog default; absent means off. The single
+ evaluation point is `resolveNativeWebSearch` in
+ `packages/shared/src/native-web-search.ts`, keyed on the resolved wire
+ API — never vendor names, base URL hostnames, or model-id substrings.
+
+2. **Attachment and extraction live in the pi-ai adapters**, delivered by
+ extending `patches/@[email protected]`:
+ - `anthropic-messages.js` appends the `web_search_20250305` tool when
+ `model.webSearch === true`, captures search blocks as `hostedSearch`
+ content parts (raw wire block kept whole, streamed `input_json_delta`
+ accumulated so the query is available live), collects `citations_delta`,
+ and replays both halves of each pair in order on later turns.
+ - `openai-responses(-shared).js` (and the Azure variant's own
+ `buildParams`) appends the `web_search` tool, asks for
+ `web_search_call.action.sources` via the opt-in `include` channel,
+ creates a `hostedSearch` slot for `web_search_call` items, collects
+ `url_citation` annotations, and replays the item for the same model.
+ Both adapters push a `hosted_search_update` stream event per block
+ transition; `patches/@[email protected]` teaches
+ the agent loop to forward it as `message_update` — without that second
+ patch the events die in the loop's switch and search activity renders only
+ when the whole turn finishes. The patches are a stopgap; the same changes
+ should be proposed upstream and dropped once released.
+
+3. **The flag travels the existing model-config channel.**
+ `ModelConfigWithBinding` copies `nativeWebSearch` into
+ `ModelConfig.webSearch`; `buildProviderModel` spreads catalog fields into
+ the pi-ai `Model`, so no new IPC, sidecar parameter, or runtime rebuild
+ hook is needed.
+
+4. **Display data and replay data are separated, and both persist.** The
+ runtime normalizes `hostedSearch` blocks into `UiMessage.hostedSearch`
+ (`status/rounds[]` for activity rows; `replay[]` for the adapter's raw
+ content parts, stripped of streaming scratch). Persistence is an additive
+ `hostedSearch` transcript block (`ui_to_record` / `record_to_ui`) — no SQL
+ migration. `historyToEntries` restores `replay` after thinking and before
+ text so convertMessages can ground later turns after a restart. Transcripts
+ written without `replay` still render; they just cannot ground. The
+ wire-slimming functions (`streamingMessageIdentity`, `applyMessageUpdate`)
+ carry the field so delta frames cannot drop it.
+
+5. **UI is a transcript activity row per round, not a composer control.**
+ Each round of a `hostedSearch` activity renders as a `HostedSearchRow` on
+ the tool-row idiom (icon + name + query summary, chevron disclosure):
+ searching state while the round is in flight, sources when expanded.
+ Sources are plain text links — no favicon fetches — so reading a
+ transcript never leaks source hostnames anywhere (the #579 rule).
+ Citation badges in message text are out of scope for v1; links stay
+ ordinary markdown links.
+
+## Consequences
+
+- Enabling the tool is per model, always-on while enabled, and takes effect
+ on the next turn (runtime rebuild follows from modelConfig change). A
+ session-level toggle and composer affordance can layer on later without
+ touching the adapter contract.
+- Gateways that mangle replay blocks surface as provider errors on the next
+ turn; the user-visible remedy is unchecking the model's opt-in. No silent
+ fallback strips the tool — a search that quietly did not happen is worse
+ than an error.
+- `pause_turn` keeps pi-ai's existing mapping to `stop`; a long searching
+ turn may end early on the official Anthropic wire. This is a known
+ limitation to revisit with the upstream patch.
+- Compaction rewrites history and drops search blocks; later turns lose old
+ grounding and the model searches again as needed. Documented behavior.
+- Search executes on the provider. There is no local fetch, no ask/allow
+ prompt, and billing is the provider's. The model-level opt-in (default
+ off) is the user consent surface.
diff --cc docs/adr/README.md
index 8764079e1,6d276e2e5..9e55b770e
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@@ -322,5 -323,6 +323,7 @@@ Each ADR includes
| 0293 | [SSH password authentication for the remote-host bootstrap](0293-ssh-password-authentication.md) | Accepted (D454; amends ADR 0292) |
| 0294 | [Project archive is a list + inspector workbench](0294-project-archive-list-inspector.md) | Accepted (D455; amends D267 / D168) |
| 0295 | [Session thinking-parameter omission](0295-session-thinking-parameter-omission.md) | Accepted (D456; amends ADR 0194 / ADR 0144 / ADR 0221) |
- | 0296 | [Provider-hosted web search as an adapter capability](0296-provider-hosted-web-search-adapter-capability.md) | Proposed |
+ | 0296 | [Signed macOS DMG is a two-icon install](0296-macos-signed-dmg-two-icon-install.md) | Accepted (D457; amends ADR 0232 / ADR 0204) |
++| 0297 | [Provider-hosted web search as an adapter capability](0297-provider-hosted-web-search-adapter-capability.md) | Accepted |
| turn-process-and-thinking-display | [Turn process and thinking presentation](turn-process-and-thinking-display.md) | Accepted |
+ | provider-display-order | [Provider display order](provider-display-order.md) | Accepted |
diff --cc docs/spec/03-runtime/11-provider-model-system.md
index cf2710692,9d3865e29..9c459f6cd
--- a/docs/spec/03-runtime/11-provider-model-system.md
+++ b/docs/spec/03-runtime/11-provider-model-system.md
@@@ -224,19 -224,6 +224,19 @@@ PI-Desktop must not permanently restric
catalog" rather than an equal-valued override. Agreeing with models.dev is
therefore the reset, and no separate reset control or per-capability
explanatory copy is required.
+10a. `nativeWebSearch` is a two-state opt-in (absent means off; there is no
+ catalog baseline because models.dev publishes no hosted-tool capability).
+ When enabled and the model's resolved wire API is `anthropic-messages`,
+ `openai-responses`, or `azure-openai-responses` (stored apiStyle
+ `anthropic_messages` / `responses`), the adapter attaches the provider's
+ hosted web search tool (`web_search_20250305` / `web_search`), extracts
+ the search activity into `UiMessage.hostedSearch` (`rounds` for display,
+ `replay` for convertMessages), and restores those raw blocks on later
- turns including after a restart (ADR 0296). The checkbox is disabled
++ turns including after a restart (ADR 0297). The checkbox is disabled
+ when the provider's API style is neither of those two. Gateways that do
+ not support the tool surface the provider error; the remedy is unchecking.
+ Search runs on the provider: there is no local fetch and no permission
+ prompt. Compaction still drops search blocks.
11. `ModelInfo` is the published record the settings surface compares against,
so a stored binding must not shape its capabilities or reasoning fields.
Effective limits, reasoning and thinking levels are resolved through the
diff --cc docs/zh-CN/adr/index.md
index 645d19dd4,c88389d93..c583f8bc7
--- a/docs/zh-CN/adr/index.md
+++ b/docs/zh-CN/adr/index.md
@@@ -301,7 -301,7 +301,8 @@@ ADR 记录那些不应被静默改变�
| 0292 | [远端主机的 SSH 引导](/adr/0292-ssh-remote-host-bootstrap) | 已接受实施(D453;ADR 0205 R2b,扩展 ADR 0286) |
| 0294 | [项目存档改为列表 + 检查器](/adr/0294-project-archive-list-inspector) | 已接受(D455;修订 D267 / D168) |
| 0295 | [会话思考参数不发送](/adr/0295-session-thinking-parameter-omission) | 已接受(D456;修订 ADR 0194 / ADR 0144 / ADR 0221) |
- | 0296 | [提供商托管联网搜索作为适配器能力](/adr/0296-provider-hosted-web-search-adapter-capability) | 提议中 |
+ | 0296 | [已签名 macOS DMG 改为双图标安装](/adr/0296-macos-signed-dmg-two-icon-install) | 已接受(D457;修订 ADR 0232 / ADR 0204) |
++| 0297 | [提供商托管联网搜索作为适配器能力](/adr/0297-provider-hosted-web-search-adapter-capability) | 已接受 |
## 什么时候看 ADR
diff --cc docs/zh-CN/spec/03-runtime/11-provider-model-system.md
index 78b4948b6,cda3c60ee..681f8dd73
--- a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
+++ b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
@@@ -201,16 -201,6 +201,16 @@@ PI-Desktop 不得把用户永久限制�
10. 设置里的复选框展示的是相对于已发布基线的有效答案;把某一项设回已发布的
值,存下来的是"跟随目录",而不是一个取值相同的覆盖。因此与 models.dev
保持一致本身就是重置,不需要另外的重置控件,也不需要逐项能力的解释文案。
+10a. `nativeWebSearch` 是两态的主动开启(缺省即关闭;没有目录基线,因为
+ models.dev 不发布托管工具能力)。启用且模型解析后的线路 API 是
+ `anthropic-messages`、`openai-responses` 或 `azure-openai-responses`
+ (存储的 apiStyle 为 `anthropic_messages` / `responses`)时,适配器会
+ 附加提供商托管的联网搜索工具(`web_search_20250305` / `web_search`),
+ 把搜索活动提取为 `UiMessage.hostedSearch`(`rounds` 用于展示,`replay`
+ 用于 convertMessages),并在后续回合——包括重启之后——回放这些原始
- 搜索块(ADR 0296)。提供商接口风格不属于这两种时复选框禁用。不支持
++ 搜索块(ADR 0297)。提供商接口风格不属于这两种时复选框禁用。不支持
+ 该工具的网关会把提供商错误暴露出来;处理方式是取消勾选。搜索在提供商
+ 侧执行:没有本地抓取,也没有权限询问。压缩仍会丢掉搜索块。
11. `ModelInfo` 是设置界面用来对照的已发布记录,因此已存储的 binding 不得
塑造它的能力或推理字段。有效上限、推理与思考级别都通过那个确切的 binding
解析;有效的传输模态数组还会额外套用显式的附件覆盖。
diff --cc packages/agent-runtime/src/runtime.test.ts
index fae90e337,0871d78e1..45daff8ae
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@@ -8200,166 -8200,3 +8200,166 @@@ describe("DesktopAgentRuntime compactio
await runtime.dispose();
});
});
+
- describe("DesktopAgentRuntime hosted web search rounds (ADR 0296)", () => {
++describe("DesktopAgentRuntime hosted web search rounds (ADR 0297)", () => {
+ it("emits each search round as it happens and closes open rounds on message_end", async () => {
+ const onEvent = vi.fn();
+ const runtime = createRuntime({ onEvent });
+ const handleAgentEvent = (runtime as any).handleAgentEvent.bind(runtime);
+
+ const round = (id: string, status: string, query: string, sources: unknown[] = []) => ({
+ type: "hostedSearch",
+ phase: "web_search_call",
+ blockId: id,
+ status,
+ wire: {
+ type: "web_search_call",
+ id,
+ status,
+ action: { type: "search", query, ...(sources.length ? { sources } : {}) },
+ },
+ });
+
+ await handleAgentEvent({ type: "agent_start" });
+ await handleAgentEvent({ type: "turn_start" });
+ // Round 1 starts searching.
+ await handleAgentEvent({
+ type: "message_start",
+ message: { role: "assistant", content: [round("ws_1", "in_progress", "first query")] },
+ });
+ // Round 1 completes and round 2 starts, still without any text.
+ await handleAgentEvent({
+ type: "message_update",
+ message: {
+ role: "assistant",
+ content: [
+ round("ws_1", "completed", "first query", [
+ { url: "https://example.com/a", title: "A" },
+ ]),
+ round("ws_2", "in_progress", "second query"),
+ ],
+ },
+ });
+ // The turn ends while round 2 is still open; text arrived meanwhile.
+ await handleAgentEvent({
+ type: "message_end",
+ message: assistantMessage({
+ content: [
+ round("ws_1", "completed", "first query", [
+ { url: "https://example.com/a", title: "A" },
+ ]),
+ round("ws_2", "in_progress", "second query"),
+ { type: "text", text: "The answer." },
+ ],
+ }),
+ });
+ await handleAgentEvent({ type: "turn_end" });
+ await handleAgentEvent({ type: "agent_end", messages: [] });
+
+ const events = onEvent.mock.calls.map(([envelope]) => (envelope as any).event);
+ const searchUpdates = events.filter(
+ (event: any) => event.type === "message_update" && event.message?.hostedSearch,
+ );
+ // Round 1 streamed the moment it started; round 2 the moment it appeared.
+ expect(searchUpdates[0]?.message?.hostedSearch?.rounds).toEqual([
+ { id: "ws_1", status: "searching", query: "first query", sources: [] },
+ ]);
+ expect(searchUpdates.at(-1)?.message?.hostedSearch?.rounds).toEqual([
+ {
+ id: "ws_1",
+ status: "completed",
+ query: "first query",
+ sources: [{ url: "https://example.com/a", title: "A" }],
+ },
+ { id: "ws_2", status: "searching", query: "second query", sources: [] },
+ ]);
+
+ const end = events.find((event: any) => event.type === "message_end");
+ // A round still open when the turn ends closes as completed, not left
+ // blinking "searching" in a settled transcript.
+ expect(end?.message?.hostedSearch).toEqual({
+ status: "completed",
+ rounds: [
+ {
+ id: "ws_1",
+ status: "completed",
+ query: "first query",
+ sources: [{ url: "https://example.com/a", title: "A" }],
+ },
+ { id: "ws_2", status: "completed", query: "second query", sources: [] },
+ ],
+ replay: [
+ round("ws_1", "completed", "first query", [
+ { url: "https://example.com/a", title: "A" },
+ ]),
+ round("ws_2", "in_progress", "second query"),
+ ],
+ });
+ await runtime.dispose();
+ });
+
+ it("replays persisted hostedSearch blocks into model context on restore", async () => {
+ const restored = createRuntime({
+ history: [
+ {
+ id: "u1",
+ role: "user",
+ content: "news?",
+ status: "complete",
+ createdAt: "2026-09-19T00:00:00.000Z",
+ },
+ {
+ id: "a1",
+ role: "assistant",
+ content: "here is the news",
+ status: "complete",
+ createdAt: "2026-09-19T00:00:01.000Z",
+ hostedSearch: {
+ status: "completed",
+ rounds: [
+ { id: "srvtoolu_01", status: "completed", query: "news", sources: [] },
+ ],
+ replay: [
+ {
+ type: "hostedSearch",
+ phase: "server_tool_use",
+ blockId: "srvtoolu_01",
+ name: "web_search",
+ input: { query: "news" },
+ },
+ {
+ type: "hostedSearch",
+ phase: "web_search_tool_result",
+ blockId: "srvtoolu_01",
+ wire: {
+ type: "web_search_tool_result",
+ encrypted_content: "enc-1",
+ },
+ },
+ ],
+ },
+ },
+ ],
+ });
+ const assistant = (restored as unknown as { agent: Agent }).agent.state.messages.find(
+ (message) => message.role === "assistant",
+ ) as { content: unknown[] } | undefined;
+ expect(assistant?.content).toEqual([
+ {
+ type: "hostedSearch",
+ phase: "server_tool_use",
+ blockId: "srvtoolu_01",
+ name: "web_search",
+ input: { query: "news" },
+ },
+ {
+ type: "hostedSearch",
+ phase: "web_search_tool_result",
+ blockId: "srvtoolu_01",
+ wire: { type: "web_search_tool_result", encrypted_content: "enc-1" },
+ },
+ { type: "text", text: "here is the news" },
+ ]);
+ await restored.dispose();
+ });
+});
diff --cc packages/shared/src/index.ts
index d1e116798,34e41b2de..8ea44c4c4
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@@ -55,6 -55,8 +55,9 @@@ export * from "./trusted-extensions.js"
export * from "./provider-retry.js";
export * from "./message-stream.js";
export * from "./session-collaboration.js";
+ export * from "./session-presentation.js";
+ export * from "./session-outcomes.js";
+ export * from "./tray-sessions.js";
export * from "./window-chrome.js";
export * from "./prompt-enhancement.js";
+export * from "./native-web-search.js";