@tangle-network/agent-app 0.63.1 → 0.63.3

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.
Files changed (39) hide show
  1. package/dist/agent-profiles/d1.js +3 -3
  2. package/dist/agent-profiles/index.js +3 -3
  3. package/dist/chat-routes/index.d.ts +1 -0
  4. package/dist/chat-routes/index.js +15 -15
  5. package/dist/chat-routes/route-storage.d.ts +21 -1
  6. package/dist/chat-routes/turn-route-contracts.d.ts +2 -9
  7. package/dist/{chunk-A2NQMVMF.js → chunk-464WRGJ3.js} +14 -5
  8. package/dist/chunk-464WRGJ3.js.map +1 -0
  9. package/dist/{chunk-XEAZWJ3W.js → chunk-7Y7U7U7X.js} +2 -2
  10. package/dist/{chunk-5BJHKQ33.js → chunk-ACB7KOQ6.js} +4 -4
  11. package/dist/{chunk-7TZA2TAM.js → chunk-FCYHDENJ.js} +2 -2
  12. package/dist/{chunk-WSQFNMIA.js → chunk-HRBF4B5L.js} +5 -5
  13. package/dist/{chunk-WSQFNMIA.js.map → chunk-HRBF4B5L.js.map} +1 -1
  14. package/dist/{chunk-SRWQCWLR.js → chunk-IX6UVHXY.js} +3 -3
  15. package/dist/{chunk-MM3GOLHO.js → chunk-PGWKYLHC.js} +11 -25
  16. package/dist/chunk-PGWKYLHC.js.map +1 -0
  17. package/dist/{chunk-ND5XWLRU.js → chunk-POIVNKYH.js} +83 -24
  18. package/dist/chunk-POIVNKYH.js.map +1 -0
  19. package/dist/preset-cloudflare/index.js +8 -8
  20. package/dist/public-consultation/index.js +8 -8
  21. package/dist/rollout/d1.d.ts +24 -3
  22. package/dist/rollout/d1.js +46 -24
  23. package/dist/rollout/d1.js.map +1 -1
  24. package/dist/sandbox/index.js +7 -5
  25. package/dist/sandbox/prewarm-route.d.ts +7 -0
  26. package/dist/stream/index.js +17 -17
  27. package/dist/tools/index.js +4 -4
  28. package/dist/turn-stream/adapters.d.ts +6 -0
  29. package/dist/turn-stream/index.js +3 -1
  30. package/dist/turn-stream/index.js.map +1 -1
  31. package/dist/work-product/index.js +2 -2
  32. package/package.json +3 -2
  33. package/dist/chunk-A2NQMVMF.js.map +0 -1
  34. package/dist/chunk-MM3GOLHO.js.map +0 -1
  35. package/dist/chunk-ND5XWLRU.js.map +0 -1
  36. /package/dist/{chunk-XEAZWJ3W.js.map → chunk-7Y7U7U7X.js.map} +0 -0
  37. /package/dist/{chunk-5BJHKQ33.js.map → chunk-ACB7KOQ6.js.map} +0 -0
  38. /package/dist/{chunk-7TZA2TAM.js.map → chunk-FCYHDENJ.js.map} +0 -0
  39. /package/dist/{chunk-SRWQCWLR.js.map → chunk-IX6UVHXY.js.map} +0 -0
@@ -11,6 +11,13 @@ import {
11
11
  sandboxTransientBackoffMs,
12
12
  serializeSandboxProvisioningError
13
13
  } from "./chunk-PK65B6HP.js";
14
+ import {
15
+ DEFAULT_TURN_LOCK_LOCATION_HINT,
16
+ createDurableTurnLock
17
+ } from "./chunk-464WRGJ3.js";
18
+ import {
19
+ createD1TurnEventStore
20
+ } from "./chunk-ISP6ZH3G.js";
14
21
  import {
15
22
  assertProfilePromptWithinBudget,
16
23
  fingerprintAgentProfile
@@ -877,6 +884,20 @@ ON CONFLICT(id) DO UPDATE SET at = ?2, expires_at = ?3, report = ?4`;
877
884
  };
878
885
  }
879
886
 
887
+ // src/chat-routes/route-storage.ts
888
+ function createDefaultTurnLock(store) {
889
+ return createDurableTurnLock({ ...store.lock, locationHint: store.region, scopeOf: () => "thread" });
890
+ }
891
+ function resolveTurnStorage(options) {
892
+ const store = options.turnStore;
893
+ if ("append" in store) return { ...options, turnStore: store };
894
+ return {
895
+ ...options,
896
+ turnStore: createD1TurnEventStore(store.db),
897
+ turnLock: options.turnLock ?? createDefaultTurnLock(store)
898
+ };
899
+ }
900
+
880
901
  // src/sandbox/prewarm-route.ts
881
902
  var AUTOMATED_AGENT = /bot\b|crawler|spider|slurp|facebookexternalhit|embedly|preview|whatsapp|curl\/|wget\/|python-requests|go-http-client/i;
882
903
  function isAutomatedPrewarmRequest(request) {
@@ -906,6 +927,8 @@ function reply(body, status = 200) {
906
927
  return Response.json(body, { status, headers });
907
928
  }
908
929
  function createWorkspacePrewarmRoute(options) {
930
+ const turnLock = options.turnStore ? createDefaultTurnLock(options.turnStore) : void 0;
931
+ const receipts = options.receipts ?? (options.turnStore ? createD1WarmReceiptStore(options.turnStore.db) : void 0);
909
932
  const recentMs = options.recentMs ?? 3e4;
910
933
  const now = options.now ?? (() => Date.now());
911
934
  const staleMs = options.staleMs ?? 45e3;
@@ -956,9 +979,9 @@ function createWorkspacePrewarmRoute(options) {
956
979
  const mark = { at: now() };
957
980
  stepsInFlight.set(id, mark);
958
981
  try {
959
- if (options.receipts) {
982
+ if (receipts) {
960
983
  try {
961
- const stored = await options.receipts.get(id);
984
+ const stored = await receipts.get(id);
962
985
  if (stored && now() < stored.expiresAt) {
963
986
  remember(id, stored.expiresAt, warmUntil);
964
987
  return result("hit");
@@ -976,8 +999,8 @@ function createWorkspacePrewarmRoute(options) {
976
999
  const at = now();
977
1000
  const expiresAt = at + step.ttlMs;
978
1001
  remember(id, expiresAt, warmUntil);
979
- if (options.receipts) {
980
- await options.receipts.put(id, { at, expiresAt, ...report ? { report } : {} }).catch((err) => {
1002
+ if (receipts) {
1003
+ await receipts.put(id, { at, expiresAt, ...report ? { report } : {} }).catch((err) => {
981
1004
  emit({ type: "failed", key: id, ms: now() - started, error: err instanceof Error ? err.message : String(err) });
982
1005
  });
983
1006
  }
@@ -986,6 +1009,20 @@ function createWorkspacePrewarmRoute(options) {
986
1009
  if (stepsInFlight.get(id) === mark) stepsInFlight.delete(id);
987
1010
  }
988
1011
  }
1012
+ async function runLegacy(key, warm) {
1013
+ const started = now();
1014
+ const running = inFlight.get(key);
1015
+ if (running && now() - running.at < staleMs) return { outcome: "joined", ms: now() - started };
1016
+ const warmedAt = recent.get(key);
1017
+ if (warmedAt !== void 0 && now() - warmedAt < recentMs) return { outcome: "recent", ms: now() - started };
1018
+ const mark = { at: now() };
1019
+ inFlight.set(key, mark);
1020
+ try {
1021
+ return { ...await run(key, warm), ms: now() - started };
1022
+ } finally {
1023
+ if (inFlight.get(key) === mark) inFlight.delete(key);
1024
+ }
1025
+ }
989
1026
  return async function prewarm(request, context) {
990
1027
  const started = now();
991
1028
  if (request.method !== "POST") {
@@ -1001,27 +1038,48 @@ function createWorkspacePrewarmRoute(options) {
1001
1038
  }
1002
1039
  if (auth.status === "denied") return auth.response;
1003
1040
  if (auth.status === "declined") return reply({ outcome: "declined", ms: now() - started, reason: auth.reason });
1004
- if ("steps" in auth) {
1005
- const steps = await Promise.all(auth.steps.map((step) => runStep(step)));
1006
- const outcome = steps.some((step) => step.outcome === "failed") ? "failed" : steps.some((step) => step.outcome === "miss") ? "warmed" : steps.some((step) => step.outcome === "joined") ? "joined" : "recent";
1007
- return reply({ outcome, ms: now() - started, steps });
1008
- }
1009
- const { key } = auth;
1010
- const running = inFlight.get(key);
1011
- if (running && now() - running.at < staleMs) {
1012
- return reply({ outcome: "joined", ms: now() - started });
1013
- }
1014
- const warmedAt = recent.get(key);
1015
- if (warmedAt !== void 0 && now() - warmedAt < recentMs) {
1016
- return reply({ outcome: "recent", ms: now() - started });
1041
+ const steps = "steps" in auth ? [...auth.steps] : [];
1042
+ let lockFailure;
1043
+ if (turnLock) {
1044
+ if (!auth.workspaceId?.trim()) return reply({ outcome: "failed", ms: now() - started, reason: "Lock prewarm requires an authorized workspaceId" });
1045
+ const workspaceId = auth.workspaceId;
1046
+ if (steps.some((step) => step.name === "turn_lock")) return reply({ outcome: "failed", ms: now() - started, reason: "turn_lock is owned by the shared prewarm route" });
1047
+ try {
1048
+ steps.push({
1049
+ name: "turn_lock",
1050
+ key: await warmKey({
1051
+ workspaceId,
1052
+ channel: String(options.turnStore.lock.namespace.idFromName(workspaceId)),
1053
+ region: options.turnStore.region ?? DEFAULT_TURN_LOCK_LOCATION_HINT
1054
+ }),
1055
+ ttlMs: 3e4,
1056
+ run: async () => {
1057
+ await turnLock.warm({ tenantId: workspaceId });
1058
+ return { lockReady: true };
1059
+ }
1060
+ });
1061
+ } catch (error) {
1062
+ const ms = now() - started;
1063
+ lockFailure = { name: "turn_lock", outcome: "failed", ms };
1064
+ emit({
1065
+ type: "step",
1066
+ name: "turn_lock",
1067
+ key: workspaceId,
1068
+ outcome: "failed",
1069
+ ms,
1070
+ error: error instanceof Error ? error.message : String(error)
1071
+ });
1072
+ }
1017
1073
  }
1018
- const mark = { at: now() };
1019
- inFlight.set(key, mark);
1020
- try {
1021
- return reply({ ...await run(key, auth.warm), ms: now() - started });
1022
- } finally {
1023
- if (inFlight.get(key) === mark) inFlight.delete(key);
1074
+ const pendingSteps = Promise.all(steps.map((step) => runStep(step))).then((results2) => lockFailure ? [...results2, lockFailure] : results2);
1075
+ if ("steps" in auth) {
1076
+ const results2 = await pendingSteps;
1077
+ const outcome2 = results2.some((step) => step.outcome === "failed") ? "failed" : results2.some((step) => step.outcome === "miss") ? "warmed" : results2.some((step) => step.outcome === "joined") ? "joined" : "recent";
1078
+ return reply({ outcome: outcome2, ms: now() - started, steps: results2 });
1024
1079
  }
1080
+ const [legacy, results] = await Promise.all([runLegacy(auth.key, auth.warm), pendingSteps]);
1081
+ const outcome = legacy.outcome === "failed" || results.some((step) => step.outcome === "failed") ? "failed" : legacy.outcome === "warmed" || results.some((step) => step.outcome === "miss") ? "warmed" : legacy.outcome === "joined" || results.some((step) => step.outcome === "joined") ? "joined" : legacy.outcome;
1082
+ return reply({ ...legacy, outcome, ms: now() - started, ...results.length ? { steps: results } : {} });
1025
1083
  };
1026
1084
  }
1027
1085
 
@@ -3611,6 +3669,7 @@ export {
3611
3669
  DEFAULT_WARM_RECEIPT_TABLE,
3612
3670
  WARM_RECEIPT_TABLE_DDL,
3613
3671
  createD1WarmReceiptStore,
3672
+ resolveTurnStorage,
3614
3673
  isAutomatedPrewarmRequest,
3615
3674
  createWorkspacePrewarmRoute,
3616
3675
  DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS,
@@ -3689,4 +3748,4 @@ export {
3689
3748
  sandboxPromptRunner,
3690
3749
  createSandboxRunTool
3691
3750
  };
3692
- //# sourceMappingURL=chunk-ND5XWLRU.js.map
3751
+ //# sourceMappingURL=chunk-POIVNKYH.js.map