@agentforge/patterns 0.16.37 → 0.16.38

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.cjs CHANGED
@@ -411,9 +411,9 @@ function stringifyActionArguments(arguments_) {
411
411
  function getLatestThought(thoughts) {
412
412
  return thoughts[thoughts.length - 1]?.content ?? "";
413
413
  }
414
- function debugIfVerbose(logger5, verbose, message, data) {
414
+ function debugIfVerbose(logger6, verbose, message, data) {
415
415
  if (verbose) {
416
- logger5.debug(message, data);
416
+ logger6.debug(message, data);
417
417
  }
418
418
  }
419
419
 
@@ -3412,9 +3412,10 @@ function createWorkerNode(config) {
3412
3412
  };
3413
3413
  }
3414
3414
 
3415
- // src/multi-agent/agent.ts
3415
+ // src/multi-agent/agent-graph.ts
3416
3416
  var import_langgraph4 = require("@langchain/langgraph");
3417
- var logger4 = createPatternLogger("agentforge:patterns:multi-agent:system");
3417
+
3418
+ // src/multi-agent/agent-workers.ts
3418
3419
  function getToolName(tool) {
3419
3420
  if (!tool || typeof tool !== "object") {
3420
3421
  return "unknown";
@@ -3428,36 +3429,82 @@ function getToolName(tool) {
3428
3429
  }
3429
3430
  return "unknown";
3430
3431
  }
3431
- function createMultiAgentSystem(config) {
3432
- const {
3433
- supervisor,
3434
- workers,
3435
- aggregator,
3436
- maxIterations = 10,
3437
- verbose = false,
3438
- checkpointer
3439
- } = config;
3440
- const workflow = new import_langgraph4.StateGraph(MultiAgentState);
3441
- const supervisorConfig = { ...supervisor, maxIterations, verbose };
3442
- const supervisorNode = createSupervisorNode(supervisorConfig);
3443
- workflow.addNode("supervisor", supervisorNode);
3444
- const workerIds = [];
3445
- const workerCapabilities = {};
3446
- for (const workerConfig of workers) {
3447
- const workerNode = createWorkerNode({
3448
- ...workerConfig,
3449
- verbose
3432
+ function toWorkerCapabilities(worker) {
3433
+ return {
3434
+ skills: worker.capabilities,
3435
+ tools: worker.tools?.map((tool) => getToolName(tool)) || [],
3436
+ available: true,
3437
+ currentWorkload: 0
3438
+ };
3439
+ }
3440
+ function toWorkerConfig(worker, fallbackModel) {
3441
+ return {
3442
+ id: worker.name,
3443
+ capabilities: toWorkerCapabilities(worker),
3444
+ model: worker.model || fallbackModel,
3445
+ tools: worker.tools,
3446
+ systemPrompt: worker.systemPrompt
3447
+ };
3448
+ }
3449
+
3450
+ // src/multi-agent/agent-runtime.ts
3451
+ function mergeWorkers(input, workerCapabilities) {
3452
+ return {
3453
+ ...input,
3454
+ workers: {
3455
+ ...workerCapabilities,
3456
+ ...input.workers || {}
3457
+ }
3458
+ };
3459
+ }
3460
+ function wrapCompiledSystem(system, workerCapabilities) {
3461
+ const originalInvoke = system.invoke.bind(system);
3462
+ system.invoke = (async function(input, config) {
3463
+ return originalInvoke(
3464
+ mergeWorkers(input, workerCapabilities),
3465
+ config
3466
+ );
3467
+ });
3468
+ const originalStream = system.stream.bind(system);
3469
+ system.stream = (async function(input, config) {
3470
+ return originalStream(
3471
+ mergeWorkers(input, workerCapabilities),
3472
+ config
3473
+ );
3474
+ });
3475
+ return system;
3476
+ }
3477
+ function registerWorkerCapabilities(system, workers) {
3478
+ if (!system._workerRegistry) {
3479
+ system._workerRegistry = {};
3480
+ }
3481
+ for (const worker of workers) {
3482
+ system._workerRegistry[worker.name] = toWorkerCapabilities(worker);
3483
+ }
3484
+ if (!system._originalInvoke) {
3485
+ system._originalInvoke = system.invoke.bind(system);
3486
+ system.invoke = (async function(input, config) {
3487
+ return system._originalInvoke(
3488
+ mergeWorkers(input, system._workerRegistry || {}),
3489
+ config
3490
+ );
3450
3491
  });
3451
- workflow.addNode(workerConfig.id, workerNode);
3452
- workerIds.push(workerConfig.id);
3453
- workerCapabilities[workerConfig.id] = workerConfig.capabilities;
3454
3492
  }
3455
- const aggregatorNode = createAggregatorNode({
3456
- ...aggregator,
3457
- verbose
3458
- });
3459
- workflow.addNode("aggregator", aggregatorNode);
3460
- const supervisorRouter = (state) => {
3493
+ if (!system._originalStream) {
3494
+ system._originalStream = system.stream.bind(system);
3495
+ system.stream = (async function(input, config) {
3496
+ return system._originalStream(
3497
+ mergeWorkers(input, system._workerRegistry || {}),
3498
+ config
3499
+ );
3500
+ });
3501
+ }
3502
+ }
3503
+
3504
+ // src/multi-agent/agent-graph.ts
3505
+ var logger4 = createPatternLogger("agentforge:patterns:multi-agent:system");
3506
+ function createSupervisorRouter() {
3507
+ return (state) => {
3461
3508
  logger4.debug("Supervisor router executing", {
3462
3509
  status: state.status,
3463
3510
  ...state.currentAgent ? { currentAgent: state.currentAgent } : {},
@@ -3473,7 +3520,7 @@ function createMultiAgentSystem(config) {
3473
3520
  }
3474
3521
  if (state.currentAgent && state.currentAgent !== "supervisor") {
3475
3522
  if (state.currentAgent.includes(",")) {
3476
- const agents = state.currentAgent.split(",").map((a) => a.trim());
3523
+ const agents = state.currentAgent.split(",").map((agent) => agent.trim());
3477
3524
  logger4.info("Supervisor router: parallel routing", {
3478
3525
  agents,
3479
3526
  count: agents.length
@@ -3488,7 +3535,9 @@ function createMultiAgentSystem(config) {
3488
3535
  logger4.debug("Supervisor router: staying at supervisor");
3489
3536
  return "supervisor";
3490
3537
  };
3491
- const workerRouter = (state) => {
3538
+ }
3539
+ function createWorkerRouter() {
3540
+ return (state) => {
3492
3541
  logger4.debug("Worker router executing", {
3493
3542
  iteration: state.iteration,
3494
3543
  completedTasks: state.completedTasks.length
@@ -3496,54 +3545,66 @@ function createMultiAgentSystem(config) {
3496
3545
  logger4.debug("Worker router: returning to supervisor");
3497
3546
  return "supervisor";
3498
3547
  };
3499
- const aggregatorRouter = (state) => {
3548
+ }
3549
+ function createAggregatorRouter() {
3550
+ return (state) => {
3500
3551
  logger4.info("Aggregator router: ending workflow", {
3501
3552
  completedTasks: state.completedTasks.length,
3502
3553
  status: state.status
3503
3554
  });
3504
3555
  return import_langgraph4.END;
3505
3556
  };
3506
- workflow.setEntryPoint("supervisor");
3507
- workflow.addConditionalEdges("supervisor", supervisorRouter, [
3557
+ }
3558
+ function createCompiledMultiAgentSystem(config) {
3559
+ const {
3560
+ supervisor,
3561
+ workers,
3562
+ aggregator,
3563
+ maxIterations = 10,
3564
+ verbose = false,
3565
+ checkpointer
3566
+ } = config;
3567
+ const workflow = new import_langgraph4.StateGraph(MultiAgentState);
3568
+ const workerIds = [];
3569
+ const workerCapabilities = {};
3570
+ workflow.addNode(
3571
+ "supervisor",
3572
+ createSupervisorNode({ ...supervisor, maxIterations, verbose })
3573
+ );
3574
+ for (const workerConfig of workers) {
3575
+ workflow.addNode(
3576
+ workerConfig.id,
3577
+ createWorkerNode({
3578
+ ...workerConfig,
3579
+ verbose
3580
+ })
3581
+ );
3582
+ workerIds.push(workerConfig.id);
3583
+ workerCapabilities[workerConfig.id] = workerConfig.capabilities;
3584
+ }
3585
+ workflow.addNode(
3508
3586
  "aggregator",
3509
- import_langgraph4.END,
3510
- ...workerIds
3511
- ]);
3587
+ createAggregatorNode({
3588
+ ...aggregator,
3589
+ verbose
3590
+ })
3591
+ );
3592
+ const supervisorRouter = createSupervisorRouter();
3593
+ const workerRouter = createWorkerRouter();
3594
+ const aggregatorRouter = createAggregatorRouter();
3595
+ workflow.setEntryPoint("supervisor");
3596
+ workflow.addConditionalEdges("supervisor", supervisorRouter, ["aggregator", import_langgraph4.END, ...workerIds]);
3512
3597
  for (const workerId of workerIds) {
3513
3598
  workflow.addConditionalEdges(workerId, workerRouter, ["supervisor"]);
3514
3599
  }
3515
3600
  workflow.addConditionalEdges("aggregator", aggregatorRouter, [import_langgraph4.END]);
3516
- const compiled = workflow.compile(checkpointer ? { checkpointer } : void 0);
3517
- const originalInvoke = compiled.invoke.bind(compiled);
3518
- compiled.invoke = (async function(input, config2) {
3519
- const mergedInput = {
3520
- ...input,
3521
- workers: {
3522
- ...workerCapabilities,
3523
- ...input.workers || {}
3524
- }
3525
- };
3526
- return originalInvoke(
3527
- mergedInput,
3528
- config2
3529
- );
3530
- });
3531
- const originalStream = compiled.stream.bind(compiled);
3532
- compiled.stream = (async function(input, config2) {
3533
- const mergedInput = {
3534
- ...input,
3535
- workers: {
3536
- ...workerCapabilities,
3537
- ...input.workers || {}
3538
- }
3539
- };
3540
- return originalStream(
3541
- mergedInput,
3542
- config2
3543
- );
3544
- });
3545
- return compiled;
3601
+ const compiled = workflow.compile(
3602
+ checkpointer ? { checkpointer } : void 0
3603
+ );
3604
+ return wrapCompiledSystem(compiled, workerCapabilities);
3546
3605
  }
3606
+
3607
+ // src/multi-agent/agent-builder.ts
3547
3608
  var MultiAgentSystemBuilder = class {
3548
3609
  config;
3549
3610
  additionalWorkers = [];
@@ -3554,55 +3615,15 @@ var MultiAgentSystemBuilder = class {
3554
3615
  workers: config.workers || []
3555
3616
  };
3556
3617
  }
3557
- /**
3558
- * Register workers with the system builder
3559
- *
3560
- * @param workers - Array of worker configurations
3561
- * @returns this builder for chaining
3562
- *
3563
- * @example
3564
- * ```typescript
3565
- * const builder = new MultiAgentSystemBuilder({
3566
- * supervisor: { llm, strategy: 'skill-based' },
3567
- * aggregator: { llm },
3568
- * });
3569
- *
3570
- * builder.registerWorkers([
3571
- * {
3572
- * name: 'math_worker',
3573
- * capabilities: ['math', 'calculations'],
3574
- * tools: [calculatorTool],
3575
- * },
3576
- * ]);
3577
- *
3578
- * const system = builder.build();
3579
- * ```
3580
- */
3581
3618
  registerWorkers(workers) {
3582
3619
  if (this.compiled) {
3583
3620
  throw new Error("Cannot register workers after the system has been compiled");
3584
3621
  }
3585
3622
  for (const worker of workers) {
3586
- this.additionalWorkers.push({
3587
- id: worker.name,
3588
- capabilities: {
3589
- skills: worker.capabilities,
3590
- tools: worker.tools?.map((t) => getToolName(t)) || [],
3591
- available: true,
3592
- currentWorkload: 0
3593
- },
3594
- model: worker.model || this.config.supervisor.model,
3595
- tools: worker.tools,
3596
- systemPrompt: worker.systemPrompt
3597
- });
3623
+ this.additionalWorkers.push(toWorkerConfig(worker, this.config.supervisor.model));
3598
3624
  }
3599
3625
  return this;
3600
3626
  }
3601
- /**
3602
- * Build and compile the multi-agent system
3603
- *
3604
- * @returns Compiled LangGraph workflow
3605
- */
3606
3627
  build() {
3607
3628
  if (this.compiled) {
3608
3629
  throw new Error("System has already been compiled");
@@ -3612,59 +3633,23 @@ var MultiAgentSystemBuilder = class {
3612
3633
  throw new Error("At least one worker must be registered before building the system");
3613
3634
  }
3614
3635
  this.compiled = true;
3615
- return createMultiAgentSystem({
3636
+ return createCompiledMultiAgentSystem({
3616
3637
  ...this.config,
3617
3638
  workers: allWorkers
3618
3639
  });
3619
3640
  }
3620
3641
  };
3642
+
3643
+ // src/multi-agent/agent.ts
3644
+ var logger5 = createPatternLogger("agentforge:patterns:multi-agent:system");
3645
+ function createMultiAgentSystem(config) {
3646
+ return createCompiledMultiAgentSystem(config);
3647
+ }
3621
3648
  function registerWorkers(system, workers) {
3622
- console.warn(
3649
+ logger5.warn(
3623
3650
  "[AgentForge] registerWorkers() on a compiled system only updates worker capabilities in state.\nIt does NOT add worker nodes to the graph. Use MultiAgentSystemBuilder for proper worker registration.\nSee: https://github.com/TVScoundrel/agentforge/blob/main/packages/patterns/docs/multi-agent-pattern.md"
3624
3651
  );
3625
- if (!system._workerRegistry) {
3626
- system._workerRegistry = {};
3627
- }
3628
- for (const worker of workers) {
3629
- system._workerRegistry[worker.name] = {
3630
- skills: worker.capabilities,
3631
- tools: worker.tools?.map((t) => getToolName(t)) || [],
3632
- available: true,
3633
- currentWorkload: 0
3634
- };
3635
- }
3636
- if (!system._originalInvoke) {
3637
- system._originalInvoke = system.invoke.bind(system);
3638
- system.invoke = (async function(input, config) {
3639
- const mergedInput = {
3640
- ...input,
3641
- workers: {
3642
- ...system._workerRegistry || {},
3643
- ...input.workers || {}
3644
- }
3645
- };
3646
- return system._originalInvoke(
3647
- mergedInput,
3648
- config
3649
- );
3650
- });
3651
- }
3652
- if (!system._originalStream) {
3653
- system._originalStream = system.stream.bind(system);
3654
- system.stream = (async function(input, config) {
3655
- const mergedInput = {
3656
- ...input,
3657
- workers: {
3658
- ...system._workerRegistry || {},
3659
- ...input.workers || {}
3660
- }
3661
- };
3662
- return system._originalStream(
3663
- mergedInput,
3664
- config
3665
- );
3666
- });
3667
- }
3652
+ registerWorkerCapabilities(system, workers);
3668
3653
  }
3669
3654
  // Annotate the CommonJS export names for ESM import in node:
3670
3655
  0 && (module.exports = {
package/dist/index.d.cts CHANGED
@@ -3452,10 +3452,37 @@ declare function createSupervisorNode(config: SupervisorConfig): (state: MultiAg
3452
3452
  */
3453
3453
  declare function createWorkerNode(config: WorkerConfig): (state: MultiAgentStateType, runConfig?: WorkerExecutionConfig) => Promise<Partial<MultiAgentStateType>>;
3454
3454
 
3455
+ interface RegisterWorkerInput {
3456
+ name: string;
3457
+ description?: string;
3458
+ capabilities: string[];
3459
+ tools?: WorkerConfig['tools'];
3460
+ systemPrompt?: string;
3461
+ }
3462
+ interface BuilderWorkerInput extends RegisterWorkerInput {
3463
+ model?: WorkerConfig['model'];
3464
+ }
3465
+ interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStateType, unknown> {
3466
+ _workerRegistry?: Record<string, WorkerCapabilities>;
3467
+ _originalInvoke?: (input: Partial<MultiAgentStateType>, config?: RunnableConfig) => ReturnType<MultiAgentSystemWithRegistry['invoke']>;
3468
+ _originalStream?: (input: Partial<MultiAgentStateType>, config?: RunnableConfig) => ReturnType<MultiAgentSystemWithRegistry['stream']>;
3469
+ }
3470
+
3471
+ declare class MultiAgentSystemBuilder {
3472
+ private config;
3473
+ private additionalWorkers;
3474
+ private compiled;
3475
+ constructor(config: Omit<MultiAgentSystemConfig, 'workers'> & {
3476
+ workers?: WorkerConfig[];
3477
+ });
3478
+ registerWorkers(workers: BuilderWorkerInput[]): this;
3479
+ build(): MultiAgentSystemWithRegistry;
3480
+ }
3481
+
3455
3482
  /**
3456
3483
  * Multi-Agent System Factory
3457
3484
  *
3458
- * This module provides the main factory function for creating multi-agent systems.
3485
+ * This module provides the public facade for creating multi-agent systems.
3459
3486
  *
3460
3487
  * @module patterns/multi-agent/agent
3461
3488
  */
@@ -3545,66 +3572,6 @@ declare function createWorkerNode(config: WorkerConfig): (state: MultiAgentState
3545
3572
  * ```
3546
3573
  */
3547
3574
  declare function createMultiAgentSystem(config: MultiAgentSystemConfig): MultiAgentSystemWithRegistry;
3548
- /**
3549
- * Multi-agent system builder for dynamic worker registration
3550
- *
3551
- * This builder allows you to register workers before compiling the graph.
3552
- * Once compiled, the graph is immutable and workers cannot be added.
3553
- */
3554
- declare class MultiAgentSystemBuilder {
3555
- private config;
3556
- private additionalWorkers;
3557
- private compiled;
3558
- constructor(config: Omit<MultiAgentSystemConfig, 'workers'> & {
3559
- workers?: WorkerConfig[];
3560
- });
3561
- /**
3562
- * Register workers with the system builder
3563
- *
3564
- * @param workers - Array of worker configurations
3565
- * @returns this builder for chaining
3566
- *
3567
- * @example
3568
- * ```typescript
3569
- * const builder = new MultiAgentSystemBuilder({
3570
- * supervisor: { llm, strategy: 'skill-based' },
3571
- * aggregator: { llm },
3572
- * });
3573
- *
3574
- * builder.registerWorkers([
3575
- * {
3576
- * name: 'math_worker',
3577
- * capabilities: ['math', 'calculations'],
3578
- * tools: [calculatorTool],
3579
- * },
3580
- * ]);
3581
- *
3582
- * const system = builder.build();
3583
- * ```
3584
- */
3585
- registerWorkers(workers: Array<{
3586
- name: string;
3587
- description?: string;
3588
- capabilities: string[];
3589
- tools?: WorkerConfig['tools'];
3590
- systemPrompt?: string;
3591
- model?: WorkerConfig['model'];
3592
- }>): this;
3593
- /**
3594
- * Build and compile the multi-agent system
3595
- *
3596
- * @returns Compiled LangGraph workflow
3597
- */
3598
- build(): MultiAgentSystemWithRegistry;
3599
- }
3600
- /**
3601
- * Extended multi-agent system with worker registration support
3602
- */
3603
- interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStateType, unknown> {
3604
- _workerRegistry?: Record<string, WorkerCapabilities>;
3605
- _originalInvoke?: MultiAgentSystemWithRegistry['invoke'];
3606
- _originalStream?: MultiAgentSystemWithRegistry['stream'];
3607
- }
3608
3575
  /**
3609
3576
  * Register workers with a compiled multi-agent system
3610
3577
  *
@@ -3632,13 +3599,7 @@ interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStat
3632
3599
  *
3633
3600
  * @deprecated Use `MultiAgentSystemBuilder` instead for proper worker registration
3634
3601
  */
3635
- declare function registerWorkers(system: MultiAgentSystemWithRegistry, workers: Array<{
3636
- name: string;
3637
- description?: string;
3638
- capabilities: string[];
3639
- tools?: WorkerConfig['tools'];
3640
- systemPrompt?: string;
3641
- }>): void;
3602
+ declare function registerWorkers(system: MultiAgentSystemWithRegistry, workers: RegisterWorkerInput[]): void;
3642
3603
 
3643
3604
  /**
3644
3605
  * Generate a cache key for a tool call based on tool name and arguments
package/dist/index.d.ts CHANGED
@@ -3452,10 +3452,37 @@ declare function createSupervisorNode(config: SupervisorConfig): (state: MultiAg
3452
3452
  */
3453
3453
  declare function createWorkerNode(config: WorkerConfig): (state: MultiAgentStateType, runConfig?: WorkerExecutionConfig) => Promise<Partial<MultiAgentStateType>>;
3454
3454
 
3455
+ interface RegisterWorkerInput {
3456
+ name: string;
3457
+ description?: string;
3458
+ capabilities: string[];
3459
+ tools?: WorkerConfig['tools'];
3460
+ systemPrompt?: string;
3461
+ }
3462
+ interface BuilderWorkerInput extends RegisterWorkerInput {
3463
+ model?: WorkerConfig['model'];
3464
+ }
3465
+ interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStateType, unknown> {
3466
+ _workerRegistry?: Record<string, WorkerCapabilities>;
3467
+ _originalInvoke?: (input: Partial<MultiAgentStateType>, config?: RunnableConfig) => ReturnType<MultiAgentSystemWithRegistry['invoke']>;
3468
+ _originalStream?: (input: Partial<MultiAgentStateType>, config?: RunnableConfig) => ReturnType<MultiAgentSystemWithRegistry['stream']>;
3469
+ }
3470
+
3471
+ declare class MultiAgentSystemBuilder {
3472
+ private config;
3473
+ private additionalWorkers;
3474
+ private compiled;
3475
+ constructor(config: Omit<MultiAgentSystemConfig, 'workers'> & {
3476
+ workers?: WorkerConfig[];
3477
+ });
3478
+ registerWorkers(workers: BuilderWorkerInput[]): this;
3479
+ build(): MultiAgentSystemWithRegistry;
3480
+ }
3481
+
3455
3482
  /**
3456
3483
  * Multi-Agent System Factory
3457
3484
  *
3458
- * This module provides the main factory function for creating multi-agent systems.
3485
+ * This module provides the public facade for creating multi-agent systems.
3459
3486
  *
3460
3487
  * @module patterns/multi-agent/agent
3461
3488
  */
@@ -3545,66 +3572,6 @@ declare function createWorkerNode(config: WorkerConfig): (state: MultiAgentState
3545
3572
  * ```
3546
3573
  */
3547
3574
  declare function createMultiAgentSystem(config: MultiAgentSystemConfig): MultiAgentSystemWithRegistry;
3548
- /**
3549
- * Multi-agent system builder for dynamic worker registration
3550
- *
3551
- * This builder allows you to register workers before compiling the graph.
3552
- * Once compiled, the graph is immutable and workers cannot be added.
3553
- */
3554
- declare class MultiAgentSystemBuilder {
3555
- private config;
3556
- private additionalWorkers;
3557
- private compiled;
3558
- constructor(config: Omit<MultiAgentSystemConfig, 'workers'> & {
3559
- workers?: WorkerConfig[];
3560
- });
3561
- /**
3562
- * Register workers with the system builder
3563
- *
3564
- * @param workers - Array of worker configurations
3565
- * @returns this builder for chaining
3566
- *
3567
- * @example
3568
- * ```typescript
3569
- * const builder = new MultiAgentSystemBuilder({
3570
- * supervisor: { llm, strategy: 'skill-based' },
3571
- * aggregator: { llm },
3572
- * });
3573
- *
3574
- * builder.registerWorkers([
3575
- * {
3576
- * name: 'math_worker',
3577
- * capabilities: ['math', 'calculations'],
3578
- * tools: [calculatorTool],
3579
- * },
3580
- * ]);
3581
- *
3582
- * const system = builder.build();
3583
- * ```
3584
- */
3585
- registerWorkers(workers: Array<{
3586
- name: string;
3587
- description?: string;
3588
- capabilities: string[];
3589
- tools?: WorkerConfig['tools'];
3590
- systemPrompt?: string;
3591
- model?: WorkerConfig['model'];
3592
- }>): this;
3593
- /**
3594
- * Build and compile the multi-agent system
3595
- *
3596
- * @returns Compiled LangGraph workflow
3597
- */
3598
- build(): MultiAgentSystemWithRegistry;
3599
- }
3600
- /**
3601
- * Extended multi-agent system with worker registration support
3602
- */
3603
- interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStateType, unknown> {
3604
- _workerRegistry?: Record<string, WorkerCapabilities>;
3605
- _originalInvoke?: MultiAgentSystemWithRegistry['invoke'];
3606
- _originalStream?: MultiAgentSystemWithRegistry['stream'];
3607
- }
3608
3575
  /**
3609
3576
  * Register workers with a compiled multi-agent system
3610
3577
  *
@@ -3632,13 +3599,7 @@ interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStat
3632
3599
  *
3633
3600
  * @deprecated Use `MultiAgentSystemBuilder` instead for proper worker registration
3634
3601
  */
3635
- declare function registerWorkers(system: MultiAgentSystemWithRegistry, workers: Array<{
3636
- name: string;
3637
- description?: string;
3638
- capabilities: string[];
3639
- tools?: WorkerConfig['tools'];
3640
- systemPrompt?: string;
3641
- }>): void;
3602
+ declare function registerWorkers(system: MultiAgentSystemWithRegistry, workers: RegisterWorkerInput[]): void;
3642
3603
 
3643
3604
  /**
3644
3605
  * Generate a cache key for a tool call based on tool name and arguments
package/dist/index.js CHANGED
@@ -313,9 +313,9 @@ function stringifyActionArguments(arguments_) {
313
313
  function getLatestThought(thoughts) {
314
314
  return thoughts[thoughts.length - 1]?.content ?? "";
315
315
  }
316
- function debugIfVerbose(logger5, verbose, message, data) {
316
+ function debugIfVerbose(logger6, verbose, message, data) {
317
317
  if (verbose) {
318
- logger5.debug(message, data);
318
+ logger6.debug(message, data);
319
319
  }
320
320
  }
321
321
 
@@ -3314,9 +3314,10 @@ function createWorkerNode(config) {
3314
3314
  };
3315
3315
  }
3316
3316
 
3317
- // src/multi-agent/agent.ts
3318
- import { StateGraph as StateGraph4, END as END4 } from "@langchain/langgraph";
3319
- var logger4 = createPatternLogger("agentforge:patterns:multi-agent:system");
3317
+ // src/multi-agent/agent-graph.ts
3318
+ import { END as END4, StateGraph as StateGraph4 } from "@langchain/langgraph";
3319
+
3320
+ // src/multi-agent/agent-workers.ts
3320
3321
  function getToolName(tool) {
3321
3322
  if (!tool || typeof tool !== "object") {
3322
3323
  return "unknown";
@@ -3330,36 +3331,82 @@ function getToolName(tool) {
3330
3331
  }
3331
3332
  return "unknown";
3332
3333
  }
3333
- function createMultiAgentSystem(config) {
3334
- const {
3335
- supervisor,
3336
- workers,
3337
- aggregator,
3338
- maxIterations = 10,
3339
- verbose = false,
3340
- checkpointer
3341
- } = config;
3342
- const workflow = new StateGraph4(MultiAgentState);
3343
- const supervisorConfig = { ...supervisor, maxIterations, verbose };
3344
- const supervisorNode = createSupervisorNode(supervisorConfig);
3345
- workflow.addNode("supervisor", supervisorNode);
3346
- const workerIds = [];
3347
- const workerCapabilities = {};
3348
- for (const workerConfig of workers) {
3349
- const workerNode = createWorkerNode({
3350
- ...workerConfig,
3351
- verbose
3334
+ function toWorkerCapabilities(worker) {
3335
+ return {
3336
+ skills: worker.capabilities,
3337
+ tools: worker.tools?.map((tool) => getToolName(tool)) || [],
3338
+ available: true,
3339
+ currentWorkload: 0
3340
+ };
3341
+ }
3342
+ function toWorkerConfig(worker, fallbackModel) {
3343
+ return {
3344
+ id: worker.name,
3345
+ capabilities: toWorkerCapabilities(worker),
3346
+ model: worker.model || fallbackModel,
3347
+ tools: worker.tools,
3348
+ systemPrompt: worker.systemPrompt
3349
+ };
3350
+ }
3351
+
3352
+ // src/multi-agent/agent-runtime.ts
3353
+ function mergeWorkers(input, workerCapabilities) {
3354
+ return {
3355
+ ...input,
3356
+ workers: {
3357
+ ...workerCapabilities,
3358
+ ...input.workers || {}
3359
+ }
3360
+ };
3361
+ }
3362
+ function wrapCompiledSystem(system, workerCapabilities) {
3363
+ const originalInvoke = system.invoke.bind(system);
3364
+ system.invoke = (async function(input, config) {
3365
+ return originalInvoke(
3366
+ mergeWorkers(input, workerCapabilities),
3367
+ config
3368
+ );
3369
+ });
3370
+ const originalStream = system.stream.bind(system);
3371
+ system.stream = (async function(input, config) {
3372
+ return originalStream(
3373
+ mergeWorkers(input, workerCapabilities),
3374
+ config
3375
+ );
3376
+ });
3377
+ return system;
3378
+ }
3379
+ function registerWorkerCapabilities(system, workers) {
3380
+ if (!system._workerRegistry) {
3381
+ system._workerRegistry = {};
3382
+ }
3383
+ for (const worker of workers) {
3384
+ system._workerRegistry[worker.name] = toWorkerCapabilities(worker);
3385
+ }
3386
+ if (!system._originalInvoke) {
3387
+ system._originalInvoke = system.invoke.bind(system);
3388
+ system.invoke = (async function(input, config) {
3389
+ return system._originalInvoke(
3390
+ mergeWorkers(input, system._workerRegistry || {}),
3391
+ config
3392
+ );
3352
3393
  });
3353
- workflow.addNode(workerConfig.id, workerNode);
3354
- workerIds.push(workerConfig.id);
3355
- workerCapabilities[workerConfig.id] = workerConfig.capabilities;
3356
3394
  }
3357
- const aggregatorNode = createAggregatorNode({
3358
- ...aggregator,
3359
- verbose
3360
- });
3361
- workflow.addNode("aggregator", aggregatorNode);
3362
- const supervisorRouter = (state) => {
3395
+ if (!system._originalStream) {
3396
+ system._originalStream = system.stream.bind(system);
3397
+ system.stream = (async function(input, config) {
3398
+ return system._originalStream(
3399
+ mergeWorkers(input, system._workerRegistry || {}),
3400
+ config
3401
+ );
3402
+ });
3403
+ }
3404
+ }
3405
+
3406
+ // src/multi-agent/agent-graph.ts
3407
+ var logger4 = createPatternLogger("agentforge:patterns:multi-agent:system");
3408
+ function createSupervisorRouter() {
3409
+ return (state) => {
3363
3410
  logger4.debug("Supervisor router executing", {
3364
3411
  status: state.status,
3365
3412
  ...state.currentAgent ? { currentAgent: state.currentAgent } : {},
@@ -3375,7 +3422,7 @@ function createMultiAgentSystem(config) {
3375
3422
  }
3376
3423
  if (state.currentAgent && state.currentAgent !== "supervisor") {
3377
3424
  if (state.currentAgent.includes(",")) {
3378
- const agents = state.currentAgent.split(",").map((a) => a.trim());
3425
+ const agents = state.currentAgent.split(",").map((agent) => agent.trim());
3379
3426
  logger4.info("Supervisor router: parallel routing", {
3380
3427
  agents,
3381
3428
  count: agents.length
@@ -3390,7 +3437,9 @@ function createMultiAgentSystem(config) {
3390
3437
  logger4.debug("Supervisor router: staying at supervisor");
3391
3438
  return "supervisor";
3392
3439
  };
3393
- const workerRouter = (state) => {
3440
+ }
3441
+ function createWorkerRouter() {
3442
+ return (state) => {
3394
3443
  logger4.debug("Worker router executing", {
3395
3444
  iteration: state.iteration,
3396
3445
  completedTasks: state.completedTasks.length
@@ -3398,54 +3447,66 @@ function createMultiAgentSystem(config) {
3398
3447
  logger4.debug("Worker router: returning to supervisor");
3399
3448
  return "supervisor";
3400
3449
  };
3401
- const aggregatorRouter = (state) => {
3450
+ }
3451
+ function createAggregatorRouter() {
3452
+ return (state) => {
3402
3453
  logger4.info("Aggregator router: ending workflow", {
3403
3454
  completedTasks: state.completedTasks.length,
3404
3455
  status: state.status
3405
3456
  });
3406
3457
  return END4;
3407
3458
  };
3408
- workflow.setEntryPoint("supervisor");
3409
- workflow.addConditionalEdges("supervisor", supervisorRouter, [
3459
+ }
3460
+ function createCompiledMultiAgentSystem(config) {
3461
+ const {
3462
+ supervisor,
3463
+ workers,
3464
+ aggregator,
3465
+ maxIterations = 10,
3466
+ verbose = false,
3467
+ checkpointer
3468
+ } = config;
3469
+ const workflow = new StateGraph4(MultiAgentState);
3470
+ const workerIds = [];
3471
+ const workerCapabilities = {};
3472
+ workflow.addNode(
3473
+ "supervisor",
3474
+ createSupervisorNode({ ...supervisor, maxIterations, verbose })
3475
+ );
3476
+ for (const workerConfig of workers) {
3477
+ workflow.addNode(
3478
+ workerConfig.id,
3479
+ createWorkerNode({
3480
+ ...workerConfig,
3481
+ verbose
3482
+ })
3483
+ );
3484
+ workerIds.push(workerConfig.id);
3485
+ workerCapabilities[workerConfig.id] = workerConfig.capabilities;
3486
+ }
3487
+ workflow.addNode(
3410
3488
  "aggregator",
3411
- END4,
3412
- ...workerIds
3413
- ]);
3489
+ createAggregatorNode({
3490
+ ...aggregator,
3491
+ verbose
3492
+ })
3493
+ );
3494
+ const supervisorRouter = createSupervisorRouter();
3495
+ const workerRouter = createWorkerRouter();
3496
+ const aggregatorRouter = createAggregatorRouter();
3497
+ workflow.setEntryPoint("supervisor");
3498
+ workflow.addConditionalEdges("supervisor", supervisorRouter, ["aggregator", END4, ...workerIds]);
3414
3499
  for (const workerId of workerIds) {
3415
3500
  workflow.addConditionalEdges(workerId, workerRouter, ["supervisor"]);
3416
3501
  }
3417
3502
  workflow.addConditionalEdges("aggregator", aggregatorRouter, [END4]);
3418
- const compiled = workflow.compile(checkpointer ? { checkpointer } : void 0);
3419
- const originalInvoke = compiled.invoke.bind(compiled);
3420
- compiled.invoke = (async function(input, config2) {
3421
- const mergedInput = {
3422
- ...input,
3423
- workers: {
3424
- ...workerCapabilities,
3425
- ...input.workers || {}
3426
- }
3427
- };
3428
- return originalInvoke(
3429
- mergedInput,
3430
- config2
3431
- );
3432
- });
3433
- const originalStream = compiled.stream.bind(compiled);
3434
- compiled.stream = (async function(input, config2) {
3435
- const mergedInput = {
3436
- ...input,
3437
- workers: {
3438
- ...workerCapabilities,
3439
- ...input.workers || {}
3440
- }
3441
- };
3442
- return originalStream(
3443
- mergedInput,
3444
- config2
3445
- );
3446
- });
3447
- return compiled;
3503
+ const compiled = workflow.compile(
3504
+ checkpointer ? { checkpointer } : void 0
3505
+ );
3506
+ return wrapCompiledSystem(compiled, workerCapabilities);
3448
3507
  }
3508
+
3509
+ // src/multi-agent/agent-builder.ts
3449
3510
  var MultiAgentSystemBuilder = class {
3450
3511
  config;
3451
3512
  additionalWorkers = [];
@@ -3456,55 +3517,15 @@ var MultiAgentSystemBuilder = class {
3456
3517
  workers: config.workers || []
3457
3518
  };
3458
3519
  }
3459
- /**
3460
- * Register workers with the system builder
3461
- *
3462
- * @param workers - Array of worker configurations
3463
- * @returns this builder for chaining
3464
- *
3465
- * @example
3466
- * ```typescript
3467
- * const builder = new MultiAgentSystemBuilder({
3468
- * supervisor: { llm, strategy: 'skill-based' },
3469
- * aggregator: { llm },
3470
- * });
3471
- *
3472
- * builder.registerWorkers([
3473
- * {
3474
- * name: 'math_worker',
3475
- * capabilities: ['math', 'calculations'],
3476
- * tools: [calculatorTool],
3477
- * },
3478
- * ]);
3479
- *
3480
- * const system = builder.build();
3481
- * ```
3482
- */
3483
3520
  registerWorkers(workers) {
3484
3521
  if (this.compiled) {
3485
3522
  throw new Error("Cannot register workers after the system has been compiled");
3486
3523
  }
3487
3524
  for (const worker of workers) {
3488
- this.additionalWorkers.push({
3489
- id: worker.name,
3490
- capabilities: {
3491
- skills: worker.capabilities,
3492
- tools: worker.tools?.map((t) => getToolName(t)) || [],
3493
- available: true,
3494
- currentWorkload: 0
3495
- },
3496
- model: worker.model || this.config.supervisor.model,
3497
- tools: worker.tools,
3498
- systemPrompt: worker.systemPrompt
3499
- });
3525
+ this.additionalWorkers.push(toWorkerConfig(worker, this.config.supervisor.model));
3500
3526
  }
3501
3527
  return this;
3502
3528
  }
3503
- /**
3504
- * Build and compile the multi-agent system
3505
- *
3506
- * @returns Compiled LangGraph workflow
3507
- */
3508
3529
  build() {
3509
3530
  if (this.compiled) {
3510
3531
  throw new Error("System has already been compiled");
@@ -3514,59 +3535,23 @@ var MultiAgentSystemBuilder = class {
3514
3535
  throw new Error("At least one worker must be registered before building the system");
3515
3536
  }
3516
3537
  this.compiled = true;
3517
- return createMultiAgentSystem({
3538
+ return createCompiledMultiAgentSystem({
3518
3539
  ...this.config,
3519
3540
  workers: allWorkers
3520
3541
  });
3521
3542
  }
3522
3543
  };
3544
+
3545
+ // src/multi-agent/agent.ts
3546
+ var logger5 = createPatternLogger("agentforge:patterns:multi-agent:system");
3547
+ function createMultiAgentSystem(config) {
3548
+ return createCompiledMultiAgentSystem(config);
3549
+ }
3523
3550
  function registerWorkers(system, workers) {
3524
- console.warn(
3551
+ logger5.warn(
3525
3552
  "[AgentForge] registerWorkers() on a compiled system only updates worker capabilities in state.\nIt does NOT add worker nodes to the graph. Use MultiAgentSystemBuilder for proper worker registration.\nSee: https://github.com/TVScoundrel/agentforge/blob/main/packages/patterns/docs/multi-agent-pattern.md"
3526
3553
  );
3527
- if (!system._workerRegistry) {
3528
- system._workerRegistry = {};
3529
- }
3530
- for (const worker of workers) {
3531
- system._workerRegistry[worker.name] = {
3532
- skills: worker.capabilities,
3533
- tools: worker.tools?.map((t) => getToolName(t)) || [],
3534
- available: true,
3535
- currentWorkload: 0
3536
- };
3537
- }
3538
- if (!system._originalInvoke) {
3539
- system._originalInvoke = system.invoke.bind(system);
3540
- system.invoke = (async function(input, config) {
3541
- const mergedInput = {
3542
- ...input,
3543
- workers: {
3544
- ...system._workerRegistry || {},
3545
- ...input.workers || {}
3546
- }
3547
- };
3548
- return system._originalInvoke(
3549
- mergedInput,
3550
- config
3551
- );
3552
- });
3553
- }
3554
- if (!system._originalStream) {
3555
- system._originalStream = system.stream.bind(system);
3556
- system.stream = (async function(input, config) {
3557
- const mergedInput = {
3558
- ...input,
3559
- workers: {
3560
- ...system._workerRegistry || {},
3561
- ...input.workers || {}
3562
- }
3563
- };
3564
- return system._originalStream(
3565
- mergedInput,
3566
- config
3567
- );
3568
- });
3569
- }
3554
+ registerWorkerCapabilities(system, workers);
3570
3555
  }
3571
3556
  export {
3572
3557
  AgentMessageSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentforge/patterns",
3
- "version": "0.16.37",
3
+ "version": "0.16.38",
4
4
  "description": "Production-ready agent workflow patterns for TypeScript including ReAct and Planner-Executor, built on LangGraph.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -41,13 +41,13 @@
41
41
  "url": "https://github.com/TVScoundrel/agentforge/issues"
42
42
  },
43
43
  "dependencies": {
44
- "@agentforge/core": "0.16.37",
44
+ "@agentforge/core": "0.16.38",
45
45
  "@langchain/core": "^1.1.17",
46
46
  "@langchain/langgraph": "^1.1.2",
47
47
  "zod": "^3.23.8"
48
48
  },
49
49
  "devDependencies": {
50
- "@agentforge/testing": "0.16.37",
50
+ "@agentforge/testing": "0.16.38",
51
51
  "@eslint/js": "^9.17.0",
52
52
  "@types/node": "^22.10.2",
53
53
  "eslint": "^9.17.0",