@samuelchung2010/dshift 0.0.0-stage → 0.3.0
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/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +116 -0
- package/README.md +116 -3
- package/assets/dshift-readme.gif +0 -0
- package/client.js +18831 -0
- package/cordis.patch.yml +58 -0
- package/docs/architecture.md +203 -0
- package/docs/configuration.md +103 -0
- package/docs/development.md +125 -0
- package/docs/limitations.md +74 -0
- package/docs/math-boxed-qquad.png +0 -0
- package/docs/math-boxed-single.png +0 -0
- package/docs/memory-integration.md +111 -0
- package/docs/perf/baseline-before.json +4872 -0
- package/docs/perf/baseline-before.summary.json +659 -0
- package/docs/perf/fixed-after.json +2927 -0
- package/docs/perf/fixed-after.summary.json +583 -0
- package/docs/perf/long-chat-performance.md +340 -0
- package/docs/perf/merged-after.json +1953 -0
- package/docs/perf/merged-after.summary.json +382 -0
- package/docs/perf/mount-after.json +82 -0
- package/docs/perf/mount-baseline.json +82 -0
- package/docs/user-guide.md +202 -0
- package/icon.svg +13 -0
- package/index.js +16 -0
- package/lib/api.js +336 -0
- package/lib/artifact/approval-bus.js +94 -0
- package/lib/artifact/prompt.js +99 -0
- package/lib/artifact/service.js +602 -0
- package/lib/artifact/workspace.js +260 -0
- package/lib/import/convert.js +343 -0
- package/lib/import/format.js +262 -0
- package/lib/import/index.js +585 -0
- package/lib/import/media.js +101 -0
- package/lib/import/zip.js +296 -0
- package/lib/memory-store.js +629 -0
- package/lib/memory-text.js +162 -0
- package/lib/memory.js +871 -0
- package/lib/model-config.js +280 -0
- package/lib/protocol.js +593 -0
- package/lib/service.js +2102 -0
- package/lib/store.js +1011 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +49 -6
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation for the DeepSeek chat export (`manifest.json`,
|
|
3
|
+
* `chat_session_index.json`, `chat_sessions/{id}.json`).
|
|
4
|
+
*
|
|
5
|
+
* The export format is versioned by `manifest.json` alone and has drifted at the
|
|
6
|
+
* field level before, so only the fields the importer reads are required and
|
|
7
|
+
* mapped fragment types are validated individually. A fragment type this module
|
|
8
|
+
* does not know is ignored rather than failing the whole conversation, and a
|
|
9
|
+
* malformed fragment of a known type fails only that conversation.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { ZipError } from './zip.js'
|
|
13
|
+
|
|
14
|
+
/** The only `manifest.json` format version this importer reads. */
|
|
15
|
+
export const SUPPORTED_EXPORT_FORMAT_VERSION = 3
|
|
16
|
+
|
|
17
|
+
/** An export file whose content is not the shape this importer read. */
|
|
18
|
+
export class ExportFormatError extends Error {
|
|
19
|
+
constructor(code, message) {
|
|
20
|
+
super(message)
|
|
21
|
+
this.name = 'ExportFormatError'
|
|
22
|
+
this.code = code
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parse an export timestamp: an RFC 3339 string, with 0, 3, 6 or 9 fractional
|
|
28
|
+
* digits. The exporter always writes UTC (`Z`), but an offset is accepted too.
|
|
29
|
+
* @returns epoch milliseconds, or undefined when the text is not a timestamp.
|
|
30
|
+
*/
|
|
31
|
+
export function parseExportTime(value) {
|
|
32
|
+
if (typeof value !== 'string' || value.trim() === '') return undefined
|
|
33
|
+
const time = Date.parse(value)
|
|
34
|
+
return Number.isSafeInteger(time) && time >= 0 ? time : undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function decodeJson(bytes) {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(new TextDecoder().decode(bytes))
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isObject(value) {
|
|
46
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function text(value) {
|
|
50
|
+
return typeof value === 'string' ? value : ''
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function optionalText(value) {
|
|
54
|
+
return typeof value === 'string' && value.trim() !== '' ? value : undefined
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Read `manifest.json`.
|
|
59
|
+
* @returns the declared export format version.
|
|
60
|
+
* @throws {ExportFormatError} when the entry is not a manifest of this format.
|
|
61
|
+
*/
|
|
62
|
+
export function readManifest(bytes) {
|
|
63
|
+
const parsed = decodeJson(bytes)
|
|
64
|
+
if (!isObject(parsed)) throw new ExportFormatError('BAD_EXPORT', 'manifest.json is not a JSON object')
|
|
65
|
+
const version = parsed.export_format_version
|
|
66
|
+
if (typeof version !== 'number' || !Number.isFinite(version)) {
|
|
67
|
+
throw new ExportFormatError('BAD_EXPORT', 'manifest.json declares no export format version')
|
|
68
|
+
}
|
|
69
|
+
return { version }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Read `chat_session_index.json`, the list of every conversation in the export.
|
|
74
|
+
*
|
|
75
|
+
* The index is what makes a preview possible without parsing hundreds of
|
|
76
|
+
* conversation files: it already carries the title, the message count, the
|
|
77
|
+
* timestamps and the pin flag. Rows this importer cannot use are dropped, so a
|
|
78
|
+
* single damaged row never hides the rest of the export.
|
|
79
|
+
*
|
|
80
|
+
* @returns conversations in index order; duplicates keep their first row.
|
|
81
|
+
*/
|
|
82
|
+
export function readSessionIndex(bytes) {
|
|
83
|
+
const parsed = decodeJson(bytes)
|
|
84
|
+
if (!Array.isArray(parsed)) throw new ExportFormatError('MISSING_INDEX', 'chat_session_index.json is not a JSON array')
|
|
85
|
+
const rows = []
|
|
86
|
+
const seen = new Set()
|
|
87
|
+
for (const raw of parsed) {
|
|
88
|
+
if (!isObject(raw)) continue
|
|
89
|
+
const id = optionalText(raw.id)
|
|
90
|
+
if (id === undefined || seen.has(id)) continue
|
|
91
|
+
seen.add(id)
|
|
92
|
+
rows.push({
|
|
93
|
+
id,
|
|
94
|
+
title: text(raw.title).trim(),
|
|
95
|
+
pinned: raw.pinned === true,
|
|
96
|
+
messageCount: typeof raw.message_count === 'number' && Number.isFinite(raw.message_count) && raw.message_count >= 0
|
|
97
|
+
? Math.floor(raw.message_count)
|
|
98
|
+
: 0,
|
|
99
|
+
insertedAt: parseExportTime(raw.inserted_at),
|
|
100
|
+
updatedAt: parseExportTime(raw.updated_at),
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
if (rows.length === 0) throw new ExportFormatError('NO_CONVERSATIONS', 'the export lists no conversation')
|
|
104
|
+
return rows
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** One mapped fragment; the union of the fragment types this importer reads. */
|
|
108
|
+
function readFragment(raw) {
|
|
109
|
+
if (!isObject(raw)) return undefined
|
|
110
|
+
switch (raw.type) {
|
|
111
|
+
case 'REQUEST':
|
|
112
|
+
case 'RESPONSE':
|
|
113
|
+
case 'TEMPLATE_RESPONSE':
|
|
114
|
+
return typeof raw.content === 'string' ? { type: raw.type, content: raw.content } : undefined
|
|
115
|
+
case 'THINK':
|
|
116
|
+
return typeof raw.content === 'string'
|
|
117
|
+
? {
|
|
118
|
+
type: 'THINK',
|
|
119
|
+
content: raw.content,
|
|
120
|
+
elapsedMs: typeof raw.elapsed_secs === 'number' && Number.isFinite(raw.elapsed_secs) && raw.elapsed_secs > 0
|
|
121
|
+
? Math.round(raw.elapsed_secs * 1000)
|
|
122
|
+
: 0,
|
|
123
|
+
}
|
|
124
|
+
: undefined
|
|
125
|
+
case 'TOOL_SEARCH':
|
|
126
|
+
return {
|
|
127
|
+
type: 'TOOL_SEARCH',
|
|
128
|
+
status: optionalText(raw.status) ?? 'FINISHED',
|
|
129
|
+
queries: Array.isArray(raw.queries) ? raw.queries.filter(query => typeof query === 'string') : [],
|
|
130
|
+
results: Array.isArray(raw.results) ? raw.results.filter(isObject).map(result => ({
|
|
131
|
+
url: text(result.url),
|
|
132
|
+
title: text(result.title),
|
|
133
|
+
snippet: text(result.snippet),
|
|
134
|
+
siteName: text(result.site_name),
|
|
135
|
+
siteIcon: text(result.site_icon),
|
|
136
|
+
publishedAt: optionalText(result.published_at),
|
|
137
|
+
})).filter(result => result.url !== '') : [],
|
|
138
|
+
}
|
|
139
|
+
case 'TOOL_OPEN':
|
|
140
|
+
return {
|
|
141
|
+
type: 'TOOL_OPEN',
|
|
142
|
+
status: optionalText(raw.status) ?? 'FINISHED',
|
|
143
|
+
url: optionalText(raw.url),
|
|
144
|
+
title: optionalText(raw.title),
|
|
145
|
+
snippet: optionalText(raw.snippet),
|
|
146
|
+
siteName: optionalText(raw.site_name),
|
|
147
|
+
siteIcon: optionalText(raw.site_icon),
|
|
148
|
+
publishedAt: optionalText(raw.published_at),
|
|
149
|
+
}
|
|
150
|
+
case 'CONTENT_RISK':
|
|
151
|
+
return typeof raw.content === 'string' && raw.content.trim() !== ''
|
|
152
|
+
? { type: 'CONTENT_RISK', content: raw.content, style: text(raw.style) }
|
|
153
|
+
: undefined
|
|
154
|
+
case 'FILE':
|
|
155
|
+
return {
|
|
156
|
+
type: 'FILE',
|
|
157
|
+
files: Array.isArray(raw.files) ? raw.files.filter(isObject).map(file => ({
|
|
158
|
+
id: optionalText(file.id),
|
|
159
|
+
name: typeof file.file_name === 'string' && file.file_name.trim() !== '' ? file.file_name : 'file',
|
|
160
|
+
size: typeof file.file_size === 'number' && Number.isFinite(file.file_size) && file.file_size >= 0
|
|
161
|
+
? Math.floor(file.file_size)
|
|
162
|
+
: 0,
|
|
163
|
+
isImage: file.is_image === true,
|
|
164
|
+
exportPath: optionalText(file.export_path),
|
|
165
|
+
})) : [],
|
|
166
|
+
}
|
|
167
|
+
case 'TOOL_FIND':
|
|
168
|
+
case 'TOOL_IMAGE_SEARCH':
|
|
169
|
+
case 'READ_LINK':
|
|
170
|
+
case 'PARTIAL_FILE_READ':
|
|
171
|
+
case 'TIP':
|
|
172
|
+
case 'REQUEST_SEARCH':
|
|
173
|
+
// Recognized but not mapped: these carry no text or source this importer
|
|
174
|
+
// can restore into a chat message.
|
|
175
|
+
return { type: raw.type, ignored: true }
|
|
176
|
+
default:
|
|
177
|
+
return { type: typeof raw.type === 'string' ? raw.type : 'UNKNOWN', ignored: true }
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Read one `chat_sessions/{id}.json` file.
|
|
183
|
+
* @returns the conversation with its complete message tree.
|
|
184
|
+
* @throws {ExportFormatError} when the file is not a conversation of this format.
|
|
185
|
+
*/
|
|
186
|
+
export function readSessionFile(bytes) {
|
|
187
|
+
const parsed = decodeJson(bytes)
|
|
188
|
+
if (!isObject(parsed)) throw new ExportFormatError('BAD_SESSION', 'the conversation file is not a JSON object')
|
|
189
|
+
const id = optionalText(parsed.id)
|
|
190
|
+
if (id === undefined) throw new ExportFormatError('BAD_SESSION', 'the conversation file names no id')
|
|
191
|
+
if (!Array.isArray(parsed.messages)) {
|
|
192
|
+
throw new ExportFormatError('BAD_SESSION', 'the conversation file holds no message list')
|
|
193
|
+
}
|
|
194
|
+
const messages = []
|
|
195
|
+
for (const raw of parsed.messages) {
|
|
196
|
+
if (!isObject(raw)) continue
|
|
197
|
+
const messageId = raw.id
|
|
198
|
+
if (typeof messageId !== 'number' || !Number.isInteger(messageId) || messageId < 0) continue
|
|
199
|
+
if (raw.role !== 'USER' && raw.role !== 'ASSISTANT') continue
|
|
200
|
+
const parentId = typeof raw.parent_id === 'number' && Number.isInteger(raw.parent_id) && raw.parent_id >= 0
|
|
201
|
+
? raw.parent_id
|
|
202
|
+
: null
|
|
203
|
+
messages.push({
|
|
204
|
+
id: messageId,
|
|
205
|
+
parentId,
|
|
206
|
+
role: raw.role,
|
|
207
|
+
status: optionalText(raw.status) ?? 'FINISHED',
|
|
208
|
+
createdAt: parseExportTime(raw.inserted_at),
|
|
209
|
+
fragments: Array.isArray(raw.fragments) ? raw.fragments.map(readFragment).filter(Boolean) : [],
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
const current = parsed.current_message_id
|
|
213
|
+
return {
|
|
214
|
+
id,
|
|
215
|
+
title: typeof parsed.title === 'string' ? parsed.title.trim() : '',
|
|
216
|
+
currentMessageId: typeof current === 'number' && Number.isInteger(current) && current >= 0 ? current : null,
|
|
217
|
+
insertedAt: parseExportTime(parsed.inserted_at),
|
|
218
|
+
updatedAt: parseExportTime(parsed.updated_at),
|
|
219
|
+
messages,
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Select the messages of the active branch, from the root to the current leaf.
|
|
225
|
+
*
|
|
226
|
+
* The export keeps a sibling for every edited question and regenerated answer;
|
|
227
|
+
* `current_message_id` names the message the user last saw, so the path to it is
|
|
228
|
+
* the conversation the website would still show. When that field is absent or
|
|
229
|
+
* names no message, the branch to the greatest message id is used because a
|
|
230
|
+
* child always has a greater id than its parent.
|
|
231
|
+
*
|
|
232
|
+
* @returns messages from root to leaf.
|
|
233
|
+
* @throws {ExportFormatError} for an empty tree, a duplicate id, a missing parent, or a cycle.
|
|
234
|
+
*/
|
|
235
|
+
export function selectActiveBranch(messages, currentMessageId) {
|
|
236
|
+
const byId = new Map()
|
|
237
|
+
let newest = null
|
|
238
|
+
for (const message of messages) {
|
|
239
|
+
if (byId.has(message.id)) throw new ExportFormatError('BAD_SESSION', 'the conversation repeats one message id')
|
|
240
|
+
byId.set(message.id, message)
|
|
241
|
+
if (newest === null || message.id > newest.id) newest = message
|
|
242
|
+
}
|
|
243
|
+
if (newest === null) throw new ExportFormatError('NO_MESSAGES', 'the conversation has no message')
|
|
244
|
+
const selected = currentMessageId === null ? undefined : byId.get(currentMessageId)
|
|
245
|
+
const chain = []
|
|
246
|
+
let message = selected ?? newest
|
|
247
|
+
for (;;) {
|
|
248
|
+
if (chain.length > byId.size) {
|
|
249
|
+
throw new ExportFormatError('BAD_SESSION', 'the message tree has a parent cycle')
|
|
250
|
+
}
|
|
251
|
+
chain.push(message)
|
|
252
|
+
if (message.parentId === null) return chain.reverse()
|
|
253
|
+
const parent = byId.get(message.parentId)
|
|
254
|
+
if (parent === undefined) {
|
|
255
|
+
throw new ExportFormatError('BAD_SESSION', 'a message names a parent that does not exist')
|
|
256
|
+
}
|
|
257
|
+
message = parent
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Re-export so callers can catch both archive and format failures with one type. */
|
|
262
|
+
export { ZipError }
|