@tangle-network/agent-app 0.60.79 → 0.60.81

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.
@@ -476,12 +476,22 @@ function isAutomatedPrewarmRequest(request) {
476
476
  return AUTOMATED_AGENT.test(request.headers.get("user-agent") ?? "");
477
477
  }
478
478
  var RECENT_LIMIT = 1e3;
479
+ async function requestReason(request) {
480
+ if (Number(request.headers.get("content-length") ?? 0) > 1024) return "page-open";
481
+ try {
482
+ const body = await request.clone().json();
483
+ return body?.reason === "composer-focus" ? "composer-focus" : "page-open";
484
+ } catch {
485
+ return "page-open";
486
+ }
487
+ }
479
488
  function reply(body, status = 200) {
480
489
  return Response.json(body, { status, headers: { "Cache-Control": "private, no-store" } });
481
490
  }
482
491
  function createWorkspacePrewarmRoute(options) {
483
492
  const recentMs = options.recentMs ?? 3e4;
484
493
  const now = options.now ?? (() => Date.now());
494
+ const staleMs = options.staleMs ?? 45e3;
485
495
  const inFlight = /* @__PURE__ */ new Map();
486
496
  const recent = /* @__PURE__ */ new Map();
487
497
  const emit = (event) => {
@@ -520,7 +530,7 @@ function createWorkspacePrewarmRoute(options) {
520
530
  if (isAutomatedPrewarmRequest(request)) return reply({ outcome: "automated", ms: 0 });
521
531
  let auth;
522
532
  try {
523
- auth = await options.authorize({ request, context });
533
+ auth = await options.authorize({ request, context, reason: await requestReason(request) });
524
534
  } catch (err) {
525
535
  emit({ type: "failed", key: "", ms: now() - started, error: err instanceof Error ? err.message : String(err) });
526
536
  return reply({ outcome: "failed", ms: now() - started });
@@ -529,20 +539,19 @@ function createWorkspacePrewarmRoute(options) {
529
539
  if (auth.status === "declined") return reply({ outcome: "declined", ms: now() - started, reason: auth.reason });
530
540
  const { key } = auth;
531
541
  const running = inFlight.get(key);
532
- if (running) {
533
- const result2 = await running;
534
- return reply({ ...result2, outcome: result2.outcome === "warmed" ? "joined" : result2.outcome, ms: now() - started });
542
+ if (running && now() - running.at < staleMs) {
543
+ return reply({ outcome: "joined", ms: now() - started });
535
544
  }
536
545
  const warmedAt = recent.get(key);
537
546
  if (warmedAt !== void 0 && now() - warmedAt < recentMs) {
538
547
  return reply({ outcome: "recent", ms: now() - started });
539
548
  }
540
- const result = run(key, auth.warm);
541
- inFlight.set(key, result);
549
+ const mark = { at: now() };
550
+ inFlight.set(key, mark);
542
551
  try {
543
- return reply({ ...await result, ms: now() - started });
552
+ return reply({ ...await run(key, auth.warm), ms: now() - started });
544
553
  } finally {
545
- if (inFlight.get(key) === result) inFlight.delete(key);
554
+ if (inFlight.get(key) === mark) inFlight.delete(key);
546
555
  }
547
556
  };
548
557
  }
@@ -2918,4 +2927,4 @@ export {
2918
2927
  isTerminalPromptEvent,
2919
2928
  detectInteractiveQuestion
2920
2929
  };
2921
- //# sourceMappingURL=chunk-MBEJPQW6.js.map
2930
+ //# sourceMappingURL=chunk-A5CZ2X6C.js.map