@jmcombs/pi-tavily-search 0.0.1-bootstrap.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.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/index.ts +138 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jeremy Combs
|
|
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,123 @@
|
|
|
1
|
+
# @jmcombs/pi-tavily-search
|
|
2
|
+
|
|
3
|
+
A [Pi coding agent](https://pi.dev) extension that adds real-time web search via the
|
|
4
|
+
[Tavily API](https://tavily.com).
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
# Globally (recommended)
|
|
10
|
+
pi install npm:@jmcombs/pi-tavily-search
|
|
11
|
+
|
|
12
|
+
# For a single session, without installing
|
|
13
|
+
pi -e npm:@jmcombs/pi-tavily-search
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
A Tavily API key is required. [Sign up at tavily.com](https://tavily.com) (free tier
|
|
17
|
+
available) to get one, then configure it using one of the methods below.
|
|
18
|
+
|
|
19
|
+
## What It Adds
|
|
20
|
+
|
|
21
|
+
- **Tool**: `tavily_search` — performs an advanced Tavily web search and returns up to
|
|
22
|
+
five formatted results (title, URL, content) plus the raw API response under
|
|
23
|
+
`details.raw`. The tool is callable by the LLM whenever it needs current
|
|
24
|
+
information from the public web.
|
|
25
|
+
|
|
26
|
+
## Configuration
|
|
27
|
+
|
|
28
|
+
You must configure a Tavily API key. Pi resolves the key in this order:
|
|
29
|
+
|
|
30
|
+
1. `AuthStorage` under the `tavily` key (`~/.pi/agent/auth.json`) — **recommended**.
|
|
31
|
+
2. The `TAVILY_API_KEY` environment variable.
|
|
32
|
+
|
|
33
|
+
### Option 1 — `~/.pi/agent/auth.json` (recommended)
|
|
34
|
+
|
|
35
|
+
#### Plain key
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"tavily": {
|
|
40
|
+
"type": "api_key",
|
|
41
|
+
"key": "tvly-..."
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### Shell-resolved key (macOS Keychain)
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"tavily": {
|
|
51
|
+
"type": "api_key",
|
|
52
|
+
"key": "!security find-generic-password -ws tavily"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Shell-resolved key (1Password)
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"tavily": {
|
|
62
|
+
"type": "api_key",
|
|
63
|
+
"key": "!op read 'op://Personal/tavily/credential'"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Shell-resolved key (`pass`)
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"tavily": {
|
|
73
|
+
"type": "api_key",
|
|
74
|
+
"key": "!pass show tavily"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `!`-prefixed value is executed by your shell at lookup time, so no secret is
|
|
80
|
+
ever stored on disk in plaintext.
|
|
81
|
+
|
|
82
|
+
### Option 2 — environment variable
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
export TAVILY_API_KEY="tvly-..."
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Behavior Notes
|
|
89
|
+
|
|
90
|
+
- Search depth: `advanced`
|
|
91
|
+
- Max results returned: 5
|
|
92
|
+
- The tool honors Pi's abort signal — pressing **Esc** during a search cancels the
|
|
93
|
+
HTTP request.
|
|
94
|
+
- If the API key is missing the tool returns an error result with a helpful
|
|
95
|
+
configuration hint instead of throwing.
|
|
96
|
+
- Non-2xx responses from Tavily surface as tool errors (with status, status text,
|
|
97
|
+
and response body) rather than throwing.
|
|
98
|
+
|
|
99
|
+
## Requirements
|
|
100
|
+
|
|
101
|
+
- Pi `>= 0.72.0` (uses `AuthStorage` and `ExtensionAPI`)
|
|
102
|
+
- Node `>= 20.6.0`
|
|
103
|
+
- A Tavily API key
|
|
104
|
+
|
|
105
|
+
## Development
|
|
106
|
+
|
|
107
|
+
This package lives in the [pi-extensions monorepo](https://github.com/jmcombs/pi-extensions).
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# From the repo root
|
|
111
|
+
npm ci
|
|
112
|
+
npm run check # full quality gate
|
|
113
|
+
|
|
114
|
+
# Try local changes against a real pi session
|
|
115
|
+
pi -e ./packages/tavily-search
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The smoke test in `index.test.ts` does **not** mock the Tavily API; it only
|
|
119
|
+
verifies registration shape. Real end-to-end behavior is exercised via `pi -e`.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
[MIT](./LICENSE) © Jeremy Combs
|
package/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jmcombs/pi-tavily-search — Real-time web search for the Pi coding agent.
|
|
3
|
+
*
|
|
4
|
+
* Registers a `tavily_search` tool that the LLM can call to perform a Tavily
|
|
5
|
+
* web search. The Tavily API key is resolved from (in order):
|
|
6
|
+
* 1. `AuthStorage` under the "tavily" key (`~/.pi/agent/auth.json`)
|
|
7
|
+
* 2. The `TAVILY_API_KEY` environment variable
|
|
8
|
+
*
|
|
9
|
+
* See README.md for configuration details and recommended secret-storage
|
|
10
|
+
* patterns (env var, plain auth.json, or shell-resolved 1Password / Keychain).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { AuthStorage, type ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
14
|
+
import { Type, type Static } from "typebox";
|
|
15
|
+
|
|
16
|
+
const TAVILY_SEARCH_ENDPOINT = "https://api.tavily.com/search";
|
|
17
|
+
|
|
18
|
+
// ── Tool parameter schema ──────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
const tavilySearchSchema = Type.Object({
|
|
21
|
+
query: Type.String({
|
|
22
|
+
description: "The search query to perform.",
|
|
23
|
+
minLength: 1,
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export type TavilySearchInput = Static<typeof tavilySearchSchema>;
|
|
28
|
+
|
|
29
|
+
// ── Tavily API response types ──────────────────────────────────────────
|
|
30
|
+
//
|
|
31
|
+
// Documented at https://docs.tavily.com/documentation/api-reference/endpoint/search
|
|
32
|
+
// We model only the fields we actually consume; unknown fields pass through
|
|
33
|
+
// untouched in the `details.raw` field returned by the tool.
|
|
34
|
+
|
|
35
|
+
interface TavilySearchResult {
|
|
36
|
+
title: string;
|
|
37
|
+
url: string;
|
|
38
|
+
content: string;
|
|
39
|
+
score?: number;
|
|
40
|
+
raw_content?: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface TavilySearchResponse {
|
|
44
|
+
query?: string;
|
|
45
|
+
answer?: string;
|
|
46
|
+
results?: TavilySearchResult[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── Helpers ────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
const MISSING_KEY_MESSAGE = [
|
|
52
|
+
"Error: No Tavily API key configured.",
|
|
53
|
+
"",
|
|
54
|
+
"Configure one of the following:",
|
|
55
|
+
" • Environment variable: export TAVILY_API_KEY=<your-key>",
|
|
56
|
+
' • ~/.pi/agent/auth.json: { "tavily": { "type": "api_key", "key": "<your-key>" } }',
|
|
57
|
+
' • Shell-resolved: { "tavily": { "type": "api_key", "key": "!security find-generic-password -ws tavily" } }',
|
|
58
|
+
' • 1Password: { "tavily": { "type": "api_key", "key": "!op read \'op://Personal/tavily/credential\'" } }',
|
|
59
|
+
].join("\n");
|
|
60
|
+
|
|
61
|
+
function formatResults(data: TavilySearchResponse, query: string): string {
|
|
62
|
+
const results = data.results ?? [];
|
|
63
|
+
if (results.length === 0) {
|
|
64
|
+
return `No search results found for "${query}".`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const formatted = results
|
|
68
|
+
.map((r) => `Title: ${r.title}\nURL: ${r.url}\nContent: ${r.content}\n`)
|
|
69
|
+
.join("\n---\n");
|
|
70
|
+
|
|
71
|
+
const answer = data.answer ? `Answer: ${data.answer}\n\n` : "";
|
|
72
|
+
return `${answer}Search results for "${query}":\n\n${formatted}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── Extension factory ──────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
export default function (pi: ExtensionAPI): void {
|
|
78
|
+
const authStorage = AuthStorage.create();
|
|
79
|
+
|
|
80
|
+
pi.registerTool({
|
|
81
|
+
name: "tavily_search",
|
|
82
|
+
label: "Tavily Web Search",
|
|
83
|
+
description:
|
|
84
|
+
"Performs a web search using the Tavily API to get real-time information from the internet.",
|
|
85
|
+
parameters: tavilySearchSchema,
|
|
86
|
+
async execute(_toolCallId, params, signal, _onUpdate, _ctx) {
|
|
87
|
+
const apiKey = (await authStorage.getApiKey("tavily")) ?? process.env.TAVILY_API_KEY;
|
|
88
|
+
if (!apiKey) {
|
|
89
|
+
return {
|
|
90
|
+
content: [{ type: "text", text: MISSING_KEY_MESSAGE }],
|
|
91
|
+
details: { error: "missing_api_key" },
|
|
92
|
+
isError: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const response = await fetch(TAVILY_SEARCH_ENDPOINT, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: { "Content-Type": "application/json" },
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
api_key: apiKey,
|
|
102
|
+
query: params.query,
|
|
103
|
+
search_depth: "advanced",
|
|
104
|
+
max_results: 5,
|
|
105
|
+
}),
|
|
106
|
+
signal,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
const errorText = await response.text();
|
|
111
|
+
return {
|
|
112
|
+
content: [
|
|
113
|
+
{
|
|
114
|
+
type: "text",
|
|
115
|
+
text: `Tavily API error: ${String(response.status)} ${response.statusText}\n${errorText}`,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
details: { status: response.status, body: errorText },
|
|
119
|
+
isError: true,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const data = (await response.json()) as TavilySearchResponse;
|
|
124
|
+
return {
|
|
125
|
+
content: [{ type: "text", text: formatResults(data, params.query) }],
|
|
126
|
+
details: { raw: data },
|
|
127
|
+
};
|
|
128
|
+
} catch (error) {
|
|
129
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
130
|
+
return {
|
|
131
|
+
content: [{ type: "text", text: `Error performing Tavily search: ${message}` }],
|
|
132
|
+
details: { error: message },
|
|
133
|
+
isError: true,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jmcombs/pi-tavily-search",
|
|
3
|
+
"version": "0.0.1-bootstrap.0",
|
|
4
|
+
"description": "Pi extension that performs real-time web search via the Tavily API.",
|
|
5
|
+
"homepage": "https://github.com/jmcombs/pi-extensions/tree/main/packages/tavily-search",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/jmcombs/pi-extensions.git",
|
|
9
|
+
"directory": "packages/tavily-search"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/jmcombs/pi-extensions/issues"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Jeremy Combs",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./index.ts",
|
|
18
|
+
"types": "./index.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"index.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"pi-package",
|
|
26
|
+
"pi-extension",
|
|
27
|
+
"tavily",
|
|
28
|
+
"web-search",
|
|
29
|
+
"search",
|
|
30
|
+
"rag"
|
|
31
|
+
],
|
|
32
|
+
"pi": {
|
|
33
|
+
"extensions": [
|
|
34
|
+
"./index.ts"
|
|
35
|
+
],
|
|
36
|
+
"image": "https://raw.githubusercontent.com/jmcombs/pi-extensions/main/assets/tavily-search/preview.png"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20.6.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
43
|
+
"typebox": "*"
|
|
44
|
+
}
|
|
45
|
+
}
|