# pi-ds-web-search

为 [Pi](https://pi.dev) 提供 DeepSeek 官方**服务端联网搜索**能力——安装即静默生效。

[English README](README.md)

## 解决什么问题

Pi 内置的 `deepseek` provider 走 Chat Completions，不支持服务端搜索；而 DeepSeek 官方 Responses 端点原生带 `web_search` 工具（一次请求完成「搜索 → 读全文 → 带引用总结」），Pi 却没有暴露它。本插件补上这条通路：装上之后，你的 DeepSeek 模型无需额外配置即可联网搜索。

## 功能

- **安装即静默生效**——覆盖内置 `deepseek` provider：`deepseek-v4-flash` 切换到 Responses API 并注入 `web_search`；`deepseek-v4-pro` 保持 Chat Completions 不变（官方尚未支持 Pro 的 Responses）。
- **任何主模型都能用**——`deepseek_search` 工具直接调用 DeepSeek 官方 API，主模型是其他供应商（GLM、OpenAI、Claude 等）时同样可用。
- **交互式配置**——`/ds-ws config`：API key（缺失时提醒）、模型选择、自动搜索开关。
- **成本可控**——自动搜索默认开启，随时 `/ds-ws off` 关掉。

## 安装

要求：Pi 0.83+，Node 22+。

```powershell
pi install npm:pi-ds-web-search        # npm 发布后
# 或
pi install git:github.com/Lan-zk/pi-ds-web-search
# 或本地目录
pi install E:\File\self\github\pi-ds-web-search
```

然后 `/reload`。已用 `pi /login` 登录过 DeepSeek 就无需配置；否则运行 `/ds-ws config`。

## 使用

| 命令 | 说明 |
|---|---|
| `/ds-ws` | 查看状态 |
| `/ds-ws config` | 交互式配置：API key、搜索模型、自动开关 |
| `/ds-ws on` / `/ds-ws off` | 开关自动联网搜索 |

自动模式（默认）：模型自行判断是否需要联网。按需：让 agent 调用 `deepseek_search({ query, reasoning_effort?, max_output_tokens? })`。

## 实现思路

1. `pi.registerProvider("deepseek", ...)` 覆盖内置 provider：`deepseek-v4-flash` 走 Responses API（`openai-responses`）；认证继承 Pi（已存凭据 → `DEEPSEEK_API_KEY`）。
2. `before_provider_request` 钩子给每个 `deepseek-v4-flash` 的 Responses 请求注入 `{ type: "web_search" }`，DeepSeek 服务端执行搜索并在同一响应里返回最终答案。
3. `deepseek_search` 工具直接 `POST https://api.deepseek.com/responses`——与主模型无关，返回答案 + 来源 + token 用量。

## 成本

每次服务端搜索都是一次付费请求，搜索上下文单次可能增加约 10 万+ input tokens。不需要时用 `/ds-ws off` 关闭。

## 参考

- DeepSeek 官方：[Responses API](https://api-docs.deepseek.com/guides/responses_api/)、[Anthropic API](https://api-docs.deepseek.com/guides/anthropic_api)、[接入 Pi](https://api-docs.deepseek.com/quick_start/agent_integrations/pi_mono)
- 同类项目：[pi-seek](https://github.com/bjoernaagaard/pi-seek)、[bxff/pi-deepseek-search](https://github.com/bxff/pi-deepseek-search)、[websearch-deepseek](https://github.com/lyumeng/websearch-deepseek)、[ModSearch](https://github.com/liustack/modsearch)

## 开源协议

MIT
