@wherever-dev/client 0.8.1 → 0.9.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.
- package/CHANGELOG.md +18 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +131 -45
- package/dist/client.js.map +1 -1
- package/dist/frame-chunks.d.ts +39 -0
- package/dist/frame-chunks.js +70 -0
- package/dist/frame-chunks.js.map +1 -0
- package/dist/types.d.ts +13 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +135 -50
- package/src/frame-chunks.ts +77 -0
- package/src/types.ts +13 -1
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAuWA,8EAA8E;AAC9E,oEAAoE;AACpE,EAAE;AACF,0EAA0E;AAC1E,iEAAiE;AACjE,oCAAoC;AACpC,8EAA8E;AAC9E,4CAA4C;AAC5C,oFAAoF;AACpF,8EAA8E;AAC9E,+EAA+E;AAC/E,4EAA4E;AAC5E,iCAAiC;AACjC,MAAM,UAAU,oBAAoB,CACnC,OAAe;IAEf,IAAI,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IAC1B,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IACpC,gDAAgD;IAChD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;IAC7D,IAAI,IAAI,EAAE,CAAC;QACV,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC7C,IAAI,QAAQ,KAAK,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC;QACjC,OAAO;YACN,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;YAClB,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE;SACxD,CAAC;IACH,CAAC;IACD,yEAAyE;IACzE,IAAI,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAC7C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,SAAS,GAAG,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;QACnE,IAAI,CAAC,SAAS;YAAE,OAAO,IAAI,CAAC;QAC5B,MAAM,IAAI,GAAG,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACpE,OAAO,EAAC,SAAS,EAAE,IAAI,EAAC,CAAC;IAC1B,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,iFAAiF;AACjF,iFAAiF;AACjF,kEAAkE;AAClE,MAAM,UAAU,uBAAuB,CAAC,OAAe;IACtD,MAAM,MAAM,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,OAAO,SAAS,MAAM,CAAC,SAAS,SAAS,MAAM,CAAC,IAAI,EAAE,CAAC;AACxD,CAAC"}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
type WhereverState,
|
|
12
12
|
skillInvocationIdentity,
|
|
13
13
|
} from "./types.js";
|
|
14
|
+
import { FrameReassembler, FRAME_CHUNK_PARAM, FRAME_CHUNK_VERSION } from "./frame-chunks.js";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Per-send options for a user message.
|
|
@@ -50,6 +51,11 @@ export interface WhereverClientConfig {
|
|
|
50
51
|
// wherever `clientKey` came from, so the collision is resolved for good rather
|
|
51
52
|
// than re-created on the next reconnect.
|
|
52
53
|
onClientKeyChange?: (key: string) => void;
|
|
54
|
+
// Ask for large `message_history` tool images BY REFERENCE (`imageRefs=1`):
|
|
55
|
+
// they then arrive without `data`, and the app must load them from
|
|
56
|
+
// `GET /session/tool-image` (see ToolImage). Off by default, so a consumer
|
|
57
|
+
// that renders `img.data` keeps getting it; the web app turns it on.
|
|
58
|
+
imageRefs?: boolean;
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
// Per-instance fallback identity: stable for the lifetime of this client object,
|
|
@@ -307,7 +313,11 @@ export class WhereverClient {
|
|
|
307
313
|
private createWatchdog: any = null;
|
|
308
314
|
private static readonly CREATE_WATCHDOG_MS = 25_000;
|
|
309
315
|
private reconnectAttempts = 0;
|
|
310
|
-
private
|
|
316
|
+
private static readonly MIN_RECONNECT_DELAY = 2000;
|
|
317
|
+
private reconnectDelay = WhereverClient.MIN_RECONNECT_DELAY;
|
|
318
|
+
// Wall-clock time the current socket was created, so a return to the page can
|
|
319
|
+
// tell a handshake that has hung across a freeze from one just started.
|
|
320
|
+
private socketStartedAt = 0;
|
|
311
321
|
private maxReconnectDelay = 15000;
|
|
312
322
|
// Whether the socket has EVER opened successfully since construction. Repeated
|
|
313
323
|
// closes with zero successful opens almost always mean a rejected handshake
|
|
@@ -343,6 +353,20 @@ export class WhereverClient {
|
|
|
343
353
|
// the timers fire BEFORE the frames queued during the freeze are dispatched,
|
|
344
354
|
// so the raw silence verdict is routinely false on a phone.
|
|
345
355
|
private static readonly PROBE_GRACE_MS = 5_000;
|
|
356
|
+
// The probe a return to the page starts (noteVisible) uses a shorter grace.
|
|
357
|
+
// The long grace above exists because timers that came due DURING a freeze
|
|
358
|
+
// fire before the frames queued during it; a probe started from the
|
|
359
|
+
// visibility event is a fresh timer, so the queued frames (and a healthy
|
|
360
|
+
// socket's pong, one round trip later) are dispatched well before it fires.
|
|
361
|
+
// Waiting the full 5 s there, then the reconnect backoff on top, is what made
|
|
362
|
+
// coming back to the PWA slower than a reload.
|
|
363
|
+
private static readonly VISIBLE_PROBE_GRACE_MS = 2_000;
|
|
364
|
+
// A socket still CONNECTING this long when the page becomes visible is
|
|
365
|
+
// treated as a handshake that hung (typically started before a freeze,
|
|
366
|
+
// against a network that was gone) and restarted. Well above an honest slow
|
|
367
|
+
// mobile TLS handshake, so a return never throws away one that is about to
|
|
368
|
+
// complete.
|
|
369
|
+
private static readonly STALLED_HANDSHAKE_MS = 8_000;
|
|
346
370
|
// The pending probe's grace timer, and the socket it was started on. Cleared
|
|
347
371
|
// by any inbound frame, by stopLivenessWatchdog (every socket change), and
|
|
348
372
|
// ignored if the socket it belongs to is no longer current.
|
|
@@ -496,6 +520,14 @@ export class WhereverClient {
|
|
|
496
520
|
const params: string[] = [];
|
|
497
521
|
if (this.config.token) params.push(`token=${encodeURIComponent(this.config.token)}`);
|
|
498
522
|
params.push(`clientKey=${encodeURIComponent(this.clientKey)}`);
|
|
523
|
+
// Ask the server to split large frames (frame-chunks.ts): a multi-MB history
|
|
524
|
+
// arriving as ONE frame over a slow link looked like a dead socket to the
|
|
525
|
+
// liveness watchdog, which tore it down and looped forever.
|
|
526
|
+
params.push(`${FRAME_CHUNK_PARAM}=${FRAME_CHUNK_VERSION}`);
|
|
527
|
+
// History tool images by reference (fetched lazily, cached by the browser)
|
|
528
|
+
// rather than as base64 re-sent on every load and reconnect. Opt-in: the
|
|
529
|
+
// caller must then render images that have no `data`.
|
|
530
|
+
if (this.config.imageRefs) params.push('imageRefs=1');
|
|
499
531
|
const wsUrl = `${protocol}://${host}:${this.config.port}/ws?${params.join('&')}`;
|
|
500
532
|
|
|
501
533
|
const WebSocketImpl = this.config.WebSocketCtor || (typeof globalThis !== 'undefined' ? (globalThis as any).WebSocket : null);
|
|
@@ -511,6 +543,7 @@ export class WhereverClient {
|
|
|
511
543
|
|
|
512
544
|
// A new socket: its capabilities are unknown until its `connected` frame.
|
|
513
545
|
this.socketCapabilities = null;
|
|
546
|
+
this.socketStartedAt = Date.now();
|
|
514
547
|
try {
|
|
515
548
|
const wsOptions = this.config.secure && this.config.WebSocketCtor ? { rejectUnauthorized: false } : undefined;
|
|
516
549
|
this.ws = wsOptions ? new WebSocketImpl(wsUrl, wsOptions) : new WebSocketImpl(wsUrl);
|
|
@@ -525,6 +558,10 @@ export class WhereverClient {
|
|
|
525
558
|
}
|
|
526
559
|
|
|
527
560
|
const currentWs = this.ws;
|
|
561
|
+
const reassembler = new FrameReassembler();
|
|
562
|
+
// One warning per run of bad chunks: after one, every remaining chunk of
|
|
563
|
+
// that message is invalid too.
|
|
564
|
+
let inBadChunkRun = false;
|
|
528
565
|
|
|
529
566
|
const onOpen = () => {
|
|
530
567
|
if (this.ws !== currentWs) return;
|
|
@@ -533,7 +570,7 @@ export class WhereverClient {
|
|
|
533
570
|
}
|
|
534
571
|
this.hasEverConnected = true;
|
|
535
572
|
this.reconnectAttempts = 0;
|
|
536
|
-
this.reconnectDelay =
|
|
573
|
+
this.reconnectDelay = WhereverClient.MIN_RECONNECT_DELAY;
|
|
537
574
|
this.isInitialConnect = false;
|
|
538
575
|
this.startLivenessWatchdog();
|
|
539
576
|
this.stateStore.update(s => ({
|
|
@@ -610,7 +647,20 @@ export class WhereverClient {
|
|
|
610
647
|
this.cancelLivenessProbe();
|
|
611
648
|
try {
|
|
612
649
|
const rawData = typeof event.data === 'string' ? event.data : event.data.toString();
|
|
613
|
-
const
|
|
650
|
+
const piece = reassembler.push(rawData);
|
|
651
|
+
if (piece.kind !== 'invalid') inBadChunkRun = false;
|
|
652
|
+
if (piece.kind === 'partial') {
|
|
653
|
+
// A large frame is still arriving: the load it answers is making
|
|
654
|
+
// progress, so it must not time out meanwhile.
|
|
655
|
+
if (this.loadWatchdog) this.armLoadWatchdog();
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (piece.kind === 'invalid') {
|
|
659
|
+
if (!inBadChunkRun) console.warn('[wherever] dropped a malformed or incomplete chunked frame');
|
|
660
|
+
inBadChunkRun = true;
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const msg = JSON.parse(piece.text);
|
|
614
664
|
this.handleMessage(msg);
|
|
615
665
|
} catch (err) {
|
|
616
666
|
console.error("Failed to parse WebSocket message:", err);
|
|
@@ -870,20 +920,40 @@ export class WhereverClient {
|
|
|
870
920
|
// socket is OPEN and watched, probe it right away instead of waiting for the
|
|
871
921
|
// next tick, and reset the stale baseline so the time the page spent frozen is
|
|
872
922
|
// not counted as silence (the probe decides whether the socket is alive).
|
|
873
|
-
//
|
|
874
|
-
//
|
|
923
|
+
//
|
|
924
|
+
// The user is looking at the page, so a return must never pay the slow paths
|
|
925
|
+
// meant for an unattended client: the probe uses the short
|
|
926
|
+
// VISIBLE_PROBE_GRACE_MS, a socket it condemns is replaced at once instead of
|
|
927
|
+
// after the reconnect backoff, and the backoff grown while hidden is reset.
|
|
928
|
+
// A socket still CONNECTING after STALLED_HANDSHAKE_MS is restarted.
|
|
929
|
+
// With no socket at all this only resets the backoff: the page follows up
|
|
930
|
+
// with resume() / connect(), which connect at once.
|
|
875
931
|
public noteVisible() {
|
|
932
|
+
this.reconnectDelay = WhereverClient.MIN_RECONNECT_DELAY;
|
|
876
933
|
const ws = this.ws as any;
|
|
877
|
-
if (!ws
|
|
934
|
+
if (!ws) return;
|
|
935
|
+
if (ws.readyState === 0 /* CONNECTING */) {
|
|
936
|
+
if (Date.now() - this.socketStartedAt > WhereverClient.STALLED_HANDSHAKE_MS) {
|
|
937
|
+
console.info('[wherever] handshake stalled; restarting it');
|
|
938
|
+
this.reconnectNow();
|
|
939
|
+
}
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
if (ws.readyState !== 1 /* OPEN */ || !this.livenessTimer) return;
|
|
878
943
|
this.lastInboundAt = Date.now();
|
|
879
944
|
this.lastLivenessTickAt = this.lastInboundAt;
|
|
880
945
|
// Restart any pending probe: its grace may have been eaten by the freeze.
|
|
881
946
|
this.cancelLivenessProbe();
|
|
882
|
-
this.startLivenessProbe('page became visible');
|
|
947
|
+
this.startLivenessProbe('page became visible', true);
|
|
883
948
|
}
|
|
884
949
|
|
|
885
950
|
// Send a ping and arm the grace check. Idempotent while a probe is pending.
|
|
886
|
-
|
|
951
|
+
// `onReturn` marks the probe a return to the page started: short grace, and
|
|
952
|
+
// an immediate reconnect if it goes unanswered.
|
|
953
|
+
private startLivenessProbe(reason: string, onReturn = false) {
|
|
954
|
+
const grace = onReturn
|
|
955
|
+
? WhereverClient.VISIBLE_PROBE_GRACE_MS
|
|
956
|
+
: WhereverClient.PROBE_GRACE_MS;
|
|
887
957
|
if (this.probeTimer || !this.ws || typeof setTimeout !== 'function') return;
|
|
888
958
|
const probedWs = this.ws;
|
|
889
959
|
try {
|
|
@@ -900,15 +970,17 @@ export class WhereverClient {
|
|
|
900
970
|
// background timer throttling (timers batched to ~1/min in a long-hidden
|
|
901
971
|
// tab) every grace check is late, so the verdict on a dead socket is
|
|
902
972
|
// deferred until the page is visible again, where noteVisible() restarts
|
|
903
|
-
// an unthrottled probe and a dead socket goes ~
|
|
973
|
+
// an unthrottled probe and a dead socket goes ~2 s later. Accepted: a
|
|
904
974
|
// hidden page has no viewer, and a capped retry would reintroduce the
|
|
905
975
|
// false teardown on thaw this probe exists to prevent.
|
|
906
|
-
|
|
976
|
+
// A return probe whose check ran late means the page froze again, so
|
|
977
|
+
// nobody is waiting on it any more: re-probe as an ordinary probe.
|
|
978
|
+
if (Date.now() - sentAt > 2 * grace) {
|
|
907
979
|
this.startLivenessProbe(reason);
|
|
908
980
|
return;
|
|
909
981
|
}
|
|
910
|
-
this.tearDownStaleSocket(reason);
|
|
911
|
-
},
|
|
982
|
+
this.tearDownStaleSocket(reason, grace, onReturn);
|
|
983
|
+
}, grace);
|
|
912
984
|
if (this.probeTimer && typeof (this.probeTimer as any).unref === 'function') {
|
|
913
985
|
(this.probeTimer as any).unref();
|
|
914
986
|
}
|
|
@@ -924,12 +996,12 @@ export class WhereverClient {
|
|
|
924
996
|
// The probe went unanswered: treat the socket as dead. Tearing it down
|
|
925
997
|
// forcibly is what makes a half-open connection recover, since the OS never
|
|
926
998
|
// produced a FIN/RST to trigger the normal close path.
|
|
927
|
-
private tearDownStaleSocket(reason: string) {
|
|
999
|
+
private tearDownStaleSocket(reason: string, graceMs: number, immediate: boolean) {
|
|
928
1000
|
if (!this.ws) return;
|
|
929
1001
|
const streaming = get(this.stateStore).isStreaming;
|
|
930
1002
|
console.warn(
|
|
931
1003
|
`[wherever] relay connection stale: ${reason}; probe unanswered for ` +
|
|
932
|
-
`${
|
|
1004
|
+
`${graceMs}ms; tearing down dead socket and reconnecting`,
|
|
933
1005
|
);
|
|
934
1006
|
this.stopLivenessWatchdog();
|
|
935
1007
|
const deadWs = this.ws;
|
|
@@ -957,7 +1029,10 @@ export class WhereverClient {
|
|
|
957
1029
|
: s.sessionError,
|
|
958
1030
|
isStreaming: false,
|
|
959
1031
|
}));
|
|
960
|
-
|
|
1032
|
+
// A probe started by a return to the page has a user waiting on it: no
|
|
1033
|
+
// backoff, the dead socket is replaced now.
|
|
1034
|
+
if (immediate) this.reconnectNow();
|
|
1035
|
+
else this.scheduleReconnect();
|
|
961
1036
|
}
|
|
962
1037
|
|
|
963
1038
|
private stopLivenessWatchdog() {
|
|
@@ -1040,46 +1115,56 @@ export class WhereverClient {
|
|
|
1040
1115
|
this.reconnectTimer = setTimeout(() => {
|
|
1041
1116
|
this.reconnectTimer = null;
|
|
1042
1117
|
this.reconnectDelay = Math.min(this.reconnectDelay * 1.5, this.maxReconnectDelay);
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
// almost certainly a rejected handshake, not a flaky network. Surface a
|
|
1047
|
-
// concrete, actionable error (the server rejects a bad/missing token with
|
|
1048
|
-
// HTTP 401 during the WS upgrade, which the browser reports only as an
|
|
1049
|
-
// opaque 1006 close). Keep retrying, but stop looking like a mere network
|
|
1050
|
-
// hiccup.
|
|
1051
|
-
if (
|
|
1052
|
-
!this.hasEverConnected &&
|
|
1053
|
-
this.reconnectAttempts >= WhereverClient.HANDSHAKE_FAIL_HINT_AFTER
|
|
1054
|
-
) {
|
|
1055
|
-
const scheme = this.config.secure ? 'wss' : 'ws';
|
|
1056
|
-
this.stateStore.update(s => ({
|
|
1057
|
-
...s,
|
|
1058
|
-
error:
|
|
1059
|
-
'Cannot connect to the wherever server: the connection is being rejected. ' +
|
|
1060
|
-
'This usually means a missing or wrong token, or a wrong host/port/scheme ' +
|
|
1061
|
-
`(currently ${scheme}://${this.config.host}:${this.config.port}). ` +
|
|
1062
|
-
'Check the auth token and connection settings.',
|
|
1063
|
-
}));
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
// If we still hold an active session, PRESERVE the cached store across the
|
|
1067
|
-
// reconnect. A default connect() resets to a blank store, which wiped
|
|
1068
|
-
// activeSessionFile BEFORE onOpen could re-attach to it -- that is what
|
|
1069
|
-
// silently detached the frontend from a still-running session (frozen
|
|
1070
|
-
// conversation, no re-follow, not even a "connecting"/"loading" hint).
|
|
1071
|
-
// Preserving the store keeps the conversation on screen and lets onOpen
|
|
1072
|
-
// re-issue session_load from activeSessionFile.
|
|
1073
|
-
const preserve = !!get(this.stateStore).activeSessionFile;
|
|
1118
|
+
this.reconnectNow();
|
|
1119
|
+
}, this.reconnectDelay);
|
|
1120
|
+
}
|
|
1074
1121
|
|
|
1122
|
+
// Reconnect right away (the body of a backoff timer firing), preserving the
|
|
1123
|
+
// store when a session is active so onOpen rejoins it in place.
|
|
1124
|
+
private reconnectNow() {
|
|
1125
|
+
if (this.reconnectTimer) {
|
|
1126
|
+
clearTimeout(this.reconnectTimer);
|
|
1127
|
+
this.reconnectTimer = null;
|
|
1128
|
+
}
|
|
1129
|
+
console.info(`[wherever] reconnecting to relay (attempt ${++this.reconnectAttempts})`);
|
|
1130
|
+
|
|
1131
|
+
// If we have retried a few times and NEVER once opened the socket, this is
|
|
1132
|
+
// almost certainly a rejected handshake, not a flaky network. Surface a
|
|
1133
|
+
// concrete, actionable error (the server rejects a bad/missing token with
|
|
1134
|
+
// HTTP 401 during the WS upgrade, which the browser reports only as an
|
|
1135
|
+
// opaque 1006 close). Keep retrying, but stop looking like a mere network
|
|
1136
|
+
// hiccup.
|
|
1137
|
+
if (
|
|
1138
|
+
!this.hasEverConnected &&
|
|
1139
|
+
this.reconnectAttempts >= WhereverClient.HANDSHAKE_FAIL_HINT_AFTER
|
|
1140
|
+
) {
|
|
1141
|
+
const scheme = this.config.secure ? 'wss' : 'ws';
|
|
1075
1142
|
this.stateStore.update(s => ({
|
|
1076
1143
|
...s,
|
|
1077
|
-
|
|
1078
|
-
|
|
1144
|
+
error:
|
|
1145
|
+
'Cannot connect to the wherever server: the connection is being rejected. ' +
|
|
1146
|
+
'This usually means a missing or wrong token, or a wrong host/port/scheme ' +
|
|
1147
|
+
`(currently ${scheme}://${this.config.host}:${this.config.port}). ` +
|
|
1148
|
+
'Check the auth token and connection settings.',
|
|
1079
1149
|
}));
|
|
1150
|
+
}
|
|
1080
1151
|
|
|
1081
|
-
|
|
1082
|
-
|
|
1152
|
+
// If we still hold an active session, PRESERVE the cached store across the
|
|
1153
|
+
// reconnect. A default connect() resets to a blank store, which wiped
|
|
1154
|
+
// activeSessionFile BEFORE onOpen could re-attach to it -- that is what
|
|
1155
|
+
// silently detached the frontend from a still-running session (frozen
|
|
1156
|
+
// conversation, no re-follow, not even a "connecting"/"loading" hint).
|
|
1157
|
+
// Preserving the store keeps the conversation on screen and lets onOpen
|
|
1158
|
+
// re-issue session_load from activeSessionFile.
|
|
1159
|
+
const preserve = !!get(this.stateStore).activeSessionFile;
|
|
1160
|
+
|
|
1161
|
+
this.stateStore.update(s => ({
|
|
1162
|
+
...s,
|
|
1163
|
+
connecting: true,
|
|
1164
|
+
error: 'Reconnecting...',
|
|
1165
|
+
}));
|
|
1166
|
+
|
|
1167
|
+
this.connect(undefined, preserve);
|
|
1083
1168
|
}
|
|
1084
1169
|
|
|
1085
1170
|
private handleMessage(msg: any) {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of frame chunking (`frames=chunk.v1`); the server half and the
|
|
3
|
+
* rationale are in `server/src/frame-chunks.ts` (kept in sync by hand).
|
|
4
|
+
*
|
|
5
|
+
* The client asks for chunks with `frames=chunk.v1` on the socket URL. A large
|
|
6
|
+
* server frame then arrives as several text frames, each
|
|
7
|
+
* `\u0001<id> <seq> <count>\n<data>`; concatenating the data in order gives back
|
|
8
|
+
* the original JSON. Every chunk is an inbound frame, which is what keeps the
|
|
9
|
+
* liveness watchdog from condemning a socket that is busy delivering a
|
|
10
|
+
* multi-MB history over a slow link.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const FRAME_CHUNK_PARAM = 'frames';
|
|
14
|
+
export const FRAME_CHUNK_VERSION = 'chunk.v1';
|
|
15
|
+
const MARK = 1; // U+0001: never the first character of a JSON document
|
|
16
|
+
|
|
17
|
+
export type ChunkResult =
|
|
18
|
+
| {kind: 'frame'; text: string}
|
|
19
|
+
| {kind: 'partial'; received: number; count: number}
|
|
20
|
+
| {kind: 'invalid'};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reassembles chunked frames for ONE socket (make a new one per socket: a
|
|
24
|
+
* message whose chunks were cut short by a drop is never completed on another).
|
|
25
|
+
* Chunks of one message are sent back to back, so only one message is ever in
|
|
26
|
+
* progress; a new id simply replaces an unfinished one.
|
|
27
|
+
*/
|
|
28
|
+
export class FrameReassembler {
|
|
29
|
+
private id: string | null = null;
|
|
30
|
+
private count = 0;
|
|
31
|
+
private parts: string[] = [];
|
|
32
|
+
|
|
33
|
+
/** True when `text` is a chunk rather than an ordinary frame. */
|
|
34
|
+
static isChunk(text: string): boolean {
|
|
35
|
+
return text.charCodeAt(0) === MARK;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Feed one text frame. A non-chunk frame is passed straight through. */
|
|
39
|
+
push(text: string): ChunkResult {
|
|
40
|
+
if (!FrameReassembler.isChunk(text)) return {kind: 'frame', text};
|
|
41
|
+
const nl = text.indexOf('\n');
|
|
42
|
+
if (nl < 0) return {kind: 'invalid'};
|
|
43
|
+
const [id, seqRaw, countRaw] = text.slice(1, nl).split(' ');
|
|
44
|
+
const seq = Number(seqRaw);
|
|
45
|
+
const count = Number(countRaw);
|
|
46
|
+
if (!id || !Number.isInteger(seq) || !Number.isInteger(count) || count < 1 || seq < 0 || seq >= count) {
|
|
47
|
+
return {kind: 'invalid'};
|
|
48
|
+
}
|
|
49
|
+
if (seq === 0 || id !== this.id) {
|
|
50
|
+
if (seq !== 0) {
|
|
51
|
+
// The start of this message was never seen: drop it.
|
|
52
|
+
this.reset();
|
|
53
|
+
return {kind: 'invalid'};
|
|
54
|
+
}
|
|
55
|
+
this.id = id;
|
|
56
|
+
this.count = count;
|
|
57
|
+
this.parts = [];
|
|
58
|
+
}
|
|
59
|
+
if (seq !== this.parts.length || count !== this.count) {
|
|
60
|
+
this.reset();
|
|
61
|
+
return {kind: 'invalid'};
|
|
62
|
+
}
|
|
63
|
+
this.parts.push(text.slice(nl + 1));
|
|
64
|
+
if (this.parts.length < this.count) {
|
|
65
|
+
return {kind: 'partial', received: this.parts.length, count: this.count};
|
|
66
|
+
}
|
|
67
|
+
const full = this.parts.join('');
|
|
68
|
+
this.reset();
|
|
69
|
+
return {kind: 'frame', text: full};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
reset() {
|
|
73
|
+
this.id = null;
|
|
74
|
+
this.count = 0;
|
|
75
|
+
this.parts = [];
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tool image. Live (`tool_end`) images always carry base64 `data`. In
|
|
3
|
+
* `message_history` a large image may come BY REFERENCE (this client asks for
|
|
4
|
+
* that with `imageRefs=1`): no `data`; instead its decoded size (`bytes`) and
|
|
5
|
+
* its address (`toolCallId`, `entryId`, `index`), fetched from
|
|
6
|
+
* `GET /session/tool-image?sessionId&toolCallId&entryId&index`.
|
|
7
|
+
*/
|
|
1
8
|
export interface ToolImage {
|
|
2
9
|
mimeType: string;
|
|
3
|
-
data
|
|
10
|
+
data?: string;
|
|
11
|
+
bytes?: number;
|
|
12
|
+
toolCallId?: string;
|
|
13
|
+
/** The transcript entry holding the image (sent alongside `toolCallId`). */
|
|
14
|
+
entryId?: string;
|
|
15
|
+
index?: number;
|
|
4
16
|
}
|
|
5
17
|
|
|
6
18
|
export interface ChatMessage {
|