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