@clarionstack/notesia 0.0.9 → 0.1.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.
- package/README.md +65 -1
- package/package.json +1 -1
- package/src/commands.mjs +123 -5
- package/src/import.mjs +116 -8
- package/src/io.mjs +42 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Notesia CLI
|
|
2
2
|
|
|
3
|
-
Local CLI for reading,
|
|
3
|
+
Local CLI for reading, searching, tagging, and bulk-importing notes in Notesia through the desktop app bridge. Includes AI knowledge base queries, quick capture to Inbox, and deep link navigation.
|
|
4
4
|
|
|
5
5
|
## Requirements
|
|
6
6
|
|
|
@@ -23,10 +23,26 @@ npm install -g @clarionstack/notesia
|
|
|
23
23
|
- `notesia store --title "<title>" [--tag <tag>]... [--folder-id <id>] [--content <text>]`
|
|
24
24
|
- `notesia update --id <id>|... [--content <text>] [--title "<new title>"]`
|
|
25
25
|
- `notesia append --id <id>|... [--content <text>]`
|
|
26
|
+
- `notesia prepend --id <id>|... [--content <text>]`
|
|
27
|
+
- `notesia move --id <id>|... --folder-id <id>` *(use 0 for root)*
|
|
26
28
|
- `notesia trash --id <id>|...`
|
|
29
|
+
- `notesia capture --title "<title>" [--content <text>] [--tag <tag>]...`
|
|
30
|
+
- `notesia open --id <id>|--title "<title>"`
|
|
27
31
|
- `notesia sync`
|
|
28
32
|
- `notesia auth status [--format text|json]`
|
|
29
33
|
|
|
34
|
+
### Search
|
|
35
|
+
|
|
36
|
+
- `notesia search <query> [--limit <n>] [--format text|json]`
|
|
37
|
+
|
|
38
|
+
Alias for `notesia kb search`. Full-text search across all notes by title and content.
|
|
39
|
+
|
|
40
|
+
### Tags
|
|
41
|
+
|
|
42
|
+
- `notesia tags [--format text|json]` — List all tags with note counts
|
|
43
|
+
- `notesia tag add --id <id>|... --tag <tag> [--tag <tag>]...` — Add tags to a note
|
|
44
|
+
- `notesia tag remove --id <id>|... --tag <tag> [--tag <tag>]...` — Remove tags from a note
|
|
45
|
+
|
|
30
46
|
### Bulk Import
|
|
31
47
|
|
|
32
48
|
```
|
|
@@ -62,6 +78,13 @@ Notes exceeding 100,000 characters are automatically split into multiple parts.
|
|
|
62
78
|
|
|
63
79
|
`kb ask` automatically reuses the same conversation in the same terminal session. Use `--conversation-id` only when you want to override/reset the session.
|
|
64
80
|
|
|
81
|
+
### Deep Links
|
|
82
|
+
|
|
83
|
+
Open a note in the desktop app via URL scheme:
|
|
84
|
+
|
|
85
|
+
- `com.notesia.app://open?id=<id>` — Open by local ID
|
|
86
|
+
- `com.notesia.app://open?title=<title>` — Open by title
|
|
87
|
+
|
|
65
88
|
## Examples
|
|
66
89
|
|
|
67
90
|
### Basic note operations
|
|
@@ -77,6 +100,47 @@ notesia sync
|
|
|
77
100
|
notesia auth status
|
|
78
101
|
```
|
|
79
102
|
|
|
103
|
+
### Search, move, open, and capture
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
# Full-text search
|
|
107
|
+
notesia search "meeting notes"
|
|
108
|
+
notesia search "meeting" --json | jq '.notes[].title'
|
|
109
|
+
|
|
110
|
+
# Move a note to a different folder
|
|
111
|
+
notesia move --id 12 --folder-id 3
|
|
112
|
+
|
|
113
|
+
# Open a note in the desktop app
|
|
114
|
+
notesia open --title "My Note"
|
|
115
|
+
|
|
116
|
+
# Quick capture to Inbox
|
|
117
|
+
notesia capture --title "Quick idea" --content "Look into this later"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Prepend and pipe examples
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# Prepend content before existing note body
|
|
124
|
+
echo "Important: " | notesia prepend --id 5
|
|
125
|
+
|
|
126
|
+
# Capture from clipboard (macOS)
|
|
127
|
+
pbpaste | notesia capture --title "Clipboard"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Tags
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
# List all tags with usage counts
|
|
134
|
+
notesia tags
|
|
135
|
+
notesia tags --format json
|
|
136
|
+
|
|
137
|
+
# Add tags to a note
|
|
138
|
+
notesia tag add --id 42 --tag project --tag important
|
|
139
|
+
|
|
140
|
+
# Remove tags from a note
|
|
141
|
+
notesia tag remove --id 42 --tag draft
|
|
142
|
+
```
|
|
143
|
+
|
|
80
144
|
### Bulk import
|
|
81
145
|
|
|
82
146
|
```bash
|
package/package.json
CHANGED
package/src/commands.mjs
CHANGED
|
@@ -7,15 +7,18 @@ import {
|
|
|
7
7
|
printListResult,
|
|
8
8
|
printRetrieveResult,
|
|
9
9
|
printMutationResult,
|
|
10
|
+
printOpenResult,
|
|
10
11
|
printAuthStatusResult,
|
|
11
12
|
printKbResult,
|
|
13
|
+
printTagsResult,
|
|
12
14
|
readInputContent as defaultReadInputContent,
|
|
13
15
|
} from './io.mjs';
|
|
14
16
|
|
|
15
|
-
const SUPPORTED_OPS = new Set(['get', 'list', 'store', 'update', 'append', 'sync', 'auth-status', 'kb-search', 'kb-folders', 'kb-context', 'kb-ask', 'trash']);
|
|
17
|
+
const SUPPORTED_OPS = new Set(['get', 'list', 'store', 'update', 'append', 'prepend', 'move', 'open', 'sync', 'auth-status', 'kb-search', 'kb-folders', 'kb-context', 'kb-ask', 'trash', 'search', 'capture', 'tags', 'tag-add', 'tag-remove']);
|
|
16
18
|
const OP_ALIASES = new Map([
|
|
17
19
|
['retrieve', 'get'],
|
|
18
20
|
['storage', 'store'],
|
|
21
|
+
['whoami', 'auth-status'],
|
|
19
22
|
]);
|
|
20
23
|
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
21
24
|
const KB_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
@@ -103,6 +106,25 @@ export async function defaultResolveKbConversationId(explicitConversationId = nu
|
|
|
103
106
|
}
|
|
104
107
|
}
|
|
105
108
|
|
|
109
|
+
export async function defaultPersistKbConversationId(conversationId) {
|
|
110
|
+
if (!UUID_REGEX.test(conversationId || '')) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const nowMs = Date.now();
|
|
115
|
+
const sessionFilePath = getKbSessionStorePath();
|
|
116
|
+
const sessionKey = getTerminalSessionKey();
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const loaded = await loadKbSessionStore(sessionFilePath);
|
|
120
|
+
const store = pruneKbSessionStore(loaded, nowMs);
|
|
121
|
+
store[sessionKey] = { conversationId, updatedAt: nowMs };
|
|
122
|
+
await saveKbSessionStore(sessionFilePath, store);
|
|
123
|
+
} catch {
|
|
124
|
+
// Ignore persistence failures; kb ask can still continue statelessly.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
106
128
|
export class CliCommandError extends Error {
|
|
107
129
|
constructor(code, message) {
|
|
108
130
|
super(message);
|
|
@@ -152,6 +174,24 @@ export function parseArgs(argv = []) {
|
|
|
152
174
|
}
|
|
153
175
|
}
|
|
154
176
|
|
|
177
|
+
if (rawOp === 'tag') {
|
|
178
|
+
const subcommand = args.shift();
|
|
179
|
+
if (!subcommand || subcommand === '--help' || subcommand === '-h') {
|
|
180
|
+
return { op: 'help' };
|
|
181
|
+
}
|
|
182
|
+
const tagOps = { add: 'tag-add', remove: 'tag-remove' };
|
|
183
|
+
if (!tagOps[subcommand]) {
|
|
184
|
+
throw new CliCommandError('INVALID_OP', `Unsupported tag command: ${subcommand}. Use 'tags' to list or 'tag add/remove'.`);
|
|
185
|
+
}
|
|
186
|
+
op = tagOps[subcommand];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (rawOp === 'search') {
|
|
190
|
+
if (args.length > 0 && !args[0].startsWith('--')) {
|
|
191
|
+
args.unshift('--query', args.shift());
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
155
195
|
if (!op || op === '--help' || op === '-h') {
|
|
156
196
|
return { op: 'help' };
|
|
157
197
|
}
|
|
@@ -290,7 +330,7 @@ export function parseArgs(argv = []) {
|
|
|
290
330
|
const selector = {};
|
|
291
331
|
if (parsed.id !== null) selector.id = parsed.id;
|
|
292
332
|
if (parsed.supabaseId) selector.supabase_id = parsed.supabaseId;
|
|
293
|
-
if (parsed.title && op === 'get') selector.title = parsed.title;
|
|
333
|
+
if (parsed.title && (op === 'get' || op === 'open')) selector.title = parsed.title;
|
|
294
334
|
if (Object.keys(selector).length > 0) parsed.selector = selector;
|
|
295
335
|
|
|
296
336
|
return parsed;
|
|
@@ -304,9 +344,17 @@ function usageText() {
|
|
|
304
344
|
' get --id <id>|--supabase-id <uuid>|--title <title> [--json] [--with-meta]',
|
|
305
345
|
' list [--limit <count>] [--format table|json]',
|
|
306
346
|
' store [--title <title>] [--tag <tag>]... [--folder-id <id>] [--content <text>]',
|
|
347
|
+
' capture [--title <title>] [--tag <tag>]... [--content <text>] (saves to Inbox)',
|
|
307
348
|
' update --id <id>|--supabase-id <uuid>|--title <title> [--content <text>] [--title <new>]',
|
|
308
349
|
' append --id <id>|--supabase-id <uuid>|--title <title> [--content <text>]',
|
|
350
|
+
' prepend --id <id>|--supabase-id <uuid>|--title <title> [--content <text>]',
|
|
351
|
+
' search <query>|--query <text> [--limit <n>] [--threshold <0-1>] [--format text|json]',
|
|
352
|
+
' move --id <id>|--supabase-id <uuid>|--title <title> --folder-id <id> (0 = root)',
|
|
353
|
+
' open --id <id>|--supabase-id <uuid>|--title <title>',
|
|
309
354
|
' trash --id <id>|--supabase-id <uuid>|--title <title>',
|
|
355
|
+
' tags [--format table|json]',
|
|
356
|
+
' tag add --id <id>|... --tag <name> [--tag <name2>]...',
|
|
357
|
+
' tag remove --id <id>|... --tag <name> [--tag <name2>]...',
|
|
310
358
|
' sync',
|
|
311
359
|
' auth status [--format text|json]',
|
|
312
360
|
'',
|
|
@@ -351,6 +399,10 @@ function buildPayload(parsed, pipedContent) {
|
|
|
351
399
|
};
|
|
352
400
|
}
|
|
353
401
|
|
|
402
|
+
if (parsed.op === 'open') {
|
|
403
|
+
return { selector: requireSelector(parsed) };
|
|
404
|
+
}
|
|
405
|
+
|
|
354
406
|
if (parsed.op === 'list') {
|
|
355
407
|
return {
|
|
356
408
|
limit: parsed.limit ?? 100,
|
|
@@ -374,6 +426,14 @@ function buildPayload(parsed, pipedContent) {
|
|
|
374
426
|
};
|
|
375
427
|
}
|
|
376
428
|
|
|
429
|
+
if (parsed.op === 'capture') {
|
|
430
|
+
return {
|
|
431
|
+
title: parsed.title || undefined,
|
|
432
|
+
content: parsed.content ?? pipedContent ?? '',
|
|
433
|
+
tags: parsed.tags,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
377
437
|
if (parsed.op === 'update') {
|
|
378
438
|
const content = parsed.content ?? pipedContent;
|
|
379
439
|
const hasContent = content !== undefined;
|
|
@@ -405,6 +465,30 @@ function buildPayload(parsed, pipedContent) {
|
|
|
405
465
|
};
|
|
406
466
|
}
|
|
407
467
|
|
|
468
|
+
if (parsed.op === 'prepend') {
|
|
469
|
+
const content = parsed.content ?? pipedContent;
|
|
470
|
+
if (content === undefined || content === '') {
|
|
471
|
+
throw new CliCommandError(
|
|
472
|
+
'MISSING_CONTENT',
|
|
473
|
+
'Prepend requires non-empty --content or piped stdin'
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
selector: requireSelector(parsed),
|
|
478
|
+
content,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (parsed.op === 'move') {
|
|
483
|
+
if (parsed.folderId === null || parsed.folderId === undefined) {
|
|
484
|
+
throw new CliCommandError('MISSING_ARGS', '--folder-id is required for move (use 0 for root)');
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
selector: requireSelector(parsed),
|
|
488
|
+
folder_id: parsed.folderId === 0 ? null : parsed.folderId,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
408
492
|
if (parsed.op === 'trash') {
|
|
409
493
|
return { selector: requireSelector(parsed) };
|
|
410
494
|
}
|
|
@@ -443,6 +527,27 @@ function buildPayload(parsed, pipedContent) {
|
|
|
443
527
|
};
|
|
444
528
|
}
|
|
445
529
|
|
|
530
|
+
if (parsed.op === 'search') {
|
|
531
|
+
if (!parsed.query) {
|
|
532
|
+
throw new CliCommandError('MISSING_ARGS', '--query is required for search');
|
|
533
|
+
}
|
|
534
|
+
return { query: parsed.query, limit: parsed.limit ?? 20, threshold: parsed.threshold ?? undefined };
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (parsed.op === 'tags') {
|
|
538
|
+
return {};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (parsed.op === 'tag-add' || parsed.op === 'tag-remove') {
|
|
542
|
+
if (parsed.tags.length === 0) {
|
|
543
|
+
throw new CliCommandError('MISSING_ARGS', '--tag is required for tag add/remove');
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
selector: requireSelector(parsed),
|
|
547
|
+
tags: parsed.tags,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
446
551
|
throw new CliCommandError('INVALID_OP', `Unsupported command: ${parsed.op} `);
|
|
447
552
|
}
|
|
448
553
|
|
|
@@ -451,6 +556,7 @@ export async function runCli(argv = [], {
|
|
|
451
556
|
readInputContent = defaultReadInputContent,
|
|
452
557
|
writeStdout = (chunk) => process.stdout.write(chunk),
|
|
453
558
|
resolveKbConversationId = defaultResolveKbConversationId,
|
|
559
|
+
persistKbConversationId = defaultPersistKbConversationId,
|
|
454
560
|
} = {}) {
|
|
455
561
|
const parsed = parseArgs(argv);
|
|
456
562
|
|
|
@@ -459,7 +565,7 @@ export async function runCli(argv = [], {
|
|
|
459
565
|
return { ok: true, help: true };
|
|
460
566
|
}
|
|
461
567
|
|
|
462
|
-
const needsPipedInput = ['store', 'update', 'append'].includes(parsed.op) && parsed.content === undefined;
|
|
568
|
+
const needsPipedInput = ['store', 'update', 'append', 'prepend', 'capture'].includes(parsed.op) && parsed.content === undefined;
|
|
463
569
|
const pipedContent = needsPipedInput ? await readInputContent() : undefined;
|
|
464
570
|
|
|
465
571
|
const payload = buildPayload(parsed, pipedContent);
|
|
@@ -482,6 +588,14 @@ export async function runCli(argv = [], {
|
|
|
482
588
|
throw new CliCommandError(code, message);
|
|
483
589
|
}
|
|
484
590
|
|
|
591
|
+
if (
|
|
592
|
+
parsed.op === 'kb-ask'
|
|
593
|
+
&& typeof persistKbConversationId === 'function'
|
|
594
|
+
&& UUID_REGEX.test(response.data?.conversation_id || '')
|
|
595
|
+
) {
|
|
596
|
+
await persistKbConversationId(response.data.conversation_id);
|
|
597
|
+
}
|
|
598
|
+
|
|
485
599
|
if (parsed.op === 'get') {
|
|
486
600
|
printRetrieveResult(response.data, {
|
|
487
601
|
format: parsed.format,
|
|
@@ -498,8 +612,12 @@ export async function runCli(argv = [], {
|
|
|
498
612
|
format: parsed.format,
|
|
499
613
|
writeStdout,
|
|
500
614
|
});
|
|
501
|
-
} else if (
|
|
502
|
-
|
|
615
|
+
} else if (parsed.op === 'open') {
|
|
616
|
+
printOpenResult(response.data, { format: parsed.format, writeStdout });
|
|
617
|
+
} else if (parsed.op === 'tags') {
|
|
618
|
+
printTagsResult(response.data, { format: parsed.format === 'text' ? 'table' : parsed.format, writeStdout });
|
|
619
|
+
} else if (['search', 'kb-search', 'kb-folders', 'kb-context', 'kb-ask'].includes(parsed.op)) {
|
|
620
|
+
printKbResult(parsed.op === 'search' ? 'kb-search' : parsed.op, response.data, { format: parsed.format, writeStdout });
|
|
503
621
|
} else {
|
|
504
622
|
printMutationResult(response.data, {
|
|
505
623
|
format: parsed.format,
|
package/src/import.mjs
CHANGED
|
@@ -18,8 +18,11 @@ const DEFAULT_EXTENSIONS = ['md', 'txt', 'pdf', 'docx', 'json'];
|
|
|
18
18
|
const JSON_MAX_DEPTH = 12;
|
|
19
19
|
const JSON_MAX_ENTRIES = 5_000;
|
|
20
20
|
const JSON_MAX_ARRAY_ITEMS = 100;
|
|
21
|
+
const JSON_MAX_OBJECT_KEYS = 200;
|
|
21
22
|
const JSON_MAX_STRING_LENGTH = 500;
|
|
22
23
|
const JSON_MAX_TOP_LEVEL_KEYS = 25;
|
|
24
|
+
const JSON_PREVIEW_BYTES = 64 * 1024;
|
|
25
|
+
const JSON_DEFAULT_MAX_PARSE_MB = 8;
|
|
23
26
|
const SIMPLE_JSON_KEY_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
24
27
|
|
|
25
28
|
const USAGE = [
|
|
@@ -236,6 +239,61 @@ function normalizeFencedCodeBlocks(markdown) {
|
|
|
236
239
|
});
|
|
237
240
|
}
|
|
238
241
|
|
|
242
|
+
function getJsonMaxParseBytes() {
|
|
243
|
+
const rawLimit = Number(process.env.NOTESIA_CLI_JSON_MAX_PARSE_MB);
|
|
244
|
+
if (Number.isFinite(rawLimit) && rawLimit > 0) {
|
|
245
|
+
return Math.floor(rawLimit * 1024 * 1024);
|
|
246
|
+
}
|
|
247
|
+
return JSON_DEFAULT_MAX_PARSE_MB * 1024 * 1024;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function formatByteSize(bytes) {
|
|
251
|
+
if (!Number.isFinite(bytes) || bytes < 0) return 'unknown';
|
|
252
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
253
|
+
const kb = bytes / 1024;
|
|
254
|
+
if (kb < 1024) return `${kb.toFixed(1)} KB`;
|
|
255
|
+
const mb = kb / 1024;
|
|
256
|
+
return `${mb.toFixed(2)} MB`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function readFilePreview(filePath, maxBytes) {
|
|
260
|
+
const handle = await fs.open(filePath, 'r');
|
|
261
|
+
try {
|
|
262
|
+
const buffer = Buffer.alloc(Math.max(1, maxBytes));
|
|
263
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
264
|
+
return buffer.subarray(0, bytesRead).toString('utf8').replace(/\u0000/g, '');
|
|
265
|
+
} finally {
|
|
266
|
+
await handle.close();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function formatOversizedJsonImport({
|
|
271
|
+
sourcePath = '',
|
|
272
|
+
fileSizeBytes = 0,
|
|
273
|
+
parseLimitBytes = getJsonMaxParseBytes(),
|
|
274
|
+
previewText = '',
|
|
275
|
+
}) {
|
|
276
|
+
const cleanedPreview = String(previewText || '').trim() || '[empty preview]';
|
|
277
|
+
return [
|
|
278
|
+
'# JSON Import',
|
|
279
|
+
'',
|
|
280
|
+
`Source: ${sourcePath || 'unknown'}`,
|
|
281
|
+
'Root type: unknown (skipped full parse)',
|
|
282
|
+
`File size: ${formatByteSize(fileSizeBytes)}`,
|
|
283
|
+
'',
|
|
284
|
+
'## Import Notes',
|
|
285
|
+
'',
|
|
286
|
+
`- [large-file] Full JSON parse skipped (${formatByteSize(fileSizeBytes)} exceeds limit ${formatByteSize(parseLimitBytes)}).`,
|
|
287
|
+
'- Set NOTESIA_CLI_JSON_MAX_PARSE_MB to raise the parse threshold when needed.',
|
|
288
|
+
'',
|
|
289
|
+
`## Raw Preview (first ${formatByteSize(Math.min(fileSizeBytes, JSON_PREVIEW_BYTES))})`,
|
|
290
|
+
'',
|
|
291
|
+
'~~~json',
|
|
292
|
+
cleanedPreview,
|
|
293
|
+
'~~~',
|
|
294
|
+
].join('\n').trim();
|
|
295
|
+
}
|
|
296
|
+
|
|
239
297
|
function appendJsonPath(basePath, key) {
|
|
240
298
|
if (SIMPLE_JSON_KEY_RE.test(key)) {
|
|
241
299
|
return basePath === '$' ? `$.${key}` : `${basePath}.${key}`;
|
|
@@ -244,10 +302,11 @@ function appendJsonPath(basePath, key) {
|
|
|
244
302
|
return `${basePath}["${escapedKey}"]`;
|
|
245
303
|
}
|
|
246
304
|
|
|
247
|
-
function formatJsonValue(value) {
|
|
305
|
+
function formatJsonValue(value, metrics) {
|
|
248
306
|
if (typeof value === 'string') {
|
|
249
307
|
const normalized = value.replace(/\s+/g, ' ').trim();
|
|
250
308
|
if (normalized.length > JSON_MAX_STRING_LENGTH) {
|
|
309
|
+
metrics.truncatedStrings += 1;
|
|
251
310
|
const truncatedChars = normalized.length - JSON_MAX_STRING_LENGTH;
|
|
252
311
|
const shortened = normalized.slice(0, JSON_MAX_STRING_LENGTH);
|
|
253
312
|
return `${JSON.stringify(shortened)} [truncated ${truncatedChars} chars]`;
|
|
@@ -263,11 +322,19 @@ function flattenJsonForEmbedding(root) {
|
|
|
263
322
|
const lines = [];
|
|
264
323
|
const notes = [];
|
|
265
324
|
const stack = [{ path: '$', value: root, depth: 0 }];
|
|
266
|
-
|
|
325
|
+
const metrics = {
|
|
326
|
+
hitEntryLimit: false,
|
|
327
|
+
depthTruncations: 0,
|
|
328
|
+
arraysTruncated: 0,
|
|
329
|
+
arrayItemsOmitted: 0,
|
|
330
|
+
objectsTruncated: 0,
|
|
331
|
+
objectKeysOmitted: 0,
|
|
332
|
+
truncatedStrings: 0,
|
|
333
|
+
};
|
|
267
334
|
|
|
268
335
|
while (stack.length > 0) {
|
|
269
336
|
if (lines.length >= JSON_MAX_ENTRIES) {
|
|
270
|
-
hitEntryLimit = true;
|
|
337
|
+
metrics.hitEntryLimit = true;
|
|
271
338
|
break;
|
|
272
339
|
}
|
|
273
340
|
|
|
@@ -275,11 +342,12 @@ function flattenJsonForEmbedding(root) {
|
|
|
275
342
|
const { path: currentPath, value, depth } = current;
|
|
276
343
|
|
|
277
344
|
if (value === null || typeof value !== 'object') {
|
|
278
|
-
lines.push(`${currentPath}: ${formatJsonValue(value)}`);
|
|
345
|
+
lines.push(`${currentPath}: ${formatJsonValue(value, metrics)}`);
|
|
279
346
|
continue;
|
|
280
347
|
}
|
|
281
348
|
|
|
282
349
|
if (depth >= JSON_MAX_DEPTH) {
|
|
350
|
+
metrics.depthTruncations += 1;
|
|
283
351
|
lines.push(`${currentPath}: [max depth ${JSON_MAX_DEPTH} reached]`);
|
|
284
352
|
continue;
|
|
285
353
|
}
|
|
@@ -297,6 +365,8 @@ function flattenJsonForEmbedding(root) {
|
|
|
297
365
|
}
|
|
298
366
|
|
|
299
367
|
if (value.length > keptItems) {
|
|
368
|
+
metrics.arraysTruncated += 1;
|
|
369
|
+
metrics.arrayItemsOmitted += value.length - keptItems;
|
|
300
370
|
lines.push(`${currentPath}: [omitted after item ${keptItems - 1}] (${value.length - keptItems} items omitted)`);
|
|
301
371
|
}
|
|
302
372
|
continue;
|
|
@@ -308,8 +378,17 @@ function flattenJsonForEmbedding(root) {
|
|
|
308
378
|
continue;
|
|
309
379
|
}
|
|
310
380
|
|
|
311
|
-
|
|
312
|
-
|
|
381
|
+
const keptEntries = entries.slice(0, JSON_MAX_OBJECT_KEYS);
|
|
382
|
+
if (entries.length > keptEntries.length) {
|
|
383
|
+
const omittedCount = entries.length - keptEntries.length;
|
|
384
|
+
metrics.objectsTruncated += 1;
|
|
385
|
+
metrics.objectKeysOmitted += omittedCount;
|
|
386
|
+
lines.push(`${currentPath}: [object keys=${entries.length}]`);
|
|
387
|
+
lines.push(`${currentPath}: [omitted ${omittedCount} keys after first ${keptEntries.length}]`);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
for (let i = keptEntries.length - 1; i >= 0; i -= 1) {
|
|
391
|
+
const [key, childValue] = keptEntries[i];
|
|
313
392
|
stack.push({
|
|
314
393
|
path: appendJsonPath(currentPath, key),
|
|
315
394
|
value: childValue,
|
|
@@ -318,9 +397,21 @@ function flattenJsonForEmbedding(root) {
|
|
|
318
397
|
}
|
|
319
398
|
}
|
|
320
399
|
|
|
321
|
-
if (hitEntryLimit) {
|
|
400
|
+
if (metrics.hitEntryLimit) {
|
|
322
401
|
notes.push(`[truncated] Flattened field limit reached (${JSON_MAX_ENTRIES}); remaining fields omitted.`);
|
|
323
402
|
}
|
|
403
|
+
if (metrics.depthTruncations > 0) {
|
|
404
|
+
notes.push(`[depth] Max-depth limit reached at ${metrics.depthTruncations} path(s) (limit=${JSON_MAX_DEPTH}).`);
|
|
405
|
+
}
|
|
406
|
+
if (metrics.arraysTruncated > 0) {
|
|
407
|
+
notes.push(`[arrays] Truncated ${metrics.arraysTruncated} array(s); ${metrics.arrayItemsOmitted} item(s) omitted beyond index ${JSON_MAX_ARRAY_ITEMS - 1}.`);
|
|
408
|
+
}
|
|
409
|
+
if (metrics.objectsTruncated > 0) {
|
|
410
|
+
notes.push(`[objects] Truncated ${metrics.objectsTruncated} object(s); ${metrics.objectKeysOmitted} key(s) omitted beyond first ${JSON_MAX_OBJECT_KEYS}.`);
|
|
411
|
+
}
|
|
412
|
+
if (metrics.truncatedStrings > 0) {
|
|
413
|
+
notes.push(`[strings] Truncated ${metrics.truncatedStrings} long string value(s) to ${JSON_MAX_STRING_LENGTH} chars each.`);
|
|
414
|
+
}
|
|
324
415
|
|
|
325
416
|
return { lines, notes };
|
|
326
417
|
}
|
|
@@ -376,6 +467,23 @@ async function parseFile(absolutePath, extension, relativePath) {
|
|
|
376
467
|
return { title: htmlTitle || defaultTitle, body: stripHtml(raw) };
|
|
377
468
|
}
|
|
378
469
|
if (extension === 'json') {
|
|
470
|
+
const sourcePath = relativePath || path.basename(absolutePath);
|
|
471
|
+
const stat = await fs.stat(absolutePath).catch(() => null);
|
|
472
|
+
const jsonMaxParseBytes = getJsonMaxParseBytes();
|
|
473
|
+
|
|
474
|
+
if (stat && stat.size > jsonMaxParseBytes) {
|
|
475
|
+
const preview = await readFilePreview(absolutePath, JSON_PREVIEW_BYTES).catch(() => '');
|
|
476
|
+
return {
|
|
477
|
+
title: defaultTitle,
|
|
478
|
+
body: formatOversizedJsonImport({
|
|
479
|
+
sourcePath,
|
|
480
|
+
fileSizeBytes: stat.size,
|
|
481
|
+
parseLimitBytes: jsonMaxParseBytes,
|
|
482
|
+
previewText: preview,
|
|
483
|
+
}),
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
379
487
|
const raw = await fs.readFile(absolutePath, 'utf8');
|
|
380
488
|
let parsedJson;
|
|
381
489
|
try {
|
|
@@ -385,7 +493,7 @@ async function parseFile(absolutePath, extension, relativePath) {
|
|
|
385
493
|
}
|
|
386
494
|
return {
|
|
387
495
|
title: defaultTitle,
|
|
388
|
-
body: formatJsonForEmbedding(parsedJson,
|
|
496
|
+
body: formatJsonForEmbedding(parsedJson, sourcePath),
|
|
389
497
|
};
|
|
390
498
|
}
|
|
391
499
|
if (extension === 'pdf') {
|
package/src/io.mjs
CHANGED
|
@@ -47,6 +47,18 @@ export function printRetrieveResult(result, {
|
|
|
47
47
|
writeStdout(String(result?.content ?? ''));
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
export function printOpenResult(result, {
|
|
51
|
+
format = 'text',
|
|
52
|
+
writeStdout = defaultWriteStdout,
|
|
53
|
+
} = {}) {
|
|
54
|
+
if (format === 'json') {
|
|
55
|
+
writeStdout(`${JSON.stringify(result ?? {}, null, 2)}\n`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const title = result?.title || 'Untitled';
|
|
59
|
+
writeStdout(`Opened: ${title} (id: ${result?.id ?? 'unknown'})\n`);
|
|
60
|
+
}
|
|
61
|
+
|
|
50
62
|
export function printMutationResult(result, {
|
|
51
63
|
format = 'text',
|
|
52
64
|
writeStdout = defaultWriteStdout,
|
|
@@ -153,6 +165,32 @@ export function printAuthStatusResult(result, {
|
|
|
153
165
|
writeStdout(`${lines.join('\n')}\n`);
|
|
154
166
|
}
|
|
155
167
|
|
|
168
|
+
export function printTagsResult(result, {
|
|
169
|
+
format = 'table',
|
|
170
|
+
writeStdout = defaultWriteStdout,
|
|
171
|
+
} = {}) {
|
|
172
|
+
if (format === 'json') {
|
|
173
|
+
writeStdout(`${JSON.stringify(result ?? { tags: [], total: 0 }, null, 2)}\n`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const tags = Array.isArray(result?.tags) ? result.tags : [];
|
|
177
|
+
if (tags.length === 0) {
|
|
178
|
+
writeStdout('No tags found.\n');
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const headers = ['ID', 'NAME', 'NOTES'];
|
|
182
|
+
const rows = tags.map((t) => [
|
|
183
|
+
formatCell(t.id),
|
|
184
|
+
formatCell(t.name),
|
|
185
|
+
formatCell(t.note_count),
|
|
186
|
+
]);
|
|
187
|
+
const widths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
188
|
+
const renderRow = (cells) => cells.map((c, i) => c.padEnd(widths[i])).join(' ').trimEnd();
|
|
189
|
+
const lines = [renderRow(headers), widths.map((w) => '-'.repeat(w)).join(' '), ...rows.map(renderRow)];
|
|
190
|
+
writeStdout(`${lines.join('\n')}\n`);
|
|
191
|
+
if (result?.total != null) writeStdout(`Total: ${result.total}\n`);
|
|
192
|
+
}
|
|
193
|
+
|
|
156
194
|
export function printKbResult(op, result, {
|
|
157
195
|
format = 'text',
|
|
158
196
|
writeStdout = defaultWriteStdout,
|
|
@@ -164,14 +202,15 @@ export function printKbResult(op, result, {
|
|
|
164
202
|
}
|
|
165
203
|
|
|
166
204
|
if (op === 'kb-ask') {
|
|
167
|
-
const answer = result?.answer || '';
|
|
205
|
+
const answer = result?.answer || result?.message?.text || '';
|
|
168
206
|
const citations = Array.isArray(result?.citations) ? result.citations : [];
|
|
169
207
|
writeStdout(answer);
|
|
170
208
|
if (citations.length > 0) {
|
|
171
209
|
writeStdout('\n\nSources:\n');
|
|
172
210
|
for (const c of citations) {
|
|
173
|
-
const
|
|
174
|
-
const
|
|
211
|
+
const citationType = c.type || c.entity_type || null;
|
|
212
|
+
const sourceType = citationType ? `[${String(citationType).toUpperCase()}] ` : '';
|
|
213
|
+
const id = c.noteId ?? c.todoId ?? c.entity_id ?? 'unknown';
|
|
175
214
|
writeStdout(` - ${sourceType}[${id}] ${c.title}\n`);
|
|
176
215
|
}
|
|
177
216
|
} else {
|