@mozaik-ai/core 3.6.1 → 3.6.2
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 +269 -186
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Mozaik
|
|
2
2
|
|
|
3
|
-
**Mozaik** is a TypeScript framework for building
|
|
3
|
+
**Mozaik** is a TypeScript framework for building AI agents that share an **agentic environment** instead of being orchestrated through rigid pipelines.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
In Mozaik, humans, agents, observers, and tools are all `Participant`s of the same `AgenticEnvironment`. Each participant runs **non-blocking** and streams typed `ContextItem`s into the environment. Every other participant sees those items in real time and can react, intercept, or stay silent — without any central scheduler.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -21,283 +21,366 @@ OPENAI_API_KEY=your-openai-key-here
|
|
|
21
21
|
|
|
22
22
|
---
|
|
23
23
|
|
|
24
|
-
##
|
|
24
|
+
## The agentic environment
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
`AgenticEnvironment` is a broadcast bus for typed context items. `Participant`s `join()` it, and any item produced by one participant is delivered to every subscriber's `onContextItem(source, item)` callback.
|
|
27
27
|
|
|
28
28
|
```mermaid
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Agent
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
flowchart LR
|
|
30
|
+
Human[BaseHumanParticipant] -->|streamInput| Env(("AgenticEnvironment"))
|
|
31
|
+
Agent[BaseAgentParticipant] -->|"runInference / executeFunctionCall"| Env
|
|
32
|
+
Observer[Custom Participant] -->|join| Env
|
|
33
|
+
Env -->|onContextItem| Human
|
|
34
|
+
Env -->|onContextItem| Agent
|
|
35
|
+
Env -->|onContextItem| Observer
|
|
35
36
|
```
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
| ----------------------- | ------------------------------------------------------ |
|
|
39
|
-
| **AgentSociety** | Runs multiple agents concurrently (experimental) |
|
|
40
|
-
| **Agent** | Lifecycle callbacks, visitor hooks, high-level `run()` |
|
|
41
|
-
| **AgentRuntime** | Drives the loop, dispatches hooks, manages errors |
|
|
42
|
-
| **AgentLoop + Context** | FSM transitions and typed context model |
|
|
38
|
+
The environment itself is intentionally minimal — it owns the subscriber list and the delivery loop, nothing more:
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
```16:31:src/domain/agentic-environment/agentic-environment.ts
|
|
41
|
+
deliverContextItem(source: Participant, item: ContextItem): void {
|
|
42
|
+
for (const subscriber of this.subscribers) {
|
|
43
|
+
subscriber.onContextItem(source, item)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
49
46
|
|
|
50
|
-
|
|
47
|
+
async start() {
|
|
48
|
+
this.isActive = true
|
|
49
|
+
while (this.isActive) {
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
INFERENCE_PENDING --> FUNCTION_CALL_PENDING: model returned a function call
|
|
56
|
-
INFERENCE_PENDING --> MODEL_MESSAGE_RECEIVED: model returned a message
|
|
57
|
-
FUNCTION_CALL_PENDING --> INFERENCE_PENDING: tool executed, feeding output back
|
|
58
|
-
MODEL_MESSAGE_RECEIVED --> [*]: execution complete
|
|
54
|
+
stop() {
|
|
55
|
+
this.isActive = false
|
|
56
|
+
}
|
|
59
57
|
```
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
A `Participant` is anything that can subscribe and receive items. The only required method is `onContextItem`:
|
|
62
60
|
|
|
63
|
-
|
|
61
|
+
```4:29:src/domain/agentic-environment/participant.ts
|
|
62
|
+
export abstract class Participant {
|
|
64
63
|
|
|
65
|
-
|
|
64
|
+
private environments: AgenticEnvironment[] = []
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
runtime.on(HookId.BEFORE_INFERENCE, async (context) => {
|
|
75
|
-
console.log("About to call the model...")
|
|
76
|
-
})
|
|
66
|
+
join(environment: AgenticEnvironment) {
|
|
67
|
+
if (this.isJoinedTo(environment)) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
environment.subscribe(this)
|
|
71
|
+
this.environments.push(environment)
|
|
72
|
+
}
|
|
77
73
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
leave(environment: AgenticEnvironment) {
|
|
75
|
+
if (!this.isJoinedTo(environment)) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
environment.unsubscribe(this)
|
|
79
|
+
this.environments = this.environments.filter((e) => e !== environment)
|
|
80
|
+
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
82
|
+
protected isJoinedTo(environment: AgenticEnvironment): boolean {
|
|
83
|
+
return this.environments.includes(environment)
|
|
84
|
+
}
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
})
|
|
86
|
+
abstract onContextItem(source: Participant, item: ContextItem): Promise<void>
|
|
87
|
+
}
|
|
89
88
|
```
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
| Hook | Fires |
|
|
94
|
-
| ---------------------- | ------------------------------------ |
|
|
95
|
-
| `BEFORE_INFERENCE` | Before the model is called |
|
|
96
|
-
| `AFTER_INFERENCE` | After the model responds |
|
|
97
|
-
| `BEFORE_FUNCTION_CALL` | Before a tool is invoked |
|
|
98
|
-
| `AFTER_FUNCTION_CALL` | After a tool returns |
|
|
99
|
-
| `BEFORE_MODEL_MESSAGE` | Before the final message is received |
|
|
100
|
-
| `AFTER_MODEL_MESSAGE` | After the final message is received |
|
|
101
|
-
| `ON_ERROR` | When any state handler throws |
|
|
90
|
+
Notice what's *not* there: no FSM, no hook bus, no per-participant queues, no orchestrator. Everything else in the framework is a participant or a generator that participants pull from.
|
|
102
91
|
|
|
103
92
|
---
|
|
104
93
|
|
|
105
|
-
|
|
94
|
+
## Non-blocking participants
|
|
106
95
|
|
|
107
|
-
|
|
96
|
+
Mozaik ships two ready-to-use participants:
|
|
108
97
|
|
|
109
|
-
|
|
110
|
-
|
|
98
|
+
| Participant | Capabilities | Pulls from |
|
|
99
|
+
| --- | --- | --- |
|
|
100
|
+
| `BaseHumanParticipant` | `InputCapable` | `InputItemSource` |
|
|
101
|
+
| `BaseAgentParticipant` | `InputCapable`, `InferenceCapable`, `FunctionCallCapable` | `InputItemSource`, `InferenceRunner`, `FunctionCallRunner` |
|
|
111
102
|
|
|
112
|
-
|
|
113
|
-
constructor() {
|
|
114
|
-
super(new AgentRuntime())
|
|
115
|
-
}
|
|
103
|
+
Each capability method is **non-blocking**: it returns `Promise<void>`, and as the underlying generator yields items they are streamed into the environment one-by-one through the `deliverStream` helper:
|
|
116
104
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
105
|
+
```5:14:src/application/deliver-stream.ts
|
|
106
|
+
export async function deliverStream<T extends ContextItem>(
|
|
107
|
+
environment: AgenticEnvironment,
|
|
108
|
+
initiator: Participant,
|
|
109
|
+
stream: AsyncIterable<T>,
|
|
110
|
+
): Promise<void> {
|
|
120
111
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
112
|
+
for await (const item of stream) {
|
|
113
|
+
await environment.deliverContextItem(initiator, item);
|
|
114
|
+
}
|
|
124
115
|
}
|
|
125
|
-
|
|
126
|
-
const agent = new MyAgent()
|
|
127
|
-
await agent.run("Summarize the latest news", model, context)
|
|
128
116
|
```
|
|
129
117
|
|
|
130
|
-
|
|
118
|
+
Because each call is a fresh promise that wraps an async iterable, multiple participants can act on the same environment **concurrently**:
|
|
131
119
|
|
|
132
|
-
|
|
120
|
+
```ts
|
|
121
|
+
import {
|
|
122
|
+
AgenticEnvironment,
|
|
123
|
+
BaseAgentParticipant,
|
|
124
|
+
BaseHumanParticipant,
|
|
125
|
+
Gpt54Mini,
|
|
126
|
+
ModelContext,
|
|
127
|
+
} from "@mozaik-ai/core"
|
|
133
128
|
|
|
134
|
-
|
|
129
|
+
const environment = new AgenticEnvironment()
|
|
135
130
|
|
|
136
|
-
|
|
137
|
-
|
|
131
|
+
const human = new BaseHumanParticipant(humanInputSource)
|
|
132
|
+
const agent = new BaseAgentParticipant(agentInputSource, inferenceRunner, functionCallRunner)
|
|
138
133
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
console.log("Agent started, execution:", context.execution.executionId)
|
|
142
|
-
}
|
|
134
|
+
human.join(environment)
|
|
135
|
+
agent.join(environment)
|
|
143
136
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
137
|
+
environment.start()
|
|
138
|
+
|
|
139
|
+
const context = ModelContext.create("demo")
|
|
140
|
+
const model = new Gpt54Mini()
|
|
148
141
|
|
|
149
|
-
|
|
150
|
-
|
|
142
|
+
// Both participants produce items in parallel — neither awaits the other.
|
|
143
|
+
human.streamInput(environment)
|
|
144
|
+
agent.runInference(environment, context, model)
|
|
151
145
|
```
|
|
152
146
|
|
|
153
|
-
|
|
147
|
+
The environment fans every item out to every subscriber synchronously and without awaiting them, so a slow listener never blocks producers or other listeners.
|
|
154
148
|
|
|
155
149
|
---
|
|
156
150
|
|
|
157
|
-
|
|
151
|
+
## Intercepting items from other participants
|
|
158
152
|
|
|
159
|
-
`
|
|
153
|
+
`onContextItem(source, item)` is the single intercept point. A participant can:
|
|
160
154
|
|
|
161
|
-
|
|
155
|
+
- **Observe** items from other participants (telemetry, audit, UI streaming).
|
|
156
|
+
- **React** to items by triggering its own capabilities (turn a `UserMessageItem` into an inference run, turn a `FunctionCallItem` into a tool execution).
|
|
157
|
+
- **Ignore** items it doesn't care about — it is just a method call.
|
|
162
158
|
|
|
163
|
-
|
|
164
|
-
- `DeveloperMessage` — system instructions
|
|
165
|
-
- `FunctionCallOutput` — tool result fed back to the model
|
|
159
|
+
Items are discriminated by the `ContextItem` subclass and, for messages, the `role` field. The full taxonomy is in [src/domain/model-context/context-item](src/domain/model-context/context-item):
|
|
166
160
|
|
|
167
|
-
|
|
161
|
+
- Client-produced: `UserMessageItem`, `DeveloperMessageItem`, `SystemMessageItem`, `FunctionCallOutputItem`
|
|
162
|
+
- Model-produced: `ModelMessageItem`, `FunctionCallItem`, `ReasoningItem`
|
|
168
163
|
|
|
169
|
-
|
|
170
|
-
- `FunctionCall` — a tool invocation requested by the model
|
|
171
|
-
- `Reasoning` — the model's internal reasoning trace
|
|
164
|
+
### Passive observer
|
|
172
165
|
|
|
173
166
|
```ts
|
|
174
|
-
import {
|
|
167
|
+
import { Participant, ContextItem } from "@mozaik-ai/core"
|
|
175
168
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
169
|
+
export class TranscriptLogger extends Participant {
|
|
170
|
+
async onContextItem(source: Participant, item: ContextItem): Promise<void> {
|
|
171
|
+
console.log(`[${source.constructor.name}]`, item.toJSON())
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
179
175
|
|
|
180
|
-
|
|
181
|
-
|
|
176
|
+
### Reactive agent
|
|
177
|
+
|
|
178
|
+
A reactive agent extends `BaseAgentParticipant` and uses incoming items from *other* participants to decide when to run inference or execute a tool call:
|
|
182
179
|
|
|
183
|
-
|
|
184
|
-
|
|
180
|
+
```ts
|
|
181
|
+
import {
|
|
182
|
+
BaseAgentParticipant,
|
|
183
|
+
Participant,
|
|
184
|
+
ContextItem,
|
|
185
|
+
UserMessageItem,
|
|
186
|
+
FunctionCallItem,
|
|
187
|
+
AgenticEnvironment,
|
|
188
|
+
ModelContext,
|
|
189
|
+
GenerativeModel,
|
|
190
|
+
InputItemSource,
|
|
191
|
+
InferenceRunner,
|
|
192
|
+
FunctionCallRunner,
|
|
193
|
+
} from "@mozaik-ai/core"
|
|
194
|
+
|
|
195
|
+
export class ReactiveAgent extends BaseAgentParticipant {
|
|
196
|
+
constructor(
|
|
197
|
+
inputSource: InputItemSource,
|
|
198
|
+
inferenceRunner: InferenceRunner,
|
|
199
|
+
functionCallRunner: FunctionCallRunner,
|
|
200
|
+
private readonly environment: AgenticEnvironment,
|
|
201
|
+
private readonly context: ModelContext,
|
|
202
|
+
private readonly model: GenerativeModel,
|
|
203
|
+
) {
|
|
204
|
+
super(inputSource, inferenceRunner, functionCallRunner)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async onContextItem(source: Participant, item: ContextItem): Promise<void> {
|
|
208
|
+
if (source === this) return
|
|
209
|
+
|
|
210
|
+
this.context.addContextItem(item)
|
|
211
|
+
|
|
212
|
+
if (item instanceof UserMessageItem) {
|
|
213
|
+
this.runInference(this.environment, this.context, this.model)
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (item instanceof FunctionCallItem) {
|
|
218
|
+
this.executeFunctionCall(this.environment, item)
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
185
223
|
```
|
|
186
224
|
|
|
187
|
-
|
|
225
|
+
Two things to note:
|
|
226
|
+
|
|
227
|
+
1. The agent never `await`s its own capability calls inside `onContextItem` — the methods are non-blocking, so the environment keeps delivering items while inference and tool execution run in the background.
|
|
228
|
+
2. Behaviors compose by **reaction**, not orchestration. Add a second agent that listens for `ModelMessageItem`s and you get a critique loop. Add a `TranscriptLogger` and you get a UI stream. Neither change touches the existing participants.
|
|
188
229
|
|
|
189
230
|
---
|
|
190
231
|
|
|
191
|
-
|
|
232
|
+
## Context and models (reference)
|
|
233
|
+
|
|
234
|
+
`ModelContext` is the ordered list of `ContextItem`s a `GenerativeModel` is asked to reason over. It is constructed and mutated explicitly — typically inside a participant in response to delivered items.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import {
|
|
238
|
+
ModelContext,
|
|
239
|
+
DeveloperMessageItem,
|
|
240
|
+
UserMessageItem,
|
|
241
|
+
InMemoryModelContextRepository,
|
|
242
|
+
} from "@mozaik-ai/core"
|
|
192
243
|
|
|
193
|
-
|
|
244
|
+
const context = ModelContext.create("project-id")
|
|
245
|
+
.addContextItem(DeveloperMessageItem.create("You are a helpful assistant."))
|
|
246
|
+
.addContextItem(UserMessageItem.create("What is the capital of France?"))
|
|
194
247
|
|
|
195
|
-
|
|
248
|
+
const repo = new InMemoryModelContextRepository()
|
|
249
|
+
await repo.save(context)
|
|
250
|
+
```
|
|
196
251
|
|
|
197
|
-
|
|
198
|
-
- Deserializing model output into `ModelMessage`, `FunctionCall`, and `Reasoning` items
|
|
199
|
-
- Extracting token usage from the response
|
|
252
|
+
Implement `ModelContextRepository` to plug in any storage backend.
|
|
200
253
|
|
|
201
|
-
|
|
254
|
+
The default OpenAI provider is `OpenAIResponses`, implementing the [OpenResponses](https://www.openresponses.org/) spec. It maps `ModelContext` to the OpenAI Responses API and back into typed `ContextItem`s. Bundled models: `Gpt54`, `Gpt54Mini`, `Gpt54Nano`.
|
|
202
255
|
|
|
203
256
|
```ts
|
|
204
257
|
import { OpenAIResponses, InferenceRequest, Gpt54 } from "@mozaik-ai/core"
|
|
205
258
|
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
const response = await openaiResponses.infer(request)
|
|
209
|
-
|
|
210
|
-
context.applyModelOutput(response.contextItems)
|
|
259
|
+
const runtime = new OpenAIResponses()
|
|
260
|
+
const response = await runtime.infer(new InferenceRequest(new Gpt54(), context))
|
|
211
261
|
```
|
|
212
262
|
|
|
213
|
-
Available models: `Gpt54`, `Gpt54Mini`, `Gpt54Nano`.
|
|
214
|
-
|
|
215
263
|
---
|
|
216
264
|
|
|
217
|
-
|
|
265
|
+
## Advanced: overriding generators
|
|
218
266
|
|
|
219
|
-
`
|
|
267
|
+
`BaseAgentParticipant` and `BaseHumanParticipant` are deliberately thin shells around three generator interfaces. Swap any of them to change *how* items are produced without touching the environment, the participants, or any consumers.
|
|
220
268
|
|
|
221
|
-
|
|
269
|
+
### Custom `InputItemSource`
|
|
222
270
|
|
|
223
271
|
```ts
|
|
224
|
-
import {
|
|
225
|
-
|
|
226
|
-
|
|
272
|
+
import {
|
|
273
|
+
InputItemSource,
|
|
274
|
+
UserMessageItem,
|
|
275
|
+
DeveloperMessageItem,
|
|
276
|
+
SystemMessageItem,
|
|
277
|
+
} from "@mozaik-ai/core"
|
|
227
278
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
279
|
+
type InputItem = UserMessageItem | DeveloperMessageItem | SystemMessageItem
|
|
280
|
+
|
|
281
|
+
export class QueueInputSource implements InputItemSource {
|
|
282
|
+
private readonly queue: InputItem[] = []
|
|
283
|
+
private resolveNext?: () => void
|
|
284
|
+
|
|
285
|
+
push(item: InputItem) {
|
|
286
|
+
this.queue.push(item)
|
|
287
|
+
this.resolveNext?.()
|
|
288
|
+
this.resolveNext = undefined
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async *stream(signal?: AbortSignal): AsyncIterable<InputItem> {
|
|
292
|
+
while (!signal?.aborted) {
|
|
293
|
+
while (this.queue.length > 0) {
|
|
294
|
+
yield this.queue.shift()!
|
|
295
|
+
}
|
|
296
|
+
await new Promise<void>((resolve) => (this.resolveNext = resolve))
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
```
|
|
231
301
|
|
|
232
|
-
|
|
233
|
-
society.start()
|
|
302
|
+
Use it for stdin, websockets, an HTTP queue, or anything that produces user/developer/system messages over time.
|
|
234
303
|
|
|
235
|
-
|
|
236
|
-
society.enter("Analyze the impact of AI on software development", model, context)
|
|
304
|
+
### Custom `InferenceRunner`
|
|
237
305
|
|
|
238
|
-
|
|
239
|
-
society.stop()
|
|
240
|
-
```
|
|
306
|
+
Wrap any model runtime — including `OpenAIResponses` — and decide how its output becomes a stream of items. Here we expand a single `InferenceResponse` into per-item delivery:
|
|
241
307
|
|
|
242
|
-
|
|
308
|
+
```ts
|
|
309
|
+
import {
|
|
310
|
+
InferenceRunner,
|
|
311
|
+
InferenceRequest,
|
|
312
|
+
ModelContext,
|
|
313
|
+
GenerativeModel,
|
|
314
|
+
OpenAIResponses,
|
|
315
|
+
ReasoningItem,
|
|
316
|
+
FunctionCallItem,
|
|
317
|
+
ModelMessageItem,
|
|
318
|
+
} from "@mozaik-ai/core"
|
|
243
319
|
|
|
244
|
-
|
|
320
|
+
type InferenceItem = ReasoningItem | FunctionCallItem | ModelMessageItem
|
|
321
|
+
|
|
322
|
+
export class OpenAIInferenceRunner implements InferenceRunner {
|
|
323
|
+
private readonly runtime = new OpenAIResponses()
|
|
324
|
+
|
|
325
|
+
async *run(
|
|
326
|
+
context: ModelContext,
|
|
327
|
+
model: GenerativeModel,
|
|
328
|
+
signal?: AbortSignal,
|
|
329
|
+
): AsyncIterable<InferenceItem> {
|
|
330
|
+
const response = await this.runtime.infer(new InferenceRequest(model, context))
|
|
331
|
+
for (const item of response.contextItems) {
|
|
332
|
+
yield item as InferenceItem
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
245
337
|
|
|
246
|
-
|
|
338
|
+
Replace the body with a streaming runtime and items will flow into the environment as soon as the model produces them.
|
|
247
339
|
|
|
248
|
-
|
|
340
|
+
### Custom `FunctionCallRunner`
|
|
249
341
|
|
|
250
|
-
|
|
342
|
+
Resolve a `FunctionCallItem` against a tool registry and yield its output:
|
|
251
343
|
|
|
252
344
|
```ts
|
|
253
345
|
import {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
DeveloperMessage,
|
|
259
|
-
Gpt54Mini,
|
|
260
|
-
InMemoryModelContextRepository,
|
|
261
|
-
Tool,
|
|
346
|
+
FunctionCallRunner,
|
|
347
|
+
FunctionCallItem,
|
|
348
|
+
FunctionCallOutputItem,
|
|
349
|
+
Tool,
|
|
262
350
|
} from "@mozaik-ai/core"
|
|
263
351
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
name: "get_weather",
|
|
267
|
-
description: "Get the current weather for a city",
|
|
268
|
-
parameters: {
|
|
269
|
-
type: "object",
|
|
270
|
-
properties: { city: { type: "string" } },
|
|
271
|
-
required: ["city"],
|
|
272
|
-
},
|
|
273
|
-
invoke: async ({ city }) => ({ temperature: "22°C", condition: "sunny" }),
|
|
274
|
-
})
|
|
275
|
-
|
|
276
|
-
// Create runtime and wire hooks
|
|
277
|
-
const runtime = new AgentRuntime()
|
|
352
|
+
export class ToolRegistryFunctionCallRunner implements FunctionCallRunner {
|
|
353
|
+
constructor(private readonly tools: Tool[]) {}
|
|
278
354
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
355
|
+
async *run(
|
|
356
|
+
call: FunctionCallItem,
|
|
357
|
+
signal?: AbortSignal,
|
|
358
|
+
): AsyncIterable<FunctionCallOutputItem> {
|
|
359
|
+
const tool = this.tools.find((t) => t.name === call.name)
|
|
360
|
+
if (!tool) throw new Error(`Unknown tool: ${call.name}`)
|
|
282
361
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
362
|
+
const result = await tool.invoke(JSON.parse(call.args))
|
|
363
|
+
yield FunctionCallOutputItem.create(call.callId, JSON.stringify(result))
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
```
|
|
286
367
|
|
|
287
|
-
|
|
288
|
-
const agent = new Agent(runtime)
|
|
368
|
+
### Wiring it together
|
|
289
369
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const context = ModelContext.create("demo").addContextItem(DeveloperMessage.create("You are a weather assistant."))
|
|
370
|
+
```ts
|
|
371
|
+
import { BaseAgentParticipant, AgenticEnvironment } from "@mozaik-ai/core"
|
|
293
372
|
|
|
294
|
-
const
|
|
295
|
-
|
|
373
|
+
const agent = new BaseAgentParticipant(
|
|
374
|
+
new QueueInputSource(),
|
|
375
|
+
new OpenAIInferenceRunner(),
|
|
376
|
+
new ToolRegistryFunctionCallRunner(tools),
|
|
377
|
+
)
|
|
296
378
|
|
|
297
|
-
|
|
298
|
-
await agent.run("What's the weather like in Tokyo?", model, context)
|
|
379
|
+
agent.join(new AgenticEnvironment())
|
|
299
380
|
```
|
|
300
381
|
|
|
382
|
+
You now own input, inference, and tool execution end-to-end while keeping the same `Participant` contract — and any other participant in the environment can still observe and react to everything the agent emits.
|
|
383
|
+
|
|
301
384
|
---
|
|
302
385
|
|
|
303
386
|
## Author & License
|