pi-web 0.12.0 → 0.12.1

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.
@@ -1,4 +1,5 @@
1
1
  import { createServer } from 'node:http';
2
+ import { randomUUID } from 'node:crypto';
2
3
  import { createReadStream, existsSync, readFileSync, statSync, unlinkSync } from 'node:fs';
3
4
  import { readdir } from 'node:fs/promises';
4
5
  import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve } from 'node:path';
@@ -210,8 +211,19 @@ const server = createServer((req, res) => {
210
211
  const wss = new WebSocketServer({ server });
211
212
  const rpcSessions = new Map();
212
213
  const socketBindings = new Map();
213
- function buildSessionKey(cwd, sessionFile) {
214
- return `${cwd}::${sessionFile ? basename(sessionFile) : '__new__'}`;
214
+ const socketClientIds = new WeakMap();
215
+ function getSocketClientId(ws) {
216
+ const existing = socketClientIds.get(ws);
217
+ if (existing)
218
+ return existing;
219
+ const next = randomUUID();
220
+ socketClientIds.set(ws, next);
221
+ return next;
222
+ }
223
+ function buildSessionKey(cwd, sessionFile, scope) {
224
+ if (sessionFile)
225
+ return `${cwd}::${basename(sessionFile)}`;
226
+ return `${cwd}::__new__::${scope ?? 'shared'}`;
215
227
  }
216
228
  function getSessionRuntimeStatus(cwd, sessionFile) {
217
229
  const key = buildSessionKey(resolve(cwd), basename(sessionFile));
@@ -330,7 +342,7 @@ function updateSessionModelCapability(managed, event) {
330
342
  }
331
343
  }
332
344
  }
333
- function createManagedSession(cwd, sessionFile) {
345
+ function createManagedSession(cwd, sessionFile, initialKey) {
334
346
  const sessionPath = sessionFile ? getSessionFilePath(cwd, sessionFile, AGENT) : undefined;
335
347
  let managed = null;
336
348
  const rpc = new RpcSession({
@@ -383,7 +395,7 @@ function createManagedSession(cwd, sessionFile) {
383
395
  idleCleanupTimer: null,
384
396
  isClosing: false,
385
397
  };
386
- registerManagedSessionKey(managed, buildSessionKey(cwd, sessionFile));
398
+ registerManagedSessionKey(managed, initialKey);
387
399
  return managed;
388
400
  }
389
401
  wss.on('connection', (ws) => {
@@ -402,7 +414,7 @@ wss.on('connection', (ws) => {
402
414
  const sessionFile = typeof msg.sessionFile === 'string' && msg.sessionFile.length > 0
403
415
  ? basename(msg.sessionFile)
404
416
  : null;
405
- const key = buildSessionKey(cwd, sessionFile);
417
+ const key = buildSessionKey(cwd, sessionFile, sessionFile ? undefined : getSocketClientId(ws));
406
418
  const currentlyBound = socketBindings.get(ws);
407
419
  if (currentlyBound?.keys.has(key)) {
408
420
  clearIdleCleanupTimer(currentlyBound);
@@ -411,7 +423,7 @@ wss.on('connection', (ws) => {
411
423
  detachSocket(ws);
412
424
  let managed = rpcSessions.get(key);
413
425
  if (!managed || managed.isClosing)
414
- managed = createManagedSession(cwd, sessionFile);
426
+ managed = createManagedSession(cwd, sessionFile, key);
415
427
  managed.clients.add(ws);
416
428
  clearIdleCleanupTimer(managed);
417
429
  socketBindings.set(ws, managed);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-web",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "^24"