Skip to content

Commit 63ee682

Browse files
authored
Merge pull request #1 from rennzhang/rennzhang/fix-gemini-400-error
fix: resolve Gemini 400 error for multi-account paths
2 parents ad44cba + 904c04e commit 63ee682

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

packages/core-plugins/src/plugins/gemini/parser.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { createAppError } from "@ctxport/core-schema";
22
import type { GeminiRuntimeParams } from "./types";
33

4-
const BATCH_EXECUTE_ENDPOINT =
5-
"https://gemini.google.com/_/BardChatUi/data/batchexecute";
4+
const BATCH_EXECUTE_BASE = "https://gemini.google.com";
65

76
const GEMINI_IMAGE_URL_PATTERN =
87
/^https:\/\/lh3\.googleusercontent\.com\/gg(?:-dl)?\//;
@@ -12,12 +11,13 @@ const GEMINI_IMAGE_URL_PATTERN =
1211
export async function fetchConversationPayload(
1312
conversationId: string,
1413
runtimeParams: GeminiRuntimeParams,
14+
pathPrefix = "",
1515
): Promise<unknown> {
1616
const rpcId = "hNvQHb";
1717

1818
const query = new URLSearchParams({
1919
rpcids: rpcId,
20-
"source-path": `/app/${conversationId}`,
20+
"source-path": `${pathPrefix}/app/${conversationId}`,
2121
bl: runtimeParams.bl,
2222
"f.sid": runtimeParams.fSid,
2323
hl: runtimeParams.hl,
@@ -29,16 +29,7 @@ export async function fetchConversationPayload(
2929
[
3030
[
3131
rpcId,
32-
JSON.stringify([
33-
`c_${conversationId}`,
34-
100,
35-
null,
36-
1,
37-
[0],
38-
[4],
39-
null,
40-
1,
41-
]),
32+
JSON.stringify([`c_${conversationId}`, 10, null, 1, [0], [4], null, 1]),
4233
null,
4334
"generic",
4435
],
@@ -49,24 +40,22 @@ export async function fetchConversationPayload(
4940
body.set("at", runtimeParams.at);
5041
}
5142

52-
const response = await fetch(
53-
`${BATCH_EXECUTE_ENDPOINT}?${query.toString()}`,
54-
{
55-
method: "POST",
56-
mode: "cors",
57-
credentials: "include",
58-
cache: "no-store",
59-
referrer: `https://gemini.google.com/app/${conversationId}`,
60-
referrerPolicy: "strict-origin-when-cross-origin",
61-
headers: {
62-
Accept: "*/*",
63-
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
64-
Origin: "https://gemini.google.com",
65-
"X-Same-Domain": "1",
66-
},
67-
body: body.toString(),
43+
const endpoint = `${BATCH_EXECUTE_BASE}${pathPrefix}/_/BardChatUi/data/batchexecute`;
44+
const response = await fetch(`${endpoint}?${query.toString()}`, {
45+
method: "POST",
46+
mode: "cors",
47+
credentials: "include",
48+
cache: "no-store",
49+
referrer: `https://gemini.google.com${pathPrefix}/app/${conversationId}`,
50+
referrerPolicy: "strict-origin-when-cross-origin",
51+
headers: {
52+
Accept: "*/*",
53+
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
54+
Origin: "https://gemini.google.com",
55+
"X-Same-Domain": "1",
6856
},
69-
);
57+
body: body.toString(),
58+
});
7059

7160
if (!response.ok) {
7261
throw createAppError(

packages/core-plugins/src/plugins/gemini/plugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@ export const geminiPlugin: Plugin = {
2727
throw createAppError("E-PARSE-001", "Not a Gemini conversation page");
2828
}
2929

30+
const pathPrefix = extractPathPrefix(ctx.url);
3031
const runtimeParams = await resolveRuntimeParams(ctx.document);
3132
const payload = await fetchConversationPayload(
3233
conversationId,
3334
runtimeParams,
35+
pathPrefix,
3436
);
3537
return buildContentBundle(payload, ctx.url);
3638
},
3739

3840
async fetchById(conversationId: string): Promise<ContentBundle> {
41+
const pathPrefix = extractPathPrefix(document.location.href);
3942
const runtimeParams = await resolveRuntimeParams(document);
4043
const payload = await fetchConversationPayload(
4144
conversationId,
4245
runtimeParams,
46+
pathPrefix,
4347
);
44-
const url = `https://gemini.google.com/app/${conversationId}`;
48+
const url = `https://gemini.google.com${pathPrefix}/app/${conversationId}`;
4549
return buildContentBundle(payload, url);
4650
},
4751

@@ -81,6 +85,11 @@ function extractConversationId(url: string): string | null {
8185
return match?.[1] ?? null;
8286
}
8387

88+
function extractPathPrefix(url: string): string {
89+
const match = /^https?:\/\/gemini\.google\.com\/(u\/\d+)\//.exec(url);
90+
return match ? `/${match[1]}` : "";
91+
}
92+
8493
async function resolveRuntimeParams(
8594
doc: Document,
8695
): Promise<GeminiRuntimeParams> {

0 commit comments

Comments
 (0)