@mozaik-ai/core 4.0.0-beta.8 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -186,6 +186,24 @@ interface FunctionCallParams {
186
186
  inferenceInput: InferenceInput;
187
187
  }
188
188
 
189
+ interface InterceptionHandler {
190
+ isSatisfiedBy(transition: ExecutableTransition): boolean;
191
+ handle(transition: ExecutableTransition): Promise<ExecutableTransition>;
192
+ }
193
+ type InterceptionOutput = {
194
+ type: "continue";
195
+ transition: LoopTransition;
196
+ } | {
197
+ type: "pause";
198
+ pendingTransition: LoopTransition;
199
+ } | {
200
+ type: "stop";
201
+ reason?: string;
202
+ };
203
+ interface InterceptionParams {
204
+ pendingTransition: LoopTransition;
205
+ }
206
+
189
207
  type LoopStateId = "context_update" | "inference" | "inference_streaming" | "function_call" | "model_message" | "idle";
190
208
  type ReceivedMessage = {
191
209
  content: string;
@@ -227,19 +245,15 @@ interface LoopStateContract {
227
245
  output: void;
228
246
  };
229
247
  }
230
- type InterceptionOutput = {
231
- type: "continue";
232
- transition: LoopTransition;
233
- } | {
234
- type: "pause";
235
- pendingTransition: LoopTransition;
236
- } | {
237
- type: "stop";
238
- reason?: string;
239
- };
240
- interface InterceptionParams {
241
- pendingTransition: LoopTransition;
242
- }
248
+ type ExecutableLoopStateId = Exclude<LoopStateId, "idle">;
249
+ type ExecutableTransition = LoopTransition<ExecutableLoopStateId>;
250
+ type LoopStateExecution<TStateId extends ExecutableLoopStateId = ExecutableLoopStateId> = {
251
+ [K in TStateId]: {
252
+ stateId: K;
253
+ input: LoopStateContract[K]["input"];
254
+ output: LoopStateContract[K]["output"];
255
+ };
256
+ }[TStateId];
243
257
  type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
244
258
  [K in TStateId]: {
245
259
  nextStateId: K;
@@ -247,6 +261,23 @@ type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
247
261
  };
248
262
  }[TStateId];
249
263
 
264
+ declare class InputTokenDetails {
265
+ readonly cached_tokens: number;
266
+ constructor(cached_tokens: number);
267
+ }
268
+ declare class OutputTokenDetails {
269
+ readonly reasoning_tokens: number;
270
+ constructor(reasoning_tokens: number);
271
+ }
272
+ declare class TokenUsage {
273
+ readonly inputTokens: number;
274
+ readonly outputTokens: number;
275
+ readonly totalTokens: number;
276
+ readonly inputTokenDetails: InputTokenDetails;
277
+ readonly outputTokenDetails: OutputTokenDetails;
278
+ constructor(inputTokens: number, outputTokens: number, totalTokens: number, inputTokenDetails: InputTokenDetails, outputTokenDetails: OutputTokenDetails);
279
+ }
280
+
250
281
  type InferenceInput = {
251
282
  model: string;
252
283
  maxOutputTokens?: number;
@@ -259,6 +290,7 @@ type InferenceInput = {
259
290
  type InferenceItem = FunctionCallItem | ReasoningItem | ModelMessageItem;
260
291
  type InferenceOutput = {
261
292
  items: InferenceItem[];
293
+ tokenUsage: TokenUsage | undefined;
262
294
  rowResponse: any;
263
295
  };
264
296
  interface InferenceRunner {
@@ -311,23 +343,6 @@ interface ModelContextRepository {
311
343
  getByProjectId(projectId: string): Promise<ModelContext[]>;
312
344
  }
313
345
 
314
- declare class InputTokenDetails {
315
- readonly cached_tokens: number;
316
- constructor(cached_tokens: number);
317
- }
318
- declare class OutputTokenDetails {
319
- readonly reasoning_tokens: number;
320
- constructor(reasoning_tokens: number);
321
- }
322
- declare class TokenUsage {
323
- readonly inputTokens: number;
324
- readonly outputTokens: number;
325
- readonly totalTokens: number;
326
- readonly inputTokenDetails: InputTokenDetails;
327
- readonly outputTokenDetails: OutputTokenDetails;
328
- constructor(inputTokens: number, outputTokens: number, totalTokens: number, inputTokenDetails: InputTokenDetails, outputTokenDetails: OutputTokenDetails);
329
- }
330
-
331
346
  interface McpServerConfig {
332
347
  /** The MCP server URL, e.g. https://api.githubcopilot.com/mcp/ */
333
348
  url: string;
@@ -459,11 +474,6 @@ declare abstract class RuntimeState {
459
474
  getParticipants(): readonly Participant[];
460
475
  }
461
476
 
462
- interface InterceptionHandler {
463
- isSatisfiedBy(transition: LoopTransition): boolean;
464
- handle(transition: LoopTransition): Promise<LoopTransition>;
465
- }
466
-
467
477
  declare class EventProcessor {
468
478
  process(event: SemanticEvent, consumer: Participant): void;
469
479
  }
@@ -497,7 +507,7 @@ declare function defineRuntime<TRuntimeState extends RuntimeState>(): {
497
507
  leave: (participant: Participant) => void;
498
508
  sendMessage: (message: string, senderId: string) => void;
499
509
  sendEvent: (event: SemanticEvent, senderId: string) => void;
500
- runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler: InterceptionHandler) => void;
510
+ runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler?: InterceptionHandler) => void;
501
511
  };
502
512
 
503
513
  declare const supportedModels: GenerativeModel[];
@@ -615,4 +625,4 @@ declare class DefaultInferenceRunner implements InferenceRunner {
615
625
  stream(input: InferenceInput): AsyncGenerator<SemanticEvent>;
616
626
  }
617
627
 
618
- export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
628
+ export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, type ExecutableLoopStateId, type ExecutableTransition, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, type InterceptionHandler, type LoopStateExecution, type LoopTransition, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
package/dist/index.d.ts CHANGED
@@ -186,6 +186,24 @@ interface FunctionCallParams {
186
186
  inferenceInput: InferenceInput;
187
187
  }
188
188
 
189
+ interface InterceptionHandler {
190
+ isSatisfiedBy(transition: ExecutableTransition): boolean;
191
+ handle(transition: ExecutableTransition): Promise<ExecutableTransition>;
192
+ }
193
+ type InterceptionOutput = {
194
+ type: "continue";
195
+ transition: LoopTransition;
196
+ } | {
197
+ type: "pause";
198
+ pendingTransition: LoopTransition;
199
+ } | {
200
+ type: "stop";
201
+ reason?: string;
202
+ };
203
+ interface InterceptionParams {
204
+ pendingTransition: LoopTransition;
205
+ }
206
+
189
207
  type LoopStateId = "context_update" | "inference" | "inference_streaming" | "function_call" | "model_message" | "idle";
190
208
  type ReceivedMessage = {
191
209
  content: string;
@@ -227,19 +245,15 @@ interface LoopStateContract {
227
245
  output: void;
228
246
  };
229
247
  }
230
- type InterceptionOutput = {
231
- type: "continue";
232
- transition: LoopTransition;
233
- } | {
234
- type: "pause";
235
- pendingTransition: LoopTransition;
236
- } | {
237
- type: "stop";
238
- reason?: string;
239
- };
240
- interface InterceptionParams {
241
- pendingTransition: LoopTransition;
242
- }
248
+ type ExecutableLoopStateId = Exclude<LoopStateId, "idle">;
249
+ type ExecutableTransition = LoopTransition<ExecutableLoopStateId>;
250
+ type LoopStateExecution<TStateId extends ExecutableLoopStateId = ExecutableLoopStateId> = {
251
+ [K in TStateId]: {
252
+ stateId: K;
253
+ input: LoopStateContract[K]["input"];
254
+ output: LoopStateContract[K]["output"];
255
+ };
256
+ }[TStateId];
243
257
  type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
244
258
  [K in TStateId]: {
245
259
  nextStateId: K;
@@ -247,6 +261,23 @@ type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
247
261
  };
248
262
  }[TStateId];
249
263
 
264
+ declare class InputTokenDetails {
265
+ readonly cached_tokens: number;
266
+ constructor(cached_tokens: number);
267
+ }
268
+ declare class OutputTokenDetails {
269
+ readonly reasoning_tokens: number;
270
+ constructor(reasoning_tokens: number);
271
+ }
272
+ declare class TokenUsage {
273
+ readonly inputTokens: number;
274
+ readonly outputTokens: number;
275
+ readonly totalTokens: number;
276
+ readonly inputTokenDetails: InputTokenDetails;
277
+ readonly outputTokenDetails: OutputTokenDetails;
278
+ constructor(inputTokens: number, outputTokens: number, totalTokens: number, inputTokenDetails: InputTokenDetails, outputTokenDetails: OutputTokenDetails);
279
+ }
280
+
250
281
  type InferenceInput = {
251
282
  model: string;
252
283
  maxOutputTokens?: number;
@@ -259,6 +290,7 @@ type InferenceInput = {
259
290
  type InferenceItem = FunctionCallItem | ReasoningItem | ModelMessageItem;
260
291
  type InferenceOutput = {
261
292
  items: InferenceItem[];
293
+ tokenUsage: TokenUsage | undefined;
262
294
  rowResponse: any;
263
295
  };
264
296
  interface InferenceRunner {
@@ -311,23 +343,6 @@ interface ModelContextRepository {
311
343
  getByProjectId(projectId: string): Promise<ModelContext[]>;
312
344
  }
313
345
 
314
- declare class InputTokenDetails {
315
- readonly cached_tokens: number;
316
- constructor(cached_tokens: number);
317
- }
318
- declare class OutputTokenDetails {
319
- readonly reasoning_tokens: number;
320
- constructor(reasoning_tokens: number);
321
- }
322
- declare class TokenUsage {
323
- readonly inputTokens: number;
324
- readonly outputTokens: number;
325
- readonly totalTokens: number;
326
- readonly inputTokenDetails: InputTokenDetails;
327
- readonly outputTokenDetails: OutputTokenDetails;
328
- constructor(inputTokens: number, outputTokens: number, totalTokens: number, inputTokenDetails: InputTokenDetails, outputTokenDetails: OutputTokenDetails);
329
- }
330
-
331
346
  interface McpServerConfig {
332
347
  /** The MCP server URL, e.g. https://api.githubcopilot.com/mcp/ */
333
348
  url: string;
@@ -459,11 +474,6 @@ declare abstract class RuntimeState {
459
474
  getParticipants(): readonly Participant[];
460
475
  }
461
476
 
462
- interface InterceptionHandler {
463
- isSatisfiedBy(transition: LoopTransition): boolean;
464
- handle(transition: LoopTransition): Promise<LoopTransition>;
465
- }
466
-
467
477
  declare class EventProcessor {
468
478
  process(event: SemanticEvent, consumer: Participant): void;
469
479
  }
@@ -497,7 +507,7 @@ declare function defineRuntime<TRuntimeState extends RuntimeState>(): {
497
507
  leave: (participant: Participant) => void;
498
508
  sendMessage: (message: string, senderId: string) => void;
499
509
  sendEvent: (event: SemanticEvent, senderId: string) => void;
500
- runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler: InterceptionHandler) => void;
510
+ runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler?: InterceptionHandler) => void;
501
511
  };
502
512
 
503
513
  declare const supportedModels: GenerativeModel[];
@@ -615,4 +625,4 @@ declare class DefaultInferenceRunner implements InferenceRunner {
615
625
  stream(input: InferenceInput): AsyncGenerator<SemanticEvent>;
616
626
  }
617
627
 
618
- export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
628
+ export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, type ExecutableLoopStateId, type ExecutableTransition, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, type InterceptionHandler, type LoopStateExecution, type LoopTransition, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
package/dist/index.js CHANGED
@@ -622,20 +622,20 @@ var AgentLoop = class _AgentLoop {
622
622
  this.interceptionHandler = interceptionHandler;
623
623
  }
624
624
  async run(message, loopVisitor) {
625
+ var _a;
625
626
  let transition = {
626
627
  nextStateId: "context_update",
627
628
  input: message
628
629
  };
629
630
  while (transition.nextStateId !== "idle") {
630
- const isInterceptionSatisfied = this.interceptionHandler.isSatisfiedBy(transition);
631
- if (isInterceptionSatisfied) {
631
+ const isInterceptionSatisfied = (_a = this.interceptionHandler) == null ? void 0 : _a.isSatisfiedBy(transition);
632
+ if (isInterceptionSatisfied && this.interceptionHandler) {
632
633
  loopVisitor.visitInterceptionStarted(transition);
633
634
  transition = await this.interceptionHandler.handle(transition);
634
635
  loopVisitor.visitInterceptionFinished(transition);
635
- } else {
636
- const execution = await this.stateExecutor.execute(transition, loopVisitor);
637
- transition = this.transitionResolver.resolve(execution);
638
636
  }
637
+ const execution = await this.stateExecutor.execute(transition, loopVisitor);
638
+ transition = this.transitionResolver.resolve(execution);
639
639
  }
640
640
  }
641
641
  getLoopId() {
@@ -877,11 +877,13 @@ var ModelMessageToIdleRule = class {
877
877
  };
878
878
 
879
879
  // src/domain/agentic-environment/loop/event-publisher-visitor.ts
880
+ var import_cloud_sdk = require("@mozaik-ai/cloud-sdk");
880
881
  var EventPublisherLoopVisitor = class {
881
882
  constructor(agentId, loopId, runtime) {
882
883
  this.agentId = agentId;
883
884
  this.loopId = loopId;
884
885
  this.runtime = runtime;
886
+ this.cloud = (0, import_cloud_sdk.createCloudClient)();
885
887
  }
886
888
  visitContextUpdateStarted(input) {
887
889
  this.publish("context_update.started", { ...input, loopId: this.loopId });
@@ -914,12 +916,11 @@ var EventPublisherLoopVisitor = class {
914
916
  this.publish("interception.finished", transition);
915
917
  }
916
918
  publish(type, payload) {
917
- this.runtime.publish({
918
- type,
919
- producerId: this.agentId,
920
- occurredAt: /* @__PURE__ */ new Date(),
921
- payload: { ...payload, loopId: this.loopId }
922
- });
919
+ const event = new SemanticEvent(type, this.agentId, /* @__PURE__ */ new Date(), payload);
920
+ this.runtime.publish(event);
921
+ if (this.cloud.enabled) {
922
+ this.cloud.send(event);
923
+ }
923
924
  }
924
925
  };
925
926
 
@@ -1129,7 +1130,7 @@ var AnthropicMessagesMapper = class {
1129
1130
  );
1130
1131
  }
1131
1132
  }
1132
- return { items, rowResponse: response };
1133
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1133
1134
  }
1134
1135
  };
1135
1136
 
@@ -1317,6 +1318,7 @@ var OpenAIResponsesMapper = class {
1317
1318
  }
1318
1319
  return {
1319
1320
  items,
1321
+ tokenUsage: this.extractTokenUsage(response),
1320
1322
  rowResponse: response
1321
1323
  };
1322
1324
  }
@@ -1505,7 +1507,7 @@ var GeminiGenerateContentMapper = class {
1505
1507
  );
1506
1508
  }
1507
1509
  }
1508
- return { items, rowResponse: response };
1510
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1509
1511
  }
1510
1512
  };
1511
1513
 
@@ -1628,7 +1630,7 @@ var OpenAIChatCompletionsMapper = class {
1628
1630
  const items = [];
1629
1631
  const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1630
1632
  if (!message) {
1631
- return { items: [], rowResponse: response };
1633
+ return { items: [], tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1632
1634
  }
1633
1635
  if (message.reasoning_content) {
1634
1636
  items.push(
@@ -1651,7 +1653,7 @@ var OpenAIChatCompletionsMapper = class {
1651
1653
  })
1652
1654
  );
1653
1655
  }
1654
- return { items, rowResponse: response };
1656
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1655
1657
  }
1656
1658
  };
1657
1659