@dev-anywhere/proxy 0.4.3 → 0.4.5

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/serve.js CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  createScopedApprovalRequestIdFactory,
11
11
  disposeSeqCounter,
12
12
  getSeqCounterFor
13
- } from "./chunk-UDLPU2NC.js";
13
+ } from "./chunk-V2Z6E3NX.js";
14
14
  import {
15
15
  appendPtySemanticTextTail,
16
16
  decidePtySemanticTransition,
@@ -42,22 +42,23 @@ import {
42
42
  serializeControl,
43
43
  serializeIpc,
44
44
  serializeWorkerMsg
45
- } from "./chunk-IWWADGYM.js";
45
+ } from "./chunk-7J7RDKYI.js";
46
46
  import {
47
47
  buildProviderEnv,
48
48
  loadConfig,
49
49
  saveAgentCliPath
50
- } from "./chunk-RIQ6OL7X.js";
50
+ } from "./chunk-4543KN27.js";
51
51
  import {
52
52
  atomicWriteFileSync
53
53
  } from "./chunk-NQDJ6QAM.js";
54
54
  import {
55
55
  flushLogger,
56
56
  serviceLogger
57
- } from "./chunk-7UOPAMX7.js";
57
+ } from "./chunk-LWDOXOWY.js";
58
58
  import {
59
59
  DATA_DIR,
60
60
  DEFAULT_PROXY_PROFILE,
61
+ HISTORY_METADATA_PATH,
61
62
  HOOK_REGISTRY_PATH,
62
63
  PID_PATH,
63
64
  PROFILE_NAME,
@@ -68,15 +69,95 @@ import {
68
69
  ensureProfileWorkspace,
69
70
  sessionPaths,
70
71
  tildify
71
- } from "./chunk-PWG6K5QB.js";
72
+ } from "./chunk-7RSBFN2G.js";
72
73
 
73
74
  // src/serve.ts
74
75
  import { createServer as createServer2 } from "net";
75
76
  import { unlinkSync as unlinkSync2, writeFileSync as writeFileSync4, chmodSync, rmSync as rmSync2 } from "fs";
76
77
 
77
78
  // src/serve/session-manager.ts
78
- import { readFileSync, existsSync } from "fs";
79
+ import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
79
80
  import { nanoid } from "nanoid";
81
+
82
+ // src/serve/session-history-metadata.ts
83
+ import { existsSync, readFileSync } from "fs";
84
+ function isProviderId(value) {
85
+ return value === "claude" || value === "codex";
86
+ }
87
+ function isSessionMode(value) {
88
+ return value === "pty" || value === "json";
89
+ }
90
+ function normalizeMetadataRecord(value) {
91
+ if (!value || typeof value !== "object") return null;
92
+ const record = value;
93
+ if (typeof record.nativeSessionId !== "string" || !record.nativeSessionId) return null;
94
+ if (typeof record.devAnywhereSessionId !== "string" || !record.devAnywhereSessionId) return null;
95
+ if (!isProviderId(record.provider)) return null;
96
+ if (!isSessionMode(record.mode)) return null;
97
+ if (typeof record.cwd !== "string" || !record.cwd) return null;
98
+ const updatedAt = typeof record.updatedAt === "number" && Number.isFinite(record.updatedAt) ? record.updatedAt : Date.now();
99
+ return {
100
+ nativeSessionId: record.nativeSessionId,
101
+ devAnywhereSessionId: record.devAnywhereSessionId,
102
+ provider: record.provider,
103
+ mode: record.mode,
104
+ cwd: record.cwd,
105
+ ...typeof record.title === "string" && record.title.trim() ? { title: record.title.trim() } : {},
106
+ updatedAt
107
+ };
108
+ }
109
+ function readSessionHistoryMetadata(metadataPath) {
110
+ if (!metadataPath || !existsSync(metadataPath)) return [];
111
+ try {
112
+ const parsed = JSON.parse(readFileSync(metadataPath, "utf-8"));
113
+ if (!Array.isArray(parsed)) return [];
114
+ return parsed.flatMap((value) => {
115
+ const record = normalizeMetadataRecord(value);
116
+ return record ? [record] : [];
117
+ });
118
+ } catch {
119
+ return [];
120
+ }
121
+ }
122
+ function writeSessionHistoryMetadata(metadataPath, records) {
123
+ atomicWriteFileSync(metadataPath, `${JSON.stringify(records, null, 2)}
124
+ `, { ensureDir: true });
125
+ }
126
+ function upsertSessionHistoryMetadata(metadataPath, record) {
127
+ if (!metadataPath) return;
128
+ const records = readSessionHistoryMetadata(metadataPath);
129
+ const key = metadataKey(record.provider, record.nativeSessionId);
130
+ const next = [
131
+ record,
132
+ ...records.filter(
133
+ (existing) => metadataKey(existing.provider, existing.nativeSessionId) !== key
134
+ )
135
+ ];
136
+ writeSessionHistoryMetadata(metadataPath, next);
137
+ }
138
+ function applySessionHistoryMetadata(sessions, metadata) {
139
+ if (metadata.length === 0) return sessions;
140
+ const byNativeSession = /* @__PURE__ */ new Map();
141
+ for (const record of metadata) {
142
+ const key = metadataKey(record.provider, record.nativeSessionId);
143
+ const existing = byNativeSession.get(key);
144
+ if (!existing || existing.updatedAt < record.updatedAt) byNativeSession.set(key, record);
145
+ }
146
+ return sessions.map((session) => {
147
+ const record = byNativeSession.get(metadataKey(session.provider, session.id));
148
+ return record ? {
149
+ ...session,
150
+ title: record.title ?? session.title,
151
+ projectDir: record.cwd || session.projectDir,
152
+ preferredMode: record.mode
153
+ } : session;
154
+ });
155
+ }
156
+ function metadataKey(provider, nativeSessionId) {
157
+ return `${provider}:${nativeSessionId}`;
158
+ }
159
+
160
+ // src/serve/session-manager.ts
80
161
  var PTY_TRANSITIONS = {
81
162
  [SessionState.IDLE]: [
82
163
  // claude 开始响应用户输入 → handlePtyData 首字节翻 working
@@ -147,7 +228,7 @@ var jsonFSM = defineFSM(JSON_TRANSITIONS);
147
228
  function fsmForMode(mode) {
148
229
  return mode === "pty" ? ptyFSM : jsonFSM;
149
230
  }
150
- function isProviderId(value) {
231
+ function isProviderId2(value) {
151
232
  return value === "claude" || value === "codex";
152
233
  }
153
234
  var SessionManager = class {
@@ -155,10 +236,12 @@ var SessionManager = class {
155
236
  pendingPtyReconnectMetadata = /* @__PURE__ */ new Map();
156
237
  reaperTimer = null;
157
238
  persistPath;
239
+ historyMetadataPath;
158
240
  reaperIntervalMs;
159
241
  onSessionRemoved;
160
242
  constructor(options) {
161
243
  this.persistPath = options.persistPath;
244
+ this.historyMetadataPath = options.historyMetadataPath;
162
245
  this.reaperIntervalMs = options.reaperIntervalMs ?? 6e4;
163
246
  this.onSessionRemoved = options.onSessionRemoved;
164
247
  this.load();
@@ -268,6 +351,7 @@ var SessionManager = class {
268
351
  }
269
352
  session.claudeSessionId = claudeSessionId;
270
353
  this.save();
354
+ this.recordRestoreMetadata(session, claudeSessionId);
271
355
  }
272
356
  setPid(id, pid) {
273
357
  const session = this.sessions.get(id);
@@ -289,9 +373,23 @@ var SessionManager = class {
289
373
  session.name = trimmed;
290
374
  session.nameLocked = true;
291
375
  this.save();
376
+ if (session.claudeSessionId) {
377
+ this.recordRestoreMetadata(session, session.claudeSessionId);
378
+ }
292
379
  serviceLogger.info({ sessionId: id }, "Session renamed");
293
380
  return { success: true, name: trimmed };
294
381
  }
382
+ recordRestoreMetadata(session, nativeSessionId) {
383
+ upsertSessionHistoryMetadata(this.historyMetadataPath, {
384
+ nativeSessionId,
385
+ devAnywhereSessionId: session.id,
386
+ provider: session.provider,
387
+ mode: session.mode,
388
+ cwd: session.cwd,
389
+ ...session.name !== void 0 ? { title: session.name } : {},
390
+ updatedAt: Date.now()
391
+ });
392
+ }
295
393
  startReaper(intervalMs = this.reaperIntervalMs) {
296
394
  this.stopReaper();
297
395
  this.reaperTimer = setInterval(() => this.reap(), intervalMs);
@@ -350,10 +448,10 @@ var SessionManager = class {
350
448
  };
351
449
  }
352
450
  load() {
353
- if (!existsSync(this.persistPath)) {
451
+ if (!existsSync2(this.persistPath)) {
354
452
  return;
355
453
  }
356
- const raw = readFileSync(this.persistPath, "utf-8");
454
+ const raw = readFileSync2(this.persistPath, "utf-8");
357
455
  let parsed;
358
456
  try {
359
457
  parsed = JSON.parse(raw);
@@ -382,7 +480,7 @@ var SessionManager = class {
382
480
  continue;
383
481
  }
384
482
  const info = item;
385
- if (!isProviderId(info.provider)) {
483
+ if (!isProviderId2(info.provider)) {
386
484
  const sessionId = String(info.id);
387
485
  this.onSessionRemoved?.(sessionId);
388
486
  serviceLogger.warn(
@@ -426,7 +524,7 @@ var SessionManager = class {
426
524
 
427
525
  // src/serve/relay-connection.ts
428
526
  import WebSocket from "ws";
429
- import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
527
+ import { readFileSync as readFileSync3, existsSync as existsSync3 } from "fs";
430
528
  import { homedir } from "os";
431
529
  import { join } from "path";
432
530
  import { nanoid as nanoid2 } from "nanoid";
@@ -521,8 +619,8 @@ var RelayConnection = class extends EventEmitter {
521
619
  }
522
620
  // 从文件读取或生成新的 proxyId,生成后持久化到文件
523
621
  loadOrCreateProxyId(idPath) {
524
- if (existsSync2(idPath)) {
525
- const existing = readFileSync2(idPath, "utf-8").trim();
622
+ if (existsSync3(idPath)) {
623
+ const existing = readFileSync3(idPath, "utf-8").trim();
526
624
  if (existing.length > 0) {
527
625
  return existing;
528
626
  }
@@ -725,16 +823,20 @@ var INTERNAL_TITLE_PATTERNS = [
725
823
  /^codex agent history\b/i,
726
824
  /^conversation summary\b/i
727
825
  ];
728
- async function scanSessionHistory() {
729
- const entries = [...await scanClaudeSessionHistory(), ...await scanCodexSessionHistory()];
826
+ async function scanSessionHistory(options = {}) {
827
+ const entries = applySessionHistoryMetadata(
828
+ [...await scanClaudeSessionHistory(), ...await scanCodexSessionHistory()],
829
+ readSessionHistoryMetadata(options.metadataPath)
830
+ );
730
831
  entries.sort((a, b) => b.updatedAt - a.updatedAt);
731
832
  const seen = /* @__PURE__ */ new Set();
732
- return entries.filter((e) => {
733
- const key = `${e.provider}::${e.projectDir}::${e.title}`;
833
+ const uniqueEntries = entries.filter((e) => {
834
+ const key = `${e.provider}::${e.projectDir}::${e.title}::${e.preferredMode ?? "unknown"}`;
734
835
  if (seen.has(key)) return false;
735
836
  seen.add(key);
736
837
  return true;
737
838
  });
839
+ return uniqueEntries;
738
840
  }
739
841
  async function scanClaudeSessionHistory() {
740
842
  const entries = [];
@@ -1105,7 +1207,7 @@ function extractCodexUserText(payload) {
1105
1207
  }
1106
1208
 
1107
1209
  // src/serve/command-discovery.ts
1108
- import { readdirSync, readFileSync as readFileSync3 } from "fs";
1210
+ import { readdirSync, readFileSync as readFileSync4 } from "fs";
1109
1211
  import { homedir as homedir3 } from "os";
1110
1212
  import { join as join3 } from "path";
1111
1213
  var REPL_BUILTINS = [
@@ -1181,7 +1283,7 @@ function scanSkillsDir(dirPath, source) {
1181
1283
  for (const name of entries) {
1182
1284
  const skillPath = join3(dirPath, name, "SKILL.md");
1183
1285
  try {
1184
- const content = readFileSync3(skillPath, "utf-8");
1286
+ const content = readFileSync4(skillPath, "utf-8");
1185
1287
  const parsed = parseSkillFrontmatter(content);
1186
1288
  commands.push({
1187
1289
  name: `/${parsed.name ?? name}`,
@@ -1205,7 +1307,7 @@ function scanCommandsDir(dirPath, source) {
1205
1307
  for (const filename of entries) {
1206
1308
  const cmdName = filename.replace(/\.md$/, "");
1207
1309
  try {
1208
- const content = readFileSync3(join3(dirPath, filename), "utf-8");
1310
+ const content = readFileSync4(join3(dirPath, filename), "utf-8");
1209
1311
  const firstLine = content.split("\n")[0].trim();
1210
1312
  commands.push({
1211
1313
  name: `/${cmdName}`,
@@ -1434,7 +1536,7 @@ function createControlMessageHandlers(send, sessionManager) {
1434
1536
  },
1435
1537
  async handleSessionHistoryRequest(msg) {
1436
1538
  try {
1437
- const sessions = await scanSessionHistory();
1539
+ const sessions = await scanSessionHistory({ metadataPath: HISTORY_METADATA_PATH });
1438
1540
  send(
1439
1541
  serializeControl({
1440
1542
  type: "session_history_response",
@@ -1580,7 +1682,7 @@ function createControlMessageHandlers(send, sessionManager) {
1580
1682
 
1581
1683
  // src/serve/worker-registry.ts
1582
1684
  import { connect } from "net";
1583
- import { unlinkSync, existsSync as existsSync3, readdirSync as readdirSync2 } from "fs";
1685
+ import { unlinkSync, existsSync as existsSync4, readdirSync as readdirSync2 } from "fs";
1584
1686
  var WorkerRegistry = class {
1585
1687
  constructor(deps) {
1586
1688
  this.deps = deps;
@@ -1674,12 +1776,12 @@ var WorkerRegistry = class {
1674
1776
  }
1675
1777
  // 枚举 DATA_DIR 下所有 session 目录,尝试连接存活的 worker.sock;失败则清理 stale socket。
1676
1778
  async reconnectAll() {
1677
- if (!existsSync3(DATA_DIR)) return;
1779
+ if (!existsSync4(DATA_DIR)) return;
1678
1780
  const dirs = readdirSync2(DATA_DIR, { withFileTypes: true }).filter((d) => d.isDirectory());
1679
1781
  for (const dir of dirs) {
1680
1782
  const sessionId = dir.name;
1681
1783
  const paths = sessionPaths(sessionId);
1682
- if (!existsSync3(paths.workerSock)) continue;
1784
+ if (!existsSync4(paths.workerSock)) continue;
1683
1785
  const sock = await this.connect(sessionId, paths.workerSock);
1684
1786
  if (sock) {
1685
1787
  if (!this.deps.sessionManager.getSession(sessionId)) {
@@ -2058,7 +2160,7 @@ function saveClipboardImageUpload(request, options = {}) {
2058
2160
  }
2059
2161
 
2060
2162
  // src/serve/file-download.ts
2061
- import { readFileSync as readFileSync4, realpathSync, statSync } from "fs";
2163
+ import { readFileSync as readFileSync5, realpathSync, statSync } from "fs";
2062
2164
  import { extname, isAbsolute as isAbsolute2, resolve } from "path";
2063
2165
  var MAX_FILE_DOWNLOAD_BYTES = 100 * 1024 * 1024;
2064
2166
  var EXT_MIME_MAP = {
@@ -2126,7 +2228,7 @@ function loadFileDownload(request, options) {
2126
2228
  errorCode: ControlErrorCode.UNKNOWN
2127
2229
  };
2128
2230
  }
2129
- const buffer = readFileSync4(resolvedPath);
2231
+ const buffer = readFileSync5(resolvedPath);
2130
2232
  return {
2131
2233
  success: true,
2132
2234
  sessionId: request.sessionId,
@@ -2197,7 +2299,7 @@ async function saveFileUpload(request, options = {}) {
2197
2299
  }
2198
2300
 
2199
2301
  // src/serve/image-preview.ts
2200
- import { readFileSync as readFileSync5, realpathSync as realpathSync2, statSync as statSync2 } from "fs";
2302
+ import { readFileSync as readFileSync6, realpathSync as realpathSync2, statSync as statSync2 } from "fs";
2201
2303
  import { tmpdir as tmpdir3 } from "os";
2202
2304
  import { isAbsolute as isAbsolute3, relative, resolve as resolve2 } from "path";
2203
2305
  var MAX_IMAGE_PREVIEW_BYTES = 10 * 1024 * 1024;
@@ -2268,7 +2370,7 @@ function loadImagePreview(request, options) {
2268
2370
  errorCode: ControlErrorCode.UNKNOWN
2269
2371
  };
2270
2372
  }
2271
- const buffer = readFileSync5(resolvedPath);
2373
+ const buffer = readFileSync6(resolvedPath);
2272
2374
  const mimeType = detectImageMime(buffer);
2273
2375
  if (!mimeType) {
2274
2376
  return {
@@ -4387,7 +4489,7 @@ function createEventBridge(deps) {
4387
4489
 
4388
4490
  // src/serve/service-files.ts
4389
4491
  import { execSync } from "child_process";
4390
- import { existsSync as existsSync4, readFileSync as readFileSync6 } from "fs";
4492
+ import { existsSync as existsSync5, readFileSync as readFileSync7 } from "fs";
4391
4493
  import { hostname } from "os";
4392
4494
  import { connect as connect2 } from "net";
4393
4495
  function tryConnectSocket(sockPath) {
@@ -4406,7 +4508,7 @@ function isProcessAlive(pid) {
4406
4508
  }
4407
4509
  }
4408
4510
  async function cleanupStaleResources() {
4409
- if (existsSync4(SOCK_PATH)) {
4511
+ if (existsSync5(SOCK_PATH)) {
4410
4512
  const existing = await tryConnectSocket(SOCK_PATH);
4411
4513
  if (existing) {
4412
4514
  existing.destroy();
@@ -4419,8 +4521,8 @@ async function cleanupStaleResources() {
4419
4521
  unlinkIfPresent(SOCK_PATH);
4420
4522
  serviceLogger.info("Removed stale socket file");
4421
4523
  }
4422
- if (existsSync4(PID_PATH)) {
4423
- const pidStr = readFileSync6(PID_PATH, "utf-8").trim();
4524
+ if (existsSync5(PID_PATH)) {
4525
+ const pidStr = readFileSync7(PID_PATH, "utf-8").trim();
4424
4526
  const pid = parseInt(pidStr, 10);
4425
4527
  if (!isNaN(pid) && isProcessAlive(pid)) {
4426
4528
  const msg = `Another service is already running with PID ${pid}`;
@@ -4753,7 +4855,7 @@ function handleTerminalConnection(socket, deps) {
4753
4855
 
4754
4856
  // src/serve/hook-registry.ts
4755
4857
  import { createHash as createHash2, randomBytes } from "crypto";
4756
- import { existsSync as existsSync5, mkdirSync as mkdirSync3, readFileSync as readFileSync7, renameSync, writeFileSync as writeFileSync3 } from "fs";
4858
+ import { existsSync as existsSync6, mkdirSync as mkdirSync3, readFileSync as readFileSync8, renameSync, writeFileSync as writeFileSync3 } from "fs";
4757
4859
  import { dirname } from "path";
4758
4860
  import { z } from "zod";
4759
4861
  var PersistedHookSessionBindingSchema = z.object({
@@ -4814,10 +4916,10 @@ var HookRegistry = class {
4814
4916
  }
4815
4917
  }
4816
4918
  load() {
4817
- if (!this.persistPath || !existsSync5(this.persistPath)) return;
4919
+ if (!this.persistPath || !existsSync6(this.persistPath)) return;
4818
4920
  try {
4819
4921
  const parsed = PersistedHookRegistrySchema.parse(
4820
- JSON.parse(readFileSync7(this.persistPath, "utf8"))
4922
+ JSON.parse(readFileSync8(this.persistPath, "utf8"))
4821
4923
  );
4822
4924
  this.bindingsBySession.clear();
4823
4925
  for (const binding of parsed.bindings) {
@@ -5182,6 +5284,7 @@ async function startService(options) {
5182
5284
  };
5183
5285
  const sessionManager = new SessionManager({
5184
5286
  persistPath: SESSIONS_PATH,
5287
+ historyMetadataPath: HISTORY_METADATA_PATH,
5185
5288
  onSessionRemoved: (id, context) => {
5186
5289
  if (!context?.preserveProviderHooks) {
5187
5290
  unregisterHookSession(id);