anywhere-ai 0.0.30 → 0.0.32

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.
Files changed (3) hide show
  1. package/dist/cli.js +18 -35
  2. package/dist/server.js +123 -56
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import fs from "fs/promises";
5
- import { readFileSync, existsSync, writeFileSync, unlinkSync, openSync } from "fs";
5
+ import { readFileSync, existsSync, writeFileSync, unlinkSync } from "fs";
6
6
  import os from "os";
7
7
  import path from "path";
8
8
  import crypto from "crypto";
@@ -17,11 +17,11 @@ var STATUS_PATH = path.join(ANYWHERE_DIR, "status.json");
17
17
  var args = process.argv.slice(2);
18
18
  var command = args.find((a) => !a.startsWith("-"));
19
19
  if (args.includes("--version") || args.includes("-v")) {
20
- console.log(`anywhere-ai v${"0.0.30"}`);
20
+ console.log(`anywhere-ai v${"0.0.32"}`);
21
21
  process.exit(0);
22
22
  }
23
23
  if (args.includes("--help") || args.includes("-h") || command === "help") {
24
- console.log(`anywhere-ai v${"0.0.30"} \u2014 Mobile coding agent
24
+ console.log(`anywhere-ai v${"0.0.32"} \u2014 Mobile coding agent
25
25
 
26
26
  Usage: anywhere-ai [command] [options]
27
27
 
@@ -34,7 +34,7 @@ Commands:
34
34
  Options:
35
35
  --help, -h Show this help message
36
36
  --version, -v Show version
37
- --daemon, -d Run server in background
37
+ --foreground Run in foreground (exit when terminal closes)
38
38
  --no-tunnel Skip Cloudflare tunnel`);
39
39
  process.exit(0);
40
40
  }
@@ -121,32 +121,9 @@ if (command === "regenerate-token") {
121
121
  console.log("New auth token: " + config2.authToken);
122
122
  process.exit(0);
123
123
  }
124
- var isDaemon = args.includes("--daemon") || args.includes("-d");
125
- if (isDaemon && !args.includes("--_daemonized")) {
126
- await fs.mkdir(ANYWHERE_DIR, { recursive: true });
127
- const existingPid2 = getDaemonPid();
128
- if (existingPid2) {
129
- console.log(`Server already running (PID ${existingPid2}). Use 'anywhere-ai stop' first.`);
130
- process.exit(1);
131
- }
132
- const logFd = openSync(LOG_PATH, "a");
133
- const daemonArgs = args.filter((a) => a !== "--daemon" && a !== "-d");
134
- daemonArgs.push("--_daemonized");
135
- const child = spawn(process.execPath, [new URL(import.meta.url).pathname, ...daemonArgs], {
136
- detached: true,
137
- stdio: ["ignore", logFd, logFd],
138
- env: { ...process.env }
139
- });
140
- child.unref();
141
- writeFileSync(PID_PATH, String(child.pid));
142
- console.log(`Starting server in background (PID ${child.pid})...`);
143
- console.log(` Logs: ${LOG_PATH}`);
144
- console.log(` Status: anywhere-ai status`);
145
- console.log(` Stop: anywhere-ai stop`);
146
- process.exit(0);
147
- }
124
+ var isDaemon = !args.includes("--foreground");
148
125
  var existingPid = getDaemonPid();
149
- if (existingPid && !args.includes("--_daemonized")) {
126
+ if (existingPid) {
150
127
  console.log(`Server already running (PID ${existingPid}). Use 'anywhere-ai stop' first.`);
151
128
  process.exit(1);
152
129
  }
@@ -154,9 +131,9 @@ async function checkForUpdate() {
154
131
  try {
155
132
  const res = await fetch("https://registry.npmjs.org/anywhere-ai/latest", { signal: AbortSignal.timeout(3e3) });
156
133
  const data = await res.json();
157
- if (data.version && data.version !== "0.0.30") {
134
+ if (data.version && data.version !== "0.0.32") {
158
135
  console.log(`
159
- Update available: v${"0.0.30"} \u2192 v${data.version}`);
136
+ Update available: v${"0.0.32"} \u2192 v${data.version}`);
160
137
  console.log(` Run: npx anywhere-ai@latest
161
138
  `);
162
139
  }
@@ -438,10 +415,7 @@ if (!args.includes("--no-tunnel")) {
438
415
  }
439
416
  var localURL = isVPS ? "http://" + publicIP + ":" + port : "http://" + (localIP || "localhost") + ":" + port;
440
417
  var serverURL = tunnelURL || localURL;
441
- var isDaemonized = args.includes("--_daemonized");
442
- if (!isDaemonized) {
443
- await printBanner(serverURL, localURL);
444
- }
418
+ await printBanner(serverURL, localURL);
445
419
  writeFileSync(PID_PATH, String(process.pid));
446
420
  writeFileSync(STATUS_PATH, JSON.stringify({
447
421
  serverURL,
@@ -450,6 +424,15 @@ writeFileSync(STATUS_PATH, JSON.stringify({
450
424
  startedAt: Date.now(),
451
425
  pid: process.pid
452
426
  }, null, 2));
427
+ if (isDaemon) {
428
+ process.on("SIGHUP", () => {
429
+ });
430
+ console.log(" Server will keep running after terminal closes.");
431
+ console.log(" Use --foreground to disable this behavior.");
432
+ console.log(` Status: anywhere-ai status`);
433
+ console.log(` Stop: anywhere-ai stop`);
434
+ console.log();
435
+ }
453
436
  function cleanup() {
454
437
  intentionalShutdown = true;
455
438
  if (serverProcess) {
package/dist/server.js CHANGED
@@ -300,6 +300,32 @@ ${prompt.slice(0, 500)}`;
300
300
 
301
301
  // src/routes/chats/index.ts
302
302
  var repoNameCache = /* @__PURE__ */ new Map();
303
+ var sessionFileIndex = /* @__PURE__ */ new Map();
304
+ var indexBuilt = false;
305
+ async function buildSessionIndex() {
306
+ const projectsBase = path3.join(os3.homedir(), ".claude", "projects");
307
+ try {
308
+ const dirs = await readdir2(projectsBase);
309
+ for (const dir of dirs) {
310
+ const dirPath = path3.join(projectsBase, dir);
311
+ try {
312
+ const s = await stat2(dirPath);
313
+ if (!s.isDirectory()) continue;
314
+ const files = await readdir2(dirPath);
315
+ for (const file of files) {
316
+ if (!file.endsWith(".jsonl")) continue;
317
+ const sid = file.replace(".jsonl", "");
318
+ sessionFileIndex.set(sid, path3.join(dirPath, file));
319
+ }
320
+ } catch {
321
+ }
322
+ }
323
+ } catch {
324
+ }
325
+ indexBuilt = true;
326
+ }
327
+ buildSessionIndex();
328
+ var chatDetailCache = /* @__PURE__ */ new Map();
303
329
  function isHomeDir(dir) {
304
330
  const home = os3.homedir();
305
331
  if (dir === home) return true;
@@ -359,6 +385,15 @@ function deriveRepoName(cwd) {
359
385
  return name;
360
386
  }
361
387
  async function findSessionFile(sessionId) {
388
+ if (sessionFileIndex.has(sessionId)) {
389
+ const cached = sessionFileIndex.get(sessionId);
390
+ try {
391
+ await stat2(cached);
392
+ return cached;
393
+ } catch {
394
+ sessionFileIndex.delete(sessionId);
395
+ }
396
+ }
362
397
  const projectsBase = path3.join(os3.homedir(), ".claude", "projects");
363
398
  try {
364
399
  const dirs = await readdir2(projectsBase);
@@ -366,6 +401,7 @@ async function findSessionFile(sessionId) {
366
401
  const filePath = path3.join(projectsBase, dir, `${sessionId}.jsonl`);
367
402
  try {
368
403
  await stat2(filePath);
404
+ sessionFileIndex.set(sessionId, filePath);
369
405
  return filePath;
370
406
  } catch {
371
407
  }
@@ -551,6 +587,64 @@ chats.post("/:id/permission", async (c) => {
551
587
  }
552
588
  return c.json({ ok: true });
553
589
  });
590
+ var listEntryCache = /* @__PURE__ */ new Map();
591
+ function parseListEntry(content, sessionId, fileMtimeIso, chatName) {
592
+ const lines = content.split("\n").filter(Boolean);
593
+ let preview = "";
594
+ let timestamp = "";
595
+ let cwd = "";
596
+ let gitBranch = "";
597
+ let lastMessage = "";
598
+ let lastTimestamp = "";
599
+ for (const line of lines) {
600
+ try {
601
+ const obj = JSON.parse(line);
602
+ if (obj.type === "user") {
603
+ if (!cwd && obj.cwd) cwd = obj.cwd;
604
+ if (!gitBranch && obj.gitBranch) gitBranch = obj.gitBranch;
605
+ if (!obj.isMeta) {
606
+ if (!preview) {
607
+ const msgContent = obj.message.content;
608
+ if (typeof msgContent === "string") {
609
+ preview = msgContent.slice(0, 100);
610
+ } else {
611
+ preview = msgContent.filter((b) => b.type === "text").map((b) => b.text).join("").slice(0, 100);
612
+ }
613
+ timestamp = obj.timestamp;
614
+ }
615
+ if (obj.timestamp) lastTimestamp = obj.timestamp;
616
+ }
617
+ }
618
+ if (obj.type === "assistant") {
619
+ if (obj.timestamp) lastTimestamp = obj.timestamp;
620
+ const content2 = obj.message?.content;
621
+ if (typeof content2 === "string") {
622
+ lastMessage = content2.slice(0, 120);
623
+ } else if (Array.isArray(content2)) {
624
+ const text = content2.filter((b) => b.type === "text").map((b) => b.text).join("").trim();
625
+ if (text) lastMessage = text.slice(0, 120);
626
+ }
627
+ }
628
+ } catch {
629
+ continue;
630
+ }
631
+ }
632
+ if (!timestamp) return null;
633
+ const updatedAt = lastTimestamp || fileMtimeIso;
634
+ const repo = deriveRepoName(cwd);
635
+ return {
636
+ sessionId,
637
+ text: preview,
638
+ timestamp,
639
+ updatedAt,
640
+ lastMessage,
641
+ isActive: activeSessions.has(sessionId),
642
+ repo,
643
+ cwd,
644
+ gitBranch,
645
+ chatName
646
+ };
647
+ }
554
648
  chats.get("/", async (c) => {
555
649
  try {
556
650
  const projectsBase = path3.join(os3.homedir(), ".claude", "projects");
@@ -570,64 +664,30 @@ chats.get("/", async (c) => {
570
664
  );
571
665
  for (const file of files) {
572
666
  const filePath = path3.join(dirPath, file);
573
- const content = await readFile2(filePath, "utf-8");
574
- const lines = content.split("\n").filter(Boolean);
575
667
  const sessionId = file.replace(".jsonl", "");
576
- let preview = "";
577
- let timestamp = "";
578
- let cwd = "";
579
- let gitBranch = "";
580
- let lastMessage = "";
581
- let lastTimestamp = "";
582
- for (const line of lines) {
583
- try {
584
- const obj = JSON.parse(line);
585
- if (obj.type === "user") {
586
- if (!cwd && obj.cwd) cwd = obj.cwd;
587
- if (!gitBranch && obj.gitBranch) gitBranch = obj.gitBranch;
588
- if (!obj.isMeta) {
589
- if (!preview) {
590
- const msgContent = obj.message.content;
591
- if (typeof msgContent === "string") {
592
- preview = msgContent.slice(0, 100);
593
- } else {
594
- preview = msgContent.filter((b) => b.type === "text").map((b) => b.text).join("").slice(0, 100);
595
- }
596
- timestamp = obj.timestamp;
597
- }
598
- if (obj.timestamp) lastTimestamp = obj.timestamp;
599
- }
600
- }
601
- if (obj.type === "assistant") {
602
- if (obj.timestamp) lastTimestamp = obj.timestamp;
603
- const content2 = obj.message?.content;
604
- if (typeof content2 === "string") {
605
- lastMessage = content2.slice(0, 120);
606
- } else if (Array.isArray(content2)) {
607
- const text = content2.filter((b) => b.type === "text").map((b) => b.text).join("").trim();
608
- if (text) lastMessage = text.slice(0, 120);
609
- }
610
- }
611
- } catch {
612
- continue;
613
- }
614
- }
615
- if (!timestamp) continue;
616
668
  const fileStat = await stat2(filePath);
617
- const updatedAt = lastTimestamp || fileStat.mtime.toISOString();
618
- const repo = deriveRepoName(cwd);
619
- allChats.push({
669
+ const cached = listEntryCache.get(sessionId);
670
+ if (cached && cached.mtimeMs === fileStat.mtimeMs) {
671
+ const entry2 = {
672
+ ...cached.entry,
673
+ isActive: activeSessions.has(sessionId),
674
+ chatName: chatNamesMap[sessionId] || ""
675
+ };
676
+ allChats.push(entry2);
677
+ continue;
678
+ }
679
+ const content = await readFile2(filePath, "utf-8");
680
+ const entry = parseListEntry(
681
+ content,
620
682
  sessionId,
621
- text: preview,
622
- timestamp,
623
- updatedAt,
624
- lastMessage,
625
- isActive: activeSessions.has(sessionId),
626
- repo,
627
- cwd,
628
- gitBranch,
629
- chatName: chatNamesMap[sessionId] || ""
630
- });
683
+ fileStat.mtime.toISOString(),
684
+ chatNamesMap[sessionId] || ""
685
+ );
686
+ if (entry) {
687
+ listEntryCache.set(sessionId, { mtimeMs: fileStat.mtimeMs, entry });
688
+ sessionFileIndex.set(sessionId, filePath);
689
+ allChats.push(entry);
690
+ }
631
691
  }
632
692
  }
633
693
  return c.json({ result: allChats });
@@ -643,6 +703,11 @@ chats.get("/:id", async (c) => {
643
703
  try {
644
704
  const file = await findSessionFile(sessionId);
645
705
  if (!file) return c.json({ error: "Chat not found" }, 404);
706
+ const fileStat = await stat2(file);
707
+ const cached = chatDetailCache.get(sessionId);
708
+ if (cached && cached.mtimeMs === fileStat.mtimeMs) {
709
+ return c.json(cached.response);
710
+ }
646
711
  const content = await readFile2(file, "utf-8");
647
712
  const lines = content.split("\n").filter(Boolean);
648
713
  const parsed = lines.map((line) => JSON.parse(line));
@@ -704,7 +769,9 @@ chats.get("/:id", async (c) => {
704
769
  }).filter(Boolean);
705
770
  });
706
771
  const chatName = await getChatName(sessionId);
707
- return c.json({ messages, model, permissionMode, cwd, chatName: chatName || "" });
772
+ const response = { messages, model, permissionMode, cwd, chatName: chatName || "" };
773
+ chatDetailCache.set(sessionId, { mtimeMs: fileStat.mtimeMs, response });
774
+ return c.json(response);
708
775
  } catch {
709
776
  return c.json({ error: "Chat not found" }, 404);
710
777
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anywhere-ai",
3
- "version": "0.0.30",
3
+ "version": "0.0.32",
4
4
  "type": "module",
5
5
  "description": "Code on any repo from your phone",
6
6
  "bin": {