@refhub/cli 1.0.0 → 1.2.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 +68 -16
- package/dist/client.d.ts +35 -1
- package/dist/client.js +81 -6
- package/dist/commands/discover.d.ts +12 -0
- package/dist/commands/discover.js +96 -0
- package/dist/commands/enrich.d.ts +5 -0
- package/dist/commands/enrich.js +80 -0
- package/dist/commands/pdf.d.ts +4 -0
- package/dist/commands/pdf.js +30 -0
- package/dist/index.js +10 -1
- package/dist/types.d.ts +26 -0
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -3,37 +3,43 @@
|
|
|
3
3
|
> command-line execution layer for the [refhub.io](https://refhub.io) API.
|
|
4
4
|
> manage vaults, papers, tags, and relations — scriptable, pipeable, agent-ready.
|
|
5
5
|
|
|
6
|
+
[](https://www.npmjs.com/package/@refhub/cli)
|
|
7
|
+
|
|
6
8
|
---
|
|
7
9
|
|
|
8
10
|
## // install
|
|
9
11
|
|
|
10
12
|
```bash
|
|
11
|
-
|
|
13
|
+
npm install -g @refhub/cli
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**requires** node ≥ 18.
|
|
17
|
+
|
|
18
|
+
### install from source
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
git clone https://github.com/refhub-io/refhub-cli
|
|
12
22
|
cd refhub-cli
|
|
13
23
|
npm install
|
|
14
24
|
npm run build
|
|
15
25
|
npm link
|
|
16
26
|
```
|
|
17
27
|
|
|
18
|
-
`npm link` makes `refhub` available globally without publishing to npm.
|
|
19
|
-
|
|
20
|
-
**requires** node ≥ 18.
|
|
21
|
-
|
|
22
28
|
---
|
|
23
29
|
|
|
24
30
|
## // auth
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
export REFHUB_API_KEY=your_key_here
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
or pass inline:
|
|
32
|
+
The CLI uses a RefHub API key for normal agent/runtime work:
|
|
31
33
|
|
|
34
|
+
**API key** — required for all vault/item/tag/relation/import/export/audit commands:
|
|
32
35
|
```bash
|
|
36
|
+
export REFHUB_API_KEY=your_key_here
|
|
37
|
+
# or pass inline:
|
|
33
38
|
refhub --api-key <key> vaults list
|
|
34
39
|
```
|
|
40
|
+
Resolution order: `--api-key` flag → `REFHUB_API_KEY` env → exit 3.
|
|
35
41
|
|
|
36
|
-
|
|
42
|
+
Session JWTs are no longer required for CLI enrichment or item PDF upload. Browser/account setup flows still use RefHub web auth: API key creation/revocation and Google Drive connect/disconnect.
|
|
37
43
|
|
|
38
44
|
---
|
|
39
45
|
|
|
@@ -137,6 +143,49 @@ refhub export --vault <id> [--format json|bibtex]
|
|
|
137
143
|
refhub audit --vault <id> [--since <ISO>] [--until <ISO>] [--limit] [--page]
|
|
138
144
|
```
|
|
139
145
|
|
|
146
|
+
|
|
147
|
+
### discover
|
|
148
|
+
|
|
149
|
+
Semantic Scholar discovery/enrichment through API-key routes (`vaults:read`).
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
refhub discover search --query "visual analytics" [--limit 10]
|
|
153
|
+
refhub discover lookup (--doi <doi> | --title <title>)
|
|
154
|
+
refhub discover recommendations --paper <paperId-or-DOI:id> [--limit]
|
|
155
|
+
refhub discover related --paper <paperId-or-DOI:id> [--limit]
|
|
156
|
+
refhub discover references --paper <paperId-or-DOI:id> [--limit]
|
|
157
|
+
refhub discover citations --paper <paperId-or-DOI:id> [--limit]
|
|
158
|
+
refhub discover cited-by --paper <paperId-or-DOI:id> [--limit]
|
|
159
|
+
refhub discover add --vault <id> --file <semantic-scholar-results.json> [--idempotency-key]
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`discover add` expects a JSON array of normalized Semantic Scholar paper objects, such as the `.data` returned by `discover search/recommendations/references/citations`, and upserts them into the target vault. Open-access PDF URLs are mapped to `pdf_url` where present.
|
|
163
|
+
|
|
164
|
+
### enrich
|
|
165
|
+
|
|
166
|
+
Enriches incomplete publication metadata by looking up each item's DOI against Semantic Scholar and patching missing fields (title, authors, year, abstract). Requires only the RefHub API key (`vaults:read` for lookup plus `vaults:write` when patching items).
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
refhub enrich --vault <id> [--item <itemId>] [--dry-run]
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
- omit `--item` to process all items in the vault that have a DOI and missing fields
|
|
173
|
+
- `--dry-run` shows what would be updated without writing anything
|
|
174
|
+
- rate-limited client-side to 1 req/s; backend cache/rate-limit/stale fallback also applies
|
|
175
|
+
|
|
176
|
+
### pdf
|
|
177
|
+
|
|
178
|
+
Uploads a PDF file to the user's linked Google Drive and links it to a vault item. Requires only the RefHub API key (`vaults:write`) after Google Drive has been connected in the RefHub web account UI.
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
refhub pdf upload --vault <vaultId> --item <itemId> --file <path/to/file.pdf>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
- small PDFs use the raw API-key upload route: `POST /api/v1/vaults/:vaultId/items/:itemId/pdf`
|
|
185
|
+
- raw API uploads are capped by the backend at the smallest of `REFHUB_API_MAX_BODY_BYTES`, `GOOGLE_DRIVE_MAX_UPLOAD_BYTES`, and the Netlify synchronous Function ceiling (6 MiB); oversized raw requests return structured `413 pdf_upload_too_large_for_api`
|
|
186
|
+
- larger PDFs use the API-key resumable Drive flow: `POST /api/v1/vaults/:vaultId/items/:itemId/pdf/session`, direct `PUT` of the PDF bytes to the returned Google Drive `upload_url`, then `POST /api/v1/vaults/:vaultId/items/:itemId/pdf/complete`
|
|
187
|
+
- max file size: 26 MB by default, matching the backend Google Drive upload limit
|
|
188
|
+
|
|
140
189
|
---
|
|
141
190
|
|
|
142
191
|
## // exit codes
|
|
@@ -187,6 +236,10 @@ npm run build # tsc → dist/
|
|
|
187
236
|
|
|
188
237
|
smoke test covers: `list_vaults` → `create_vault` → `tag_crud` → `relation_crud` → `export` → `delete_vault`.
|
|
189
238
|
|
|
239
|
+
### releases
|
|
240
|
+
|
|
241
|
+
Releases are published from GitHub Actions via npm Trusted Publishing. See [RELEASE.md](./RELEASE.md) for the version bump, tag, and publish procedure.
|
|
242
|
+
|
|
190
243
|
---
|
|
191
244
|
|
|
192
245
|
## // stack
|
|
@@ -204,9 +257,8 @@ smoke test covers: `list_vaults` → `create_vault` → `tag_crud` → `relation
|
|
|
204
257
|
|
|
205
258
|
## // out of scope (v1)
|
|
206
259
|
|
|
207
|
-
not exposed by the cli
|
|
260
|
+
not exposed by the cli:
|
|
208
261
|
|
|
209
|
-
- api key management
|
|
210
|
-
- google drive
|
|
211
|
-
-
|
|
212
|
-
- global audit log (non-vault-scoped)
|
|
262
|
+
- api key management (jwt-only, no cli command)
|
|
263
|
+
- google drive link/unlink setup (browser/JWT-only, no cli command)
|
|
264
|
+
- global audit log (jwt-only, non-vault-scoped, no cli command)
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ApiResponse, Vault, VaultDetail, Share, Item, UpsertResult, PreviewResult, Tag, Relation, BibTeXImportResult, AuditEntry, VaultStats, RelationType } from './types.js';
|
|
1
|
+
import type { ApiResponse, Vault, VaultDetail, Share, Item, UpsertResult, PreviewResult, Tag, Relation, BibTeXImportResult, AuditEntry, VaultStats, RelationType, SemanticScholarPaper, SemanticScholarDoiMetadata } from './types.js';
|
|
2
2
|
export declare class RefHubError extends Error {
|
|
3
3
|
readonly code: string;
|
|
4
4
|
readonly request_id: string;
|
|
@@ -7,10 +7,12 @@ export declare class RefHubError extends Error {
|
|
|
7
7
|
constructor(status: number, code: string, message: string, request_id: string, retry_after_seconds?: number);
|
|
8
8
|
}
|
|
9
9
|
export declare class RefHubClient {
|
|
10
|
+
static readonly RAW_PDF_UPLOAD_LIMIT_BYTES: number;
|
|
10
11
|
private readonly baseUrl;
|
|
11
12
|
private readonly headers;
|
|
12
13
|
constructor(apiKey: string);
|
|
13
14
|
private req;
|
|
15
|
+
private reqBinary;
|
|
14
16
|
private reqText;
|
|
15
17
|
listVaults(): Promise<ApiResponse<Vault[]>>;
|
|
16
18
|
getVault(vaultId: string): Promise<ApiResponse<VaultDetail>>;
|
|
@@ -131,6 +133,38 @@ export declare class RefHubClient {
|
|
|
131
133
|
limit?: number;
|
|
132
134
|
page?: number;
|
|
133
135
|
}): Promise<ApiResponse<AuditEntry[]>>;
|
|
136
|
+
doiMetadata(doi: string): Promise<SemanticScholarDoiMetadata | null>;
|
|
137
|
+
semanticScholarLookup(body: {
|
|
138
|
+
doi?: string;
|
|
139
|
+
title?: string;
|
|
140
|
+
}): Promise<ApiResponse<{
|
|
141
|
+
paper_id: string;
|
|
142
|
+
}>>;
|
|
143
|
+
semanticScholarSearch(query: string, limit?: number): Promise<ApiResponse<SemanticScholarPaper[]>>;
|
|
144
|
+
semanticScholarPaperList(kind: 'recommendations' | 'related' | 'references' | 'citations' | 'cited-by', paperId: string, limit?: number): Promise<ApiResponse<SemanticScholarPaper[]>>;
|
|
145
|
+
uploadItemPdfRaw(vaultId: string, itemId: string, pdfBuffer: Buffer): Promise<ApiResponse<PdfUploadResult>>;
|
|
146
|
+
createItemPdfUploadSession(vaultId: string, itemId: string): Promise<ApiResponse<PdfUploadSession>>;
|
|
147
|
+
completeItemPdfUpload(vaultId: string, itemId: string, body: {
|
|
148
|
+
file_id: string;
|
|
149
|
+
web_view_link?: string | null;
|
|
150
|
+
source_url?: string | null;
|
|
151
|
+
}): Promise<ApiResponse<PdfUploadResult>>;
|
|
152
|
+
uploadItemPdfResumable(vaultId: string, itemId: string, pdfBuffer: Buffer): Promise<ApiResponse<PdfUploadResult>>;
|
|
153
|
+
uploadItemPdf(vaultId: string, itemId: string, pdfBuffer: Buffer): Promise<ApiResponse<PdfUploadResult>>;
|
|
154
|
+
}
|
|
155
|
+
export interface PdfUploadResult {
|
|
156
|
+
attempted: boolean;
|
|
157
|
+
stored: boolean;
|
|
158
|
+
provider: string;
|
|
159
|
+
fileId?: string;
|
|
160
|
+
folderId?: string;
|
|
161
|
+
folderName?: string;
|
|
162
|
+
pdfUrl?: string;
|
|
163
|
+
sourceUrl?: string | null;
|
|
164
|
+
}
|
|
165
|
+
export interface PdfUploadSession {
|
|
166
|
+
upload_url: string;
|
|
167
|
+
file_name?: string;
|
|
134
168
|
}
|
|
135
169
|
export declare function resolveClient(apiKey?: string): RefHubClient;
|
|
136
170
|
export declare function run(fn: () => Promise<void>): Promise<void>;
|
package/dist/client.js
CHANGED
|
@@ -13,6 +13,7 @@ export class RefHubError extends Error {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
export class RefHubClient {
|
|
16
|
+
static RAW_PDF_UPLOAD_LIMIT_BYTES = 6 * 1024 * 1024;
|
|
16
17
|
baseUrl = 'https://refhub-api.netlify.app/api/v1';
|
|
17
18
|
headers;
|
|
18
19
|
constructor(apiKey) {
|
|
@@ -30,7 +31,22 @@ export class RefHubClient {
|
|
|
30
31
|
if (!res.ok) {
|
|
31
32
|
const payload = await res.json().catch(() => ({}));
|
|
32
33
|
const err = (payload['error'] ?? {});
|
|
33
|
-
throw new RefHubError(res.status, String(err['code'] ?? 'unknown_error'), String(err['message'] ?? `HTTP ${res.status}`), String(err['request_id'] ?? ''), typeof
|
|
34
|
+
throw new RefHubError(res.status, String(err['code'] ?? 'unknown_error'), String(err['message'] ?? `HTTP ${res.status}`), String(payload['meta']?.['request_id'] ?? err['request_id'] ?? ''), typeof err['details']?.['retry_after_seconds'] === 'number'
|
|
35
|
+
? Number(err['details']['retry_after_seconds'])
|
|
36
|
+
: typeof payload['retry_after_seconds'] === 'number' ? payload['retry_after_seconds'] : undefined);
|
|
37
|
+
}
|
|
38
|
+
return res.json();
|
|
39
|
+
}
|
|
40
|
+
async reqBinary(method, path, body, contentType = 'application/octet-stream') {
|
|
41
|
+
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
42
|
+
method,
|
|
43
|
+
headers: { ...this.headers, 'Content-Type': contentType },
|
|
44
|
+
body: body,
|
|
45
|
+
});
|
|
46
|
+
if (!res.ok) {
|
|
47
|
+
const payload = await res.json().catch(() => ({}));
|
|
48
|
+
const err = (payload['error'] ?? {});
|
|
49
|
+
throw new RefHubError(res.status, String(err['code'] ?? 'unknown_error'), String(err['message'] ?? `HTTP ${res.status}`), String(payload['meta']?.['request_id'] ?? err['request_id'] ?? ''));
|
|
34
50
|
}
|
|
35
51
|
return res.json();
|
|
36
52
|
}
|
|
@@ -39,7 +55,7 @@ export class RefHubClient {
|
|
|
39
55
|
if (!res.ok) {
|
|
40
56
|
const payload = await res.json().catch(() => ({}));
|
|
41
57
|
const err = (payload['error'] ?? {});
|
|
42
|
-
throw new RefHubError(res.status, String(err['code'] ?? 'unknown_error'), String(err['message'] ?? `HTTP ${res.status}`), String(err['request_id'] ?? ''));
|
|
58
|
+
throw new RefHubError(res.status, String(err['code'] ?? 'unknown_error'), String(err['message'] ?? `HTTP ${res.status}`), String(payload['meta']?.['request_id'] ?? err['request_id'] ?? ''));
|
|
43
59
|
}
|
|
44
60
|
return res.text();
|
|
45
61
|
}
|
|
@@ -80,7 +96,7 @@ export class RefHubClient {
|
|
|
80
96
|
if (params.page !== undefined)
|
|
81
97
|
q.set('page', String(params.page));
|
|
82
98
|
if (params.limit !== undefined)
|
|
83
|
-
q.set('
|
|
99
|
+
q.set('per_page', String(params.limit));
|
|
84
100
|
const qs = q.toString() ? `?${q}` : '';
|
|
85
101
|
return this.req('GET', `/vaults/${vaultId}/items${qs}`);
|
|
86
102
|
}
|
|
@@ -116,11 +132,11 @@ export class RefHubClient {
|
|
|
116
132
|
if (params.doi)
|
|
117
133
|
q.set('doi', params.doi);
|
|
118
134
|
if (params.tag_id)
|
|
119
|
-
q.set('
|
|
135
|
+
q.set('tag', params.tag_id);
|
|
120
136
|
if (params.page !== undefined)
|
|
121
137
|
q.set('page', String(params.page));
|
|
122
138
|
if (params.limit !== undefined)
|
|
123
|
-
q.set('
|
|
139
|
+
q.set('per_page', String(params.limit));
|
|
124
140
|
const qs = q.toString() ? `?${q}` : '';
|
|
125
141
|
return this.req('GET', `/vaults/${vaultId}/search${qs}`);
|
|
126
142
|
}
|
|
@@ -185,12 +201,71 @@ export class RefHubClient {
|
|
|
185
201
|
if (params.until)
|
|
186
202
|
q.set('until', params.until);
|
|
187
203
|
if (params.limit !== undefined)
|
|
188
|
-
q.set('
|
|
204
|
+
q.set('per_page', String(params.limit));
|
|
189
205
|
if (params.page !== undefined)
|
|
190
206
|
q.set('page', String(params.page));
|
|
191
207
|
const qs = q.toString() ? `?${q}` : '';
|
|
192
208
|
return this.req('GET', `/vaults/${vaultId}/audit${qs}`);
|
|
193
209
|
}
|
|
210
|
+
// ── Semantic Scholar discovery/enrichment ──────────────────────────────────
|
|
211
|
+
doiMetadata(doi) {
|
|
212
|
+
return this.req('POST', '/semantic-scholar/doi-metadata', { doi }).then((result) => result.data);
|
|
213
|
+
}
|
|
214
|
+
semanticScholarLookup(body) {
|
|
215
|
+
return this.req('POST', '/semantic-scholar/lookup', body);
|
|
216
|
+
}
|
|
217
|
+
semanticScholarSearch(query, limit) {
|
|
218
|
+
return this.req('POST', '/semantic-scholar/search', { query, ...(limit !== undefined ? { limit } : {}) });
|
|
219
|
+
}
|
|
220
|
+
semanticScholarPaperList(kind, paperId, limit) {
|
|
221
|
+
return this.req('POST', `/semantic-scholar/${kind}`, { paper_id: paperId, ...(limit !== undefined ? { limit } : {}) });
|
|
222
|
+
}
|
|
223
|
+
uploadItemPdfRaw(vaultId, itemId, pdfBuffer) {
|
|
224
|
+
return this.reqBinary('POST', `/vaults/${encodeURIComponent(vaultId)}/items/${encodeURIComponent(itemId)}/pdf`, pdfBuffer, 'application/pdf');
|
|
225
|
+
}
|
|
226
|
+
createItemPdfUploadSession(vaultId, itemId) {
|
|
227
|
+
return this.req('POST', `/vaults/${encodeURIComponent(vaultId)}/items/${encodeURIComponent(itemId)}/pdf/session`, {});
|
|
228
|
+
}
|
|
229
|
+
completeItemPdfUpload(vaultId, itemId, body) {
|
|
230
|
+
return this.req('POST', `/vaults/${encodeURIComponent(vaultId)}/items/${encodeURIComponent(itemId)}/pdf/complete`, body);
|
|
231
|
+
}
|
|
232
|
+
async uploadItemPdfResumable(vaultId, itemId, pdfBuffer) {
|
|
233
|
+
const session = await this.createItemPdfUploadSession(vaultId, itemId);
|
|
234
|
+
const driveResponse = await fetch(session.data.upload_url, {
|
|
235
|
+
method: 'PUT',
|
|
236
|
+
headers: {
|
|
237
|
+
'Content-Type': 'application/pdf',
|
|
238
|
+
'Content-Length': String(pdfBuffer.length),
|
|
239
|
+
},
|
|
240
|
+
body: pdfBuffer,
|
|
241
|
+
});
|
|
242
|
+
if (!driveResponse.ok) {
|
|
243
|
+
const body = await driveResponse.text().catch(() => '');
|
|
244
|
+
throw new Error(`Google Drive resumable upload failed (${driveResponse.status})${body ? `: ${body}` : ''}`);
|
|
245
|
+
}
|
|
246
|
+
const driveUpload = await driveResponse.json().catch(() => ({}));
|
|
247
|
+
if (!driveUpload.id) {
|
|
248
|
+
throw new Error('Google Drive upload completed without returning a file id');
|
|
249
|
+
}
|
|
250
|
+
return this.completeItemPdfUpload(vaultId, itemId, {
|
|
251
|
+
file_id: driveUpload.id,
|
|
252
|
+
web_view_link: driveUpload.webViewLink ?? null,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async uploadItemPdf(vaultId, itemId, pdfBuffer) {
|
|
256
|
+
if (pdfBuffer.length > RefHubClient.RAW_PDF_UPLOAD_LIMIT_BYTES) {
|
|
257
|
+
return this.uploadItemPdfResumable(vaultId, itemId, pdfBuffer);
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
return await this.uploadItemPdfRaw(vaultId, itemId, pdfBuffer);
|
|
261
|
+
}
|
|
262
|
+
catch (err) {
|
|
263
|
+
if (err instanceof RefHubError && err.status === 413 && err.code === 'pdf_upload_too_large_for_api') {
|
|
264
|
+
return this.uploadItemPdfResumable(vaultId, itemId, pdfBuffer);
|
|
265
|
+
}
|
|
266
|
+
throw err;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
194
269
|
}
|
|
195
270
|
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
196
271
|
export function resolveClient(apiKey) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
import { RefHubClient } from '../client.js';
|
|
3
|
+
import type { SemanticScholarPaper } from '../types.js';
|
|
4
|
+
export declare function paperToItem(paper: SemanticScholarPaper): Record<string, unknown>;
|
|
5
|
+
export declare function handleDiscoverSearch(client: RefHubClient, query: string, limit: number | undefined, tableMode: boolean): Promise<void>;
|
|
6
|
+
export declare function handleDiscoverLookup(client: RefHubClient, opts: {
|
|
7
|
+
doi?: string;
|
|
8
|
+
title?: string;
|
|
9
|
+
}, tableMode: boolean): Promise<void>;
|
|
10
|
+
export declare function handleDiscoverList(client: RefHubClient, kind: 'recommendations' | 'related' | 'references' | 'citations' | 'cited-by', paperId: string, limit: number | undefined, tableMode: boolean): Promise<void>;
|
|
11
|
+
export declare function handleDiscoverAdd(client: RefHubClient, vaultId: string, filePath: string, idempotencyKey: string | undefined, tableMode: boolean): Promise<void>;
|
|
12
|
+
export declare function registerDiscover(program: Command): void;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { resolveClient, run } from '../client.js';
|
|
3
|
+
import { format } from '../format.js';
|
|
4
|
+
function authorsToStrings(paper) {
|
|
5
|
+
return (paper.authors ?? []).map((a) => a.name).filter((name) => Boolean(name));
|
|
6
|
+
}
|
|
7
|
+
export function paperToItem(paper) {
|
|
8
|
+
const doi = paper.external_ids?.['DOI'] ?? paper.external_ids?.['doi'];
|
|
9
|
+
return {
|
|
10
|
+
title: paper.title ?? 'Untitled Semantic Scholar paper',
|
|
11
|
+
authors: authorsToStrings(paper),
|
|
12
|
+
year: paper.year ?? undefined,
|
|
13
|
+
journal: paper.venue ?? undefined,
|
|
14
|
+
doi: doi ?? undefined,
|
|
15
|
+
url: paper.url ?? undefined,
|
|
16
|
+
abstract: paper.abstract ?? undefined,
|
|
17
|
+
pdf_url: paper.open_access_pdf_url ?? undefined,
|
|
18
|
+
publication_type: 'article',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export async function handleDiscoverSearch(client, query, limit, tableMode) {
|
|
22
|
+
const result = await client.semanticScholarSearch(query, limit);
|
|
23
|
+
format(result, tableMode, ['paper_id', 'title', 'year', 'venue', 'citation_count', 'open_access_pdf_url']);
|
|
24
|
+
}
|
|
25
|
+
export async function handleDiscoverLookup(client, opts, tableMode) {
|
|
26
|
+
const result = await client.semanticScholarLookup(opts);
|
|
27
|
+
format(result, tableMode);
|
|
28
|
+
}
|
|
29
|
+
export async function handleDiscoverList(client, kind, paperId, limit, tableMode) {
|
|
30
|
+
const result = await client.semanticScholarPaperList(kind, paperId, limit);
|
|
31
|
+
format(result, tableMode, ['paper_id', 'title', 'year', 'venue', 'citation_count', 'open_access_pdf_url']);
|
|
32
|
+
}
|
|
33
|
+
export async function handleDiscoverAdd(client, vaultId, filePath, idempotencyKey, tableMode) {
|
|
34
|
+
const papers = JSON.parse(readFileSync(filePath, 'utf8'));
|
|
35
|
+
if (!Array.isArray(papers)) {
|
|
36
|
+
process.stderr.write(JSON.stringify({ error: { code: 'invalid_file', message: 'File must contain a JSON array of Semantic Scholar paper results.' } }) + '\n');
|
|
37
|
+
process.exit(2);
|
|
38
|
+
}
|
|
39
|
+
const result = await client.upsertItems(vaultId, papers.map(paperToItem), idempotencyKey);
|
|
40
|
+
format(result, tableMode);
|
|
41
|
+
}
|
|
42
|
+
function addPaperListCommand(discover, name, description) {
|
|
43
|
+
discover
|
|
44
|
+
.command(name)
|
|
45
|
+
.description(description)
|
|
46
|
+
.requiredOption('--paper <paperId>', 'Semantic Scholar paper id, DOI:<doi>, or other supported paper identifier')
|
|
47
|
+
.option('--limit <n>', 'result limit, 1-25', (v) => parseInt(v, 10))
|
|
48
|
+
.action(async (opts, cmd) => {
|
|
49
|
+
const g = cmd.optsWithGlobals();
|
|
50
|
+
const client = resolveClient(g.apiKey);
|
|
51
|
+
await run(() => handleDiscoverList(client, name, opts.paper, opts.limit, g.table ?? false));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
export function registerDiscover(program) {
|
|
55
|
+
const discover = program.command('discover').description('Semantic Scholar discovery via RefHub API key');
|
|
56
|
+
discover
|
|
57
|
+
.command('search')
|
|
58
|
+
.description('Search Semantic Scholar by keyword/topic')
|
|
59
|
+
.requiredOption('--query <query>', 'keyword/topic query')
|
|
60
|
+
.option('--limit <n>', 'result limit, 1-25', (v) => parseInt(v, 10))
|
|
61
|
+
.action(async (opts, cmd) => {
|
|
62
|
+
const g = cmd.optsWithGlobals();
|
|
63
|
+
const client = resolveClient(g.apiKey);
|
|
64
|
+
await run(() => handleDiscoverSearch(client, opts.query, opts.limit, g.table ?? false));
|
|
65
|
+
});
|
|
66
|
+
discover
|
|
67
|
+
.command('lookup')
|
|
68
|
+
.description('Resolve a DOI or title to a Semantic Scholar paper id')
|
|
69
|
+
.option('--doi <doi>')
|
|
70
|
+
.option('--title <title>')
|
|
71
|
+
.action(async (opts, cmd) => {
|
|
72
|
+
if ((opts.doi && opts.title) || (!opts.doi && !opts.title)) {
|
|
73
|
+
process.stderr.write(JSON.stringify({ error: { code: 'invalid_lookup_request', message: 'Provide exactly one of --doi or --title.' } }) + '\n');
|
|
74
|
+
process.exit(2);
|
|
75
|
+
}
|
|
76
|
+
const g = cmd.optsWithGlobals();
|
|
77
|
+
const client = resolveClient(g.apiKey);
|
|
78
|
+
await run(() => handleDiscoverLookup(client, { doi: opts.doi, title: opts.title }, g.table ?? false));
|
|
79
|
+
});
|
|
80
|
+
addPaperListCommand(discover, 'recommendations', 'Get Semantic Scholar recommendations from a seed paper');
|
|
81
|
+
addPaperListCommand(discover, 'related', 'Alias for recommendations');
|
|
82
|
+
addPaperListCommand(discover, 'references', 'Get papers cited by a seed paper');
|
|
83
|
+
addPaperListCommand(discover, 'citations', 'Get papers citing a seed paper');
|
|
84
|
+
addPaperListCommand(discover, 'cited-by', 'Alias for citations');
|
|
85
|
+
discover
|
|
86
|
+
.command('add')
|
|
87
|
+
.description('Upsert selected Semantic Scholar paper JSON results into a vault')
|
|
88
|
+
.requiredOption('--vault <id>')
|
|
89
|
+
.requiredOption('--file <path>', 'JSON array of Semantic Scholar paper results')
|
|
90
|
+
.option('--idempotency-key <key>', 'safe retry key')
|
|
91
|
+
.action(async (opts, cmd) => {
|
|
92
|
+
const g = cmd.optsWithGlobals();
|
|
93
|
+
const client = resolveClient(g.apiKey);
|
|
94
|
+
await run(() => handleDiscoverAdd(client, opts.vault, opts.file, opts.idempotencyKey, g.table ?? false));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
import { RefHubClient } from '../client.js';
|
|
3
|
+
import type { Item } from '../types.js';
|
|
4
|
+
export declare function enrichItem(item: Item, client: RefHubClient, vaultId: string, dryRun: boolean): Promise<'enriched' | 'skipped' | 'not_found' | 'no_missing_fields'>;
|
|
5
|
+
export declare function registerEnrich(program: Command): void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { resolveClient, run } from '../client.js';
|
|
2
|
+
export async function enrichItem(item, client, vaultId, dryRun) {
|
|
3
|
+
if (!item.doi)
|
|
4
|
+
return 'skipped';
|
|
5
|
+
const missing = [];
|
|
6
|
+
if (!item.title)
|
|
7
|
+
missing.push('title');
|
|
8
|
+
if (!item.authors || item.authors.length === 0)
|
|
9
|
+
missing.push('authors');
|
|
10
|
+
if (!item.year)
|
|
11
|
+
missing.push('year');
|
|
12
|
+
if (!item.abstract)
|
|
13
|
+
missing.push('abstract');
|
|
14
|
+
if (missing.length === 0)
|
|
15
|
+
return 'no_missing_fields';
|
|
16
|
+
const meta = await client.doiMetadata(item.doi);
|
|
17
|
+
if (!meta)
|
|
18
|
+
return 'not_found';
|
|
19
|
+
const patch = {};
|
|
20
|
+
if (missing.includes('title') && meta.title)
|
|
21
|
+
patch['title'] = meta.title;
|
|
22
|
+
if (missing.includes('authors') && meta.authors?.length)
|
|
23
|
+
patch['authors'] = meta.authors;
|
|
24
|
+
if (missing.includes('year') && meta.year)
|
|
25
|
+
patch['year'] = meta.year;
|
|
26
|
+
if (missing.includes('abstract') && meta.abstract)
|
|
27
|
+
patch['abstract'] = meta.abstract;
|
|
28
|
+
if (Object.keys(patch).length === 0)
|
|
29
|
+
return 'not_found';
|
|
30
|
+
if (!dryRun) {
|
|
31
|
+
await client.updateItem(vaultId, item.id, patch);
|
|
32
|
+
}
|
|
33
|
+
return 'enriched';
|
|
34
|
+
}
|
|
35
|
+
export function registerEnrich(program) {
|
|
36
|
+
program
|
|
37
|
+
.command('enrich')
|
|
38
|
+
.description('Enrich incomplete publication metadata from Semantic Scholar via DOI')
|
|
39
|
+
.requiredOption('--vault <id>', 'vault to enrich')
|
|
40
|
+
.option('--item <itemId>', 'enrich a single item instead of the whole vault')
|
|
41
|
+
.option('--dry-run', 'show what would be updated without writing')
|
|
42
|
+
.action(async (opts, cmd) => {
|
|
43
|
+
const g = cmd.optsWithGlobals();
|
|
44
|
+
const client = resolveClient(g.apiKey);
|
|
45
|
+
const dryRun = opts.dryRun ?? false;
|
|
46
|
+
await run(async () => {
|
|
47
|
+
let items;
|
|
48
|
+
if (opts.item) {
|
|
49
|
+
const result = await client.getItem(opts.vault, opts.item);
|
|
50
|
+
items = [result.data];
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const result = await client.listItems(opts.vault);
|
|
54
|
+
items = result.data;
|
|
55
|
+
}
|
|
56
|
+
const withDoi = items.filter((i) => i.doi);
|
|
57
|
+
if (withDoi.length === 0) {
|
|
58
|
+
process.stdout.write(JSON.stringify({ data: { enriched: 0, skipped: 0, not_found: 0, no_missing_fields: 0, message: 'No items with a DOI found.' } }, null, 2) + '\n');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const counts = { enriched: 0, skipped: 0, not_found: 0, no_missing_fields: 0 };
|
|
62
|
+
const enriched = [];
|
|
63
|
+
for (const item of withDoi) {
|
|
64
|
+
const outcome = await enrichItem(item, client, opts.vault, dryRun);
|
|
65
|
+
counts[outcome]++;
|
|
66
|
+
if (outcome === 'enriched')
|
|
67
|
+
enriched.push(item.id);
|
|
68
|
+
// 1 req/s to respect Semantic Scholar rate limit
|
|
69
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
70
|
+
}
|
|
71
|
+
process.stdout.write(JSON.stringify({
|
|
72
|
+
data: {
|
|
73
|
+
dry_run: dryRun,
|
|
74
|
+
...counts,
|
|
75
|
+
enriched_ids: enriched,
|
|
76
|
+
},
|
|
77
|
+
}, null, 2) + '\n');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
import { RefHubClient } from '../client.js';
|
|
3
|
+
export declare function handlePdfUpload(client: RefHubClient, vaultId: string, itemId: string, filePath: string, tableMode: boolean): Promise<void>;
|
|
4
|
+
export declare function registerPdf(program: Command): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/commands/pdf.ts
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { resolveClient, run } from '../client.js';
|
|
4
|
+
import { format } from '../format.js';
|
|
5
|
+
export async function handlePdfUpload(client, vaultId, itemId, filePath, tableMode) {
|
|
6
|
+
let pdfBuffer;
|
|
7
|
+
try {
|
|
8
|
+
pdfBuffer = readFileSync(filePath);
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
process.stderr.write(JSON.stringify({ error: { code: 'file_read_error', message: `Cannot read file: ${String(e)}` } }) + '\n');
|
|
12
|
+
process.exit(2);
|
|
13
|
+
}
|
|
14
|
+
const result = await client.uploadItemPdf(vaultId, itemId, pdfBuffer);
|
|
15
|
+
format(result, tableMode);
|
|
16
|
+
}
|
|
17
|
+
export function registerPdf(program) {
|
|
18
|
+
const pdf = program.command('pdf').description('Upload a PDF to Google Drive for a vault item');
|
|
19
|
+
pdf
|
|
20
|
+
.command('upload')
|
|
21
|
+
.description('Upload a PDF file and link it to a vault item')
|
|
22
|
+
.requiredOption('--vault <id>', 'vault ID')
|
|
23
|
+
.requiredOption('--item <id>', 'vault item ID')
|
|
24
|
+
.requiredOption('--file <path>', 'path to PDF file')
|
|
25
|
+
.action(async (opts, cmd) => {
|
|
26
|
+
const g = cmd.optsWithGlobals();
|
|
27
|
+
const client = resolveClient(g.apiKey);
|
|
28
|
+
await run(() => handlePdfUpload(client, opts.vault, opts.item, opts.file, g.table ?? false));
|
|
29
|
+
});
|
|
30
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// src/index.ts
|
|
3
3
|
import { Command } from 'commander';
|
|
4
|
+
import { createRequire } from 'module';
|
|
4
5
|
import { registerVaults } from './commands/vaults.js';
|
|
5
6
|
import { registerItems } from './commands/items.js';
|
|
6
7
|
import { registerTags } from './commands/tags.js';
|
|
@@ -8,11 +9,16 @@ import { registerRelations } from './commands/relations.js';
|
|
|
8
9
|
import { registerImport } from './commands/import.js';
|
|
9
10
|
import { registerExport } from './commands/export.js';
|
|
10
11
|
import { registerAudit } from './commands/audit.js';
|
|
12
|
+
import { registerEnrich } from './commands/enrich.js';
|
|
13
|
+
import { registerPdf } from './commands/pdf.js';
|
|
14
|
+
import { registerDiscover } from './commands/discover.js';
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
const { version } = require('../package.json');
|
|
11
17
|
const program = new Command();
|
|
12
18
|
program
|
|
13
19
|
.name('refhub')
|
|
14
20
|
.description('RefHub CLI — manage vaults, items, tags, and relations')
|
|
15
|
-
.version(
|
|
21
|
+
.version(version)
|
|
16
22
|
.option('--api-key <key>', 'RefHub API key (overrides REFHUB_API_KEY env var)')
|
|
17
23
|
.option('--table', 'human-readable table output (default: JSON)');
|
|
18
24
|
registerVaults(program);
|
|
@@ -22,4 +28,7 @@ registerRelations(program);
|
|
|
22
28
|
registerImport(program);
|
|
23
29
|
registerExport(program);
|
|
24
30
|
registerAudit(program);
|
|
31
|
+
registerEnrich(program);
|
|
32
|
+
registerDiscover(program);
|
|
33
|
+
registerPdf(program);
|
|
25
34
|
program.parseAsync();
|
package/dist/types.d.ts
CHANGED
|
@@ -97,3 +97,29 @@ export interface AuditEntry {
|
|
|
97
97
|
export interface ApiResponse<T> {
|
|
98
98
|
data: T;
|
|
99
99
|
}
|
|
100
|
+
export interface SemanticScholarAuthor {
|
|
101
|
+
author_id?: string | null;
|
|
102
|
+
name?: string | null;
|
|
103
|
+
}
|
|
104
|
+
export interface SemanticScholarPaper {
|
|
105
|
+
paper_id: string | null;
|
|
106
|
+
external_ids?: Record<string, string>;
|
|
107
|
+
title?: string | null;
|
|
108
|
+
abstract?: string | null;
|
|
109
|
+
year?: number | null;
|
|
110
|
+
venue?: string | null;
|
|
111
|
+
url?: string | null;
|
|
112
|
+
citation_count?: number | null;
|
|
113
|
+
open_access_pdf_url?: string | null;
|
|
114
|
+
authors?: SemanticScholarAuthor[];
|
|
115
|
+
}
|
|
116
|
+
export interface SemanticScholarDoiMetadata {
|
|
117
|
+
title: string;
|
|
118
|
+
authors: string[];
|
|
119
|
+
year?: number;
|
|
120
|
+
journal?: string;
|
|
121
|
+
doi: string;
|
|
122
|
+
url: string;
|
|
123
|
+
abstract?: string;
|
|
124
|
+
type: string;
|
|
125
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@refhub/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "refhub CLI execution layer for agent skills",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/refhub-io/refhub-cli"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/refhub-io/refhub-cli#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/refhub-io/refhub-cli/issues"
|
|
13
|
+
},
|
|
6
14
|
"engines": { "node": ">=18" },
|
|
7
15
|
"bin": { "refhub": "dist/index.js" },
|
|
8
16
|
"scripts": {
|