@dudousxd/nestjs-agent 1.17.1 → 1.18.0

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.
@@ -130,6 +130,24 @@ declare class LedgerQuotaProvider implements QuotaProvider {
130
130
  private window;
131
131
  }
132
132
 
133
+ /**
134
+ * A wire protocol other than the native one, served over the same runs — what `adapters` on
135
+ * `AgentModule.forRoot` takes. An adapter contributes controllers, mounted under the agent's
136
+ * `path` and guarded by its `guards` exactly like the native routes; the runs, the store, the
137
+ * approvals and the quota are the library's, unchanged.
138
+ *
139
+ * `agUiAdapter()` is the one that ships. Write your own the same way: a controller that injects
140
+ * `AgentService` (and `AGENT_ACTOR_RESOLVER`) and translates.
141
+ */
142
+ interface AgentProtocolAdapter {
143
+ /** For logs and diagnostics. */
144
+ name: string;
145
+ /** Mounted under the agent's path. Not mounted at all on `surface: 'engine'`. */
146
+ controllers: Type<object>[];
147
+ /** Extra providers the controllers inject. */
148
+ providers?: Provider[];
149
+ }
150
+
133
151
  /**
134
152
  * Which half of the module this process mounts — the split that lets an API pod and a WORKER pod
135
153
  * each load `AgentModule` without either one doing the other's job:
@@ -432,6 +450,12 @@ interface AgentModuleOptions {
432
450
  * which controllers exist at module-build time, not something a request can flip.
433
451
  */
434
452
  surface?: AgentSurface;
453
+ /**
454
+ * Wire protocols served alongside the native one, over the same runs — e.g. `[agUiAdapter()]`
455
+ * mounts `POST <path>/ag-ui` (AG-UI 1.0). Their controllers mount under `path`, behind `guards`,
456
+ * and not at all on `surface: 'engine'`. STATIC, like `surface`.
457
+ */
458
+ adapters?: AgentProtocolAdapter[];
435
459
  }
436
460
  interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
437
461
  inject?: Array<InjectionToken | OptionalFactoryDependency>;
@@ -462,6 +486,8 @@ interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
462
486
  * which controllers exist. Omit → `'both'`, today's behavior with zero change.
463
487
  */
464
488
  surface?: AgentSurface;
489
+ /** Wire protocols served alongside the native one — see `AgentModuleOptions.adapters`. STATIC. */
490
+ adapters?: AgentProtocolAdapter[];
465
491
  }
466
492
 
467
493
  /**
@@ -642,4 +668,4 @@ declare class ChatQueueService {
642
668
  private freshAttachments;
643
669
  }
644
670
 
645
- export { AgentDepsFactory as A, ChatQueueService as C, type FunctionalTool as F, LedgerQuotaProvider as L, type QueuePlan as Q, type AgentSurface as a, type AgentModuleOptions as b, type AgentModuleAsyncOptions as c, type AgentSkillsOptions as d, AGENT_TOOL_BRAND as e, type AgentAttachmentsOptions as f, type AgentDeps as g, type AgentMemoryOptions as h, type QueueSettleOutcome as i, type QueuedTurn as j, type QueuedTurnStarter as k, type QuotaLimits as l, type QuotaWindowLimits as m, delegateToolName as n, provideAgentTools as o, provideAgentTool as p, utcDay as u };
671
+ export { AgentDepsFactory as A, ChatQueueService as C, type FunctionalTool as F, LedgerQuotaProvider as L, type QueuePlan as Q, type AgentSurface as a, type AgentModuleOptions as b, type AgentModuleAsyncOptions as c, type AgentProtocolAdapter as d, type AgentSkillsOptions as e, AGENT_TOOL_BRAND as f, type AgentAttachmentsOptions as g, type AgentDeps as h, type AgentMemoryOptions as i, type QueueSettleOutcome as j, type QueuedTurn as k, type QueuedTurnStarter as l, type QuotaLimits as m, type QuotaWindowLimits as n, delegateToolName as o, provideAgentTool as p, provideAgentTools as q, utcDay as u };
@@ -130,6 +130,24 @@ declare class LedgerQuotaProvider implements QuotaProvider {
130
130
  private window;
131
131
  }
132
132
 
133
+ /**
134
+ * A wire protocol other than the native one, served over the same runs — what `adapters` on
135
+ * `AgentModule.forRoot` takes. An adapter contributes controllers, mounted under the agent's
136
+ * `path` and guarded by its `guards` exactly like the native routes; the runs, the store, the
137
+ * approvals and the quota are the library's, unchanged.
138
+ *
139
+ * `agUiAdapter()` is the one that ships. Write your own the same way: a controller that injects
140
+ * `AgentService` (and `AGENT_ACTOR_RESOLVER`) and translates.
141
+ */
142
+ interface AgentProtocolAdapter {
143
+ /** For logs and diagnostics. */
144
+ name: string;
145
+ /** Mounted under the agent's path. Not mounted at all on `surface: 'engine'`. */
146
+ controllers: Type<object>[];
147
+ /** Extra providers the controllers inject. */
148
+ providers?: Provider[];
149
+ }
150
+
133
151
  /**
134
152
  * Which half of the module this process mounts — the split that lets an API pod and a WORKER pod
135
153
  * each load `AgentModule` without either one doing the other's job:
@@ -432,6 +450,12 @@ interface AgentModuleOptions {
432
450
  * which controllers exist at module-build time, not something a request can flip.
433
451
  */
434
452
  surface?: AgentSurface;
453
+ /**
454
+ * Wire protocols served alongside the native one, over the same runs — e.g. `[agUiAdapter()]`
455
+ * mounts `POST <path>/ag-ui` (AG-UI 1.0). Their controllers mount under `path`, behind `guards`,
456
+ * and not at all on `surface: 'engine'`. STATIC, like `surface`.
457
+ */
458
+ adapters?: AgentProtocolAdapter[];
435
459
  }
436
460
  interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
437
461
  inject?: Array<InjectionToken | OptionalFactoryDependency>;
@@ -462,6 +486,8 @@ interface AgentModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
462
486
  * which controllers exist. Omit → `'both'`, today's behavior with zero change.
463
487
  */
464
488
  surface?: AgentSurface;
489
+ /** Wire protocols served alongside the native one — see `AgentModuleOptions.adapters`. STATIC. */
490
+ adapters?: AgentProtocolAdapter[];
465
491
  }
466
492
 
467
493
  /**
@@ -642,4 +668,4 @@ declare class ChatQueueService {
642
668
  private freshAttachments;
643
669
  }
644
670
 
645
- export { AgentDepsFactory as A, ChatQueueService as C, type FunctionalTool as F, LedgerQuotaProvider as L, type QueuePlan as Q, type AgentSurface as a, type AgentModuleOptions as b, type AgentModuleAsyncOptions as c, type AgentSkillsOptions as d, AGENT_TOOL_BRAND as e, type AgentAttachmentsOptions as f, type AgentDeps as g, type AgentMemoryOptions as h, type QueueSettleOutcome as i, type QueuedTurn as j, type QueuedTurnStarter as k, type QuotaLimits as l, type QuotaWindowLimits as m, delegateToolName as n, provideAgentTools as o, provideAgentTool as p, utcDay as u };
671
+ export { AgentDepsFactory as A, ChatQueueService as C, type FunctionalTool as F, LedgerQuotaProvider as L, type QueuePlan as Q, type AgentSurface as a, type AgentModuleOptions as b, type AgentModuleAsyncOptions as c, type AgentProtocolAdapter as d, type AgentSkillsOptions as e, AGENT_TOOL_BRAND as f, type AgentAttachmentsOptions as g, type AgentDeps as h, type AgentMemoryOptions as i, type QueueSettleOutcome as j, type QueuedTurn as k, type QueuedTurnStarter as l, type QuotaLimits as m, type QuotaWindowLimits as n, delegateToolName as o, provideAgentTool as p, provideAgentTools as q, utcDay as u };
@@ -1911,6 +1911,9 @@ var AgentService = class {
1911
1911
  actor: params.actor,
1912
1912
  ...params.transient === true ? {
1913
1913
  transient: true
1914
+ } : {},
1915
+ ...params.newThreadId !== void 0 ? {
1916
+ id: params.newThreadId
1914
1917
  } : {}
1915
1918
  });
1916
1919
  threadId = created.id;
@@ -2335,6 +2338,38 @@ var AgentService = class {
2335
2338
  });
2336
2339
  throw new RunNotActiveException(runId);
2337
2340
  }
2341
+ /** Who owns the thread, or `null` when there is no such (live) thread. */
2342
+ async threadOwner(threadId) {
2343
+ return this.store.ownerOfThread(threadId);
2344
+ }
2345
+ /**
2346
+ * May `actor` continue the run whose stream is `runId` — the AG-UI resume? Only the actor whose
2347
+ * thread is streaming it (`403` otherwise), and only while it still is: a run that is over is
2348
+ * waiting on no one (`409 run_not_active`), whatever an old interrupt id says.
2349
+ */
2350
+ async assertResumable(actor, runId) {
2351
+ const owner = await this.store.ownerOfActiveStream(runId);
2352
+ if (owner === null) {
2353
+ throw new RunNotActiveException(runId);
2354
+ }
2355
+ if (owner !== actor.id) {
2356
+ throw new import_common8.ForbiddenException("run belongs to another actor");
2357
+ }
2358
+ }
2359
+ /**
2360
+ * Everything {@link approve} / {@link reject} check before they signal — who may decide, and
2361
+ * that the request has not lapsed — without signalling. For a caller that settles several
2362
+ * decisions at once and must refuse all of them if one is refused (an AG-UI resume).
2363
+ */
2364
+ async checkDecision(actor, toolCallId2) {
2365
+ await this.assertMayDecide(actor, toolCallId2);
2366
+ await this.assertNotExpired(toolCallId2);
2367
+ }
2368
+ /** Everything {@link answer} checks before it signals, without signalling — see {@link checkDecision}. */
2369
+ async checkAnswer(actor, toolCallId2, answers2) {
2370
+ await this.assertOwnsToolCall(actor, toolCallId2);
2371
+ await this.assertAnswersFit(toolCallId2, answers2);
2372
+ }
2338
2373
  async cancel(actor, runId) {
2339
2374
  await this.assertOwnsActiveStream(actor, runId);
2340
2375
  return this.runner.cancel(runId);
@@ -5877,15 +5912,28 @@ var BASE_CONTROLLERS = [
5877
5912
  ConfigController,
5878
5913
  AttachmentsController
5879
5914
  ];
5880
- var GUARDABLE_CONTROLLERS = BASE_CONTROLLERS;
5881
- function controllersForSurface(surface) {
5882
- return surface === "engine" ? [] : BASE_CONTROLLERS;
5915
+ function adapterControllers(adapters) {
5916
+ return (adapters ?? []).flatMap((adapter) => adapter.controllers);
5917
+ }
5918
+ __name(adapterControllers, "adapterControllers");
5919
+ function adapterProviders(adapters) {
5920
+ return (adapters ?? []).flatMap((adapter) => adapter.providers ?? []);
5921
+ }
5922
+ __name(adapterProviders, "adapterProviders");
5923
+ function controllersForSurface(surface, adapters) {
5924
+ return surface === "engine" ? [] : [
5925
+ ...BASE_CONTROLLERS,
5926
+ ...adapterControllers(adapters)
5927
+ ];
5883
5928
  }
5884
5929
  __name(controllersForSurface, "controllersForSurface");
5885
5930
  var GUARDS_METADATA = "__guards__";
5886
- function applyGuards(guards) {
5931
+ function applyGuards(guards, adapters) {
5887
5932
  const resolved = guards ?? [];
5888
- for (const controller of GUARDABLE_CONTROLLERS) {
5933
+ for (const controller of [
5934
+ ...BASE_CONTROLLERS,
5935
+ ...adapterControllers(adapters)
5936
+ ]) {
5889
5937
  Reflect.defineMetadata(GUARDS_METADATA, resolved, controller);
5890
5938
  }
5891
5939
  }
@@ -5911,7 +5959,7 @@ var AgentModule = class _AgentModule {
5911
5959
  }
5912
5960
  static forRoot(options) {
5913
5961
  const path = options.path ?? DEFAULT_PATH;
5914
- applyGuards(options.guards);
5962
+ applyGuards(options.guards, options.adapters);
5915
5963
  return {
5916
5964
  module: _AgentModule,
5917
5965
  global: true,
@@ -5919,21 +5967,22 @@ var AgentModule = class _AgentModule {
5919
5967
  import_core4.DiscoveryModule,
5920
5968
  routerFor(path)
5921
5969
  ],
5922
- controllers: controllersForSurface(options.surface),
5970
+ controllers: controllersForSurface(options.surface, options.adapters),
5923
5971
  providers: [
5924
5972
  {
5925
5973
  provide: import_nestjs_agent_core31.AGENT_OPTIONS,
5926
5974
  useValue: options
5927
5975
  },
5928
5976
  ...sharedProviders(options.durable ?? false),
5929
- ...guardProviders(options.guards)
5977
+ ...guardProviders(options.guards),
5978
+ ...adapterProviders(options.adapters)
5930
5979
  ],
5931
5980
  exports: exportsFor()
5932
5981
  };
5933
5982
  }
5934
5983
  static forRootAsync(options) {
5935
5984
  const path = options.path ?? DEFAULT_PATH;
5936
- applyGuards(options.guards);
5985
+ applyGuards(options.guards, options.adapters);
5937
5986
  return {
5938
5987
  module: _AgentModule,
5939
5988
  global: true,
@@ -5942,7 +5991,7 @@ var AgentModule = class _AgentModule {
5942
5991
  routerFor(path),
5943
5992
  ...options.imports ?? []
5944
5993
  ],
5945
- controllers: controllersForSurface(options.surface),
5994
+ controllers: controllersForSurface(options.surface, options.adapters),
5946
5995
  providers: [
5947
5996
  {
5948
5997
  provide: import_nestjs_agent_core31.AGENT_OPTIONS,
@@ -5960,7 +6009,8 @@ var AgentModule = class _AgentModule {
5960
6009
  inject: options.inject ?? []
5961
6010
  },
5962
6011
  ...sharedProviders(options.durable ?? false),
5963
- ...guardProviders(options.guards)
6012
+ ...guardProviders(options.guards),
6013
+ ...adapterProviders(options.adapters)
5964
6014
  ],
5965
6015
  exports: exportsFor()
5966
6016
  };