@mammothb/pi-websearch 0.1.0 → 0.3.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/bin/searxng +48 -0
- package/index.ts +36 -2
- package/package.json +7 -2
- package/searxng/.env +16 -0
- package/searxng/core-config/settings.yml +2458 -0
- package/searxng/docker-compose.yml +24 -0
- package/src/config.ts +128 -0
- package/src/lib/parsers.ts +42 -0
- package/src/lib/providers/exa-mcp.ts +84 -0
- package/src/lib/providers/index.ts +29 -0
- package/src/lib/providers/searxng.ts +100 -0
- package/src/lib/searxng-manager.ts +161 -0
- package/src/lib/types.ts +60 -0
- package/src/websearch.ts +136 -30
package/src/websearch.ts
CHANGED
|
@@ -1,22 +1,88 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
1
|
+
import type { ImageContent, TextContent } from "@earendil-works/pi-ai";
|
|
2
|
+
import {
|
|
3
|
+
keyText,
|
|
4
|
+
type Theme,
|
|
5
|
+
type ToolDefinition,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Container, Spacer, Text } from "@earendil-works/pi-tui";
|
|
8
|
+
import type { WebsearchConfig } from "./config";
|
|
9
|
+
import { createProvider } from "./lib/providers";
|
|
10
|
+
import type { SearchArgs } from "./lib/types";
|
|
11
|
+
import { WebsearchParameters } from "./lib/types";
|
|
12
|
+
|
|
13
|
+
const COLLAPSED_PREVIEW_LINES = 7;
|
|
3
14
|
|
|
4
15
|
interface WebsearchDetails {
|
|
5
16
|
query: string;
|
|
6
|
-
numResults: number;
|
|
7
17
|
}
|
|
8
18
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
function isTextContent(c: TextContent | ImageContent): c is TextContent {
|
|
20
|
+
return c.type === "text";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function renderExpandableResult(
|
|
24
|
+
details: WebsearchDetails,
|
|
25
|
+
textContent: string,
|
|
26
|
+
expanded: boolean,
|
|
27
|
+
theme: Theme,
|
|
28
|
+
): Container {
|
|
29
|
+
const container = new Container();
|
|
30
|
+
|
|
31
|
+
const title = details.query;
|
|
32
|
+
container.addChild(
|
|
33
|
+
new Text(
|
|
34
|
+
theme.fg("syntaxKeyword", "query: ") + theme.fg("syntaxString", title),
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
if (!textContent) {
|
|
39
|
+
return container;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
container.addChild(new Spacer(1));
|
|
43
|
+
|
|
44
|
+
if (expanded) {
|
|
45
|
+
container.addChild(new Text(textContent));
|
|
46
|
+
} else {
|
|
47
|
+
const lines = textContent
|
|
48
|
+
.split("\n")
|
|
49
|
+
.filter(
|
|
50
|
+
(line, index, arr) =>
|
|
51
|
+
line.length > 0 || index === 0 || index < arr.length - 1,
|
|
52
|
+
);
|
|
53
|
+
const previewLines = lines.slice(0, COLLAPSED_PREVIEW_LINES);
|
|
54
|
+
const remaining = Math.max(0, lines.length - previewLines.length);
|
|
55
|
+
|
|
56
|
+
const preview = previewLines.join("\n");
|
|
57
|
+
container.addChild(new Text(preview));
|
|
58
|
+
|
|
59
|
+
if (remaining > 0) {
|
|
60
|
+
container.addChild(new Spacer(1));
|
|
61
|
+
const expandKey = keyText("app.tools.expand") || "Ctrl+O";
|
|
62
|
+
container.addChild(
|
|
63
|
+
new Text(
|
|
64
|
+
theme.fg("muted", `... (${remaining} more lines, `) +
|
|
65
|
+
theme.fg("dim", expandKey) +
|
|
66
|
+
theme.fg("muted", " to expand)"),
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return container;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function createWebsearchTool(
|
|
75
|
+
config: WebsearchConfig,
|
|
76
|
+
): ToolDefinition<typeof WebsearchParameters, WebsearchDetails> {
|
|
77
|
+
const year = new Date().getFullYear();
|
|
78
|
+
const { defaults } = config;
|
|
79
|
+
const provider = createProvider(config);
|
|
80
|
+
|
|
81
|
+
const usageNotes =
|
|
82
|
+
config.provider === "searxng"
|
|
83
|
+
? "\n - Results are fetched from a self-hosted SearXNG metasearch instance"
|
|
84
|
+
: "\n - Supports live crawling modes when available: 'fallback' (backup if cached unavailable) or 'preferred' (prioritize live crawling)\n - Search types when available: 'auto' (balanced), 'fast' (quick results), 'deep' (comprehensive search)";
|
|
85
|
+
|
|
20
86
|
return {
|
|
21
87
|
name: "websearch",
|
|
22
88
|
label: "Web Search",
|
|
@@ -26,25 +92,65 @@ export function createWebsearchTool(): ToolDefinition<
|
|
|
26
92
|
- Use this tool for accessing information beyond knowledge cutoff.
|
|
27
93
|
- Searches are performed automatically within a single API call.
|
|
28
94
|
|
|
29
|
-
Usage notes
|
|
30
|
-
- Supports live crawling modes when available: 'fallback' (backup if cached unavailable) or 'preferred' (prioritize live crawling)
|
|
31
|
-
- Search types when available: 'auto' (balanced), 'fast' (quick results), 'deep' (comprehensive search)
|
|
95
|
+
Usage notes:${usageNotes}
|
|
32
96
|
- Configurable context length for optimal LLM integration`,
|
|
33
97
|
promptSnippet: "Search the web",
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
98
|
+
promptGuidelines: [
|
|
99
|
+
"Use websearch to find current information, documentation, or answers that require up-to-date web data. Always cite sources from search results.",
|
|
100
|
+
`The current year is ${year}. You MUST use this year when searching for recent information or current events.\n- Example: If the current year is ${year} and the user asks for "latest AI news", search for "AI news ${year}", NOT "AI news ${year - 1}"`,
|
|
101
|
+
],
|
|
102
|
+
parameters: WebsearchParameters,
|
|
103
|
+
async execute(_toolCallId, params, signal, _onUpdate, _ctx) {
|
|
104
|
+
const args: SearchArgs = {
|
|
105
|
+
query: params.query,
|
|
106
|
+
type: params.type ?? defaults.type,
|
|
107
|
+
numResults: params.numResults ?? defaults.numResults,
|
|
108
|
+
livecrawl: params.livecrawl ?? defaults.livecrawl,
|
|
109
|
+
contextMaxCharacters:
|
|
110
|
+
params.contextMaxCharacters ?? defaults.contextMaxCharacters,
|
|
47
111
|
};
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const result = await provider.search(args, signal);
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
content: [
|
|
118
|
+
{
|
|
119
|
+
type: "text",
|
|
120
|
+
text:
|
|
121
|
+
result ??
|
|
122
|
+
"No search results found. Please try a different query.",
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
details: { query: params.query },
|
|
126
|
+
};
|
|
127
|
+
} catch (err) {
|
|
128
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
129
|
+
throw new Error(`Web search failed: ${message}`);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
renderCall(args, theme, _context) {
|
|
133
|
+
return new Text(
|
|
134
|
+
theme.fg("toolTitle", theme.bold("websearch ")) +
|
|
135
|
+
theme.fg("muted", `"${args.query}"`),
|
|
136
|
+
);
|
|
137
|
+
},
|
|
138
|
+
renderResult(result, options, theme, _context) {
|
|
139
|
+
if (options.isPartial) {
|
|
140
|
+
return new Text(theme.fg("warning", "Searching..."));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const textContent = result.content
|
|
144
|
+
.filter(isTextContent)
|
|
145
|
+
.map((c) => c.text)
|
|
146
|
+
.join("\n");
|
|
147
|
+
|
|
148
|
+
return renderExpandableResult(
|
|
149
|
+
result.details,
|
|
150
|
+
textContent,
|
|
151
|
+
options.expanded,
|
|
152
|
+
theme,
|
|
153
|
+
);
|
|
48
154
|
},
|
|
49
155
|
};
|
|
50
156
|
}
|