@mozaik-ai/core 4.2.0-beta.3 → 4.2.0-beta.5

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
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ Agent: () => Agent,
33
34
  Participant: () => Participant,
34
35
  SituationSpecification: () => SituationSpecification,
35
36
  SpaceEvent: () => SpaceEvent,
@@ -442,11 +443,6 @@ var CreateAgentUseCase = class {
442
443
  const id = UuidGenerator.create();
443
444
  const manifest = { id, name, role: "agent" };
444
445
  const memory = this.memoryFactory.create();
445
- const developerMessageItem = {
446
- type: "developer_message",
447
- text: instruction
448
- };
449
- memory.saveItem(developerMessageItem);
450
446
  const agentRecord = { id, manifest, tools, memory, handlers };
451
447
  const agent = Agent.create(agentRecord);
452
448
  await this.agentRepository.save(agent);
@@ -473,9 +469,206 @@ var InMemoryAgentRepository = class {
473
469
  }
474
470
  };
475
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
+
476
669
  // src/domain/agent/loop/loop.ts
477
670
  var Loop = class _Loop {
478
- constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, rules) {
671
+ constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, ruleBook) {
479
672
  this.loopId = loopId;
480
673
  this.agentId = agentId;
481
674
  this.subject = subject;
@@ -485,7 +678,7 @@ var Loop = class _Loop {
485
678
  this.pendingOperation = pendingOperation;
486
679
  this.transitionHistory = transitionHistory;
487
680
  this.operationHistory = operationHistory;
488
- this.loopRules = rules;
681
+ this._ruleBook = ruleBook;
489
682
  }
490
683
  get id() {
491
684
  return this.loopId;
@@ -505,8 +698,11 @@ var Loop = class _Loop {
505
698
  get request() {
506
699
  return this.inferenceRequest;
507
700
  }
508
- get rules() {
509
- return this.loopRules;
701
+ get ruleBook() {
702
+ return this._ruleBook;
703
+ }
704
+ getSubject() {
705
+ return this.subject;
510
706
  }
511
707
  getAgentId() {
512
708
  return this.agentId;
@@ -522,7 +718,7 @@ var Loop = class _Loop {
522
718
  pendingOperation: this.pendingOperation,
523
719
  transitionHistory: [...this.transitionHistory],
524
720
  operationHistory: [...this.operationHistory],
525
- rules: [...this.rules]
721
+ ruleBook: this.ruleBook.record()
526
722
  };
527
723
  }
528
724
  moveToIdle(reason, transitionId) {
@@ -661,8 +857,9 @@ var Loop = class _Loop {
661
857
  throw new Error(`Cannot ${intent} a loop that is already ${this.state}`);
662
858
  }
663
859
  }
664
- static create(id, agentId, subject, rules) {
860
+ static create(agentId, subject, ruleBook) {
665
861
  const createdAt = SystemClock.now();
862
+ const id = UuidGenerator.create();
666
863
  return new _Loop(
667
864
  id,
668
865
  agentId,
@@ -673,7 +870,7 @@ var Loop = class _Loop {
673
870
  void 0,
674
871
  [],
675
872
  [],
676
- rules
873
+ ruleBook
677
874
  );
678
875
  }
679
876
  static rehydrate(record) {
@@ -687,7 +884,7 @@ var Loop = class _Loop {
687
884
  record.pendingOperation,
688
885
  [...record.transitionHistory],
689
886
  [...record.operationHistory],
690
- [...record.rules]
887
+ RuleBook.rehydrate(record.ruleBook)
691
888
  );
692
889
  }
693
890
  };
@@ -697,10 +894,18 @@ var CreateAgentLoopUseCase = class {
697
894
  constructor(loopRepository) {
698
895
  this.loopRepository = loopRepository;
699
896
  }
700
- async execute(subject, agentId, rules) {
701
- const loop = Loop.create(UuidGenerator.create(), agentId, subject, rules);
702
- await this.loopRepository.save(loop);
703
- 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
+ }
704
909
  }
705
910
  };
706
911
 
@@ -2236,7 +2441,7 @@ var supportedModels = [
2236
2441
  var RuleEngine = class {
2237
2442
  decide(agent, loop) {
2238
2443
  const agentLoop = { agent, loop };
2239
- const satisfiedRules = loop.rules.filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2444
+ const satisfiedRules = loop.ruleBook.getRules().filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2240
2445
  if (satisfiedRules.length === 0) {
2241
2446
  return void 0;
2242
2447
  }
@@ -2275,203 +2480,16 @@ var RunLoopUseCase = class {
2275
2480
  }
2276
2481
  };
2277
2482
 
2278
- // src/domain/agent/loop/specification.ts
2279
- var LoopSpecification = class {
2280
- and(other) {
2281
- return new AndLoopSpecification(this, other);
2282
- }
2283
- or(other) {
2284
- return new OrLoopSpecification(this, other);
2285
- }
2286
- not() {
2287
- return new NotLoopSpecification(this);
2288
- }
2289
- };
2290
- var AndLoopSpecification = class extends LoopSpecification {
2291
- constructor(left, right) {
2292
- super();
2293
- this.left = left;
2294
- this.right = right;
2295
- }
2296
- isSatisfiedBy(agentLoop) {
2297
- return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
2298
- }
2299
- };
2300
- var OrLoopSpecification = class extends LoopSpecification {
2301
- constructor(left, right) {
2302
- super();
2303
- this.left = left;
2304
- this.right = right;
2305
- }
2306
- isSatisfiedBy(agentLoop) {
2307
- return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
2308
- }
2309
- };
2310
- var NotLoopSpecification = class extends LoopSpecification {
2311
- constructor(rule) {
2312
- super();
2313
- this.rule = rule;
2314
- }
2315
- isSatisfiedBy(agentLoop) {
2316
- return !this.rule.isSatisfiedBy(agentLoop);
2317
- }
2318
- };
2319
- var Idle = class extends LoopSpecification {
2320
- isSatisfiedBy(agentLoop) {
2321
- return agentLoop.loop.stateId === "idle";
2322
- }
2323
- };
2324
- var AwaitingInference = class extends LoopSpecification {
2325
- isSatisfiedBy(agentLoop) {
2326
- return agentLoop.loop.stateId === "awaiting_inference";
2327
- }
2328
- };
2329
- var AwaitingInferenceRequest = class extends LoopSpecification {
2330
- isSatisfiedBy(agentLoop) {
2331
- return agentLoop.loop.stateId === "awaiting_inference_request";
2332
- }
2333
- };
2334
- var AwaitingToolOutput = class extends LoopSpecification {
2335
- isSatisfiedBy(agentLoop) {
2336
- return agentLoop.loop.stateId === "awaiting_tool_output";
2337
- }
2338
- };
2339
- var Stopped = class extends LoopSpecification {
2340
- isSatisfiedBy(agentLoop) {
2341
- return agentLoop.loop.stateId === "stopped";
2342
- }
2343
- };
2344
- var Completed = class extends LoopSpecification {
2345
- isSatisfiedBy(agentLoop) {
2346
- return agentLoop.loop.stateId === "completed";
2347
- }
2348
- };
2349
- var ModelAnswered = class extends LoopSpecification {
2350
- isSatisfiedBy(agentLoop) {
2351
- const operations = agentLoop.loop.completedOperations;
2352
- const lastOperation = operations[operations.length - 1];
2353
- if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
2354
- return false;
2355
- }
2356
- return lastOperation.result.items.some((item) => item.type === "model_message");
2357
- }
2358
- };
2359
-
2360
- // src/application/use-cases/loop-state.ts
2361
- var LoopStateUseCase = class {
2362
- execute(loopStateId) {
2363
- if (loopStateId === "idle") {
2364
- return new Idle();
2365
- }
2366
- if (loopStateId === "awaiting_inference_request") {
2367
- return new AwaitingInferenceRequest();
2368
- }
2369
- if (loopStateId === "awaiting_inference") {
2370
- return new AwaitingInference();
2371
- }
2372
- if (loopStateId === "awaiting_tool_output") {
2373
- return new AwaitingToolOutput();
2374
- }
2375
- if (loopStateId === "stopped") {
2376
- return new Stopped();
2377
- }
2378
- if (loopStateId === "completed") {
2379
- return new Completed();
2380
- }
2381
- throw new Error(`Invalid loop state: ${loopStateId}`);
2382
- }
2383
- };
2384
-
2385
- // src/domain/agent/loop/action.ts
2386
- var LoopAction = class {
2387
- };
2388
- var InferenceAction = class extends LoopAction {
2389
- execute(agentLoop) {
2390
- const request = agentLoop.loop.request;
2391
- if (!request) {
2392
- return { type: "wait", reason: "no request" };
2393
- }
2394
- return { type: "inference", request };
2395
- }
2396
- };
2397
- var ToolUseAction = class extends LoopAction {
2398
- execute(agentLoop) {
2399
- if (!agentLoop.loop.pending) {
2400
- return { type: "wait" };
2401
- }
2402
- const pendingOperation = agentLoop.loop.pending;
2403
- if (pendingOperation.type === "tool_execution") {
2404
- return { type: "tool_use", call: pendingOperation.call };
2405
- }
2406
- return { type: "wait" };
2407
- }
2408
- };
2409
- var CompleteAction = class extends LoopAction {
2410
- constructor(reason) {
2411
- super();
2412
- this.reason = reason;
2413
- }
2414
- execute() {
2415
- return { type: "complete", reason: this.reason };
2416
- }
2417
- };
2418
- var WaitAction = class extends LoopAction {
2419
- constructor(reason) {
2420
- super();
2421
- this.reason = reason;
2422
- }
2423
- execute() {
2424
- return { type: "wait", reason: this.reason };
2425
- }
2426
- };
2427
-
2428
- // src/domain/agent/loop/rule.ts
2429
- var LoopRule = class _LoopRule {
2430
- constructor(id, condition, action, priority) {
2431
- this._id = id;
2432
- this._priority = priority;
2433
- this._condition = condition;
2434
- this._action = action;
2435
- }
2436
- get id() {
2437
- return this._id;
2438
- }
2439
- get priority() {
2440
- return this._priority;
2441
- }
2442
- get condition() {
2443
- return this._condition;
2444
- }
2445
- get action() {
2446
- return this._action;
2447
- }
2448
- static create(spec) {
2449
- var _a;
2450
- const priority = (_a = spec.priority) != null ? _a : 0;
2451
- const id = crypto.randomUUID();
2452
- return new _LoopRule(id, spec.when, spec.then, priority);
2453
- }
2454
- static rehydrate(spec) {
2455
- var _a;
2456
- const priority = (_a = spec.priority) != null ? _a : 0;
2457
- const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
2458
- return rule;
2459
- }
2460
- };
2461
-
2462
2483
  // src/infrastructure/memory/runtime-memory.ts
2463
2484
  var RuntimeMemory = class {
2464
2485
  constructor(context) {
2465
2486
  this.context = context;
2466
2487
  }
2467
- getContext() {
2468
- return this.context;
2488
+ remember(items, participantId) {
2489
+ throw new Error("Method not implemented.");
2469
2490
  }
2470
- saveItem(item) {
2471
- this.context.items.push(item);
2472
- }
2473
- saveItems(items) {
2474
- this.context.items.push(...items);
2491
+ recall(topic, participantId) {
2492
+ throw new Error("Method not implemented.");
2475
2493
  }
2476
2494
  };
2477
2495
 
@@ -2569,8 +2587,7 @@ function defineAgentModule(config) {
2569
2587
  return await createAgentUseCase.execute(config2.name, config2.instruction, config2.tools, config2.handlers);
2570
2588
  }
2571
2589
  async function createLoop(params) {
2572
- const rules = params.rules.map((rule) => LoopRule.create(rule));
2573
- return await createLoopUseCase.execute(params.subject, params.agentId, rules);
2590
+ return await createLoopUseCase.execute(params);
2574
2591
  }
2575
2592
  const directiveExecutionStrategyResolver = new DirectiveExecutionStrategyResolver({
2576
2593
  inference: new InferenceExecutionStrategy(inferenceRunner),
@@ -2582,39 +2599,15 @@ function defineAgentModule(config) {
2582
2599
  async function runLoop(loopId) {
2583
2600
  return await runLoopUseCase.execute(loopId);
2584
2601
  }
2585
- const getLoopStateUseCase = new LoopStateUseCase();
2586
- function state(loopStateId) {
2587
- return getLoopStateUseCase.execute(loopStateId);
2588
- }
2589
- function modelAnswered() {
2590
- return new ModelAnswered();
2591
- }
2592
- function runInference() {
2593
- return new InferenceAction();
2594
- }
2595
- function toolUse() {
2596
- return new ToolUseAction();
2597
- }
2598
- function complete(reason) {
2599
- return new CompleteAction(reason != null ? reason : "completed");
2600
- }
2601
- function wait(reason) {
2602
- return new WaitAction(reason != null ? reason : "waiting");
2603
- }
2604
2602
  return {
2605
2603
  createAgent,
2606
2604
  createLoop,
2607
- runLoop,
2608
- state,
2609
- modelAnswered,
2610
- runInference,
2611
- toolUse,
2612
- complete,
2613
- wait
2605
+ runLoop
2614
2606
  };
2615
2607
  }
2616
2608
  // Annotate the CommonJS export names for ESM import in node:
2617
2609
  0 && (module.exports = {
2610
+ Agent,
2618
2611
  Participant,
2619
2612
  SituationSpecification,
2620
2613
  SpaceEvent,