@mozaik-ai/core 0.7.1 → 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 +205 -119
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +204 -118
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
33
|
+
MozaikAgent: () => MozaikAgent,
|
|
34
34
|
PlanningAgent: () => PlanningAgent,
|
|
35
35
|
Task: () => Task,
|
|
36
36
|
WorkUnit: () => WorkUnit,
|
|
@@ -49,9 +49,9 @@ var RequestGateway = class {
|
|
|
49
49
|
constructor(endpointResolver) {
|
|
50
50
|
this.endpointResolver = endpointResolver;
|
|
51
51
|
}
|
|
52
|
-
async invoke(
|
|
53
|
-
this.endpoint = this.endpointResolver.resolve(
|
|
54
|
-
return await this.endpoint.sendRequest(
|
|
52
|
+
async invoke(request) {
|
|
53
|
+
this.endpoint = this.endpointResolver.resolve(request.model);
|
|
54
|
+
return await this.endpoint.sendRequest(request);
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
57
|
|
|
@@ -77,55 +77,55 @@ var CapabilityHandler = class {
|
|
|
77
77
|
this.nextHandler = capability;
|
|
78
78
|
return this.nextHandler;
|
|
79
79
|
}
|
|
80
|
-
handle(
|
|
81
|
-
this.apply(
|
|
80
|
+
handle(request, builder) {
|
|
81
|
+
this.apply(request, builder);
|
|
82
82
|
if (this.nextHandler) {
|
|
83
|
-
this.nextHandler.handle(
|
|
83
|
+
this.nextHandler.handle(request, builder);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
87
|
|
|
88
88
|
// src/core/command-handler/messages.ts
|
|
89
89
|
var MessagesHandler = class extends CapabilityHandler {
|
|
90
|
-
apply(
|
|
91
|
-
if (
|
|
92
|
-
builder.addMessages(
|
|
90
|
+
apply(request, builder) {
|
|
91
|
+
if (request.messages) {
|
|
92
|
+
builder.addMessages(request.messages);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
96
|
|
|
97
97
|
// src/core/command-handler/task.ts
|
|
98
98
|
var TaskHandler = class extends CapabilityHandler {
|
|
99
|
-
apply(
|
|
100
|
-
if (
|
|
101
|
-
builder.addTask(
|
|
99
|
+
apply(request, builder) {
|
|
100
|
+
if (request.task) {
|
|
101
|
+
builder.addTask(request.task);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
};
|
|
105
105
|
|
|
106
106
|
// src/core/command-handler/model.ts
|
|
107
107
|
var ModelHandler = class extends CapabilityHandler {
|
|
108
|
-
apply(
|
|
109
|
-
if (
|
|
110
|
-
builder.addModel(
|
|
108
|
+
apply(request, builder) {
|
|
109
|
+
if (request.model) {
|
|
110
|
+
builder.addModel(request.model);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
114
|
|
|
115
115
|
// src/core/command-handler/structured-output.ts
|
|
116
116
|
var StructuredOutputlHandler = class extends CapabilityHandler {
|
|
117
|
-
apply(
|
|
118
|
-
if (
|
|
119
|
-
builder.addStructuredOutput(
|
|
117
|
+
apply(request, builder) {
|
|
118
|
+
if (request.structuredOutput) {
|
|
119
|
+
builder.addStructuredOutput(request.structuredOutput);
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
};
|
|
123
123
|
|
|
124
124
|
// src/core/command-handler/tools.ts
|
|
125
125
|
var ToolsHandler = class extends CapabilityHandler {
|
|
126
|
-
apply(
|
|
127
|
-
if (
|
|
128
|
-
requestBuilder.addTools(
|
|
126
|
+
apply(mozaikRequest, requestBuilder) {
|
|
127
|
+
if (mozaikRequest.tools && mozaikRequest.tools.length > 0) {
|
|
128
|
+
requestBuilder.addTools(mozaikRequest.tools);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
};
|
|
@@ -133,10 +133,10 @@ var ToolsHandler = class extends CapabilityHandler {
|
|
|
133
133
|
// src/core/endpoint/endpoint.ts
|
|
134
134
|
var Endpoint = class {
|
|
135
135
|
constructor() {
|
|
136
|
-
this.
|
|
136
|
+
this.request = null;
|
|
137
137
|
}
|
|
138
|
-
buildRequest(
|
|
139
|
-
this.
|
|
138
|
+
buildRequest(request) {
|
|
139
|
+
this.request = request;
|
|
140
140
|
this.requestBuilder.initialize();
|
|
141
141
|
const messagesHandler = new MessagesHandler();
|
|
142
142
|
const taskHandler = new TaskHandler();
|
|
@@ -144,7 +144,7 @@ var Endpoint = class {
|
|
|
144
144
|
const structuredOutputHandler = new StructuredOutputlHandler();
|
|
145
145
|
const toolsHandler = new ToolsHandler();
|
|
146
146
|
messagesHandler.setNextHandler(taskHandler).setNextHandler(modelHandler).setNextHandler(structuredOutputHandler).setNextHandler(toolsHandler);
|
|
147
|
-
messagesHandler.handle(
|
|
147
|
+
messagesHandler.handle(request, this.requestBuilder);
|
|
148
148
|
return this.requestBuilder.build();
|
|
149
149
|
}
|
|
150
150
|
};
|
|
@@ -294,7 +294,7 @@ var OutputParsedHandler = class extends ResponseHandler {
|
|
|
294
294
|
async handle(responseContext) {
|
|
295
295
|
const providerResponse = responseContext.providerResponse;
|
|
296
296
|
if (providerResponse.output_parsed) {
|
|
297
|
-
responseContext.
|
|
297
|
+
responseContext.setResponseData(providerResponse.output_parsed);
|
|
298
298
|
return responseContext;
|
|
299
299
|
}
|
|
300
300
|
return await this.nextHandler.handle(responseContext);
|
|
@@ -303,40 +303,72 @@ var OutputParsedHandler = class extends ResponseHandler {
|
|
|
303
303
|
|
|
304
304
|
// src/providers/openai/response-handler/content.ts
|
|
305
305
|
var ContentHandler = class extends ResponseHandler {
|
|
306
|
-
async handle(
|
|
306
|
+
async handle(mozaikResponse) {
|
|
307
307
|
var _a, _b;
|
|
308
|
-
const providerResponse =
|
|
308
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
309
309
|
const firstOutput = (_a = providerResponse.output) == null ? void 0 : _a[0];
|
|
310
310
|
if (firstOutput && "content" in firstOutput) {
|
|
311
311
|
const firstContent = (_b = firstOutput.content) == null ? void 0 : _b[0];
|
|
312
312
|
if (firstContent && "text" in firstContent) {
|
|
313
|
-
|
|
314
|
-
return
|
|
313
|
+
mozaikResponse.setResponseData(firstContent.text);
|
|
314
|
+
return mozaikResponse;
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
|
-
return await this.nextHandler.handle(
|
|
317
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
318
318
|
}
|
|
319
319
|
};
|
|
320
320
|
|
|
321
321
|
// src/providers/openai/response-handler/output-text.ts
|
|
322
322
|
var OutputTextHandler = class extends ResponseHandler {
|
|
323
|
-
async handle(
|
|
324
|
-
const providerResponse =
|
|
323
|
+
async handle(mozaikResponse) {
|
|
324
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
325
325
|
if (providerResponse.output_text) {
|
|
326
|
-
|
|
327
|
-
return
|
|
326
|
+
mozaikResponse.setResponseData(providerResponse.output_text);
|
|
327
|
+
return mozaikResponse;
|
|
328
328
|
}
|
|
329
|
-
return await this.nextHandler.handle(
|
|
329
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
330
330
|
}
|
|
331
331
|
};
|
|
332
332
|
|
|
333
|
-
// src/core/
|
|
333
|
+
// src/core/usage-entry.ts
|
|
334
334
|
var UsageEntry = class {
|
|
335
|
-
constructor(
|
|
336
|
-
this.
|
|
337
|
-
this.outputTokens = outputTokens;
|
|
335
|
+
constructor(priceInUsd, model) {
|
|
336
|
+
this.priceInUsd = priceInUsd;
|
|
338
337
|
this.model = model;
|
|
339
|
-
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/providers/openai/model-pricing.ts
|
|
342
|
+
var OpenAIModelPricing = class {
|
|
343
|
+
calculateTotalCost(inputTokens, outputTokens, cachedInputTokens) {
|
|
344
|
+
const inputCost = (inputTokens - cachedInputTokens) * this.inputPricePer1MToken / 1e6;
|
|
345
|
+
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
346
|
+
const cachedCost = cachedInputTokens * this.cachedPricePer1MToken / 1e6;
|
|
347
|
+
return inputCost + outputCost + cachedCost;
|
|
348
|
+
}
|
|
349
|
+
getPriceInUsd(modelName, inputTokens, outputTokens, cachedInputTokens) {
|
|
350
|
+
if (modelName === "gpt-5") {
|
|
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 === "gpt-5-mini") {
|
|
356
|
+
this.inputPricePer1MToken = 0.25;
|
|
357
|
+
this.outputPricePer1MToken = 2;
|
|
358
|
+
this.cachedPricePer1MToken = 0.025;
|
|
359
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
360
|
+
} else if (modelName === "gpt-5-nano") {
|
|
361
|
+
this.inputPricePer1MToken = 0.05;
|
|
362
|
+
this.outputPricePer1MToken = 0.4;
|
|
363
|
+
this.cachedPricePer1MToken = 5e-3;
|
|
364
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
365
|
+
} else if (modelName === "gpt-5.1") {
|
|
366
|
+
this.inputPricePer1MToken = 1.25;
|
|
367
|
+
this.outputPricePer1MToken = 10;
|
|
368
|
+
this.cachedPricePer1MToken = 0.125;
|
|
369
|
+
return this.calculateTotalCost(inputTokens, outputTokens, cachedInputTokens);
|
|
370
|
+
}
|
|
371
|
+
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
340
372
|
}
|
|
341
373
|
};
|
|
342
374
|
|
|
@@ -397,8 +429,9 @@ var FunctionCallsHandler = class extends ResponseHandler {
|
|
|
397
429
|
}
|
|
398
430
|
return results;
|
|
399
431
|
}
|
|
400
|
-
async handle(
|
|
401
|
-
|
|
432
|
+
async handle(mozaikResponse) {
|
|
433
|
+
var _a, _b;
|
|
434
|
+
let providerResponse = mozaikResponse.providerResponse;
|
|
402
435
|
let toolCallingResponse;
|
|
403
436
|
while (this.hasToolCalls(providerResponse)) {
|
|
404
437
|
const toolCalls = this.extractToolCalls(providerResponse);
|
|
@@ -406,23 +439,25 @@ var FunctionCallsHandler = class extends ResponseHandler {
|
|
|
406
439
|
this.request.input = toolResults;
|
|
407
440
|
this.request.previous_response_id = providerResponse.id;
|
|
408
441
|
toolCallingResponse = await this.client.send(this.request);
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
442
|
+
const openAIModelPricing = new OpenAIModelPricing();
|
|
443
|
+
const usage = toolCallingResponse.usage;
|
|
444
|
+
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
445
|
+
providerResponse.model,
|
|
446
|
+
usage.input_tokens,
|
|
447
|
+
usage.output_tokens,
|
|
448
|
+
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
415
449
|
);
|
|
416
|
-
|
|
417
|
-
|
|
450
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
451
|
+
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
452
|
+
mozaikResponse.setResponseData(toolCallingResponse);
|
|
418
453
|
providerResponse = toolCallingResponse;
|
|
419
454
|
}
|
|
420
|
-
return await this.nextHandler.handle(
|
|
455
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
421
456
|
}
|
|
422
457
|
};
|
|
423
458
|
|
|
424
|
-
// src/core/
|
|
425
|
-
var
|
|
459
|
+
// src/core/response.ts
|
|
460
|
+
var MozaikResponse = class {
|
|
426
461
|
constructor() {
|
|
427
462
|
this.usageEntries = [];
|
|
428
463
|
}
|
|
@@ -434,23 +469,39 @@ var ResponseContext = class {
|
|
|
434
469
|
this.usageEntries.push(usageEntry);
|
|
435
470
|
return this;
|
|
436
471
|
}
|
|
437
|
-
|
|
438
|
-
this.
|
|
472
|
+
setResponseData(response) {
|
|
473
|
+
this.responseData = response;
|
|
439
474
|
return this;
|
|
440
475
|
}
|
|
476
|
+
getResponse() {
|
|
477
|
+
return {
|
|
478
|
+
data: this.responseData,
|
|
479
|
+
usage: {
|
|
480
|
+
entries: this.usageEntries
|
|
481
|
+
},
|
|
482
|
+
totalCost: this.usageEntries.reduce((acc, entry) => acc + entry.priceInUsd, 0)
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
getUsageEntries() {
|
|
486
|
+
return this.usageEntries;
|
|
487
|
+
}
|
|
441
488
|
};
|
|
442
489
|
|
|
443
490
|
// src/providers/openai/response-handler/usage.ts
|
|
444
491
|
var UsageHandler = class extends ResponseHandler {
|
|
445
|
-
async handle(
|
|
446
|
-
|
|
492
|
+
async handle(mozaikResponse) {
|
|
493
|
+
var _a, _b;
|
|
494
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
447
495
|
const usage = providerResponse.usage;
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
496
|
+
const openAIModelPricing = new OpenAIModelPricing();
|
|
497
|
+
const totalCost = openAIModelPricing.getPriceInUsd(
|
|
498
|
+
providerResponse.model,
|
|
499
|
+
usage.input_tokens,
|
|
500
|
+
usage.output_tokens,
|
|
501
|
+
(_b = (_a = usage.input_token_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0
|
|
502
|
+
);
|
|
503
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
504
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
454
505
|
}
|
|
455
506
|
};
|
|
456
507
|
|
|
@@ -465,8 +516,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
465
516
|
const request = this.buildRequest(command);
|
|
466
517
|
const client = OpenAIClientResolver.resolve(request);
|
|
467
518
|
const response = await client.send(request);
|
|
468
|
-
const
|
|
469
|
-
|
|
519
|
+
const mozaikResponse = new MozaikResponse();
|
|
520
|
+
mozaikResponse.setProviderResponse(response);
|
|
470
521
|
const usageHandler = new UsageHandler();
|
|
471
522
|
const functionCallsHandler = new FunctionCallsHandler(
|
|
472
523
|
request,
|
|
@@ -477,7 +528,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
477
528
|
const contentHandler = new ContentHandler();
|
|
478
529
|
usageHandler.setNextHandler(functionCallsHandler).setNextHandler(outputParsedHandler).setNextHandler(outputTextHandler).setNextHandler(contentHandler);
|
|
479
530
|
const responseHandler = usageHandler;
|
|
480
|
-
|
|
531
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
532
|
+
return result.getResponse();
|
|
481
533
|
} catch (error) {
|
|
482
534
|
console.warn("[OpenAIProvider] Responses API request failed:", error);
|
|
483
535
|
throw error;
|
|
@@ -668,26 +720,51 @@ var AnthropicClientResolver = class {
|
|
|
668
720
|
|
|
669
721
|
// src/providers/anthropic/response-handler/parsed-output.ts
|
|
670
722
|
var ParsedOutputHandler = class extends ResponseHandler {
|
|
671
|
-
async handle(
|
|
672
|
-
const providerResponse =
|
|
723
|
+
async handle(mozaikResponse) {
|
|
724
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
673
725
|
if (providerResponse.parsed_output) {
|
|
674
|
-
|
|
675
|
-
return
|
|
726
|
+
mozaikResponse.setResponseData(providerResponse.parsed_output);
|
|
727
|
+
return mozaikResponse;
|
|
676
728
|
}
|
|
677
|
-
return await this.nextHandler.handle(
|
|
729
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
678
730
|
}
|
|
679
731
|
};
|
|
680
732
|
|
|
681
733
|
// src/providers/anthropic/response-handler/content.ts
|
|
682
734
|
var ContentHandler2 = class extends ResponseHandler {
|
|
683
|
-
async handle(
|
|
684
|
-
const providerResponse =
|
|
735
|
+
async handle(mozaikResponse) {
|
|
736
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
685
737
|
const content = providerResponse.content.filter((block) => block.type === "text").map((block) => block.type === "text" ? block.text : "").join("");
|
|
686
738
|
if (content) {
|
|
687
|
-
|
|
688
|
-
return
|
|
739
|
+
mozaikResponse.setResponseData(content);
|
|
740
|
+
return mozaikResponse;
|
|
689
741
|
}
|
|
690
|
-
return await this.nextHandler.handle(
|
|
742
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
// src/providers/anthropic/model-pricing.ts
|
|
747
|
+
var AnthropicModelPricing = class {
|
|
748
|
+
calculateTotalCost(inputTokens, outputTokens) {
|
|
749
|
+
const inputCost = inputTokens * this.inputPricePer1MToken / 1e6;
|
|
750
|
+
const outputCost = outputTokens * this.outputPricePer1MToken / 1e6;
|
|
751
|
+
return inputCost + outputCost;
|
|
752
|
+
}
|
|
753
|
+
getPriceInUsd(modelName, inputTokens, outputTokens) {
|
|
754
|
+
if (modelName === "claude-sonnet-4.5") {
|
|
755
|
+
this.inputPricePer1MToken = 3;
|
|
756
|
+
this.outputPricePer1MToken = 15;
|
|
757
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
758
|
+
} else if (modelName === "claude-haiku-4.5") {
|
|
759
|
+
this.inputPricePer1MToken = 1;
|
|
760
|
+
this.outputPricePer1MToken = 5;
|
|
761
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
762
|
+
} else if (modelName === "claude-opus-4.5") {
|
|
763
|
+
this.inputPricePer1MToken = 5;
|
|
764
|
+
this.outputPricePer1MToken = 25;
|
|
765
|
+
return this.calculateTotalCost(inputTokens, outputTokens);
|
|
766
|
+
}
|
|
767
|
+
throw new Error(`Model name ${modelName} is not supported for this pricing`);
|
|
691
768
|
}
|
|
692
769
|
};
|
|
693
770
|
|
|
@@ -728,8 +805,8 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
728
805
|
}
|
|
729
806
|
return results;
|
|
730
807
|
}
|
|
731
|
-
async handle(
|
|
732
|
-
let providerResponse =
|
|
808
|
+
async handle(mozaikResponse) {
|
|
809
|
+
let providerResponse = mozaikResponse.providerResponse;
|
|
733
810
|
while (providerResponse.stop_reason === "tool_use") {
|
|
734
811
|
const toolUseBlocks = providerResponse.content.filter(
|
|
735
812
|
(block) => block.type === "tool_use"
|
|
@@ -744,39 +821,44 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
744
821
|
content: toolResults
|
|
745
822
|
});
|
|
746
823
|
const toolCallingResponse = await this.client.send(this.request);
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
824
|
+
const usage = toolCallingResponse.usage;
|
|
825
|
+
const anthropicModelPricing = new AnthropicModelPricing();
|
|
826
|
+
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
827
|
+
providerResponse.model,
|
|
828
|
+
usage.input_tokens,
|
|
829
|
+
usage.output_tokens
|
|
753
830
|
);
|
|
754
|
-
|
|
831
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, toolCallingResponse.model.name));
|
|
832
|
+
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
755
833
|
providerResponse = toolCallingResponse;
|
|
756
834
|
}
|
|
757
|
-
return await this.nextHandler.handle(
|
|
835
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
758
836
|
}
|
|
759
837
|
};
|
|
760
838
|
|
|
761
839
|
// src/providers/anthropic/response-handler/usage.ts
|
|
762
840
|
var UsageHandler2 = class extends ResponseHandler {
|
|
763
|
-
async handle(
|
|
764
|
-
const providerResponse =
|
|
841
|
+
async handle(mozaikResponse) {
|
|
842
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
765
843
|
const usage = providerResponse.usage;
|
|
844
|
+
const anthropicModelPricing = new AnthropicModelPricing();
|
|
845
|
+
const totalCost = anthropicModelPricing.getPriceInUsd(
|
|
846
|
+
providerResponse.model,
|
|
847
|
+
usage.input_tokens,
|
|
848
|
+
usage.output_tokens
|
|
849
|
+
);
|
|
766
850
|
if (usage) {
|
|
767
|
-
|
|
768
|
-
new UsageEntry(usage.input_tokens, usage.output_tokens, providerResponse.model)
|
|
769
|
-
);
|
|
851
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
770
852
|
}
|
|
771
|
-
return await this.nextHandler.handle(
|
|
853
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
772
854
|
}
|
|
773
855
|
};
|
|
774
856
|
|
|
775
857
|
// src/providers/anthropic/response-handler/undhandled.ts
|
|
776
858
|
var UnhandledResponseHandler = class extends ResponseHandler {
|
|
777
|
-
async handle(
|
|
859
|
+
async handle(mozaikResponse) {
|
|
778
860
|
var _a, _b;
|
|
779
|
-
const providerResponse =
|
|
861
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
780
862
|
const id = (_a = providerResponse == null ? void 0 : providerResponse.id) != null ? _a : "unknown";
|
|
781
863
|
const model = (_b = providerResponse == null ? void 0 : providerResponse.model) != null ? _b : "unknown";
|
|
782
864
|
throw new Error(`No response handler matched. response_id=${id} model=${model}`);
|
|
@@ -789,20 +871,24 @@ var AnthropicEndpoint = class extends Endpoint {
|
|
|
789
871
|
super(...arguments);
|
|
790
872
|
this.requestBuilder = new AnthropicRequestBuilder();
|
|
791
873
|
}
|
|
792
|
-
async sendRequest(
|
|
793
|
-
const
|
|
794
|
-
const client = AnthropicClientResolver.resolve(
|
|
795
|
-
const response = await client.send(
|
|
796
|
-
const
|
|
797
|
-
|
|
874
|
+
async sendRequest(mozaikRequest) {
|
|
875
|
+
const providerRequest = this.buildRequest(mozaikRequest);
|
|
876
|
+
const client = AnthropicClientResolver.resolve(providerRequest);
|
|
877
|
+
const response = await client.send(providerRequest);
|
|
878
|
+
const mozaikResponse = new MozaikResponse();
|
|
879
|
+
mozaikResponse.setProviderResponse(response);
|
|
798
880
|
const usageHandler = new UsageHandler2();
|
|
799
|
-
const toolUseHandler = new ToolUseHandler(
|
|
881
|
+
const toolUseHandler = new ToolUseHandler(
|
|
882
|
+
providerRequest,
|
|
883
|
+
mozaikRequest.tools ? mozaikRequest.tools : []
|
|
884
|
+
);
|
|
800
885
|
const parsedOutputHandler = new ParsedOutputHandler();
|
|
801
886
|
const contentHandler = new ContentHandler2();
|
|
802
887
|
const unhandledResponseHandler = new UnhandledResponseHandler();
|
|
803
888
|
usageHandler.setNextHandler(toolUseHandler).setNextHandler(parsedOutputHandler).setNextHandler(contentHandler).setNextHandler(unhandledResponseHandler);
|
|
804
889
|
const responseHandler = usageHandler;
|
|
805
|
-
|
|
890
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
891
|
+
return result.getResponse();
|
|
806
892
|
}
|
|
807
893
|
};
|
|
808
894
|
|
|
@@ -825,28 +911,28 @@ var DefaultEndpointResolver = class extends EndpointResolver {
|
|
|
825
911
|
};
|
|
826
912
|
|
|
827
913
|
// src/core/agents/agent.ts
|
|
828
|
-
var
|
|
829
|
-
constructor(
|
|
914
|
+
var MozaikAgent = class {
|
|
915
|
+
constructor(request) {
|
|
830
916
|
this.gateway = new RequestGateway(new DefaultEndpointResolver());
|
|
831
|
-
this.
|
|
917
|
+
this.request = request;
|
|
832
918
|
}
|
|
833
919
|
setModel(model) {
|
|
834
|
-
this.
|
|
920
|
+
this.request.model = model;
|
|
835
921
|
}
|
|
836
922
|
setMessages(messages) {
|
|
837
|
-
this.
|
|
923
|
+
this.request.messages = messages;
|
|
838
924
|
}
|
|
839
925
|
setTask(task) {
|
|
840
|
-
this.
|
|
926
|
+
this.request.task = task;
|
|
841
927
|
}
|
|
842
928
|
setStructuredOutput(schema) {
|
|
843
|
-
this.
|
|
929
|
+
this.request.structuredOutput = schema;
|
|
844
930
|
}
|
|
845
931
|
async act(task) {
|
|
846
932
|
if (task) {
|
|
847
|
-
this.
|
|
933
|
+
this.request.task = task;
|
|
848
934
|
}
|
|
849
|
-
return await this.gateway.invoke(this.
|
|
935
|
+
return await this.gateway.invoke(this.request);
|
|
850
936
|
}
|
|
851
937
|
};
|
|
852
938
|
|
|
@@ -978,11 +1064,11 @@ var Task = class extends WorkUnit {
|
|
|
978
1064
|
}
|
|
979
1065
|
async execute(hook = DEFAULT_CLUSTER_HOOK) {
|
|
980
1066
|
hook.beforeTask(this);
|
|
981
|
-
const
|
|
1067
|
+
const request = {
|
|
982
1068
|
model: this.model,
|
|
983
1069
|
task: this.task
|
|
984
1070
|
};
|
|
985
|
-
const agent = new
|
|
1071
|
+
const agent = new MozaikAgent(request);
|
|
986
1072
|
const result = await agent.act(this.task);
|
|
987
1073
|
hook.afterTask(this, result);
|
|
988
1074
|
return result;
|
|
@@ -1017,9 +1103,9 @@ Rules:
|
|
|
1017
1103
|
- Keep prompts actionable.
|
|
1018
1104
|
- Don't ask user for any input, just do the thing with available data you have.
|
|
1019
1105
|
`;
|
|
1020
|
-
var PlanningAgent = class extends
|
|
1021
|
-
constructor(
|
|
1022
|
-
super(
|
|
1106
|
+
var PlanningAgent = class extends MozaikAgent {
|
|
1107
|
+
constructor(request) {
|
|
1108
|
+
super(request);
|
|
1023
1109
|
}
|
|
1024
1110
|
async planFromGoal(goal) {
|
|
1025
1111
|
this.setStructuredOutput(PlanSchema);
|
|
@@ -1030,7 +1116,7 @@ Goal: ${goal}`);
|
|
|
1030
1116
|
};
|
|
1031
1117
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1032
1118
|
0 && (module.exports = {
|
|
1033
|
-
|
|
1119
|
+
MozaikAgent,
|
|
1034
1120
|
PlanningAgent,
|
|
1035
1121
|
Task,
|
|
1036
1122
|
WorkUnit,
|