@mozaik-ai/core 3.8.3 → 3.8.4
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/README.md +49 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Mozaik
|
|
2
2
|
|
|
3
|
-
**Mozaik** is
|
|
3
|
+
**Mozaik** is TypeScript framework for building reactive agents. It provides the easiest way to build collaborative, **event-driven agents** that can work together in parallel.
|
|
4
4
|
|
|
5
5
|
  
|
|
6
6
|
|
|
@@ -25,17 +25,21 @@ OPENAI_API_KEY=your-openai-key-here
|
|
|
25
25
|
|
|
26
26
|
## The agentic environment
|
|
27
27
|
|
|
28
|
-
`AgenticEnvironment` is
|
|
28
|
+
`AgenticEnvironment` is where everything happens. `Participant`s `join()` it, and from that moment on they can **listen to messages and events** flowing through the environment by overriding any of the handlers below:
|
|
29
29
|
|
|
30
|
-
|
|
|
31
|
-
|
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
34
|
-
| `
|
|
35
|
-
| `
|
|
36
|
-
| `
|
|
30
|
+
| Handler | Triggered when… |
|
|
31
|
+
| ------------------------------ | ------------------------------------------------------ |
|
|
32
|
+
| `onMessage` | any participant sends a message |
|
|
33
|
+
| `onFunctionCall` | its own inference returns a function call |
|
|
34
|
+
| `onExternalFunctionCall` | another agent's inference returns a function call |
|
|
35
|
+
| `onFunctionCallOutput` | its own function call runner returns a result |
|
|
36
|
+
| `onExternalFunctionCallOutput` | another agent's function call runner returns a result |
|
|
37
|
+
| `onReasoning` | its own inference returns a reasoning item |
|
|
38
|
+
| `onExternalReasoning` | another agent's inference returns a reasoning item |
|
|
39
|
+
| `onModelMessage` | its own inference returns an assistant message |
|
|
40
|
+
| `onExternalModelMessage` | another agent's inference returns an assistant message |
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
Every handler defaults to a no-op — override only the ones you care about.
|
|
39
43
|
|
|
40
44
|
```mermaid
|
|
41
45
|
flowchart LR
|
|
@@ -89,24 +93,49 @@ The environment fans every item out to every subscriber synchronously and withou
|
|
|
89
93
|
|
|
90
94
|
---
|
|
91
95
|
|
|
92
|
-
##
|
|
96
|
+
## Agent Loop Implementation
|
|
93
97
|
|
|
94
|
-
|
|
98
|
+
The easiest way to build **and control** an agent loop is to override three handlers on `BaseAgentParticipant`:
|
|
95
99
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
```ts
|
|
101
|
+
export class CustomAgent extends BaseAgentParticipant {
|
|
102
|
+
constructor(
|
|
103
|
+
inputSource: InputStream,
|
|
104
|
+
inferenceRunner: InferenceRunner,
|
|
105
|
+
functionCallRunner: FunctionCallRunner,
|
|
106
|
+
private readonly environment: AgenticEnvironment,
|
|
107
|
+
private readonly context: ModelContext,
|
|
108
|
+
private readonly model: GenerativeModel,
|
|
109
|
+
) {
|
|
110
|
+
super(inputSource, inferenceRunner, functionCallRunner)
|
|
111
|
+
}
|
|
99
112
|
|
|
100
|
-
|
|
113
|
+
async onMessage(message: string): Promise<void> {
|
|
114
|
+
this.context.addContextItem(UserMessageItem.create(message))
|
|
115
|
+
this.runInference(this.environment, this.context, this.model)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async onFunctionCall(item: FunctionCallItem): Promise<void> {
|
|
119
|
+
this.context.addContextItem(item)
|
|
120
|
+
this.executeFunctionCall(this.environment, item)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async onFunctionCallOutput(item: FunctionCallOutputItem): Promise<void> {
|
|
124
|
+
this.context.addContextItem(item)
|
|
125
|
+
this.runInference(this.environment, this.context, this.model)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
101
131
|
|
|
102
|
-
|
|
103
|
-
- Model-produced: `ModelMessageItem`, `FunctionCallItem`, `ReasoningItem`
|
|
132
|
+
## Reacting to external events
|
|
104
133
|
|
|
105
|
-
|
|
134
|
+
Participants can listen to external events and react by overriding methods like `onMessage`, `onExternalFunctionCall`, `onExternalFunctionCallOutput`, `onExternalReasoning`, and `onExternalModelMessage`.
|
|
106
135
|
|
|
107
136
|
### Passive observer
|
|
108
137
|
|
|
109
|
-
|
|
138
|
+
You can create observers that don't run inference themselves but watch what's happening in the conversation and take side actions (logging, metrics, persistence, etc.). Subclass `Participant` and override only the handlers you care about:
|
|
110
139
|
|
|
111
140
|
```ts
|
|
112
141
|
import { Participant, FunctionCallItem, FunctionCallOutputItem, ReasoningItem, ModelMessageItem } from "@mozaik-ai/core"
|