@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
@@ -0,0 +1,128 @@
1
+ import { activityMonitor } from "./activity.ts";
2
+ import { CredentialResolutionError } from "./credential-source.ts";
3
+ import { getApiKey, getVersionedApiBase, fetchGeminiApi, isGatewayConfigured, DEFAULT_MODEL } from "./gemini-api.ts";
4
+ import { isGeminiAdcAvailable } from "./gemini-adc.ts";
5
+ import { isGeminiWebAvailable, queryWithCookies } from "./gemini-web.ts";
6
+ import { extractHeadingTitle, type ExtractedContent } from "./extract.ts";
7
+
8
+ const EXTRACTION_PROMPT = `Extract the complete readable content from this URL as clean markdown.
9
+ Include the page title, all text content, code blocks, and tables.
10
+ Do not summarize — extract the full content.
11
+
12
+ URL: `;
13
+
14
+ function shouldRethrow(err: unknown): boolean {
15
+ const message = err instanceof Error ? err.message : String(err);
16
+ return err instanceof CredentialResolutionError || message.startsWith("Failed to parse ");
17
+ }
18
+
19
+ export async function extractWithUrlContext(
20
+ url: string,
21
+ signal?: AbortSignal,
22
+ ): Promise<ExtractedContent | null> {
23
+ const requestSignal = AbortSignal.any([
24
+ AbortSignal.timeout(60000),
25
+ ...(signal ? [signal] : []),
26
+ ]);
27
+ const apiKey = isGeminiAdcAvailable() ? null : await getApiKey(requestSignal);
28
+ if (!apiKey && !isGatewayConfigured() && !isGeminiAdcAvailable()) return null;
29
+
30
+ const activityId = activityMonitor.logStart({ type: "api", query: `url_context: ${url}` });
31
+
32
+ try {
33
+ const model = DEFAULT_MODEL;
34
+ const body = {
35
+ contents: [{ role: "user", parts: [{ text: EXTRACTION_PROMPT + url }] }],
36
+ tools: [{ url_context: {} }],
37
+ };
38
+
39
+ const res = await fetchGeminiApi(`${getVersionedApiBase()}/models/${model}:generateContent`, {
40
+ method: "POST",
41
+ headers: { "Content-Type": "application/json" },
42
+ body: JSON.stringify(body),
43
+ signal: requestSignal,
44
+ }, apiKey);
45
+
46
+ if (!res.ok) {
47
+ activityMonitor.logComplete(activityId, res.status);
48
+ return null;
49
+ }
50
+
51
+ const data = await res.json() as UrlContextResponse;
52
+ activityMonitor.logComplete(activityId, res.status);
53
+
54
+ const metadata = data.candidates?.[0]?.url_context_metadata;
55
+ if (metadata?.url_metadata?.length) {
56
+ const status = metadata.url_metadata[0].url_retrieval_status;
57
+ if (status === "URL_RETRIEVAL_STATUS_UNSAFE" || status === "URL_RETRIEVAL_STATUS_ERROR") {
58
+ return null;
59
+ }
60
+ }
61
+
62
+ const content = data.candidates?.[0]?.content?.parts
63
+ ?.map(p => p.text).filter(Boolean).join("\n") ?? "";
64
+
65
+ if (!content || content.length < 50) return null;
66
+
67
+ const title = extractTitleFromContent(content, url);
68
+ return { url, title, content, error: null };
69
+ } catch (err) {
70
+ if (shouldRethrow(err)) throw err;
71
+ const message = err instanceof Error ? err.message : String(err);
72
+ if (message.toLowerCase().includes("abort")) {
73
+ activityMonitor.logComplete(activityId, 0);
74
+ } else {
75
+ activityMonitor.logError(activityId, message);
76
+ }
77
+ return null;
78
+ }
79
+ }
80
+
81
+ export async function extractWithGeminiWeb(
82
+ url: string,
83
+ signal?: AbortSignal,
84
+ ): Promise<ExtractedContent | null> {
85
+ const cookies = await isGeminiWebAvailable();
86
+ if (!cookies) return null;
87
+
88
+ const activityId = activityMonitor.logStart({ type: "api", query: `gemini_web: ${url}` });
89
+
90
+ try {
91
+ const text = await queryWithCookies(EXTRACTION_PROMPT + url, cookies, {
92
+ signal,
93
+ timeoutMs: 60000,
94
+ });
95
+
96
+ activityMonitor.logComplete(activityId, 200);
97
+
98
+ if (!text || text.length < 50) return null;
99
+
100
+ const title = extractTitleFromContent(text, url);
101
+ return { url, title, content: text, error: null };
102
+ } catch (err) {
103
+ if (shouldRethrow(err)) throw err;
104
+ const message = err instanceof Error ? err.message : String(err);
105
+ if (message.toLowerCase().includes("abort")) {
106
+ activityMonitor.logComplete(activityId, 0);
107
+ } else {
108
+ activityMonitor.logError(activityId, message);
109
+ }
110
+ return null;
111
+ }
112
+ }
113
+
114
+ function extractTitleFromContent(text: string, url: string): string {
115
+ return extractHeadingTitle(text) ?? (new URL(url).pathname.split("/").pop() || url);
116
+ }
117
+
118
+ interface UrlContextResponse {
119
+ candidates?: Array<{
120
+ content?: { parts?: Array<{ text?: string }> };
121
+ url_context_metadata?: {
122
+ url_metadata?: Array<{
123
+ retrieved_url?: string;
124
+ url_retrieval_status?: string;
125
+ }>;
126
+ };
127
+ }>;
128
+ }
@@ -0,0 +1,131 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { getWebSearchConfigPath } from "./utils.ts";
3
+
4
+ const CONFIG_PATH = getWebSearchConfigPath();
5
+
6
+ interface GeminiWebConfig {
7
+ browserCookies?: BrowserCookieSelection;
8
+ allowBrowserCookies?: boolean;
9
+ }
10
+
11
+ const BROWSER_COOKIE_PRESETS = ["helium", "ardia", "chrome", "brave", "arc", "chromium", "edge"] as const;
12
+ export type BrowserCookiePreset = typeof BROWSER_COOKIE_PRESETS[number];
13
+
14
+ export interface BrowserCookieSelection {
15
+ browser?: BrowserCookiePreset;
16
+ profile?: string;
17
+ }
18
+
19
+ let cachedConfig: GeminiWebConfig | null = null;
20
+ // Set when the user allows browser cookie access for this session only; never written to config.
21
+ let sessionGrant: BrowserCookieSelection | null = null;
22
+
23
+ /** How a tool call can get browser cookie access turned on. */
24
+ export interface BrowserCookieConsent {
25
+ /** Asks the user; resolves true once access is on, false on a no or when signal aborts. Absent without a UI. */
26
+ ask?: (signal?: AbortSignal) => Promise<boolean>;
27
+ /** Pi command that turns access on or off; absent outside Pi. */
28
+ command?: string;
29
+ }
30
+
31
+ export function normalizeChromeProfile(value: unknown): string | undefined {
32
+ if (typeof value !== "string") return undefined;
33
+ const normalized = value.trim();
34
+ return normalized.length > 0 ? normalized : undefined;
35
+ }
36
+
37
+ function parseBrowserCookies(value: unknown): BrowserCookieSelection | undefined {
38
+ if (value === undefined) return undefined;
39
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
40
+ throw new Error(`browserCookies in ${CONFIG_PATH} must be an object`);
41
+ }
42
+ const raw = value as Record<string, unknown>;
43
+ if ("profilePath" in raw) {
44
+ throw new Error(`browserCookies.profilePath is not supported; use a browser preset and profile directory name in ${CONFIG_PATH}`);
45
+ }
46
+ let browser: BrowserCookiePreset | undefined;
47
+ if (raw.browser !== undefined) browser = parseBrowserCookiePreset(raw.browser, "browserCookies.browser");
48
+ if (raw.profile !== undefined && typeof raw.profile !== "string") {
49
+ throw new Error(`browserCookies.profile in ${CONFIG_PATH} must be a profile directory name`);
50
+ }
51
+ const profile = normalizeChromeProfile(raw.profile);
52
+ if (profile && (profile === "." || profile === ".." || profile.includes("/") || profile.includes("\\"))) {
53
+ throw new Error(`browserCookies.profile in ${CONFIG_PATH} must be a profile directory name, not a path`);
54
+ }
55
+ return { ...(browser ? { browser } : {}), ...(profile ? { profile } : {}) };
56
+ }
57
+
58
+ export function parseBrowserCookiePreset(value: unknown, label: string): BrowserCookiePreset {
59
+ if (typeof value !== "string") throw new Error(`${label} in ${CONFIG_PATH} must be a supported browser name`);
60
+ const normalized = value.trim().toLowerCase();
61
+ const preset = BROWSER_COOKIE_PRESETS.find((candidate) => candidate === normalized);
62
+ if (!preset) throw new Error(`Unsupported ${label} ${JSON.stringify(value)} in ${CONFIG_PATH}`);
63
+ return preset;
64
+ }
65
+
66
+ function loadConfig(): GeminiWebConfig {
67
+ if (cachedConfig) return cachedConfig;
68
+ if (!existsSync(CONFIG_PATH)) {
69
+ cachedConfig = {};
70
+ return cachedConfig;
71
+ }
72
+
73
+ const rawText = readFileSync(CONFIG_PATH, "utf-8");
74
+ let raw: { chromeProfile?: unknown; browserCookies?: unknown; allowBrowserCookies?: unknown };
75
+ try {
76
+ raw = JSON.parse(rawText) as { chromeProfile?: unknown; browserCookies?: unknown; allowBrowserCookies?: unknown };
77
+ } catch (err) {
78
+ const message = err instanceof Error ? err.message : String(err);
79
+ throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
80
+ }
81
+
82
+ if (raw.chromeProfile !== undefined) {
83
+ throw new Error(`chromeProfile in ${CONFIG_PATH} is no longer supported; use browserCookies.profile`);
84
+ }
85
+ cachedConfig = {
86
+ browserCookies: parseBrowserCookies(raw.browserCookies),
87
+ allowBrowserCookies: raw.allowBrowserCookies === true,
88
+ };
89
+ return cachedConfig;
90
+ }
91
+
92
+ export function getBrowserCookieSelectionFromConfig(): BrowserCookieSelection {
93
+ return { ...loadConfig().browserCookies };
94
+ }
95
+
96
+ /** The browser a session grant chose, otherwise the configured selection. */
97
+ export function getBrowserCookieSelection(): BrowserCookieSelection {
98
+ return sessionGrant ? { ...sessionGrant } : getBrowserCookieSelectionFromConfig();
99
+ }
100
+
101
+ /** Turns browser cookie access on until the process exits, without saving anything. */
102
+ export function grantBrowserCookieAccessForSession(selection: BrowserCookieSelection): void {
103
+ sessionGrant = { ...selection };
104
+ }
105
+
106
+ export function revokeBrowserCookieAccessForSession(): void {
107
+ sessionGrant = null;
108
+ }
109
+
110
+ /** Call after writing web-search.json so the next read sees the new browser selection. */
111
+ export function clearGeminiWebConfigCache(): void {
112
+ cachedConfig = null;
113
+ }
114
+
115
+ export function isBrowserCookieAccessAllowed(): boolean {
116
+ if (sessionGrant) return true;
117
+ if (process.env.PI_ALLOW_BROWSER_COOKIES === "1" || process.env.FEYNMAN_ALLOW_BROWSER_COOKIES === "1") {
118
+ return true;
119
+ }
120
+ if (!existsSync(CONFIG_PATH)) return false;
121
+
122
+ const rawText = readFileSync(CONFIG_PATH, "utf-8");
123
+ let raw: { allowBrowserCookies?: unknown };
124
+ try {
125
+ raw = JSON.parse(rawText) as { allowBrowserCookies?: unknown };
126
+ } catch (err) {
127
+ const message = err instanceof Error ? err.message : String(err);
128
+ throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
129
+ }
130
+ return raw.allowBrowserCookies === true;
131
+ }