pi-web-access 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { activityMonitor } from "./activity.js";
5
+ import { getApiKey, API_BASE, DEFAULT_MODEL } from "./gemini-api.js";
6
+ import { isGeminiWebAvailable, queryWithCookies } from "./gemini-web.js";
7
+ import { isPerplexityAvailable, searchWithPerplexity, type SearchResult, type SearchResponse, type SearchOptions } from "./perplexity.js";
8
+
9
+ export type SearchProvider = "auto" | "perplexity" | "gemini";
10
+
11
+ const CONFIG_PATH = join(homedir(), ".pi", "web-search.json");
12
+
13
+ let cachedSearchConfig: { searchProvider: SearchProvider } | null = null;
14
+
15
+ function getSearchConfig(): { searchProvider: SearchProvider } {
16
+ if (cachedSearchConfig) return cachedSearchConfig;
17
+ try {
18
+ if (existsSync(CONFIG_PATH)) {
19
+ const raw = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
20
+ cachedSearchConfig = { searchProvider: raw.searchProvider ?? "auto" };
21
+ return cachedSearchConfig;
22
+ }
23
+ } catch {}
24
+ cachedSearchConfig = { searchProvider: "auto" };
25
+ return cachedSearchConfig;
26
+ }
27
+
28
+ export interface FullSearchOptions extends SearchOptions {
29
+ provider?: SearchProvider;
30
+ }
31
+
32
+ export async function search(query: string, options: FullSearchOptions = {}): Promise<SearchResponse> {
33
+ const config = getSearchConfig();
34
+ const provider = options.provider ?? config.searchProvider;
35
+
36
+ if (provider === "perplexity") {
37
+ return searchWithPerplexity(query, options);
38
+ }
39
+
40
+ if (provider === "gemini") {
41
+ const result = await searchWithGeminiApi(query, options)
42
+ ?? await searchWithGeminiWeb(query, options);
43
+ if (result) return result;
44
+ throw new Error(
45
+ "Gemini search unavailable. Either:\n" +
46
+ " 1. Set GEMINI_API_KEY in ~/.pi/web-search.json\n" +
47
+ " 2. Sign into gemini.google.com in Chrome"
48
+ );
49
+ }
50
+
51
+ if (isPerplexityAvailable()) {
52
+ return searchWithPerplexity(query, options);
53
+ }
54
+
55
+ const geminiResult = await searchWithGeminiApi(query, options)
56
+ ?? await searchWithGeminiWeb(query, options);
57
+ if (geminiResult) return geminiResult;
58
+
59
+ throw new Error(
60
+ "No search provider available. Either:\n" +
61
+ " 1. Set perplexityApiKey in ~/.pi/web-search.json\n" +
62
+ " 2. Set GEMINI_API_KEY in ~/.pi/web-search.json\n" +
63
+ " 3. Sign into gemini.google.com in Chrome"
64
+ );
65
+ }
66
+
67
+ async function searchWithGeminiApi(query: string, options: SearchOptions = {}): Promise<SearchResponse | null> {
68
+ const apiKey = getApiKey();
69
+ if (!apiKey) return null;
70
+
71
+ const activityId = activityMonitor.logStart({ type: "api", query });
72
+
73
+ try {
74
+ const model = DEFAULT_MODEL;
75
+ const body = {
76
+ contents: [{ parts: [{ text: query }] }],
77
+ tools: [{ google_search: {} }],
78
+ };
79
+
80
+ const res = await fetch(`${API_BASE}/models/${model}:generateContent?key=${apiKey}`, {
81
+ method: "POST",
82
+ headers: { "Content-Type": "application/json" },
83
+ body: JSON.stringify(body),
84
+ signal: AbortSignal.any([
85
+ AbortSignal.timeout(60000),
86
+ ...(options.signal ? [options.signal] : []),
87
+ ]),
88
+ });
89
+
90
+ if (!res.ok) {
91
+ const errorText = await res.text();
92
+ throw new Error(`Gemini API error ${res.status}: ${errorText.slice(0, 300)}`);
93
+ }
94
+
95
+ const data = await res.json() as GeminiSearchResponse;
96
+ activityMonitor.logComplete(activityId, res.status);
97
+
98
+ const answer = data.candidates?.[0]?.content?.parts
99
+ ?.map(p => p.text).filter(Boolean).join("\n") ?? "";
100
+
101
+ const metadata = data.candidates?.[0]?.groundingMetadata;
102
+ const results = await resolveGroundingChunks(metadata?.groundingChunks, options.signal);
103
+
104
+ if (!answer && results.length === 0) return null;
105
+ return { answer, results };
106
+ } catch (err) {
107
+ const message = err instanceof Error ? err.message : String(err);
108
+ if (message.toLowerCase().includes("abort")) {
109
+ activityMonitor.logComplete(activityId, 0);
110
+ } else {
111
+ activityMonitor.logError(activityId, message);
112
+ }
113
+ return null;
114
+ }
115
+ }
116
+
117
+ async function searchWithGeminiWeb(query: string, options: SearchOptions = {}): Promise<SearchResponse | null> {
118
+ const cookies = await isGeminiWebAvailable();
119
+ if (!cookies) return null;
120
+
121
+ const prompt = buildSearchPrompt(query, options);
122
+ const activityId = activityMonitor.logStart({ type: "api", query });
123
+
124
+ try {
125
+ const text = await queryWithCookies(prompt, cookies, {
126
+ model: "gemini-2.5-flash",
127
+ signal: options.signal,
128
+ timeoutMs: 60000,
129
+ });
130
+
131
+ activityMonitor.logComplete(activityId, 200);
132
+
133
+ const results = extractSourceUrls(text);
134
+ return { answer: text, results };
135
+ } catch (err) {
136
+ const message = err instanceof Error ? err.message : String(err);
137
+ if (message.toLowerCase().includes("abort")) {
138
+ activityMonitor.logComplete(activityId, 0);
139
+ } else {
140
+ activityMonitor.logError(activityId, message);
141
+ }
142
+ return null;
143
+ }
144
+ }
145
+
146
+ function buildSearchPrompt(query: string, options: SearchOptions): string {
147
+ let prompt = `Search the web and answer the following question. Include source URLs for your claims.\nFormat your response as:\n1. A direct answer to the question\n2. Cited sources as markdown links\n\nQuestion: ${query}`;
148
+
149
+ if (options.recencyFilter) {
150
+ const labels: Record<string, string> = {
151
+ day: "past 24 hours",
152
+ week: "past week",
153
+ month: "past month",
154
+ year: "past year",
155
+ };
156
+ prompt += `\n\nOnly include results from the ${labels[options.recencyFilter]}.`;
157
+ }
158
+
159
+ if (options.domainFilter?.length) {
160
+ const includes = options.domainFilter.filter(d => !d.startsWith("-"));
161
+ const excludes = options.domainFilter.filter(d => d.startsWith("-")).map(d => d.slice(1));
162
+ if (includes.length) prompt += `\n\nOnly cite sources from: ${includes.join(", ")}`;
163
+ if (excludes.length) prompt += `\n\nDo not cite sources from: ${excludes.join(", ")}`;
164
+ }
165
+
166
+ return prompt;
167
+ }
168
+
169
+ function extractSourceUrls(markdown: string): SearchResult[] {
170
+ const results: SearchResult[] = [];
171
+ const seen = new Set<string>();
172
+ const linkRegex = /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g;
173
+ for (const match of markdown.matchAll(linkRegex)) {
174
+ const url = match[2];
175
+ if (seen.has(url)) continue;
176
+ seen.add(url);
177
+ results.push({ title: match[1], url, snippet: "" });
178
+ }
179
+ return results;
180
+ }
181
+
182
+ async function resolveGroundingChunks(
183
+ chunks: GroundingChunk[] | undefined,
184
+ signal?: AbortSignal,
185
+ ): Promise<SearchResult[]> {
186
+ if (!chunks?.length) return [];
187
+
188
+ const results: SearchResult[] = [];
189
+ for (const chunk of chunks) {
190
+ if (!chunk.web) continue;
191
+ const title = chunk.web.title || "";
192
+ let url = chunk.web.uri || "";
193
+
194
+ if (url.includes("vertexaisearch.cloud.google.com/grounding-api-redirect")) {
195
+ const resolved = await resolveRedirect(url, signal);
196
+ if (resolved) url = resolved;
197
+ }
198
+
199
+ if (url) results.push({ title, url, snippet: "" });
200
+ }
201
+ return results;
202
+ }
203
+
204
+ async function resolveRedirect(proxyUrl: string, signal?: AbortSignal): Promise<string | null> {
205
+ try {
206
+ const res = await fetch(proxyUrl, {
207
+ method: "HEAD",
208
+ redirect: "manual",
209
+ signal: AbortSignal.any([
210
+ AbortSignal.timeout(5000),
211
+ ...(signal ? [signal] : []),
212
+ ]),
213
+ });
214
+ return res.headers.get("location") || null;
215
+ } catch {
216
+ return null;
217
+ }
218
+ }
219
+
220
+ interface GeminiSearchResponse {
221
+ candidates?: Array<{
222
+ content?: { parts?: Array<{ text?: string }> };
223
+ groundingMetadata?: {
224
+ webSearchQueries?: string[];
225
+ groundingChunks?: GroundingChunk[];
226
+ groundingSupports?: Array<{
227
+ segment?: { startIndex?: number; endIndex?: number; text?: string };
228
+ groundingChunkIndices?: number[];
229
+ }>;
230
+ };
231
+ }>;
232
+ }
233
+
234
+ interface GroundingChunk {
235
+ web?: { uri?: string; title?: string };
236
+ }
@@ -0,0 +1,119 @@
1
+ import { activityMonitor } from "./activity.js";
2
+ import { getApiKey, API_BASE, DEFAULT_MODEL } from "./gemini-api.js";
3
+ import { isGeminiWebAvailable, queryWithCookies } from "./gemini-web.js";
4
+ import { extractHeadingTitle, type ExtractedContent } from "./extract.js";
5
+
6
+ const EXTRACTION_PROMPT = `Extract the complete readable content from this URL as clean markdown.
7
+ Include the page title, all text content, code blocks, and tables.
8
+ Do not summarize — extract the full content.
9
+
10
+ URL: `;
11
+
12
+ export async function extractWithUrlContext(
13
+ url: string,
14
+ signal?: AbortSignal,
15
+ ): Promise<ExtractedContent | null> {
16
+ const apiKey = getApiKey();
17
+ if (!apiKey) return null;
18
+
19
+ const activityId = activityMonitor.logStart({ type: "api", query: `url_context: ${url}` });
20
+
21
+ try {
22
+ const model = DEFAULT_MODEL;
23
+ const body = {
24
+ contents: [{ parts: [{ text: EXTRACTION_PROMPT + url }] }],
25
+ tools: [{ url_context: {} }],
26
+ };
27
+
28
+ const res = await fetch(`${API_BASE}/models/${model}:generateContent?key=${apiKey}`, {
29
+ method: "POST",
30
+ headers: { "Content-Type": "application/json" },
31
+ body: JSON.stringify(body),
32
+ signal: AbortSignal.any([
33
+ AbortSignal.timeout(60000),
34
+ ...(signal ? [signal] : []),
35
+ ]),
36
+ });
37
+
38
+ if (!res.ok) {
39
+ activityMonitor.logComplete(activityId, res.status);
40
+ return null;
41
+ }
42
+
43
+ const data = await res.json() as UrlContextResponse;
44
+ activityMonitor.logComplete(activityId, res.status);
45
+
46
+ const metadata = data.candidates?.[0]?.url_context_metadata;
47
+ if (metadata?.url_metadata?.length) {
48
+ const status = metadata.url_metadata[0].url_retrieval_status;
49
+ if (status === "URL_RETRIEVAL_STATUS_UNSAFE" || status === "URL_RETRIEVAL_STATUS_ERROR") {
50
+ return null;
51
+ }
52
+ }
53
+
54
+ const content = data.candidates?.[0]?.content?.parts
55
+ ?.map(p => p.text).filter(Boolean).join("\n") ?? "";
56
+
57
+ if (!content || content.length < 50) return null;
58
+
59
+ const title = extractTitleFromContent(content, url);
60
+ return { url, title, content, error: null };
61
+ } catch (err) {
62
+ const message = err instanceof Error ? err.message : String(err);
63
+ if (message.toLowerCase().includes("abort")) {
64
+ activityMonitor.logComplete(activityId, 0);
65
+ } else {
66
+ activityMonitor.logError(activityId, message);
67
+ }
68
+ return null;
69
+ }
70
+ }
71
+
72
+ export async function extractWithGeminiWeb(
73
+ url: string,
74
+ signal?: AbortSignal,
75
+ ): Promise<ExtractedContent | null> {
76
+ const cookies = await isGeminiWebAvailable();
77
+ if (!cookies) return null;
78
+
79
+ const activityId = activityMonitor.logStart({ type: "api", query: `gemini_web: ${url}` });
80
+
81
+ try {
82
+ const text = await queryWithCookies(EXTRACTION_PROMPT + url, cookies, {
83
+ model: "gemini-2.5-flash",
84
+ signal,
85
+ timeoutMs: 60000,
86
+ });
87
+
88
+ activityMonitor.logComplete(activityId, 200);
89
+
90
+ if (!text || text.length < 50) return null;
91
+
92
+ const title = extractTitleFromContent(text, url);
93
+ return { url, title, content: text, error: null };
94
+ } catch (err) {
95
+ const message = err instanceof Error ? err.message : String(err);
96
+ if (message.toLowerCase().includes("abort")) {
97
+ activityMonitor.logComplete(activityId, 0);
98
+ } else {
99
+ activityMonitor.logError(activityId, message);
100
+ }
101
+ return null;
102
+ }
103
+ }
104
+
105
+ function extractTitleFromContent(text: string, url: string): string {
106
+ return extractHeadingTitle(text) ?? (new URL(url).pathname.split("/").pop() || url);
107
+ }
108
+
109
+ interface UrlContextResponse {
110
+ candidates?: Array<{
111
+ content?: { parts?: Array<{ text?: string }> };
112
+ url_context_metadata?: {
113
+ url_metadata?: Array<{
114
+ retrieved_url?: string;
115
+ url_retrieval_status?: string;
116
+ }>;
117
+ };
118
+ }>;
119
+ }
package/gemini-web.ts ADDED
@@ -0,0 +1,296 @@
1
+ import { type CookieMap, getGoogleCookies } from "./chrome-cookies.js";
2
+
3
+ const GEMINI_APP_URL = "https://gemini.google.com/app";
4
+ const GEMINI_STREAM_GENERATE_URL =
5
+ "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate";
6
+ const GEMINI_UPLOAD_URL = "https://content-push.googleapis.com/upload";
7
+ const GEMINI_UPLOAD_PUSH_ID = "feeds/mcudyrk2a4khkz";
8
+
9
+ const USER_AGENT =
10
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
11
+
12
+ const MODEL_HEADER_NAME = "x-goog-ext-525001261-jspb";
13
+ const MODEL_HEADERS: Record<string, string> = {
14
+ "gemini-3-pro": '[1,null,null,null,"9d8ca3786ebdfbea",null,null,0,[4]]',
15
+ "gemini-2.5-pro": '[1,null,null,null,"4af6c7f5da75d65d",null,null,0,[4]]',
16
+ "gemini-2.5-flash": '[1,null,null,null,"9ec249fc9ad08861",null,null,0,[4]]',
17
+ };
18
+
19
+ const REQUIRED_COOKIES = ["__Secure-1PSID", "__Secure-1PSIDTS"];
20
+
21
+ export interface GeminiWebOptions {
22
+ youtubeUrl?: string;
23
+ model?: string;
24
+ files?: string[];
25
+ signal?: AbortSignal;
26
+ timeoutMs?: number;
27
+ }
28
+
29
+ function hasRequiredCookies(cookieMap: CookieMap): boolean {
30
+ return REQUIRED_COOKIES.every((name) => Boolean(cookieMap[name]));
31
+ }
32
+
33
+ export async function isGeminiWebAvailable(): Promise<CookieMap | null> {
34
+ const result = await getGoogleCookies();
35
+ if (!result || !hasRequiredCookies(result.cookies)) return null;
36
+ return result.cookies;
37
+ }
38
+
39
+ export async function queryWithCookies(
40
+ prompt: string,
41
+ cookieMap: CookieMap,
42
+ options: GeminiWebOptions = {},
43
+ ): Promise<string> {
44
+ const model = options.model && MODEL_HEADERS[options.model] ? options.model : "gemini-2.5-flash";
45
+ const timeoutMs = options.timeoutMs ?? 120000;
46
+
47
+ let fullPrompt = prompt;
48
+ if (options.youtubeUrl) {
49
+ fullPrompt = `${fullPrompt}\n\nYouTube video: ${options.youtubeUrl}`;
50
+ }
51
+
52
+ const result = await runGeminiWebOnce(fullPrompt, cookieMap, model, options.files, timeoutMs, options.signal);
53
+
54
+ if (isModelUnavailable(result.errorCode) && model !== "gemini-2.5-flash") {
55
+ const fallback = await runGeminiWebOnce(fullPrompt, cookieMap, "gemini-2.5-flash", options.files, timeoutMs, options.signal);
56
+ if (fallback.errorMessage) throw new Error(fallback.errorMessage);
57
+ if (!fallback.text) throw new Error("Gemini Web returned empty response (fallback model)");
58
+ return fallback.text;
59
+ }
60
+
61
+ if (result.errorMessage) throw new Error(result.errorMessage);
62
+ if (!result.text) throw new Error("Gemini Web returned empty response");
63
+ return result.text;
64
+ }
65
+
66
+ interface GeminiWebResult {
67
+ text: string;
68
+ errorCode?: number;
69
+ errorMessage?: string;
70
+ }
71
+
72
+ async function runGeminiWebOnce(
73
+ prompt: string,
74
+ cookieMap: CookieMap,
75
+ model: string,
76
+ files: string[] | undefined,
77
+ timeoutMs: number,
78
+ signal?: AbortSignal,
79
+ ): Promise<GeminiWebResult> {
80
+ const effectiveSignal = withTimeout(signal, timeoutMs);
81
+ const cookieHeader = buildCookieHeader(cookieMap);
82
+ const accessToken = await fetchAccessToken(cookieHeader, effectiveSignal);
83
+
84
+ const uploaded: Array<{ id: string; name: string }> = [];
85
+ if (files) {
86
+ for (const filePath of files) {
87
+ uploaded.push(await uploadFile(filePath, cookieHeader, effectiveSignal));
88
+ }
89
+ }
90
+
91
+ const fReq = buildFReqPayload(prompt, uploaded);
92
+ const params = new URLSearchParams();
93
+ params.set("at", accessToken);
94
+ params.set("f.req", fReq);
95
+
96
+ const res = await fetch(GEMINI_STREAM_GENERATE_URL, {
97
+ method: "POST",
98
+ headers: {
99
+ "content-type": "application/x-www-form-urlencoded;charset=utf-8",
100
+ host: "gemini.google.com",
101
+ origin: "https://gemini.google.com",
102
+ referer: "https://gemini.google.com/",
103
+ "x-same-domain": "1",
104
+ "user-agent": USER_AGENT,
105
+ cookie: cookieHeader,
106
+ [MODEL_HEADER_NAME]: MODEL_HEADERS[model],
107
+ },
108
+ body: params.toString(),
109
+ signal: effectiveSignal,
110
+ });
111
+
112
+ const rawText = await res.text();
113
+
114
+ if (!res.ok) {
115
+ return { text: "", errorMessage: `Gemini request failed: ${res.status}` };
116
+ }
117
+
118
+ try {
119
+ return parseStreamGenerateResponse(rawText);
120
+ } catch (err) {
121
+ let errorCode: number | undefined;
122
+ try {
123
+ const json = JSON.parse(trimJsonEnvelope(rawText));
124
+ errorCode = extractErrorCode(json);
125
+ } catch {}
126
+ return {
127
+ text: "",
128
+ errorCode,
129
+ errorMessage: err instanceof Error ? err.message : String(err),
130
+ };
131
+ }
132
+ }
133
+
134
+ async function fetchAccessToken(
135
+ cookieHeader: string,
136
+ signal: AbortSignal,
137
+ ): Promise<string> {
138
+ const html = await fetchWithCookieRedirects(GEMINI_APP_URL, cookieHeader, 10, signal);
139
+
140
+ for (const key of ["SNlM0e", "thykhd"]) {
141
+ const match = html.match(new RegExp(`"${key}":"(.*?)"`));
142
+ if (match?.[1]) return match[1];
143
+ }
144
+
145
+ throw new Error("Unable to authenticate with Gemini. Make sure you're signed into gemini.google.com in Chrome.");
146
+ }
147
+
148
+ async function fetchWithCookieRedirects(
149
+ url: string,
150
+ cookieHeader: string,
151
+ maxRedirects: number,
152
+ signal: AbortSignal,
153
+ ): Promise<string> {
154
+ let current = url;
155
+ for (let i = 0; i <= maxRedirects; i++) {
156
+ const res = await fetch(current, {
157
+ headers: { "user-agent": USER_AGENT, cookie: cookieHeader },
158
+ redirect: "manual",
159
+ signal,
160
+ });
161
+ if (res.status >= 300 && res.status < 400) {
162
+ const location = res.headers.get("location");
163
+ if (location) {
164
+ current = new URL(location, current).toString();
165
+ continue;
166
+ }
167
+ }
168
+ return await res.text();
169
+ }
170
+ throw new Error(`Too many redirects (>${maxRedirects})`);
171
+ }
172
+
173
+ async function uploadFile(
174
+ filePath: string,
175
+ cookieHeader: string,
176
+ signal: AbortSignal,
177
+ ): Promise<{ id: string; name: string }> {
178
+ const { readFileSync } = await import("node:fs");
179
+ const { basename } = await import("node:path");
180
+
181
+ const data = readFileSync(filePath);
182
+ const fileName = basename(filePath);
183
+ const boundary = "----FormBoundary" + Math.random().toString(36).slice(2);
184
+ const header = `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${fileName}"\r\nContent-Type: application/octet-stream\r\n\r\n`;
185
+ const footer = `\r\n--${boundary}--\r\n`;
186
+
187
+ const body = Buffer.concat([
188
+ Buffer.from(header, "utf-8"),
189
+ data,
190
+ Buffer.from(footer, "utf-8"),
191
+ ]);
192
+
193
+ const res = await fetch(GEMINI_UPLOAD_URL, {
194
+ method: "POST",
195
+ headers: {
196
+ "content-type": `multipart/form-data; boundary=${boundary}`,
197
+ "push-id": GEMINI_UPLOAD_PUSH_ID,
198
+ "user-agent": USER_AGENT,
199
+ cookie: cookieHeader,
200
+ },
201
+ body,
202
+ signal,
203
+ });
204
+
205
+ if (!res.ok) {
206
+ const text = await res.text();
207
+ throw new Error(`File upload failed: ${res.status} (${text.slice(0, 200)})`);
208
+ }
209
+
210
+ return { id: await res.text(), name: fileName };
211
+ }
212
+
213
+ function buildFReqPayload(
214
+ prompt: string,
215
+ uploaded: Array<{ id: string; name: string }>,
216
+ ): string {
217
+ const promptPayload =
218
+ uploaded.length > 0
219
+ ? [prompt, 0, null, uploaded.map((file) => [[file.id, 1]])]
220
+ : [prompt];
221
+ const innerList = [promptPayload, null, null];
222
+ return JSON.stringify([null, JSON.stringify(innerList)]);
223
+ }
224
+
225
+ function withTimeout(signal: AbortSignal | undefined, timeoutMs: number): AbortSignal {
226
+ const timeout = AbortSignal.timeout(timeoutMs);
227
+ return signal ? AbortSignal.any([signal, timeout]) : timeout;
228
+ }
229
+
230
+ function buildCookieHeader(cookieMap: CookieMap): string {
231
+ return Object.entries(cookieMap)
232
+ .filter(([, value]) => typeof value === "string" && value.length > 0)
233
+ .map(([name, value]) => `${name}=${value}`)
234
+ .join("; ");
235
+ }
236
+
237
+ function getNestedValue(value: unknown, pathParts: number[]): unknown {
238
+ let current: unknown = value;
239
+ for (const part of pathParts) {
240
+ if (current == null) return undefined;
241
+ if (!Array.isArray(current)) return undefined;
242
+ current = (current as unknown[])[part];
243
+ }
244
+ return current;
245
+ }
246
+
247
+ function trimJsonEnvelope(text: string): string {
248
+ const start = text.indexOf("[");
249
+ const end = text.lastIndexOf("]");
250
+ if (start === -1 || end === -1 || end <= start) {
251
+ throw new Error("Gemini response did not contain a JSON payload.");
252
+ }
253
+ return text.slice(start, end + 1);
254
+ }
255
+
256
+ function extractErrorCode(responseJson: unknown): number | undefined {
257
+ const code = getNestedValue(responseJson, [0, 5, 2, 0, 1, 0]);
258
+ return typeof code === "number" && code >= 0 ? code : undefined;
259
+ }
260
+
261
+ function isModelUnavailable(errorCode: number | undefined): boolean {
262
+ return errorCode === 1052;
263
+ }
264
+
265
+ function parseStreamGenerateResponse(rawText: string): GeminiWebResult {
266
+ const responseJson = JSON.parse(trimJsonEnvelope(rawText));
267
+ const errorCode = extractErrorCode(responseJson);
268
+
269
+ const parts = Array.isArray(responseJson) ? responseJson : [];
270
+ let body: unknown = null;
271
+
272
+ for (let i = 0; i < parts.length; i++) {
273
+ const partBody = getNestedValue(parts[i], [2]);
274
+ if (!partBody || typeof partBody !== "string") continue;
275
+ try {
276
+ const parsed = JSON.parse(partBody);
277
+ const candidateList = getNestedValue(parsed, [4]);
278
+ if (Array.isArray(candidateList) && candidateList.length > 0) {
279
+ body = parsed;
280
+ break;
281
+ }
282
+ } catch {}
283
+ }
284
+
285
+ const candidateList = getNestedValue(body, [4]);
286
+ const firstCandidate = Array.isArray(candidateList) ? (candidateList as unknown[])[0] : undefined;
287
+ const textRaw = getNestedValue(firstCandidate, [1, 0]) as string | undefined;
288
+
289
+ let text = textRaw ?? "";
290
+ if (/^http:\/\/googleusercontent\.com\/card_content\/\d+/.test(text)) {
291
+ const alt = getNestedValue(firstCandidate, [22, 0]) as string | undefined;
292
+ if (alt) text = alt;
293
+ }
294
+
295
+ return { text, errorCode };
296
+ }
package/github-api.ts CHANGED
@@ -41,7 +41,7 @@ export async function checkRepoSize(owner: string, repo: string): Promise<number
41
41
  });
42
42
  }
43
43
 
44
- export async function getDefaultBranch(owner: string, repo: string): Promise<string | null> {
44
+ async function getDefaultBranch(owner: string, repo: string): Promise<string | null> {
45
45
  if (!(await checkGhAvailable())) return null;
46
46
 
47
47
  return new Promise((resolve) => {
@@ -186,9 +186,10 @@ export async function fetchViaApi(
186
186
 
187
187
  lines.push("This is an API-only view. Clone the repo or use `read`/`bash` for deeper exploration.");
188
188
 
189
+ const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
189
190
  return {
190
191
  url,
191
- title: `${owner}/${repo}`,
192
+ title,
192
193
  content: lines.join("\n"),
193
194
  error: null,
194
195
  };