# MiMo Web Search Extension for Pi

Enables web search for Xiaomi MiMo models in [Pi](https://github.com/earendil-works/pi-coding-agent) by automatically injecting the `web_search` tool into every request.

## What It Does

When you ask MiMo about current events, recent news, or anything that benefits from up-to-date information, the model will search the web and use the results to answer — just like it would through the MiMo web UI.

The extension hooks into Pi's `before_provider_request` event and injects the `web_search` tool into every outgoing request to a MiMo direct API provider. No manual configuration or keywords needed — the model decides when to search based on your query.

## Prerequisites

1. **MiMo direct API key** — a pay-as-you-go key from [api.xiaomimimo.com](https://api.xiaomimimo.com), not a token plan key.

2. **Web Search plugin activated** — enable it on the MiMo platform console:
   👉 https://platform.xiaomimimo.com/#/console/plugin

3. **Pi configured with MiMo** — add your API key to `~/.pi/agent/auth.json`:
   ```json
   {
     "xiaomi": {
       "type": "api_key",
       "key": "sk-your-api-key-here"
     }
   }
   ```

## Install

Copy the extension to Pi's global extensions directory:

```bash
mkdir -p ~/.pi/agent/extensions/mimo-web-search
cp index.ts ~/.pi/agent/extensions/mimo-web-search/
```

Then restart Pi or run `/reload`.

You should see `[mimo-web-search]` in the extensions list at startup, and a notification confirming it loaded.

## How It Works

The extension registers a single event handler:

```
before_provider_request → injects {"type": "web_search", "web_search": {}} into tools array
```

It targets only the `xiaomi` provider (direct API). Token plan providers are explicitly skipped because MiMo does not support web search on token plan endpoints.

## Billing

Web search uses **pay-as-you-go credits**, separate from your API key usage or any token plan subscription. Each search request consumes credits based on the number of searches performed and pages crawled. Check your usage on the [MiMo platform console](https://platform.xiaomimimo.com).

## Limitations

### No Structured Citations in Pi

MiMo returns structured URL annotations (`url_citation` objects with title, URL, and summary) in the streaming response. However, **Pi's openai-completions handler drops these annotations** from the stream — it only processes `content`, `tool_calls`, and `reasoning_content` fields.

This means:
- ✅ The model receives search results and uses them to answer
- ✅ The model can mention sources by name in its text (e.g., "according to Reuters")
- ❌ Structured citations (clickable URLs, source cards) are not displayed in the TUI

The model will often cite sources naturally in its response text, but the links themselves won't be rendered. This is a Pi limitation, not a MiMo limitation — the annotations are sent by MiMo but discarded by Pi's streaming handler.

### Token Plan Not Supported

Web search is **not available on MiMo token plan endpoints** (`token-plan-sgp`, `token-plan-cn`, `token-plan-ams`). If you try to use web search with a token plan key, MiMo returns:

```
web search tool found in the request body, but webSearchEnabled is false
```

This extension automatically skips token plan providers to avoid this error. If you switch between token plan and direct API, the extension will only activate web search when using the `xiaomi` (direct API) provider.

### Model-Driven Search

The `web_search` tool is sent with `force_search: false` (the default), meaning the **model decides** when to search. It will search for current events, recent news, or real-time information, but won't search when you're asking it to write code, explain concepts, or do tasks that don't require up-to-date information. This is usually the desired behavior — it avoids wasting credits on queries that don't need web search.

## Development

The extension is a single TypeScript file. To modify it:

1. Edit `~/.pi/agent/extensions/mimo-web-search/index.ts`
2. Run `/reload` in Pi
3. Test with a query that benefits from web search (e.g., "what's the latest news on X?")

### Extension Structure

```typescript
export default function (pi: any) {
  // Notify on load
  pi.on("session_start", ...);

  // Inject web_search tool into MiMo requests
  pi.on("before_provider_request", (event, ctx) => {
    // Check if current model is a MiMo direct API provider
    // Inject web_search tool into the tools array
    // Return modified payload
  });
}
```

### Relevant Pi Extension API

- `before_provider_request` — fires before each LLM request, can modify the payload
- `session_start` — fires when a session starts, used for the load notification
- `ctx.model.provider` — the current model's provider ID (e.g., `"xiaomi"`, `"xiaomi-token-plan-sgp"`)

## References

- [MiMo Web Search Documentation](https://mimo.mi.com/docs/en-US/usage-guide/tool-calling/web-search)
- [MiMo OpenAI Chat Completions API](https://mimo.mi.com/docs/en-US/api/chat/openai-api)
- [MiMo Platform Console](https://platform.xiaomimimo.com)
- [Pi Extension Documentation](https://github.com/earendil-works/pi-coding-agent/blob/main/docs/extensions.md)
