pi-auto-web-search 0.1.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +28 -13
- package/README_EN.md +28 -13
- package/package.json +1 -1
- package/src/adapters/anysearch.ts +1 -1
- package/src/adapters/cloudflare.ts +8 -13
- package/src/adapters/deepseek.ts +1 -1
- package/src/adapters/exa.ts +1 -1
- package/src/adapters/firecrawl.ts +8 -13
- package/src/adapters/tavily.ts +11 -6
- package/src/adapters/tencent.ts +2 -2
- package/src/adapters/types.ts +8 -4
- package/src/adapters/util.ts +13 -3
- package/src/adapters/xai.ts +19 -22
- package/src/cloudflare-oauth.ts +412 -147
- package/src/config.ts +113 -35
- package/src/http.ts +71 -40
- package/src/key-quota.ts +153 -180
- package/src/oauth-lock.ts +29 -0
- package/src/presets.ts +12 -1
- package/src/search-service.ts +110 -64
- package/src/settings-ui.ts +202 -55
- package/src/tools.ts +389 -196
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.2.1
|
|
4
|
+
|
|
5
|
+
- Cloudflare OAuth 按实例管理凭据和账号绑定;旧授权只迁移到一个实例,存储故障不再阻断其他供应商。
|
|
6
|
+
|
|
7
|
+
## 0.1.2
|
|
8
|
+
|
|
9
|
+
- `web-search`、`web-fetch`、`web-map` 不再接受 `instance` 或 `chain`。默认沿用配置顺序;点名供应商时传 `provider`,只在当前链上同供应商的实例间切换。`/search test <实例ID>` 保留。
|
|
10
|
+
- 三个工具的公共结果只报告供应商、任务说明与实际请求的错误分类。实例 ID、跳过原因和上游原始错误保留在本地诊断中;成功切换后也保留前一跳的失败记录。
|
|
11
|
+
- 搜索会在请求前跳过不能兑现域名过滤或 Markdown 正文的实例。xAI 不再截断超过 5 个域名的列表;带域名过滤的请求会跳过固定启用 `x_search` 的实例。
|
|
12
|
+
- `web-fetch` 保留 Tavily 原生纯文本,只转换 Markdown。`json` 和 `links` 序列化后超过 12,000 字符会报本地输出错误,不返回残缺内容或切换供应商。
|
|
13
|
+
- `failover.maxAttempts` 默认 3,不再用当前链长校验;超时默认在 `stopOn` 中。Cloudflare 按实际端点检查账号,缺账号的操作不发请求。`limits.maxPages` 保留为配置字段,三个工具都不使用。
|
|
14
|
+
- 多个进程共用同一 `search-usage.json` 时,发送前在 SQLite 旁文件锁内持久预占。校准和清零只修改已确认次数;崩溃遗留的预占保留至周期结束,旧计数保留;本地结算失败不重发请求。
|
|
15
|
+
- Cloudflare OAuth 登录只回填使用该身份且未绑定账号的启用实例;保留账号变量引用,静态凭据失效时不改用 OAuth。
|
|
16
|
+
|
|
3
17
|
## 0.1.1
|
|
4
18
|
|
|
5
19
|
- 版本号递增,包内容与 0.1.0 相同
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
| 能力 | 说明 |
|
|
12
12
|
| --- | --- |
|
|
13
13
|
| 统一搜索、抓取与站点地图 | 提供 `web-search`(联网搜索)、`web-fetch`(单页正文提取,支持 Markdown、纯文本、html、json 与 links)和 `web-map`(遍历站点发现 URL) |
|
|
14
|
-
| 多实例自动故障切换 |
|
|
14
|
+
| 多实例自动故障切换 | 默认遇到限流、额度不足、认证、网络或服务端错误时尝试下一实例;超时属于 `stopOn`,不会继续切换 |
|
|
15
15
|
| 多 Key 轮换与冷却 | 单实例可配置多个 API Key,失败自动冷却并切换,支持按密钥调用次数硬限额与并发控制 |
|
|
16
16
|
| 多种接入方式 | 支持直连官方(`sdk`)、上游密钥池网关(`pool`)和自定义代理转发(`proxy`) |
|
|
17
17
|
| 实时正文策略 | 搜索和抓取正文默认优先获取最新页面内容,避免返回过时缓存 |
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
| DeepSeek | `deepseek` | 支持 | 支持 | 不支持 | 支持官方 Search 与 Fetch 接口 |
|
|
32
32
|
| xAI | `xai` | 支持 | 不支持 | 不支持 | 基于 Responses API `web_search`,实例配置 `xSearch: true` 时固定附加 `x_search` |
|
|
33
33
|
|
|
34
|
-
`web-fetch`
|
|
34
|
+
`web-fetch` 的正文输出上限为 12,000 字符(按 JavaScript 字符串长度计算)。`markdown`、`text`、`html` 超限时返回带截断提示的预览;`json` 和 `links` 只返回完整结果,序列化后超限会报本地输出错误,不重试或切换供应商。
|
|
35
35
|
|
|
36
36
|
## 安装
|
|
37
37
|
|
|
@@ -94,19 +94,28 @@ pi -e .
|
|
|
94
94
|
- `pool`:接入自带 Key 池的上游中转网关(需填写 `baseUrl`),插件侧只发一把网关 Key,轮换交给上游。
|
|
95
95
|
- `proxy`:通过自定义代理转发请求(需填写 `baseUrl`),仍由插件本地做多 Key 轮换与冷却(支持添加或逐条移除多个 Key)。
|
|
96
96
|
- **`baseUrl` 填写层级**:只需填到域名或反代前缀(例如 `https://proxy.example.com`),不要包含 `/search`、`/extract`、`/responses` 等具体接口路径;结尾的 `/v1`、`/v2`、`/v4` 版本段会自动归一化处理。Cloudflare 可填到 `/client` 或 `/client/v4`。
|
|
97
|
-
- **凭证与环境变量**:腾讯云元宝搜索采用服务 API KEY 兼容模式(`api.wsa.cloud.tencent.com/SearchPro`,在控制台「概览 > 服务 API KEY 方式」创建 `sk-...` 密钥,推荐使用 `${TENCENTCLOUD_WSA_APIKEY}
|
|
97
|
+
- **凭证与环境变量**:腾讯云元宝搜索采用服务 API KEY 兼容模式(`api.wsa.cloud.tencent.com/SearchPro`,在控制台「概览 > 服务 API KEY 方式」创建 `sk-...` 密钥,推荐使用 `${TENCENTCLOUD_WSA_APIKEY}`)。没有可用凭据的实例会标为“未配置”并跳过。
|
|
98
|
+
- **Cloudflare OAuth**:只有启用、使用 `bearer` 且没有配置静态 `keys` 的 Cloudflare `sdk` 实例才使用 OAuth;静态凭据失效时不会改用 OAuth。每个实例独立登录、刷新和退出,实例名不能与已有 OAuth 凭据仅大小写不同。读取配置和测试连接不回填账号。
|
|
99
|
+
- **旧版授权**:`search-oauth.json` 只自动迁移给唯一且账号无冲突的合格实例。多个候选时,用 `/search migrate cloudflare <实例>` 指定一个;其他实例需独立登录。已有不同或损坏的目标凭据不会被覆盖。未迁移的授权可用 `/search logout cloudflare @legacy` 撤销。
|
|
100
|
+
- **Cloudflare 账号**:登录只回填当前实例。自动回填的 `accountId` 会随重新登录切号更新;旧配置里没有 `oauthBackfilledAccountId` 标记的字面值和 `${VAR}` 始终按手工绑定处理。在面板手工设置账号会清除标记。未绑定时使用令牌账号;显式账号变量未设置时,只能使用不含 `{accountId}` 的自定义端点。
|
|
98
101
|
- **按密钥调用次数限额(`keyQuota`)**:支持为任意实例配置 `{ "limit": 2000, "period": "month" }`,周期支持自然周期(`day`、`week`、`month`、`year`)与固定时长(`24h`、`7d`、`30d`、`2w` 等),用量以密钥 SHA-256 指纹持久化在配置同目录的 `search-usage.json`(权限 `0600`)。由于腾讯云元宝搜索属于后付费服务且超出免费额度不会自动停服,`tencent` 实例默认启用单密钥每自然月 `2000` 次硬拦截(可设为 `keyQuota: false` 关闭或在 `/search` 面板修改/校准);单密钥达到上限后自动切换到池内下一把未满密钥,全部用满时直接停止发请求并触发故障切换。
|
|
102
|
+
- **多进程共用配额**:所有客户端都应升级,共用同一个本地 `search-usage.json`,并为同一密钥设置一致的周期。已有 v1 计数继续有效;同目录的 `search-usage.json.lock.sqlite` 只用于互斥,不保存用量,不要删除。锁竞争最多等待 1 秒,网络请求期间不持锁。请求发送前先持久预占;成功或超时保留计数,明确失败或取消退回。锁或台账不可用时停止请求;本地结算失败不会触发重发。
|
|
103
|
+
- **核对与校准**:本地台账不是供应商账单。`/search` 的校准和清零只调整当前周期已确认的次数,不释放未结算预占。在途请求按实际结果结算;如果进程在结算前退出,遗留的预占会一直占用名额,直到周期结束。
|
|
99
104
|
- **高级选项**:可通过 `defaultChain` / `fetchChain` 指定搜索与抓取的回退顺序,也可配置 `limits`、`failover`、`priority`、`concurrency`、`cooldownSeconds` 与 `enabled`。xAI 实例支持配置 `model`(默认 `grok-4.7`)以及 `xSearch: true` 固定启用 `x_search`(默认关闭,不受 `quality` 影响)。
|
|
105
|
+
- **尝试次数与页数**:`failover.maxAttempts` 是正整数,默认 3,不随实例就绪状态改变;如果本次候选数小于该值,就只尝试这些候选实例。`limits.maxPages` 可保留在配置中,但三个工具均不使用它。
|
|
100
106
|
|
|
101
107
|
## 工具说明
|
|
102
108
|
|
|
109
|
+
三个工具默认使用你配置的有效链:搜索用搜索链,抓取和站点地图用抓取链。只有点名供应商时才传 `provider`(供应商的 `preset` 值)。工具只按原顺序尝试链上该供应商的可用实例,并遵守现有故障切换设置;如果没有实例支持本次请求,工具会直接报错,不发请求。
|
|
110
|
+
|
|
111
|
+
旧调用请移除 `instance` 和 `chain`,这两个输入都会被拒绝。要测试指定实例,请用 `/search test <实例ID>`。
|
|
112
|
+
|
|
103
113
|
### `web-search`
|
|
104
114
|
|
|
105
115
|
| 参数 | 说明 |
|
|
106
116
|
| --- | --- |
|
|
107
117
|
| `query` | 必填,搜索关键词 |
|
|
108
|
-
| `
|
|
109
|
-
| `chain` | 仅本次调用覆盖实例尝试顺序 |
|
|
118
|
+
| `provider` | 可选,支持上表中的八家供应商 |
|
|
110
119
|
| `limit` | 结果条数,范围 1–20,默认 10 |
|
|
111
120
|
| `domains` | 仅保留指定域名下的结果 |
|
|
112
121
|
| `excludeDomains` | 排除指定域名下的结果 |
|
|
@@ -120,6 +129,9 @@ pi -e .
|
|
|
120
129
|
|
|
121
130
|
#### 跨供应商参数映射
|
|
122
131
|
|
|
132
|
+
- **域名过滤**:Firecrawl、xAI 和腾讯云不支持同时使用 `domains` 和 `excludeDomains`。xAI 每个列表最多 5 个域名,不会截断;腾讯云最多包含 1 个或排除 5 个。请求带有任一域名列表时,工具会跳过启用 `xSearch` 的 xAI 实例,因为 X 搜索不支持网站域名过滤;链内未启用 `xSearch` 的实例仍可用。
|
|
133
|
+
- **搜索正文**:`contents=markdown` 会跳过只返回网页摘要的 Cloudflare Web Search,不会额外发起抓取。以上硬条件都在发送请求前检查。
|
|
134
|
+
|
|
123
135
|
- **搜索质量(`quality`)**:
|
|
124
136
|
|
|
125
137
|
| `quality` | Exa `type` | Tavily `search_depth` | xAI `reasoning.effort` |
|
|
@@ -143,9 +155,9 @@ pi -e .
|
|
|
143
155
|
| 参数 | 说明 |
|
|
144
156
|
| --- | --- |
|
|
145
157
|
| `url` | 必填,要抓取的 HTTP(S) 地址 |
|
|
146
|
-
| `format` | `markdown`(默认)、`text`、`html`、`json`、`links
|
|
158
|
+
| `format` | `markdown`(默认)、`text`、`html`、`json`、`links`。Tavily 的原生纯文本保留字面 Markdown 符号和空白;其他实例返回的 Markdown 在请求 `text` 时转为纯文本;`html` / `json` / `links` 仅 Firecrawl 与 Cloudflare 兑现,链上其余实例跳过 |
|
|
147
159
|
| `schema` | JSON 抓取结构;单独出现时当作 `json`。仅 Firecrawl、Cloudflare |
|
|
148
|
-
| `
|
|
160
|
+
| `provider` | 可选:`tavily`、`exa`、`firecrawl`、`deepseek`、`cloudflare`、`anysearch` |
|
|
149
161
|
|
|
150
162
|
### `web-map`
|
|
151
163
|
|
|
@@ -154,14 +166,15 @@ pi -e .
|
|
|
154
166
|
| `url` | 必填,要遍历的 HTTP(S) 地址 |
|
|
155
167
|
| `instructions` | 站点地图指引,仅 Tavily 兑现;Firecrawl 跳过并写 notes |
|
|
156
168
|
| `limit` | 返回条数,范围 1–100,默认 50 |
|
|
157
|
-
| `
|
|
169
|
+
| `provider` | 可选:`tavily`、`firecrawl` |
|
|
158
170
|
|
|
159
171
|
## 返回格式与失败排查
|
|
160
172
|
|
|
161
173
|
三个工具都返回 `content`(模型可读文本)、`details`(TUI 摘要)和 `structuredContent`(结构化数据):
|
|
162
174
|
|
|
163
|
-
- **成功时**:`
|
|
164
|
-
-
|
|
175
|
+
- **成功时**:`providerUsed` 标明供应商,搜索条目也保留来源。候选实例失败后切换成功时,`degraded` 为 `true`,包括本地密钥配额拦截后切换;能力预筛和搜索质量调整不算切换。`notes` 说明搜索深度、结果条数等调整。
|
|
176
|
+
- **失败时**:`error` 给出可公开的原因;`attempts` 按顺序列出已请求且失败的供应商(`provider`、`code`、可选 `status`),同一供应商可以出现多次。`unattempted` 列出候选链中没有发出请求的项(`provider`)。预筛、请求前拦截和取消本身不计入失败尝试。取消时模型文本只显示“请求已取消”,结构化结果保留此前的失败请求。
|
|
177
|
+
- **本地排查**:实例 ID、预筛原因和上游错误详情保留在 `details` 和 TUI 中;展开成功结果可查看此前的失败记录。`/search` 面板和 `/search status` 显示上次命中的实例和各实例的上次错误。
|
|
165
178
|
|
|
166
179
|
## 命令
|
|
167
180
|
|
|
@@ -169,8 +182,9 @@ pi -e .
|
|
|
169
182
|
/search 打开交互式设置面板
|
|
170
183
|
/search status 查看实例状态以及搜索、抓取顺序
|
|
171
184
|
/search test [实例] 发起一次连通性测试请求
|
|
172
|
-
/search login cloudflare 打开 Cloudflare OAuth 授权登录页面
|
|
173
|
-
/search logout cloudflare
|
|
185
|
+
/search login cloudflare [实例] 打开 Cloudflare OAuth 授权登录页面
|
|
186
|
+
/search logout cloudflare [实例] 撤销并清除对应实例的 Cloudflare OAuth 令牌
|
|
187
|
+
/search migrate cloudflare <实例> 将旧版授权迁移到指定实例
|
|
174
188
|
```
|
|
175
189
|
|
|
176
190
|
## 相关文件
|
|
@@ -179,7 +193,8 @@ pi -e .
|
|
|
179
193
|
| --- | --- |
|
|
180
194
|
| `~/.pi/agent/search.json` | 默认配置文件 |
|
|
181
195
|
| `PI_SEARCH_CONFIG` | 覆盖配置文件路径的环境变量 |
|
|
182
|
-
| `~/.pi/agent/search-oauth
|
|
196
|
+
| `~/.pi/agent/search-oauth/<实例名>+<accountId>.json` | 实例 OAuth 令牌(目录 `0700`、文件 `0600`) |
|
|
197
|
+
| `~/.pi/agent/search-oauth.lock.sqlite` | 多个 pi 进程写入或迁移 OAuth 凭据时使用的锁,不保存令牌 |
|
|
183
198
|
|
|
184
199
|
## 开发检查
|
|
185
200
|
|
package/README_EN.md
CHANGED
|
@@ -11,7 +11,7 @@ Connect multiple search and scraping services behind one interface with automati
|
|
|
11
11
|
| Capability | Description |
|
|
12
12
|
| --- | --- |
|
|
13
13
|
| Unified search, fetch & map | Provides `web-search` for web queries, `web-fetch` for single-page Markdown, plain text, html, json, or links extraction, and `web-map` to discover URLs on a site |
|
|
14
|
-
| Automatic failover |
|
|
14
|
+
| Automatic failover | By default, rate limits, quota, authentication, network, and server errors allow failover. A timeout is in `stopOn` and stops the chain. |
|
|
15
15
|
| Key rotation & quotas | Supports multiple API keys per instance with failure cooldowns, per-key call quota hard limits, and concurrency limits |
|
|
16
16
|
| Flexible connection modes | Supports direct vendor APIs (`sdk`), upstream key-pool gateways (`pool`), and custom proxies (`proxy`) |
|
|
17
17
|
| Live content by default | Requests fresh page content instead of stale cached copies whenever providers expose freshness controls |
|
|
@@ -31,7 +31,7 @@ Connect multiple search and scraping services behind one interface with automati
|
|
|
31
31
|
| DeepSeek | `deepseek` | Yes | Yes | No | Official Search and Fetch endpoints |
|
|
32
32
|
| xAI | `xai` | Yes | No | No | Responses API `web_search`; set `xSearch: true` on an instance to also enable `x_search` |
|
|
33
33
|
|
|
34
|
-
`web-fetch`
|
|
34
|
+
`web-fetch` limits the returned content to 12,000 characters (JavaScript string length). Oversized `markdown`, `text`, and `html` responses return a marked preview. `json` and `links` are returned only in full: exceeding the limit after serialization produces a local output error without retrying or switching providers.
|
|
35
35
|
|
|
36
36
|
## Installation
|
|
37
37
|
|
|
@@ -94,19 +94,28 @@ When no configuration file exists yet, the extension includes an anonymous `anys
|
|
|
94
94
|
- `pool`: Connects to an upstream key-pool gateway (`baseUrl` required); the extension sends a single gateway key and delegates rotation upstream.
|
|
95
95
|
- `proxy`: Routes requests through a custom proxy (`baseUrl` required) while keeping local multi-key rotation and cooldowns (supports adding and removing multiple keys in `/search`).
|
|
96
96
|
- **`baseUrl` path level**: Provide only the origin or proxy prefix (for example `https://proxy.example.com`) without endpoint paths such as `/search`, `/extract`, or `/responses`; trailing version segments (`/v1`, `/v2`, `/v4`) are normalized automatically. For Cloudflare, point to the origin or `/client` (or `/client/v4`).
|
|
97
|
-
- **Credentials & environment variables**: Tencent Yuanbao uses the Service API KEY compatibility mode (`api.wsa.cloud.tencent.com/SearchPro`, Bearer `sk-...` key created under Console → Overview → Service API KEY, e.g. `${TENCENTCLOUD_WSA_APIKEY}`). Instances
|
|
97
|
+
- **Credentials & environment variables**: Tencent Yuanbao uses the Service API KEY compatibility mode (`api.wsa.cloud.tencent.com/SearchPro`, Bearer `sk-...` key created under Console → Overview → Service API KEY, e.g. `${TENCENTCLOUD_WSA_APIKEY}`). Instances without usable credentials are marked as unconfigured and skipped automatically.
|
|
98
|
+
- **Cloudflare OAuth:** Only enabled Cloudflare `sdk` instances using `bearer` without static `keys` use OAuth. An unresolved static key never falls back to OAuth. Each instance logs in, refreshes, and logs out independently. Instance IDs must not differ from a stored OAuth ID only by case. Reading configuration or testing a connection does not backfill an account.
|
|
99
|
+
- **Legacy authorization:** `search-oauth.json` migrates automatically only to a single eligible instance with no conflicting account. For multiple candidates, choose one with `/search migrate cloudflare <id>`; other instances require separate logins. Existing different or damaged credentials are never overwritten. Use `/search logout cloudflare @legacy` to revoke an unmigrated grant.
|
|
100
|
+
- **Cloudflare account:** Login backfills only the selected instance. An automatically filled `accountId` follows account changes on re-login. Unmarked literals in older configurations and `${VAR}` references remain manual bindings. Setting the account in the panel clears the `oauthBackfilledAccountId` marker. An unbound instance uses the token account; an unset explicit variable permits only custom endpoints without `{accountId}`.
|
|
98
101
|
- **Per-key call quota (`keyQuota`)**: Any instance can enforce a client-side per-key hard limit via `{ "limit": 2000, "period": "month" }`. Supported periods are calendar cycles (`day`, `week`, `month`, `year`) and fixed durations (`24h`, `7d`, `30d`, `2w`, etc.). Counters are persisted by SHA-256 key fingerprint in `search-usage.json` (mode `0600`) next to `search.json`. Because Tencent Yuanbao Search is postpaid and keeps billing past its free tier, `tencent` enables a default limit of `2000` calls per key per calendar month (set `keyQuota: false` to disable, or adjust/calibrate in `/search`). Exhausted keys are skipped for other keys in the pool; once all keys reach their limit, no upstream request is sent and failover advances to the next provider.
|
|
102
|
+
- **Shared quotas:** Upgrade every client, use the same local `search-usage.json`, and configure the same period for each shared key. Existing v1 counters remain valid. The private sibling `search-usage.json.lock.sqlite` is a lock file, not a usage database; leave it in place. Lock contention waits at most one second, and no lock is held across a network request. Each request reserves capacity on disk before dispatch. Success and timeout consume it; explicit failure and cancellation refund it. Storage or lock failures stop dispatch, and a local settlement failure never triggers a resend.
|
|
103
|
+
- **Reconciliation:** The local ledger is not a provider bill. Calibration and reset in `/search` change only the confirmed usage for the current window; they do not release pending reservations. In-flight requests settle normally. If a client exits before settlement, its reservation occupies capacity until the window ends.
|
|
99
104
|
- **Advanced options**: Set `defaultChain` and `fetchChain` to customize fallback order, or configure `limits`, `failover`, `priority`, `concurrency`, `cooldownSeconds`, and `enabled`. An xAI instance supports setting `model` (default `grok-4.7`) and `xSearch: true` to always include `x_search` (disabled by default, independent of `quality`).
|
|
105
|
+
- **Attempt and page limits:** `failover.maxAttempts` is a positive integer (default `3`), independent of instance readiness. Each call tries at most the smaller of that limit and its candidate count. `limits.maxPages` is accepted in the configuration but is not used by any of the three tools.
|
|
100
106
|
|
|
101
107
|
## Tools
|
|
102
108
|
|
|
109
|
+
All three tools use your effective chain by default: the search chain for searches, and the fetch chain for fetches and site maps. Set `provider` (a provider `preset` ID) only when requesting a particular provider. Only its available instances on that chain are tried, in order and under your failover settings. If none supports the request, the tool returns an error without sending a request.
|
|
110
|
+
|
|
111
|
+
Remove `instance` and `chain` from existing tool calls; both inputs are rejected. To test a specific instance, use `/search test <instance-id>`.
|
|
112
|
+
|
|
103
113
|
### `web-search`
|
|
104
114
|
|
|
105
115
|
| Parameter | Description |
|
|
106
116
|
| --- | --- |
|
|
107
117
|
| `query` | Required search query |
|
|
108
|
-
| `
|
|
109
|
-
| `chain` | Override the fallback instance order for one call |
|
|
118
|
+
| `provider` | Optional; accepts any of the eight preset IDs listed above |
|
|
110
119
|
| `limit` | Number of results, 1–20, default `10` |
|
|
111
120
|
| `domains` | Restrict results to these domains |
|
|
112
121
|
| `excludeDomains` | Exclude results from these domains |
|
|
@@ -120,6 +129,9 @@ When no configuration file exists yet, the extension includes an anonymous `anys
|
|
|
120
129
|
|
|
121
130
|
#### Cross-provider parameter mapping
|
|
122
131
|
|
|
132
|
+
- **Domain filters:** Firecrawl, xAI, and Tencent cannot combine `domains` with `excludeDomains`. xAI accepts up to five domains in either list without truncation; Tencent accepts one included domain or up to five excluded domains. When a domain filter is set, xAI instances with `xSearch` enabled are skipped because X results cannot be filtered by website domain. Other xAI instances on the chain remain eligible.
|
|
133
|
+
- **Search content:** `contents=markdown` skips Cloudflare Web Search, which returns page descriptions rather than full text. These requirements are checked before sending a request; no extra fetch is made.
|
|
134
|
+
|
|
123
135
|
- **Quality (`quality`)**:
|
|
124
136
|
|
|
125
137
|
| `quality` | Exa `type` | Tavily `search_depth` | xAI `reasoning.effort` |
|
|
@@ -143,9 +155,9 @@ When no configuration file exists yet, the extension includes an anonymous `anys
|
|
|
143
155
|
| Parameter | Description |
|
|
144
156
|
| --- | --- |
|
|
145
157
|
| `url` | Required HTTP(S) URL to fetch |
|
|
146
|
-
| `format` | `markdown` (default), `text`, `html`, `json`, `links`.
|
|
158
|
+
| `format` | `markdown` (default), `text`, `html`, `json`, `links`. Tavily's native text preserves literal Markdown characters and whitespace; Markdown returned by other instances is converted locally when `text` is requested; `html` / `json` / `links` are fulfilled only by Firecrawl and Cloudflare, other instances on the chain are skipped |
|
|
147
159
|
| `schema` | JSON scrape schema; treated as `json` when given alone. Firecrawl and Cloudflare only |
|
|
148
|
-
| `
|
|
160
|
+
| `provider` | Optional: `tavily`, `exa`, `firecrawl`, `deepseek`, `cloudflare`, `anysearch` |
|
|
149
161
|
|
|
150
162
|
### `web-map`
|
|
151
163
|
|
|
@@ -154,14 +166,15 @@ When no configuration file exists yet, the extension includes an anonymous `anys
|
|
|
154
166
|
| `url` | Required HTTP(S) URL to map |
|
|
155
167
|
| `instructions` | Mapping guidance; Tavily only. Firecrawl skips it and records a note |
|
|
156
168
|
| `limit` | Number of URLs, 1–100, default `50` |
|
|
157
|
-
| `
|
|
169
|
+
| `provider` | Optional: `tavily`, `firecrawl` |
|
|
158
170
|
|
|
159
171
|
## Output & Failure Diagnostics
|
|
160
172
|
|
|
161
173
|
All three tools return `content` (model-facing text), `details` (TUI summary), and `structuredContent` (typed payload):
|
|
162
174
|
|
|
163
|
-
- **
|
|
164
|
-
- **
|
|
175
|
+
- **Success:** `providerUsed` identifies the provider, including on each search result. `degraded` is true when a candidate fails and another instance succeeds, even if a local key quota blocked the first request. Capability preselection and search quality adjustments are not failover. `notes` describes adjustments to search depth, result limits, and similar parameters.
|
|
176
|
+
- **Failure:** `error` gives a public explanation. Ordered `attempts` contain only failed requests (`provider`, `code`, optional `status`); a provider may occur more than once. `unattempted` lists candidates for which no request was sent (`provider`). Preselection, local blocks, and cancellation itself do not count as failed requests. On cancellation the text only reports cancellation; the structured result retains earlier failures.
|
|
177
|
+
- **Local diagnostics:** Instance IDs, skipped capabilities, and upstream error details stay in `details` and the TUI. Expand a successful result to inspect earlier failures, or use `/search` or `/search status` to see the last successful instance and each instance’s last error.
|
|
165
178
|
|
|
166
179
|
## Commands
|
|
167
180
|
|
|
@@ -169,8 +182,9 @@ All three tools return `content` (model-facing text), `details` (TUI summary), a
|
|
|
169
182
|
/search Open the interactive settings panel
|
|
170
183
|
/search status Show instance status and search/fetch chains
|
|
171
184
|
/search test [id] Run a live connectivity test request
|
|
172
|
-
/search login cloudflare Open the Cloudflare OAuth login dialog
|
|
173
|
-
/search logout cloudflare Revoke and remove the
|
|
185
|
+
/search login cloudflare [id] Open the Cloudflare OAuth login dialog
|
|
186
|
+
/search logout cloudflare [id] Revoke and remove the Cloudflare OAuth token for an instance
|
|
187
|
+
/search migrate cloudflare <id> Move the legacy grant to the specified instance
|
|
174
188
|
```
|
|
175
189
|
|
|
176
190
|
## Files
|
|
@@ -179,7 +193,8 @@ All three tools return `content` (model-facing text), `details` (TUI summary), a
|
|
|
179
193
|
| --- | --- |
|
|
180
194
|
| `~/.pi/agent/search.json` | Default search configuration |
|
|
181
195
|
| `PI_SEARCH_CONFIG` | Overrides the configuration file path |
|
|
182
|
-
| `~/.pi/agent/search-oauth
|
|
196
|
+
| `~/.pi/agent/search-oauth/<id>+<accountId>.json` | Per-instance OAuth token (`0700` directory, `0600` file) |
|
|
197
|
+
| `~/.pi/agent/search-oauth.lock.sqlite` | Cross-process lock for OAuth writes and migration; contains no tokens |
|
|
183
198
|
|
|
184
199
|
## Development Checks
|
|
185
200
|
|
package/package.json
CHANGED
|
@@ -92,7 +92,7 @@ export const anysearchAdapter: SearchAdapter = {
|
|
|
92
92
|
|
|
93
93
|
mapExtract(response: ProviderResponse): ExtractOutput {
|
|
94
94
|
const data = envelope(response);
|
|
95
|
-
return { title: asString(data.title), content: asString(data.content) ?? "" };
|
|
95
|
+
return { format: "markdown", title: asString(data.title), content: asString(data.content) ?? "" };
|
|
96
96
|
},
|
|
97
97
|
|
|
98
98
|
unsupportedExtract: markdownOnlyExtract,
|
|
@@ -1,28 +1,22 @@
|
|
|
1
1
|
import type { ResolvedInstance } from "../config.ts";
|
|
2
|
+
import { CLOUDFLARE_TOOL_PATHS } from "../presets.ts";
|
|
2
3
|
import type { ProviderResponse, RequestSpec } from "../http.ts";
|
|
3
4
|
import type { ExtractOutput, ExtractParams, SearchAdapter, SearchHit, SearchOutput, SearchParams } from "./types.ts";
|
|
4
|
-
import { asArray, asRecord, asString, businessFailure, countryGapNote, qualityGapNote, rejectNoMap, rejectUnsupported, sourceModeGapNote, topicGapNote } from "./util.ts";
|
|
5
|
+
import { asArray, asRecord, asString, businessFailure, countryGapNote, markdownForText, qualityGapNote, rejectNoMap, rejectUnsupported, sourceModeGapNote, topicGapNote } from "./util.ts";
|
|
5
6
|
|
|
6
7
|
const MAX_RESULTS = 10;
|
|
7
8
|
|
|
8
9
|
/** 请求体必须带网关 id;每个账号有名为 default 的网关。 */
|
|
9
10
|
const GATEWAY_ID = "default";
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
markdown: "markdown",
|
|
13
|
-
text: "markdown",
|
|
14
|
-
html: "content",
|
|
15
|
-
json: "json",
|
|
16
|
-
links: "links",
|
|
17
|
-
} as const;
|
|
18
|
-
|
|
19
|
-
/** 域名、时间与答案过滤都没有对应请求字段。 */
|
|
12
|
+
/** Web Search 返回网页摘要,不返回 Markdown 正文。 */
|
|
20
13
|
function unsupportedParams(params: SearchParams): string[] {
|
|
21
14
|
const names: string[] = [];
|
|
22
15
|
if (params.domains?.length) names.push("domains");
|
|
23
16
|
if (params.excludeDomains?.length) names.push("excludeDomains");
|
|
24
17
|
if (params.freshness) names.push("freshness");
|
|
25
18
|
if (params.answer) names.push("answer");
|
|
19
|
+
if (params.contents === "markdown") names.push("contents=markdown");
|
|
26
20
|
return names;
|
|
27
21
|
}
|
|
28
22
|
|
|
@@ -80,7 +74,7 @@ export const cloudflareAdapter: SearchAdapter = {
|
|
|
80
74
|
limit: Math.min(params.limit, MAX_RESULTS),
|
|
81
75
|
options: { gateway: { id: GATEWAY_ID } },
|
|
82
76
|
};
|
|
83
|
-
return { instance, path:
|
|
77
|
+
return { instance, path: CLOUDFLARE_TOOL_PATHS.search, method: "POST", body };
|
|
84
78
|
},
|
|
85
79
|
|
|
86
80
|
checkSearch(response: ProviderResponse): void {
|
|
@@ -111,7 +105,7 @@ export const cloudflareAdapter: SearchAdapter = {
|
|
|
111
105
|
if (params.format === "json") {
|
|
112
106
|
body.response_format = { type: "json_schema", json_schema: params.schema };
|
|
113
107
|
}
|
|
114
|
-
return { instance, path:
|
|
108
|
+
return { instance, path: CLOUDFLARE_TOOL_PATHS[params.format], method: "POST", body };
|
|
115
109
|
},
|
|
116
110
|
|
|
117
111
|
checkExtract(response: ProviderResponse, params: ExtractParams): void {
|
|
@@ -121,7 +115,8 @@ export const cloudflareAdapter: SearchAdapter = {
|
|
|
121
115
|
},
|
|
122
116
|
|
|
123
117
|
mapExtract(response: ProviderResponse, params: ExtractParams): ExtractOutput {
|
|
124
|
-
|
|
118
|
+
const format = markdownForText(params.format);
|
|
119
|
+
return { format, content: extractContent(resultOf(response), params) ?? "" };
|
|
125
120
|
},
|
|
126
121
|
|
|
127
122
|
/** 已兑现 markdown、html、json 与 links,不按格式裁链。 */
|
package/src/adapters/deepseek.ts
CHANGED
|
@@ -93,7 +93,7 @@ export const deepseekAdapter: SearchAdapter = {
|
|
|
93
93
|
|
|
94
94
|
mapExtract(response: ProviderResponse): ExtractOutput {
|
|
95
95
|
const data = asRecord(response.data) ?? {};
|
|
96
|
-
return { title: asString(data.title), content: asString(data.content) ?? "" };
|
|
96
|
+
return { format: "markdown", title: asString(data.title), content: asString(data.content) ?? "" };
|
|
97
97
|
},
|
|
98
98
|
|
|
99
99
|
unsupportedExtract: markdownOnlyExtract,
|
package/src/adapters/exa.ts
CHANGED
|
@@ -146,7 +146,7 @@ export const exaAdapter: SearchAdapter = {
|
|
|
146
146
|
mapExtract(response: ProviderResponse): ExtractOutput {
|
|
147
147
|
const data = asRecord(response.data) ?? {};
|
|
148
148
|
const first = asRecord(asArray(data.results)[0]) ?? {};
|
|
149
|
-
return { title: asString(first.title), content: asString(first.text) ?? "" };
|
|
149
|
+
return { format: "markdown", title: asString(first.title), content: asString(first.text) ?? "" };
|
|
150
150
|
},
|
|
151
151
|
|
|
152
152
|
unsupportedExtract: markdownOnlyExtract,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ResolvedInstance } from "../config.ts";
|
|
2
|
-
import {
|
|
2
|
+
import type { ProviderResponse, RequestSpec } from "../http.ts";
|
|
3
3
|
import type {
|
|
4
4
|
ExtractOutput,
|
|
5
5
|
ExtractParams,
|
|
@@ -11,16 +11,17 @@ import type {
|
|
|
11
11
|
SearchOutput,
|
|
12
12
|
SearchParams,
|
|
13
13
|
} from "./types.ts";
|
|
14
|
-
import { asArray, asRecord, asString, businessFailure, qualityGapNote, rejectUnsupported, sourceModeGapNote } from "./util.ts";
|
|
14
|
+
import { asArray, asRecord, asString, businessFailure, exclusiveDomainGaps, markdownForText, qualityGapNote, rejectUnsupported, sourceModeGapNote } from "./util.ts";
|
|
15
15
|
|
|
16
16
|
const MAX_RESULTS = 100;
|
|
17
17
|
|
|
18
18
|
/** 官方 tbs 的预置时间范围。 */
|
|
19
19
|
const FRESHNESS_TBS = { day: "qdr:d", week: "qdr:w", month: "qdr:m", year: "qdr:y" } as const;
|
|
20
20
|
|
|
21
|
-
/** 搜索响应里没有 answer 字段。 */
|
|
22
21
|
function unsupportedParams(params: SearchParams): string[] {
|
|
23
|
-
|
|
22
|
+
const names = exclusiveDomainGaps(params);
|
|
23
|
+
if (params.answer) names.push("answer");
|
|
24
|
+
return names;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
/** 上游用 success 判成败,错误在 error 与 code 里。 */
|
|
@@ -102,13 +103,6 @@ export const firecrawlAdapter: SearchAdapter = {
|
|
|
102
103
|
|
|
103
104
|
buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
|
|
104
105
|
rejectUnsupported(instance, "firecrawl", unsupportedParams(params));
|
|
105
|
-
if (params.domains?.length && params.excludeDomains?.length) {
|
|
106
|
-
throw new ProviderError({
|
|
107
|
-
instance: instance.id,
|
|
108
|
-
code: "invalidRequest",
|
|
109
|
-
message: "firecrawl 不能同时指定 domains 与 excludeDomains",
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
106
|
|
|
113
107
|
const body: Record<string, unknown> = {
|
|
114
108
|
query: params.query,
|
|
@@ -150,7 +144,7 @@ export const firecrawlAdapter: SearchAdapter = {
|
|
|
150
144
|
const formats =
|
|
151
145
|
params.format === "json"
|
|
152
146
|
? [{ type: "json", schema: params.schema }]
|
|
153
|
-
: [params.format
|
|
147
|
+
: [markdownForText(params.format)];
|
|
154
148
|
return { instance, path: "scrape", method: "POST", body: { url: params.url, formats, maxAge: 0 } };
|
|
155
149
|
},
|
|
156
150
|
|
|
@@ -164,7 +158,8 @@ export const firecrawlAdapter: SearchAdapter = {
|
|
|
164
158
|
mapExtract(response: ProviderResponse, params: ExtractParams): ExtractOutput {
|
|
165
159
|
const data = envelope(response);
|
|
166
160
|
const metadata = asRecord(data.metadata) ?? {};
|
|
167
|
-
|
|
161
|
+
const format = markdownForText(params.format);
|
|
162
|
+
return { format, title: asString(metadata.title), content: extractContent(data, params) ?? "" };
|
|
168
163
|
},
|
|
169
164
|
|
|
170
165
|
/** 已兑现 markdown、html、json 与 links,不按格式裁链。 */
|
package/src/adapters/tavily.ts
CHANGED
|
@@ -44,6 +44,12 @@ function hitOf(value: unknown, params: SearchParams): SearchHit | undefined {
|
|
|
44
44
|
return hit;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
function extractedBody(data: Record<string, unknown>, params: ExtractParams): string | undefined {
|
|
48
|
+
const raw = asRecord(asArray(data.results)[0])?.raw_content;
|
|
49
|
+
if (params.format === "text") return typeof raw === "string" && raw.length > 0 ? raw : undefined;
|
|
50
|
+
return asString(raw);
|
|
51
|
+
}
|
|
52
|
+
|
|
47
53
|
/** Tavily:Bearer 鉴权,search 与 extract 都是 POST;结果字段见官方 API 参考。 */
|
|
48
54
|
export const tavilyAdapter: SearchAdapter = {
|
|
49
55
|
preset: "tavily",
|
|
@@ -99,7 +105,7 @@ export const tavilyAdapter: SearchAdapter = {
|
|
|
99
105
|
return { instance, path: "extract", method: "POST", body };
|
|
100
106
|
},
|
|
101
107
|
|
|
102
|
-
checkExtract(response: ProviderResponse): void {
|
|
108
|
+
checkExtract(response: ProviderResponse, params: ExtractParams): void {
|
|
103
109
|
const data = asRecord(response.data);
|
|
104
110
|
if (!data) throw businessFailure(response, "tavily 响应格式无法解析");
|
|
105
111
|
|
|
@@ -110,14 +116,13 @@ export const tavilyAdapter: SearchAdapter = {
|
|
|
110
116
|
const reason = asString(first?.error) ?? "未知原因";
|
|
111
117
|
throw businessFailure(response, `tavily 提取失败:${url}(${reason})`, "network");
|
|
112
118
|
}
|
|
113
|
-
|
|
114
|
-
if (!raw) throw businessFailure(response, "tavily 提取未返回正文", "network");
|
|
119
|
+
if (extractedBody(data, params) === undefined) throw businessFailure(response, "tavily 提取未返回正文", "network");
|
|
115
120
|
},
|
|
116
121
|
|
|
117
|
-
mapExtract(response: ProviderResponse): ExtractOutput {
|
|
122
|
+
mapExtract(response: ProviderResponse, params: ExtractParams): ExtractOutput {
|
|
118
123
|
const data = asRecord(response.data) ?? {};
|
|
119
|
-
const
|
|
120
|
-
return { content };
|
|
124
|
+
const format = params.format === "text" ? "text" : "markdown";
|
|
125
|
+
return { format, content: extractedBody(data, params) ?? "" };
|
|
121
126
|
},
|
|
122
127
|
|
|
123
128
|
unsupportedExtract(params) {
|
package/src/adapters/tencent.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
SearchParams,
|
|
9
9
|
SourceMode,
|
|
10
10
|
} from "./types.ts";
|
|
11
|
-
import { asArray, asNumber, asRecord, asString, businessFailure, countryGapNote, qualityGapNote, rejectNoMap, rejectUnsupported } from "./util.ts";
|
|
11
|
+
import { asArray, asNumber, asRecord, asString, businessFailure, countryGapNote, exclusiveDomainGaps, qualityGapNote, rejectNoMap, rejectUnsupported } from "./util.ts";
|
|
12
12
|
|
|
13
13
|
/** SearchPro 只按 10/20 条档位返回,超出 10 条的一律请求 20 条。 */
|
|
14
14
|
const CNT_TIERS = [10, 20] as const;
|
|
@@ -31,7 +31,7 @@ function unsupportedParams(params: SearchParams): string[] {
|
|
|
31
31
|
const domains = params.domains ?? [];
|
|
32
32
|
const excluded = params.excludeDomains ?? [];
|
|
33
33
|
if (domains.length > 1) names.push("多个 domains");
|
|
34
|
-
|
|
34
|
+
names.push(...exclusiveDomainGaps(params));
|
|
35
35
|
if (excluded.length > MAX_EXCLUDE_DOMAINS) names.push(`超过 ${MAX_EXCLUDE_DOMAINS} 个 excludeDomains`);
|
|
36
36
|
if (params.answer) names.push("answer");
|
|
37
37
|
if (params.contents === "markdown") names.push("contents=markdown");
|
package/src/adapters/types.ts
CHANGED
|
@@ -61,8 +61,10 @@ export interface SearchOutput {
|
|
|
61
61
|
rateLimit?: RateLimitInfo;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/**
|
|
65
|
-
export
|
|
64
|
+
/** web-fetch 可请求的输出格式。 */
|
|
65
|
+
export const FETCH_FORMATS = ["markdown", "text", "html", "json", "links"] as const;
|
|
66
|
+
|
|
67
|
+
export type FetchOutputFormat = (typeof FETCH_FORMATS)[number];
|
|
66
68
|
|
|
67
69
|
/** 与供应商无关的抓取参数;url、format 与 schema 已由 search-service 规范化。 */
|
|
68
70
|
export interface ExtractParams {
|
|
@@ -72,6 +74,8 @@ export interface ExtractParams {
|
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
export interface ExtractOutput {
|
|
77
|
+
/** 供应商实际返回的正文格式。 */
|
|
78
|
+
format: FetchOutputFormat;
|
|
75
79
|
title?: string;
|
|
76
80
|
content: string;
|
|
77
81
|
}
|
|
@@ -105,8 +109,8 @@ export interface AdapterCapabilities {
|
|
|
105
109
|
export interface SearchAdapter {
|
|
106
110
|
readonly preset: PresetId;
|
|
107
111
|
readonly capabilities: AdapterCapabilities;
|
|
108
|
-
/**
|
|
109
|
-
unsupported(params: SearchParams): string[];
|
|
112
|
+
/** 当前实例无法兑现的搜索参数;search-service 据此跳过实例。 */
|
|
113
|
+
unsupported(params: SearchParams, instance: ResolvedInstance): string[];
|
|
110
114
|
buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec;
|
|
111
115
|
/** HTTP 200 里的业务失败在这里抛 ProviderError。 */
|
|
112
116
|
checkSearch?(response: ProviderResponse, params: SearchParams): void;
|
package/src/adapters/util.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ErrorClass, ResolvedInstance } from "../config.ts";
|
|
2
2
|
import { ProviderError, type ProviderResponse } from "../http.ts";
|
|
3
3
|
import type { PresetId } from "../presets.ts";
|
|
4
|
-
import type { ExtractParams, SearchQuality, SourceMode, Topic } from "./types.ts";
|
|
4
|
+
import type { ExtractParams, FetchOutputFormat, SearchParams, SearchQuality, SourceMode, Topic } from "./types.ts";
|
|
5
5
|
|
|
6
6
|
export function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
7
7
|
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
@@ -36,7 +36,12 @@ export function businessFailure(
|
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
/**
|
|
39
|
+
/** 仅用于不能同时接收包含和排除列表的搜索接口。 */
|
|
40
|
+
export function exclusiveDomainGaps(params: SearchParams): string[] {
|
|
41
|
+
return params.domains?.length && params.excludeDomains?.length ? ["domains 与 excludeDomains 同时使用"] : [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 请求构造与服务预筛使用同一份不支持参数清单。 */
|
|
40
45
|
export function rejectUnsupported(
|
|
41
46
|
instance: ResolvedInstance,
|
|
42
47
|
preset: PresetId,
|
|
@@ -59,7 +64,12 @@ export function rejectNoMap(instanceId: string, preset: string): never {
|
|
|
59
64
|
});
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
/**
|
|
67
|
+
/** 没有原生纯文本的多格式抓取接口用 Markdown 兑现 text。 */
|
|
68
|
+
export function markdownForText(format: FetchOutputFormat) {
|
|
69
|
+
return format === "text" ? "markdown" : format;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 只返回 Markdown 的接口也能满足 text 请求,其他格式在预筛时跳过。 */
|
|
63
73
|
export function markdownOnlyExtract(params: ExtractParams): string[] {
|
|
64
74
|
return params.format === "markdown" || params.format === "text" ? [] : [params.format];
|
|
65
75
|
}
|