@cosmicstack/mercury-agent 0.5.2 → 0.5.4

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/dist/index.js CHANGED
@@ -942,12 +942,11 @@ function decodeHtmlEntities(text) {
942
942
  }
943
943
  function renderMarkdown(text) {
944
944
  try {
945
- const decoded = decodeHtmlEntities(text);
946
- const tokens = lexer.lexer(decoded);
945
+ const tokens = lexer.lexer(text);
947
946
  const result = renderTokens(tokens);
948
- return result.replace(/\n{3,}/g, "\n\n").trimEnd();
947
+ return decodeHtmlEntities(result.replace(/\n{3,}/g, "\n\n").trimEnd());
949
948
  } catch {
950
- return text;
949
+ return decodeHtmlEntities(text);
951
950
  }
952
951
  }
953
952
  function renderTokens(tokens) {
@@ -1313,10 +1312,8 @@ async function selectWithArrowKeys(title, options, config = {}) {
1313
1312
 
1314
1313
  // src/channels/cli.ts
1315
1314
  var USER_PROMPT = " You: ";
1316
- var USER_PROMPT_VISIBLE_LEN = USER_PROMPT.length;
1317
- function agentPrefix(name, suffix) {
1318
- const time = suffix ?? "";
1319
- return chalk3.cyan(` ${name}:`) + time;
1315
+ function agentName(name, suffix) {
1316
+ return chalk3.cyan(` ${name}:`) + (suffix ?? "");
1320
1317
  }
1321
1318
  var CLIChannel = class extends BaseChannel {
1322
1319
  type = "cli";
@@ -1326,11 +1323,10 @@ var CLIChannel = class extends BaseChannel {
1326
1323
  menuAbortController = null;
1327
1324
  outputInProgress = 0;
1328
1325
  streamActive = false;
1329
- streamToolLines = 0;
1330
- lastUserInput = "";
1331
- constructor(agentName = "Mercury") {
1326
+ streamLines = 0;
1327
+ constructor(agentName2 = "Mercury") {
1332
1328
  super();
1333
- this.agentName = agentName;
1329
+ this.agentName = agentName2;
1334
1330
  }
1335
1331
  setAgentName(name) {
1336
1332
  this.agentName = name;
@@ -1345,10 +1341,8 @@ var CLIChannel = class extends BaseChannel {
1345
1341
  input: process.stdin,
1346
1342
  output: process.stdout
1347
1343
  });
1348
- this.rl.setPrompt(chalk3.yellow(USER_PROMPT));
1349
- this.rl._promptLength = USER_PROMPT_VISIBLE_LEN;
1344
+ this.rl.setPrompt(USER_PROMPT);
1350
1345
  this.rl.on("line", (line) => {
1351
- this.lastUserInput = line.trim();
1352
1346
  const trimmed = line.trim();
1353
1347
  if (!trimmed) {
1354
1348
  this.showPrompt();
@@ -1374,12 +1368,10 @@ var CLIChannel = class extends BaseChannel {
1374
1368
  this.closeActiveMenu();
1375
1369
  this.beginOutput();
1376
1370
  const timeStr = elapsedMs != null ? chalk3.dim(` (${(elapsedMs / 1e3).toFixed(1)}s)`) : "";
1377
- const rendered = renderMarkdown(content);
1378
- const indented = this.indent(rendered);
1379
- console.log("");
1380
- console.log(agentPrefix(this.agentName, timeStr));
1381
- console.log(indented);
1382
- console.log("");
1371
+ const block = this.formatBlock(this.agentName, timeStr, content);
1372
+ for (const line of block) {
1373
+ console.log(line);
1374
+ }
1383
1375
  this.endOutput();
1384
1376
  }
1385
1377
  async sendFile(filePath, _targetId) {
@@ -1393,17 +1385,19 @@ var CLIChannel = class extends BaseChannel {
1393
1385
  }
1394
1386
  const stat = fs.statSync(resolved);
1395
1387
  const sizeStr = stat.size > 1024 * 1024 ? `${(stat.size / (1024 * 1024)).toFixed(1)}MB` : stat.size > 1024 ? `${(stat.size / 1024).toFixed(1)}KB` : `${stat.size}B`;
1396
- console.log("");
1397
- console.log(agentPrefix(this.agentName, chalk3.dim(" (file)")));
1398
- console.log(chalk3.dim(` path: ${resolved}`));
1399
- console.log(chalk3.dim(` size: ${sizeStr}`));
1400
- console.log("");
1388
+ const block = this.formatBlock(this.agentName, chalk3.dim(" (file)"), [
1389
+ chalk3.dim(`path: ${resolved}`),
1390
+ chalk3.dim(`size: ${sizeStr}`)
1391
+ ].join("\n"));
1392
+ for (const line of block) {
1393
+ console.log(line);
1394
+ }
1401
1395
  this.endOutput();
1402
1396
  }
1403
1397
  async sendToolFeedback(toolName, args) {
1404
1398
  const label = formatToolStep(toolName, args);
1405
1399
  if (this.streamActive) {
1406
- this.streamToolLines++;
1400
+ this.streamLines += 2;
1407
1401
  process.stdout.write(chalk3.dim(`
1408
1402
  ${label}
1409
1403
  `));
@@ -1415,7 +1409,7 @@ var CLIChannel = class extends BaseChannel {
1415
1409
  this.closeActiveMenu();
1416
1410
  this.beginOutput();
1417
1411
  this.streamActive = true;
1418
- this.streamToolLines = 0;
1412
+ this.streamLines = 0;
1419
1413
  if (!process.stdout.isTTY) {
1420
1414
  process.stdout.write(chalk3.cyan(` ${this.agentName}: `));
1421
1415
  let full2 = "";
@@ -1428,7 +1422,6 @@ var CLIChannel = class extends BaseChannel {
1428
1422
  this.endOutput();
1429
1423
  return full2;
1430
1424
  }
1431
- console.log(chalk3.cyan(` ${this.agentName}:`));
1432
1425
  let full = "";
1433
1426
  for await (const chunk of content) {
1434
1427
  process.stdout.write(chunk);
@@ -1441,27 +1434,26 @@ var CLIChannel = class extends BaseChannel {
1441
1434
  return full;
1442
1435
  }
1443
1436
  const termWidth = process.stdout.columns || 80;
1444
- let textLineCount = 0;
1437
+ let streamedScreenLines = 0;
1445
1438
  for (const line of full.split("\n")) {
1446
1439
  const visualLen = line.replace(/\x1b\[[0-9;]*m/g, "").length;
1447
- textLineCount += Math.max(1, Math.ceil((visualLen + 2) / termWidth));
1440
+ streamedScreenLines += Math.max(1, Math.ceil(visualLen / termWidth));
1448
1441
  }
1449
- const totalLines = 1 + textLineCount + this.streamToolLines;
1450
- process.stdout.write(`\x1B[${totalLines}A`);
1451
- for (let i = 0; i < totalLines; i++) {
1442
+ const totalRawLines = streamedScreenLines + this.streamLines + 1;
1443
+ process.stdout.write(`\x1B[${totalRawLines}A`);
1444
+ for (let i = 0; i < totalRawLines; i++) {
1452
1445
  process.stdout.write("\x1B[2K\x1B[1B");
1453
1446
  }
1454
- process.stdout.write(`\x1B[${totalLines}A`);
1455
- const rendered = renderMarkdown(full);
1456
- const indented = this.indent(rendered);
1457
- console.log(agentPrefix(this.agentName));
1458
- console.log(indented);
1459
- console.log("");
1447
+ process.stdout.write(`\x1B[${totalRawLines}A`);
1448
+ const block = this.formatBlock(this.agentName, "", full);
1449
+ for (const line of block) {
1450
+ console.log(line);
1451
+ }
1460
1452
  this.endOutput();
1461
1453
  return full;
1462
1454
  }
1463
1455
  async typing(_targetId) {
1464
- process.stdout.write(chalk3.dim(` ${this.agentName} is thinking...\r`));
1456
+ process.stdout.write(chalk3.dim(` ${this.agentName} is responding...\r`));
1465
1457
  }
1466
1458
  showPrompt() {
1467
1459
  if (this.rl) {
@@ -1469,6 +1461,11 @@ var CLIChannel = class extends BaseChannel {
1469
1461
  process.stdout.write(chalk3.yellow(USER_PROMPT));
1470
1462
  }
1471
1463
  }
1464
+ formatBlock(name, suffix, content) {
1465
+ const header = agentName(name, suffix);
1466
+ const body = renderMarkdown(content).split("\n").map((line) => ` ${line}`).join("\n");
1467
+ return ["", header, "", body, ""];
1468
+ }
1472
1469
  async withMenu(runner) {
1473
1470
  this.menuDepth += 1;
1474
1471
  this.menuAbortController = new AbortController();
@@ -1519,9 +1516,6 @@ var CLIChannel = class extends BaseChannel {
1519
1516
  if (!this.ready || this.rl) return;
1520
1517
  this.createInterface();
1521
1518
  }
1522
- indent(text) {
1523
- return text.split("\n").map((line) => ` ${line}`).join("\n");
1524
- }
1525
1519
  async prompt(question) {
1526
1520
  return new Promise((resolve13) => {
1527
1521
  this.rl?.question(question, (answer) => resolve13(answer.trim()));
@@ -6712,8 +6706,8 @@ async function configure(existingConfig) {
6712
6706
  if (isReconfig) {
6713
6707
  const ownerName = await ask(chalk7.white(` Your name [${config.identity.owner}]: `));
6714
6708
  if (ownerName) config.identity.owner = ownerName;
6715
- const agentName = await ask(chalk7.white(` Agent name [${config.identity.name}]: `));
6716
- if (agentName) config.identity.name = agentName;
6709
+ const agentName2 = await ask(chalk7.white(` Agent name [${config.identity.name}]: `));
6710
+ if (agentName2) config.identity.name = agentName2;
6717
6711
  } else {
6718
6712
  const ownerName = await ask(chalk7.white(" Your name: "));
6719
6713
  if (!ownerName) {
@@ -6721,8 +6715,8 @@ async function configure(existingConfig) {
6721
6715
  process.exit(1);
6722
6716
  }
6723
6717
  config.identity.owner = ownerName;
6724
- const agentName = await ask(chalk7.white(` Agent name [${config.identity.name}]: `));
6725
- if (agentName) config.identity.name = agentName;
6718
+ const agentName2 = await ask(chalk7.white(` Agent name [${config.identity.name}]: `));
6719
+ if (agentName2) config.identity.name = agentName2;
6726
6720
  }
6727
6721
  config.identity.creator = config.identity.creator || "Cosmic Stack";
6728
6722
  hr();
@@ -7351,5 +7345,60 @@ serviceCmd.command("uninstall").description("Uninstall the system service").acti
7351
7345
  serviceCmd.command("status").description("Show system service status").action(() => {
7352
7346
  showServiceStatus();
7353
7347
  });
7348
+ program.command("upgrade").description("Upgrade Mercury to the latest version from npm").action(async () => {
7349
+ console.log("");
7350
+ console.log(chalk7.cyan(` Mercury ${chalk7.white(`v${pkgVersion}`)}`));
7351
+ console.log("");
7352
+ const daemon = getDaemonStatus();
7353
+ if (daemon.running) {
7354
+ console.log(chalk7.dim(" Stopping background daemon..."));
7355
+ stopDaemon();
7356
+ await new Promise((r) => setTimeout(r, 1e3));
7357
+ console.log(chalk7.green(" \u2713 Daemon stopped"));
7358
+ }
7359
+ console.log(chalk7.dim(" Checking for latest version..."));
7360
+ const { execSync: execSync9 } = await import("child_process");
7361
+ let latestVersion = "";
7362
+ try {
7363
+ latestVersion = execSync9("npm view @cosmicstack/mercury-agent version", { encoding: "utf-8" }).trim();
7364
+ } catch {
7365
+ console.log(chalk7.red(" \u2717 Failed to fetch latest version from npm"));
7366
+ console.log("");
7367
+ return;
7368
+ }
7369
+ console.log(chalk7.dim(` Latest: v${latestVersion}`));
7370
+ if (latestVersion === pkgVersion) {
7371
+ console.log(chalk7.green(` \u2713 Already on the latest version (v${pkgVersion})`));
7372
+ console.log("");
7373
+ return;
7374
+ }
7375
+ console.log(chalk7.dim(` Upgrading v${pkgVersion} \u2192 v${latestVersion}...`));
7376
+ console.log("");
7377
+ try {
7378
+ execSync9("npm rm -g @cosmicstack/mercury-agent", { stdio: "pipe" });
7379
+ } catch {
7380
+ try {
7381
+ const globalDir = execSync9("npm root -g", { encoding: "utf-8" }).trim();
7382
+ const pkgDir = join11(globalDir, "@cosmicstack", "mercury-agent");
7383
+ const { rmSync } = await import("fs");
7384
+ try {
7385
+ rmSync(pkgDir, { recursive: true, force: true });
7386
+ } catch {
7387
+ }
7388
+ } catch {
7389
+ }
7390
+ }
7391
+ try {
7392
+ execSync9("npm i -g @cosmicstack/mercury-agent@latest", { stdio: "inherit" });
7393
+ console.log("");
7394
+ console.log(chalk7.green(` \u2713 Upgraded to v${latestVersion}`));
7395
+ console.log(chalk7.dim(" Run `mercury` to start the new version."));
7396
+ } catch {
7397
+ console.log("");
7398
+ console.log(chalk7.red(" \u2717 Upgrade failed. Try manually:"));
7399
+ console.log(chalk7.dim(" npm rm -g @cosmicstack/mercury-agent && npm i -g @cosmicstack/mercury-agent"));
7400
+ }
7401
+ console.log("");
7402
+ });
7354
7403
  program.parse();
7355
7404
  //# sourceMappingURL=index.js.map