@johorion/magpie-quark-kbot 0.1.2 → 0.1.3

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 (2) hide show
  1. package/index.mjs +67 -7
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -57,6 +57,59 @@ function magpieSessionKey(accountId, sessionId) {
57
57
  return `${accountId}::${sessionId || "default"}`
58
58
  }
59
59
 
60
+ /**
61
+ * 宽松解析:接受四种粘贴形态,统一成 cookie 名值对。
62
+ *
63
+ * 1. curl 'https://…' -H 'cookie: a=b; c=d' ← DevTools「Copy as cURL」
64
+ * 2. Cookie: a=b; c=d ← 复制请求头
65
+ * 3. a=b; c=d ← 裸 cookie 串
66
+ * 4. [{"name":"a","value":"b"}, …] ← DevTools Application 面板导出的 JSON
67
+ *
68
+ * 之所以要 cURL:__kuus / __uus 是 httpOnly,document.cookie 根本读不到,
69
+ * 只能在 Network 面板里整条请求复制出来。
70
+ */
71
+ function parseLooseCookies(text) {
72
+ const raw = String(text ?? "").trim()
73
+ if (!raw) return []
74
+
75
+ // 4) JSON 数组
76
+ if (raw.startsWith("[") || raw.startsWith("{")) {
77
+ try {
78
+ const parsed = JSON.parse(raw)
79
+ const list = Array.isArray(parsed) ? parsed : parsed.cookies || []
80
+ const out = []
81
+ for (const c of list) {
82
+ if (c && c.name) out.push(`${c.name}=${c.value ?? ""}`)
83
+ }
84
+ if (out.length) return parseCookieHeader(out.join("; "))
85
+ } catch { /* 不是 JSON,继续 */ }
86
+ }
87
+
88
+ // 1) cURL:优先取 -H/-b 里的 cookie 行
89
+ if (/\bcurl\b/i.test(raw)) {
90
+ const headerMatches = [...raw.matchAll(/-H\s+(['"])([\s\S]*?)\1/g)]
91
+ for (const m of headerMatches) {
92
+ const line = m[2]
93
+ const ci = line.toLowerCase().indexOf("cookie:")
94
+ if (ci >= 0) return parseCookieHeader(line.slice(ci + 7).trim())
95
+ }
96
+ // curl -b 'a=b; c=d'
97
+ const bMatch = raw.match(/-b\s+(['"])([\s\S]*?)\1/) || raw.match(/--cookie\s+(['"])([\s\S]*?)\1/)
98
+ if (bMatch) return parseCookieHeader(bMatch[2].trim())
99
+ }
100
+
101
+ // 2) Cookie: 前缀(可能带多行头)
102
+ const ci = raw.toLowerCase().indexOf("cookie:")
103
+ if (ci >= 0) {
104
+ const after = raw.slice(ci + 7)
105
+ const line = after.split(/\r?\n/)[0]
106
+ return parseCookieHeader(line.trim())
107
+ }
108
+
109
+ // 3) 裸串
110
+ return parseCookieHeader(raw)
111
+ }
112
+
60
113
  function parseCookieHeader(header) {
61
114
  const out = []
62
115
  for (const part of String(header ?? "").split(";")) {
@@ -178,8 +231,8 @@ async function reuseInstalledBrowser() {
178
231
  }
179
232
 
180
233
  async function pasteCookieLogin(inputs) {
181
- const cookies = parseCookieHeader(inputs?.cookie)
182
- if (!cookies.length) return { type: "failed", error: "Cookie 为空,请粘贴包含 __kuus 或 __uus 的 Cookie。" }
234
+ const cookies = parseLooseCookies(inputs?.cookie)
235
+ if (!cookies.length) return { type: "failed", error: "没解析出 Cookie,请粘贴 cURL 或包含 __kuus / __uus 的 Cookie 串。" }
183
236
  const base = makeBinding(cookies, {})
184
237
  base.profileDir = findNativeProfile(base.uid) || ""
185
238
  if (!base.profileDir) {
@@ -682,14 +735,20 @@ export default async function quarkKbotPlugin() {
682
735
  },
683
736
  {
684
737
  type: "api",
685
- label: "粘贴夸克 Cookie",
686
- placeholder: "__kuus=...; __uus=...; b-user-id=...",
738
+ label: "粘贴回调(cURL / Cookie)",
739
+ placeholder: "curl 'https://kbot.quark.cn/...' -H 'cookie: __kuus=...; __uus=...'",
687
740
  prompts: [{
688
741
  type: "password",
689
742
  key: "cookie",
690
- message: "粘贴夸克 Cookie(登录 kbot.quark.cn 后从开发者工具复制,需包含 __kuus 或 __uus)",
691
- placeholder: "__kuus=...; __uus=...; b-user-id=...",
692
- validate: (v) => (v && /(__kuus|__uus)=/.test(v)) ? undefined : "Cookie 里必须有 __kuus 或 __uus。",
743
+ message: "在夸克窗口按 F12 → Network → 点任意 kbot.quark.cn 请求 → 右键 Copy → Copy as cURL (bash),整段粘到这里。\n也可以用 Cookie: 开头的请求头,或 DevTools Application 面板导出的 JSON。\n注意:__kuus / __uus 是 httpOnly,document.cookie 读不到,必须走 Network 复制。",
744
+ placeholder: "curl 'https://kbot.quark.cn/...' -H 'cookie: __kuus=...; __uus=...'",
745
+ validate: (v) => {
746
+ const list = parseLooseCookies(v)
747
+ const names = new Set(list.map((c) => c.name))
748
+ return (names.has("__kuus") || names.has("__uus"))
749
+ ? undefined
750
+ : "没从粘贴内容里找到 __kuus 或 __uus,请确认复制的是 kbot.quark.cn 的请求。"
751
+ },
693
752
  }],
694
753
  authorize: pasteCookieLogin,
695
754
  },
@@ -773,4 +832,5 @@ export const _internal = {
773
832
  chatSessions,
774
833
  closeAllSigners,
775
834
  parseCookieHeader,
835
+ parseLooseCookies,
776
836
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johorion/magpie-quark-kbot",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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",