@undefineds.co/linx 0.3.5 → 0.3.8

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 (172) hide show
  1. package/README.md +58 -23
  2. package/dist/generated/version.js +1 -1
  3. package/dist/generated/version.js.map +1 -1
  4. package/dist/index.js +336 -162
  5. package/dist/index.js.map +1 -1
  6. package/dist/lib/account-session.js +4 -8
  7. package/dist/lib/account-session.js.map +1 -1
  8. package/dist/lib/ai-command.js +228 -178
  9. package/dist/lib/ai-command.js.map +1 -1
  10. package/dist/lib/auto-mode/archive.js +38 -7
  11. package/dist/lib/auto-mode/archive.js.map +1 -1
  12. package/dist/lib/auto-mode/auth.js.map +1 -1
  13. package/dist/lib/auto-mode/display.js +71 -45
  14. package/dist/lib/auto-mode/display.js.map +1 -1
  15. package/dist/lib/auto-mode/format.js +9 -7
  16. package/dist/lib/auto-mode/format.js.map +1 -1
  17. package/dist/lib/auto-mode/hooks/claude.js +12 -2
  18. package/dist/lib/auto-mode/hooks/claude.js.map +1 -1
  19. package/dist/lib/auto-mode/hooks/codex.js +17 -7
  20. package/dist/lib/auto-mode/hooks/codex.js.map +1 -1
  21. package/dist/lib/auto-mode/hooks/index.js +28 -8
  22. package/dist/lib/auto-mode/hooks/index.js.map +1 -1
  23. package/dist/lib/auto-mode/pod-ai.js +20 -37
  24. package/dist/lib/auto-mode/pod-ai.js.map +1 -1
  25. package/dist/lib/auto-mode/pod-approval.js +124 -195
  26. package/dist/lib/auto-mode/pod-approval.js.map +1 -1
  27. package/dist/lib/auto-mode/pod-persistence.js +169 -90
  28. package/dist/lib/auto-mode/pod-persistence.js.map +1 -1
  29. package/dist/lib/auto-mode/runner.js +683 -81
  30. package/dist/lib/auto-mode/runner.js.map +1 -1
  31. package/dist/lib/auto-mode/secretary.js +186 -41
  32. package/dist/lib/auto-mode/secretary.js.map +1 -1
  33. package/dist/lib/auto-mode-command.js +32 -32
  34. package/dist/lib/auto-mode-command.js.map +1 -1
  35. package/dist/lib/chat-api.js +242 -50
  36. package/dist/lib/chat-api.js.map +1 -1
  37. package/dist/lib/codex-plugin/bridge.js +164 -17
  38. package/dist/lib/codex-plugin/bridge.js.map +1 -1
  39. package/dist/lib/codex-plugin/codex-native-proxy.js +370 -34
  40. package/dist/lib/codex-plugin/codex-native-proxy.js.map +1 -1
  41. package/dist/lib/credentials-store.js +33 -42
  42. package/dist/lib/credentials-store.js.map +1 -1
  43. package/dist/lib/linx-cloud-errors.js +61 -0
  44. package/dist/lib/linx-cloud-errors.js.map +1 -0
  45. package/dist/lib/linx-tui-contract.js +8 -5
  46. package/dist/lib/linx-tui-contract.js.map +1 -1
  47. package/dist/lib/login-command.js +9 -2
  48. package/dist/lib/login-command.js.map +1 -1
  49. package/dist/lib/models.js +3 -20
  50. package/dist/lib/models.js.map +1 -1
  51. package/dist/lib/oidc-auth.js +143 -17
  52. package/dist/lib/oidc-auth.js.map +1 -1
  53. package/dist/lib/oidc-session-storage.js +2 -6
  54. package/dist/lib/oidc-session-storage.js.map +1 -1
  55. package/dist/lib/pi-adapter/auto-input-controller.js +988 -0
  56. package/dist/lib/pi-adapter/auto-input-controller.js.map +1 -0
  57. package/dist/lib/pi-adapter/backend-command.js +2 -0
  58. package/dist/lib/pi-adapter/backend-command.js.map +1 -0
  59. package/dist/lib/pi-adapter/backend-credentials.js +80 -0
  60. package/dist/lib/pi-adapter/backend-credentials.js.map +1 -0
  61. package/dist/lib/pi-adapter/branding.js +246 -108
  62. package/dist/lib/pi-adapter/branding.js.map +1 -1
  63. package/dist/lib/pi-adapter/control-state.js +72 -0
  64. package/dist/lib/pi-adapter/control-state.js.map +1 -0
  65. package/dist/lib/pi-adapter/interactive.js +2634 -30
  66. package/dist/lib/pi-adapter/interactive.js.map +1 -1
  67. package/dist/lib/pi-adapter/pod-approval.js +382 -210
  68. package/dist/lib/pi-adapter/pod-approval.js.map +1 -1
  69. package/dist/lib/pi-adapter/pod-mirror-mapping.js +71 -17
  70. package/dist/lib/pi-adapter/pod-mirror-mapping.js.map +1 -1
  71. package/dist/lib/pi-adapter/pod-mirror.js +531 -64
  72. package/dist/lib/pi-adapter/pod-mirror.js.map +1 -1
  73. package/dist/lib/pi-adapter/pod-native.js +81 -85
  74. package/dist/lib/pi-adapter/pod-native.js.map +1 -1
  75. package/dist/lib/pi-adapter/pod-status-output.js +54 -0
  76. package/dist/lib/pi-adapter/pod-status-output.js.map +1 -0
  77. package/dist/lib/pi-adapter/runtime.js +458 -228
  78. package/dist/lib/pi-adapter/runtime.js.map +1 -1
  79. package/dist/lib/pi-adapter/session-control.js +509 -0
  80. package/dist/lib/pi-adapter/session-control.js.map +1 -0
  81. package/dist/lib/pi-adapter/session.js +35 -22
  82. package/dist/lib/pi-adapter/session.js.map +1 -1
  83. package/dist/lib/pi-adapter/stream.js +89 -32
  84. package/dist/lib/pi-adapter/stream.js.map +1 -1
  85. package/dist/lib/pi-adapter/sync-recovery.js +89 -0
  86. package/dist/lib/pi-adapter/sync-recovery.js.map +1 -0
  87. package/dist/lib/pi-adapter/web-fetch.js +13 -14
  88. package/dist/lib/pi-adapter/web-fetch.js.map +1 -1
  89. package/dist/lib/pod-chat-store.js +254 -78
  90. package/dist/lib/pod-chat-store.js.map +1 -1
  91. package/dist/lib/pod-data-session.js +156 -35
  92. package/dist/lib/pod-data-session.js.map +1 -1
  93. package/dist/lib/solid-auth-store.js +27 -0
  94. package/dist/lib/solid-auth-store.js.map +1 -0
  95. package/dist/lib/solid-auth.js +2 -4
  96. package/dist/lib/solid-auth.js.map +1 -1
  97. package/dist/lib/solid-client-credentials-login.js +100 -0
  98. package/dist/lib/solid-client-credentials-login.js.map +1 -0
  99. package/dist/lib/solid-local-store.js +31 -0
  100. package/dist/lib/solid-local-store.js.map +1 -0
  101. package/dist/lib/symphony/archive.js +328 -18
  102. package/dist/lib/symphony/archive.js.map +1 -1
  103. package/dist/lib/symphony/pod-projection.js +2222 -0
  104. package/dist/lib/symphony/pod-projection.js.map +1 -0
  105. package/dist/lib/symphony-command.js +602 -178
  106. package/dist/lib/symphony-command.js.map +1 -1
  107. package/dist/lib/sync-checkpoint-store.js +74 -0
  108. package/dist/lib/sync-checkpoint-store.js.map +1 -0
  109. package/dist/skills/symphony/SKILL.md +665 -0
  110. package/package.json +15 -9
  111. package/vendor/agent-runtime/dist/agent-runtime.d.ts +137 -0
  112. package/vendor/agent-runtime/dist/agent-runtime.js +211 -0
  113. package/vendor/agent-runtime/dist/auto-mode.d.ts +78 -13
  114. package/vendor/agent-runtime/dist/auto-mode.js +288 -31
  115. package/vendor/agent-runtime/dist/control-plane.d.ts +28 -0
  116. package/vendor/agent-runtime/dist/control-plane.js +79 -0
  117. package/vendor/agent-runtime/dist/file-sync.d.ts +157 -0
  118. package/vendor/agent-runtime/dist/file-sync.js +314 -0
  119. package/vendor/agent-runtime/dist/index.d.ts +7 -0
  120. package/vendor/agent-runtime/dist/index.js +7 -0
  121. package/vendor/agent-runtime/dist/reconciler.d.ts +117 -0
  122. package/vendor/agent-runtime/dist/reconciler.js +361 -0
  123. package/vendor/agent-runtime/dist/symphony.d.ts +128 -8
  124. package/vendor/agent-runtime/dist/symphony.js +362 -57
  125. package/vendor/agent-runtime/dist/sync.d.ts +271 -0
  126. package/vendor/agent-runtime/dist/sync.js +550 -0
  127. package/vendor/agent-runtime/dist/thread-reconciler-controller.d.ts +58 -0
  128. package/vendor/agent-runtime/dist/thread-reconciler-controller.js +137 -0
  129. package/vendor/agent-runtime/dist/turn-controller.js +2 -2
  130. package/vendor/agent-runtime/dist/wake-scheduler.d.ts +67 -0
  131. package/vendor/agent-runtime/dist/wake-scheduler.js +194 -0
  132. package/vendor/agent-runtime/package.json +8 -1
  133. package/vendor/pi-web-access/CHANGELOG.md +387 -0
  134. package/vendor/pi-web-access/LICENSE +21 -0
  135. package/vendor/pi-web-access/README.md +352 -0
  136. package/vendor/pi-web-access/activity.ts +101 -0
  137. package/vendor/pi-web-access/banner.png +0 -0
  138. package/vendor/pi-web-access/chrome-cookies.ts +322 -0
  139. package/vendor/pi-web-access/code-search.ts +107 -0
  140. package/vendor/pi-web-access/curator-page.ts +3359 -0
  141. package/vendor/pi-web-access/curator-server.ts +605 -0
  142. package/vendor/pi-web-access/exa.ts +520 -0
  143. package/vendor/pi-web-access/extract.ts +641 -0
  144. package/vendor/pi-web-access/gemini-api.ts +112 -0
  145. package/vendor/pi-web-access/gemini-search.ts +361 -0
  146. package/vendor/pi-web-access/gemini-url-context.ts +126 -0
  147. package/vendor/pi-web-access/gemini-web-config.ts +52 -0
  148. package/vendor/pi-web-access/gemini-web.ts +396 -0
  149. package/vendor/pi-web-access/github-api.ts +196 -0
  150. package/vendor/pi-web-access/github-extract.ts +634 -0
  151. package/vendor/pi-web-access/index.ts +2346 -0
  152. package/vendor/pi-web-access/package.json +45 -0
  153. package/vendor/pi-web-access/pdf-extract.ts +192 -0
  154. package/vendor/pi-web-access/perplexity.ts +195 -0
  155. package/vendor/pi-web-access/pi-web-fetch-demo.mp4 +0 -0
  156. package/vendor/pi-web-access/rsc-extract.ts +338 -0
  157. package/vendor/pi-web-access/skills/librarian/SKILL.md +195 -0
  158. package/vendor/pi-web-access/storage.ts +72 -0
  159. package/vendor/pi-web-access/summary-review.ts +276 -0
  160. package/vendor/pi-web-access/test/gemini-web-cookie-opt-in.test.mjs +41 -0
  161. package/vendor/pi-web-access/test/pdf-extract.test.mjs +95 -0
  162. package/vendor/pi-web-access/utils.ts +44 -0
  163. package/vendor/pi-web-access/video-extract.ts +378 -0
  164. package/vendor/pi-web-access/youtube-extract.ts +310 -0
  165. package/dist/lib/pi-adapter/auth.js +0 -68
  166. package/dist/lib/pi-adapter/auth.js.map +0 -1
  167. package/dist/lib/pi-adapter/pod-tools.js +0 -140
  168. package/dist/lib/pi-adapter/pod-tools.js.map +0 -1
  169. package/dist/skills/drizzle-solid/SKILL.md +0 -340
  170. package/dist/skills/pod-storage/SKILL.md +0 -100
  171. package/dist/skills/solid-modeling/SKILL.md +0 -274
  172. package/dist/skills/xpod-componentsjs/SKILL.md +0 -284
@@ -0,0 +1,396 @@
1
+ import { basename } from "node:path";
2
+ import { type CookieMap, getGoogleCookies } from "./chrome-cookies.js";
3
+ import { getChromeProfileFromConfig, isBrowserCookieAccessAllowed, normalizeChromeProfile } from "./gemini-web-config.ts";
4
+
5
+ const GEMINI_APP_URL = "https://gemini.google.com/app";
6
+ const GEMINI_STREAM_GENERATE_URL =
7
+ "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate";
8
+ const GEMINI_UPLOAD_URL = "https://content-push.googleapis.com/upload";
9
+ const GEMINI_UPLOAD_PUSH_ID = "feeds/mcudyrk2a4khkz";
10
+ const GOOGLE_LIST_ACCOUNTS_URL =
11
+ "https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&laf=b64bin&json=standard";
12
+
13
+ const USER_AGENT =
14
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
15
+
16
+ const MODEL_HEADER_NAME = "x-goog-ext-525001261-jspb";
17
+ const MODEL_HEADERS: Record<string, string> = {
18
+ "gemini-3-pro": '[1,null,null,null,"9d8ca3786ebdfbea",null,null,0,[4]]',
19
+ "gemini-2.5-pro": '[1,null,null,null,"4af6c7f5da75d65d",null,null,0,[4]]',
20
+ "gemini-2.5-flash": '[1,null,null,null,"9ec249fc9ad08861",null,null,0,[4]]',
21
+ };
22
+
23
+ const REQUIRED_COOKIES = ["__Secure-1PSID", "__Secure-1PSIDTS"];
24
+
25
+ export interface GeminiWebOptions {
26
+ youtubeUrl?: string;
27
+ model?: string;
28
+ files?: string[];
29
+ signal?: AbortSignal;
30
+ timeoutMs?: number;
31
+ }
32
+
33
+ export async function isGeminiWebAvailable(chromeProfile?: string): Promise<CookieMap | null> {
34
+ if (!isBrowserCookieAccessAllowed()) return null;
35
+
36
+ const result = await getGoogleCookies({
37
+ profile: normalizeChromeProfile(chromeProfile) ?? getChromeProfileFromConfig(),
38
+ requiredCookies: REQUIRED_COOKIES,
39
+ });
40
+ if (!result) return null;
41
+ return result.cookies;
42
+ }
43
+
44
+ export async function getActiveGoogleEmail(cookies: CookieMap): Promise<string | null> {
45
+ const cookieHeader = buildCookieHeader(cookies);
46
+ if (!cookieHeader) return null;
47
+
48
+ try {
49
+ const html = await fetchWithCookieRedirects(
50
+ GEMINI_APP_URL,
51
+ cookieHeader,
52
+ 10,
53
+ AbortSignal.timeout(10000),
54
+ );
55
+ const email = extractEmailFromGeminiHtml(html);
56
+ if (email) return email;
57
+ } catch {
58
+ }
59
+
60
+ try {
61
+ const response = await fetchWithCookieRedirects(
62
+ GOOGLE_LIST_ACCOUNTS_URL,
63
+ cookieHeader,
64
+ 10,
65
+ AbortSignal.timeout(10000),
66
+ );
67
+ return extractEmailFromListAccounts(response);
68
+ } catch {
69
+ return null;
70
+ }
71
+ }
72
+
73
+ export async function queryWithCookies(
74
+ prompt: string,
75
+ cookieMap: CookieMap,
76
+ options: GeminiWebOptions = {},
77
+ ): Promise<string> {
78
+ const model = options.model && MODEL_HEADERS[options.model] ? options.model : "gemini-2.5-flash";
79
+ const timeoutMs = options.timeoutMs ?? 120000;
80
+
81
+ let fullPrompt = prompt;
82
+ if (options.youtubeUrl) {
83
+ fullPrompt = `${fullPrompt}\n\nYouTube video: ${options.youtubeUrl}`;
84
+ }
85
+
86
+ const result = await runGeminiWebOnce(fullPrompt, cookieMap, model, options.files, timeoutMs, options.signal);
87
+
88
+ if (isModelUnavailable(result.errorCode) && model !== "gemini-2.5-flash") {
89
+ const fallback = await runGeminiWebOnce(fullPrompt, cookieMap, "gemini-2.5-flash", options.files, timeoutMs, options.signal);
90
+ if (fallback.errorMessage) throw new Error(fallback.errorMessage);
91
+ if (!fallback.text) throw new Error("Gemini Web returned empty response (fallback model)");
92
+ return fallback.text;
93
+ }
94
+
95
+ if (result.errorMessage) throw new Error(result.errorMessage);
96
+ if (!result.text) throw new Error("Gemini Web returned empty response");
97
+ return result.text;
98
+ }
99
+
100
+ interface GeminiWebResult {
101
+ text: string;
102
+ errorCode?: number;
103
+ errorMessage?: string;
104
+ }
105
+
106
+ async function runGeminiWebOnce(
107
+ prompt: string,
108
+ cookieMap: CookieMap,
109
+ model: string,
110
+ files: string[] | undefined,
111
+ timeoutMs: number,
112
+ signal?: AbortSignal,
113
+ ): Promise<GeminiWebResult> {
114
+ const effectiveSignal = withTimeout(signal, timeoutMs);
115
+ const cookieHeader = buildCookieHeader(cookieMap);
116
+ const accessToken = await fetchAccessToken(cookieHeader, effectiveSignal);
117
+
118
+ const uploaded: Array<{ id: string; name: string }> = [];
119
+ if (files) {
120
+ for (const filePath of files) {
121
+ uploaded.push(await uploadFile(filePath, cookieHeader, effectiveSignal));
122
+ }
123
+ }
124
+
125
+ const fReq = buildFReqPayload(prompt, uploaded);
126
+ const params = new URLSearchParams();
127
+ params.set("at", accessToken);
128
+ params.set("f.req", fReq);
129
+
130
+ const res = await fetch(GEMINI_STREAM_GENERATE_URL, {
131
+ method: "POST",
132
+ headers: {
133
+ "content-type": "application/x-www-form-urlencoded;charset=utf-8",
134
+ host: "gemini.google.com",
135
+ origin: "https://gemini.google.com",
136
+ referer: "https://gemini.google.com/",
137
+ "x-same-domain": "1",
138
+ "user-agent": USER_AGENT,
139
+ cookie: cookieHeader,
140
+ [MODEL_HEADER_NAME]: MODEL_HEADERS[model],
141
+ },
142
+ body: params.toString(),
143
+ signal: effectiveSignal,
144
+ });
145
+
146
+ const rawText = await res.text();
147
+
148
+ if (!res.ok) {
149
+ return { text: "", errorMessage: `Gemini request failed: ${res.status}` };
150
+ }
151
+
152
+ try {
153
+ return parseStreamGenerateResponse(rawText);
154
+ } catch (err) {
155
+ let errorCode: number | undefined;
156
+ try {
157
+ const json = JSON.parse(trimJsonEnvelope(rawText));
158
+ errorCode = extractErrorCode(json);
159
+ } catch {
160
+ }
161
+ return {
162
+ text: "",
163
+ errorCode,
164
+ errorMessage: err instanceof Error ? err.message : String(err),
165
+ };
166
+ }
167
+ }
168
+
169
+ async function fetchAccessToken(
170
+ cookieHeader: string,
171
+ signal: AbortSignal,
172
+ ): Promise<string> {
173
+ const html = await fetchWithCookieRedirects(GEMINI_APP_URL, cookieHeader, 10, signal);
174
+
175
+ for (const key of ["SNlM0e", "thykhd"]) {
176
+ const match = html.match(new RegExp(`"${key}":"(.*?)"`));
177
+ if (match?.[1]) return match[1];
178
+ }
179
+
180
+ throw new Error("Unable to authenticate with Gemini. Make sure you're signed into gemini.google.com in a supported Chromium-based browser.");
181
+ }
182
+
183
+ async function fetchWithCookieRedirects(
184
+ url: string,
185
+ cookieHeader: string,
186
+ maxRedirects: number,
187
+ signal: AbortSignal,
188
+ ): Promise<string> {
189
+ let current = url;
190
+ for (let i = 0; i <= maxRedirects; i++) {
191
+ const res = await fetch(current, {
192
+ headers: { "user-agent": USER_AGENT, cookie: cookieHeader },
193
+ redirect: "manual",
194
+ signal,
195
+ });
196
+ if (res.status >= 300 && res.status < 400) {
197
+ const location = res.headers.get("location");
198
+ if (location) {
199
+ current = new URL(location, current).toString();
200
+ continue;
201
+ }
202
+ }
203
+ return await res.text();
204
+ }
205
+ throw new Error(`Too many redirects (>${maxRedirects})`);
206
+ }
207
+
208
+ function extractEmailFromGeminiHtml(html: string): string | null {
209
+ const patterns = [
210
+ /"email"\s*:\s*"([^"]+)"/,
211
+ /"displayEmail"\s*:\s*"([^"]+)"/,
212
+ /"identifier"\s*:\s*"([^"]+)"/,
213
+ /"defaultEmail"\s*:\s*"([^"]+)"/,
214
+ /"gaiaIdentifier"\s*:\s*"([^"]+)"/,
215
+ ];
216
+
217
+ for (const pattern of patterns) {
218
+ const match = html.match(pattern);
219
+ const email = normalizeEmail(match?.[1]);
220
+ if (email) return email;
221
+ }
222
+
223
+ return findFirstEmail(html);
224
+ }
225
+
226
+ function extractEmailFromListAccounts(text: string): string | null {
227
+ const trimmed = text.replace(/^\)\]\}'\s*/, "");
228
+ try {
229
+ return findEmailInValue(JSON.parse(trimmed)) ?? findFirstEmail(trimmed);
230
+ } catch {
231
+ return findFirstEmail(trimmed);
232
+ }
233
+ }
234
+
235
+ function findEmailInValue(value: unknown): string | null {
236
+ if (typeof value === "string") return normalizeEmail(value);
237
+ if (Array.isArray(value)) {
238
+ for (const item of value) {
239
+ const email = findEmailInValue(item);
240
+ if (email) return email;
241
+ }
242
+ return null;
243
+ }
244
+ if (value && typeof value === "object") {
245
+ for (const item of Object.values(value as Record<string, unknown>)) {
246
+ const email = findEmailInValue(item);
247
+ if (email) return email;
248
+ }
249
+ }
250
+ return null;
251
+ }
252
+
253
+ function findFirstEmail(text: string): string | null {
254
+ const normalized = decodeEmailEscapes(text);
255
+ const match = normalized.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i);
256
+ return match?.[0] ?? null;
257
+ }
258
+
259
+ function normalizeEmail(value: string | undefined): string | null {
260
+ if (!value) return null;
261
+ const normalized = decodeEmailEscapes(value.trim());
262
+ return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(normalized) ? normalized : null;
263
+ }
264
+
265
+ function decodeEmailEscapes(value: string): string {
266
+ return value
267
+ .replace(/\\u0040/gi, "@")
268
+ .replace(/\\x40/gi, "@")
269
+ .replace(/&#64;/gi, "@")
270
+ .replace(/&commat;/gi, "@")
271
+ .replace(/\\"/g, "\"")
272
+ .replace(/\\\\/g, "\\");
273
+ }
274
+
275
+ async function uploadFile(
276
+ filePath: string,
277
+ cookieHeader: string,
278
+ signal: AbortSignal,
279
+ ): Promise<{ id: string; name: string }> {
280
+ const data = readFileSync(filePath);
281
+ const fileName = basename(filePath);
282
+ const boundary = "----FormBoundary" + Math.random().toString(36).slice(2);
283
+ const header = `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${fileName}"\r\nContent-Type: application/octet-stream\r\n\r\n`;
284
+ const footer = `\r\n--${boundary}--\r\n`;
285
+
286
+ const body = Buffer.concat([
287
+ Buffer.from(header, "utf-8"),
288
+ data,
289
+ Buffer.from(footer, "utf-8"),
290
+ ]);
291
+
292
+ const res = await fetch(GEMINI_UPLOAD_URL, {
293
+ method: "POST",
294
+ headers: {
295
+ "content-type": `multipart/form-data; boundary=${boundary}`,
296
+ "push-id": GEMINI_UPLOAD_PUSH_ID,
297
+ "user-agent": USER_AGENT,
298
+ cookie: cookieHeader,
299
+ },
300
+ body,
301
+ signal,
302
+ });
303
+
304
+ if (!res.ok) {
305
+ const text = await res.text();
306
+ throw new Error(`File upload failed: ${res.status} (${text.slice(0, 200)})`);
307
+ }
308
+
309
+ return { id: await res.text(), name: fileName };
310
+ }
311
+
312
+ function buildFReqPayload(
313
+ prompt: string,
314
+ uploaded: Array<{ id: string; name: string }>,
315
+ ): string {
316
+ const promptPayload =
317
+ uploaded.length > 0
318
+ ? [prompt, 0, null, uploaded.map((file) => [[file.id, 1]])]
319
+ : [prompt];
320
+ const innerList = [promptPayload, null, null];
321
+ return JSON.stringify([null, JSON.stringify(innerList)]);
322
+ }
323
+
324
+ function withTimeout(signal: AbortSignal | undefined, timeoutMs: number): AbortSignal {
325
+ const timeout = AbortSignal.timeout(timeoutMs);
326
+ return signal ? AbortSignal.any([signal, timeout]) : timeout;
327
+ }
328
+
329
+ function buildCookieHeader(cookieMap: CookieMap): string {
330
+ return Object.entries(cookieMap)
331
+ .filter(([, value]) => typeof value === "string" && value.length > 0)
332
+ .map(([name, value]) => `${name}=${value}`)
333
+ .join("; ");
334
+ }
335
+
336
+ function getNestedValue(value: unknown, pathParts: number[]): unknown {
337
+ let current: unknown = value;
338
+ for (const part of pathParts) {
339
+ if (current == null) return undefined;
340
+ if (!Array.isArray(current)) return undefined;
341
+ current = (current as unknown[])[part];
342
+ }
343
+ return current;
344
+ }
345
+
346
+ function trimJsonEnvelope(text: string): string {
347
+ const start = text.indexOf("[");
348
+ const end = text.lastIndexOf("]");
349
+ if (start === -1 || end === -1 || end <= start) {
350
+ throw new Error("Gemini response did not contain a JSON payload.");
351
+ }
352
+ return text.slice(start, end + 1);
353
+ }
354
+
355
+ function extractErrorCode(responseJson: unknown): number | undefined {
356
+ const code = getNestedValue(responseJson, [0, 5, 2, 0, 1, 0]);
357
+ return typeof code === "number" && code >= 0 ? code : undefined;
358
+ }
359
+
360
+ function isModelUnavailable(errorCode: number | undefined): boolean {
361
+ return errorCode === 1052;
362
+ }
363
+
364
+ function parseStreamGenerateResponse(rawText: string): GeminiWebResult {
365
+ const responseJson = JSON.parse(trimJsonEnvelope(rawText));
366
+ const errorCode = extractErrorCode(responseJson);
367
+
368
+ const parts = Array.isArray(responseJson) ? responseJson : [];
369
+ let body: unknown = null;
370
+
371
+ for (let i = 0; i < parts.length; i++) {
372
+ const partBody = getNestedValue(parts[i], [2]);
373
+ if (!partBody || typeof partBody !== "string") continue;
374
+ try {
375
+ const parsed = JSON.parse(partBody);
376
+ const candidateList = getNestedValue(parsed, [4]);
377
+ if (Array.isArray(candidateList) && candidateList.length > 0) {
378
+ body = parsed;
379
+ break;
380
+ }
381
+ } catch {
382
+ }
383
+ }
384
+
385
+ const candidateList = getNestedValue(body, [4]);
386
+ const firstCandidate = Array.isArray(candidateList) ? (candidateList as unknown[])[0] : undefined;
387
+ const textRaw = getNestedValue(firstCandidate, [1, 0]) as string | undefined;
388
+
389
+ let text = textRaw ?? "";
390
+ if (/^http:\/\/googleusercontent\.com\/card_content\/\d+/.test(text)) {
391
+ const alt = getNestedValue(firstCandidate, [22, 0]) as string | undefined;
392
+ if (alt) text = alt;
393
+ }
394
+
395
+ return { text, errorCode };
396
+ }
@@ -0,0 +1,196 @@
1
+ import { execFile } from "node:child_process";
2
+ import type { ExtractedContent } from "./extract.js";
3
+ import type { GitHubUrlInfo } from "./github-extract.js";
4
+
5
+ const MAX_TREE_ENTRIES = 200;
6
+ const MAX_INLINE_FILE_CHARS = 100_000;
7
+
8
+ let ghAvailable: boolean | null = null;
9
+ let ghHintShown = false;
10
+
11
+ export async function checkGhAvailable(): Promise<boolean> {
12
+ if (ghAvailable !== null) return ghAvailable;
13
+
14
+ return new Promise((resolve) => {
15
+ execFile("gh", ["--version"], { timeout: 5000 }, (err) => {
16
+ ghAvailable = !err;
17
+ resolve(ghAvailable);
18
+ });
19
+ });
20
+ }
21
+
22
+ export function showGhHint(): void {
23
+ if (!ghHintShown) {
24
+ ghHintShown = true;
25
+ console.error("[pi-web-access] Install `gh` CLI for better GitHub repo access including private repos.");
26
+ }
27
+ }
28
+
29
+ export async function checkRepoSize(owner: string, repo: string): Promise<number | null> {
30
+ if (!(await checkGhAvailable())) return null;
31
+
32
+ return new Promise((resolve) => {
33
+ execFile("gh", ["api", `repos/${owner}/${repo}`, "--jq", ".size"], { timeout: 10000 }, (err, stdout) => {
34
+ if (err) {
35
+ resolve(null);
36
+ return;
37
+ }
38
+ const kb = parseInt(stdout.trim(), 10);
39
+ resolve(Number.isNaN(kb) ? null : kb);
40
+ });
41
+ });
42
+ }
43
+
44
+ async function getDefaultBranch(owner: string, repo: string): Promise<string | null> {
45
+ if (!(await checkGhAvailable())) return null;
46
+
47
+ return new Promise((resolve) => {
48
+ execFile("gh", ["api", `repos/${owner}/${repo}`, "--jq", ".default_branch"], { timeout: 10000 }, (err, stdout) => {
49
+ if (err) {
50
+ resolve(null);
51
+ return;
52
+ }
53
+ const branch = stdout.trim();
54
+ resolve(branch || null);
55
+ });
56
+ });
57
+ }
58
+
59
+ async function fetchTreeViaApi(owner: string, repo: string, ref: string): Promise<string | null> {
60
+ if (!(await checkGhAvailable())) return null;
61
+
62
+ return new Promise((resolve) => {
63
+ execFile(
64
+ "gh",
65
+ ["api", `repos/${owner}/${repo}/git/trees/${ref}?recursive=1`, "--jq", ".tree[].path"],
66
+ { timeout: 15000, maxBuffer: 5 * 1024 * 1024 },
67
+ (err, stdout) => {
68
+ if (err) {
69
+ resolve(null);
70
+ return;
71
+ }
72
+ const paths = stdout.trim().split("\n").filter(Boolean);
73
+ if (paths.length === 0) {
74
+ resolve(null);
75
+ return;
76
+ }
77
+ const truncated = paths.length > MAX_TREE_ENTRIES;
78
+ const display = paths.slice(0, MAX_TREE_ENTRIES).join("\n");
79
+ resolve(truncated ? display + `\n... (${paths.length} total entries)` : display);
80
+ },
81
+ );
82
+ });
83
+ }
84
+
85
+ async function fetchReadmeViaApi(owner: string, repo: string, ref: string): Promise<string | null> {
86
+ if (!(await checkGhAvailable())) return null;
87
+
88
+ return new Promise((resolve) => {
89
+ execFile(
90
+ "gh",
91
+ ["api", `repos/${owner}/${repo}/readme?ref=${ref}`, "--jq", ".content"],
92
+ { timeout: 10000 },
93
+ (err, stdout) => {
94
+ if (err) {
95
+ resolve(null);
96
+ return;
97
+ }
98
+ try {
99
+ const decoded = Buffer.from(stdout.trim(), "base64").toString("utf-8");
100
+ resolve(decoded.length > 8192 ? decoded.slice(0, 8192) + "\n\n[README truncated at 8K chars]" : decoded);
101
+ } catch {
102
+ resolve(null);
103
+ }
104
+ },
105
+ );
106
+ });
107
+ }
108
+
109
+ async function fetchFileViaApi(owner: string, repo: string, path: string, ref: string): Promise<string | null> {
110
+ if (!(await checkGhAvailable())) return null;
111
+
112
+ return new Promise((resolve) => {
113
+ execFile(
114
+ "gh",
115
+ ["api", `repos/${owner}/${repo}/contents/${path}?ref=${ref}`, "--jq", ".content"],
116
+ { timeout: 10000, maxBuffer: 2 * 1024 * 1024 },
117
+ (err, stdout) => {
118
+ if (err) {
119
+ resolve(null);
120
+ return;
121
+ }
122
+ try {
123
+ resolve(Buffer.from(stdout.trim(), "base64").toString("utf-8"));
124
+ } catch {
125
+ resolve(null);
126
+ }
127
+ },
128
+ );
129
+ });
130
+ }
131
+
132
+ export async function fetchViaApi(
133
+ url: string,
134
+ owner: string,
135
+ repo: string,
136
+ info: GitHubUrlInfo,
137
+ sizeNote?: string,
138
+ ): Promise<ExtractedContent | null> {
139
+ const ref = info.ref || (await getDefaultBranch(owner, repo));
140
+ if (!ref) return null;
141
+
142
+ const lines: string[] = [];
143
+ if (sizeNote) {
144
+ lines.push(sizeNote);
145
+ lines.push("");
146
+ }
147
+
148
+ if (info.type === "blob" && info.path) {
149
+ const content = await fetchFileViaApi(owner, repo, info.path, ref);
150
+ if (!content) return null;
151
+
152
+ lines.push(`## ${info.path}`);
153
+ if (content.length > MAX_INLINE_FILE_CHARS) {
154
+ lines.push(content.slice(0, MAX_INLINE_FILE_CHARS));
155
+ lines.push(`\n[File truncated at 100K chars]`);
156
+ } else {
157
+ lines.push(content);
158
+ }
159
+
160
+ return {
161
+ url,
162
+ title: `${owner}/${repo} - ${info.path}`,
163
+ content: lines.join("\n"),
164
+ error: null,
165
+ };
166
+ }
167
+
168
+ const [tree, readme] = await Promise.all([
169
+ fetchTreeViaApi(owner, repo, ref),
170
+ fetchReadmeViaApi(owner, repo, ref),
171
+ ]);
172
+
173
+ if (!tree && !readme) return null;
174
+
175
+ if (tree) {
176
+ lines.push("## Structure");
177
+ lines.push(tree);
178
+ lines.push("");
179
+ }
180
+
181
+ if (readme) {
182
+ lines.push("## README.md");
183
+ lines.push(readme);
184
+ lines.push("");
185
+ }
186
+
187
+ lines.push("This is an API-only view. Clone the repo or use `read`/`bash` for deeper exploration.");
188
+
189
+ const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
190
+ return {
191
+ url,
192
+ title,
193
+ content: lines.join("\n"),
194
+ error: null,
195
+ };
196
+ }