@asgard-js/core 0.3.53 → 0.3.55

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/index.mjs CHANGED
@@ -16,7 +16,7 @@ class w extends Error {
16
16
  super(`HTTP ${t}: ${e}`), this.name = "HttpError", this.status = t, this.statusText = e, this.body = r;
17
17
  }
18
18
  }
19
- function _n(n) {
19
+ function Ln(n) {
20
20
  return n instanceof w;
21
21
  }
22
22
  class Z extends Error {
@@ -26,14 +26,21 @@ class Z extends Error {
26
26
  super(`Cannot send a message while a "${t}" run is in flight on this channel.`), this.name = "ChannelBusyError", this.runKind = t;
27
27
  }
28
28
  }
29
- function Ln(n) {
29
+ function _n(n) {
30
30
  return n instanceof Z;
31
31
  }
32
32
  class ee extends Error {
33
- /** The paused batch's process id, as carried by the pending consent prompt. */
33
+ /**
34
+ * The paused batch's process id, as carried by the pending consent prompt.
35
+ *
36
+ * **Empty string on a rejoin.** A prompt recovered after a reload is reconstructed by the backend
37
+ * from its durable pause state, and that path deliberately leaves the process id blank (clients are
38
+ * meant to detect a consent pause from the prompt's presence, not from this id). Treat it as a
39
+ * diagnostic hint, never as a key.
40
+ */
34
41
  processId;
35
42
  constructor(t) {
36
- super("Cannot send a message while this channel is awaiting a tool-call consent response."), this.name = "ChannelAwaitingConsentError", this.processId = t;
43
+ super("Cannot send this turn while the channel is awaiting a tool-call consent response."), this.name = "ChannelAwaitingConsentError", this.processId = t;
37
44
  }
38
45
  }
39
46
  function Un(n) {
@@ -123,7 +130,7 @@ var et = function(n, t) {
123
130
  const te = "text/event-stream", tt = 1e3, le = "last-event-id";
124
131
  function Ee(n, t) {
125
132
  var { signal: e, headers: r, onopen: s, onmessage: o, onclose: i, onerror: a, openWhenHidden: c, fetch: u } = t, h = et(t, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
126
- return new Promise((m, b) => {
133
+ return new Promise((m, v) => {
127
134
  const d = Object.assign({}, r);
128
135
  d.accept || (d.accept = te);
129
136
  let p;
@@ -131,12 +138,12 @@ function Ee(n, t) {
131
138
  p.abort(), document.hidden || X();
132
139
  }
133
140
  c || document.addEventListener("visibilitychange", f);
134
- let _ = tt, E = 0;
135
- function L() {
141
+ let L = tt, E = 0;
142
+ function _() {
136
143
  document.removeEventListener("visibilitychange", f), window.clearTimeout(E), p.abort();
137
144
  }
138
145
  e?.addEventListener("abort", () => {
139
- L(), m();
146
+ _(), m();
140
147
  });
141
148
  const Ye = u ?? window.fetch, We = s ?? nt;
142
149
  async function X() {
@@ -147,15 +154,15 @@ function Ee(n, t) {
147
154
  await We(H), await ze(H.body, Je(Qe((C) => {
148
155
  C ? d[le] = C : delete d[le];
149
156
  }, (C) => {
150
- _ = C;
151
- }, o))), i?.(), L(), m();
157
+ L = C;
158
+ }, o))), i?.(), _(), m();
152
159
  } catch (H) {
153
160
  if (!p.signal.aborted)
154
161
  try {
155
- const C = (V = a?.(H)) !== null && V !== void 0 ? V : _;
162
+ const C = (V = a?.(H)) !== null && V !== void 0 ? V : L;
156
163
  window.clearTimeout(E), E = window.setTimeout(X, C);
157
164
  } catch (C) {
158
- L(), b(C);
165
+ _(), v(C);
159
166
  }
160
167
  }
161
168
  }
@@ -318,8 +325,8 @@ function st(n, t, e) {
318
325
  }
319
326
  function a(d, p) {
320
327
  r[d] && (s[d] = function(f) {
321
- return new Promise(function(_, E) {
322
- o.push([d, f, _, E]) > 1 || c(d, f);
328
+ return new Promise(function(L, E) {
329
+ o.push([d, f, L, E]) > 1 || c(d, f);
323
330
  });
324
331
  }, p && (s[d] = p(s[d])));
325
332
  }
@@ -327,11 +334,11 @@ function st(n, t, e) {
327
334
  try {
328
335
  u(r[d](p));
329
336
  } catch (f) {
330
- b(o[0][3], f);
337
+ v(o[0][3], f);
331
338
  }
332
339
  }
333
340
  function u(d) {
334
- d.value instanceof k ? Promise.resolve(d.value.v).then(h, m) : b(o[0][2], d);
341
+ d.value instanceof k ? Promise.resolve(d.value.v).then(h, m) : v(o[0][2], d);
335
342
  }
336
343
  function h(d) {
337
344
  c("next", d);
@@ -339,7 +346,7 @@ function st(n, t, e) {
339
346
  function m(d) {
340
347
  c("throw", d);
341
348
  }
342
- function b(d, p) {
349
+ function v(d, p) {
343
350
  d(p), o.shift(), o.length && c(o[0][0], o[0][1]);
344
351
  }
345
352
  }
@@ -424,7 +431,7 @@ var j = (function() {
424
431
  if (m) {
425
432
  this._finalizers = null;
426
433
  try {
427
- for (var b = N(m), d = b.next(); !d.done; d = b.next()) {
434
+ for (var v = N(m), d = v.next(); !d.done; d = v.next()) {
428
435
  var p = d.value;
429
436
  try {
430
437
  de(p);
@@ -436,7 +443,7 @@ var j = (function() {
436
443
  r = { error: f };
437
444
  } finally {
438
445
  try {
439
- d && !d.done && (s = b.return) && s.call(b);
446
+ d && !d.done && (s = v.return) && s.call(v);
440
447
  } finally {
441
448
  if (r) throw r.error;
442
449
  }
@@ -953,14 +960,14 @@ var mt = (function(n) {
953
960
  function Pe(n) {
954
961
  return n && g(n.schedule);
955
962
  }
956
- function _e(n) {
963
+ function Le(n) {
957
964
  return n[n.length - 1];
958
965
  }
959
966
  function It(n) {
960
- return g(_e(n)) ? n.pop() : void 0;
967
+ return g(Le(n)) ? n.pop() : void 0;
961
968
  }
962
- function Le(n) {
963
- return Pe(_e(n)) ? n.pop() : void 0;
969
+ function _e(n) {
970
+ return Pe(Le(n)) ? n.pop() : void 0;
964
971
  }
965
972
  var Ue = (function(n) {
966
973
  return n && typeof n.length == "number" && typeof n != "function";
@@ -1084,7 +1091,7 @@ function At(n) {
1084
1091
  }
1085
1092
  function He(n) {
1086
1093
  return new S(function(t) {
1087
- _t(n, t).catch(function(e) {
1094
+ Lt(n, t).catch(function(e) {
1088
1095
  return t.error(e);
1089
1096
  });
1090
1097
  });
@@ -1092,7 +1099,7 @@ function He(n) {
1092
1099
  function Pt(n) {
1093
1100
  return He(De(n));
1094
1101
  }
1095
- function _t(n, t) {
1102
+ function Lt(n, t) {
1096
1103
  var e, r, s, o;
1097
1104
  return rt(this, void 0, void 0, function() {
1098
1105
  var i, a;
@@ -1162,7 +1169,7 @@ function Ge(n, t) {
1162
1169
  }, t));
1163
1170
  });
1164
1171
  }
1165
- function Lt(n, t) {
1172
+ function _t(n, t) {
1166
1173
  return U(n).pipe(Ge(t), Be(t));
1167
1174
  }
1168
1175
  function Ut(n, t) {
@@ -1215,7 +1222,7 @@ function Nt(n, t) {
1215
1222
  function Rt(n, t) {
1216
1223
  if (n != null) {
1217
1224
  if (ke(n))
1218
- return Lt(n, t);
1225
+ return _t(n, t);
1219
1226
  if (Ue(n))
1220
1227
  return Mt(n, t);
1221
1228
  if (Me(n))
@@ -1235,7 +1242,7 @@ function ce(n, t) {
1235
1242
  function jt() {
1236
1243
  for (var n = [], t = 0; t < arguments.length; t++)
1237
1244
  n[t] = arguments[t];
1238
- var e = Le(n);
1245
+ var e = _e(n);
1239
1246
  return ce(n, e);
1240
1247
  }
1241
1248
  function $t(n) {
@@ -1287,7 +1294,7 @@ function Xt(n, t) {
1287
1294
  function Vt() {
1288
1295
  for (var n = [], t = 0; t < arguments.length; t++)
1289
1296
  n[t] = arguments[t];
1290
- var e = Le(n), r = It(n), s = Yt(n), o = s.args, i = s.keys;
1297
+ var e = _e(n), r = It(n), s = Yt(n), o = s.args, i = s.keys;
1291
1298
  if (o.length === 0)
1292
1299
  return ce([], e);
1293
1300
  var a = new S(zt(o, e, i ? function(c) {
@@ -1300,9 +1307,9 @@ function zt(n, t, e) {
1300
1307
  ge(t, function() {
1301
1308
  for (var s = n.length, o = new Array(s), i = s, a = s, c = function(h) {
1302
1309
  ge(t, function() {
1303
- var m = ce(n[h], t), b = !1;
1310
+ var m = ce(n[h], t), v = !1;
1304
1311
  m.subscribe(A(r, function(d) {
1305
- o[h] = d, b || (b = !0, a--), a || r.next(e(o.slice()));
1312
+ o[h] = d, v || (v = !0, a--), a || r.next(e(o.slice()));
1306
1313
  }, function() {
1307
1314
  --i || r.complete();
1308
1315
  }));
@@ -1316,34 +1323,34 @@ function ge(n, t, e) {
1316
1323
  n ? O(e, n, t) : t();
1317
1324
  }
1318
1325
  function Jt(n, t, e, r, s, o, i, a) {
1319
- var c = [], u = 0, h = 0, m = !1, b = function() {
1326
+ var c = [], u = 0, h = 0, m = !1, v = function() {
1320
1327
  m && !c.length && !u && t.complete();
1321
1328
  }, d = function(f) {
1322
1329
  return u < r ? p(f) : c.push(f);
1323
1330
  }, p = function(f) {
1324
1331
  u++;
1325
- var _ = !1;
1332
+ var L = !1;
1326
1333
  U(e(f, h++)).subscribe(A(t, function(E) {
1327
1334
  t.next(E);
1328
1335
  }, function() {
1329
- _ = !0;
1336
+ L = !0;
1330
1337
  }, void 0, function() {
1331
- if (_)
1338
+ if (L)
1332
1339
  try {
1333
1340
  u--;
1334
1341
  for (var E = function() {
1335
- var L = c.shift();
1336
- i || p(L);
1342
+ var _ = c.shift();
1343
+ i || p(_);
1337
1344
  }; c.length && u < r; )
1338
1345
  E();
1339
- b();
1340
- } catch (L) {
1341
- t.error(L);
1346
+ v();
1347
+ } catch (_) {
1348
+ t.error(_);
1342
1349
  }
1343
1350
  }));
1344
1351
  };
1345
1352
  return n.subscribe(A(t, d, function() {
1346
- m = !0, b();
1353
+ m = !0, v();
1347
1354
  })), function() {
1348
1355
  };
1349
1356
  }
@@ -1431,15 +1438,15 @@ function be(n) {
1431
1438
  return new S((a) => {
1432
1439
  const c = new AbortController();
1433
1440
  let u, h = !1, m = !1;
1434
- const b = {
1441
+ const v = {
1435
1442
  "Content-Type": "application/json",
1436
1443
  ...o
1437
1444
  };
1438
- e && (b["X-API-KEY"] = e);
1445
+ e && (v["X-API-KEY"] = e);
1439
1446
  const d = new URL(t);
1440
1447
  return s && d.searchParams.set("is_debug", "true"), Ee(d.toString(), {
1441
1448
  method: i,
1442
- headers: b,
1449
+ headers: v,
1443
1450
  body: i === "POST" && r ? JSON.stringify(r) : void 0,
1444
1451
  signal: c.signal,
1445
1452
  /**
@@ -1978,7 +1985,7 @@ function fn(n, t, e) {
1978
1985
  function Ke(n, t, e) {
1979
1986
  return ve.randomUUID && !n ? ve.randomUUID() : fn(n);
1980
1987
  }
1981
- class v {
1988
+ class b {
1982
1989
  messages = null;
1983
1990
  pendingConsent = null;
1984
1991
  constructor({ messages: t, pendingConsent: e = null }) {
@@ -1986,10 +1993,18 @@ class v {
1986
1993
  }
1987
1994
  pushMessage(t) {
1988
1995
  const e = new Map(this.messages);
1989
- return e.set(t.messageId, t), new v({ messages: e, pendingConsent: this.pendingConsent });
1996
+ return e.set(t.messageId, t), new b({ messages: e, pendingConsent: this.pendingConsent });
1990
1997
  }
1991
1998
  clearPendingConsent() {
1992
- return this.pendingConsent ? new v({ messages: this.messages, pendingConsent: null }) : this;
1999
+ return this.pendingConsent ? new b({ messages: this.messages, pendingConsent: null }) : this;
2000
+ }
2001
+ /**
2002
+ * Put a consent prompt back (#410) — the mirror of {@link clearPendingConsent}, used to undo the
2003
+ * optimistic clear when the reply never reaches the server. Yields to a prompt that arrived in the
2004
+ * meantime: that one is newer than the batch being restored.
2005
+ */
2006
+ restorePendingConsent(t) {
2007
+ return this.pendingConsent ? this : new b({ messages: this.messages, pendingConsent: t });
1993
2008
  }
1994
2009
  /**
1995
2010
  * Converge everything the aborted run left mid-flight (F-020 AC10, F-023). A stopped run sends no
@@ -2010,7 +2025,7 @@ class v {
2010
2025
  const e = new Map(this.messages);
2011
2026
  for (const [r, s] of e)
2012
2027
  s.type === "tool-call" && !s.isComplete && (e.set(r, { ...s, isComplete: !0, isCancelled: !0 }), t = !0), s.type === "thinking" && s.isThinking && (e.set(r, { ...s, isThinking: !1 }), t = !0);
2013
- return t ? new v({ messages: e, pendingConsent: this.pendingConsent }) : this;
2028
+ return t ? new b({ messages: e, pendingConsent: this.pendingConsent }) : this;
2014
2029
  }
2015
2030
  /**
2016
2031
  * @deprecated Renamed to {@link settleInFlightMessages} in 0.3.27 — it settles in-flight thinking
@@ -2092,7 +2107,7 @@ class v {
2092
2107
  time: /* @__PURE__ */ new Date(),
2093
2108
  traceId: t.traceId,
2094
2109
  raw: ""
2095
- }), new v({ messages: r, pendingConsent: this.pendingConsent });
2110
+ }), new b({ messages: r, pendingConsent: this.pendingConsent });
2096
2111
  }
2097
2112
  onMessageDelta(t) {
2098
2113
  const e = t.fact.messageDelta.message;
@@ -2110,7 +2125,7 @@ class v {
2110
2125
  time: /* @__PURE__ */ new Date(),
2111
2126
  traceId: t.traceId ?? s?.traceId,
2112
2127
  raw: s?.raw ?? ""
2113
- }), new v({ messages: o, pendingConsent: this.pendingConsent });
2128
+ }), new b({ messages: o, pendingConsent: this.pendingConsent });
2114
2129
  }
2115
2130
  onMessageComplete(t) {
2116
2131
  const e = t.fact.messageComplete.message;
@@ -2126,7 +2141,7 @@ class v {
2126
2141
  time: /* @__PURE__ */ new Date(),
2127
2142
  traceId: t.traceId ?? (s?.type === "bot" ? s.traceId : void 0),
2128
2143
  raw: JSON.stringify(t)
2129
- }), new v({ messages: r, pendingConsent: this.pendingConsent });
2144
+ }), new b({ messages: r, pendingConsent: this.pendingConsent });
2130
2145
  }
2131
2146
  onThinkingStart(t) {
2132
2147
  const e = t.fact.messageThinkingStart.message;
@@ -2140,7 +2155,7 @@ class v {
2140
2155
  time: /* @__PURE__ */ new Date(),
2141
2156
  traceId: t.traceId
2142
2157
  };
2143
- return r.set(e.messageId, s), new v({ messages: r, pendingConsent: this.pendingConsent });
2158
+ return r.set(e.messageId, s), new b({ messages: r, pendingConsent: this.pendingConsent });
2144
2159
  }
2145
2160
  onThinkingDelta(t) {
2146
2161
  const e = t.fact.messageThinkingDelta.message;
@@ -2155,7 +2170,7 @@ class v {
2155
2170
  time: s?.time ?? /* @__PURE__ */ new Date(),
2156
2171
  traceId: t.traceId ?? s?.traceId
2157
2172
  };
2158
- return o.set(e.messageId, i), new v({ messages: o, pendingConsent: this.pendingConsent });
2173
+ return o.set(e.messageId, i), new b({ messages: o, pendingConsent: this.pendingConsent });
2159
2174
  }
2160
2175
  onThinkingComplete(t) {
2161
2176
  const e = t.fact.messageThinkingComplete.message;
@@ -2170,7 +2185,7 @@ class v {
2170
2185
  time: s?.time ?? /* @__PURE__ */ new Date(),
2171
2186
  traceId: t.traceId ?? s?.traceId
2172
2187
  };
2173
- return o.set(e.messageId, i), new v({ messages: o, pendingConsent: this.pendingConsent });
2188
+ return o.set(e.messageId, i), new b({ messages: o, pendingConsent: this.pendingConsent });
2174
2189
  }
2175
2190
  onMessageUser(t) {
2176
2191
  const e = t.fact.messageUser;
@@ -2185,7 +2200,7 @@ class v {
2185
2200
  time: /* @__PURE__ */ new Date(),
2186
2201
  traceId: t.traceId
2187
2202
  };
2188
- return s.set(e.messageId, o), new v({ messages: s, pendingConsent: this.pendingConsent });
2203
+ return s.set(e.messageId, o), new b({ messages: s, pendingConsent: this.pendingConsent });
2189
2204
  }
2190
2205
  onMessageError(t) {
2191
2206
  const e = Ke(), r = t.fact.runError.error, s = new Map(this.messages);
@@ -2196,7 +2211,7 @@ class v {
2196
2211
  error: r,
2197
2212
  time: /* @__PURE__ */ new Date(),
2198
2213
  traceId: t.traceId
2199
- }), new v({ messages: s, pendingConsent: this.pendingConsent });
2214
+ }), new b({ messages: s, pendingConsent: this.pendingConsent });
2200
2215
  }
2201
2216
  onToolCallStart(t) {
2202
2217
  const e = t.fact.toolCallStart, r = new Map(this.messages), s = `${e.processId}-${e.callSeq}`;
@@ -2217,7 +2232,7 @@ class v {
2217
2232
  time: /* @__PURE__ */ new Date(),
2218
2233
  traceId: t.traceId
2219
2234
  };
2220
- return r.set(s, o), new v({ messages: r, pendingConsent: this.pendingConsent });
2235
+ return r.set(s, o), new b({ messages: r, pendingConsent: this.pendingConsent });
2221
2236
  }
2222
2237
  onToolCallComplete(t) {
2223
2238
  const e = t.fact.toolCallComplete, r = new Map(this.messages), s = `${e.processId}-${e.callSeq}`, o = r.get(s);
@@ -2254,11 +2269,11 @@ class v {
2254
2269
  };
2255
2270
  r.set(s, i);
2256
2271
  }
2257
- return new v({ messages: r, pendingConsent: this.pendingConsent });
2272
+ return new b({ messages: r, pendingConsent: this.pendingConsent });
2258
2273
  }
2259
2274
  onToolCallConsent(t) {
2260
2275
  const e = t.fact.toolCallConsent;
2261
- return new v({ messages: this.messages, pendingConsent: e });
2276
+ return new b({ messages: this.messages, pendingConsent: e });
2262
2277
  }
2263
2278
  onSubagentStart(t) {
2264
2279
  const e = t.fact.subagentStart, r = new Map(this.messages), s = `subagent:${e.parentToolUseId}:start`, o = {
@@ -2272,7 +2287,7 @@ class v {
2272
2287
  time: /* @__PURE__ */ new Date(),
2273
2288
  traceId: t.traceId
2274
2289
  };
2275
- return r.delete(s), r.set(s, o), new v({ messages: r, pendingConsent: this.pendingConsent });
2290
+ return r.delete(s), r.set(s, o), new b({ messages: r, pendingConsent: this.pendingConsent });
2276
2291
  }
2277
2292
  onSubagentComplete(t) {
2278
2293
  const e = t.fact.subagentComplete, r = new Map(this.messages), s = `subagent:${e.parentToolUseId}:complete`, o = {
@@ -2287,7 +2302,7 @@ class v {
2287
2302
  time: /* @__PURE__ */ new Date(),
2288
2303
  traceId: t.traceId
2289
2304
  };
2290
- return r.delete(s), r.set(s, o), new v({ messages: r, pendingConsent: this.pendingConsent });
2305
+ return r.delete(s), r.set(s, o), new b({ messages: r, pendingConsent: this.pendingConsent });
2291
2306
  }
2292
2307
  }
2293
2308
  const pn = /* @__PURE__ */ new Set(["TaskCreate", "TaskUpdate"]);
@@ -2710,7 +2725,7 @@ class q {
2710
2725
  ...i,
2711
2726
  traceId: s.traceId
2712
2727
  }), this.conversation$.next(
2713
- new v({ messages: o, pendingConsent: this.conversation$.value.pendingConsent })
2728
+ new b({ messages: o, pendingConsent: this.conversation$.value.pendingConsent })
2714
2729
  )), this.currentUserMessageId = void 0;
2715
2730
  }
2716
2731
  this.conversation$.next(this.conversation$.value.onMessage(s));
@@ -2806,6 +2821,7 @@ class q {
2806
2821
  );
2807
2822
  }
2808
2823
  replyToolCallConsents(t, e, r) {
2824
+ const s = this.conversation$.value.pendingConsent;
2809
2825
  return this.conversation$.next(this.conversation$.value.clearPendingConsent()), this.fetchSse(
2810
2826
  "user",
2811
2827
  {
@@ -2817,7 +2833,9 @@ class q {
2817
2833
  toolCallConsents: t
2818
2834
  },
2819
2835
  e
2820
- );
2836
+ ).catch((o) => {
2837
+ throw s && this.conversation$.next(this.conversation$.value.restorePendingConsent(s)), o;
2838
+ });
2821
2839
  }
2822
2840
  /**
2823
2841
  * Nudge an idle / recycled sandbox back to life (F-021 AC4). Sends an `action=NUDGE` turn with empty
@@ -2945,7 +2963,7 @@ export {
2945
2963
  q as Channel,
2946
2964
  ee as ChannelAwaitingConsentError,
2947
2965
  Z as ChannelBusyError,
2948
- v as Conversation,
2966
+ b as Conversation,
2949
2967
  l as EventType,
2950
2968
  $ as FetchSseAction,
2951
2969
  w as HttpError,
@@ -2957,8 +2975,8 @@ export {
2957
2975
  En as deriveTasks,
2958
2976
  bn as isAgentTool,
2959
2977
  Un as isChannelAwaitingConsentError,
2960
- Ln as isChannelBusyError,
2961
- _n as isHttpError,
2978
+ _n as isChannelBusyError,
2979
+ Ln as isHttpError,
2962
2980
  vn as isSubagentChildTool,
2963
2981
  mn as isTaskTool,
2964
2982
  we as reconcileLaunched,