dsh-reading-inbox 0.1.1 → 0.2.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/README.en.md +13 -9
- package/README.md +13 -9
- package/VERIFY.md +17 -0
- package/index.js +3 -2
- package/package.json +5 -3
- package/src/annotations.js +187 -0
- package/src/markdown.js +22 -2
- package/src/routes.js +28 -0
- package/src/store.js +148 -2
- package/src/tools.js +145 -0
- package/web/app.js +342 -1
- package/web/index.html +12 -0
- package/web/style.css +165 -0
package/src/store.js
CHANGED
|
@@ -2,13 +2,20 @@ import { createHash } from 'node:crypto'
|
|
|
2
2
|
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
3
3
|
import { join } from 'node:path'
|
|
4
4
|
import MiniSearch from 'minisearch'
|
|
5
|
+
import {
|
|
6
|
+
buildAnnotationAnchor,
|
|
7
|
+
normalizeColor,
|
|
8
|
+
normalizeNote,
|
|
9
|
+
resolveAnnotations,
|
|
10
|
+
} from './annotations.js'
|
|
5
11
|
import { fetchHtml } from './fetch.js'
|
|
6
|
-
import { exportMarkdown, parseArticle, renderMarkdown } from './markdown.js'
|
|
12
|
+
import { exportMarkdown, parseArticle, renderMarkdown, renderedText } from './markdown.js'
|
|
7
13
|
|
|
8
14
|
const INDEX_VERSION = 1
|
|
9
15
|
const STATUSES = new Set(['unread', 'reading', 'read', 'archived'])
|
|
10
16
|
const MAX_TAGS = 20
|
|
11
17
|
const MAX_TAG_LENGTH = 40
|
|
18
|
+
const MAX_ANNOTATIONS = 500
|
|
12
19
|
|
|
13
20
|
function nowIso() {
|
|
14
21
|
return new Date().toISOString()
|
|
@@ -125,6 +132,7 @@ function publicMeta(meta) {
|
|
|
125
132
|
lang: meta.lang,
|
|
126
133
|
images: meta.images,
|
|
127
134
|
truncated: meta.truncated === true,
|
|
135
|
+
annotationCount: Number.isInteger(meta.annotationCount) ? meta.annotationCount : 0,
|
|
128
136
|
}
|
|
129
137
|
}
|
|
130
138
|
|
|
@@ -153,6 +161,7 @@ export class ReadingInbox {
|
|
|
153
161
|
this._writeChain = Promise.resolve()
|
|
154
162
|
this._mutationChain = Promise.resolve()
|
|
155
163
|
this._inflight = new Map()
|
|
164
|
+
this._textCache = new Map()
|
|
156
165
|
}
|
|
157
166
|
|
|
158
167
|
ready() {
|
|
@@ -207,6 +216,137 @@ export class ReadingInbox {
|
|
|
207
216
|
return readFile(join(this.dir, meta.articlePath), 'utf8')
|
|
208
217
|
}
|
|
209
218
|
|
|
219
|
+
annotationPath(meta) {
|
|
220
|
+
return join(this.clipsDir, meta.id, 'annotations.json')
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
textForClip(meta, markdown) {
|
|
224
|
+
const key = `${meta.id}:${meta.contentHash ?? ''}`
|
|
225
|
+
const cached = this._textCache.get(key)
|
|
226
|
+
if (cached !== undefined) return cached
|
|
227
|
+
const text = renderedText(markdown)
|
|
228
|
+
if (this._textCache.size > 30) this._textCache.clear()
|
|
229
|
+
this._textCache.set(key, text)
|
|
230
|
+
return text
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async readAnnotations(meta) {
|
|
234
|
+
try {
|
|
235
|
+
const raw = await readFile(this.annotationPath(meta), 'utf8')
|
|
236
|
+
const parsed = JSON.parse(raw)
|
|
237
|
+
if (Array.isArray(parsed)) return parsed
|
|
238
|
+
return Array.isArray(parsed?.annotations) ? parsed.annotations : []
|
|
239
|
+
} catch (error) {
|
|
240
|
+
if (error?.code !== 'ENOENT') {
|
|
241
|
+
this.warnings.push(
|
|
242
|
+
`批注读取失败(${meta.id}):${error instanceof Error ? error.message : String(error)}`,
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
return []
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async writeAnnotations(meta, annotations) {
|
|
250
|
+
await writeJsonAtomic(this.annotationPath(meta), {
|
|
251
|
+
version: 1,
|
|
252
|
+
contentVersion: meta.contentHash ?? '',
|
|
253
|
+
annotations,
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async listAnnotations(id, body = null) {
|
|
258
|
+
await this.ready()
|
|
259
|
+
const meta = this._byId.get(String(id))
|
|
260
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
261
|
+
const markdown = body ?? await this.readBody(meta.id)
|
|
262
|
+
const text = this.textForClip(meta, markdown)
|
|
263
|
+
const annotations = await this.readAnnotations(meta)
|
|
264
|
+
return resolveAnnotations(text, annotations)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async addAnnotation(id, input = {}) {
|
|
268
|
+
await this.ready()
|
|
269
|
+
return this._withMutation(async () => {
|
|
270
|
+
const meta = this._byId.get(String(id))
|
|
271
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
272
|
+
const body = await this.readBody(meta.id)
|
|
273
|
+
const text = this.textForClip(meta, body)
|
|
274
|
+
const anchor = buildAnnotationAnchor(text, {
|
|
275
|
+
quote: input.quote,
|
|
276
|
+
prefix: input.prefix,
|
|
277
|
+
suffix: input.suffix,
|
|
278
|
+
startOffset: Number.isInteger(input.startOffset) ? input.startOffset : undefined,
|
|
279
|
+
endOffset: Number.isInteger(input.endOffset) ? input.endOffset : undefined,
|
|
280
|
+
})
|
|
281
|
+
const existing = await this.readAnnotations(meta)
|
|
282
|
+
if (existing.length >= MAX_ANNOTATIONS) {
|
|
283
|
+
throw new Error(`单篇批注数量已达上限(${MAX_ANNOTATIONS})`)
|
|
284
|
+
}
|
|
285
|
+
const now = nowIso()
|
|
286
|
+
const used = new Set(existing.map(annotation => annotation.id))
|
|
287
|
+
let annotationId = `hl-${digest(`${anchor.quote}\n${anchor.startOffset}\n${now}`, 12)}`
|
|
288
|
+
let counter = 2
|
|
289
|
+
while (used.has(annotationId)) {
|
|
290
|
+
annotationId = `hl-${digest(`${anchor.quote}\n${anchor.startOffset}\n${now}\n${counter}`, 12)}`
|
|
291
|
+
counter += 1
|
|
292
|
+
}
|
|
293
|
+
const annotation = {
|
|
294
|
+
id: annotationId,
|
|
295
|
+
...anchor,
|
|
296
|
+
color: normalizeColor(input.color),
|
|
297
|
+
note: normalizeNote(input.note),
|
|
298
|
+
contentVersion: meta.contentHash ?? '',
|
|
299
|
+
createdAt: now,
|
|
300
|
+
updatedAt: now,
|
|
301
|
+
}
|
|
302
|
+
const next = [...existing, annotation]
|
|
303
|
+
await this.writeAnnotations(meta, next)
|
|
304
|
+
meta.annotationCount = next.length
|
|
305
|
+
await this.persist()
|
|
306
|
+
return resolveAnnotations(text, [annotation])[0]
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async updateAnnotation(id, annotationId, patch = {}) {
|
|
311
|
+
await this.ready()
|
|
312
|
+
return this._withMutation(async () => {
|
|
313
|
+
const meta = this._byId.get(String(id))
|
|
314
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
315
|
+
const body = await this.readBody(meta.id)
|
|
316
|
+
const text = this.textForClip(meta, body)
|
|
317
|
+
const existing = await this.readAnnotations(meta)
|
|
318
|
+
const index = existing.findIndex(annotation => annotation.id === String(annotationId))
|
|
319
|
+
if (index === -1) throw new Error(`找不到批注:${annotationId}`)
|
|
320
|
+
const updated = {
|
|
321
|
+
...existing[index],
|
|
322
|
+
note: patch.note === undefined ? existing[index].note : normalizeNote(patch.note),
|
|
323
|
+
color: patch.color === undefined ? existing[index].color : normalizeColor(patch.color),
|
|
324
|
+
updatedAt: nowIso(),
|
|
325
|
+
}
|
|
326
|
+
const next = [...existing]
|
|
327
|
+
next[index] = updated
|
|
328
|
+
await this.writeAnnotations(meta, next)
|
|
329
|
+
meta.annotationCount = next.length
|
|
330
|
+
await this.persist()
|
|
331
|
+
return resolveAnnotations(text, [updated])[0]
|
|
332
|
+
})
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async deleteAnnotation(id, annotationId) {
|
|
336
|
+
await this.ready()
|
|
337
|
+
return this._withMutation(async () => {
|
|
338
|
+
const meta = this._byId.get(String(id))
|
|
339
|
+
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
340
|
+
const existing = await this.readAnnotations(meta)
|
|
341
|
+
const next = existing.filter(annotation => annotation.id !== String(annotationId))
|
|
342
|
+
if (next.length === existing.length) throw new Error(`找不到批注:${annotationId}`)
|
|
343
|
+
await this.writeAnnotations(meta, next)
|
|
344
|
+
meta.annotationCount = next.length
|
|
345
|
+
await this.persist()
|
|
346
|
+
return { count: next.length }
|
|
347
|
+
})
|
|
348
|
+
}
|
|
349
|
+
|
|
210
350
|
async clip(rawUrl, options = {}) {
|
|
211
351
|
await this.ready()
|
|
212
352
|
const url = normalizeUrl(rawUrl)
|
|
@@ -289,6 +429,7 @@ export class ReadingInbox {
|
|
|
289
429
|
lang: parsed.lang,
|
|
290
430
|
images: parsed.images,
|
|
291
431
|
truncated: parsed.truncated === true,
|
|
432
|
+
annotationCount: 0,
|
|
292
433
|
contentHash: digest(parsed.markdown, 16),
|
|
293
434
|
articlePath: `clips/${id}/article.md`,
|
|
294
435
|
htmlPath: `clips/${id}/original.html`,
|
|
@@ -330,6 +471,7 @@ export class ReadingInbox {
|
|
|
330
471
|
meta.lang = parsed.lang
|
|
331
472
|
meta.images = parsed.images
|
|
332
473
|
meta.truncated = parsed.truncated === true
|
|
474
|
+
meta.annotationCount = (await this.readAnnotations(meta)).length
|
|
333
475
|
meta.updatedAt = nowIso()
|
|
334
476
|
meta.contentHash = digest(parsed.markdown, 16)
|
|
335
477
|
if (Array.isArray(options.tags) && options.tags.length > 0) {
|
|
@@ -416,10 +558,13 @@ export class ReadingInbox {
|
|
|
416
558
|
const meta = this._byId.get(String(id))
|
|
417
559
|
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
418
560
|
const body = await this.readBody(meta.id)
|
|
561
|
+
const annotations = await this.listAnnotations(meta.id, body)
|
|
419
562
|
return {
|
|
420
563
|
...publicMeta(meta),
|
|
564
|
+
annotationCount: annotations.length,
|
|
421
565
|
content: body,
|
|
422
566
|
html: renderMarkdown(body),
|
|
567
|
+
annotations,
|
|
423
568
|
}
|
|
424
569
|
}
|
|
425
570
|
|
|
@@ -488,7 +633,8 @@ export class ReadingInbox {
|
|
|
488
633
|
const meta = this._byId.get(String(id))
|
|
489
634
|
if (meta === undefined) throw new Error(`找不到文章:${id}`)
|
|
490
635
|
const body = await this.readBody(meta.id)
|
|
491
|
-
|
|
636
|
+
const annotations = await this.listAnnotations(meta.id, body)
|
|
637
|
+
return { meta: publicMeta(meta), markdown: exportMarkdown(meta, body, annotations) }
|
|
492
638
|
}
|
|
493
639
|
|
|
494
640
|
async persist() {
|
package/src/tools.js
CHANGED
|
@@ -14,13 +14,57 @@ const metaProperties = {
|
|
|
14
14
|
lang: { type: 'string' },
|
|
15
15
|
images: { type: 'integer' },
|
|
16
16
|
truncated: { type: 'boolean' },
|
|
17
|
+
annotationCount: { type: 'integer' },
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
const metaRequired = [
|
|
20
21
|
'id', 'title', 'url', 'site', 'author', 'published', 'clippedAt',
|
|
21
22
|
'tags', 'status', 'excerpt', 'wordCount', 'charCount', 'lang', 'images', 'truncated',
|
|
23
|
+
'annotationCount',
|
|
22
24
|
]
|
|
23
25
|
|
|
26
|
+
const annotationProperties = {
|
|
27
|
+
annotationId: { type: 'string' },
|
|
28
|
+
quote: { type: 'string' },
|
|
29
|
+
prefix: { type: 'string' },
|
|
30
|
+
suffix: { type: 'string' },
|
|
31
|
+
startOffset: { type: 'integer' },
|
|
32
|
+
endOffset: { type: 'integer' },
|
|
33
|
+
color: { type: 'string' },
|
|
34
|
+
note: { type: 'string' },
|
|
35
|
+
status: { type: 'string' },
|
|
36
|
+
createdAt: { type: 'string' },
|
|
37
|
+
updatedAt: { type: 'string' },
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const annotationRequired = [
|
|
41
|
+
'annotationId', 'quote', 'prefix', 'suffix', 'startOffset', 'endOffset',
|
|
42
|
+
'color', 'note', 'status', 'createdAt', 'updatedAt',
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
const annotationSchema = {
|
|
46
|
+
type: 'object',
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
properties: annotationProperties,
|
|
49
|
+
required: annotationRequired,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function toolAnnotation(annotation) {
|
|
53
|
+
return {
|
|
54
|
+
annotationId: annotation.id,
|
|
55
|
+
quote: annotation.quote,
|
|
56
|
+
prefix: annotation.prefix ?? '',
|
|
57
|
+
suffix: annotation.suffix ?? '',
|
|
58
|
+
startOffset: Number.isInteger(annotation.startOffset) ? annotation.startOffset : -1,
|
|
59
|
+
endOffset: Number.isInteger(annotation.endOffset) ? annotation.endOffset : -1,
|
|
60
|
+
color: annotation.color ?? 'yellow',
|
|
61
|
+
note: annotation.note ?? '',
|
|
62
|
+
status: annotation.status ?? 'active',
|
|
63
|
+
createdAt: annotation.createdAt,
|
|
64
|
+
updatedAt: annotation.updatedAt,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
24
68
|
const clipProperties = {
|
|
25
69
|
...metaProperties,
|
|
26
70
|
duplicate: { type: 'boolean' },
|
|
@@ -289,4 +333,105 @@ export function registerTools(ctx, { inbox }) {
|
|
|
289
333
|
return inbox.update(args.id, patch)
|
|
290
334
|
},
|
|
291
335
|
})
|
|
336
|
+
|
|
337
|
+
ctx.tools.register({
|
|
338
|
+
name: 'reading_highlight',
|
|
339
|
+
description:
|
|
340
|
+
'Create, list, update, or delete highlights/annotations on a clipped article. ' +
|
|
341
|
+
'For create, pass a quote copied from the article (or startOffset/endOffset); optional note and color. ' +
|
|
342
|
+
'For list, omit quote. For update/delete, pass annotationId. ' +
|
|
343
|
+
'Use this when the user asks to highlight, mark, annotate, or review key passages.',
|
|
344
|
+
parameters: {
|
|
345
|
+
type: 'object',
|
|
346
|
+
additionalProperties: false,
|
|
347
|
+
properties: {
|
|
348
|
+
id: { type: 'string', description: 'Clip id.' },
|
|
349
|
+
action: { type: 'string', enum: ['create', 'list', 'update', 'delete'] },
|
|
350
|
+
quote: { type: 'string', description: 'Selected text, max 2000 characters.' },
|
|
351
|
+
startOffset: { type: 'integer', description: 'Code-point offset into the rendered article text.' },
|
|
352
|
+
endOffset: { type: 'integer' },
|
|
353
|
+
note: { type: 'string', description: 'Optional annotation note, max 2000 characters.' },
|
|
354
|
+
color: { type: 'string', enum: ['yellow', 'green', 'blue', 'red'] },
|
|
355
|
+
annotationId: { type: 'string' },
|
|
356
|
+
},
|
|
357
|
+
required: ['id', 'action'],
|
|
358
|
+
},
|
|
359
|
+
output: {
|
|
360
|
+
schema: {
|
|
361
|
+
type: 'object',
|
|
362
|
+
additionalProperties: false,
|
|
363
|
+
properties: {
|
|
364
|
+
ok: { type: 'boolean' },
|
|
365
|
+
annotation: annotationSchema,
|
|
366
|
+
annotations: { type: 'array', items: annotationSchema },
|
|
367
|
+
count: { type: 'integer' },
|
|
368
|
+
},
|
|
369
|
+
required: ['ok', 'count'],
|
|
370
|
+
},
|
|
371
|
+
render: (_args, value) => {
|
|
372
|
+
if (Array.isArray(value.annotations)) {
|
|
373
|
+
const lines = [`批注:${value.annotations.length} 条`, '']
|
|
374
|
+
for (const item of value.annotations) {
|
|
375
|
+
lines.push(`- [${item.annotationId}] ${item.status === 'stale' ? '(已失效)' : ''}“${item.quote}”`)
|
|
376
|
+
if (item.note) lines.push(` note: ${item.note}`)
|
|
377
|
+
}
|
|
378
|
+
if (value.annotations.length === 0) lines.push('(还没有批注)')
|
|
379
|
+
return text(lines.join('\n'))
|
|
380
|
+
}
|
|
381
|
+
if (value.annotation) {
|
|
382
|
+
const item = value.annotation
|
|
383
|
+
const lines = [
|
|
384
|
+
`批注已保存:${item.annotationId}`,
|
|
385
|
+
`quote: ${item.quote}`,
|
|
386
|
+
`color: ${item.color}`,
|
|
387
|
+
`status: ${item.status}`,
|
|
388
|
+
]
|
|
389
|
+
if (item.note) lines.push(`note: ${item.note}`)
|
|
390
|
+
lines.push(`剩余批注:${value.count}`)
|
|
391
|
+
return text(lines.join('\n'))
|
|
392
|
+
}
|
|
393
|
+
return text(`操作完成;剩余批注:${value.count}`)
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
timeoutMs: 15_000,
|
|
397
|
+
isConcurrencySafe: () => false,
|
|
398
|
+
async execute(args) {
|
|
399
|
+
if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_highlight requires `id`')
|
|
400
|
+
const action = args.action
|
|
401
|
+
if (action === 'list') {
|
|
402
|
+
const annotations = await inbox.listAnnotations(args.id)
|
|
403
|
+
return { ok: true, annotations: annotations.map(toolAnnotation), count: annotations.length }
|
|
404
|
+
}
|
|
405
|
+
if (action === 'create') {
|
|
406
|
+
const annotation = await inbox.addAnnotation(args.id, {
|
|
407
|
+
quote: args.quote,
|
|
408
|
+
startOffset: args.startOffset,
|
|
409
|
+
endOffset: args.endOffset,
|
|
410
|
+
note: args.note,
|
|
411
|
+
color: args.color,
|
|
412
|
+
})
|
|
413
|
+
const annotations = await inbox.listAnnotations(args.id)
|
|
414
|
+
return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
|
|
415
|
+
}
|
|
416
|
+
if (action === 'update') {
|
|
417
|
+
if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
|
|
418
|
+
throw new Error('reading_highlight update requires `annotationId`')
|
|
419
|
+
}
|
|
420
|
+
const annotation = await inbox.updateAnnotation(args.id, args.annotationId, {
|
|
421
|
+
note: args.note,
|
|
422
|
+
color: args.color,
|
|
423
|
+
})
|
|
424
|
+
const annotations = await inbox.listAnnotations(args.id)
|
|
425
|
+
return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
|
|
426
|
+
}
|
|
427
|
+
if (action === 'delete') {
|
|
428
|
+
if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
|
|
429
|
+
throw new Error('reading_highlight delete requires `annotationId`')
|
|
430
|
+
}
|
|
431
|
+
const result = await inbox.deleteAnnotation(args.id, args.annotationId)
|
|
432
|
+
return { ok: true, count: result.count }
|
|
433
|
+
}
|
|
434
|
+
throw new Error(`未知 action:${action}`)
|
|
435
|
+
},
|
|
436
|
+
})
|
|
292
437
|
}
|