@mozaik-ai/core 4.2.0-beta.4 → 4.2.0-beta.6

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.js CHANGED
@@ -443,11 +443,6 @@ var CreateAgentUseCase = class {
443
443
  const id = UuidGenerator.create();
444
444
  const manifest = { id, name, role: "agent" };
445
445
  const memory = this.memoryFactory.create();
446
- const developerMessageItem = {
447
- type: "developer_message",
448
- text: instruction
449
- };
450
- memory.saveItem(developerMessageItem);
451
446
  const agentRecord = { id, manifest, tools, memory, handlers };
452
447
  const agent = Agent.create(agentRecord);
453
448
  await this.agentRepository.save(agent);
@@ -474,9 +469,206 @@ var InMemoryAgentRepository = class {
474
469
  }
475
470
  };
476
471
 
472
+ // src/domain/agent/loop/specification.ts
473
+ var LoopSpecification = class {
474
+ and(other) {
475
+ return new AndLoopSpecification(this, other);
476
+ }
477
+ or(other) {
478
+ return new OrLoopSpecification(this, other);
479
+ }
480
+ not() {
481
+ return new NotLoopSpecification(this);
482
+ }
483
+ };
484
+ var AndLoopSpecification = class extends LoopSpecification {
485
+ constructor(left, right) {
486
+ super();
487
+ this.left = left;
488
+ this.right = right;
489
+ }
490
+ isSatisfiedBy(agentLoop) {
491
+ return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
492
+ }
493
+ };
494
+ var OrLoopSpecification = class extends LoopSpecification {
495
+ constructor(left, right) {
496
+ super();
497
+ this.left = left;
498
+ this.right = right;
499
+ }
500
+ isSatisfiedBy(agentLoop) {
501
+ return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
502
+ }
503
+ };
504
+ var NotLoopSpecification = class extends LoopSpecification {
505
+ constructor(rule) {
506
+ super();
507
+ this.rule = rule;
508
+ }
509
+ isSatisfiedBy(agentLoop) {
510
+ return !this.rule.isSatisfiedBy(agentLoop);
511
+ }
512
+ };
513
+ var AwaitingInference = class extends LoopSpecification {
514
+ isSatisfiedBy(agentLoop) {
515
+ return agentLoop.loop.stateId === "awaiting_inference";
516
+ }
517
+ };
518
+ var AwaitingRequest = class extends LoopSpecification {
519
+ isSatisfiedBy(agentLoop) {
520
+ return agentLoop.loop.stateId === "awaiting_inference_request";
521
+ }
522
+ };
523
+ var AwaitingToolOutput = class extends LoopSpecification {
524
+ isSatisfiedBy(agentLoop) {
525
+ return agentLoop.loop.stateId === "awaiting_tool_output";
526
+ }
527
+ };
528
+ var ModelAnswered = class extends LoopSpecification {
529
+ isSatisfiedBy(agentLoop) {
530
+ const operations = agentLoop.loop.completedOperations;
531
+ const lastOperation = operations[operations.length - 1];
532
+ if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
533
+ return false;
534
+ }
535
+ return lastOperation.result.items.some((item) => item.type === "model_message");
536
+ }
537
+ };
538
+
539
+ // src/domain/agent/loop/action.ts
540
+ var LoopAction = class {
541
+ };
542
+ var RequestPreparationAction = class extends LoopAction {
543
+ constructor(request) {
544
+ super();
545
+ this.request = request;
546
+ }
547
+ execute(agentLoop) {
548
+ return {
549
+ type: "inference",
550
+ request: this.request
551
+ };
552
+ }
553
+ };
554
+ var InferenceAction = class extends LoopAction {
555
+ execute(agentLoop) {
556
+ const request = agentLoop.loop.request;
557
+ if (!request) {
558
+ return { type: "wait", reason: "no request" };
559
+ }
560
+ return { type: "inference", request };
561
+ }
562
+ };
563
+ var ToolUseAction = class extends LoopAction {
564
+ execute(agentLoop) {
565
+ if (!agentLoop.loop.pending) {
566
+ return { type: "wait" };
567
+ }
568
+ const pendingOperation = agentLoop.loop.pending;
569
+ if (pendingOperation.type === "tool_execution") {
570
+ return { type: "tool_use", call: pendingOperation.call };
571
+ }
572
+ return { type: "wait" };
573
+ }
574
+ };
575
+ var CompleteAction = class extends LoopAction {
576
+ constructor(reason) {
577
+ super();
578
+ this.reason = reason;
579
+ }
580
+ execute() {
581
+ return { type: "complete", reason: this.reason };
582
+ }
583
+ };
584
+
585
+ // src/domain/agent/loop/rule.ts
586
+ var LoopRule = class _LoopRule {
587
+ constructor(id, condition, action, priority) {
588
+ this._id = id;
589
+ this._priority = priority;
590
+ this._condition = condition;
591
+ this._action = action;
592
+ }
593
+ get id() {
594
+ return this._id;
595
+ }
596
+ get priority() {
597
+ return this._priority;
598
+ }
599
+ get condition() {
600
+ return this._condition;
601
+ }
602
+ get action() {
603
+ return this._action;
604
+ }
605
+ static create(spec) {
606
+ var _a;
607
+ const priority = (_a = spec.priority) != null ? _a : 0;
608
+ const id = crypto.randomUUID();
609
+ return new _LoopRule(id, spec.when, spec.then, priority);
610
+ }
611
+ static rehydrate(spec) {
612
+ var _a;
613
+ const priority = (_a = spec.priority) != null ? _a : 0;
614
+ const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
615
+ return rule;
616
+ }
617
+ };
618
+ var RuleBook = class _RuleBook {
619
+ constructor(rules) {
620
+ this.rules = rules;
621
+ }
622
+ addRule(rule) {
623
+ this.rules.push(rule);
624
+ }
625
+ removeRule(rule) {
626
+ this.rules = this.rules.filter((r) => r.id !== rule.id);
627
+ }
628
+ getRules() {
629
+ return this.rules;
630
+ }
631
+ static create(loopRuleParams) {
632
+ const rules = loopRuleParams.map((param) => LoopRule.create(param));
633
+ return new _RuleBook(rules);
634
+ }
635
+ static rehydrate(record) {
636
+ const rules = record.rules.map((rule) => LoopRule.rehydrate(rule));
637
+ return new _RuleBook(rules);
638
+ }
639
+ record() {
640
+ return {
641
+ rules: this.rules.map((rule) => ({
642
+ id: rule.id,
643
+ priority: rule.priority,
644
+ condition: rule.condition,
645
+ action: rule.action
646
+ }))
647
+ };
648
+ }
649
+ };
650
+ var defaultRules = (request) => [
651
+ {
652
+ when: new AwaitingRequest(),
653
+ then: new RequestPreparationAction(request)
654
+ },
655
+ {
656
+ when: new AwaitingInference(),
657
+ then: new InferenceAction()
658
+ },
659
+ {
660
+ when: new AwaitingToolOutput(),
661
+ then: new ToolUseAction()
662
+ },
663
+ {
664
+ when: new ModelAnswered(),
665
+ then: new CompleteAction("completed")
666
+ }
667
+ ];
668
+
477
669
  // src/domain/agent/loop/loop.ts
478
670
  var Loop = class _Loop {
479
- constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, rules) {
671
+ constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, ruleBook) {
480
672
  this.loopId = loopId;
481
673
  this.agentId = agentId;
482
674
  this.subject = subject;
@@ -486,7 +678,7 @@ var Loop = class _Loop {
486
678
  this.pendingOperation = pendingOperation;
487
679
  this.transitionHistory = transitionHistory;
488
680
  this.operationHistory = operationHistory;
489
- this.loopRules = rules;
681
+ this._ruleBook = ruleBook;
490
682
  }
491
683
  get id() {
492
684
  return this.loopId;
@@ -506,8 +698,11 @@ var Loop = class _Loop {
506
698
  get request() {
507
699
  return this.inferenceRequest;
508
700
  }
509
- get rules() {
510
- return this.loopRules;
701
+ get ruleBook() {
702
+ return this._ruleBook;
703
+ }
704
+ getSubject() {
705
+ return this.subject;
511
706
  }
512
707
  getAgentId() {
513
708
  return this.agentId;
@@ -523,7 +718,7 @@ var Loop = class _Loop {
523
718
  pendingOperation: this.pendingOperation,
524
719
  transitionHistory: [...this.transitionHistory],
525
720
  operationHistory: [...this.operationHistory],
526
- rules: [...this.rules]
721
+ ruleBook: this.ruleBook.record()
527
722
  };
528
723
  }
529
724
  moveToIdle(reason, transitionId) {
@@ -662,8 +857,9 @@ var Loop = class _Loop {
662
857
  throw new Error(`Cannot ${intent} a loop that is already ${this.state}`);
663
858
  }
664
859
  }
665
- static create(id, agentId, subject, rules) {
860
+ static create(agentId, subject, ruleBook) {
666
861
  const createdAt = SystemClock.now();
862
+ const id = UuidGenerator.create();
667
863
  return new _Loop(
668
864
  id,
669
865
  agentId,
@@ -674,7 +870,7 @@ var Loop = class _Loop {
674
870
  void 0,
675
871
  [],
676
872
  [],
677
- rules
873
+ ruleBook
678
874
  );
679
875
  }
680
876
  static rehydrate(record) {
@@ -688,7 +884,7 @@ var Loop = class _Loop {
688
884
  record.pendingOperation,
689
885
  [...record.transitionHistory],
690
886
  [...record.operationHistory],
691
- [...record.rules]
887
+ RuleBook.rehydrate(record.ruleBook)
692
888
  );
693
889
  }
694
890
  };
@@ -698,10 +894,18 @@ var CreateAgentLoopUseCase = class {
698
894
  constructor(loopRepository) {
699
895
  this.loopRepository = loopRepository;
700
896
  }
701
- async execute(subject, agentId, rules) {
702
- const loop = Loop.create(UuidGenerator.create(), agentId, subject, rules);
703
- await this.loopRepository.save(loop);
704
- return loop;
897
+ async execute(params) {
898
+ if ("rules" in params) {
899
+ const ruleBook = RuleBook.create(params.rules);
900
+ const loop = Loop.create(params.agentId, params.subject, ruleBook);
901
+ await this.loopRepository.save(loop);
902
+ return loop;
903
+ } else {
904
+ const ruleBook = RuleBook.create(defaultRules(params.request));
905
+ const loop = Loop.create(params.agentId, params.subject, ruleBook);
906
+ await this.loopRepository.save(loop);
907
+ return loop;
908
+ }
705
909
  }
706
910
  };
707
911
 
@@ -2237,7 +2441,7 @@ var supportedModels = [
2237
2441
  var RuleEngine = class {
2238
2442
  decide(agent, loop) {
2239
2443
  const agentLoop = { agent, loop };
2240
- const satisfiedRules = loop.rules.filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2444
+ const satisfiedRules = loop.ruleBook.getRules().filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2241
2445
  if (satisfiedRules.length === 0) {
2242
2446
  return void 0;
2243
2447
  }
@@ -2276,203 +2480,16 @@ var RunLoopUseCase = class {
2276
2480
  }
2277
2481
  };
2278
2482
 
2279
- // src/domain/agent/loop/specification.ts
2280
- var LoopSpecification = class {
2281
- and(other) {
2282
- return new AndLoopSpecification(this, other);
2283
- }
2284
- or(other) {
2285
- return new OrLoopSpecification(this, other);
2286
- }
2287
- not() {
2288
- return new NotLoopSpecification(this);
2289
- }
2290
- };
2291
- var AndLoopSpecification = class extends LoopSpecification {
2292
- constructor(left, right) {
2293
- super();
2294
- this.left = left;
2295
- this.right = right;
2296
- }
2297
- isSatisfiedBy(agentLoop) {
2298
- return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
2299
- }
2300
- };
2301
- var OrLoopSpecification = class extends LoopSpecification {
2302
- constructor(left, right) {
2303
- super();
2304
- this.left = left;
2305
- this.right = right;
2306
- }
2307
- isSatisfiedBy(agentLoop) {
2308
- return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
2309
- }
2310
- };
2311
- var NotLoopSpecification = class extends LoopSpecification {
2312
- constructor(rule) {
2313
- super();
2314
- this.rule = rule;
2315
- }
2316
- isSatisfiedBy(agentLoop) {
2317
- return !this.rule.isSatisfiedBy(agentLoop);
2318
- }
2319
- };
2320
- var Idle = class extends LoopSpecification {
2321
- isSatisfiedBy(agentLoop) {
2322
- return agentLoop.loop.stateId === "idle";
2323
- }
2324
- };
2325
- var AwaitingInference = class extends LoopSpecification {
2326
- isSatisfiedBy(agentLoop) {
2327
- return agentLoop.loop.stateId === "awaiting_inference";
2328
- }
2329
- };
2330
- var AwaitingInferenceRequest = class extends LoopSpecification {
2331
- isSatisfiedBy(agentLoop) {
2332
- return agentLoop.loop.stateId === "awaiting_inference_request";
2333
- }
2334
- };
2335
- var AwaitingToolOutput = class extends LoopSpecification {
2336
- isSatisfiedBy(agentLoop) {
2337
- return agentLoop.loop.stateId === "awaiting_tool_output";
2338
- }
2339
- };
2340
- var Stopped = class extends LoopSpecification {
2341
- isSatisfiedBy(agentLoop) {
2342
- return agentLoop.loop.stateId === "stopped";
2343
- }
2344
- };
2345
- var Completed = class extends LoopSpecification {
2346
- isSatisfiedBy(agentLoop) {
2347
- return agentLoop.loop.stateId === "completed";
2348
- }
2349
- };
2350
- var ModelAnswered = class extends LoopSpecification {
2351
- isSatisfiedBy(agentLoop) {
2352
- const operations = agentLoop.loop.completedOperations;
2353
- const lastOperation = operations[operations.length - 1];
2354
- if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
2355
- return false;
2356
- }
2357
- return lastOperation.result.items.some((item) => item.type === "model_message");
2358
- }
2359
- };
2360
-
2361
- // src/application/use-cases/loop-state.ts
2362
- var LoopStateUseCase = class {
2363
- execute(loopStateId) {
2364
- if (loopStateId === "idle") {
2365
- return new Idle();
2366
- }
2367
- if (loopStateId === "awaiting_inference_request") {
2368
- return new AwaitingInferenceRequest();
2369
- }
2370
- if (loopStateId === "awaiting_inference") {
2371
- return new AwaitingInference();
2372
- }
2373
- if (loopStateId === "awaiting_tool_output") {
2374
- return new AwaitingToolOutput();
2375
- }
2376
- if (loopStateId === "stopped") {
2377
- return new Stopped();
2378
- }
2379
- if (loopStateId === "completed") {
2380
- return new Completed();
2381
- }
2382
- throw new Error(`Invalid loop state: ${loopStateId}`);
2383
- }
2384
- };
2385
-
2386
- // src/domain/agent/loop/action.ts
2387
- var LoopAction = class {
2388
- };
2389
- var InferenceAction = class extends LoopAction {
2390
- execute(agentLoop) {
2391
- const request = agentLoop.loop.request;
2392
- if (!request) {
2393
- return { type: "wait", reason: "no request" };
2394
- }
2395
- return { type: "inference", request };
2396
- }
2397
- };
2398
- var ToolUseAction = class extends LoopAction {
2399
- execute(agentLoop) {
2400
- if (!agentLoop.loop.pending) {
2401
- return { type: "wait" };
2402
- }
2403
- const pendingOperation = agentLoop.loop.pending;
2404
- if (pendingOperation.type === "tool_execution") {
2405
- return { type: "tool_use", call: pendingOperation.call };
2406
- }
2407
- return { type: "wait" };
2408
- }
2409
- };
2410
- var CompleteAction = class extends LoopAction {
2411
- constructor(reason) {
2412
- super();
2413
- this.reason = reason;
2414
- }
2415
- execute() {
2416
- return { type: "complete", reason: this.reason };
2417
- }
2418
- };
2419
- var WaitAction = class extends LoopAction {
2420
- constructor(reason) {
2421
- super();
2422
- this.reason = reason;
2423
- }
2424
- execute() {
2425
- return { type: "wait", reason: this.reason };
2426
- }
2427
- };
2428
-
2429
- // src/domain/agent/loop/rule.ts
2430
- var LoopRule = class _LoopRule {
2431
- constructor(id, condition, action, priority) {
2432
- this._id = id;
2433
- this._priority = priority;
2434
- this._condition = condition;
2435
- this._action = action;
2436
- }
2437
- get id() {
2438
- return this._id;
2439
- }
2440
- get priority() {
2441
- return this._priority;
2442
- }
2443
- get condition() {
2444
- return this._condition;
2445
- }
2446
- get action() {
2447
- return this._action;
2448
- }
2449
- static create(spec) {
2450
- var _a;
2451
- const priority = (_a = spec.priority) != null ? _a : 0;
2452
- const id = crypto.randomUUID();
2453
- return new _LoopRule(id, spec.when, spec.then, priority);
2454
- }
2455
- static rehydrate(spec) {
2456
- var _a;
2457
- const priority = (_a = spec.priority) != null ? _a : 0;
2458
- const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
2459
- return rule;
2460
- }
2461
- };
2462
-
2463
2483
  // src/infrastructure/memory/runtime-memory.ts
2464
2484
  var RuntimeMemory = class {
2465
2485
  constructor(context) {
2466
2486
  this.context = context;
2467
2487
  }
2468
- getContext() {
2469
- return this.context;
2488
+ remember(items, participantId) {
2489
+ throw new Error("Method not implemented.");
2470
2490
  }
2471
- saveItem(item) {
2472
- this.context.items.push(item);
2473
- }
2474
- saveItems(items) {
2475
- this.context.items.push(...items);
2491
+ recall(topic, participantId) {
2492
+ throw new Error("Method not implemented.");
2476
2493
  }
2477
2494
  };
2478
2495
 
@@ -2570,8 +2587,7 @@ function defineAgentModule(config) {
2570
2587
  return await createAgentUseCase.execute(config2.name, config2.instruction, config2.tools, config2.handlers);
2571
2588
  }
2572
2589
  async function createLoop(params) {
2573
- const rules = params.rules.map((rule) => LoopRule.create(rule));
2574
- return await createLoopUseCase.execute(params.subject, params.agentId, rules);
2590
+ return await createLoopUseCase.execute(params);
2575
2591
  }
2576
2592
  const directiveExecutionStrategyResolver = new DirectiveExecutionStrategyResolver({
2577
2593
  inference: new InferenceExecutionStrategy(inferenceRunner),
@@ -2583,35 +2599,10 @@ function defineAgentModule(config) {
2583
2599
  async function runLoop(loopId) {
2584
2600
  return await runLoopUseCase.execute(loopId);
2585
2601
  }
2586
- const getLoopStateUseCase = new LoopStateUseCase();
2587
- function state(loopStateId) {
2588
- return getLoopStateUseCase.execute(loopStateId);
2589
- }
2590
- function modelAnswered() {
2591
- return new ModelAnswered();
2592
- }
2593
- function runInference() {
2594
- return new InferenceAction();
2595
- }
2596
- function toolUse() {
2597
- return new ToolUseAction();
2598
- }
2599
- function complete(reason) {
2600
- return new CompleteAction(reason != null ? reason : "completed");
2601
- }
2602
- function wait(reason) {
2603
- return new WaitAction(reason != null ? reason : "waiting");
2604
- }
2605
2602
  return {
2606
2603
  createAgent,
2607
2604
  createLoop,
2608
- runLoop,
2609
- state,
2610
- modelAnswered,
2611
- runInference,
2612
- toolUse,
2613
- complete,
2614
- wait
2605
+ runLoop
2615
2606
  };
2616
2607
  }
2617
2608
  // Annotate the CommonJS export names for ESM import in node: