@moxt-ai/mobius-sdk 0.0.64 → 0.0.66

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,16 +1,12 @@
1
- import { decodeAgentSettingsDiscoveredEvent, decodeMobiusSessionExportV2, decodeNativeSessionEventsV2, decodeNativeSessionResult, decodeSessionConfigUpdatedEvent, decodeSessionEventLogPageV2, decodeSessionEventV2, decodeSessionReadyEvent, decodeSessionsListedEvent, decodeSessionsQueriedEvent, decodeWorkspaceEntriesEvent, isIdentifier, MOBIUS_API_PREFIX, SessionPromptDisposition, } from "@moxt-ai/mobius-protocol";
1
+ import { decodeAgentSettingsDiscoveredEvent, decodeMobiusSessionExportV2, decodeNativeSessionEventsV2, decodeNativeSessionResult, decodeSessionConfigUpdatedEvent, decodeSessionEventBatchV2, decodeSessionEventLogPageV2, decodeSessionReadyEvent, decodeSessionsListedEvent, decodeSessionsQueriedEvent, decodeWorkspaceEntriesEvent, isIdentifier, MOBIUS_API_PREFIX, SessionPromptDisposition, } from "@moxt-ai/mobius-protocol";
2
2
  import { MobiusApiError, MobiusCancellationError, MobiusConnectionError, MobiusError, MobiusProtocolError, MobiusTimeoutError, MobiusValidationError, } from "./errors.js";
3
3
  import { MobiusPublicRequestExecutor } from "./public-request.js";
4
- import { decodeMobiusAttempt, decodeMobiusBrowserCredential, decodeMobiusChannelAccessGrant, decodeMobiusChannelDescriptor, decodeMobiusEventPage, decodeMobiusInteraction, decodeMobiusInteractionList, decodeMobiusLinkedChannelDeviceList, decodeMobiusMessagePage, decodeMobiusRedeemedChannelCredential, decodeMobiusRuntimeList, decodeMobiusRuntimePairing, decodeMobiusRuntimePairingStatus, decodeMobiusServiceCapabilities, decodeMobiusSession, decodeMobiusSessionEvent, decodeMobiusSessionExport, decodeMobiusSessionPage, decodeMobiusSessionState, decodeMobiusToolCallDetail, decodeMobiusTurn, decodeRuntimeDescriptor, MobiusPairedRuntimePairingStatus, MobiusRuntimePairingLifecycle, MobiusSessionAccess, } from "./resources.js";
4
+ import { decodeMobiusAttempt, decodeMobiusBrowserCredential, decodeMobiusChannelAccessGrant, decodeMobiusChannelDescriptor, decodeMobiusInteraction, decodeMobiusInteractionList, decodeMobiusLinkedChannelDeviceList, decodeMobiusMessagePage, decodeMobiusRedeemedChannelCredential, decodeMobiusRuntimeList, decodeMobiusRuntimePairing, decodeMobiusRuntimePairingStatus, decodeMobiusServiceCapabilities, decodeMobiusSession, decodeMobiusSessionExport, decodeMobiusSessionPage, decodeMobiusSessionState, decodeMobiusToolCallDetail, decodeMobiusTurn, decodeRuntimeDescriptor, MobiusPairedRuntimePairingStatus, MobiusRuntimePairingLifecycle, MobiusSessionAccess, } from "./resources.js";
5
5
  import { HttpTransport, isMobiusLiveEventTransport, } from "./transport/http-transport.js";
6
- const MOBIUS_EVENT_FORMAT_VERSION = "1";
7
- const MOBIUS_SESSION_EVENT_STREAM_EVENT = "mobius.session-event.v1";
8
- const MOBIUS_SESSION_EVENT_STREAM_MEDIA_TYPE = "text/event-stream";
9
6
  const DEFAULT_OPERATION_TIMEOUT_MILLISECONDS = 20_000;
10
7
  const DEFAULT_STREAM_CONNECTION_TIMEOUT_MILLISECONDS = 15_000;
11
8
  const DEFAULT_STREAM_LIVENESS_TIMEOUT_MILLISECONDS = 45_000;
12
9
  const DEFAULT_STREAM_RECONNECT_ATTEMPTS = 4;
13
- const MAX_EVENT_BLOCK_LENGTH = 9 * 1024 * 1024;
14
10
  export function externalContentPath(content) {
15
11
  let kind;
16
12
  let key;
@@ -176,55 +172,6 @@ class RequestDeadline {
176
172
  this.#controller.abort(new Error("The Mobius operation timed out"));
177
173
  };
178
174
  }
179
- class StreamConnection {
180
- #closed = false;
181
- #controller;
182
- #hasActivity = false;
183
- #livenessTimedOut = false;
184
- #livenessTimeoutId;
185
- #livenessTimeoutMilliseconds;
186
- #release;
187
- response;
188
- constructor(response, controller, release, livenessTimeoutMilliseconds) {
189
- this.#controller = controller;
190
- this.#livenessTimeoutMilliseconds = livenessTimeoutMilliseconds;
191
- this.#release = release;
192
- this.response = response;
193
- this.#livenessTimeoutId = globalThis.setTimeout(this.abortFromLivenessTimeout, this.#livenessTimeoutMilliseconds);
194
- }
195
- get hasActivity() {
196
- return this.#hasActivity;
197
- }
198
- close = () => {
199
- if (this.#closed) {
200
- return;
201
- }
202
- this.#closed = true;
203
- globalThis.clearTimeout(this.#livenessTimeoutId);
204
- this.#controller.abort(new Error("The Mobius event connection closed"));
205
- this.#release();
206
- };
207
- touch = () => {
208
- if (this.#closed) {
209
- return;
210
- }
211
- this.#hasActivity = true;
212
- globalThis.clearTimeout(this.#livenessTimeoutId);
213
- this.#livenessTimeoutId = globalThis.setTimeout(this.abortFromLivenessTimeout, this.#livenessTimeoutMilliseconds);
214
- };
215
- translateFailure = (cause) => {
216
- if (this.#livenessTimedOut) {
217
- throw new MobiusTimeoutError({ cause });
218
- }
219
- throw new MobiusConnectionError("The Mobius event stream was interrupted", {
220
- cause,
221
- });
222
- };
223
- abortFromLivenessTimeout = () => {
224
- this.#livenessTimedOut = true;
225
- this.#controller.abort(new Error("The Mobius event stream became inactive"));
226
- };
227
- }
228
175
  class LiveEventConnection {
229
176
  events;
230
177
  #controller;
@@ -338,71 +285,6 @@ export class MobiusRequestExecutor {
338
285
  deleteEmpty = async (path, signal) => {
339
286
  await this.requestEmpty(path, "DELETE", signal);
340
287
  };
341
- openEventStream = async (path, signal) => {
342
- const controller = new AbortController();
343
- let timedOut = false;
344
- const abortFromRequest = () => controller.abort(signal.reason);
345
- if (signal.aborted) {
346
- abortFromRequest();
347
- }
348
- else {
349
- signal.addEventListener("abort", abortFromRequest, { once: true });
350
- }
351
- const timeoutId = globalThis.setTimeout(() => {
352
- timedOut = true;
353
- controller.abort(new Error("The Mobius event stream timed out"));
354
- }, this.#streamConnectionTimeoutMilliseconds);
355
- const release = () => {
356
- globalThis.clearTimeout(timeoutId);
357
- signal.removeEventListener("abort", abortFromRequest);
358
- };
359
- let response;
360
- try {
361
- const credential = validateCredential(await this.#credentialProvider.credential(controller.signal));
362
- response = await this.#transport.fetch(new Request(requestUrl(this.#endpoint, path), {
363
- headers: this.authenticationHeaders(credential, MOBIUS_SESSION_EVENT_STREAM_MEDIA_TYPE),
364
- signal: controller.signal,
365
- }));
366
- }
367
- catch (cause) {
368
- release();
369
- if (signal.aborted) {
370
- throw new MobiusCancellationError({ cause });
371
- }
372
- if (timedOut) {
373
- throw new MobiusTimeoutError({ cause });
374
- }
375
- if (cause instanceof MobiusError) {
376
- throw cause;
377
- }
378
- throw new MobiusConnectionError("The Mobius event stream could not connect", { cause });
379
- }
380
- if (!response.ok) {
381
- try {
382
- await throwApiError(response);
383
- }
384
- catch (cause) {
385
- release();
386
- if (signal.aborted) {
387
- throw new MobiusCancellationError({ cause });
388
- }
389
- if (timedOut) {
390
- throw new MobiusTimeoutError({ cause });
391
- }
392
- throw cause;
393
- }
394
- }
395
- globalThis.clearTimeout(timeoutId);
396
- const contentType = response.headers.get("content-type") ?? "";
397
- if (!contentType.startsWith(MOBIUS_SESSION_EVENT_STREAM_MEDIA_TYPE) ||
398
- response.headers.get("x-mobius-event-format") !== MOBIUS_EVENT_FORMAT_VERSION ||
399
- response.body === null) {
400
- controller.abort(new Error("The Mobius event stream is invalid"));
401
- release();
402
- throw new MobiusProtocolError("The service returned an invalid Session event stream");
403
- }
404
- return new StreamConnection(response, controller, release, this.#streamLivenessTimeoutMilliseconds);
405
- };
406
288
  openLiveEventStream = async (path, signal) => {
407
289
  if (!isMobiusLiveEventTransport(this.#transport)) {
408
290
  throw new MobiusValidationError("The configured transport does not support live Session events");
@@ -874,387 +756,6 @@ export class MobiusInteractionResources {
874
756
  }
875
757
  };
876
758
  }
877
- export class MobiusEventSubscription {
878
- #controller;
879
- #executor;
880
- #externalSignal;
881
- #maximumReconnectAttempts;
882
- #observer;
883
- #path;
884
- #closed = false;
885
- #cursor;
886
- #hasSequence = false;
887
- #lastSequence = 0n;
888
- completed;
889
- constructor(executor, path, cursor, observer, signal, controller, firstConnection, maximumReconnectAttempts) {
890
- this.#cursor = cursor;
891
- this.#controller = controller;
892
- this.#executor = executor;
893
- this.#externalSignal = signal;
894
- this.#maximumReconnectAttempts = maximumReconnectAttempts;
895
- this.#observer = observer;
896
- this.#path = path;
897
- if (this.#externalSignal.aborted) {
898
- this.abortFromExternal();
899
- }
900
- else {
901
- this.#externalSignal.addEventListener("abort", this.abortFromExternal, {
902
- once: true,
903
- });
904
- }
905
- this.completed = this.run(firstConnection).finally(() => {
906
- this.#externalSignal.removeEventListener("abort", this.abortFromExternal);
907
- });
908
- }
909
- get cursor() {
910
- return this.#cursor;
911
- }
912
- close = () => {
913
- if (this.#closed) {
914
- return;
915
- }
916
- this.#closed = true;
917
- this.#controller.abort(new Error("The Mobius event subscription closed"));
918
- };
919
- abortFromExternal = () => {
920
- this.#controller.abort(this.#externalSignal.reason);
921
- };
922
- run = async (firstConnection) => {
923
- let connection = firstConnection;
924
- let reconnectAttempts = 0;
925
- while (!this.#closed) {
926
- try {
927
- const stopped = await this.consume(connection);
928
- connection.close();
929
- if (stopped || this.#closed) {
930
- this.#closed = true;
931
- return;
932
- }
933
- throw new MobiusConnectionError("The Mobius event stream closed before the observer stopped it");
934
- }
935
- catch (cause) {
936
- connection.close();
937
- if (this.#closed) {
938
- return;
939
- }
940
- if (connection.hasActivity) {
941
- reconnectAttempts = 0;
942
- }
943
- try {
944
- const recovery = await this.recover(cause, reconnectAttempts);
945
- reconnectAttempts = recovery.attempts;
946
- connection = recovery.connection;
947
- }
948
- catch (recoveryCause) {
949
- if (this.#closed) {
950
- return;
951
- }
952
- throw recoveryCause;
953
- }
954
- }
955
- }
956
- };
957
- consume = async (connection) => {
958
- const body = connection.response.body;
959
- if (body === null) {
960
- throw new MobiusProtocolError("The Mobius event stream has no body");
961
- }
962
- const reader = body.getReader();
963
- const decoder = new TextDecoder();
964
- let buffered = "";
965
- try {
966
- while (!this.#closed) {
967
- const result = await reader.read();
968
- if (result.done) {
969
- buffered += decoder.decode();
970
- if (buffered.trim().length > 0) {
971
- throw new MobiusProtocolError("The Mobius event stream ended with an incomplete event");
972
- }
973
- return false;
974
- }
975
- connection.touch();
976
- buffered += decoder.decode(result.value, { stream: true });
977
- if (buffered.length > MAX_EVENT_BLOCK_LENGTH) {
978
- throw new MobiusProtocolError("The Mobius event stream exceeded its event limit");
979
- }
980
- let boundary = buffered.indexOf("\n\n");
981
- let boundaryLength = 2;
982
- const carriageReturnBoundary = buffered.indexOf("\r\n\r\n");
983
- if (carriageReturnBoundary >= 0 && (boundary < 0 || carriageReturnBoundary < boundary)) {
984
- boundary = carriageReturnBoundary;
985
- boundaryLength = 4;
986
- }
987
- while (boundary >= 0) {
988
- const block = buffered.slice(0, boundary);
989
- buffered = buffered.slice(boundary + boundaryLength);
990
- if (await this.consumeBlock(block.replaceAll("\r\n", "\n"))) {
991
- return true;
992
- }
993
- boundary = buffered.indexOf("\n\n");
994
- boundaryLength = 2;
995
- const nextCarriageReturnBoundary = buffered.indexOf("\r\n\r\n");
996
- if (nextCarriageReturnBoundary >= 0 && (boundary < 0 || nextCarriageReturnBoundary < boundary)) {
997
- boundary = nextCarriageReturnBoundary;
998
- boundaryLength = 4;
999
- }
1000
- }
1001
- }
1002
- return true;
1003
- }
1004
- catch (cause) {
1005
- if (this.#closed || this.#controller.signal.aborted) {
1006
- throw cause;
1007
- }
1008
- if (cause instanceof MobiusError) {
1009
- throw cause;
1010
- }
1011
- return connection.translateFailure(cause);
1012
- }
1013
- finally {
1014
- reader.releaseLock();
1015
- }
1016
- };
1017
- consumeBlock = async (block) => {
1018
- if (block.length === 0 || block.startsWith(":")) {
1019
- return false;
1020
- }
1021
- let eventName = "";
1022
- let eventId = "";
1023
- let data = "";
1024
- for (const line of block.split("\n")) {
1025
- if (line.startsWith("event:")) {
1026
- eventName = line.slice("event:".length).trimStart();
1027
- }
1028
- else if (line.startsWith("id:")) {
1029
- eventId = line.slice("id:".length).trimStart();
1030
- }
1031
- else if (line.startsWith("data:")) {
1032
- data += `${line.slice("data:".length).trimStart()}\n`;
1033
- }
1034
- else if (!line.startsWith(":")) {
1035
- throw new MobiusProtocolError("The Mobius event stream contains an invalid field");
1036
- }
1037
- }
1038
- if (eventName !== MOBIUS_SESSION_EVENT_STREAM_EVENT || data.length === 0) {
1039
- throw new MobiusProtocolError("The Mobius event stream contains an unsupported event");
1040
- }
1041
- let value;
1042
- try {
1043
- value = JSON.parse(data.slice(0, -1));
1044
- }
1045
- catch (cause) {
1046
- throw new MobiusProtocolError("The Mobius event stream contains invalid JSON", { cause });
1047
- }
1048
- const event = decodeMobiusSessionEvent(value);
1049
- if (event.cursor !== eventId) {
1050
- throw new MobiusProtocolError("The Mobius event identifier does not match its cursor");
1051
- }
1052
- const sequence = BigInt(event.sequence);
1053
- if (this.#hasSequence && sequence <= this.#lastSequence) {
1054
- return false;
1055
- }
1056
- if (this.#hasSequence && sequence !== this.#lastSequence + 1n) {
1057
- throw new MobiusProtocolError("The Mobius event stream contains a gap");
1058
- }
1059
- let shouldContinue;
1060
- try {
1061
- shouldContinue = await this.#observer.receive(event);
1062
- }
1063
- catch (cause) {
1064
- throw new MobiusError("event_observer_failed", "The Mobius event observer failed", { cause });
1065
- }
1066
- this.#cursor = event.cursor;
1067
- this.#hasSequence = true;
1068
- this.#lastSequence = sequence;
1069
- return !shouldContinue;
1070
- };
1071
- pathWithCursor = () => {
1072
- if (this.#cursor.length === 0) {
1073
- return this.#path;
1074
- }
1075
- return `${this.#path}${this.#path.includes("?") ? "&" : "?"}cursor=${encodeURIComponent(this.#cursor)}`;
1076
- };
1077
- recover = async (initialCause, completedAttempts) => {
1078
- this.throwIfTerminal(initialCause);
1079
- let attempts = completedAttempts;
1080
- let latestCause = initialCause;
1081
- while (attempts < this.#maximumReconnectAttempts) {
1082
- attempts += 1;
1083
- await this.reconnectDelay(attempts);
1084
- try {
1085
- return new MobiusEventStreamRecovery(await this.#executor.openEventStream(this.pathWithCursor(), this.#controller.signal), attempts);
1086
- }
1087
- catch (cause) {
1088
- latestCause = cause;
1089
- this.throwIfTerminal(cause);
1090
- }
1091
- }
1092
- throw new MobiusConnectionError("The Mobius event stream could not recover", { cause: latestCause });
1093
- };
1094
- throwIfTerminal = (cause) => {
1095
- if (this.#externalSignal.aborted) {
1096
- throw new MobiusCancellationError({ cause });
1097
- }
1098
- if (cause instanceof MobiusProtocolError ||
1099
- (cause instanceof MobiusApiError && cause.status < 500) ||
1100
- (cause instanceof MobiusError &&
1101
- !(cause instanceof MobiusConnectionError) &&
1102
- !(cause instanceof MobiusTimeoutError) &&
1103
- !(cause instanceof MobiusApiError))) {
1104
- throw cause;
1105
- }
1106
- };
1107
- reconnectDelay = async (attempt) => {
1108
- const delayMilliseconds = Math.min(250 * 2 ** (attempt - 1), 2_000);
1109
- await new Promise((resolve, reject) => {
1110
- const timeoutId = globalThis.setTimeout(() => {
1111
- this.#controller.signal.removeEventListener("abort", abort);
1112
- resolve();
1113
- }, delayMilliseconds);
1114
- const abort = () => {
1115
- globalThis.clearTimeout(timeoutId);
1116
- reject(new MobiusCancellationError());
1117
- };
1118
- this.#controller.signal.addEventListener("abort", abort, { once: true });
1119
- });
1120
- };
1121
- }
1122
- class MobiusEventStreamRecovery {
1123
- attempts;
1124
- connection;
1125
- constructor(connection, attempts) {
1126
- this.attempts = attempts;
1127
- this.connection = connection;
1128
- }
1129
- }
1130
- export class MobiusLiveEventSubscription {
1131
- #controller = new AbortController();
1132
- #executor;
1133
- #maximumReconnectAttempts;
1134
- #observer;
1135
- #path;
1136
- #closed = false;
1137
- connected;
1138
- completed;
1139
- constructor(executor, path, observer, signal, maximumReconnectAttempts) {
1140
- this.#executor = executor;
1141
- this.#maximumReconnectAttempts = maximumReconnectAttempts;
1142
- this.#observer = observer;
1143
- this.#path = path;
1144
- const abort = () => this.#controller.abort(signal.reason);
1145
- if (signal.aborted) {
1146
- abort();
1147
- }
1148
- else {
1149
- signal.addEventListener("abort", abort, { once: true });
1150
- }
1151
- let resolveConnected = () => undefined;
1152
- let rejectConnected = (_cause) => undefined;
1153
- this.connected = new Promise((resolve, reject) => {
1154
- resolveConnected = resolve;
1155
- rejectConnected = reject;
1156
- });
1157
- this.completed = this.run(resolveConnected, rejectConnected).finally(() => signal.removeEventListener("abort", abort));
1158
- }
1159
- close = () => {
1160
- if (this.#closed) {
1161
- return;
1162
- }
1163
- this.#closed = true;
1164
- this.#controller.abort(new Error("The live Session event subscription closed"));
1165
- };
1166
- run = async (resolveConnected, rejectConnected) => {
1167
- let attempts = 0;
1168
- let connectionState = { kind: "initial" };
1169
- while (!this.#closed) {
1170
- let connection = null;
1171
- try {
1172
- connection = await this.#executor.openLiveEventStream(this.#path, this.#controller.signal);
1173
- await this.#observer.reconcile(connectionState);
1174
- if (connectionState.kind === "initial") {
1175
- resolveConnected();
1176
- connectionState = { kind: "reconnected" };
1177
- }
1178
- attempts = 0;
1179
- const reader = connection.events.getReader();
1180
- try {
1181
- while (!this.#closed) {
1182
- const result = await reader.read();
1183
- if (result.done) {
1184
- throw new MobiusConnectionError("The live Session event stream disconnected");
1185
- }
1186
- let value;
1187
- try {
1188
- value = JSON.parse(result.value.data);
1189
- }
1190
- catch (cause) {
1191
- throw new MobiusProtocolError("The live Session event stream contains invalid JSON", {
1192
- cause,
1193
- });
1194
- }
1195
- const event = decodeMobiusSessionEvent(value);
1196
- if (event.cursor !== result.value.eventId) {
1197
- throw new MobiusProtocolError("The live Session event identifier is invalid");
1198
- }
1199
- if (!(await this.#observer.receive(event))) {
1200
- this.close();
1201
- }
1202
- }
1203
- }
1204
- finally {
1205
- reader.releaseLock();
1206
- }
1207
- }
1208
- catch (cause) {
1209
- if (this.#closed || this.#controller.signal.aborted) {
1210
- if (connectionState.kind === "initial") {
1211
- rejectConnected(new MobiusCancellationError({ cause }));
1212
- }
1213
- return;
1214
- }
1215
- if (cause instanceof MobiusProtocolError ||
1216
- (cause instanceof MobiusApiError && cause.status < 500) ||
1217
- (cause instanceof MobiusError &&
1218
- !(cause instanceof MobiusConnectionError) &&
1219
- !(cause instanceof MobiusTimeoutError) &&
1220
- !(cause instanceof MobiusApiError))) {
1221
- if (connectionState.kind === "initial") {
1222
- rejectConnected(cause);
1223
- }
1224
- throw cause;
1225
- }
1226
- attempts += 1;
1227
- if (attempts > this.#maximumReconnectAttempts) {
1228
- const failure = new MobiusConnectionError("The live Session event stream could not recover", {
1229
- cause,
1230
- });
1231
- if (connectionState.kind === "initial") {
1232
- rejectConnected(failure);
1233
- }
1234
- throw failure;
1235
- }
1236
- await this.reconnectDelay(attempts);
1237
- }
1238
- finally {
1239
- connection?.close();
1240
- }
1241
- }
1242
- };
1243
- reconnectDelay = async (attempt) => {
1244
- const delayMilliseconds = Math.min(250 * 2 ** (attempt - 1), 2_000);
1245
- await new Promise((resolve, reject) => {
1246
- const timeoutId = globalThis.setTimeout(() => {
1247
- this.#controller.signal.removeEventListener("abort", abort);
1248
- resolve();
1249
- }, delayMilliseconds);
1250
- const abort = () => {
1251
- globalThis.clearTimeout(timeoutId);
1252
- reject(new MobiusCancellationError());
1253
- };
1254
- this.#controller.signal.addEventListener("abort", abort, { once: true });
1255
- });
1256
- };
1257
- }
1258
759
  export class MobiusLiveEventSubscriptionV2 {
1259
760
  #controller = new AbortController();
1260
761
  #executor;
@@ -1320,8 +821,8 @@ export class MobiusLiveEventSubscriptionV2 {
1320
821
  cause,
1321
822
  });
1322
823
  }
1323
- const event = decodeSessionEventV2(value);
1324
- if (!(await this.#observer.receive(event))) {
824
+ const events = decodeSessionEventBatchV2(value);
825
+ if (!(await this.#observer.receive(events))) {
1325
826
  this.close();
1326
827
  }
1327
828
  }
@@ -1384,50 +885,12 @@ export class MobiusEventResources {
1384
885
  #executor;
1385
886
  #maximumReconnectAttempts;
1386
887
  #projectId;
1387
- #subscriptions = new Set();
1388
- #liveSubscriptions = new Set();
1389
888
  #liveSubscriptionsV2 = new Set();
1390
889
  constructor(executor, projectId, maximumReconnectAttempts) {
1391
890
  this.#executor = executor;
1392
891
  this.#maximumReconnectAttempts = maximumReconnectAttempts;
1393
892
  this.#projectId = projectId;
1394
893
  }
1395
- subscribe = async (sessionId, cursor, observer, signal) => {
1396
- if (cursor.length > 512) {
1397
- throw new MobiusValidationError("The Session event cursor is invalid");
1398
- }
1399
- const path = sessionPath(this.#projectId, sessionId, "events");
1400
- const streamPath = cursor.length === 0 ? path : `${path}?cursor=${encodeURIComponent(cursor)}`;
1401
- const controller = new AbortController();
1402
- const abortFromExternal = () => controller.abort(signal.reason);
1403
- if (signal.aborted) {
1404
- abortFromExternal();
1405
- }
1406
- else {
1407
- signal.addEventListener("abort", abortFromExternal, { once: true });
1408
- }
1409
- let firstConnection;
1410
- try {
1411
- firstConnection = await this.#executor.openEventStream(streamPath, controller.signal);
1412
- }
1413
- finally {
1414
- signal.removeEventListener("abort", abortFromExternal);
1415
- }
1416
- const subscription = new MobiusEventSubscription(this.#executor, path, cursor, observer, signal, controller, firstConnection, this.#maximumReconnectAttempts);
1417
- this.#subscriptions.add(subscription);
1418
- const remove = subscription.completed.finally(() => {
1419
- this.#subscriptions.delete(subscription);
1420
- });
1421
- void remove.catch(() => { });
1422
- return subscription;
1423
- };
1424
- subscribeLive = async (sessionId, observer, signal) => {
1425
- const subscription = new MobiusLiveEventSubscription(this.#executor, sessionPath(this.#projectId, sessionId, "events"), observer, signal, this.#maximumReconnectAttempts);
1426
- this.#liveSubscriptions.add(subscription);
1427
- void subscription.completed.finally(() => this.#liveSubscriptions.delete(subscription)).catch(() => undefined);
1428
- await subscription.connected;
1429
- return subscription;
1430
- };
1431
894
  subscribeLiveV2 = async (sessionId, observer, signal) => {
1432
895
  const subscription = new MobiusLiveEventSubscriptionV2(this.#executor, sessionPath(this.#projectId, sessionId, "events-v2"), observer, signal, this.#maximumReconnectAttempts);
1433
896
  this.#liveSubscriptionsV2.add(subscription);
@@ -1445,35 +908,7 @@ export class MobiusEventResources {
1445
908
  }
1446
909
  return decodeSessionEventLogPageV2(await this.#executor.get(`${path.pathname.slice(1)}${path.search}`, signal));
1447
910
  };
1448
- list = async (sessionId, page, signal) => {
1449
- const path = requestUrl(new URL("https://mobius.invalid/"), sessionPath(this.#projectId, sessionId, "events"));
1450
- path.searchParams.set("limit", page.limit.toString());
1451
- if (page.cursor.length > 0) {
1452
- path.searchParams.set("cursor", page.cursor);
1453
- }
1454
- return decodeMobiusEventPage(await this.#executor.get(`${path.pathname.slice(1)}${path.search}`, signal));
1455
- };
1456
- wait = async (sessionId, page, waitMilliseconds, signal) => {
1457
- if (!Number.isSafeInteger(waitMilliseconds) || waitMilliseconds < 1 || waitMilliseconds > 10_000) {
1458
- throw new MobiusValidationError("The Session event wait is invalid");
1459
- }
1460
- const path = requestUrl(new URL("https://mobius.invalid/"), sessionPath(this.#projectId, sessionId, "events"));
1461
- path.searchParams.set("limit", page.limit.toString());
1462
- path.searchParams.set("wait", waitMilliseconds.toString());
1463
- if (page.cursor.length > 0) {
1464
- path.searchParams.set("cursor", page.cursor);
1465
- }
1466
- return decodeMobiusEventPage(await this.#executor.get(`${path.pathname.slice(1)}${path.search}`, signal));
1467
- };
1468
911
  close = () => {
1469
- for (const subscription of this.#subscriptions) {
1470
- subscription.close();
1471
- }
1472
- this.#subscriptions.clear();
1473
- for (const subscription of this.#liveSubscriptions) {
1474
- subscription.close();
1475
- }
1476
- this.#liveSubscriptions.clear();
1477
912
  for (const subscription of this.#liveSubscriptionsV2) {
1478
913
  subscription.close();
1479
914
  }