@hasna/mementos 0.1.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/LICENSE +190 -0
- package/README.md +276 -0
- package/dashboard/dist/assets/index-C8vbNL_5.css +1 -0
- package/dashboard/dist/assets/index-Dnhl5e5q.js +270 -0
- package/dashboard/dist/index.html +13 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +3643 -0
- package/dist/db/agents.d.ts +6 -0
- package/dist/db/agents.d.ts.map +1 -0
- package/dist/db/database.d.ts +10 -0
- package/dist/db/database.d.ts.map +1 -0
- package/dist/db/memories.d.ts +12 -0
- package/dist/db/memories.d.ts.map +1 -0
- package/dist/db/projects.d.ts +6 -0
- package/dist/db/projects.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1174 -0
- package/dist/lib/config.d.ts +5 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/injector.d.ts +31 -0
- package/dist/lib/injector.d.ts.map +1 -0
- package/dist/lib/retention.d.ts +10 -0
- package/dist/lib/retention.d.ts.map +1 -0
- package/dist/lib/search.d.ts +11 -0
- package/dist/lib/search.d.ts.map +1 -0
- package/dist/lib/sync.d.ts +10 -0
- package/dist/lib/sync.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +5185 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +1144 -0
- package/dist/types/index.d.ts +158 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,3643 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
20
|
+
var __require = import.meta.require;
|
|
21
|
+
|
|
22
|
+
// node_modules/commander/lib/error.js
|
|
23
|
+
var require_error = __commonJS((exports) => {
|
|
24
|
+
class CommanderError extends Error {
|
|
25
|
+
constructor(exitCode, code, message) {
|
|
26
|
+
super(message);
|
|
27
|
+
Error.captureStackTrace(this, this.constructor);
|
|
28
|
+
this.name = this.constructor.name;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.exitCode = exitCode;
|
|
31
|
+
this.nestedError = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class InvalidArgumentError extends CommanderError {
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super(1, "commander.invalidArgument", message);
|
|
38
|
+
Error.captureStackTrace(this, this.constructor);
|
|
39
|
+
this.name = this.constructor.name;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.CommanderError = CommanderError;
|
|
43
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// node_modules/commander/lib/argument.js
|
|
47
|
+
var require_argument = __commonJS((exports) => {
|
|
48
|
+
var { InvalidArgumentError } = require_error();
|
|
49
|
+
|
|
50
|
+
class Argument {
|
|
51
|
+
constructor(name, description) {
|
|
52
|
+
this.description = description || "";
|
|
53
|
+
this.variadic = false;
|
|
54
|
+
this.parseArg = undefined;
|
|
55
|
+
this.defaultValue = undefined;
|
|
56
|
+
this.defaultValueDescription = undefined;
|
|
57
|
+
this.argChoices = undefined;
|
|
58
|
+
switch (name[0]) {
|
|
59
|
+
case "<":
|
|
60
|
+
this.required = true;
|
|
61
|
+
this._name = name.slice(1, -1);
|
|
62
|
+
break;
|
|
63
|
+
case "[":
|
|
64
|
+
this.required = false;
|
|
65
|
+
this._name = name.slice(1, -1);
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
this.required = true;
|
|
69
|
+
this._name = name;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
if (this._name.length > 3 && this._name.slice(-3) === "...") {
|
|
73
|
+
this.variadic = true;
|
|
74
|
+
this._name = this._name.slice(0, -3);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
name() {
|
|
78
|
+
return this._name;
|
|
79
|
+
}
|
|
80
|
+
_concatValue(value, previous) {
|
|
81
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
82
|
+
return [value];
|
|
83
|
+
}
|
|
84
|
+
return previous.concat(value);
|
|
85
|
+
}
|
|
86
|
+
default(value, description) {
|
|
87
|
+
this.defaultValue = value;
|
|
88
|
+
this.defaultValueDescription = description;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
argParser(fn) {
|
|
92
|
+
this.parseArg = fn;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
choices(values) {
|
|
96
|
+
this.argChoices = values.slice();
|
|
97
|
+
this.parseArg = (arg, previous) => {
|
|
98
|
+
if (!this.argChoices.includes(arg)) {
|
|
99
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
100
|
+
}
|
|
101
|
+
if (this.variadic) {
|
|
102
|
+
return this._concatValue(arg, previous);
|
|
103
|
+
}
|
|
104
|
+
return arg;
|
|
105
|
+
};
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
argRequired() {
|
|
109
|
+
this.required = true;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
argOptional() {
|
|
113
|
+
this.required = false;
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function humanReadableArgName(arg) {
|
|
118
|
+
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");
|
|
119
|
+
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
120
|
+
}
|
|
121
|
+
exports.Argument = Argument;
|
|
122
|
+
exports.humanReadableArgName = humanReadableArgName;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// node_modules/commander/lib/help.js
|
|
126
|
+
var require_help = __commonJS((exports) => {
|
|
127
|
+
var { humanReadableArgName } = require_argument();
|
|
128
|
+
|
|
129
|
+
class Help {
|
|
130
|
+
constructor() {
|
|
131
|
+
this.helpWidth = undefined;
|
|
132
|
+
this.minWidthToWrap = 40;
|
|
133
|
+
this.sortSubcommands = false;
|
|
134
|
+
this.sortOptions = false;
|
|
135
|
+
this.showGlobalOptions = false;
|
|
136
|
+
}
|
|
137
|
+
prepareContext(contextOptions) {
|
|
138
|
+
this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
|
|
139
|
+
}
|
|
140
|
+
visibleCommands(cmd) {
|
|
141
|
+
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2._hidden);
|
|
142
|
+
const helpCommand = cmd._getHelpCommand();
|
|
143
|
+
if (helpCommand && !helpCommand._hidden) {
|
|
144
|
+
visibleCommands.push(helpCommand);
|
|
145
|
+
}
|
|
146
|
+
if (this.sortSubcommands) {
|
|
147
|
+
visibleCommands.sort((a, b) => {
|
|
148
|
+
return a.name().localeCompare(b.name());
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return visibleCommands;
|
|
152
|
+
}
|
|
153
|
+
compareOptions(a, b) {
|
|
154
|
+
const getSortKey = (option) => {
|
|
155
|
+
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
156
|
+
};
|
|
157
|
+
return getSortKey(a).localeCompare(getSortKey(b));
|
|
158
|
+
}
|
|
159
|
+
visibleOptions(cmd) {
|
|
160
|
+
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
161
|
+
const helpOption = cmd._getHelpOption();
|
|
162
|
+
if (helpOption && !helpOption.hidden) {
|
|
163
|
+
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
|
|
164
|
+
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
|
|
165
|
+
if (!removeShort && !removeLong) {
|
|
166
|
+
visibleOptions.push(helpOption);
|
|
167
|
+
} else if (helpOption.long && !removeLong) {
|
|
168
|
+
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
|
|
169
|
+
} else if (helpOption.short && !removeShort) {
|
|
170
|
+
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (this.sortOptions) {
|
|
174
|
+
visibleOptions.sort(this.compareOptions);
|
|
175
|
+
}
|
|
176
|
+
return visibleOptions;
|
|
177
|
+
}
|
|
178
|
+
visibleGlobalOptions(cmd) {
|
|
179
|
+
if (!this.showGlobalOptions)
|
|
180
|
+
return [];
|
|
181
|
+
const globalOptions = [];
|
|
182
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
183
|
+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
184
|
+
globalOptions.push(...visibleOptions);
|
|
185
|
+
}
|
|
186
|
+
if (this.sortOptions) {
|
|
187
|
+
globalOptions.sort(this.compareOptions);
|
|
188
|
+
}
|
|
189
|
+
return globalOptions;
|
|
190
|
+
}
|
|
191
|
+
visibleArguments(cmd) {
|
|
192
|
+
if (cmd._argsDescription) {
|
|
193
|
+
cmd.registeredArguments.forEach((argument) => {
|
|
194
|
+
argument.description = argument.description || cmd._argsDescription[argument.name()] || "";
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (cmd.registeredArguments.find((argument) => argument.description)) {
|
|
198
|
+
return cmd.registeredArguments;
|
|
199
|
+
}
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
subcommandTerm(cmd) {
|
|
203
|
+
const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(" ");
|
|
204
|
+
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (cmd.options.length ? " [options]" : "") + (args ? " " + args : "");
|
|
205
|
+
}
|
|
206
|
+
optionTerm(option) {
|
|
207
|
+
return option.flags;
|
|
208
|
+
}
|
|
209
|
+
argumentTerm(argument) {
|
|
210
|
+
return argument.name();
|
|
211
|
+
}
|
|
212
|
+
longestSubcommandTermLength(cmd, helper) {
|
|
213
|
+
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
214
|
+
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
215
|
+
}, 0);
|
|
216
|
+
}
|
|
217
|
+
longestOptionTermLength(cmd, helper) {
|
|
218
|
+
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
219
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
220
|
+
}, 0);
|
|
221
|
+
}
|
|
222
|
+
longestGlobalOptionTermLength(cmd, helper) {
|
|
223
|
+
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
224
|
+
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
225
|
+
}, 0);
|
|
226
|
+
}
|
|
227
|
+
longestArgumentTermLength(cmd, helper) {
|
|
228
|
+
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
229
|
+
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
230
|
+
}, 0);
|
|
231
|
+
}
|
|
232
|
+
commandUsage(cmd) {
|
|
233
|
+
let cmdName = cmd._name;
|
|
234
|
+
if (cmd._aliases[0]) {
|
|
235
|
+
cmdName = cmdName + "|" + cmd._aliases[0];
|
|
236
|
+
}
|
|
237
|
+
let ancestorCmdNames = "";
|
|
238
|
+
for (let ancestorCmd = cmd.parent;ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
239
|
+
ancestorCmdNames = ancestorCmd.name() + " " + ancestorCmdNames;
|
|
240
|
+
}
|
|
241
|
+
return ancestorCmdNames + cmdName + " " + cmd.usage();
|
|
242
|
+
}
|
|
243
|
+
commandDescription(cmd) {
|
|
244
|
+
return cmd.description();
|
|
245
|
+
}
|
|
246
|
+
subcommandDescription(cmd) {
|
|
247
|
+
return cmd.summary() || cmd.description();
|
|
248
|
+
}
|
|
249
|
+
optionDescription(option) {
|
|
250
|
+
const extraInfo = [];
|
|
251
|
+
if (option.argChoices) {
|
|
252
|
+
extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
253
|
+
}
|
|
254
|
+
if (option.defaultValue !== undefined) {
|
|
255
|
+
const showDefault = option.required || option.optional || option.isBoolean() && typeof option.defaultValue === "boolean";
|
|
256
|
+
if (showDefault) {
|
|
257
|
+
extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (option.presetArg !== undefined && option.optional) {
|
|
261
|
+
extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
|
|
262
|
+
}
|
|
263
|
+
if (option.envVar !== undefined) {
|
|
264
|
+
extraInfo.push(`env: ${option.envVar}`);
|
|
265
|
+
}
|
|
266
|
+
if (extraInfo.length > 0) {
|
|
267
|
+
return `${option.description} (${extraInfo.join(", ")})`;
|
|
268
|
+
}
|
|
269
|
+
return option.description;
|
|
270
|
+
}
|
|
271
|
+
argumentDescription(argument) {
|
|
272
|
+
const extraInfo = [];
|
|
273
|
+
if (argument.argChoices) {
|
|
274
|
+
extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(", ")}`);
|
|
275
|
+
}
|
|
276
|
+
if (argument.defaultValue !== undefined) {
|
|
277
|
+
extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
|
|
278
|
+
}
|
|
279
|
+
if (extraInfo.length > 0) {
|
|
280
|
+
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
281
|
+
if (argument.description) {
|
|
282
|
+
return `${argument.description} ${extraDescription}`;
|
|
283
|
+
}
|
|
284
|
+
return extraDescription;
|
|
285
|
+
}
|
|
286
|
+
return argument.description;
|
|
287
|
+
}
|
|
288
|
+
formatHelp(cmd, helper) {
|
|
289
|
+
const termWidth = helper.padWidth(cmd, helper);
|
|
290
|
+
const helpWidth = helper.helpWidth ?? 80;
|
|
291
|
+
function callFormatItem(term, description) {
|
|
292
|
+
return helper.formatItem(term, termWidth, description, helper);
|
|
293
|
+
}
|
|
294
|
+
let output = [
|
|
295
|
+
`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`,
|
|
296
|
+
""
|
|
297
|
+
];
|
|
298
|
+
const commandDescription = helper.commandDescription(cmd);
|
|
299
|
+
if (commandDescription.length > 0) {
|
|
300
|
+
output = output.concat([
|
|
301
|
+
helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth),
|
|
302
|
+
""
|
|
303
|
+
]);
|
|
304
|
+
}
|
|
305
|
+
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
306
|
+
return callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument)));
|
|
307
|
+
});
|
|
308
|
+
if (argumentList.length > 0) {
|
|
309
|
+
output = output.concat([
|
|
310
|
+
helper.styleTitle("Arguments:"),
|
|
311
|
+
...argumentList,
|
|
312
|
+
""
|
|
313
|
+
]);
|
|
314
|
+
}
|
|
315
|
+
const optionList = helper.visibleOptions(cmd).map((option) => {
|
|
316
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
317
|
+
});
|
|
318
|
+
if (optionList.length > 0) {
|
|
319
|
+
output = output.concat([
|
|
320
|
+
helper.styleTitle("Options:"),
|
|
321
|
+
...optionList,
|
|
322
|
+
""
|
|
323
|
+
]);
|
|
324
|
+
}
|
|
325
|
+
if (helper.showGlobalOptions) {
|
|
326
|
+
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
327
|
+
return callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option)));
|
|
328
|
+
});
|
|
329
|
+
if (globalOptionList.length > 0) {
|
|
330
|
+
output = output.concat([
|
|
331
|
+
helper.styleTitle("Global Options:"),
|
|
332
|
+
...globalOptionList,
|
|
333
|
+
""
|
|
334
|
+
]);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const commandList = helper.visibleCommands(cmd).map((cmd2) => {
|
|
338
|
+
return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(cmd2)), helper.styleSubcommandDescription(helper.subcommandDescription(cmd2)));
|
|
339
|
+
});
|
|
340
|
+
if (commandList.length > 0) {
|
|
341
|
+
output = output.concat([
|
|
342
|
+
helper.styleTitle("Commands:"),
|
|
343
|
+
...commandList,
|
|
344
|
+
""
|
|
345
|
+
]);
|
|
346
|
+
}
|
|
347
|
+
return output.join(`
|
|
348
|
+
`);
|
|
349
|
+
}
|
|
350
|
+
displayWidth(str) {
|
|
351
|
+
return stripColor(str).length;
|
|
352
|
+
}
|
|
353
|
+
styleTitle(str) {
|
|
354
|
+
return str;
|
|
355
|
+
}
|
|
356
|
+
styleUsage(str) {
|
|
357
|
+
return str.split(" ").map((word) => {
|
|
358
|
+
if (word === "[options]")
|
|
359
|
+
return this.styleOptionText(word);
|
|
360
|
+
if (word === "[command]")
|
|
361
|
+
return this.styleSubcommandText(word);
|
|
362
|
+
if (word[0] === "[" || word[0] === "<")
|
|
363
|
+
return this.styleArgumentText(word);
|
|
364
|
+
return this.styleCommandText(word);
|
|
365
|
+
}).join(" ");
|
|
366
|
+
}
|
|
367
|
+
styleCommandDescription(str) {
|
|
368
|
+
return this.styleDescriptionText(str);
|
|
369
|
+
}
|
|
370
|
+
styleOptionDescription(str) {
|
|
371
|
+
return this.styleDescriptionText(str);
|
|
372
|
+
}
|
|
373
|
+
styleSubcommandDescription(str) {
|
|
374
|
+
return this.styleDescriptionText(str);
|
|
375
|
+
}
|
|
376
|
+
styleArgumentDescription(str) {
|
|
377
|
+
return this.styleDescriptionText(str);
|
|
378
|
+
}
|
|
379
|
+
styleDescriptionText(str) {
|
|
380
|
+
return str;
|
|
381
|
+
}
|
|
382
|
+
styleOptionTerm(str) {
|
|
383
|
+
return this.styleOptionText(str);
|
|
384
|
+
}
|
|
385
|
+
styleSubcommandTerm(str) {
|
|
386
|
+
return str.split(" ").map((word) => {
|
|
387
|
+
if (word === "[options]")
|
|
388
|
+
return this.styleOptionText(word);
|
|
389
|
+
if (word[0] === "[" || word[0] === "<")
|
|
390
|
+
return this.styleArgumentText(word);
|
|
391
|
+
return this.styleSubcommandText(word);
|
|
392
|
+
}).join(" ");
|
|
393
|
+
}
|
|
394
|
+
styleArgumentTerm(str) {
|
|
395
|
+
return this.styleArgumentText(str);
|
|
396
|
+
}
|
|
397
|
+
styleOptionText(str) {
|
|
398
|
+
return str;
|
|
399
|
+
}
|
|
400
|
+
styleArgumentText(str) {
|
|
401
|
+
return str;
|
|
402
|
+
}
|
|
403
|
+
styleSubcommandText(str) {
|
|
404
|
+
return str;
|
|
405
|
+
}
|
|
406
|
+
styleCommandText(str) {
|
|
407
|
+
return str;
|
|
408
|
+
}
|
|
409
|
+
padWidth(cmd, helper) {
|
|
410
|
+
return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
|
|
411
|
+
}
|
|
412
|
+
preformatted(str) {
|
|
413
|
+
return /\n[^\S\r\n]/.test(str);
|
|
414
|
+
}
|
|
415
|
+
formatItem(term, termWidth, description, helper) {
|
|
416
|
+
const itemIndent = 2;
|
|
417
|
+
const itemIndentStr = " ".repeat(itemIndent);
|
|
418
|
+
if (!description)
|
|
419
|
+
return itemIndentStr + term;
|
|
420
|
+
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
421
|
+
const spacerWidth = 2;
|
|
422
|
+
const helpWidth = this.helpWidth ?? 80;
|
|
423
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
424
|
+
let formattedDescription;
|
|
425
|
+
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
426
|
+
formattedDescription = description;
|
|
427
|
+
} else {
|
|
428
|
+
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
429
|
+
formattedDescription = wrappedDescription.replace(/\n/g, `
|
|
430
|
+
` + " ".repeat(termWidth + spacerWidth));
|
|
431
|
+
}
|
|
432
|
+
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
433
|
+
${itemIndentStr}`);
|
|
434
|
+
}
|
|
435
|
+
boxWrap(str, width) {
|
|
436
|
+
if (width < this.minWidthToWrap)
|
|
437
|
+
return str;
|
|
438
|
+
const rawLines = str.split(/\r\n|\n/);
|
|
439
|
+
const chunkPattern = /[\s]*[^\s]+/g;
|
|
440
|
+
const wrappedLines = [];
|
|
441
|
+
rawLines.forEach((line) => {
|
|
442
|
+
const chunks = line.match(chunkPattern);
|
|
443
|
+
if (chunks === null) {
|
|
444
|
+
wrappedLines.push("");
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
let sumChunks = [chunks.shift()];
|
|
448
|
+
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
449
|
+
chunks.forEach((chunk) => {
|
|
450
|
+
const visibleWidth = this.displayWidth(chunk);
|
|
451
|
+
if (sumWidth + visibleWidth <= width) {
|
|
452
|
+
sumChunks.push(chunk);
|
|
453
|
+
sumWidth += visibleWidth;
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
wrappedLines.push(sumChunks.join(""));
|
|
457
|
+
const nextChunk = chunk.trimStart();
|
|
458
|
+
sumChunks = [nextChunk];
|
|
459
|
+
sumWidth = this.displayWidth(nextChunk);
|
|
460
|
+
});
|
|
461
|
+
wrappedLines.push(sumChunks.join(""));
|
|
462
|
+
});
|
|
463
|
+
return wrappedLines.join(`
|
|
464
|
+
`);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function stripColor(str) {
|
|
468
|
+
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
469
|
+
return str.replace(sgrPattern, "");
|
|
470
|
+
}
|
|
471
|
+
exports.Help = Help;
|
|
472
|
+
exports.stripColor = stripColor;
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// node_modules/commander/lib/option.js
|
|
476
|
+
var require_option = __commonJS((exports) => {
|
|
477
|
+
var { InvalidArgumentError } = require_error();
|
|
478
|
+
|
|
479
|
+
class Option {
|
|
480
|
+
constructor(flags, description) {
|
|
481
|
+
this.flags = flags;
|
|
482
|
+
this.description = description || "";
|
|
483
|
+
this.required = flags.includes("<");
|
|
484
|
+
this.optional = flags.includes("[");
|
|
485
|
+
this.variadic = /\w\.\.\.[>\]]$/.test(flags);
|
|
486
|
+
this.mandatory = false;
|
|
487
|
+
const optionFlags = splitOptionFlags(flags);
|
|
488
|
+
this.short = optionFlags.shortFlag;
|
|
489
|
+
this.long = optionFlags.longFlag;
|
|
490
|
+
this.negate = false;
|
|
491
|
+
if (this.long) {
|
|
492
|
+
this.negate = this.long.startsWith("--no-");
|
|
493
|
+
}
|
|
494
|
+
this.defaultValue = undefined;
|
|
495
|
+
this.defaultValueDescription = undefined;
|
|
496
|
+
this.presetArg = undefined;
|
|
497
|
+
this.envVar = undefined;
|
|
498
|
+
this.parseArg = undefined;
|
|
499
|
+
this.hidden = false;
|
|
500
|
+
this.argChoices = undefined;
|
|
501
|
+
this.conflictsWith = [];
|
|
502
|
+
this.implied = undefined;
|
|
503
|
+
}
|
|
504
|
+
default(value, description) {
|
|
505
|
+
this.defaultValue = value;
|
|
506
|
+
this.defaultValueDescription = description;
|
|
507
|
+
return this;
|
|
508
|
+
}
|
|
509
|
+
preset(arg) {
|
|
510
|
+
this.presetArg = arg;
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
conflicts(names) {
|
|
514
|
+
this.conflictsWith = this.conflictsWith.concat(names);
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
implies(impliedOptionValues) {
|
|
518
|
+
let newImplied = impliedOptionValues;
|
|
519
|
+
if (typeof impliedOptionValues === "string") {
|
|
520
|
+
newImplied = { [impliedOptionValues]: true };
|
|
521
|
+
}
|
|
522
|
+
this.implied = Object.assign(this.implied || {}, newImplied);
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
env(name) {
|
|
526
|
+
this.envVar = name;
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
argParser(fn) {
|
|
530
|
+
this.parseArg = fn;
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
makeOptionMandatory(mandatory = true) {
|
|
534
|
+
this.mandatory = !!mandatory;
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
hideHelp(hide = true) {
|
|
538
|
+
this.hidden = !!hide;
|
|
539
|
+
return this;
|
|
540
|
+
}
|
|
541
|
+
_concatValue(value, previous) {
|
|
542
|
+
if (previous === this.defaultValue || !Array.isArray(previous)) {
|
|
543
|
+
return [value];
|
|
544
|
+
}
|
|
545
|
+
return previous.concat(value);
|
|
546
|
+
}
|
|
547
|
+
choices(values) {
|
|
548
|
+
this.argChoices = values.slice();
|
|
549
|
+
this.parseArg = (arg, previous) => {
|
|
550
|
+
if (!this.argChoices.includes(arg)) {
|
|
551
|
+
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(", ")}.`);
|
|
552
|
+
}
|
|
553
|
+
if (this.variadic) {
|
|
554
|
+
return this._concatValue(arg, previous);
|
|
555
|
+
}
|
|
556
|
+
return arg;
|
|
557
|
+
};
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
name() {
|
|
561
|
+
if (this.long) {
|
|
562
|
+
return this.long.replace(/^--/, "");
|
|
563
|
+
}
|
|
564
|
+
return this.short.replace(/^-/, "");
|
|
565
|
+
}
|
|
566
|
+
attributeName() {
|
|
567
|
+
if (this.negate) {
|
|
568
|
+
return camelcase(this.name().replace(/^no-/, ""));
|
|
569
|
+
}
|
|
570
|
+
return camelcase(this.name());
|
|
571
|
+
}
|
|
572
|
+
is(arg) {
|
|
573
|
+
return this.short === arg || this.long === arg;
|
|
574
|
+
}
|
|
575
|
+
isBoolean() {
|
|
576
|
+
return !this.required && !this.optional && !this.negate;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
class DualOptions {
|
|
581
|
+
constructor(options) {
|
|
582
|
+
this.positiveOptions = new Map;
|
|
583
|
+
this.negativeOptions = new Map;
|
|
584
|
+
this.dualOptions = new Set;
|
|
585
|
+
options.forEach((option) => {
|
|
586
|
+
if (option.negate) {
|
|
587
|
+
this.negativeOptions.set(option.attributeName(), option);
|
|
588
|
+
} else {
|
|
589
|
+
this.positiveOptions.set(option.attributeName(), option);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
this.negativeOptions.forEach((value, key) => {
|
|
593
|
+
if (this.positiveOptions.has(key)) {
|
|
594
|
+
this.dualOptions.add(key);
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
valueFromOption(value, option) {
|
|
599
|
+
const optionKey = option.attributeName();
|
|
600
|
+
if (!this.dualOptions.has(optionKey))
|
|
601
|
+
return true;
|
|
602
|
+
const preset = this.negativeOptions.get(optionKey).presetArg;
|
|
603
|
+
const negativeValue = preset !== undefined ? preset : false;
|
|
604
|
+
return option.negate === (negativeValue === value);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function camelcase(str) {
|
|
608
|
+
return str.split("-").reduce((str2, word) => {
|
|
609
|
+
return str2 + word[0].toUpperCase() + word.slice(1);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
function splitOptionFlags(flags) {
|
|
613
|
+
let shortFlag;
|
|
614
|
+
let longFlag;
|
|
615
|
+
const shortFlagExp = /^-[^-]$/;
|
|
616
|
+
const longFlagExp = /^--[^-]/;
|
|
617
|
+
const flagParts = flags.split(/[ |,]+/).concat("guard");
|
|
618
|
+
if (shortFlagExp.test(flagParts[0]))
|
|
619
|
+
shortFlag = flagParts.shift();
|
|
620
|
+
if (longFlagExp.test(flagParts[0]))
|
|
621
|
+
longFlag = flagParts.shift();
|
|
622
|
+
if (!shortFlag && shortFlagExp.test(flagParts[0]))
|
|
623
|
+
shortFlag = flagParts.shift();
|
|
624
|
+
if (!shortFlag && longFlagExp.test(flagParts[0])) {
|
|
625
|
+
shortFlag = longFlag;
|
|
626
|
+
longFlag = flagParts.shift();
|
|
627
|
+
}
|
|
628
|
+
if (flagParts[0].startsWith("-")) {
|
|
629
|
+
const unsupportedFlag = flagParts[0];
|
|
630
|
+
const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
|
|
631
|
+
if (/^-[^-][^-]/.test(unsupportedFlag))
|
|
632
|
+
throw new Error(`${baseError}
|
|
633
|
+
- a short flag is a single dash and a single character
|
|
634
|
+
- either use a single dash and a single character (for a short flag)
|
|
635
|
+
- or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
|
|
636
|
+
if (shortFlagExp.test(unsupportedFlag))
|
|
637
|
+
throw new Error(`${baseError}
|
|
638
|
+
- too many short flags`);
|
|
639
|
+
if (longFlagExp.test(unsupportedFlag))
|
|
640
|
+
throw new Error(`${baseError}
|
|
641
|
+
- too many long flags`);
|
|
642
|
+
throw new Error(`${baseError}
|
|
643
|
+
- unrecognised flag format`);
|
|
644
|
+
}
|
|
645
|
+
if (shortFlag === undefined && longFlag === undefined)
|
|
646
|
+
throw new Error(`option creation failed due to no flags found in '${flags}'.`);
|
|
647
|
+
return { shortFlag, longFlag };
|
|
648
|
+
}
|
|
649
|
+
exports.Option = Option;
|
|
650
|
+
exports.DualOptions = DualOptions;
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// node_modules/commander/lib/suggestSimilar.js
|
|
654
|
+
var require_suggestSimilar = __commonJS((exports) => {
|
|
655
|
+
var maxDistance = 3;
|
|
656
|
+
function editDistance(a, b) {
|
|
657
|
+
if (Math.abs(a.length - b.length) > maxDistance)
|
|
658
|
+
return Math.max(a.length, b.length);
|
|
659
|
+
const d = [];
|
|
660
|
+
for (let i = 0;i <= a.length; i++) {
|
|
661
|
+
d[i] = [i];
|
|
662
|
+
}
|
|
663
|
+
for (let j = 0;j <= b.length; j++) {
|
|
664
|
+
d[0][j] = j;
|
|
665
|
+
}
|
|
666
|
+
for (let j = 1;j <= b.length; j++) {
|
|
667
|
+
for (let i = 1;i <= a.length; i++) {
|
|
668
|
+
let cost = 1;
|
|
669
|
+
if (a[i - 1] === b[j - 1]) {
|
|
670
|
+
cost = 0;
|
|
671
|
+
} else {
|
|
672
|
+
cost = 1;
|
|
673
|
+
}
|
|
674
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
675
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
676
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return d[a.length][b.length];
|
|
681
|
+
}
|
|
682
|
+
function suggestSimilar(word, candidates) {
|
|
683
|
+
if (!candidates || candidates.length === 0)
|
|
684
|
+
return "";
|
|
685
|
+
candidates = Array.from(new Set(candidates));
|
|
686
|
+
const searchingOptions = word.startsWith("--");
|
|
687
|
+
if (searchingOptions) {
|
|
688
|
+
word = word.slice(2);
|
|
689
|
+
candidates = candidates.map((candidate) => candidate.slice(2));
|
|
690
|
+
}
|
|
691
|
+
let similar = [];
|
|
692
|
+
let bestDistance = maxDistance;
|
|
693
|
+
const minSimilarity = 0.4;
|
|
694
|
+
candidates.forEach((candidate) => {
|
|
695
|
+
if (candidate.length <= 1)
|
|
696
|
+
return;
|
|
697
|
+
const distance = editDistance(word, candidate);
|
|
698
|
+
const length = Math.max(word.length, candidate.length);
|
|
699
|
+
const similarity = (length - distance) / length;
|
|
700
|
+
if (similarity > minSimilarity) {
|
|
701
|
+
if (distance < bestDistance) {
|
|
702
|
+
bestDistance = distance;
|
|
703
|
+
similar = [candidate];
|
|
704
|
+
} else if (distance === bestDistance) {
|
|
705
|
+
similar.push(candidate);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
similar.sort((a, b) => a.localeCompare(b));
|
|
710
|
+
if (searchingOptions) {
|
|
711
|
+
similar = similar.map((candidate) => `--${candidate}`);
|
|
712
|
+
}
|
|
713
|
+
if (similar.length > 1) {
|
|
714
|
+
return `
|
|
715
|
+
(Did you mean one of ${similar.join(", ")}?)`;
|
|
716
|
+
}
|
|
717
|
+
if (similar.length === 1) {
|
|
718
|
+
return `
|
|
719
|
+
(Did you mean ${similar[0]}?)`;
|
|
720
|
+
}
|
|
721
|
+
return "";
|
|
722
|
+
}
|
|
723
|
+
exports.suggestSimilar = suggestSimilar;
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
// node_modules/commander/lib/command.js
|
|
727
|
+
var require_command = __commonJS((exports) => {
|
|
728
|
+
var EventEmitter = __require("events").EventEmitter;
|
|
729
|
+
var childProcess = __require("child_process");
|
|
730
|
+
var path = __require("path");
|
|
731
|
+
var fs = __require("fs");
|
|
732
|
+
var process2 = __require("process");
|
|
733
|
+
var { Argument, humanReadableArgName } = require_argument();
|
|
734
|
+
var { CommanderError } = require_error();
|
|
735
|
+
var { Help, stripColor } = require_help();
|
|
736
|
+
var { Option, DualOptions } = require_option();
|
|
737
|
+
var { suggestSimilar } = require_suggestSimilar();
|
|
738
|
+
|
|
739
|
+
class Command extends EventEmitter {
|
|
740
|
+
constructor(name) {
|
|
741
|
+
super();
|
|
742
|
+
this.commands = [];
|
|
743
|
+
this.options = [];
|
|
744
|
+
this.parent = null;
|
|
745
|
+
this._allowUnknownOption = false;
|
|
746
|
+
this._allowExcessArguments = false;
|
|
747
|
+
this.registeredArguments = [];
|
|
748
|
+
this._args = this.registeredArguments;
|
|
749
|
+
this.args = [];
|
|
750
|
+
this.rawArgs = [];
|
|
751
|
+
this.processedArgs = [];
|
|
752
|
+
this._scriptPath = null;
|
|
753
|
+
this._name = name || "";
|
|
754
|
+
this._optionValues = {};
|
|
755
|
+
this._optionValueSources = {};
|
|
756
|
+
this._storeOptionsAsProperties = false;
|
|
757
|
+
this._actionHandler = null;
|
|
758
|
+
this._executableHandler = false;
|
|
759
|
+
this._executableFile = null;
|
|
760
|
+
this._executableDir = null;
|
|
761
|
+
this._defaultCommandName = null;
|
|
762
|
+
this._exitCallback = null;
|
|
763
|
+
this._aliases = [];
|
|
764
|
+
this._combineFlagAndOptionalValue = true;
|
|
765
|
+
this._description = "";
|
|
766
|
+
this._summary = "";
|
|
767
|
+
this._argsDescription = undefined;
|
|
768
|
+
this._enablePositionalOptions = false;
|
|
769
|
+
this._passThroughOptions = false;
|
|
770
|
+
this._lifeCycleHooks = {};
|
|
771
|
+
this._showHelpAfterError = false;
|
|
772
|
+
this._showSuggestionAfterError = true;
|
|
773
|
+
this._savedState = null;
|
|
774
|
+
this._outputConfiguration = {
|
|
775
|
+
writeOut: (str) => process2.stdout.write(str),
|
|
776
|
+
writeErr: (str) => process2.stderr.write(str),
|
|
777
|
+
outputError: (str, write) => write(str),
|
|
778
|
+
getOutHelpWidth: () => process2.stdout.isTTY ? process2.stdout.columns : undefined,
|
|
779
|
+
getErrHelpWidth: () => process2.stderr.isTTY ? process2.stderr.columns : undefined,
|
|
780
|
+
getOutHasColors: () => useColor() ?? (process2.stdout.isTTY && process2.stdout.hasColors?.()),
|
|
781
|
+
getErrHasColors: () => useColor() ?? (process2.stderr.isTTY && process2.stderr.hasColors?.()),
|
|
782
|
+
stripColor: (str) => stripColor(str)
|
|
783
|
+
};
|
|
784
|
+
this._hidden = false;
|
|
785
|
+
this._helpOption = undefined;
|
|
786
|
+
this._addImplicitHelpCommand = undefined;
|
|
787
|
+
this._helpCommand = undefined;
|
|
788
|
+
this._helpConfiguration = {};
|
|
789
|
+
}
|
|
790
|
+
copyInheritedSettings(sourceCommand) {
|
|
791
|
+
this._outputConfiguration = sourceCommand._outputConfiguration;
|
|
792
|
+
this._helpOption = sourceCommand._helpOption;
|
|
793
|
+
this._helpCommand = sourceCommand._helpCommand;
|
|
794
|
+
this._helpConfiguration = sourceCommand._helpConfiguration;
|
|
795
|
+
this._exitCallback = sourceCommand._exitCallback;
|
|
796
|
+
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
|
|
797
|
+
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
|
|
798
|
+
this._allowExcessArguments = sourceCommand._allowExcessArguments;
|
|
799
|
+
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
|
|
800
|
+
this._showHelpAfterError = sourceCommand._showHelpAfterError;
|
|
801
|
+
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;
|
|
802
|
+
return this;
|
|
803
|
+
}
|
|
804
|
+
_getCommandAndAncestors() {
|
|
805
|
+
const result = [];
|
|
806
|
+
for (let command = this;command; command = command.parent) {
|
|
807
|
+
result.push(command);
|
|
808
|
+
}
|
|
809
|
+
return result;
|
|
810
|
+
}
|
|
811
|
+
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
|
|
812
|
+
let desc = actionOptsOrExecDesc;
|
|
813
|
+
let opts = execOpts;
|
|
814
|
+
if (typeof desc === "object" && desc !== null) {
|
|
815
|
+
opts = desc;
|
|
816
|
+
desc = null;
|
|
817
|
+
}
|
|
818
|
+
opts = opts || {};
|
|
819
|
+
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
|
|
820
|
+
const cmd = this.createCommand(name);
|
|
821
|
+
if (desc) {
|
|
822
|
+
cmd.description(desc);
|
|
823
|
+
cmd._executableHandler = true;
|
|
824
|
+
}
|
|
825
|
+
if (opts.isDefault)
|
|
826
|
+
this._defaultCommandName = cmd._name;
|
|
827
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
828
|
+
cmd._executableFile = opts.executableFile || null;
|
|
829
|
+
if (args)
|
|
830
|
+
cmd.arguments(args);
|
|
831
|
+
this._registerCommand(cmd);
|
|
832
|
+
cmd.parent = this;
|
|
833
|
+
cmd.copyInheritedSettings(this);
|
|
834
|
+
if (desc)
|
|
835
|
+
return this;
|
|
836
|
+
return cmd;
|
|
837
|
+
}
|
|
838
|
+
createCommand(name) {
|
|
839
|
+
return new Command(name);
|
|
840
|
+
}
|
|
841
|
+
createHelp() {
|
|
842
|
+
return Object.assign(new Help, this.configureHelp());
|
|
843
|
+
}
|
|
844
|
+
configureHelp(configuration) {
|
|
845
|
+
if (configuration === undefined)
|
|
846
|
+
return this._helpConfiguration;
|
|
847
|
+
this._helpConfiguration = configuration;
|
|
848
|
+
return this;
|
|
849
|
+
}
|
|
850
|
+
configureOutput(configuration) {
|
|
851
|
+
if (configuration === undefined)
|
|
852
|
+
return this._outputConfiguration;
|
|
853
|
+
Object.assign(this._outputConfiguration, configuration);
|
|
854
|
+
return this;
|
|
855
|
+
}
|
|
856
|
+
showHelpAfterError(displayHelp = true) {
|
|
857
|
+
if (typeof displayHelp !== "string")
|
|
858
|
+
displayHelp = !!displayHelp;
|
|
859
|
+
this._showHelpAfterError = displayHelp;
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
showSuggestionAfterError(displaySuggestion = true) {
|
|
863
|
+
this._showSuggestionAfterError = !!displaySuggestion;
|
|
864
|
+
return this;
|
|
865
|
+
}
|
|
866
|
+
addCommand(cmd, opts) {
|
|
867
|
+
if (!cmd._name) {
|
|
868
|
+
throw new Error(`Command passed to .addCommand() must have a name
|
|
869
|
+
- specify the name in Command constructor or using .name()`);
|
|
870
|
+
}
|
|
871
|
+
opts = opts || {};
|
|
872
|
+
if (opts.isDefault)
|
|
873
|
+
this._defaultCommandName = cmd._name;
|
|
874
|
+
if (opts.noHelp || opts.hidden)
|
|
875
|
+
cmd._hidden = true;
|
|
876
|
+
this._registerCommand(cmd);
|
|
877
|
+
cmd.parent = this;
|
|
878
|
+
cmd._checkForBrokenPassThrough();
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
createArgument(name, description) {
|
|
882
|
+
return new Argument(name, description);
|
|
883
|
+
}
|
|
884
|
+
argument(name, description, fn, defaultValue) {
|
|
885
|
+
const argument = this.createArgument(name, description);
|
|
886
|
+
if (typeof fn === "function") {
|
|
887
|
+
argument.default(defaultValue).argParser(fn);
|
|
888
|
+
} else {
|
|
889
|
+
argument.default(fn);
|
|
890
|
+
}
|
|
891
|
+
this.addArgument(argument);
|
|
892
|
+
return this;
|
|
893
|
+
}
|
|
894
|
+
arguments(names) {
|
|
895
|
+
names.trim().split(/ +/).forEach((detail) => {
|
|
896
|
+
this.argument(detail);
|
|
897
|
+
});
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
addArgument(argument) {
|
|
901
|
+
const previousArgument = this.registeredArguments.slice(-1)[0];
|
|
902
|
+
if (previousArgument && previousArgument.variadic) {
|
|
903
|
+
throw new Error(`only the last argument can be variadic '${previousArgument.name()}'`);
|
|
904
|
+
}
|
|
905
|
+
if (argument.required && argument.defaultValue !== undefined && argument.parseArg === undefined) {
|
|
906
|
+
throw new Error(`a default value for a required argument is never used: '${argument.name()}'`);
|
|
907
|
+
}
|
|
908
|
+
this.registeredArguments.push(argument);
|
|
909
|
+
return this;
|
|
910
|
+
}
|
|
911
|
+
helpCommand(enableOrNameAndArgs, description) {
|
|
912
|
+
if (typeof enableOrNameAndArgs === "boolean") {
|
|
913
|
+
this._addImplicitHelpCommand = enableOrNameAndArgs;
|
|
914
|
+
return this;
|
|
915
|
+
}
|
|
916
|
+
enableOrNameAndArgs = enableOrNameAndArgs ?? "help [command]";
|
|
917
|
+
const [, helpName, helpArgs] = enableOrNameAndArgs.match(/([^ ]+) *(.*)/);
|
|
918
|
+
const helpDescription = description ?? "display help for command";
|
|
919
|
+
const helpCommand = this.createCommand(helpName);
|
|
920
|
+
helpCommand.helpOption(false);
|
|
921
|
+
if (helpArgs)
|
|
922
|
+
helpCommand.arguments(helpArgs);
|
|
923
|
+
if (helpDescription)
|
|
924
|
+
helpCommand.description(helpDescription);
|
|
925
|
+
this._addImplicitHelpCommand = true;
|
|
926
|
+
this._helpCommand = helpCommand;
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
addHelpCommand(helpCommand, deprecatedDescription) {
|
|
930
|
+
if (typeof helpCommand !== "object") {
|
|
931
|
+
this.helpCommand(helpCommand, deprecatedDescription);
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
this._addImplicitHelpCommand = true;
|
|
935
|
+
this._helpCommand = helpCommand;
|
|
936
|
+
return this;
|
|
937
|
+
}
|
|
938
|
+
_getHelpCommand() {
|
|
939
|
+
const hasImplicitHelpCommand = this._addImplicitHelpCommand ?? (this.commands.length && !this._actionHandler && !this._findCommand("help"));
|
|
940
|
+
if (hasImplicitHelpCommand) {
|
|
941
|
+
if (this._helpCommand === undefined) {
|
|
942
|
+
this.helpCommand(undefined, undefined);
|
|
943
|
+
}
|
|
944
|
+
return this._helpCommand;
|
|
945
|
+
}
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
hook(event, listener) {
|
|
949
|
+
const allowedValues = ["preSubcommand", "preAction", "postAction"];
|
|
950
|
+
if (!allowedValues.includes(event)) {
|
|
951
|
+
throw new Error(`Unexpected value for event passed to hook : '${event}'.
|
|
952
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
953
|
+
}
|
|
954
|
+
if (this._lifeCycleHooks[event]) {
|
|
955
|
+
this._lifeCycleHooks[event].push(listener);
|
|
956
|
+
} else {
|
|
957
|
+
this._lifeCycleHooks[event] = [listener];
|
|
958
|
+
}
|
|
959
|
+
return this;
|
|
960
|
+
}
|
|
961
|
+
exitOverride(fn) {
|
|
962
|
+
if (fn) {
|
|
963
|
+
this._exitCallback = fn;
|
|
964
|
+
} else {
|
|
965
|
+
this._exitCallback = (err) => {
|
|
966
|
+
if (err.code !== "commander.executeSubCommandAsync") {
|
|
967
|
+
throw err;
|
|
968
|
+
} else {}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
_exit(exitCode, code, message) {
|
|
974
|
+
if (this._exitCallback) {
|
|
975
|
+
this._exitCallback(new CommanderError(exitCode, code, message));
|
|
976
|
+
}
|
|
977
|
+
process2.exit(exitCode);
|
|
978
|
+
}
|
|
979
|
+
action(fn) {
|
|
980
|
+
const listener = (args) => {
|
|
981
|
+
const expectedArgsCount = this.registeredArguments.length;
|
|
982
|
+
const actionArgs = args.slice(0, expectedArgsCount);
|
|
983
|
+
if (this._storeOptionsAsProperties) {
|
|
984
|
+
actionArgs[expectedArgsCount] = this;
|
|
985
|
+
} else {
|
|
986
|
+
actionArgs[expectedArgsCount] = this.opts();
|
|
987
|
+
}
|
|
988
|
+
actionArgs.push(this);
|
|
989
|
+
return fn.apply(this, actionArgs);
|
|
990
|
+
};
|
|
991
|
+
this._actionHandler = listener;
|
|
992
|
+
return this;
|
|
993
|
+
}
|
|
994
|
+
createOption(flags, description) {
|
|
995
|
+
return new Option(flags, description);
|
|
996
|
+
}
|
|
997
|
+
_callParseArg(target, value, previous, invalidArgumentMessage) {
|
|
998
|
+
try {
|
|
999
|
+
return target.parseArg(value, previous);
|
|
1000
|
+
} catch (err) {
|
|
1001
|
+
if (err.code === "commander.invalidArgument") {
|
|
1002
|
+
const message = `${invalidArgumentMessage} ${err.message}`;
|
|
1003
|
+
this.error(message, { exitCode: err.exitCode, code: err.code });
|
|
1004
|
+
}
|
|
1005
|
+
throw err;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
_registerOption(option) {
|
|
1009
|
+
const matchingOption = option.short && this._findOption(option.short) || option.long && this._findOption(option.long);
|
|
1010
|
+
if (matchingOption) {
|
|
1011
|
+
const matchingFlag = option.long && this._findOption(option.long) ? option.long : option.short;
|
|
1012
|
+
throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'
|
|
1013
|
+
- already used by option '${matchingOption.flags}'`);
|
|
1014
|
+
}
|
|
1015
|
+
this.options.push(option);
|
|
1016
|
+
}
|
|
1017
|
+
_registerCommand(command) {
|
|
1018
|
+
const knownBy = (cmd) => {
|
|
1019
|
+
return [cmd.name()].concat(cmd.aliases());
|
|
1020
|
+
};
|
|
1021
|
+
const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));
|
|
1022
|
+
if (alreadyUsed) {
|
|
1023
|
+
const existingCmd = knownBy(this._findCommand(alreadyUsed)).join("|");
|
|
1024
|
+
const newCmd = knownBy(command).join("|");
|
|
1025
|
+
throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);
|
|
1026
|
+
}
|
|
1027
|
+
this.commands.push(command);
|
|
1028
|
+
}
|
|
1029
|
+
addOption(option) {
|
|
1030
|
+
this._registerOption(option);
|
|
1031
|
+
const oname = option.name();
|
|
1032
|
+
const name = option.attributeName();
|
|
1033
|
+
if (option.negate) {
|
|
1034
|
+
const positiveLongFlag = option.long.replace(/^--no-/, "--");
|
|
1035
|
+
if (!this._findOption(positiveLongFlag)) {
|
|
1036
|
+
this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, "default");
|
|
1037
|
+
}
|
|
1038
|
+
} else if (option.defaultValue !== undefined) {
|
|
1039
|
+
this.setOptionValueWithSource(name, option.defaultValue, "default");
|
|
1040
|
+
}
|
|
1041
|
+
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
|
|
1042
|
+
if (val == null && option.presetArg !== undefined) {
|
|
1043
|
+
val = option.presetArg;
|
|
1044
|
+
}
|
|
1045
|
+
const oldValue = this.getOptionValue(name);
|
|
1046
|
+
if (val !== null && option.parseArg) {
|
|
1047
|
+
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
|
|
1048
|
+
} else if (val !== null && option.variadic) {
|
|
1049
|
+
val = option._concatValue(val, oldValue);
|
|
1050
|
+
}
|
|
1051
|
+
if (val == null) {
|
|
1052
|
+
if (option.negate) {
|
|
1053
|
+
val = false;
|
|
1054
|
+
} else if (option.isBoolean() || option.optional) {
|
|
1055
|
+
val = true;
|
|
1056
|
+
} else {
|
|
1057
|
+
val = "";
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
this.setOptionValueWithSource(name, val, valueSource);
|
|
1061
|
+
};
|
|
1062
|
+
this.on("option:" + oname, (val) => {
|
|
1063
|
+
const invalidValueMessage = `error: option '${option.flags}' argument '${val}' is invalid.`;
|
|
1064
|
+
handleOptionValue(val, invalidValueMessage, "cli");
|
|
1065
|
+
});
|
|
1066
|
+
if (option.envVar) {
|
|
1067
|
+
this.on("optionEnv:" + oname, (val) => {
|
|
1068
|
+
const invalidValueMessage = `error: option '${option.flags}' value '${val}' from env '${option.envVar}' is invalid.`;
|
|
1069
|
+
handleOptionValue(val, invalidValueMessage, "env");
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
return this;
|
|
1073
|
+
}
|
|
1074
|
+
_optionEx(config, flags, description, fn, defaultValue) {
|
|
1075
|
+
if (typeof flags === "object" && flags instanceof Option) {
|
|
1076
|
+
throw new Error("To add an Option object use addOption() instead of option() or requiredOption()");
|
|
1077
|
+
}
|
|
1078
|
+
const option = this.createOption(flags, description);
|
|
1079
|
+
option.makeOptionMandatory(!!config.mandatory);
|
|
1080
|
+
if (typeof fn === "function") {
|
|
1081
|
+
option.default(defaultValue).argParser(fn);
|
|
1082
|
+
} else if (fn instanceof RegExp) {
|
|
1083
|
+
const regex = fn;
|
|
1084
|
+
fn = (val, def) => {
|
|
1085
|
+
const m = regex.exec(val);
|
|
1086
|
+
return m ? m[0] : def;
|
|
1087
|
+
};
|
|
1088
|
+
option.default(defaultValue).argParser(fn);
|
|
1089
|
+
} else {
|
|
1090
|
+
option.default(fn);
|
|
1091
|
+
}
|
|
1092
|
+
return this.addOption(option);
|
|
1093
|
+
}
|
|
1094
|
+
option(flags, description, parseArg, defaultValue) {
|
|
1095
|
+
return this._optionEx({}, flags, description, parseArg, defaultValue);
|
|
1096
|
+
}
|
|
1097
|
+
requiredOption(flags, description, parseArg, defaultValue) {
|
|
1098
|
+
return this._optionEx({ mandatory: true }, flags, description, parseArg, defaultValue);
|
|
1099
|
+
}
|
|
1100
|
+
combineFlagAndOptionalValue(combine = true) {
|
|
1101
|
+
this._combineFlagAndOptionalValue = !!combine;
|
|
1102
|
+
return this;
|
|
1103
|
+
}
|
|
1104
|
+
allowUnknownOption(allowUnknown = true) {
|
|
1105
|
+
this._allowUnknownOption = !!allowUnknown;
|
|
1106
|
+
return this;
|
|
1107
|
+
}
|
|
1108
|
+
allowExcessArguments(allowExcess = true) {
|
|
1109
|
+
this._allowExcessArguments = !!allowExcess;
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
enablePositionalOptions(positional = true) {
|
|
1113
|
+
this._enablePositionalOptions = !!positional;
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
passThroughOptions(passThrough = true) {
|
|
1117
|
+
this._passThroughOptions = !!passThrough;
|
|
1118
|
+
this._checkForBrokenPassThrough();
|
|
1119
|
+
return this;
|
|
1120
|
+
}
|
|
1121
|
+
_checkForBrokenPassThrough() {
|
|
1122
|
+
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
|
|
1123
|
+
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
storeOptionsAsProperties(storeAsProperties = true) {
|
|
1127
|
+
if (this.options.length) {
|
|
1128
|
+
throw new Error("call .storeOptionsAsProperties() before adding options");
|
|
1129
|
+
}
|
|
1130
|
+
if (Object.keys(this._optionValues).length) {
|
|
1131
|
+
throw new Error("call .storeOptionsAsProperties() before setting option values");
|
|
1132
|
+
}
|
|
1133
|
+
this._storeOptionsAsProperties = !!storeAsProperties;
|
|
1134
|
+
return this;
|
|
1135
|
+
}
|
|
1136
|
+
getOptionValue(key) {
|
|
1137
|
+
if (this._storeOptionsAsProperties) {
|
|
1138
|
+
return this[key];
|
|
1139
|
+
}
|
|
1140
|
+
return this._optionValues[key];
|
|
1141
|
+
}
|
|
1142
|
+
setOptionValue(key, value) {
|
|
1143
|
+
return this.setOptionValueWithSource(key, value, undefined);
|
|
1144
|
+
}
|
|
1145
|
+
setOptionValueWithSource(key, value, source) {
|
|
1146
|
+
if (this._storeOptionsAsProperties) {
|
|
1147
|
+
this[key] = value;
|
|
1148
|
+
} else {
|
|
1149
|
+
this._optionValues[key] = value;
|
|
1150
|
+
}
|
|
1151
|
+
this._optionValueSources[key] = source;
|
|
1152
|
+
return this;
|
|
1153
|
+
}
|
|
1154
|
+
getOptionValueSource(key) {
|
|
1155
|
+
return this._optionValueSources[key];
|
|
1156
|
+
}
|
|
1157
|
+
getOptionValueSourceWithGlobals(key) {
|
|
1158
|
+
let source;
|
|
1159
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1160
|
+
if (cmd.getOptionValueSource(key) !== undefined) {
|
|
1161
|
+
source = cmd.getOptionValueSource(key);
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
return source;
|
|
1165
|
+
}
|
|
1166
|
+
_prepareUserArgs(argv, parseOptions) {
|
|
1167
|
+
if (argv !== undefined && !Array.isArray(argv)) {
|
|
1168
|
+
throw new Error("first parameter to parse must be array or undefined");
|
|
1169
|
+
}
|
|
1170
|
+
parseOptions = parseOptions || {};
|
|
1171
|
+
if (argv === undefined && parseOptions.from === undefined) {
|
|
1172
|
+
if (process2.versions?.electron) {
|
|
1173
|
+
parseOptions.from = "electron";
|
|
1174
|
+
}
|
|
1175
|
+
const execArgv = process2.execArgv ?? [];
|
|
1176
|
+
if (execArgv.includes("-e") || execArgv.includes("--eval") || execArgv.includes("-p") || execArgv.includes("--print")) {
|
|
1177
|
+
parseOptions.from = "eval";
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
if (argv === undefined) {
|
|
1181
|
+
argv = process2.argv;
|
|
1182
|
+
}
|
|
1183
|
+
this.rawArgs = argv.slice();
|
|
1184
|
+
let userArgs;
|
|
1185
|
+
switch (parseOptions.from) {
|
|
1186
|
+
case undefined:
|
|
1187
|
+
case "node":
|
|
1188
|
+
this._scriptPath = argv[1];
|
|
1189
|
+
userArgs = argv.slice(2);
|
|
1190
|
+
break;
|
|
1191
|
+
case "electron":
|
|
1192
|
+
if (process2.defaultApp) {
|
|
1193
|
+
this._scriptPath = argv[1];
|
|
1194
|
+
userArgs = argv.slice(2);
|
|
1195
|
+
} else {
|
|
1196
|
+
userArgs = argv.slice(1);
|
|
1197
|
+
}
|
|
1198
|
+
break;
|
|
1199
|
+
case "user":
|
|
1200
|
+
userArgs = argv.slice(0);
|
|
1201
|
+
break;
|
|
1202
|
+
case "eval":
|
|
1203
|
+
userArgs = argv.slice(1);
|
|
1204
|
+
break;
|
|
1205
|
+
default:
|
|
1206
|
+
throw new Error(`unexpected parse option { from: '${parseOptions.from}' }`);
|
|
1207
|
+
}
|
|
1208
|
+
if (!this._name && this._scriptPath)
|
|
1209
|
+
this.nameFromFilename(this._scriptPath);
|
|
1210
|
+
this._name = this._name || "program";
|
|
1211
|
+
return userArgs;
|
|
1212
|
+
}
|
|
1213
|
+
parse(argv, parseOptions) {
|
|
1214
|
+
this._prepareForParse();
|
|
1215
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1216
|
+
this._parseCommand([], userArgs);
|
|
1217
|
+
return this;
|
|
1218
|
+
}
|
|
1219
|
+
async parseAsync(argv, parseOptions) {
|
|
1220
|
+
this._prepareForParse();
|
|
1221
|
+
const userArgs = this._prepareUserArgs(argv, parseOptions);
|
|
1222
|
+
await this._parseCommand([], userArgs);
|
|
1223
|
+
return this;
|
|
1224
|
+
}
|
|
1225
|
+
_prepareForParse() {
|
|
1226
|
+
if (this._savedState === null) {
|
|
1227
|
+
this.saveStateBeforeParse();
|
|
1228
|
+
} else {
|
|
1229
|
+
this.restoreStateBeforeParse();
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
saveStateBeforeParse() {
|
|
1233
|
+
this._savedState = {
|
|
1234
|
+
_name: this._name,
|
|
1235
|
+
_optionValues: { ...this._optionValues },
|
|
1236
|
+
_optionValueSources: { ...this._optionValueSources }
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
restoreStateBeforeParse() {
|
|
1240
|
+
if (this._storeOptionsAsProperties)
|
|
1241
|
+
throw new Error(`Can not call parse again when storeOptionsAsProperties is true.
|
|
1242
|
+
- either make a new Command for each call to parse, or stop storing options as properties`);
|
|
1243
|
+
this._name = this._savedState._name;
|
|
1244
|
+
this._scriptPath = null;
|
|
1245
|
+
this.rawArgs = [];
|
|
1246
|
+
this._optionValues = { ...this._savedState._optionValues };
|
|
1247
|
+
this._optionValueSources = { ...this._savedState._optionValueSources };
|
|
1248
|
+
this.args = [];
|
|
1249
|
+
this.processedArgs = [];
|
|
1250
|
+
}
|
|
1251
|
+
_checkForMissingExecutable(executableFile, executableDir, subcommandName) {
|
|
1252
|
+
if (fs.existsSync(executableFile))
|
|
1253
|
+
return;
|
|
1254
|
+
const executableDirMessage = executableDir ? `searched for local subcommand relative to directory '${executableDir}'` : "no directory for search for local subcommand, use .executableDir() to supply a custom directory";
|
|
1255
|
+
const executableMissing = `'${executableFile}' does not exist
|
|
1256
|
+
- if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
|
|
1257
|
+
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
|
|
1258
|
+
- ${executableDirMessage}`;
|
|
1259
|
+
throw new Error(executableMissing);
|
|
1260
|
+
}
|
|
1261
|
+
_executeSubCommand(subcommand, args) {
|
|
1262
|
+
args = args.slice();
|
|
1263
|
+
let launchWithNode = false;
|
|
1264
|
+
const sourceExt = [".js", ".ts", ".tsx", ".mjs", ".cjs"];
|
|
1265
|
+
function findFile(baseDir, baseName) {
|
|
1266
|
+
const localBin = path.resolve(baseDir, baseName);
|
|
1267
|
+
if (fs.existsSync(localBin))
|
|
1268
|
+
return localBin;
|
|
1269
|
+
if (sourceExt.includes(path.extname(baseName)))
|
|
1270
|
+
return;
|
|
1271
|
+
const foundExt = sourceExt.find((ext) => fs.existsSync(`${localBin}${ext}`));
|
|
1272
|
+
if (foundExt)
|
|
1273
|
+
return `${localBin}${foundExt}`;
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
this._checkForMissingMandatoryOptions();
|
|
1277
|
+
this._checkForConflictingOptions();
|
|
1278
|
+
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
|
|
1279
|
+
let executableDir = this._executableDir || "";
|
|
1280
|
+
if (this._scriptPath) {
|
|
1281
|
+
let resolvedScriptPath;
|
|
1282
|
+
try {
|
|
1283
|
+
resolvedScriptPath = fs.realpathSync(this._scriptPath);
|
|
1284
|
+
} catch {
|
|
1285
|
+
resolvedScriptPath = this._scriptPath;
|
|
1286
|
+
}
|
|
1287
|
+
executableDir = path.resolve(path.dirname(resolvedScriptPath), executableDir);
|
|
1288
|
+
}
|
|
1289
|
+
if (executableDir) {
|
|
1290
|
+
let localFile = findFile(executableDir, executableFile);
|
|
1291
|
+
if (!localFile && !subcommand._executableFile && this._scriptPath) {
|
|
1292
|
+
const legacyName = path.basename(this._scriptPath, path.extname(this._scriptPath));
|
|
1293
|
+
if (legacyName !== this._name) {
|
|
1294
|
+
localFile = findFile(executableDir, `${legacyName}-${subcommand._name}`);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
executableFile = localFile || executableFile;
|
|
1298
|
+
}
|
|
1299
|
+
launchWithNode = sourceExt.includes(path.extname(executableFile));
|
|
1300
|
+
let proc;
|
|
1301
|
+
if (process2.platform !== "win32") {
|
|
1302
|
+
if (launchWithNode) {
|
|
1303
|
+
args.unshift(executableFile);
|
|
1304
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1305
|
+
proc = childProcess.spawn(process2.argv[0], args, { stdio: "inherit" });
|
|
1306
|
+
} else {
|
|
1307
|
+
proc = childProcess.spawn(executableFile, args, { stdio: "inherit" });
|
|
1308
|
+
}
|
|
1309
|
+
} else {
|
|
1310
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1311
|
+
args.unshift(executableFile);
|
|
1312
|
+
args = incrementNodeInspectorPort(process2.execArgv).concat(args);
|
|
1313
|
+
proc = childProcess.spawn(process2.execPath, args, { stdio: "inherit" });
|
|
1314
|
+
}
|
|
1315
|
+
if (!proc.killed) {
|
|
1316
|
+
const signals = ["SIGUSR1", "SIGUSR2", "SIGTERM", "SIGINT", "SIGHUP"];
|
|
1317
|
+
signals.forEach((signal) => {
|
|
1318
|
+
process2.on(signal, () => {
|
|
1319
|
+
if (proc.killed === false && proc.exitCode === null) {
|
|
1320
|
+
proc.kill(signal);
|
|
1321
|
+
}
|
|
1322
|
+
});
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
const exitCallback = this._exitCallback;
|
|
1326
|
+
proc.on("close", (code) => {
|
|
1327
|
+
code = code ?? 1;
|
|
1328
|
+
if (!exitCallback) {
|
|
1329
|
+
process2.exit(code);
|
|
1330
|
+
} else {
|
|
1331
|
+
exitCallback(new CommanderError(code, "commander.executeSubCommandAsync", "(close)"));
|
|
1332
|
+
}
|
|
1333
|
+
});
|
|
1334
|
+
proc.on("error", (err) => {
|
|
1335
|
+
if (err.code === "ENOENT") {
|
|
1336
|
+
this._checkForMissingExecutable(executableFile, executableDir, subcommand._name);
|
|
1337
|
+
} else if (err.code === "EACCES") {
|
|
1338
|
+
throw new Error(`'${executableFile}' not executable`);
|
|
1339
|
+
}
|
|
1340
|
+
if (!exitCallback) {
|
|
1341
|
+
process2.exit(1);
|
|
1342
|
+
} else {
|
|
1343
|
+
const wrappedError = new CommanderError(1, "commander.executeSubCommandAsync", "(error)");
|
|
1344
|
+
wrappedError.nestedError = err;
|
|
1345
|
+
exitCallback(wrappedError);
|
|
1346
|
+
}
|
|
1347
|
+
});
|
|
1348
|
+
this.runningCommand = proc;
|
|
1349
|
+
}
|
|
1350
|
+
_dispatchSubcommand(commandName, operands, unknown) {
|
|
1351
|
+
const subCommand = this._findCommand(commandName);
|
|
1352
|
+
if (!subCommand)
|
|
1353
|
+
this.help({ error: true });
|
|
1354
|
+
subCommand._prepareForParse();
|
|
1355
|
+
let promiseChain;
|
|
1356
|
+
promiseChain = this._chainOrCallSubCommandHook(promiseChain, subCommand, "preSubcommand");
|
|
1357
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1358
|
+
if (subCommand._executableHandler) {
|
|
1359
|
+
this._executeSubCommand(subCommand, operands.concat(unknown));
|
|
1360
|
+
} else {
|
|
1361
|
+
return subCommand._parseCommand(operands, unknown);
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
return promiseChain;
|
|
1365
|
+
}
|
|
1366
|
+
_dispatchHelpCommand(subcommandName) {
|
|
1367
|
+
if (!subcommandName) {
|
|
1368
|
+
this.help();
|
|
1369
|
+
}
|
|
1370
|
+
const subCommand = this._findCommand(subcommandName);
|
|
1371
|
+
if (subCommand && !subCommand._executableHandler) {
|
|
1372
|
+
subCommand.help();
|
|
1373
|
+
}
|
|
1374
|
+
return this._dispatchSubcommand(subcommandName, [], [this._getHelpOption()?.long ?? this._getHelpOption()?.short ?? "--help"]);
|
|
1375
|
+
}
|
|
1376
|
+
_checkNumberOfArguments() {
|
|
1377
|
+
this.registeredArguments.forEach((arg, i) => {
|
|
1378
|
+
if (arg.required && this.args[i] == null) {
|
|
1379
|
+
this.missingArgument(arg.name());
|
|
1380
|
+
}
|
|
1381
|
+
});
|
|
1382
|
+
if (this.registeredArguments.length > 0 && this.registeredArguments[this.registeredArguments.length - 1].variadic) {
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
if (this.args.length > this.registeredArguments.length) {
|
|
1386
|
+
this._excessArguments(this.args);
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
_processArguments() {
|
|
1390
|
+
const myParseArg = (argument, value, previous) => {
|
|
1391
|
+
let parsedValue = value;
|
|
1392
|
+
if (value !== null && argument.parseArg) {
|
|
1393
|
+
const invalidValueMessage = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'.`;
|
|
1394
|
+
parsedValue = this._callParseArg(argument, value, previous, invalidValueMessage);
|
|
1395
|
+
}
|
|
1396
|
+
return parsedValue;
|
|
1397
|
+
};
|
|
1398
|
+
this._checkNumberOfArguments();
|
|
1399
|
+
const processedArgs = [];
|
|
1400
|
+
this.registeredArguments.forEach((declaredArg, index) => {
|
|
1401
|
+
let value = declaredArg.defaultValue;
|
|
1402
|
+
if (declaredArg.variadic) {
|
|
1403
|
+
if (index < this.args.length) {
|
|
1404
|
+
value = this.args.slice(index);
|
|
1405
|
+
if (declaredArg.parseArg) {
|
|
1406
|
+
value = value.reduce((processed, v) => {
|
|
1407
|
+
return myParseArg(declaredArg, v, processed);
|
|
1408
|
+
}, declaredArg.defaultValue);
|
|
1409
|
+
}
|
|
1410
|
+
} else if (value === undefined) {
|
|
1411
|
+
value = [];
|
|
1412
|
+
}
|
|
1413
|
+
} else if (index < this.args.length) {
|
|
1414
|
+
value = this.args[index];
|
|
1415
|
+
if (declaredArg.parseArg) {
|
|
1416
|
+
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
processedArgs[index] = value;
|
|
1420
|
+
});
|
|
1421
|
+
this.processedArgs = processedArgs;
|
|
1422
|
+
}
|
|
1423
|
+
_chainOrCall(promise, fn) {
|
|
1424
|
+
if (promise && promise.then && typeof promise.then === "function") {
|
|
1425
|
+
return promise.then(() => fn());
|
|
1426
|
+
}
|
|
1427
|
+
return fn();
|
|
1428
|
+
}
|
|
1429
|
+
_chainOrCallHooks(promise, event) {
|
|
1430
|
+
let result = promise;
|
|
1431
|
+
const hooks = [];
|
|
1432
|
+
this._getCommandAndAncestors().reverse().filter((cmd) => cmd._lifeCycleHooks[event] !== undefined).forEach((hookedCommand) => {
|
|
1433
|
+
hookedCommand._lifeCycleHooks[event].forEach((callback) => {
|
|
1434
|
+
hooks.push({ hookedCommand, callback });
|
|
1435
|
+
});
|
|
1436
|
+
});
|
|
1437
|
+
if (event === "postAction") {
|
|
1438
|
+
hooks.reverse();
|
|
1439
|
+
}
|
|
1440
|
+
hooks.forEach((hookDetail) => {
|
|
1441
|
+
result = this._chainOrCall(result, () => {
|
|
1442
|
+
return hookDetail.callback(hookDetail.hookedCommand, this);
|
|
1443
|
+
});
|
|
1444
|
+
});
|
|
1445
|
+
return result;
|
|
1446
|
+
}
|
|
1447
|
+
_chainOrCallSubCommandHook(promise, subCommand, event) {
|
|
1448
|
+
let result = promise;
|
|
1449
|
+
if (this._lifeCycleHooks[event] !== undefined) {
|
|
1450
|
+
this._lifeCycleHooks[event].forEach((hook) => {
|
|
1451
|
+
result = this._chainOrCall(result, () => {
|
|
1452
|
+
return hook(this, subCommand);
|
|
1453
|
+
});
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
return result;
|
|
1457
|
+
}
|
|
1458
|
+
_parseCommand(operands, unknown) {
|
|
1459
|
+
const parsed = this.parseOptions(unknown);
|
|
1460
|
+
this._parseOptionsEnv();
|
|
1461
|
+
this._parseOptionsImplied();
|
|
1462
|
+
operands = operands.concat(parsed.operands);
|
|
1463
|
+
unknown = parsed.unknown;
|
|
1464
|
+
this.args = operands.concat(unknown);
|
|
1465
|
+
if (operands && this._findCommand(operands[0])) {
|
|
1466
|
+
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
|
|
1467
|
+
}
|
|
1468
|
+
if (this._getHelpCommand() && operands[0] === this._getHelpCommand().name()) {
|
|
1469
|
+
return this._dispatchHelpCommand(operands[1]);
|
|
1470
|
+
}
|
|
1471
|
+
if (this._defaultCommandName) {
|
|
1472
|
+
this._outputHelpIfRequested(unknown);
|
|
1473
|
+
return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);
|
|
1474
|
+
}
|
|
1475
|
+
if (this.commands.length && this.args.length === 0 && !this._actionHandler && !this._defaultCommandName) {
|
|
1476
|
+
this.help({ error: true });
|
|
1477
|
+
}
|
|
1478
|
+
this._outputHelpIfRequested(parsed.unknown);
|
|
1479
|
+
this._checkForMissingMandatoryOptions();
|
|
1480
|
+
this._checkForConflictingOptions();
|
|
1481
|
+
const checkForUnknownOptions = () => {
|
|
1482
|
+
if (parsed.unknown.length > 0) {
|
|
1483
|
+
this.unknownOption(parsed.unknown[0]);
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
const commandEvent = `command:${this.name()}`;
|
|
1487
|
+
if (this._actionHandler) {
|
|
1488
|
+
checkForUnknownOptions();
|
|
1489
|
+
this._processArguments();
|
|
1490
|
+
let promiseChain;
|
|
1491
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "preAction");
|
|
1492
|
+
promiseChain = this._chainOrCall(promiseChain, () => this._actionHandler(this.processedArgs));
|
|
1493
|
+
if (this.parent) {
|
|
1494
|
+
promiseChain = this._chainOrCall(promiseChain, () => {
|
|
1495
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
promiseChain = this._chainOrCallHooks(promiseChain, "postAction");
|
|
1499
|
+
return promiseChain;
|
|
1500
|
+
}
|
|
1501
|
+
if (this.parent && this.parent.listenerCount(commandEvent)) {
|
|
1502
|
+
checkForUnknownOptions();
|
|
1503
|
+
this._processArguments();
|
|
1504
|
+
this.parent.emit(commandEvent, operands, unknown);
|
|
1505
|
+
} else if (operands.length) {
|
|
1506
|
+
if (this._findCommand("*")) {
|
|
1507
|
+
return this._dispatchSubcommand("*", operands, unknown);
|
|
1508
|
+
}
|
|
1509
|
+
if (this.listenerCount("command:*")) {
|
|
1510
|
+
this.emit("command:*", operands, unknown);
|
|
1511
|
+
} else if (this.commands.length) {
|
|
1512
|
+
this.unknownCommand();
|
|
1513
|
+
} else {
|
|
1514
|
+
checkForUnknownOptions();
|
|
1515
|
+
this._processArguments();
|
|
1516
|
+
}
|
|
1517
|
+
} else if (this.commands.length) {
|
|
1518
|
+
checkForUnknownOptions();
|
|
1519
|
+
this.help({ error: true });
|
|
1520
|
+
} else {
|
|
1521
|
+
checkForUnknownOptions();
|
|
1522
|
+
this._processArguments();
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
_findCommand(name) {
|
|
1526
|
+
if (!name)
|
|
1527
|
+
return;
|
|
1528
|
+
return this.commands.find((cmd) => cmd._name === name || cmd._aliases.includes(name));
|
|
1529
|
+
}
|
|
1530
|
+
_findOption(arg) {
|
|
1531
|
+
return this.options.find((option) => option.is(arg));
|
|
1532
|
+
}
|
|
1533
|
+
_checkForMissingMandatoryOptions() {
|
|
1534
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1535
|
+
cmd.options.forEach((anOption) => {
|
|
1536
|
+
if (anOption.mandatory && cmd.getOptionValue(anOption.attributeName()) === undefined) {
|
|
1537
|
+
cmd.missingMandatoryOptionValue(anOption);
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
});
|
|
1541
|
+
}
|
|
1542
|
+
_checkForConflictingLocalOptions() {
|
|
1543
|
+
const definedNonDefaultOptions = this.options.filter((option) => {
|
|
1544
|
+
const optionKey = option.attributeName();
|
|
1545
|
+
if (this.getOptionValue(optionKey) === undefined) {
|
|
1546
|
+
return false;
|
|
1547
|
+
}
|
|
1548
|
+
return this.getOptionValueSource(optionKey) !== "default";
|
|
1549
|
+
});
|
|
1550
|
+
const optionsWithConflicting = definedNonDefaultOptions.filter((option) => option.conflictsWith.length > 0);
|
|
1551
|
+
optionsWithConflicting.forEach((option) => {
|
|
1552
|
+
const conflictingAndDefined = definedNonDefaultOptions.find((defined) => option.conflictsWith.includes(defined.attributeName()));
|
|
1553
|
+
if (conflictingAndDefined) {
|
|
1554
|
+
this._conflictingOption(option, conflictingAndDefined);
|
|
1555
|
+
}
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
_checkForConflictingOptions() {
|
|
1559
|
+
this._getCommandAndAncestors().forEach((cmd) => {
|
|
1560
|
+
cmd._checkForConflictingLocalOptions();
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
parseOptions(argv) {
|
|
1564
|
+
const operands = [];
|
|
1565
|
+
const unknown = [];
|
|
1566
|
+
let dest = operands;
|
|
1567
|
+
const args = argv.slice();
|
|
1568
|
+
function maybeOption(arg) {
|
|
1569
|
+
return arg.length > 1 && arg[0] === "-";
|
|
1570
|
+
}
|
|
1571
|
+
let activeVariadicOption = null;
|
|
1572
|
+
while (args.length) {
|
|
1573
|
+
const arg = args.shift();
|
|
1574
|
+
if (arg === "--") {
|
|
1575
|
+
if (dest === unknown)
|
|
1576
|
+
dest.push(arg);
|
|
1577
|
+
dest.push(...args);
|
|
1578
|
+
break;
|
|
1579
|
+
}
|
|
1580
|
+
if (activeVariadicOption && !maybeOption(arg)) {
|
|
1581
|
+
this.emit(`option:${activeVariadicOption.name()}`, arg);
|
|
1582
|
+
continue;
|
|
1583
|
+
}
|
|
1584
|
+
activeVariadicOption = null;
|
|
1585
|
+
if (maybeOption(arg)) {
|
|
1586
|
+
const option = this._findOption(arg);
|
|
1587
|
+
if (option) {
|
|
1588
|
+
if (option.required) {
|
|
1589
|
+
const value = args.shift();
|
|
1590
|
+
if (value === undefined)
|
|
1591
|
+
this.optionMissingArgument(option);
|
|
1592
|
+
this.emit(`option:${option.name()}`, value);
|
|
1593
|
+
} else if (option.optional) {
|
|
1594
|
+
let value = null;
|
|
1595
|
+
if (args.length > 0 && !maybeOption(args[0])) {
|
|
1596
|
+
value = args.shift();
|
|
1597
|
+
}
|
|
1598
|
+
this.emit(`option:${option.name()}`, value);
|
|
1599
|
+
} else {
|
|
1600
|
+
this.emit(`option:${option.name()}`);
|
|
1601
|
+
}
|
|
1602
|
+
activeVariadicOption = option.variadic ? option : null;
|
|
1603
|
+
continue;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
if (arg.length > 2 && arg[0] === "-" && arg[1] !== "-") {
|
|
1607
|
+
const option = this._findOption(`-${arg[1]}`);
|
|
1608
|
+
if (option) {
|
|
1609
|
+
if (option.required || option.optional && this._combineFlagAndOptionalValue) {
|
|
1610
|
+
this.emit(`option:${option.name()}`, arg.slice(2));
|
|
1611
|
+
} else {
|
|
1612
|
+
this.emit(`option:${option.name()}`);
|
|
1613
|
+
args.unshift(`-${arg.slice(2)}`);
|
|
1614
|
+
}
|
|
1615
|
+
continue;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
if (/^--[^=]+=/.test(arg)) {
|
|
1619
|
+
const index = arg.indexOf("=");
|
|
1620
|
+
const option = this._findOption(arg.slice(0, index));
|
|
1621
|
+
if (option && (option.required || option.optional)) {
|
|
1622
|
+
this.emit(`option:${option.name()}`, arg.slice(index + 1));
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
if (maybeOption(arg)) {
|
|
1627
|
+
dest = unknown;
|
|
1628
|
+
}
|
|
1629
|
+
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
|
|
1630
|
+
if (this._findCommand(arg)) {
|
|
1631
|
+
operands.push(arg);
|
|
1632
|
+
if (args.length > 0)
|
|
1633
|
+
unknown.push(...args);
|
|
1634
|
+
break;
|
|
1635
|
+
} else if (this._getHelpCommand() && arg === this._getHelpCommand().name()) {
|
|
1636
|
+
operands.push(arg);
|
|
1637
|
+
if (args.length > 0)
|
|
1638
|
+
operands.push(...args);
|
|
1639
|
+
break;
|
|
1640
|
+
} else if (this._defaultCommandName) {
|
|
1641
|
+
unknown.push(arg);
|
|
1642
|
+
if (args.length > 0)
|
|
1643
|
+
unknown.push(...args);
|
|
1644
|
+
break;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
if (this._passThroughOptions) {
|
|
1648
|
+
dest.push(arg);
|
|
1649
|
+
if (args.length > 0)
|
|
1650
|
+
dest.push(...args);
|
|
1651
|
+
break;
|
|
1652
|
+
}
|
|
1653
|
+
dest.push(arg);
|
|
1654
|
+
}
|
|
1655
|
+
return { operands, unknown };
|
|
1656
|
+
}
|
|
1657
|
+
opts() {
|
|
1658
|
+
if (this._storeOptionsAsProperties) {
|
|
1659
|
+
const result = {};
|
|
1660
|
+
const len = this.options.length;
|
|
1661
|
+
for (let i = 0;i < len; i++) {
|
|
1662
|
+
const key = this.options[i].attributeName();
|
|
1663
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1664
|
+
}
|
|
1665
|
+
return result;
|
|
1666
|
+
}
|
|
1667
|
+
return this._optionValues;
|
|
1668
|
+
}
|
|
1669
|
+
optsWithGlobals() {
|
|
1670
|
+
return this._getCommandAndAncestors().reduce((combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {});
|
|
1671
|
+
}
|
|
1672
|
+
error(message, errorOptions) {
|
|
1673
|
+
this._outputConfiguration.outputError(`${message}
|
|
1674
|
+
`, this._outputConfiguration.writeErr);
|
|
1675
|
+
if (typeof this._showHelpAfterError === "string") {
|
|
1676
|
+
this._outputConfiguration.writeErr(`${this._showHelpAfterError}
|
|
1677
|
+
`);
|
|
1678
|
+
} else if (this._showHelpAfterError) {
|
|
1679
|
+
this._outputConfiguration.writeErr(`
|
|
1680
|
+
`);
|
|
1681
|
+
this.outputHelp({ error: true });
|
|
1682
|
+
}
|
|
1683
|
+
const config = errorOptions || {};
|
|
1684
|
+
const exitCode = config.exitCode || 1;
|
|
1685
|
+
const code = config.code || "commander.error";
|
|
1686
|
+
this._exit(exitCode, code, message);
|
|
1687
|
+
}
|
|
1688
|
+
_parseOptionsEnv() {
|
|
1689
|
+
this.options.forEach((option) => {
|
|
1690
|
+
if (option.envVar && option.envVar in process2.env) {
|
|
1691
|
+
const optionKey = option.attributeName();
|
|
1692
|
+
if (this.getOptionValue(optionKey) === undefined || ["default", "config", "env"].includes(this.getOptionValueSource(optionKey))) {
|
|
1693
|
+
if (option.required || option.optional) {
|
|
1694
|
+
this.emit(`optionEnv:${option.name()}`, process2.env[option.envVar]);
|
|
1695
|
+
} else {
|
|
1696
|
+
this.emit(`optionEnv:${option.name()}`);
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
_parseOptionsImplied() {
|
|
1703
|
+
const dualHelper = new DualOptions(this.options);
|
|
1704
|
+
const hasCustomOptionValue = (optionKey) => {
|
|
1705
|
+
return this.getOptionValue(optionKey) !== undefined && !["default", "implied"].includes(this.getOptionValueSource(optionKey));
|
|
1706
|
+
};
|
|
1707
|
+
this.options.filter((option) => option.implied !== undefined && hasCustomOptionValue(option.attributeName()) && dualHelper.valueFromOption(this.getOptionValue(option.attributeName()), option)).forEach((option) => {
|
|
1708
|
+
Object.keys(option.implied).filter((impliedKey) => !hasCustomOptionValue(impliedKey)).forEach((impliedKey) => {
|
|
1709
|
+
this.setOptionValueWithSource(impliedKey, option.implied[impliedKey], "implied");
|
|
1710
|
+
});
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
missingArgument(name) {
|
|
1714
|
+
const message = `error: missing required argument '${name}'`;
|
|
1715
|
+
this.error(message, { code: "commander.missingArgument" });
|
|
1716
|
+
}
|
|
1717
|
+
optionMissingArgument(option) {
|
|
1718
|
+
const message = `error: option '${option.flags}' argument missing`;
|
|
1719
|
+
this.error(message, { code: "commander.optionMissingArgument" });
|
|
1720
|
+
}
|
|
1721
|
+
missingMandatoryOptionValue(option) {
|
|
1722
|
+
const message = `error: required option '${option.flags}' not specified`;
|
|
1723
|
+
this.error(message, { code: "commander.missingMandatoryOptionValue" });
|
|
1724
|
+
}
|
|
1725
|
+
_conflictingOption(option, conflictingOption) {
|
|
1726
|
+
const findBestOptionFromValue = (option2) => {
|
|
1727
|
+
const optionKey = option2.attributeName();
|
|
1728
|
+
const optionValue = this.getOptionValue(optionKey);
|
|
1729
|
+
const negativeOption = this.options.find((target) => target.negate && optionKey === target.attributeName());
|
|
1730
|
+
const positiveOption = this.options.find((target) => !target.negate && optionKey === target.attributeName());
|
|
1731
|
+
if (negativeOption && (negativeOption.presetArg === undefined && optionValue === false || negativeOption.presetArg !== undefined && optionValue === negativeOption.presetArg)) {
|
|
1732
|
+
return negativeOption;
|
|
1733
|
+
}
|
|
1734
|
+
return positiveOption || option2;
|
|
1735
|
+
};
|
|
1736
|
+
const getErrorMessage = (option2) => {
|
|
1737
|
+
const bestOption = findBestOptionFromValue(option2);
|
|
1738
|
+
const optionKey = bestOption.attributeName();
|
|
1739
|
+
const source = this.getOptionValueSource(optionKey);
|
|
1740
|
+
if (source === "env") {
|
|
1741
|
+
return `environment variable '${bestOption.envVar}'`;
|
|
1742
|
+
}
|
|
1743
|
+
return `option '${bestOption.flags}'`;
|
|
1744
|
+
};
|
|
1745
|
+
const message = `error: ${getErrorMessage(option)} cannot be used with ${getErrorMessage(conflictingOption)}`;
|
|
1746
|
+
this.error(message, { code: "commander.conflictingOption" });
|
|
1747
|
+
}
|
|
1748
|
+
unknownOption(flag) {
|
|
1749
|
+
if (this._allowUnknownOption)
|
|
1750
|
+
return;
|
|
1751
|
+
let suggestion = "";
|
|
1752
|
+
if (flag.startsWith("--") && this._showSuggestionAfterError) {
|
|
1753
|
+
let candidateFlags = [];
|
|
1754
|
+
let command = this;
|
|
1755
|
+
do {
|
|
1756
|
+
const moreFlags = command.createHelp().visibleOptions(command).filter((option) => option.long).map((option) => option.long);
|
|
1757
|
+
candidateFlags = candidateFlags.concat(moreFlags);
|
|
1758
|
+
command = command.parent;
|
|
1759
|
+
} while (command && !command._enablePositionalOptions);
|
|
1760
|
+
suggestion = suggestSimilar(flag, candidateFlags);
|
|
1761
|
+
}
|
|
1762
|
+
const message = `error: unknown option '${flag}'${suggestion}`;
|
|
1763
|
+
this.error(message, { code: "commander.unknownOption" });
|
|
1764
|
+
}
|
|
1765
|
+
_excessArguments(receivedArgs) {
|
|
1766
|
+
if (this._allowExcessArguments)
|
|
1767
|
+
return;
|
|
1768
|
+
const expected = this.registeredArguments.length;
|
|
1769
|
+
const s = expected === 1 ? "" : "s";
|
|
1770
|
+
const forSubcommand = this.parent ? ` for '${this.name()}'` : "";
|
|
1771
|
+
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
|
|
1772
|
+
this.error(message, { code: "commander.excessArguments" });
|
|
1773
|
+
}
|
|
1774
|
+
unknownCommand() {
|
|
1775
|
+
const unknownName = this.args[0];
|
|
1776
|
+
let suggestion = "";
|
|
1777
|
+
if (this._showSuggestionAfterError) {
|
|
1778
|
+
const candidateNames = [];
|
|
1779
|
+
this.createHelp().visibleCommands(this).forEach((command) => {
|
|
1780
|
+
candidateNames.push(command.name());
|
|
1781
|
+
if (command.alias())
|
|
1782
|
+
candidateNames.push(command.alias());
|
|
1783
|
+
});
|
|
1784
|
+
suggestion = suggestSimilar(unknownName, candidateNames);
|
|
1785
|
+
}
|
|
1786
|
+
const message = `error: unknown command '${unknownName}'${suggestion}`;
|
|
1787
|
+
this.error(message, { code: "commander.unknownCommand" });
|
|
1788
|
+
}
|
|
1789
|
+
version(str, flags, description) {
|
|
1790
|
+
if (str === undefined)
|
|
1791
|
+
return this._version;
|
|
1792
|
+
this._version = str;
|
|
1793
|
+
flags = flags || "-V, --version";
|
|
1794
|
+
description = description || "output the version number";
|
|
1795
|
+
const versionOption = this.createOption(flags, description);
|
|
1796
|
+
this._versionOptionName = versionOption.attributeName();
|
|
1797
|
+
this._registerOption(versionOption);
|
|
1798
|
+
this.on("option:" + versionOption.name(), () => {
|
|
1799
|
+
this._outputConfiguration.writeOut(`${str}
|
|
1800
|
+
`);
|
|
1801
|
+
this._exit(0, "commander.version", str);
|
|
1802
|
+
});
|
|
1803
|
+
return this;
|
|
1804
|
+
}
|
|
1805
|
+
description(str, argsDescription) {
|
|
1806
|
+
if (str === undefined && argsDescription === undefined)
|
|
1807
|
+
return this._description;
|
|
1808
|
+
this._description = str;
|
|
1809
|
+
if (argsDescription) {
|
|
1810
|
+
this._argsDescription = argsDescription;
|
|
1811
|
+
}
|
|
1812
|
+
return this;
|
|
1813
|
+
}
|
|
1814
|
+
summary(str) {
|
|
1815
|
+
if (str === undefined)
|
|
1816
|
+
return this._summary;
|
|
1817
|
+
this._summary = str;
|
|
1818
|
+
return this;
|
|
1819
|
+
}
|
|
1820
|
+
alias(alias) {
|
|
1821
|
+
if (alias === undefined)
|
|
1822
|
+
return this._aliases[0];
|
|
1823
|
+
let command = this;
|
|
1824
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1825
|
+
command = this.commands[this.commands.length - 1];
|
|
1826
|
+
}
|
|
1827
|
+
if (alias === command._name)
|
|
1828
|
+
throw new Error("Command alias can't be the same as its name");
|
|
1829
|
+
const matchingCommand = this.parent?._findCommand(alias);
|
|
1830
|
+
if (matchingCommand) {
|
|
1831
|
+
const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join("|");
|
|
1832
|
+
throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);
|
|
1833
|
+
}
|
|
1834
|
+
command._aliases.push(alias);
|
|
1835
|
+
return this;
|
|
1836
|
+
}
|
|
1837
|
+
aliases(aliases) {
|
|
1838
|
+
if (aliases === undefined)
|
|
1839
|
+
return this._aliases;
|
|
1840
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1841
|
+
return this;
|
|
1842
|
+
}
|
|
1843
|
+
usage(str) {
|
|
1844
|
+
if (str === undefined) {
|
|
1845
|
+
if (this._usage)
|
|
1846
|
+
return this._usage;
|
|
1847
|
+
const args = this.registeredArguments.map((arg) => {
|
|
1848
|
+
return humanReadableArgName(arg);
|
|
1849
|
+
});
|
|
1850
|
+
return [].concat(this.options.length || this._helpOption !== null ? "[options]" : [], this.commands.length ? "[command]" : [], this.registeredArguments.length ? args : []).join(" ");
|
|
1851
|
+
}
|
|
1852
|
+
this._usage = str;
|
|
1853
|
+
return this;
|
|
1854
|
+
}
|
|
1855
|
+
name(str) {
|
|
1856
|
+
if (str === undefined)
|
|
1857
|
+
return this._name;
|
|
1858
|
+
this._name = str;
|
|
1859
|
+
return this;
|
|
1860
|
+
}
|
|
1861
|
+
nameFromFilename(filename) {
|
|
1862
|
+
this._name = path.basename(filename, path.extname(filename));
|
|
1863
|
+
return this;
|
|
1864
|
+
}
|
|
1865
|
+
executableDir(path2) {
|
|
1866
|
+
if (path2 === undefined)
|
|
1867
|
+
return this._executableDir;
|
|
1868
|
+
this._executableDir = path2;
|
|
1869
|
+
return this;
|
|
1870
|
+
}
|
|
1871
|
+
helpInformation(contextOptions) {
|
|
1872
|
+
const helper = this.createHelp();
|
|
1873
|
+
const context = this._getOutputContext(contextOptions);
|
|
1874
|
+
helper.prepareContext({
|
|
1875
|
+
error: context.error,
|
|
1876
|
+
helpWidth: context.helpWidth,
|
|
1877
|
+
outputHasColors: context.hasColors
|
|
1878
|
+
});
|
|
1879
|
+
const text = helper.formatHelp(this, helper);
|
|
1880
|
+
if (context.hasColors)
|
|
1881
|
+
return text;
|
|
1882
|
+
return this._outputConfiguration.stripColor(text);
|
|
1883
|
+
}
|
|
1884
|
+
_getOutputContext(contextOptions) {
|
|
1885
|
+
contextOptions = contextOptions || {};
|
|
1886
|
+
const error = !!contextOptions.error;
|
|
1887
|
+
let baseWrite;
|
|
1888
|
+
let hasColors;
|
|
1889
|
+
let helpWidth;
|
|
1890
|
+
if (error) {
|
|
1891
|
+
baseWrite = (str) => this._outputConfiguration.writeErr(str);
|
|
1892
|
+
hasColors = this._outputConfiguration.getErrHasColors();
|
|
1893
|
+
helpWidth = this._outputConfiguration.getErrHelpWidth();
|
|
1894
|
+
} else {
|
|
1895
|
+
baseWrite = (str) => this._outputConfiguration.writeOut(str);
|
|
1896
|
+
hasColors = this._outputConfiguration.getOutHasColors();
|
|
1897
|
+
helpWidth = this._outputConfiguration.getOutHelpWidth();
|
|
1898
|
+
}
|
|
1899
|
+
const write = (str) => {
|
|
1900
|
+
if (!hasColors)
|
|
1901
|
+
str = this._outputConfiguration.stripColor(str);
|
|
1902
|
+
return baseWrite(str);
|
|
1903
|
+
};
|
|
1904
|
+
return { error, write, hasColors, helpWidth };
|
|
1905
|
+
}
|
|
1906
|
+
outputHelp(contextOptions) {
|
|
1907
|
+
let deprecatedCallback;
|
|
1908
|
+
if (typeof contextOptions === "function") {
|
|
1909
|
+
deprecatedCallback = contextOptions;
|
|
1910
|
+
contextOptions = undefined;
|
|
1911
|
+
}
|
|
1912
|
+
const outputContext = this._getOutputContext(contextOptions);
|
|
1913
|
+
const eventContext = {
|
|
1914
|
+
error: outputContext.error,
|
|
1915
|
+
write: outputContext.write,
|
|
1916
|
+
command: this
|
|
1917
|
+
};
|
|
1918
|
+
this._getCommandAndAncestors().reverse().forEach((command) => command.emit("beforeAllHelp", eventContext));
|
|
1919
|
+
this.emit("beforeHelp", eventContext);
|
|
1920
|
+
let helpInformation = this.helpInformation({ error: outputContext.error });
|
|
1921
|
+
if (deprecatedCallback) {
|
|
1922
|
+
helpInformation = deprecatedCallback(helpInformation);
|
|
1923
|
+
if (typeof helpInformation !== "string" && !Buffer.isBuffer(helpInformation)) {
|
|
1924
|
+
throw new Error("outputHelp callback must return a string or a Buffer");
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
outputContext.write(helpInformation);
|
|
1928
|
+
if (this._getHelpOption()?.long) {
|
|
1929
|
+
this.emit(this._getHelpOption().long);
|
|
1930
|
+
}
|
|
1931
|
+
this.emit("afterHelp", eventContext);
|
|
1932
|
+
this._getCommandAndAncestors().forEach((command) => command.emit("afterAllHelp", eventContext));
|
|
1933
|
+
}
|
|
1934
|
+
helpOption(flags, description) {
|
|
1935
|
+
if (typeof flags === "boolean") {
|
|
1936
|
+
if (flags) {
|
|
1937
|
+
this._helpOption = this._helpOption ?? undefined;
|
|
1938
|
+
} else {
|
|
1939
|
+
this._helpOption = null;
|
|
1940
|
+
}
|
|
1941
|
+
return this;
|
|
1942
|
+
}
|
|
1943
|
+
flags = flags ?? "-h, --help";
|
|
1944
|
+
description = description ?? "display help for command";
|
|
1945
|
+
this._helpOption = this.createOption(flags, description);
|
|
1946
|
+
return this;
|
|
1947
|
+
}
|
|
1948
|
+
_getHelpOption() {
|
|
1949
|
+
if (this._helpOption === undefined) {
|
|
1950
|
+
this.helpOption(undefined, undefined);
|
|
1951
|
+
}
|
|
1952
|
+
return this._helpOption;
|
|
1953
|
+
}
|
|
1954
|
+
addHelpOption(option) {
|
|
1955
|
+
this._helpOption = option;
|
|
1956
|
+
return this;
|
|
1957
|
+
}
|
|
1958
|
+
help(contextOptions) {
|
|
1959
|
+
this.outputHelp(contextOptions);
|
|
1960
|
+
let exitCode = Number(process2.exitCode ?? 0);
|
|
1961
|
+
if (exitCode === 0 && contextOptions && typeof contextOptions !== "function" && contextOptions.error) {
|
|
1962
|
+
exitCode = 1;
|
|
1963
|
+
}
|
|
1964
|
+
this._exit(exitCode, "commander.help", "(outputHelp)");
|
|
1965
|
+
}
|
|
1966
|
+
addHelpText(position, text) {
|
|
1967
|
+
const allowedValues = ["beforeAll", "before", "after", "afterAll"];
|
|
1968
|
+
if (!allowedValues.includes(position)) {
|
|
1969
|
+
throw new Error(`Unexpected value for position to addHelpText.
|
|
1970
|
+
Expecting one of '${allowedValues.join("', '")}'`);
|
|
1971
|
+
}
|
|
1972
|
+
const helpEvent = `${position}Help`;
|
|
1973
|
+
this.on(helpEvent, (context) => {
|
|
1974
|
+
let helpStr;
|
|
1975
|
+
if (typeof text === "function") {
|
|
1976
|
+
helpStr = text({ error: context.error, command: context.command });
|
|
1977
|
+
} else {
|
|
1978
|
+
helpStr = text;
|
|
1979
|
+
}
|
|
1980
|
+
if (helpStr) {
|
|
1981
|
+
context.write(`${helpStr}
|
|
1982
|
+
`);
|
|
1983
|
+
}
|
|
1984
|
+
});
|
|
1985
|
+
return this;
|
|
1986
|
+
}
|
|
1987
|
+
_outputHelpIfRequested(args) {
|
|
1988
|
+
const helpOption = this._getHelpOption();
|
|
1989
|
+
const helpRequested = helpOption && args.find((arg) => helpOption.is(arg));
|
|
1990
|
+
if (helpRequested) {
|
|
1991
|
+
this.outputHelp();
|
|
1992
|
+
this._exit(0, "commander.helpDisplayed", "(outputHelp)");
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function incrementNodeInspectorPort(args) {
|
|
1997
|
+
return args.map((arg) => {
|
|
1998
|
+
if (!arg.startsWith("--inspect")) {
|
|
1999
|
+
return arg;
|
|
2000
|
+
}
|
|
2001
|
+
let debugOption;
|
|
2002
|
+
let debugHost = "127.0.0.1";
|
|
2003
|
+
let debugPort = "9229";
|
|
2004
|
+
let match;
|
|
2005
|
+
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
|
|
2006
|
+
debugOption = match[1];
|
|
2007
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
|
|
2008
|
+
debugOption = match[1];
|
|
2009
|
+
if (/^\d+$/.test(match[3])) {
|
|
2010
|
+
debugPort = match[3];
|
|
2011
|
+
} else {
|
|
2012
|
+
debugHost = match[3];
|
|
2013
|
+
}
|
|
2014
|
+
} else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) {
|
|
2015
|
+
debugOption = match[1];
|
|
2016
|
+
debugHost = match[3];
|
|
2017
|
+
debugPort = match[4];
|
|
2018
|
+
}
|
|
2019
|
+
if (debugOption && debugPort !== "0") {
|
|
2020
|
+
return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`;
|
|
2021
|
+
}
|
|
2022
|
+
return arg;
|
|
2023
|
+
});
|
|
2024
|
+
}
|
|
2025
|
+
function useColor() {
|
|
2026
|
+
if (process2.env.NO_COLOR || process2.env.FORCE_COLOR === "0" || process2.env.FORCE_COLOR === "false")
|
|
2027
|
+
return false;
|
|
2028
|
+
if (process2.env.FORCE_COLOR || process2.env.CLICOLOR_FORCE !== undefined)
|
|
2029
|
+
return true;
|
|
2030
|
+
return;
|
|
2031
|
+
}
|
|
2032
|
+
exports.Command = Command;
|
|
2033
|
+
exports.useColor = useColor;
|
|
2034
|
+
});
|
|
2035
|
+
|
|
2036
|
+
// node_modules/commander/index.js
|
|
2037
|
+
var require_commander = __commonJS((exports) => {
|
|
2038
|
+
var { Argument } = require_argument();
|
|
2039
|
+
var { Command } = require_command();
|
|
2040
|
+
var { CommanderError, InvalidArgumentError } = require_error();
|
|
2041
|
+
var { Help } = require_help();
|
|
2042
|
+
var { Option } = require_option();
|
|
2043
|
+
exports.program = new Command;
|
|
2044
|
+
exports.createCommand = (name) => new Command(name);
|
|
2045
|
+
exports.createOption = (flags, description) => new Option(flags, description);
|
|
2046
|
+
exports.createArgument = (name, description) => new Argument(name, description);
|
|
2047
|
+
exports.Command = Command;
|
|
2048
|
+
exports.Option = Option;
|
|
2049
|
+
exports.Argument = Argument;
|
|
2050
|
+
exports.Help = Help;
|
|
2051
|
+
exports.CommanderError = CommanderError;
|
|
2052
|
+
exports.InvalidArgumentError = InvalidArgumentError;
|
|
2053
|
+
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2054
|
+
});
|
|
2055
|
+
|
|
2056
|
+
// node_modules/commander/esm.mjs
|
|
2057
|
+
var import__ = __toESM(require_commander(), 1);
|
|
2058
|
+
var {
|
|
2059
|
+
program,
|
|
2060
|
+
createCommand,
|
|
2061
|
+
createArgument,
|
|
2062
|
+
createOption,
|
|
2063
|
+
CommanderError,
|
|
2064
|
+
InvalidArgumentError,
|
|
2065
|
+
InvalidOptionArgumentError,
|
|
2066
|
+
Command,
|
|
2067
|
+
Argument,
|
|
2068
|
+
Option,
|
|
2069
|
+
Help
|
|
2070
|
+
} = import__.default;
|
|
2071
|
+
|
|
2072
|
+
// src/cli/index.tsx
|
|
2073
|
+
import chalk from "chalk";
|
|
2074
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
2075
|
+
import { dirname as dirname3, join as join3, resolve as resolve3 } from "path";
|
|
2076
|
+
import { fileURLToPath } from "url";
|
|
2077
|
+
|
|
2078
|
+
// src/db/database.ts
|
|
2079
|
+
import { Database } from "bun:sqlite";
|
|
2080
|
+
import { existsSync, mkdirSync } from "fs";
|
|
2081
|
+
import { dirname, join, resolve } from "path";
|
|
2082
|
+
function isInMemoryDb(path) {
|
|
2083
|
+
return path === ":memory:" || path.startsWith("file::memory:");
|
|
2084
|
+
}
|
|
2085
|
+
function findNearestMementosDb(startDir) {
|
|
2086
|
+
let dir = resolve(startDir);
|
|
2087
|
+
while (true) {
|
|
2088
|
+
const candidate = join(dir, ".mementos", "mementos.db");
|
|
2089
|
+
if (existsSync(candidate))
|
|
2090
|
+
return candidate;
|
|
2091
|
+
const parent = dirname(dir);
|
|
2092
|
+
if (parent === dir)
|
|
2093
|
+
break;
|
|
2094
|
+
dir = parent;
|
|
2095
|
+
}
|
|
2096
|
+
return null;
|
|
2097
|
+
}
|
|
2098
|
+
function findGitRoot(startDir) {
|
|
2099
|
+
let dir = resolve(startDir);
|
|
2100
|
+
while (true) {
|
|
2101
|
+
if (existsSync(join(dir, ".git")))
|
|
2102
|
+
return dir;
|
|
2103
|
+
const parent = dirname(dir);
|
|
2104
|
+
if (parent === dir)
|
|
2105
|
+
break;
|
|
2106
|
+
dir = parent;
|
|
2107
|
+
}
|
|
2108
|
+
return null;
|
|
2109
|
+
}
|
|
2110
|
+
function getDbPath() {
|
|
2111
|
+
if (process.env["MEMENTOS_DB_PATH"]) {
|
|
2112
|
+
return process.env["MEMENTOS_DB_PATH"];
|
|
2113
|
+
}
|
|
2114
|
+
const cwd = process.cwd();
|
|
2115
|
+
const nearest = findNearestMementosDb(cwd);
|
|
2116
|
+
if (nearest)
|
|
2117
|
+
return nearest;
|
|
2118
|
+
if (process.env["MEMENTOS_DB_SCOPE"] === "project") {
|
|
2119
|
+
const gitRoot = findGitRoot(cwd);
|
|
2120
|
+
if (gitRoot) {
|
|
2121
|
+
return join(gitRoot, ".mementos", "mementos.db");
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
|
|
2125
|
+
return join(home, ".mementos", "mementos.db");
|
|
2126
|
+
}
|
|
2127
|
+
function ensureDir(filePath) {
|
|
2128
|
+
if (isInMemoryDb(filePath))
|
|
2129
|
+
return;
|
|
2130
|
+
const dir = dirname(resolve(filePath));
|
|
2131
|
+
if (!existsSync(dir)) {
|
|
2132
|
+
mkdirSync(dir, { recursive: true });
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
var MIGRATIONS = [
|
|
2136
|
+
`
|
|
2137
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
2138
|
+
id TEXT PRIMARY KEY,
|
|
2139
|
+
name TEXT NOT NULL,
|
|
2140
|
+
path TEXT UNIQUE NOT NULL,
|
|
2141
|
+
description TEXT,
|
|
2142
|
+
memory_prefix TEXT,
|
|
2143
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2144
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2145
|
+
);
|
|
2146
|
+
|
|
2147
|
+
CREATE TABLE IF NOT EXISTS agents (
|
|
2148
|
+
id TEXT PRIMARY KEY,
|
|
2149
|
+
name TEXT NOT NULL UNIQUE,
|
|
2150
|
+
description TEXT,
|
|
2151
|
+
role TEXT DEFAULT 'agent',
|
|
2152
|
+
metadata TEXT DEFAULT '{}',
|
|
2153
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2154
|
+
last_seen_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2155
|
+
);
|
|
2156
|
+
|
|
2157
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
2158
|
+
id TEXT PRIMARY KEY,
|
|
2159
|
+
key TEXT NOT NULL,
|
|
2160
|
+
value TEXT NOT NULL,
|
|
2161
|
+
category TEXT NOT NULL DEFAULT 'knowledge' CHECK(category IN ('preference', 'fact', 'knowledge', 'history')),
|
|
2162
|
+
scope TEXT NOT NULL DEFAULT 'private' CHECK(scope IN ('global', 'shared', 'private')),
|
|
2163
|
+
summary TEXT,
|
|
2164
|
+
tags TEXT DEFAULT '[]',
|
|
2165
|
+
importance INTEGER NOT NULL DEFAULT 5 CHECK(importance >= 1 AND importance <= 10),
|
|
2166
|
+
source TEXT NOT NULL DEFAULT 'agent' CHECK(source IN ('user', 'agent', 'system', 'auto', 'imported')),
|
|
2167
|
+
status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active', 'archived', 'expired')),
|
|
2168
|
+
pinned INTEGER NOT NULL DEFAULT 0,
|
|
2169
|
+
agent_id TEXT REFERENCES agents(id) ON DELETE SET NULL,
|
|
2170
|
+
project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
|
|
2171
|
+
session_id TEXT,
|
|
2172
|
+
metadata TEXT DEFAULT '{}',
|
|
2173
|
+
access_count INTEGER NOT NULL DEFAULT 0,
|
|
2174
|
+
version INTEGER NOT NULL DEFAULT 1,
|
|
2175
|
+
expires_at TEXT,
|
|
2176
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2177
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2178
|
+
accessed_at TEXT
|
|
2179
|
+
);
|
|
2180
|
+
|
|
2181
|
+
CREATE TABLE IF NOT EXISTS memory_tags (
|
|
2182
|
+
memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
|
|
2183
|
+
tag TEXT NOT NULL,
|
|
2184
|
+
PRIMARY KEY (memory_id, tag)
|
|
2185
|
+
);
|
|
2186
|
+
|
|
2187
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
2188
|
+
id TEXT PRIMARY KEY,
|
|
2189
|
+
agent_id TEXT REFERENCES agents(id) ON DELETE SET NULL,
|
|
2190
|
+
project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
|
|
2191
|
+
started_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2192
|
+
last_activity TEXT NOT NULL DEFAULT (datetime('now')),
|
|
2193
|
+
metadata TEXT DEFAULT '{}'
|
|
2194
|
+
);
|
|
2195
|
+
|
|
2196
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_memories_unique_key
|
|
2197
|
+
ON memories(key, scope, COALESCE(agent_id, ''), COALESCE(project_id, ''), COALESCE(session_id, ''));
|
|
2198
|
+
|
|
2199
|
+
CREATE INDEX IF NOT EXISTS idx_memories_key ON memories(key);
|
|
2200
|
+
CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope);
|
|
2201
|
+
CREATE INDEX IF NOT EXISTS idx_memories_category ON memories(category);
|
|
2202
|
+
CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status);
|
|
2203
|
+
CREATE INDEX IF NOT EXISTS idx_memories_importance ON memories(importance);
|
|
2204
|
+
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
|
2205
|
+
CREATE INDEX IF NOT EXISTS idx_memories_project ON memories(project_id);
|
|
2206
|
+
CREATE INDEX IF NOT EXISTS idx_memories_session ON memories(session_id);
|
|
2207
|
+
CREATE INDEX IF NOT EXISTS idx_memories_pinned ON memories(pinned);
|
|
2208
|
+
CREATE INDEX IF NOT EXISTS idx_memories_expires ON memories(expires_at);
|
|
2209
|
+
CREATE INDEX IF NOT EXISTS idx_memories_created ON memories(created_at);
|
|
2210
|
+
CREATE INDEX IF NOT EXISTS idx_memory_tags_tag ON memory_tags(tag);
|
|
2211
|
+
CREATE INDEX IF NOT EXISTS idx_memory_tags_memory ON memory_tags(memory_id);
|
|
2212
|
+
CREATE INDEX IF NOT EXISTS idx_agents_name ON agents(name);
|
|
2213
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_agent ON sessions(agent_id);
|
|
2214
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_project ON sessions(project_id);
|
|
2215
|
+
|
|
2216
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
2217
|
+
id INTEGER PRIMARY KEY,
|
|
2218
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2219
|
+
);
|
|
2220
|
+
|
|
2221
|
+
INSERT OR IGNORE INTO _migrations (id) VALUES (1);
|
|
2222
|
+
`
|
|
2223
|
+
];
|
|
2224
|
+
var _db = null;
|
|
2225
|
+
function getDatabase(dbPath) {
|
|
2226
|
+
if (_db)
|
|
2227
|
+
return _db;
|
|
2228
|
+
const path = dbPath || getDbPath();
|
|
2229
|
+
ensureDir(path);
|
|
2230
|
+
_db = new Database(path, { create: true });
|
|
2231
|
+
_db.run("PRAGMA journal_mode = WAL");
|
|
2232
|
+
_db.run("PRAGMA busy_timeout = 5000");
|
|
2233
|
+
_db.run("PRAGMA foreign_keys = ON");
|
|
2234
|
+
runMigrations(_db);
|
|
2235
|
+
return _db;
|
|
2236
|
+
}
|
|
2237
|
+
function runMigrations(db) {
|
|
2238
|
+
try {
|
|
2239
|
+
const result = db.query("SELECT MAX(id) as max_id FROM _migrations").get();
|
|
2240
|
+
const currentLevel = result?.max_id ?? 0;
|
|
2241
|
+
for (let i = currentLevel;i < MIGRATIONS.length; i++) {
|
|
2242
|
+
try {
|
|
2243
|
+
db.exec(MIGRATIONS[i]);
|
|
2244
|
+
} catch {}
|
|
2245
|
+
}
|
|
2246
|
+
} catch {
|
|
2247
|
+
for (const migration of MIGRATIONS) {
|
|
2248
|
+
try {
|
|
2249
|
+
db.exec(migration);
|
|
2250
|
+
} catch {}
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
function now() {
|
|
2255
|
+
return new Date().toISOString();
|
|
2256
|
+
}
|
|
2257
|
+
function uuid() {
|
|
2258
|
+
return crypto.randomUUID();
|
|
2259
|
+
}
|
|
2260
|
+
function shortUuid() {
|
|
2261
|
+
return crypto.randomUUID().slice(0, 8);
|
|
2262
|
+
}
|
|
2263
|
+
function resolvePartialId(db, table, partialId) {
|
|
2264
|
+
if (partialId.length >= 36) {
|
|
2265
|
+
const row = db.query(`SELECT id FROM ${table} WHERE id = ?`).get(partialId);
|
|
2266
|
+
return row?.id ?? null;
|
|
2267
|
+
}
|
|
2268
|
+
const rows = db.query(`SELECT id FROM ${table} WHERE id LIKE ?`).all(`${partialId}%`);
|
|
2269
|
+
if (rows.length === 1) {
|
|
2270
|
+
return rows[0].id;
|
|
2271
|
+
}
|
|
2272
|
+
return null;
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
// src/types/index.ts
|
|
2276
|
+
class MemoryNotFoundError extends Error {
|
|
2277
|
+
constructor(id) {
|
|
2278
|
+
super(`Memory not found: ${id}`);
|
|
2279
|
+
this.name = "MemoryNotFoundError";
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
class VersionConflictError extends Error {
|
|
2283
|
+
expected;
|
|
2284
|
+
actual;
|
|
2285
|
+
constructor(id, expected, actual) {
|
|
2286
|
+
super(`Version conflict for memory ${id}: expected ${expected}, got ${actual}`);
|
|
2287
|
+
this.name = "VersionConflictError";
|
|
2288
|
+
this.expected = expected;
|
|
2289
|
+
this.actual = actual;
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
// src/db/memories.ts
|
|
2294
|
+
function parseMemoryRow(row) {
|
|
2295
|
+
return {
|
|
2296
|
+
id: row["id"],
|
|
2297
|
+
key: row["key"],
|
|
2298
|
+
value: row["value"],
|
|
2299
|
+
category: row["category"],
|
|
2300
|
+
scope: row["scope"],
|
|
2301
|
+
summary: row["summary"] || null,
|
|
2302
|
+
tags: JSON.parse(row["tags"] || "[]"),
|
|
2303
|
+
importance: row["importance"],
|
|
2304
|
+
source: row["source"],
|
|
2305
|
+
status: row["status"],
|
|
2306
|
+
pinned: !!row["pinned"],
|
|
2307
|
+
agent_id: row["agent_id"] || null,
|
|
2308
|
+
project_id: row["project_id"] || null,
|
|
2309
|
+
session_id: row["session_id"] || null,
|
|
2310
|
+
metadata: JSON.parse(row["metadata"] || "{}"),
|
|
2311
|
+
access_count: row["access_count"],
|
|
2312
|
+
version: row["version"],
|
|
2313
|
+
expires_at: row["expires_at"] || null,
|
|
2314
|
+
created_at: row["created_at"],
|
|
2315
|
+
updated_at: row["updated_at"],
|
|
2316
|
+
accessed_at: row["accessed_at"] || null
|
|
2317
|
+
};
|
|
2318
|
+
}
|
|
2319
|
+
function createMemory(input, dedupeMode = "merge", db) {
|
|
2320
|
+
const d = db || getDatabase();
|
|
2321
|
+
const timestamp = now();
|
|
2322
|
+
let expiresAt = input.expires_at || null;
|
|
2323
|
+
if (input.ttl_ms && !expiresAt) {
|
|
2324
|
+
expiresAt = new Date(Date.now() + input.ttl_ms).toISOString();
|
|
2325
|
+
}
|
|
2326
|
+
const id = uuid();
|
|
2327
|
+
const tags = input.tags || [];
|
|
2328
|
+
const tagsJson = JSON.stringify(tags);
|
|
2329
|
+
const metadataJson = JSON.stringify(input.metadata || {});
|
|
2330
|
+
if (dedupeMode === "merge") {
|
|
2331
|
+
const existing = d.query(`SELECT id, version FROM memories
|
|
2332
|
+
WHERE key = ? AND scope = ?
|
|
2333
|
+
AND COALESCE(agent_id, '') = ?
|
|
2334
|
+
AND COALESCE(project_id, '') = ?
|
|
2335
|
+
AND COALESCE(session_id, '') = ?`).get(input.key, input.scope || "private", input.agent_id || "", input.project_id || "", input.session_id || "");
|
|
2336
|
+
if (existing) {
|
|
2337
|
+
d.run(`UPDATE memories SET
|
|
2338
|
+
value = ?, category = ?, summary = ?, tags = ?,
|
|
2339
|
+
importance = ?, metadata = ?, expires_at = ?,
|
|
2340
|
+
pinned = COALESCE(pinned, 0),
|
|
2341
|
+
version = version + 1, updated_at = ?
|
|
2342
|
+
WHERE id = ?`, [
|
|
2343
|
+
input.value,
|
|
2344
|
+
input.category || "knowledge",
|
|
2345
|
+
input.summary || null,
|
|
2346
|
+
tagsJson,
|
|
2347
|
+
input.importance ?? 5,
|
|
2348
|
+
metadataJson,
|
|
2349
|
+
expiresAt,
|
|
2350
|
+
timestamp,
|
|
2351
|
+
existing.id
|
|
2352
|
+
]);
|
|
2353
|
+
d.run("DELETE FROM memory_tags WHERE memory_id = ?", [existing.id]);
|
|
2354
|
+
const insertTag2 = d.prepare("INSERT OR IGNORE INTO memory_tags (memory_id, tag) VALUES (?, ?)");
|
|
2355
|
+
for (const tag of tags) {
|
|
2356
|
+
insertTag2.run(existing.id, tag);
|
|
2357
|
+
}
|
|
2358
|
+
return getMemory(existing.id, d);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
d.run(`INSERT INTO memories (id, key, value, category, scope, summary, tags, importance, source, status, pinned, agent_id, project_id, session_id, metadata, access_count, version, expires_at, created_at, updated_at)
|
|
2362
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', 0, ?, ?, ?, ?, 0, 1, ?, ?, ?)`, [
|
|
2363
|
+
id,
|
|
2364
|
+
input.key,
|
|
2365
|
+
input.value,
|
|
2366
|
+
input.category || "knowledge",
|
|
2367
|
+
input.scope || "private",
|
|
2368
|
+
input.summary || null,
|
|
2369
|
+
tagsJson,
|
|
2370
|
+
input.importance ?? 5,
|
|
2371
|
+
input.source || "agent",
|
|
2372
|
+
input.agent_id || null,
|
|
2373
|
+
input.project_id || null,
|
|
2374
|
+
input.session_id || null,
|
|
2375
|
+
metadataJson,
|
|
2376
|
+
expiresAt,
|
|
2377
|
+
timestamp,
|
|
2378
|
+
timestamp
|
|
2379
|
+
]);
|
|
2380
|
+
const insertTag = d.prepare("INSERT OR IGNORE INTO memory_tags (memory_id, tag) VALUES (?, ?)");
|
|
2381
|
+
for (const tag of tags) {
|
|
2382
|
+
insertTag.run(id, tag);
|
|
2383
|
+
}
|
|
2384
|
+
return getMemory(id, d);
|
|
2385
|
+
}
|
|
2386
|
+
function getMemory(id, db) {
|
|
2387
|
+
const d = db || getDatabase();
|
|
2388
|
+
const row = d.query("SELECT * FROM memories WHERE id = ?").get(id);
|
|
2389
|
+
if (!row)
|
|
2390
|
+
return null;
|
|
2391
|
+
return parseMemoryRow(row);
|
|
2392
|
+
}
|
|
2393
|
+
function getMemoryByKey(key, scope, agentId, projectId, sessionId, db) {
|
|
2394
|
+
const d = db || getDatabase();
|
|
2395
|
+
let sql = "SELECT * FROM memories WHERE key = ?";
|
|
2396
|
+
const params = [key];
|
|
2397
|
+
if (scope) {
|
|
2398
|
+
sql += " AND scope = ?";
|
|
2399
|
+
params.push(scope);
|
|
2400
|
+
}
|
|
2401
|
+
if (agentId) {
|
|
2402
|
+
sql += " AND agent_id = ?";
|
|
2403
|
+
params.push(agentId);
|
|
2404
|
+
}
|
|
2405
|
+
if (projectId) {
|
|
2406
|
+
sql += " AND project_id = ?";
|
|
2407
|
+
params.push(projectId);
|
|
2408
|
+
}
|
|
2409
|
+
if (sessionId) {
|
|
2410
|
+
sql += " AND session_id = ?";
|
|
2411
|
+
params.push(sessionId);
|
|
2412
|
+
}
|
|
2413
|
+
sql += " AND status = 'active' ORDER BY importance DESC LIMIT 1";
|
|
2414
|
+
const row = d.query(sql).get(...params);
|
|
2415
|
+
if (!row)
|
|
2416
|
+
return null;
|
|
2417
|
+
return parseMemoryRow(row);
|
|
2418
|
+
}
|
|
2419
|
+
function listMemories(filter, db) {
|
|
2420
|
+
const d = db || getDatabase();
|
|
2421
|
+
const conditions = [];
|
|
2422
|
+
const params = [];
|
|
2423
|
+
if (filter) {
|
|
2424
|
+
if (filter.scope) {
|
|
2425
|
+
if (Array.isArray(filter.scope)) {
|
|
2426
|
+
conditions.push(`scope IN (${filter.scope.map(() => "?").join(",")})`);
|
|
2427
|
+
params.push(...filter.scope);
|
|
2428
|
+
} else {
|
|
2429
|
+
conditions.push("scope = ?");
|
|
2430
|
+
params.push(filter.scope);
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
if (filter.category) {
|
|
2434
|
+
if (Array.isArray(filter.category)) {
|
|
2435
|
+
conditions.push(`category IN (${filter.category.map(() => "?").join(",")})`);
|
|
2436
|
+
params.push(...filter.category);
|
|
2437
|
+
} else {
|
|
2438
|
+
conditions.push("category = ?");
|
|
2439
|
+
params.push(filter.category);
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
if (filter.source) {
|
|
2443
|
+
if (Array.isArray(filter.source)) {
|
|
2444
|
+
conditions.push(`source IN (${filter.source.map(() => "?").join(",")})`);
|
|
2445
|
+
params.push(...filter.source);
|
|
2446
|
+
} else {
|
|
2447
|
+
conditions.push("source = ?");
|
|
2448
|
+
params.push(filter.source);
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
if (filter.status) {
|
|
2452
|
+
if (Array.isArray(filter.status)) {
|
|
2453
|
+
conditions.push(`status IN (${filter.status.map(() => "?").join(",")})`);
|
|
2454
|
+
params.push(...filter.status);
|
|
2455
|
+
} else {
|
|
2456
|
+
conditions.push("status = ?");
|
|
2457
|
+
params.push(filter.status);
|
|
2458
|
+
}
|
|
2459
|
+
} else {
|
|
2460
|
+
conditions.push("status = 'active'");
|
|
2461
|
+
}
|
|
2462
|
+
if (filter.project_id) {
|
|
2463
|
+
conditions.push("project_id = ?");
|
|
2464
|
+
params.push(filter.project_id);
|
|
2465
|
+
}
|
|
2466
|
+
if (filter.agent_id) {
|
|
2467
|
+
conditions.push("agent_id = ?");
|
|
2468
|
+
params.push(filter.agent_id);
|
|
2469
|
+
}
|
|
2470
|
+
if (filter.session_id) {
|
|
2471
|
+
conditions.push("session_id = ?");
|
|
2472
|
+
params.push(filter.session_id);
|
|
2473
|
+
}
|
|
2474
|
+
if (filter.min_importance) {
|
|
2475
|
+
conditions.push("importance >= ?");
|
|
2476
|
+
params.push(filter.min_importance);
|
|
2477
|
+
}
|
|
2478
|
+
if (filter.pinned !== undefined) {
|
|
2479
|
+
conditions.push("pinned = ?");
|
|
2480
|
+
params.push(filter.pinned ? 1 : 0);
|
|
2481
|
+
}
|
|
2482
|
+
if (filter.tags && filter.tags.length > 0) {
|
|
2483
|
+
for (const tag of filter.tags) {
|
|
2484
|
+
conditions.push("id IN (SELECT memory_id FROM memory_tags WHERE tag = ?)");
|
|
2485
|
+
params.push(tag);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
if (filter.search) {
|
|
2489
|
+
conditions.push("(key LIKE ? OR value LIKE ? OR summary LIKE ?)");
|
|
2490
|
+
const term = `%${filter.search}%`;
|
|
2491
|
+
params.push(term, term, term);
|
|
2492
|
+
}
|
|
2493
|
+
} else {
|
|
2494
|
+
conditions.push("status = 'active'");
|
|
2495
|
+
}
|
|
2496
|
+
let sql = "SELECT * FROM memories";
|
|
2497
|
+
if (conditions.length > 0) {
|
|
2498
|
+
sql += ` WHERE ${conditions.join(" AND ")}`;
|
|
2499
|
+
}
|
|
2500
|
+
sql += " ORDER BY importance DESC, created_at DESC";
|
|
2501
|
+
if (filter?.limit) {
|
|
2502
|
+
sql += " LIMIT ?";
|
|
2503
|
+
params.push(filter.limit);
|
|
2504
|
+
}
|
|
2505
|
+
if (filter?.offset) {
|
|
2506
|
+
sql += " OFFSET ?";
|
|
2507
|
+
params.push(filter.offset);
|
|
2508
|
+
}
|
|
2509
|
+
const rows = d.query(sql).all(...params);
|
|
2510
|
+
return rows.map(parseMemoryRow);
|
|
2511
|
+
}
|
|
2512
|
+
function updateMemory(id, input, db) {
|
|
2513
|
+
const d = db || getDatabase();
|
|
2514
|
+
const existing = getMemory(id, d);
|
|
2515
|
+
if (!existing)
|
|
2516
|
+
throw new MemoryNotFoundError(id);
|
|
2517
|
+
if (existing.version !== input.version) {
|
|
2518
|
+
throw new VersionConflictError(id, input.version, existing.version);
|
|
2519
|
+
}
|
|
2520
|
+
const sets = ["version = version + 1", "updated_at = ?"];
|
|
2521
|
+
const params = [now()];
|
|
2522
|
+
if (input.value !== undefined) {
|
|
2523
|
+
sets.push("value = ?");
|
|
2524
|
+
params.push(input.value);
|
|
2525
|
+
}
|
|
2526
|
+
if (input.category !== undefined) {
|
|
2527
|
+
sets.push("category = ?");
|
|
2528
|
+
params.push(input.category);
|
|
2529
|
+
}
|
|
2530
|
+
if (input.scope !== undefined) {
|
|
2531
|
+
sets.push("scope = ?");
|
|
2532
|
+
params.push(input.scope);
|
|
2533
|
+
}
|
|
2534
|
+
if (input.summary !== undefined) {
|
|
2535
|
+
sets.push("summary = ?");
|
|
2536
|
+
params.push(input.summary);
|
|
2537
|
+
}
|
|
2538
|
+
if (input.importance !== undefined) {
|
|
2539
|
+
sets.push("importance = ?");
|
|
2540
|
+
params.push(input.importance);
|
|
2541
|
+
}
|
|
2542
|
+
if (input.pinned !== undefined) {
|
|
2543
|
+
sets.push("pinned = ?");
|
|
2544
|
+
params.push(input.pinned ? 1 : 0);
|
|
2545
|
+
}
|
|
2546
|
+
if (input.status !== undefined) {
|
|
2547
|
+
sets.push("status = ?");
|
|
2548
|
+
params.push(input.status);
|
|
2549
|
+
}
|
|
2550
|
+
if (input.metadata !== undefined) {
|
|
2551
|
+
sets.push("metadata = ?");
|
|
2552
|
+
params.push(JSON.stringify(input.metadata));
|
|
2553
|
+
}
|
|
2554
|
+
if (input.expires_at !== undefined) {
|
|
2555
|
+
sets.push("expires_at = ?");
|
|
2556
|
+
params.push(input.expires_at);
|
|
2557
|
+
}
|
|
2558
|
+
if (input.tags !== undefined) {
|
|
2559
|
+
sets.push("tags = ?");
|
|
2560
|
+
params.push(JSON.stringify(input.tags));
|
|
2561
|
+
d.run("DELETE FROM memory_tags WHERE memory_id = ?", [id]);
|
|
2562
|
+
const insertTag = d.prepare("INSERT OR IGNORE INTO memory_tags (memory_id, tag) VALUES (?, ?)");
|
|
2563
|
+
for (const tag of input.tags) {
|
|
2564
|
+
insertTag.run(id, tag);
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
params.push(id);
|
|
2568
|
+
d.run(`UPDATE memories SET ${sets.join(", ")} WHERE id = ?`, params);
|
|
2569
|
+
return getMemory(id, d);
|
|
2570
|
+
}
|
|
2571
|
+
function deleteMemory(id, db) {
|
|
2572
|
+
const d = db || getDatabase();
|
|
2573
|
+
const result = d.run("DELETE FROM memories WHERE id = ?", [id]);
|
|
2574
|
+
return result.changes > 0;
|
|
2575
|
+
}
|
|
2576
|
+
function bulkDeleteMemories(ids, db) {
|
|
2577
|
+
const d = db || getDatabase();
|
|
2578
|
+
if (ids.length === 0)
|
|
2579
|
+
return 0;
|
|
2580
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
2581
|
+
const result = d.run(`DELETE FROM memories WHERE id IN (${placeholders})`, ids);
|
|
2582
|
+
return result.changes;
|
|
2583
|
+
}
|
|
2584
|
+
function touchMemory(id, db) {
|
|
2585
|
+
const d = db || getDatabase();
|
|
2586
|
+
d.run("UPDATE memories SET access_count = access_count + 1, accessed_at = ? WHERE id = ?", [now(), id]);
|
|
2587
|
+
}
|
|
2588
|
+
function cleanExpiredMemories(db) {
|
|
2589
|
+
const d = db || getDatabase();
|
|
2590
|
+
const timestamp = now();
|
|
2591
|
+
const result = d.run("DELETE FROM memories WHERE expires_at IS NOT NULL AND expires_at < ?", [timestamp]);
|
|
2592
|
+
return result.changes;
|
|
2593
|
+
}
|
|
2594
|
+
|
|
2595
|
+
// src/db/agents.ts
|
|
2596
|
+
function parseAgentRow(row) {
|
|
2597
|
+
return {
|
|
2598
|
+
id: row["id"],
|
|
2599
|
+
name: row["name"],
|
|
2600
|
+
description: row["description"] || null,
|
|
2601
|
+
role: row["role"] || null,
|
|
2602
|
+
metadata: JSON.parse(row["metadata"] || "{}"),
|
|
2603
|
+
created_at: row["created_at"],
|
|
2604
|
+
last_seen_at: row["last_seen_at"]
|
|
2605
|
+
};
|
|
2606
|
+
}
|
|
2607
|
+
function registerAgent(name, description, role, db) {
|
|
2608
|
+
const d = db || getDatabase();
|
|
2609
|
+
const timestamp = now();
|
|
2610
|
+
const existing = d.query("SELECT * FROM agents WHERE name = ?").get(name);
|
|
2611
|
+
if (existing) {
|
|
2612
|
+
const existingId = existing["id"];
|
|
2613
|
+
d.run("UPDATE agents SET last_seen_at = ? WHERE id = ?", [
|
|
2614
|
+
timestamp,
|
|
2615
|
+
existingId
|
|
2616
|
+
]);
|
|
2617
|
+
if (description) {
|
|
2618
|
+
d.run("UPDATE agents SET description = ? WHERE id = ?", [
|
|
2619
|
+
description,
|
|
2620
|
+
existingId
|
|
2621
|
+
]);
|
|
2622
|
+
}
|
|
2623
|
+
if (role) {
|
|
2624
|
+
d.run("UPDATE agents SET role = ? WHERE id = ?", [
|
|
2625
|
+
role,
|
|
2626
|
+
existingId
|
|
2627
|
+
]);
|
|
2628
|
+
}
|
|
2629
|
+
return getAgent(existingId, d);
|
|
2630
|
+
}
|
|
2631
|
+
const id = shortUuid();
|
|
2632
|
+
d.run("INSERT INTO agents (id, name, description, role, created_at, last_seen_at) VALUES (?, ?, ?, ?, ?, ?)", [id, name, description || null, role || "agent", timestamp, timestamp]);
|
|
2633
|
+
return getAgent(id, d);
|
|
2634
|
+
}
|
|
2635
|
+
function getAgent(idOrName, db) {
|
|
2636
|
+
const d = db || getDatabase();
|
|
2637
|
+
let row = d.query("SELECT * FROM agents WHERE id = ?").get(idOrName);
|
|
2638
|
+
if (row)
|
|
2639
|
+
return parseAgentRow(row);
|
|
2640
|
+
row = d.query("SELECT * FROM agents WHERE name = ?").get(idOrName);
|
|
2641
|
+
if (row)
|
|
2642
|
+
return parseAgentRow(row);
|
|
2643
|
+
const rows = d.query("SELECT * FROM agents WHERE id LIKE ?").all(`${idOrName}%`);
|
|
2644
|
+
if (rows.length === 1)
|
|
2645
|
+
return parseAgentRow(rows[0]);
|
|
2646
|
+
return null;
|
|
2647
|
+
}
|
|
2648
|
+
function listAgents(db) {
|
|
2649
|
+
const d = db || getDatabase();
|
|
2650
|
+
const rows = d.query("SELECT * FROM agents ORDER BY last_seen_at DESC").all();
|
|
2651
|
+
return rows.map(parseAgentRow);
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
// src/db/projects.ts
|
|
2655
|
+
function parseProjectRow(row) {
|
|
2656
|
+
return {
|
|
2657
|
+
id: row["id"],
|
|
2658
|
+
name: row["name"],
|
|
2659
|
+
path: row["path"],
|
|
2660
|
+
description: row["description"] || null,
|
|
2661
|
+
memory_prefix: row["memory_prefix"] || null,
|
|
2662
|
+
created_at: row["created_at"],
|
|
2663
|
+
updated_at: row["updated_at"]
|
|
2664
|
+
};
|
|
2665
|
+
}
|
|
2666
|
+
function registerProject(name, path, description, memoryPrefix, db) {
|
|
2667
|
+
const d = db || getDatabase();
|
|
2668
|
+
const timestamp = now();
|
|
2669
|
+
const existing = d.query("SELECT * FROM projects WHERE path = ?").get(path);
|
|
2670
|
+
if (existing) {
|
|
2671
|
+
const existingId = existing["id"];
|
|
2672
|
+
d.run("UPDATE projects SET updated_at = ? WHERE id = ?", [
|
|
2673
|
+
timestamp,
|
|
2674
|
+
existingId
|
|
2675
|
+
]);
|
|
2676
|
+
return parseProjectRow(existing);
|
|
2677
|
+
}
|
|
2678
|
+
const id = uuid();
|
|
2679
|
+
d.run("INSERT INTO projects (id, name, path, description, memory_prefix, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", [id, name, path, description || null, memoryPrefix || null, timestamp, timestamp]);
|
|
2680
|
+
return getProject(id, d);
|
|
2681
|
+
}
|
|
2682
|
+
function getProject(idOrPath, db) {
|
|
2683
|
+
const d = db || getDatabase();
|
|
2684
|
+
let row = d.query("SELECT * FROM projects WHERE id = ?").get(idOrPath);
|
|
2685
|
+
if (row)
|
|
2686
|
+
return parseProjectRow(row);
|
|
2687
|
+
row = d.query("SELECT * FROM projects WHERE path = ?").get(idOrPath);
|
|
2688
|
+
if (row)
|
|
2689
|
+
return parseProjectRow(row);
|
|
2690
|
+
return null;
|
|
2691
|
+
}
|
|
2692
|
+
function listProjects(db) {
|
|
2693
|
+
const d = db || getDatabase();
|
|
2694
|
+
const rows = d.query("SELECT * FROM projects ORDER BY updated_at DESC").all();
|
|
2695
|
+
return rows.map(parseProjectRow);
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
// src/lib/search.ts
|
|
2699
|
+
function parseMemoryRow2(row) {
|
|
2700
|
+
return {
|
|
2701
|
+
id: row["id"],
|
|
2702
|
+
key: row["key"],
|
|
2703
|
+
value: row["value"],
|
|
2704
|
+
category: row["category"],
|
|
2705
|
+
scope: row["scope"],
|
|
2706
|
+
summary: row["summary"] || null,
|
|
2707
|
+
tags: JSON.parse(row["tags"] || "[]"),
|
|
2708
|
+
importance: row["importance"],
|
|
2709
|
+
source: row["source"],
|
|
2710
|
+
status: row["status"],
|
|
2711
|
+
pinned: !!row["pinned"],
|
|
2712
|
+
agent_id: row["agent_id"] || null,
|
|
2713
|
+
project_id: row["project_id"] || null,
|
|
2714
|
+
session_id: row["session_id"] || null,
|
|
2715
|
+
metadata: JSON.parse(row["metadata"] || "{}"),
|
|
2716
|
+
access_count: row["access_count"],
|
|
2717
|
+
version: row["version"],
|
|
2718
|
+
expires_at: row["expires_at"] || null,
|
|
2719
|
+
created_at: row["created_at"],
|
|
2720
|
+
updated_at: row["updated_at"],
|
|
2721
|
+
accessed_at: row["accessed_at"] || null
|
|
2722
|
+
};
|
|
2723
|
+
}
|
|
2724
|
+
function determineMatchType(memory, queryLower) {
|
|
2725
|
+
if (memory.key.toLowerCase() === queryLower)
|
|
2726
|
+
return "exact";
|
|
2727
|
+
if (memory.tags.some((t) => t.toLowerCase() === queryLower))
|
|
2728
|
+
return "tag";
|
|
2729
|
+
return "fuzzy";
|
|
2730
|
+
}
|
|
2731
|
+
function computeScore(memory, queryLower) {
|
|
2732
|
+
let score = 0;
|
|
2733
|
+
const keyLower = memory.key.toLowerCase();
|
|
2734
|
+
if (keyLower === queryLower) {
|
|
2735
|
+
score += 10;
|
|
2736
|
+
} else if (keyLower.includes(queryLower)) {
|
|
2737
|
+
score += 7;
|
|
2738
|
+
}
|
|
2739
|
+
if (memory.tags.some((t) => t.toLowerCase() === queryLower)) {
|
|
2740
|
+
score += 6;
|
|
2741
|
+
}
|
|
2742
|
+
if (memory.summary && memory.summary.toLowerCase().includes(queryLower)) {
|
|
2743
|
+
score += 4;
|
|
2744
|
+
}
|
|
2745
|
+
if (memory.value.toLowerCase().includes(queryLower)) {
|
|
2746
|
+
score += 3;
|
|
2747
|
+
}
|
|
2748
|
+
return score;
|
|
2749
|
+
}
|
|
2750
|
+
function searchMemories(query, filter, db) {
|
|
2751
|
+
const d = db || getDatabase();
|
|
2752
|
+
const queryLower = query.toLowerCase();
|
|
2753
|
+
const queryParam = `%${query}%`;
|
|
2754
|
+
const conditions = [];
|
|
2755
|
+
const params = [];
|
|
2756
|
+
conditions.push("m.status = 'active'");
|
|
2757
|
+
conditions.push("(m.expires_at IS NULL OR m.expires_at >= datetime('now'))");
|
|
2758
|
+
conditions.push(`(m.key LIKE ? OR m.value LIKE ? OR m.summary LIKE ? OR m.id IN (SELECT memory_id FROM memory_tags WHERE tag LIKE ?))`);
|
|
2759
|
+
params.push(queryParam, queryParam, queryParam, queryParam);
|
|
2760
|
+
if (filter) {
|
|
2761
|
+
if (filter.scope) {
|
|
2762
|
+
if (Array.isArray(filter.scope)) {
|
|
2763
|
+
conditions.push(`m.scope IN (${filter.scope.map(() => "?").join(",")})`);
|
|
2764
|
+
params.push(...filter.scope);
|
|
2765
|
+
} else {
|
|
2766
|
+
conditions.push("m.scope = ?");
|
|
2767
|
+
params.push(filter.scope);
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
if (filter.category) {
|
|
2771
|
+
if (Array.isArray(filter.category)) {
|
|
2772
|
+
conditions.push(`m.category IN (${filter.category.map(() => "?").join(",")})`);
|
|
2773
|
+
params.push(...filter.category);
|
|
2774
|
+
} else {
|
|
2775
|
+
conditions.push("m.category = ?");
|
|
2776
|
+
params.push(filter.category);
|
|
2777
|
+
}
|
|
2778
|
+
}
|
|
2779
|
+
if (filter.source) {
|
|
2780
|
+
if (Array.isArray(filter.source)) {
|
|
2781
|
+
conditions.push(`m.source IN (${filter.source.map(() => "?").join(",")})`);
|
|
2782
|
+
params.push(...filter.source);
|
|
2783
|
+
} else {
|
|
2784
|
+
conditions.push("m.source = ?");
|
|
2785
|
+
params.push(filter.source);
|
|
2786
|
+
}
|
|
2787
|
+
}
|
|
2788
|
+
if (filter.status) {
|
|
2789
|
+
conditions.shift();
|
|
2790
|
+
if (Array.isArray(filter.status)) {
|
|
2791
|
+
conditions.push(`m.status IN (${filter.status.map(() => "?").join(",")})`);
|
|
2792
|
+
params.push(...filter.status);
|
|
2793
|
+
} else {
|
|
2794
|
+
conditions.push("m.status = ?");
|
|
2795
|
+
params.push(filter.status);
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
if (filter.project_id) {
|
|
2799
|
+
conditions.push("m.project_id = ?");
|
|
2800
|
+
params.push(filter.project_id);
|
|
2801
|
+
}
|
|
2802
|
+
if (filter.agent_id) {
|
|
2803
|
+
conditions.push("m.agent_id = ?");
|
|
2804
|
+
params.push(filter.agent_id);
|
|
2805
|
+
}
|
|
2806
|
+
if (filter.session_id) {
|
|
2807
|
+
conditions.push("m.session_id = ?");
|
|
2808
|
+
params.push(filter.session_id);
|
|
2809
|
+
}
|
|
2810
|
+
if (filter.min_importance) {
|
|
2811
|
+
conditions.push("m.importance >= ?");
|
|
2812
|
+
params.push(filter.min_importance);
|
|
2813
|
+
}
|
|
2814
|
+
if (filter.pinned !== undefined) {
|
|
2815
|
+
conditions.push("m.pinned = ?");
|
|
2816
|
+
params.push(filter.pinned ? 1 : 0);
|
|
2817
|
+
}
|
|
2818
|
+
if (filter.tags && filter.tags.length > 0) {
|
|
2819
|
+
for (const tag of filter.tags) {
|
|
2820
|
+
conditions.push("m.id IN (SELECT memory_id FROM memory_tags WHERE tag = ?)");
|
|
2821
|
+
params.push(tag);
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
const sql = `SELECT m.* FROM memories m WHERE ${conditions.join(" AND ")}`;
|
|
2826
|
+
const rows = d.query(sql).all(...params);
|
|
2827
|
+
const scored = [];
|
|
2828
|
+
for (const row of rows) {
|
|
2829
|
+
const memory = parseMemoryRow2(row);
|
|
2830
|
+
const rawScore = computeScore(memory, queryLower);
|
|
2831
|
+
if (rawScore === 0)
|
|
2832
|
+
continue;
|
|
2833
|
+
const weightedScore = rawScore * memory.importance / 10;
|
|
2834
|
+
const matchType = determineMatchType(memory, queryLower);
|
|
2835
|
+
scored.push({
|
|
2836
|
+
memory,
|
|
2837
|
+
score: weightedScore,
|
|
2838
|
+
match_type: matchType
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
scored.sort((a, b) => {
|
|
2842
|
+
if (b.score !== a.score)
|
|
2843
|
+
return b.score - a.score;
|
|
2844
|
+
return b.memory.importance - a.memory.importance;
|
|
2845
|
+
});
|
|
2846
|
+
const offset = filter?.offset ?? 0;
|
|
2847
|
+
const limit = filter?.limit ?? scored.length;
|
|
2848
|
+
return scored.slice(offset, offset + limit);
|
|
2849
|
+
}
|
|
2850
|
+
|
|
2851
|
+
// src/lib/config.ts
|
|
2852
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync } from "fs";
|
|
2853
|
+
import { homedir } from "os";
|
|
2854
|
+
import { dirname as dirname2, join as join2, resolve as resolve2 } from "path";
|
|
2855
|
+
var DEFAULT_CONFIG = {
|
|
2856
|
+
default_scope: "private",
|
|
2857
|
+
default_category: "knowledge",
|
|
2858
|
+
default_importance: 5,
|
|
2859
|
+
max_entries: 1000,
|
|
2860
|
+
max_entries_per_scope: {
|
|
2861
|
+
global: 500,
|
|
2862
|
+
shared: 300,
|
|
2863
|
+
private: 200
|
|
2864
|
+
},
|
|
2865
|
+
injection: {
|
|
2866
|
+
max_tokens: 500,
|
|
2867
|
+
min_importance: 5,
|
|
2868
|
+
categories: ["preference", "fact"],
|
|
2869
|
+
refresh_interval: 5
|
|
2870
|
+
},
|
|
2871
|
+
sync_agents: ["claude", "codex", "gemini"],
|
|
2872
|
+
auto_cleanup: {
|
|
2873
|
+
enabled: true,
|
|
2874
|
+
expired_check_interval: 3600
|
|
2875
|
+
}
|
|
2876
|
+
};
|
|
2877
|
+
function deepMerge(target, source) {
|
|
2878
|
+
const result = { ...target };
|
|
2879
|
+
for (const key of Object.keys(source)) {
|
|
2880
|
+
const sourceVal = source[key];
|
|
2881
|
+
const targetVal = result[key];
|
|
2882
|
+
if (sourceVal !== null && typeof sourceVal === "object" && !Array.isArray(sourceVal) && targetVal !== null && typeof targetVal === "object" && !Array.isArray(targetVal)) {
|
|
2883
|
+
result[key] = deepMerge(targetVal, sourceVal);
|
|
2884
|
+
} else {
|
|
2885
|
+
result[key] = sourceVal;
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
return result;
|
|
2889
|
+
}
|
|
2890
|
+
var VALID_SCOPES = ["global", "shared", "private"];
|
|
2891
|
+
var VALID_CATEGORIES = [
|
|
2892
|
+
"preference",
|
|
2893
|
+
"fact",
|
|
2894
|
+
"knowledge",
|
|
2895
|
+
"history"
|
|
2896
|
+
];
|
|
2897
|
+
function isValidScope(value) {
|
|
2898
|
+
return VALID_SCOPES.includes(value);
|
|
2899
|
+
}
|
|
2900
|
+
function isValidCategory(value) {
|
|
2901
|
+
return VALID_CATEGORIES.includes(value);
|
|
2902
|
+
}
|
|
2903
|
+
function loadConfig() {
|
|
2904
|
+
const configPath = join2(homedir(), ".mementos", "config.json");
|
|
2905
|
+
let fileConfig = {};
|
|
2906
|
+
if (existsSync2(configPath)) {
|
|
2907
|
+
try {
|
|
2908
|
+
const raw = readFileSync(configPath, "utf-8");
|
|
2909
|
+
fileConfig = JSON.parse(raw);
|
|
2910
|
+
} catch {}
|
|
2911
|
+
}
|
|
2912
|
+
const merged = deepMerge(DEFAULT_CONFIG, fileConfig);
|
|
2913
|
+
const envScope = process.env["MEMENTOS_DEFAULT_SCOPE"];
|
|
2914
|
+
if (envScope && isValidScope(envScope)) {
|
|
2915
|
+
merged.default_scope = envScope;
|
|
2916
|
+
}
|
|
2917
|
+
const envCategory = process.env["MEMENTOS_DEFAULT_CATEGORY"];
|
|
2918
|
+
if (envCategory && isValidCategory(envCategory)) {
|
|
2919
|
+
merged.default_category = envCategory;
|
|
2920
|
+
}
|
|
2921
|
+
const envImportance = process.env["MEMENTOS_DEFAULT_IMPORTANCE"];
|
|
2922
|
+
if (envImportance) {
|
|
2923
|
+
const parsed = parseInt(envImportance, 10);
|
|
2924
|
+
if (!Number.isNaN(parsed) && parsed >= 1 && parsed <= 10) {
|
|
2925
|
+
merged.default_importance = parsed;
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
return merged;
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
// src/lib/retention.ts
|
|
2932
|
+
function enforceQuotas(config, db) {
|
|
2933
|
+
const d = db || getDatabase();
|
|
2934
|
+
let totalEvicted = 0;
|
|
2935
|
+
const scopes = ["global", "shared", "private"];
|
|
2936
|
+
for (const scope of scopes) {
|
|
2937
|
+
const limit = config.max_entries_per_scope[scope];
|
|
2938
|
+
if (!limit || limit <= 0)
|
|
2939
|
+
continue;
|
|
2940
|
+
const countRow = d.query("SELECT COUNT(*) as cnt FROM memories WHERE scope = ? AND status = 'active'").get(scope);
|
|
2941
|
+
const count = countRow.cnt;
|
|
2942
|
+
if (count <= limit)
|
|
2943
|
+
continue;
|
|
2944
|
+
const excess = count - limit;
|
|
2945
|
+
const result = d.run(`DELETE FROM memories WHERE id IN (
|
|
2946
|
+
SELECT id FROM memories
|
|
2947
|
+
WHERE scope = ? AND status = 'active' AND pinned = 0
|
|
2948
|
+
ORDER BY importance ASC, created_at ASC
|
|
2949
|
+
LIMIT ?
|
|
2950
|
+
)`, [scope, excess]);
|
|
2951
|
+
totalEvicted += result.changes;
|
|
2952
|
+
}
|
|
2953
|
+
return totalEvicted;
|
|
2954
|
+
}
|
|
2955
|
+
function archiveStale(staleDays, db) {
|
|
2956
|
+
const d = db || getDatabase();
|
|
2957
|
+
const timestamp = now();
|
|
2958
|
+
const cutoff = new Date(Date.now() - staleDays * 24 * 60 * 60 * 1000).toISOString();
|
|
2959
|
+
const result = d.run(`UPDATE memories
|
|
2960
|
+
SET status = 'archived', updated_at = ?
|
|
2961
|
+
WHERE status = 'active'
|
|
2962
|
+
AND pinned = 0
|
|
2963
|
+
AND COALESCE(accessed_at, created_at) < ?`, [timestamp, cutoff]);
|
|
2964
|
+
return result.changes;
|
|
2965
|
+
}
|
|
2966
|
+
function runCleanup(config, db) {
|
|
2967
|
+
const d = db || getDatabase();
|
|
2968
|
+
const expired = cleanExpiredMemories(d);
|
|
2969
|
+
const evicted = enforceQuotas(config, d);
|
|
2970
|
+
const archived = archiveStale(90, d);
|
|
2971
|
+
return { expired, evicted, archived };
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
// src/cli/index.tsx
|
|
2975
|
+
function getPackageVersion() {
|
|
2976
|
+
try {
|
|
2977
|
+
const pkgPath = join3(dirname3(fileURLToPath(import.meta.url)), "..", "..", "package.json");
|
|
2978
|
+
const pkg = JSON.parse(readFileSync2(pkgPath, "utf-8"));
|
|
2979
|
+
return pkg.version || "0.0.0";
|
|
2980
|
+
} catch {
|
|
2981
|
+
return "0.0.0";
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
var scopeColor = {
|
|
2985
|
+
global: chalk.cyan,
|
|
2986
|
+
shared: chalk.yellow,
|
|
2987
|
+
private: chalk.magenta
|
|
2988
|
+
};
|
|
2989
|
+
var categoryColor = {
|
|
2990
|
+
preference: chalk.blue,
|
|
2991
|
+
fact: chalk.green,
|
|
2992
|
+
knowledge: chalk.yellow,
|
|
2993
|
+
history: chalk.gray
|
|
2994
|
+
};
|
|
2995
|
+
function importanceColor(importance) {
|
|
2996
|
+
if (importance >= 8)
|
|
2997
|
+
return chalk.red.bold;
|
|
2998
|
+
if (importance >= 5)
|
|
2999
|
+
return chalk.yellow;
|
|
3000
|
+
return chalk.gray;
|
|
3001
|
+
}
|
|
3002
|
+
function colorScope(scope) {
|
|
3003
|
+
return scopeColor[scope](scope);
|
|
3004
|
+
}
|
|
3005
|
+
function colorCategory(category) {
|
|
3006
|
+
return categoryColor[category](category);
|
|
3007
|
+
}
|
|
3008
|
+
function colorImportance(importance) {
|
|
3009
|
+
return importanceColor(importance)(String(importance));
|
|
3010
|
+
}
|
|
3011
|
+
function outputJson(data) {
|
|
3012
|
+
console.log(JSON.stringify(data, null, 2));
|
|
3013
|
+
}
|
|
3014
|
+
function formatMemoryLine(m) {
|
|
3015
|
+
const id = chalk.dim(m.id.slice(0, 8));
|
|
3016
|
+
const scope = colorScope(m.scope);
|
|
3017
|
+
const cat = colorCategory(m.category);
|
|
3018
|
+
const imp = colorImportance(m.importance);
|
|
3019
|
+
const pin = m.pinned ? chalk.red(" *") : "";
|
|
3020
|
+
const value = m.value.length > 80 ? m.value.slice(0, 80) + "..." : m.value;
|
|
3021
|
+
return `${id} [${scope}/${cat}] ${chalk.bold(m.key)} = ${value} (${imp})${pin}`;
|
|
3022
|
+
}
|
|
3023
|
+
function formatMemoryDetail(m) {
|
|
3024
|
+
const lines = [
|
|
3025
|
+
`${chalk.bold("ID:")} ${m.id}`,
|
|
3026
|
+
`${chalk.bold("Key:")} ${m.key}`,
|
|
3027
|
+
`${chalk.bold("Value:")} ${m.value}`,
|
|
3028
|
+
`${chalk.bold("Scope:")} ${colorScope(m.scope)}`,
|
|
3029
|
+
`${chalk.bold("Category:")} ${colorCategory(m.category)}`,
|
|
3030
|
+
`${chalk.bold("Importance:")} ${colorImportance(m.importance)}/10`,
|
|
3031
|
+
`${chalk.bold("Source:")} ${m.source}`,
|
|
3032
|
+
`${chalk.bold("Status:")} ${m.status}`,
|
|
3033
|
+
`${chalk.bold("Pinned:")} ${m.pinned ? chalk.red("yes") : "no"}`
|
|
3034
|
+
];
|
|
3035
|
+
if (m.summary)
|
|
3036
|
+
lines.push(`${chalk.bold("Summary:")} ${m.summary}`);
|
|
3037
|
+
if (m.tags.length > 0)
|
|
3038
|
+
lines.push(`${chalk.bold("Tags:")} ${m.tags.join(", ")}`);
|
|
3039
|
+
if (m.agent_id)
|
|
3040
|
+
lines.push(`${chalk.bold("Agent:")} ${m.agent_id}`);
|
|
3041
|
+
if (m.project_id)
|
|
3042
|
+
lines.push(`${chalk.bold("Project:")} ${m.project_id}`);
|
|
3043
|
+
if (m.session_id)
|
|
3044
|
+
lines.push(`${chalk.bold("Session:")} ${m.session_id}`);
|
|
3045
|
+
if (m.expires_at)
|
|
3046
|
+
lines.push(`${chalk.bold("Expires:")} ${m.expires_at}`);
|
|
3047
|
+
lines.push(`${chalk.bold("Access:")} ${m.access_count}`);
|
|
3048
|
+
lines.push(`${chalk.bold("Version:")} ${m.version}`);
|
|
3049
|
+
lines.push(`${chalk.bold("Created:")} ${m.created_at}`);
|
|
3050
|
+
lines.push(`${chalk.bold("Updated:")} ${m.updated_at}`);
|
|
3051
|
+
if (m.accessed_at)
|
|
3052
|
+
lines.push(`${chalk.bold("Accessed:")} ${m.accessed_at}`);
|
|
3053
|
+
return lines.join(`
|
|
3054
|
+
`);
|
|
3055
|
+
}
|
|
3056
|
+
var program2 = new Command;
|
|
3057
|
+
function handleError(e) {
|
|
3058
|
+
const globalOpts = program2.opts();
|
|
3059
|
+
if (globalOpts.json) {
|
|
3060
|
+
outputJson({
|
|
3061
|
+
error: e instanceof Error ? e.message : String(e)
|
|
3062
|
+
});
|
|
3063
|
+
} else {
|
|
3064
|
+
console.error(chalk.red(e instanceof Error ? e.message : String(e)));
|
|
3065
|
+
}
|
|
3066
|
+
process.exit(1);
|
|
3067
|
+
}
|
|
3068
|
+
function resolveMemoryId(partialId) {
|
|
3069
|
+
const db = getDatabase();
|
|
3070
|
+
const id = resolvePartialId(db, "memories", partialId);
|
|
3071
|
+
if (!id) {
|
|
3072
|
+
console.error(chalk.red(`Could not resolve memory ID: ${partialId}`));
|
|
3073
|
+
process.exit(1);
|
|
3074
|
+
}
|
|
3075
|
+
return id;
|
|
3076
|
+
}
|
|
3077
|
+
program2.name("mementos").description("Universal memory system for AI agents").version(getPackageVersion()).option("--project <path>", "Project path for scoping").option("--json", "Output as JSON").option("--agent <name>", "Agent name or ID").option("--session <id>", "Session ID");
|
|
3078
|
+
program2.command("save <key> <value>").description("Save a memory (create or upsert)").option("-c, --category <cat>", "Category: preference, fact, knowledge, history").option("-s, --scope <scope>", "Scope: global, shared, private").option("--importance <n>", "Importance 1-10", parseInt).option("--tags <tags>", "Comma-separated tags").option("--summary <text>", "Brief summary").option("--ttl <ms>", "Time-to-live in milliseconds", parseInt).option("--source <src>", "Source: user, agent, system, auto, imported").action((key, value, opts) => {
|
|
3079
|
+
try {
|
|
3080
|
+
const globalOpts = program2.opts();
|
|
3081
|
+
const input = {
|
|
3082
|
+
key,
|
|
3083
|
+
value,
|
|
3084
|
+
category: opts.category,
|
|
3085
|
+
scope: opts.scope,
|
|
3086
|
+
importance: opts.importance,
|
|
3087
|
+
tags: opts.tags ? opts.tags.split(",").map((t) => t.trim()) : undefined,
|
|
3088
|
+
summary: opts.summary,
|
|
3089
|
+
ttl_ms: opts.ttl,
|
|
3090
|
+
source: opts.source,
|
|
3091
|
+
agent_id: globalOpts.agent,
|
|
3092
|
+
session_id: globalOpts.session
|
|
3093
|
+
};
|
|
3094
|
+
if (globalOpts.project) {
|
|
3095
|
+
const project = getProject(resolve3(globalOpts.project));
|
|
3096
|
+
if (project)
|
|
3097
|
+
input.project_id = project.id;
|
|
3098
|
+
}
|
|
3099
|
+
const memory = createMemory(input);
|
|
3100
|
+
if (globalOpts.json) {
|
|
3101
|
+
outputJson(memory);
|
|
3102
|
+
} else {
|
|
3103
|
+
console.log(chalk.green("Memory saved:"));
|
|
3104
|
+
console.log(formatMemoryDetail(memory));
|
|
3105
|
+
}
|
|
3106
|
+
} catch (e) {
|
|
3107
|
+
handleError(e);
|
|
3108
|
+
}
|
|
3109
|
+
});
|
|
3110
|
+
program2.command("recall <key>").description("Recall a memory by key").option("-s, --scope <scope>", "Scope filter").option("--agent <name>", "Agent filter").option("--project <path>", "Project filter").action((key, opts) => {
|
|
3111
|
+
try {
|
|
3112
|
+
const globalOpts = program2.opts();
|
|
3113
|
+
const agentId = opts.agent || globalOpts.agent;
|
|
3114
|
+
const projectPath = opts.project || globalOpts.project;
|
|
3115
|
+
let projectId;
|
|
3116
|
+
if (projectPath) {
|
|
3117
|
+
const project = getProject(resolve3(projectPath));
|
|
3118
|
+
if (project)
|
|
3119
|
+
projectId = project.id;
|
|
3120
|
+
}
|
|
3121
|
+
const memory = getMemoryByKey(key, opts.scope, agentId, projectId);
|
|
3122
|
+
if (!memory) {
|
|
3123
|
+
if (globalOpts.json) {
|
|
3124
|
+
outputJson({ error: `No memory found for key: ${key}` });
|
|
3125
|
+
} else {
|
|
3126
|
+
console.error(chalk.yellow(`No memory found for key: ${key}`));
|
|
3127
|
+
}
|
|
3128
|
+
process.exit(1);
|
|
3129
|
+
}
|
|
3130
|
+
touchMemory(memory.id);
|
|
3131
|
+
if (globalOpts.json) {
|
|
3132
|
+
outputJson(memory);
|
|
3133
|
+
} else {
|
|
3134
|
+
console.log(formatMemoryDetail(memory));
|
|
3135
|
+
}
|
|
3136
|
+
} catch (e) {
|
|
3137
|
+
handleError(e);
|
|
3138
|
+
}
|
|
3139
|
+
});
|
|
3140
|
+
program2.command("list").description("List memories with optional filters").option("-s, --scope <scope>", "Scope filter").option("-c, --category <cat>", "Category filter").option("--tags <tags>", "Comma-separated tags filter").option("--importance-min <n>", "Minimum importance", parseInt).option("--pinned", "Show only pinned").option("--agent <name>", "Agent filter").option("--project <path>", "Project filter").option("--limit <n>", "Max results", parseInt).option("--offset <n>", "Offset for pagination", parseInt).option("--status <status>", "Status filter: active, archived, expired").action((opts) => {
|
|
3141
|
+
try {
|
|
3142
|
+
const globalOpts = program2.opts();
|
|
3143
|
+
const agentId = opts.agent || globalOpts.agent;
|
|
3144
|
+
const projectPath = opts.project || globalOpts.project;
|
|
3145
|
+
let projectId;
|
|
3146
|
+
if (projectPath) {
|
|
3147
|
+
const project = getProject(resolve3(projectPath));
|
|
3148
|
+
if (project)
|
|
3149
|
+
projectId = project.id;
|
|
3150
|
+
}
|
|
3151
|
+
const filter = {
|
|
3152
|
+
scope: opts.scope,
|
|
3153
|
+
category: opts.category,
|
|
3154
|
+
tags: opts.tags ? opts.tags.split(",").map((t) => t.trim()) : undefined,
|
|
3155
|
+
min_importance: opts.importanceMin,
|
|
3156
|
+
pinned: opts.pinned ? true : undefined,
|
|
3157
|
+
agent_id: agentId,
|
|
3158
|
+
project_id: projectId,
|
|
3159
|
+
limit: opts.limit || 50,
|
|
3160
|
+
offset: opts.offset,
|
|
3161
|
+
status: opts.status,
|
|
3162
|
+
session_id: globalOpts.session
|
|
3163
|
+
};
|
|
3164
|
+
const memories = listMemories(filter);
|
|
3165
|
+
if (globalOpts.json) {
|
|
3166
|
+
outputJson(memories);
|
|
3167
|
+
return;
|
|
3168
|
+
}
|
|
3169
|
+
if (memories.length === 0) {
|
|
3170
|
+
console.log(chalk.yellow("No memories found."));
|
|
3171
|
+
return;
|
|
3172
|
+
}
|
|
3173
|
+
console.log(chalk.bold(`${memories.length} memor${memories.length === 1 ? "y" : "ies"}:`));
|
|
3174
|
+
for (const m of memories) {
|
|
3175
|
+
console.log(formatMemoryLine(m));
|
|
3176
|
+
}
|
|
3177
|
+
} catch (e) {
|
|
3178
|
+
handleError(e);
|
|
3179
|
+
}
|
|
3180
|
+
});
|
|
3181
|
+
program2.command("update <id>").description("Update a memory by ID").option("--value <text>", "New value").option("--importance <n>", "New importance 1-10", parseInt).option("--tags <tags>", "New comma-separated tags").option("--summary <text>", "New summary").option("--pin", "Pin the memory").option("--unpin", "Unpin the memory").option("-c, --category <cat>", "New category").option("-s, --scope <scope>", "New scope").option("--status <status>", "New status: active, archived, expired").action((id, opts) => {
|
|
3182
|
+
try {
|
|
3183
|
+
const globalOpts = program2.opts();
|
|
3184
|
+
const resolvedId = resolveMemoryId(id);
|
|
3185
|
+
const existing = getMemory(resolvedId);
|
|
3186
|
+
if (!existing) {
|
|
3187
|
+
if (globalOpts.json) {
|
|
3188
|
+
outputJson({ error: `Memory not found: ${id}` });
|
|
3189
|
+
} else {
|
|
3190
|
+
console.error(chalk.red(`Memory not found: ${id}`));
|
|
3191
|
+
}
|
|
3192
|
+
process.exit(1);
|
|
3193
|
+
}
|
|
3194
|
+
const updateInput = {
|
|
3195
|
+
version: existing.version
|
|
3196
|
+
};
|
|
3197
|
+
if (opts.value !== undefined)
|
|
3198
|
+
updateInput.value = opts.value;
|
|
3199
|
+
if (opts.importance !== undefined)
|
|
3200
|
+
updateInput.importance = opts.importance;
|
|
3201
|
+
if (opts.tags !== undefined)
|
|
3202
|
+
updateInput.tags = opts.tags.split(",").map((t) => t.trim());
|
|
3203
|
+
if (opts.summary !== undefined)
|
|
3204
|
+
updateInput.summary = opts.summary;
|
|
3205
|
+
if (opts.pin)
|
|
3206
|
+
updateInput.pinned = true;
|
|
3207
|
+
if (opts.unpin)
|
|
3208
|
+
updateInput.pinned = false;
|
|
3209
|
+
if (opts.category !== undefined)
|
|
3210
|
+
updateInput.category = opts.category;
|
|
3211
|
+
if (opts.scope !== undefined)
|
|
3212
|
+
updateInput.scope = opts.scope;
|
|
3213
|
+
if (opts.status !== undefined)
|
|
3214
|
+
updateInput.status = opts.status;
|
|
3215
|
+
const updated = updateMemory(resolvedId, updateInput);
|
|
3216
|
+
if (globalOpts.json) {
|
|
3217
|
+
outputJson(updated);
|
|
3218
|
+
} else {
|
|
3219
|
+
console.log(chalk.green("Memory updated:"));
|
|
3220
|
+
console.log(formatMemoryDetail(updated));
|
|
3221
|
+
}
|
|
3222
|
+
} catch (e) {
|
|
3223
|
+
handleError(e);
|
|
3224
|
+
}
|
|
3225
|
+
});
|
|
3226
|
+
program2.command("forget <keyOrId>").description("Delete a memory by key or ID").action((keyOrId) => {
|
|
3227
|
+
try {
|
|
3228
|
+
const globalOpts = program2.opts();
|
|
3229
|
+
const db = getDatabase();
|
|
3230
|
+
const idMatch = resolvePartialId(db, "memories", keyOrId);
|
|
3231
|
+
if (idMatch) {
|
|
3232
|
+
deleteMemory(idMatch);
|
|
3233
|
+
if (globalOpts.json) {
|
|
3234
|
+
outputJson({ deleted: idMatch });
|
|
3235
|
+
} else {
|
|
3236
|
+
console.log(chalk.green(`Memory ${idMatch} deleted.`));
|
|
3237
|
+
}
|
|
3238
|
+
return;
|
|
3239
|
+
}
|
|
3240
|
+
const memory = getMemoryByKey(keyOrId);
|
|
3241
|
+
if (memory) {
|
|
3242
|
+
deleteMemory(memory.id);
|
|
3243
|
+
if (globalOpts.json) {
|
|
3244
|
+
outputJson({ deleted: memory.id, key: keyOrId });
|
|
3245
|
+
} else {
|
|
3246
|
+
console.log(chalk.green(`Memory "${keyOrId}" (${memory.id}) deleted.`));
|
|
3247
|
+
}
|
|
3248
|
+
return;
|
|
3249
|
+
}
|
|
3250
|
+
if (globalOpts.json) {
|
|
3251
|
+
outputJson({ error: `No memory found: ${keyOrId}` });
|
|
3252
|
+
} else {
|
|
3253
|
+
console.error(chalk.red(`No memory found: ${keyOrId}`));
|
|
3254
|
+
}
|
|
3255
|
+
process.exit(1);
|
|
3256
|
+
} catch (e) {
|
|
3257
|
+
handleError(e);
|
|
3258
|
+
}
|
|
3259
|
+
});
|
|
3260
|
+
program2.command("search <query>").description("Full-text search across memories").option("-s, --scope <scope>", "Scope filter").option("-c, --category <cat>", "Category filter").option("--tags <tags>", "Comma-separated tags filter").option("--limit <n>", "Max results", parseInt).action((query, opts) => {
|
|
3261
|
+
try {
|
|
3262
|
+
const globalOpts = program2.opts();
|
|
3263
|
+
const filter = {
|
|
3264
|
+
scope: opts.scope,
|
|
3265
|
+
category: opts.category,
|
|
3266
|
+
tags: opts.tags ? opts.tags.split(",").map((t) => t.trim()) : undefined,
|
|
3267
|
+
limit: opts.limit || 20
|
|
3268
|
+
};
|
|
3269
|
+
const results = searchMemories(query, filter);
|
|
3270
|
+
if (globalOpts.json) {
|
|
3271
|
+
outputJson(results);
|
|
3272
|
+
return;
|
|
3273
|
+
}
|
|
3274
|
+
if (results.length === 0) {
|
|
3275
|
+
console.log(chalk.yellow(`No memories found matching "${query}".`));
|
|
3276
|
+
return;
|
|
3277
|
+
}
|
|
3278
|
+
console.log(chalk.bold(`${results.length} result${results.length === 1 ? "" : "s"} for "${query}":`));
|
|
3279
|
+
for (const r of results) {
|
|
3280
|
+
const score = chalk.dim(`(score: ${r.score.toFixed(1)})`);
|
|
3281
|
+
console.log(`${formatMemoryLine(r.memory)} ${score}`);
|
|
3282
|
+
}
|
|
3283
|
+
} catch (e) {
|
|
3284
|
+
handleError(e);
|
|
3285
|
+
}
|
|
3286
|
+
});
|
|
3287
|
+
program2.command("stats").description("Show memory statistics").action(() => {
|
|
3288
|
+
try {
|
|
3289
|
+
const globalOpts = program2.opts();
|
|
3290
|
+
const db = getDatabase();
|
|
3291
|
+
const total = db.query("SELECT COUNT(*) as c FROM memories WHERE status = 'active'").get().c;
|
|
3292
|
+
const byScope = db.query("SELECT scope, COUNT(*) as c FROM memories WHERE status = 'active' GROUP BY scope").all();
|
|
3293
|
+
const byCategory = db.query("SELECT category, COUNT(*) as c FROM memories WHERE status = 'active' GROUP BY category").all();
|
|
3294
|
+
const byStatus = db.query("SELECT status, COUNT(*) as c FROM memories GROUP BY status").all();
|
|
3295
|
+
const pinnedCount = db.query("SELECT COUNT(*) as c FROM memories WHERE pinned = 1 AND status = 'active'").get().c;
|
|
3296
|
+
const expiredCount = db.query("SELECT COUNT(*) as c FROM memories WHERE status = 'expired' OR (expires_at IS NOT NULL AND expires_at < datetime('now'))").get().c;
|
|
3297
|
+
const byAgent = db.query("SELECT agent_id, COUNT(*) as c FROM memories WHERE status = 'active' AND agent_id IS NOT NULL GROUP BY agent_id").all();
|
|
3298
|
+
const stats = {
|
|
3299
|
+
total,
|
|
3300
|
+
by_scope: { global: 0, shared: 0, private: 0 },
|
|
3301
|
+
by_category: {
|
|
3302
|
+
preference: 0,
|
|
3303
|
+
fact: 0,
|
|
3304
|
+
knowledge: 0,
|
|
3305
|
+
history: 0
|
|
3306
|
+
},
|
|
3307
|
+
by_status: { active: 0, archived: 0, expired: 0 },
|
|
3308
|
+
by_agent: {},
|
|
3309
|
+
pinned_count: pinnedCount,
|
|
3310
|
+
expired_count: expiredCount
|
|
3311
|
+
};
|
|
3312
|
+
for (const row of byScope)
|
|
3313
|
+
stats.by_scope[row.scope] = row.c;
|
|
3314
|
+
for (const row of byCategory)
|
|
3315
|
+
stats.by_category[row.category] = row.c;
|
|
3316
|
+
for (const row of byStatus) {
|
|
3317
|
+
if (row.status in stats.by_status) {
|
|
3318
|
+
stats.by_status[row.status] = row.c;
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
for (const row of byAgent)
|
|
3322
|
+
stats.by_agent[row.agent_id] = row.c;
|
|
3323
|
+
if (globalOpts.json) {
|
|
3324
|
+
outputJson(stats);
|
|
3325
|
+
return;
|
|
3326
|
+
}
|
|
3327
|
+
console.log(chalk.bold("Memory Statistics"));
|
|
3328
|
+
console.log(`${chalk.bold("Total active:")} ${chalk.white(String(total))}`);
|
|
3329
|
+
console.log(`${chalk.bold("By scope:")} ${chalk.cyan("global")}=${stats.by_scope.global} ${chalk.yellow("shared")}=${stats.by_scope.shared} ${chalk.magenta("private")}=${stats.by_scope.private}`);
|
|
3330
|
+
console.log(`${chalk.bold("By category:")} ${chalk.blue("preference")}=${stats.by_category.preference} ${chalk.green("fact")}=${stats.by_category.fact} ${chalk.yellow("knowledge")}=${stats.by_category.knowledge} ${chalk.gray("history")}=${stats.by_category.history}`);
|
|
3331
|
+
console.log(`${chalk.bold("By status:")} active=${stats.by_status.active} archived=${stats.by_status.archived} expired=${stats.by_status.expired}`);
|
|
3332
|
+
console.log(`${chalk.bold("Pinned:")} ${stats.pinned_count}`);
|
|
3333
|
+
console.log(`${chalk.bold("Expired:")} ${stats.expired_count}`);
|
|
3334
|
+
if (Object.keys(stats.by_agent).length > 0) {
|
|
3335
|
+
const agentParts = Object.entries(stats.by_agent).map(([k, v]) => `${k}=${v}`).join(" ");
|
|
3336
|
+
console.log(`${chalk.bold("By agent:")} ${agentParts}`);
|
|
3337
|
+
}
|
|
3338
|
+
} catch (e) {
|
|
3339
|
+
handleError(e);
|
|
3340
|
+
}
|
|
3341
|
+
});
|
|
3342
|
+
program2.command("export").description("Export memories as JSON").option("-s, --scope <scope>", "Scope filter").option("-c, --category <cat>", "Category filter").option("--agent <name>", "Agent filter").option("--project <path>", "Project filter").action((opts) => {
|
|
3343
|
+
try {
|
|
3344
|
+
const globalOpts = program2.opts();
|
|
3345
|
+
const agentId = opts.agent || globalOpts.agent;
|
|
3346
|
+
const projectPath = opts.project || globalOpts.project;
|
|
3347
|
+
let projectId;
|
|
3348
|
+
if (projectPath) {
|
|
3349
|
+
const project = getProject(resolve3(projectPath));
|
|
3350
|
+
if (project)
|
|
3351
|
+
projectId = project.id;
|
|
3352
|
+
}
|
|
3353
|
+
const filter = {
|
|
3354
|
+
scope: opts.scope,
|
|
3355
|
+
category: opts.category,
|
|
3356
|
+
agent_id: agentId,
|
|
3357
|
+
project_id: projectId,
|
|
3358
|
+
limit: 1e4
|
|
3359
|
+
};
|
|
3360
|
+
const memories = listMemories(filter);
|
|
3361
|
+
outputJson(memories);
|
|
3362
|
+
} catch (e) {
|
|
3363
|
+
handleError(e);
|
|
3364
|
+
}
|
|
3365
|
+
});
|
|
3366
|
+
program2.command("import <file>").description("Import memories from a JSON file").option("--overwrite", "Overwrite existing memories (default: merge)").action((file, opts) => {
|
|
3367
|
+
try {
|
|
3368
|
+
const globalOpts = program2.opts();
|
|
3369
|
+
const raw = readFileSync2(resolve3(file), "utf-8");
|
|
3370
|
+
const memories = JSON.parse(raw);
|
|
3371
|
+
if (!Array.isArray(memories)) {
|
|
3372
|
+
throw new Error("JSON file must contain an array of memories");
|
|
3373
|
+
}
|
|
3374
|
+
const dedupeMode = opts.overwrite ? "merge" : "create";
|
|
3375
|
+
let imported = 0;
|
|
3376
|
+
for (const mem of memories) {
|
|
3377
|
+
createMemory({ ...mem, source: mem.source || "imported" }, dedupeMode);
|
|
3378
|
+
imported++;
|
|
3379
|
+
}
|
|
3380
|
+
if (globalOpts.json) {
|
|
3381
|
+
outputJson({ imported });
|
|
3382
|
+
} else {
|
|
3383
|
+
console.log(chalk.green(`Imported ${imported} memor${imported === 1 ? "y" : "ies"}.`));
|
|
3384
|
+
}
|
|
3385
|
+
} catch (e) {
|
|
3386
|
+
handleError(e);
|
|
3387
|
+
}
|
|
3388
|
+
});
|
|
3389
|
+
program2.command("clean").description("Remove expired memories and enforce quotas").action(() => {
|
|
3390
|
+
try {
|
|
3391
|
+
const globalOpts = program2.opts();
|
|
3392
|
+
const config = loadConfig();
|
|
3393
|
+
const result = runCleanup(config);
|
|
3394
|
+
if (globalOpts.json) {
|
|
3395
|
+
outputJson(result);
|
|
3396
|
+
} else {
|
|
3397
|
+
console.log(chalk.bold("Cleanup complete:"));
|
|
3398
|
+
console.log(` Expired removed: ${chalk.red(String(result.expired))}`);
|
|
3399
|
+
console.log(` Evicted (quota): ${chalk.yellow(String(result.evicted))}`);
|
|
3400
|
+
console.log(` Archived (stale): ${chalk.gray(String(result.archived))}`);
|
|
3401
|
+
}
|
|
3402
|
+
} catch (e) {
|
|
3403
|
+
handleError(e);
|
|
3404
|
+
}
|
|
3405
|
+
});
|
|
3406
|
+
program2.command("init <name>").description("Register an agent (returns ID)").option("-d, --description <text>", "Agent description").option("-r, --role <role>", "Agent role").action((name, opts) => {
|
|
3407
|
+
try {
|
|
3408
|
+
const globalOpts = program2.opts();
|
|
3409
|
+
const agent = registerAgent(name, opts.description, opts.role);
|
|
3410
|
+
if (globalOpts.json) {
|
|
3411
|
+
outputJson(agent);
|
|
3412
|
+
} else {
|
|
3413
|
+
console.log(chalk.green("Agent registered:"));
|
|
3414
|
+
console.log(` ${chalk.bold("ID:")} ${agent.id}`);
|
|
3415
|
+
console.log(` ${chalk.bold("Name:")} ${agent.name}`);
|
|
3416
|
+
console.log(` ${chalk.bold("Role:")} ${agent.role || "agent"}`);
|
|
3417
|
+
console.log(` ${chalk.bold("Created:")} ${agent.created_at}`);
|
|
3418
|
+
}
|
|
3419
|
+
} catch (e) {
|
|
3420
|
+
handleError(e);
|
|
3421
|
+
}
|
|
3422
|
+
});
|
|
3423
|
+
program2.command("agents").description("List all registered agents").action(() => {
|
|
3424
|
+
try {
|
|
3425
|
+
const globalOpts = program2.opts();
|
|
3426
|
+
const agents = listAgents();
|
|
3427
|
+
if (globalOpts.json) {
|
|
3428
|
+
outputJson(agents);
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
if (agents.length === 0) {
|
|
3432
|
+
console.log(chalk.yellow("No agents registered."));
|
|
3433
|
+
return;
|
|
3434
|
+
}
|
|
3435
|
+
console.log(chalk.bold(`${agents.length} agent${agents.length === 1 ? "" : "s"}:`));
|
|
3436
|
+
for (const a of agents) {
|
|
3437
|
+
console.log(` ${chalk.dim(a.id)} ${chalk.bold(a.name)} ${chalk.gray(a.role || "agent")} ${chalk.dim(`last seen: ${a.last_seen_at}`)}`);
|
|
3438
|
+
}
|
|
3439
|
+
} catch (e) {
|
|
3440
|
+
handleError(e);
|
|
3441
|
+
}
|
|
3442
|
+
});
|
|
3443
|
+
program2.command("projects").description("Manage projects").option("--add", "Add a new project").option("--name <name>", "Project name").option("--path <path>", "Project path").option("--description <text>", "Project description").action((opts) => {
|
|
3444
|
+
try {
|
|
3445
|
+
const globalOpts = program2.opts();
|
|
3446
|
+
if (opts.add) {
|
|
3447
|
+
const name = opts.name;
|
|
3448
|
+
const path = opts.path;
|
|
3449
|
+
if (!name || !path) {
|
|
3450
|
+
console.error(chalk.red("--name and --path are required when adding a project"));
|
|
3451
|
+
process.exit(1);
|
|
3452
|
+
}
|
|
3453
|
+
const project = registerProject(name, resolve3(path), opts.description);
|
|
3454
|
+
if (globalOpts.json) {
|
|
3455
|
+
outputJson(project);
|
|
3456
|
+
} else {
|
|
3457
|
+
console.log(chalk.green("Project registered:"));
|
|
3458
|
+
console.log(` ${chalk.bold("ID:")} ${project.id}`);
|
|
3459
|
+
console.log(` ${chalk.bold("Name:")} ${project.name}`);
|
|
3460
|
+
console.log(` ${chalk.bold("Path:")} ${project.path}`);
|
|
3461
|
+
}
|
|
3462
|
+
return;
|
|
3463
|
+
}
|
|
3464
|
+
const projects = listProjects();
|
|
3465
|
+
if (globalOpts.json) {
|
|
3466
|
+
outputJson(projects);
|
|
3467
|
+
return;
|
|
3468
|
+
}
|
|
3469
|
+
if (projects.length === 0) {
|
|
3470
|
+
console.log(chalk.yellow("No projects registered."));
|
|
3471
|
+
return;
|
|
3472
|
+
}
|
|
3473
|
+
console.log(chalk.bold(`${projects.length} project${projects.length === 1 ? "" : "s"}:`));
|
|
3474
|
+
for (const p of projects) {
|
|
3475
|
+
console.log(` ${chalk.dim(p.id.slice(0, 8))} ${chalk.bold(p.name)} ${chalk.gray(p.path)}${p.description ? chalk.dim(` \u2014 ${p.description}`) : ""}`);
|
|
3476
|
+
}
|
|
3477
|
+
} catch (e) {
|
|
3478
|
+
handleError(e);
|
|
3479
|
+
}
|
|
3480
|
+
});
|
|
3481
|
+
program2.command("inject").description("Output injection context for agent system prompts").option("--agent <name>", "Agent ID for scope filtering").option("--project <path>", "Project path for scope filtering").option("--session <id>", "Session ID for scope filtering").option("--max-tokens <n>", "Max approximate token budget", parseInt).option("--categories <cats>", "Comma-separated categories to include").action((opts) => {
|
|
3482
|
+
try {
|
|
3483
|
+
const globalOpts = program2.opts();
|
|
3484
|
+
const maxTokens = opts.maxTokens || 500;
|
|
3485
|
+
const minImportance = 3;
|
|
3486
|
+
const categoriesRaw = opts.categories || "preference,fact,knowledge";
|
|
3487
|
+
const categories = categoriesRaw.split(",").map((c) => c.trim());
|
|
3488
|
+
const agentId = opts.agent || globalOpts.agent;
|
|
3489
|
+
const projectPath = opts.project || globalOpts.project;
|
|
3490
|
+
const sessionId = opts.session || globalOpts.session;
|
|
3491
|
+
let projectId;
|
|
3492
|
+
if (projectPath) {
|
|
3493
|
+
const project = getProject(resolve3(projectPath));
|
|
3494
|
+
if (project)
|
|
3495
|
+
projectId = project.id;
|
|
3496
|
+
}
|
|
3497
|
+
const allMemories = [];
|
|
3498
|
+
const globalMems = listMemories({
|
|
3499
|
+
scope: "global",
|
|
3500
|
+
category: categories,
|
|
3501
|
+
min_importance: minImportance,
|
|
3502
|
+
status: "active",
|
|
3503
|
+
project_id: projectId,
|
|
3504
|
+
limit: 50
|
|
3505
|
+
});
|
|
3506
|
+
allMemories.push(...globalMems);
|
|
3507
|
+
if (projectId) {
|
|
3508
|
+
const sharedMems = listMemories({
|
|
3509
|
+
scope: "shared",
|
|
3510
|
+
category: categories,
|
|
3511
|
+
min_importance: minImportance,
|
|
3512
|
+
status: "active",
|
|
3513
|
+
project_id: projectId,
|
|
3514
|
+
limit: 50
|
|
3515
|
+
});
|
|
3516
|
+
allMemories.push(...sharedMems);
|
|
3517
|
+
}
|
|
3518
|
+
if (agentId) {
|
|
3519
|
+
const privateMems = listMemories({
|
|
3520
|
+
scope: "private",
|
|
3521
|
+
category: categories,
|
|
3522
|
+
min_importance: minImportance,
|
|
3523
|
+
status: "active",
|
|
3524
|
+
agent_id: agentId,
|
|
3525
|
+
session_id: sessionId,
|
|
3526
|
+
limit: 50
|
|
3527
|
+
});
|
|
3528
|
+
allMemories.push(...privateMems);
|
|
3529
|
+
}
|
|
3530
|
+
const seen = new Set;
|
|
3531
|
+
const unique = allMemories.filter((m) => {
|
|
3532
|
+
if (seen.has(m.id))
|
|
3533
|
+
return false;
|
|
3534
|
+
seen.add(m.id);
|
|
3535
|
+
return true;
|
|
3536
|
+
});
|
|
3537
|
+
unique.sort((a, b) => {
|
|
3538
|
+
if (b.importance !== a.importance)
|
|
3539
|
+
return b.importance - a.importance;
|
|
3540
|
+
return new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime();
|
|
3541
|
+
});
|
|
3542
|
+
const charBudget = maxTokens * 4;
|
|
3543
|
+
const lines = [];
|
|
3544
|
+
let totalChars = 0;
|
|
3545
|
+
for (const m of unique) {
|
|
3546
|
+
const line = `- [${m.scope}/${m.category}] ${m.key}: ${m.value}`;
|
|
3547
|
+
if (totalChars + line.length > charBudget)
|
|
3548
|
+
break;
|
|
3549
|
+
lines.push(line);
|
|
3550
|
+
totalChars += line.length;
|
|
3551
|
+
touchMemory(m.id);
|
|
3552
|
+
}
|
|
3553
|
+
if (lines.length === 0) {
|
|
3554
|
+
if (globalOpts.json) {
|
|
3555
|
+
outputJson({ context: "", count: 0 });
|
|
3556
|
+
} else {
|
|
3557
|
+
console.log(chalk.yellow("No relevant memories found for injection."));
|
|
3558
|
+
}
|
|
3559
|
+
return;
|
|
3560
|
+
}
|
|
3561
|
+
const context = `<agent-memories>
|
|
3562
|
+
${lines.join(`
|
|
3563
|
+
`)}
|
|
3564
|
+
</agent-memories>`;
|
|
3565
|
+
if (globalOpts.json) {
|
|
3566
|
+
outputJson({ context, count: lines.length });
|
|
3567
|
+
} else {
|
|
3568
|
+
console.log(context);
|
|
3569
|
+
}
|
|
3570
|
+
} catch (e) {
|
|
3571
|
+
handleError(e);
|
|
3572
|
+
}
|
|
3573
|
+
});
|
|
3574
|
+
program2.command("bulk <action> <ids...>").description("Batch operations: forget, archive, pin, unpin").action((action, ids) => {
|
|
3575
|
+
try {
|
|
3576
|
+
const globalOpts = program2.opts();
|
|
3577
|
+
const validActions = [
|
|
3578
|
+
"forget",
|
|
3579
|
+
"archive",
|
|
3580
|
+
"pin",
|
|
3581
|
+
"unpin"
|
|
3582
|
+
];
|
|
3583
|
+
if (!validActions.includes(action)) {
|
|
3584
|
+
console.error(chalk.red(`Invalid action: ${action}. Valid: ${validActions.join(", ")}`));
|
|
3585
|
+
process.exit(1);
|
|
3586
|
+
}
|
|
3587
|
+
const resolvedIds = ids.map((id) => resolveMemoryId(id));
|
|
3588
|
+
let affected = 0;
|
|
3589
|
+
switch (action) {
|
|
3590
|
+
case "forget": {
|
|
3591
|
+
affected = bulkDeleteMemories(resolvedIds);
|
|
3592
|
+
break;
|
|
3593
|
+
}
|
|
3594
|
+
case "archive": {
|
|
3595
|
+
for (const id of resolvedIds) {
|
|
3596
|
+
const mem = getMemory(id);
|
|
3597
|
+
if (mem) {
|
|
3598
|
+
updateMemory(id, {
|
|
3599
|
+
status: "archived",
|
|
3600
|
+
version: mem.version
|
|
3601
|
+
});
|
|
3602
|
+
affected++;
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
break;
|
|
3606
|
+
}
|
|
3607
|
+
case "pin": {
|
|
3608
|
+
for (const id of resolvedIds) {
|
|
3609
|
+
const mem = getMemory(id);
|
|
3610
|
+
if (mem) {
|
|
3611
|
+
updateMemory(id, {
|
|
3612
|
+
pinned: true,
|
|
3613
|
+
version: mem.version
|
|
3614
|
+
});
|
|
3615
|
+
affected++;
|
|
3616
|
+
}
|
|
3617
|
+
}
|
|
3618
|
+
break;
|
|
3619
|
+
}
|
|
3620
|
+
case "unpin": {
|
|
3621
|
+
for (const id of resolvedIds) {
|
|
3622
|
+
const mem = getMemory(id);
|
|
3623
|
+
if (mem) {
|
|
3624
|
+
updateMemory(id, {
|
|
3625
|
+
pinned: false,
|
|
3626
|
+
version: mem.version
|
|
3627
|
+
});
|
|
3628
|
+
affected++;
|
|
3629
|
+
}
|
|
3630
|
+
}
|
|
3631
|
+
break;
|
|
3632
|
+
}
|
|
3633
|
+
}
|
|
3634
|
+
if (globalOpts.json) {
|
|
3635
|
+
outputJson({ action, affected, ids: resolvedIds });
|
|
3636
|
+
} else {
|
|
3637
|
+
console.log(chalk.green(`${action}: ${affected} memor${affected === 1 ? "y" : "ies"} affected.`));
|
|
3638
|
+
}
|
|
3639
|
+
} catch (e) {
|
|
3640
|
+
handleError(e);
|
|
3641
|
+
}
|
|
3642
|
+
});
|
|
3643
|
+
program2.parse(process.argv);
|