pi-web-kit 0.1.6 → 0.2.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/CHANGELOG.md +8 -0
- package/README.md +44 -6
- package/extensions/index.ts +185 -7
- package/package.json +9 -6
- package/src/config.ts +3 -2
- package/src/providers/context7.ts +105 -0
- package/src/providers/exa.ts +23 -1
- package/src/providers/index.ts +9 -0
- package/src/types.ts +72 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,14 @@ This project follows the spirit of [Keep a Changelog](https://keepachangelog.com
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.2.0] - 2026-06-13
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Add `library_search` and `library_docs` tools backed by Context7.
|
|
14
|
+
- Add `code_search` backed by Exa Code.
|
|
15
|
+
- Gate optional developer-search tool registration on API key availability.
|
|
16
|
+
|
|
9
17
|
## [0.1.6] - 2026-06-06
|
|
10
18
|
|
|
11
19
|
### Changed
|
package/README.md
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# pi-web-kit
|
|
2
2
|
|
|
3
|
-
Context-efficient web
|
|
3
|
+
Context-efficient web and developer search tools for [Pi](https://pi.dev): `web_search`, `web_fetch`, `library_search`, `library_docs`, and `code_search`.
|
|
4
4
|
|
|
5
|
-
`pi-web-kit` provides provider-backed search
|
|
5
|
+
`pi-web-kit` provides provider-backed search, page fetching, library docs lookup, and code-context search with bounded output, chunked reads, URL validation, and an in-memory fetch cache designed for agent workflows.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
9
|
- `web_search` for current/external web information, including multi-query searches.
|
|
10
10
|
- `web_fetch` for reading one or more URLs, with `offset` / `limit` chunk reads for long pages.
|
|
11
|
-
-
|
|
11
|
+
- `library_search` and `library_docs` for library resolution and current, version-aware documentation/code examples.
|
|
12
|
+
- `code_search` for practical examples and implementation context.
|
|
13
|
+
- Multiple provider backends: Exa MCP, Exa API, TinyFish, Brave Search, Firecrawl, markdown.new, Context7, and Exa Code.
|
|
12
14
|
- Provider-tailored tool schemas at Pi startup/reload.
|
|
13
15
|
- URL validation: HTTP(S)-only, no embedded credentials, fragment stripping, duplicate removal, and length/count limits.
|
|
14
16
|
- In-memory fetch cache with TTL, LRU eviction, max entry count, max byte count, and cache keys based on provider/config/fetch-affecting options.
|
|
@@ -96,7 +98,8 @@ PI_OFFLINE=1 # disables install/update telemetry
|
|
|
96
98
|
PI_TELEMETRY=0 # disables install/update telemetry
|
|
97
99
|
PI_WEB_KIT_PROVIDER_SEARCH=exa_mcp|exa|tinyfish|brave|firecrawl
|
|
98
100
|
PI_WEB_KIT_PROVIDER_FETCH=exa_mcp|exa|tinyfish|markdown_new|firecrawl
|
|
99
|
-
EXA_API_KEY=...
|
|
101
|
+
EXA_API_KEY=... # enables Exa provider and code_search
|
|
102
|
+
CONTEXT7_API_KEY=... # enables library_search and library_docs
|
|
100
103
|
TINYFISH_API_KEY=...
|
|
101
104
|
BRAVE_SEARCH_API_KEY=...
|
|
102
105
|
FIRECRAWL_API_KEY=...
|
|
@@ -118,7 +121,9 @@ Example:
|
|
|
118
121
|
"provider_search": "firecrawl",
|
|
119
122
|
"provider_fetch": "markdown_new",
|
|
120
123
|
"apiKeys": {
|
|
121
|
-
"firecrawl": "..."
|
|
124
|
+
"firecrawl": "...",
|
|
125
|
+
"context7": "...",
|
|
126
|
+
"exa": "..."
|
|
122
127
|
},
|
|
123
128
|
"markdownNew": {
|
|
124
129
|
"method": "auto",
|
|
@@ -165,6 +170,39 @@ Fetches page content with the active fetch provider. Results are cached in memor
|
|
|
165
170
|
|
|
166
171
|
Provider-specific parameters are exposed only for the configured provider, such as TinyFish `format`, markdown.new `method` / `retainImages`, or Firecrawl `format`, `waitFor`, `mobile`, `location`, and `maxAge`.
|
|
167
172
|
|
|
173
|
+
### `library_search`
|
|
174
|
+
|
|
175
|
+
Resolves packages, frameworks, SDKs, APIs, CLIs, and libraries to canonical library IDs.
|
|
176
|
+
|
|
177
|
+
| Parameter | Type | Description |
|
|
178
|
+
|---|---|---|
|
|
179
|
+
| `libraryName` | string | Library/package/framework name to search for. |
|
|
180
|
+
| `query` | string | Optional user task/question for relevance ranking. |
|
|
181
|
+
| `fast` | boolean | Skip LLM reranking for lower latency. |
|
|
182
|
+
| `limit` | integer | Maximum libraries to return. Range: 1-20. Default: 10. |
|
|
183
|
+
|
|
184
|
+
### `library_docs`
|
|
185
|
+
|
|
186
|
+
Fetches current docs and code snippets for a library. Provide `libraryId`, or provide `libraryName` and the tool resolves the best match first.
|
|
187
|
+
|
|
188
|
+
| Parameter | Type | Description |
|
|
189
|
+
|---|---|---|
|
|
190
|
+
| `libraryId` | string | Canonical library ID, such as `/vercel/next.js`. |
|
|
191
|
+
| `libraryName` | string | Library name to resolve when `libraryId` is not known. |
|
|
192
|
+
| `query` | string | Specific docs question or coding task. |
|
|
193
|
+
| `version` | string | Optional version/tag to pin, appended as `@version`. |
|
|
194
|
+
| `fast` | boolean | Skip LLM reranking for lower latency. |
|
|
195
|
+
| `limit` | integer | Maximum code and info snippets to return. Range: 1-20. Default: 10. |
|
|
196
|
+
|
|
197
|
+
### `code_search`
|
|
198
|
+
|
|
199
|
+
Finds practical code examples, implementation context, setup snippets, migrations, usage patterns, and error-message research.
|
|
200
|
+
|
|
201
|
+
| Parameter | Type | Description |
|
|
202
|
+
|---|---|---|
|
|
203
|
+
| `query` | string | Code-context query. |
|
|
204
|
+
| `tokensNum` | `"dynamic"` or integer | Output token target. Integer range: 50-100000. Default: `"dynamic"`. |
|
|
205
|
+
|
|
168
206
|
## Cache and limits
|
|
169
207
|
|
|
170
208
|
`web_fetch` uses an in-memory cache for the current Pi process.
|
|
@@ -183,7 +221,7 @@ Cache keys include the provider, canonical URL, fetch-affecting parameters, rele
|
|
|
183
221
|
|
|
184
222
|
## Privacy and security
|
|
185
223
|
|
|
186
|
-
`pi-web-kit` sends search queries and fetched URLs to the configured provider. Fetch providers may also receive provider-specific options. API keys are read from environment variables or local config files and are used only for provider requests.
|
|
224
|
+
`pi-web-kit` sends search queries and fetched URLs to the configured provider. Developer-search tools send library/doc queries to Context7 and code-context queries to Exa when those tools are enabled. Fetch providers may also receive provider-specific options. API keys are read from environment variables or local config files and are used only for provider requests.
|
|
187
225
|
|
|
188
226
|
The extension rejects non-HTTP(S) URLs and URLs with embedded username/password credentials. Provider responses are not sandboxed; they are returned to Pi as tool output.
|
|
189
227
|
|
package/extensions/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { fetchCache, type CachedPage } from "../src/cache.js";
|
|
|
5
5
|
import { resolveConfig } from "../src/config.js";
|
|
6
6
|
import { DEFAULT_FETCH_LIMIT, DEFAULT_NUM_RESULTS, MAX_LIMIT, MAX_NUM_RESULTS, MAX_OFFSET, MAX_QUERY_COUNT, MAX_URL_COUNT, MULTI_FETCH_LIMIT } from "../src/limits.js";
|
|
7
7
|
import { truncateText } from "../src/http.js";
|
|
8
|
-
import { createFetchProvider, createSearchProvider } from "../src/providers/index.js";
|
|
8
|
+
import { createCodeSearchProvider, createContext7Provider, createFetchProvider, createSearchProvider } from "../src/providers/index.js";
|
|
9
9
|
import { mapFetchResults } from "../src/providers/fallback.js";
|
|
10
10
|
import type { FetchProviderName, SearchProviderName, WebFetchResult } from "../src/types.js";
|
|
11
11
|
import { canonicalWebUrl, normalizeUrlInput } from "../src/urls.js";
|
|
@@ -100,6 +100,69 @@ export default function (pi: ExtensionAPI) {
|
|
|
100
100
|
return renderWebResult("fetch", result, options, theme, context);
|
|
101
101
|
},
|
|
102
102
|
});
|
|
103
|
+
|
|
104
|
+
if (startupConfig.apiKeys.context7) {
|
|
105
|
+
pi.registerTool({
|
|
106
|
+
name: "library_search",
|
|
107
|
+
label: "Library Search",
|
|
108
|
+
description: "Resolve library, package, framework, SDK, API, or CLI names to canonical library IDs.",
|
|
109
|
+
promptSnippet: "Resolve a library name to a canonical library ID before querying docs.",
|
|
110
|
+
promptGuidelines: ["Use library_search when a library/framework/package is ambiguous or you need a canonical library ID."],
|
|
111
|
+
parameters: buildLibrarySearchSchema(),
|
|
112
|
+
async execute(_toolCallId, rawParams, signal, _onUpdate, ctx) {
|
|
113
|
+
const params = rawParams as Record<string, any>;
|
|
114
|
+
const libraryName = requiredString(params.libraryName, "libraryName");
|
|
115
|
+
const query = optionalString(params.query, "query") ?? libraryName;
|
|
116
|
+
const limit = parseInteger(params.limit, 10, "limit", 1, MAX_NUM_RESULTS);
|
|
117
|
+
const provider = createContext7Provider(runtimeConfig(pi, ctx.cwd));
|
|
118
|
+
const result = await provider.searchLibraries({ libraryName, query, fast: params.fast === true, limit }, signal);
|
|
119
|
+
return jsonToolResult(result);
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
pi.registerTool({
|
|
124
|
+
name: "library_docs",
|
|
125
|
+
label: "Library Docs",
|
|
126
|
+
description: "Fetch current, version-aware documentation and code examples for a library.",
|
|
127
|
+
promptSnippet: "Get current library documentation and code examples.",
|
|
128
|
+
promptGuidelines: ["Use library_docs for current APIs, framework behavior, SDK examples, package docs, and version-specific library questions."],
|
|
129
|
+
parameters: buildLibraryDocsSchema(),
|
|
130
|
+
async execute(_toolCallId, rawParams, signal, _onUpdate, ctx) {
|
|
131
|
+
const params = rawParams as Record<string, any>;
|
|
132
|
+
const query = requiredString(params.query, "query");
|
|
133
|
+
const limit = parseInteger(params.limit, 10, "limit", 1, MAX_NUM_RESULTS);
|
|
134
|
+
const provider = createContext7Provider(runtimeConfig(pi, ctx.cwd));
|
|
135
|
+
let libraryId = optionalString(params.libraryId, "libraryId");
|
|
136
|
+
if (!libraryId) {
|
|
137
|
+
const libraryName = requiredString(params.libraryName, "libraryName");
|
|
138
|
+
const resolved = await provider.searchLibraries({ libraryName, query, fast: params.fast === true, limit: 1 }, signal);
|
|
139
|
+
libraryId = resolved.results[0]?.id;
|
|
140
|
+
if (!libraryId) throw new Error(`No library found for '${libraryName}'. Try library_search with a more specific name.`);
|
|
141
|
+
}
|
|
142
|
+
const result = await provider.getDocs({ libraryId, query, version: optionalString(params.version, "version"), type: "json", fast: params.fast === true, limit }, signal);
|
|
143
|
+
return jsonToolResult(result);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (startupConfig.apiKeys.exa) {
|
|
149
|
+
pi.registerTool({
|
|
150
|
+
name: "code_search",
|
|
151
|
+
label: "Code Search",
|
|
152
|
+
description: "Find practical code examples, usage patterns, setup snippets, migrations, and error context.",
|
|
153
|
+
promptSnippet: "Find real-world code examples, usage patterns, migrations, and error context.",
|
|
154
|
+
promptGuidelines: ["Use code_search for real-world code examples, GitHub/open-source usage patterns, API syntax examples, setup snippets, migrations, and error messages."],
|
|
155
|
+
parameters: buildCodeSearchSchema(),
|
|
156
|
+
async execute(_toolCallId, rawParams, signal, _onUpdate, ctx) {
|
|
157
|
+
const params = rawParams as Record<string, any>;
|
|
158
|
+
const query = requiredString(params.query, "query");
|
|
159
|
+
const tokensNum = parseTokensNum(params.tokensNum);
|
|
160
|
+
const provider = createCodeSearchProvider(runtimeConfig(pi, ctx.cwd));
|
|
161
|
+
const result = await provider.searchCode({ query, tokensNum }, signal);
|
|
162
|
+
return jsonToolResult(result);
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
103
166
|
}
|
|
104
167
|
|
|
105
168
|
type ProgressKind = "search" | "fetch";
|
|
@@ -152,12 +215,39 @@ export function buildFetchSchema(provider: FetchProviderName) {
|
|
|
152
215
|
return Type.Object(props, { additionalProperties: false });
|
|
153
216
|
}
|
|
154
217
|
|
|
155
|
-
function
|
|
156
|
-
return
|
|
218
|
+
export function buildLibrarySearchSchema() {
|
|
219
|
+
return Type.Object({
|
|
220
|
+
libraryName: Type.String({ description: "Library, package, framework, SDK, API, CLI, or product name", minLength: 1, maxLength: 500 }),
|
|
221
|
+
query: Type.Optional(Type.String({ description: "User task/question used for relevance ranking", minLength: 1, maxLength: 500 })),
|
|
222
|
+
fast: Type.Optional(Type.Boolean({ description: "Skip LLM reranking for lower latency" })),
|
|
223
|
+
limit: Type.Optional(int("Maximum libraries to return", 1, MAX_NUM_RESULTS)),
|
|
224
|
+
}, { additionalProperties: false });
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function buildLibraryDocsSchema() {
|
|
228
|
+
return Type.Object({
|
|
229
|
+
libraryId: Type.Optional(Type.String({ description: "Canonical library ID, for example /vercel/next.js", minLength: 1, maxLength: 500 })),
|
|
230
|
+
libraryName: Type.Optional(Type.String({ description: "Library name to resolve when libraryId is not known", minLength: 1, maxLength: 500 })),
|
|
231
|
+
query: Type.String({ description: "Specific docs question or coding task", minLength: 1, maxLength: 500 }),
|
|
232
|
+
version: Type.Optional(Type.String({ description: "Optional version/tag to pin, appended as @version", minLength: 1, maxLength: 100 })),
|
|
233
|
+
fast: Type.Optional(Type.Boolean({ description: "Skip LLM reranking for lower latency" })),
|
|
234
|
+
limit: Type.Optional(int("Maximum code and info snippets to return", 1, MAX_NUM_RESULTS)),
|
|
235
|
+
}, { additionalProperties: false });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function buildCodeSearchSchema() {
|
|
239
|
+
return Type.Object({
|
|
240
|
+
query: Type.String({ description: "Code-context query for examples, APIs, setup, migrations, or errors", minLength: 1, maxLength: 2000 }),
|
|
241
|
+
tokensNum: Type.Optional(Type.Union([Type.Literal("dynamic"), int("Target response tokens", 50, MAX_LIMIT)])),
|
|
242
|
+
}, { additionalProperties: false });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function buildSearchDescription(_provider: SearchProviderName): string {
|
|
246
|
+
return "Search the web. Use query or queries; returns compact results grouped by query.";
|
|
157
247
|
}
|
|
158
248
|
|
|
159
|
-
function buildFetchDescription(
|
|
160
|
-
return
|
|
249
|
+
function buildFetchDescription(_provider: FetchProviderName): string {
|
|
250
|
+
return "Fetch URL content. Results are cached by URL/options; use offset/limit to read long pages in chunks.";
|
|
161
251
|
}
|
|
162
252
|
|
|
163
253
|
|
|
@@ -249,12 +339,37 @@ export function buildCacheKey(provider: FetchProviderName, url: string, params:
|
|
|
249
339
|
return `${provider}\0${scope}\0${JSON.stringify(affecting)}\0${canonical}`;
|
|
250
340
|
}
|
|
251
341
|
|
|
342
|
+
function runtimeConfig(pi: ExtensionAPI, cwd: string) {
|
|
343
|
+
return resolveConfig({ providerSearch: pi.getFlag("web-provider-search"), providerFetch: pi.getFlag("web-provider-fetch") }, cwd);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function jsonToolResult(result: unknown) {
|
|
347
|
+
const text = truncateText(JSON.stringify(result, null, 2));
|
|
348
|
+
return { content: [{ type: "text" as const, text }], details: boundedDetails(result) };
|
|
349
|
+
}
|
|
350
|
+
|
|
252
351
|
function parseInteger(value: unknown, defaultValue: number, name: string, min: number, max: number): number {
|
|
253
352
|
if (value == null) return defaultValue;
|
|
254
353
|
if (typeof value !== "number" || !Number.isInteger(value) || !Number.isFinite(value) || value < min || value > max) throw new Error(`${name} must be a finite integer between ${min} and ${max}.`);
|
|
255
354
|
return value;
|
|
256
355
|
}
|
|
257
356
|
|
|
357
|
+
function requiredString(value: unknown, name: string): string {
|
|
358
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(`${name} must be a non-empty string.`);
|
|
359
|
+
return value.trim();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function optionalString(value: unknown, name: string): string | undefined {
|
|
363
|
+
if (value == null) return undefined;
|
|
364
|
+
return requiredString(value, name);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function parseTokensNum(value: unknown): "dynamic" | number {
|
|
368
|
+
if (value == null) return "dynamic";
|
|
369
|
+
if (value === "dynamic") return "dynamic";
|
|
370
|
+
return parseInteger(value, 0, "tokensNum", 50, MAX_LIMIT);
|
|
371
|
+
}
|
|
372
|
+
|
|
258
373
|
function fetchConfigDefaults(provider: FetchProviderName, config?: any): Record<string, unknown> {
|
|
259
374
|
if (provider === "markdown_new") return { method: config?.markdownNew?.method ?? "auto", retainImages: config?.markdownNew?.retainImages ?? false };
|
|
260
375
|
if (provider === "firecrawl") return { onlyMainContent: true, format: "markdown" };
|
|
@@ -277,11 +392,74 @@ function assertProviderUnchanged(tool: string, startup: string, runtime: string)
|
|
|
277
392
|
}
|
|
278
393
|
|
|
279
394
|
function boundedDetails(value: any): unknown {
|
|
280
|
-
if (value?.queries && Array.isArray(value.queries)) return
|
|
281
|
-
if (value?.results && Array.isArray(value.results)) return
|
|
395
|
+
if (value?.queries && Array.isArray(value.queries)) return searchDetails(value);
|
|
396
|
+
if (value?.provider === "context7" && value?.results && Array.isArray(value.results)) return librarySearchDetails(value);
|
|
397
|
+
if (value?.provider === "context7" && value?.codeSnippets && value?.infoSnippets) return libraryDocsDetails(value);
|
|
398
|
+
if (value?.provider === "exa" && typeof value.response === "string") return codeSearchDetails(value);
|
|
399
|
+
if (value?.results && Array.isArray(value.results)) return fetchDetails(value);
|
|
282
400
|
return value;
|
|
283
401
|
}
|
|
284
402
|
|
|
403
|
+
function searchDetails(value: any) {
|
|
404
|
+
return {
|
|
405
|
+
provider: value.provider,
|
|
406
|
+
queries: value.queries.map((q: any) => ({
|
|
407
|
+
query: q.query,
|
|
408
|
+
resultCount: (q.results ?? []).length,
|
|
409
|
+
results: (q.results ?? []).map((r: any) => ({ title: r.title, url: r.url, siteName: r.siteName, position: r.position })),
|
|
410
|
+
})),
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function librarySearchDetails(value: any) {
|
|
415
|
+
return {
|
|
416
|
+
provider: value.provider,
|
|
417
|
+
libraryName: value.libraryName,
|
|
418
|
+
resultCount: value.results.length,
|
|
419
|
+
results: value.results.map((r: any) => ({ id: r.id, title: r.title, state: r.state, trustScore: r.trustScore, versions: r.versions })),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function libraryDocsDetails(value: any) {
|
|
424
|
+
const codeSources = value.codeSnippets.map((s: any) => s.codeId);
|
|
425
|
+
const infoSources = value.infoSnippets.map((s: any) => s.pageId);
|
|
426
|
+
return {
|
|
427
|
+
provider: value.provider,
|
|
428
|
+
libraryId: value.libraryId,
|
|
429
|
+
query: value.query,
|
|
430
|
+
codeSnippetCount: value.codeSnippets.length,
|
|
431
|
+
infoSnippetCount: value.infoSnippets.length,
|
|
432
|
+
sources: [...codeSources, ...infoSources].filter(Boolean).slice(0, MAX_NUM_RESULTS),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function codeSearchDetails(value: any) {
|
|
437
|
+
return {
|
|
438
|
+
provider: value.provider,
|
|
439
|
+
query: value.query,
|
|
440
|
+
resultsCount: value.resultsCount,
|
|
441
|
+
outputTokens: value.outputTokens,
|
|
442
|
+
requestId: value.requestId,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function fetchDetails(value: any) {
|
|
447
|
+
return {
|
|
448
|
+
provider: value.provider,
|
|
449
|
+
results: value.results.map((r: any) => ({
|
|
450
|
+
url: r.url,
|
|
451
|
+
fetchedUrl: r.fetchedUrl,
|
|
452
|
+
title: r.title,
|
|
453
|
+
format: r.format,
|
|
454
|
+
cached: r.cached,
|
|
455
|
+
refreshed: r.refreshed,
|
|
456
|
+
cacheKey: r.cacheKey,
|
|
457
|
+
range: r.range,
|
|
458
|
+
error: r.error,
|
|
459
|
+
})),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
285
463
|
function createProgress(kind: ProgressKind, provider: string, labels: string[]): WebProgress {
|
|
286
464
|
return { kind, provider, total: labels.length, completed: 0, items: labels.map((label) => ({ label, status: "pending" })) };
|
|
287
465
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Context-efficient web search and fetch tools for Pi.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"pi",
|
|
21
21
|
"web-search",
|
|
22
22
|
"web-fetch",
|
|
23
|
+
"library-docs",
|
|
24
|
+
"code-search",
|
|
25
|
+
"context7",
|
|
23
26
|
"exa",
|
|
24
27
|
"firecrawl"
|
|
25
28
|
],
|
|
@@ -55,12 +58,12 @@
|
|
|
55
58
|
"typebox": "*"
|
|
56
59
|
},
|
|
57
60
|
"devDependencies": {
|
|
58
|
-
"@earendil-works/pi-ai": "^0.
|
|
59
|
-
"@earendil-works/pi-coding-agent": "^0.
|
|
60
|
-
"@earendil-works/pi-tui": "^0.
|
|
61
|
+
"@earendil-works/pi-ai": "^0.78.0",
|
|
62
|
+
"@earendil-works/pi-coding-agent": "^0.78.0",
|
|
63
|
+
"@earendil-works/pi-tui": "^0.78.0",
|
|
61
64
|
"@types/node": "^25.6.2",
|
|
62
|
-
"tsx": "^4.
|
|
63
|
-
"typebox": "^1.1.
|
|
65
|
+
"tsx": "^4.22.4",
|
|
66
|
+
"typebox": "^1.1.39",
|
|
64
67
|
"typescript": "^6.0.3"
|
|
65
68
|
},
|
|
66
69
|
"publishConfig": {
|
package/src/config.ts
CHANGED
|
@@ -26,6 +26,7 @@ export function resolveConfig(flags: { providerSearch?: unknown; providerFetch?:
|
|
|
26
26
|
tinyfish: env.TINYFISH_API_KEY,
|
|
27
27
|
brave: env.BRAVE_SEARCH_API_KEY,
|
|
28
28
|
firecrawl: env.FIRECRAWL_API_KEY,
|
|
29
|
+
context7: env.CONTEXT7_API_KEY,
|
|
29
30
|
},
|
|
30
31
|
});
|
|
31
32
|
const home = env.HOME ?? homedir();
|
|
@@ -63,9 +64,9 @@ export function validateFetchProvider(name: string): asserts name is FetchProvid
|
|
|
63
64
|
if (!FETCH.includes(name as FetchProviderName)) throw new Error(`Unknown fetch provider '${name}'. Expected one of: ${FETCH.join(", ")}.`);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
const PROVIDER_ENV_NAMES = { exa: "EXA_API_KEY", tinyfish: "TINYFISH_API_KEY", brave: "BRAVE_SEARCH_API_KEY", firecrawl: "FIRECRAWL_API_KEY" } as const;
|
|
67
|
+
const PROVIDER_ENV_NAMES = { exa: "EXA_API_KEY", tinyfish: "TINYFISH_API_KEY", brave: "BRAVE_SEARCH_API_KEY", firecrawl: "FIRECRAWL_API_KEY", context7: "CONTEXT7_API_KEY" } as const;
|
|
67
68
|
|
|
68
|
-
export function requireKey(config: WebKitConfig, provider: "exa" | "tinyfish" | "brave" | "firecrawl"): string {
|
|
69
|
+
export function requireKey(config: WebKitConfig, provider: "exa" | "tinyfish" | "brave" | "firecrawl" | "context7"): string {
|
|
69
70
|
const key = config.apiKeys[provider];
|
|
70
71
|
const envName = PROVIDER_ENV_NAMES[provider];
|
|
71
72
|
if (!key) throw new Error(`${provider} provider requires ${envName} or apiKeys.${provider} in .pi-web-kit.json / ~/.pi/agent/pi-web-kit.json.`);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { requestJson } from "../http.js";
|
|
2
|
+
import type { Context7ContextInput, Context7DocsResult, Context7LibrarySearchInput, Context7LibrarySearchResult, WebKitConfig } from "../types.js";
|
|
3
|
+
import { requireKey } from "../config.js";
|
|
4
|
+
|
|
5
|
+
const BASE_URL = "https://context7.com/api/v2";
|
|
6
|
+
|
|
7
|
+
export class Context7Provider {
|
|
8
|
+
private key: string;
|
|
9
|
+
constructor(config: WebKitConfig) { this.key = requireKey(config, "context7"); }
|
|
10
|
+
|
|
11
|
+
async searchLibraries(input: Context7LibrarySearchInput, signal?: AbortSignal): Promise<Context7LibrarySearchResult> {
|
|
12
|
+
const query = input.query ?? input.libraryName;
|
|
13
|
+
const params = new URLSearchParams({ libraryName: input.libraryName, query });
|
|
14
|
+
addFastParam(params, input.fast);
|
|
15
|
+
const data = await requestJson<any>(`${BASE_URL}/libs/search?${params}`, {
|
|
16
|
+
headers: this.headers(),
|
|
17
|
+
signal,
|
|
18
|
+
timeoutMs: 30_000,
|
|
19
|
+
});
|
|
20
|
+
const limit = input.limit ?? 10;
|
|
21
|
+
return {
|
|
22
|
+
provider: "context7",
|
|
23
|
+
libraryName: input.libraryName,
|
|
24
|
+
query,
|
|
25
|
+
searchFilterApplied: data.searchFilterApplied,
|
|
26
|
+
results: (data.results ?? []).slice(0, limit).map(toLibraryResult).filter((r: any) => r.id),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getDocs(input: Context7ContextInput, signal?: AbortSignal): Promise<Context7DocsResult> {
|
|
31
|
+
const libraryId = withVersion(input.libraryId, input.version);
|
|
32
|
+
const params = new URLSearchParams({ libraryId, query: input.query, type: input.type ?? "json" });
|
|
33
|
+
addFastParam(params, input.fast);
|
|
34
|
+
const data = await requestJson<any>(`${BASE_URL}/context?${params}`, {
|
|
35
|
+
headers: this.headers(),
|
|
36
|
+
signal,
|
|
37
|
+
timeoutMs: 45_000,
|
|
38
|
+
});
|
|
39
|
+
const limit = input.limit ?? 10;
|
|
40
|
+
return {
|
|
41
|
+
provider: "context7",
|
|
42
|
+
libraryId,
|
|
43
|
+
query: input.query,
|
|
44
|
+
codeSnippets: (data.codeSnippets ?? []).slice(0, limit).map(toCodeSnippet),
|
|
45
|
+
infoSnippets: (data.infoSnippets ?? []).slice(0, limit).map(toInfoSnippet),
|
|
46
|
+
rules: data.rules,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private headers(): HeadersInit {
|
|
51
|
+
return { authorization: `Bearer ${this.key}` };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function addFastParam(params: URLSearchParams, fast?: boolean) {
|
|
56
|
+
if (fast != null) params.set("fast", String(fast));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function toLibraryResult(r: any) {
|
|
60
|
+
return {
|
|
61
|
+
id: r.id,
|
|
62
|
+
title: r.title,
|
|
63
|
+
description: r.description,
|
|
64
|
+
branch: r.branch,
|
|
65
|
+
lastUpdateDate: r.lastUpdateDate,
|
|
66
|
+
state: r.state,
|
|
67
|
+
totalTokens: r.totalTokens,
|
|
68
|
+
totalSnippets: r.totalSnippets,
|
|
69
|
+
stars: r.stars,
|
|
70
|
+
trustScore: r.trustScore,
|
|
71
|
+
benchmarkScore: r.benchmarkScore,
|
|
72
|
+
versions: r.versions,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toCodeSnippet(s: any) {
|
|
77
|
+
return {
|
|
78
|
+
codeTitle: s.codeTitle,
|
|
79
|
+
codeDescription: s.codeDescription,
|
|
80
|
+
codeLanguage: s.codeLanguage,
|
|
81
|
+
codeTokens: s.codeTokens,
|
|
82
|
+
codeId: s.codeId,
|
|
83
|
+
pageTitle: s.pageTitle,
|
|
84
|
+
sourceFile: s.sourceFile,
|
|
85
|
+
isDynamic: s.isDynamic,
|
|
86
|
+
codeList: s.codeList,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toInfoSnippet(s: any) {
|
|
91
|
+
return {
|
|
92
|
+
pageId: s.pageId,
|
|
93
|
+
breadcrumb: s.breadcrumb,
|
|
94
|
+
content: s.content,
|
|
95
|
+
contentTokens: s.contentTokens,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function withVersion(libraryId: string, version?: string): string {
|
|
100
|
+
const id = libraryId.trim();
|
|
101
|
+
const v = version?.trim();
|
|
102
|
+
if (!v) return id;
|
|
103
|
+
if (id.endsWith(`@${v}`) || id.endsWith(`/${v}`)) return id;
|
|
104
|
+
return `${id}@${v}`;
|
|
105
|
+
}
|
package/src/providers/exa.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { asSnippet, normalizeUrls, requestJson } from "../http.js";
|
|
2
2
|
import { DEFAULT_NUM_RESULTS } from "../limits.js";
|
|
3
3
|
import { urlsMatch } from "../urls.js";
|
|
4
|
-
import type { FetchInput, FetchProvider, SearchInput, SearchProvider, WebFetchResult, WebKitConfig } from "../types.js";
|
|
4
|
+
import type { ExaCodeInput, ExaCodeResult, FetchInput, FetchProvider, SearchInput, SearchProvider, WebFetchResult, WebKitConfig } from "../types.js";
|
|
5
5
|
import { requireKey } from "../config.js";
|
|
6
6
|
import { applyExaFetchFallbacks } from "./fallback.js";
|
|
7
7
|
|
|
@@ -61,4 +61,26 @@ export class ExaProvider implements SearchProvider, FetchProvider {
|
|
|
61
61
|
}) };
|
|
62
62
|
return applyExaFetchFallbacks(this.config, input, urls, primary, signal);
|
|
63
63
|
}
|
|
64
|
+
|
|
65
|
+
async searchCode(input: ExaCodeInput, signal?: AbortSignal): Promise<ExaCodeResult> {
|
|
66
|
+
const data = await requestJson<any>("https://api.exa.ai/context", {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: { "content-type": "application/json", "x-api-key": this.key },
|
|
69
|
+
body: JSON.stringify({
|
|
70
|
+
query: input.query,
|
|
71
|
+
tokensNum: input.tokensNum ?? "dynamic",
|
|
72
|
+
}),
|
|
73
|
+
signal,
|
|
74
|
+
timeoutMs: 45_000,
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
provider: "exa",
|
|
78
|
+
query: data.query ?? input.query,
|
|
79
|
+
response: data.response ?? "",
|
|
80
|
+
resultsCount: data.resultsCount,
|
|
81
|
+
searchTime: data.searchTime,
|
|
82
|
+
outputTokens: data.outputTokens,
|
|
83
|
+
requestId: data.requestId,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
64
86
|
}
|
package/src/providers/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { validateFetchProvider, validateSearchProvider } from "../config.js";
|
|
2
2
|
import type { FetchProvider, SearchProvider, WebKitConfig } from "../types.js";
|
|
3
3
|
import { BraveProvider } from "./brave.js";
|
|
4
|
+
import { Context7Provider } from "./context7.js";
|
|
4
5
|
import { ExaMcpProvider } from "./exa-mcp.js";
|
|
5
6
|
import { ExaProvider } from "./exa.js";
|
|
6
7
|
import { FirecrawlProvider } from "./firecrawl.js";
|
|
@@ -28,3 +29,11 @@ export function createFetchProvider(config: WebKitConfig): FetchProvider {
|
|
|
28
29
|
case "firecrawl": return new FirecrawlProvider(config);
|
|
29
30
|
}
|
|
30
31
|
}
|
|
32
|
+
|
|
33
|
+
export function createContext7Provider(config: WebKitConfig): Context7Provider {
|
|
34
|
+
return new Context7Provider(config);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createCodeSearchProvider(config: WebKitConfig): ExaProvider {
|
|
38
|
+
return new ExaProvider(config);
|
|
39
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -5,7 +5,7 @@ export type FetchFormat = "markdown" | "html" | "json";
|
|
|
5
5
|
export interface WebKitConfig {
|
|
6
6
|
provider_search: SearchProviderName;
|
|
7
7
|
provider_fetch: FetchProviderName;
|
|
8
|
-
apiKeys: Partial<Record<"exa" | "tinyfish" | "brave" | "firecrawl", string>>;
|
|
8
|
+
apiKeys: Partial<Record<"exa" | "tinyfish" | "brave" | "firecrawl" | "context7", string>>;
|
|
9
9
|
markdownNew: { method: "auto" | "ai" | "browser"; retainImages: boolean };
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -38,6 +38,77 @@ export interface WebFetchResult {
|
|
|
38
38
|
results: Array<{ url: string; content?: string; format?: FetchFormat; title?: string; metadata?: Record<string, unknown>; error?: string }>;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
export interface Context7LibrarySearchInput {
|
|
42
|
+
libraryName: string;
|
|
43
|
+
query?: string;
|
|
44
|
+
fast?: boolean;
|
|
45
|
+
limit?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface Context7LibrarySearchResult {
|
|
49
|
+
provider: "context7";
|
|
50
|
+
libraryName: string;
|
|
51
|
+
query: string;
|
|
52
|
+
searchFilterApplied?: boolean;
|
|
53
|
+
results: Array<{
|
|
54
|
+
id: string;
|
|
55
|
+
title?: string;
|
|
56
|
+
description?: string;
|
|
57
|
+
branch?: string;
|
|
58
|
+
lastUpdateDate?: string;
|
|
59
|
+
state?: string;
|
|
60
|
+
totalTokens?: number;
|
|
61
|
+
totalSnippets?: number;
|
|
62
|
+
stars?: number;
|
|
63
|
+
trustScore?: number;
|
|
64
|
+
benchmarkScore?: number;
|
|
65
|
+
versions?: string[];
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface Context7ContextInput {
|
|
70
|
+
libraryId: string;
|
|
71
|
+
query: string;
|
|
72
|
+
version?: string;
|
|
73
|
+
type?: "json";
|
|
74
|
+
fast?: boolean;
|
|
75
|
+
limit?: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface Context7DocsResult {
|
|
79
|
+
provider: "context7";
|
|
80
|
+
libraryId: string;
|
|
81
|
+
query: string;
|
|
82
|
+
codeSnippets: Array<{
|
|
83
|
+
codeTitle?: string;
|
|
84
|
+
codeDescription?: string;
|
|
85
|
+
codeLanguage?: string;
|
|
86
|
+
codeTokens?: number;
|
|
87
|
+
codeId?: string;
|
|
88
|
+
pageTitle?: string;
|
|
89
|
+
sourceFile?: string;
|
|
90
|
+
isDynamic?: boolean;
|
|
91
|
+
codeList?: Array<{ language: string; code: string }>;
|
|
92
|
+
}>;
|
|
93
|
+
infoSnippets: Array<{ pageId?: string; breadcrumb?: string; content: string; contentTokens?: number }>;
|
|
94
|
+
rules?: unknown;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ExaCodeInput {
|
|
98
|
+
query: string;
|
|
99
|
+
tokensNum?: "dynamic" | number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface ExaCodeResult {
|
|
103
|
+
provider: "exa";
|
|
104
|
+
query: string;
|
|
105
|
+
response: string;
|
|
106
|
+
resultsCount?: number;
|
|
107
|
+
searchTime?: number;
|
|
108
|
+
outputTokens?: number;
|
|
109
|
+
requestId?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
41
112
|
export interface SearchProvider {
|
|
42
113
|
search(input: SearchInput, signal?: AbortSignal): Promise<WebSearchResult>;
|
|
43
114
|
}
|