@zeldrisho/pi-web-search 0.14.4 → 0.14.6
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 +20 -1
- package/package.json +3 -3
- package/src/index.ts +14 -2
- package/src/provider.ts +14 -41
- package/src/render.ts +1 -1
- package/src/search.ts +6 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.14.6] - 2026-11-09
|
|
11
|
+
|
|
12
|
+
### Security
|
|
13
|
+
|
|
14
|
+
- Refuse provider redirects to prevent subscription tokens from being forwarded to another origin.
|
|
15
|
+
- Omit untrusted provider error bodies, which could echo credentials or search queries.
|
|
16
|
+
|
|
17
|
+
## [0.14.5] - 2026-11-01
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- Shorten search guidance, error, and output-limit notices.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- Avoid duplicate overflow notices in expanded search results.
|
|
26
|
+
|
|
10
27
|
## [0.14.4] - 2026-10-30
|
|
11
28
|
|
|
12
29
|
### Changed
|
|
@@ -196,7 +213,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
196
213
|
|
|
197
214
|
- Add bounded Brave web search ([cbc9491](https://github.com/zeldrisho/pi-packages/commit/cbc9491dce36555ab91b6bef203ec8b380596a89))
|
|
198
215
|
|
|
199
|
-
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.
|
|
216
|
+
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.6...HEAD
|
|
217
|
+
[0.14.6]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.5...pi-web-search-v0.14.6
|
|
218
|
+
[0.14.5]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.4...pi-web-search-v0.14.5
|
|
200
219
|
[0.14.4]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.3...pi-web-search-v0.14.4
|
|
201
220
|
[0.14.3]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.2...pi-web-search-v0.14.3
|
|
202
221
|
[0.14.2]: https://github.com/zeldrisho/pi-packages/compare/pi-web-search-v0.14.1...pi-web-search-v0.14.2
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeldrisho/pi-web-search",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.6",
|
|
4
4
|
"description": "Pi extension for bounded Brave web search with generated-answer fallback",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"brave-search",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@earendil-works/pi-tui": "^1.4.0",
|
|
35
35
|
"typebox": "^1.4.2",
|
|
36
36
|
"typescript": "^7.0.2",
|
|
37
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@1.
|
|
38
|
-
"vite-plus": "1.
|
|
37
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
38
|
+
"vite-plus": "1.4.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@earendil-works/pi-coding-agent": "*",
|
package/src/index.ts
CHANGED
|
@@ -72,14 +72,26 @@ export default function (pi: ExtensionAPI) {
|
|
|
72
72
|
|
|
73
73
|
if (isPartial) return new Text(theme.fg("warning", content?.text || "Searching…"), 0, 0);
|
|
74
74
|
|
|
75
|
+
const path = result.details?.fullOutputPath;
|
|
76
|
+
const lastLine = content?.text.trimEnd().split("\n").at(-1);
|
|
77
|
+
|
|
78
|
+
const overflowNotice =
|
|
79
|
+
path &&
|
|
80
|
+
lastLine?.endsWith(`Full output: ${path}]`) &&
|
|
81
|
+
(lastLine.startsWith("[Output truncated:") ||
|
|
82
|
+
lastLine.startsWith("[Structured payload omitted due to limits."))
|
|
83
|
+
? lastLine
|
|
84
|
+
: undefined;
|
|
85
|
+
|
|
75
86
|
return content?.type === "text"
|
|
76
87
|
? renderCollapsibleOutput(
|
|
77
88
|
content.text,
|
|
78
89
|
expanded,
|
|
79
90
|
theme,
|
|
80
91
|
context?.isError,
|
|
81
|
-
|
|
82
|
-
?
|
|
92
|
+
path
|
|
93
|
+
? (overflowNotice ??
|
|
94
|
+
`[${result.details?.truncation?.truncated ? "Output truncated. " : "Structured payload omitted. "}Full output: ${path}]`)
|
|
83
95
|
: undefined,
|
|
84
96
|
)
|
|
85
97
|
: new Text(theme.fg("dim", "No results"), 0, 0);
|
package/src/provider.ts
CHANGED
|
@@ -2,8 +2,6 @@ const REQUEST_TIMEOUT_MS = 20_000;
|
|
|
2
2
|
|
|
3
3
|
const SEARCH_MAX_RESPONSE_BYTES = 2_000_000;
|
|
4
4
|
|
|
5
|
-
const SEARCH_ERROR_EXCERPT_BYTES = 8_192;
|
|
6
|
-
|
|
7
5
|
/**
|
|
8
6
|
* Normalizes text by removing HTML tags, collapsing whitespace, and truncating to a maximum length.
|
|
9
7
|
*
|
|
@@ -23,23 +21,18 @@ export function normalizeText(value: string, maxLength: number): string {
|
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
/**
|
|
26
|
-
* Reads response body bytes with size limits
|
|
24
|
+
* Reads response body bytes with size limits.
|
|
27
25
|
*
|
|
28
26
|
* @param response - HTTP response to read
|
|
29
27
|
* @param maxBytes - Maximum bytes to read
|
|
30
|
-
* @param truncate - Whether to truncate at limit or throw error
|
|
31
28
|
* @returns Promise resolving to the response bytes
|
|
32
|
-
* @throws {Error} When response exceeds maxBytes
|
|
29
|
+
* @throws {Error} When response exceeds maxBytes
|
|
33
30
|
*/
|
|
34
|
-
async function readResponseBytes(
|
|
35
|
-
response: Response,
|
|
36
|
-
maxBytes: number,
|
|
37
|
-
truncate: boolean,
|
|
38
|
-
): Promise<Uint8Array> {
|
|
31
|
+
async function readResponseBytes(response: Response, maxBytes: number): Promise<Uint8Array> {
|
|
39
32
|
const contentLength = response.headers.get("content-length");
|
|
40
33
|
const declared = contentLength === null ? Number.NaN : Number(contentLength);
|
|
41
34
|
|
|
42
|
-
if (
|
|
35
|
+
if (Number.isFinite(declared) && declared > maxBytes && declared > 0) {
|
|
43
36
|
await response.body?.cancel().catch(() => undefined);
|
|
44
37
|
throw new Error("Search provider response is too large.");
|
|
45
38
|
}
|
|
@@ -58,14 +51,7 @@ async function readResponseBytes(
|
|
|
58
51
|
const remaining = maxBytes - total;
|
|
59
52
|
|
|
60
53
|
if (value.byteLength > remaining) {
|
|
61
|
-
if (truncate && remaining > 0) chunks.push(value.subarray(0, remaining));
|
|
62
54
|
await reader.cancel().catch(() => undefined);
|
|
63
|
-
|
|
64
|
-
if (truncate) {
|
|
65
|
-
total = maxBytes;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
55
|
throw new Error("Search provider response is too large.");
|
|
70
56
|
}
|
|
71
57
|
|
|
@@ -92,16 +78,11 @@ async function readResponseBytes(
|
|
|
92
78
|
*
|
|
93
79
|
* @param response - HTTP response to read
|
|
94
80
|
* @param maxBytes - Maximum bytes to read
|
|
95
|
-
* @param truncate - Whether to truncate at limit or throw error
|
|
96
81
|
* @returns Promise resolving to the decoded text
|
|
97
82
|
* @throws {Error} When response exceeds maxBytes and truncate is false
|
|
98
83
|
*/
|
|
99
|
-
async function readResponseText(
|
|
100
|
-
response
|
|
101
|
-
maxBytes: number,
|
|
102
|
-
truncate = false,
|
|
103
|
-
): Promise<string> {
|
|
104
|
-
return new TextDecoder().decode(await readResponseBytes(response, maxBytes, truncate));
|
|
84
|
+
async function readResponseText(response: Response, maxBytes: number): Promise<string> {
|
|
85
|
+
return new TextDecoder().decode(await readResponseBytes(response, maxBytes));
|
|
105
86
|
}
|
|
106
87
|
|
|
107
88
|
/**
|
|
@@ -132,29 +113,21 @@ export async function requestText(
|
|
|
132
113
|
if (signal?.aborted) controller.abort();
|
|
133
114
|
|
|
134
115
|
try {
|
|
135
|
-
|
|
116
|
+
// Custom subscription-token headers are not stripped by fetch on cross-origin
|
|
117
|
+
// redirects. Never follow a provider-controlled redirect with credentials.
|
|
118
|
+
const response = await fetch(url, { ...init, redirect: "error", signal: controller.signal });
|
|
136
119
|
|
|
137
120
|
if (!response.ok) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
body = normalizeText(
|
|
142
|
-
await readResponseText(response, SEARCH_ERROR_EXCERPT_BYTES, true),
|
|
143
|
-
500,
|
|
144
|
-
);
|
|
145
|
-
} catch {
|
|
146
|
-
// Preserve the useful HTTP status when an untrusted error body cannot be read.
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
throw new Error(`Search provider returned HTTP ${response.status}${body ? `: ${body}` : ""}`);
|
|
121
|
+
// Error bodies are untrusted and can echo credentials or private queries.
|
|
122
|
+
void response.body?.cancel().catch(() => undefined);
|
|
123
|
+
throw new Error(`Provider returned HTTP ${response.status}`);
|
|
150
124
|
}
|
|
151
125
|
|
|
152
126
|
return await readResponseText(response, SEARCH_MAX_RESPONSE_BYTES);
|
|
153
127
|
} catch (error) {
|
|
154
|
-
if (timedOut)
|
|
155
|
-
throw new Error(`Web search timed out after ${REQUEST_TIMEOUT_MS / 1000} seconds.`);
|
|
128
|
+
if (timedOut) throw new Error(`Timed out after ${REQUEST_TIMEOUT_MS / 1000} seconds.`);
|
|
156
129
|
|
|
157
|
-
if (signal?.aborted) throw new Error("
|
|
130
|
+
if (signal?.aborted) throw new Error("Cancelled.");
|
|
158
131
|
throw error;
|
|
159
132
|
} finally {
|
|
160
133
|
clearTimeout(timeout);
|
package/src/render.ts
CHANGED
package/src/search.ts
CHANGED
|
@@ -159,16 +159,13 @@ function escapeMarkdownLinkText(value: string): string {
|
|
|
159
159
|
function formatResults(query: string, results: SearchResult[], requestedCount: number): string {
|
|
160
160
|
const returnedCount = results.length;
|
|
161
161
|
|
|
162
|
-
const retryAdvice =
|
|
163
|
-
"If evidence is insufficient, fetch promising sources or broaden the query (fewer quotes; relax nonessential site: restrictions).";
|
|
164
|
-
|
|
165
162
|
const guidance =
|
|
166
163
|
returnedCount < requestedCount
|
|
167
|
-
? `Returned ${returnedCount}
|
|
164
|
+
? `Returned ${returnedCount}/${requestedCount} results. If needed, read relevant pages or broaden the query; keep required site restrictions.`
|
|
168
165
|
: "";
|
|
169
166
|
|
|
170
167
|
if (returnedCount === 0) {
|
|
171
|
-
return `No web results
|
|
168
|
+
return `No web results for ${JSON.stringify(query)}. Try broadening the query; keep required site restrictions. No results do not prove absence.`;
|
|
172
169
|
}
|
|
173
170
|
|
|
174
171
|
const entries = results.map((result, index) => {
|
|
@@ -466,7 +463,7 @@ export class SearchRuntime {
|
|
|
466
463
|
return entry;
|
|
467
464
|
},
|
|
468
465
|
signal,
|
|
469
|
-
"
|
|
466
|
+
"Cancelled.",
|
|
470
467
|
));
|
|
471
468
|
|
|
472
469
|
if (payload.results.length === 0 && !payload.answer) {
|
|
@@ -508,7 +505,7 @@ export class SearchRuntime {
|
|
|
508
505
|
return entry;
|
|
509
506
|
},
|
|
510
507
|
signal,
|
|
511
|
-
"
|
|
508
|
+
"Cancelled.",
|
|
512
509
|
));
|
|
513
510
|
cached = cachedAnswer !== undefined;
|
|
514
511
|
apiKeySource = answerCredentials.source;
|
|
@@ -598,8 +595,8 @@ export class SearchRuntime {
|
|
|
598
595
|
}
|
|
599
596
|
|
|
600
597
|
text += truncation.truncated
|
|
601
|
-
? `\n\n[Output truncated
|
|
602
|
-
: `\n\n[Structured payload omitted to
|
|
598
|
+
? `\n\n[Output truncated: ${truncation.outputLines}/${truncation.totalLines} lines (${formatSize(truncation.outputBytes)}/${formatSize(truncation.totalBytes)}). Full output: ${fullOutputPath}]`
|
|
599
|
+
: `\n\n[Structured payload omitted due to limits. Full output: ${fullOutputPath}]`;
|
|
603
600
|
}
|
|
604
601
|
|
|
605
602
|
structured.fullOutputPath = fullOutputPath;
|