@artis-lab/cli 0.8.0 → 0.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.
- package/package.json +1 -1
- package/src/bin.js +2 -1
- package/src/lib/cli.d.ts +9 -1
- package/src/lib/cli.js +162 -24
- package/src/lib/exit-codes.d.ts +26 -0
- package/src/lib/exit-codes.js +47 -0
package/package.json
CHANGED
package/src/bin.js
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
// symlink, so `import.meta.url` (realpath) never equals `process.argv[1]` (the
|
|
4
4
|
// symlink) and any main-module guard here would silently do nothing.
|
|
5
5
|
import { runArtisCli } from './lib/cli.js';
|
|
6
|
+
import { exitCodeFor } from './lib/exit-codes.js';
|
|
6
7
|
runArtisCli().catch((error) => {
|
|
7
8
|
console.error(error instanceof Error ? error.message : String(error));
|
|
8
|
-
process.exitCode =
|
|
9
|
+
process.exitCode = exitCodeFor(error);
|
|
9
10
|
});
|
|
10
11
|
//# sourceMappingURL=bin.js.map
|
package/src/lib/cli.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { installSkillFromCli } from './skill-install.js';
|
|
|
2
2
|
import { artisCliVersion } from './version.js';
|
|
3
3
|
export { artisCliVersion };
|
|
4
4
|
export { installSkillFromCli };
|
|
5
|
-
export declare const commands: readonly ["annotate", "annotation", "artifact", "guidelines", "help", "install-skill", "login", "publish", "reply", "revise", "share", "version", "whoami"];
|
|
5
|
+
export declare const commands: readonly ["annotate", "annotation", "artifact", "guidelines", "help", "install-skill", "login", "publish", "reply", "revise", "share", "version", "watch", "whoami"];
|
|
6
6
|
export type ArtisCommand = (typeof commands)[number];
|
|
7
7
|
/**
|
|
8
8
|
* Mirrors the sdk's `ArtifactVisibility`, deliberately re-declared instead of
|
|
@@ -54,4 +54,12 @@ export declare function createAnnotationFromCli(args: string[]): Promise<void>;
|
|
|
54
54
|
export declare function replyFromCli(args: string[]): Promise<void>;
|
|
55
55
|
export declare function editAnnotationFromCli(args: string[]): Promise<void>;
|
|
56
56
|
export declare function retractAnnotationFromCli(args: string[]): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Wait for annotation events on one artifact caused by anyone but this API
|
|
59
|
+
* key, by long-polling `GET /api/artifacts/:id/events` from a feedback
|
|
60
|
+
* cursor. Streams one JSON line per event until stopped; `--once` prints the
|
|
61
|
+
* first batch as `{events, cursor}` and exits 0. Network drops and rate
|
|
62
|
+
* limits are retried from the last cursor, so no event is lost or repeated.
|
|
63
|
+
*/
|
|
64
|
+
export declare function watchArtifactFromCli(args: string[]): Promise<void>;
|
|
57
65
|
export declare function runArtisCli(args?: string[]): Promise<void>;
|
package/src/lib/cli.js
CHANGED
|
@@ -17,6 +17,7 @@ import { getRememberedBase, rememberBase } from './revision-bases.js';
|
|
|
17
17
|
import { installSkillFromCli } from './skill-install.js';
|
|
18
18
|
import { artisCliVersion } from './version.js';
|
|
19
19
|
import { collectFlagValues, hasFlag, parseFlagValue } from './args.js';
|
|
20
|
+
import { CliError, exitCodeForResponse, exitCodes, usageError, } from './exit-codes.js';
|
|
20
21
|
export { artisCliVersion };
|
|
21
22
|
export { installSkillFromCli };
|
|
22
23
|
export const commands = [
|
|
@@ -32,6 +33,7 @@ export const commands = [
|
|
|
32
33
|
'revise',
|
|
33
34
|
'share',
|
|
34
35
|
'version',
|
|
36
|
+
'watch',
|
|
35
37
|
'whoami',
|
|
36
38
|
];
|
|
37
39
|
/**
|
|
@@ -82,11 +84,13 @@ const valueFlags = new Set([
|
|
|
82
84
|
'--resolve',
|
|
83
85
|
'--revision',
|
|
84
86
|
'--role',
|
|
87
|
+
'--since',
|
|
85
88
|
'--status',
|
|
86
89
|
'--base',
|
|
87
90
|
'--summary',
|
|
88
91
|
'--text-end',
|
|
89
92
|
'--text-start',
|
|
93
|
+
'--timeout',
|
|
90
94
|
'--title',
|
|
91
95
|
'--url',
|
|
92
96
|
'--visibility',
|
|
@@ -114,25 +118,25 @@ function parseVisibility(value) {
|
|
|
114
118
|
if (cliVisibilityOptions.includes(value)) {
|
|
115
119
|
return value;
|
|
116
120
|
}
|
|
117
|
-
throw
|
|
121
|
+
throw usageError(`Invalid visibility "${value}". Expected one of: ${cliVisibilityOptions.join(', ')}`);
|
|
118
122
|
}
|
|
119
123
|
function parseKind(value) {
|
|
120
124
|
if (cliArtifactKinds.includes(value)) {
|
|
121
125
|
return value;
|
|
122
126
|
}
|
|
123
|
-
throw
|
|
127
|
+
throw usageError(`Invalid kind "${value}". Expected one of: ${cliArtifactKinds.join(', ')}`);
|
|
124
128
|
}
|
|
125
129
|
function parseAnnotationStatus(value) {
|
|
126
130
|
if (cliAnnotationStatusOptions.includes(value)) {
|
|
127
131
|
return value;
|
|
128
132
|
}
|
|
129
|
-
throw
|
|
133
|
+
throw usageError(`Invalid status "${value}". Expected one of: ${cliAnnotationStatusOptions.join(', ')}`);
|
|
130
134
|
}
|
|
131
135
|
function parseShareRole(value) {
|
|
132
136
|
if (cliShareRoleOptions.includes(value)) {
|
|
133
137
|
return value;
|
|
134
138
|
}
|
|
135
|
-
throw
|
|
139
|
+
throw usageError(`Invalid role "${value}". Expected one of: ${cliShareRoleOptions.join(', ')}`);
|
|
136
140
|
}
|
|
137
141
|
/** Parse a `--rect x,y,w,h` percentage tuple into a Media Fragments
|
|
138
142
|
* `xywh=percent:` FragmentSelector. Width/height default to 0 (a point pin). */
|
|
@@ -143,7 +147,7 @@ function parseRectSelector(value) {
|
|
|
143
147
|
const parts = value.split(',').map((part) => Number(part.trim()));
|
|
144
148
|
if ((parts.length !== 2 && parts.length !== 4) ||
|
|
145
149
|
parts.some((part) => !Number.isFinite(part))) {
|
|
146
|
-
throw
|
|
150
|
+
throw usageError(`Invalid --rect "${value}". Expected "x,y" or "x,y,w,h" percentages.`);
|
|
147
151
|
}
|
|
148
152
|
const [x, y, width = 0, height = 0] = parts;
|
|
149
153
|
return {
|
|
@@ -179,7 +183,7 @@ function buildAnnotationSelectors(args) {
|
|
|
179
183
|
const start = Number(textStart);
|
|
180
184
|
const end = Number(textEnd);
|
|
181
185
|
if (!Number.isFinite(start) || !Number.isFinite(end)) {
|
|
182
|
-
throw
|
|
186
|
+
throw usageError('--text-start and --text-end must be numbers.');
|
|
183
187
|
}
|
|
184
188
|
selectors.push({ type: 'TextPositionSelector', start, end });
|
|
185
189
|
}
|
|
@@ -225,7 +229,7 @@ function resolveCredentials(args) {
|
|
|
225
229
|
const baseUrl = resolveBaseUrl(args, config);
|
|
226
230
|
const { apiKey } = resolveApiKey(args, config, baseUrl);
|
|
227
231
|
if (!apiKey) {
|
|
228
|
-
throw new
|
|
232
|
+
throw new CliError(`Missing API key. Run "artis login --host ${baseUrl}" or set ARTIS_API_KEY.`, exitCodes.auth);
|
|
229
233
|
}
|
|
230
234
|
return { apiKey, baseUrl };
|
|
231
235
|
});
|
|
@@ -251,7 +255,7 @@ export function guidelinesFromCli(args) {
|
|
|
251
255
|
const response = yield fetch(`${baseUrl}/skills.md?section=${encodeURIComponent(section !== null && section !== void 0 ? section : 'index')}`);
|
|
252
256
|
const text = yield response.text();
|
|
253
257
|
if (!response.ok) {
|
|
254
|
-
throw new
|
|
258
|
+
throw new CliError(text.trim() || `Guidelines request failed with ${response.status}.`, exitCodeForResponse(response.status));
|
|
255
259
|
}
|
|
256
260
|
process.stdout.write(text.endsWith('\n') ? text : `${text}\n`);
|
|
257
261
|
});
|
|
@@ -261,7 +265,7 @@ function parsePublishOptions(args) {
|
|
|
261
265
|
var _a, _b, _c, _d;
|
|
262
266
|
const [filePath] = getPositionals(args);
|
|
263
267
|
if (!filePath) {
|
|
264
|
-
throw
|
|
268
|
+
throw usageError('Missing artifact HTML file path.');
|
|
265
269
|
}
|
|
266
270
|
const credentials = yield resolveCredentials(args);
|
|
267
271
|
const visibility = parseVisibility((_a = parseFlagValue(args, '--visibility')) !== null && _a !== void 0 ? _a : 'unlisted');
|
|
@@ -291,9 +295,9 @@ function apiFetch(credentials_1, path_1) {
|
|
|
291
295
|
if (!response.ok) {
|
|
292
296
|
const message = (_a = payload === null || payload === void 0 ? void 0 : payload.error) !== null && _a !== void 0 ? _a : `Request failed with ${response.status}.`;
|
|
293
297
|
// A revise conflict is only recoverable with the current revision id.
|
|
294
|
-
throw new
|
|
298
|
+
throw new CliError((payload === null || payload === void 0 ? void 0 : payload.currentRevisionId)
|
|
295
299
|
? `${message} Current revision: ${payload.currentRevisionId}.`
|
|
296
|
-
: message);
|
|
300
|
+
: message, exitCodeForResponse(response.status, payload === null || payload === void 0 ? void 0 : payload.code));
|
|
297
301
|
}
|
|
298
302
|
return payload;
|
|
299
303
|
});
|
|
@@ -319,7 +323,7 @@ function apiFetchOptional(target_1, path_1) {
|
|
|
319
323
|
status = retryResponse.status;
|
|
320
324
|
}
|
|
321
325
|
if (status < 200 || status >= 300) {
|
|
322
|
-
throw new
|
|
326
|
+
throw new CliError((_a = payload === null || payload === void 0 ? void 0 : payload.error) !== null && _a !== void 0 ? _a : `Request failed with ${status}.`, exitCodeForResponse(status, payload === null || payload === void 0 ? void 0 : payload.code));
|
|
323
327
|
}
|
|
324
328
|
return payload;
|
|
325
329
|
});
|
|
@@ -405,6 +409,7 @@ Usage:
|
|
|
405
409
|
artis reply <annotation-id> --comment <text>
|
|
406
410
|
artis annotation edit <annotation-id> [--comment <text>] [--title <text>] [--requested-change <text>]
|
|
407
411
|
artis annotation retract <annotation-id>
|
|
412
|
+
artis watch <artifact-id-or-slug> [--since <cursor>] [--once] [--timeout <duration>] [--include-self]
|
|
408
413
|
|
|
409
414
|
Options:
|
|
410
415
|
--api-key <key> API key. Defaults to ARTIS_API_KEY or ~/.artis/config.json. Run "artis login" to obtain one.
|
|
@@ -438,8 +443,15 @@ Options:
|
|
|
438
443
|
--dir <path> Install the skill into a custom skills directory (install-skill).
|
|
439
444
|
--force Overwrite an already-installed skill (install-skill).
|
|
440
445
|
--list List harnesses, detection, and install paths (install-skill).
|
|
446
|
+
--since <cursor> Feedback cursor to watch from (watch). Defaults to now; 0 replays every event.
|
|
447
|
+
--once Exit after the first batch of events, printed as {events, cursor} (watch).
|
|
448
|
+
--timeout <duration> Give up after this long, e.g. 30s, 10m (watch). Exits 6 if nothing happened.
|
|
449
|
+
--include-self Also report events caused by this API key (watch).
|
|
441
450
|
--json Print machine-readable JSON for supported commands.
|
|
442
451
|
|
|
452
|
+
Exit codes:
|
|
453
|
+
0 success, 1 error, 2 usage, 3 auth, 4 not found, 5 revision conflict, 6 watch timeout, 7 rate limited.
|
|
454
|
+
|
|
443
455
|
Examples (--host is optional; without it commands target https://artis.xiispace.eu.org):
|
|
444
456
|
artis publish report.html
|
|
445
457
|
artis login
|
|
@@ -452,6 +464,7 @@ Examples (--host is optional; without it commands target https://artis.xiispace.
|
|
|
452
464
|
artis reply annotation-123 --host https://artis.example.com --comment "Addressed in the latest revision"
|
|
453
465
|
artis annotation edit annotation-123 --host https://artis.example.com --comment "Corrected: the latency spike is on checkout, not search"
|
|
454
466
|
artis annotation retract annotation-123 --host https://artis.example.com
|
|
467
|
+
artis watch artifact-slug --since 42
|
|
455
468
|
artis guidelines authoring
|
|
456
469
|
artis install-skill --yes
|
|
457
470
|
artis revise artifact-slug report.html --host https://artis.example.com --summary "Addressed open feedback" --base revision-abc --resolve annotation-123`;
|
|
@@ -505,7 +518,7 @@ export function loginFromCli(args) {
|
|
|
505
518
|
!login.userCode ||
|
|
506
519
|
!login.verificationUrl ||
|
|
507
520
|
!login.expiresAt) {
|
|
508
|
-
throw new
|
|
521
|
+
throw new CliError((_a = login === null || login === void 0 ? void 0 : login.error) !== null && _a !== void 0 ? _a : `Login failed with ${response.status}.`, exitCodeForResponse(response.status));
|
|
509
522
|
}
|
|
510
523
|
// Validate the host-controlled verification URL before opening a browser.
|
|
511
524
|
// A non-default or compromised host could otherwise return a crafted URL
|
|
@@ -559,7 +572,7 @@ export function getArtifactFromCli(args) {
|
|
|
559
572
|
var _a;
|
|
560
573
|
const [artifactId] = getPositionals(args);
|
|
561
574
|
if (!artifactId) {
|
|
562
|
-
throw
|
|
575
|
+
throw usageError('Missing artifact id or slug.');
|
|
563
576
|
}
|
|
564
577
|
const target = yield resolveOptionalTarget(args);
|
|
565
578
|
const result = yield apiFetchOptional(target, `/api/artifacts/${encodeURIComponent(artifactId)}`);
|
|
@@ -585,7 +598,7 @@ export function getArtifactHtmlFromCli(args) {
|
|
|
585
598
|
return __awaiter(this, void 0, void 0, function* () {
|
|
586
599
|
const [artifactId] = getPositionals(args);
|
|
587
600
|
if (!artifactId) {
|
|
588
|
-
throw
|
|
601
|
+
throw usageError('Usage: artis artifact html <artifact-id-or-slug> [--output <path>] [--revision <revision-id>]');
|
|
589
602
|
}
|
|
590
603
|
const target = yield resolveOptionalTarget(args);
|
|
591
604
|
const revisionId = parseFlagValue(args, '--revision');
|
|
@@ -622,7 +635,7 @@ export function getArtifactFeedbackFromCli(args) {
|
|
|
622
635
|
return __awaiter(this, void 0, void 0, function* () {
|
|
623
636
|
const [artifactId] = getPositionals(args);
|
|
624
637
|
if (!artifactId) {
|
|
625
|
-
throw
|
|
638
|
+
throw usageError('Missing artifact id or slug.');
|
|
626
639
|
}
|
|
627
640
|
const target = yield resolveOptionalTarget(args);
|
|
628
641
|
const revisionId = parseFlagValue(args, '--revision');
|
|
@@ -638,6 +651,9 @@ export function getArtifactFeedbackFromCli(args) {
|
|
|
638
651
|
throw new Error('Feedback response did not include a prompt.');
|
|
639
652
|
}
|
|
640
653
|
console.log(result.prompt);
|
|
654
|
+
if (result.cursor !== undefined) {
|
|
655
|
+
console.log(`\nFeedback cursor: ${result.cursor} (artis watch ${artifactId} --since ${result.cursor})`);
|
|
656
|
+
}
|
|
641
657
|
});
|
|
642
658
|
}
|
|
643
659
|
export function shareArtifactFromCli(args) {
|
|
@@ -645,7 +661,7 @@ export function shareArtifactFromCli(args) {
|
|
|
645
661
|
var _a, _b, _c;
|
|
646
662
|
const [artifactId] = getPositionals(args);
|
|
647
663
|
if (!artifactId) {
|
|
648
|
-
throw
|
|
664
|
+
throw usageError('Usage: artis share <artifact-id-or-slug> [--email <email> --role <viewer|commenter>] [--remove]');
|
|
649
665
|
}
|
|
650
666
|
const email = (_a = parseFlagValue(args, '--email')) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
|
|
651
667
|
const credentials = yield resolveCredentials(args);
|
|
@@ -695,10 +711,10 @@ export function reviseArtifactFromCli(args) {
|
|
|
695
711
|
const [artifactId, filePath] = getPositionals(args);
|
|
696
712
|
const summary = parseFlagValue(args, '--summary');
|
|
697
713
|
if (!artifactId || !filePath) {
|
|
698
|
-
throw
|
|
714
|
+
throw usageError('Usage: artis revise <artifact-id-or-slug> artifact.html --summary <text> [--base <revision-id>]');
|
|
699
715
|
}
|
|
700
716
|
if (!summary) {
|
|
701
|
-
throw
|
|
717
|
+
throw usageError('Missing --summary for revision.');
|
|
702
718
|
}
|
|
703
719
|
const credentials = yield resolveCredentials(args);
|
|
704
720
|
const html = yield readFile(filePath, 'utf8');
|
|
@@ -744,7 +760,7 @@ export function createAnnotationFromCli(args) {
|
|
|
744
760
|
const [artifactId] = getPositionals(args);
|
|
745
761
|
const comment = parseFlagValue(args, '--comment');
|
|
746
762
|
if (!artifactId || !comment) {
|
|
747
|
-
throw
|
|
763
|
+
throw usageError('Usage: artis annotate <artifact-id-or-slug> --comment <text> [--title <text>] [--quote <text>] [--css <selector>] [--text-start <n> --text-end <n>] [--rect <x,y,w,h>] [--pin-label <text>] [--requested-change <text>] [--status <open|needs-revision|resolved>]');
|
|
748
764
|
}
|
|
749
765
|
const selectors = buildAnnotationSelectors(args);
|
|
750
766
|
const credentials = yield resolveCredentials(args);
|
|
@@ -774,7 +790,7 @@ export function replyFromCli(args) {
|
|
|
774
790
|
const [annotationId] = getPositionals(args);
|
|
775
791
|
const comment = parseFlagValue(args, '--comment');
|
|
776
792
|
if (!annotationId || !comment) {
|
|
777
|
-
throw
|
|
793
|
+
throw usageError('Usage: artis reply <annotation-id> --comment <text>');
|
|
778
794
|
}
|
|
779
795
|
const credentials = yield resolveCredentials(args);
|
|
780
796
|
const result = yield apiFetch(credentials, `/api/annotations/${encodeURIComponent(annotationId)}/replies`, {
|
|
@@ -801,7 +817,7 @@ export function editAnnotationFromCli(args) {
|
|
|
801
817
|
(comment === undefined &&
|
|
802
818
|
title === undefined &&
|
|
803
819
|
requestedChange === undefined)) {
|
|
804
|
-
throw
|
|
820
|
+
throw usageError('Usage: artis annotation edit <annotation-id> [--comment <text>] [--title <text>] [--requested-change <text>]');
|
|
805
821
|
}
|
|
806
822
|
const credentials = yield resolveCredentials(args);
|
|
807
823
|
const result = yield apiFetch(credentials, `/api/annotations/${encodeURIComponent(annotationId)}`, {
|
|
@@ -818,7 +834,7 @@ export function retractAnnotationFromCli(args) {
|
|
|
818
834
|
return __awaiter(this, void 0, void 0, function* () {
|
|
819
835
|
const [annotationId] = getPositionals(args);
|
|
820
836
|
if (!annotationId) {
|
|
821
|
-
throw
|
|
837
|
+
throw usageError('Usage: artis annotation retract <annotation-id>');
|
|
822
838
|
}
|
|
823
839
|
const credentials = yield resolveCredentials(args);
|
|
824
840
|
const result = yield apiFetch(credentials, `/api/annotations/${encodeURIComponent(annotationId)}`, {
|
|
@@ -830,6 +846,124 @@ export function retractAnnotationFromCli(args) {
|
|
|
830
846
|
console.log(`Annotation ${annotationIdFromIri(result.annotation.id)} retracted`);
|
|
831
847
|
});
|
|
832
848
|
}
|
|
849
|
+
const watchMaxWaitSeconds = 25;
|
|
850
|
+
const watchInitialBackoffMs = 1000;
|
|
851
|
+
const watchMaxBackoffMs = 60000;
|
|
852
|
+
/** `30`, `30s`, `500ms`, `5m`, `1h` → milliseconds. A bare number is seconds. */
|
|
853
|
+
function parseDuration(value) {
|
|
854
|
+
var _a;
|
|
855
|
+
const match = /^(\d+(?:\.\d+)?)(ms|s|m|h)?$/.exec(value.trim());
|
|
856
|
+
if (!match) {
|
|
857
|
+
throw usageError(`Invalid duration "${value}". Use e.g. 30s, 5m, 1h, or 500ms.`);
|
|
858
|
+
}
|
|
859
|
+
const unitMs = { ms: 1, s: 1000, m: 60000, h: 3600000 }[((_a = match[2]) !== null && _a !== void 0 ? _a : 's')];
|
|
860
|
+
return Number(match[1]) * unitMs;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Wait for annotation events on one artifact caused by anyone but this API
|
|
864
|
+
* key, by long-polling `GET /api/artifacts/:id/events` from a feedback
|
|
865
|
+
* cursor. Streams one JSON line per event until stopped; `--once` prints the
|
|
866
|
+
* first batch as `{events, cursor}` and exits 0. Network drops and rate
|
|
867
|
+
* limits are retried from the last cursor, so no event is lost or repeated.
|
|
868
|
+
*/
|
|
869
|
+
export function watchArtifactFromCli(args) {
|
|
870
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
871
|
+
var _a, _b, _c;
|
|
872
|
+
const [artifactId] = getPositionals(args);
|
|
873
|
+
if (!artifactId) {
|
|
874
|
+
throw usageError('Usage: artis watch <artifact-id-or-slug> [--since <cursor>] [--once] [--timeout <duration>] [--include-self]');
|
|
875
|
+
}
|
|
876
|
+
let cursor = parseFlagValue(args, '--since');
|
|
877
|
+
if (cursor !== undefined && !/^\d+$/.test(cursor)) {
|
|
878
|
+
throw usageError(`Invalid --since "${cursor}". Pass a cursor from artifact feedback or a previous event.`);
|
|
879
|
+
}
|
|
880
|
+
const timeoutFlag = parseFlagValue(args, '--timeout');
|
|
881
|
+
const deadline = timeoutFlag !== undefined
|
|
882
|
+
? Date.now() + parseDuration(timeoutFlag)
|
|
883
|
+
: undefined;
|
|
884
|
+
const once = hasFlag(args, '--once');
|
|
885
|
+
const includeSelf = hasFlag(args, '--include-self');
|
|
886
|
+
const credentials = yield resolveCredentials(args);
|
|
887
|
+
let backoffMs = watchInitialBackoffMs;
|
|
888
|
+
let delivered = false;
|
|
889
|
+
const timedOut = () => new CliError(`No feedback events within ${timeoutFlag}.${cursor ? ` Resume with --since ${cursor}.` : ''}`, exitCodes.watchTimeout);
|
|
890
|
+
// Sleep before retrying a transient failure. Returns false when the
|
|
891
|
+
// deadline would pass first and the stream already delivered (a clean end).
|
|
892
|
+
const backOff = () => __awaiter(this, void 0, void 0, function* () {
|
|
893
|
+
const remaining = deadline === undefined ? Infinity : deadline - Date.now();
|
|
894
|
+
if (remaining <= backoffMs) {
|
|
895
|
+
if (delivered) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
throw timedOut();
|
|
899
|
+
}
|
|
900
|
+
yield sleep(backoffMs);
|
|
901
|
+
backoffMs = Math.min(backoffMs * 2, watchMaxBackoffMs);
|
|
902
|
+
return true;
|
|
903
|
+
});
|
|
904
|
+
for (let attempt = 0;; attempt += 1) {
|
|
905
|
+
const remainingMs = deadline === undefined ? Infinity : deadline - Date.now();
|
|
906
|
+
// The first request always goes out, so a short --timeout still reports
|
|
907
|
+
// auth or not-found errors. After that, under a second left would only
|
|
908
|
+
// produce wait=0 requests in a tight loop.
|
|
909
|
+
if (attempt > 0 && remainingMs < 1000) {
|
|
910
|
+
// Exit 6 means "nothing happened"; a stream that delivered ends cleanly.
|
|
911
|
+
if (delivered) {
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
throw timedOut();
|
|
915
|
+
}
|
|
916
|
+
const query = new URLSearchParams({
|
|
917
|
+
wait: String(Math.min(watchMaxWaitSeconds, Math.floor(remainingMs / 1000))),
|
|
918
|
+
});
|
|
919
|
+
if (cursor !== undefined) {
|
|
920
|
+
query.set('since', cursor);
|
|
921
|
+
}
|
|
922
|
+
if (includeSelf) {
|
|
923
|
+
query.set('includeSelf', 'true');
|
|
924
|
+
}
|
|
925
|
+
let response;
|
|
926
|
+
try {
|
|
927
|
+
response = yield fetch(`${credentials.baseUrl}/api/artifacts/${encodeURIComponent(artifactId)}/events?${query}`, { headers: { authorization: `Bearer ${credentials.apiKey}` } });
|
|
928
|
+
}
|
|
929
|
+
catch (_d) {
|
|
930
|
+
// Network drop: reconnect from the last cursor.
|
|
931
|
+
if (yield backOff()) {
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const payload = yield readJsonResponse(response);
|
|
937
|
+
// Rate limits and server errors are transient for a long-running stream.
|
|
938
|
+
if ((response.status === 429 || response.status >= 500) && !once) {
|
|
939
|
+
if (yield backOff()) {
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
if (!response.ok) {
|
|
945
|
+
const message = (_a = payload === null || payload === void 0 ? void 0 : payload.error) !== null && _a !== void 0 ? _a : `Watch request failed with ${response.status}.`;
|
|
946
|
+
throw new CliError(response.status === 410
|
|
947
|
+
? `${message} Re-read feedback (artis artifact feedback ${artifactId} --json) and watch from its cursor.`
|
|
948
|
+
: message, exitCodeForResponse(response.status, payload === null || payload === void 0 ? void 0 : payload.code));
|
|
949
|
+
}
|
|
950
|
+
backoffMs = watchInitialBackoffMs;
|
|
951
|
+
const events = (_b = payload === null || payload === void 0 ? void 0 : payload.events) !== null && _b !== void 0 ? _b : [];
|
|
952
|
+
cursor = (_c = payload === null || payload === void 0 ? void 0 : payload.cursor) !== null && _c !== void 0 ? _c : cursor;
|
|
953
|
+
if (events.length === 0) {
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
if (once) {
|
|
957
|
+
console.log(JSON.stringify({ events, cursor }));
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
for (const event of events) {
|
|
961
|
+
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
962
|
+
}
|
|
963
|
+
delivered = true;
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
}
|
|
833
967
|
export function runArtisCli() {
|
|
834
968
|
return __awaiter(this, arguments, void 0, function* (args = process.argv.slice(2)) {
|
|
835
969
|
const [command, ...rest] = args;
|
|
@@ -894,6 +1028,10 @@ export function runArtisCli() {
|
|
|
894
1028
|
yield createAnnotationFromCli(rest);
|
|
895
1029
|
return;
|
|
896
1030
|
}
|
|
1031
|
+
if (command === 'watch') {
|
|
1032
|
+
yield watchArtifactFromCli(rest);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
897
1035
|
if (command === 'reply') {
|
|
898
1036
|
yield replyFromCli(rest);
|
|
899
1037
|
return;
|
|
@@ -906,7 +1044,7 @@ export function runArtisCli() {
|
|
|
906
1044
|
yield retractAnnotationFromCli(rest.slice(1));
|
|
907
1045
|
return;
|
|
908
1046
|
}
|
|
909
|
-
throw
|
|
1047
|
+
throw usageError(`Unknown command "${args.join(' ')}". Run artis help.`);
|
|
910
1048
|
});
|
|
911
1049
|
}
|
|
912
1050
|
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CLI's process exit codes, one table for every command so an agent can
|
|
3
|
+
* branch on the outcome without parsing messages. Documented in the `cli`
|
|
4
|
+
* guidelines section; keep the two in step.
|
|
5
|
+
*/
|
|
6
|
+
export declare const exitCodes: {
|
|
7
|
+
readonly ok: 0;
|
|
8
|
+
readonly error: 1;
|
|
9
|
+
readonly usage: 2;
|
|
10
|
+
readonly auth: 3;
|
|
11
|
+
readonly notFound: 4;
|
|
12
|
+
readonly revisionConflict: 5;
|
|
13
|
+
readonly watchTimeout: 6;
|
|
14
|
+
readonly rateLimited: 7;
|
|
15
|
+
};
|
|
16
|
+
export type ExitCode = (typeof exitCodes)[keyof typeof exitCodes];
|
|
17
|
+
/** An error that knows which exit code it should end the process with. */
|
|
18
|
+
export declare class CliError extends Error {
|
|
19
|
+
readonly exitCode: ExitCode;
|
|
20
|
+
constructor(message: string, exitCode: ExitCode);
|
|
21
|
+
}
|
|
22
|
+
/** Bad or missing arguments: exit 2. */
|
|
23
|
+
export declare function usageError(message: string): CliError;
|
|
24
|
+
/** Exit code for a failed API response. */
|
|
25
|
+
export declare function exitCodeForResponse(status: number, code?: string): ExitCode;
|
|
26
|
+
export declare function exitCodeFor(error: unknown): ExitCode;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CLI's process exit codes, one table for every command so an agent can
|
|
3
|
+
* branch on the outcome without parsing messages. Documented in the `cli`
|
|
4
|
+
* guidelines section; keep the two in step.
|
|
5
|
+
*/
|
|
6
|
+
export const exitCodes = {
|
|
7
|
+
ok: 0,
|
|
8
|
+
error: 1,
|
|
9
|
+
usage: 2,
|
|
10
|
+
auth: 3,
|
|
11
|
+
notFound: 4,
|
|
12
|
+
revisionConflict: 5,
|
|
13
|
+
watchTimeout: 6,
|
|
14
|
+
rateLimited: 7,
|
|
15
|
+
};
|
|
16
|
+
/** An error that knows which exit code it should end the process with. */
|
|
17
|
+
export class CliError extends Error {
|
|
18
|
+
constructor(message, exitCode) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'CliError';
|
|
21
|
+
this.exitCode = exitCode;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Bad or missing arguments: exit 2. */
|
|
25
|
+
export function usageError(message) {
|
|
26
|
+
return new CliError(message, exitCodes.usage);
|
|
27
|
+
}
|
|
28
|
+
/** Exit code for a failed API response. */
|
|
29
|
+
export function exitCodeForResponse(status, code) {
|
|
30
|
+
if (status === 401 || status === 403) {
|
|
31
|
+
return exitCodes.auth;
|
|
32
|
+
}
|
|
33
|
+
if (status === 404) {
|
|
34
|
+
return exitCodes.notFound;
|
|
35
|
+
}
|
|
36
|
+
if (status === 409 && code === 'revision_conflict') {
|
|
37
|
+
return exitCodes.revisionConflict;
|
|
38
|
+
}
|
|
39
|
+
if (status === 429) {
|
|
40
|
+
return exitCodes.rateLimited;
|
|
41
|
+
}
|
|
42
|
+
return exitCodes.error;
|
|
43
|
+
}
|
|
44
|
+
export function exitCodeFor(error) {
|
|
45
|
+
return error instanceof CliError ? error.exitCode : exitCodes.error;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=exit-codes.js.map
|