@mozaik-ai/core 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +20 -10
- package/dist/index.d.ts +20 -10
- package/dist/index.js +200 -138
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +199 -137
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -9,9 +9,9 @@ var RequestGateway = class {
|
|
|
9
9
|
constructor(endpointResolver) {
|
|
10
10
|
this.endpointResolver = endpointResolver;
|
|
11
11
|
}
|
|
12
|
-
async invoke(
|
|
13
|
-
this.endpoint = this.endpointResolver.resolve(
|
|
14
|
-
return await this.endpoint.sendRequest(
|
|
12
|
+
async invoke(request) {
|
|
13
|
+
this.endpoint = this.endpointResolver.resolve(request.model);
|
|
14
|
+
return await this.endpoint.sendRequest(request);
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
|
|
@@ -37,55 +37,55 @@ var CapabilityHandler = class {
|
|
|
37
37
|
this.nextHandler = capability;
|
|
38
38
|
return this.nextHandler;
|
|
39
39
|
}
|
|
40
|
-
handle(
|
|
41
|
-
this.apply(
|
|
40
|
+
handle(request, builder) {
|
|
41
|
+
this.apply(request, builder);
|
|
42
42
|
if (this.nextHandler) {
|
|
43
|
-
this.nextHandler.handle(
|
|
43
|
+
this.nextHandler.handle(request, builder);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
// src/core/command-handler/messages.ts
|
|
49
49
|
var MessagesHandler = class extends CapabilityHandler {
|
|
50
|
-
apply(
|
|
51
|
-
if (
|
|
52
|
-
builder.addMessages(
|
|
50
|
+
apply(request, builder) {
|
|
51
|
+
if (request.messages) {
|
|
52
|
+
builder.addMessages(request.messages);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
// src/core/command-handler/task.ts
|
|
58
58
|
var TaskHandler = class extends CapabilityHandler {
|
|
59
|
-
apply(
|
|
60
|
-
if (
|
|
61
|
-
builder.addTask(
|
|
59
|
+
apply(request, builder) {
|
|
60
|
+
if (request.task) {
|
|
61
|
+
builder.addTask(request.task);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
|
|
66
66
|
// src/core/command-handler/model.ts
|
|
67
67
|
var ModelHandler = class extends CapabilityHandler {
|
|
68
|
-
apply(
|
|
69
|
-
if (
|
|
70
|
-
builder.addModel(
|
|
68
|
+
apply(request, builder) {
|
|
69
|
+
if (request.model) {
|
|
70
|
+
builder.addModel(request.model);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
// src/core/command-handler/structured-output.ts
|
|
76
76
|
var StructuredOutputlHandler = class extends CapabilityHandler {
|
|
77
|
-
apply(
|
|
78
|
-
if (
|
|
79
|
-
builder.addStructuredOutput(
|
|
77
|
+
apply(request, builder) {
|
|
78
|
+
if (request.structuredOutput) {
|
|
79
|
+
builder.addStructuredOutput(request.structuredOutput);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
83
|
|
|
84
84
|
// src/core/command-handler/tools.ts
|
|
85
85
|
var ToolsHandler = class extends CapabilityHandler {
|
|
86
|
-
apply(
|
|
87
|
-
if (
|
|
88
|
-
requestBuilder.addTools(
|
|
86
|
+
apply(mozaikRequest, requestBuilder) {
|
|
87
|
+
if (mozaikRequest.tools && mozaikRequest.tools.length > 0) {
|
|
88
|
+
requestBuilder.addTools(mozaikRequest.tools);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
};
|
|
@@ -93,10 +93,10 @@ var ToolsHandler = class extends CapabilityHandler {
|
|
|
93
93
|
// src/core/endpoint/endpoint.ts
|
|
94
94
|
var Endpoint = class {
|
|
95
95
|
constructor() {
|
|
96
|
-
this.
|
|
96
|
+
this.request = null;
|
|
97
97
|
}
|
|
98
|
-
buildRequest(
|
|
99
|
-
this.
|
|
98
|
+
buildRequest(request) {
|
|
99
|
+
this.request = request;
|
|
100
100
|
this.requestBuilder.initialize();
|
|
101
101
|
const messagesHandler = new MessagesHandler();
|
|
102
102
|
const taskHandler = new TaskHandler();
|
|
@@ -104,7 +104,7 @@ var Endpoint = class {
|
|
|
104
104
|
const structuredOutputHandler = new StructuredOutputlHandler();
|
|
105
105
|
const toolsHandler = new ToolsHandler();
|
|
106
106
|
messagesHandler.setNextHandler(taskHandler).setNextHandler(modelHandler).setNextHandler(structuredOutputHandler).setNextHandler(toolsHandler);
|
|
107
|
-
messagesHandler.handle(
|
|
107
|
+
messagesHandler.handle(request, this.requestBuilder);
|
|
108
108
|
return this.requestBuilder.build();
|
|
109
109
|
}
|
|
110
110
|
};
|
|
@@ -254,7 +254,7 @@ var OutputParsedHandler = class extends ResponseHandler {
|
|
|
254
254
|
async handle(responseContext) {
|
|
255
255
|
const providerResponse = responseContext.providerResponse;
|
|
256
256
|
if (providerResponse.output_parsed) {
|
|
257
|
-
responseContext.
|
|
257
|
+
responseContext.setResponseData(providerResponse.output_parsed);
|
|
258
258
|
return responseContext;
|
|
259
259
|
}
|
|
260
260
|
return await this.nextHandler.handle(responseContext);
|
|
@@ -263,40 +263,72 @@ var OutputParsedHandler = class extends ResponseHandler {
|
|
|
263
263
|
|
|
264
264
|
// src/providers/openai/response-handler/content.ts
|
|
265
265
|
var ContentHandler = class extends ResponseHandler {
|
|
266
|
-
async handle(
|
|
266
|
+
async handle(mozaikResponse) {
|
|
267
267
|
var _a, _b;
|
|
268
|
-
const providerResponse =
|
|
268
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
269
269
|
const firstOutput = (_a = providerResponse.output) == null ? void 0 : _a[0];
|
|
270
270
|
if (firstOutput && "content" in firstOutput) {
|
|
271
271
|
const firstContent = (_b = firstOutput.content) == null ? void 0 : _b[0];
|
|
272
272
|
if (firstContent && "text" in firstContent) {
|
|
273
|
-
|
|
274
|
-
return
|
|
273
|
+
mozaikResponse.setResponseData(firstContent.text);
|
|
274
|
+
return mozaikResponse;
|
|
275
275
|
}
|
|
276
276
|
}
|
|
277
|
-
return await this.nextHandler.handle(
|
|
277
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
278
278
|
}
|
|
279
279
|
};
|
|
280
280
|
|
|
281
281
|
// src/providers/openai/response-handler/output-text.ts
|
|
282
282
|
var OutputTextHandler = class extends ResponseHandler {
|
|
283
|
-
async handle(
|
|
284
|
-
const providerResponse =
|
|
283
|
+
async handle(mozaikResponse) {
|
|
284
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
285
285
|
if (providerResponse.output_text) {
|
|
286
|
-
|
|
287
|
-
return
|
|
286
|
+
mozaikResponse.setResponseData(providerResponse.output_text);
|
|
287
|
+
return mozaikResponse;
|
|
288
288
|
}
|
|
289
|
-
return await this.nextHandler.handle(
|
|
289
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
290
290
|
}
|
|
291
291
|
};
|
|
292
292
|
|
|
293
|
-
// src/core/
|
|
293
|
+
// src/core/usage-entry.ts
|
|
294
294
|
var UsageEntry = class {
|
|
295
|
-
constructor(
|
|
296
|
-
this.
|
|
297
|
-
this.outputTokens = outputTokens;
|
|
295
|
+
constructor(priceInUsd, model) {
|
|
296
|
+
this.priceInUsd = priceInUsd;
|
|
298
297
|
this.model = model;
|
|
299
|
-
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// src/providers/openai/model-pricing.ts
|
|
302
|
+
var OpenAIModelPricing = class {
|
|
303
|
+
calculateTotalCost(inputTokens, outputTokens, cachedInputTokens) {
|
|
304
|
+
const inputCost = (inputTokens - cachedInputTokens) * this.inputPricePer1MToken / 1e6;
|
|
305
|
+
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
306
|
+
const cachedCost = cachedInputTokens * this.cachedPricePer1MToken / 1e6;
|
|
307
|
+
return inputCost + outputCost + cachedCost;
|
|
308
|
+
}
|
|
309
|
+
getPriceInUsd(modelName, inputTokens, outputTokens, cachedInputTokens) {
|
|
310
|
+
if (modelName === "gpt-5") {
|
|
311
|
+
this.inputPricePer1MToken = 1.25;
|
|
312
|
+
this.outputPricePer1MToken = 10;
|
|
313
|
+
this.cachedPricePer1MToken = 0.125;
|
|
314
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
315
|
+
} else if (modelName === "gpt-5-mini") {
|
|
316
|
+
this.inputPricePer1MToken = 0.25;
|
|
317
|
+
this.outputPricePer1MToken = 2;
|
|
318
|
+
this.cachedPricePer1MToken = 0.025;
|
|
319
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
320
|
+
} else if (modelName === "gpt-5-nano") {
|
|
321
|
+
this.inputPricePer1MToken = 0.05;
|
|
322
|
+
this.outputPricePer1MToken = 0.4;
|
|
323
|
+
this.cachedPricePer1MToken = 5e-3;
|
|
324
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
325
|
+
} else if (modelName === "gpt-5.1") {
|
|
326
|
+
this.inputPricePer1MToken = 1.25;
|
|
327
|
+
this.outputPricePer1MToken = 10;
|
|
328
|
+
this.cachedPricePer1MToken = 0.125;
|
|
329
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
330
|
+
}
|
|
331
|
+
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
300
332
|
}
|
|
301
333
|
};
|
|
302
334
|
|
|
@@ -357,8 +389,9 @@ var FunctionCallsHandler = class extends ResponseHandler {
|
|
|
357
389
|
}
|
|
358
390
|
return results;
|
|
359
391
|
}
|
|
360
|
-
async handle(
|
|
361
|
-
|
|
392
|
+
async handle(mozaikResponse) {
|
|
393
|
+
var _a, _b;
|
|
394
|
+
let providerResponse = mozaikResponse.providerResponse;
|
|
362
395
|
let toolCallingResponse;
|
|
363
396
|
while (this.hasToolCalls(providerResponse)) {
|
|
364
397
|
const toolCalls = this.extractToolCalls(providerResponse);
|
|
@@ -366,23 +399,25 @@ var FunctionCallsHandler = class extends ResponseHandler {
|
|
|
366
399
|
this.request.input = toolResults;
|
|
367
400
|
this.request.previous_response_id = providerResponse.id;
|
|
368
401
|
toolCallingResponse = await this.client.send(this.request);
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
402
|
+
const openAIModelPricing = new OpenAIModelPricing();
|
|
403
|
+
const usage = toolCallingResponse.usage;
|
|
404
|
+
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
405
|
+
providerResponse.model,
|
|
406
|
+
usage.input_tokens,
|
|
407
|
+
usage.output_tokens,
|
|
408
|
+
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
375
409
|
);
|
|
376
|
-
|
|
377
|
-
|
|
410
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
411
|
+
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
412
|
+
mozaikResponse.setResponseData(toolCallingResponse);
|
|
378
413
|
providerResponse = toolCallingResponse;
|
|
379
414
|
}
|
|
380
|
-
return await this.nextHandler.handle(
|
|
415
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
381
416
|
}
|
|
382
417
|
};
|
|
383
418
|
|
|
384
|
-
// src/core/
|
|
385
|
-
var
|
|
419
|
+
// src/core/response.ts
|
|
420
|
+
var MozaikResponse = class {
|
|
386
421
|
constructor() {
|
|
387
422
|
this.usageEntries = [];
|
|
388
423
|
}
|
|
@@ -394,12 +429,18 @@ var ResponseContext = class {
|
|
|
394
429
|
this.usageEntries.push(usageEntry);
|
|
395
430
|
return this;
|
|
396
431
|
}
|
|
397
|
-
|
|
398
|
-
this.
|
|
432
|
+
setResponseData(response) {
|
|
433
|
+
this.responseData = response;
|
|
399
434
|
return this;
|
|
400
435
|
}
|
|
401
436
|
getResponse() {
|
|
402
|
-
return
|
|
437
|
+
return {
|
|
438
|
+
data: this.responseData,
|
|
439
|
+
usage: {
|
|
440
|
+
entries: this.usageEntries
|
|
441
|
+
},
|
|
442
|
+
totalCost: this.usageEntries.reduce((acc, entry) => acc + entry.priceInUsd, 0)
|
|
443
|
+
};
|
|
403
444
|
}
|
|
404
445
|
getUsageEntries() {
|
|
405
446
|
return this.usageEntries;
|
|
@@ -408,15 +449,19 @@ var ResponseContext = class {
|
|
|
408
449
|
|
|
409
450
|
// src/providers/openai/response-handler/usage.ts
|
|
410
451
|
var UsageHandler = class extends ResponseHandler {
|
|
411
|
-
async handle(
|
|
412
|
-
|
|
452
|
+
async handle(mozaikResponse) {
|
|
453
|
+
var _a, _b;
|
|
454
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
413
455
|
const usage = providerResponse.usage;
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
456
|
+
const openAIModelPricing = new OpenAIModelPricing();
|
|
457
|
+
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
458
|
+
providerResponse.model,
|
|
459
|
+
usage.input_tokens,
|
|
460
|
+
usage.output_tokens,
|
|
461
|
+
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
462
|
+
);
|
|
463
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
464
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
420
465
|
}
|
|
421
466
|
};
|
|
422
467
|
|
|
@@ -431,8 +476,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
431
476
|
const request = this.buildRequest(command);
|
|
432
477
|
const client = OpenAIClientResolver.resolve(request);
|
|
433
478
|
const response = await client.send(request);
|
|
434
|
-
const
|
|
435
|
-
|
|
479
|
+
const mozaikResponse = new MozaikResponse();
|
|
480
|
+
mozaikResponse.setProviderResponse(response);
|
|
436
481
|
const usageHandler = new UsageHandler();
|
|
437
482
|
const functionCallsHandler = new FunctionCallsHandler(
|
|
438
483
|
request,
|
|
@@ -443,16 +488,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
443
488
|
const contentHandler = new ContentHandler();
|
|
444
489
|
usageHandler.setNextHandler(functionCallsHandler).setNextHandler(outputParsedHandler).setNextHandler(outputTextHandler).setNextHandler(contentHandler);
|
|
445
490
|
const responseHandler = usageHandler;
|
|
446
|
-
const result = await responseHandler.handle(
|
|
447
|
-
|
|
448
|
-
inputTokens: e.inputTokens,
|
|
449
|
-
outputTokens: e.outputTokens,
|
|
450
|
-
model: e.model
|
|
451
|
-
}));
|
|
452
|
-
return {
|
|
453
|
-
data: result.getResponse(),
|
|
454
|
-
usageEntries
|
|
455
|
-
};
|
|
491
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
492
|
+
return result.getResponse();
|
|
456
493
|
} catch (error) {
|
|
457
494
|
console.warn("[OpenAIProvider] Responses API request failed:", error);
|
|
458
495
|
throw error;
|
|
@@ -643,26 +680,51 @@ var AnthropicClientResolver = class {
|
|
|
643
680
|
|
|
644
681
|
// src/providers/anthropic/response-handler/parsed-output.ts
|
|
645
682
|
var ParsedOutputHandler = class extends ResponseHandler {
|
|
646
|
-
async handle(
|
|
647
|
-
const providerResponse =
|
|
683
|
+
async handle(mozaikResponse) {
|
|
684
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
648
685
|
if (providerResponse.parsed_output) {
|
|
649
|
-
|
|
650
|
-
return
|
|
686
|
+
mozaikResponse.setResponseData(providerResponse.parsed_output);
|
|
687
|
+
return mozaikResponse;
|
|
651
688
|
}
|
|
652
|
-
return await this.nextHandler.handle(
|
|
689
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
653
690
|
}
|
|
654
691
|
};
|
|
655
692
|
|
|
656
693
|
// src/providers/anthropic/response-handler/content.ts
|
|
657
694
|
var ContentHandler2 = class extends ResponseHandler {
|
|
658
|
-
async handle(
|
|
659
|
-
const providerResponse =
|
|
695
|
+
async handle(mozaikResponse) {
|
|
696
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
660
697
|
const content = providerResponse.content.filter((block) => block.type === "text").map((block) => block.type === "text" ? block.text : "").join("");
|
|
661
698
|
if (content) {
|
|
662
|
-
|
|
663
|
-
return
|
|
699
|
+
mozaikResponse.setResponseData(content);
|
|
700
|
+
return mozaikResponse;
|
|
664
701
|
}
|
|
665
|
-
return await this.nextHandler.handle(
|
|
702
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
// src/providers/anthropic/model-pricing.ts
|
|
707
|
+
var AnthropicModelPricing = class {
|
|
708
|
+
calculateTotalCost(inputTokens, outputTokens) {
|
|
709
|
+
const inputCost = inputTokens * this.inputPricePer1MToken / 1e6;
|
|
710
|
+
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
711
|
+
return inputCost + outputCost;
|
|
712
|
+
}
|
|
713
|
+
getPriceInUsd(modelName, inputTokens, outputTokens) {
|
|
714
|
+
if (modelName === "claude-sonnet-4.5") {
|
|
715
|
+
this.inputPricePer1MToken = 3;
|
|
716
|
+
this.outputPricePer1MToken = 15;
|
|
717
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
718
|
+
} else if (modelName === "claude-haiku-4.5") {
|
|
719
|
+
this.inputPricePer1MToken = 1;
|
|
720
|
+
this.outputPricePer1MToken = 5;
|
|
721
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
722
|
+
} else if (modelName === "claude-opus-4.5") {
|
|
723
|
+
this.inputPricePer1MToken = 5;
|
|
724
|
+
this.outputPricePer1MToken = 25;
|
|
725
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
726
|
+
}
|
|
727
|
+
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
666
728
|
}
|
|
667
729
|
};
|
|
668
730
|
|
|
@@ -703,8 +765,8 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
703
765
|
}
|
|
704
766
|
return results;
|
|
705
767
|
}
|
|
706
|
-
async handle(
|
|
707
|
-
let providerResponse =
|
|
768
|
+
async handle(mozaikResponse) {
|
|
769
|
+
let providerResponse = mozaikResponse.providerResponse;
|
|
708
770
|
while (providerResponse.stop_reason === "tool_use") {
|
|
709
771
|
const toolUseBlocks = providerResponse.content.filter(
|
|
710
772
|
(block) => block.type === "tool_use"
|
|
@@ -719,39 +781,44 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
719
781
|
content: toolResults
|
|
720
782
|
});
|
|
721
783
|
const toolCallingResponse = await this.client.send(this.request);
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
784
|
+
const usage = toolCallingResponse.usage;
|
|
785
|
+
const anthropicModelPricing = new AnthropicModelPricing();
|
|
786
|
+
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
787
|
+
providerResponse.model,
|
|
788
|
+
usage.input_tokens,
|
|
789
|
+
usage.output_tokens
|
|
728
790
|
);
|
|
729
|
-
|
|
791
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, toolCallingResponse.model.name));
|
|
792
|
+
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
730
793
|
providerResponse = toolCallingResponse;
|
|
731
794
|
}
|
|
732
|
-
return await this.nextHandler.handle(
|
|
795
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
733
796
|
}
|
|
734
797
|
};
|
|
735
798
|
|
|
736
799
|
// src/providers/anthropic/response-handler/usage.ts
|
|
737
800
|
var UsageHandler2 = class extends ResponseHandler {
|
|
738
|
-
async handle(
|
|
739
|
-
const providerResponse =
|
|
801
|
+
async handle(mozaikResponse) {
|
|
802
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
740
803
|
const usage = providerResponse.usage;
|
|
804
|
+
const anthropicModelPricing = new AnthropicModelPricing();
|
|
805
|
+
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
806
|
+
providerResponse.model,
|
|
807
|
+
usage.input_tokens,
|
|
808
|
+
usage.output_tokens
|
|
809
|
+
);
|
|
741
810
|
if (usage) {
|
|
742
|
-
|
|
743
|
-
new UsageEntry(usage.input_tokens, usage.output_tokens, providerResponse.model)
|
|
744
|
-
);
|
|
811
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
745
812
|
}
|
|
746
|
-
return await this.nextHandler.handle(
|
|
813
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
747
814
|
}
|
|
748
815
|
};
|
|
749
816
|
|
|
750
817
|
// src/providers/anthropic/response-handler/undhandled.ts
|
|
751
818
|
var UnhandledResponseHandler = class extends ResponseHandler {
|
|
752
|
-
async handle(
|
|
819
|
+
async handle(mozaikResponse) {
|
|
753
820
|
var _a, _b;
|
|
754
|
-
const providerResponse =
|
|
821
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
755
822
|
const id = (_a = providerResponse == null ? void 0 : providerResponse.id) != null ? _a : "unknown";
|
|
756
823
|
const model = (_b = providerResponse == null ? void 0 : providerResponse.model) != null ? _b : "unknown";
|
|
757
824
|
throw new Error(`No response handler matched. response_id=${id} model=${model}`);
|
|
@@ -764,29 +831,24 @@ var AnthropicEndpoint = class extends Endpoint {
|
|
|
764
831
|
super(...arguments);
|
|
765
832
|
this.requestBuilder = new AnthropicRequestBuilder();
|
|
766
833
|
}
|
|
767
|
-
async sendRequest(
|
|
768
|
-
const
|
|
769
|
-
const client = AnthropicClientResolver.resolve(
|
|
770
|
-
const response = await client.send(
|
|
771
|
-
const
|
|
772
|
-
|
|
834
|
+
async sendRequest(mozaikRequest) {
|
|
835
|
+
const providerRequest = this.buildRequest(mozaikRequest);
|
|
836
|
+
const client = AnthropicClientResolver.resolve(providerRequest);
|
|
837
|
+
const response = await client.send(providerRequest);
|
|
838
|
+
const mozaikResponse = new MozaikResponse();
|
|
839
|
+
mozaikResponse.setProviderResponse(response);
|
|
773
840
|
const usageHandler = new UsageHandler2();
|
|
774
|
-
const toolUseHandler = new ToolUseHandler(
|
|
841
|
+
const toolUseHandler = new ToolUseHandler(
|
|
842
|
+
providerRequest,
|
|
843
|
+
mozaikRequest.tools ? mozaikRequest.tools : []
|
|
844
|
+
);
|
|
775
845
|
const parsedOutputHandler = new ParsedOutputHandler();
|
|
776
846
|
const contentHandler = new ContentHandler2();
|
|
777
847
|
const unhandledResponseHandler = new UnhandledResponseHandler();
|
|
778
848
|
usageHandler.setNextHandler(toolUseHandler).setNextHandler(parsedOutputHandler).setNextHandler(contentHandler).setNextHandler(unhandledResponseHandler);
|
|
779
849
|
const responseHandler = usageHandler;
|
|
780
|
-
const result = await responseHandler.handle(
|
|
781
|
-
|
|
782
|
-
inputTokens: e.inputTokens,
|
|
783
|
-
outputTokens: e.outputTokens,
|
|
784
|
-
model: e.model
|
|
785
|
-
}));
|
|
786
|
-
return {
|
|
787
|
-
data: result.getResponse(),
|
|
788
|
-
usageEntries
|
|
789
|
-
};
|
|
850
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
851
|
+
return result.getResponse();
|
|
790
852
|
}
|
|
791
853
|
};
|
|
792
854
|
|
|
@@ -809,28 +871,28 @@ var DefaultEndpointResolver = class extends EndpointResolver {
|
|
|
809
871
|
};
|
|
810
872
|
|
|
811
873
|
// src/core/agents/agent.ts
|
|
812
|
-
var
|
|
813
|
-
constructor(
|
|
874
|
+
var MozaikAgent = class {
|
|
875
|
+
constructor(request) {
|
|
814
876
|
this.gateway = new RequestGateway(new DefaultEndpointResolver());
|
|
815
|
-
this.
|
|
877
|
+
this.request = request;
|
|
816
878
|
}
|
|
817
879
|
setModel(model) {
|
|
818
|
-
this.
|
|
880
|
+
this.request.model = model;
|
|
819
881
|
}
|
|
820
882
|
setMessages(messages) {
|
|
821
|
-
this.
|
|
883
|
+
this.request.messages = messages;
|
|
822
884
|
}
|
|
823
885
|
setTask(task) {
|
|
824
|
-
this.
|
|
886
|
+
this.request.task = task;
|
|
825
887
|
}
|
|
826
888
|
setStructuredOutput(schema) {
|
|
827
|
-
this.
|
|
889
|
+
this.request.structuredOutput = schema;
|
|
828
890
|
}
|
|
829
891
|
async act(task) {
|
|
830
892
|
if (task) {
|
|
831
|
-
this.
|
|
893
|
+
this.request.task = task;
|
|
832
894
|
}
|
|
833
|
-
return await this.gateway.invoke(this.
|
|
895
|
+
return await this.gateway.invoke(this.request);
|
|
834
896
|
}
|
|
835
897
|
};
|
|
836
898
|
|
|
@@ -962,11 +1024,11 @@ var Task = class extends WorkUnit {
|
|
|
962
1024
|
}
|
|
963
1025
|
async execute(hook = DEFAULT_CLUSTER_HOOK) {
|
|
964
1026
|
hook.beforeTask(this);
|
|
965
|
-
const
|
|
1027
|
+
const request = {
|
|
966
1028
|
model: this.model,
|
|
967
1029
|
task: this.task
|
|
968
1030
|
};
|
|
969
|
-
const agent = new
|
|
1031
|
+
const agent = new MozaikAgent(request);
|
|
970
1032
|
const result = await agent.act(this.task);
|
|
971
1033
|
hook.afterTask(this, result);
|
|
972
1034
|
return result;
|
|
@@ -1001,9 +1063,9 @@ Rules:
|
|
|
1001
1063
|
- Keep prompts actionable.
|
|
1002
1064
|
- Don't ask user for any input, just do the thing with available data you have.
|
|
1003
1065
|
`;
|
|
1004
|
-
var PlanningAgent = class extends
|
|
1005
|
-
constructor(
|
|
1006
|
-
super(
|
|
1066
|
+
var PlanningAgent = class extends MozaikAgent {
|
|
1067
|
+
constructor(request) {
|
|
1068
|
+
super(request);
|
|
1007
1069
|
}
|
|
1008
1070
|
async planFromGoal(goal) {
|
|
1009
1071
|
this.setStructuredOutput(PlanSchema);
|
|
@@ -1013,7 +1075,7 @@ Goal: ${goal}`);
|
|
|
1013
1075
|
}
|
|
1014
1076
|
};
|
|
1015
1077
|
export {
|
|
1016
|
-
|
|
1078
|
+
MozaikAgent,
|
|
1017
1079
|
PlanningAgent,
|
|
1018
1080
|
Task,
|
|
1019
1081
|
WorkUnit,
|