@mastra/ai-sdk 1.1.0 → 1.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed message history and semantic recall persistence after AI SDK streams finish. ([#13297](https://github.com/mastra-ai/mastra/pull/13297))
8
+
9
+ - Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
10
+ - @mastra/core@1.9.0
11
+
12
+ ## 1.1.1-alpha.0
13
+
14
+ ### Patch Changes
15
+
16
+ - Fixed message history and semantic recall persistence after AI SDK streams finish. ([#13297](https://github.com/mastra-ai/mastra/pull/13297))
17
+
18
+ - Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
19
+ - @mastra/core@1.9.0-alpha.0
20
+
3
21
  ## 1.1.0
4
22
 
5
23
  ### Minor Changes
package/LICENSE.md CHANGED
@@ -1,3 +1,18 @@
1
+ Portions of this software are licensed as follows:
2
+
3
+ - All content that resides under any directory named "ee/" within this
4
+ repository, including but not limited to:
5
+ - `packages/core/src/auth/ee/`
6
+ - `packages/server/src/server/auth/ee/`
7
+ is licensed under the license defined in `ee/LICENSE`.
8
+
9
+ - All third-party components incorporated into the Mastra Software are
10
+ licensed under the original license provided by the owner of the
11
+ applicable component.
12
+
13
+ - Content outside of the above-mentioned directories or restrictions is
14
+ available under the "Apache License 2.0" as defined below.
15
+
1
16
  # Apache License 2.0
2
17
 
3
18
  Copyright (c) 2025 Kepler Software, Inc.
package/dist/index.cjs CHANGED
@@ -4637,6 +4637,142 @@ function withMastra(model, options = {}) {
4637
4637
  })
4638
4638
  });
4639
4639
  }
4640
+ var StreamOutputAccumulator = class {
4641
+ /** Ordered sequence of part placeholders: either a text buffer index or a tool call ID */
4642
+ partOrder = [];
4643
+ textBuffers = [];
4644
+ toolStates = /* @__PURE__ */ new Map();
4645
+ addChunk(chunk) {
4646
+ switch (chunk.type) {
4647
+ case "text-delta":
4648
+ if (chunk.payload.text) {
4649
+ this.appendTextDelta(chunk.payload.text);
4650
+ }
4651
+ return;
4652
+ case "tool-call-input-streaming-start": {
4653
+ const state = this.ensureToolState(chunk.payload.toolCallId);
4654
+ state.toolName = state.toolName || chunk.payload.toolName;
4655
+ state.providerMetadata = chunk.payload.providerMetadata || state.providerMetadata;
4656
+ if (!state.argDeltas) {
4657
+ state.argDeltas = [];
4658
+ }
4659
+ return;
4660
+ }
4661
+ case "tool-call-delta": {
4662
+ const state = this.ensureToolState(chunk.payload.toolCallId);
4663
+ if (chunk.payload.argsTextDelta) {
4664
+ if (!state.argDeltas) state.argDeltas = [];
4665
+ state.argDeltas.push(chunk.payload.argsTextDelta);
4666
+ }
4667
+ return;
4668
+ }
4669
+ case "tool-call-input-streaming-end": {
4670
+ const state = this.ensureToolState(chunk.payload.toolCallId);
4671
+ this.finalizeToolArgs(state);
4672
+ return;
4673
+ }
4674
+ case "tool-call": {
4675
+ const state = this.ensureToolState(chunk.payload.toolCallId);
4676
+ state.toolName = state.toolName || chunk.payload.toolName;
4677
+ if (chunk.payload.args !== void 0) {
4678
+ state.args = chunk.payload.args;
4679
+ } else {
4680
+ this.finalizeToolArgs(state);
4681
+ }
4682
+ state.providerMetadata = chunk.payload.providerMetadata || state.providerMetadata;
4683
+ return;
4684
+ }
4685
+ case "tool-result": {
4686
+ const state = this.ensureToolState(chunk.payload.toolCallId);
4687
+ state.toolName = state.toolName || chunk.payload.toolName;
4688
+ state.result = chunk.payload.result;
4689
+ if (state.args === void 0) {
4690
+ this.finalizeToolArgs(state);
4691
+ }
4692
+ state.providerMetadata = chunk.payload.providerMetadata || state.providerMetadata;
4693
+ return;
4694
+ }
4695
+ default:
4696
+ return;
4697
+ }
4698
+ }
4699
+ buildResponseMessage(memory) {
4700
+ if (this.partOrder.length === 0) {
4701
+ return null;
4702
+ }
4703
+ const parts = [];
4704
+ const textSegments = [];
4705
+ for (const entry of this.partOrder) {
4706
+ if (entry.kind === "text") {
4707
+ const text2 = this.textBuffers[entry.bufferIndex].join("");
4708
+ parts.push({ type: "text", text: text2 });
4709
+ textSegments.push(text2);
4710
+ } else {
4711
+ const state = this.toolStates.get(entry.toolCallId);
4712
+ this.finalizeToolArgs(state);
4713
+ parts.push(this.buildToolPart(state));
4714
+ }
4715
+ }
4716
+ const textContent = textSegments.join("");
4717
+ const content = {
4718
+ format: 2,
4719
+ parts,
4720
+ ...textContent ? { content: textContent } : {}
4721
+ };
4722
+ return {
4723
+ id: crypto.randomUUID(),
4724
+ role: "assistant",
4725
+ content,
4726
+ createdAt: /* @__PURE__ */ new Date(),
4727
+ ...memory?.threadId && { threadId: memory.threadId },
4728
+ ...memory?.resourceId && { resourceId: memory.resourceId }
4729
+ };
4730
+ }
4731
+ ensureToolState(toolCallId) {
4732
+ let state = this.toolStates.get(toolCallId);
4733
+ if (!state) {
4734
+ state = { toolCallId };
4735
+ this.toolStates.set(toolCallId, state);
4736
+ this.partOrder.push({ kind: "tool", toolCallId });
4737
+ }
4738
+ return state;
4739
+ }
4740
+ finalizeToolArgs(state) {
4741
+ if (state.args !== void 0 || !state.argDeltas?.length) {
4742
+ return;
4743
+ }
4744
+ try {
4745
+ state.args = JSON.parse(state.argDeltas.join(""));
4746
+ } catch {
4747
+ return;
4748
+ }
4749
+ }
4750
+ appendTextDelta(text2) {
4751
+ const last = this.partOrder[this.partOrder.length - 1];
4752
+ if (last?.kind === "text") {
4753
+ this.textBuffers[last.bufferIndex].push(text2);
4754
+ return;
4755
+ }
4756
+ const bufferIndex = this.textBuffers.length;
4757
+ this.textBuffers.push([text2]);
4758
+ this.partOrder.push({ kind: "text", bufferIndex });
4759
+ }
4760
+ buildToolPart(state) {
4761
+ const hasResult = state.result !== void 0;
4762
+ const toolInvocation = {
4763
+ state: hasResult ? "result" : "call",
4764
+ toolCallId: state.toolCallId,
4765
+ toolName: state.toolName || "unknown",
4766
+ args: state.args ?? {},
4767
+ ...hasResult ? { result: state.result } : {}
4768
+ };
4769
+ return {
4770
+ type: "tool-invocation",
4771
+ toolInvocation,
4772
+ ...state.providerMetadata ? { providerMetadata: state.providerMetadata } : {}
4773
+ };
4774
+ }
4775
+ };
4640
4776
  function createProcessorMiddleware(options) {
4641
4777
  const { inputProcessors = [], outputProcessors = [], memory } = options;
4642
4778
  const requestContext = new di.RequestContext();
@@ -4758,6 +4894,7 @@ function createProcessorMiddleware(options) {
4758
4894
  await processor.processOutputResult({
4759
4895
  messages: messageList.get.all.db(),
4760
4896
  messageList,
4897
+ state: {},
4761
4898
  requestContext,
4762
4899
  abort: (reason) => {
4763
4900
  throw new agent.TripWire(reason || "Aborted by processor");
@@ -4795,8 +4932,12 @@ function createProcessorMiddleware(options) {
4795
4932
  }
4796
4933
  const { stream: stream$1, ...rest } = await doStream();
4797
4934
  if (!outputProcessors.length) return { stream: stream$1, ...rest };
4935
+ const outputResultProcessors = outputProcessors.filter((processor) => processor.processOutputResult);
4936
+ const streamAccumulator = outputResultProcessors.length ? new StreamOutputAccumulator() : null;
4798
4937
  const processorStates = /* @__PURE__ */ new Map();
4799
4938
  const runId = crypto.randomUUID();
4939
+ let streamAborted = false;
4940
+ let sawFinish = false;
4800
4941
  const transformedStream = stream$1.pipeThrough(
4801
4942
  new web.TransformStream({
4802
4943
  async transform(chunk, controller) {
@@ -4833,6 +4974,7 @@ function createProcessorMiddleware(options) {
4833
4974
  }
4834
4975
  } catch (error) {
4835
4976
  if (error instanceof agent.TripWire) {
4977
+ streamAborted = true;
4836
4978
  controller.enqueue({
4837
4979
  type: "error",
4838
4980
  error: new Error(error.message)
@@ -4844,12 +4986,69 @@ function createProcessorMiddleware(options) {
4844
4986
  }
4845
4987
  }
4846
4988
  }
4989
+ if (mastraChunk) {
4990
+ if (mastraChunk.type === "finish") {
4991
+ sawFinish = true;
4992
+ }
4993
+ if (streamAccumulator) {
4994
+ streamAccumulator.addChunk(mastraChunk);
4995
+ }
4996
+ }
4847
4997
  if (mastraChunk) {
4848
4998
  const aiChunk = convertMastraChunkToAISDKStreamPart(mastraChunk);
4849
4999
  if (aiChunk) {
4850
5000
  controller.enqueue(aiChunk);
4851
5001
  }
4852
5002
  }
5003
+ },
5004
+ async flush(controller) {
5005
+ if (!streamAccumulator || streamAborted || !sawFinish) {
5006
+ return;
5007
+ }
5008
+ const messageList = new agent.MessageList({
5009
+ threadId: memory?.threadId,
5010
+ resourceId: memory?.resourceId
5011
+ });
5012
+ const flushOriginalInputCount = processorState?.originalInputCount ?? params.prompt.filter((m) => m.role !== "system").length;
5013
+ const flushNonSystemTotal = params.prompt.filter((m) => m.role !== "system").length;
5014
+ const flushMemoryCount = flushNonSystemTotal - flushOriginalInputCount;
5015
+ let flushNonSystemIndex = 0;
5016
+ for (const msg of params.prompt) {
5017
+ if (msg.role === "system") {
5018
+ messageList.addSystem(msg.content);
5019
+ } else {
5020
+ messageList.add(msg, flushNonSystemIndex < flushMemoryCount ? "memory" : "input");
5021
+ flushNonSystemIndex++;
5022
+ }
5023
+ }
5024
+ const responseMessage = streamAccumulator.buildResponseMessage(memory);
5025
+ if (responseMessage) {
5026
+ messageList.add(responseMessage, "response");
5027
+ }
5028
+ for (const processor of outputResultProcessors) {
5029
+ if (!processor.processOutputResult) continue;
5030
+ try {
5031
+ const procState = processorStates.get(processor.id);
5032
+ await processor.processOutputResult({
5033
+ messages: messageList.get.all.db(),
5034
+ messageList,
5035
+ state: procState?.customState ?? {},
5036
+ requestContext,
5037
+ abort: (reason) => {
5038
+ throw new agent.TripWire(reason || "Aborted by processor");
5039
+ }
5040
+ });
5041
+ } catch (error) {
5042
+ if (error instanceof agent.TripWire) {
5043
+ controller.enqueue({
5044
+ type: "error",
5045
+ error: new Error(error.message)
5046
+ });
5047
+ return;
5048
+ }
5049
+ throw error;
5050
+ }
5051
+ }
4853
5052
  }
4854
5053
  })
4855
5054
  );