decant-core 1.2.1 → 1.2.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.
- package/ai/gemini.js +31 -2
- package/package.json +1 -1
package/ai/gemini.js
CHANGED
|
@@ -4,6 +4,16 @@ import { convertToMarkdown } from "../utils/html-to-markdown.js";
|
|
|
4
4
|
const GEMINI_RPC_ID = "hNvQHb";
|
|
5
5
|
const DEFAULT_BARD_PATH = "/_/BardChatUi";
|
|
6
6
|
|
|
7
|
+
function isValidMessageText(str, convoId = "") {
|
|
8
|
+
if (typeof str !== "string") return false;
|
|
9
|
+
const trimmed = str.trim();
|
|
10
|
+
if (!trimmed) return false;
|
|
11
|
+
if (/^(?:c_|rc_|r_)[a-zA-Z0-9_-]+$/.test(trimmed)) return false;
|
|
12
|
+
if (convoId && (trimmed === convoId || trimmed === `c_${convoId}`))
|
|
13
|
+
return false;
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
export class GeminiParser extends ChatParser {
|
|
8
18
|
name = "Gemini";
|
|
9
19
|
|
|
@@ -84,7 +94,23 @@ export class GeminiParser extends ChatParser {
|
|
|
84
94
|
|
|
85
95
|
const mode = options.parserMode || "auto";
|
|
86
96
|
|
|
87
|
-
//
|
|
97
|
+
// 1. Prefer DOM extraction first when on a live page with conversation containers
|
|
98
|
+
if (typeof document !== "undefined" && document.querySelector) {
|
|
99
|
+
const hasDomMessages = document.querySelector(
|
|
100
|
+
".conversation-container, user-query, model-response, deep-research-immersive-panel",
|
|
101
|
+
);
|
|
102
|
+
if (hasDomMessages && mode !== "api") {
|
|
103
|
+
const domResult = this.parseFromDom(currentUrl, options);
|
|
104
|
+
if (domResult && domResult.messages && domResult.messages.length > 0) {
|
|
105
|
+
console.log(
|
|
106
|
+
`[Gemini Parser] Successfully parsed ${domResult.messages.length} messages from DOM`,
|
|
107
|
+
);
|
|
108
|
+
return domResult;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 2. Attempt API / RPC extraction if DOM parsing didn't find messages or mode is API
|
|
88
114
|
if (mode !== "dom" && typeof fetch === "function") {
|
|
89
115
|
try {
|
|
90
116
|
const convoId = this.getConversationId(currentUrl);
|
|
@@ -104,7 +130,10 @@ export class GeminiParser extends ChatParser {
|
|
|
104
130
|
if (
|
|
105
131
|
apiResult &&
|
|
106
132
|
apiResult.messages &&
|
|
107
|
-
apiResult.messages.length > 0
|
|
133
|
+
apiResult.messages.length > 0 &&
|
|
134
|
+
apiResult.messages.some((m) =>
|
|
135
|
+
isValidMessageText(m.content, convoId),
|
|
136
|
+
)
|
|
108
137
|
) {
|
|
109
138
|
console.log(
|
|
110
139
|
`[Gemini Parser] Successfully parsed ${apiResult.messages.length} messages via API`,
|