@mozaik-ai/core 0.9.4 → 2.0.1
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 +51 -214
- package/dist/index.d.mts +121 -105
- package/dist/index.d.ts +121 -105
- package/dist/index.js +175 -1073
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +167 -1058
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/index.mjs
CHANGED
|
@@ -1,1134 +1,243 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
var
|
|
3
|
-
constructor() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.endpointResolver = endpointResolver;
|
|
11
|
-
}
|
|
12
|
-
async invoke(request) {
|
|
13
|
-
this.endpoint = this.endpointResolver.resolve(request.model);
|
|
14
|
-
return await this.endpoint.sendRequest(request);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// src/core/endpoint/endpoint-resolver.ts
|
|
19
|
-
var EndpointResolver = class {
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// src/types/model.ts
|
|
23
|
-
var OPENAI_MODELS = ["gpt-5", "gpt-5-mini", "gpt-5-nano", "gpt-5.1", "gpt-5.2", "gpt-5.4"];
|
|
24
|
-
var latest_sonnet = "claude-sonnet-4-5-20250929";
|
|
25
|
-
var latest_haiku = "claude-haiku-4-5-20251001";
|
|
26
|
-
var latest_opus = "claude-opus-4-5-20251101";
|
|
27
|
-
var ANTHROPIC_MODELS = ["claude-sonnet-4.5", "claude-haiku-4.5", "claude-opus-4.5"];
|
|
28
|
-
var ANTHROPIC_MODEL_MAP = {
|
|
29
|
-
"claude-sonnet-4.5": latest_sonnet,
|
|
30
|
-
"claude-haiku-4.5": latest_haiku,
|
|
31
|
-
"claude-opus-4.5": latest_opus
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// src/core/request-handler/capability.ts
|
|
35
|
-
var CapabilityHandler = class {
|
|
36
|
-
setNextHandler(capability) {
|
|
37
|
-
this.nextHandler = capability;
|
|
38
|
-
return this.nextHandler;
|
|
39
|
-
}
|
|
40
|
-
handle(request, builder) {
|
|
41
|
-
this.apply(request, builder);
|
|
42
|
-
if (this.nextHandler) {
|
|
43
|
-
this.nextHandler.handle(request, builder);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// src/core/request-handler/messages.ts
|
|
49
|
-
var MessagesHandler = class extends CapabilityHandler {
|
|
50
|
-
apply(request, builder) {
|
|
51
|
-
if (request.messages) {
|
|
52
|
-
builder.addMessages(request.messages);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// src/core/request-handler/task.ts
|
|
58
|
-
var TaskHandler = class extends CapabilityHandler {
|
|
59
|
-
apply(request, builder) {
|
|
60
|
-
if (request.task) {
|
|
61
|
-
builder.addTask(request.task);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// src/core/request-handler/model.ts
|
|
67
|
-
var ModelHandler = class extends CapabilityHandler {
|
|
68
|
-
apply(request, builder) {
|
|
69
|
-
if (request.model) {
|
|
70
|
-
builder.addModel(request.model);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// src/core/request-handler/structured-output.ts
|
|
76
|
-
var StructuredOutputlHandler = class extends CapabilityHandler {
|
|
77
|
-
apply(request, builder) {
|
|
78
|
-
if (request.structuredOutput) {
|
|
79
|
-
builder.addStructuredOutput(request.structuredOutput);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// src/core/request-handler/tools.ts
|
|
85
|
-
var ToolsHandler = class extends CapabilityHandler {
|
|
86
|
-
apply(mozaikRequest, requestBuilder) {
|
|
87
|
-
if (mozaikRequest.tools && mozaikRequest.tools.length > 0) {
|
|
88
|
-
requestBuilder.addTools(mozaikRequest.tools);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// src/core/endpoint/endpoint.ts
|
|
94
|
-
var Endpoint = class {
|
|
95
|
-
constructor() {
|
|
96
|
-
this.request = null;
|
|
97
|
-
}
|
|
98
|
-
buildRequest(request) {
|
|
99
|
-
this.request = request;
|
|
100
|
-
this.requestBuilder.initialize();
|
|
101
|
-
const messagesHandler = new MessagesHandler();
|
|
102
|
-
const taskHandler = new TaskHandler();
|
|
103
|
-
const modelHandler = new ModelHandler();
|
|
104
|
-
const structuredOutputHandler = new StructuredOutputlHandler();
|
|
105
|
-
const toolsHandler = new ToolsHandler();
|
|
106
|
-
messagesHandler.setNextHandler(taskHandler).setNextHandler(modelHandler).setNextHandler(structuredOutputHandler).setNextHandler(toolsHandler);
|
|
107
|
-
messagesHandler.handle(request, this.requestBuilder);
|
|
108
|
-
return this.requestBuilder.build();
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// src/core/endpoint/request-builder.ts
|
|
113
|
-
var RequestBuilder = class {
|
|
114
|
-
initialize() {
|
|
115
|
-
this.request = {};
|
|
116
|
-
}
|
|
117
|
-
build() {
|
|
118
|
-
return this.request;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// src/providers/openai/mapper.ts
|
|
123
|
-
var OpenAIResponsesMapper = class {
|
|
124
|
-
/**
|
|
125
|
-
* Convert domain Messages to Responses API instructions format
|
|
126
|
-
*
|
|
127
|
-
* The Responses API uses 'instructions' (system prompt) rather than messages array.
|
|
128
|
-
* For multi-turn conversations, use conversation.id or previous_response_id parameters.
|
|
129
|
-
*/
|
|
130
|
-
toInstructions(messages) {
|
|
131
|
-
if (messages.length === 0) {
|
|
132
|
-
throw new Error("[ResponsesMapper] Cannot create instructions from empty messages");
|
|
133
|
-
}
|
|
134
|
-
const systemMessage = messages.find((m) => m.role === "system");
|
|
135
|
-
if (systemMessage) {
|
|
136
|
-
return typeof systemMessage.content === "string" ? systemMessage.content : this.extractTextFromContent(systemMessage.content);
|
|
137
|
-
}
|
|
138
|
-
return "You are a helpful assistant.";
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Extract the user's input from messages
|
|
142
|
-
* Returns the last user message content for use in prompt variables.
|
|
143
|
-
* In Responses API, the actual user query is typically passed via prompt variables
|
|
144
|
-
* or handled through conversation continuation.
|
|
145
|
-
*/
|
|
146
|
-
extractUserInput(messages) {
|
|
147
|
-
const userMessages = messages.filter((m) => m.role === "user");
|
|
148
|
-
if (userMessages.length === 0) {
|
|
149
|
-
return "";
|
|
150
|
-
}
|
|
151
|
-
const lastUserMessage = userMessages[userMessages.length - 1];
|
|
152
|
-
return typeof lastUserMessage.content === "string" ? lastUserMessage.content : this.extractTextFromContent(lastUserMessage.content);
|
|
153
|
-
}
|
|
154
|
-
extractTextFromContent(content) {
|
|
155
|
-
return content.filter((part) => part.type === "text").map((part) => part.text).join(" ");
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Map tools to Responses API format
|
|
159
|
-
*/
|
|
160
|
-
toTools(tools) {
|
|
161
|
-
return tools.map((t) => ({
|
|
162
|
-
type: "function",
|
|
163
|
-
name: t.name,
|
|
164
|
-
description: t.description,
|
|
165
|
-
parameters: t.schema,
|
|
166
|
-
strict: true
|
|
167
|
-
// Enable strict mode by default for better validation
|
|
168
|
-
}));
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
// src/providers/openai/builder.ts
|
|
173
|
-
import { zodTextFormat } from "openai/helpers/zod";
|
|
174
|
-
|
|
175
|
-
// src/types/request.ts
|
|
176
|
-
var ReasoningEffort = /* @__PURE__ */ ((ReasoningEffort2) => {
|
|
177
|
-
ReasoningEffort2["NONE"] = "none";
|
|
178
|
-
ReasoningEffort2["LOW"] = "low";
|
|
179
|
-
ReasoningEffort2["MEDIUM"] = "medium";
|
|
180
|
-
ReasoningEffort2["HIGH"] = "high";
|
|
181
|
-
ReasoningEffort2["MAX"] = "max";
|
|
182
|
-
return ReasoningEffort2;
|
|
183
|
-
})(ReasoningEffort || {});
|
|
184
|
-
|
|
185
|
-
// src/providers/openai/builder.ts
|
|
186
|
-
var OpenAIResponsesBuilder = class extends RequestBuilder {
|
|
187
|
-
constructor(mapper = new OpenAIResponsesMapper()) {
|
|
188
|
-
super();
|
|
189
|
-
this.mapper = mapper;
|
|
190
|
-
}
|
|
191
|
-
addModel(model) {
|
|
192
|
-
this.request.model = model;
|
|
1
|
+
// src/domain/context-runtime/context.ts
|
|
2
|
+
var Context = class _Context {
|
|
3
|
+
constructor(id, projectId, items) {
|
|
4
|
+
this.projectId = projectId;
|
|
5
|
+
this.id = id;
|
|
6
|
+
this.items = items;
|
|
7
|
+
}
|
|
8
|
+
addItem(item) {
|
|
9
|
+
this.items.push(item);
|
|
193
10
|
return this;
|
|
194
11
|
}
|
|
195
|
-
|
|
196
|
-
this.
|
|
12
|
+
addItems(items) {
|
|
13
|
+
this.items.push(...items);
|
|
197
14
|
return this;
|
|
198
15
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (instructions && instructions !== "You are a helpful assistant.") {
|
|
202
|
-
this.request.instructions = instructions;
|
|
203
|
-
}
|
|
204
|
-
return this;
|
|
16
|
+
getItems() {
|
|
17
|
+
return this.items;
|
|
205
18
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
};
|
|
210
|
-
return this;
|
|
19
|
+
static create(projectId) {
|
|
20
|
+
const id = crypto.randomUUID();
|
|
21
|
+
return new _Context(id, projectId, []);
|
|
211
22
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
type: "function",
|
|
215
|
-
name: tool.name,
|
|
216
|
-
description: tool.description,
|
|
217
|
-
parameters: tool.schema
|
|
218
|
-
}));
|
|
219
|
-
return this;
|
|
23
|
+
static rehydrate(data) {
|
|
24
|
+
return new _Context(data.id, data.projectId, data.items);
|
|
220
25
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (effort === "none" /* NONE */) {
|
|
224
|
-
reasoningEffort = "none";
|
|
225
|
-
} else if (effort === "low" /* LOW */) {
|
|
226
|
-
reasoningEffort = "low";
|
|
227
|
-
} else if (effort === "medium" /* MEDIUM */) {
|
|
228
|
-
reasoningEffort = "medium";
|
|
229
|
-
} else if (effort === "high" /* HIGH */) {
|
|
230
|
-
reasoningEffort = "high";
|
|
231
|
-
} else if (effort === "max" /* MAX */) {
|
|
232
|
-
reasoningEffort = "xhigh";
|
|
233
|
-
}
|
|
234
|
-
this.request.reasoning = {
|
|
235
|
-
effort: reasoningEffort
|
|
236
|
-
};
|
|
237
|
-
return this;
|
|
26
|
+
toJSON() {
|
|
27
|
+
return this.items.map((item) => item.toJSON());
|
|
238
28
|
}
|
|
239
29
|
};
|
|
240
30
|
|
|
241
|
-
// src/
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
this.client = client;
|
|
246
|
-
}
|
|
247
|
-
async send(request) {
|
|
248
|
-
return await this.client.responses.create(request);
|
|
31
|
+
// src/domain/context-runtime/context-item.ts
|
|
32
|
+
var ContextItem = class {
|
|
33
|
+
getType() {
|
|
34
|
+
return this.type;
|
|
249
35
|
}
|
|
250
36
|
};
|
|
251
37
|
|
|
252
|
-
// src/
|
|
253
|
-
|
|
254
|
-
var OpenAIParserClient = class {
|
|
255
|
-
constructor(client = new OpenAI2()) {
|
|
256
|
-
this.client = client;
|
|
257
|
-
}
|
|
258
|
-
async send(request) {
|
|
259
|
-
return await this.client.responses.parse(request);
|
|
260
|
-
}
|
|
38
|
+
// src/domain/context-runtime/content.ts
|
|
39
|
+
var Content = class {
|
|
261
40
|
};
|
|
262
41
|
|
|
263
|
-
// src/
|
|
264
|
-
var
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return new OpenAIDefaultClient();
|
|
270
|
-
}
|
|
42
|
+
// src/domain/context-runtime/content/input-text.ts
|
|
43
|
+
var InputText = class _InputText extends Content {
|
|
44
|
+
constructor(text) {
|
|
45
|
+
super();
|
|
46
|
+
this.text = text;
|
|
47
|
+
this.type = "input_text";
|
|
271
48
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
// src/core/endpoint/response-handler.ts
|
|
275
|
-
var ResponseHandler = class {
|
|
276
|
-
setNextHandler(responseHandler) {
|
|
277
|
-
this.nextHandler = responseHandler;
|
|
278
|
-
return this.nextHandler;
|
|
49
|
+
static create(text) {
|
|
50
|
+
return new _InputText(text);
|
|
279
51
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
// src/providers/openai/response-handler/output-parsed.ts
|
|
283
|
-
var OutputParsedHandler = class extends ResponseHandler {
|
|
284
|
-
async handle(mozaikResponse) {
|
|
285
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
286
|
-
if (providerResponse.output_parsed) {
|
|
287
|
-
mozaikResponse.setResponseData(providerResponse.output_parsed);
|
|
288
|
-
return mozaikResponse;
|
|
289
|
-
}
|
|
290
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
52
|
+
static rehydrate(data) {
|
|
53
|
+
return new _InputText(data.text);
|
|
291
54
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
var _a, _b;
|
|
298
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
299
|
-
const firstOutput = (_a = providerResponse.output) == null ? void 0 : _a[0];
|
|
300
|
-
if (firstOutput && "content" in firstOutput) {
|
|
301
|
-
const firstContent = (_b = firstOutput.content) == null ? void 0 : _b[0];
|
|
302
|
-
if (firstContent && "text" in firstContent) {
|
|
303
|
-
mozaikResponse.setResponseData(firstContent.text);
|
|
304
|
-
return mozaikResponse;
|
|
55
|
+
toJSON() {
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
type: this.type,
|
|
59
|
+
text: this.text
|
|
305
60
|
}
|
|
306
|
-
|
|
307
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
// src/providers/openai/response-handler/output-text.ts
|
|
312
|
-
var OutputTextHandler = class extends ResponseHandler {
|
|
313
|
-
async handle(mozaikResponse) {
|
|
314
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
315
|
-
if (providerResponse.output_text) {
|
|
316
|
-
mozaikResponse.setResponseData(providerResponse.output_text);
|
|
317
|
-
return mozaikResponse;
|
|
318
|
-
}
|
|
319
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
// src/core/usage-entry.ts
|
|
324
|
-
var UsageEntry = class {
|
|
325
|
-
constructor(priceInUsd, model) {
|
|
326
|
-
this.priceInUsd = priceInUsd;
|
|
327
|
-
this.model = model;
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
// src/providers/openai/model-pricing.ts
|
|
332
|
-
var OpenAIModelPricing = class {
|
|
333
|
-
calculateTotalCost(inputTokens, outputTokens, cachedInputTokens) {
|
|
334
|
-
const inputCost = (inputTokens - cachedInputTokens) * this.inputPricePer1MToken / 1e6;
|
|
335
|
-
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
336
|
-
const cachedCost = cachedInputTokens * this.cachedPricePer1MToken / 1e6;
|
|
337
|
-
return inputCost + outputCost + cachedCost;
|
|
338
|
-
}
|
|
339
|
-
getPriceInUsd(modelName, inputTokens, outputTokens, cachedInputTokens) {
|
|
340
|
-
if (modelName.startsWith("gpt-5-mini")) {
|
|
341
|
-
this.inputPricePer1MToken = 0.25;
|
|
342
|
-
this.outputPricePer1MToken = 2;
|
|
343
|
-
this.cachedPricePer1MToken = 0.025;
|
|
344
|
-
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
345
|
-
} else if (modelName.startsWith("gpt-5-nano")) {
|
|
346
|
-
this.inputPricePer1MToken = 0.05;
|
|
347
|
-
this.outputPricePer1MToken = 0.4;
|
|
348
|
-
this.cachedPricePer1MToken = 5e-3;
|
|
349
|
-
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
350
|
-
} else if (modelName.startsWith("gpt-5.1")) {
|
|
351
|
-
this.inputPricePer1MToken = 1.25;
|
|
352
|
-
this.outputPricePer1MToken = 10;
|
|
353
|
-
this.cachedPricePer1MToken = 0.125;
|
|
354
|
-
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
355
|
-
} else if (modelName.startsWith("gpt-5")) {
|
|
356
|
-
this.inputPricePer1MToken = 1.25;
|
|
357
|
-
this.outputPricePer1MToken = 10;
|
|
358
|
-
this.cachedPricePer1MToken = 0.125;
|
|
359
|
-
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
360
|
-
}
|
|
361
|
-
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
61
|
+
];
|
|
362
62
|
}
|
|
363
63
|
};
|
|
364
64
|
|
|
365
|
-
// src/
|
|
366
|
-
var
|
|
367
|
-
constructor(
|
|
65
|
+
// src/domain/context-runtime/input/user-message.ts
|
|
66
|
+
var UserMessage = class _UserMessage extends ContextItem {
|
|
67
|
+
constructor(content) {
|
|
368
68
|
super();
|
|
369
|
-
this.
|
|
370
|
-
this.
|
|
371
|
-
this.
|
|
372
|
-
}
|
|
373
|
-
hasToolCalls(response) {
|
|
374
|
-
var _a;
|
|
375
|
-
return (_a = response.output) == null ? void 0 : _a.some((item) => item.type === "function_call");
|
|
376
|
-
}
|
|
377
|
-
extractToolCalls(response) {
|
|
378
|
-
var _a;
|
|
379
|
-
return ((_a = response.output) == null ? void 0 : _a.filter((item) => item.type === "function_call")) || [];
|
|
69
|
+
this.type = "message";
|
|
70
|
+
this.role = "user";
|
|
71
|
+
this.content = content;
|
|
380
72
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return
|
|
73
|
+
static create(text) {
|
|
74
|
+
const content = InputText.create(text);
|
|
75
|
+
return new _UserMessage(content);
|
|
384
76
|
}
|
|
385
|
-
|
|
386
|
-
const
|
|
387
|
-
return
|
|
77
|
+
static rehydrate(data) {
|
|
78
|
+
const content = InputText.rehydrate(data);
|
|
79
|
+
return new _UserMessage(content);
|
|
388
80
|
}
|
|
389
|
-
|
|
81
|
+
toJSON() {
|
|
390
82
|
return {
|
|
391
|
-
type:
|
|
392
|
-
|
|
393
|
-
|
|
83
|
+
type: this.type,
|
|
84
|
+
role: this.role,
|
|
85
|
+
content: this.content.toJSON()
|
|
394
86
|
};
|
|
395
87
|
}
|
|
396
|
-
async executeToolCalls(toolCalls, tools) {
|
|
397
|
-
var _a;
|
|
398
|
-
const results = [];
|
|
399
|
-
for (const call of toolCalls) {
|
|
400
|
-
const tool = this.resolveTool(call, tools);
|
|
401
|
-
if (!tool) {
|
|
402
|
-
results.push(
|
|
403
|
-
this.formatToolResult(call.call_id, {
|
|
404
|
-
error: `Unknown tool: ${call.name}`
|
|
405
|
-
})
|
|
406
|
-
);
|
|
407
|
-
continue;
|
|
408
|
-
}
|
|
409
|
-
try {
|
|
410
|
-
const result = await this.executeTool(tool, call.arguments);
|
|
411
|
-
results.push(this.formatToolResult(call.call_id, result));
|
|
412
|
-
} catch (err) {
|
|
413
|
-
results.push(
|
|
414
|
-
this.formatToolResult(call.call_id, {
|
|
415
|
-
error: (_a = err.message) != null ? _a : "Tool execution failed"
|
|
416
|
-
})
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
return results;
|
|
421
|
-
}
|
|
422
|
-
async handle(mozaikResponse) {
|
|
423
|
-
var _a, _b;
|
|
424
|
-
let providerResponse = mozaikResponse.providerResponse;
|
|
425
|
-
let toolCallingResponse;
|
|
426
|
-
while (this.hasToolCalls(providerResponse)) {
|
|
427
|
-
const toolCalls = this.extractToolCalls(providerResponse);
|
|
428
|
-
const toolResults = await this.executeToolCalls(toolCalls, this.tools);
|
|
429
|
-
this.request.input = toolResults;
|
|
430
|
-
this.request.previous_response_id = providerResponse.id;
|
|
431
|
-
toolCallingResponse = await this.client.send(this.request);
|
|
432
|
-
const openAIModelPricing = new OpenAIModelPricing();
|
|
433
|
-
const usage = toolCallingResponse.usage;
|
|
434
|
-
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
435
|
-
providerResponse.model,
|
|
436
|
-
usage.input_tokens,
|
|
437
|
-
usage.output_tokens,
|
|
438
|
-
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
439
|
-
);
|
|
440
|
-
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
441
|
-
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
442
|
-
mozaikResponse.setResponseData(toolCallingResponse);
|
|
443
|
-
providerResponse = toolCallingResponse;
|
|
444
|
-
}
|
|
445
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
446
|
-
}
|
|
447
88
|
};
|
|
448
89
|
|
|
449
|
-
// src/
|
|
450
|
-
var
|
|
451
|
-
constructor() {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
this.
|
|
456
|
-
return this;
|
|
457
|
-
}
|
|
458
|
-
addUsageEntry(usageEntry) {
|
|
459
|
-
this.usageEntries.push(usageEntry);
|
|
460
|
-
return this;
|
|
461
|
-
}
|
|
462
|
-
setResponseData(response) {
|
|
463
|
-
this.responseData = response;
|
|
464
|
-
return this;
|
|
90
|
+
// src/domain/context-runtime/input/developer-message.ts
|
|
91
|
+
var DeveloperMessage = class _DeveloperMessage extends ContextItem {
|
|
92
|
+
constructor(content) {
|
|
93
|
+
super();
|
|
94
|
+
this.type = "message";
|
|
95
|
+
this.role = "developer";
|
|
96
|
+
this.content = content;
|
|
465
97
|
}
|
|
466
|
-
|
|
98
|
+
toJSON() {
|
|
467
99
|
return {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
},
|
|
472
|
-
totalCost: this.usageEntries.reduce((acc, entry) => acc + entry.priceInUsd, 0)
|
|
100
|
+
type: this.type,
|
|
101
|
+
role: this.role,
|
|
102
|
+
content: this.content.toJSON()
|
|
473
103
|
};
|
|
474
104
|
}
|
|
475
|
-
|
|
476
|
-
|
|
105
|
+
static create(text) {
|
|
106
|
+
const content = InputText.create(text);
|
|
107
|
+
return new _DeveloperMessage(content);
|
|
477
108
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
var UsageHandler = class extends ResponseHandler {
|
|
482
|
-
async handle(mozaikResponse) {
|
|
483
|
-
var _a, _b;
|
|
484
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
485
|
-
const usage = providerResponse.usage;
|
|
486
|
-
const openAIModelPricing = new OpenAIModelPricing();
|
|
487
|
-
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
488
|
-
providerResponse.model,
|
|
489
|
-
usage.input_tokens,
|
|
490
|
-
usage.output_tokens,
|
|
491
|
-
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
492
|
-
);
|
|
493
|
-
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
494
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
109
|
+
static rehydrate(data) {
|
|
110
|
+
const content = InputText.rehydrate(data);
|
|
111
|
+
return new _DeveloperMessage(content);
|
|
495
112
|
}
|
|
496
113
|
};
|
|
497
114
|
|
|
498
|
-
// src/
|
|
499
|
-
var
|
|
500
|
-
constructor() {
|
|
115
|
+
// src/domain/context-runtime/content/output-text.ts
|
|
116
|
+
var OutputText = class _OutputText extends Content {
|
|
117
|
+
constructor(text) {
|
|
501
118
|
super();
|
|
502
|
-
this.
|
|
503
|
-
|
|
504
|
-
async sendRequest(mozaikRequest) {
|
|
505
|
-
try {
|
|
506
|
-
const request = this.buildRequest(mozaikRequest);
|
|
507
|
-
const client = OpenAIClientResolver.resolve(request);
|
|
508
|
-
const response = await client.send(request);
|
|
509
|
-
const mozaikResponse = new MozaikResponse();
|
|
510
|
-
mozaikResponse.setProviderResponse(response);
|
|
511
|
-
const usageHandler = new UsageHandler();
|
|
512
|
-
const functionCallsHandler = new FunctionCallsHandler(
|
|
513
|
-
request,
|
|
514
|
-
mozaikRequest.tools ? mozaikRequest.tools : []
|
|
515
|
-
);
|
|
516
|
-
const outputParsedHandler = new OutputParsedHandler();
|
|
517
|
-
const outputTextHandler = new OutputTextHandler();
|
|
518
|
-
const contentHandler = new ContentHandler();
|
|
519
|
-
usageHandler.setNextHandler(functionCallsHandler).setNextHandler(outputParsedHandler).setNextHandler(outputTextHandler).setNextHandler(contentHandler);
|
|
520
|
-
const responseHandler = usageHandler;
|
|
521
|
-
const result = await responseHandler.handle(mozaikResponse);
|
|
522
|
-
return result.getResponse();
|
|
523
|
-
} catch (error) {
|
|
524
|
-
console.warn("[OpenAIProvider] Responses API request failed:", error);
|
|
525
|
-
throw error;
|
|
526
|
-
}
|
|
119
|
+
this.text = text;
|
|
120
|
+
this.type = "output_text";
|
|
527
121
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
// src/providers/anthropic/mapper.ts
|
|
531
|
-
var AnthropicMapper = class {
|
|
532
|
-
/**
|
|
533
|
-
* Transforms domain messages to Anthropic API format.
|
|
534
|
-
* Separates system messages from conversation messages.
|
|
535
|
-
*/
|
|
536
|
-
toMessages(messages) {
|
|
537
|
-
const anthropicMessages = [];
|
|
538
|
-
let systemPrompt;
|
|
539
|
-
for (const msg of messages) {
|
|
540
|
-
if (msg.role === "system") {
|
|
541
|
-
const content = typeof msg.content === "string" ? msg.content : msg.content.map((p) => p.text).join("\n");
|
|
542
|
-
systemPrompt = systemPrompt ? `${systemPrompt}
|
|
543
|
-
|
|
544
|
-
${content}` : content;
|
|
545
|
-
continue;
|
|
546
|
-
}
|
|
547
|
-
if (msg.role === "tool") {
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
if (msg.role === "user" || msg.role === "assistant") {
|
|
551
|
-
if (typeof msg.content === "string") {
|
|
552
|
-
anthropicMessages.push({
|
|
553
|
-
role: msg.role,
|
|
554
|
-
content: msg.content
|
|
555
|
-
});
|
|
556
|
-
} else {
|
|
557
|
-
const contentBlocks = msg.content.map((part) => {
|
|
558
|
-
if (part.type === "text") {
|
|
559
|
-
return {
|
|
560
|
-
type: "text",
|
|
561
|
-
text: part.text
|
|
562
|
-
};
|
|
563
|
-
} else if (part.type === "image_url") {
|
|
564
|
-
const imageUrl = part.url;
|
|
565
|
-
if (imageUrl.startsWith("data:image")) {
|
|
566
|
-
const matches = imageUrl.match(/data:image\/(\w+);base64,(.+)/);
|
|
567
|
-
if (matches) {
|
|
568
|
-
const [, mediaType, data] = matches;
|
|
569
|
-
return {
|
|
570
|
-
type: "image",
|
|
571
|
-
source: {
|
|
572
|
-
type: "base64",
|
|
573
|
-
media_type: `image/${mediaType}`,
|
|
574
|
-
data
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
throw new Error(
|
|
580
|
-
"[AnthropicMapper] Image URLs must be base64-encoded. Convert to data:image/[type];base64,[data] format."
|
|
581
|
-
);
|
|
582
|
-
}
|
|
583
|
-
return { type: "text", text: "" };
|
|
584
|
-
});
|
|
585
|
-
anthropicMessages.push({
|
|
586
|
-
role: msg.role,
|
|
587
|
-
content: contentBlocks
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
const validated = this.ensureAlternatingRoles(anthropicMessages);
|
|
593
|
-
return {
|
|
594
|
-
messages: validated,
|
|
595
|
-
system: systemPrompt
|
|
596
|
-
};
|
|
122
|
+
static rehydrate(data) {
|
|
123
|
+
return new _OutputText(data.text);
|
|
597
124
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
if (messages.length === 0) return messages;
|
|
604
|
-
const result = [];
|
|
605
|
-
let lastRole;
|
|
606
|
-
for (const msg of messages) {
|
|
607
|
-
if (lastRole === msg.role && result.length > 0) {
|
|
608
|
-
const lastMsg = result[result.length - 1];
|
|
609
|
-
if (typeof lastMsg.content === "string" && typeof msg.content === "string") {
|
|
610
|
-
lastMsg.content = `${lastMsg.content}
|
|
611
|
-
${msg.content}`;
|
|
612
|
-
} else {
|
|
613
|
-
const lastContent = Array.isArray(lastMsg.content) ? lastMsg.content : [{ type: "text", text: lastMsg.content }];
|
|
614
|
-
const newContent = Array.isArray(msg.content) ? msg.content : [{ type: "text", text: msg.content }];
|
|
615
|
-
lastMsg.content = [...lastContent, ...newContent];
|
|
616
|
-
}
|
|
617
|
-
} else {
|
|
618
|
-
result.push(msg);
|
|
619
|
-
lastRole = msg.role;
|
|
125
|
+
toJSON() {
|
|
126
|
+
return [
|
|
127
|
+
{
|
|
128
|
+
type: this.type,
|
|
129
|
+
text: this.text
|
|
620
130
|
}
|
|
621
|
-
|
|
622
|
-
if (result.length > 0 && result[0].role !== "user") {
|
|
623
|
-
throw new Error(
|
|
624
|
-
"[AnthropicMapper] First message must be from user. Anthropic API requires conversations to start with a user message."
|
|
625
|
-
);
|
|
626
|
-
}
|
|
627
|
-
return result;
|
|
131
|
+
];
|
|
628
132
|
}
|
|
629
133
|
};
|
|
630
134
|
|
|
631
|
-
// src/
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
this.
|
|
637
|
-
|
|
638
|
-
addModel(model) {
|
|
639
|
-
this.request.model = ANTHROPIC_MODEL_MAP[model];
|
|
640
|
-
return this;
|
|
641
|
-
}
|
|
642
|
-
addTask(task) {
|
|
643
|
-
const message = {
|
|
644
|
-
role: "user",
|
|
645
|
-
content: task
|
|
646
|
-
};
|
|
647
|
-
if (!this.request.messages) {
|
|
648
|
-
this.request.messages = [];
|
|
649
|
-
}
|
|
650
|
-
this.request.messages.push(message);
|
|
651
|
-
return this;
|
|
652
|
-
}
|
|
653
|
-
addMessages(messages) {
|
|
654
|
-
const { messages: anthropicMessages, system } = this.mapper.toMessages(messages);
|
|
655
|
-
this.request.messages = anthropicMessages;
|
|
656
|
-
if (system) {
|
|
657
|
-
this.request.system = system;
|
|
658
|
-
}
|
|
659
|
-
return this;
|
|
660
|
-
}
|
|
661
|
-
addStructuredOutput(schema) {
|
|
662
|
-
this.request.betas = ["structured-outputs-2025-11-13"];
|
|
663
|
-
this.request.output_format = betaZodOutputFormat(schema);
|
|
664
|
-
return this;
|
|
135
|
+
// src/domain/context-runtime/output/model-message.ts
|
|
136
|
+
var ModelMessage = class _ModelMessage extends ContextItem {
|
|
137
|
+
constructor(content) {
|
|
138
|
+
super();
|
|
139
|
+
this.type = "message";
|
|
140
|
+
this.role = "assistant";
|
|
141
|
+
this.content = content;
|
|
665
142
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
description: tool.description,
|
|
670
|
-
input_schema: tool.schema
|
|
671
|
-
}));
|
|
672
|
-
return this;
|
|
143
|
+
static rehydrate(data) {
|
|
144
|
+
const content = OutputText.rehydrate(data);
|
|
145
|
+
return new _ModelMessage(content);
|
|
673
146
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
reasoningEffort = "low";
|
|
680
|
-
} else if (effort === "medium" /* MEDIUM */) {
|
|
681
|
-
reasoningEffort = "medium";
|
|
682
|
-
} else if (effort === "high" /* HIGH */) {
|
|
683
|
-
reasoningEffort = "high";
|
|
684
|
-
} else if (effort === "max" /* MAX */) {
|
|
685
|
-
reasoningEffort = "max";
|
|
686
|
-
}
|
|
687
|
-
this.request.output_config = {
|
|
688
|
-
effort: reasoningEffort
|
|
147
|
+
toJSON() {
|
|
148
|
+
return {
|
|
149
|
+
type: this.type,
|
|
150
|
+
role: this.role,
|
|
151
|
+
content: this.content.toJSON()
|
|
689
152
|
};
|
|
690
|
-
return this;
|
|
691
|
-
}
|
|
692
|
-
build() {
|
|
693
|
-
if (!this.request.max_tokens) {
|
|
694
|
-
this.request.max_tokens = 1024;
|
|
695
|
-
}
|
|
696
|
-
return this.request;
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
|
|
700
|
-
// src/providers/anthropic/client/default.ts
|
|
701
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
702
|
-
var AnthropicDefaultClient = class {
|
|
703
|
-
constructor(client = new Anthropic()) {
|
|
704
|
-
this.client = client;
|
|
705
|
-
}
|
|
706
|
-
async send(request) {
|
|
707
|
-
return await this.client.messages.create(request);
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
|
|
711
|
-
// src/providers/anthropic/client/parser.ts
|
|
712
|
-
import Anthropic2 from "@anthropic-ai/sdk";
|
|
713
|
-
var AnthropicParserClient = class {
|
|
714
|
-
constructor(client = new Anthropic2()) {
|
|
715
|
-
this.client = client;
|
|
716
|
-
}
|
|
717
|
-
async send(request) {
|
|
718
|
-
return await this.client.beta.messages.parse(request);
|
|
719
|
-
}
|
|
720
|
-
};
|
|
721
|
-
|
|
722
|
-
// src/providers/anthropic/client/resolver.ts
|
|
723
|
-
var AnthropicClientResolver = class {
|
|
724
|
-
static resolve(request) {
|
|
725
|
-
return request.output_format ? new AnthropicParserClient() : new AnthropicDefaultClient();
|
|
726
153
|
}
|
|
727
154
|
};
|
|
728
155
|
|
|
729
|
-
// src/
|
|
730
|
-
var
|
|
731
|
-
|
|
732
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
733
|
-
if (providerResponse.parsed_output) {
|
|
734
|
-
mozaikResponse.setResponseData(providerResponse.parsed_output);
|
|
735
|
-
return mozaikResponse;
|
|
736
|
-
}
|
|
737
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
|
|
741
|
-
// src/providers/anthropic/response-handler/content.ts
|
|
742
|
-
var ContentHandler2 = class extends ResponseHandler {
|
|
743
|
-
async handle(mozaikResponse) {
|
|
744
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
745
|
-
const content = providerResponse.content.filter((block) => block.type === "text").map((block) => block.type === "text" ? block.text : "").join("");
|
|
746
|
-
if (content) {
|
|
747
|
-
mozaikResponse.setResponseData(content);
|
|
748
|
-
return mozaikResponse;
|
|
749
|
-
}
|
|
750
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
751
|
-
}
|
|
752
|
-
};
|
|
753
|
-
|
|
754
|
-
// src/providers/anthropic/model-pricing.ts
|
|
755
|
-
var AnthropicModelPricing = class {
|
|
756
|
-
calculateTotalCost(inputTokens, outputTokens) {
|
|
757
|
-
const inputCost = inputTokens * this.inputPricePer1MToken / 1e6;
|
|
758
|
-
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
759
|
-
return inputCost + outputCost;
|
|
760
|
-
}
|
|
761
|
-
getPriceInUsd(modelName, inputTokens, outputTokens) {
|
|
762
|
-
if (modelName.startsWith("claude-sonnet-4-5")) {
|
|
763
|
-
this.inputPricePer1MToken = 3;
|
|
764
|
-
this.outputPricePer1MToken = 15;
|
|
765
|
-
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
766
|
-
} else if (modelName.startsWith("claude-haiku-4-5")) {
|
|
767
|
-
this.inputPricePer1MToken = 1;
|
|
768
|
-
this.outputPricePer1MToken = 5;
|
|
769
|
-
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
770
|
-
} else if (modelName.startsWith("claude-opus-4-5")) {
|
|
771
|
-
this.inputPricePer1MToken = 5;
|
|
772
|
-
this.outputPricePer1MToken = 25;
|
|
773
|
-
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
774
|
-
}
|
|
775
|
-
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
|
|
779
|
-
// src/providers/anthropic/response-handler/tool-use.ts
|
|
780
|
-
var ToolUseHandler = class extends ResponseHandler {
|
|
781
|
-
constructor(request, tools) {
|
|
156
|
+
// src/domain/context-runtime/output/function-call.ts
|
|
157
|
+
var FunctionCall = class _FunctionCall extends ContextItem {
|
|
158
|
+
constructor(callId, name, args) {
|
|
782
159
|
super();
|
|
783
|
-
this.
|
|
784
|
-
this.
|
|
785
|
-
this.
|
|
786
|
-
|
|
787
|
-
async executeToolCalls(tools, toolUseBlocks) {
|
|
788
|
-
const results = [];
|
|
789
|
-
for (const toolUse of toolUseBlocks) {
|
|
790
|
-
const tool = tools == null ? void 0 : tools.find((t) => t.name === toolUse.name);
|
|
791
|
-
if (!tool) {
|
|
792
|
-
results.push({
|
|
793
|
-
type: "tool_result",
|
|
794
|
-
tool_use_id: toolUse.id,
|
|
795
|
-
content: JSON.stringify({ error: `Unknown tool: ${toolUse.name}` })
|
|
796
|
-
});
|
|
797
|
-
continue;
|
|
798
|
-
}
|
|
799
|
-
try {
|
|
800
|
-
const result = await tool.invoke(toolUse.input);
|
|
801
|
-
results.push({
|
|
802
|
-
type: "tool_result",
|
|
803
|
-
tool_use_id: toolUse.id,
|
|
804
|
-
content: JSON.stringify(result)
|
|
805
|
-
});
|
|
806
|
-
} catch (error) {
|
|
807
|
-
results.push({
|
|
808
|
-
type: "tool_result",
|
|
809
|
-
tool_use_id: toolUse.id,
|
|
810
|
-
content: JSON.stringify({ error: error.message })
|
|
811
|
-
});
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
return results;
|
|
815
|
-
}
|
|
816
|
-
async handle(mozaikResponse) {
|
|
817
|
-
let providerResponse = mozaikResponse.providerResponse;
|
|
818
|
-
while (providerResponse.stop_reason === "tool_use") {
|
|
819
|
-
const toolUseBlocks = providerResponse.content.filter(
|
|
820
|
-
(block) => block.type === "tool_use"
|
|
821
|
-
);
|
|
822
|
-
this.request.messages.push({
|
|
823
|
-
role: "assistant",
|
|
824
|
-
content: providerResponse.content
|
|
825
|
-
});
|
|
826
|
-
const toolResults = await this.executeToolCalls(this.tools, toolUseBlocks);
|
|
827
|
-
this.request.messages.push({
|
|
828
|
-
role: "user",
|
|
829
|
-
content: toolResults
|
|
830
|
-
});
|
|
831
|
-
const toolCallingResponse = await this.client.send(this.request);
|
|
832
|
-
const usage = toolCallingResponse.usage;
|
|
833
|
-
const anthropicModelPricing = new AnthropicModelPricing();
|
|
834
|
-
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
835
|
-
providerResponse.model,
|
|
836
|
-
usage.input_tokens,
|
|
837
|
-
usage.output_tokens
|
|
838
|
-
);
|
|
839
|
-
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, toolCallingResponse.model.name));
|
|
840
|
-
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
841
|
-
providerResponse = toolCallingResponse;
|
|
842
|
-
}
|
|
843
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
844
|
-
}
|
|
845
|
-
};
|
|
846
|
-
|
|
847
|
-
// src/providers/anthropic/response-handler/usage.ts
|
|
848
|
-
var UsageHandler2 = class extends ResponseHandler {
|
|
849
|
-
async handle(mozaikResponse) {
|
|
850
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
851
|
-
const usage = providerResponse.usage;
|
|
852
|
-
const anthropicModelPricing = new AnthropicModelPricing();
|
|
853
|
-
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
854
|
-
providerResponse.model,
|
|
855
|
-
usage.input_tokens,
|
|
856
|
-
usage.output_tokens
|
|
857
|
-
);
|
|
858
|
-
if (usage) {
|
|
859
|
-
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
860
|
-
}
|
|
861
|
-
return await this.nextHandler.handle(mozaikResponse);
|
|
862
|
-
}
|
|
863
|
-
};
|
|
864
|
-
|
|
865
|
-
// src/providers/anthropic/response-handler/undhandled.ts
|
|
866
|
-
var UnhandledResponseHandler = class extends ResponseHandler {
|
|
867
|
-
async handle(mozaikResponse) {
|
|
868
|
-
var _a, _b;
|
|
869
|
-
const providerResponse = mozaikResponse.providerResponse;
|
|
870
|
-
const id = (_a = providerResponse == null ? void 0 : providerResponse.id) != null ? _a : "unknown";
|
|
871
|
-
const model = (_b = providerResponse == null ? void 0 : providerResponse.model) != null ? _b : "unknown";
|
|
872
|
-
throw new Error(`No response handler matched. response_id=${id} model=${model}`);
|
|
873
|
-
}
|
|
874
|
-
};
|
|
875
|
-
|
|
876
|
-
// src/providers/anthropic/endpoint.ts
|
|
877
|
-
var AnthropicEndpoint = class extends Endpoint {
|
|
878
|
-
constructor() {
|
|
879
|
-
super(...arguments);
|
|
880
|
-
this.requestBuilder = new AnthropicRequestBuilder();
|
|
160
|
+
this.type = "function_call";
|
|
161
|
+
this.callId = callId;
|
|
162
|
+
this.name = name;
|
|
163
|
+
this.args = args;
|
|
881
164
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
const client = AnthropicClientResolver.resolve(providerRequest);
|
|
885
|
-
const response = await client.send(providerRequest);
|
|
886
|
-
const mozaikResponse = new MozaikResponse();
|
|
887
|
-
mozaikResponse.setProviderResponse(response);
|
|
888
|
-
const usageHandler = new UsageHandler2();
|
|
889
|
-
const toolUseHandler = new ToolUseHandler(
|
|
890
|
-
providerRequest,
|
|
891
|
-
mozaikRequest.tools ? mozaikRequest.tools : []
|
|
892
|
-
);
|
|
893
|
-
const parsedOutputHandler = new ParsedOutputHandler();
|
|
894
|
-
const contentHandler = new ContentHandler2();
|
|
895
|
-
const unhandledResponseHandler = new UnhandledResponseHandler();
|
|
896
|
-
usageHandler.setNextHandler(toolUseHandler).setNextHandler(parsedOutputHandler).setNextHandler(contentHandler).setNextHandler(unhandledResponseHandler);
|
|
897
|
-
const responseHandler = usageHandler;
|
|
898
|
-
const result = await responseHandler.handle(mozaikResponse);
|
|
899
|
-
return result.getResponse();
|
|
165
|
+
static rehydrate(data) {
|
|
166
|
+
return new _FunctionCall(data.callId, data.name, data.args);
|
|
900
167
|
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
isAnthropicModel(value) {
|
|
909
|
-
return ANTHROPIC_MODELS.includes(value);
|
|
910
|
-
}
|
|
911
|
-
resolve(model) {
|
|
912
|
-
if (this.isOpenAIModel(model)) {
|
|
913
|
-
return new OpenAIResponses();
|
|
914
|
-
} else if (this.isAnthropicModel(model)) {
|
|
915
|
-
return new AnthropicEndpoint();
|
|
916
|
-
}
|
|
917
|
-
throw new Error("Provider not found");
|
|
918
|
-
}
|
|
919
|
-
};
|
|
920
|
-
|
|
921
|
-
// src/core/agents/agent.ts
|
|
922
|
-
var MozaikAgent = class {
|
|
923
|
-
constructor(request) {
|
|
924
|
-
this.gateway = new RequestGateway(new DefaultEndpointResolver());
|
|
925
|
-
this.request = request;
|
|
926
|
-
}
|
|
927
|
-
setModel(model) {
|
|
928
|
-
this.request.model = model;
|
|
929
|
-
}
|
|
930
|
-
setMessages(messages) {
|
|
931
|
-
this.request.messages = messages;
|
|
932
|
-
}
|
|
933
|
-
setTask(task) {
|
|
934
|
-
this.request.task = task;
|
|
935
|
-
}
|
|
936
|
-
setStructuredOutput(schema) {
|
|
937
|
-
this.request.structuredOutput = schema;
|
|
938
|
-
}
|
|
939
|
-
async act(task) {
|
|
940
|
-
if (task) {
|
|
941
|
-
this.request.task = task;
|
|
942
|
-
}
|
|
943
|
-
return await this.gateway.invoke(this.request);
|
|
944
|
-
}
|
|
945
|
-
};
|
|
946
|
-
|
|
947
|
-
// src/core/workflow/schema/plan.ts
|
|
948
|
-
import z from "zod";
|
|
949
|
-
var ModelSchema = z.enum([...OPENAI_MODELS, ...ANTHROPIC_MODELS]);
|
|
950
|
-
var TaskPlanNodeSchema = z.object({
|
|
951
|
-
kind: z.literal("task"),
|
|
952
|
-
task: z.string(),
|
|
953
|
-
model: ModelSchema
|
|
954
|
-
});
|
|
955
|
-
var WorkflowPlanNodeSchema = z.lazy(
|
|
956
|
-
() => z.object({
|
|
957
|
-
kind: z.literal("workflow"),
|
|
958
|
-
mode: z.enum(["sequential", "parallel"]),
|
|
959
|
-
units: z.array(PlanNodeSchema)
|
|
960
|
-
})
|
|
961
|
-
);
|
|
962
|
-
var PlanNodeSchema = TaskPlanNodeSchema.or(WorkflowPlanNodeSchema);
|
|
963
|
-
var PlanSchema = z.object({
|
|
964
|
-
root: PlanNodeSchema
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
// src/core/workflow/execution/parallel.ts
|
|
968
|
-
var ParallelExecution = class {
|
|
969
|
-
async execute(workflow, hook) {
|
|
970
|
-
const promises = [];
|
|
971
|
-
for (const u of workflow.units) {
|
|
972
|
-
promises.push(u.execute(hook));
|
|
973
|
-
}
|
|
974
|
-
const results = await Promise.all(promises);
|
|
975
|
-
return results;
|
|
976
|
-
}
|
|
977
|
-
};
|
|
978
|
-
|
|
979
|
-
// src/core/workflow/execution/sequential.ts
|
|
980
|
-
var SequentalExecution = class {
|
|
981
|
-
async execute(workflow, hook) {
|
|
982
|
-
const results = [];
|
|
983
|
-
for (const u of workflow.units) {
|
|
984
|
-
results.push(await u.execute(hook));
|
|
985
|
-
}
|
|
986
|
-
return results;
|
|
987
|
-
}
|
|
988
|
-
};
|
|
989
|
-
|
|
990
|
-
// src/core/workflow/execution/strategy-factory.ts
|
|
991
|
-
var ExecutionStrategyFactory = class {
|
|
992
|
-
static create(mode) {
|
|
993
|
-
let strategy;
|
|
994
|
-
if (mode == "parallel") {
|
|
995
|
-
strategy = new ParallelExecution();
|
|
996
|
-
} else {
|
|
997
|
-
strategy = new SequentalExecution();
|
|
998
|
-
}
|
|
999
|
-
return strategy;
|
|
1000
|
-
}
|
|
1001
|
-
};
|
|
1002
|
-
|
|
1003
|
-
// src/core/workflow/hooks/cluster.ts
|
|
1004
|
-
var ClusterHook = class {
|
|
1005
|
-
constructor(hooks) {
|
|
1006
|
-
this.hooks = hooks;
|
|
1007
|
-
}
|
|
1008
|
-
beforeTask(task) {
|
|
1009
|
-
this.hooks.forEach((h) => h.beforeTask(task));
|
|
1010
|
-
}
|
|
1011
|
-
afterTask(task, result) {
|
|
1012
|
-
this.hooks.forEach((h) => h.afterTask(task, result));
|
|
1013
|
-
}
|
|
1014
|
-
beforeWorkflow(wf) {
|
|
1015
|
-
this.hooks.forEach((h) => h.beforeWorkflow(wf));
|
|
1016
|
-
}
|
|
1017
|
-
afterWorkflow(wf, result) {
|
|
1018
|
-
this.hooks.forEach((h) => h.afterWorkflow(wf, result));
|
|
168
|
+
toJSON() {
|
|
169
|
+
return {
|
|
170
|
+
type: this.type,
|
|
171
|
+
callId: this.callId,
|
|
172
|
+
name: this.name,
|
|
173
|
+
arguments: this.args
|
|
174
|
+
};
|
|
1019
175
|
}
|
|
1020
176
|
};
|
|
1021
177
|
|
|
1022
|
-
// src/
|
|
1023
|
-
var
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
178
|
+
// src/domain/context-runtime/output/reasoning.ts
|
|
179
|
+
var Reasoning = class _Reasoning extends ContextItem {
|
|
180
|
+
constructor(content, encryptedContent, summary = []) {
|
|
181
|
+
super();
|
|
182
|
+
this.type = "reasoning";
|
|
183
|
+
this.content = content;
|
|
184
|
+
this.encryptedContent = encryptedContent;
|
|
185
|
+
this.summary = summary;
|
|
1029
186
|
}
|
|
1030
|
-
|
|
1031
|
-
|
|
187
|
+
static rehydrate(data) {
|
|
188
|
+
return new _Reasoning(data.content, data.encryptedContent, data.summary);
|
|
1032
189
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
190
|
+
toJSON() {
|
|
191
|
+
var _a;
|
|
192
|
+
return {
|
|
193
|
+
type: this.type,
|
|
194
|
+
content: (_a = this.content) == null ? void 0 : _a.toJSON(),
|
|
195
|
+
encryptedContent: this.encryptedContent,
|
|
196
|
+
summary: this.summary.map((s) => s.toJSON())
|
|
197
|
+
};
|
|
1035
198
|
}
|
|
1036
199
|
};
|
|
1037
200
|
|
|
1038
|
-
// src/
|
|
1039
|
-
var
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
// src/core/workflow/workflow.ts
|
|
1045
|
-
var Workflow = class extends WorkUnit {
|
|
1046
|
-
constructor(mode, units) {
|
|
1047
|
-
super();
|
|
1048
|
-
this.mode = mode;
|
|
1049
|
-
this.units = units;
|
|
1050
|
-
}
|
|
1051
|
-
async execute(hook = DEFAULT_CLUSTER_HOOK) {
|
|
1052
|
-
hook.beforeWorkflow(this);
|
|
1053
|
-
const executionStrategy = ExecutionStrategyFactory.create(this.mode);
|
|
1054
|
-
const result = await executionStrategy.execute(this, hook);
|
|
1055
|
-
hook.afterWorkflow(this, result);
|
|
1056
|
-
return result;
|
|
201
|
+
// src/domain/generative-model/generative-model.ts
|
|
202
|
+
var GenerativeModel = class {
|
|
203
|
+
async call(context) {
|
|
204
|
+
const request = this.mapContextToRequest(context);
|
|
205
|
+
const response = await this.infer(request);
|
|
206
|
+
return this.extractContextItems(response);
|
|
1057
207
|
}
|
|
1058
208
|
};
|
|
1059
209
|
|
|
1060
|
-
// src/
|
|
1061
|
-
var
|
|
1062
|
-
constructor(
|
|
210
|
+
// src/domain/context-runtime/input/function-call-output.ts
|
|
211
|
+
var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
|
|
212
|
+
constructor(callId, output) {
|
|
1063
213
|
super();
|
|
1064
|
-
this.
|
|
1065
|
-
this.
|
|
214
|
+
this.type = "function_call_output";
|
|
215
|
+
this.callId = callId;
|
|
216
|
+
this.output = output;
|
|
1066
217
|
}
|
|
1067
|
-
|
|
1068
|
-
return
|
|
218
|
+
static create(callId, output) {
|
|
219
|
+
return new _FunctionCallOutput(callId, output);
|
|
1069
220
|
}
|
|
1070
|
-
|
|
1071
|
-
return
|
|
221
|
+
static rehydrate(data) {
|
|
222
|
+
return new _FunctionCallOutput(data.callId, data.output);
|
|
1072
223
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
224
|
+
toJSON() {
|
|
225
|
+
return {
|
|
226
|
+
type: this.type,
|
|
227
|
+
callId: this.callId,
|
|
228
|
+
output: this.output.map((o) => o.toJSON())
|
|
1078
229
|
};
|
|
1079
|
-
const agent = new MozaikAgent(request);
|
|
1080
|
-
const result = await agent.act(this.task);
|
|
1081
|
-
hook.afterTask(this, result);
|
|
1082
|
-
return result;
|
|
1083
|
-
}
|
|
1084
|
-
};
|
|
1085
|
-
|
|
1086
|
-
// src/core/workflow/mapper.ts
|
|
1087
|
-
var PlanWorkflowMapper = class {
|
|
1088
|
-
static fromPlan(plan) {
|
|
1089
|
-
return this.fromNode(plan.root);
|
|
1090
|
-
}
|
|
1091
|
-
static fromNode(node) {
|
|
1092
|
-
if (node.kind !== "workflow") {
|
|
1093
|
-
throw new Error("Root must be workflow");
|
|
1094
|
-
}
|
|
1095
|
-
return new Workflow(node.mode, node.units.map(this.mapNode));
|
|
1096
|
-
}
|
|
1097
|
-
static mapNode(node) {
|
|
1098
|
-
if (node.kind === "task") {
|
|
1099
|
-
return new Task(node.task, node.model);
|
|
1100
|
-
}
|
|
1101
|
-
return new Workflow(node.mode, node.units.map(this.mapNode));
|
|
1102
|
-
}
|
|
1103
|
-
};
|
|
1104
|
-
|
|
1105
|
-
// src/core/agents/planner.ts
|
|
1106
|
-
var PROMPT = `You are a planner.
|
|
1107
|
-
|
|
1108
|
-
Rules:
|
|
1109
|
-
- Use 'parallel' for the task that can be run in parallel.
|
|
1110
|
-
- Pick model based on the task complexity
|
|
1111
|
-
- Keep prompts actionable.
|
|
1112
|
-
- Don't ask user for any input, just do the thing with available data you have.
|
|
1113
|
-
`;
|
|
1114
|
-
var PlanningAgent = class extends MozaikAgent {
|
|
1115
|
-
constructor(request) {
|
|
1116
|
-
super(request);
|
|
1117
|
-
}
|
|
1118
|
-
async planFromGoal(goal) {
|
|
1119
|
-
this.setStructuredOutput(PlanSchema);
|
|
1120
|
-
const response = await this.act(`${PROMPT}
|
|
1121
|
-
Goal: ${goal}`);
|
|
1122
|
-
const plan = response.data;
|
|
1123
|
-
return PlanWorkflowMapper.fromPlan(plan);
|
|
1124
230
|
}
|
|
1125
231
|
};
|
|
1126
232
|
export {
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
233
|
+
Context,
|
|
234
|
+
ContextItem,
|
|
235
|
+
DeveloperMessage,
|
|
236
|
+
FunctionCall,
|
|
237
|
+
FunctionCallOutput,
|
|
238
|
+
GenerativeModel,
|
|
239
|
+
ModelMessage,
|
|
240
|
+
Reasoning,
|
|
241
|
+
UserMessage
|
|
1133
242
|
};
|
|
1134
243
|
//# sourceMappingURL=index.mjs.map
|