@chainlesschain/personal-data-hub 0.1.0 → 0.2.1

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 (154) hide show
  1. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +211 -0
  2. package/__tests__/adapters/ai-chat-health-checker.test.js +262 -0
  3. package/__tests__/adapters/ai-chat-history.test.js +396 -0
  4. package/__tests__/adapters/ai-chat-http-client.test.js +242 -0
  5. package/__tests__/adapters/ai-chat-vendors.test.js +874 -0
  6. package/__tests__/adapters/alipay-bill-adapter.test.js +538 -0
  7. package/__tests__/adapters/email-adapter.test.js +138 -1
  8. package/__tests__/adapters/email-classifier.test.js +347 -0
  9. package/__tests__/adapters/email-pdf-extractor.test.js +529 -0
  10. package/__tests__/adapters/email-retry-progress.test.js +294 -0
  11. package/__tests__/adapters/email-templates.test.js +699 -0
  12. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +269 -0
  13. package/__tests__/adapters/system-data-adapter.test.js +440 -0
  14. package/__tests__/adapters/system-data-android-ingest.test.js +144 -0
  15. package/__tests__/adapters/system-data-android.test.js +387 -0
  16. package/__tests__/adapters/system-data-disclosure.test.js +153 -0
  17. package/__tests__/adapters/wechat-bootstrap.test.js +240 -0
  18. package/__tests__/adapters/wechat-env-probe.test.js +162 -0
  19. package/__tests__/adapters/wechat-frida-agent.test.js +191 -0
  20. package/__tests__/adapters/wechat-frida-integration.test.js +149 -0
  21. package/__tests__/adapters/wechat-frida-key-provider.test.js +188 -0
  22. package/__tests__/adapters/wechat-md5-key-provider.test.js +101 -0
  23. package/__tests__/analysis-skills.test.js +556 -0
  24. package/__tests__/analysis.test.js +329 -1
  25. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +213 -0
  26. package/__tests__/e2e/full-user-journey.test.js +188 -0
  27. package/__tests__/entity-resolver-ingest-hook.test.js +177 -0
  28. package/__tests__/entity-resolver-stages.test.js +411 -0
  29. package/__tests__/entity-resolver-vault.test.js +246 -0
  30. package/__tests__/entity-resolver.test.js +526 -0
  31. package/__tests__/fixtures/entity-resolver-200-mock.json +96 -0
  32. package/__tests__/integration/ai-chat-history-registry.test.js +228 -0
  33. package/__tests__/integration/aichat-wizard-end-to-end.test.js +282 -0
  34. package/__tests__/integration/cross-adapter-pipelines.test.js +396 -0
  35. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +390 -0
  36. package/__tests__/longtail-adapters.test.js +217 -0
  37. package/__tests__/mobile-extractor.test.js +288 -0
  38. package/__tests__/registry.test.js +4 -2
  39. package/__tests__/shopping-adapters.test.js +296 -0
  40. package/__tests__/sidecar-contacts-cross-validate.test.js +163 -0
  41. package/__tests__/sidecar-supervisor.test.js +120 -0
  42. package/__tests__/social-adapters.test.js +206 -0
  43. package/__tests__/travel-adapters.test.js +325 -0
  44. package/__tests__/vault.test.js +3 -3
  45. package/__tests__/wechat-adapter.test.js +476 -0
  46. package/__tests__/whatsapp-adapter.test.js +135 -0
  47. package/lib/adapter-spec.js +12 -0
  48. package/lib/adapters/_python-sidecar-base.js +207 -0
  49. package/lib/adapters/ai-chat-history/ai-chat-adapter.js +374 -0
  50. package/lib/adapters/ai-chat-history/cookie-auth.js +109 -0
  51. package/lib/adapters/ai-chat-history/cookie-capture-spec.js +331 -0
  52. package/lib/adapters/ai-chat-history/health-checker.js +210 -0
  53. package/lib/adapters/ai-chat-history/http-client.js +211 -0
  54. package/lib/adapters/ai-chat-history/index.js +28 -0
  55. package/lib/adapters/ai-chat-history/schema-map.js +258 -0
  56. package/lib/adapters/ai-chat-history/vendor-spec.js +86 -0
  57. package/lib/adapters/ai-chat-history/vendors/coze.js +179 -0
  58. package/lib/adapters/ai-chat-history/vendors/deepseek.js +199 -0
  59. package/lib/adapters/ai-chat-history/vendors/doubao.js +255 -0
  60. package/lib/adapters/ai-chat-history/vendors/dreamina.js +174 -0
  61. package/lib/adapters/ai-chat-history/vendors/hunyuan.js +176 -0
  62. package/lib/adapters/ai-chat-history/vendors/kimi.js +182 -0
  63. package/lib/adapters/ai-chat-history/vendors/qianfan.js +160 -0
  64. package/lib/adapters/ai-chat-history/vendors/tongyi.js +193 -0
  65. package/lib/adapters/ai-chat-history/vendors/zhipu.js +202 -0
  66. package/lib/adapters/ai-chat-history/wizard-controller.js +473 -0
  67. package/lib/adapters/alipay-bill/alipay-bill-adapter.js +311 -0
  68. package/lib/adapters/alipay-bill/counterparty.js +129 -0
  69. package/lib/adapters/alipay-bill/csv-parser.js +217 -0
  70. package/lib/adapters/alipay-bill/index.js +41 -0
  71. package/lib/adapters/alipay-bill/zip-decryptor.js +111 -0
  72. package/lib/adapters/email-imap/classifier.js +495 -0
  73. package/lib/adapters/email-imap/email-adapter.js +419 -8
  74. package/lib/adapters/email-imap/index.js +42 -0
  75. package/lib/adapters/email-imap/pdf-extractor.js +192 -0
  76. package/lib/adapters/email-imap/templates/bill.js +232 -0
  77. package/lib/adapters/email-imap/templates/government.js +120 -0
  78. package/lib/adapters/email-imap/templates/index.js +78 -0
  79. package/lib/adapters/email-imap/templates/order.js +186 -0
  80. package/lib/adapters/email-imap/templates/other.js +114 -0
  81. package/lib/adapters/email-imap/templates/register.js +113 -0
  82. package/lib/adapters/email-imap/templates/travel.js +157 -0
  83. package/lib/adapters/email-imap/templates/utils.js +275 -0
  84. package/lib/adapters/email-imap/transactions.js +234 -0
  85. package/lib/adapters/messaging-qq/index.js +158 -0
  86. package/lib/adapters/messaging-telegram/index.js +142 -0
  87. package/lib/adapters/messaging-whatsapp/index.js +189 -0
  88. package/lib/adapters/shopping-base/index.js +208 -0
  89. package/lib/adapters/shopping-jd/index.js +150 -0
  90. package/lib/adapters/shopping-meituan/index.js +154 -0
  91. package/lib/adapters/shopping-taobao/index.js +176 -0
  92. package/lib/adapters/social-bilibili/index.js +171 -0
  93. package/lib/adapters/social-douyin/index.js +116 -0
  94. package/lib/adapters/social-kuaishou/index.js +237 -0
  95. package/lib/adapters/social-toutiao/index.js +236 -0
  96. package/lib/adapters/social-weibo/index.js +164 -0
  97. package/lib/adapters/social-xiaohongshu/index.js +96 -0
  98. package/lib/adapters/system-data/disclosure.js +166 -0
  99. package/lib/adapters/system-data/index.js +34 -0
  100. package/lib/adapters/system-data/system-data-adapter.js +344 -0
  101. package/lib/adapters/system-data-android/adapter.js +348 -0
  102. package/lib/adapters/system-data-android/index.js +76 -0
  103. package/lib/adapters/travel-12306/index.js +151 -0
  104. package/lib/adapters/travel-amap/index.js +164 -0
  105. package/lib/adapters/travel-baidu-map/index.js +162 -0
  106. package/lib/adapters/travel-base/index.js +240 -0
  107. package/lib/adapters/travel-ctrip/index.js +151 -0
  108. package/lib/adapters/wechat/bootstrap.js +146 -0
  109. package/lib/adapters/wechat/content-parser.js +326 -0
  110. package/lib/adapters/wechat/db-reader.js +209 -0
  111. package/lib/adapters/wechat/env-probe.js +218 -0
  112. package/lib/adapters/wechat/frida-agent/loader.js +67 -0
  113. package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +126 -0
  114. package/lib/adapters/wechat/index.js +37 -0
  115. package/lib/adapters/wechat/key-extractor.js +158 -0
  116. package/lib/adapters/wechat/key-providers/frida-key-provider.js +244 -0
  117. package/lib/adapters/wechat/key-providers/index.js +22 -0
  118. package/lib/adapters/wechat/key-providers/key-provider-base.js +44 -0
  119. package/lib/adapters/wechat/key-providers/md5-key-provider.js +81 -0
  120. package/lib/adapters/wechat/normalize.js +220 -0
  121. package/lib/adapters/wechat/wechat-adapter.js +205 -0
  122. package/lib/analysis-skills/base.js +113 -0
  123. package/lib/analysis-skills/footprint.js +167 -0
  124. package/lib/analysis-skills/index.js +58 -0
  125. package/lib/analysis-skills/interests.js +161 -0
  126. package/lib/analysis-skills/relations.js +226 -0
  127. package/lib/analysis-skills/spending.js +219 -0
  128. package/lib/analysis-skills/timeline.js +167 -0
  129. package/lib/analysis.js +191 -2
  130. package/lib/entity-resolver/embedding-stage.js +198 -0
  131. package/lib/entity-resolver/entity-resolver.js +384 -0
  132. package/lib/entity-resolver/index.js +42 -0
  133. package/lib/entity-resolver/llm-stage.js +191 -0
  134. package/lib/entity-resolver/rule-stage.js +208 -0
  135. package/lib/entity-resolver/worker.js +149 -0
  136. package/lib/index.js +131 -0
  137. package/lib/migrations.js +73 -0
  138. package/lib/mobile-extractor/android.js +193 -0
  139. package/lib/mobile-extractor/index.js +9 -0
  140. package/lib/mobile-extractor/ios.js +223 -0
  141. package/lib/prompt-builder.js +11 -1
  142. package/lib/query-parser.js +7 -1
  143. package/lib/registry.js +42 -0
  144. package/lib/sidecar/index.js +15 -0
  145. package/lib/sidecar/supervisor.js +359 -0
  146. package/lib/vault.js +343 -0
  147. package/package.json +36 -3
  148. package/scripts/_make-fixture-all.js +126 -0
  149. package/scripts/_make-fixture-contacts.js +84 -0
  150. package/scripts/evaluate-entity-resolver.js +213 -0
  151. package/scripts/smoke-phase-5-5.js +196 -0
  152. package/scripts/smoke-phase-5-7.js +181 -0
  153. package/scripts/smoke-system-data-contacts.js +309 -0
  154. package/scripts/smoke-system-data.js +312 -0
@@ -0,0 +1,193 @@
1
+ /**
2
+ * 通义千问 (Aliyun Tongyi) vendor adapter — Phase 10.2 wiring.
3
+ *
4
+ * Reference: docs/design/Adapter_AIChat_History.md §6.3
5
+ * - login https://tongyi.aliyun.com/
6
+ * - convs POST /dialog/conversation/list
7
+ * - msgs POST /dialog/conversation/messages
8
+ * - X-Csrf-Token header required (from XSRF-TOKEN cookie)
9
+ * - 阿里通用 anti-bot — conservative rateLimits.
10
+ *
11
+ * Endpoints + payload shapes are reverse-engineered and SUBJECT TO DRIFT.
12
+ * Tests use the same fixture-fetch pattern as DeepSeek + Kimi so wire breakage
13
+ * surfaces immediately when a real cookie is reconnected.
14
+ */
15
+
16
+ "use strict";
17
+
18
+ const BASE = "https://tongyi.aliyun.com";
19
+ const CONV_LIST_PATH = "/dialog/conversation/list";
20
+ const MSG_LIST_PATH = "/dialog/conversation/messages";
21
+ const USER_INFO_PATH = "/api/user/info";
22
+
23
+ const DEFAULT_PAGE_SIZE = 30;
24
+
25
+ function _ensureClient(ctx) {
26
+ if (!ctx || !ctx.httpClient) {
27
+ throw new Error("tongyi: ctx.httpClient required");
28
+ }
29
+ return ctx.httpClient;
30
+ }
31
+
32
+ function _csrfHeader(session) {
33
+ // Tongyi reads CSRF from a cookie + echoes it back as a header.
34
+ const value = session && (session.get("XSRF-TOKEN") || session.get("_csrf"));
35
+ return value ? { "X-Csrf-Token": value, "X-Xsrf-Token": value } : {};
36
+ }
37
+
38
+ async function validateCookie(ctx) {
39
+ const client = _ensureClient(ctx);
40
+ try {
41
+ const data = await client.getJson(BASE + USER_INFO_PATH, {
42
+ session: ctx.session,
43
+ headers: _csrfHeader(ctx.session),
44
+ });
45
+ if (data && (data.success || data.code === 200) && (data.data || data.userId)) {
46
+ const userId = (data.data && (data.data.userId || data.data.uid)) || data.userId;
47
+ return { ok: true, userId };
48
+ }
49
+ return { ok: false, reason: "UNEXPECTED_RESPONSE_SHAPE" };
50
+ } catch (err) {
51
+ return { ok: false, reason: err.code || err.message };
52
+ }
53
+ }
54
+
55
+ async function *listConversations(ctx, opts = {}) {
56
+ const client = _ensureClient(ctx);
57
+ const pageSize = Number.isFinite(opts.pageSize) ? opts.pageSize : DEFAULT_PAGE_SIZE;
58
+ const sinceTs = opts.since && opts.since.lastUpdatedAt ? Number(opts.since.lastUpdatedAt) : 0;
59
+
60
+ let pageNum = 1;
61
+ while (true) {
62
+ const body = { pageNum, pageSize };
63
+ const data = await client.postJson(BASE + CONV_LIST_PATH, body, {
64
+ session: ctx.session,
65
+ headers: _csrfHeader(ctx.session),
66
+ });
67
+ const items = _extractList(data);
68
+ if (items.length === 0) return;
69
+
70
+ let stopped = false;
71
+ for (const c of items) {
72
+ const updatedAt = _toMs(c.gmtModified || c.updatedAt || c.gmtCreate);
73
+ if (sinceTs && updatedAt <= sinceTs) {
74
+ stopped = true;
75
+ break;
76
+ }
77
+ yield {
78
+ vendor: "tongyi",
79
+ originalId: String(c.sessionId || c.id),
80
+ title: c.summary || c.firstQuery || c.title || undefined,
81
+ modelName: c.modelName || c.model || undefined,
82
+ createdAt: _toMs(c.gmtCreate || c.createdAt),
83
+ updatedAt,
84
+ messageCount: c.messageCount || undefined,
85
+ archived: Boolean(c.archived),
86
+ extra: { agentName: c.agentName || c.botName },
87
+ };
88
+ }
89
+ if (stopped) return;
90
+ if (items.length < pageSize) return;
91
+ pageNum++;
92
+ if (pageNum > 200) return; // safety
93
+ }
94
+ }
95
+
96
+ async function *listMessages(ctx, conversationId, _opts = {}) {
97
+ const client = _ensureClient(ctx);
98
+ const body = { sessionId: String(conversationId), parentMsgId: "" };
99
+ const data = await client.postJson(BASE + MSG_LIST_PATH, body, {
100
+ session: ctx.session,
101
+ headers: _csrfHeader(ctx.session),
102
+ });
103
+ const msgs = _extractList(data);
104
+
105
+ // Tongyi returns messages mixed user/bot in their own order; we sort
106
+ // ascending by createTime for stable chronological yield.
107
+ msgs.sort((a, b) => _toMs(a.createTime || a.gmtCreate) - _toMs(b.createTime || b.gmtCreate));
108
+
109
+ for (const m of msgs) {
110
+ yield {
111
+ vendor: "tongyi",
112
+ originalId: String(m.msgId || m.id),
113
+ conversationId: String(conversationId),
114
+ role: _normalizeRole(m.senderType || m.role || (m.contentType === "user" ? "user" : "assistant")),
115
+ content: _buildContent(m),
116
+ createdAt: _toMs(m.createTime || m.gmtCreate),
117
+ parentMessageId: m.parentMsgId ? String(m.parentMsgId) : undefined,
118
+ modelName: m.modelName || m.model || undefined,
119
+ extra: m.feedback ? { feedback: m.feedback } : undefined,
120
+ };
121
+ }
122
+ }
123
+
124
+ function _extractList(data) {
125
+ if (!data) return [];
126
+ if (Array.isArray(data.data)) return data.data;
127
+ if (data.data && Array.isArray(data.data.list)) return data.data.list;
128
+ if (data.data && Array.isArray(data.data.records)) return data.data.records;
129
+ if (Array.isArray(data.list)) return data.list;
130
+ return [];
131
+ }
132
+
133
+ function _normalizeRole(r) {
134
+ if (r === "user" || r === "USER" || r === 1) return "user";
135
+ if (r === "assistant" || r === "ASSISTANT" || r === "bot" || r === 2) return "assistant";
136
+ if (r === "system" || r === "SYSTEM" || r === 0) return "system";
137
+ return r ? String(r).toLowerCase() : "assistant";
138
+ }
139
+
140
+ function _buildContent(m) {
141
+ // Tongyi messages have content fanned across `contents[].content` for
142
+ // multi-segment replies (image + text). Flatten to single text.
143
+ const segments = Array.isArray(m.contents) ? m.contents : null;
144
+ const text = segments
145
+ ? segments.map((s) => s.content || "").filter(Boolean).join("\n")
146
+ : (m.content || "");
147
+ const content = { text };
148
+
149
+ const files = []
150
+ .concat(Array.isArray(m.fileList) ? m.fileList : [])
151
+ .concat(Array.isArray(m.imageList) ? m.imageList : []);
152
+ if (files.length > 0) {
153
+ content.attachments = files
154
+ .map((f) => ({
155
+ type: (f.type === "image" || /image/i.test(f.fileType || "")) ? "image" : "file",
156
+ filename: f.fileName || f.name,
157
+ url: f.url || f.fileUrl,
158
+ size: f.size,
159
+ mimeType: f.fileType,
160
+ }))
161
+ .filter((a) => a.url || a.filename);
162
+ }
163
+ return content;
164
+ }
165
+
166
+ function _toMs(t) {
167
+ if (typeof t === "number") return t > 1e12 ? t : t * 1000;
168
+ if (typeof t === "string") {
169
+ const n = Number(t);
170
+ if (Number.isFinite(n)) return n > 1e12 ? n : n * 1000;
171
+ const d = Date.parse(t);
172
+ return Number.isFinite(d) ? d : 0;
173
+ }
174
+ return 0;
175
+ }
176
+
177
+ const SPEC = {
178
+ name: "tongyi",
179
+ displayName: "通义千问",
180
+ androidPackage: "com.aliyun.tongyi",
181
+ loginUrl: "https://tongyi.aliyun.com/",
182
+ cookieDomains: ["tongyi.aliyun.com", ".aliyun.com"],
183
+ rateLimits: { perMinute: 20, minIntervalMs: 2000 }, // Aliyun anti-bot — conservative
184
+
185
+ validateCookie,
186
+ listConversations,
187
+ listMessages,
188
+ };
189
+
190
+ module.exports = {
191
+ SPEC,
192
+ _internal: { _toMs, _normalizeRole, _buildContent, _csrfHeader, _extractList, BASE },
193
+ };
@@ -0,0 +1,202 @@
1
+ /**
2
+ * 智谱清言 (Zhipu Qingyan) vendor adapter — Phase 10.2 wiring.
3
+ *
4
+ * Reference: docs/design/Adapter_AIChat_History.md §6.4
5
+ * - login https://chatglm.cn/
6
+ * - convs GET /chatglm/backend-api/v1/conversation/list
7
+ * - msgs GET /chatglm/backend-api/v1/conversation/<id>
8
+ * - cookie chatglm_token (Bearer-style — re-projected from cookie when
9
+ * present; otherwise relies on cookie auth alone)
10
+ *
11
+ * GLM-4 messages have optional `tool_calls` for web-search injection; we
12
+ * thread those into RawMessage.extra.toolCalls so schema-map preserves them
13
+ * across the KG / RAG flow.
14
+ *
15
+ * Endpoint shapes are reverse-engineered + SUBJECT TO DRIFT.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ const BASE = "https://chatglm.cn";
21
+ const CONV_LIST_PATH = "/chatglm/backend-api/v1/conversation/list";
22
+ const CONV_DETAIL_PATH = "/chatglm/backend-api/v1/conversation/";
23
+ const USER_INFO_PATH = "/chatglm/backend-api/v1/user/info";
24
+
25
+ const DEFAULT_PAGE_SIZE = 30;
26
+
27
+ function _ensureClient(ctx) {
28
+ if (!ctx || !ctx.httpClient) {
29
+ throw new Error("zhipu: ctx.httpClient required");
30
+ }
31
+ return ctx.httpClient;
32
+ }
33
+
34
+ function _authHeader(session) {
35
+ const token = session && session.get("chatglm_token");
36
+ return token ? { Authorization: `Bearer ${token}` } : {};
37
+ }
38
+
39
+ async function validateCookie(ctx) {
40
+ const client = _ensureClient(ctx);
41
+ try {
42
+ const data = await client.getJson(BASE + USER_INFO_PATH, {
43
+ session: ctx.session,
44
+ headers: _authHeader(ctx.session),
45
+ });
46
+ if (data && data.status === 0 && data.result) {
47
+ return { ok: true, userId: data.result.user_id || data.result.id };
48
+ }
49
+ if (data && (data.user_id || data.id)) {
50
+ return { ok: true, userId: data.user_id || data.id };
51
+ }
52
+ return { ok: false, reason: "UNEXPECTED_RESPONSE_SHAPE" };
53
+ } catch (err) {
54
+ return { ok: false, reason: err.code || err.message };
55
+ }
56
+ }
57
+
58
+ async function *listConversations(ctx, opts = {}) {
59
+ const client = _ensureClient(ctx);
60
+ const pageSize = Number.isFinite(opts.pageSize) ? opts.pageSize : DEFAULT_PAGE_SIZE;
61
+ const sinceTs = opts.since && opts.since.lastUpdatedAt ? Number(opts.since.lastUpdatedAt) : 0;
62
+
63
+ let page = 1;
64
+ while (true) {
65
+ const url = new URL(BASE + CONV_LIST_PATH);
66
+ url.searchParams.set("page", String(page));
67
+ url.searchParams.set("page_size", String(pageSize));
68
+
69
+ const data = await client.getJson(url.toString(), {
70
+ session: ctx.session,
71
+ headers: _authHeader(ctx.session),
72
+ });
73
+ const list = _extractList(data);
74
+ if (list.length === 0) return;
75
+
76
+ let stopped = false;
77
+ for (const c of list) {
78
+ const updatedAt = _toMs(c.update_time || c.updateTime || c.create_time);
79
+ if (sinceTs && updatedAt <= sinceTs) {
80
+ stopped = true;
81
+ break;
82
+ }
83
+ yield {
84
+ vendor: "zhipu",
85
+ originalId: String(c.conversation_id || c.id),
86
+ title: c.title || c.summary || undefined,
87
+ modelName: c.model || c.model_name || undefined,
88
+ createdAt: _toMs(c.create_time || c.createTime),
89
+ updatedAt,
90
+ messageCount: c.message_count || undefined,
91
+ archived: Boolean(c.archived),
92
+ extra: { assistantId: c.assistant_id },
93
+ };
94
+ }
95
+ if (stopped) return;
96
+ if (list.length < pageSize) return;
97
+ page++;
98
+ if (page > 200) return;
99
+ }
100
+ }
101
+
102
+ async function *listMessages(ctx, conversationId, _opts = {}) {
103
+ const client = _ensureClient(ctx);
104
+ const url = BASE + CONV_DETAIL_PATH + encodeURIComponent(conversationId);
105
+ const data = await client.getJson(url, {
106
+ session: ctx.session,
107
+ headers: _authHeader(ctx.session),
108
+ });
109
+
110
+ // GLM detail returns { result: { messages: [...] } } or { messages: [...] }.
111
+ let msgs = [];
112
+ if (data && data.result && Array.isArray(data.result.messages)) msgs = data.result.messages;
113
+ else if (data && Array.isArray(data.messages)) msgs = data.messages;
114
+ else if (data && data.result && Array.isArray(data.result.history)) msgs = data.result.history;
115
+
116
+ msgs.sort((a, b) => _toMs(a.create_time || a.timestamp) - _toMs(b.create_time || b.timestamp));
117
+
118
+ for (const m of msgs) {
119
+ yield {
120
+ vendor: "zhipu",
121
+ originalId: String(m.id || m.message_id),
122
+ conversationId: String(conversationId),
123
+ role: _normalizeRole(m.role || m.sender),
124
+ content: _buildContent(m),
125
+ createdAt: _toMs(m.create_time || m.timestamp),
126
+ parentMessageId: m.parent_id ? String(m.parent_id) : undefined,
127
+ modelName: m.model || undefined,
128
+ extra: {
129
+ toolCalls: Array.isArray(m.tool_calls) ? m.tool_calls : undefined,
130
+ searchQuery: m.search_query,
131
+ },
132
+ };
133
+ }
134
+ }
135
+
136
+ function _extractList(data) {
137
+ if (!data) return [];
138
+ if (Array.isArray(data.result)) return data.result;
139
+ if (data.result && Array.isArray(data.result.list)) return data.result.list;
140
+ if (data.result && Array.isArray(data.result.conversations)) return data.result.conversations;
141
+ if (Array.isArray(data.conversations)) return data.conversations;
142
+ if (Array.isArray(data.list)) return data.list;
143
+ return [];
144
+ }
145
+
146
+ function _normalizeRole(r) {
147
+ if (r === "user" || r === "USER") return "user";
148
+ if (r === "assistant" || r === "ASSISTANT" || r === "bot") return "assistant";
149
+ if (r === "system" || r === "SYSTEM") return "system";
150
+ if (r === "tool" || r === "TOOL") return "tool";
151
+ return r ? String(r).toLowerCase() : "assistant";
152
+ }
153
+
154
+ function _buildContent(m) {
155
+ const content = { text: m.content || m.text || "" };
156
+ if (Array.isArray(m.attachments) && m.attachments.length > 0) {
157
+ content.attachments = m.attachments
158
+ .map((a) => ({
159
+ type: (a.type === "image" || /image/i.test(a.mime_type || "")) ? "image" : "file",
160
+ filename: a.name || a.file_name,
161
+ url: a.url || a.download_url,
162
+ size: a.size,
163
+ mimeType: a.mime_type,
164
+ }))
165
+ .filter((a) => a.url || a.filename);
166
+ }
167
+ if (Array.isArray(m.images) && m.images.length > 0) {
168
+ content.attachments = (content.attachments || []).concat(
169
+ m.images.map((i) => ({ type: "image", url: i.url || i, mimeType: "image/png" })),
170
+ );
171
+ }
172
+ return content;
173
+ }
174
+
175
+ function _toMs(t) {
176
+ if (typeof t === "number") return t > 1e12 ? t : t * 1000;
177
+ if (typeof t === "string") {
178
+ const n = Number(t);
179
+ if (Number.isFinite(n)) return n > 1e12 ? n : n * 1000;
180
+ const d = Date.parse(t);
181
+ return Number.isFinite(d) ? d : 0;
182
+ }
183
+ return 0;
184
+ }
185
+
186
+ const SPEC = {
187
+ name: "zhipu",
188
+ displayName: "智谱清言",
189
+ androidPackage: "com.zhipuai.qingyan",
190
+ loginUrl: "https://chatglm.cn/",
191
+ cookieDomains: ["chatglm.cn", ".chatglm.cn"],
192
+ rateLimits: { perMinute: 20, minIntervalMs: 2000 },
193
+
194
+ validateCookie,
195
+ listConversations,
196
+ listMessages,
197
+ };
198
+
199
+ module.exports = {
200
+ SPEC,
201
+ _internal: { _toMs, _normalizeRole, _buildContent, _authHeader, _extractList, BASE },
202
+ };