@enclave-vm/client 2.19.2 → 2.20.0-rc.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/client.d.ts CHANGED
@@ -73,6 +73,11 @@ export declare class EnclaveClient {
73
73
  * Never rejects.
74
74
  */
75
75
  private stopSession;
76
+ /**
77
+ * Ask the server to end a session (`DELETE /sessions/:sessionId`). Never rejects: a failure is
78
+ * what it answers with. An absent or replaced creation is no failure after its POST settles.
79
+ */
80
+ private sendStop;
76
81
  /**
77
82
  * Start a session and begin streaming events.
78
83
  */
@@ -139,5 +144,7 @@ export declare class EnclaveClient {
139
144
  * Build request headers.
140
145
  */
141
146
  private buildHeaders;
147
+ /** The creation identifier always takes precedence over configured headers. */
148
+ private sessionHeaders;
142
149
  }
143
150
  //# sourceMappingURL=client.d.ts.map
package/client.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAA0C,MAAM,mBAAmB,CAAC;AA0B3F,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACd,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,WAAW,EACZ,MAAM,YAAY,CAAC;AA+KpB;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAErB;IACF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAuC;IAEtE;;;;;OAKG;gBACS,MAAM,EAAE,mBAAmB;IAIvC;;;;;;;;;;OAUG;IACG,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAAc,GAAG,oBAAyB,GAAG,OAAO,CAAC,aAAa,CAAC;IAKxG;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAAc,GAAG,oBAAyB,GAAG,a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= {
178
178
  sessionId,
179
+ instanceId: generateSessionId(),
179
180
  events: [],
180
181
  resolve: resolveResult,
181
182
  handlers: options,
@@ -294,33 +295,57 @@ var EnclaveClient = class {
294
295
  if (session.ended) {
295
296
  return;
296
297
  }
298
+ const wasCreating = session.creating;
297
299
  this.endSession(session, this.cancelledResult(session, reason));
300
+ const stop = await this.sendStop(session);
301
+ let failure = stop.failure;
302
+ if (stop.retryIfCreating && wasCreating) {
303
+ failure ??= "the session was still being created";
304
+ if (session.creating) {
305
+ session.stopAfterCreation = true;
306
+ } else {
307
+ void this.sendStop(session);
308
+ }
309
+ }
310
+ if (failure !== null) {
311
+ try {
312
+ session.handlers.onError?.(
313
+ "CANCEL_FAILED",
314
+ `The server did not confirm that session ${session.sessionId} was stopped: ${failure}`
315
+ );
316
+ } catch {
317
+ }
318
+ }
319
+ }
320
+ /**
321
+ * Ask the server to end a session (`DELETE /sessions/:sessionId`). Never rejects: a failure is
322
+ * what it answers with. An absent or replaced creation is no failure after its POST settles.
323
+ */
324
+ async sendStop(session) {
298
325
  const url = `${this.config.baseUrl}/sessions/${sessionPathSegment(session.sessionId)}`;
299
326
  let failure = null;
327
+ let retryIfCreating = false;
300
328
  try {
301
329
  await this.withDeadline("Stopping the session", async (signal) => {
302
330
  const response = await this.config.fetch(url, {
303
331
  method: "DELETE",
304
- headers: this.buildHeaders(),
332
+ headers: this.sessionHeaders(session),
305
333
  signal
306
334
  });
307
335
  void response.body?.cancel().catch(() => void 0);
308
- if (!response.ok && response.status !== 404) {
336
+ if (response.status === 404 || response.status === 412) {
337
+ retryIfCreating = true;
338
+ } else if (response.status === 409) {
339
+ retryIfCreating = true;
340
+ failure = "the server could not confirm the session creation";
341
+ } else if (!response.ok) {
309
342
  failure = `the server answered ${response.status}`;
310
343
  }
311
344
  });
312
345
  } catch (error) {
313
346
  failure = error instanceof Error ? error.message : "the request failed";
314
347
  }
315
- if (failure !== null) {
316
- try {
317
- session.handlers.onError?.(
318
- "CANCEL_FAILED",
319
- `The server did not confirm that session ${session.sessionId} was stopped: ${failure}`
320
- );
321
- } catch {
322
- }
323
- }
348
+ return { failure, retryIfCreating };
324
349
  }
325
350
  /**
326
351
  * Start a session and begin streaming events.
@@ -339,16 +364,26 @@ var EnclaveClient = class {
339
364
  };
340
365
  }
341
366
  await this.runConnection(session, async (signal) => {
342
- const response = await this.config.fetch(url, {
343
- method: "POST",
344
- headers: {
345
- ...this.buildHeaders(),
346
- "Content-Type": "application/json",
347
- Accept: "application/x-ndjson"
348
- },
349
- body: JSON.stringify(body),
350
- signal
351
- });
367
+ session.creating = true;
368
+ let response;
369
+ try {
370
+ response = await this.config.fetch(url, {
371
+ method: "POST",
372
+ headers: {
373
+ ...this.sessionHeaders(session),
374
+ "Content-Type": "application/json",
375
+ Accept: "application/x-ndjson"
376
+ },
377
+ body: JSON.stringify(body),
378
+ signal
379
+ });
380
+ } finally {
381
+ session.creating = false;
382
+ if (session.stopAfterCreation) {
383
+ session.stopAfterCreation = false;
384
+ void this.sendStop(session);
385
+ }
386
+ }
352
387
  if (!response.ok) {
353
388
  const errorText = (await response.text()).slice(0, 500);
354
389
  throw new EnclaveClientError("SESSION_ERROR", `Failed to start session: ${response.status} - ${errorText}`);
@@ -650,6 +685,15 @@ var EnclaveClient = class {
650
685
  ...this.config.headers
651
686
  };
652
687
  }
688
+ /** The creation identifier always takes precedence over configured headers. */
689
+ sessionHeaders(session) {
690
+ const headers = this.buildHeaders();
691
+ for (const name of Object.keys(headers)) {
692
+ if (name.toLowerCase() === "x-session-instance-id") delete headers[name];
693
+ }
694
+ headers["X-Session-Instance-ID"] = session.instanceId;
695
+ return headers;
696
+ }
653
697
  };
654
698
 
655
699
  // libs/client/src/index.ts
package/index.js CHANGED
@@ -182,6 +182,7 @@ var EnclaveClient = class {
182
182
  });
183
183
  const session = {
184
184
  sessionId,
185
+ instanceId: (0, import_types.generateSessionId)(),
185
186
  events: [],
186
187
  resolve: resolveResult,
187
188
  handlers: options,
@@ -300,33 +301,57 @@ var EnclaveClient = class {
300
301
  if (session.ended) {
301
302
  return;
302
303
  }
304
+ const wasCreating = session.creating;
303
305
  this.endSession(session, this.cancelledResult(session, reason));
306
+ const stop = await this.sendStop(session);
307
+ let failure = stop.failure;
308
+ if (stop.retryIfCreating && wasCreating) {
309
+ failure ??= "the session was still being created";
310
+ if (session.creating) {
311
+ session.stopAfterCreation = true;
312
+ } else {
313
+ void this.sendStop(session);
314
+ }
315
+ }
316
+ if (failure !== null) {
317
+ try {
318
+ session.handlers.onError?.(
319
+ "CANCEL_FAILED",
320
+ `The server did not confirm that session ${session.sessionId} was stopped: ${failure}`
321
+ );
322
+ } catch {
323
+ }
324
+ }
325
+ }
326
+ /**
327
+ * Ask the server to end a session (`DELETE /sessions/:sessionId`). Never rejects: a failure is
328
+ * what it answers with. An absent or replaced creation is no failure after its POST settles.
329
+ */
330
+ async sendStop(session) {
304
331
  const url = `${this.config.baseUrl}/sessions/${sessionPathSegment(session.sessionId)}`;
305
332
  let failure = null;
333
+ let retryIfCreating = false;
306
334
  try {
307
335
  await this.withDeadline("Stopping the session", async (signal) => {
308
336
  const response = await this.config.fetch(url, {
309
337
  method: "DELETE",
310
- headers: this.buildHeaders(),
338
+ headers: this.sessionHeaders(session),
311
339
  signal
312
340
  });
313
341
  void response.body?.cancel().catch(() => void 0);
314
- if (!response.ok && response.status !== 404) {
342
+ if (response.status === 404 || response.status === 412) {
343
+ retryIfCreating = true;
344
+ } else if (response.status === 409) {
345
+ retryIfCreating = true;
346
+ failure = "the server could not confirm the session creation";
347
+ } else if (!response.ok) {
315
348
  failure = `the server answered ${response.status}`;
316
349
  }
317
350
  });
318
351
  } catch (error) {
319
352
  failure = error instanceof Error ? error.message : "the request failed";
320
353
  }
321
- if (failure !== null) {
322
- try {
323
- session.handlers.onError?.(
324
- "CANCEL_FAILED",
325
- `The server did not confirm that session ${session.sessionId} was stopped: ${failure}`
326
- );
327
- } catch {
328
- }
329
- }
354
+ return { failure, retryIfCreating };
330
355
  }
331
356
  /**
332
357
  * Start a session and begin streaming events.
@@ -345,16 +370,26 @@ var EnclaveClient = class {
345
370
  };
346
371
  }
347
372
  await this.runConnection(session, async (signal) => {
348
- const response = await this.config.fetch(url, {
349
- method: "POST",
350
- headers: {
351
- ...this.buildHeaders(),
352
- "Content-Type": "application/json",
353
- Accept: "application/x-ndjson"
354
- },
355
- body: JSON.stringify(body),
356
- signal
357
- });
373
+ session.creating = true;
374
+ let response;
375
+ try {
376
+ response = await this.config.fetch(url, {
377
+ method: "POST",
378
+ headers: {
379
+ ...this.sessionHeaders(session),
380
+ "Content-Type": "application/json",
381
+ Accept: "application/x-ndjson"
382
+ },
383
+ body: JSON.stringify(body),
384
+ signal
385
+ });
386
+ } finally {
387
+ session.creating = false;
388
+ if (session.stopAfterCreation) {
389
+ session.stopAfterCreation = false;
390
+ void this.sendStop(session);
391
+ }
392
+ }
358
393
  if (!response.ok) {
359
394
  const errorText = (await response.text()).slice(0, 500);
360
395
  throw new EnclaveClientError("SESSION_ERROR", `Failed to start session: ${response.status} - ${errorText}`);
@@ -656,6 +691,15 @@ var EnclaveClient = class {
656
691
  ...this.config.headers
657
692
  };
658
693
  }
694
+ /** The creation identifier always takes precedence over configured headers. */
695
+ sessionHeaders(session) {
696
+ const headers = this.buildHeaders();
697
+ for (const name of Object.keys(headers)) {
698
+ if (name.toLowerCase() === "x-session-instance-id") delete headers[name];
699
+ }
700
+ headers["X-Session-Instance-ID"] = session.instanceId;
701
+ return headers;
702
+ }
659
703
  };
660
704
 
661
705
  // libs/client/src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enclave-vm/client",
3
- "version": "2.19.2",
3
+ "version": "2.20.0-rc.1",
4
4
  "description": "Browser and Node.js client SDK for the EnclaveJS streaming runtime",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://github.com/agentfront/enclave",
@@ -33,7 +33,7 @@
33
33
  }
34
34
  },
35
35
  "dependencies": {
36
- "@enclave-vm/stream": "2.19.2",
37
- "@enclave-vm/types": "2.19.2"
36
+ "@enclave-vm/stream": "2.20.0-rc.1",
37
+ "@enclave-vm/types": "2.20.0-rc.1"
38
38
  }
39
39
  }