-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhanced-conversation-saver-part1.js
216 lines (184 loc) · 7.23 KB
/
enhanced-conversation-saver-part1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// ==UserScript==
// @name 增强版三合一会话记录保存
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 集成JSON下载、Markdown转换和Unicode字符清理的增强版会话记录保存工具
// @author You
// @match https://chatgpt.com/c/*
// @match https://abcd.dwai.world/c/*
// @match https://ed.dawuai.buzz/c/*
// @match https://node.dawuai.buzz/c/*
// @match https://newnode.dawuai.buzz/c/*
// @match https://france.dwai.work/c/*
// @match https://dw.dwai.world/c/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 状态追踪
let state = {
largestResponse: null,
largestResponseSize: 0,
largestResponseUrl: null,
lastUpdateTime: null,
markdownContent: null,
modelName: null // 新增:模型名称
};
// 日志函数
const log = {
info: (msg) => console.log(`[Conversation Saver] ${msg}`),
error: (msg, e) => console.error(`[Conversation Saver] ${msg}`, e)
};
// Unicode清理函数
function removeSpecificUnicode(text) {
const regex = /[\uE203\uE204]/gu;
return text.replace(regex, '');
}
// 新增:提取模型名称函数
function extractModelName(jsonData) {
try {
const mapping = jsonData.mapping || {};
for (const key in mapping) {
const message = mapping[key].message;
if (message?.metadata?.model_slug) {
return message.metadata.model_slug;
}
}
return null;
} catch (e) {
log.error('提取模型名称时出错:', e);
return null;
}
}
// JSON到Markdown转换相关函数
function processCitations(text, citations, contentReferences) {
if (!citations || !contentReferences) {
return text;
}
const citationMap = {};
for (const ref of contentReferences) {
if (ref.type === 'webpage' || ref.url) {
const startIdx = ref.start_idx;
const endIdx = ref.end_idx;
const url = ref.url;
const title = ref.title || url;
if (startIdx !== null && endIdx !== null && url) {
citationMap[`${startIdx},${endIdx}`] = [url, title];
}
}
}
const sortedCitations = Object.entries(citationMap)
.map(([key, value]) => {
const [start, end] = key.split(',').map(Number);
return [start, end, ...value];
})
.sort((a, b) => b[0] - a[0]);
for (const [start, end, url, title] of sortedCitations) {
if (start < text.length && end <= text.length) {
const link = `[${title}](${url})`;
text = text.slice(0, start) + link + text.slice(end);
}
}
return text;
}
function adjustHeaderLevels(text, increaseBy = 2) {
return text.replace(/^(#+)(.*?)$/gm, (match, hashes, rest) => {
return '#'.repeat(hashes.length + increaseBy) + rest;
});
}
function extractMessageParts(message) {
if (!message?.message) return null;
const author = message.message?.author;
if (!author) return null;
if (author.name?.startsWith('canmore.') || author.name?.startsWith('dalle.')) {
return null;
}
const content = message.message.content;
if (!content) return null;
let text = '';
if (content.content_type === 'multimodal_text') {
const parts = [];
for (const part of content.parts || []) {
if (typeof part === 'string') {
parts.push(part);
}
}
text = parts.join(' ');
} else {
const parts = content.parts || [];
text = parts.filter(part => part != null).join(' ');
}
if (!text) return null;
const metadata = message.message.metadata || {};
text = processCitations(text, metadata.citations, metadata.content_references);
if (text.trim().startsWith('{') && text.trim().endsWith('}')) {
try {
const jsonContent = JSON.parse(text);
if (['name', 'type', 'content', 'updates'].some(key => key in jsonContent)) {
return null;
}
} catch {
// 解析失败,继续处理
}
}
return text;
}
function buildConversationTree(mapping, nodeId, indent = 0) {
if (!mapping[nodeId]) return [];
const node = mapping[nodeId];
const conversation = [];
const messageContent = extractMessageParts(node);
if (messageContent) {
const role = node.message?.author?.role;
if (role === 'user' || role === 'assistant') {
const prefix = role === 'user' ? '## Human\n\n' : '## Assistant\n\n';
const adjustedContent = adjustHeaderLevels(messageContent);
conversation.push(`${prefix}${adjustedContent}`);
}
}
for (const childId of node.children || []) {
conversation.push(...buildConversationTree(mapping, childId, indent + 1));
}
return conversation;
}
function convertToMarkdown(jsonData) {
try {
const title = jsonData.title || 'Conversation';
const mapping = jsonData.mapping;
const rootId = Object.keys(mapping).find(nodeId => !mapping[nodeId].parent);
if (!rootId) return '';
const conversation = buildConversationTree(mapping, rootId);
let markdownContent = `# ${title}\n\n`;
markdownContent += conversation.join('\n\n').replace(/\n{3,}/g, '\n\n');
state.markdownContent = markdownContent;
return markdownContent;
} catch (e) {
log.error('转换Markdown时出错:', e);
return null;
}
}
// 响应处理函数
function processResponse(text, url) {
try {
const responseSize = text.length;
if (responseSize > state.largestResponseSize) {
state.largestResponse = text;
state.largestResponseSize = responseSize;
state.largestResponseUrl = url;
state.lastUpdateTime = new Date().toLocaleTimeString();
// 预处理Markdown内容和提取模型名称
try {
const cleanedJson = removeSpecificUnicode(text);
const jsonData = JSON.parse(cleanedJson);
state.modelName = extractModelName(jsonData); // 新增:提取模型名称
convertToMarkdown(jsonData);
} catch (e) {
log.error('预处理Markdown内容时出错:', e);
}
updateButtonsStatus();
log.info(`发现更大的响应 (${responseSize} bytes) 来自: ${url}${state.modelName ? `, 模型: ${state.modelName}` : ''}`);
}
} catch (e) {
log.error('处理响应时出错:', e);
}
}