pi-web 0.4.2 → 0.4.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.
@@ -150,7 +150,13 @@ wss.on('connection', (ws) => {
150
150
  return;
151
151
  }
152
152
  if (msg.type === 'start_session') {
153
- rpcSessions.get(ws)?.kill();
153
+ const previousRpc = rpcSessions.get(ws);
154
+ if (previousRpc) {
155
+ rpcSessions.delete(ws);
156
+ previousRpc.kill();
157
+ }
158
+ const rpcRef = { current: null };
159
+ const isCurrentRpc = () => rpcRef.current != null && rpcSessions.get(ws) === rpcRef.current;
154
160
  const rpc = new RpcSession({
155
161
  piCmd: PI_CMD,
156
162
  cwd: msg.cwd || process.env.HOME || '/',
@@ -158,19 +164,26 @@ wss.on('connection', (ws) => {
158
164
  ? getSessionFilePath(msg.cwd || process.env.HOME || '/', msg.sessionFile)
159
165
  : undefined,
160
166
  onEvent: (event) => {
167
+ if (!isCurrentRpc())
168
+ return;
161
169
  if (ws.readyState === WebSocket.OPEN)
162
170
  ws.send(JSON.stringify({ type: 'rpc_event', event }));
163
171
  },
164
172
  onError: (error) => {
173
+ if (!isCurrentRpc())
174
+ return;
165
175
  if (ws.readyState === WebSocket.OPEN)
166
176
  ws.send(JSON.stringify({ type: 'error', message: error }));
167
177
  },
168
178
  onExit: (code) => {
179
+ if (!isCurrentRpc())
180
+ return;
169
181
  rpcSessions.delete(ws);
170
182
  if (ws.readyState === WebSocket.OPEN)
171
183
  ws.send(JSON.stringify({ type: 'session_ended', code }));
172
184
  },
173
185
  });
186
+ rpcRef.current = rpc;
174
187
  rpcSessions.set(ws, rpc);
175
188
  return;
176
189
  }
@@ -5,7 +5,7 @@ import { createReadStream } from 'node:fs';
5
5
  import { createInterface } from 'node:readline';
6
6
  const SESSION_DIR = join(homedir(), '.pi', 'agent', 'sessions');
7
7
  function cwdToSessionDir(cwd) {
8
- return '-' + cwd.replace(/\//g, '-') + '-';
8
+ return '-' + cwd.replace(/\//g, '-') + '--';
9
9
  }
10
10
  export function getSessionFilePath(cwd, filename) {
11
11
  return join(SESSION_DIR, cwdToSessionDir(cwd), filename);