@refhub/cli 1.8.1 → 1.9.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.
@@ -1,4 +1,4 @@
1
- import { resolveClient, run } from '../client.js';
1
+ import { UsageError, action } from '../client.js';
2
2
  function normalizeDoi(doi) {
3
3
  return doi.trim().toLowerCase().replace(/^https?:\/\/(dx\.)?doi\.org\//, '');
4
4
  }
@@ -85,53 +85,49 @@ export function registerRelationsScan(relations) {
85
85
  .option('--item <itemId>', 'scan a single item instead of the whole vault')
86
86
  .option('--dry-run', 'show what would be created without writing')
87
87
  .option('--limit <n>', 'references/citations fetched per item from Semantic Scholar, 1-25', (v) => parseInt(v, 10))
88
- .action(async (opts, cmd) => {
89
- const g = cmd.optsWithGlobals();
90
- const client = resolveClient(g.apiKey);
88
+ .action(action(async ({ opts, g, getClient }) => {
89
+ const client = getClient();
91
90
  const dryRun = opts.dryRun ?? false;
92
- await run(async () => {
93
- const vaultResult = await client.getVault(opts.vault);
94
- const allItems = vaultResult.data.items;
95
- const existingRelations = [...vaultResult.data.relations];
96
- const targets = opts.item ? allItems.filter((i) => i.id === opts.item) : allItems;
97
- if (opts.item && targets.length === 0) {
98
- process.stderr.write(JSON.stringify({ error: { code: 'item_not_found', message: `Item ${opts.item} not found in vault ${opts.vault}.` } }) + '\n');
99
- process.exit(2);
100
- }
101
- const withDoi = targets.filter((i) => i.doi);
102
- if (withDoi.length === 0) {
103
- process.stdout.write(JSON.stringify({ data: { dry_run: dryRun, scanned: 0, suggested: 0, created: 0, skipped_duplicate: 0, no_doi: targets.length, message: 'No items with a DOI to scan.' } }, null, 2) + '\n');
104
- return;
105
- }
106
- let suggested = 0;
107
- let created = 0;
108
- let skippedDuplicate = 0;
109
- let noMatches = 0;
110
- let lookupFailed = 0;
111
- for (const item of withDoi) {
112
- const result = await scanItemRelations(item, client, opts.vault, allItems, existingRelations, { dryRun, limit: opts.limit });
113
- suggested += result.suggested;
114
- created += result.created;
115
- skippedDuplicate += result.skippedDuplicate;
116
- if (result.outcome === 'no_matches')
117
- noMatches++;
118
- if (result.outcome === 'lookup_failed')
119
- lookupFailed++;
120
- // pace Semantic Scholar calls — each item makes a lookup + references + citations request
121
- await new Promise((r) => setTimeout(r, 1000));
122
- }
123
- process.stdout.write(JSON.stringify({
124
- data: {
125
- dry_run: dryRun,
126
- scanned: withDoi.length,
127
- no_doi: targets.length - withDoi.length,
128
- lookup_failed: lookupFailed,
129
- no_matches: noMatches,
130
- suggested,
131
- created,
132
- skipped_duplicate: skippedDuplicate,
133
- },
134
- }, null, 2) + '\n');
135
- });
136
- });
91
+ const vaultResult = await client.getVault(opts.vault);
92
+ const allItems = vaultResult.data.items;
93
+ const existingRelations = [...vaultResult.data.relations];
94
+ const targets = opts.item ? allItems.filter((i) => i.id === opts.item) : allItems;
95
+ if (opts.item && targets.length === 0) {
96
+ throw new UsageError('item_not_found', `Item ${opts.item} not found in vault ${opts.vault}.`);
97
+ }
98
+ const withDoi = targets.filter((i) => i.doi);
99
+ if (withDoi.length === 0) {
100
+ process.stdout.write(JSON.stringify({ data: { dry_run: dryRun, scanned: 0, suggested: 0, created: 0, skipped_duplicate: 0, no_doi: targets.length, message: 'No items with a DOI to scan.' } }, null, 2) + '\n');
101
+ return;
102
+ }
103
+ let suggested = 0;
104
+ let created = 0;
105
+ let skippedDuplicate = 0;
106
+ let noMatches = 0;
107
+ let lookupFailed = 0;
108
+ for (const item of withDoi) {
109
+ const result = await scanItemRelations(item, client, opts.vault, allItems, existingRelations, { dryRun, limit: opts.limit });
110
+ suggested += result.suggested;
111
+ created += result.created;
112
+ skippedDuplicate += result.skippedDuplicate;
113
+ if (result.outcome === 'no_matches')
114
+ noMatches++;
115
+ if (result.outcome === 'lookup_failed')
116
+ lookupFailed++;
117
+ // pace Semantic Scholar calls — each item makes a lookup + references + citations request
118
+ await new Promise((r) => setTimeout(r, 1000));
119
+ }
120
+ process.stdout.write(JSON.stringify({
121
+ data: {
122
+ dry_run: dryRun,
123
+ scanned: withDoi.length,
124
+ no_doi: targets.length - withDoi.length,
125
+ lookup_failed: lookupFailed,
126
+ no_matches: noMatches,
127
+ suggested,
128
+ created,
129
+ skipped_duplicate: skippedDuplicate,
130
+ },
131
+ }, null, 2) + '\n');
132
+ }));
137
133
  }
@@ -1,4 +1,4 @@
1
- import { resolveClient, run } from '../client.js';
1
+ import { action } from '../client.js';
2
2
  import { format } from '../format.js';
3
3
  export async function handleSectionsList(client, vaultId, tableMode) {
4
4
  const result = await client.listSections(vaultId);
@@ -34,11 +34,10 @@ export function registerSections(program) {
34
34
  .command('list')
35
35
  .description('List sections in a vault')
36
36
  .requiredOption('--vault <id>')
37
- .action(async (opts, cmd) => {
38
- const g = cmd.optsWithGlobals();
39
- const client = resolveClient(g.apiKey);
40
- await run(() => handleSectionsList(client, opts.vault, g.table ?? false));
41
- });
37
+ .action(action(async ({ opts, g, getClient }) => {
38
+ const client = getClient();
39
+ await handleSectionsList(client, opts.vault, g.table ?? false);
40
+ }));
42
41
  sections
43
42
  .command('create')
44
43
  .description('Create a section (owner-only)')
@@ -46,11 +45,10 @@ export function registerSections(program) {
46
45
  .requiredOption('--name <name>')
47
46
  .option('--description <text>')
48
47
  .option('--position <n>', 'sort position, defaults to 0', (v) => parseInt(v, 10))
49
- .action(async (opts, cmd) => {
50
- const g = cmd.optsWithGlobals();
51
- const client = resolveClient(g.apiKey);
52
- await run(() => handleSectionCreate(client, opts.vault, { name: opts.name, description: opts.description, position: opts.position }, g.table ?? false));
53
- });
48
+ .action(action(async ({ opts, g, getClient }) => {
49
+ const client = getClient();
50
+ await handleSectionCreate(client, opts.vault, { name: opts.name, description: opts.description, position: opts.position }, g.table ?? false);
51
+ }));
54
52
  sections
55
53
  .command('update')
56
54
  .argument('<sectionId>')
@@ -59,19 +57,17 @@ export function registerSections(program) {
59
57
  .option('--name <name>')
60
58
  .option('--description <text>')
61
59
  .option('--position <n>', '', (v) => parseInt(v, 10))
62
- .action(async (sectionId, opts, cmd) => {
63
- const g = cmd.optsWithGlobals();
64
- const client = resolveClient(g.apiKey);
65
- await run(() => handleSectionUpdate(client, opts.vault, sectionId, { name: opts.name, description: opts.description, position: opts.position }, g.table ?? false));
66
- });
60
+ .action(action(async ({ args: [sectionId], opts, g, getClient }) => {
61
+ const client = getClient();
62
+ await handleSectionUpdate(client, opts.vault, sectionId, { name: opts.name, description: opts.description, position: opts.position }, g.table ?? false);
63
+ }));
67
64
  sections
68
65
  .command('delete')
69
66
  .argument('<sectionId>')
70
67
  .description('Delete a section (owner-only) — items in it are unfiled, not deleted')
71
68
  .requiredOption('--vault <id>')
72
- .action(async (sectionId, opts, cmd) => {
73
- const g = cmd.optsWithGlobals();
74
- const client = resolveClient(g.apiKey);
75
- await run(() => handleSectionDelete(client, opts.vault, sectionId));
76
- });
69
+ .action(action(async ({ args: [sectionId], opts, g, getClient }) => {
70
+ const client = getClient();
71
+ await handleSectionDelete(client, opts.vault, sectionId);
72
+ }));
77
73
  }
@@ -1,4 +1,4 @@
1
- import { resolveClient, run } from '../client.js';
1
+ import { action } from '../client.js';
2
2
  import { format } from '../format.js';
3
3
  export async function handleTagsList(client, vaultId, tableMode) {
4
4
  const result = await client.listTags(vaultId);
@@ -44,11 +44,10 @@ export function registerTags(program) {
44
44
  .command('list')
45
45
  .description('List tags in a vault')
46
46
  .requiredOption('--vault <id>')
47
- .action(async (opts, cmd) => {
48
- const g = cmd.optsWithGlobals();
49
- const client = resolveClient(g.apiKey);
50
- await run(() => handleTagsList(client, opts.vault, g.table ?? false));
51
- });
47
+ .action(action(async ({ opts, g, getClient }) => {
48
+ const client = getClient();
49
+ await handleTagsList(client, opts.vault, g.table ?? false);
50
+ }));
52
51
  tags
53
52
  .command('create')
54
53
  .description('Create a tag')
@@ -56,11 +55,10 @@ export function registerTags(program) {
56
55
  .requiredOption('--name <name>')
57
56
  .option('--color <color>')
58
57
  .option('--parent <tagId>', 'parent tag ID')
59
- .action(async (opts, cmd) => {
60
- const g = cmd.optsWithGlobals();
61
- const client = resolveClient(g.apiKey);
62
- await run(() => handleTagCreate(client, opts.vault, { name: opts.name, color: opts.color, parent: opts.parent }, g.table ?? false));
63
- });
58
+ .action(action(async ({ opts, g, getClient }) => {
59
+ const client = getClient();
60
+ await handleTagCreate(client, opts.vault, { name: opts.name, color: opts.color, parent: opts.parent }, g.table ?? false);
61
+ }));
64
62
  tags
65
63
  .command('update')
66
64
  .argument('<tagId>')
@@ -69,41 +67,37 @@ export function registerTags(program) {
69
67
  .option('--name <name>')
70
68
  .option('--color <color>')
71
69
  .option('--parent <tagId>')
72
- .action(async (tagId, opts, cmd) => {
73
- const g = cmd.optsWithGlobals();
74
- const client = resolveClient(g.apiKey);
75
- await run(() => handleTagUpdate(client, opts.vault, tagId, { name: opts.name, color: opts.color, parent: opts.parent }, g.table ?? false));
76
- });
70
+ .action(action(async ({ args: [tagId], opts, g, getClient }) => {
71
+ const client = getClient();
72
+ await handleTagUpdate(client, opts.vault, tagId, { name: opts.name, color: opts.color, parent: opts.parent }, g.table ?? false);
73
+ }));
77
74
  tags
78
75
  .command('delete')
79
76
  .argument('<tagId>')
80
77
  .description('Delete a tag')
81
78
  .requiredOption('--vault <id>')
82
- .action(async (tagId, opts, cmd) => {
83
- const g = cmd.optsWithGlobals();
84
- const client = resolveClient(g.apiKey);
85
- await run(() => handleTagDelete(client, opts.vault, tagId));
86
- });
79
+ .action(action(async ({ args: [tagId], opts, g, getClient }) => {
80
+ const client = getClient();
81
+ await handleTagDelete(client, opts.vault, tagId);
82
+ }));
87
83
  tags
88
84
  .command('attach')
89
85
  .description('Attach tags to an item (idempotent)')
90
86
  .requiredOption('--vault <id>')
91
87
  .requiredOption('--item <itemId>')
92
88
  .requiredOption('--tags <ids>', 'comma-separated tag IDs')
93
- .action(async (opts, cmd) => {
94
- const g = cmd.optsWithGlobals();
95
- const client = resolveClient(g.apiKey);
96
- await run(() => handleTagAttach(client, opts.vault, opts.item, opts.tags));
97
- });
89
+ .action(action(async ({ opts, g, getClient }) => {
90
+ const client = getClient();
91
+ await handleTagAttach(client, opts.vault, opts.item, opts.tags);
92
+ }));
98
93
  tags
99
94
  .command('detach')
100
95
  .description('Detach tags from an item')
101
96
  .requiredOption('--vault <id>')
102
97
  .requiredOption('--item <itemId>')
103
98
  .requiredOption('--tags <ids>', 'comma-separated tag IDs')
104
- .action(async (opts, cmd) => {
105
- const g = cmd.optsWithGlobals();
106
- const client = resolveClient(g.apiKey);
107
- await run(() => handleTagDetach(client, opts.vault, opts.item, opts.tags));
108
- });
99
+ .action(action(async ({ opts, g, getClient }) => {
100
+ const client = getClient();
101
+ await handleTagDetach(client, opts.vault, opts.item, opts.tags);
102
+ }));
109
103
  }
@@ -1,4 +1,4 @@
1
- import { resolveClient, run } from '../client.js';
1
+ import { UsageError, action } from '../client.js';
2
2
  import { format } from '../format.js';
3
3
  export async function handleVaultsList(client, tableMode) {
4
4
  const result = await client.listVaults();
@@ -40,16 +40,14 @@ export async function handleVaultUpdate(client, vaultId, opts, tableMode) {
40
40
  }
41
41
  export async function handleVaultDelete(client, vaultId, confirmed) {
42
42
  if (!confirmed) {
43
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge this is a hard delete with no undo.' } }) + '\n');
44
- process.exit(2);
43
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge this is a hard delete with no undo.');
45
44
  }
46
45
  const result = await client.deleteVault(vaultId);
47
46
  format(result, false);
48
47
  }
49
48
  export async function handleVaultArchive(client, vaultId, confirmed, tableMode) {
50
49
  if (!confirmed) {
51
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge this is permanent: archiving makes the vault read-only forever (items/tags/relations/shares) and cannot be undone (no unarchive).' } }) + '\n');
52
- process.exit(2);
50
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge this is permanent: archiving makes the vault read-only forever (items/tags/relations/shares) and cannot be undone (no unarchive).');
53
51
  }
54
52
  const result = await client.archiveVault(vaultId);
55
53
  format(result, tableMode);
@@ -82,20 +80,18 @@ export function registerVaults(program) {
82
80
  vaults
83
81
  .command('list')
84
82
  .description('List all accessible vaults')
85
- .action(async (opts, cmd) => {
86
- const g = cmd.optsWithGlobals();
87
- const client = resolveClient(g.apiKey);
88
- await run(() => handleVaultsList(client, g.table ?? false));
89
- });
83
+ .action(action(async ({ opts, g, getClient }) => {
84
+ const client = getClient();
85
+ await handleVaultsList(client, g.table ?? false);
86
+ }));
90
87
  vaults
91
88
  .command('get')
92
89
  .argument('<vaultId>')
93
90
  .description('Read one vault with full contents')
94
- .action(async (vaultId, opts, cmd) => {
95
- const g = cmd.optsWithGlobals();
96
- const client = resolveClient(g.apiKey);
97
- await run(() => handleVaultGet(client, vaultId, g.table ?? false));
98
- });
91
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
92
+ const client = getClient();
93
+ await handleVaultGet(client, vaultId, g.table ?? false);
94
+ }));
99
95
  vaults
100
96
  .command('create')
101
97
  .description('Create a vault')
@@ -105,11 +101,10 @@ export function registerVaults(program) {
105
101
  .option('--visibility <visibility>', 'private|protected|public')
106
102
  .option('--category <category>')
107
103
  .option('--abstract <abstract>')
108
- .action(async (opts, cmd) => {
109
- const g = cmd.optsWithGlobals();
110
- const client = resolveClient(g.apiKey);
111
- await run(() => handleVaultCreate(client, opts, g.table ?? false));
112
- });
104
+ .action(action(async ({ opts, g, getClient }) => {
105
+ const client = getClient();
106
+ await handleVaultCreate(client, opts, g.table ?? false);
107
+ }));
113
108
  vaults
114
109
  .command('update')
115
110
  .argument('<vaultId>')
@@ -119,52 +114,47 @@ export function registerVaults(program) {
119
114
  .option('--color <color>')
120
115
  .option('--category <category>')
121
116
  .option('--abstract <abstract>')
122
- .action(async (vaultId, opts, cmd) => {
123
- const g = cmd.optsWithGlobals();
124
- const client = resolveClient(g.apiKey);
125
- await run(() => handleVaultUpdate(client, vaultId, opts, g.table ?? false));
126
- });
117
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
118
+ const client = getClient();
119
+ await handleVaultUpdate(client, vaultId, opts, g.table ?? false);
120
+ }));
127
121
  vaults
128
122
  .command('delete')
129
123
  .argument('<vaultId>')
130
124
  .description('Delete a vault (hard delete, no undo)')
131
125
  .option('--confirm', 'required: acknowledge this is a permanent deletion')
132
- .action(async (vaultId, opts, cmd) => {
133
- const g = cmd.optsWithGlobals();
134
- const client = resolveClient(g.apiKey);
135
- await run(() => handleVaultDelete(client, vaultId, opts.confirm ?? false));
136
- });
126
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
127
+ const client = getClient();
128
+ await handleVaultDelete(client, vaultId, opts.confirm ?? false);
129
+ }));
137
130
  vaults
138
131
  .command('archive')
139
132
  .argument('<vaultId>')
140
133
  .description('Permanently archive a vault (read-only forever, no unarchive)')
141
134
  .option('--confirm', 'required: acknowledge this cannot be undone')
142
- .action(async (vaultId, opts, cmd) => {
143
- const g = cmd.optsWithGlobals();
144
- const client = resolveClient(g.apiKey);
145
- await run(() => handleVaultArchive(client, vaultId, opts.confirm ?? false, g.table ?? false));
146
- });
135
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
136
+ const client = getClient();
137
+ await handleVaultArchive(client, vaultId, opts.confirm ?? false, g.table ?? false);
138
+ }));
147
139
  vaults
148
140
  .command('visibility')
149
141
  .argument('<vaultId>')
150
142
  .description('Set vault visibility')
151
143
  .requiredOption('--visibility <visibility>', 'private|protected|public')
152
144
  .option('--slug <slug>', 'public_slug for public vaults')
153
- .action(async (vaultId, opts, cmd) => {
154
- const g = cmd.optsWithGlobals();
155
- const client = resolveClient(g.apiKey);
156
- await run(() => handleVaultVisibility(client, vaultId, { visibility: opts.visibility, slug: opts.slug }, g.table ?? false));
157
- });
145
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
146
+ const client = getClient();
147
+ await handleVaultVisibility(client, vaultId, { visibility: opts.visibility, slug: opts.slug }, g.table ?? false);
148
+ }));
158
149
  const shares = vaults.command('shares').description('Manage vault collaborators');
159
150
  shares
160
151
  .command('list')
161
152
  .argument('<vaultId>')
162
153
  .description('List vault collaborators')
163
- .action(async (vaultId, opts, cmd) => {
164
- const g = cmd.optsWithGlobals();
165
- const client = resolveClient(g.apiKey);
166
- await run(() => handleSharesList(client, vaultId, g.table ?? false));
167
- });
154
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
155
+ const client = getClient();
156
+ await handleSharesList(client, vaultId, g.table ?? false);
157
+ }));
168
158
  shares
169
159
  .command('add')
170
160
  .argument('<vaultId>')
@@ -172,30 +162,27 @@ export function registerVaults(program) {
172
162
  .requiredOption('--email <email>')
173
163
  .requiredOption('--role <role>', 'viewer|editor')
174
164
  .option('--name <name>')
175
- .action(async (vaultId, opts, cmd) => {
176
- const g = cmd.optsWithGlobals();
177
- const client = resolveClient(g.apiKey);
178
- await run(() => handleShareAdd(client, vaultId, { email: opts.email, role: opts.role, name: opts.name }, g.table ?? false));
179
- });
165
+ .action(action(async ({ args: [vaultId], opts, g, getClient }) => {
166
+ const client = getClient();
167
+ await handleShareAdd(client, vaultId, { email: opts.email, role: opts.role, name: opts.name }, g.table ?? false);
168
+ }));
180
169
  shares
181
170
  .command('update')
182
171
  .argument('<vaultId>')
183
172
  .argument('<shareId>')
184
173
  .description('Update a collaborator role')
185
174
  .requiredOption('--role <role>', 'viewer|editor')
186
- .action(async (vaultId, shareId, opts, cmd) => {
187
- const g = cmd.optsWithGlobals();
188
- const client = resolveClient(g.apiKey);
189
- await run(() => handleShareUpdate(client, vaultId, shareId, opts.role, g.table ?? false));
190
- });
175
+ .action(action(async ({ args: [vaultId, shareId], opts, g, getClient }) => {
176
+ const client = getClient();
177
+ await handleShareUpdate(client, vaultId, shareId, opts.role, g.table ?? false);
178
+ }));
191
179
  shares
192
180
  .command('remove')
193
181
  .argument('<vaultId>')
194
182
  .argument('<shareId>')
195
183
  .description('Remove a collaborator')
196
- .action(async (vaultId, shareId, opts, cmd) => {
197
- const g = cmd.optsWithGlobals();
198
- const client = resolveClient(g.apiKey);
199
- await run(() => handleShareRemove(client, vaultId, shareId));
200
- });
184
+ .action(action(async ({ args: [vaultId, shareId], opts, g, getClient }) => {
185
+ const client = getClient();
186
+ await handleShareRemove(client, vaultId, shareId);
187
+ }));
201
188
  }
package/dist/format.js CHANGED
@@ -7,6 +7,15 @@ export function format(response, tableMode, columns) {
7
7
  return;
8
8
  }
9
9
  const data = response?.data;
10
+ // One record: a key/value table, one row per field.
11
+ if (data !== null && typeof data === 'object' && !Array.isArray(data)) {
12
+ const table = new Table();
13
+ for (const [key, value] of Object.entries(data)) {
14
+ table.push({ [chalk.cyan(key)]: cell(value) });
15
+ }
16
+ process.stdout.write(table.toString() + '\n');
17
+ return;
18
+ }
10
19
  if (!Array.isArray(data) || data.length === 0) {
11
20
  process.stdout.write(JSON.stringify(response, null, 2) + '\n');
12
21
  return;
@@ -20,3 +29,9 @@ export function format(response, tableMode, columns) {
20
29
  }
21
30
  process.stdout.write(table.toString() + '\n');
22
31
  }
32
+ /** A value as table text: empty for null, JSON for arrays and objects. */
33
+ function cell(value) {
34
+ if (value === null || value === undefined)
35
+ return '';
36
+ return typeof value === 'object' ? JSON.stringify(value) : String(value);
37
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@refhub/cli",
3
- "version": "1.8.1",
3
+ "version": "1.9.0",
4
4
  "description": "refhub CLI execution layer for agent skills",
5
5
  "type": "module",
6
6
  "repository": {