@clarionstack/notesia 0.0.2 → 0.0.5
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 +70 -4
- package/bin/notesia.mjs +16 -5
- package/package.json +3 -3
- package/src/commands.mjs +153 -7
- package/src/import.mjs +303 -0
- package/src/io.mjs +15 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Notesia CLI
|
|
2
2
|
|
|
3
|
-
Local CLI for reading
|
|
3
|
+
Local CLI for reading, updating, and bulk-importing notes in Notesia through the desktop app bridge.
|
|
4
4
|
|
|
5
5
|
## Requirements
|
|
6
6
|
|
|
@@ -27,14 +27,43 @@ npm install -g @clarionstack/notesia
|
|
|
27
27
|
- `notesia sync`
|
|
28
28
|
- `notesia auth status [--format text|json]`
|
|
29
29
|
|
|
30
|
+
### Bulk Import
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
notesia import <path> [options]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Recursively discovers files in a directory and imports them as notes into Notesia.
|
|
37
|
+
|
|
38
|
+
| Option | Description | Default |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `<path>` | Source directory or file (required) | — |
|
|
41
|
+
| `--ext <list>` | Comma-separated file extensions | `md,txt` |
|
|
42
|
+
| `--folder <name>` | Target folder name (created if absent) | — |
|
|
43
|
+
| `--tags <list>` | Comma-separated tags for all imported notes | — |
|
|
44
|
+
| `--conflict <mode>` | `skip` or `overwrite` existing notes by title | `skip` |
|
|
45
|
+
| `--dry-run` | Preview what would be imported without writing | — |
|
|
46
|
+
| `--format <fmt>` | Output format: `text` or `json` | `text` |
|
|
47
|
+
| `--quiet` | Suppress stdout output | — |
|
|
48
|
+
| `--verbose` | Print per-file progress to stderr | — |
|
|
49
|
+
|
|
50
|
+
Supported file types: `.md`, `.txt`, `.html`, `.pdf` (requires `pdfjs-dist`).
|
|
51
|
+
|
|
52
|
+
Notes exceeding 100,000 characters are automatically split into multiple parts.
|
|
53
|
+
|
|
30
54
|
### AI Knowledge Base (`kb`)
|
|
31
55
|
|
|
32
|
-
- `notesia kb search --query "<text>" [--limit <n>] [--format text|json]`
|
|
56
|
+
- `notesia kb search --query "<text>" [--limit <n>] [--threshold <0-1>] [--format text|json]`
|
|
57
|
+
- `notesia kb ask --query "<text>" [--limit <n>] [--threshold <0-1>] [--conversation-id <uuid>] [--format text|json]`
|
|
33
58
|
- `notesia kb folders [--format text|json]`
|
|
34
|
-
- `notesia kb context --query "<text>" [--limit <n>]` *(always JSON)*
|
|
59
|
+
- `notesia kb context --query "<text>" [--limit <n>] [--threshold <0-1>]` *(always JSON)*
|
|
60
|
+
|
|
61
|
+
`kb ask` automatically reuses the same conversation in the same terminal session. Use `--conversation-id` only when you want to override/reset the session.
|
|
35
62
|
|
|
36
63
|
## Examples
|
|
37
64
|
|
|
65
|
+
### Basic note operations
|
|
66
|
+
|
|
38
67
|
```bash
|
|
39
68
|
notesia get --id 42 > note.md
|
|
40
69
|
notesia get --id 42 --with-meta # prepends YAML frontmatter
|
|
@@ -46,7 +75,29 @@ notesia sync
|
|
|
46
75
|
notesia auth status
|
|
47
76
|
```
|
|
48
77
|
|
|
49
|
-
|
|
78
|
+
### Bulk import
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# Import all markdown and text files from a directory
|
|
82
|
+
notesia import ~/Documents/notes
|
|
83
|
+
|
|
84
|
+
# Import into a specific folder with tags
|
|
85
|
+
notesia import ./project-docs --folder "Work" --tags "project,2026"
|
|
86
|
+
|
|
87
|
+
# Import HTML and PDF files too
|
|
88
|
+
notesia import ./archive --ext md,txt,html,pdf
|
|
89
|
+
|
|
90
|
+
# Overwrite existing notes with the same title
|
|
91
|
+
notesia import ./updated-notes --conflict overwrite
|
|
92
|
+
|
|
93
|
+
# Preview what would be imported
|
|
94
|
+
notesia import ~/notes --dry-run --verbose
|
|
95
|
+
|
|
96
|
+
# JSON output for scripting
|
|
97
|
+
notesia import ./docs --format json --quiet 2>/dev/null | jq '.created'
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### AI Knowledge Base
|
|
50
101
|
|
|
51
102
|
```bash
|
|
52
103
|
# Check auth
|
|
@@ -58,9 +109,16 @@ notesia kb folders --json
|
|
|
58
109
|
# Find relevant notes
|
|
59
110
|
notesia kb search --query "project plan" --limit 10
|
|
60
111
|
|
|
112
|
+
# Ask KB directly (searches notes and todos)
|
|
113
|
+
notesia kb ask --query "what are my pending tasks?"
|
|
114
|
+
|
|
61
115
|
# Load full context for AI prompt (returns JSON with full content)
|
|
62
116
|
notesia kb context --query "Q1 roadmap" --limit 5
|
|
63
117
|
|
|
118
|
+
# Continue a KB conversation across calls
|
|
119
|
+
notesia kb ask --query "what did I change yesterday?"
|
|
120
|
+
notesia kb ask --query "and what about folder sync?"
|
|
121
|
+
|
|
64
122
|
# Create a tagged note
|
|
65
123
|
echo "## Summary\n..." | notesia store --title "AI Summary" --tag ai --tag generated
|
|
66
124
|
|
|
@@ -68,6 +126,14 @@ echo "## Summary\n..." | notesia store --title "AI Summary" --tag ai --tag gener
|
|
|
68
126
|
notesia get --id 42 --with-meta
|
|
69
127
|
```
|
|
70
128
|
|
|
129
|
+
## Exit Codes
|
|
130
|
+
|
|
131
|
+
| Code | Meaning |
|
|
132
|
+
|---|---|
|
|
133
|
+
| `0` | Success (including skipped items) |
|
|
134
|
+
| `1` | Runtime error (bridge unavailable, failed items, renderer error) |
|
|
135
|
+
| `2` | Invalid arguments |
|
|
136
|
+
|
|
71
137
|
## Security Notes
|
|
72
138
|
|
|
73
139
|
- **Secure notes** (encrypted vault notes) are **never** exposed through the CLI
|
package/bin/notesia.mjs
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { runCli } from '../src/commands.mjs';
|
|
3
|
+
import { runImport } from '../src/import.mjs';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const argv = process.argv.slice(2);
|
|
6
|
+
|
|
7
|
+
// Dispatch `notesia import` to the dedicated import handler
|
|
8
|
+
if (argv[0] === 'import') {
|
|
9
|
+
runImport(argv.slice(1)).catch((error) => {
|
|
10
|
+
process.stderr.write(`${error?.message || 'notesia import failed'}\n`);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
runCli(argv).catch((error) => {
|
|
15
|
+
const message = error?.message || 'notesia CLI failed';
|
|
16
|
+
process.stderr.write(`${message}\n`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
});
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarionstack/notesia",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
5
|
-
"description": "Notesia desktop bridge CLI",
|
|
4
|
+
"version": "0.0.5",
|
|
5
|
+
"description": "Notesia desktop bridge CLI — read, write, and bulk-import notes",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"bin",
|
|
@@ -24,4 +24,4 @@
|
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=20"
|
|
26
26
|
}
|
|
27
|
-
}
|
|
27
|
+
}
|
package/src/commands.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
2
5
|
import { requestBridge as defaultRequestBridge } from './client.mjs';
|
|
3
6
|
import {
|
|
4
7
|
printListResult,
|
|
@@ -9,11 +12,96 @@ import {
|
|
|
9
12
|
readInputContent as defaultReadInputContent,
|
|
10
13
|
} from './io.mjs';
|
|
11
14
|
|
|
12
|
-
const SUPPORTED_OPS = new Set(['get', 'list', 'store', 'update', 'append', 'sync', 'auth-status', 'kb-search', 'kb-folders', 'kb-context', 'trash']);
|
|
15
|
+
const SUPPORTED_OPS = new Set(['get', 'list', 'store', 'update', 'append', 'sync', 'auth-status', 'kb-search', 'kb-folders', 'kb-context', 'kb-ask', 'trash']);
|
|
13
16
|
const OP_ALIASES = new Map([
|
|
14
17
|
['retrieve', 'get'],
|
|
15
18
|
['storage', 'store'],
|
|
16
19
|
]);
|
|
20
|
+
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
|
+
const KB_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
22
|
+
const SAFE_SEGMENT = /[^a-zA-Z0-9_-]/g;
|
|
23
|
+
|
|
24
|
+
function toSafeSegment(value, fallback = 'user') {
|
|
25
|
+
const normalized = String(value || '').trim().replace(SAFE_SEGMENT, '_');
|
|
26
|
+
return normalized || fallback;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getKbSessionStorePath({
|
|
30
|
+
tmpDir = os.tmpdir(),
|
|
31
|
+
user = os.userInfo().username,
|
|
32
|
+
} = {}) {
|
|
33
|
+
return path.join(tmpDir, `notesia-cli-kb-session-${toSafeSegment(user)}.json`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getTerminalSessionKey() {
|
|
37
|
+
return (
|
|
38
|
+
process.env.NOTESIA_CLI_SESSION_ID
|
|
39
|
+
|| process.env.TERM_SESSION_ID
|
|
40
|
+
|| process.env.WT_SESSION
|
|
41
|
+
|| (process.env.TMUX_PANE ? `tmux:${process.env.TMUX_PANE}` : null)
|
|
42
|
+
|| `ppid:${process.ppid || process.pid}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function loadKbSessionStore(sessionFilePath) {
|
|
47
|
+
try {
|
|
48
|
+
const raw = await fs.readFile(sessionFilePath, 'utf8');
|
|
49
|
+
const parsed = JSON.parse(raw);
|
|
50
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
51
|
+
} catch {
|
|
52
|
+
return {};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function saveKbSessionStore(sessionFilePath, store) {
|
|
57
|
+
await fs.writeFile(sessionFilePath, JSON.stringify(store, null, 2), 'utf8');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function pruneKbSessionStore(store, nowMs) {
|
|
61
|
+
const pruned = {};
|
|
62
|
+
for (const [key, value] of Object.entries(store || {})) {
|
|
63
|
+
if (!value || typeof value !== 'object') continue;
|
|
64
|
+
if (!UUID_REGEX.test(value.conversationId || '')) continue;
|
|
65
|
+
const updatedAt = Number(value.updatedAt || 0);
|
|
66
|
+
if (!Number.isFinite(updatedAt) || nowMs - updatedAt > KB_SESSION_TTL_MS) continue;
|
|
67
|
+
pruned[key] = {
|
|
68
|
+
conversationId: value.conversationId,
|
|
69
|
+
updatedAt,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return pruned;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function defaultResolveKbConversationId(explicitConversationId = null) {
|
|
76
|
+
const nowMs = Date.now();
|
|
77
|
+
const sessionFilePath = getKbSessionStorePath();
|
|
78
|
+
const sessionKey = getTerminalSessionKey();
|
|
79
|
+
const fallbackId = explicitConversationId || randomUUID();
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const loaded = await loadKbSessionStore(sessionFilePath);
|
|
83
|
+
const store = pruneKbSessionStore(loaded, nowMs);
|
|
84
|
+
|
|
85
|
+
if (explicitConversationId) {
|
|
86
|
+
store[sessionKey] = { conversationId: explicitConversationId, updatedAt: nowMs };
|
|
87
|
+
await saveKbSessionStore(sessionFilePath, store);
|
|
88
|
+
return explicitConversationId;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const existingId = store[sessionKey]?.conversationId;
|
|
92
|
+
if (UUID_REGEX.test(existingId || '')) {
|
|
93
|
+
store[sessionKey].updatedAt = nowMs;
|
|
94
|
+
await saveKbSessionStore(sessionFilePath, store);
|
|
95
|
+
return existingId;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
store[sessionKey] = { conversationId: fallbackId, updatedAt: nowMs };
|
|
99
|
+
await saveKbSessionStore(sessionFilePath, store);
|
|
100
|
+
return fallbackId;
|
|
101
|
+
} catch {
|
|
102
|
+
return fallbackId;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
17
105
|
|
|
18
106
|
export class CliCommandError extends Error {
|
|
19
107
|
constructor(code, message) {
|
|
@@ -51,11 +139,17 @@ export function parseArgs(argv = []) {
|
|
|
51
139
|
if (!subcommand || subcommand === '--help' || subcommand === '-h') {
|
|
52
140
|
return { op: 'help' };
|
|
53
141
|
}
|
|
54
|
-
const kbOps = { search: 'kb-search', folders: 'kb-folders', context: 'kb-context' };
|
|
142
|
+
const kbOps = { search: 'kb-search', folders: 'kb-folders', context: 'kb-context', ask: 'kb-ask' };
|
|
55
143
|
if (!kbOps[subcommand]) {
|
|
56
144
|
throw new CliCommandError('INVALID_OP', `Unsupported kb command: ${subcommand} `);
|
|
57
145
|
}
|
|
58
146
|
op = kbOps[subcommand];
|
|
147
|
+
|
|
148
|
+
// Support positional query arg for kb ask: `notesia kb ask "my question"`
|
|
149
|
+
if (op === 'kb-ask' && args.length > 0 && !args[0].startsWith('--')) {
|
|
150
|
+
// Stash the positional query; it will be picked up below as parsed.query
|
|
151
|
+
args.unshift('--query', args.shift());
|
|
152
|
+
}
|
|
59
153
|
}
|
|
60
154
|
|
|
61
155
|
if (!op || op === '--help' || op === '-h') {
|
|
@@ -74,8 +168,10 @@ export function parseArgs(argv = []) {
|
|
|
74
168
|
folderId: null,
|
|
75
169
|
content: undefined,
|
|
76
170
|
limit: null,
|
|
171
|
+
threshold: null,
|
|
77
172
|
withMeta: false,
|
|
78
173
|
query: null,
|
|
174
|
+
conversationId: null,
|
|
79
175
|
tags: [],
|
|
80
176
|
};
|
|
81
177
|
|
|
@@ -136,6 +232,16 @@ export function parseArgs(argv = []) {
|
|
|
136
232
|
continue;
|
|
137
233
|
}
|
|
138
234
|
|
|
235
|
+
if (token === '--threshold') {
|
|
236
|
+
const threshold = Number(readFlagValue(args, i));
|
|
237
|
+
if (!Number.isFinite(threshold) || threshold < 0 || threshold > 1) {
|
|
238
|
+
throw new CliCommandError('INVALID_ARGS', '--threshold must be a number between 0 and 1');
|
|
239
|
+
}
|
|
240
|
+
parsed.threshold = threshold;
|
|
241
|
+
i += 1;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
139
245
|
if (token === '--format') {
|
|
140
246
|
const format = readFlagValue(args, i);
|
|
141
247
|
if (format !== 'text' && format !== 'json' && format !== 'table') {
|
|
@@ -162,6 +268,16 @@ export function parseArgs(argv = []) {
|
|
|
162
268
|
continue;
|
|
163
269
|
}
|
|
164
270
|
|
|
271
|
+
if (token === '--conversation-id') {
|
|
272
|
+
const value = readFlagValue(args, i);
|
|
273
|
+
if (!UUID_REGEX.test(value)) {
|
|
274
|
+
throw new CliCommandError('INVALID_ARGS', '--conversation-id must be a valid UUID');
|
|
275
|
+
}
|
|
276
|
+
parsed.conversationId = value;
|
|
277
|
+
i += 1;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
165
281
|
if (token === '--tag') {
|
|
166
282
|
parsed.tags.push(readFlagValue(args, i));
|
|
167
283
|
i += 1;
|
|
@@ -194,16 +310,26 @@ function usageText() {
|
|
|
194
310
|
' sync',
|
|
195
311
|
' auth status [--format text|json]',
|
|
196
312
|
'',
|
|
313
|
+
'Bulk Import:',
|
|
314
|
+
' import <path> [--ext md,txt] [--folder <name>] [--tags <list>]',
|
|
315
|
+
' [--conflict skip|overwrite] [--dry-run]',
|
|
316
|
+
' [--format text|json] [--quiet] [--verbose]',
|
|
317
|
+
'',
|
|
197
318
|
'AI Knowledge Base:',
|
|
198
|
-
' kb search --query <text> [--limit <n>] [--format text|json]',
|
|
319
|
+
' kb search --query <text> [--limit <n>] [--threshold <0-1>] [--format text|json]',
|
|
320
|
+
' kb ask --query <text> [--limit <n>] [--threshold <0-1>] [--conversation-id <uuid>] [--format text|json]',
|
|
199
321
|
' kb folders [--format text|json]',
|
|
200
|
-
' kb context --query <text> [--limit <n>]',
|
|
322
|
+
' kb context --query <text> [--limit <n>] [--threshold <0-1>]',
|
|
201
323
|
'',
|
|
202
324
|
'Pipe examples:',
|
|
203
325
|
' notesia get --id 12 > note.md',
|
|
204
326
|
' notesia list --json > notes.json',
|
|
205
327
|
' cat patch.md | notesia update --id 12',
|
|
206
328
|
' echo "- bullet" | notesia append --id 12',
|
|
329
|
+
' notesia import ./notes --ext md,txt --folder "Imported" --verbose',
|
|
330
|
+
'',
|
|
331
|
+
'Note:',
|
|
332
|
+
' kb ask reuses the same conversation automatically in the same terminal session.',
|
|
207
333
|
'',
|
|
208
334
|
].join('\n');
|
|
209
335
|
}
|
|
@@ -287,7 +413,7 @@ function buildPayload(parsed, pipedContent) {
|
|
|
287
413
|
if (!parsed.query) {
|
|
288
414
|
throw new CliCommandError('MISSING_ARGS', '--query is required for kb search');
|
|
289
415
|
}
|
|
290
|
-
return { query: parsed.query, limit: parsed.limit ?? 20 };
|
|
416
|
+
return { query: parsed.query, limit: parsed.limit ?? 20, threshold: parsed.threshold ?? undefined };
|
|
291
417
|
}
|
|
292
418
|
|
|
293
419
|
if (parsed.op === 'kb-folders') {
|
|
@@ -298,7 +424,23 @@ function buildPayload(parsed, pipedContent) {
|
|
|
298
424
|
if (!parsed.query) {
|
|
299
425
|
throw new CliCommandError('MISSING_ARGS', '--query is required for kb context');
|
|
300
426
|
}
|
|
301
|
-
return {
|
|
427
|
+
return {
|
|
428
|
+
query: parsed.query,
|
|
429
|
+
limit: parsed.limit ?? 5,
|
|
430
|
+
threshold: parsed.threshold ?? undefined,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (parsed.op === 'kb-ask') {
|
|
435
|
+
if (!parsed.query) {
|
|
436
|
+
throw new CliCommandError('MISSING_ARGS', '--query is required for kb ask');
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
query: parsed.query,
|
|
440
|
+
limit: parsed.limit ?? 5,
|
|
441
|
+
threshold: parsed.threshold ?? undefined,
|
|
442
|
+
conversation_id: parsed.conversationId ?? undefined,
|
|
443
|
+
};
|
|
302
444
|
}
|
|
303
445
|
|
|
304
446
|
throw new CliCommandError('INVALID_OP', `Unsupported command: ${parsed.op} `);
|
|
@@ -308,6 +450,7 @@ export async function runCli(argv = [], {
|
|
|
308
450
|
requestBridge = defaultRequestBridge,
|
|
309
451
|
readInputContent = defaultReadInputContent,
|
|
310
452
|
writeStdout = (chunk) => process.stdout.write(chunk),
|
|
453
|
+
resolveKbConversationId = defaultResolveKbConversationId,
|
|
311
454
|
} = {}) {
|
|
312
455
|
const parsed = parseArgs(argv);
|
|
313
456
|
|
|
@@ -320,6 +463,9 @@ export async function runCli(argv = [], {
|
|
|
320
463
|
const pipedContent = needsPipedInput ? await readInputContent() : undefined;
|
|
321
464
|
|
|
322
465
|
const payload = buildPayload(parsed, pipedContent);
|
|
466
|
+
if (parsed.op === 'kb-ask' && typeof resolveKbConversationId === 'function') {
|
|
467
|
+
payload.conversation_id = await resolveKbConversationId(payload.conversation_id ?? null);
|
|
468
|
+
}
|
|
323
469
|
const request = {
|
|
324
470
|
id: randomUUID(),
|
|
325
471
|
op: parsed.op,
|
|
@@ -352,7 +498,7 @@ export async function runCli(argv = [], {
|
|
|
352
498
|
format: parsed.format,
|
|
353
499
|
writeStdout,
|
|
354
500
|
});
|
|
355
|
-
} else if (['kb-search', 'kb-folders', 'kb-context'].includes(parsed.op)) {
|
|
501
|
+
} else if (['kb-search', 'kb-folders', 'kb-context', 'kb-ask'].includes(parsed.op)) {
|
|
356
502
|
printKbResult(parsed.op, response.data, { format: parsed.format, writeStdout });
|
|
357
503
|
} else {
|
|
358
504
|
printMutationResult(response.data, {
|
package/src/import.mjs
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* notesia import <path> [options]
|
|
3
|
+
*
|
|
4
|
+
* Bulk-imports files from a directory into Notesia via the bridge.
|
|
5
|
+
* File discovery and content parsing happen here (in the CLI process).
|
|
6
|
+
* Dexie writes happen in the renderer via the bulk_import bridge op.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createReadStream } from 'node:fs';
|
|
10
|
+
import fs from 'node:fs/promises';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { requestBridge } from './client.mjs';
|
|
13
|
+
|
|
14
|
+
const NOTE_CHAR_LIMIT = 100_000;
|
|
15
|
+
|
|
16
|
+
const USAGE = [
|
|
17
|
+
'Usage: notesia import <path> [options]',
|
|
18
|
+
'',
|
|
19
|
+
'Options:',
|
|
20
|
+
' --ext <list> Comma-separated extensions to import (default: md,txt)',
|
|
21
|
+
' --folder <name> Target folder name in Notesia',
|
|
22
|
+
' --tags <list> Comma-separated tags to apply to all imported notes',
|
|
23
|
+
' --conflict <mode> Conflict resolution: skip | overwrite (default: skip)',
|
|
24
|
+
' --dry-run Preview without writing',
|
|
25
|
+
' --format <fmt> Output format: text | json (default: text)',
|
|
26
|
+
' --quiet Suppress stdout output',
|
|
27
|
+
' --verbose Print per-file progress to stderr',
|
|
28
|
+
'',
|
|
29
|
+
].join('\n');
|
|
30
|
+
|
|
31
|
+
// ─── Argument parsing ────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
export function parseImportArgs(argv) {
|
|
34
|
+
const args = [...argv];
|
|
35
|
+
|
|
36
|
+
if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
|
|
37
|
+
process.stderr.write(USAGE);
|
|
38
|
+
process.exit(0);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const importPath = args.shift();
|
|
42
|
+
if (!importPath || importPath.startsWith('--')) {
|
|
43
|
+
process.stderr.write('Error: <path> argument is required\n\n' + USAGE);
|
|
44
|
+
process.exit(2);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const opts = {
|
|
48
|
+
importPath: path.resolve(importPath),
|
|
49
|
+
ext: ['md', 'txt'],
|
|
50
|
+
folder: null,
|
|
51
|
+
tags: [],
|
|
52
|
+
conflict: 'skip',
|
|
53
|
+
dryRun: false,
|
|
54
|
+
format: 'text',
|
|
55
|
+
quiet: false,
|
|
56
|
+
verbose: false,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < args.length; i++) {
|
|
60
|
+
const token = args[i];
|
|
61
|
+
|
|
62
|
+
if (token === '--ext') {
|
|
63
|
+
const val = args[++i];
|
|
64
|
+
if (!val || val.startsWith('--')) { process.stderr.write('Error: --ext requires a value\n\n' + USAGE); process.exit(2); }
|
|
65
|
+
opts.ext = val.split(',').map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
66
|
+
} else if (token === '--folder') {
|
|
67
|
+
const val = args[++i];
|
|
68
|
+
if (!val || val.startsWith('--')) { process.stderr.write('Error: --folder requires a value\n\n' + USAGE); process.exit(2); }
|
|
69
|
+
opts.folder = val;
|
|
70
|
+
} else if (token === '--tags') {
|
|
71
|
+
const val = args[++i];
|
|
72
|
+
if (!val || val.startsWith('--')) { process.stderr.write('Error: --tags requires a value\n\n' + USAGE); process.exit(2); }
|
|
73
|
+
opts.tags = val.split(',').map((t) => t.trim()).filter(Boolean);
|
|
74
|
+
} else if (token === '--conflict') {
|
|
75
|
+
const val = args[++i];
|
|
76
|
+
if (val !== 'skip' && val !== 'overwrite') { process.stderr.write('Error: --conflict must be "skip" or "overwrite"\n\n' + USAGE); process.exit(2); }
|
|
77
|
+
opts.conflict = val;
|
|
78
|
+
} else if (token === '--dry-run') {
|
|
79
|
+
opts.dryRun = true;
|
|
80
|
+
} else if (token === '--format') {
|
|
81
|
+
const val = args[++i];
|
|
82
|
+
if (val !== 'text' && val !== 'json') { process.stderr.write('Error: --format must be "text" or "json"\n\n' + USAGE); process.exit(2); }
|
|
83
|
+
opts.format = val;
|
|
84
|
+
} else if (token === '--quiet') {
|
|
85
|
+
opts.quiet = true;
|
|
86
|
+
} else if (token === '--verbose') {
|
|
87
|
+
opts.verbose = true;
|
|
88
|
+
} else {
|
|
89
|
+
process.stderr.write(`Error: Unknown flag: ${token}\n\n` + USAGE);
|
|
90
|
+
process.exit(2);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return opts;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ─── File discovery ──────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
async function discoverFiles(rootPath, extensions) {
|
|
100
|
+
const normalizedExts = extensions.map((e) => e.toLowerCase());
|
|
101
|
+
const results = [];
|
|
102
|
+
|
|
103
|
+
async function walk(dir, relBase) {
|
|
104
|
+
let entries;
|
|
105
|
+
try {
|
|
106
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
107
|
+
} catch {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
for (const entry of entries) {
|
|
111
|
+
if (entry.isSymbolicLink()) continue;
|
|
112
|
+
const relPath = relBase ? `${relBase}/${entry.name}` : entry.name;
|
|
113
|
+
const absPath = path.join(dir, entry.name);
|
|
114
|
+
if (entry.isDirectory()) {
|
|
115
|
+
await walk(absPath, relPath);
|
|
116
|
+
} else {
|
|
117
|
+
const ext = path.extname(entry.name).slice(1).toLowerCase();
|
|
118
|
+
if (!normalizedExts.includes(ext)) continue;
|
|
119
|
+
const stat = await fs.stat(absPath).catch(() => null);
|
|
120
|
+
if (!stat) continue;
|
|
121
|
+
results.push({ absolutePath: absPath, relativePath: relPath, extension: ext, sizeBytes: stat.size });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
await walk(rootPath, '');
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ─── Content parsing ─────────────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
function titleFromPath(filePath) {
|
|
133
|
+
const stem = path.basename(filePath, path.extname(filePath));
|
|
134
|
+
return stem.replace(/[-_]+/g, ' ').replace(/\s+/g, ' ').trim();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function stripHtml(html) {
|
|
138
|
+
return html
|
|
139
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
140
|
+
.replace(/<[^>]+>/g, '')
|
|
141
|
+
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
142
|
+
.replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, ' ')
|
|
143
|
+
.trim();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function parseFile(absolutePath, extension) {
|
|
147
|
+
const defaultTitle = titleFromPath(absolutePath);
|
|
148
|
+
try {
|
|
149
|
+
if (extension === 'md' || extension === 'txt') {
|
|
150
|
+
const body = await fs.readFile(absolutePath, 'utf8');
|
|
151
|
+
return { title: defaultTitle, body };
|
|
152
|
+
}
|
|
153
|
+
if (extension === 'html' || extension === 'htm') {
|
|
154
|
+
const raw = await fs.readFile(absolutePath, 'utf8');
|
|
155
|
+
const titleMatch = raw.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
|
|
156
|
+
const htmlTitle = titleMatch ? titleMatch[1].replace(/\s+/g, ' ').trim() : null;
|
|
157
|
+
return { title: htmlTitle || defaultTitle, body: stripHtml(raw) };
|
|
158
|
+
}
|
|
159
|
+
if (extension === 'pdf') {
|
|
160
|
+
// Dynamic import so missing pdfjs-dist doesn't break non-PDF imports
|
|
161
|
+
try {
|
|
162
|
+
const { default: pdfjsLib } = await import('pdfjs-dist/legacy/build/pdf.js');
|
|
163
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = '';
|
|
164
|
+
const data = await fs.readFile(absolutePath);
|
|
165
|
+
const pdf = await pdfjsLib.getDocument({ data: new Uint8Array(data), disableWorker: true }).promise;
|
|
166
|
+
const pages = [];
|
|
167
|
+
for (let i = 1; i <= pdf.numPages; i++) {
|
|
168
|
+
const page = await pdf.getPage(i);
|
|
169
|
+
const content = await page.getTextContent();
|
|
170
|
+
pages.push(content.items.map((item) => item.str).join(' '));
|
|
171
|
+
page.cleanup();
|
|
172
|
+
}
|
|
173
|
+
let title = defaultTitle;
|
|
174
|
+
try { const meta = await pdf.getMetadata(); if (meta?.info?.Title?.trim()) title = meta.info.Title.trim(); } catch {}
|
|
175
|
+
return { title, body: pages.join('\n\n').replace(/\s+\n/g, '\n').trim() };
|
|
176
|
+
} catch (err) {
|
|
177
|
+
return { title: defaultTitle, body: '', error: err.message };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return { title: defaultTitle, body: '', error: `Unsupported extension: ${extension}` };
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return { title: defaultTitle, body: '', error: err.message };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ─── Note splitter ───────────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
function splitContent(title, body) {
|
|
189
|
+
if (body.length <= NOTE_CHAR_LIMIT) return [{ title, content: body }];
|
|
190
|
+
|
|
191
|
+
const chunks = [];
|
|
192
|
+
let remaining = body;
|
|
193
|
+
while (remaining.length > 0) {
|
|
194
|
+
if (remaining.length <= NOTE_CHAR_LIMIT) { chunks.push(remaining); break; }
|
|
195
|
+
const searchStart = Math.max(0, NOTE_CHAR_LIMIT - 500);
|
|
196
|
+
const slice = remaining.slice(searchStart, NOTE_CHAR_LIMIT);
|
|
197
|
+
const lastBreak = slice.lastIndexOf('\n\n');
|
|
198
|
+
const splitAt = lastBreak !== -1 ? searchStart + lastBreak + 2 : NOTE_CHAR_LIMIT;
|
|
199
|
+
chunks.push(remaining.slice(0, splitAt));
|
|
200
|
+
remaining = remaining.slice(splitAt);
|
|
201
|
+
}
|
|
202
|
+
return chunks.map((content, i) => ({ title: `${title} (Part ${i + 1})`, content }));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ─── Output formatting ───────────────────────────────────────────────────────
|
|
206
|
+
|
|
207
|
+
function formatElapsed(ms) { return `${(ms / 1000).toFixed(1)}s`; }
|
|
208
|
+
|
|
209
|
+
function printResult(result, opts) {
|
|
210
|
+
const { format = 'text', quiet = false } = opts;
|
|
211
|
+
if (quiet) return;
|
|
212
|
+
if (format === 'json') { process.stdout.write(JSON.stringify(result) + '\n'); return; }
|
|
213
|
+
const { created, skipped, overwritten, failed, elapsedMs } = result;
|
|
214
|
+
const parts = [`${created} created`];
|
|
215
|
+
if (overwritten > 0) parts.push(`${overwritten} overwritten`);
|
|
216
|
+
parts.push(`${skipped} skipped`, `${failed} failed`);
|
|
217
|
+
process.stdout.write(`Import complete: ${parts.join(', ')} (${formatElapsed(elapsedMs)})\n`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ─── Main entry point ────────────────────────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
export async function runImport(argv, deps = {}) {
|
|
223
|
+
const opts = parseImportArgs(argv);
|
|
224
|
+
const bridge = deps.requestBridge || requestBridge;
|
|
225
|
+
|
|
226
|
+
// Discover files
|
|
227
|
+
let files;
|
|
228
|
+
try {
|
|
229
|
+
files = await discoverFiles(opts.importPath, opts.ext);
|
|
230
|
+
} catch (err) {
|
|
231
|
+
process.stderr.write(`[IO_ERROR] Failed to read directory: ${err.message}\n`);
|
|
232
|
+
process.exit(1);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (files.length === 0) {
|
|
236
|
+
process.stderr.write(`[NO_FILES] No files found matching [${opts.ext.join(', ')}] in: ${opts.importPath}\n`);
|
|
237
|
+
process.exit(0);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Parse and split
|
|
241
|
+
const items = [];
|
|
242
|
+
for (const file of files) {
|
|
243
|
+
if (opts.verbose) process.stderr.write(` [parsing] ${file.relativePath}\n`);
|
|
244
|
+
|
|
245
|
+
const parsed = await parseFile(file.absolutePath, file.extension);
|
|
246
|
+
|
|
247
|
+
if (parsed.error && !parsed.body) {
|
|
248
|
+
if (opts.verbose) process.stderr.write(` [failed] ${file.relativePath} — ${parsed.error}\n`);
|
|
249
|
+
items.push({ title: parsed.title, content: '', source_path: file.relativePath, _parseError: parsed.error });
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const chunks = splitContent(parsed.title, parsed.body);
|
|
254
|
+
for (const chunk of chunks) {
|
|
255
|
+
items.push({ title: chunk.title, content: chunk.content, source_path: file.relativePath });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (opts.verbose) {
|
|
259
|
+
const suffix = chunks.length > 1 ? ` (split into ${chunks.length} parts)` : '';
|
|
260
|
+
process.stderr.write(` [done] ${file.relativePath} — "${parsed.title}"${suffix}\n`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const payload = {
|
|
265
|
+
items: items.map((item) => ({
|
|
266
|
+
title: item.title,
|
|
267
|
+
content: item._parseError ? '' : item.content,
|
|
268
|
+
source_path: item.source_path,
|
|
269
|
+
_parseError: item._parseError || null,
|
|
270
|
+
})),
|
|
271
|
+
folder: opts.folder,
|
|
272
|
+
tags: opts.tags,
|
|
273
|
+
conflict: opts.conflict,
|
|
274
|
+
dry_run: opts.dryRun,
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const timeoutMs = Number(process.env.NOTESIA_CLI_BULK_TIMEOUT_MS) || 120_000;
|
|
278
|
+
const startMs = Date.now();
|
|
279
|
+
|
|
280
|
+
let response;
|
|
281
|
+
try {
|
|
282
|
+
response = await bridge({ op: 'bulk_import', payload }, { timeoutMs });
|
|
283
|
+
} catch (err) {
|
|
284
|
+
const code = err.code || 'BRIDGE_ERROR';
|
|
285
|
+
if (code === 'NOTESIA_APP_NOT_RUNNING') {
|
|
286
|
+
process.stderr.write('NOTESIA_APP_NOT_RUNNING\n');
|
|
287
|
+
process.exit(1);
|
|
288
|
+
}
|
|
289
|
+
process.stderr.write(`[${code}] ${err.message}\n`);
|
|
290
|
+
process.exit(1);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (!response || response.ok === false) {
|
|
294
|
+
const code = response?.error?.code || 'BRIDGE_ERROR';
|
|
295
|
+
const message = response?.error?.message || 'Bridge request failed';
|
|
296
|
+
process.stderr.write(`[${code}] ${message}\n`);
|
|
297
|
+
process.exit(1);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const result = { ...response.data, elapsedMs: response.data?.elapsedMs ?? (Date.now() - startMs) };
|
|
301
|
+
printResult(result, { format: opts.format, quiet: opts.quiet });
|
|
302
|
+
process.exit(result.failed === 0 ? 0 : 1);
|
|
303
|
+
}
|
package/src/io.mjs
CHANGED
|
@@ -163,6 +163,21 @@ export function printKbResult(op, result, {
|
|
|
163
163
|
return;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
if (op === 'kb-ask') {
|
|
167
|
+
const answer = result?.answer || '';
|
|
168
|
+
const citations = Array.isArray(result?.citations) ? result.citations : [];
|
|
169
|
+
writeStdout(answer);
|
|
170
|
+
if (citations.length > 0) {
|
|
171
|
+
writeStdout('\n\nSources:\n');
|
|
172
|
+
for (const c of citations) {
|
|
173
|
+
writeStdout(` - [${c.noteId}] ${c.title}\n`);
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
writeStdout('\n');
|
|
177
|
+
}
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
166
181
|
if (op === 'kb-search') {
|
|
167
182
|
const notes = Array.isArray(result?.notes) ? result.notes : [];
|
|
168
183
|
if (notes.length === 0) {
|