@herbertgao/pi-extensions 2026.11.18 → 2026.11.20

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 (177) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +5 -5
  3. package/THIRD_PARTY_NOTICES.md +3 -24
  4. package/node_modules/@estebanforge/pi-antigravity-bridge/CHANGELOG.md +956 -0
  5. package/node_modules/{pi-antigravity → @estebanforge/pi-antigravity-bridge}/LICENSE +1 -1
  6. package/node_modules/@estebanforge/pi-antigravity-bridge/README.md +240 -0
  7. package/node_modules/@estebanforge/pi-antigravity-bridge/extensions/index.ts +2117 -0
  8. package/node_modules/@estebanforge/pi-antigravity-bridge/package.json +61 -0
  9. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/connection.ts +644 -0
  10. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/driver.ts +1043 -0
  11. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/events.ts +323 -0
  12. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/home.ts +145 -0
  13. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/jsonrpc.ts +243 -0
  14. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/setup.ts +107 -0
  15. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/usage-estimate.ts +61 -0
  16. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agents.ts +64 -0
  17. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-install.ts +93 -0
  18. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-paths.ts +30 -0
  19. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-setup.ts +81 -0
  20. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-version.ts +289 -0
  21. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-detect.ts +161 -0
  22. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-gate.ts +233 -0
  23. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-hook.ts +325 -0
  24. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts-ui.ts +88 -0
  25. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts.ts +144 -0
  26. package/node_modules/@estebanforge/pi-antigravity-bridge/src/ask-tool.ts +1221 -0
  27. package/node_modules/@estebanforge/pi-antigravity-bridge/src/background-watch.ts +225 -0
  28. package/node_modules/@estebanforge/pi-antigravity-bridge/src/bridge-catalog.ts +197 -0
  29. package/node_modules/@estebanforge/pi-antigravity-bridge/src/config.ts +523 -0
  30. package/node_modules/@estebanforge/pi-antigravity-bridge/src/daily-log.ts +196 -0
  31. package/node_modules/@estebanforge/pi-antigravity-bridge/src/dialog-lock.ts +48 -0
  32. package/node_modules/@estebanforge/pi-antigravity-bridge/src/diff-render.ts +207 -0
  33. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver-types.ts +168 -0
  34. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver.ts +907 -0
  35. package/node_modules/@estebanforge/pi-antigravity-bridge/src/engine-picker.ts +160 -0
  36. package/node_modules/@estebanforge/pi-antigravity-bridge/src/frame-guard.ts +38 -0
  37. package/node_modules/@estebanforge/pi-antigravity-bridge/src/handoff-toast.ts +21 -0
  38. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-registration.ts +376 -0
  39. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-server.ts +618 -0
  40. package/node_modules/@estebanforge/pi-antigravity-bridge/src/models.ts +404 -0
  41. package/node_modules/@estebanforge/pi-antigravity-bridge/src/native-tools.ts +104 -0
  42. package/node_modules/@estebanforge/pi-antigravity-bridge/src/parked-turn.ts +125 -0
  43. package/node_modules/@estebanforge/pi-antigravity-bridge/src/patch-cleanup.ts +523 -0
  44. package/node_modules/@estebanforge/pi-antigravity-bridge/src/poll-fold.ts +55 -0
  45. package/node_modules/@estebanforge/pi-antigravity-bridge/src/prompt-images.ts +92 -0
  46. package/node_modules/@estebanforge/pi-antigravity-bridge/src/provider.ts +2214 -0
  47. package/node_modules/@estebanforge/pi-antigravity-bridge/src/queued-messages.ts +152 -0
  48. package/node_modules/@estebanforge/pi-antigravity-bridge/src/quota-notice.ts +40 -0
  49. package/node_modules/@estebanforge/pi-antigravity-bridge/src/redact.ts +27 -0
  50. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sessions.ts +197 -0
  51. package/node_modules/@estebanforge/pi-antigravity-bridge/src/skills.ts +207 -0
  52. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-events.ts +161 -0
  53. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-sanitizer.ts +180 -0
  54. package/node_modules/@estebanforge/pi-antigravity-bridge/src/subagent-roster.ts +245 -0
  55. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sysprompt-agent.ts +130 -0
  56. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks-ui.ts +90 -0
  57. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks.ts +174 -0
  58. package/node_modules/@estebanforge/pi-antigravity-bridge/src/usage.ts +174 -0
  59. package/node_modules/@estebanforge/pi-antigravity-bridge/src/web-tools.ts +320 -0
  60. package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +6 -0
  61. package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +1 -1
  62. package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +3 -1
  63. package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
  64. package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +317 -105
  65. package/node_modules/@herbertgao/pi-subagents/SECURITY.md +2 -2
  66. package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +240 -36
  67. package/node_modules/@herbertgao/pi-subagents/docs/translations.md +63 -0
  68. package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +1 -1
  69. package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
  70. package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +2 -0
  71. package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +89 -6
  72. package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +6 -2
  73. package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +20 -11
  74. package/node_modules/@herbertgao/pi-subagents/src/cache-retention.ts +39 -0
  75. package/node_modules/@herbertgao/pi-subagents/src/completion-outbox.ts +5 -0
  76. package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +185 -44
  77. package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +7 -0
  78. package/node_modules/@herbertgao/pi-subagents/src/group-join.ts +9 -0
  79. package/node_modules/@herbertgao/pi-subagents/src/index.ts +126 -116
  80. package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +3 -2
  81. package/node_modules/@herbertgao/pi-subagents/src/settings.ts +21 -0
  82. package/node_modules/@herbertgao/pi-subagents/src/subagent-info.ts +115 -0
  83. package/node_modules/@herbertgao/pi-subagents/src/types.ts +11 -7
  84. package/node_modules/@herbertgao/pi-subagents/src/ui/agent-widget.ts +2 -3
  85. package/node_modules/@herbertgao/pi-subagents/src/ui/conversation-viewer.ts +1 -1
  86. package/node_modules/@herbertgao/pi-subagents/src/workflow/meta.ts +2 -2
  87. package/node_modules/@herbertgao/pi-subagents/src/workflow-info.ts +184 -0
  88. package/node_modules/pi-commandcode-provider/CHANGELOG.md +10 -0
  89. package/node_modules/pi-commandcode-provider/README.md +3 -2
  90. package/node_modules/pi-commandcode-provider/index.ts +120 -13
  91. package/node_modules/pi-commandcode-provider/package.json +4 -3
  92. package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +11 -2
  93. package/node_modules/pi-commandcode-provider/src/pricing.ts +17 -1
  94. package/node_modules/pi-commandcode-provider/src/quota-command.ts +3 -2
  95. package/node_modules/pi-commandcode-provider/src/zdr.ts +163 -0
  96. package/node_modules/pi-typesafe/README.md +1 -1
  97. package/node_modules/pi-typesafe/dist/ask.js +6 -3
  98. package/node_modules/pi-typesafe/dist/auth.d.ts +31 -7
  99. package/node_modules/pi-typesafe/dist/auth.js +159 -55
  100. package/node_modules/pi-typesafe/dist/batch.d.ts +16 -2
  101. package/node_modules/pi-typesafe/dist/batch.js +62 -22
  102. package/node_modules/pi-typesafe/dist/calibrate.d.ts +38 -11
  103. package/node_modules/pi-typesafe/dist/calibrate.js +56 -24
  104. package/node_modules/pi-typesafe/dist/client.d.ts +13 -0
  105. package/node_modules/pi-typesafe/dist/client.js +67 -27
  106. package/node_modules/pi-typesafe/dist/errors.d.ts +20 -0
  107. package/node_modules/pi-typesafe/dist/errors.js +105 -2
  108. package/node_modules/pi-typesafe/dist/extension.d.ts +106 -0
  109. package/node_modules/pi-typesafe/dist/extension.js +206 -106
  110. package/node_modules/pi-typesafe/dist/index.d.ts +8 -8
  111. package/node_modules/pi-typesafe/dist/index.js +3 -3
  112. package/node_modules/pi-typesafe/dist/key-prompt.d.ts +20 -0
  113. package/node_modules/pi-typesafe/dist/key-prompt.js +23 -3
  114. package/node_modules/pi-typesafe/dist/login.d.ts +58 -1
  115. package/node_modules/pi-typesafe/dist/login.js +167 -13
  116. package/node_modules/pi-typesafe/dist/schema.d.ts +3 -0
  117. package/node_modules/pi-typesafe/dist/schema.js +47 -13
  118. package/node_modules/pi-typesafe/dist/ui.d.ts +2 -1
  119. package/node_modules/pi-typesafe/dist/usage.d.ts +53 -0
  120. package/node_modules/pi-typesafe/dist/usage.js +79 -40
  121. package/node_modules/pi-typesafe/package.json +3 -3
  122. package/package.json +12 -15
  123. package/node_modules/pi-antigravity/README.md +0 -298
  124. package/node_modules/pi-antigravity/docs/reader-guarantees.md +0 -35
  125. package/node_modules/pi-antigravity/package.json +0 -87
  126. package/node_modules/pi-antigravity/src/auth/accounts.ts +0 -327
  127. package/node_modules/pi-antigravity/src/auth/index.ts +0 -23
  128. package/node_modules/pi-antigravity/src/auth/oauth.ts +0 -445
  129. package/node_modules/pi-antigravity/src/client/client.ts +0 -602
  130. package/node_modules/pi-antigravity/src/client/enterprise.ts +0 -150
  131. package/node_modules/pi-antigravity/src/client/index.ts +0 -2
  132. package/node_modules/pi-antigravity/src/diagnostics/diagnostics.ts +0 -104
  133. package/node_modules/pi-antigravity/src/diagnostics/index.ts +0 -1
  134. package/node_modules/pi-antigravity/src/features/generate-image.ts +0 -132
  135. package/node_modules/pi-antigravity/src/features/google-search.ts +0 -72
  136. package/node_modules/pi-antigravity/src/features/read-url-content.ts +0 -124
  137. package/node_modules/pi-antigravity/src/image/image.ts +0 -427
  138. package/node_modules/pi-antigravity/src/image/index.ts +0 -1
  139. package/node_modules/pi-antigravity/src/image/native.ts +0 -69
  140. package/node_modules/pi-antigravity/src/index.ts +0 -547
  141. package/node_modules/pi-antigravity/src/models/discovery.ts +0 -176
  142. package/node_modules/pi-antigravity/src/models/grouping.ts +0 -487
  143. package/node_modules/pi-antigravity/src/models/index.ts +0 -3
  144. package/node_modules/pi-antigravity/src/models/models.ts +0 -647
  145. package/node_modules/pi-antigravity/src/search/index.ts +0 -1
  146. package/node_modules/pi-antigravity/src/search/search.ts +0 -967
  147. package/node_modules/pi-antigravity/src/stream/index.ts +0 -1
  148. package/node_modules/pi-antigravity/src/stream/stream.ts +0 -1961
  149. package/node_modules/pi-antigravity/src/types/enums.ts +0 -52
  150. package/node_modules/pi-antigravity/src/types/index.ts +0 -2
  151. package/node_modules/pi-antigravity/src/types/types.ts +0 -329
  152. package/node_modules/pi-antigravity/src/usage/index.ts +0 -1
  153. package/node_modules/pi-antigravity/src/usage/usage.ts +0 -437
  154. package/node_modules/pi-antigravity/src/utils/address.ts +0 -180
  155. package/node_modules/pi-antigravity/src/utils/credits.ts +0 -149
  156. package/node_modules/pi-antigravity/src/utils/html.ts +0 -31
  157. package/node_modules/pi-antigravity/src/utils/http.ts +0 -96
  158. package/node_modules/pi-antigravity/src/utils/index.ts +0 -5
  159. package/node_modules/pi-antigravity/src/utils/security.ts +0 -278
  160. package/node_modules/pi-antigravity/src/utils/text.ts +0 -66
  161. package/node_modules/pi-antigravity/src/utils/util.ts +0 -217
  162. package/node_modules/pi-antigravity/src/web-fetch/document.ts +0 -83
  163. package/node_modules/pi-antigravity/src/web-fetch/fences.ts +0 -24
  164. package/node_modules/pi-antigravity/src/web-fetch/header.ts +0 -97
  165. package/node_modules/pi-antigravity/src/web-fetch/index.ts +0 -3
  166. package/node_modules/pi-antigravity/src/web-fetch/inspect.ts +0 -328
  167. package/node_modules/pi-antigravity/src/web-fetch/limits.ts +0 -4
  168. package/node_modules/pi-antigravity/src/web-fetch/markdown.ts +0 -744
  169. package/node_modules/pi-antigravity/src/web-fetch/mdn.ts +0 -164
  170. package/node_modules/pi-antigravity/src/web-fetch/media.ts +0 -11
  171. package/node_modules/pi-antigravity/src/web-fetch/metadata.ts +0 -9
  172. package/node_modules/pi-antigravity/src/web-fetch/representation.ts +0 -93
  173. package/node_modules/pi-antigravity/src/web-fetch/save.ts +0 -71
  174. package/node_modules/pi-antigravity/src/web-fetch/sites.ts +0 -138
  175. package/node_modules/pi-antigravity/src/web-fetch/vendor.d.ts +0 -9
  176. package/node_modules/pi-antigravity/src/web-fetch/web-fetch.ts +0 -957
  177. package/node_modules/pi-antigravity/tsconfig.json +0 -21
@@ -1,164 +0,0 @@
1
- import { parseDocument } from "./document.js";
2
- import { cleanText } from "../utils/text.js";
3
- import { METADATA_LIMITS } from "./metadata.js";
4
- import { isRecord } from "../utils/util.js";
5
- import type { PageInspection } from "./inspect.js";
6
-
7
- /** MDN's observed article-data representation; no cross-origin or guessed source-repo mapping. */
8
- export function mdnRepresentationUrl(articleUrl: string): string | undefined {
9
- const url = new URL(articleUrl);
10
- if (
11
- url.origin !== "https://developer.mozilla.org" ||
12
- url.search ||
13
- !/^\/[a-z]{2,3}(?:-[a-z0-9]{2,8})*\/docs\/.+/i.test(url.pathname) ||
14
- /\.(?:json|md|txt)\/?$/i.test(url.pathname)
15
- )
16
- return undefined;
17
- url.pathname = url.pathname.replace(/\/$/, "") + "/index.json";
18
- url.hash = "";
19
- return url.href;
20
- }
21
-
22
- /**
23
- * Read only verified MDN article data. Google's copy can end inside sidebarHTML:
24
- * in that case parse the strictly valid document prefix before that property.
25
- * Never repair an unfinished article body or synthesize missing section content.
26
- */
27
- export function inspectMdnRepresentation(
28
- content: string,
29
- articleUrl: string,
30
- ):
31
- | {
32
- html: string;
33
- resolvedUrl: string;
34
- completeness: PageInspection["completeness"];
35
- metadata: PageInspection["metadata"];
36
- notes: string[];
37
- }
38
- | undefined {
39
- if (!mdnRepresentationUrl(articleUrl) || content.length > 2_000_000) return undefined;
40
- let value: unknown;
41
- let completeness: PageInspection["completeness"] = "unknown";
42
- try {
43
- value = JSON.parse(content);
44
- } catch {
45
- // This schema places body and mdn_url before the large, unrelated sidebar.
46
- const sidebar = /,\s*"sidebarHTML"\s*:/.exec(content);
47
- if (!sidebar) return undefined;
48
- try {
49
- value = JSON.parse(content.slice(0, sidebar.index) + "}}");
50
- completeness = "incomplete";
51
- } catch {
52
- return undefined;
53
- }
54
- }
55
- if (!isRecord(value) || !isRecord(value.doc)) return undefined;
56
- const doc = value.doc;
57
- const pageTitle =
58
- typeof doc.title === "string"
59
- ? doc.title
60
- : typeof doc.short_title === "string"
61
- ? doc.short_title
62
- : undefined;
63
- if (
64
- typeof doc.mdn_url !== "string" ||
65
- !Array.isArray(doc.body) ||
66
- !doc.body.length ||
67
- pageTitle === undefined
68
- )
69
- return undefined;
70
- const expected = new URL(articleUrl);
71
- let identity: URL;
72
- try {
73
- identity = new URL(doc.mdn_url, expected);
74
- } catch {
75
- return undefined;
76
- }
77
- if (
78
- identity.username ||
79
- identity.password ||
80
- identity.origin !== expected.origin ||
81
- !mdnRepresentationUrl(identity.href) ||
82
- !identity.pathname.startsWith(expected.pathname.match(/^\/[^/]+\/docs\//)![0]) ||
83
- identity.search ||
84
- identity.hash
85
- )
86
- return undefined;
87
- const document = parseDocument("<html><body><main></main></body></html>");
88
- const notes: string[] = [];
89
- const omitted = new Set<string>();
90
- const main = document.querySelector("main")!;
91
- const title = document.createElement("h1");
92
- title.textContent = pageTitle;
93
- main.appendChild(title);
94
- for (const section of doc.body) {
95
- if (!isRecord(section) || !isRecord(section.value)) {
96
- omitted.add("unrecognized");
97
- continue;
98
- }
99
- const data = section.value;
100
- if (section.type !== "prose" && section.type !== "specifications") {
101
- omitted.add(cleanText(typeof section.type === "string" ? section.type : "unrecognized", 60)!);
102
- continue;
103
- }
104
- if (typeof data.title === "string" && data.title) {
105
- const heading = document.createElement(data.isH3 === true ? "h3" : "h2");
106
- heading.textContent = data.title;
107
- main.appendChild(heading);
108
- }
109
- if (section.type === "prose") {
110
- if (typeof data.content !== "string") return undefined;
111
- const block = document.createElement("div");
112
- const sectionDocument = parseDocument(data.content);
113
- for (const child of Array.from(sectionDocument.body.childNodes))
114
- block.appendChild(document.importNode(child, true));
115
- main.appendChild(block);
116
- } else {
117
- if (!Array.isArray(data.specifications)) return undefined;
118
- const list = document.createElement("ul");
119
- for (const spec of data.specifications) {
120
- if (
121
- !isRecord(spec) ||
122
- typeof spec.title !== "string" ||
123
- typeof spec.bcdSpecificationURL !== "string"
124
- )
125
- return undefined;
126
- let href: URL;
127
- try {
128
- href = new URL(spec.bcdSpecificationURL, expected);
129
- } catch {
130
- return undefined;
131
- }
132
- if (href.protocol !== "https:" && href.protocol !== "http:") return undefined;
133
- const item = document.createElement("li");
134
- const link = document.createElement("a");
135
- link.href = href.href;
136
- link.textContent = spec.title;
137
- item.appendChild(link);
138
- list.appendChild(item);
139
- }
140
- main.appendChild(list);
141
- }
142
- }
143
- if (omitted.size) {
144
- completeness = "incomplete";
145
- notes.push(`Publisher sections omitted: ${[...omitted].join(", ")}.`);
146
- }
147
- return {
148
- html: document.documentElement.outerHTML,
149
- notes,
150
- resolvedUrl: identity.href,
151
- completeness,
152
- metadata: {
153
- title: cleanText(pageTitle, METADATA_LIMITS.title),
154
- updated: cleanText(
155
- typeof doc.modified === "string" ? doc.modified : undefined,
156
- METADATA_LIMITS.updated,
157
- ),
158
- language: cleanText(
159
- typeof doc.locale === "string" ? doc.locale : undefined,
160
- METADATA_LIMITS.language,
161
- ),
162
- },
163
- };
164
- }
@@ -1,11 +0,0 @@
1
- /** Media types have one owner for detection and PDF refusal. */
2
- export type MediaKind = "html" | "text" | "json" | "xml" | "pdf" | "unknown";
3
- export function mediaKind(value?: string): MediaKind {
4
- const type = value?.split(";")[0].trim().toLowerCase() ?? "";
5
- if (type === "text/html" || type === "application/xhtml+xml") return "html";
6
- if (type === "application/pdf") return "pdf";
7
- if (type === "application/json" || type.endsWith("+json")) return "json";
8
- if (type === "text/xml" || type === "application/xml" || type.endsWith("+xml")) return "xml";
9
- if (["text/plain", "text/markdown", "text/x-markdown"].includes(type)) return "text";
10
- return "unknown";
11
- }
@@ -1,9 +0,0 @@
1
- /** Page-controlled field limits, shared by extraction and display. */
2
- export const METADATA_LIMITS = {
3
- title: 300,
4
- description: 1_000,
5
- updated: 100,
6
- author: 300,
7
- published: 100,
8
- language: 50,
9
- } as const;
@@ -1,93 +0,0 @@
1
- /**
2
- * A fuller copy of a page that its publisher serves at another URL.
3
- *
4
- * Retrieval does not care which kind it has: it fetches `url`, passes the response to
5
- * `read`, and inspects the document that comes back. Each kind owns its identity
6
- * checks, so a copy is used only when it names the requested page, and never rebuilt
7
- * from partial data.
8
- */
9
- import { articleRepresentation, type PageInspection } from "./inspect.js";
10
- import { inspectMdnRepresentation, mdnRepresentationUrl } from "./mdn.js";
11
- import { mediaKind } from "./media.js";
12
- import { siteRepresentation } from "./sites.js";
13
-
14
- export type RepresentationDocument = {
15
- /** HTML or text to inspect. */
16
- body: string;
17
- /** Media type of `body`; unset when it is HTML decoded out of a JSON envelope. */
18
- contentType?: string;
19
- /** The article this document is for, when the publisher names a canonical URL. */
20
- articleUrl?: string;
21
- /** Set when the representation itself proves the document whole or cut off. */
22
- completeness?: PageInspection["completeness"];
23
- notes?: string[];
24
- metadata?: PageInspection["metadata"];
25
- language?: string;
26
- };
27
-
28
- export type Representation = {
29
- url: string;
30
- /**
31
- * Resolve Google's redirect for `url` first. Links a page advertises need it, so they
32
- * cannot leave the page's origin; a URL built from the page's own address does not.
33
- */
34
- resolve: boolean;
35
- /** Language the publisher declared for this copy. */
36
- language?: string;
37
- /** The document in a response, or undefined when it is not the expected one. */
38
- read(
39
- content: string,
40
- reportedType: string | undefined,
41
- articleUrl: string,
42
- ): RepresentationDocument | undefined;
43
- };
44
-
45
- type Advertised = PageInspection["alternates"][number];
46
-
47
- function advertised(alternate: Advertised): Representation {
48
- return {
49
- url: alternate.url,
50
- resolve: true,
51
- language: alternate.language,
52
- read(content, reportedType, articleUrl) {
53
- const json =
54
- mediaKind(reportedType) === "json" ||
55
- (alternate.type === "application/json" && /^\s*[{[]/.test(content));
56
- if (!json) return { body: content, contentType: reportedType };
57
- const body = articleRepresentation(content, articleUrl);
58
- return body === undefined ? undefined : { body };
59
- },
60
- };
61
- }
62
-
63
- function mdn(url: string): Representation {
64
- return {
65
- url,
66
- resolve: true,
67
- read(content, _reportedType, articleUrl) {
68
- const article = inspectMdnRepresentation(content, articleUrl);
69
- return (
70
- article && {
71
- body: article.html,
72
- articleUrl: article.resolvedUrl,
73
- completeness: article.completeness,
74
- notes: article.notes,
75
- metadata: article.metadata,
76
- language: article.metadata.language,
77
- }
78
- );
79
- },
80
- };
81
- }
82
-
83
- /** The copy to try when the page itself is cut off or unavailable. */
84
- export function representationFor(
85
- articleUrl: string,
86
- alternate?: Advertised,
87
- ): Representation | undefined {
88
- if (alternate) return advertised(alternate);
89
- const site = siteRepresentation(articleUrl);
90
- if (site) return site;
91
- const url = mdnRepresentationUrl(articleUrl);
92
- return url ? mdn(url) : undefined;
93
- }
@@ -1,71 +0,0 @@
1
- /**
2
- * Keep a page too long for one answer on disk, the way Pi's own tools keep long output:
3
- * the caller shows the beginning and points to the file for the rest.
4
- *
5
- * Saving is a convenience around a read that already succeeded, so it never throws: a
6
- * failure becomes part of the answer, and the page that was read is not lost with it.
7
- */
8
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
9
- import { tmpdir } from "node:os";
10
- import { basename, dirname, join } from "node:path";
11
- import { redactSecrets } from "../utils/security.js";
12
- import { cleanText } from "../utils/text.js";
13
-
14
- export type SavedPage = { ok: true; path: string } | { ok: false; reason: string };
15
-
16
- const DIRECTORY_PREFIX = "pi-url-";
17
-
18
- /** Write text to a new private temp directory. */
19
- export async function savePage(text: string, extension: "md" | "html" | "txt"): Promise<SavedPage> {
20
- let directory: string | undefined;
21
- try {
22
- directory = await mkdtemp(join(tmpdir(), DIRECTORY_PREFIX));
23
- const path = join(directory, `page.${extension}`);
24
- await writeFile(path, text, { encoding: "utf8", mode: 0o600 });
25
- return { ok: true, path };
26
- } catch (error) {
27
- // A failed write can leave a partial page. Preserve the write error if cleanup fails.
28
- if (directory) await rm(directory, { recursive: true, force: true }).catch(() => {});
29
- const reason = error instanceof Error ? error.message : String(error);
30
- return { ok: false, reason: cleanText(redactSecrets(reason), 200) ?? "unknown error" };
31
- }
32
- }
33
-
34
- /** Remove a page that was saved for an answer that then could not be given. */
35
- export async function discardSavedPage(saved: SavedPage): Promise<void> {
36
- if (!saved.ok) return;
37
- const directory = dirname(saved.path);
38
- // Callers pass only results from savePage; guard against deleting an unrelated path.
39
- if (
40
- !basename(directory).startsWith(DIRECTORY_PREFIX) ||
41
- !basename(saved.path).startsWith("page.")
42
- )
43
- return;
44
- await rm(directory, { recursive: true, force: true });
45
- }
46
-
47
- const count = (chars: number) => chars.toLocaleString("en-US");
48
-
49
- /** Footer naming the saved page, or saying why there is none. */
50
- export function savedPageNote(
51
- saved: SavedPage,
52
- chars: number,
53
- next = "Read the rest with your file tools.",
54
- ): string {
55
- return saved.ok
56
- ? `\n\n[Full page saved to: ${saved.path} (${count(chars)} characters). ${next}]`
57
- : `\n\n[The full page (${count(chars)} characters) could not be saved: ${saved.reason}. Only the beginning is shown.]`;
58
- }
59
-
60
- /** Error whose message points to Google's unconverted copy, saved for searching. */
61
- export async function withSavedCopy(error: unknown): Promise<unknown> {
62
- const copy = (error as { unconvertedCopy?: string } | undefined)?.unconvertedCopy;
63
- if (!(error instanceof Error) || !copy) return error;
64
- const saved = await savePage(copy, "html");
65
- const where = saved.ok
66
- ? `was saved to: ${saved.path}. Search it with your file tools.`
67
- : `could not be saved: ${saved.reason}.`;
68
- return new Error(
69
- `${error.message} Google's unconverted HTML (${count(copy.length)} characters) ${where}`,
70
- );
71
- }
@@ -1,138 +0,0 @@
1
- /**
2
- * Fuller same-publisher copies for sites whose ordinary pages Google cuts short.
3
- *
4
- * Google's copy of a Wikipedia article can stop in its header, and raw GitHub files stop
5
- * after a third or so of the file. Each site publishes the same document in another
6
- * form that Google returns whole. Like the MDN recovery, a copy is used only after its
7
- * identity is verified against the requested page, and never reconstructed.
8
- */
9
- import { isRecord } from "../utils/util.js";
10
- import type { Representation } from "./representation.js";
11
-
12
- function decodedPath(value: string): string | undefined {
13
- try {
14
- return decodeURIComponent(value).replace(/ /g, "_");
15
- } catch {
16
- return undefined;
17
- }
18
- }
19
-
20
- /** MediaWiki capitalizes the first letter of a title, so `unix_philosophy` is `Unix philosophy`. */
21
- function sameTitle(a: string, b: string): boolean {
22
- const normal = (title: string) => {
23
- const spaced = title.replace(/ /g, "_");
24
- return spaced.charAt(0).toUpperCase() + spaced.slice(1);
25
- };
26
- return normal(a) === normal(b);
27
- }
28
-
29
- function escapeHtml(text: string): string {
30
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
31
- }
32
-
33
- /**
34
- * Wikipedia's parse API for `https://<lang>.wikipedia.org/wiki/<title>`. Its JSON
35
- * names the article, and a JSON document that parses is not cut off.
36
- */
37
- function wikipedia(url: URL): Representation | undefined {
38
- const segment = /^\/wiki\/(.+)$/.exec(url.pathname)?.[1];
39
- if (!/^[a-z0-9-]+\.wikipedia\.org$/.test(url.hostname) || url.search || !segment)
40
- return undefined;
41
- const title = decodedPath(segment);
42
- if (!title || /^Special:/i.test(title)) return undefined;
43
- const api = new URL("/w/api.php", url.origin);
44
- api.search = new URLSearchParams({
45
- action: "parse",
46
- page: title,
47
- prop: "text",
48
- format: "json",
49
- formatversion: "2",
50
- redirects: "1",
51
- disableeditsection: "1",
52
- disabletoc: "1",
53
- }).toString();
54
- return {
55
- url: api.href,
56
- resolve: false,
57
- read(content) {
58
- let data: unknown;
59
- try {
60
- data = JSON.parse(content);
61
- } catch {
62
- return undefined;
63
- }
64
- const parse = isRecord(data) && isRecord(data.parse) ? data.parse : undefined;
65
- if (!parse || typeof parse.title !== "string" || typeof parse.text !== "string")
66
- return undefined;
67
- const names = [
68
- parse.title,
69
- ...(Array.isArray(parse.redirects)
70
- ? parse.redirects.map((item) => (isRecord(item) ? item.from : undefined))
71
- : []),
72
- ];
73
- // The article must be the requested one, directly or through its redirect.
74
- if (!names.some((name) => typeof name === "string" && sameTitle(name, title)))
75
- return undefined;
76
- const heading = escapeHtml(parse.title);
77
- return {
78
- body: `<!doctype html><html><head><title>${heading}</title></head><body><main><h1>${heading}</h1>${parse.text}</main></body></html>`,
79
- contentType: "text/html",
80
- completeness: "complete",
81
- };
82
- },
83
- };
84
- }
85
-
86
- /** GitHub's file page for `https://raw.githubusercontent.com/<owner>/<repo>/<ref>/<path>`. */
87
- function githubRaw(url: URL): Representation | undefined {
88
- const parts = url.pathname.split("/").slice(1);
89
- if (url.hostname !== "raw.githubusercontent.com" || url.search || parts.length < 4)
90
- return undefined;
91
- if (parts.some((part) => !part)) return undefined;
92
- const [owner, repository, ...rest] = parts;
93
- const blob = `https://github.com/${owner}/${repository}/blob/${rest.join("/")}`;
94
- return {
95
- url: blob,
96
- resolve: false,
97
- read(content) {
98
- const embedded =
99
- /<script type="application\/json" data-target="react-app\.embeddedData">([\s\S]*?)<\/script>/.exec(
100
- content,
101
- )?.[1];
102
- if (!embedded) return undefined;
103
- let data: unknown;
104
- try {
105
- data = JSON.parse(embedded);
106
- } catch {
107
- return undefined;
108
- }
109
- const payload = isRecord(data) && isRecord(data.payload) ? data.payload : undefined;
110
- const route =
111
- payload && isRecord(payload.codeViewBlobLayoutRoute)
112
- ? payload.codeViewBlobLayoutRoute
113
- : undefined;
114
- const styled = payload?.["codeViewBlobLayoutRoute.StyledBlob"];
115
- const file = route && isRecord(route.blob) ? route.blob : undefined;
116
- const lines = isRecord(styled) ? styled.rawLines : undefined;
117
- // The file page must name the requested file, and carry all of its lines.
118
- const requested = `/${rest.join("/")}`;
119
- if (
120
- !route ||
121
- !file ||
122
- typeof route.path !== "string" ||
123
- !requested.endsWith(`/${route.path}`) ||
124
- file.truncated !== false ||
125
- !Array.isArray(lines) ||
126
- !lines.every((line) => typeof line === "string")
127
- )
128
- return undefined;
129
- return { body: lines.join("\n"), contentType: "text/plain", completeness: "complete" };
130
- },
131
- };
132
- }
133
-
134
- /** A fuller copy the publisher serves for this page, when one is known. */
135
- export function siteRepresentation(articleUrl: string): Representation | undefined {
136
- const url = new URL(articleUrl);
137
- return wikipedia(url) ?? githubRaw(url);
138
- }
@@ -1,9 +0,0 @@
1
- // These packages ship no usable declarations for their public module names.
2
- declare module "@mixmark-io/domino" {
3
- export function createDocument(html: string): Document;
4
- }
5
-
6
- declare module "turndown-plugin-gfm" {
7
- import type TurndownService from "turndown";
8
- export const tables: TurndownService.Plugin;
9
- }