@clarionstack/notesia 0.0.6 → 0.0.7

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 CHANGED
@@ -38,7 +38,7 @@ Recursively discovers files in a directory and imports them as notes into Notesi
38
38
  | Option | Description | Default |
39
39
  |---|---|---|
40
40
  | `<path>` | Source directory or file (required) | — |
41
- | `--ext <list>` | Comma-separated file extensions | `md,txt` |
41
+ | `--ext <list>` | Comma-separated file extensions | `md,txt,pdf,docx,json` |
42
42
  | `--folder <name>` | Target folder name (created if absent) | — |
43
43
  | `--tags <list>` | Comma-separated tags for all imported notes | — |
44
44
  | `--conflict <mode>` | `skip` or `overwrite` existing notes by title | `skip` |
@@ -47,7 +47,9 @@ Recursively discovers files in a directory and imports them as notes into Notesi
47
47
  | `--quiet` | Suppress stdout output | — |
48
48
  | `--verbose` | Print per-file progress to stderr | — |
49
49
 
50
- Supported file types: `.md`, `.txt`, `.html`, `.pdf` (requires `pdfjs-dist`).
50
+ Supported file types: `.md`, `.txt`, `.html`, `.pdf`, `.docx`, `.json`.
51
+
52
+ JSON imports are transformed into flattened key-path text (with truncation guards for very large arrays/strings) to improve embedding quality.
51
53
 
52
54
  Notes exceeding 100,000 characters are automatically split into multiple parts.
53
55
 
@@ -84,8 +86,8 @@ notesia import ~/Documents/notes
84
86
  # Import into a specific folder with tags
85
87
  notesia import ./project-docs --folder "Work" --tags "project,2026"
86
88
 
87
- # Import HTML and PDF files too
88
- notesia import ./archive --ext md,txt,html,pdf
89
+ # Import HTML, PDF, DOCX and JSON files too
90
+ notesia import ./archive --ext md,txt,html,pdf,docx,json
89
91
 
90
92
  # Overwrite existing notes with the same title
91
93
  notesia import ./updated-notes --conflict overwrite
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@clarionstack/notesia",
3
3
  "private": false,
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "description": "Notesia desktop bridge CLI — read, write, and bulk-import notes",
6
6
  "type": "module",
7
7
  "files": [
@@ -23,5 +23,9 @@
23
23
  ],
24
24
  "engines": {
25
25
  "node": ">=20"
26
+ },
27
+ "dependencies": {
28
+ "pdfjs-dist": "^4.0.379",
29
+ "jszip": "^3.10.1"
26
30
  }
27
- }
31
+ }
package/src/commands.mjs CHANGED
@@ -311,7 +311,7 @@ function usageText() {
311
311
  ' auth status [--format text|json]',
312
312
  '',
313
313
  'Bulk Import:',
314
- ' import <path> [--ext md,txt] [--folder <name>] [--tags <list>]',
314
+ ' import <path> [--ext md,txt,pdf,docx,json] [--folder <name>] [--tags <list>]',
315
315
  ' [--conflict skip|overwrite] [--dry-run]',
316
316
  ' [--format text|json] [--quiet] [--verbose]',
317
317
  '',
package/src/import.mjs CHANGED
@@ -6,13 +6,19 @@
6
6
  * Dexie writes happen in the renderer via the bulk_import bridge op.
7
7
  */
8
8
 
9
- import { createReadStream } from 'node:fs';
10
9
  import fs from 'node:fs/promises';
11
10
  import path from 'node:path';
12
11
  import readline from 'node:readline';
13
12
  import { requestBridge } from './client.mjs';
14
13
 
15
14
  const NOTE_CHAR_LIMIT = 100_000;
15
+ const DEFAULT_EXTENSIONS = ['md', 'txt', 'pdf', 'docx', 'json'];
16
+ const JSON_MAX_DEPTH = 12;
17
+ const JSON_MAX_ENTRIES = 5_000;
18
+ const JSON_MAX_ARRAY_ITEMS = 100;
19
+ const JSON_MAX_STRING_LENGTH = 500;
20
+ const JSON_MAX_TOP_LEVEL_KEYS = 25;
21
+ const SIMPLE_JSON_KEY_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
16
22
 
17
23
  const USAGE = [
18
24
  'Usage: notesia import <path> [options]',
@@ -22,7 +28,7 @@ const USAGE = [
22
28
  '',
23
29
  'Options:',
24
30
  ' --path <dir> Path to directory to import',
25
- ' --ext <list> Comma-separated extensions to import (default: md,txt)',
31
+ ` --ext <list> Comma-separated extensions to import (default: ${DEFAULT_EXTENSIONS.join(',')})`,
26
32
  ' --folder <name> Target folder name in Notesia',
27
33
  ' --tags <list> Comma-separated tags to apply to all imported notes',
28
34
  ' --conflict <mode> Conflict resolution: skip | overwrite (default: skip)',
@@ -47,7 +53,7 @@ export function parseImportArgs(argv) {
47
53
 
48
54
  const opts = {
49
55
  importPath: null,
50
- ext: ['md', 'txt'],
56
+ ext: [...DEFAULT_EXTENSIONS],
51
57
  folder: null,
52
58
  tags: [],
53
59
  conflict: 'skip',
@@ -76,6 +82,10 @@ export function parseImportArgs(argv) {
76
82
  } else if (token === '--ext') {
77
83
  const val = args[++i];
78
84
  if (!val || val.startsWith('--')) { process.stderr.write('Error: --ext requires a value\n\n' + USAGE); process.exit(2); }
85
+ opts.ext = val.split(',').map((e) => e.trim().toLowerCase()).filter(Boolean);
86
+ } else if (token === '--folder') {
87
+ const val = args[++i];
88
+ if (!val || val.startsWith('--')) { process.stderr.write('Error: --folder requires a value\n\n' + USAGE); process.exit(2); }
79
89
  opts.folder = val;
80
90
  } else if (token === '--tags') {
81
91
  const val = args[++i];
@@ -149,9 +159,19 @@ async function discoverFiles(rootPath, extensions) {
149
159
 
150
160
  // ─── Content parsing ─────────────────────────────────────────────────────────
151
161
 
152
- function titleFromPath(filePath) {
153
- const stem = path.basename(filePath, path.extname(filePath));
154
- return stem.replace(/[-_]+/g, ' ').replace(/\s+/g, ' ').trim();
162
+ function titleFromPath(filePath, relativePath) {
163
+ // Use relative path to include folder structure in title
164
+ // e.g., "folder1/SKILL.md" → "folder1/SKILL"
165
+ const baseName = path.basename(relativePath, path.extname(relativePath));
166
+ const folderName = path.dirname(relativePath);
167
+ const stem = baseName.replace(/[-_]+/g, ' ').replace(/\s+/g, ' ').trim();
168
+
169
+ // If file is in a subfolder, prepend folder to title
170
+ if (folderName && folderName !== '.') {
171
+ const folderStem = folderName.replace(/[-_]+/g, ' ').replace(/\s+/g, ' ').trim();
172
+ return `${folderStem}/${stem}`;
173
+ }
174
+ return stem;
155
175
  }
156
176
 
157
177
  function stripHtml(html) {
@@ -163,8 +183,185 @@ function stripHtml(html) {
163
183
  .trim();
164
184
  }
165
185
 
166
- async function parseFile(absolutePath, extension) {
167
- const defaultTitle = titleFromPath(absolutePath);
186
+ function decodeXmlEntities(value) {
187
+ if (!value) return '';
188
+ return value
189
+ .replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1')
190
+ .replace(/&amp;/g, '&')
191
+ .replace(/&lt;/g, '<')
192
+ .replace(/&gt;/g, '>')
193
+ .replace(/&quot;/g, '"')
194
+ .replace(/&#39;/g, "'")
195
+ .replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
196
+ .replace(/&#(\d+);/g, (_, dec) => String.fromCharCode(parseInt(dec, 10)))
197
+ .replace(/\u00A0/g, ' ');
198
+ }
199
+
200
+ function sharedLeadingWhitespace(a, b) {
201
+ const max = Math.min(a.length, b.length);
202
+ let i = 0;
203
+ while (i < max && a[i] === b[i]) i++;
204
+ return a.slice(0, i);
205
+ }
206
+
207
+ function normalizeFencedCodeBlocks(markdown) {
208
+ if (!markdown || !markdown.includes('```')) return markdown;
209
+
210
+ return markdown.replace(/```([^\n]*)\n([\s\S]*?)\n```/g, (full, lang, body) => {
211
+ const lines = String(body).replace(/\u00A0/g, ' ').split('\n');
212
+ const nonEmpty = lines.filter((line) => line.trim().length > 0);
213
+ if (nonEmpty.length === 0) return full;
214
+
215
+ let commonIndent = null;
216
+ for (const line of nonEmpty) {
217
+ const indent = line.match(/^[ \t]*/)?.[0] || '';
218
+ if (!indent) {
219
+ commonIndent = '';
220
+ break;
221
+ }
222
+ commonIndent = commonIndent === null ? indent : sharedLeadingWhitespace(commonIndent, indent);
223
+ if (!commonIndent) break;
224
+ }
225
+
226
+ if (!commonIndent) {
227
+ return `\`\`\`${lang}\n${lines.join('\n')}\n\`\`\``;
228
+ }
229
+
230
+ const dedented = lines.map((line) =>
231
+ line.startsWith(commonIndent) ? line.slice(commonIndent.length) : line
232
+ );
233
+ return `\`\`\`${lang}\n${dedented.join('\n')}\n\`\`\``;
234
+ });
235
+ }
236
+
237
+ function appendJsonPath(basePath, key) {
238
+ if (SIMPLE_JSON_KEY_RE.test(key)) {
239
+ return basePath === '$' ? `$.${key}` : `${basePath}.${key}`;
240
+ }
241
+ const escapedKey = String(key).replaceAll('\\', '\\\\').replaceAll('"', '\\"');
242
+ return `${basePath}["${escapedKey}"]`;
243
+ }
244
+
245
+ function formatJsonValue(value) {
246
+ if (typeof value === 'string') {
247
+ const normalized = value.replace(/\s+/g, ' ').trim();
248
+ if (normalized.length > JSON_MAX_STRING_LENGTH) {
249
+ const truncatedChars = normalized.length - JSON_MAX_STRING_LENGTH;
250
+ const shortened = normalized.slice(0, JSON_MAX_STRING_LENGTH);
251
+ return `${JSON.stringify(shortened)} [truncated ${truncatedChars} chars]`;
252
+ }
253
+ return JSON.stringify(normalized);
254
+ }
255
+ if (value === null) return 'null';
256
+ if (typeof value === 'number' || typeof value === 'boolean') return String(value);
257
+ return JSON.stringify(value);
258
+ }
259
+
260
+ function flattenJsonForEmbedding(root) {
261
+ const lines = [];
262
+ const notes = [];
263
+ const stack = [{ path: '$', value: root, depth: 0 }];
264
+ let hitEntryLimit = false;
265
+
266
+ while (stack.length > 0) {
267
+ if (lines.length >= JSON_MAX_ENTRIES) {
268
+ hitEntryLimit = true;
269
+ break;
270
+ }
271
+
272
+ const current = stack.pop();
273
+ const { path: currentPath, value, depth } = current;
274
+
275
+ if (value === null || typeof value !== 'object') {
276
+ lines.push(`${currentPath}: ${formatJsonValue(value)}`);
277
+ continue;
278
+ }
279
+
280
+ if (depth >= JSON_MAX_DEPTH) {
281
+ lines.push(`${currentPath}: [max depth ${JSON_MAX_DEPTH} reached]`);
282
+ continue;
283
+ }
284
+
285
+ if (Array.isArray(value)) {
286
+ lines.push(`${currentPath}: [array length=${value.length}]`);
287
+ const keptItems = Math.min(value.length, JSON_MAX_ARRAY_ITEMS);
288
+
289
+ for (let i = keptItems - 1; i >= 0; i -= 1) {
290
+ stack.push({
291
+ path: `${currentPath}[${i}]`,
292
+ value: value[i],
293
+ depth: depth + 1,
294
+ });
295
+ }
296
+
297
+ if (value.length > keptItems) {
298
+ lines.push(`${currentPath}: [omitted after item ${keptItems - 1}] (${value.length - keptItems} items omitted)`);
299
+ }
300
+ continue;
301
+ }
302
+
303
+ const entries = Object.entries(value);
304
+ if (entries.length === 0) {
305
+ lines.push(`${currentPath}: {}`);
306
+ continue;
307
+ }
308
+
309
+ for (let i = entries.length - 1; i >= 0; i -= 1) {
310
+ const [key, childValue] = entries[i];
311
+ stack.push({
312
+ path: appendJsonPath(currentPath, key),
313
+ value: childValue,
314
+ depth: depth + 1,
315
+ });
316
+ }
317
+ }
318
+
319
+ if (hitEntryLimit) {
320
+ notes.push(`[truncated] Flattened field limit reached (${JSON_MAX_ENTRIES}); remaining fields omitted.`);
321
+ }
322
+
323
+ return { lines, notes };
324
+ }
325
+
326
+ function formatJsonForEmbedding(jsonValue, sourcePath = '') {
327
+ const rootType = Array.isArray(jsonValue)
328
+ ? 'array'
329
+ : (jsonValue === null ? 'null' : typeof jsonValue);
330
+ const topLevelKeys = rootType === 'object' && jsonValue
331
+ ? Object.keys(jsonValue)
332
+ : [];
333
+ const { lines, notes } = flattenJsonForEmbedding(jsonValue);
334
+
335
+ const output = [
336
+ '# JSON Import',
337
+ '',
338
+ `Source: ${sourcePath || 'unknown'}`,
339
+ `Root type: ${rootType}`,
340
+ ];
341
+
342
+ if (rootType === 'array') {
343
+ output.push(`Top-level items: ${jsonValue.length}`);
344
+ } else if (rootType === 'object') {
345
+ const visibleKeys = topLevelKeys.slice(0, JSON_MAX_TOP_LEVEL_KEYS);
346
+ const suffix = topLevelKeys.length > visibleKeys.length
347
+ ? ` (+${topLevelKeys.length - visibleKeys.length} more)`
348
+ : '';
349
+ output.push(`Top-level keys (${topLevelKeys.length}): ${visibleKeys.join(', ')}${suffix}`);
350
+ }
351
+
352
+ output.push('', '## Flattened Fields', '');
353
+ output.push(...(lines.length > 0 ? lines : ['$: [empty document]']));
354
+
355
+ if (notes.length > 0) {
356
+ output.push('', '## Import Notes', '');
357
+ output.push(...notes.map((note) => `- ${note}`));
358
+ }
359
+
360
+ return output.join('\n').trim();
361
+ }
362
+
363
+ async function parseFile(absolutePath, extension, relativePath) {
364
+ const defaultTitle = titleFromPath(absolutePath, relativePath);
168
365
  try {
169
366
  if (extension === 'md' || extension === 'txt') {
170
367
  const body = await fs.readFile(absolutePath, 'utf8');
@@ -176,23 +373,114 @@ async function parseFile(absolutePath, extension) {
176
373
  const htmlTitle = titleMatch ? titleMatch[1].replace(/\s+/g, ' ').trim() : null;
177
374
  return { title: htmlTitle || defaultTitle, body: stripHtml(raw) };
178
375
  }
376
+ if (extension === 'json') {
377
+ const raw = await fs.readFile(absolutePath, 'utf8');
378
+ let parsedJson;
379
+ try {
380
+ parsedJson = JSON.parse(raw.replace(/^\uFEFF/, ''));
381
+ } catch (err) {
382
+ return { title: defaultTitle, body: '', error: `Invalid JSON: ${err.message}` };
383
+ }
384
+ return {
385
+ title: defaultTitle,
386
+ body: formatJsonForEmbedding(parsedJson, relativePath || path.basename(absolutePath)),
387
+ };
388
+ }
179
389
  if (extension === 'pdf') {
180
- // Dynamic import so missing pdfjs-dist doesn't break non-PDF imports
390
+ // Use pdfjs-dist legacy build for Node.js PDF text extraction
181
391
  try {
182
- const { default: pdfjsLib } = await import('pdfjs-dist/legacy/build/pdf.js');
183
- pdfjsLib.GlobalWorkerOptions.workerSrc = '';
392
+ const pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
184
393
  const data = await fs.readFile(absolutePath);
185
- const pdf = await pdfjsLib.getDocument({ data: new Uint8Array(data), disableWorker: true }).promise;
394
+ const pdf = await pdfjsLib.getDocument({ data: new Uint8Array(data.buffer) }).promise;
186
395
  const pages = [];
187
396
  for (let i = 1; i <= pdf.numPages; i++) {
188
397
  const page = await pdf.getPage(i);
189
398
  const content = await page.getTextContent();
190
- pages.push(content.items.map((item) => item.str).join(' '));
399
+
400
+ // Extract text with better structure preservation
401
+ const items = content.items;
402
+ let pageText = '';
403
+ let lastY = null;
404
+
405
+ for (const item of items) {
406
+ if (lastY !== null && Math.abs(item.transform[5] - lastY) > 5) {
407
+ // Different line
408
+ pageText += '\n';
409
+ } else if (pageText !== '') {
410
+ // Same line - add space between items
411
+ pageText += ' ';
412
+ }
413
+ pageText += item.str;
414
+ lastY = item.transform[5];
415
+ }
416
+
417
+ pages.push(pageText.trim());
191
418
  page.cleanup();
192
419
  }
420
+
193
421
  let title = defaultTitle;
194
422
  try { const meta = await pdf.getMetadata(); if (meta?.info?.Title?.trim()) title = meta.info.Title.trim(); } catch {}
195
- return { title, body: pages.join('\n\n').replace(/\s+\n/g, '\n').trim() };
423
+
424
+ // Join pages with double newline and clean up
425
+ const body = pages.join('\n\n')
426
+ .replace(/\n{3,}/g, '\n\n')
427
+ .replace(/[ \t]+/g, ' ')
428
+ .trim();
429
+
430
+ return { title, body };
431
+ } catch (err) {
432
+ return { title: defaultTitle, body: '', error: err.message };
433
+ }
434
+ }
435
+ if (extension === 'docx') {
436
+ // Use JSZip for DOCX text extraction
437
+ try {
438
+ const JSZip = (await import('jszip')).default;
439
+ const data = await fs.readFile(absolutePath);
440
+ const zip = await JSZip.loadAsync(data);
441
+ const docXml = await zip.file('word/document.xml')?.async('string');
442
+ if (!docXml) {
443
+ return { title: defaultTitle, body: '', error: 'Invalid DOCX file: missing document.xml' };
444
+ }
445
+
446
+ // Extract title from docProps/core.xml if available
447
+ let title = defaultTitle;
448
+ try {
449
+ const coreXml = await zip.file('docProps/core.xml')?.async('string');
450
+ if (coreXml) {
451
+ const titleMatch = coreXml.match(/<dc:title[^>]*>([^<]*)<\/dc:title>/);
452
+ if (titleMatch && titleMatch[1]?.trim()) {
453
+ title = titleMatch[1].trim();
454
+ }
455
+ }
456
+ } catch {}
457
+
458
+ // Parse XML to extract plain text with proper line breaks
459
+ let text = docXml;
460
+
461
+ // Preserve CDATA text content before stripping tags.
462
+ text = text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1');
463
+
464
+ // Handle paragraph spacing - use double newline for paragraphs
465
+ text = text
466
+ .replace(/<\/w:p>/g, '\n\n')
467
+ .replace(/<w:tab[^>]*\/>/g, '\t')
468
+ .replace(/<w:br[^>]*\/>/g, '\n')
469
+ .replace(/<w:cr[^>]*\/>/g, '\n');
470
+
471
+ // Remove all remaining XML tags
472
+ text = text
473
+ .replace(/<[^>]+>/g, '')
474
+ .replace(/\r\n/g, '\n')
475
+ .replace(/\r/g, '\n');
476
+
477
+ text = decodeXmlEntities(text)
478
+ .replace(/\n{3,}/g, '\n\n')
479
+ .trim();
480
+
481
+ text = normalizeFencedCodeBlocks(text);
482
+
483
+ return { title, body: text };
196
484
  } catch (err) {
197
485
  return { title: defaultTitle, body: '', error: err.message };
198
486
  }
@@ -214,8 +502,15 @@ function splitContent(title, body) {
214
502
  if (remaining.length <= NOTE_CHAR_LIMIT) { chunks.push(remaining); break; }
215
503
  const searchStart = Math.max(0, NOTE_CHAR_LIMIT - 500);
216
504
  const slice = remaining.slice(searchStart, NOTE_CHAR_LIMIT);
217
- const lastBreak = slice.lastIndexOf('\n\n');
218
- const splitAt = lastBreak !== -1 ? searchStart + lastBreak + 2 : NOTE_CHAR_LIMIT;
505
+ const lastParagraphBreak = slice.lastIndexOf('\n\n');
506
+ if (lastParagraphBreak !== -1) {
507
+ chunks.push(remaining.slice(0, searchStart + lastParagraphBreak + 2));
508
+ remaining = remaining.slice(searchStart + lastParagraphBreak + 2);
509
+ continue;
510
+ }
511
+
512
+ const lastLineBreak = slice.lastIndexOf('\n');
513
+ const splitAt = lastLineBreak !== -1 ? searchStart + lastLineBreak + 1 : NOTE_CHAR_LIMIT;
219
514
  chunks.push(remaining.slice(0, splitAt));
220
515
  remaining = remaining.slice(splitAt);
221
516
  }
@@ -252,7 +547,7 @@ function printResult(result, opts) {
252
547
  if (item.status === 'created') {
253
548
  reason = `noteId: ${item.noteId}`;
254
549
  } else if (item.status === 'skipped') {
255
- reason = `noteId: ${item.noteId}`;
550
+ reason = `Note with same title already exists in target folder (noteId: ${item.noteId})`;
256
551
  } else if (item.status === 'overwritten') {
257
552
  reason = `noteId: ${item.noteId}`;
258
553
  } else if (item.status === 'failed') {
@@ -311,7 +606,7 @@ export async function runImport(argv, deps = {}) {
311
606
  for (const file of files) {
312
607
  if (opts.verbose) process.stderr.write(` [parsing] ${file.relativePath}\n`);
313
608
 
314
- const parsed = await parseFile(file.absolutePath, file.extension);
609
+ const parsed = await parseFile(file.absolutePath, file.extension, file.relativePath);
315
610
 
316
611
  if (parsed.error && !parsed.body) {
317
612
  if (opts.verbose) process.stderr.write(` [failed] ${file.relativePath} — ${parsed.error}\n`);
@@ -337,9 +632,9 @@ export async function runImport(argv, deps = {}) {
337
632
  source_path: item.source_path,
338
633
  _parseError: item._parseError || null,
339
634
  })),
340
- folder: opts.folder,
341
- tags: opts.tags,
342
- conflict: opts.conflict,
635
+ folder: opts.folder || null,
636
+ tags: opts.tags || [],
637
+ conflict: opts.conflict || 'skip',
343
638
  // Always check first - we'll prompt if needed
344
639
  create_folder_if_missing: false,
345
640
  dry_run: opts.dryRun,
@@ -350,7 +645,14 @@ export async function runImport(argv, deps = {}) {
350
645
 
351
646
  let response;
352
647
  try {
648
+ if (opts.verbose) {
649
+ process.stderr.write(` [debug] calling bridge with ${items.length} items, folder: "${opts.folder}"\n`);
650
+ }
353
651
  response = await bridge({ op: 'bulk_import', payload }, { timeoutMs });
652
+ process.stderr.write(` [debug] bridge response ok: ${response?.ok}, hasData: ${!!response?.data}\n`);
653
+ if (opts.verbose || !response?.ok) {
654
+ process.stderr.write(` [debug] full response: ${JSON.stringify(response)}\n`);
655
+ }
354
656
  } catch (err) {
355
657
  const code = err.code || 'BRIDGE_ERROR';
356
658
  if (code === 'NOTESIA_APP_NOT_RUNNING') {
@@ -361,25 +663,31 @@ export async function runImport(argv, deps = {}) {
361
663
  process.exit(1);
362
664
  }
363
665
 
364
- if (!response || response.ok === false) {
365
- const code = response?.error?.code || 'BRIDGE_ERROR';
366
- const message = response?.error?.message || 'Bridge request failed';
666
+ // Check for errors - could be at top level or nested in data
667
+ const hasError = !response || response.ok === false || (response.data && response.data.ok === false);
668
+ if (hasError) {
669
+ const code = response?.error?.code || response?.data?.error?.code || 'BRIDGE_ERROR';
670
+ const message = response?.error?.message || response?.data?.error?.message || 'Bridge request failed';
367
671
 
368
672
  // Handle folder not found - prompt user to create (only if --create-folder is set)
673
+ const errorData = response?.error || response?.data?.error;
369
674
  if (code === 'FOLDER_NOT_FOUND' && opts.folder && opts.createFolder) {
370
- const folderName = response?.error?.folderName || opts.folder;
675
+ const folderName = errorData?.folderName || opts.folder;
371
676
  process.stdout.write(`\nFolder "${folderName}" does not exist in Notesia.\n`);
372
677
  const shouldCreate = await promptYesNo('Do you want to create it and continue import? [y/N] ');
373
678
  if (shouldCreate) {
374
679
  // Retry with create_folder_if_missing = true
375
680
  const retryPayload = { ...payload, create_folder_if_missing: true };
376
681
  response = await bridge({ op: 'bulk_import', payload: retryPayload }, { timeoutMs });
377
- if (response?.ok === false) {
378
- const retryCode = response?.error?.code || 'BRIDGE_ERROR';
379
- const retryMessage = response?.error?.message || 'Bridge request failed';
682
+ // Check both top-level ok and nested data.ok
683
+ const retryOk = response?.ok === true && (!response.data || response.data.ok !== false);
684
+ if (!retryOk) {
685
+ const retryCode = response?.error?.code || response?.data?.error?.code || 'BRIDGE_ERROR';
686
+ const retryMessage = response?.error?.message || response?.data?.error?.message || 'Bridge request failed';
380
687
  process.stderr.write(`[${retryCode}] ${retryMessage}\n`);
381
688
  process.exit(1);
382
689
  }
690
+ // Success - continue with response.data as the result
383
691
  } else {
384
692
  process.stderr.write('Import cancelled.\n');
385
693
  process.exit(1);
@@ -394,7 +702,20 @@ export async function runImport(argv, deps = {}) {
394
702
  }
395
703
  }
396
704
 
397
- const result = { ...response.data, elapsedMs: response.data?.elapsedMs ?? (Date.now() - startMs) };
705
+ // Check if we have valid data in the response
706
+ if (!response.data) {
707
+ process.stderr.write(`[BRIDGE_ERROR] No data in bridge response. Raw response: ${JSON.stringify(response)}\n`);
708
+ process.exit(1);
709
+ }
710
+
711
+ const result = {
712
+ created: response.data?.created ?? 0,
713
+ skipped: response.data?.skipped ?? 0,
714
+ overwritten: response.data?.overwritten ?? 0,
715
+ failed: response.data?.failed ?? 0,
716
+ elapsedMs: response.data?.elapsedMs ?? (Date.now() - startMs),
717
+ items: response.data?.items ?? [],
718
+ };
398
719
  printResult(result, { format: opts.format, quiet: opts.quiet });
399
720
  process.exit(result.failed === 0 ? 0 : 1);
400
721
  }