@zinn-dev/cli 0.40.0 → 0.42.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 +11 -561
- package/index.js +24 -13
- package/package.json +8 -2
- package/skills/zinn/SKILL.md +63 -0
- package/src/actor.d.ts +2 -1
- package/src/actor.js +78 -11
- package/src/capabilities-schema.d.ts +162 -0
- package/src/capabilities-schema.js +59 -0
- package/src/capabilities.d.ts +29 -0
- package/src/capabilities.js +30 -0
- package/src/column-hint.d.ts +3 -0
- package/src/column-hint.js +225 -0
- package/src/database.d.ts +1 -1
- package/src/database.js +5 -8
- package/src/errors.d.ts +158 -0
- package/src/errors.js +226 -0
- package/src/input.d.ts +227 -0
- package/src/input.js +192 -0
- package/src/lib.d.ts +71 -8
- package/src/lib.js +275 -18
- package/src/presentation/activity-presentation.d.ts +9 -1
- package/src/presentation/activity-presentation.js +120 -23
- package/src/presentation/capabilities-presentation.d.ts +2 -0
- package/src/presentation/capabilities-presentation.js +3 -0
- package/src/presentation/column-presentation.d.ts +9 -5
- package/src/presentation/column-presentation.js +10 -0
- package/src/presentation/comment-presentation.d.ts +15 -0
- package/src/presentation/comment-presentation.js +29 -0
- package/src/presentation/description-presentation.d.ts +17 -0
- package/src/presentation/description-presentation.js +26 -4
- package/src/presentation/info-presentation.d.ts +4 -0
- package/src/presentation/info-presentation.js +96 -0
- package/src/presentation/label-presentation.d.ts +8 -0
- package/src/presentation/label-presentation.js +28 -0
- package/src/presentation/project-presentation.d.ts +7 -0
- package/src/presentation/project-presentation.js +19 -0
- package/src/presentation/relation-presentation.d.ts +7 -0
- package/src/presentation/relation-presentation.js +73 -0
- package/src/presentation/task-presentation.d.ts +4 -12
- package/src/presentation/task-presentation.js +22 -18
- package/src/project.d.ts +18 -3
- package/src/project.js +37 -12
- package/src/result-schemas.d.ts +1194 -0
- package/src/result-schemas.js +279 -0
- package/src/results.d.ts +40 -0
- package/src/results.js +36 -0
- package/src/routes/activity-router.d.ts +104 -0
- package/src/routes/activity-router.js +155 -34
- package/src/routes/aliases.d.ts +2 -0
- package/src/routes/aliases.js +11 -0
- package/src/routes/backup-router.d.ts +17 -0
- package/src/routes/backup-router.js +15 -3
- package/src/routes/capabilities-router.d.ts +21 -0
- package/src/routes/capabilities-router.js +34 -0
- package/src/routes/comment-router.d.ts +210 -8
- package/src/routes/comment-router.js +189 -65
- package/src/routes/info-router.d.ts +21 -0
- package/src/routes/info-router.js +29 -0
- package/src/routes/label-router.d.ts +217 -2
- package/src/routes/label-router.js +189 -62
- package/src/routes/migrate-router.d.ts +17 -0
- package/src/routes/migrate-router.js +17 -5
- package/src/routes/project-router.d.ts +373 -2
- package/src/routes/project-router.js +322 -132
- package/src/routes/reindex-router.d.ts +17 -0
- package/src/routes/reindex-router.js +15 -3
- package/src/routes/relation-router.d.ts +110 -14
- package/src/routes/relation-router.js +122 -79
- package/src/routes/restore-router.d.ts +29 -0
- package/src/routes/restore-router.js +31 -14
- package/src/routes/router.d.ts +1 -1
- package/src/routes/router.js +80 -32
- package/src/routes/routes.d.ts +1781 -8
- package/src/routes/routes.js +9 -1
- package/src/routes/task-router.d.ts +946 -4
- package/src/routes/task-router.js +524 -260
- package/src/routes/whoami-router.d.ts +21 -0
- package/src/routes/whoami-router.js +31 -0
- package/src/text-edit.d.ts +28 -6
- package/src/text-edit.js +40 -23
- package/src/text-input.d.ts +47 -0
- package/src/text-input.js +187 -0
- package/src/types.d.ts +37 -0
- package/src/types.js +9 -1
package/src/lib.js
CHANGED
|
@@ -1,15 +1,48 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
1
2
|
import { createInterface } from "node:readline/promises";
|
|
2
|
-
import {
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { assertWritable, InputValidationError, ReadOnlyDatabaseError, TextEditMatchError, UnmarkedDatabaseError, } from "@zinn-dev/core";
|
|
5
|
+
import { CliError, classifyFailure } from "./errors.js";
|
|
6
|
+
import { helpSpellings, routerOptionNames, positionalWords } from "./input.js";
|
|
3
7
|
let outputMode = "text";
|
|
4
8
|
let legacyReadAllowed = false;
|
|
9
|
+
const diagnostics = [];
|
|
10
|
+
const DOUBLE_DASH = "--";
|
|
5
11
|
export function setOutputMode(mode) {
|
|
6
12
|
outputMode = mode;
|
|
7
13
|
}
|
|
14
|
+
export function getOutputMode() {
|
|
15
|
+
return outputMode;
|
|
16
|
+
}
|
|
17
|
+
/** Selects output before routing, without treating words after `--` as flags. */
|
|
18
|
+
export function prepareOutput(args) {
|
|
19
|
+
const separatorIndex = args.indexOf(DOUBLE_DASH);
|
|
20
|
+
const options = separatorIndex === -1 ? args : args.slice(0, separatorIndex);
|
|
21
|
+
setOutputMode(options.some(isJsonFlag) ? "json" : "text");
|
|
22
|
+
}
|
|
23
|
+
/** Keeps JSON failures parseable while text-mode notices remain immediate. */
|
|
24
|
+
export function diagnostic(message) {
|
|
25
|
+
if (outputMode === "json") {
|
|
26
|
+
diagnostics.push(message);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
console.error(message);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function flushDiagnostics() {
|
|
33
|
+
for (const message of diagnostics.splice(0)) {
|
|
34
|
+
console.error(message);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
8
37
|
export function isJsonFlag(flag) {
|
|
9
|
-
return flag ===
|
|
38
|
+
return flag === `--${routerOptionNames.json}`;
|
|
10
39
|
}
|
|
11
40
|
export function isAllowLegacyReadFlag(flag) {
|
|
12
|
-
return flag ===
|
|
41
|
+
return flag === `--${routerOptionNames.legacyRead}`;
|
|
42
|
+
}
|
|
43
|
+
/** A flag the router reads wherever it appears before --, for every command. */
|
|
44
|
+
export function isRouterFlag(flag) {
|
|
45
|
+
return isJsonFlag(flag) || isAllowLegacyReadFlag(flag);
|
|
13
46
|
}
|
|
14
47
|
export function setLegacyReadAllowed(allowed) {
|
|
15
48
|
legacyReadAllowed = allowed;
|
|
@@ -27,23 +60,42 @@ export function output(data, transformToString) {
|
|
|
27
60
|
console.info(dataText);
|
|
28
61
|
}
|
|
29
62
|
}
|
|
63
|
+
/** Includes change data in JSON and adds a stderr note for each unchanged object */
|
|
64
|
+
export function outputChanges(changes, present, formatEntries, describeUnchanged) {
|
|
65
|
+
const presentedChanges = changes.map((change) => ({
|
|
66
|
+
...present(change),
|
|
67
|
+
changed: change.changed,
|
|
68
|
+
}));
|
|
69
|
+
if (presentedChanges.length === 1) {
|
|
70
|
+
output(presentedChanges[0], (entry) => formatEntries([entry]));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
output(presentedChanges, formatEntries);
|
|
74
|
+
}
|
|
75
|
+
for (const entry of presentedChanges) {
|
|
76
|
+
if (!entry.changed) {
|
|
77
|
+
diagnostic(describeUnchanged(entry));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
30
81
|
/** Asks before a change that cannot be undone, unless --yes already confirmed it. */
|
|
31
82
|
export async function confirmAction(message, isPreconfirmed, jsonRefusal) {
|
|
32
83
|
// Throws on a read-only database, to avoid asking for confirmation to an already refused action
|
|
33
84
|
assertWritable();
|
|
34
85
|
return askConfirmation(message, isPreconfirmed, jsonRefusal);
|
|
35
86
|
}
|
|
87
|
+
export const confirmationDescription = "Requires --yes with --json or without a terminal. Otherwise prompts on stderr and only y or Y confirms. Declining exits successfully without the requested mutation; interruption exits 130.";
|
|
36
88
|
/** Like confirmAction, without opening the database, for commands that bypass the open policy. */
|
|
37
89
|
export async function askConfirmation(message, isPreconfirmed, jsonRefusal = "Commands that ask for confirmation need --yes when used with --json") {
|
|
38
90
|
if (isPreconfirmed) {
|
|
39
91
|
return true;
|
|
40
92
|
}
|
|
41
93
|
if (outputMode === "json") {
|
|
42
|
-
quit(jsonRefusal);
|
|
94
|
+
quit(jsonRefusal, undefined, { argument: "--yes", reason: "confirmation_required" });
|
|
43
95
|
}
|
|
44
96
|
// Refuses rather than treating an unanswered prompt as declined, so scripts do not see a false success
|
|
45
97
|
if (!canPrompt()) {
|
|
46
|
-
quit("Commands that ask for confirmation need --yes when not running in a terminal");
|
|
98
|
+
quit("Commands that ask for confirmation need --yes when not running in a terminal", undefined, { argument: "--yes", reason: "confirmation_required" });
|
|
47
99
|
}
|
|
48
100
|
const prompt = createInterface({ input: process.stdin, output: process.stderr });
|
|
49
101
|
const closed = new AbortController();
|
|
@@ -73,6 +125,9 @@ export function canPrompt() {
|
|
|
73
125
|
}
|
|
74
126
|
/** The message to report for a failed command, or fallback when the error has none. */
|
|
75
127
|
export function failureMessage(error, fallback) {
|
|
128
|
+
if (error instanceof z.ZodError) {
|
|
129
|
+
return new InputValidationError(error.issues).message;
|
|
130
|
+
}
|
|
76
131
|
const isOlderReadOnly = error instanceof ReadOnlyDatabaseError && error.reason === "older";
|
|
77
132
|
if (isOlderReadOnly) {
|
|
78
133
|
return `${error.message}. Run \`zinn migrate\` to upgrade it.`;
|
|
@@ -86,40 +141,70 @@ export function failureMessage(error, fallback) {
|
|
|
86
141
|
if (error?.message === "no such table: task_search") {
|
|
87
142
|
return `${error.message}. Run \`zinn reindex\` to rebuild the search index.`;
|
|
88
143
|
}
|
|
89
|
-
return error?.message
|
|
144
|
+
return typeof error?.message === "string" ? error.message : fallback;
|
|
90
145
|
}
|
|
91
|
-
/**
|
|
92
|
-
export function quit(reason, help) {
|
|
146
|
+
/** Stops a command while retaining its classified error for the outer renderer. */
|
|
147
|
+
export function quit(reason, help, details) {
|
|
148
|
+
if (reason instanceof Error) {
|
|
149
|
+
throw reason;
|
|
150
|
+
}
|
|
151
|
+
throw new CliError(reason, "invalid_input", details, help);
|
|
152
|
+
}
|
|
153
|
+
/** Emits one handled failure, including any notices collected before it. */
|
|
154
|
+
export function renderFailure(reason) {
|
|
155
|
+
const failure = {
|
|
156
|
+
...classifyFailure(reason),
|
|
157
|
+
message: failureMessage(reason, "Something went wrong"),
|
|
158
|
+
};
|
|
159
|
+
const pendingDiagnostics = diagnostics.splice(0);
|
|
93
160
|
if (outputMode === "json") {
|
|
94
|
-
const
|
|
161
|
+
const details = failure.details === undefined
|
|
162
|
+
? undefined
|
|
163
|
+
: Object.fromEntries(Object.entries(failure.details).filter(([, value]) => value !== undefined));
|
|
164
|
+
const error = {
|
|
165
|
+
...failure,
|
|
166
|
+
details: details !== undefined && Object.keys(details).length > 0 ? details : undefined,
|
|
167
|
+
diagnostics: pendingDiagnostics.length > 0 ? pendingDiagnostics : undefined,
|
|
168
|
+
};
|
|
95
169
|
console.error(JSON.stringify({ error }, null, 2));
|
|
96
170
|
}
|
|
97
171
|
else {
|
|
98
|
-
|
|
172
|
+
for (const message of pendingDiagnostics) {
|
|
173
|
+
console.error(message);
|
|
174
|
+
}
|
|
175
|
+
console.error(failure.help == null ? failure.message : `${failure.message}\n\n${failure.help}`);
|
|
99
176
|
}
|
|
100
|
-
|
|
177
|
+
// Let piped stderr finish writing before Node exits
|
|
178
|
+
process.stderr.write("", () => process.exit(1));
|
|
101
179
|
}
|
|
102
180
|
/** A command was given the wrong words. The router prints the message with the command's usage lines. */
|
|
103
|
-
export class UsageError extends
|
|
181
|
+
export class UsageError extends CliError {
|
|
182
|
+
constructor(message, details) {
|
|
183
|
+
super(message, "invalid_input", details);
|
|
184
|
+
}
|
|
104
185
|
}
|
|
105
186
|
/**
|
|
106
187
|
* Checks a command's words against its usage line, with the words written the same way:
|
|
107
188
|
* `<task-key>` is required, `[<project-key>]` is optional and `<task-key>...` repeats.
|
|
108
189
|
* Optional words go last.
|
|
109
190
|
*/
|
|
110
|
-
export function validatePositionals(positionals,
|
|
191
|
+
export function validatePositionals(positionals, form) {
|
|
192
|
+
const words = positionalWords(form);
|
|
111
193
|
const requiredWords = words.filter((word) => !word.startsWith("["));
|
|
112
194
|
const missingWord = requiredWords[positionals.length];
|
|
113
195
|
if (missingWord !== undefined) {
|
|
114
|
-
throw new UsageError(`Missing argument ${missingWord}
|
|
196
|
+
throw new UsageError(`Missing argument ${missingWord}`, {
|
|
197
|
+
argument: missingWord.replaceAll(/[\[\]<>]/g, "").replace(/\.\.\.$/, ""),
|
|
198
|
+
});
|
|
115
199
|
}
|
|
116
200
|
const hasRepeatingWord = words.some((word) => word.endsWith("..."));
|
|
117
201
|
const extraWord = positionals[words.length];
|
|
118
202
|
if (!hasRepeatingWord && extraWord !== undefined) {
|
|
119
|
-
throw new UsageError(`Unexpected argument "${extraWord}"
|
|
203
|
+
throw new UsageError(`Unexpected argument "${extraWord}"`, { field: [] });
|
|
120
204
|
}
|
|
121
205
|
}
|
|
122
|
-
export function validateUniqueOptions(tokens,
|
|
206
|
+
export function validateUniqueOptions(tokens, options) {
|
|
207
|
+
const repeatableOptions = Object.keys(options).filter((name) => options[name].multiple);
|
|
123
208
|
const parsedArgs = new Set();
|
|
124
209
|
for (const token of tokens) {
|
|
125
210
|
if (token.kind !== "option") {
|
|
@@ -127,11 +212,183 @@ export function validateUniqueOptions(tokens, repeatableOptions = []) {
|
|
|
127
212
|
}
|
|
128
213
|
const isRepeatable = repeatableOptions.includes(token.name);
|
|
129
214
|
if (parsedArgs.has(token.name) && !isRepeatable) {
|
|
130
|
-
quit(`Option "--${token.name}" can only be specified once
|
|
215
|
+
quit(`Option "--${token.name}" can only be specified once`, undefined, {
|
|
216
|
+
argument: `--${token.name}`,
|
|
217
|
+
});
|
|
131
218
|
}
|
|
132
219
|
parsedArgs.add(token.name);
|
|
133
220
|
}
|
|
134
221
|
}
|
|
222
|
+
/** Selects an order form after checking its required leading words. */
|
|
223
|
+
export function validateOrderForms(positionals, forms) {
|
|
224
|
+
const firstForm = forms[0];
|
|
225
|
+
const directionPosition = firstForm.positionals.findIndex(({ name }) => name === "direction");
|
|
226
|
+
const leadingPositionals = firstForm.positionals.slice(0, directionPosition + 1);
|
|
227
|
+
const directions = forms.flatMap((form) => form.positionals[directionPosition].value.choices);
|
|
228
|
+
const hasRepeatingSources = leadingPositionals.some((input) => input.max === null);
|
|
229
|
+
const recognizedDirectionIndex = positionals.findIndex((word, index) => index >= directionPosition && directions.includes(word));
|
|
230
|
+
const directionIndex = hasRepeatingSources && recognizedDirectionIndex !== -1
|
|
231
|
+
? recognizedDirectionIndex
|
|
232
|
+
: directionPosition;
|
|
233
|
+
leadingPositionals[directionPosition] = {
|
|
234
|
+
...leadingPositionals[directionPosition],
|
|
235
|
+
value: { kind: "enum", choices: directions },
|
|
236
|
+
};
|
|
237
|
+
validatePositionals(positionals.slice(0, leadingPositionals.length), {
|
|
238
|
+
id: "leading",
|
|
239
|
+
positionals: leadingPositionals,
|
|
240
|
+
});
|
|
241
|
+
const direction = positionals[directionIndex];
|
|
242
|
+
const form = forms.find((candidate) => candidate.positionals[directionPosition].value.choices.includes(direction));
|
|
243
|
+
if (form === undefined) {
|
|
244
|
+
throw new UsageError(`Unknown order direction "${direction}"`, { argument: "direction" });
|
|
245
|
+
}
|
|
246
|
+
const sourceCount = directionIndex - directionPosition + 1;
|
|
247
|
+
const expandedPositionals = form.positionals.flatMap((input) => input.max === null
|
|
248
|
+
? Array.from({ length: sourceCount }, () => ({ ...input, max: 1 }))
|
|
249
|
+
: [input]);
|
|
250
|
+
validatePositionals(positionals, { ...form, positionals: expandedPositionals });
|
|
251
|
+
// The direction is one of the matched form's choices, checked above.
|
|
252
|
+
return { ...form, direction, directionIndex };
|
|
253
|
+
}
|
|
254
|
+
export function requireStringOption(option, value, name, message) {
|
|
255
|
+
if (value === undefined) {
|
|
256
|
+
quit(message, undefined, { argument: `--${name}` });
|
|
257
|
+
}
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
135
260
|
export function isHelpFlag(flag) {
|
|
136
|
-
return
|
|
261
|
+
return helpSpellings.includes(flag);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Help sentence for a flag whose value may begin with a dash, such as a Markdown bullet.
|
|
265
|
+
* `parseArgs` refuses `--flag -value`, since a value that looks like an option may mean the value
|
|
266
|
+
* was forgotten, so we respect that refusal and not take the next word as the value
|
|
267
|
+
* @see {@link https://github.com/pkgjs/parseargs/issues/77}
|
|
268
|
+
*/
|
|
269
|
+
export function dashValueHelp(flag) {
|
|
270
|
+
return `A value that begins with a dash must be joined to its flag with =, as in ${flag}='- item'.`;
|
|
271
|
+
}
|
|
272
|
+
// "Option '--title' argument is ambiguous."
|
|
273
|
+
const DASH_VALUE_ERROR_PATTERN = /^Option '(--[^']+)' argument is ambiguous\./;
|
|
274
|
+
// The characters Python's `shlex.quote` leaves unquoted.
|
|
275
|
+
const SHELL_SAFE_WORD_PATTERN = /^[\w@%+=:,./-]+$/;
|
|
276
|
+
/** Quotes a word for a POSIX shell when it needs it, like Python's `shlex.quote` */
|
|
277
|
+
export function shellQuote(word) {
|
|
278
|
+
if (SHELL_SAFE_WORD_PATTERN.test(word)) {
|
|
279
|
+
return word;
|
|
280
|
+
}
|
|
281
|
+
return `'${word.replaceAll("'", `'\\''`)}'`;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* The message for `parseArgs` refusal of a value that begins with a dash, showing the user's
|
|
285
|
+
* command with that value joined to its flag.
|
|
286
|
+
* `null` for any other error
|
|
287
|
+
*/
|
|
288
|
+
function dashValueMessage(error, args) {
|
|
289
|
+
const isInvalidOptionValue = error?.code === "ERR_PARSE_ARGS_INVALID_OPTION_VALUE";
|
|
290
|
+
const flag = isInvalidOptionValue ? DASH_VALUE_ERROR_PATTERN.exec(error.message)?.[1] : undefined;
|
|
291
|
+
const flagIndex = args.findIndex((arg, index) => arg === flag && args[index + 1]?.startsWith("-"));
|
|
292
|
+
if (flagIndex === -1) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
const value = args[flagIndex + 1];
|
|
296
|
+
const fixedCommand = [
|
|
297
|
+
"zinn",
|
|
298
|
+
...args.slice(0, flagIndex).map(shellQuote),
|
|
299
|
+
`${flag}=${shellQuote(value)}`,
|
|
300
|
+
...args.slice(flagIndex + 2).map(shellQuote),
|
|
301
|
+
].join(" ");
|
|
302
|
+
return `Option "${flag}" is followed by "${value}", which begins with a dash. If it is the value, join it to the flag with =: ${fixedCommand}`;
|
|
303
|
+
}
|
|
304
|
+
// "Unknown option '--project'."
|
|
305
|
+
const UNKNOWN_LONG_OPTION_PATTERN = /^Unknown option '(--[^']+)'/;
|
|
306
|
+
// A word that begins with a single dash, such as -later.
|
|
307
|
+
const SHORT_OPTION_WORD_PATTERN = /^-[^-]/;
|
|
308
|
+
/** A command line to show, with each word quoted for the shell as needed. */
|
|
309
|
+
export function formatCommand(args) {
|
|
310
|
+
return ["zinn", ...args].map(shellQuote).join(" ");
|
|
311
|
+
}
|
|
312
|
+
/** Renames an option without changing values or words after `--` */
|
|
313
|
+
export function renameFlag(args, options, source, target) {
|
|
314
|
+
const { tokens } = parseArgs({
|
|
315
|
+
args,
|
|
316
|
+
options,
|
|
317
|
+
allowPositionals: true,
|
|
318
|
+
strict: false,
|
|
319
|
+
tokens: true,
|
|
320
|
+
});
|
|
321
|
+
const renamed = [...args];
|
|
322
|
+
for (const token of tokens) {
|
|
323
|
+
if (token.kind === "option" && token.name === source) {
|
|
324
|
+
renamed[token.index] = `--${target}${token.inlineValue ? `=${token.value}` : ""}`;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return renamed;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* The message for `parseArgs` refusal of an unknown option.
|
|
331
|
+
* A long flag is named, with the command the user likely meant when one is known,
|
|
332
|
+
* and otherwise the usage line shown with it is the fix.
|
|
333
|
+
* A word with a single dash shows the user's command with it after `--`.
|
|
334
|
+
*/
|
|
335
|
+
function unknownOptionMessage(message, args, suggestFor) {
|
|
336
|
+
const flag = UNKNOWN_LONG_OPTION_PATTERN.exec(message)?.[1];
|
|
337
|
+
if (flag !== undefined) {
|
|
338
|
+
const suggestion = suggestFor(flag);
|
|
339
|
+
const reason = `Unknown option "${flag}"`;
|
|
340
|
+
if (suggestion === undefined) {
|
|
341
|
+
return reason;
|
|
342
|
+
}
|
|
343
|
+
const separatorIndex = suggestion.indexOf(DOUBLE_DASH);
|
|
344
|
+
const optionEnd = separatorIndex === -1 ? suggestion.length : separatorIndex;
|
|
345
|
+
const correctedArgs = [
|
|
346
|
+
...suggestion.slice(0, optionEnd),
|
|
347
|
+
...args.filter(isRouterFlag),
|
|
348
|
+
...suggestion.slice(optionEnd),
|
|
349
|
+
];
|
|
350
|
+
return `${reason}. Did you mean: ${formatCommand(correctedArgs)}`;
|
|
351
|
+
}
|
|
352
|
+
// Commands define no short options, so `parseArgs` refused the first single-dash word before `--`.
|
|
353
|
+
const separatorIndex = args.indexOf(DOUBLE_DASH);
|
|
354
|
+
const optionArgs = separatorIndex === -1 ? args : args.slice(0, separatorIndex);
|
|
355
|
+
const wordIndex = optionArgs.findIndex((arg) => SHORT_OPTION_WORD_PATTERN.test(arg));
|
|
356
|
+
if (wordIndex === -1) {
|
|
357
|
+
return message;
|
|
358
|
+
}
|
|
359
|
+
const word = args[wordIndex];
|
|
360
|
+
const readOptions = [...word.slice(1)].map((letter) => `-${letter}`);
|
|
361
|
+
const readAs = readOptions.length > 1 ? ` (Zinn read ${word} as ${readOptions.join(" ")})` : "";
|
|
362
|
+
const reason = `Unknown option "${readOptions[0]}"${readAs}. If it is a value, put it after --`;
|
|
363
|
+
// Flags after the word must stay before --, and only the router's own flags are known to be flags.
|
|
364
|
+
const wordsAfter = args.slice(wordIndex + 1);
|
|
365
|
+
const routerFlagsAfter = wordsAfter.filter(isRouterFlag);
|
|
366
|
+
const plainWordsAfter = wordsAfter.filter((arg) => !isRouterFlag(arg));
|
|
367
|
+
if (plainWordsAfter.some((arg) => arg.startsWith("-"))) {
|
|
368
|
+
return `${reason}.`;
|
|
369
|
+
}
|
|
370
|
+
const fixedCommand = formatCommand([
|
|
371
|
+
...args.slice(0, wordIndex),
|
|
372
|
+
...routerFlagsAfter,
|
|
373
|
+
DOUBLE_DASH,
|
|
374
|
+
word,
|
|
375
|
+
...plainWordsAfter,
|
|
376
|
+
]);
|
|
377
|
+
return `${reason}: ${fixedCommand}`;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* The message for an argument error from parseArgs, rewritten where parseArgs' own advice misleads.
|
|
381
|
+
* parseArgs names the flag only in its message text, which Node and Bun word the same.
|
|
382
|
+
* `null` for any other error.
|
|
383
|
+
*/
|
|
384
|
+
export function parseArgsMessage(error, args, suggestFor) {
|
|
385
|
+
const code = error?.code;
|
|
386
|
+
const isParseArgsError = typeof code === "string" && code.startsWith("ERR_PARSE_ARGS_");
|
|
387
|
+
if (!isParseArgsError) {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
if (code === "ERR_PARSE_ARGS_UNKNOWN_OPTION") {
|
|
391
|
+
return unknownOptionMessage(error.message, args, suggestFor);
|
|
392
|
+
}
|
|
393
|
+
return dashValueMessage(error, args) ?? error.message;
|
|
137
394
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { Activity, ActivityTaskIdentity } from "@zinn-dev/core";
|
|
1
|
+
import type { Activity, ActivityTaskIdentity, Actor } from "@zinn-dev/core";
|
|
2
|
+
import type { PresentedActor } from "../result-schemas.js";
|
|
2
3
|
type NavigationIdentity = {
|
|
3
4
|
/** Undefined when the entity no longer exists and this event recorded no name for it. */
|
|
4
5
|
name?: string;
|
|
5
6
|
status: "current" | "archived" | "deleted";
|
|
6
7
|
distinguishesTaskId?: boolean;
|
|
8
|
+
showsStatus?: boolean;
|
|
7
9
|
};
|
|
8
10
|
export type PresentedActivity = {
|
|
9
11
|
event: Activity;
|
|
@@ -14,9 +16,15 @@ export type PresentedActivity = {
|
|
|
14
16
|
/** Neighbors in sorted UUID order determine the shortest globally unique left prefix. */
|
|
15
17
|
export declare function abbreviateActivityIds(ids: Array<string>): Map<string, string>;
|
|
16
18
|
export declare function presentActivities(events: Array<Activity>, options?: {
|
|
19
|
+
projectKey?: string;
|
|
20
|
+
projectId?: string;
|
|
21
|
+
showDeleted?: boolean;
|
|
17
22
|
taskKey?: string;
|
|
23
|
+
taskId?: string;
|
|
18
24
|
showsFullIds?: boolean;
|
|
19
25
|
}): Array<PresentedActivity>;
|
|
26
|
+
export declare function presentActor(actor: Actor): PresentedActor;
|
|
27
|
+
export declare function formatActor(actor: PresentedActor): string;
|
|
20
28
|
export declare function formatActivityList(entries: Array<PresentedActivity>, options?: {
|
|
21
29
|
showsDetails?: boolean;
|
|
22
30
|
}): string;
|
|
@@ -25,6 +25,17 @@ const RECORDED_FIELDS = new Set([
|
|
|
25
25
|
"text",
|
|
26
26
|
]);
|
|
27
27
|
const ENTITY_PREVIEW_LENGTH = 24;
|
|
28
|
+
const UNKNOWN_ACTOR_NAME = "unknown";
|
|
29
|
+
const ACTOR_DETAIL_FIELDS = [
|
|
30
|
+
{ key: "actor_kind", label: "Kind" },
|
|
31
|
+
{ key: "actor_email", label: "Email" },
|
|
32
|
+
{ key: "actor_source", label: "Source" },
|
|
33
|
+
{ key: "actor_session_id", label: "Session" },
|
|
34
|
+
{ key: "actor_thread_id", label: "Thread" },
|
|
35
|
+
{ key: "actor_agent", label: "Agent" },
|
|
36
|
+
{ key: "actor_model", label: "Model" },
|
|
37
|
+
{ key: "actor_effort", label: "Effort" },
|
|
38
|
+
];
|
|
28
39
|
const ACTION_LABELS = {
|
|
29
40
|
create: "created",
|
|
30
41
|
edit: "edited",
|
|
@@ -75,20 +86,12 @@ export function abbreviateActivityIds(ids) {
|
|
|
75
86
|
}
|
|
76
87
|
return prefixes;
|
|
77
88
|
}
|
|
78
|
-
function getTaskNavigation(events, taskKey) {
|
|
79
|
-
if (
|
|
80
|
-
return activity.getTaskIdentities(
|
|
89
|
+
function getTaskNavigation(events, taskKey, taskId) {
|
|
90
|
+
if (taskId !== undefined) {
|
|
91
|
+
return activity.getTaskIdentities({ taskId });
|
|
81
92
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
for (const item of task.getAll({ archive: "all" })) {
|
|
85
|
-
identities.set(item.id, {
|
|
86
|
-
id: item.id,
|
|
87
|
-
key: item.key,
|
|
88
|
-
title: item.title,
|
|
89
|
-
status: item.archived_at == null ? "active" : "archived",
|
|
90
|
-
});
|
|
91
|
-
}
|
|
93
|
+
if (taskKey !== undefined) {
|
|
94
|
+
return activity.getTaskIdentities({ taskKey, showDeleted: true });
|
|
92
95
|
}
|
|
93
96
|
const historicalKeys = new Set();
|
|
94
97
|
function addKey(key) {
|
|
@@ -112,9 +115,44 @@ function getTaskNavigation(events, taskKey) {
|
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
const identities = new Map();
|
|
119
|
+
const needsTaskIdentities = historicalKeys.size > 0 || events.some((event) => event.entity_type === "task");
|
|
120
|
+
if (needsTaskIdentities) {
|
|
121
|
+
for (const item of task.getAll({ archive: "all" })) {
|
|
122
|
+
identities.set(item.id, {
|
|
123
|
+
id: item.id,
|
|
124
|
+
key: item.key,
|
|
125
|
+
title: item.title,
|
|
126
|
+
status: item.archived_at == null ? "active" : "archived",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function addSnapshot(id, key, title) {
|
|
131
|
+
const parsedKey = activityTaskKeySchema.safeParse(key);
|
|
132
|
+
const isMatchingSnapshot = typeof id === "string" &&
|
|
133
|
+
typeof key === "string" &&
|
|
134
|
+
typeof title === "string" &&
|
|
135
|
+
parsedKey.success &&
|
|
136
|
+
historicalKeys.has(parsedKey.data);
|
|
137
|
+
if (isMatchingSnapshot && !identities.has(id)) {
|
|
138
|
+
identities.set(id, { id, key, title, status: "deleted" });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (historicalKeys.size > 0) {
|
|
142
|
+
for (const event of activity.getAll()) {
|
|
143
|
+
if (event.action !== "delete") {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (event.entity_type === "task") {
|
|
147
|
+
addSnapshot(event.entity_id, event.changes?.key, event.changes?.title);
|
|
148
|
+
}
|
|
149
|
+
else if (event.entity_type === "project" && Array.isArray(event.changes?.tasks)) {
|
|
150
|
+
for (const snapshot of event.changes.tasks) {
|
|
151
|
+
if (isRecord(snapshot)) {
|
|
152
|
+
addSnapshot(snapshot.id, snapshot.key, snapshot.title);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
118
156
|
}
|
|
119
157
|
}
|
|
120
158
|
return [...identities.values()];
|
|
@@ -126,7 +164,15 @@ export function presentActivities(events, options = {}) {
|
|
|
126
164
|
const prefixes = options.showsFullIds
|
|
127
165
|
? new Map()
|
|
128
166
|
: abbreviateActivityIds(activity.getIds());
|
|
129
|
-
const
|
|
167
|
+
const hasProjectSelector = options.projectKey !== undefined || options.projectId !== undefined;
|
|
168
|
+
const navigationEvents = hasProjectSelector
|
|
169
|
+
? activity.getAll({
|
|
170
|
+
projectKey: options.projectKey,
|
|
171
|
+
projectId: options.projectId,
|
|
172
|
+
showDeleted: options.showDeleted,
|
|
173
|
+
})
|
|
174
|
+
: events;
|
|
175
|
+
const tasks = getTaskNavigation(navigationEvents, options.taskKey, options.taskId);
|
|
130
176
|
const tasksById = new Map(tasks.map((identity) => [identity.id, identity]));
|
|
131
177
|
const taskIdsByKey = new Map();
|
|
132
178
|
for (const identity of tasks) {
|
|
@@ -137,6 +183,32 @@ export function presentActivities(events, options = {}) {
|
|
|
137
183
|
? new Map(label.getAll().map((item) => [item.id, item]))
|
|
138
184
|
: new Map();
|
|
139
185
|
const navigationByEntity = new Map();
|
|
186
|
+
const recordedNames = new Map();
|
|
187
|
+
if (hasProjectSelector) {
|
|
188
|
+
for (const event of navigationEvents) {
|
|
189
|
+
const isSnapshot = event.action === "create" || event.action === "delete";
|
|
190
|
+
if (!isSnapshot) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const name = event.changes?.key ?? event.changes?.name;
|
|
194
|
+
const entityKey = `${event.entity_type}:${event.entity_id}`;
|
|
195
|
+
if (typeof name === "string" && !recordedNames.has(entityKey)) {
|
|
196
|
+
recordedNames.set(entityKey, name);
|
|
197
|
+
}
|
|
198
|
+
if (event.entity_type === "project" && Array.isArray(event.changes?.columns)) {
|
|
199
|
+
for (const snapshot of event.changes.columns) {
|
|
200
|
+
if (isRecord(snapshot) &&
|
|
201
|
+
typeof snapshot.id === "string" &&
|
|
202
|
+
typeof snapshot.name === "string") {
|
|
203
|
+
const columnKey = `column:${snapshot.id}`;
|
|
204
|
+
if (!recordedNames.has(columnKey)) {
|
|
205
|
+
recordedNames.set(columnKey, snapshot.name);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
140
212
|
return events.map((event) => {
|
|
141
213
|
const entityKey = `${event.entity_type}:${event.entity_id}`;
|
|
142
214
|
let navigation = navigationByEntity.get(entityKey);
|
|
@@ -148,7 +220,8 @@ export function presentActivities(events, options = {}) {
|
|
|
148
220
|
navigation = {
|
|
149
221
|
name: identity.key,
|
|
150
222
|
status: identity.status === "active" ? "current" : identity.status,
|
|
151
|
-
distinguishesTaskId: taskIdsByKey.get(identity.key) > 1,
|
|
223
|
+
distinguishesTaskId: options.taskId !== undefined || taskIdsByKey.get(identity.key) > 1,
|
|
224
|
+
showsStatus: options.taskId !== undefined,
|
|
152
225
|
};
|
|
153
226
|
}
|
|
154
227
|
break;
|
|
@@ -182,7 +255,8 @@ export function presentActivities(events, options = {}) {
|
|
|
182
255
|
// Every existing entity was found above, so it means that a missing one has been *deleted*.
|
|
183
256
|
if (navigation === undefined) {
|
|
184
257
|
navigation = {
|
|
185
|
-
name:
|
|
258
|
+
name: recordedNames.get(entityKey) ??
|
|
259
|
+
(isSnapshot && typeof recordedName === "string" ? recordedName : undefined),
|
|
186
260
|
status: "deleted",
|
|
187
261
|
};
|
|
188
262
|
}
|
|
@@ -194,7 +268,7 @@ export function presentActivities(events, options = {}) {
|
|
|
194
268
|
event,
|
|
195
269
|
displayId: options.showsFullIds ? event.id : prefixes.get(event.id),
|
|
196
270
|
navigation,
|
|
197
|
-
removedTasks: options.taskKey === undefined
|
|
271
|
+
removedTasks: !hasProjectSelector && options.taskKey === undefined && options.taskId === undefined
|
|
198
272
|
? []
|
|
199
273
|
: tasks.filter((identity) => removedTaskIds.has(identity.id)),
|
|
200
274
|
};
|
|
@@ -295,7 +369,11 @@ function formatActivityContent(entry) {
|
|
|
295
369
|
const entityType = event.entity_type === "task" && navigation.name !== undefined ? "" : `${event.entity_type} `;
|
|
296
370
|
const isMutableCurrentName = navigation.status === "current" &&
|
|
297
371
|
(event.entity_type === "label" || event.entity_type === "column");
|
|
298
|
-
const marker = navigation.status === "deleted"
|
|
372
|
+
const marker = navigation.status === "deleted" || navigation.showsStatus
|
|
373
|
+
? ` [${navigation.status}]`
|
|
374
|
+
: isMutableCurrentName
|
|
375
|
+
? " [current name]"
|
|
376
|
+
: "";
|
|
299
377
|
const action = ACTION_LABELS[event.action] ?? event.action;
|
|
300
378
|
const summary = formatChangeSummary(entry, { showsRemovedTasks: false, omitsIdentity: true });
|
|
301
379
|
const lines = [
|
|
@@ -355,12 +433,31 @@ function formatRecordedChanges(event) {
|
|
|
355
433
|
}
|
|
356
434
|
return sections.join("\n\n") || "{}";
|
|
357
435
|
}
|
|
436
|
+
export function presentActor(actor) {
|
|
437
|
+
return {
|
|
438
|
+
actor_name: actor.name,
|
|
439
|
+
actor_email: actor.email,
|
|
440
|
+
actor_kind: actor.kind,
|
|
441
|
+
actor_source: actor.source ?? null,
|
|
442
|
+
actor_session_id: actor.sessionId ?? null,
|
|
443
|
+
actor_thread_id: actor.threadId ?? null,
|
|
444
|
+
actor_agent: actor.agent ?? null,
|
|
445
|
+
actor_model: actor.model ?? null,
|
|
446
|
+
actor_effort: actor.effort ?? null,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
export function formatActor(actor) {
|
|
450
|
+
return [
|
|
451
|
+
actor.actor_name ?? UNKNOWN_ACTOR_NAME,
|
|
452
|
+
...ACTOR_DETAIL_FIELDS.map(({ key, label }) => `${label}: ${actor[key] ?? "null"}`),
|
|
453
|
+
].join("\n");
|
|
454
|
+
}
|
|
358
455
|
export function formatActivityList(entries, options = {}) {
|
|
359
456
|
const rows = entries.map((entry) => {
|
|
360
457
|
const metadata = [
|
|
361
458
|
{ text: entry.displayId },
|
|
362
459
|
{ text: formatLocalTime(entry.event.created_at), style: "dim" },
|
|
363
|
-
{ text: `${entry.event.actor_name} (${entry.event.actor_kind})` },
|
|
460
|
+
{ text: `${entry.event.actor_name ?? UNKNOWN_ACTOR_NAME} (${entry.event.actor_kind})` },
|
|
364
461
|
];
|
|
365
462
|
if (!options.showsDetails) {
|
|
366
463
|
return [...metadata, { text: formatActivityContent(entry) }];
|
|
@@ -382,7 +479,7 @@ export function formatActivityList(entries, options = {}) {
|
|
|
382
479
|
...(entry.navigation.name === undefined
|
|
383
480
|
? []
|
|
384
481
|
: [formatSection("Navigation", formatNavigation(entry, { showsFullNames: true }))]),
|
|
385
|
-
formatSection("Actor",
|
|
482
|
+
formatSection("Actor", formatActor(entry.event)),
|
|
386
483
|
formatSection("Changes", formatRecordedChanges(entry.event)),
|
|
387
484
|
].join("\n\n"))
|
|
388
485
|
.join("\n\n");
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type { RowCell } from "./row-layout.js";
|
|
2
|
-
|
|
2
|
+
import type { PresentedColumn } from "../result-schemas.js";
|
|
3
|
+
type ColumnRow = {
|
|
3
4
|
name: string;
|
|
4
5
|
project?: string;
|
|
5
6
|
taskCount?: number;
|
|
6
7
|
};
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
export declare function presentColumn(columnMatch: {
|
|
9
|
+
id: string;
|
|
10
|
+
project_id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
}): PresentedColumn;
|
|
10
13
|
export declare function columnNameCell(columnName: string): RowCell;
|
|
11
14
|
/** The archived row is left out when there are no archived tasks. */
|
|
12
|
-
export declare function formatColumnList(presentedColumns: Array<
|
|
15
|
+
export declare function formatColumnList(presentedColumns: Array<ColumnRow>, options?: {
|
|
13
16
|
showsProject?: boolean;
|
|
14
17
|
showsTaskCounts?: boolean;
|
|
15
18
|
archivedTaskCount?: number;
|
|
16
19
|
}): string;
|
|
20
|
+
export {};
|