@johorion/magpie-quark-kbot 0.1.2 → 0.1.4
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/index.mjs +142 -8
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
AGENT_API_MODEL,
|
|
18
18
|
KBOT_AGENT_API,
|
|
19
19
|
} from "./lib/config.mjs"
|
|
20
|
+
import { createServer } from "node:http"
|
|
20
21
|
import { connect, sleep } from "./lib/cdp.mjs"
|
|
21
22
|
import { launchBrowser, closeBrowser, waitForLogin, defaultProfileDir } from "./lib/quark.mjs"
|
|
22
23
|
import { readQuarkCookies, ProfileLocked, AppBoundEncryption } from "./lib/cookies.mjs"
|
|
@@ -57,6 +58,59 @@ function magpieSessionKey(accountId, sessionId) {
|
|
|
57
58
|
return `${accountId}::${sessionId || "default"}`
|
|
58
59
|
}
|
|
59
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 宽松解析:接受四种粘贴形态,统一成 cookie 名值对。
|
|
63
|
+
*
|
|
64
|
+
* 1. curl 'https://…' -H 'cookie: a=b; c=d' ← DevTools「Copy as cURL」
|
|
65
|
+
* 2. Cookie: a=b; c=d ← 复制请求头
|
|
66
|
+
* 3. a=b; c=d ← 裸 cookie 串
|
|
67
|
+
* 4. [{"name":"a","value":"b"}, …] ← DevTools Application 面板导出的 JSON
|
|
68
|
+
*
|
|
69
|
+
* 之所以要 cURL:__kuus / __uus 是 httpOnly,document.cookie 根本读不到,
|
|
70
|
+
* 只能在 Network 面板里整条请求复制出来。
|
|
71
|
+
*/
|
|
72
|
+
function parseLooseCookies(text) {
|
|
73
|
+
const raw = String(text ?? "").trim()
|
|
74
|
+
if (!raw) return []
|
|
75
|
+
|
|
76
|
+
// 4) JSON 数组
|
|
77
|
+
if (raw.startsWith("[") || raw.startsWith("{")) {
|
|
78
|
+
try {
|
|
79
|
+
const parsed = JSON.parse(raw)
|
|
80
|
+
const list = Array.isArray(parsed) ? parsed : parsed.cookies || []
|
|
81
|
+
const out = []
|
|
82
|
+
for (const c of list) {
|
|
83
|
+
if (c && c.name) out.push(`${c.name}=${c.value ?? ""}`)
|
|
84
|
+
}
|
|
85
|
+
if (out.length) return parseCookieHeader(out.join("; "))
|
|
86
|
+
} catch { /* 不是 JSON,继续 */ }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 1) cURL:优先取 -H/-b 里的 cookie 行
|
|
90
|
+
if (/\bcurl\b/i.test(raw)) {
|
|
91
|
+
const headerMatches = [...raw.matchAll(/-H\s+(['"])([\s\S]*?)\1/g)]
|
|
92
|
+
for (const m of headerMatches) {
|
|
93
|
+
const line = m[2]
|
|
94
|
+
const ci = line.toLowerCase().indexOf("cookie:")
|
|
95
|
+
if (ci >= 0) return parseCookieHeader(line.slice(ci + 7).trim())
|
|
96
|
+
}
|
|
97
|
+
// curl -b 'a=b; c=d'
|
|
98
|
+
const bMatch = raw.match(/-b\s+(['"])([\s\S]*?)\1/) || raw.match(/--cookie\s+(['"])([\s\S]*?)\1/)
|
|
99
|
+
if (bMatch) return parseCookieHeader(bMatch[2].trim())
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 2) Cookie: 前缀(可能带多行头)
|
|
103
|
+
const ci = raw.toLowerCase().indexOf("cookie:")
|
|
104
|
+
if (ci >= 0) {
|
|
105
|
+
const after = raw.slice(ci + 7)
|
|
106
|
+
const line = after.split(/\r?\n/)[0]
|
|
107
|
+
return parseCookieHeader(line.trim())
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 3) 裸串
|
|
111
|
+
return parseCookieHeader(raw)
|
|
112
|
+
}
|
|
113
|
+
|
|
60
114
|
function parseCookieHeader(header) {
|
|
61
115
|
const out = []
|
|
62
116
|
for (const part of String(header ?? "").split(";")) {
|
|
@@ -82,7 +136,57 @@ function parseCookieHeader(header) {
|
|
|
82
136
|
return out
|
|
83
137
|
}
|
|
84
138
|
|
|
85
|
-
|
|
139
|
+
/**
|
|
140
|
+
* 起一个只在本机监听的回调服务,第一次被访问就完成。
|
|
141
|
+
*
|
|
142
|
+
* 用它是为了「登录完立刻结束」:页面登录成功后主动 fetch 这个地址,
|
|
143
|
+
* 插件收到就马上读 cookie 收尾,不必等 8 分钟的轮询窗口。
|
|
144
|
+
*/
|
|
145
|
+
async function startCallbackServer() {
|
|
146
|
+
let resolveHit
|
|
147
|
+
const hit = new Promise((resolve) => { resolveHit = resolve })
|
|
148
|
+
const server = createServer((req, res) => {
|
|
149
|
+
res.writeHead(200, { "content-type": "text/plain", "access-control-allow-origin": "*" })
|
|
150
|
+
res.end("ok")
|
|
151
|
+
resolveHit(true)
|
|
152
|
+
})
|
|
153
|
+
await new Promise((resolve, reject) => {
|
|
154
|
+
server.once("error", reject)
|
|
155
|
+
server.listen(0, "127.0.0.1", resolve)
|
|
156
|
+
})
|
|
157
|
+
const port = server.address().port
|
|
158
|
+
return {
|
|
159
|
+
port,
|
|
160
|
+
hit,
|
|
161
|
+
close() { try { server.close() } catch { /* ignore */ } },
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 页面侧探针:每 800ms 问一次登录态,成功就回调本机并停止。
|
|
166
|
+
function beaconScript(port) {
|
|
167
|
+
return `(function () {
|
|
168
|
+
if (window.__magpieQuarkBeacon) return
|
|
169
|
+
window.__magpieQuarkBeacon = true
|
|
170
|
+
var tries = 0
|
|
171
|
+
var timer = setInterval(async function () {
|
|
172
|
+
tries += 1
|
|
173
|
+
if (tries > 900) { clearInterval(timer); return }
|
|
174
|
+
try {
|
|
175
|
+
var sdk = window.__QIANWEN_CHAT_SDK__
|
|
176
|
+
var ok = sdk && sdk.checkLogin ? await sdk.checkLogin() : false
|
|
177
|
+
if (!ok && window.chrome && chrome.account && chrome.account.getUserInfo) {
|
|
178
|
+
try { var u = await chrome.account.getUserInfo({}); ok = !!(u && (u.data || u).uId) } catch (e) {}
|
|
179
|
+
}
|
|
180
|
+
if (ok) {
|
|
181
|
+
clearInterval(timer)
|
|
182
|
+
try { await fetch("http://127.0.0.1:${port}/cb?ok=1", { mode: "no-cors" }) } catch (e) {}
|
|
183
|
+
}
|
|
184
|
+
} catch (e) {}
|
|
185
|
+
}, 800)
|
|
186
|
+
})()`
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function browserLogin(_inputs, { signal, beaconPort } = {}) {
|
|
86
190
|
let launched
|
|
87
191
|
try {
|
|
88
192
|
// Every login opens a brand-new, signed-out Quark profile. This means
|
|
@@ -101,6 +205,13 @@ async function browserLogin(_inputs, { signal } = {}) {
|
|
|
101
205
|
try { await cdp.send("Storage.clearDataForOrigin", { origin, storageTypes: "all" }) } catch { /* ignore */ }
|
|
102
206
|
}
|
|
103
207
|
await prepareKbotPage(cdp)
|
|
208
|
+
if (beaconPort) {
|
|
209
|
+
// 注入登录成功回调探针:登录一完成就通知本机,比轮询更快收尾。
|
|
210
|
+
try {
|
|
211
|
+
await cdp.send("Runtime.enable")
|
|
212
|
+
await cdp.send("Runtime.evaluate", { expression: beaconScript(beaconPort) })
|
|
213
|
+
} catch { /* 探针失败不影响后面的轮询 */ }
|
|
214
|
+
}
|
|
104
215
|
const { state, cookies } = await waitForLogin(launched.port, {
|
|
105
216
|
cdp,
|
|
106
217
|
signal,
|
|
@@ -178,8 +289,8 @@ async function reuseInstalledBrowser() {
|
|
|
178
289
|
}
|
|
179
290
|
|
|
180
291
|
async function pasteCookieLogin(inputs) {
|
|
181
|
-
const cookies =
|
|
182
|
-
if (!cookies.length) return { type: "failed", error: "Cookie
|
|
292
|
+
const cookies = parseLooseCookies(inputs?.cookie)
|
|
293
|
+
if (!cookies.length) return { type: "failed", error: "没解析出 Cookie,请粘贴 cURL 或包含 __kuus / __uus 的 Cookie 串。" }
|
|
183
294
|
const base = makeBinding(cookies, {})
|
|
184
295
|
base.profileDir = findNativeProfile(base.uid) || ""
|
|
185
296
|
if (!base.profileDir) {
|
|
@@ -682,17 +793,37 @@ export default async function quarkKbotPlugin() {
|
|
|
682
793
|
},
|
|
683
794
|
{
|
|
684
795
|
type: "api",
|
|
685
|
-
label: "
|
|
686
|
-
placeholder: "__kuus=...; __uus
|
|
796
|
+
label: "粘贴回调(cURL / Cookie)",
|
|
797
|
+
placeholder: "curl 'https://kbot.quark.cn/...' -H 'cookie: __kuus=...; __uus=...'",
|
|
687
798
|
prompts: [{
|
|
688
799
|
type: "password",
|
|
689
800
|
key: "cookie",
|
|
690
|
-
message: "
|
|
691
|
-
placeholder: "__kuus=...; __uus
|
|
692
|
-
validate: (v) =>
|
|
801
|
+
message: "在夸克窗口按 F12 → Network → 点任意 kbot.quark.cn 请求 → 右键 Copy → Copy as cURL (bash),整段粘到这里。\n也可以用 Cookie: 开头的请求头,或 DevTools Application 面板导出的 JSON。\n注意:__kuus / __uus 是 httpOnly,document.cookie 读不到,必须走 Network 复制。",
|
|
802
|
+
placeholder: "curl 'https://kbot.quark.cn/...' -H 'cookie: __kuus=...; __uus=...'",
|
|
803
|
+
validate: (v) => {
|
|
804
|
+
const list = parseLooseCookies(v)
|
|
805
|
+
const names = new Set(list.map((c) => c.name))
|
|
806
|
+
return (names.has("__kuus") || names.has("__uus"))
|
|
807
|
+
? undefined
|
|
808
|
+
: "没从粘贴内容里找到 __kuus 或 __uus,请确认复制的是 kbot.quark.cn 的请求。"
|
|
809
|
+
},
|
|
693
810
|
}],
|
|
694
811
|
authorize: pasteCookieLogin,
|
|
695
812
|
},
|
|
813
|
+
{
|
|
814
|
+
type: "api",
|
|
815
|
+
keyless: true,
|
|
816
|
+
label: "浏览器登录(登录完成自动回调本机)",
|
|
817
|
+
placeholder: "打开夸克窗口登录,完成后立即回填,不用等",
|
|
818
|
+
async authorize(_inputs, { signal } = {}) {
|
|
819
|
+
const server = await startCallbackServer()
|
|
820
|
+
try {
|
|
821
|
+
return await browserLogin(_inputs ?? {}, { signal, beaconPort: server.port })
|
|
822
|
+
} finally {
|
|
823
|
+
server.close()
|
|
824
|
+
}
|
|
825
|
+
},
|
|
826
|
+
},
|
|
696
827
|
],
|
|
697
828
|
|
|
698
829
|
async loader(getAuth) {
|
|
@@ -773,4 +904,7 @@ export const _internal = {
|
|
|
773
904
|
chatSessions,
|
|
774
905
|
closeAllSigners,
|
|
775
906
|
parseCookieHeader,
|
|
907
|
+
parseLooseCookies,
|
|
908
|
+
startCallbackServer,
|
|
909
|
+
beaconScript,
|
|
776
910
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johorion/magpie-quark-kbot",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Magpie provider plugin: Quark Kbot models via Quark browser login (Qwen / DeepSeek / GLM / Kimi / MiniMax + Kbot Agent)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|