@abhishek944/pi-web-access 0.0.0-stage → 0.1.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.
Files changed (201) hide show
  1. package/ASTACK.md +23 -0
  2. package/CHANGELOG.md +1289 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1215 -2
  5. package/SECURITY.md +5 -0
  6. package/abortable.ts +17 -0
  7. package/activity.ts +101 -0
  8. package/anysearch.ts +188 -0
  9. package/auth-fetch.ts +152 -0
  10. package/baizhi.ts +124 -0
  11. package/banner.png +0 -0
  12. package/bocha.ts +203 -0
  13. package/brave-rate-limit.ts +176 -0
  14. package/brave.ts +247 -0
  15. package/brightdata-unlocker.ts +272 -0
  16. package/ceramic.ts +220 -0
  17. package/chrome-cookies.ts +752 -0
  18. package/cloudflare-search.ts +237 -0
  19. package/content-find.ts +255 -0
  20. package/crawl4ai.ts +196 -0
  21. package/credential-source.ts +242 -0
  22. package/curator-page.ts +3607 -0
  23. package/curator-run.ts +45 -0
  24. package/curator-server.ts +761 -0
  25. package/custom-search.ts +226 -0
  26. package/data-uri-sanitize.ts +406 -0
  27. package/datalab-pdf-extract.ts +568 -0
  28. package/declared-web-links.ts +173 -0
  29. package/deepseek.ts +377 -0
  30. package/degoog.ts +323 -0
  31. package/dist/index.js +28286 -0
  32. package/dist/mcp-cli.js +29186 -0
  33. package/dist/pi-mcp.js +20855 -0
  34. package/dist/types/abortable.d.ts +2 -0
  35. package/dist/types/activity.d.ts +33 -0
  36. package/dist/types/anysearch.d.ts +7 -0
  37. package/dist/types/auth-fetch.d.ts +14 -0
  38. package/dist/types/baizhi.d.ts +3 -0
  39. package/dist/types/bocha.d.ts +3 -0
  40. package/dist/types/brave-rate-limit.d.ts +35 -0
  41. package/dist/types/brave.d.ts +3 -0
  42. package/dist/types/brightdata-unlocker.d.ts +11 -0
  43. package/dist/types/ceramic.d.ts +3 -0
  44. package/dist/types/chrome-cookies.d.ts +44 -0
  45. package/dist/types/cloudflare-search.d.ts +4 -0
  46. package/dist/types/content-find.d.ts +10 -0
  47. package/dist/types/crawl4ai.d.ts +8 -0
  48. package/dist/types/credential-source.d.ts +29 -0
  49. package/dist/types/curator-page.d.ts +5 -0
  50. package/dist/types/curator-run.d.ts +10 -0
  51. package/dist/types/curator-server.d.ts +72 -0
  52. package/dist/types/custom-search.d.ts +22 -0
  53. package/dist/types/data-uri-sanitize.d.ts +20 -0
  54. package/dist/types/datalab-pdf-extract.d.ts +59 -0
  55. package/dist/types/declared-web-links.d.ts +7 -0
  56. package/dist/types/deepseek.d.ts +13 -0
  57. package/dist/types/degoog.d.ts +3 -0
  58. package/dist/types/domain-filter-normalization.d.ts +9 -0
  59. package/dist/types/duckduckgo-rate-limit.d.ts +52 -0
  60. package/dist/types/duckduckgo.d.ts +6 -0
  61. package/dist/types/endpoint-pdf-extract.d.ts +22 -0
  62. package/dist/types/exa.d.ts +17 -0
  63. package/dist/types/extract.d.ts +70 -0
  64. package/dist/types/feature-config.d.ts +2 -0
  65. package/dist/types/fetch-params.d.ts +39 -0
  66. package/dist/types/field-extract.d.ts +37 -0
  67. package/dist/types/firecrawl.d.ts +18 -0
  68. package/dist/types/gemini-adc.d.ts +9 -0
  69. package/dist/types/gemini-api.d.ts +37 -0
  70. package/dist/types/gemini-pdf-extract.d.ts +8 -0
  71. package/dist/types/gemini-search.d.ts +76 -0
  72. package/dist/types/gemini-url-context.d.ts +3 -0
  73. package/dist/types/gemini-web-config.d.ts +25 -0
  74. package/dist/types/gemini-web.d.ts +31 -0
  75. package/dist/types/github-api.d.ts +6 -0
  76. package/dist/types/github-extract.d.ts +19 -0
  77. package/dist/types/github-issue-pr.d.ts +28 -0
  78. package/dist/types/index.d.ts +53 -0
  79. package/dist/types/jina-search.d.ts +7 -0
  80. package/dist/types/kagi.d.ts +14 -0
  81. package/dist/types/keenable.d.ts +3 -0
  82. package/dist/types/kimi-search.d.ts +4 -0
  83. package/dist/types/linkup.d.ts +5 -0
  84. package/dist/types/mcp-cli.d.ts +2 -0
  85. package/dist/types/mcp-server.d.ts +10 -0
  86. package/dist/types/mcp-tool-definitions.d.ts +16 -0
  87. package/dist/types/mistral-search.d.ts +3 -0
  88. package/dist/types/ninerouter.d.ts +3 -0
  89. package/dist/types/offtopic-judge.d.ts +26 -0
  90. package/dist/types/ollama.d.ts +14 -0
  91. package/dist/types/openai-search.d.ts +23 -0
  92. package/dist/types/page-query.d.ts +22 -0
  93. package/dist/types/parallel-mcp.d.ts +10 -0
  94. package/dist/types/parallel.d.ts +13 -0
  95. package/dist/types/pdf-extract.d.ts +44 -0
  96. package/dist/types/perplexity.d.ts +26 -0
  97. package/dist/types/pi-mcp.d.ts +56 -0
  98. package/dist/types/pi-session-results.d.ts +8 -0
  99. package/dist/types/querit.d.ts +9 -0
  100. package/dist/types/query-rewrite.d.ts +2 -0
  101. package/dist/types/render-search-error.d.ts +56 -0
  102. package/dist/types/retrieval-only.d.ts +78 -0
  103. package/dist/types/rsc-extract.d.ts +11 -0
  104. package/dist/types/search-answer-formatting.d.ts +7 -0
  105. package/dist/types/search-result-count-normalization.d.ts +1 -0
  106. package/dist/types/searchinfinity.d.ts +7 -0
  107. package/dist/types/searxng.d.ts +3 -0
  108. package/dist/types/source-check.d.ts +102 -0
  109. package/dist/types/ssrf-protection.d.ts +66 -0
  110. package/dist/types/storage.d.ts +69 -0
  111. package/dist/types/summary-model-scope.d.ts +33 -0
  112. package/dist/types/summary-review.d.ts +30 -0
  113. package/dist/types/tabstack.d.ts +5 -0
  114. package/dist/types/tavily.d.ts +7 -0
  115. package/dist/types/tinyfish.d.ts +10 -0
  116. package/dist/types/tool-activation.d.ts +7 -0
  117. package/dist/types/tool-arguments.d.ts +1 -0
  118. package/dist/types/upgrade-notice.d.ts +6 -0
  119. package/dist/types/utils.d.ts +55 -0
  120. package/dist/types/valyu.d.ts +5 -0
  121. package/dist/types/video-extract.d.ts +15 -0
  122. package/dist/types/web-tool-contract.d.ts +98 -0
  123. package/dist/types/web-tool-core.d.ts +227 -0
  124. package/dist/types/xai-search.d.ts +12 -0
  125. package/dist/types/you.d.ts +3 -0
  126. package/dist/types/youtube-extract.d.ts +29 -0
  127. package/dist/types/zai.d.ts +3 -0
  128. package/docs/pi-mcp.md +41 -0
  129. package/domain-filter-normalization.ts +44 -0
  130. package/duckduckgo-rate-limit.ts +205 -0
  131. package/duckduckgo.ts +123 -0
  132. package/endpoint-pdf-extract.ts +225 -0
  133. package/exa.ts +620 -0
  134. package/extract.ts +1721 -0
  135. package/feature-config.ts +29 -0
  136. package/fetch-params.ts +147 -0
  137. package/field-extract.ts +347 -0
  138. package/firecrawl.ts +476 -0
  139. package/gemini-adc.ts +345 -0
  140. package/gemini-api.ts +368 -0
  141. package/gemini-pdf-extract.ts +108 -0
  142. package/gemini-search.ts +1162 -0
  143. package/gemini-url-context.ts +128 -0
  144. package/gemini-web-config.ts +131 -0
  145. package/gemini-web.ts +527 -0
  146. package/github-api.ts +197 -0
  147. package/github-extract.ts +1064 -0
  148. package/github-issue-pr.ts +717 -0
  149. package/index.ts +2819 -0
  150. package/jina-search.ts +275 -0
  151. package/kagi.ts +257 -0
  152. package/keenable.ts +222 -0
  153. package/kimi-search.ts +209 -0
  154. package/linkup.ts +170 -0
  155. package/mcp-cli.ts +40 -0
  156. package/mcp-server.ts +84 -0
  157. package/mcp-tool-definitions.ts +143 -0
  158. package/mistral-search.ts +268 -0
  159. package/ninerouter.ts +92 -0
  160. package/offtopic-judge.ts +72 -0
  161. package/ollama.ts +237 -0
  162. package/openai-search.ts +855 -0
  163. package/package.json +108 -4
  164. package/page-query.ts +185 -0
  165. package/parallel-mcp.ts +255 -0
  166. package/parallel.ts +373 -0
  167. package/pdf-extract.ts +447 -0
  168. package/perplexity.ts +245 -0
  169. package/pi-mcp.ts +139 -0
  170. package/pi-session-results.ts +40 -0
  171. package/pi-web-fetch-demo.mp4 +0 -0
  172. package/promise-try.d.ts +7 -0
  173. package/querit.ts +394 -0
  174. package/query-rewrite.ts +84 -0
  175. package/render-search-error.ts +170 -0
  176. package/retrieval-only.ts +102 -0
  177. package/rsc-extract.ts +338 -0
  178. package/search-answer-formatting.ts +11 -0
  179. package/search-result-count-normalization.ts +4 -0
  180. package/searchinfinity.ts +246 -0
  181. package/searxng.ts +236 -0
  182. package/source-check.ts +269 -0
  183. package/ssrf-protection.ts +602 -0
  184. package/storage.ts +547 -0
  185. package/summary-model-scope.ts +72 -0
  186. package/summary-review.ts +476 -0
  187. package/tabstack.ts +289 -0
  188. package/tavily.ts +257 -0
  189. package/tinyfish.ts +373 -0
  190. package/tool-activation.ts +169 -0
  191. package/tool-arguments.ts +23 -0
  192. package/upgrade-notice.ts +47 -0
  193. package/utils.ts +693 -0
  194. package/valyu.ts +177 -0
  195. package/video-extract.ts +398 -0
  196. package/web-tool-contract.ts +93 -0
  197. package/web-tool-core.ts +1810 -0
  198. package/xai-search.ts +443 -0
  199. package/you.ts +165 -0
  200. package/youtube-extract.ts +336 -0
  201. package/zai.ts +161 -0
package/ollama.ts ADDED
@@ -0,0 +1,237 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { activityMonitor } from "./activity.ts";
3
+ import type { ExtractedContent, ExtractOptions } from "./extract.ts";
4
+ import type { SearchOptions, SearchResponse } from "./perplexity.ts";
5
+ import { formatSearchResultsAsAnswer } from "./search-answer-formatting.ts";
6
+ import { hasCredentialSource, redactCredential, resolveCredential } from "./credential-source.ts";
7
+ import { fetchRemoteUrl, loadFetchContentDomainPolicy, loadSsrfConfig, validateRemoteUrl, type Lookup, type SsrfConfig } from "./ssrf-protection.ts";
8
+ import { getWebSearchConfigPath, resolveProviderApiUrl, fetchWithCredentialRedirects } from "./utils.ts";
9
+
10
+ const OLLAMA_SEARCH_URL = "https://ollama.com/api/web_search";
11
+ const OLLAMA_FETCH_URL = "https://ollama.com/api/web_fetch";
12
+ const CONFIG_PATH = getWebSearchConfigPath();
13
+ const SEARCH_TIMEOUT_MS = 60_000;
14
+
15
+ interface WebSearchConfig {
16
+ ollamaBaseUrl?: unknown;
17
+ ollamaApiKey?: unknown;
18
+ }
19
+
20
+ interface OllamaSearchResult {
21
+ title: string;
22
+ url: string;
23
+ content: string;
24
+ }
25
+
26
+ interface OllamaSearchResponse {
27
+ results: OllamaSearchResult[];
28
+ }
29
+
30
+ interface OllamaFetchResponse {
31
+ title: string;
32
+ content: string;
33
+ links?: unknown;
34
+ }
35
+
36
+ interface OllamaSearchOptions extends SearchOptions {
37
+ includeContent?: boolean;
38
+ }
39
+
40
+ export interface OllamaExtractOptions extends Pick<ExtractOptions, "timeoutMs" | "lookup"> {
41
+ ssrf?: SsrfConfig;
42
+ }
43
+
44
+ let cachedConfig: WebSearchConfig | null = null;
45
+
46
+ function loadConfig(): WebSearchConfig {
47
+ if (cachedConfig) return cachedConfig;
48
+ if (!existsSync(CONFIG_PATH)) {
49
+ cachedConfig = {};
50
+ return cachedConfig;
51
+ }
52
+
53
+ const raw = readFileSync(CONFIG_PATH, "utf-8");
54
+ let parsed: unknown;
55
+ try {
56
+ parsed = JSON.parse(raw);
57
+ } catch (err) {
58
+ const message = err instanceof Error ? err.message : String(err);
59
+ throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
60
+ }
61
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
62
+ throw new Error(`Invalid config in ${CONFIG_PATH}: expected a JSON object`);
63
+ }
64
+ cachedConfig = parsed as WebSearchConfig;
65
+ return cachedConfig;
66
+ }
67
+
68
+ async function getApiKey(signal?: AbortSignal): Promise<string | null> {
69
+ return resolveCredential({
70
+ provider: "Ollama",
71
+ configuredValue: loadConfig().ollamaApiKey,
72
+ environmentValue: process.env.OLLAMA_API_KEY,
73
+ signal,
74
+ });
75
+ }
76
+
77
+ async function requireApiKey(signal?: AbortSignal): Promise<string> {
78
+ const apiKey = await getApiKey(signal);
79
+ if (!apiKey) {
80
+ throw new Error(
81
+ "Ollama API key not found. Either:\n" +
82
+ ` 1. Create ${CONFIG_PATH} with { "ollamaApiKey": "your-key" }\n` +
83
+ " 2. Set OLLAMA_API_KEY environment variable\n" +
84
+ "Create a key at https://ollama.com/settings/keys",
85
+ );
86
+ }
87
+ return apiKey;
88
+ }
89
+
90
+ function normalizeCount(value: number | undefined): number {
91
+ if (typeof value !== "number" || !Number.isFinite(value)) return 5;
92
+ return Math.max(1, Math.min(Math.floor(value), 10));
93
+ }
94
+
95
+ function errorMessage(err: unknown): string {
96
+ return err instanceof Error ? err.message : String(err);
97
+ }
98
+
99
+ function invalidResponse(message: string): Error {
100
+ return new Error(`Ollama API returned invalid response: ${message}`);
101
+ }
102
+
103
+ function parseSearchResponse(value: unknown): OllamaSearchResponse {
104
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw invalidResponse("expected an object envelope");
105
+ const envelope = value as Record<string, unknown>;
106
+ if (!Array.isArray(envelope.results)) throw invalidResponse("expected results array");
107
+ const results: OllamaSearchResult[] = [];
108
+ for (const [index, value] of envelope.results.entries()) {
109
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw invalidResponse(`expected results[${index}] object`);
110
+ const item = value as Record<string, unknown>;
111
+ if (typeof item.title !== "string") throw invalidResponse(`expected results[${index}].title string`);
112
+ if (typeof item.url !== "string" || !item.url) throw invalidResponse(`expected results[${index}].url non-empty string`);
113
+ if (typeof item.content !== "string") throw invalidResponse(`expected results[${index}].content string`);
114
+ results.push({ title: item.title, url: item.url, content: item.content });
115
+ }
116
+ return { results };
117
+ }
118
+
119
+ function parseFetchResponse(value: unknown): OllamaFetchResponse {
120
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw invalidResponse("expected fetch object envelope");
121
+ const envelope = value as Record<string, unknown>;
122
+ if (typeof envelope.title !== "string") throw invalidResponse("expected title string");
123
+ if (typeof envelope.content !== "string") throw invalidResponse("expected content string");
124
+ return { title: envelope.title, content: envelope.content, links: envelope.links };
125
+ }
126
+
127
+ export function isOllamaAvailable(): boolean {
128
+ return hasCredentialSource({ provider: "Ollama", configuredValue: loadConfig().ollamaApiKey, environmentValue: process.env.OLLAMA_API_KEY });
129
+ }
130
+
131
+ export async function searchWithOllama(query: string, options: OllamaSearchOptions = {}): Promise<SearchResponse> {
132
+ const apiKey = await requireApiKey(options.signal);
133
+ const numResults = normalizeCount(options.numResults);
134
+ const activityId = activityMonitor.logStart({ type: "api", query });
135
+ let response: Response;
136
+ try {
137
+ response = await fetchWithCredentialRedirects(resolveProviderApiUrl("ollama", OLLAMA_SEARCH_URL, loadConfig().ollamaBaseUrl), {
138
+ method: "POST",
139
+ headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
140
+ body: JSON.stringify({ query, max_results: numResults }),
141
+ signal: options.signal ? AbortSignal.any([AbortSignal.timeout(SEARCH_TIMEOUT_MS), options.signal]) : AbortSignal.timeout(SEARCH_TIMEOUT_MS),
142
+ }, ["Authorization"]);
143
+ } catch (err) {
144
+ const message = errorMessage(err);
145
+ const redactedMessage = redactCredential(message, apiKey);
146
+ if (redactedMessage.toLowerCase().includes("abort")) activityMonitor.logComplete(activityId, 0);
147
+ else activityMonitor.logError(activityId, redactedMessage);
148
+ if (redactedMessage === message) throw err;
149
+ const redactedError = new Error(redactedMessage);
150
+ if (err instanceof Error) redactedError.name = err.name;
151
+ throw redactedError;
152
+ }
153
+
154
+ if (!response.ok) {
155
+ activityMonitor.logComplete(activityId, response.status);
156
+ const errorText = redactCredential(await response.text(), apiKey);
157
+ throw new Error(`Ollama API error ${response.status}: ${errorText.slice(0, 300)}`);
158
+ }
159
+
160
+ let rawData: unknown;
161
+ try {
162
+ rawData = await response.json();
163
+ } catch (err) {
164
+ activityMonitor.logComplete(activityId, response.status);
165
+ throw new Error(`Ollama API returned invalid JSON: ${errorMessage(err)}`);
166
+ }
167
+
168
+ const data = parseSearchResponse(rawData);
169
+ activityMonitor.logComplete(activityId, response.status);
170
+ const results = data.results.slice(0, numResults).map((result) => ({ title: result.title, url: result.url, snippet: result.content }));
171
+ const mapped: SearchResponse = { answer: formatSearchResultsAsAnswer(results), results };
172
+ if (options.includeContent) {
173
+ const inlineContent: ExtractedContent[] = data.results.slice(0, numResults)
174
+ .filter(result => result.content.trim().length > 0)
175
+ .map(result => ({ url: result.url, title: result.title, content: result.content, error: null }));
176
+ if (inlineContent.length > 0) mapped.inlineContent = inlineContent;
177
+ }
178
+ return mapped;
179
+ }
180
+
181
+ export function isOllamaFetchAvailable(): boolean {
182
+ return isOllamaAvailable();
183
+ }
184
+
185
+ export async function extractWithOllama(url: string, signal?: AbortSignal, options: OllamaExtractOptions = {}): Promise<ExtractedContent | null> {
186
+ const ssrf = options.ssrf ?? loadSsrfConfig();
187
+ const domainPolicy = loadFetchContentDomainPolicy();
188
+ await validateRemoteUrl(url, {
189
+ allowRanges: ssrf.allowRanges,
190
+ trustEnvProxy: ssrf.trustEnvProxy,
191
+ domainPolicy,
192
+ ...(options.lookup ? { lookup: options.lookup } : {}),
193
+ });
194
+ const apiKey = await requireApiKey(signal);
195
+ const activityId = activityMonitor.logStart({ type: "api", query: `ollama fetch: ${url}` });
196
+ let response: Response;
197
+ try {
198
+ response = await fetchRemoteUrl(resolveProviderApiUrl("ollama", OLLAMA_FETCH_URL, loadConfig().ollamaBaseUrl), {
199
+ method: "POST",
200
+ headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
201
+ body: JSON.stringify({ url }),
202
+ signal: signal ? AbortSignal.any([AbortSignal.timeout(options.timeoutMs ?? SEARCH_TIMEOUT_MS), signal]) : AbortSignal.timeout(options.timeoutMs ?? SEARCH_TIMEOUT_MS),
203
+ }, {
204
+ allowRanges: ssrf.allowRanges,
205
+ trustEnvProxy: ssrf.trustEnvProxy,
206
+ onRedirect: ({ from, to, init }) => to.origin === from.origin ? init : { ...init, headers: { "Content-Type": "application/json" } },
207
+ ...(options.lookup ? { lookup: options.lookup } : {}),
208
+ });
209
+ } catch (err) {
210
+ const message = errorMessage(err);
211
+ const redactedMessage = redactCredential(message, apiKey);
212
+ if (redactedMessage.toLowerCase().includes("abort")) activityMonitor.logComplete(activityId, 0);
213
+ else activityMonitor.logError(activityId, redactedMessage);
214
+ if (redactedMessage === message) throw err;
215
+ const redactedError = new Error(redactedMessage);
216
+ if (err instanceof Error) redactedError.name = err.name;
217
+ throw redactedError;
218
+ }
219
+
220
+ if (!response.ok) {
221
+ activityMonitor.logComplete(activityId, response.status);
222
+ const errorText = redactCredential(await response.text(), apiKey);
223
+ throw new Error(`Ollama Web Fetch error ${response.status}: ${errorText.slice(0, 300)}`);
224
+ }
225
+ let rawData: unknown;
226
+ try {
227
+ rawData = await response.json();
228
+ } catch (err) {
229
+ activityMonitor.logComplete(activityId, response.status);
230
+ throw new Error(`Ollama Web Fetch returned invalid JSON: ${errorMessage(err)}`);
231
+ }
232
+ const data = parseFetchResponse(rawData);
233
+ activityMonitor.logComplete(activityId, response.status);
234
+ const content = data.content.trim();
235
+ if (!content) return null;
236
+ return { url, title: data.title, content, error: null };
237
+ }