wherever-dev 0.22.0 → 0.23.0

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 (65) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/assistant-tools.d.ts +15 -0
  3. package/dist/assistant-tools.d.ts.map +1 -1
  4. package/dist/assistant-tools.js +21 -0
  5. package/dist/assistant-tools.js.map +1 -1
  6. package/dist/delta-coalescer.d.ts +77 -0
  7. package/dist/delta-coalescer.d.ts.map +1 -0
  8. package/dist/delta-coalescer.js +117 -0
  9. package/dist/delta-coalescer.js.map +1 -0
  10. package/dist/frame-chunks.d.ts +41 -0
  11. package/dist/frame-chunks.d.ts.map +1 -0
  12. package/dist/frame-chunks.js +61 -0
  13. package/dist/frame-chunks.js.map +1 -0
  14. package/dist/history-images.d.ts +33 -0
  15. package/dist/history-images.d.ts.map +1 -0
  16. package/dist/history-images.js +47 -0
  17. package/dist/history-images.js.map +1 -0
  18. package/dist/index.js +148 -7
  19. package/dist/index.js.map +1 -1
  20. package/dist/session-pool.d.ts +3 -0
  21. package/dist/session-pool.d.ts.map +1 -1
  22. package/dist/session-pool.js.map +1 -1
  23. package/dist/session-transcript.d.ts +19 -0
  24. package/dist/session-transcript.d.ts.map +1 -1
  25. package/dist/session-transcript.js +67 -0
  26. package/dist/session-transcript.js.map +1 -1
  27. package/dist/session-types.d.ts +21 -2
  28. package/dist/session-types.d.ts.map +1 -1
  29. package/dist/static-compression.d.ts +15 -0
  30. package/dist/static-compression.d.ts.map +1 -0
  31. package/dist/static-compression.js +87 -0
  32. package/dist/static-compression.js.map +1 -0
  33. package/dist/telegram-channel.d.ts +3 -0
  34. package/dist/telegram-channel.d.ts.map +1 -1
  35. package/dist/telegram-channel.js +1 -0
  36. package/dist/telegram-channel.js.map +1 -1
  37. package/dist/telegram-replies.d.ts +53 -7
  38. package/dist/telegram-replies.d.ts.map +1 -1
  39. package/dist/telegram-replies.js +167 -22
  40. package/dist/telegram-replies.js.map +1 -1
  41. package/package.json +1 -1
  42. package/public/_app/immutable/assets/{0.BkzT3rFr.css → 0.hWfYEET-.css} +1 -1
  43. package/public/_app/immutable/chunks/{CkOw6fRa.js → BHw5ar2A.js} +2 -2
  44. package/public/_app/immutable/chunks/{CkOw6fRa.js.map → BHw5ar2A.js.map} +1 -1
  45. package/public/_app/immutable/chunks/{DyHllYtq.js → CLKlGLL7.js} +2 -2
  46. package/public/_app/immutable/chunks/{DyHllYtq.js.map → CLKlGLL7.js.map} +1 -1
  47. package/public/_app/immutable/chunks/{CExVgK9Z.js → FePVnZ_7.js} +2 -2
  48. package/public/_app/immutable/chunks/{CExVgK9Z.js.map → FePVnZ_7.js.map} +1 -1
  49. package/public/_app/immutable/entry/{app.D3PEFbtP.js → app.BJxkSaKZ.js} +3 -3
  50. package/public/_app/immutable/entry/{app.D3PEFbtP.js.map → app.BJxkSaKZ.js.map} +1 -1
  51. package/public/_app/immutable/entry/start.CRP1MCwE.js +2 -0
  52. package/public/_app/immutable/entry/start.CRP1MCwE.js.map +1 -0
  53. package/public/_app/immutable/nodes/{0.DG3L8hMN.js → 0.DGFJlad5.js} +2 -2
  54. package/public/_app/immutable/nodes/{0.DG3L8hMN.js.map → 0.DGFJlad5.js.map} +1 -1
  55. package/public/_app/immutable/nodes/{1.BCpuoZxW.js → 1.Cy3IVwDh.js} +2 -2
  56. package/public/_app/immutable/nodes/{1.BCpuoZxW.js.map → 1.Cy3IVwDh.js.map} +1 -1
  57. package/public/_app/immutable/nodes/2.eb9p7ShF.js +123 -0
  58. package/public/_app/immutable/nodes/2.eb9p7ShF.js.map +1 -0
  59. package/public/_app/version.json +1 -1
  60. package/public/index.html +11 -11
  61. package/public/service-worker.js +1 -1
  62. package/public/_app/immutable/entry/start.BR00Qps5.js +0 -2
  63. package/public/_app/immutable/entry/start.BR00Qps5.js.map +0 -1
  64. package/public/_app/immutable/nodes/2.D2auiZW1.js +0 -122
  65. package/public/_app/immutable/nodes/2.D2auiZW1.js.map +0 -1
package/dist/index.js CHANGED
@@ -8,10 +8,11 @@ import { readDrafts, addDraft, deleteDraft, validateDraftInput } from './drafts.
8
8
  import { startAssistantFolder } from './assistant-folder.js';
9
9
  import { AssistantController } from './assistant-controller.js';
10
10
  import { AssistantScheduler } from './assistant-scheduler.js';
11
- import { assistantNotActiveError, setTimezone, WEB_TIMEZONE_SOURCE, } from './assistant-tools.js';
11
+ import { assistantNotActiveError, setTimezone, testNotification, WEB_TIMEZONE_SOURCE, } from './assistant-tools.js';
12
12
  import { ChannelDispatcher } from './assistant-channels.js';
13
13
  import { WebPushChannel, resolveVapidSubject, validateSubscription } from './web-push.js';
14
14
  import { TelegramChannel } from './telegram-channel.js';
15
+ import { normalizeToolCalls } from './telegram-replies.js';
15
16
  import { normalizeUserId } from './telegram-pairing.js';
16
17
  import { resolveTelegramToken, takeTelegramTokenEnv } from './telegram-token.js';
17
18
  import { DEFAULT_GRACE_MS } from './assistant-schedule.js';
@@ -25,6 +26,11 @@ import { INITIAL_HISTORY_LIMIT, HISTORY_PAGE_SIZE } from './protocol.js';
25
26
  import { gateUploadFolder, gateUploadWrite, uploadMaxBytes, uploadTooLarge } from './upload-gate.js';
26
27
  import { readAttachGrants } from './download-grants.js';
27
28
  import { DEFAULT_DOWNLOAD_MAX_BYTES } from './attach-file-tool.js';
29
+ import { splitFrame, wantsFrameChunks, FRAME_CHUNK_CHARS } from './frame-chunks.js';
30
+ import { compressCached, isCompressible, negotiateEncoding } from './static-compression.js';
31
+ import { stripHistoryImages, wantsImageRefs, SERVABLE_IMAGE_TYPES } from './history-images.js';
32
+ import { readToolResultImage } from './session-transcript.js';
33
+ import { DeltaCoalescer } from './delta-coalescer.js';
28
34
  import { UploadStaging, UPLOAD_CAPABILITY, isValidUploadId, parseChunkFrame, CHUNK_FRAME_KIND } from './upload-staging.js';
29
35
  import fs from 'node:fs';
30
36
  import path from 'node:path';
@@ -271,7 +277,7 @@ async function resolveSafeDownloadPath(requested, cwd, roots, grants) {
271
277
  }
272
278
  return null;
273
279
  }
274
- function serveStaticFile(reqPath, res) {
280
+ function serveStaticFile(reqPath, res, acceptEncoding) {
275
281
  let filePath = path.join(staticDir, reqPath === '/' ? 'index.html' : reqPath);
276
282
  if (!filePath.startsWith(staticDir)) {
277
283
  res.writeHead(403);
@@ -312,8 +318,26 @@ function serveStaticFile(reqPath, res) {
312
318
  if (debugEnabled && filePath.endsWith('index.html')) {
313
319
  data = Buffer.from(data.toString().replace(ERUDA_PLUGINS_META_FALSE, ERUDA_PLUGINS_META_TRUE));
314
320
  }
315
- res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': cacheControl });
316
- res.end(data);
321
+ const encoding = isCompressible(contentType, data.length) ? negotiateEncoding(acceptEncoding) : null;
322
+ if (!encoding) {
323
+ res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': cacheControl, Vary: 'Accept-Encoding' });
324
+ res.end(data);
325
+ return;
326
+ }
327
+ const body = data;
328
+ compressCached(body, encoding).then((compressed) => {
329
+ res.writeHead(200, {
330
+ 'Content-Type': contentType,
331
+ 'Cache-Control': cacheControl,
332
+ 'Content-Encoding': encoding,
333
+ 'Content-Length': compressed.length,
334
+ Vary: 'Accept-Encoding',
335
+ });
336
+ res.end(compressed);
337
+ }, () => {
338
+ res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': cacheControl, Vary: 'Accept-Encoding' });
339
+ res.end(body);
340
+ });
317
341
  }
318
342
  });
319
343
  }
@@ -865,10 +889,48 @@ function readBody(req, maxLimitBytes = 1e6) {
865
889
  req.on('error', (err) => reject(err));
866
890
  });
867
891
  }
892
+ // Sockets whose client asked for chunked frames (`frames=chunk.v1` on the socket
893
+ // URL, see frame-chunks.ts). Keyed by socket so every sender gets it for free.
894
+ const chunkingSockets = new WeakSet();
895
+ let nextFrameChunkId = 1;
896
+ // Sockets whose client loads large history images by reference (`imageRefs=1`,
897
+ // see history-images.ts) instead of receiving their base64 inline.
898
+ const imageRefSockets = new WeakSet();
899
+ // Per-socket coalescing of streamed deltas (delta-coalescer.ts): fewer, larger
900
+ // frames, frame order unchanged. "Backed up" = more than 64 KB still unsent.
901
+ const deltaCoalescers = new WeakMap();
902
+ const BACKED_UP_BYTES = 64 * 1024;
903
+ function coalescerFor(ws) {
904
+ let c = deltaCoalescers.get(ws);
905
+ if (!c) {
906
+ c = new DeltaCoalescer((m) => writeWS(ws, m), {
907
+ isBackedUp: () => ws.bufferedAmount > BACKED_UP_BYTES,
908
+ });
909
+ deltaCoalescers.set(ws, c);
910
+ ws.once('close', () => c.dispose());
911
+ }
912
+ return c;
913
+ }
868
914
  function sendWS(ws, msg) {
915
+ if (ws.readyState !== WebSocket.OPEN)
916
+ return;
917
+ coalescerFor(ws).send(msg);
918
+ }
919
+ function writeWS(ws, msg) {
869
920
  if (ws.readyState === WebSocket.OPEN) {
870
921
  try {
871
- ws.send(JSON.stringify(msg));
922
+ if ((msg.type === 'message_history' || msg.type === 'message_history_prepend') && imageRefSockets.has(ws)) {
923
+ msg = { ...msg, messages: stripHistoryImages(msg.messages) };
924
+ }
925
+ const json = JSON.stringify(msg);
926
+ if (chunkingSockets.has(ws) && json.length > FRAME_CHUNK_CHARS) {
927
+ // Back to back, so frame order on the socket is unchanged.
928
+ for (const piece of splitFrame(json, nextFrameChunkId++))
929
+ ws.send(piece);
930
+ }
931
+ else {
932
+ ws.send(json);
933
+ }
872
934
  }
873
935
  catch (err) {
874
936
  console.error('Failed to send WS message:', err);
@@ -899,6 +961,8 @@ function sendThenTerminate(ws, msg, flushTimeoutMs = 1_000) {
899
961
  };
900
962
  const timer = setTimeout(kill, flushTimeoutMs);
901
963
  timer.unref?.();
964
+ // Anything still held by the delta coalescer goes out first, in order.
965
+ deltaCoalescers.get(ws)?.flush();
902
966
  if (ws.readyState !== WebSocket.OPEN) {
903
967
  clearTimeout(timer);
904
968
  kill();
@@ -1095,7 +1159,11 @@ async function main() {
1095
1159
  // session does (`uploads.type`), under the same size limit.
1096
1160
  uploadDir: () => resolveUploadDir(getWhereverConfig(), assistant.status.folder ?? undefined),
1097
1161
  maxUploadBytes: () => uploadMaxBytes(getWhereverConfig()),
1162
+ toolCalls: normalizeToolCalls(telegramConfig?.toolCalls),
1098
1163
  });
1164
+ if (telegramConfig?.toolCalls !== undefined && telegramConfig.toolCalls !== 'off' && telegramConfig.toolCalls !== 'compact') {
1165
+ console.warn(`[wherever] telegram: ignoring assistantTelegram.toolCalls=${JSON.stringify(telegramConfig.toolCalls)} (expected "off" or "compact"); using "off"`);
1166
+ }
1099
1167
  }
1100
1168
  }
1101
1169
  const assistantChannels = [
@@ -1731,6 +1799,32 @@ async function main() {
1731
1799
  sendJSON(res, 200, { subscribed: false, removed });
1732
1800
  return;
1733
1801
  }
1802
+ // The settings' "Send test notification": the same delivery as
1803
+ // `assistant_notify` (every channel), opening the session the calling
1804
+ // device has open (`sessionId`), or the main screen without one.
1805
+ if (pathname === '/push/test' && req.method === 'POST') {
1806
+ let parsed = {};
1807
+ try {
1808
+ parsed = JSON.parse((await readBody(req)) || '{}');
1809
+ }
1810
+ catch {
1811
+ parsed = {};
1812
+ }
1813
+ if (!notifyBackend || !assistant.status.folder) {
1814
+ sendJSON(res, 503, { error: 'the assistant channels are still starting; try again in a moment' });
1815
+ return;
1816
+ }
1817
+ const outcome = await testNotification(notifyBackend, {
1818
+ sessionId: parsed && typeof parsed === 'object' ? parsed.sessionId : undefined,
1819
+ assistantFolder: assistant.status.folder,
1820
+ });
1821
+ if (!outcome.ok) {
1822
+ sendJSON(res, 502, { error: outcome.error, deliveries: outcome.deliveries });
1823
+ return;
1824
+ }
1825
+ sendJSON(res, 200, { ok: true, deliveries: outcome.deliveries });
1826
+ return;
1827
+ }
1734
1828
  }
1735
1829
  catch (err) {
1736
1830
  sendJSON(res, 500, { error: err.message || 'Web Push failed' });
@@ -2322,6 +2416,41 @@ async function main() {
2322
2416
  }
2323
2417
  return;
2324
2418
  }
2419
+ // A tool image sent by reference in message_history (history-images.ts).
2420
+ // Same token gate as every /session/ route. A tool result never changes once
2421
+ // written, so the browser may keep the image for good.
2422
+ if (pathname === '/session/tool-image' && req.method === 'GET') {
2423
+ const qSessionId = url.searchParams.get('sessionId') || '';
2424
+ const qToolCallId = url.searchParams.get('toolCallId') || '';
2425
+ const qEntryId = url.searchParams.get('entryId') || undefined;
2426
+ const qIndexRaw = url.searchParams.get('index');
2427
+ const qIndex = qIndexRaw ? Number(qIndexRaw) : NaN;
2428
+ if (!qSessionId || !qToolCallId || !Number.isInteger(qIndex) || qIndex < 0) {
2429
+ sendJSON(res, 400, { error: 'Missing sessionId, toolCallId or index' });
2430
+ return;
2431
+ }
2432
+ const located = await sessionPool.locateSession(qSessionId);
2433
+ const image = located
2434
+ ? await readToolResultImage(located.sessionFile, { toolCallId: qToolCallId, entryId: qEntryId }, qIndex)
2435
+ : null;
2436
+ if (!image) {
2437
+ sendJSON(res, 404, { error: 'Image not found' });
2438
+ return;
2439
+ }
2440
+ if (!SERVABLE_IMAGE_TYPES.has(image.mimeType)) {
2441
+ sendJSON(res, 415, { error: `Unsupported image type ${image.mimeType}` });
2442
+ return;
2443
+ }
2444
+ const bytes = Buffer.from(image.data, 'base64');
2445
+ res.writeHead(200, {
2446
+ 'Content-Type': image.mimeType,
2447
+ 'Content-Length': bytes.length,
2448
+ 'Cache-Control': 'private, max-age=31536000, immutable',
2449
+ 'X-Content-Type-Options': 'nosniff',
2450
+ });
2451
+ res.end(bytes);
2452
+ return;
2453
+ }
2325
2454
  if (pathname === '/session/download' && req.method === 'GET') {
2326
2455
  const config = getWhereverConfig();
2327
2456
  if (config.downloads?.enabled === false) {
@@ -2490,7 +2619,7 @@ async function main() {
2490
2619
  });
2491
2620
  return;
2492
2621
  }
2493
- serveStaticFile(pathname, res);
2622
+ serveStaticFile(pathname, res, req.headers['accept-encoding']);
2494
2623
  };
2495
2624
  let server;
2496
2625
  let httpServer = null;
@@ -2530,7 +2659,15 @@ async function main() {
2530
2659
  else {
2531
2660
  server = createHttpServer(requestHandler);
2532
2661
  }
2533
- const wss = new WebSocketServer({ noServer: true });
2662
+ // permessage-deflate: frames are JSON text (history, tool output, streamed
2663
+ // replies) that compresses several times over, which matters most on the slow
2664
+ // links phones are on. Browsers negotiate it natively; a client that does not
2665
+ // offer the extension simply gets uncompressed frames. Small frames (most
2666
+ // streaming deltas) are left alone: deflating them costs CPU for no gain.
2667
+ const wss = new WebSocketServer({
2668
+ noServer: true,
2669
+ perMessageDeflate: { threshold: 1024 },
2670
+ });
2534
2671
  // Connection-liveness heartbeat. A half-open TCP socket (peer vanished without
2535
2672
  // a clean FIN/RST: process restart, network blip, dropped upstream) stays in
2536
2673
  // ESTAB and fires neither 'close' nor 'error', so the connected agent/session
@@ -2602,6 +2739,10 @@ async function main() {
2602
2739
  else if (raw) {
2603
2740
  clientKey = raw;
2604
2741
  }
2742
+ if (wantsFrameChunks(url.searchParams))
2743
+ chunkingSockets.add(ws);
2744
+ if (wantsImageRefs(url.searchParams))
2745
+ imageRefSockets.add(ws);
2605
2746
  }
2606
2747
  catch { }
2607
2748
  if (clientKey) {