@syntrologie/adapt-chatbot 2.8.0-canary.294 → 2.8.0-canary.296

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,8 +1,8 @@
1
1
  import {
2
2
  AdaptiveChatBar
3
3
  } from "./chunk-SCVTTLFJ.js";
4
- import "./chunk-IGRIDG7O.js";
5
- import "./chunk-NLQR3MGO.js";
4
+ import "./chunk-TIHH27JQ.js";
5
+ import "./chunk-ZZUKH2D6.js";
6
6
  import "./chunk-UVKRO5ER.js";
7
7
  export {
8
8
  AdaptiveChatBar
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  AdaptiveChatTrail
3
- } from "./chunk-IGRIDG7O.js";
4
- import "./chunk-NLQR3MGO.js";
3
+ } from "./chunk-TIHH27JQ.js";
4
+ import "./chunk-ZZUKH2D6.js";
5
5
  import "./chunk-UVKRO5ER.js";
6
6
  export {
7
7
  AdaptiveChatTrail
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  ChatAssistantLitMountable,
3
3
  renderFallbackHtml
4
- } from "./chunk-FLSMUFFX.js";
5
- import "./chunk-NLQR3MGO.js";
4
+ } from "./chunk-ES5JYJA7.js";
5
+ import "./chunk-ZZUKH2D6.js";
6
6
  import "./chunk-VLJ3WOEX.js";
7
7
  import "./chunk-UVKRO5ER.js";
8
8
  export {
@@ -7324,6 +7324,7 @@ var AgUiTransport = class {
7324
7324
  this._handlers = /* @__PURE__ */ new Set();
7325
7325
  this._pendingResume = false;
7326
7326
  this._currentAssistantMessageId = null;
7327
+ this._toolCallMessageIds = /* @__PURE__ */ new Map();
7327
7328
  this._url = options.url;
7328
7329
  this._headers = options.headers ?? {};
7329
7330
  this._threadId = options.threadId;
@@ -7331,6 +7332,7 @@ var AgUiTransport = class {
7331
7332
  this._credentials = options.credentials;
7332
7333
  this._onA2UIEvent = options.onA2UIEvent;
7333
7334
  this._forwardedProps = options.forwardedProps;
7335
+ this._serverOwnedHistory = options.serverOwnedHistory ?? false;
7334
7336
  }
7335
7337
  get connected() {
7336
7338
  return this._connected;
@@ -7362,6 +7364,8 @@ var AgUiTransport = class {
7362
7364
  this._agent = null;
7363
7365
  this._connected = false;
7364
7366
  this._pendingResume = false;
7367
+ this._currentAssistantMessageId = null;
7368
+ this._toolCallMessageIds.clear();
7365
7369
  }
7366
7370
  send(action) {
7367
7371
  if (!this._agent) return;
@@ -7391,7 +7395,9 @@ var AgUiTransport = class {
7391
7395
  // ---------------------------------------------------------------------------
7392
7396
  _sendUserMessage(text) {
7393
7397
  const agent = this._agent;
7394
- const pendingInterrupts = agent.pendingInterrupts ?? [];
7398
+ const pendingInterrupts = (agent.pendingInterrupts ?? []).map((i) => i).filter(
7399
+ (i) => typeof i?.id === "string" && i.id.length > 0
7400
+ );
7395
7401
  const cancelResume = pendingInterrupts.length > 0 ? pendingInterrupts.map((i) => {
7396
7402
  const toolCallId = i.toolCallId ?? i.tool_call_id;
7397
7403
  if (toolCallId) {
@@ -7461,22 +7467,80 @@ var AgUiTransport = class {
7461
7467
  if (typeof this._headers === "function") {
7462
7468
  agent.headers = this._headers();
7463
7469
  }
7470
+ const sentMessageIds = new Set((agent.messages ?? []).map((m) => m.id));
7464
7471
  const fwd = typeof this._forwardedProps === "function" ? this._forwardedProps() : this._forwardedProps;
7465
7472
  const runParams = {
7466
7473
  ...fwd ? { forwardedProps: fwd } : {},
7467
7474
  ...resume && resume.length > 0 ? { resume: [...resume] } : {}
7468
7475
  };
7469
- agent.runAgent(runParams, this._buildSubscriber()).catch((err) => {
7476
+ const runPromise = agent.runAgent(runParams, this._buildSubscriber());
7477
+ runPromise.then(() => {
7478
+ this._resetAgentMessages(agent, sentMessageIds);
7479
+ }).catch(() => {
7480
+ });
7481
+ runPromise.catch((err) => {
7470
7482
  const message = err instanceof Error ? err.message : "Agent run failed";
7471
7483
  const errorName = err instanceof Error ? err.name : null;
7472
7484
  const e = err;
7473
7485
  const status = typeof e.status === "number" ? e.status : typeof e.statusCode === "number" ? e.statusCode : typeof e.response?.status === "number" ? e.response.status : null;
7474
7486
  const rawBody = e.body ?? e.response?.body ?? null;
7475
7487
  const body = typeof rawBody === "string" ? rawBody.slice(0, 500) : rawBody != null ? JSON.stringify(rawBody).slice(0, 500) : null;
7488
+ this._pendingResume = false;
7489
+ this._currentAssistantMessageId = null;
7490
+ this._resetAgentMessages(agent, sentMessageIds);
7476
7491
  this._emit({ type: "error", message, status, body, errorName });
7477
7492
  this._emit({ type: "typing", isTyping: false });
7478
7493
  });
7479
7494
  }
7495
+ /**
7496
+ * Reset `agent.messages` after a settled run (server-owned history).
7497
+ *
7498
+ * The backend owns conversation state (`chat_threads.native_messages`);
7499
+ * the agent object is only a request builder, so anything that already
7500
+ * shipped in a run is dropped. The ONLY retention is undelivered
7501
+ * client-tool state: an assistant message carrying `toolCalls` for a
7502
+ * tool in `clientTools` whose result either doesn't exist yet (the UI
7503
+ * is still computing/asking) or was queued mid-run and never shipped
7504
+ * (`_sendToolResult` while `isRunning`). The retained pair makes the
7505
+ * next run's wire self-consistent — a `role: "tool"` message without
7506
+ * its matching call in the SAME client list is dropped by the backend
7507
+ * filter and the result would be lost.
7508
+ *
7509
+ * No-op unless `serverOwnedHistory` is set: the adaptive runtime and
7510
+ * editor chat backends still build the model conversation from the
7511
+ * client's `messages` array.
7512
+ */
7513
+ _resetAgentMessages(agent, sentMessageIds) {
7514
+ if (!this._serverOwnedHistory) return;
7515
+ if (agent !== this._agent) return;
7516
+ const messages = agent.messages ?? [];
7517
+ const resultsByCallId = /* @__PURE__ */ new Map();
7518
+ for (const msg of messages) {
7519
+ if (msg.role === "tool" && typeof msg.toolCallId === "string") {
7520
+ resultsByCallId.set(msg.toolCallId, msg);
7521
+ }
7522
+ }
7523
+ const retained = [];
7524
+ for (const msg of messages) {
7525
+ if (msg.role !== "assistant" || !Array.isArray(msg.toolCalls)) continue;
7526
+ const pendingCalls = msg.toolCalls.filter((tc) => {
7527
+ if (!this._clientTools.has(tc.function?.name)) return false;
7528
+ const result = resultsByCallId.get(tc.id);
7529
+ return result === void 0 || !sentMessageIds.has(result.id);
7530
+ });
7531
+ if (pendingCalls.length === 0) continue;
7532
+ retained.push({ ...msg, toolCalls: pendingCalls });
7533
+ for (const tc of pendingCalls) {
7534
+ const result = resultsByCallId.get(tc.id);
7535
+ if (result !== void 0) retained.push(result);
7536
+ }
7537
+ }
7538
+ if (typeof agent.setMessages === "function") {
7539
+ agent.setMessages(retained);
7540
+ } else {
7541
+ agent.messages = retained;
7542
+ }
7543
+ }
7480
7544
  _buildSubscriber() {
7481
7545
  return {
7482
7546
  onTextMessageStartEvent: (params) => {
@@ -7506,6 +7570,7 @@ var AgUiTransport = class {
7506
7570
  onToolCallStartEvent: (params) => {
7507
7571
  const { event } = params;
7508
7572
  const messageId = event.parentMessageId ?? this._currentAssistantMessageId ?? event.toolCallId;
7573
+ this._toolCallMessageIds.set(event.toolCallId, messageId);
7509
7574
  this._emit({
7510
7575
  type: "tool-call",
7511
7576
  messageId,
@@ -7528,8 +7593,10 @@ var AgUiTransport = class {
7528
7593
  },
7529
7594
  onToolCallEndEvent: (params) => {
7530
7595
  const { event, toolCallName, toolCallArgs } = params;
7596
+ const startMessageId = this._toolCallMessageIds.get(event.toolCallId);
7597
+ this._toolCallMessageIds.delete(event.toolCallId);
7531
7598
  if (this._clientTools.has(toolCallName)) {
7532
- const messageId = this._currentAssistantMessageId ?? event.toolCallId;
7599
+ const messageId = startMessageId ?? this._currentAssistantMessageId ?? event.toolCallId;
7533
7600
  this._emit({
7534
7601
  type: "tool-call",
7535
7602
  messageId,
@@ -7599,6 +7666,7 @@ var AgUiTransport = class {
7599
7666
  // Private — emit to subscribers
7600
7667
  // ---------------------------------------------------------------------------
7601
7668
  _emit(event) {
7669
+ if (!this._connected) return;
7602
7670
  for (const handler of this._handlers) {
7603
7671
  handler(event);
7604
7672
  }
@@ -8684,4 +8752,4 @@ fast-json-patch/module/duplex.mjs:
8684
8752
  * MIT license
8685
8753
  *)
8686
8754
  */
8687
- //# sourceMappingURL=chunk-FLSMUFFX.js.map
8755
+ //# sourceMappingURL=chunk-ES5JYJA7.js.map