pi-parallel-web-search 1.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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +34 -0
  3. package/index.ts +198 -0
  4. package/package.json +13 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Philipp Spiess
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,34 @@
1
+ # pi-parallel-web-search
2
+
3
+ A [pi](https://github.com/badlogic/pi) extension that adds a `web_search` tool powered by [Parallel AI](https://www.parallel.ai/). Lets the LLM search the web for up-to-date information like recent news, documentation, product releases, or current events.
4
+
5
+ ## Setup
6
+
7
+ 1. Get an API key from [platform.parallel.ai](https://platform.parallel.ai/home) — you get **$80 of free credits** (~16,000 web searches)
8
+ 2. Set the `PARALLEL_API_KEY` environment variable:
9
+ ```bash
10
+ export PARALLEL_API_KEY=your-api-key
11
+ ```
12
+ 3. Copy this extension to your pi extensions directory:
13
+ ```bash
14
+ cp -r . ~/.pi/agent/extensions/web-search
15
+ cd ~/.pi/agent/extensions/web-search
16
+ npm install
17
+ ```
18
+ 4. Reload pi with `/reload` or restart it
19
+
20
+ ## Usage
21
+
22
+ Once loaded, the LLM has access to a `web_search` tool and will use it automatically when it needs current information. You can also ask it directly:
23
+
24
+ > Search the web for the latest Node.js release
25
+
26
+ The tool accepts:
27
+
28
+ - **`objective`** — what you're looking for and why
29
+ - **`search_queries`** — 1–5 search queries for comprehensive results
30
+ - **`max_results`** — maximum number of results (1–20, default 10)
31
+
32
+ ## License
33
+
34
+ MIT
package/index.ts ADDED
@@ -0,0 +1,198 @@
1
+ /**
2
+ * Web Search Extension - Search the web using Parallel AI
3
+ *
4
+ * Provides a `web_search` tool that lets the LLM search the web for current information.
5
+ * Requires PARALLEL_API_KEY environment variable.
6
+ */
7
+
8
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
9
+ import { Text } from "@mariozechner/pi-tui";
10
+ import { Type } from "@sinclair/typebox";
11
+ import Parallel from "parallel-web";
12
+
13
+ const WebSearchParams = Type.Object({
14
+ objective: Type.String({
15
+ description:
16
+ "The search objective - describe what information you're looking for and why",
17
+ }),
18
+ search_queries: Type.Array(Type.String(), {
19
+ description:
20
+ "List of search queries to run (1-5 queries for comprehensive results)",
21
+ minItems: 1,
22
+ maxItems: 5,
23
+ }),
24
+ max_results: Type.Optional(
25
+ Type.Number({
26
+ description: "Maximum number of results to return (default: 10)",
27
+ minimum: 1,
28
+ maximum: 20,
29
+ })
30
+ ),
31
+ });
32
+
33
+ interface WebSearchDetails {
34
+ objective: string;
35
+ queryCount: number;
36
+ resultCount: number;
37
+ queries: string[];
38
+ }
39
+
40
+ export default function (pi: ExtensionAPI) {
41
+ const apiKey = process.env.PARALLEL_API_KEY;
42
+
43
+ if (!apiKey) {
44
+ pi.on("session_start", async (_event, ctx) => {
45
+ if (ctx.hasUI) {
46
+ ctx.ui.notify(
47
+ "web_search: PARALLEL_API_KEY not set — tool disabled",
48
+ "warning"
49
+ );
50
+ }
51
+ });
52
+ return;
53
+ }
54
+
55
+ const client = new Parallel({ apiKey });
56
+
57
+ pi.registerTool({
58
+ name: "web_search",
59
+ label: "Web Search",
60
+ description: "Search the web for current information. Use this when you need up-to-date information that may not be in your training data, such as recent news, documentation, product releases, or current events.",
61
+ parameters: WebSearchParams,
62
+
63
+ async execute(_toolCallId, params, signal, onUpdate) {
64
+ const {
65
+ objective,
66
+ search_queries,
67
+ max_results = 10,
68
+ } = params;
69
+
70
+ onUpdate?.({
71
+ content: [
72
+ {
73
+ type: "text",
74
+ text: `Searching: ${search_queries.join(", ")}`,
75
+ },
76
+ ],
77
+ });
78
+
79
+ let search;
80
+ try {
81
+ search = await client.beta.search({
82
+ objective,
83
+ search_queries,
84
+ max_results,
85
+ max_chars_per_result: 10000,
86
+ });
87
+ } catch (err: any) {
88
+ return {
89
+ content: [
90
+ { type: "text", text: `Search failed: ${err.message}` },
91
+ ],
92
+ isError: true,
93
+ details: {
94
+ objective,
95
+ queryCount: search_queries.length,
96
+ resultCount: 0,
97
+ queries: search_queries,
98
+ } as WebSearchDetails,
99
+ };
100
+ }
101
+
102
+ if (signal?.aborted) {
103
+ return {
104
+ content: [{ type: "text", text: "Search cancelled" }],
105
+ details: {
106
+ objective,
107
+ queryCount: search_queries.length,
108
+ resultCount: 0,
109
+ queries: search_queries,
110
+ } as WebSearchDetails,
111
+ };
112
+ }
113
+
114
+ const results = search.results ?? [];
115
+
116
+ if (results.length === 0) {
117
+ return {
118
+ content: [{ type: "text", text: "No results found." }],
119
+ details: {
120
+ objective,
121
+ queryCount: search_queries.length,
122
+ resultCount: 0,
123
+ queries: search_queries,
124
+ } as WebSearchDetails,
125
+ };
126
+ }
127
+
128
+ // Format results
129
+ const resultText = results
130
+ .map((r: any, i: number) => {
131
+ const parts = [`## Result ${i + 1}`];
132
+ if (r.title) parts.push(`**Title:** ${r.title}`);
133
+ if (r.url) parts.push(`**URL:** ${r.url}`);
134
+ if (r.content) parts.push(`\n${r.content}`);
135
+ else if (r.text) parts.push(`\n${r.text}`);
136
+ else if (r.snippet) parts.push(`\n${r.snippet}`);
137
+ return parts.join("\n");
138
+ })
139
+ .join("\n\n---\n\n");
140
+
141
+ return {
142
+ content: [{ type: "text", text: resultText }],
143
+ details: {
144
+ objective,
145
+ queryCount: search_queries.length,
146
+ resultCount: results.length,
147
+ queries: search_queries,
148
+ } as WebSearchDetails,
149
+ };
150
+ },
151
+
152
+ renderCall(args, theme) {
153
+ let text = theme.fg("toolTitle", theme.bold("web_search "));
154
+ const queries =
155
+ args.search_queries?.map((q: string) => `"${q}"`).join(", ") ?? "";
156
+ text += theme.fg("accent", queries);
157
+ return new Text(text, 0, 0);
158
+ },
159
+
160
+ renderResult(result, { expanded, isPartial }, theme) {
161
+ const details = result.details as WebSearchDetails | undefined;
162
+
163
+ if (isPartial) {
164
+ return new Text(theme.fg("warning", "Searching the web..."), 0, 0);
165
+ }
166
+
167
+ if (result.isError) {
168
+ const errText =
169
+ result.content[0]?.type === "text" ? result.content[0].text : "Error";
170
+ return new Text(theme.fg("error", errText), 0, 0);
171
+ }
172
+
173
+ if (!details || details.resultCount === 0) {
174
+ return new Text(theme.fg("dim", "No results found"), 0, 0);
175
+ }
176
+
177
+ let text = theme.fg(
178
+ "success",
179
+ `${details.resultCount} results for ${details.queryCount} ${details.queryCount === 1 ? "query" : "queries"}`
180
+ );
181
+
182
+ if (expanded) {
183
+ const content = result.content[0];
184
+ if (content?.type === "text") {
185
+ const lines = content.text.split("\n").slice(0, 40);
186
+ for (const line of lines) {
187
+ text += `\n${theme.fg("dim", line)}`;
188
+ }
189
+ if (content.text.split("\n").length > 40) {
190
+ text += `\n${theme.fg("muted", "...")}`;
191
+ }
192
+ }
193
+ }
194
+
195
+ return new Text(text, 0, 0);
196
+ },
197
+ });
198
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "pi-parallel-web-search",
3
+ "version": "1.0.0",
4
+ "description": "A pi extension that adds a web_search tool powered by Parallel AI",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "pi": {
8
+ "extensions": ["./index.ts"]
9
+ },
10
+ "dependencies": {
11
+ "parallel-web": "latest"
12
+ }
13
+ }