@dudousxd/nestjs-agent 0.3.2 → 0.3.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.
@@ -140,6 +140,17 @@ interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
140
140
  * the runner. Requires importing `AgentDurableModule` and a configured `DurableModule`.
141
141
  */
142
142
  durable?: boolean;
143
+ /**
144
+ * Set when `AGENT_STORE` is bound by a globally-imported store module (e.g.
145
+ * `MikroOrmAgentStoreModule.forFeature()`) instead of returned as `store` from `useFactory`.
146
+ *
147
+ * `forRoot` decides this synchronously (`store` present or not), but the async factory resolves
148
+ * too late to inspect — so by default `forRootAsync` binds `AGENT_STORE` from the factory result,
149
+ * which would shadow a global store binding with `undefined` when the factory omits `store`. Set
150
+ * this to `true` to skip the local binding and defer to the global one. Leave it unset (and return
151
+ * `store` from the factory) when the store is constructed inside the factory.
152
+ */
153
+ externalStore?: boolean;
143
154
  }
144
155
 
145
156
  /** The synthesized `agent`-kind tool name an orchestrator uses to hand off to `target`. */
@@ -140,6 +140,17 @@ interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
140
140
  * the runner. Requires importing `AgentDurableModule` and a configured `DurableModule`.
141
141
  */
142
142
  durable?: boolean;
143
+ /**
144
+ * Set when `AGENT_STORE` is bound by a globally-imported store module (e.g.
145
+ * `MikroOrmAgentStoreModule.forFeature()`) instead of returned as `store` from `useFactory`.
146
+ *
147
+ * `forRoot` decides this synchronously (`store` present or not), but the async factory resolves
148
+ * too late to inspect — so by default `forRootAsync` binds `AGENT_STORE` from the factory result,
149
+ * which would shadow a global store binding with `undefined` when the factory omits `store`. Set
150
+ * this to `true` to skip the local binding and defer to the global one. Leave it unset (and return
151
+ * `store` from the factory) when the store is constructed inside the factory.
152
+ */
153
+ externalStore?: boolean;
143
154
  }
144
155
 
145
156
  /** The synthesized `agent`-kind tool name an orchestrator uses to hand off to `target`. */
@@ -151,7 +151,9 @@ AgentRunWorkflow = _ts_decorate([
151
151
  ], AgentRunWorkflow);
152
152
 
153
153
  // src/durable/durable-agent-runner.ts
154
+ var import_node_crypto = require("crypto");
154
155
  var import_nestjs_durable2 = require("@dudousxd/nestjs-durable");
156
+ var import_nestjs_durable_core2 = require("@dudousxd/nestjs-durable-core");
155
157
  var import_common2 = require("@nestjs/common");
156
158
  function _ts_decorate2(decorators, target, key, desc) {
157
159
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -177,9 +179,16 @@ var DurableAgentRunner = class {
177
179
  ...input,
178
180
  day: input.day ?? utcDay()
179
181
  };
180
- const result = await this.workflows.start(AgentRunWorkflow, stamped);
182
+ const runId = (0, import_node_crypto.randomUUID)();
183
+ try {
184
+ await this.workflows.start(AgentRunWorkflow, stamped, runId);
185
+ } catch (error) {
186
+ if (!(error instanceof import_nestjs_durable_core2.WorkflowSuspended)) {
187
+ throw error;
188
+ }
189
+ }
181
190
  return {
182
- runId: result.runId
191
+ runId
183
192
  };
184
193
  }
185
194
  async signal(runId, toolCallId, decision) {
@@ -454,6 +463,9 @@ var AgentService = class {
454
463
  ...params.regenerate === true ? {
455
464
  regenerate: true
456
465
  } : {},
466
+ ...params.attachments !== void 0 ? {
467
+ attachments: params.attachments
468
+ } : {},
457
469
  ...params.pageContext !== void 0 ? {
458
470
  pageContext: params.pageContext
459
471
  } : {}
@@ -708,6 +720,9 @@ var ChatController = class {
708
720
  ...body.agent !== void 0 ? {
709
721
  agentName: body.agent
710
722
  } : {},
723
+ ...body.attachments !== void 0 ? {
724
+ attachments: body.attachments
725
+ } : {},
711
726
  ...body.pageContext !== void 0 ? {
712
727
  pageContext: body.pageContext
713
728
  } : {},
@@ -746,11 +761,26 @@ data: ${JSON.stringify({
746
761
 
747
762
  `);
748
763
  const decoder = new TextDecoder();
764
+ let buffer = "";
749
765
  try {
750
766
  for await (const chunk of this.agent.subscribe(runId)) {
751
- res.write(`data: ${JSON.stringify({
752
- delta: decoder.decode(chunk)
753
- })}
767
+ buffer += decoder.decode(chunk, {
768
+ stream: true
769
+ });
770
+ let newline = buffer.indexOf("\n");
771
+ while (newline !== -1) {
772
+ const line = buffer.slice(0, newline);
773
+ buffer = buffer.slice(newline + 1);
774
+ if (line.length > 0) {
775
+ res.write(`data: ${line}
776
+
777
+ `);
778
+ }
779
+ newline = buffer.indexOf("\n");
780
+ }
781
+ }
782
+ if (buffer.length > 0) {
783
+ res.write(`data: ${buffer}
754
784
 
755
785
  `);
756
786
  }
@@ -1809,6 +1839,7 @@ var AgentModule = class _AgentModule {
1809
1839
  }
1810
1840
  static forRootAsync(options) {
1811
1841
  const path = options.path ?? DEFAULT_PATH;
1842
+ const includeStore = options.externalStore !== true;
1812
1843
  return {
1813
1844
  module: _AgentModule,
1814
1845
  global: true,
@@ -1824,11 +1855,9 @@ var AgentModule = class _AgentModule {
1824
1855
  useFactory: options.useFactory,
1825
1856
  inject: options.inject ?? []
1826
1857
  },
1827
- // The async factory resolves too late to inspect `store`, so we always bind AGENT_STORE here
1828
- // (its factory reads the resolved options). Omit-and-import-a-store-module is a `forRoot` path.
1829
- ...sharedProviders(options.durable ?? false, true)
1858
+ ...sharedProviders(options.durable ?? false, includeStore)
1830
1859
  ],
1831
- exports: exportsFor(true)
1860
+ exports: exportsFor(includeStore)
1832
1861
  };
1833
1862
  }
1834
1863
  };