pi-grok-search 2.0.0

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.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +151 -0
  3. package/README_EN.md +150 -0
  4. package/index.ts +1972 -0
  5. package/package.json +27 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # pi-grok-search
2
+
3
+ [English](./README_EN.md) | 简体中文
4
+
5
+ 通过 [Grok API](https://docs.x.ai/) + [Tavily](https://tavily.com/) + [Firecrawl](https://firecrawl.dev/) 为 [pi](https://github.com/earendil-works/pi-mono) 提供完整的网络访问能力。
6
+
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
8
+
9
+ > 参考: [GrokSearch MCP](https://github.com/GuDaStudio/GrokSearch)
10
+
11
+ ## 双引擎架构
12
+
13
+ ```
14
+ pi ──Extension──► pi-grok-search
15
+ ├─ grok_search ───► Grok API(AI 深度搜索)
16
+ ├─ grok_sources ───► 信源缓存(按 session_id)
17
+ ├─ web_fetch ───► Tavily Extract → Firecrawl Scrape(自动降级)
18
+ ├─ web_map ───► Tavily Map(站点映射)
19
+ └─ search_planning ──► 6 阶段结构化搜索规划
20
+ ```
21
+
22
+ ## 功能特性
23
+
24
+ - **🔍 AI 深度搜索** — Grok 驱动,自动时间注入,支持平台聚焦
25
+ - **📄 网页抓取** — Tavily Extract → Firecrawl Scrape 自动降级
26
+ - **🗺️ 站点映射** — Tavily Map 遍历网站结构
27
+ - **📋 搜索规划** — 6 阶段结构化规划
28
+ - **💾 信源缓存** — session_id 索引,按需获取
29
+ - **🔄 智能重试** — Retry-After 头解析 + 指数退避
30
+ - **⚙️ 交互式配置** — CLI 菜单配置 Grok/Tavily/Firecrawl API
31
+ - **🔍 连接诊断** — 一键测试所有 API 连通性
32
+
33
+ ## 安装
34
+
35
+ ### 方式一:pi install(推荐)
36
+
37
+ ```bash
38
+ # 从 GitHub 安装
39
+ pi install git:github.com/justhiL/pi-grok-search
40
+
41
+ # 或指定版本
42
+ pi install git:github.com/justhiL/pi-grok-search@v2.0.0
43
+ ```
44
+
45
+ ### 方式二:手动安装
46
+
47
+ ```bash
48
+ # 全局
49
+ git clone https://github.com/justhiL/pi-grok-search.git ~/.pi/agent/extensions/pi-grok-search/
50
+
51
+ # 项目本地
52
+ git clone https://github.com/justhiL/pi-grok-search.git .pi/extensions/pi-grok-search/
53
+ ```
54
+
55
+ ### 方式三:测试运行
56
+
57
+ ```bash
58
+ pi -e git:github.com/justhiL/pi-grok-search
59
+ ```
60
+
61
+ ## 配置
62
+
63
+ 安装后在 pi 中运行 `/grok-config` 进入交互式配置菜单,或直接设置环境变量:
64
+
65
+ ### 环境变量
66
+
67
+ ```bash
68
+ # Grok(必填)
69
+ export GROK_API_URL="https://api.x.ai/v1"
70
+ export GROK_API_KEY="xai-your-key"
71
+ export GROK_MODEL="grok-4-fast" # 可选
72
+
73
+ # Tavily(可选,提供 web_fetch / web_map)
74
+ export TAVILY_API_KEY="tvly-your-key"
75
+
76
+ # Firecrawl(可选,Tavily 失败时托底)
77
+ export FIRECRAWL_API_KEY="fc-your-key"
78
+ ```
79
+
80
+ ### 交互式配置
81
+
82
+ 在 pi 中输入:
83
+
84
+ ```
85
+ /grok-config
86
+ ```
87
+
88
+ 支持:查看配置、设置 Grok/Tavily/Firecrawl API、切换模型、测试连接。
89
+
90
+ ### 配置文件
91
+
92
+ 持久化到 `~/.config/pi-grok-search/config.json`:
93
+
94
+ ```json
95
+ {
96
+ "apiUrl": "https://api.x.ai/v1",
97
+ "apiKey": "xai-your-key",
98
+ "model": "grok-4-fast",
99
+ "tavilyApiKey": "tvly-your-key",
100
+ "firecrawlApiKey": "fc-your-key"
101
+ }
102
+ ```
103
+
104
+ ## 使用
105
+
106
+ ### 命令
107
+
108
+ | 命令 | 说明 |
109
+ | ------------------------ | -------------------------- |
110
+ | `/grok-search <query>` | 搜索网络信息 |
111
+ | `/grok-config` | 交互式配置管理 |
112
+ | `/grok-model [model-id]` | 切换 Grok 模型 |
113
+ | `/pi-ext-docs [topic]` | 搜索 pi Extension 开发文档 |
114
+
115
+ ### 工具(LLM 自动调用)
116
+
117
+ | 工具 | 说明 |
118
+ | ----------------- | ------------------------------------------- |
119
+ | `grok_search` | AI 深度搜索,返回结果 + session_id |
120
+ | `grok_sources` | 通过 session_id 获取信源列表 |
121
+ | `web_fetch` | 抓取网页内容(Tavily → Firecrawl 自动降级) |
122
+ | `web_map` | 遍历网站结构,生成站点地图 |
123
+ | `grok_config` | 查看/修改/测试配置 |
124
+ | `search_planning` | 6 阶段结构化搜索规划 |
125
+
126
+ 安装后 LLM 会自动识别这些工具,根据用户问题自主决定调用。
127
+
128
+ ## 信源质量准则
129
+
130
+ 本扩展内置了严格的搜索行为规范(通过 `promptGuidelines` 注入系统提示):
131
+
132
+ - 搜索用英文,输出用中文
133
+ - 即使有内部知识也必须搜索验证
134
+ - 关键事实需 ≥2 个独立来源支持
135
+ - 冲突来源需呈现双方证据
136
+ - 不确定时先说明局限性
137
+
138
+ ## 相关链接
139
+
140
+ - [linux do](https://linux.do)
141
+ - [本项目 GitHub](https://github.com/justhiL/pi-grok-search)
142
+ - [pi 官方文档](https://github.com/earendil-works/pi-mono)
143
+ - [pi Extension 文档](https://github.com/earendil-works/pi-mono/blob/main/packages/coding-agent/docs/extensions.md)
144
+ - [Grok API](https://docs.x.ai/)
145
+ - [Tavily API](https://docs.tavily.com/)
146
+ - [Firecrawl API](https://docs.firecrawl.dev/)
147
+ - [GrokSearch MCP 参考](https://github.com/GuDaStudio/GrokSearch)
148
+
149
+ ## License
150
+
151
+ [MIT](LICENSE)
package/README_EN.md ADDED
@@ -0,0 +1,150 @@
1
+ # pi-grok-search
2
+
3
+ English | [简体中文](./README.md)
4
+
5
+ Complete web access for [pi](https://github.com/earendil-works/pi-mono) powered by [Grok API](https://docs.x.ai/) + [Tavily](https://tavily.com/) + [Firecrawl](https://firecrawl.dev/).
6
+
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
8
+
9
+ > Inspired by [GrokSearch MCP](https://github.com/GuDaStudio/GrokSearch)
10
+
11
+ ## Architecture
12
+
13
+ ```
14
+ pi ──Extension──► pi-grok-search
15
+ ├─ grok_search ───► Grok API (AI Deep Search)
16
+ ├─ grok_sources ───► Source Cache (by session_id)
17
+ ├─ web_fetch ───► Tavily Extract → Firecrawl Scrape (auto-fallback)
18
+ ├─ web_map ───► Tavily Map (Site Mapping)
19
+ └─ search_planning ──► 6-Phase Structured Search Planning
20
+ ```
21
+
22
+ ## Features
23
+
24
+ - **🔍 AI Deep Search** — Grok-powered, auto time injection, platform focus
25
+ - **📄 Web Fetch** — Tavily Extract → Firecrawl Scrape auto-fallback
26
+ - **🗺️ Site Mapping** — Tavily Map traverses website structure
27
+ - **📋 Search Planning** — 6-phase structured planning
28
+ - **💾 Source Cache** — session_id indexed, on-demand retrieval
29
+ - **🔄 Smart Retry** — Retry-After header parsing + exponential backoff
30
+ - **⚙️ Interactive Config** — CLI menu for Grok/Tavily/Firecrawl API
31
+ - **🔍 Connection Diagnostics** — One-click test all API connectivity
32
+
33
+ ## Installation
34
+
35
+ ### Option 1: pi install (Recommended)
36
+
37
+ ```bash
38
+ # Install from GitHub
39
+ pi install git:github.com/justhiL/pi-grok-search
40
+
41
+ # Or with specific version
42
+ pi install git:github.com/justhiL/pi-grok-search@v2.0.0
43
+ ```
44
+
45
+ ### Option 2: Manual Install
46
+
47
+ ```bash
48
+ # Global
49
+ git clone https://github.com/justhiL/pi-grok-search.git ~/.pi/agent/extensions/pi-grok-search/
50
+
51
+ # Project-local
52
+ git clone https://github.com/justhiL/pi-grok-search.git .pi/extensions/pi-grok-search/
53
+ ```
54
+
55
+ ### Option 3: Test Run
56
+
57
+ ```bash
58
+ pi -e git:github.com/justhiL/pi-grok-search
59
+ ```
60
+
61
+ ## Configuration
62
+
63
+ After installation, run `/grok-config` in pi for interactive configuration, or set environment variables directly:
64
+
65
+ ### Environment Variables
66
+
67
+ ```bash
68
+ # Grok (required)
69
+ export GROK_API_URL="https://api.x.ai/v1"
70
+ export GROK_API_KEY="xai-your-key"
71
+ export GROK_MODEL="grok-4-fast" # optional
72
+
73
+ # Tavily (optional, provides web_fetch / web_map)
74
+ export TAVILY_API_KEY="tvly-your-key"
75
+
76
+ # Firecrawl (optional, fallback when Tavily fails)
77
+ export FIRECRAWL_API_KEY="fc-your-key"
78
+ ```
79
+
80
+ ### Interactive Config
81
+
82
+ In pi, type:
83
+
84
+ ```
85
+ /grok-config
86
+ ```
87
+
88
+ Supports: view config, set Grok/Tavily/Firecrawl API, switch model, test connections.
89
+
90
+ ### Config File
91
+
92
+ Persisted to `~/.config/pi-grok-search/config.json`:
93
+
94
+ ```json
95
+ {
96
+ "apiUrl": "https://api.x.ai/v1",
97
+ "apiKey": "xai-your-key",
98
+ "model": "grok-4-fast",
99
+ "tavilyApiKey": "tvly-your-key",
100
+ "firecrawlApiKey": "fc-your-key"
101
+ }
102
+ ```
103
+
104
+ ## Usage
105
+
106
+ ### Commands
107
+
108
+ | Command | Description |
109
+ | ------------------------ | ------------------------- |
110
+ | `/grok-search <query>` | Search the web |
111
+ | `/grok-config` | Interactive configuration |
112
+ | `/grok-model [model-id]` | Switch Grok model |
113
+ | `/pi-ext-docs [topic]` | Search pi Extension docs |
114
+
115
+ ### Tools (Auto-invoked by LLM)
116
+
117
+ | Tool | Description |
118
+ | ----------------- | ---------------------------------------------------- |
119
+ | `grok_search` | AI deep search, returns results + session_id |
120
+ | `grok_sources` | Retrieve source list by session_id |
121
+ | `web_fetch` | Fetch web content (Tavily → Firecrawl auto-fallback) |
122
+ | `web_map` | Traverse website structure, generate site map |
123
+ | `grok_config` | View / modify / test configuration |
124
+ | `search_planning` | 6-phase structured search planning |
125
+
126
+ After installation, LLM automatically recognizes these tools and decides when to call them.
127
+
128
+ ## Search Quality Guidelines
129
+
130
+ Built-in behavioral guidelines (injected into system prompt via `promptGuidelines`):
131
+
132
+ - Search queries in English, user output in Chinese
133
+ - Must verify with search even if internal knowledge exists
134
+ - Key claims require ≥2 independent sources
135
+ - Conflicting sources: present evidence from both sides
136
+ - State limitations when uncertain
137
+
138
+ ## Links
139
+
140
+ - [GitHub](https://github.com/justhiL/pi-grok-search)
141
+ - [pi Official Docs](https://github.com/earendil-works/pi-mono)
142
+ - [pi Extension Docs](https://github.com/earendil-works/pi-mono/blob/main/packages/coding-agent/docs/extensions.md)
143
+ - [Grok API](https://docs.x.ai/)
144
+ - [Tavily API](https://docs.tavily.com/)
145
+ - [Firecrawl API](https://docs.firecrawl.dev/)
146
+ - [GrokSearch MCP Reference](https://github.com/GuDaStudio/GrokSearch)
147
+
148
+ ## License
149
+
150
+ [MIT](LICENSE)