@rol1n/pi-kit 0.1.0 → 0.1.2

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 (155) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -50
  3. package/README_zh.md +136 -0
  4. package/ask-u-questions/README.md +26 -0
  5. package/{extensions/ask-u-questions → ask-u-questions}/dialog-ui.ts +23 -0
  6. package/{extensions/ask-u-questions → ask-u-questions}/index.ts +9 -5
  7. package/{extensions/ask-u-questions → ask-u-questions}/multi-choice.ts +1 -3
  8. package/{extensions/ask-u-questions → ask-u-questions}/single-choice.ts +1 -3
  9. package/{extensions/ask-u-questions → ask-u-questions}/tool-renderers.ts +1 -3
  10. package/ask-u-questions/types.ts +192 -0
  11. package/continue-signal/README.md +20 -0
  12. package/{extensions/continue-signal → continue-signal}/index.ts +21 -3
  13. package/custom-providers/README.md +20 -0
  14. package/{extensions/custom-providers → custom-providers}/index.ts +16 -2
  15. package/image-generate/README.md +25 -0
  16. package/{extensions/image-generate → image-generate}/index.ts +22 -2
  17. package/{extensions/image-generate → image-generate}/model-selector.ts +1 -1
  18. package/memory-log/README.md +121 -0
  19. package/{extensions/memory-log → memory-log}/config.ts +1 -1
  20. package/{extensions/memory-log → memory-log}/hooks/compaction-hook.ts +58 -15
  21. package/memory-log/hooks/compaction-trigger.ts +87 -0
  22. package/{extensions/memory-log → memory-log}/hooks/observer-trigger.ts +2 -1
  23. package/{extensions/memory-log → memory-log}/index.ts +4 -0
  24. package/{extensions/memory-log → memory-log}/ledger/index.ts +1 -0
  25. package/memory-log/ledger/recall.ts +111 -0
  26. package/{extensions/memory-log → memory-log}/ledger/render.ts +3 -1
  27. package/{extensions/memory-log → memory-log}/ledger/serialize.ts +2 -4
  28. package/{extensions/memory-log → memory-log}/ledger/types.ts +4 -1
  29. package/{extensions/memory-log → memory-log}/runtime.ts +0 -5
  30. package/memory-log/tools/recall.ts +60 -0
  31. package/model-vision/README.md +22 -0
  32. package/{extensions/multi-harness → multi-harness}/README.md +3 -3
  33. package/oai-subquota/README.md +22 -0
  34. package/{extensions/oai-subquota → oai-subquota}/index.ts +16 -6
  35. package/{extensions/oai-subquota → oai-subquota}/snapshot.ts +127 -1
  36. package/{extensions/oai-subquota → oai-subquota}/ui.ts +1 -1
  37. package/package.json +46 -46
  38. package/prompt-snipaste/README.md +27 -0
  39. package/{extensions/prompt-snipaste → prompt-snipaste}/renderers.ts +0 -1
  40. package/{extensions/prompt-snipaste → prompt-snipaste}/snippets.ts +5 -2
  41. package/{extensions/prompt-snipaste → prompt-snipaste}/types.ts +0 -1
  42. package/screenshot/README.md +14 -0
  43. package/srp-theme/README.md +18 -0
  44. package/{extensions/srp-theme → srp-theme}/footer.ts +0 -1
  45. package/{extensions/srp-theme → srp-theme}/tps-meter.ts +0 -1
  46. package/{extensions/subagent → subagent}/README.md +1 -1
  47. package/tui-asr/README.md +21 -0
  48. package/{extensions/tui-asr → tui-asr}/dictation.ts +70 -3
  49. package/{extensions/tui-asr → tui-asr}/index.ts +2 -2
  50. package/{extensions/tui-asr → tui-asr}/input.ts +30 -1
  51. package/{extensions/tui-asr → tui-asr}/model-selector.ts +1 -1
  52. package/web-reach/README.md +19 -0
  53. package/windows-wheel-fix/README.md +12 -0
  54. package/extensions/ask-u-questions/README.md +0 -32
  55. package/extensions/ask-u-questions/answers.ts +0 -53
  56. package/extensions/ask-u-questions/results.ts +0 -83
  57. package/extensions/ask-u-questions/schema.ts +0 -17
  58. package/extensions/ask-u-questions/types.ts +0 -45
  59. package/extensions/ask-u-questions/ui-lock.ts +0 -24
  60. package/extensions/continue-signal/README.md +0 -28
  61. package/extensions/continue-signal/renderer.ts +0 -21
  62. package/extensions/custom-providers/README.md +0 -28
  63. package/extensions/image-generate/README.md +0 -29
  64. package/extensions/image-generate/models.ts +0 -19
  65. package/extensions/memory-log/README.md +0 -54
  66. package/extensions/memory-log/hooks/compaction-trigger.ts +0 -160
  67. package/extensions/model-vision/README.md +0 -28
  68. package/extensions/oai-subquota/README.md +0 -30
  69. package/extensions/oai-subquota/api.ts +0 -27
  70. package/extensions/oai-subquota/auth.ts +0 -34
  71. package/extensions/oai-subquota/cache.ts +0 -30
  72. package/extensions/oai-subquota/types.ts +0 -40
  73. package/extensions/prompt-snipaste/README.md +0 -33
  74. package/extensions/screenshot/README.md +0 -25
  75. package/extensions/srp-theme/README.md +0 -24
  76. package/extensions/subagent/skills/subagent/SKILL.md +0 -70
  77. package/extensions/tui-asr/README.md +0 -28
  78. package/extensions/tui-asr/audio.ts +0 -40
  79. package/extensions/tui-asr/clipboard.ts +0 -30
  80. package/extensions/tui-asr/providers.ts +0 -25
  81. package/extensions/web-reach/README.md +0 -37
  82. package/extensions/web-reach/html.ts +0 -83
  83. package/extensions/web-reach/index.ts +0 -82
  84. package/extensions/web-reach/page.ts +0 -132
  85. package/extensions/web-reach/pdf.ts +0 -42
  86. package/extensions/web-reach/reader.ts +0 -42
  87. package/extensions/web-reach/rsc.ts +0 -161
  88. package/extensions/web-reach/search.ts +0 -85
  89. package/extensions/web-reach/transport.ts +0 -35
  90. package/extensions/windows-wheel-fix/README.md +0 -21
  91. /package/{extensions/custom-providers → custom-providers}/asr/handlers/dashscope.ts +0 -0
  92. /package/{extensions/custom-providers → custom-providers}/asr/index.ts +0 -0
  93. /package/{extensions/custom-providers → custom-providers}/asr/models.ts +0 -0
  94. /package/{extensions/custom-providers → custom-providers}/asr/types.ts +0 -0
  95. /package/{extensions/custom-providers → custom-providers}/chat/index.ts +0 -0
  96. /package/{extensions/custom-providers → custom-providers}/chat/magpie.ts +0 -0
  97. /package/{extensions/custom-providers → custom-providers}/chat/models.ts +0 -0
  98. /package/{extensions/custom-providers → custom-providers}/classifier/index.ts +0 -0
  99. /package/{extensions/custom-providers → custom-providers}/classifier/models.ts +0 -0
  100. /package/{extensions/custom-providers → custom-providers}/image/handlers/dashscope.ts +0 -0
  101. /package/{extensions/custom-providers → custom-providers}/image/handlers/openrouter.ts +0 -0
  102. /package/{extensions/custom-providers → custom-providers}/image/index.ts +0 -0
  103. /package/{extensions/custom-providers → custom-providers}/image/models.ts +0 -0
  104. /package/{extensions/custom-providers → custom-providers}/image/types.ts +0 -0
  105. /package/{extensions/custom-providers → custom-providers}/types.ts +0 -0
  106. /package/{extensions/image-generate → image-generate}/files.ts +0 -0
  107. /package/{extensions/memory-log → memory-log}/agent/consolidator/prompt.ts +0 -0
  108. /package/{extensions/memory-log → memory-log}/agent/consolidator/tools.ts +0 -0
  109. /package/{extensions/memory-log → memory-log}/agent/cost.ts +0 -0
  110. /package/{extensions/memory-log → memory-log}/agent/index.ts +0 -0
  111. /package/{extensions/memory-log → memory-log}/agent/observer/prompt.ts +0 -0
  112. /package/{extensions/memory-log → memory-log}/agent/observer/tool.ts +0 -0
  113. /package/{extensions/memory-log → memory-log}/commands/compact.ts +0 -0
  114. /package/{extensions/memory-log → memory-log}/commands/consolidate.ts +0 -0
  115. /package/{extensions/memory-log → memory-log}/commands/status.ts +0 -0
  116. /package/{extensions/memory-log → memory-log}/debug-log.ts +0 -0
  117. /package/{extensions/memory-log → memory-log}/hooks/consolidator-trigger.ts +0 -0
  118. /package/{extensions/memory-log → memory-log}/ids.ts +0 -0
  119. /package/{extensions/memory-log → memory-log}/ledger/fold.ts +0 -0
  120. /package/{extensions/memory-log → memory-log}/ledger/pool.ts +0 -0
  121. /package/{extensions/memory-log → memory-log}/ledger/progress.ts +0 -0
  122. /package/{extensions/memory-log → memory-log}/ledger/projection.ts +0 -0
  123. /package/{extensions/memory-log → memory-log}/memory/index-render.ts +0 -0
  124. /package/{extensions/memory-log → memory-log}/memory/paths.ts +0 -0
  125. /package/{extensions/memory-log → memory-log}/memory/session.ts +0 -0
  126. /package/{extensions/memory-log → memory-log}/spawn/launch.ts +0 -0
  127. /package/{extensions/memory-log → memory-log}/spawn/runs.ts +0 -0
  128. /package/{extensions/memory-log → memory-log}/tokens.ts +0 -0
  129. /package/{extensions/memory-log → memory-log}/ui/status-controller.ts +0 -0
  130. /package/{extensions/memory-log → memory-log}/ui/timeline.ts +0 -0
  131. /package/{extensions/multi-harness → multi-harness}/adapters.mjs +0 -0
  132. /package/{extensions/multi-harness → multi-harness}/harness-done.mjs +0 -0
  133. /package/{extensions/multi-harness → multi-harness}/index.ts +0 -0
  134. /package/{extensions/multi-harness → multi-harness}/ipc.mjs +0 -0
  135. /package/{extensions/multi-harness → multi-harness}/opencode-plugin.mjs +0 -0
  136. /package/{extensions/multi-harness → multi-harness}/process-runner.mjs +0 -0
  137. /package/{extensions/multi-harness → multi-harness}/turn-hook.mjs +0 -0
  138. /package/{extensions/multi-harness → multi-harness}/worker-bridge.ts +0 -0
  139. /package/{extensions/prompt-snipaste → prompt-snipaste}/index.ts +0 -0
  140. /package/{extensions/prompt-snipaste → prompt-snipaste}/menu.ts +0 -0
  141. /package/{prompts → prompt-snipaste/prompts}/ask-questions.md +0 -0
  142. /package/{prompts → prompt-snipaste/prompts}/delegate-exploration.md +0 -0
  143. /package/{prompts → prompt-snipaste/prompts}/diagnose-report.md +0 -0
  144. /package/{prompts → prompt-snipaste/prompts}/frontend-tech-preset.md +0 -0
  145. /package/{prompts → prompt-snipaste/prompts}/orchestrator-mode.md +0 -0
  146. /package/{prompts → prompt-snipaste/prompts}/session-kickoff.md +0 -0
  147. /package/{prompts → prompt-snipaste/prompts}/verify-not-assume.md +0 -0
  148. /package/{extensions/srp-theme → srp-theme}/index.ts +0 -0
  149. /package/{extensions/subagent → subagent}/bridge.ts +0 -0
  150. /package/{extensions/subagent → subagent}/index.ts +0 -0
  151. /package/{extensions/subagent → subagent}/mux.ts +0 -0
  152. /package/{extensions/subagent → subagent}/session.ts +0 -0
  153. /package/{extensions/subagent → subagent}/ui.ts +0 -0
  154. /package/{extensions/tui-asr → tui-asr}/status.ts +0 -0
  155. /package/{extensions/windows-wheel-fix → windows-wheel-fix}/index.ts +0 -0
@@ -1,25 +0,0 @@
1
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
-
3
- export { ALL_ASR_MODELS, createAsrProvider, type AsrModelDefinition, type StreamingAsrProvider } from "../custom-providers/asr/index.ts";
4
-
5
- export async function resolveProviderApiKey(provider: string, ctx?: ExtensionContext | null): Promise<string> {
6
- if (ctx?.modelRegistry) {
7
- const candidates = provider === "dashscope" ? ["dashscope", "aliyun"] : [provider];
8
- for (const id of candidates) {
9
- try {
10
- const key = await ctx.modelRegistry.getApiKeyForProvider(id);
11
- if (key?.trim()) return key.trim();
12
- } catch {}
13
- try {
14
- const auth = await ctx.modelRegistry.getProviderAuth(id);
15
- if (auth?.auth?.apiKey?.trim()) return auth.auth.apiKey.trim();
16
- } catch {}
17
- }
18
- }
19
- const envKey = process.env[`${provider.toUpperCase()}_API_KEY`];
20
- if (envKey?.trim()) return envKey.trim();
21
- if (provider === "dashscope") {
22
- return process.env.ALIYUN_API_KEY?.trim() || "";
23
- }
24
- return "";
25
- }
@@ -1,37 +0,0 @@
1
- # web-reach
2
-
3
- Web search and page extraction tools for Pi. Registers `web_search` through Exa's public MCP endpoint and `web_fetch` for readable Markdown or raw responses.
4
-
5
- ## Install
6
-
7
- ```sh
8
- pi install npm:@rol1n/web-reach
9
- ```
10
-
11
- Also included in `@rol1n/pi-kit`. Avoid enabling another extension that registers the same tool names unless overriding them is intentional.
12
-
13
- ## Use
14
-
15
- ```json
16
- {"query":"Pi coding agent package resource filtering"}
17
- ```
18
-
19
- ```json
20
- {"url":"https://example.com/docs","mode":"readable"}
21
- ```
22
-
23
- The first object is for `web_search`; the second is for `web_fetch`. Fetch mode defaults to `readable` and also accepts `raw`. Extraction includes HTML-to-Markdown, JSON formatting, PDF handling, and a Jina Reader fallback for pages that need it. Returned text is limited to 35,000 characters.
24
-
25
- ## Configuration
26
-
27
- | Environment variable | Default |
28
- |---|---|
29
- | `SRP_WEB_SEARCH_TIMEOUT_MS` | 60000 |
30
- | `SRP_WEB_FETCH_TIMEOUT_MS` | 20000 |
31
- | `SRP_WEB_FETCH_MAX_BYTES` | 4194304 (4 MiB) |
32
-
33
- No separate search API key is configured; availability and rate limits depend on the public Exa and Jina services. PDF extraction optionally imports `unpdf` if it is available. Without that package the implementation falls back to a basic text extraction path, with lower PDF text quality.
34
-
35
- ## Source layout
36
-
37
- `index.ts` registers the tools and re-exports `searchWeb` and `fetchUrl`. `search.ts` handles Exa MCP requests and response parsing; `transport.ts` shares timeout, cancellation, environment, and truncation helpers. `page.ts` coordinates page fetching and extraction, using `rsc.ts` for Next.js data, `html.ts` for semantic Markdown, `pdf.ts` for optional PDF extraction, and `reader.ts` for the Jina Reader fallback.
@@ -1,83 +0,0 @@
1
- export function extractTitleFromHtml(html: string): string {
2
- const match = html.match(/<title[^>]*>([^<]+)<\/title>/i);
3
- return match?.[1]?.trim() ?? "";
4
- }
5
-
6
- export function isLikelyJSRendered(html: string): boolean {
7
- const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
8
- if (!bodyMatch) return false;
9
- const textContent = bodyMatch[1]
10
- .replace(/<script[\s\S]*?<\/script>/gi, "")
11
- .replace(/<style[\s\S]*?<\/style>/gi, "")
12
- .replace(/<[^>]+>/g, "")
13
- .replace(/\s+/g, " ")
14
- .trim();
15
- const scriptCount = (html.match(/<script/gi) || []).length;
16
- return textContent.length < 300 && scriptCount >= 2;
17
- }
18
-
19
- export function htmlToMarkdown(html: string): string {
20
- let s = html.replace(/<(script|style|noscript|svg|canvas|form|nav|footer|header|aside)[\s\S]*?<\/\1>/gi, " ");
21
- s = s.replace(/<!--[\s\S]*?-->/g, " ");
22
-
23
- // Headings
24
- s = s.replace(/<h1[^>]*>([\s\S]*?)<\/h1>/gi, "\n\n# $1\n\n");
25
- s = s.replace(/<h2[^>]*>([\s\S]*?)<\/h2>/gi, "\n\n## $1\n\n");
26
- s = s.replace(/<h3[^>]*>([\s\S]*?)<\/h3>/gi, "\n\n### $1\n\n");
27
- s = s.replace(/<h4[^>]*>([\s\S]*?)<\/h4>/gi, "\n\n#### $1\n\n");
28
- s = s.replace(/<h5[^>]*>([\s\S]*?)<\/h5>/gi, "\n\n##### $1\n\n");
29
- s = s.replace(/<h6[^>]*>([\s\S]*?)<\/h6>/gi, "\n\n###### $1\n\n");
30
-
31
- // Code blocks and inline code
32
- s = s.replace(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/gi, "\n```\n$1\n```\n");
33
- s = s.replace(/<pre[^>]*>([\s\S]*?)<\/pre>/gi, "\n```\n$1\n```\n");
34
- s = s.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, " `$1` ");
35
-
36
- // Blockquotes and emphasis
37
- s = s.replace(/<blockquote[^>]*>([\s\S]*?)<\/blockquote>/gi, "\n> $1\n");
38
- s = s.replace(/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi, "**$2**");
39
- s = s.replace(/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi, "*$2*");
40
-
41
- // Links
42
- s = s.replace(/<a\s+[^>]*href=["']([^"']+)["'][^>]*>([\s\S]*?)<\/a>/gi, (_, href, text) => {
43
- const t = text.replace(/<[^>]*>/g, "").trim();
44
- if (!t) return "";
45
- if (href.startsWith("#") || href.startsWith("javascript:")) return t;
46
- return `[${t}](${href})`;
47
- });
48
-
49
- // Lists
50
- s = s.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, "\n- $1");
51
- s = s.replace(/<\/(ul|ol)>/gi, "\n\n");
52
-
53
- // Tables
54
- s = s.replace(/<tr[^>]*>([\s\S]*?)<\/tr>/gi, "\n$1 |");
55
- s = s.replace(/<t[dh][^>]*>([\s\S]*?)<\/t[dh]>/gi, " | $1");
56
- s = s.replace(/<\/(table|thead|tbody)>/gi, "\n\n");
57
-
58
- // Paragraphs and line breaks
59
- s = s.replace(/<\/(p|div|section|article)>/gi, "\n\n");
60
- s = s.replace(/<br\s*\/?>/gi, "\n");
61
-
62
- // Strip remaining tags
63
- s = s.replace(/<[^>]*>/g, " ");
64
-
65
- // HTML entity decoding
66
- s = s
67
- .replace(/&nbsp;/g, " ")
68
- .replace(/&amp;/g, "&")
69
- .replace(/&lt;/g, "<")
70
- .replace(/&gt;/g, ">")
71
- .replace(/&quot;/g, '"')
72
- .replace(/&#39;/g, "'")
73
- .replace(/&hellip;/g, "…")
74
- .replace(/&mdash;/g, "—")
75
- .replace(/&ndash;/g, "–");
76
-
77
- return s
78
- .split("\n")
79
- .map((line) => line.trimEnd())
80
- .filter((line, i, arr) => !(line === "" && arr[i - 1] === ""))
81
- .join("\n")
82
- .trim();
83
- }
@@ -1,82 +0,0 @@
1
- /**
2
- * web-reach — Lightweight web search and extraction extension for Pi.
3
- *
4
- * Injects `web_search` and `web_fetch` tools by default:
5
- * 1. web_search: Keyless Exa MCP web search returning structured snippets with source URLs.
6
- * 2. web_fetch : Multi-tier web content extraction engine:
7
- * - Next.js RSC (React Server Components) static data parsing;
8
- * - Semantic HTML to Markdown conversion;
9
- * - PDF document detection and text extraction;
10
- * - Automatic fallback to Jina Reader for JS-rendered pages;
11
- * - Pretty-printed JSON formatting.
12
- */
13
-
14
- import { type ExtensionAPI } from "@earendil-works/pi-coding-agent";
15
- import { Text } from "@earendil-works/pi-tui";
16
- import { Type } from "typebox";
17
- import { searchWeb } from "./search.ts";
18
- import { fetchUrl } from "./page.ts";
19
-
20
- export { searchWeb } from "./search.ts";
21
- export { fetchUrl } from "./page.ts";
22
-
23
- export default function (pi: ExtensionAPI) {
24
- // Register web_search tool
25
- pi.registerTool({
26
- name: "web_search",
27
- label: "Web Search",
28
- description:
29
- "Search the web using Exa MCP without an API key. Returns high-quality search results with titles, source URLs, and text snippets. Ideal for real-time information, latest news, and research; use web_fetch on result URLs to inspect full pages.",
30
- parameters: Type.Object({
31
- query: Type.String({ minLength: 1, description: "Search query, as specific as possible." }),
32
- }),
33
- async execute(_toolCallId, params, signal) {
34
- const { text, provider } = await searchWeb(params.query, signal);
35
- return { content: [{ type: "text", text }], details: { provider } };
36
- },
37
- renderCall(args, theme, context) {
38
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
39
- const { query } = args as { query?: string };
40
- const display = query && query.length > 60 ? query.slice(0, 57) + "..." : query || "";
41
- text.setText(
42
- theme.fg("toolTitle", theme.bold("search ")) + theme.fg("accent", `"${display}"`),
43
- );
44
- return text;
45
- },
46
- });
47
-
48
- // Register web_fetch tool
49
- pi.registerTool({
50
- name: "web_fetch",
51
- label: "Web Fetch",
52
- description:
53
- "Intelligently fetch and extract content from a web page. Automatically extracts main content and converts to structured Markdown (supports Next.js RSC parsing, PDF extraction, semantic HTML-to-Markdown, and falls back to Jina Reader for JS-rendered pages). Suitable for in-depth reading of documentation and web pages.",
54
- parameters: Type.Object({
55
- url: Type.String({ description: "Target http(s) URL." }),
56
- mode: Type.Optional(
57
- Type.Enum(
58
- { readable: "readable", raw: "raw" },
59
- { description: "readable: intelligently extract Markdown (default); raw: return raw content." },
60
- ),
61
- ),
62
- }),
63
- async execute(_toolCallId, params, signal) {
64
- const mode = params.mode as "readable" | "raw" | undefined;
65
- const { text, title, contentType, status, engine } = await fetchUrl(params.url, { mode }, signal);
66
- const header = title ? `# ${title}\n\nSource: ${params.url} (Engine: ${engine})\n\n---\n\n` : "";
67
- return {
68
- content: [{ type: "text", text: header + text }],
69
- details: { contentType, status, engine, title, chars: text.length },
70
- };
71
- },
72
- renderCall(args, theme, context) {
73
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
74
- const { url } = args as { url?: string };
75
- const display = url && url.length > 70 ? url.slice(0, 67) + "..." : url || "";
76
- text.setText(
77
- theme.fg("toolTitle", theme.bold("fetch ")) + theme.fg("accent", display),
78
- );
79
- return text;
80
- },
81
- });
82
- }
@@ -1,132 +0,0 @@
1
- import { DEFAULT_USER_AGENT, envNum, truncate, withTimeout } from "./transport.ts";
2
- import { extractRSCContent } from "./rsc.ts";
3
- import { extractTitleFromHtml, htmlToMarkdown, isLikelyJSRendered } from "./html.ts";
4
- import { fetchViaJinaReader } from "./reader.ts";
5
- import { extractPdfBuffer, isPdfUrl } from "./pdf.ts";
6
-
7
- const MIN_USEFUL_CONTENT_LENGTH = 300;
8
-
9
- export async function fetchUrl(
10
- url: string,
11
- opts: { mode?: "readable" | "raw" } = {},
12
- signal?: AbortSignal,
13
- ): Promise<{ text: string; title: string; contentType: string; status: number; engine: string }> {
14
- const mode = opts.mode ?? "readable";
15
- const { ac, done } = withTimeout(
16
- envNum("SRP_WEB_FETCH_TIMEOUT_MS", 20_000),
17
- "Web fetch timed out",
18
- signal,
19
- );
20
-
21
- try {
22
- const res = await fetch(url, {
23
- redirect: "follow",
24
- headers: {
25
- "User-Agent": DEFAULT_USER_AGENT,
26
- Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,application/pdf,text/plain,*/*;q=0.8",
27
- },
28
- signal: ac.signal,
29
- });
30
-
31
- if (!res.ok) {
32
- throw new Error(`HTTP ${res.status} ${res.statusText}`);
33
- }
34
-
35
- const contentType = (res.headers.get("content-type") ?? "").split(";")[0].trim().toLowerCase();
36
- const maxBytes = envNum("SRP_WEB_FETCH_MAX_BYTES", 4 * 1024 * 1024);
37
- const arrayBuf = await res.arrayBuffer();
38
-
39
- if (arrayBuf.byteLength > maxBytes) {
40
- throw new Error(`Content exceeds ${Math.round((maxBytes / 1024 / 1024) * 10) / 10} MiB limit.`);
41
- }
42
-
43
- // 1. PDF extraction
44
- if (isPdfUrl(url, contentType)) {
45
- const { text, title } = await extractPdfBuffer(arrayBuf, url);
46
- return { text: truncate(text), title, contentType, status: res.status, engine: "unpdf" };
47
- }
48
-
49
- const body = Buffer.from(arrayBuf).toString("utf8");
50
-
51
- // 2. Raw mode
52
- if (mode === "raw") {
53
- return {
54
- text: truncate(body),
55
- title: extractTitleFromHtml(body) || url,
56
- contentType,
57
- status: res.status,
58
- engine: "raw",
59
- };
60
- }
61
-
62
- // 3. JSON formatting
63
- if (contentType.includes("json")) {
64
- try {
65
- const text = JSON.stringify(JSON.parse(body), null, 2);
66
- return { text: truncate(text), title: "JSON Response", contentType, status: res.status, engine: "json" };
67
- } catch {
68
- return { text: truncate(body), title: "JSON", contentType, status: res.status, engine: "text" };
69
- }
70
- }
71
-
72
- // 4. HTML processing
73
- if (contentType.includes("html") || contentType.includes("xhtml")) {
74
- // 4.1 Attempt Next.js RSC extraction
75
- const rsc = extractRSCContent(body);
76
- if (rsc && rsc.content.length >= MIN_USEFUL_CONTENT_LENGTH) {
77
- return {
78
- text: truncate(rsc.content),
79
- title: rsc.title || extractTitleFromHtml(body),
80
- contentType,
81
- status: res.status,
82
- engine: "nextjs-rsc",
83
- };
84
- }
85
-
86
- // 4.2 Semantic HTML to Markdown
87
- const title = extractTitleFromHtml(body);
88
- const markdown = htmlToMarkdown(body);
89
-
90
- if (markdown.length >= MIN_USEFUL_CONTENT_LENGTH && !isLikelyJSRendered(body)) {
91
- return {
92
- text: truncate(markdown),
93
- title,
94
- contentType,
95
- status: res.status,
96
- engine: "html-markdown",
97
- };
98
- }
99
-
100
- // 4.3 Dynamic JS page or short content: attempt Jina Reader fallback
101
- const jina = await fetchViaJinaReader(url, signal);
102
- if (jina && jina.text.length >= 100) {
103
- return {
104
- text: truncate(jina.text),
105
- title: jina.title || title,
106
- contentType: "text/markdown",
107
- status: res.status,
108
- engine: "jina-reader",
109
- };
110
- }
111
-
112
- return {
113
- text: truncate(markdown || body.slice(0, 4000)),
114
- title,
115
- contentType,
116
- status: res.status,
117
- engine: "html-fallback",
118
- };
119
- }
120
-
121
- // 5. Plain text and other types
122
- return {
123
- text: truncate(body),
124
- title: url.split("/").pop() || "Plain Text",
125
- contentType,
126
- status: res.status,
127
- engine: "text",
128
- };
129
- } finally {
130
- done();
131
- }
132
- }
@@ -1,42 +0,0 @@
1
- export function isPdfUrl(url: string, contentType?: string): boolean {
2
- if (contentType?.includes("application/pdf")) return true;
3
- try {
4
- return new URL(url).pathname.toLowerCase().endsWith(".pdf");
5
- } catch {
6
- return false;
7
- }
8
- }
9
-
10
- export async function extractPdfBuffer(
11
- buffer: ArrayBuffer,
12
- url: string,
13
- ): Promise<{ text: string; title: string }> {
14
- try {
15
- const { getDocumentProxy } = await import("unpdf");
16
- const pdf = await getDocumentProxy(new Uint8Array(buffer));
17
- const maxPages = Math.min(pdf.numPages, 80);
18
- const pages: string[] = [];
19
-
20
- for (let i = 1; i <= maxPages; i++) {
21
- const page = await pdf.getPage(i);
22
- const textContent = await page.getTextContent();
23
- const pageText = textContent.items
24
- .map((item: unknown) => (item as { str?: string }).str || "")
25
- .join(" ")
26
- .replace(/\s+/g, " ")
27
- .trim();
28
- if (pageText) pages.push(`--- Page ${i} ---\n${pageText}`);
29
- }
30
-
31
- const title = new URL(url).pathname.split("/").pop() || "PDF Document";
32
- const text = `# ${title}\n\nPages: ${pdf.numPages}\n\n${pages.join("\n\n")}`;
33
- return { text, title };
34
- } catch {
35
- // Fallback plain text extraction
36
- const text = Buffer.from(buffer).toString("latin1").replace(/[^\x20-\x7E\n\r\t]/g, " ");
37
- return {
38
- text: `# PDF Document\n\n${text.slice(0, 8000)}`,
39
- title: "PDF Document",
40
- };
41
- }
42
- }
@@ -1,42 +0,0 @@
1
- import { DEFAULT_USER_AGENT, withTimeout } from "./transport.ts";
2
-
3
- const JINA_READER_BASE = "https://r.jina.ai/";
4
-
5
- export async function fetchViaJinaReader(
6
- url: string,
7
- signal?: AbortSignal,
8
- ): Promise<{ text: string; title: string } | null> {
9
- const { ac, done } = withTimeout(30_000, "Jina Reader timed out", signal);
10
- try {
11
- const res = await fetch(`${JINA_READER_BASE}${url}`, {
12
- headers: {
13
- Accept: "text/markdown",
14
- "X-No-Cache": "true",
15
- "User-Agent": DEFAULT_USER_AGENT,
16
- },
17
- signal: ac.signal,
18
- });
19
- if (!res.ok) return null;
20
-
21
- const raw = await res.text();
22
- const contentIndex = raw.indexOf("Markdown Content:");
23
- const markdown = contentIndex >= 0 ? raw.slice(contentIndex + 17).trim() : raw.trim();
24
-
25
- if (
26
- markdown.length < 80 ||
27
- markdown.startsWith("Loading...") ||
28
- markdown.startsWith("Please enable JavaScript")
29
- ) {
30
- return null;
31
- }
32
-
33
- const titleMatch = markdown.match(/^#+\s+(.+)$/m);
34
- const title = titleMatch?.[1]?.trim() || "";
35
-
36
- return { text: markdown, title };
37
- } catch {
38
- return null;
39
- } finally {
40
- done();
41
- }
42
- }
@@ -1,161 +0,0 @@
1
- export function extractRSCContent(html: string): { title: string; content: string } | null {
2
- if (!html.includes("self.__next_f.push")) return null;
3
-
4
- const chunkMap = new Map<string, string>();
5
- const scriptRegex = /<script>self\.__next_f\.push\(\[1,"([\s\S]*?)"\]\)<\/script>/g;
6
-
7
- for (const match of html.matchAll(scriptRegex)) {
8
- let content: string;
9
- try {
10
- content = JSON.parse('"' + match[1] + '"');
11
- } catch {
12
- continue;
13
- }
14
- for (const line of content.split("\n")) {
15
- if (!line.trim()) continue;
16
- const colonIdx = line.indexOf(":");
17
- if (colonIdx <= 0 || colonIdx > 4) continue;
18
- const id = line.slice(0, colonIdx);
19
- if (!/^[0-9a-f]+$/i.test(id)) continue;
20
- const payload = line.slice(colonIdx + 1);
21
- if (!payload) continue;
22
- const existing = chunkMap.get(id);
23
- if (!existing || payload.length > existing.length) {
24
- chunkMap.set(id, payload);
25
- }
26
- }
27
- }
28
-
29
- if (chunkMap.size === 0) return null;
30
-
31
- const titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/i);
32
- const title = titleMatch?.[1]?.split("|")[0]?.trim() || "";
33
-
34
- const parsedCache = new Map<string, unknown>();
35
- function getParsedChunk(id: string): unknown | null {
36
- if (parsedCache.has(id)) return parsedCache.get(id);
37
- const chunk = chunkMap.get(id);
38
- if (!chunk || !chunk.startsWith("[")) {
39
- parsedCache.set(id, null);
40
- return null;
41
- }
42
- try {
43
- const parsed = JSON.parse(chunk);
44
- parsedCache.set(id, parsed);
45
- return parsed;
46
- } catch {
47
- parsedCache.set(id, null);
48
- return null;
49
- }
50
- }
51
-
52
- const visitedRefs = new Set<string>();
53
-
54
- function extractNode(node: unknown, ctx = { inCode: false }): string {
55
- if (node === null || node === undefined) return "";
56
- if (typeof node === "string") {
57
- const refMatch = node.match(/^\$L([0-9a-f]+)$/i);
58
- if (refMatch) {
59
- const refId = refMatch[1];
60
- if (visitedRefs.has(refId)) return "";
61
- visitedRefs.add(refId);
62
- const refNode = getParsedChunk(refId);
63
- const result = refNode ? extractNode(refNode, ctx) : "";
64
- visitedRefs.delete(refId);
65
- return result;
66
- }
67
- if (!ctx.inCode && (node === "$undefined" || node === "$" || /^\$[A-Z]/.test(node))) {
68
- return "";
69
- }
70
- return node.trim() ? node : "";
71
- }
72
- if (typeof node === "number") return String(node);
73
- if (typeof node === "boolean") return "";
74
- if (!Array.isArray(node)) return "";
75
-
76
- if (node[0] === "$" && typeof node[1] === "string") {
77
- const tag = node[1] as string;
78
- const props = (node[3] || {}) as Record<string, unknown>;
79
- const skipTags = [
80
- "script", "style", "svg", "path", "circle", "link", "meta",
81
- "template", "button", "input", "nav", "footer", "aside",
82
- ];
83
- if (skipTags.includes(tag)) return "";
84
-
85
- if (tag.startsWith("$L")) {
86
- const refId = tag.slice(2);
87
- if (visitedRefs.has(refId)) return "";
88
- if (props.baseId && props.children) {
89
- return `## ${String(props.children)}\n\n`;
90
- }
91
- visitedRefs.add(refId);
92
- const refNode = getParsedChunk(refId);
93
- let result = "";
94
- if (refNode) result = extractNode(refNode, ctx);
95
- else if (props.children) result = extractNode(props.children, ctx);
96
- visitedRefs.delete(refId);
97
- return result;
98
- }
99
-
100
- const children = props.children;
101
- const content = children ? extractNode(children, ctx) : "";
102
-
103
- switch (tag) {
104
- case "h1": return `# ${content.trim()}\n\n`;
105
- case "h2": return `## ${content.trim()}\n\n`;
106
- case "h3": return `### ${content.trim()}\n\n`;
107
- case "h4": return `#### ${content.trim()}\n\n`;
108
- case "h5": return `##### ${content.trim()}\n\n`;
109
- case "h6": return `###### ${content.trim()}\n\n`;
110
- case "p": return `${content.trim()}\n\n`;
111
- case "code": {
112
- const cc = children ? extractNode(children, { inCode: true }) : "";
113
- return ctx.inCode ? cc : `\`${cc}\``;
114
- }
115
- case "pre": {
116
- const pc = children ? extractNode(children, { inCode: true }) : "";
117
- return "```\n" + pc + "\n```\n\n";
118
- }
119
- case "strong": case "b": return `**${content}**`;
120
- case "em": case "i": return `*${content}*`;
121
- case "li": return `- ${content.trim()}\n`;
122
- case "ul": case "ol": return content + "\n";
123
- case "blockquote": return `> ${content.trim()}\n\n`;
124
- case "a": {
125
- const href = props.href as string | undefined;
126
- return href && !href.startsWith("#") ? `[${content}](${href})` : content;
127
- }
128
- default: return content;
129
- }
130
- }
131
-
132
- return (node as unknown[]).map((n) => extractNode(n, ctx)).join("");
133
- }
134
-
135
- const contentParts: { order: number; text: string }[] = [];
136
- for (const [id] of chunkMap) {
137
- const parsed = getParsedChunk(id);
138
- if (!parsed) continue;
139
- visitedRefs.clear();
140
- const text = extractNode(parsed);
141
- if (text.trim().length > 50 && !text.includes("404") && !text.includes("not found")) {
142
- contentParts.push({ order: parseInt(id, 16) || 0, text: text.trim() });
143
- }
144
- }
145
-
146
- if (contentParts.length === 0) return null;
147
- contentParts.sort((a, b) => a.order - b.order);
148
-
149
- const seen = new Set<string>();
150
- const uniqueParts: string[] = [];
151
- for (const part of contentParts) {
152
- const key = part.text.slice(0, 150);
153
- if (!seen.has(key)) {
154
- seen.add(key);
155
- uniqueParts.push(part.text);
156
- }
157
- }
158
-
159
- const content = uniqueParts.join("\n\n").replace(/\n{3,}/g, "\n\n").trim();
160
- return content.length > 100 ? { title, content } : null;
161
- }