@mozaik-ai/core 4.2.0-beta.4 → 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.mjs CHANGED
@@ -402,11 +402,6 @@ var CreateAgentUseCase = class {
402
402
  const id = UuidGenerator.create();
403
403
  const manifest = { id, name, role: "agent" };
404
404
  const memory = this.memoryFactory.create();
405
- const developerMessageItem = {
406
- type: "developer_message",
407
- text: instruction
408
- };
409
- memory.saveItem(developerMessageItem);
410
405
  const agentRecord = { id, manifest, tools, memory, handlers };
411
406
  const agent = Agent.create(agentRecord);
412
407
  await this.agentRepository.save(agent);
@@ -433,9 +428,206 @@ var InMemoryAgentRepository = class {
433
428
  }
434
429
  };
435
430
 
431
+ // src/domain/agent/loop/specification.ts
432
+ var LoopSpecification = class {
433
+ and(other) {
434
+ return new AndLoopSpecification(this, other);
435
+ }
436
+ or(other) {
437
+ return new OrLoopSpecification(this, other);
438
+ }
439
+ not() {
440
+ return new NotLoopSpecification(this);
441
+ }
442
+ };
443
+ var AndLoopSpecification = class extends LoopSpecification {
444
+ constructor(left, right) {
445
+ super();
446
+ this.left = left;
447
+ this.right = right;
448
+ }
449
+ isSatisfiedBy(agentLoop) {
450
+ return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
451
+ }
452
+ };
453
+ var OrLoopSpecification = class extends LoopSpecification {
454
+ constructor(left, right) {
455
+ super();
456
+ this.left = left;
457
+ this.right = right;
458
+ }
459
+ isSatisfiedBy(agentLoop) {
460
+ return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
461
+ }
462
+ };
463
+ var NotLoopSpecification = class extends LoopSpecification {
464
+ constructor(rule) {
465
+ super();
466
+ this.rule = rule;
467
+ }
468
+ isSatisfiedBy(agentLoop) {
469
+ return !this.rule.isSatisfiedBy(agentLoop);
470
+ }
471
+ };
472
+ var AwaitingInference = class extends LoopSpecification {
473
+ isSatisfiedBy(agentLoop) {
474
+ return agentLoop.loop.stateId === "awaiting_inference";
475
+ }
476
+ };
477
+ var AwaitingRequest = class extends LoopSpecification {
478
+ isSatisfiedBy(agentLoop) {
479
+ return agentLoop.loop.stateId === "awaiting_inference_request";
480
+ }
481
+ };
482
+ var AwaitingToolOutput = class extends LoopSpecification {
483
+ isSatisfiedBy(agentLoop) {
484
+ return agentLoop.loop.stateId === "awaiting_tool_output";
485
+ }
486
+ };
487
+ var ModelAnswered = class extends LoopSpecification {
488
+ isSatisfiedBy(agentLoop) {
489
+ const operations = agentLoop.loop.completedOperations;
490
+ const lastOperation = operations[operations.length - 1];
491
+ if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
492
+ return false;
493
+ }
494
+ return lastOperation.result.items.some((item) => item.type === "model_message");
495
+ }
496
+ };
497
+
498
+ // src/domain/agent/loop/action.ts
499
+ var LoopAction = class {
500
+ };
501
+ var RequestPreparationAction = class extends LoopAction {
502
+ constructor(request) {
503
+ super();
504
+ this.request = request;
505
+ }
506
+ execute(agentLoop) {
507
+ return {
508
+ type: "inference",
509
+ request: this.request
510
+ };
511
+ }
512
+ };
513
+ var InferenceAction = class extends LoopAction {
514
+ execute(agentLoop) {
515
+ const request = agentLoop.loop.request;
516
+ if (!request) {
517
+ return { type: "wait", reason: "no request" };
518
+ }
519
+ return { type: "inference", request };
520
+ }
521
+ };
522
+ var ToolUseAction = class extends LoopAction {
523
+ execute(agentLoop) {
524
+ if (!agentLoop.loop.pending) {
525
+ return { type: "wait" };
526
+ }
527
+ const pendingOperation = agentLoop.loop.pending;
528
+ if (pendingOperation.type === "tool_execution") {
529
+ return { type: "tool_use", call: pendingOperation.call };
530
+ }
531
+ return { type: "wait" };
532
+ }
533
+ };
534
+ var CompleteAction = class extends LoopAction {
535
+ constructor(reason) {
536
+ super();
537
+ this.reason = reason;
538
+ }
539
+ execute() {
540
+ return { type: "complete", reason: this.reason };
541
+ }
542
+ };
543
+
544
+ // src/domain/agent/loop/rule.ts
545
+ var LoopRule = class _LoopRule {
546
+ constructor(id, condition, action, priority) {
547
+ this._id = id;
548
+ this._priority = priority;
549
+ this._condition = condition;
550
+ this._action = action;
551
+ }
552
+ get id() {
553
+ return this._id;
554
+ }
555
+ get priority() {
556
+ return this._priority;
557
+ }
558
+ get condition() {
559
+ return this._condition;
560
+ }
561
+ get action() {
562
+ return this._action;
563
+ }
564
+ static create(spec) {
565
+ var _a;
566
+ const priority = (_a = spec.priority) != null ? _a : 0;
567
+ const id = crypto.randomUUID();
568
+ return new _LoopRule(id, spec.when, spec.then, priority);
569
+ }
570
+ static rehydrate(spec) {
571
+ var _a;
572
+ const priority = (_a = spec.priority) != null ? _a : 0;
573
+ const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
574
+ return rule;
575
+ }
576
+ };
577
+ var RuleBook = class _RuleBook {
578
+ constructor(rules) {
579
+ this.rules = rules;
580
+ }
581
+ addRule(rule) {
582
+ this.rules.push(rule);
583
+ }
584
+ removeRule(rule) {
585
+ this.rules = this.rules.filter((r) => r.id !== rule.id);
586
+ }
587
+ getRules() {
588
+ return this.rules;
589
+ }
590
+ static create(loopRuleParams) {
591
+ const rules = loopRuleParams.map((param) => LoopRule.create(param));
592
+ return new _RuleBook(rules);
593
+ }
594
+ static rehydrate(record) {
595
+ const rules = record.rules.map((rule) => LoopRule.rehydrate(rule));
596
+ return new _RuleBook(rules);
597
+ }
598
+ record() {
599
+ return {
600
+ rules: this.rules.map((rule) => ({
601
+ id: rule.id,
602
+ priority: rule.priority,
603
+ condition: rule.condition,
604
+ action: rule.action
605
+ }))
606
+ };
607
+ }
608
+ };
609
+ var defaultRules = (request) => [
610
+ {
611
+ when: new AwaitingRequest(),
612
+ then: new RequestPreparationAction(request)
613
+ },
614
+ {
615
+ when: new AwaitingInference(),
616
+ then: new InferenceAction()
617
+ },
618
+ {
619
+ when: new AwaitingToolOutput(),
620
+ then: new ToolUseAction()
621
+ },
622
+ {
623
+ when: new ModelAnswered(),
624
+ then: new CompleteAction("completed")
625
+ }
626
+ ];
627
+
436
628
  // src/domain/agent/loop/loop.ts
437
629
  var Loop = class _Loop {
438
- constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, rules) {
630
+ constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, ruleBook) {
439
631
  this.loopId = loopId;
440
632
  this.agentId = agentId;
441
633
  this.subject = subject;
@@ -445,7 +637,7 @@ var Loop = class _Loop {
445
637
  this.pendingOperation = pendingOperation;
446
638
  this.transitionHistory = transitionHistory;
447
639
  this.operationHistory = operationHistory;
448
- this.loopRules = rules;
640
+ this._ruleBook = ruleBook;
449
641
  }
450
642
  get id() {
451
643
  return this.loopId;
@@ -465,8 +657,11 @@ var Loop = class _Loop {
465
657
  get request() {
466
658
  return this.inferenceRequest;
467
659
  }
468
- get rules() {
469
- return this.loopRules;
660
+ get ruleBook() {
661
+ return this._ruleBook;
662
+ }
663
+ getSubject() {
664
+ return this.subject;
470
665
  }
471
666
  getAgentId() {
472
667
  return this.agentId;
@@ -482,7 +677,7 @@ var Loop = class _Loop {
482
677
  pendingOperation: this.pendingOperation,
483
678
  transitionHistory: [...this.transitionHistory],
484
679
  operationHistory: [...this.operationHistory],
485
- rules: [...this.rules]
680
+ ruleBook: this.ruleBook.record()
486
681
  };
487
682
  }
488
683
  moveToIdle(reason, transitionId) {
@@ -621,8 +816,9 @@ var Loop = class _Loop {
621
816
  throw new Error(`Cannot ${intent} a loop that is already ${this.state}`);
622
817
  }
623
818
  }
624
- static create(id, agentId, subject, rules) {
819
+ static create(agentId, subject, ruleBook) {
625
820
  const createdAt = SystemClock.now();
821
+ const id = UuidGenerator.create();
626
822
  return new _Loop(
627
823
  id,
628
824
  agentId,
@@ -633,7 +829,7 @@ var Loop = class _Loop {
633
829
  void 0,
634
830
  [],
635
831
  [],
636
- rules
832
+ ruleBook
637
833
  );
638
834
  }
639
835
  static rehydrate(record) {
@@ -647,7 +843,7 @@ var Loop = class _Loop {
647
843
  record.pendingOperation,
648
844
  [...record.transitionHistory],
649
845
  [...record.operationHistory],
650
- [...record.rules]
846
+ RuleBook.rehydrate(record.ruleBook)
651
847
  );
652
848
  }
653
849
  };
@@ -657,10 +853,18 @@ var CreateAgentLoopUseCase = class {
657
853
  constructor(loopRepository) {
658
854
  this.loopRepository = loopRepository;
659
855
  }
660
- async execute(subject, agentId, rules) {
661
- const loop = Loop.create(UuidGenerator.create(), agentId, subject, rules);
662
- await this.loopRepository.save(loop);
663
- return loop;
856
+ async execute(params) {
857
+ if ("rules" in params) {
858
+ const ruleBook = RuleBook.create(params.rules);
859
+ const loop = Loop.create(params.agentId, params.subject, ruleBook);
860
+ await this.loopRepository.save(loop);
861
+ return loop;
862
+ } else {
863
+ const ruleBook = RuleBook.create(defaultRules(params.request));
864
+ const loop = Loop.create(params.agentId, params.subject, ruleBook);
865
+ await this.loopRepository.save(loop);
866
+ return loop;
867
+ }
664
868
  }
665
869
  };
666
870
 
@@ -2196,7 +2400,7 @@ var supportedModels = [
2196
2400
  var RuleEngine = class {
2197
2401
  decide(agent, loop) {
2198
2402
  const agentLoop = { agent, loop };
2199
- const satisfiedRules = loop.rules.filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2403
+ const satisfiedRules = loop.ruleBook.getRules().filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2200
2404
  if (satisfiedRules.length === 0) {
2201
2405
  return void 0;
2202
2406
  }
@@ -2235,203 +2439,16 @@ var RunLoopUseCase = class {
2235
2439
  }
2236
2440
  };
2237
2441
 
2238
- // src/domain/agent/loop/specification.ts
2239
- var LoopSpecification = class {
2240
- and(other) {
2241
- return new AndLoopSpecification(this, other);
2242
- }
2243
- or(other) {
2244
- return new OrLoopSpecification(this, other);
2245
- }
2246
- not() {
2247
- return new NotLoopSpecification(this);
2248
- }
2249
- };
2250
- var AndLoopSpecification = class extends LoopSpecification {
2251
- constructor(left, right) {
2252
- super();
2253
- this.left = left;
2254
- this.right = right;
2255
- }
2256
- isSatisfiedBy(agentLoop) {
2257
- return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
2258
- }
2259
- };
2260
- var OrLoopSpecification = class extends LoopSpecification {
2261
- constructor(left, right) {
2262
- super();
2263
- this.left = left;
2264
- this.right = right;
2265
- }
2266
- isSatisfiedBy(agentLoop) {
2267
- return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
2268
- }
2269
- };
2270
- var NotLoopSpecification = class extends LoopSpecification {
2271
- constructor(rule) {
2272
- super();
2273
- this.rule = rule;
2274
- }
2275
- isSatisfiedBy(agentLoop) {
2276
- return !this.rule.isSatisfiedBy(agentLoop);
2277
- }
2278
- };
2279
- var Idle = class extends LoopSpecification {
2280
- isSatisfiedBy(agentLoop) {
2281
- return agentLoop.loop.stateId === "idle";
2282
- }
2283
- };
2284
- var AwaitingInference = class extends LoopSpecification {
2285
- isSatisfiedBy(agentLoop) {
2286
- return agentLoop.loop.stateId === "awaiting_inference";
2287
- }
2288
- };
2289
- var AwaitingInferenceRequest = class extends LoopSpecification {
2290
- isSatisfiedBy(agentLoop) {
2291
- return agentLoop.loop.stateId === "awaiting_inference_request";
2292
- }
2293
- };
2294
- var AwaitingToolOutput = class extends LoopSpecification {
2295
- isSatisfiedBy(agentLoop) {
2296
- return agentLoop.loop.stateId === "awaiting_tool_output";
2297
- }
2298
- };
2299
- var Stopped = class extends LoopSpecification {
2300
- isSatisfiedBy(agentLoop) {
2301
- return agentLoop.loop.stateId === "stopped";
2302
- }
2303
- };
2304
- var Completed = class extends LoopSpecification {
2305
- isSatisfiedBy(agentLoop) {
2306
- return agentLoop.loop.stateId === "completed";
2307
- }
2308
- };
2309
- var ModelAnswered = class extends LoopSpecification {
2310
- isSatisfiedBy(agentLoop) {
2311
- const operations = agentLoop.loop.completedOperations;
2312
- const lastOperation = operations[operations.length - 1];
2313
- if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
2314
- return false;
2315
- }
2316
- return lastOperation.result.items.some((item) => item.type === "model_message");
2317
- }
2318
- };
2319
-
2320
- // src/application/use-cases/loop-state.ts
2321
- var LoopStateUseCase = class {
2322
- execute(loopStateId) {
2323
- if (loopStateId === "idle") {
2324
- return new Idle();
2325
- }
2326
- if (loopStateId === "awaiting_inference_request") {
2327
- return new AwaitingInferenceRequest();
2328
- }
2329
- if (loopStateId === "awaiting_inference") {
2330
- return new AwaitingInference();
2331
- }
2332
- if (loopStateId === "awaiting_tool_output") {
2333
- return new AwaitingToolOutput();
2334
- }
2335
- if (loopStateId === "stopped") {
2336
- return new Stopped();
2337
- }
2338
- if (loopStateId === "completed") {
2339
- return new Completed();
2340
- }
2341
- throw new Error(`Invalid loop state: ${loopStateId}`);
2342
- }
2343
- };
2344
-
2345
- // src/domain/agent/loop/action.ts
2346
- var LoopAction = class {
2347
- };
2348
- var InferenceAction = class extends LoopAction {
2349
- execute(agentLoop) {
2350
- const request = agentLoop.loop.request;
2351
- if (!request) {
2352
- return { type: "wait", reason: "no request" };
2353
- }
2354
- return { type: "inference", request };
2355
- }
2356
- };
2357
- var ToolUseAction = class extends LoopAction {
2358
- execute(agentLoop) {
2359
- if (!agentLoop.loop.pending) {
2360
- return { type: "wait" };
2361
- }
2362
- const pendingOperation = agentLoop.loop.pending;
2363
- if (pendingOperation.type === "tool_execution") {
2364
- return { type: "tool_use", call: pendingOperation.call };
2365
- }
2366
- return { type: "wait" };
2367
- }
2368
- };
2369
- var CompleteAction = class extends LoopAction {
2370
- constructor(reason) {
2371
- super();
2372
- this.reason = reason;
2373
- }
2374
- execute() {
2375
- return { type: "complete", reason: this.reason };
2376
- }
2377
- };
2378
- var WaitAction = class extends LoopAction {
2379
- constructor(reason) {
2380
- super();
2381
- this.reason = reason;
2382
- }
2383
- execute() {
2384
- return { type: "wait", reason: this.reason };
2385
- }
2386
- };
2387
-
2388
- // src/domain/agent/loop/rule.ts
2389
- var LoopRule = class _LoopRule {
2390
- constructor(id, condition, action, priority) {
2391
- this._id = id;
2392
- this._priority = priority;
2393
- this._condition = condition;
2394
- this._action = action;
2395
- }
2396
- get id() {
2397
- return this._id;
2398
- }
2399
- get priority() {
2400
- return this._priority;
2401
- }
2402
- get condition() {
2403
- return this._condition;
2404
- }
2405
- get action() {
2406
- return this._action;
2407
- }
2408
- static create(spec) {
2409
- var _a;
2410
- const priority = (_a = spec.priority) != null ? _a : 0;
2411
- const id = crypto.randomUUID();
2412
- return new _LoopRule(id, spec.when, spec.then, priority);
2413
- }
2414
- static rehydrate(spec) {
2415
- var _a;
2416
- const priority = (_a = spec.priority) != null ? _a : 0;
2417
- const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
2418
- return rule;
2419
- }
2420
- };
2421
-
2422
2442
  // src/infrastructure/memory/runtime-memory.ts
2423
2443
  var RuntimeMemory = class {
2424
2444
  constructor(context) {
2425
2445
  this.context = context;
2426
2446
  }
2427
- getContext() {
2428
- return this.context;
2447
+ remember(items, participantId) {
2448
+ throw new Error("Method not implemented.");
2429
2449
  }
2430
- saveItem(item) {
2431
- this.context.items.push(item);
2432
- }
2433
- saveItems(items) {
2434
- this.context.items.push(...items);
2450
+ recall(topic, participantId) {
2451
+ throw new Error("Method not implemented.");
2435
2452
  }
2436
2453
  };
2437
2454
 
@@ -2529,8 +2546,7 @@ function defineAgentModule(config) {
2529
2546
  return await createAgentUseCase.execute(config2.name, config2.instruction, config2.tools, config2.handlers);
2530
2547
  }
2531
2548
  async function createLoop(params) {
2532
- const rules = params.rules.map((rule) => LoopRule.create(rule));
2533
- return await createLoopUseCase.execute(params.subject, params.agentId, rules);
2549
+ return await createLoopUseCase.execute(params);
2534
2550
  }
2535
2551
  const directiveExecutionStrategyResolver = new DirectiveExecutionStrategyResolver({
2536
2552
  inference: new InferenceExecutionStrategy(inferenceRunner),
@@ -2542,35 +2558,10 @@ function defineAgentModule(config) {
2542
2558
  async function runLoop(loopId) {
2543
2559
  return await runLoopUseCase.execute(loopId);
2544
2560
  }
2545
- const getLoopStateUseCase = new LoopStateUseCase();
2546
- function state(loopStateId) {
2547
- return getLoopStateUseCase.execute(loopStateId);
2548
- }
2549
- function modelAnswered() {
2550
- return new ModelAnswered();
2551
- }
2552
- function runInference() {
2553
- return new InferenceAction();
2554
- }
2555
- function toolUse() {
2556
- return new ToolUseAction();
2557
- }
2558
- function complete(reason) {
2559
- return new CompleteAction(reason != null ? reason : "completed");
2560
- }
2561
- function wait(reason) {
2562
- return new WaitAction(reason != null ? reason : "waiting");
2563
- }
2564
2561
  return {
2565
2562
  createAgent,
2566
2563
  createLoop,
2567
- runLoop,
2568
- state,
2569
- modelAnswered,
2570
- runInference,
2571
- toolUse,
2572
- complete,
2573
- wait
2564
+ runLoop
2574
2565
  };
2575
2566
  }
2576
2567
  export {