dsh-reading-inbox 0.2.1 → 0.4.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/src/tools.js CHANGED
@@ -1,438 +1,488 @@
1
- const metaProperties = {
2
- id: { type: 'string' },
3
- title: { type: 'string' },
4
- url: { type: 'string' },
5
- site: { type: 'string' },
6
- author: { type: 'string' },
7
- published: { type: 'string' },
8
- clippedAt: { type: 'string' },
9
- tags: { type: 'array', items: { type: 'string' } },
10
- status: { type: 'string' },
11
- excerpt: { type: 'string' },
12
- wordCount: { type: 'integer' },
13
- charCount: { type: 'integer' },
14
- lang: { type: 'string' },
15
- images: { type: 'integer' },
16
- truncated: { type: 'boolean' },
17
- annotationCount: { type: 'integer' },
18
- }
19
-
20
- const metaRequired = [
21
- 'id', 'title', 'url', 'site', 'author', 'published', 'clippedAt',
22
- 'tags', 'status', 'excerpt', 'wordCount', 'charCount', 'lang', 'images', 'truncated',
23
- 'annotationCount',
24
- ]
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
-
68
- const clipProperties = {
69
- ...metaProperties,
70
- duplicate: { type: 'boolean' },
71
- preview: { type: 'string' },
72
- }
73
-
74
- const clipRequired = [...metaRequired, 'duplicate', 'preview']
75
-
76
- const clipListSchema = {
77
- type: 'object',
78
- additionalProperties: false,
79
- properties: {
80
- total: { type: 'integer' },
81
- offset: { type: 'integer' },
82
- limit: { type: 'integer' },
83
- items: {
84
- type: 'array',
85
- items: {
86
- type: 'object',
87
- additionalProperties: false,
88
- properties: metaProperties,
89
- required: metaRequired,
90
- },
91
- },
92
- },
93
- required: ['total', 'offset', 'limit', 'items'],
94
- }
95
-
96
- function text(value) {
97
- return [{ type: 'text', text: value }]
98
- }
99
-
100
- function renderMeta(meta, lines = []) {
101
- lines.push(`《${meta.title}》`)
102
- lines.push(`id: ${meta.id}`)
103
- lines.push(`source: ${meta.site || meta.url}`)
104
- lines.push(`url: ${meta.url}`)
105
- if (meta.author) lines.push(`author: ${meta.author}`)
106
- if (meta.published) lines.push(`published: ${meta.published}`)
107
- lines.push(`clipped: ${meta.clippedAt}`)
108
- lines.push(`tags: ${meta.tags.length > 0 ? meta.tags.join(', ') : '(none)'}`)
109
- lines.push(`status: ${meta.status}`)
110
- lines.push(`size: ${meta.wordCount} words / ${meta.charCount} chars${meta.truncated ? ' (truncated)' : ''}`)
111
- return lines
112
- }
113
-
114
- export function registerTools(ctx, { inbox }) {
115
- ctx.tools.register({
116
- name: 'reading_clip',
117
- description:
118
- 'Save a web page to the local reading inbox. Fetches the URL, extracts the main article, ' +
119
- 'stores a Markdown copy plus the original HTML, and indexes it for full-text search. ' +
120
- 'Use this when the user asks to clip, save, bookmark, or read a page later. Returns the clip id ' +
121
- 'and a short preview. Duplicate URLs return the existing clip unless force is true.',
122
- parameters: {
123
- type: 'object',
124
- additionalProperties: false,
125
- properties: {
126
- url: { type: 'string', description: 'http/https URL of the article.' },
127
- tags: {
128
- type: 'array',
129
- items: { type: 'string' },
130
- description: 'Optional tags; up to 20, each at most 40 characters.',
131
- },
132
- force: {
133
- type: 'boolean',
134
- description: 'Re-clip and refresh an existing URL instead of returning the existing clip.',
135
- },
136
- },
137
- required: ['url'],
138
- },
139
- output: {
140
- schema: {
141
- type: 'object',
142
- additionalProperties: false,
143
- properties: clipProperties,
144
- required: clipRequired,
145
- },
146
- render: (_args, value) => {
147
- const lines = renderMeta(value, [])
148
- if (value.duplicate) lines.push('note: this URL is already in the inbox; returning the existing clip.')
149
- lines.push('', 'preview:', value.preview)
150
- return text(lines.join('\n'))
151
- },
152
- },
153
- timeoutMs: 60_000,
154
- isConcurrencySafe: () => false,
155
- async execute(args, exec) {
156
- if (args === null || typeof args !== 'object' || typeof args.url !== 'string' || args.url.trim() === '') {
157
- throw new Error('reading_clip requires a non-empty `url`')
158
- }
159
- return inbox.clip(args.url, {
160
- tags: Array.isArray(args.tags) ? args.tags : [],
161
- force: args.force === true,
162
- signal: exec.signal,
163
- })
164
- },
165
- })
166
-
167
- ctx.tools.register({
168
- name: 'reading_search',
169
- description:
170
- 'Search the local reading inbox by full-text query, status, and/or tag. ' +
171
- 'Returns clip metadata (not full article bodies). Use reading_read to load an article.',
172
- parameters: {
173
- type: 'object',
174
- additionalProperties: false,
175
- properties: {
176
- query: { type: 'string', description: 'Full-text query; empty means list by date.' },
177
- status: {
178
- type: 'string',
179
- enum: ['all', 'unread', 'reading', 'read', 'archived'],
180
- description: 'Filter by reading status.',
181
- },
182
- tag: { type: 'string', description: 'Filter by one tag (case-insensitive).' },
183
- limit: { type: 'integer', description: '1-200, default 20.' },
184
- offset: { type: 'integer', description: 'Pagination offset, default 0.' },
185
- },
186
- required: [],
187
- },
188
- output: {
189
- schema: clipListSchema,
190
- render: (_args, value) => {
191
- const lines = [`reading inbox: ${value.total} result(s)`, '']
192
- for (const item of value.items) {
193
- lines.push(`- [${item.id}] ${item.title}`)
194
- lines.push(` ${item.site || item.url} · ${item.status} · ${item.clippedAt.slice(0, 10)} · ${item.tags.join(', ') || 'no tags'}`)
195
- }
196
- return text(lines.join('\n'))
197
- },
198
- },
199
- timeoutMs: 15_000,
200
- isConcurrencySafe: () => true,
201
- async execute(args) {
202
- const query = typeof args?.query === 'string' ? args.query : ''
203
- return inbox.list({
204
- q: query,
205
- status: args?.status ?? 'all',
206
- tag: args?.tag ?? '',
207
- limit: args?.limit ?? 20,
208
- offset: args?.offset ?? 0,
209
- })
210
- },
211
- })
212
-
213
- ctx.tools.register({
214
- name: 'reading_list',
215
- description:
216
- 'List clips in the local reading inbox by status/tag, newest first. ' +
217
- 'Use reading_search when you need full-text relevance.',
218
- parameters: {
219
- type: 'object',
220
- additionalProperties: false,
221
- properties: {
222
- status: {
223
- type: 'string',
224
- enum: ['all', 'unread', 'reading', 'read', 'archived'],
225
- },
226
- tag: { type: 'string' },
227
- limit: { type: 'integer', description: '1-200, default 50.' },
228
- offset: { type: 'integer', description: 'Pagination offset, default 0.' },
229
- },
230
- required: [],
231
- },
232
- output: {
233
- schema: clipListSchema,
234
- render: (_args, value) => {
235
- const lines = [`reading inbox: ${value.total} clip(s)`, '']
236
- for (const item of value.items) {
237
- lines.push(`- [${item.id}] ${item.title} (${item.status}) — ${item.site || item.url}`)
238
- }
239
- return text(lines.join('\n'))
240
- },
241
- },
242
- timeoutMs: 15_000,
243
- isConcurrencySafe: () => true,
244
- async execute(args) {
245
- return inbox.list({
246
- status: args?.status ?? 'all',
247
- tag: args?.tag ?? '',
248
- limit: args?.limit ?? 50,
249
- offset: args?.offset ?? 0,
250
- })
251
- },
252
- })
253
-
254
- ctx.tools.register({
255
- name: 'reading_read',
256
- description:
257
- 'Read a clipped article by id. Returns a bounded Markdown slice; use offset/nextOffset to page ' +
258
- 'through long articles. Check hasMore/totalChars before assuming the first slice is complete.',
259
- parameters: {
260
- type: 'object',
261
- additionalProperties: false,
262
- properties: {
263
- id: { type: 'string' },
264
- offset: { type: 'integer', description: 'Character offset, default 0.' },
265
- limit: { type: 'integer', description: 'Characters to return, 1-40000, default 12000.' },
266
- },
267
- required: ['id'],
268
- },
269
- output: {
270
- schema: {
271
- type: 'object',
272
- additionalProperties: false,
273
- properties: {
274
- ...metaProperties,
275
- content: { type: 'string' },
276
- offset: { type: 'integer' },
277
- totalChars: { type: 'integer' },
278
- nextOffset: { type: 'integer' },
279
- hasMore: { type: 'boolean' },
280
- },
281
- required: [...metaRequired, 'content', 'offset', 'totalChars', 'nextOffset', 'hasMore'],
282
- },
283
- render: (_args, value) => {
284
- const header = [
285
- `《${value.title}》`,
286
- `source: ${value.url}`,
287
- `chars: ${value.offset}-${value.offset + value.content.length} / ${value.totalChars}`,
288
- value.hasMore ? `nextOffset: ${value.nextOffset}` : 'complete: true',
289
- '',
290
- ].join('\n')
291
- return text(`${header}${value.content}`)
292
- },
293
- },
294
- timeoutMs: 15_000,
295
- isConcurrencySafe: () => true,
296
- async execute(args) {
297
- if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_read requires `id`')
298
- return inbox.read(args.id, { offset: args.offset ?? 0, limit: args.limit ?? 12_000 })
299
- },
300
- })
301
-
302
- ctx.tools.register({
303
- name: 'reading_update',
304
- description:
305
- 'Update a clip\'s reading status or tags. Status values: unread, reading, read, archived. ' +
306
- 'Tags replace the existing tag list.',
307
- parameters: {
308
- type: 'object',
309
- additionalProperties: false,
310
- properties: {
311
- id: { type: 'string' },
312
- status: { type: 'string', enum: ['unread', 'reading', 'read', 'archived'] },
313
- tags: { type: 'array', items: { type: 'string' } },
314
- },
315
- required: ['id'],
316
- },
317
- output: {
318
- schema: {
319
- type: 'object',
320
- additionalProperties: false,
321
- properties: metaProperties,
322
- required: metaRequired,
323
- },
324
- render: (_args, value) => text(renderMeta(value, []).join('\n')),
325
- },
326
- timeoutMs: 15_000,
327
- isConcurrencySafe: () => true,
328
- async execute(args) {
329
- if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_update requires `id`')
330
- const patch = {}
331
- if (args.status !== undefined) patch.status = args.status
332
- if (args.tags !== undefined) patch.tags = args.tags
333
- return inbox.update(args.id, patch)
334
- },
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 (plain text or Markdown; formatting is stripped if needed), ' +
342
- 'or startOffset/endOffset; optional note and color. ' +
343
- 'For list, omit quote. For update/delete, pass annotationId. ' +
344
- 'Use this when the user asks to highlight, mark, annotate, or review key passages.',
345
- parameters: {
346
- type: 'object',
347
- additionalProperties: false,
348
- properties: {
349
- id: { type: 'string', description: 'Clip id.' },
350
- action: { type: 'string', enum: ['create', 'list', 'update', 'delete'] },
351
- quote: { type: 'string', description: 'Selected text, max 2000 characters.' },
352
- startOffset: { type: 'integer', description: 'Code-point offset into the rendered article text.' },
353
- endOffset: { type: 'integer' },
354
- note: { type: 'string', description: 'Optional annotation note, max 2000 characters.' },
355
- color: { type: 'string', enum: ['yellow', 'green', 'blue', 'red'] },
356
- annotationId: { type: 'string' },
357
- },
358
- required: ['id', 'action'],
359
- },
360
- output: {
361
- schema: {
362
- type: 'object',
363
- additionalProperties: false,
364
- properties: {
365
- ok: { type: 'boolean' },
366
- annotation: annotationSchema,
367
- annotations: { type: 'array', items: annotationSchema },
368
- count: { type: 'integer' },
369
- },
370
- required: ['ok', 'count'],
371
- },
372
- render: (_args, value) => {
373
- if (Array.isArray(value.annotations)) {
374
- const lines = [`批注:${value.annotations.length} 条`, '']
375
- for (const item of value.annotations) {
376
- lines.push(`- [${item.annotationId}] ${item.status === 'stale' ? '(已失效)' : ''}“${item.quote}”`)
377
- if (item.note) lines.push(` note: ${item.note}`)
378
- }
379
- if (value.annotations.length === 0) lines.push('(还没有批注)')
380
- return text(lines.join('\n'))
381
- }
382
- if (value.annotation) {
383
- const item = value.annotation
384
- const lines = [
385
- `批注已保存:${item.annotationId}`,
386
- `quote: ${item.quote}`,
387
- `color: ${item.color}`,
388
- `status: ${item.status}`,
389
- ]
390
- if (item.note) lines.push(`note: ${item.note}`)
391
- lines.push(`剩余批注:${value.count}`)
392
- return text(lines.join('\n'))
393
- }
394
- return text(`操作完成;剩余批注:${value.count}`)
395
- },
396
- },
397
- timeoutMs: 15_000,
398
- isConcurrencySafe: () => false,
399
- async execute(args) {
400
- if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_highlight requires `id`')
401
- const action = args.action
402
- if (action === 'list') {
403
- const annotations = await inbox.listAnnotations(args.id)
404
- return { ok: true, annotations: annotations.map(toolAnnotation), count: annotations.length }
405
- }
406
- if (action === 'create') {
407
- const annotation = await inbox.addAnnotation(args.id, {
408
- quote: args.quote,
409
- startOffset: args.startOffset,
410
- endOffset: args.endOffset,
411
- note: args.note,
412
- color: args.color,
413
- })
414
- const annotations = await inbox.listAnnotations(args.id)
415
- return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
416
- }
417
- if (action === 'update') {
418
- if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
419
- throw new Error('reading_highlight update requires `annotationId`')
420
- }
421
- const annotation = await inbox.updateAnnotation(args.id, args.annotationId, {
422
- note: args.note,
423
- color: args.color,
424
- })
425
- const annotations = await inbox.listAnnotations(args.id)
426
- return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
427
- }
428
- if (action === 'delete') {
429
- if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
430
- throw new Error('reading_highlight delete requires `annotationId`')
431
- }
432
- const result = await inbox.deleteAnnotation(args.id, args.annotationId)
433
- return { ok: true, count: result.count }
434
- }
435
- throw new Error(`未知 action:${action}`)
436
- },
437
- })
438
- }
1
+ const metaProperties = {
2
+ id: { type: 'string' },
3
+ title: { type: 'string' },
4
+ url: { type: 'string' },
5
+ site: { type: 'string' },
6
+ author: { type: 'string' },
7
+ published: { type: 'string' },
8
+ clippedAt: { type: 'string' },
9
+ tags: { type: 'array', items: { type: 'string' } },
10
+ status: { type: 'string' },
11
+ excerpt: { type: 'string' },
12
+ wordCount: { type: 'integer' },
13
+ charCount: { type: 'integer' },
14
+ lang: { type: 'string' },
15
+ images: { type: 'integer' },
16
+ truncated: { type: 'boolean' },
17
+ annotationCount: { type: 'integer' },
18
+ }
19
+
20
+ const metaRequired = [
21
+ 'id', 'title', 'url', 'site', 'author', 'published', 'clippedAt',
22
+ 'tags', 'status', 'excerpt', 'wordCount', 'charCount', 'lang', 'images', 'truncated',
23
+ 'annotationCount',
24
+ ]
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
+
68
+ const clipProperties = {
69
+ ...metaProperties,
70
+ duplicate: { type: 'boolean' },
71
+ preview: { type: 'string' },
72
+ }
73
+
74
+ const clipRequired = [...metaRequired, 'duplicate', 'preview']
75
+
76
+ const clipListSchema = {
77
+ type: 'object',
78
+ additionalProperties: false,
79
+ properties: {
80
+ total: { type: 'integer' },
81
+ offset: { type: 'integer' },
82
+ limit: { type: 'integer' },
83
+ items: {
84
+ type: 'array',
85
+ items: {
86
+ type: 'object',
87
+ additionalProperties: false,
88
+ properties: { ...metaProperties, snippet: { type: 'string' } },
89
+ required: metaRequired,
90
+ },
91
+ },
92
+ },
93
+ required: ['total', 'offset', 'limit', 'items'],
94
+ }
95
+
96
+ function text(value) {
97
+ return [{ type: 'text', text: value }]
98
+ }
99
+
100
+ function renderMeta(meta, lines = []) {
101
+ lines.push(`《${meta.title}》`)
102
+ lines.push(`id: ${meta.id}`)
103
+ lines.push(`source: ${meta.site || meta.url}`)
104
+ lines.push(`url: ${meta.url}`)
105
+ if (meta.author) lines.push(`author: ${meta.author}`)
106
+ if (meta.published) lines.push(`published: ${meta.published}`)
107
+ lines.push(`clipped: ${meta.clippedAt}`)
108
+ lines.push(`tags: ${meta.tags.length > 0 ? meta.tags.join(', ') : '(none)'}`)
109
+ lines.push(`status: ${meta.status}`)
110
+ lines.push(`size: ${meta.wordCount} words / ${meta.charCount} chars${meta.truncated ? ' (truncated)' : ''}`)
111
+ return lines
112
+ }
113
+
114
+ export function registerTools(ctx, { inbox }) {
115
+ ctx.tools.register({
116
+ name: 'reading_clip',
117
+ description:
118
+ 'Save a web page to the local reading inbox. Fetches the URL, extracts the main article, ' +
119
+ 'stores a Markdown copy plus the original HTML, and indexes it for full-text search. ' +
120
+ 'Use this when the user asks to clip, save, bookmark, or read a page later. Returns the clip id ' +
121
+ 'and a short preview. Duplicate URLs return the existing clip unless force is true.',
122
+ parameters: {
123
+ type: 'object',
124
+ additionalProperties: false,
125
+ properties: {
126
+ url: { type: 'string', description: 'http/https URL of the article.' },
127
+ tags: {
128
+ type: 'array',
129
+ items: { type: 'string' },
130
+ description: 'Optional tags; up to 20, each at most 40 characters.',
131
+ },
132
+ force: {
133
+ type: 'boolean',
134
+ description: 'Re-clip and refresh an existing URL instead of returning the existing clip.',
135
+ },
136
+ },
137
+ required: ['url'],
138
+ },
139
+ output: {
140
+ schema: {
141
+ type: 'object',
142
+ additionalProperties: false,
143
+ properties: clipProperties,
144
+ required: clipRequired,
145
+ },
146
+ render: (_args, value) => {
147
+ const lines = renderMeta(value, [])
148
+ if (value.duplicate) lines.push('note: this URL is already in the inbox; returning the existing clip.')
149
+ lines.push('', 'preview:', value.preview)
150
+ return text(lines.join('\n'))
151
+ },
152
+ },
153
+ timeoutMs: 60_000,
154
+ isConcurrencySafe: () => false,
155
+ async execute(args, exec) {
156
+ if (args === null || typeof args !== 'object' || typeof args.url !== 'string' || args.url.trim() === '') {
157
+ throw new Error('reading_clip requires a non-empty `url`')
158
+ }
159
+ return inbox.clip(args.url, {
160
+ tags: Array.isArray(args.tags) ? args.tags : [],
161
+ force: args.force === true,
162
+ signal: exec.signal,
163
+ })
164
+ },
165
+ })
166
+
167
+ ctx.tools.register({
168
+ name: 'reading_save_text',
169
+ description:
170
+ 'Save pasted text, Markdown, or HTML as a local reading-inbox clip when there is no fetchable URL. ' +
171
+ 'Use this for login-walled pages, paywalled snippets, selections, or notes the user wants to keep. ' +
172
+ 'Provide at least one of text, markdown, or html; title, url and tags are optional.',
173
+ parameters: {
174
+ type: 'object',
175
+ additionalProperties: false,
176
+ properties: {
177
+ title: { type: 'string', description: 'Optional title; first non-empty line is used otherwise.' },
178
+ text: { type: 'string', description: 'Plain text content.' },
179
+ markdown: { type: 'string', description: 'Markdown content; takes precedence over text.' },
180
+ html: { type: 'string', description: 'HTML content; converted to Markdown and archived as original.html.' },
181
+ url: { type: 'string', description: 'Optional source URL for attribution and duplicate detection.' },
182
+ tags: { type: 'array', items: { type: 'string' } },
183
+ },
184
+ required: [],
185
+ },
186
+ output: {
187
+ schema: {
188
+ type: 'object',
189
+ additionalProperties: false,
190
+ properties: clipProperties,
191
+ required: clipRequired,
192
+ },
193
+ render: (_args, value) => {
194
+ const lines = renderMeta(value, [])
195
+ if (value.duplicate) lines.push('note: this URL is already in the inbox; returning the existing clip.')
196
+ lines.push('', 'preview:', value.preview)
197
+ return text(lines.join('\n'))
198
+ },
199
+ },
200
+ timeoutMs: 15_000,
201
+ isConcurrencySafe: () => false,
202
+ async execute(args) {
203
+ return inbox.saveText({
204
+ title: args?.title,
205
+ text: args?.text,
206
+ markdown: args?.markdown,
207
+ html: args?.html,
208
+ url: args?.url,
209
+ tags: Array.isArray(args?.tags) ? args.tags : [],
210
+ })
211
+ },
212
+ })
213
+
214
+ ctx.tools.register({
215
+ name: 'reading_search',
216
+ description:
217
+ 'Search the local reading inbox by full-text query, status, and/or tag. ' +
218
+ 'Query filters: tag:security, is:unread, site:example.com, after:2026-01-01, ' +
219
+ 'before:2026-12-31, has:highlight. Returns clip metadata plus a short snippet, not full bodies. ' +
220
+ 'Use reading_read to load an article.',
221
+ parameters: {
222
+ type: 'object',
223
+ additionalProperties: false,
224
+ properties: {
225
+ query: { type: 'string', description: 'Full-text query; empty means list by date.' },
226
+ status: {
227
+ type: 'string',
228
+ enum: ['all', 'unread', 'reading', 'read', 'archived'],
229
+ description: 'Filter by reading status.',
230
+ },
231
+ tag: { type: 'string', description: 'Filter by one tag (case-insensitive).' },
232
+ limit: { type: 'integer', description: '1-200, default 20.' },
233
+ offset: { type: 'integer', description: 'Pagination offset, default 0.' },
234
+ },
235
+ required: [],
236
+ },
237
+ output: {
238
+ schema: clipListSchema,
239
+ render: (_args, value) => {
240
+ const lines = [`reading inbox: ${value.total} result(s)`, '']
241
+ for (const item of value.items) {
242
+ lines.push(`- [${item.id}] ${item.title}`)
243
+ lines.push(` ${item.site || item.url} · ${item.status} · ${item.clippedAt.slice(0, 10)} · ${item.tags.join(', ') || 'no tags'}`)
244
+ if (item.snippet) lines.push(` ${item.snippet}`)
245
+ }
246
+ return text(lines.join('\n'))
247
+ },
248
+ },
249
+ timeoutMs: 15_000,
250
+ isConcurrencySafe: () => true,
251
+ async execute(args) {
252
+ const query = typeof args?.query === 'string' ? args.query : ''
253
+ return inbox.list({
254
+ q: query,
255
+ status: args?.status ?? 'all',
256
+ tag: args?.tag ?? '',
257
+ limit: args?.limit ?? 20,
258
+ offset: args?.offset ?? 0,
259
+ })
260
+ },
261
+ })
262
+
263
+ ctx.tools.register({
264
+ name: 'reading_list',
265
+ description:
266
+ 'List clips in the local reading inbox by status/tag, newest first. ' +
267
+ 'Use reading_search when you need full-text relevance.',
268
+ parameters: {
269
+ type: 'object',
270
+ additionalProperties: false,
271
+ properties: {
272
+ status: {
273
+ type: 'string',
274
+ enum: ['all', 'unread', 'reading', 'read', 'archived'],
275
+ },
276
+ tag: { type: 'string' },
277
+ limit: { type: 'integer', description: '1-200, default 50.' },
278
+ offset: { type: 'integer', description: 'Pagination offset, default 0.' },
279
+ },
280
+ required: [],
281
+ },
282
+ output: {
283
+ schema: clipListSchema,
284
+ render: (_args, value) => {
285
+ const lines = [`reading inbox: ${value.total} clip(s)`, '']
286
+ for (const item of value.items) {
287
+ lines.push(`- [${item.id}] ${item.title} (${item.status}) — ${item.site || item.url}`)
288
+ }
289
+ return text(lines.join('\n'))
290
+ },
291
+ },
292
+ timeoutMs: 15_000,
293
+ isConcurrencySafe: () => true,
294
+ async execute(args) {
295
+ return inbox.list({
296
+ status: args?.status ?? 'all',
297
+ tag: args?.tag ?? '',
298
+ limit: args?.limit ?? 50,
299
+ offset: args?.offset ?? 0,
300
+ })
301
+ },
302
+ })
303
+
304
+ ctx.tools.register({
305
+ name: 'reading_read',
306
+ description:
307
+ 'Read a clipped article by id. Returns a bounded Markdown slice; use offset/nextOffset to page ' +
308
+ 'through long articles. Check hasMore/totalChars before assuming the first slice is complete.',
309
+ parameters: {
310
+ type: 'object',
311
+ additionalProperties: false,
312
+ properties: {
313
+ id: { type: 'string' },
314
+ offset: { type: 'integer', description: 'Character offset, default 0.' },
315
+ limit: { type: 'integer', description: 'Characters to return, 1-40000, default 12000.' },
316
+ },
317
+ required: ['id'],
318
+ },
319
+ output: {
320
+ schema: {
321
+ type: 'object',
322
+ additionalProperties: false,
323
+ properties: {
324
+ ...metaProperties,
325
+ content: { type: 'string' },
326
+ offset: { type: 'integer' },
327
+ totalChars: { type: 'integer' },
328
+ nextOffset: { type: 'integer' },
329
+ hasMore: { type: 'boolean' },
330
+ },
331
+ required: [...metaRequired, 'content', 'offset', 'totalChars', 'nextOffset', 'hasMore'],
332
+ },
333
+ render: (_args, value) => {
334
+ const header = [
335
+ `《${value.title}》`,
336
+ `source: ${value.url}`,
337
+ `chars: ${value.offset}-${value.offset + value.content.length} / ${value.totalChars}`,
338
+ value.hasMore ? `nextOffset: ${value.nextOffset}` : 'complete: true',
339
+ '',
340
+ ].join('\n')
341
+ return text(`${header}${value.content}`)
342
+ },
343
+ },
344
+ timeoutMs: 15_000,
345
+ isConcurrencySafe: () => true,
346
+ async execute(args) {
347
+ if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_read requires `id`')
348
+ return inbox.read(args.id, { offset: args.offset ?? 0, limit: args.limit ?? 12_000 })
349
+ },
350
+ })
351
+
352
+ ctx.tools.register({
353
+ name: 'reading_update',
354
+ description:
355
+ 'Update a clip\'s reading status or tags. Status values: unread, reading, read, archived. ' +
356
+ 'Tags replace the existing tag list.',
357
+ parameters: {
358
+ type: 'object',
359
+ additionalProperties: false,
360
+ properties: {
361
+ id: { type: 'string' },
362
+ status: { type: 'string', enum: ['unread', 'reading', 'read', 'archived'] },
363
+ tags: { type: 'array', items: { type: 'string' } },
364
+ },
365
+ required: ['id'],
366
+ },
367
+ output: {
368
+ schema: {
369
+ type: 'object',
370
+ additionalProperties: false,
371
+ properties: metaProperties,
372
+ required: metaRequired,
373
+ },
374
+ render: (_args, value) => text(renderMeta(value, []).join('\n')),
375
+ },
376
+ timeoutMs: 15_000,
377
+ isConcurrencySafe: () => true,
378
+ async execute(args) {
379
+ if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_update requires `id`')
380
+ const patch = {}
381
+ if (args.status !== undefined) patch.status = args.status
382
+ if (args.tags !== undefined) patch.tags = args.tags
383
+ return inbox.update(args.id, patch)
384
+ },
385
+ })
386
+
387
+ ctx.tools.register({
388
+ name: 'reading_highlight',
389
+ description:
390
+ 'Create, list, update, or delete highlights/annotations on a clipped article. ' +
391
+ 'For create, pass a quote copied from the article (plain text or Markdown; formatting is stripped if needed), ' +
392
+ 'or startOffset/endOffset; optional note and color. ' +
393
+ 'For list, omit quote. For update/delete, pass annotationId. ' +
394
+ 'Use this when the user asks to highlight, mark, annotate, or review key passages.',
395
+ parameters: {
396
+ type: 'object',
397
+ additionalProperties: false,
398
+ properties: {
399
+ id: { type: 'string', description: 'Clip id.' },
400
+ action: { type: 'string', enum: ['create', 'list', 'update', 'delete'] },
401
+ quote: { type: 'string', description: 'Selected text, max 2000 characters.' },
402
+ startOffset: { type: 'integer', description: 'Code-point offset into the rendered article text.' },
403
+ endOffset: { type: 'integer' },
404
+ note: { type: 'string', description: 'Optional annotation note, max 2000 characters.' },
405
+ color: { type: 'string', enum: ['yellow', 'green', 'blue', 'red'] },
406
+ annotationId: { type: 'string' },
407
+ },
408
+ required: ['id', 'action'],
409
+ },
410
+ output: {
411
+ schema: {
412
+ type: 'object',
413
+ additionalProperties: false,
414
+ properties: {
415
+ ok: { type: 'boolean' },
416
+ annotation: annotationSchema,
417
+ annotations: { type: 'array', items: annotationSchema },
418
+ count: { type: 'integer' },
419
+ },
420
+ required: ['ok', 'count'],
421
+ },
422
+ render: (_args, value) => {
423
+ if (Array.isArray(value.annotations)) {
424
+ const lines = [`批注:${value.annotations.length} 条`, '']
425
+ for (const item of value.annotations) {
426
+ lines.push(`- [${item.annotationId}] ${item.status === 'stale' ? '(已失效)' : ''}“${item.quote}”`)
427
+ if (item.note) lines.push(` note: ${item.note}`)
428
+ }
429
+ if (value.annotations.length === 0) lines.push('(还没有批注)')
430
+ return text(lines.join('\n'))
431
+ }
432
+ if (value.annotation) {
433
+ const item = value.annotation
434
+ const lines = [
435
+ `批注已保存:${item.annotationId}`,
436
+ `quote: ${item.quote}`,
437
+ `color: ${item.color}`,
438
+ `status: ${item.status}`,
439
+ ]
440
+ if (item.note) lines.push(`note: ${item.note}`)
441
+ lines.push(`剩余批注:${value.count}`)
442
+ return text(lines.join('\n'))
443
+ }
444
+ return text(`操作完成;剩余批注:${value.count}`)
445
+ },
446
+ },
447
+ timeoutMs: 15_000,
448
+ isConcurrencySafe: () => false,
449
+ async execute(args) {
450
+ if (typeof args?.id !== 'string' || args.id.trim() === '') throw new Error('reading_highlight requires `id`')
451
+ const action = args.action
452
+ if (action === 'list') {
453
+ const annotations = await inbox.listAnnotations(args.id)
454
+ return { ok: true, annotations: annotations.map(toolAnnotation), count: annotations.length }
455
+ }
456
+ if (action === 'create') {
457
+ const annotation = await inbox.addAnnotation(args.id, {
458
+ quote: args.quote,
459
+ startOffset: args.startOffset,
460
+ endOffset: args.endOffset,
461
+ note: args.note,
462
+ color: args.color,
463
+ })
464
+ const annotations = await inbox.listAnnotations(args.id)
465
+ return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
466
+ }
467
+ if (action === 'update') {
468
+ if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
469
+ throw new Error('reading_highlight update requires `annotationId`')
470
+ }
471
+ const annotation = await inbox.updateAnnotation(args.id, args.annotationId, {
472
+ note: args.note,
473
+ color: args.color,
474
+ })
475
+ const annotations = await inbox.listAnnotations(args.id)
476
+ return { ok: true, annotation: toolAnnotation(annotation), count: annotations.length }
477
+ }
478
+ if (action === 'delete') {
479
+ if (typeof args.annotationId !== 'string' || args.annotationId.trim() === '') {
480
+ throw new Error('reading_highlight delete requires `annotationId`')
481
+ }
482
+ const result = await inbox.deleteAnnotation(args.id, args.annotationId)
483
+ return { ok: true, count: result.count }
484
+ }
485
+ throw new Error(`未知 action:${action}`)
486
+ },
487
+ })
488
+ }