@maximem/synap-js-sdk 0.5.21 → 0.6.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.
- package/dist/{chunk-CBK3H2LU.cjs → chunk-2VJV4HCF.cjs} +216 -23
- package/dist/chunk-2VJV4HCF.cjs.map +1 -0
- package/dist/{chunk-UXNVTP4D.cjs → chunk-7IZHVYO2.cjs} +50 -9
- package/dist/chunk-7IZHVYO2.cjs.map +1 -0
- package/dist/{chunk-4GE7SORD.js → chunk-DEFC7NHQ.js} +42 -1
- package/dist/chunk-DEFC7NHQ.js.map +1 -0
- package/dist/{chunk-5G3FNDB3.js → chunk-N55UBGER.js} +197 -4
- package/dist/chunk-N55UBGER.js.map +1 -0
- package/dist/{errors-BvUA5EW8.d.ts → errors-DWjdpExq.d.cts} +60 -2
- package/dist/{errors-BvUA5EW8.d.cts → errors-DWjdpExq.d.ts} +60 -2
- package/dist/grpc/index.cjs +7 -7
- package/dist/grpc/index.d.cts +22 -1
- package/dist/grpc/index.d.ts +22 -1
- package/dist/grpc/index.js +3 -3
- package/dist/index.cjs +251 -147
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -5
- package/dist/index.d.ts +20 -5
- package/dist/index.js +108 -4
- package/dist/index.js.map +1 -1
- package/dist/stream-client-GRDVRBFA.js +22 -0
- package/dist/stream-client-SZIYGVVJ.cjs +22 -0
- package/dist/stream-client-SZIYGVVJ.cjs.map +1 -0
- package/package.json +7 -3
- package/dist/chunk-4GE7SORD.js.map +0 -1
- package/dist/chunk-5G3FNDB3.js.map +0 -1
- package/dist/chunk-CBK3H2LU.cjs.map +0 -1
- package/dist/chunk-UXNVTP4D.cjs.map +0 -1
- package/dist/stream-client-2UMDGCMQ.js +0 -16
- package/dist/stream-client-FKKCBVCB.cjs +0 -16
- package/dist/stream-client-FKKCBVCB.cjs.map +0 -1
- /package/dist/{stream-client-2UMDGCMQ.js.map → stream-client-GRDVRBFA.js.map} +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunk7IZHVYO2cjs = require('./chunk-7IZHVYO2.cjs');
|
|
6
6
|
|
|
7
7
|
// src/grpc/descriptor.ts
|
|
8
8
|
var PROTO_SHA = "9857182b0cf458b4";
|
|
@@ -909,6 +909,13 @@ var MAX_MISSED_HEARTBEATS = 3;
|
|
|
909
909
|
var SEND_QUEUE_MAX_DEPTH = 100;
|
|
910
910
|
var CLOSE_GRACE_MS = 2e3;
|
|
911
911
|
var SEND_QUEUE_MAX_AGE_MS = 5 * 60 * 1e3;
|
|
912
|
+
var NOT_READY_MIN_WAIT_MS = 5e3;
|
|
913
|
+
var NOT_READY_MAX_WAIT_MS = 12e4;
|
|
914
|
+
var NOT_READY_JITTER = 0.2;
|
|
915
|
+
var NOT_READY_PROBE_GRACE_MS = 3e3;
|
|
916
|
+
var NOT_READY_MAX_HELD = 5e3;
|
|
917
|
+
var BUILD_FAILED_PROBE_INTERVAL_MS = 6e4;
|
|
918
|
+
var BUILD_FAILED_PROBE_JITTER_MS = 1e4;
|
|
912
919
|
var MIN_TTL_SECONDS = 60;
|
|
913
920
|
var GRPC_RESOURCE_EXHAUSTED = 8;
|
|
914
921
|
var CREDIT_ABORT_DETAILS = "insufficient_credits";
|
|
@@ -942,7 +949,7 @@ function creditErrorFromRpc(error, correlationId = null) {
|
|
|
942
949
|
const requestId = _nullishCoalesce(metadataValue(metadata, "request-id"), () => ( correlationId));
|
|
943
950
|
const manageUrl = metadataValue(metadata, "credit-manage-url");
|
|
944
951
|
if (reason === "overages_disabled") {
|
|
945
|
-
return new (0,
|
|
952
|
+
return new (0, _chunk7IZHVYO2cjs.InsufficientCreditsError)(details, {
|
|
946
953
|
correlationId: requestId,
|
|
947
954
|
balanceCredits: numberOrNull(metadataValue(metadata, "credit-balance")),
|
|
948
955
|
requiredCredits: numberOrNull(metadataValue(metadata, "credit-minimum-required")),
|
|
@@ -952,7 +959,7 @@ function creditErrorFromRpc(error, correlationId = null) {
|
|
|
952
959
|
manageUrl
|
|
953
960
|
});
|
|
954
961
|
}
|
|
955
|
-
return new (0,
|
|
962
|
+
return new (0, _chunk7IZHVYO2cjs.RateLimitError)(details, {
|
|
956
963
|
correlationId: requestId,
|
|
957
964
|
reason,
|
|
958
965
|
upgradeUrl: metadataValue(metadata, "credit-upgrade-url"),
|
|
@@ -960,7 +967,7 @@ function creditErrorFromRpc(error, correlationId = null) {
|
|
|
960
967
|
});
|
|
961
968
|
}
|
|
962
969
|
function isCreditStop(error) {
|
|
963
|
-
if (!(error instanceof
|
|
970
|
+
if (!(error instanceof _chunk7IZHVYO2cjs.InsufficientCreditsError) && !(error instanceof _chunk7IZHVYO2cjs.RateLimitError)) {
|
|
964
971
|
return false;
|
|
965
972
|
}
|
|
966
973
|
const reason = error.reason;
|
|
@@ -984,6 +991,22 @@ var GrpcStreamClient = class {
|
|
|
984
991
|
// actually loses sessions — the write succeeded, the stream broke before the
|
|
985
992
|
// server processed it, and nobody knows.
|
|
986
993
|
#unacked = /* @__PURE__ */ new Map();
|
|
994
|
+
// Set while the server refuses events because the instance is still being
|
|
995
|
+
// built. Null otherwise. See `#handleNotReady`.
|
|
996
|
+
#notReady = null;
|
|
997
|
+
#notReadyWarned = false;
|
|
998
|
+
// `instanceId|series` pairs already warned about for a failed build.
|
|
999
|
+
#buildFailedWarned = /* @__PURE__ */ new Set();
|
|
1000
|
+
#notReadyResignalled = false;
|
|
1001
|
+
#notReadyRunning = false;
|
|
1002
|
+
// Events held (tracked, not written) since the last replay.
|
|
1003
|
+
#notReadyUnwritten = [];
|
|
1004
|
+
// Held events dropped at NOT_READY_MAX_HELD, reported when the hold ends.
|
|
1005
|
+
#notReadyDropped = 0;
|
|
1006
|
+
// True from the first refusal until the backlog is acknowledged, so the
|
|
1007
|
+
// normal 100-event cap does not evict held events in the gap after resume.
|
|
1008
|
+
#heldBacklog = false;
|
|
1009
|
+
#notReadyWake = null;
|
|
987
1010
|
// Why the stream stopped, when it stopped for a reason the caller can act on
|
|
988
1011
|
// (today: a credit refusal). Mirrors Python's `last_error`.
|
|
989
1012
|
#lastError = null;
|
|
@@ -1027,6 +1050,17 @@ var GrpcStreamClient = class {
|
|
|
1027
1050
|
get lastError() {
|
|
1028
1051
|
return this.#lastError;
|
|
1029
1052
|
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Why the server is refusing events right now, or null.
|
|
1055
|
+
*
|
|
1056
|
+
* Set from the moment the server answers an event with an
|
|
1057
|
+
* `instance_not_ready` throttle until a replay of the held events is
|
|
1058
|
+
* accepted. The events are held, not dropped. Mirrors Python's
|
|
1059
|
+
* `GRPCTransport.instance_not_ready`.
|
|
1060
|
+
*/
|
|
1061
|
+
get instanceNotReady() {
|
|
1062
|
+
return this.#notReady === null ? null : { ...this.#notReady };
|
|
1063
|
+
}
|
|
1030
1064
|
async connect() {
|
|
1031
1065
|
await this.#loadGrpc();
|
|
1032
1066
|
await OutboxJournal.init();
|
|
@@ -1072,6 +1106,7 @@ var GrpcStreamClient = class {
|
|
|
1072
1106
|
async disconnect() {
|
|
1073
1107
|
this.#shutdownRequested = true;
|
|
1074
1108
|
this.#stopHeartbeat();
|
|
1109
|
+
_optionalChain([this, 'access', _12 => _12.#notReadyWake, 'optionalCall', _13 => _13()]);
|
|
1075
1110
|
this.#flushBeforeClose();
|
|
1076
1111
|
const call = this.#call;
|
|
1077
1112
|
if (call !== null) {
|
|
@@ -1134,13 +1169,18 @@ var GrpcStreamClient = class {
|
|
|
1134
1169
|
sendConversationEvent(event) {
|
|
1135
1170
|
const stamped = {
|
|
1136
1171
|
...event,
|
|
1137
|
-
event_id: event.event_id && event.event_id !== "" ? event.event_id :
|
|
1172
|
+
event_id: event.event_id && event.event_id !== "" ? event.event_id : _chunk7IZHVYO2cjs.newCorrelationId.call(void 0, ),
|
|
1138
1173
|
sent_at_ms: _nullishCoalesce(event.sent_at_ms, () => ( Date.now()))
|
|
1139
1174
|
};
|
|
1140
1175
|
if (!this.isConnected || this.#call === null) {
|
|
1141
1176
|
this.#enqueue(stamped);
|
|
1142
1177
|
return;
|
|
1143
1178
|
}
|
|
1179
|
+
if (this.#notReady !== null) {
|
|
1180
|
+
this.#trackUnacked(stamped);
|
|
1181
|
+
this.#notReadyUnwritten.push(stamped);
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1144
1184
|
this.#write({ conversation_event: { ...stamped } });
|
|
1145
1185
|
this.#trackUnacked(stamped);
|
|
1146
1186
|
}
|
|
@@ -1148,9 +1188,9 @@ var GrpcStreamClient = class {
|
|
|
1148
1188
|
this.#pruneQueue();
|
|
1149
1189
|
if (this.#sendQueue.length >= SEND_QUEUE_MAX_DEPTH) {
|
|
1150
1190
|
const evicted = this.#sendQueue.shift();
|
|
1151
|
-
this.#journal.recordDropped(_optionalChain([evicted, 'optionalAccess',
|
|
1191
|
+
this.#journal.recordDropped(_optionalChain([evicted, 'optionalAccess', _14 => _14.event, 'access', _15 => _15.event_id]));
|
|
1152
1192
|
console.warn(
|
|
1153
|
-
`[synap] send queue full (max=${SEND_QUEUE_MAX_DEPTH}); dropping oldest event (event_type=${_optionalChain([evicted, 'optionalAccess',
|
|
1193
|
+
`[synap] send queue full (max=${SEND_QUEUE_MAX_DEPTH}); dropping oldest event (event_type=${_optionalChain([evicted, 'optionalAccess', _16 => _16.event, 'access', _17 => _17.event_type])})`
|
|
1154
1194
|
);
|
|
1155
1195
|
}
|
|
1156
1196
|
this.#sendQueue.push({ queuedAt: Date.now(), event });
|
|
@@ -1158,11 +1198,11 @@ var GrpcStreamClient = class {
|
|
|
1158
1198
|
}
|
|
1159
1199
|
#pruneQueue() {
|
|
1160
1200
|
const cutoff = Date.now() - SEND_QUEUE_MAX_AGE_MS;
|
|
1161
|
-
while (this.#sendQueue.length > 0 && (_nullishCoalesce(_optionalChain([this, 'access',
|
|
1201
|
+
while (this.#sendQueue.length > 0 && (_nullishCoalesce(_optionalChain([this, 'access', _18 => _18.#sendQueue, 'access', _19 => _19[0], 'optionalAccess', _20 => _20.queuedAt]), () => ( 0))) < cutoff) {
|
|
1162
1202
|
const expired = this.#sendQueue.shift();
|
|
1163
|
-
this.#journal.recordDropped(_optionalChain([expired, 'optionalAccess',
|
|
1203
|
+
this.#journal.recordDropped(_optionalChain([expired, 'optionalAccess', _21 => _21.event, 'access', _22 => _22.event_id]));
|
|
1164
1204
|
console.warn(
|
|
1165
|
-
`[synap] queued event expired (age>${SEND_QUEUE_MAX_AGE_MS}ms); dropping (event_type=${_optionalChain([expired, 'optionalAccess',
|
|
1205
|
+
`[synap] queued event expired (age>${SEND_QUEUE_MAX_AGE_MS}ms); dropping (event_type=${_optionalChain([expired, 'optionalAccess', _23 => _23.event, 'access', _24 => _24.event_type])})`
|
|
1166
1206
|
);
|
|
1167
1207
|
}
|
|
1168
1208
|
}
|
|
@@ -1171,7 +1211,16 @@ var GrpcStreamClient = class {
|
|
|
1171
1211
|
if (id === void 0 || id === "") return;
|
|
1172
1212
|
const cutoff = Date.now() - SEND_QUEUE_MAX_AGE_MS;
|
|
1173
1213
|
for (const [oldestId, held] of this.#unacked) {
|
|
1174
|
-
|
|
1214
|
+
const holding = this.#notReady !== null || this.#heldBacklog;
|
|
1215
|
+
const limit = holding ? NOT_READY_MAX_HELD : SEND_QUEUE_MAX_DEPTH;
|
|
1216
|
+
const youngEnough = held.queuedAt >= cutoff || holding;
|
|
1217
|
+
if (youngEnough && this.#unacked.size < limit) break;
|
|
1218
|
+
if (holding) {
|
|
1219
|
+
this.#unacked.delete(oldestId);
|
|
1220
|
+
this.#journal.recordDropped(oldestId);
|
|
1221
|
+
this.#notReadyDropped += 1;
|
|
1222
|
+
continue;
|
|
1223
|
+
}
|
|
1175
1224
|
this.#unacked.delete(oldestId);
|
|
1176
1225
|
this.#journal.recordDropped(oldestId);
|
|
1177
1226
|
console.warn(
|
|
@@ -1193,9 +1242,13 @@ var GrpcStreamClient = class {
|
|
|
1193
1242
|
const unacked = [...this.#unacked.values()];
|
|
1194
1243
|
const queued = this.#sendQueue;
|
|
1195
1244
|
this.#sendQueue = [];
|
|
1196
|
-
for (const held of
|
|
1245
|
+
for (const held of unacked) {
|
|
1197
1246
|
this.#write({ conversation_event: { ...held.event } });
|
|
1198
1247
|
}
|
|
1248
|
+
for (const held of queued) {
|
|
1249
|
+
this.#write({ conversation_event: { ...held.event } });
|
|
1250
|
+
this.#trackUnacked(held.event);
|
|
1251
|
+
}
|
|
1199
1252
|
if (unacked.length > 0 || queued.length > 0) {
|
|
1200
1253
|
console.warn(
|
|
1201
1254
|
`[synap] replayed ${unacked.length} unacknowledged and ${queued.length} queued event(s) after reconnect`
|
|
@@ -1306,6 +1359,13 @@ var GrpcStreamClient = class {
|
|
|
1306
1359
|
this.#unacked.delete(id);
|
|
1307
1360
|
this.#journal.recordAcked(id);
|
|
1308
1361
|
}
|
|
1362
|
+
if (this.#notReady === null && this.#unacked.size < SEND_QUEUE_MAX_DEPTH) {
|
|
1363
|
+
this.#heldBacklog = false;
|
|
1364
|
+
}
|
|
1365
|
+
if (this.#notReady === null && (_nullishCoalesce(ack.event_ids, () => ( []))).length > 0 && (this.#notReadyWarned || this.#buildFailedWarned.size > 0)) {
|
|
1366
|
+
this.#notReadyWarned = false;
|
|
1367
|
+
this.#buildFailedWarned.clear();
|
|
1368
|
+
}
|
|
1309
1369
|
if (this.#journal.needsCompaction) this.#compactJournal();
|
|
1310
1370
|
return;
|
|
1311
1371
|
}
|
|
@@ -1322,30 +1382,160 @@ var GrpcStreamClient = class {
|
|
|
1322
1382
|
if (state) {
|
|
1323
1383
|
const count = Number(_nullishCoalesce(state.message_count, () => ( 0)));
|
|
1324
1384
|
if (state.conversation_id && Number.isFinite(count)) {
|
|
1325
|
-
_optionalChain([this, 'access',
|
|
1385
|
+
_optionalChain([this, 'access', _25 => _25.#options, 'access', _26 => _26.onConversationState, 'optionalCall', _27 => _27(state.conversation_id, count)]);
|
|
1326
1386
|
}
|
|
1327
1387
|
return;
|
|
1328
1388
|
}
|
|
1329
1389
|
const signal = msg["signal"];
|
|
1330
|
-
if (_optionalChain([signal, 'optionalAccess',
|
|
1390
|
+
if (_optionalChain([signal, 'optionalAccess', _28 => _28.signal_type]) === "throttle") {
|
|
1391
|
+
const metadata = _nullishCoalesce(signal.metadata, () => ( {}));
|
|
1392
|
+
if (metadata["code"] === "instance_not_ready" || metadata["code"] === "instance_build_failed") {
|
|
1393
|
+
this.#handleNotReady(metadata, _nullishCoalesce(signal.reason, () => ( "")));
|
|
1394
|
+
} else {
|
|
1395
|
+
console.warn(`[synap] server throttle signal: ${_nullishCoalesce(signal.reason, () => ( ""))}`);
|
|
1396
|
+
}
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1399
|
+
if (_optionalChain([signal, 'optionalAccess', _29 => _29.signal_type]) === "closing") {
|
|
1331
1400
|
void this.#handleDisconnect("server_signal_closing");
|
|
1332
|
-
} else if (_optionalChain([signal, 'optionalAccess',
|
|
1401
|
+
} else if (_optionalChain([signal, 'optionalAccess', _30 => _30.signal_type]) === "error") {
|
|
1333
1402
|
console.warn(`[synap] server error signal (stream stays open): ${_nullishCoalesce(signal.reason, () => ( ""))}`);
|
|
1334
1403
|
}
|
|
1335
1404
|
}
|
|
1405
|
+
/**
|
|
1406
|
+
* The server refused work: the instance is still being built.
|
|
1407
|
+
*
|
|
1408
|
+
* The refused event is still in `#unacked` (the server neither recorded nor
|
|
1409
|
+
* acknowledged it), so nothing is dropped here. This does not close the
|
|
1410
|
+
* stream and does not reconnect: a reconnect would replay the same events
|
|
1411
|
+
* into the same refusal for the whole build. One probe loop waits
|
|
1412
|
+
* `retry_after_seconds` (clamped, jittered), replays the held events and,
|
|
1413
|
+
* once the server accepts them, resumes normally. Mirrors Python's
|
|
1414
|
+
* `_handle_not_ready`.
|
|
1415
|
+
*
|
|
1416
|
+
* Logged once per build, not once per event: replaying N held events draws N
|
|
1417
|
+
* refusals and a warning each would bury the log.
|
|
1418
|
+
*/
|
|
1419
|
+
#handleNotReady(metadata, reason) {
|
|
1420
|
+
const code = metadata["code"] || "instance_not_ready";
|
|
1421
|
+
const failed = code === "instance_build_failed";
|
|
1422
|
+
const retry = Number(metadata["retry_after_seconds"]);
|
|
1423
|
+
this.#notReady = {
|
|
1424
|
+
retryAfterSeconds: Number.isFinite(retry) ? Math.trunc(retry) : NOT_READY_MAX_WAIT_MS / 1e3,
|
|
1425
|
+
instanceId: metadata["instance_id"] || this.#credentials.instanceId,
|
|
1426
|
+
state: metadata["state"] || (failed ? "failed" : "building"),
|
|
1427
|
+
message: reason,
|
|
1428
|
+
code,
|
|
1429
|
+
...failed ? { hint: metadata["hint"] || "", attempts: metadata["attempts"] || "" } : {}
|
|
1430
|
+
};
|
|
1431
|
+
this.#notReadyResignalled = true;
|
|
1432
|
+
this.#heldBacklog = true;
|
|
1433
|
+
if (failed) {
|
|
1434
|
+
const key = `${this.#notReady.instanceId}|${metadata["series"] || ""}`;
|
|
1435
|
+
if (!this.#buildFailedWarned.has(key)) {
|
|
1436
|
+
this.#buildFailedWarned.add(key);
|
|
1437
|
+
const hint = this.#notReady.hint;
|
|
1438
|
+
console.warn(
|
|
1439
|
+
`[synap] instance ${this.#notReady.instanceId}: its memory architecture build FAILED, so the server is refusing events. ${this.#unacked.size} event(s) are held in memory (up to ${NOT_READY_MAX_HELD}; beyond that the oldest are dropped and the count is reported). Waiting alone will not fix this. ${hint ? `To recover: ${hint}` : "Rebuild the instance."} The SDK checks again about every ${Math.round(BUILD_FAILED_PROBE_INTERVAL_MS / 1e3)}s and resumes by itself after a successful rebuild. Server said: ${reason}`
|
|
1440
|
+
);
|
|
1441
|
+
}
|
|
1442
|
+
} else if (!this.#notReadyWarned) {
|
|
1443
|
+
this.#notReadyWarned = true;
|
|
1444
|
+
console.warn(
|
|
1445
|
+
`[synap] instance ${this.#notReady.instanceId} is still building its memory architecture; the server is refusing events for now. ${this.#unacked.size} event(s) are held in memory and will be re-sent automatically when it is ready (next check in about ${Math.round(this.#notReadyWait() / 1e3)}s). Up to ${NOT_READY_MAX_HELD} events are held; beyond that the oldest are dropped and the count is reported. Server said: ${reason}`
|
|
1446
|
+
);
|
|
1447
|
+
}
|
|
1448
|
+
if (!this.#notReadyRunning) {
|
|
1449
|
+
this.#notReadyRunning = true;
|
|
1450
|
+
void this.#notReadyProbeLoop();
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
/** Milliseconds until the next probe: the hint, clamped, with jitter. */
|
|
1454
|
+
#notReadyWait() {
|
|
1455
|
+
if (_optionalChain([this, 'access', _31 => _31.#notReady, 'optionalAccess', _32 => _32.code]) === "instance_build_failed") {
|
|
1456
|
+
return BUILD_FAILED_PROBE_INTERVAL_MS + Math.random() * BUILD_FAILED_PROBE_JITTER_MS;
|
|
1457
|
+
}
|
|
1458
|
+
const hintMs = (_nullishCoalesce(_optionalChain([this, 'access', _33 => _33.#notReady, 'optionalAccess', _34 => _34.retryAfterSeconds]), () => ( 0))) * 1e3;
|
|
1459
|
+
const base = Math.min(Math.max(hintMs, NOT_READY_MIN_WAIT_MS), NOT_READY_MAX_WAIT_MS);
|
|
1460
|
+
const jitter = 1 - NOT_READY_JITTER + Math.random() * 2 * NOT_READY_JITTER;
|
|
1461
|
+
return Math.max(base * jitter, NOT_READY_MIN_WAIT_MS);
|
|
1462
|
+
}
|
|
1463
|
+
/** Cancellable on disconnect, and never the only thing holding the process open. */
|
|
1464
|
+
#notReadySleep(ms) {
|
|
1465
|
+
if (this.#options.sleep) return this.#options.sleep(ms);
|
|
1466
|
+
return new Promise((resolve) => {
|
|
1467
|
+
const timer = setTimeout(() => {
|
|
1468
|
+
this.#notReadyWake = null;
|
|
1469
|
+
resolve();
|
|
1470
|
+
}, ms);
|
|
1471
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
1472
|
+
this.#notReadyWake = () => {
|
|
1473
|
+
clearTimeout(timer);
|
|
1474
|
+
this.#notReadyWake = null;
|
|
1475
|
+
resolve();
|
|
1476
|
+
};
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
async #notReadyProbeLoop() {
|
|
1480
|
+
try {
|
|
1481
|
+
while (!this.#shutdownRequested) {
|
|
1482
|
+
await this.#notReadySleep(this.#notReadyWait());
|
|
1483
|
+
if (this.#shutdownRequested) return;
|
|
1484
|
+
if (!this.isConnected || this.#call === null) return;
|
|
1485
|
+
this.#notReadyResignalled = false;
|
|
1486
|
+
const now = Date.now();
|
|
1487
|
+
for (const held of this.#unacked.values()) held.queuedAt = now;
|
|
1488
|
+
const covered = new Set(this.#unacked.keys());
|
|
1489
|
+
for (const held of [...this.#unacked.values()]) {
|
|
1490
|
+
this.#write({ conversation_event: { ...held.event } });
|
|
1491
|
+
}
|
|
1492
|
+
this.#notReadyUnwritten = this.#notReadyUnwritten.filter(
|
|
1493
|
+
(e) => !covered.has(String(e.event_id))
|
|
1494
|
+
);
|
|
1495
|
+
await this.#notReadySleep(NOT_READY_PROBE_GRACE_MS);
|
|
1496
|
+
if (this.#shutdownRequested) return;
|
|
1497
|
+
if (!this.#notReadyResignalled) {
|
|
1498
|
+
const instanceId = _nullishCoalesce(_optionalChain([this, 'access', _35 => _35.#notReady, 'optionalAccess', _36 => _36.instanceId]), () => ( this.#credentials.instanceId));
|
|
1499
|
+
this.#notReady = null;
|
|
1500
|
+
const late = this.#notReadyUnwritten;
|
|
1501
|
+
this.#notReadyUnwritten = [];
|
|
1502
|
+
for (const event of late) {
|
|
1503
|
+
if (!this.#unacked.has(String(event.event_id))) continue;
|
|
1504
|
+
this.#write({ conversation_event: { ...event } });
|
|
1505
|
+
}
|
|
1506
|
+
this.#notReadyWarned = false;
|
|
1507
|
+
this.#buildFailedWarned.clear();
|
|
1508
|
+
console.info(
|
|
1509
|
+
`[synap] instance ${instanceId} is ready; held events were accepted and the stream is back to normal`
|
|
1510
|
+
);
|
|
1511
|
+
return;
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
} finally {
|
|
1515
|
+
this.#notReady = null;
|
|
1516
|
+
this.#notReadyUnwritten = [];
|
|
1517
|
+
this.#notReadyRunning = false;
|
|
1518
|
+
if (this.#notReadyDropped > 0) {
|
|
1519
|
+
console.warn(
|
|
1520
|
+
`[synap] while the instance was building, ${this.#notReadyDropped} held event(s) were DROPPED (oldest first) because the in-memory limit of ${NOT_READY_MAX_HELD} was reached. They were not sent.`
|
|
1521
|
+
);
|
|
1522
|
+
this.#notReadyDropped = 0;
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1336
1526
|
#handleBundle(bundle) {
|
|
1337
1527
|
const bundleType = String(_nullishCoalesce(bundle["bundle_type"], () => ( ""))) || "anticipation";
|
|
1338
1528
|
const conversationId = String(_nullishCoalesce(bundle["anticipation_conversation_id"], () => ( "")));
|
|
1339
1529
|
if (bundleType !== UNCACHEABLE_BUNDLE_TYPE) this.#store(bundle, bundleType, conversationId);
|
|
1340
1530
|
if (bundleType === "compaction_update" && conversationId !== "") {
|
|
1341
|
-
_optionalChain([this, 'access',
|
|
1531
|
+
_optionalChain([this, 'access', _37 => _37.#options, 'access', _38 => _38.onCompactionUpdate, 'optionalCall', _39 => _39(conversationId, bundle)]);
|
|
1342
1532
|
}
|
|
1343
|
-
_optionalChain([this, 'access',
|
|
1533
|
+
_optionalChain([this, 'access', _40 => _40.#options, 'access', _41 => _41.onContext, 'optionalCall', _42 => _42(bundle)]);
|
|
1344
1534
|
}
|
|
1345
1535
|
#store(bundle, bundleType, conversationId) {
|
|
1346
1536
|
const itemsByType = {};
|
|
1347
1537
|
for (const [type, list] of Object.entries(_nullishCoalesce(bundle["items_by_type"], () => ( {})))) {
|
|
1348
|
-
const items = _optionalChain([list, 'optionalAccess',
|
|
1538
|
+
const items = _optionalChain([list, 'optionalAccess', _43 => _43.items]);
|
|
1349
1539
|
itemsByType[type] = _nullishCoalesce(items, () => ( []));
|
|
1350
1540
|
}
|
|
1351
1541
|
const ttlHint = Number(_nullishCoalesce(bundle["ttl_hint_seconds"], () => ( 0)));
|
|
@@ -1426,7 +1616,7 @@ var GrpcStreamClient = class {
|
|
|
1426
1616
|
void this.#handleDisconnect("heartbeat_write_error");
|
|
1427
1617
|
}
|
|
1428
1618
|
}, HEARTBEAT_INTERVAL_MS);
|
|
1429
|
-
_optionalChain([this, 'access',
|
|
1619
|
+
_optionalChain([this, 'access', _44 => _44.#heartbeatTimer, 'access', _45 => _45.unref, 'optionalCall', _46 => _46()]);
|
|
1430
1620
|
}
|
|
1431
1621
|
#stopHeartbeat() {
|
|
1432
1622
|
if (this.#heartbeatTimer !== null) {
|
|
@@ -1436,14 +1626,14 @@ var GrpcStreamClient = class {
|
|
|
1436
1626
|
}
|
|
1437
1627
|
#closeCall() {
|
|
1438
1628
|
try {
|
|
1439
|
-
_optionalChain([this, 'access',
|
|
1629
|
+
_optionalChain([this, 'access', _47 => _47.#call, 'optionalAccess', _48 => _48.end, 'call', _49 => _49()]);
|
|
1440
1630
|
} catch (e11) {
|
|
1441
1631
|
}
|
|
1442
1632
|
this.#call = null;
|
|
1443
1633
|
}
|
|
1444
1634
|
#setState(state) {
|
|
1445
1635
|
this.#state = state;
|
|
1446
|
-
_optionalChain([this, 'access',
|
|
1636
|
+
_optionalChain([this, 'access', _50 => _50.#options, 'access', _51 => _51.onStateChange, 'optionalCall', _52 => _52(state)]);
|
|
1447
1637
|
}
|
|
1448
1638
|
};
|
|
1449
1639
|
|
|
@@ -1454,5 +1644,8 @@ var GrpcStreamClient = class {
|
|
|
1454
1644
|
|
|
1455
1645
|
|
|
1456
1646
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1647
|
+
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
exports.PROTO_SHA = PROTO_SHA; exports.NOT_READY_MAX_HELD = NOT_READY_MAX_HELD; exports.BUILD_FAILED_PROBE_INTERVAL_MS = BUILD_FAILED_PROBE_INTERVAL_MS; exports.BUILD_FAILED_PROBE_JITTER_MS = BUILD_FAILED_PROBE_JITTER_MS; exports.CREDIT_ABORT_DETAILS = CREDIT_ABORT_DETAILS; exports.CREDIT_STOP_REASONS = CREDIT_STOP_REASONS; exports.creditErrorFromRpc = creditErrorFromRpc; exports.isCreditStop = isCreditStop; exports.GrpcStreamClient = GrpcStreamClient;
|
|
1651
|
+
//# sourceMappingURL=chunk-2VJV4HCF.cjs.map
|