@tenderlead/cli 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # TenderLead CLI
2
2
 
3
- Search and score tenders, read your TenderLead daily briefing and manage the firm profile used for matching from a terminal or another tool you control.
3
+ Run TenderLead tender, profile, pipeline, insight and tier-gated bid-workspace tasks from a terminal or another tool you control.
4
4
 
5
5
  ## Install
6
6
 
@@ -69,11 +69,56 @@ tenderlead profile apply --file tenderlead-profile.json
69
69
  The apply command shows the field-level changes and asks you to type `APPLY`
70
70
  before saving. Trusted automation can use `--yes` after reviewing the same diff.
71
71
 
72
+ ## Manage the pipeline and feedback loop
73
+
74
+ ```bash
75
+ tenderlead pipeline list
76
+ tenderlead pipeline add CONTRACT_ID
77
+ tenderlead pipeline update PIPELINE_ITEM_ID --file pipeline-change.json
78
+ tenderlead pipeline remove PIPELINE_ITEM_ID
79
+ tenderlead insights --json
80
+ tenderlead feedback tender CONTRACT_ID positive --note "Fits our current delivery model"
81
+ tenderlead feedback product --area pipeline --note "Keep the buyer visible when I change stage"
82
+ ```
83
+
84
+ Pipeline updates accept the same JSON fields as the product: `stage`, `notes`,
85
+ `outcome_summary`, `outcome_reason`, `outcome_competitor` and `outcome_value`.
86
+
87
+ ## Use the bid workspace
88
+
89
+ Team accounts can use their bid profile, specs, evidence, templates, drafts and
90
+ evidence packs from the CLI:
91
+
92
+ ```bash
93
+ tenderlead bid-profile show --json
94
+ tenderlead bid-profile apply --file bid-profile.json
95
+ tenderlead outlines create CONTRACT_ID
96
+ tenderlead evidence list --json
97
+ tenderlead evidence upload --file certificate.pdf --type certificate
98
+ tenderlead templates upload --file house-style.docx --role primary
99
+ tenderlead drafts create CONTRACT_ID --workspace
100
+ tenderlead drafts export CONTRACT_ID --format xlsx --output workspace.xlsx
101
+ tenderlead drafts import CONTRACT_ID --file workspace.xlsx
102
+ tenderlead evidence-packs create CONTRACT_ID
103
+ ```
104
+
105
+ For draft workspace edits or evidence-pack workspace state, pass the existing
106
+ request JSON with `--file`. The CLI sets `contract_id` from the command and sends
107
+ the remaining fields to the same handler used by the signed-in product.
108
+
109
+ Commands that replace, import, update or remove saved data ask for a typed
110
+ confirmation. Reviewed automation can pass `--yes`. Run `tenderlead --help` for
111
+ the full command list.
112
+
72
113
  Tokens are customer-scoped, revocable, expire after 90 days, and are subject to
73
114
  the account's current TenderLead plan. Brief and tender-result reads are
74
115
  read-only. Profile updates require `profile:write`; on-demand scoring requires
75
- `tenders:write`. Do not commit a token to a repository or paste it into a shared
76
- prompt. Revoke exposed tokens in Settings.
116
+ `tenders:write`; pipeline, feedback, insight and workspace routes use their own
117
+ scopes. Do not commit a token to a repository or paste it into a shared prompt.
118
+ Revoke exposed tokens in Settings.
119
+
120
+ Sign-in, token creation and revocation, Stripe checkout and billing, and
121
+ permanent account deletion remain browser tasks.
77
122
 
78
123
  Provider acceptance and API responses do not prove email inbox delivery.
79
124
 
package/bin/tenderlead.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const fs = require('node:fs');
4
+ const path = require('node:path');
4
5
  const readline = require('node:readline/promises');
5
6
 
6
7
  const DEFAULT_BASE_URL = 'https://tenderlead.co.uk';
@@ -23,6 +24,33 @@ function usage() {
23
24
  ' tenderlead profile template',
24
25
  ' tenderlead profile validate --file <path>',
25
26
  ' tenderlead profile apply --file <path> [--yes]',
27
+ ' tenderlead pipeline list [--json]',
28
+ ' tenderlead pipeline add <contract-id> [--json]',
29
+ ' tenderlead pipeline update <item-id> --file <changes.json> [--yes] [--json]',
30
+ ' tenderlead pipeline remove <item-id> [--yes]',
31
+ ' tenderlead insights [--json]',
32
+ ' tenderlead feedback tender <contract-id> positive|negative [--reason <reason>] [--note <text>]',
33
+ ' tenderlead feedback product --note <text> [--type <type>] [--area <area>] [--contract-id <id>] [--section-id <id>]',
34
+ ' tenderlead bid-profile show [--json]',
35
+ ' tenderlead bid-profile apply --file <profile.json> [--yes]',
36
+ ' tenderlead outlines show <contract-id> [--json]',
37
+ ' tenderlead outlines create <contract-id> [--refresh] [--yes] [--json]',
38
+ ' tenderlead evidence list|enrich [--json]',
39
+ ' tenderlead evidence upload --file <path> [--file <path>...] [--type <type>] [--yes] [--json]',
40
+ ' tenderlead evidence record update <record-id> --status approved|rejected|pending [--yes]',
41
+ ' tenderlead evidence record remove <record-id> [--yes]',
42
+ ' tenderlead evidence document remove <document-id> [--yes]',
43
+ ' tenderlead templates list [--json]',
44
+ ' tenderlead templates upload --file <path> [--file <path>...] [--role <role>] [--yes] [--json]',
45
+ ' tenderlead templates remove <template-id> [--yes]',
46
+ ' tenderlead drafts list [--json]',
47
+ ' tenderlead drafts show <contract-id> [--workspace] [--json]',
48
+ ' tenderlead drafts create <contract-id> [--file <request.json>] [--refresh] [--workspace] [--yes] [--json]',
49
+ ' tenderlead drafts export <contract-id> --format text|docx|xlsx --output <path> [--yes]',
50
+ ' tenderlead drafts import <contract-id> --file <workspace.xlsx> [--yes]',
51
+ ' tenderlead evidence-packs list [--json]',
52
+ ' tenderlead evidence-packs show <contract-id> [--json]',
53
+ ' tenderlead evidence-packs create <contract-id> [--file <request.json>] [--refresh] [--yes] [--json]',
26
54
  '',
27
55
  'Login opens TenderLead Settings so you can create a scoped CLI token.',
28
56
  'Set TENDERLEAD_API_TOKEN to that token before reading or updating account data.',
@@ -82,7 +110,16 @@ function parseArgs(argv) {
82
110
  }
83
111
  return options;
84
112
  }
85
- if (command !== 'profile') throw new Error('Use the login, brief, tenders, tender or profile command.');
113
+ if (command === 'pipeline') return parsePipelineArgs(argv);
114
+ if (command === 'insights') return parseJsonOnlyArgs(argv, 3, { command: 'insights', json: false });
115
+ if (command === 'feedback') return parseFeedbackArgs(argv);
116
+ if (command === 'bid-profile') return parseFileResourceArgs(argv, 'bid-profile', ['show', 'apply']);
117
+ if (command === 'outlines') return parseContractResourceArgs(argv, 'outlines', ['show', 'create']);
118
+ if (command === 'evidence') return parseEvidenceArgs(argv);
119
+ if (command === 'templates') return parseTemplatesArgs(argv);
120
+ if (command === 'drafts') return parseDraftArgs(argv);
121
+ if (command === 'evidence-packs') return parseContractResourceArgs(argv, 'evidence-packs', ['list', 'show', 'create']);
122
+ if (command !== 'profile') throw new Error('Unknown command. Run tenderlead --help for the full command list.');
86
123
 
87
124
  const action = argv[3];
88
125
  if (!['show', 'template', 'validate', 'apply'].includes(action)) {
@@ -105,6 +142,196 @@ function parseArgs(argv) {
105
142
  return options;
106
143
  }
107
144
 
145
+ function optionValue(argv, index, flag) {
146
+ const value = argv[index + 1];
147
+ if (!value || value.startsWith('--')) throw new Error(`${flag} requires a value`);
148
+ return value;
149
+ }
150
+
151
+ function parseJsonOnlyArgs(argv, start, options) {
152
+ for (let index = start; index < argv.length; index += 1) {
153
+ const arg = argv[index];
154
+ if (arg === '--json') options.json = true;
155
+ else if (arg === '--help' || arg === '-h') options.help = true;
156
+ else throw new Error(`Unknown argument: ${arg}`);
157
+ }
158
+ return options;
159
+ }
160
+
161
+ function parsePipelineArgs(argv) {
162
+ const action = argv[3];
163
+ if (!['list', 'add', 'update', 'remove'].includes(action)) throw new Error('Use pipeline list, add, update or remove.');
164
+ const needsId = action !== 'list';
165
+ const id = needsId ? argv[4] : null;
166
+ if (needsId && (!id || id.startsWith('--'))) throw new Error(`pipeline ${action} requires an ID`);
167
+ const options = { command: 'pipeline', action, id, file: null, yes: false, json: false };
168
+ for (let index = needsId ? 5 : 4; index < argv.length; index += 1) {
169
+ const arg = argv[index];
170
+ if (arg === '--json') options.json = true;
171
+ else if (arg === '--yes') options.yes = true;
172
+ else if (arg === '--file' && action === 'update') { options.file = optionValue(argv, index, arg); index += 1; }
173
+ else if (arg === '--help' || arg === '-h') options.help = true;
174
+ else throw new Error(`Unknown argument: ${arg}`);
175
+ }
176
+ if (action === 'update' && !options.file) throw new Error('pipeline update requires --file <changes.json>');
177
+ return options;
178
+ }
179
+
180
+ function parseFeedbackArgs(argv) {
181
+ const action = argv[3];
182
+ if (action === 'tender') {
183
+ const contractId = argv[4];
184
+ const response = argv[5];
185
+ if (!contractId || !['positive', 'negative'].includes(response)) throw new Error('Use feedback tender <contract-id> positive|negative.');
186
+ const options = { command: 'feedback', action, contractId, response, reason: null, note: null, json: false };
187
+ for (let index = 6; index < argv.length; index += 1) {
188
+ const arg = argv[index];
189
+ if (arg === '--json') options.json = true;
190
+ else if (arg === '--reason' || arg === '--note') { options[arg.slice(2)] = optionValue(argv, index, arg); index += 1; }
191
+ else throw new Error(`Unknown argument: ${arg}`);
192
+ }
193
+ return options;
194
+ }
195
+ if (action !== 'product') throw new Error('Use feedback tender or feedback product.');
196
+ const options = { command: 'feedback', action, note: null, feedbackType: 'general', area: null, contextPath: '/app#insights', contractId: null, sectionId: null, json: false };
197
+ const flags = { '--note': 'note', '--type': 'feedbackType', '--area': 'area', '--context': 'contextPath', '--contract-id': 'contractId', '--section-id': 'sectionId' };
198
+ for (let index = 4; index < argv.length; index += 1) {
199
+ const arg = argv[index];
200
+ if (arg === '--json') options.json = true;
201
+ else if (flags[arg]) { options[flags[arg]] = optionValue(argv, index, arg); index += 1; }
202
+ else throw new Error(`Unknown argument: ${arg}`);
203
+ }
204
+ if (!options.note) throw new Error('feedback product requires --note <text>');
205
+ return options;
206
+ }
207
+
208
+ function parseFileResourceArgs(argv, command, actions) {
209
+ const action = argv[3];
210
+ if (!actions.includes(action)) throw new Error(`Use ${command} ${actions.join(' or ')}.`);
211
+ const options = { command, action, file: null, yes: false, json: false };
212
+ for (let index = 4; index < argv.length; index += 1) {
213
+ const arg = argv[index];
214
+ if (arg === '--json') options.json = true;
215
+ else if (arg === '--yes') options.yes = true;
216
+ else if (arg === '--file') { options.file = optionValue(argv, index, arg); index += 1; }
217
+ else throw new Error(`Unknown argument: ${arg}`);
218
+ }
219
+ if (action === 'apply' && !options.file) throw new Error(`${command} apply requires --file <path>`);
220
+ return options;
221
+ }
222
+
223
+ function parseContractResourceArgs(argv, command, actions) {
224
+ const action = argv[3];
225
+ if (!actions.includes(action)) throw new Error(`Use ${command} ${actions.join(', ')}.`);
226
+ const needsId = action !== 'list';
227
+ const contractId = needsId ? argv[4] : null;
228
+ if (needsId && (!contractId || contractId.startsWith('--'))) throw new Error(`${command} ${action} requires a contract ID`);
229
+ const options = { command, action, contractId, file: null, refresh: false, yes: false, json: false };
230
+ for (let index = needsId ? 5 : 4; index < argv.length; index += 1) {
231
+ const arg = argv[index];
232
+ if (arg === '--json') options.json = true;
233
+ else if (arg === '--refresh') options.refresh = true;
234
+ else if (arg === '--yes') options.yes = true;
235
+ else if (arg === '--file') { options.file = optionValue(argv, index, arg); index += 1; }
236
+ else throw new Error(`Unknown argument: ${arg}`);
237
+ }
238
+ return options;
239
+ }
240
+
241
+ function parseEvidenceArgs(argv) {
242
+ const action = argv[3];
243
+ if (action === 'list') return parseJsonOnlyArgs(argv, 4, { command: 'evidence', action, json: false });
244
+ if (action === 'enrich') {
245
+ const options = { command: 'evidence', action, yes: false, json: false };
246
+ for (let index = 4; index < argv.length; index += 1) {
247
+ const arg = argv[index];
248
+ if (arg === '--json') options.json = true;
249
+ else if (arg === '--yes') options.yes = true;
250
+ else throw new Error(`Unknown argument: ${arg}`);
251
+ }
252
+ return options;
253
+ }
254
+ if (action === 'upload') {
255
+ const options = { command: 'evidence', action, files: [], documentType: null, yes: false, json: false };
256
+ for (let index = 4; index < argv.length; index += 1) {
257
+ const arg = argv[index];
258
+ if (arg === '--json') options.json = true;
259
+ else if (arg === '--yes') options.yes = true;
260
+ else if (arg === '--file') { options.files.push(optionValue(argv, index, arg)); index += 1; }
261
+ else if (arg === '--type') { options.documentType = optionValue(argv, index, arg); index += 1; }
262
+ else throw new Error(`Unknown argument: ${arg}`);
263
+ }
264
+ if (!options.files.length) throw new Error('evidence upload requires at least one --file <path>');
265
+ return options;
266
+ }
267
+ if (!['record', 'document'].includes(action)) throw new Error('Use evidence list, enrich, upload, record or document.');
268
+ const operation = argv[4];
269
+ const id = argv[5];
270
+ if (!id || !['update', 'remove'].includes(operation) || (action === 'document' && operation !== 'remove')) throw new Error(`Use evidence ${action} ${action === 'record' ? 'update|remove' : 'remove'} <id>.`);
271
+ const options = { command: 'evidence', action, operation, id, reviewStatus: null, yes: false, json: false };
272
+ for (let index = 6; index < argv.length; index += 1) {
273
+ const arg = argv[index];
274
+ if (arg === '--json') options.json = true;
275
+ else if (arg === '--yes') options.yes = true;
276
+ else if (arg === '--status' && operation === 'update') { options.reviewStatus = optionValue(argv, index, arg); index += 1; }
277
+ else throw new Error(`Unknown argument: ${arg}`);
278
+ }
279
+ if (operation === 'update' && !['approved', 'rejected', 'pending'].includes(options.reviewStatus)) throw new Error('--status must be approved, rejected or pending');
280
+ return options;
281
+ }
282
+
283
+ function parseTemplatesArgs(argv) {
284
+ const action = argv[3];
285
+ if (action === 'list') return parseJsonOnlyArgs(argv, 4, { command: 'templates', action, json: false });
286
+ if (action === 'remove') {
287
+ const id = argv[4];
288
+ if (!id || id.startsWith('--')) throw new Error('templates remove requires a template ID');
289
+ const options = { command: 'templates', action, id, yes: false, json: false };
290
+ for (let index = 5; index < argv.length; index += 1) {
291
+ const arg = argv[index];
292
+ if (arg === '--json') options.json = true;
293
+ else if (arg === '--yes') options.yes = true;
294
+ else throw new Error(`Unknown argument: ${arg}`);
295
+ }
296
+ return options;
297
+ }
298
+ if (action !== 'upload') throw new Error('Use templates list, upload or remove.');
299
+ const options = { command: 'templates', action, files: [], role: null, yes: false, json: false };
300
+ for (let index = 4; index < argv.length; index += 1) {
301
+ const arg = argv[index];
302
+ if (arg === '--json') options.json = true;
303
+ else if (arg === '--yes') options.yes = true;
304
+ else if (arg === '--file') { options.files.push(optionValue(argv, index, arg)); index += 1; }
305
+ else if (arg === '--role') { options.role = optionValue(argv, index, arg); index += 1; }
306
+ else throw new Error(`Unknown argument: ${arg}`);
307
+ }
308
+ if (!options.files.length) throw new Error('templates upload requires at least one --file <path>');
309
+ return options;
310
+ }
311
+
312
+ function parseDraftArgs(argv) {
313
+ const action = argv[3];
314
+ if (action === 'list') return parseJsonOnlyArgs(argv, 4, { command: 'drafts', action, json: false });
315
+ if (!['show', 'create', 'export', 'import'].includes(action)) throw new Error('Use drafts list, show, create, export or import.');
316
+ const contractId = argv[4];
317
+ if (!contractId || contractId.startsWith('--')) throw new Error(`drafts ${action} requires a contract ID`);
318
+ const options = { command: 'drafts', action, contractId, file: null, refresh: false, workspace: false, format: null, output: null, yes: false, json: false };
319
+ for (let index = 5; index < argv.length; index += 1) {
320
+ const arg = argv[index];
321
+ if (arg === '--json') options.json = true;
322
+ else if (arg === '--yes') options.yes = true;
323
+ else if (arg === '--refresh') options.refresh = true;
324
+ else if (arg === '--workspace') options.workspace = true;
325
+ else if (arg === '--file') { options.file = optionValue(argv, index, arg); index += 1; }
326
+ else if (arg === '--format') { options.format = optionValue(argv, index, arg); index += 1; }
327
+ else if (arg === '--output') { options.output = optionValue(argv, index, arg); index += 1; }
328
+ else throw new Error(`Unknown argument: ${arg}`);
329
+ }
330
+ if (action === 'export' && (!['text', 'docx', 'xlsx'].includes(options.format) || !options.output)) throw new Error('drafts export requires --format text|docx|xlsx and --output <path>');
331
+ if (action === 'import' && !options.file) throw new Error('drafts import requires --file <workspace.xlsx>');
332
+ return options;
333
+ }
334
+
108
335
  function openLogin(spawnImpl = require('node:child_process').spawnSync) {
109
336
  const url = `${DEFAULT_BASE_URL}${LOGIN_PATH}`;
110
337
  const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
@@ -164,12 +391,13 @@ function apiToken() {
164
391
  }
165
392
 
166
393
  async function apiRequest(path, options = {}, fetchImpl = fetch) {
394
+ const isForm = typeof FormData !== 'undefined' && options.body instanceof FormData;
167
395
  const response = await fetchImpl(`${DEFAULT_BASE_URL}${path}`, {
168
396
  ...options,
169
397
  headers: {
170
398
  Accept: 'application/json',
171
399
  Authorization: `Bearer ${apiToken()}`,
172
- ...(options.body ? { 'Content-Type': 'application/json' } : {}),
400
+ ...(options.body && !isForm ? { 'Content-Type': 'application/json' } : {}),
173
401
  ...(options.headers || {}),
174
402
  },
175
403
  });
@@ -204,6 +432,96 @@ async function updateProfile(profile, fetchImpl = fetch) {
204
432
  return payload.profile || {};
205
433
  }
206
434
 
435
+ function readJsonFile(file, readFileImpl = fs.readFileSync) {
436
+ try {
437
+ const value = JSON.parse(readFileImpl(file, 'utf8'));
438
+ if (!isPlainObject(value)) throw new Error('JSON root must be an object');
439
+ return value;
440
+ } catch (error) {
441
+ if (error instanceof SyntaxError) throw new Error(`File is not valid JSON: ${error.message}`);
442
+ throw new Error(`Could not read JSON file: ${error.message}`);
443
+ }
444
+ }
445
+
446
+ function contentTypeForFile(file) {
447
+ const extension = path.extname(file).toLowerCase();
448
+ return ({
449
+ '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
450
+ '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
451
+ '.pdf': 'application/pdf', '.txt': 'text/plain', '.md': 'text/markdown', '.html': 'text/html',
452
+ })[extension] || 'application/octet-stream';
453
+ }
454
+
455
+ function appendFiles(form, files, field = 'files', readFileImpl = fs.readFileSync) {
456
+ for (const file of files) {
457
+ const bytes = readFileImpl(file);
458
+ form.append(field, new Blob([bytes], { type: contentTypeForFile(file) }), path.basename(file));
459
+ }
460
+ return form;
461
+ }
462
+
463
+ async function fetchPipeline(fetchImpl = fetch) { return apiRequest('/v1/pipeline', {}, fetchImpl); }
464
+ async function addPipelineItem(contractId, fetchImpl = fetch) { return apiRequest('/v1/pipeline', { method: 'POST', body: JSON.stringify({ contract_id: contractId }) }, fetchImpl); }
465
+ async function updatePipelineItem(id, changes, fetchImpl = fetch) { return apiRequest(`/v1/pipeline/${encodeURIComponent(id)}`, { method: 'PATCH', body: JSON.stringify(changes) }, fetchImpl); }
466
+ async function removePipelineItem(id, fetchImpl = fetch) { return apiRequest(`/v1/pipeline/${encodeURIComponent(id)}`, { method: 'DELETE' }, fetchImpl); }
467
+ async function fetchInsights(fetchImpl = fetch) { return apiRequest('/v1/insights', {}, fetchImpl); }
468
+ async function sendTenderFeedback(options, fetchImpl = fetch) {
469
+ return apiRequest('/v1/feedback/tender', { method: 'POST', body: JSON.stringify({ contract_id: options.contractId, response: options.response, reason: options.reason, note: options.note }) }, fetchImpl);
470
+ }
471
+ async function sendProductFeedback(options, fetchImpl = fetch) {
472
+ return apiRequest('/v1/feedback/product', { method: 'POST', body: JSON.stringify({ feedback_type: options.feedbackType, area: options.area, note: options.note, context_path: options.contextPath, contract_id: options.contractId, section_id: options.sectionId }) }, fetchImpl);
473
+ }
474
+ async function fetchBidProfile(fetchImpl = fetch) { const payload = await apiRequest('/v1/bid-profile', {}, fetchImpl); return payload.profile; }
475
+ async function updateBidProfile(profile, fetchImpl = fetch) { const payload = await apiRequest('/v1/bid-profile', { method: 'PUT', body: JSON.stringify(profile) }, fetchImpl); return payload.profile; }
476
+ async function fetchOutline(contractId, fetchImpl = fetch) { return apiRequest(`/v1/outlines/${encodeURIComponent(contractId)}`, {}, fetchImpl); }
477
+ async function createOutline(contractId, refresh, fetchImpl = fetch) { return apiRequest('/v1/outlines', { method: 'POST', body: JSON.stringify({ contract_id: contractId, refresh }) }, fetchImpl); }
478
+ async function fetchEvidence(fetchImpl = fetch) { return apiRequest('/v1/evidence', {}, fetchImpl); }
479
+ async function enrichEvidence(fetchImpl = fetch) { return apiRequest('/v1/evidence/enrich', { method: 'POST' }, fetchImpl); }
480
+ async function uploadEvidence(files, documentType, fetchImpl = fetch, readFileImpl = fs.readFileSync) {
481
+ const form = appendFiles(new FormData(), files, 'files', readFileImpl);
482
+ if (documentType) form.set('document_type', documentType);
483
+ return apiRequest('/v1/evidence/documents', { method: 'POST', body: form }, fetchImpl);
484
+ }
485
+ async function updateEvidenceRecord(id, reviewStatus, fetchImpl = fetch) { return apiRequest(`/v1/evidence/records/${encodeURIComponent(id)}`, { method: 'PUT', body: JSON.stringify({ review_status: reviewStatus }) }, fetchImpl); }
486
+ async function removeEvidenceRecord(id, fetchImpl = fetch) { return apiRequest(`/v1/evidence/records/${encodeURIComponent(id)}`, { method: 'DELETE' }, fetchImpl); }
487
+ async function removeEvidenceDocument(id, fetchImpl = fetch) { return apiRequest(`/v1/evidence/documents/${encodeURIComponent(id)}`, { method: 'DELETE' }, fetchImpl); }
488
+ async function fetchTemplates(fetchImpl = fetch) { return apiRequest('/v1/templates', {}, fetchImpl); }
489
+ async function uploadTemplates(files, role, fetchImpl = fetch, readFileImpl = fs.readFileSync) {
490
+ const form = appendFiles(new FormData(), files, 'files', readFileImpl);
491
+ if (role) form.set('template_role', role);
492
+ return apiRequest('/v1/templates', { method: 'POST', body: form }, fetchImpl);
493
+ }
494
+ async function removeTemplate(id, fetchImpl = fetch) { return apiRequest(`/v1/templates/${encodeURIComponent(id)}`, { method: 'DELETE' }, fetchImpl); }
495
+ async function fetchDrafts(fetchImpl = fetch) { return apiRequest('/v1/drafts', {}, fetchImpl); }
496
+ async function fetchDraft(contractId, workspace, fetchImpl = fetch) { return apiRequest(`/v1/drafts/${encodeURIComponent(contractId)}${workspace ? '?workspace=1' : ''}`, {}, fetchImpl); }
497
+ async function createDraft(contractId, options, fetchImpl = fetch) {
498
+ const body = { ...(options.file ? readJsonFile(options.file) : {}), contract_id: contractId };
499
+ if (options.refresh) body.refresh = true;
500
+ if (options.workspace) body.workspace = true;
501
+ return apiRequest('/v1/drafts', { method: 'POST', body: JSON.stringify(body) }, fetchImpl);
502
+ }
503
+ async function exportDraft(contractId, format, output, fetchImpl = fetch, writeFileImpl = fs.writeFileSync) {
504
+ const response = await fetchImpl(`${DEFAULT_BASE_URL}/v1/drafts/${encodeURIComponent(contractId)}?export=${encodeURIComponent(format)}`, { headers: { Authorization: `Bearer ${apiToken()}` } });
505
+ if (!response.ok) {
506
+ const body = await response.json().catch(() => ({}));
507
+ throw new Error(body.error || `TenderLead API failed: ${response.status}`);
508
+ }
509
+ const bytes = Buffer.from(await response.arrayBuffer());
510
+ writeFileImpl(output, bytes);
511
+ return { output, bytes: bytes.length };
512
+ }
513
+ async function importDraft(contractId, file, fetchImpl = fetch, readFileImpl = fs.readFileSync) {
514
+ const form = appendFiles(new FormData(), [file], 'file', readFileImpl);
515
+ return apiRequest(`/v1/drafts/${encodeURIComponent(contractId)}/import`, { method: 'POST', body: form }, fetchImpl);
516
+ }
517
+ async function fetchEvidencePacks(fetchImpl = fetch) { return apiRequest('/v1/evidence-packs', {}, fetchImpl); }
518
+ async function fetchEvidencePack(contractId, fetchImpl = fetch) { return apiRequest(`/v1/evidence-packs/${encodeURIComponent(contractId)}`, {}, fetchImpl); }
519
+ async function createEvidencePack(contractId, options, fetchImpl = fetch) {
520
+ const body = { ...(options.file ? readJsonFile(options.file) : {}), contract_id: contractId };
521
+ if (options.refresh) body.refresh = true;
522
+ return apiRequest('/v1/evidence-packs', { method: 'POST', body: JSON.stringify(body) }, fetchImpl);
523
+ }
524
+
207
525
  function isPlainObject(value) {
208
526
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
209
527
  }
@@ -323,6 +641,46 @@ async function confirmApply(input = process.stdin, output = process.stdout) {
323
641
  }
324
642
  }
325
643
 
644
+ async function confirmWord(word, message, input = process.stdin, output = process.stdout) {
645
+ const prompt = readline.createInterface({ input, output });
646
+ try {
647
+ return (await prompt.question(`${message} Type ${word} to continue: `)).trim() === word;
648
+ } finally {
649
+ prompt.close();
650
+ }
651
+ }
652
+
653
+ async function requireWriteConfirmation(options, word, message) {
654
+ if (options.yes) return true;
655
+ if (!process.stdin.isTTY) throw new Error(`Interactive confirmation requires a terminal. Re-run with --yes after reviewing the command.`);
656
+ return confirmWord(word, message);
657
+ }
658
+
659
+ function printPayload(payload, json, summary) {
660
+ if (json || !summary) console.log(JSON.stringify(payload, null, 2));
661
+ else console.log(summary(payload));
662
+ }
663
+
664
+ function formatPipeline(payload) {
665
+ const stages = ['watching', 'preparing', 'submitted', 'won', 'lost'];
666
+ const lines = ['# TenderLead pipeline', ''];
667
+ for (const stage of stages) {
668
+ const rows = Array.isArray(payload[stage]) ? payload[stage] : [];
669
+ lines.push(`## ${stage[0].toUpperCase()}${stage.slice(1)} (${rows.length})`);
670
+ for (const row of rows) lines.push(`- ${line(row.title, 'Untitled notice')} · item ${line(row.id, 'unknown')} · contract ${line(row.contract_id, 'unknown')}`);
671
+ if (!rows.length) lines.push('- None');
672
+ lines.push('');
673
+ }
674
+ return lines.join('\n').trim();
675
+ }
676
+
677
+ function formatList(title, rows, rowText) {
678
+ const lines = [`# ${title}`, '', `Results: ${rows.length}`, ''];
679
+ if (!rows.length) lines.push('None.');
680
+ for (const row of rows) lines.push(`- ${rowText(row)}`);
681
+ return lines.join('\n');
682
+ }
683
+
326
684
  async function main() {
327
685
  if (process.argv.includes('--help') || process.argv.includes('-h')) return console.log(usage());
328
686
  const options = parseArgs(process.argv);
@@ -346,6 +704,81 @@ async function main() {
346
704
  console.log(options.json ? JSON.stringify(result, null, 2) : formatScore(result));
347
705
  return;
348
706
  }
707
+ if (options.command === 'pipeline') {
708
+ if (options.action === 'list') return printPayload(await fetchPipeline(), options.json, formatPipeline);
709
+ if (options.action === 'add') return printPayload(await addPipelineItem(options.id), options.json, row => `Added ${line(row.contract_id, options.id)} to ${line(row.stage, 'watching')}.`);
710
+ if (!(await requireWriteConfirmation(options, options.action === 'remove' ? 'REMOVE' : 'UPDATE', `${options.action === 'remove' ? 'Remove' : 'Update'} pipeline item ${options.id}.`))) return console.log('Pipeline unchanged.');
711
+ if (options.action === 'update') return printPayload(await updatePipelineItem(options.id, readJsonFile(options.file)), options.json, row => `Updated pipeline item ${line(row.id, options.id)}.`);
712
+ return printPayload(await removePipelineItem(options.id), options.json, () => `Removed pipeline item ${options.id}.`);
713
+ }
714
+ if (options.command === 'insights') return printPayload(await fetchInsights(), options.json);
715
+ if (options.command === 'feedback') {
716
+ const result = options.action === 'tender' ? await sendTenderFeedback(options) : await sendProductFeedback(options);
717
+ return printPayload(result, options.json, () => 'Feedback saved.');
718
+ }
719
+ if (options.command === 'bid-profile') {
720
+ if (options.action === 'show') return printPayload(await fetchBidProfile(), options.json);
721
+ const profile = readJsonFile(options.file);
722
+ if (!(await requireWriteConfirmation(options, 'APPLY', 'Replace the bid workspace profile with the supplied JSON.'))) return console.log('Bid profile unchanged.');
723
+ return printPayload(await updateBidProfile(profile), options.json, () => 'Bid profile saved.');
724
+ }
725
+ if (options.command === 'outlines') {
726
+ if (options.action === 'show') return printPayload(await fetchOutline(options.contractId), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
727
+ if (!(await requireWriteConfirmation(options, 'GENERATE', `Generate${options.refresh ? ' or refresh' : ''} the bid spec for ${options.contractId}.`))) return console.log('Outline unchanged.');
728
+ return printPayload(await createOutline(options.contractId, options.refresh), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
729
+ }
730
+ if (options.command === 'evidence') {
731
+ if (options.action === 'list') return printPayload(await fetchEvidence(), options.json);
732
+ if (options.action === 'enrich') {
733
+ if (!(await requireWriteConfirmation(options, 'ENRICH', 'Run public evidence enrichment for this account.'))) return console.log('Evidence unchanged.');
734
+ return printPayload(await enrichEvidence(), options.json);
735
+ }
736
+ if (options.action === 'upload') {
737
+ if (!(await requireWriteConfirmation(options, 'UPLOAD', `Upload ${options.files.length} evidence document${options.files.length === 1 ? '' : 's'}.`))) return console.log('Evidence unchanged.');
738
+ return printPayload(await uploadEvidence(options.files, options.documentType), options.json);
739
+ }
740
+ const verb = options.operation === 'update' ? 'UPDATE' : 'REMOVE';
741
+ if (!(await requireWriteConfirmation(options, verb, `${options.operation === 'update' ? 'Update' : 'Remove'} evidence ${options.action} ${options.id}.`))) return console.log('Evidence unchanged.');
742
+ if (options.action === 'record' && options.operation === 'update') return printPayload(await updateEvidenceRecord(options.id, options.reviewStatus), options.json);
743
+ if (options.action === 'record') return printPayload(await removeEvidenceRecord(options.id), options.json, () => `Removed evidence record ${options.id}.`);
744
+ return printPayload(await removeEvidenceDocument(options.id), options.json, () => `Removed evidence document ${options.id}.`);
745
+ }
746
+ if (options.command === 'templates') {
747
+ if (options.action === 'list') {
748
+ const payload = await fetchTemplates();
749
+ return printPayload(payload, options.json, value => formatList('TenderLead bid templates', value.templates || [], row => `${line(row.file_name, 'Unnamed')} · ${line(row.id, 'unknown')}`));
750
+ }
751
+ if (options.action === 'upload') {
752
+ if (!(await requireWriteConfirmation(options, 'UPLOAD', `Upload ${options.files.length} bid template${options.files.length === 1 ? '' : 's'}.`))) return console.log('Templates unchanged.');
753
+ return printPayload(await uploadTemplates(options.files, options.role), options.json);
754
+ }
755
+ if (!(await requireWriteConfirmation(options, 'REMOVE', `Remove bid template ${options.id}.`))) return console.log('Templates unchanged.');
756
+ return printPayload(await removeTemplate(options.id), options.json, () => `Removed bid template ${options.id}.`);
757
+ }
758
+ if (options.command === 'drafts') {
759
+ if (options.action === 'list') {
760
+ const payload = await fetchDrafts();
761
+ return printPayload(payload, options.json, value => formatList('TenderLead bid drafts', value.drafts || [], row => `${line(row.title, 'Untitled')} · ${line(row.contract_id, 'unknown')}`));
762
+ }
763
+ if (options.action === 'show') return printPayload(await fetchDraft(options.contractId, options.workspace), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
764
+ if (options.action === 'export') {
765
+ if (fs.existsSync(options.output) && !options.yes) throw new Error('Output file already exists. Re-run with --yes to replace it.');
766
+ const result = await exportDraft(options.contractId, options.format, options.output);
767
+ return console.log(`Saved ${result.bytes} bytes to ${result.output}.`);
768
+ }
769
+ if (!(await requireWriteConfirmation(options, options.action === 'import' ? 'IMPORT' : 'GENERATE', `${options.action === 'import' ? 'Import workspace answers for' : 'Generate or update draft for'} ${options.contractId}.`))) return console.log('Draft unchanged.');
770
+ if (options.action === 'import') return printPayload(await importDraft(options.contractId, options.file), options.json);
771
+ return printPayload(await createDraft(options.contractId, options), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
772
+ }
773
+ if (options.command === 'evidence-packs') {
774
+ if (options.action === 'list') {
775
+ const payload = await fetchEvidencePacks();
776
+ return printPayload(payload, options.json, value => formatList('TenderLead evidence packs', value.packs || [], row => `${line(row.title, 'Untitled')} · ${line(row.contract_id, 'unknown')}`));
777
+ }
778
+ if (options.action === 'show') return printPayload(await fetchEvidencePack(options.contractId), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
779
+ if (!(await requireWriteConfirmation(options, 'GENERATE', `Generate${options.refresh ? ' or refresh' : ''} the evidence pack for ${options.contractId}.`))) return console.log('Evidence pack unchanged.');
780
+ return printPayload(await createEvidencePack(options.contractId, options), options.json, payload => payload.content || JSON.stringify(payload, null, 2));
781
+ }
349
782
  if (options.action === 'template') {
350
783
  console.log(JSON.stringify(profileTemplate(), null, 2));
351
784
  return;
@@ -390,5 +823,9 @@ if (require.main === module) {
390
823
  module.exports = {
391
824
  parseArgs, openLogin, formatBrief, formatTenders, formatScore, fetchBrief, searchTenders, scoreTender, fetchProfile, updateProfile,
392
825
  validateProfile, readProfileFile, profileTemplate, profileDiff, formatProfile,
393
- formatProfileDiff, confirmApply,
826
+ formatProfileDiff, confirmApply, confirmWord, readJsonFile, contentTypeForFile, appendFiles,
827
+ fetchPipeline, addPipelineItem, updatePipelineItem, removePipelineItem, fetchInsights, sendTenderFeedback, sendProductFeedback,
828
+ fetchBidProfile, updateBidProfile, fetchOutline, createOutline, fetchEvidence, enrichEvidence, uploadEvidence,
829
+ updateEvidenceRecord, removeEvidenceRecord, removeEvidenceDocument, fetchTemplates, uploadTemplates, removeTemplate,
830
+ fetchDrafts, fetchDraft, createDraft, exportDraft, importDraft, fetchEvidencePacks, fetchEvidencePack, createEvidencePack,
394
831
  };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@tenderlead/cli",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
- "description": "Search and score tenders, read briefings and manage your firm profile from the terminal",
5
+ "description": "Run TenderLead tender, profile, pipeline, insight and bid-workspace workflows from the terminal",
6
6
  "homepage": "https://tenderlead.co.uk",
7
7
  "files": [
8
8
  "bin",