pi-querit 1.0.3 → 1.1.1
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 -21
- package/README.md +6 -34
- package/package.json +1 -1
- package/src/client.ts +385 -385
- package/src/config.ts +200 -254
- package/src/format.ts +101 -105
- package/src/index.ts +378 -483
- package/src/output.ts +79 -3
- package/src/sanitize.ts +97 -97
- package/src/setup.ts +330 -514
- package/src/summary.ts +0 -219
package/src/format.ts
CHANGED
|
@@ -1,105 +1,101 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
QueritContentsResponse,
|
|
3
|
-
QueritSearchResponse,
|
|
4
|
-
} from "./client.js";
|
|
5
|
-
import { sanitizeTerminalText } from "./sanitize.js";
|
|
6
|
-
|
|
7
|
-
const encoder = new TextEncoder();
|
|
8
|
-
const decoder = new TextDecoder();
|
|
9
|
-
|
|
10
|
-
export function formatSearchResponse(response: QueritSearchResponse): string {
|
|
11
|
-
const lines: string[] = [
|
|
12
|
-
|
|
13
|
-
"",
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (result.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
)
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
lines.push("",
|
|
63
|
-
lines.push(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function singleLine(value: string, maxBytes: number): string {
|
|
104
|
-
return truncateUtf8(sanitizeTerminalText(value).replace(/\s+/g, " ").trim(), maxBytes);
|
|
105
|
-
}
|
|
1
|
+
import type {
|
|
2
|
+
QueritContentsResponse,
|
|
3
|
+
QueritSearchResponse,
|
|
4
|
+
} from "./client.js";
|
|
5
|
+
import { sanitizeTerminalText } from "./sanitize.js";
|
|
6
|
+
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
const decoder = new TextDecoder();
|
|
9
|
+
|
|
10
|
+
export function formatSearchResponse(response: QueritSearchResponse): string {
|
|
11
|
+
const lines: string[] = [
|
|
12
|
+
`# Querit search results for: ${singleLine(response.query, 1_000)}`,
|
|
13
|
+
`Results: ${response.results.length}${response.took ? ` | Server time: ${singleLine(response.took, 128)}` : ""}${response.searchId ? ` | Search ID: ${singleLine(response.searchId, 128)}` : ""}`,
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
if (response.results.length === 0) {
|
|
17
|
+
lines.push("", "No results found.");
|
|
18
|
+
return lines.join("\n");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (const [index, result] of response.results.entries()) {
|
|
22
|
+
lines.push("", `## ${index + 1}. ${singleLine(result.title || result.url, 512)}`);
|
|
23
|
+
lines.push(`URL: ${truncateUtf8(sanitizeTerminalText(result.url), 4_096)}`);
|
|
24
|
+
|
|
25
|
+
const sourceParts = [result.siteName, result.pageAge]
|
|
26
|
+
.filter((value): value is string => Boolean(value))
|
|
27
|
+
.map((value) => singleLine(value, 512));
|
|
28
|
+
if (sourceParts.length > 0) lines.push(`Source: ${sourceParts.join(" | ")}`);
|
|
29
|
+
if (result.snippet) lines.push(`Snippet: ${singleLine(result.snippet, 2_048)}`);
|
|
30
|
+
|
|
31
|
+
if (result.sentences.length > 0) {
|
|
32
|
+
lines.push("Content excerpts:");
|
|
33
|
+
for (const sentence of result.sentences) {
|
|
34
|
+
lines.push(`- ${truncateUtf8(sanitizeTerminalText(sentence), 4_096)}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return lines.join("\n");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function formatContentsResponse(
|
|
43
|
+
response: QueritContentsResponse,
|
|
44
|
+
requestedUrls: string[],
|
|
45
|
+
format: "text" | "markdown" | "html",
|
|
46
|
+
): string {
|
|
47
|
+
const successfulStatuses = response.statuses.filter((status) => status.status === "success").length;
|
|
48
|
+
const failedStatuses = response.statuses.filter((status) => status.status === "failed").length;
|
|
49
|
+
const lines: string[] = [
|
|
50
|
+
"# Querit fetched contents",
|
|
51
|
+
`Requested: ${requestedUrls.length} | Returned: ${response.results.length} | Successful: ${successfulStatuses} | Failed: ${failedStatuses}${response.searchTime === undefined ? "" : ` | Server time: ${response.searchTime}s`}${response.searchId ? ` | Search ID: ${singleLine(response.searchId, 128)}` : ""}`,
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
const returnedKeys = new Set<string>();
|
|
55
|
+
for (const [index, result] of response.results.entries()) {
|
|
56
|
+
returnedKeys.add(urlMatchKey(result.url));
|
|
57
|
+
const title = result.metadata?.title || result.url;
|
|
58
|
+
lines.push("", `## ${index + 1}. ${singleLine(title, 512)}`);
|
|
59
|
+
lines.push(`URL: ${truncateUtf8(sanitizeTerminalText(result.url), 4_096)}`);
|
|
60
|
+
if (result.metadata?.siteName) lines.push(`Site: ${singleLine(result.metadata.siteName, 512)}`);
|
|
61
|
+
if (result.metadata?.publishTime) lines.push(`Published: ${singleLine(result.metadata.publishTime, 128)}`);
|
|
62
|
+
lines.push(`Format: ${format}`, "", "--- BEGIN PAGE CONTENT ---");
|
|
63
|
+
lines.push(result.content ? sanitizeTerminalText(result.content) : "[No content returned]");
|
|
64
|
+
lines.push("--- END PAGE CONTENT ---");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const unavailable = requestedUrls.filter((url) => !returnedKeys.has(urlMatchKey(url)));
|
|
68
|
+
if (unavailable.length > 0) {
|
|
69
|
+
lines.push("", "## URLs without returned content");
|
|
70
|
+
for (const url of unavailable) lines.push(`- ${truncateUtf8(sanitizeTerminalText(url), 4_096)}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return lines.join("\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Relaxed URL key for matching requested vs returned URLs.
|
|
78
|
+
* Ignores protocol and trailing slashes so that redirects
|
|
79
|
+
* (e.g. http://a.com → https://a.com/) are not reported as unavailable.
|
|
80
|
+
*/
|
|
81
|
+
function urlMatchKey(raw: string): string {
|
|
82
|
+
try {
|
|
83
|
+
const parsed = new URL(raw);
|
|
84
|
+
return `${parsed.hostname}${parsed.pathname.replace(/\/+$/u, "")}`;
|
|
85
|
+
} catch {
|
|
86
|
+
return raw;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function truncateUtf8(value: string, maxBytes: number): string {
|
|
91
|
+
const bytes = encoder.encode(value);
|
|
92
|
+
if (bytes.byteLength <= maxBytes) return value;
|
|
93
|
+
if (maxBytes <= 3) return ".".repeat(Math.max(0, maxBytes));
|
|
94
|
+
|
|
95
|
+
const prefix = decoder.decode(bytes.slice(0, maxBytes - 3)).replace(/\uFFFD+$/u, "");
|
|
96
|
+
return `${prefix}...`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function singleLine(value: string, maxBytes: number): string {
|
|
100
|
+
return truncateUtf8(sanitizeTerminalText(value).replace(/\s+/g, " ").trim(), maxBytes);
|
|
101
|
+
}
|