@adhdev/daemon-standalone 0.8.98 → 0.8.99

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
@@ -32430,6 +32430,7 @@ ${data.message || ""}`.trim();
32430
32430
  var import_path22 = require("path");
32431
32431
  init_config();
32432
32432
  var HERMES_SESSION_ID_RE = /^\d{8}_\d{6}_[a-z0-9]+$/i;
32433
+ var CLAUDE_SESSION_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
32433
32434
  function normalizeProviderSessionId(providerType, providerSessionId) {
32434
32435
  const normalizedProviderType = typeof providerType === "string" ? providerType.trim() : "";
32435
32436
  const normalizedId = typeof providerSessionId === "string" ? providerSessionId.trim() : "";
@@ -32439,6 +32440,9 @@ ${data.message || ""}`.trim();
32439
32440
  if (normalizedProviderType === "hermes-cli" && !HERMES_SESSION_ID_RE.test(normalizedId)) {
32440
32441
  return "";
32441
32442
  }
32443
+ if (normalizedProviderType === "claude-cli" && !CLAUDE_SESSION_ID_RE.test(normalizedId)) {
32444
+ return "";
32445
+ }
32442
32446
  return normalizedId;
32443
32447
  }
32444
32448
  function isLegacyVolatileSessionReadKey(key) {
@@ -35534,9 +35538,16 @@ ${cleanBody}`;
35534
35538
  if (Number.isFinite(stringTimestamp) && stringTimestamp > 0) return stringTimestamp;
35535
35539
  return fallbackTs;
35536
35540
  }
35537
- function readExistingHermesSessionStartRecord(historySessionId) {
35541
+ function extractTimestampValue(value) {
35542
+ const numericTimestamp = Number(value || 0);
35543
+ if (Number.isFinite(numericTimestamp) && numericTimestamp > 0) return numericTimestamp;
35544
+ const stringTimestamp = typeof value === "string" ? Date.parse(value) : NaN;
35545
+ if (Number.isFinite(stringTimestamp) && stringTimestamp > 0) return stringTimestamp;
35546
+ return 0;
35547
+ }
35548
+ function readExistingSessionStartRecord(agentType, historySessionId) {
35538
35549
  try {
35539
- const dir = path7.join(HISTORY_DIR, "hermes-cli");
35550
+ const dir = path7.join(HISTORY_DIR, agentType);
35540
35551
  if (!fs32.existsSync(dir)) return null;
35541
35552
  const files = listHistoryFiles(dir, historySessionId).sort();
35542
35553
  for (const file2 of files) {
@@ -35557,6 +35568,28 @@ ${cleanBody}`;
35557
35568
  return null;
35558
35569
  }
35559
35570
  }
35571
+ function rewriteCanonicalSavedHistory(agentType, historySessionId, records) {
35572
+ if (records.length === 0) return false;
35573
+ try {
35574
+ const dir = path7.join(HISTORY_DIR, agentType);
35575
+ fs32.mkdirSync(dir, { recursive: true });
35576
+ const prefix = `${historySessionId.replace(/[^a-zA-Z0-9_-]/g, "_")}_`;
35577
+ for (const file2 of fs32.readdirSync(dir)) {
35578
+ if (file2.startsWith(prefix) && file2.endsWith(".jsonl")) {
35579
+ fs32.unlinkSync(path7.join(dir, file2));
35580
+ }
35581
+ }
35582
+ const targetDate = new Date(records[records.length - 1].receivedAt || Date.now()).toISOString().slice(0, 10);
35583
+ const filePath = path7.join(dir, `${prefix}${targetDate}.jsonl`);
35584
+ fs32.writeFileSync(filePath, `${records.map((record2) => JSON.stringify(record2)).join("\n")}
35585
+ `, "utf-8");
35586
+ invalidatePersistedSavedHistoryIndex(agentType, dir);
35587
+ savedHistorySessionCache.delete(agentType.replace(/[^a-zA-Z0-9_-]/g, "_"));
35588
+ return true;
35589
+ } catch {
35590
+ return false;
35591
+ }
35592
+ }
35560
35593
  function rebuildHermesSavedHistoryFromCanonicalSession(historySessionId) {
35561
35594
  const normalizedSessionId = normalizeSavedHistorySessionId("hermes-cli", historySessionId);
35562
35595
  if (!normalizedSessionId) return false;
@@ -35567,7 +35600,7 @@ ${cleanBody}`;
35567
35600
  const canonicalMessages = Array.isArray(raw.messages) ? raw.messages : [];
35568
35601
  const dir = path7.join(HISTORY_DIR, "hermes-cli");
35569
35602
  fs32.mkdirSync(dir, { recursive: true });
35570
- const existingSessionStart = readExistingHermesSessionStartRecord(normalizedSessionId);
35603
+ const existingSessionStart = readExistingSessionStartRecord("hermes-cli", normalizedSessionId);
35571
35604
  const records = [];
35572
35605
  if (existingSessionStart) {
35573
35606
  records.push({
@@ -35619,20 +35652,167 @@ ${cleanBody}`;
35619
35652
  });
35620
35653
  }
35621
35654
  }
35622
- if (records.length === 0) return false;
35623
- const prefix = `${normalizedSessionId.replace(/[^a-zA-Z0-9_-]/g, "_")}_`;
35624
- for (const file2 of fs32.readdirSync(dir)) {
35625
- if (file2.startsWith(prefix) && file2.endsWith(".jsonl")) {
35626
- fs32.unlinkSync(path7.join(dir, file2));
35655
+ return rewriteCanonicalSavedHistory("hermes-cli", normalizedSessionId, records);
35656
+ } catch {
35657
+ return false;
35658
+ }
35659
+ }
35660
+ function resolveClaudeProjectTranscriptPath(historySessionId, workspace) {
35661
+ const claudeProjectsDir = path7.join(os52.homedir(), ".claude", "projects");
35662
+ if (!fs32.existsSync(claudeProjectsDir)) return null;
35663
+ const normalizedWorkspace = typeof workspace === "string" ? workspace.trim() : "";
35664
+ if (normalizedWorkspace) {
35665
+ const directPath = path7.join(claudeProjectsDir, normalizedWorkspace.replace(/[\\/]/g, "-"), `${historySessionId}.jsonl`);
35666
+ if (fs32.existsSync(directPath)) return directPath;
35667
+ }
35668
+ const stack = [claudeProjectsDir];
35669
+ while (stack.length > 0) {
35670
+ const current = stack.pop();
35671
+ if (!current) continue;
35672
+ for (const entry of fs32.readdirSync(current, { withFileTypes: true })) {
35673
+ const entryPath = path7.join(current, entry.name);
35674
+ if (entry.isDirectory()) {
35675
+ stack.push(entryPath);
35676
+ continue;
35677
+ }
35678
+ if (entry.isFile() && entry.name === `${historySessionId}.jsonl`) {
35679
+ return entryPath;
35627
35680
  }
35628
35681
  }
35629
- const targetDate = new Date(records[records.length - 1].receivedAt || Date.now()).toISOString().slice(0, 10);
35630
- const filePath = path7.join(dir, `${prefix}${targetDate}.jsonl`);
35631
- fs32.writeFileSync(filePath, `${records.map((record2) => JSON.stringify(record2)).join("\n")}
35632
- `, "utf-8");
35633
- invalidatePersistedSavedHistoryIndex("hermes-cli", dir);
35634
- savedHistorySessionCache.delete("hermes-cli");
35635
- return true;
35682
+ }
35683
+ return null;
35684
+ }
35685
+ function extractClaudeAssistantContentParts(content) {
35686
+ if (typeof content === "string") {
35687
+ const trimmed = content.trim();
35688
+ return trimmed ? [{ content: trimmed, kind: "standard", role: "assistant" }] : [];
35689
+ }
35690
+ if (!Array.isArray(content)) return [];
35691
+ const parts = [];
35692
+ for (const block of content) {
35693
+ if (!block || typeof block !== "object") continue;
35694
+ const record2 = block;
35695
+ const type = String(record2.type || "").trim();
35696
+ if (type === "text") {
35697
+ const text = String(record2.text || "").trim();
35698
+ if (text) parts.push({ content: text, kind: "standard", role: "assistant" });
35699
+ continue;
35700
+ }
35701
+ if (type === "tool_use") {
35702
+ const name = String(record2.name || "").trim() || "Tool";
35703
+ const input = record2.input && typeof record2.input === "object" ? record2.input : null;
35704
+ const command = input ? String(input.command || "").trim() : "";
35705
+ const summary = command ? `${name}: ${command}` : name;
35706
+ if (summary) parts.push({ content: summary, kind: "tool", senderName: "Tool", role: "assistant" });
35707
+ }
35708
+ }
35709
+ return parts;
35710
+ }
35711
+ function extractClaudeUserContentParts(content) {
35712
+ if (typeof content === "string") {
35713
+ const trimmed = content.trim();
35714
+ return trimmed ? [{ role: "user", content: trimmed, kind: "standard" }] : [];
35715
+ }
35716
+ if (!Array.isArray(content)) return [];
35717
+ const parts = [];
35718
+ for (const block of content) {
35719
+ if (!block || typeof block !== "object") continue;
35720
+ const record2 = block;
35721
+ const type = String(record2.type || "").trim();
35722
+ if (type === "text") {
35723
+ const text = String(record2.text || "").trim();
35724
+ if (text) parts.push({ role: "user", content: text, kind: "standard" });
35725
+ continue;
35726
+ }
35727
+ if (type === "tool_result") {
35728
+ const rawContent = record2.content;
35729
+ const text = typeof rawContent === "string" ? rawContent.trim() : Array.isArray(rawContent) ? rawContent.map((entry) => {
35730
+ if (typeof entry === "string") return entry.trim();
35731
+ if (!entry || typeof entry !== "object") return "";
35732
+ const nested = entry;
35733
+ if (typeof nested.text === "string") return nested.text.trim();
35734
+ if (typeof nested.content === "string") return nested.content.trim();
35735
+ return "";
35736
+ }).filter(Boolean).join("\n") : "";
35737
+ if (text) parts.push({ role: "assistant", content: text, kind: "tool", senderName: "Tool" });
35738
+ }
35739
+ }
35740
+ return parts;
35741
+ }
35742
+ function rebuildClaudeSavedHistoryFromNativeProject(historySessionId, workspace) {
35743
+ const normalizedSessionId = normalizeSavedHistorySessionId("claude-cli", historySessionId);
35744
+ if (!normalizedSessionId) return false;
35745
+ try {
35746
+ const transcriptPath = resolveClaudeProjectTranscriptPath(normalizedSessionId, workspace);
35747
+ if (!transcriptPath) return false;
35748
+ const lines = fs32.readFileSync(transcriptPath, "utf-8").split("\n").filter(Boolean);
35749
+ const records = [];
35750
+ const existingSessionStart = readExistingSessionStartRecord("claude-cli", normalizedSessionId);
35751
+ if (existingSessionStart) {
35752
+ records.push({
35753
+ ...existingSessionStart,
35754
+ historySessionId: normalizedSessionId
35755
+ });
35756
+ }
35757
+ let fallbackTs = Date.now();
35758
+ for (const line of lines) {
35759
+ let parsed = null;
35760
+ try {
35761
+ parsed = JSON.parse(line);
35762
+ } catch {
35763
+ parsed = null;
35764
+ }
35765
+ if (!parsed) continue;
35766
+ const parsedSessionId = String(parsed.sessionId || "").trim();
35767
+ if (parsedSessionId && parsedSessionId !== normalizedSessionId) continue;
35768
+ const receivedAt = extractTimestampValue(parsed.timestamp) || fallbackTs;
35769
+ fallbackTs = receivedAt + 1;
35770
+ const parsedWorkspace = String(parsed.cwd || workspace || "").trim();
35771
+ if (records.length === 0 && parsedWorkspace) {
35772
+ records.push({
35773
+ ts: new Date(receivedAt).toISOString(),
35774
+ receivedAt,
35775
+ role: "system",
35776
+ kind: "session_start",
35777
+ content: parsedWorkspace,
35778
+ agent: "claude-cli",
35779
+ historySessionId: normalizedSessionId,
35780
+ workspace: parsedWorkspace
35781
+ });
35782
+ }
35783
+ const type = String(parsed.type || "").trim();
35784
+ const message = parsed.message && typeof parsed.message === "object" ? parsed.message : null;
35785
+ if (type === "user" && message) {
35786
+ for (const part of extractClaudeUserContentParts(message.content)) {
35787
+ records.push({
35788
+ ts: new Date(receivedAt).toISOString(),
35789
+ receivedAt,
35790
+ role: part.role,
35791
+ content: part.content,
35792
+ kind: part.kind,
35793
+ senderName: part.senderName,
35794
+ agent: "claude-cli",
35795
+ historySessionId: normalizedSessionId
35796
+ });
35797
+ }
35798
+ continue;
35799
+ }
35800
+ if (type === "assistant" && message) {
35801
+ for (const part of extractClaudeAssistantContentParts(message.content)) {
35802
+ records.push({
35803
+ ts: new Date(receivedAt).toISOString(),
35804
+ receivedAt,
35805
+ role: "assistant",
35806
+ content: part.content,
35807
+ kind: part.kind,
35808
+ senderName: part.senderName,
35809
+ agent: "claude-cli",
35810
+ historySessionId: normalizedSessionId
35811
+ });
35812
+ }
35813
+ }
35814
+ }
35815
+ return rewriteCanonicalSavedHistory("claude-cli", normalizedSessionId, records);
35636
35816
  } catch {
35637
35817
  return false;
35638
35818
  }
@@ -40627,7 +40807,7 @@ ${effect.notification.body || ""}`.trim();
40627
40807
  parsedMessages = historyMessageCount > 0 ? parsedMessages.slice(-historyMessageCount) : [];
40628
40808
  }
40629
40809
  const mergedMessages = this.mergeConversationMessages(parsedMessages);
40630
- const canonicalHermesBackedHistory = this.syncCanonicalHermesSavedHistoryIfNeeded();
40810
+ const canonicalBackedHistory = this.syncCanonicalSavedHistoryIfNeeded();
40631
40811
  const dirName = this.workingDir.split("/").filter(Boolean).pop() || "session";
40632
40812
  if (parsedMessages.length > 0) {
40633
40813
  const shouldSkipReplayPersist = this.suppressIdleHistoryReplay && adapterStatus.status === "idle" && parsedStatus?.status === "idle";
@@ -40645,7 +40825,7 @@ ${effect.notification.body || ""}`.trim();
40645
40825
  senderName: typeof message.senderName === "string" ? message.senderName : void 0,
40646
40826
  receivedAt: typeof message.receivedAt === "number" ? message.receivedAt : message.timestamp
40647
40827
  }));
40648
- if (!canonicalHermesBackedHistory && !shouldSkipReplayPersist && normalizedMessagesToSave.length > 0) {
40828
+ if (!canonicalBackedHistory && !shouldSkipReplayPersist && normalizedMessagesToSave.length > 0) {
40649
40829
  const incrementalMessages = buildIncrementalHistoryAppendMessages(this.lastPersistedHistoryMessages, normalizedMessagesToSave);
40650
40830
  this.historyWriter.appendNewMessages(
40651
40831
  this.type,
@@ -40655,7 +40835,7 @@ ${effect.notification.body || ""}`.trim();
40655
40835
  this.providerSessionId
40656
40836
  );
40657
40837
  }
40658
- if (!canonicalHermesBackedHistory) {
40838
+ if (!canonicalBackedHistory) {
40659
40839
  this.lastPersistedHistoryMessages = normalizedMessagesToSave;
40660
40840
  }
40661
40841
  }
@@ -41132,32 +41312,52 @@ ${effect.notification.body || ""}`.trim();
41132
41312
  });
41133
41313
  LOG2.info("CLI", `[${this.type}] discovered provider session id: ${nextSessionId}`);
41134
41314
  }
41135
- syncCanonicalHermesSavedHistoryIfNeeded() {
41136
- if (this.type !== "hermes-cli" || !this.providerSessionId) return false;
41137
- try {
41138
- const canonicalPath = path11.join(os11.homedir(), ".hermes", "sessions", `session_${this.providerSessionId}.json`);
41139
- if (!fs52.existsSync(canonicalPath)) return false;
41140
- const stat4 = fs52.statSync(canonicalPath);
41141
- if (stat4.mtimeMs <= this.lastCanonicalHermesSyncMtimeMs) return true;
41142
- const rebuilt = rebuildHermesSavedHistoryFromCanonicalSession(this.providerSessionId);
41143
- if (!rebuilt) return false;
41144
- this.lastCanonicalHermesSyncMtimeMs = stat4.mtimeMs;
41145
- const restoredHistory = readChatHistory(this.type, 0, Number.MAX_SAFE_INTEGER, this.providerSessionId);
41146
- this.lastPersistedHistoryMessages = restoredHistory.messages.map((message) => ({
41147
- role: message.role,
41148
- content: message.content,
41149
- kind: message.kind,
41150
- senderName: message.senderName,
41151
- receivedAt: message.receivedAt
41152
- }));
41153
- return true;
41154
- } catch {
41155
- return false;
41315
+ syncCanonicalSavedHistoryIfNeeded() {
41316
+ if (!this.providerSessionId) return false;
41317
+ if (this.type === "hermes-cli") {
41318
+ try {
41319
+ const canonicalPath = path11.join(os11.homedir(), ".hermes", "sessions", `session_${this.providerSessionId}.json`);
41320
+ if (!fs52.existsSync(canonicalPath)) return false;
41321
+ const stat4 = fs52.statSync(canonicalPath);
41322
+ if (stat4.mtimeMs <= this.lastCanonicalHermesSyncMtimeMs) return true;
41323
+ const rebuilt = rebuildHermesSavedHistoryFromCanonicalSession(this.providerSessionId);
41324
+ if (!rebuilt) return false;
41325
+ this.lastCanonicalHermesSyncMtimeMs = stat4.mtimeMs;
41326
+ const restoredHistory = readChatHistory(this.type, 0, Number.MAX_SAFE_INTEGER, this.providerSessionId);
41327
+ this.lastPersistedHistoryMessages = restoredHistory.messages.map((message) => ({
41328
+ role: message.role,
41329
+ content: message.content,
41330
+ kind: message.kind,
41331
+ senderName: message.senderName,
41332
+ receivedAt: message.receivedAt
41333
+ }));
41334
+ return true;
41335
+ } catch {
41336
+ return false;
41337
+ }
41156
41338
  }
41339
+ if (this.type === "claude-cli") {
41340
+ try {
41341
+ const rebuilt = rebuildClaudeSavedHistoryFromNativeProject(this.providerSessionId, this.workingDir);
41342
+ if (!rebuilt) return false;
41343
+ const restoredHistory = readChatHistory(this.type, 0, Number.MAX_SAFE_INTEGER, this.providerSessionId);
41344
+ this.lastPersistedHistoryMessages = restoredHistory.messages.map((message) => ({
41345
+ role: message.role,
41346
+ content: message.content,
41347
+ kind: message.kind,
41348
+ senderName: message.senderName,
41349
+ receivedAt: message.receivedAt
41350
+ }));
41351
+ return true;
41352
+ } catch {
41353
+ return false;
41354
+ }
41355
+ }
41356
+ return false;
41157
41357
  }
41158
41358
  restorePersistedHistoryFromCurrentSession() {
41159
41359
  if (!this.providerSessionId) return;
41160
- this.syncCanonicalHermesSavedHistoryIfNeeded();
41360
+ this.syncCanonicalSavedHistoryIfNeeded();
41161
41361
  this.historyWriter.compactHistorySession(this.type, this.providerSessionId);
41162
41362
  const restoredHistory = readChatHistory(this.type, 0, Number.MAX_SAFE_INTEGER, this.providerSessionId);
41163
41363
  this.historyWriter.seedSessionHistory(