@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.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,23 +429,39 @@ 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
|
}
|
|
436
|
+
getResponse() {
|
|
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
|
+
};
|
|
444
|
+
}
|
|
445
|
+
getUsageEntries() {
|
|
446
|
+
return this.usageEntries;
|
|
447
|
+
}
|
|
401
448
|
};
|
|
402
449
|
|
|
403
450
|
// src/providers/openai/response-handler/usage.ts
|
|
404
451
|
var UsageHandler = class extends ResponseHandler {
|
|
405
|
-
async handle(
|
|
406
|
-
|
|
452
|
+
async handle(mozaikResponse) {
|
|
453
|
+
var _a, _b;
|
|
454
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
407
455
|
const usage = providerResponse.usage;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
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);
|
|
414
465
|
}
|
|
415
466
|
};
|
|
416
467
|
|
|
@@ -425,8 +476,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
425
476
|
const request = this.buildRequest(command);
|
|
426
477
|
const client = OpenAIClientResolver.resolve(request);
|
|
427
478
|
const response = await client.send(request);
|
|
428
|
-
const
|
|
429
|
-
|
|
479
|
+
const mozaikResponse = new MozaikResponse();
|
|
480
|
+
mozaikResponse.setProviderResponse(response);
|
|
430
481
|
const usageHandler = new UsageHandler();
|
|
431
482
|
const functionCallsHandler = new FunctionCallsHandler(
|
|
432
483
|
request,
|
|
@@ -437,7 +488,8 @@ var OpenAIResponses = class extends Endpoint {
|
|
|
437
488
|
const contentHandler = new ContentHandler();
|
|
438
489
|
usageHandler.setNextHandler(functionCallsHandler).setNextHandler(outputParsedHandler).setNextHandler(outputTextHandler).setNextHandler(contentHandler);
|
|
439
490
|
const responseHandler = usageHandler;
|
|
440
|
-
|
|
491
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
492
|
+
return result.getResponse();
|
|
441
493
|
} catch (error) {
|
|
442
494
|
console.warn("[OpenAIProvider] Responses API request failed:", error);
|
|
443
495
|
throw error;
|
|
@@ -628,26 +680,51 @@ var AnthropicClientResolver = class {
|
|
|
628
680
|
|
|
629
681
|
// src/providers/anthropic/response-handler/parsed-output.ts
|
|
630
682
|
var ParsedOutputHandler = class extends ResponseHandler {
|
|
631
|
-
async handle(
|
|
632
|
-
const providerResponse =
|
|
683
|
+
async handle(mozaikResponse) {
|
|
684
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
633
685
|
if (providerResponse.parsed_output) {
|
|
634
|
-
|
|
635
|
-
return
|
|
686
|
+
mozaikResponse.setResponseData(providerResponse.parsed_output);
|
|
687
|
+
return mozaikResponse;
|
|
636
688
|
}
|
|
637
|
-
return await this.nextHandler.handle(
|
|
689
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
638
690
|
}
|
|
639
691
|
};
|
|
640
692
|
|
|
641
693
|
// src/providers/anthropic/response-handler/content.ts
|
|
642
694
|
var ContentHandler2 = class extends ResponseHandler {
|
|
643
|
-
async handle(
|
|
644
|
-
const providerResponse =
|
|
695
|
+
async handle(mozaikResponse) {
|
|
696
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
645
697
|
const content = providerResponse.content.filter((block) => block.type === "text").map((block) => block.type === "text" ? block.text : "").join("");
|
|
646
698
|
if (content) {
|
|
647
|
-
|
|
648
|
-
return
|
|
699
|
+
mozaikResponse.setResponseData(content);
|
|
700
|
+
return mozaikResponse;
|
|
649
701
|
}
|
|
650
|
-
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`);
|
|
651
728
|
}
|
|
652
729
|
};
|
|
653
730
|
|
|
@@ -688,8 +765,8 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
688
765
|
}
|
|
689
766
|
return results;
|
|
690
767
|
}
|
|
691
|
-
async handle(
|
|
692
|
-
let providerResponse =
|
|
768
|
+
async handle(mozaikResponse) {
|
|
769
|
+
let providerResponse = mozaikResponse.providerResponse;
|
|
693
770
|
while (providerResponse.stop_reason === "tool_use") {
|
|
694
771
|
const toolUseBlocks = providerResponse.content.filter(
|
|
695
772
|
(block) => block.type === "tool_use"
|
|
@@ -704,39 +781,44 @@ var ToolUseHandler = class extends ResponseHandler {
|
|
|
704
781
|
content: toolResults
|
|
705
782
|
});
|
|
706
783
|
const toolCallingResponse = await this.client.send(this.request);
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
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
|
|
713
790
|
);
|
|
714
|
-
|
|
791
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, toolCallingResponse.model.name));
|
|
792
|
+
mozaikResponse.setProviderResponse(toolCallingResponse);
|
|
715
793
|
providerResponse = toolCallingResponse;
|
|
716
794
|
}
|
|
717
|
-
return await this.nextHandler.handle(
|
|
795
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
718
796
|
}
|
|
719
797
|
};
|
|
720
798
|
|
|
721
799
|
// src/providers/anthropic/response-handler/usage.ts
|
|
722
800
|
var UsageHandler2 = class extends ResponseHandler {
|
|
723
|
-
async handle(
|
|
724
|
-
const providerResponse =
|
|
801
|
+
async handle(mozaikResponse) {
|
|
802
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
725
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
|
+
);
|
|
726
810
|
if (usage) {
|
|
727
|
-
|
|
728
|
-
new UsageEntry(usage.input_tokens, usage.output_tokens, providerResponse.model)
|
|
729
|
-
);
|
|
811
|
+
mozaikResponse.addUsageEntry(new UsageEntry(totalCost, providerResponse.model));
|
|
730
812
|
}
|
|
731
|
-
return await this.nextHandler.handle(
|
|
813
|
+
return await this.nextHandler.handle(mozaikResponse);
|
|
732
814
|
}
|
|
733
815
|
};
|
|
734
816
|
|
|
735
817
|
// src/providers/anthropic/response-handler/undhandled.ts
|
|
736
818
|
var UnhandledResponseHandler = class extends ResponseHandler {
|
|
737
|
-
async handle(
|
|
819
|
+
async handle(mozaikResponse) {
|
|
738
820
|
var _a, _b;
|
|
739
|
-
const providerResponse =
|
|
821
|
+
const providerResponse = mozaikResponse.providerResponse;
|
|
740
822
|
const id = (_a = providerResponse == null ? void 0 : providerResponse.id) != null ? _a : "unknown";
|
|
741
823
|
const model = (_b = providerResponse == null ? void 0 : providerResponse.model) != null ? _b : "unknown";
|
|
742
824
|
throw new Error(`No response handler matched. response_id=${id} model=${model}`);
|
|
@@ -749,20 +831,24 @@ var AnthropicEndpoint = class extends Endpoint {
|
|
|
749
831
|
super(...arguments);
|
|
750
832
|
this.requestBuilder = new AnthropicRequestBuilder();
|
|
751
833
|
}
|
|
752
|
-
async sendRequest(
|
|
753
|
-
const
|
|
754
|
-
const client = AnthropicClientResolver.resolve(
|
|
755
|
-
const response = await client.send(
|
|
756
|
-
const
|
|
757
|
-
|
|
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);
|
|
758
840
|
const usageHandler = new UsageHandler2();
|
|
759
|
-
const toolUseHandler = new ToolUseHandler(
|
|
841
|
+
const toolUseHandler = new ToolUseHandler(
|
|
842
|
+
providerRequest,
|
|
843
|
+
mozaikRequest.tools ? mozaikRequest.tools : []
|
|
844
|
+
);
|
|
760
845
|
const parsedOutputHandler = new ParsedOutputHandler();
|
|
761
846
|
const contentHandler = new ContentHandler2();
|
|
762
847
|
const unhandledResponseHandler = new UnhandledResponseHandler();
|
|
763
848
|
usageHandler.setNextHandler(toolUseHandler).setNextHandler(parsedOutputHandler).setNextHandler(contentHandler).setNextHandler(unhandledResponseHandler);
|
|
764
849
|
const responseHandler = usageHandler;
|
|
765
|
-
|
|
850
|
+
const result = await responseHandler.handle(mozaikResponse);
|
|
851
|
+
return result.getResponse();
|
|
766
852
|
}
|
|
767
853
|
};
|
|
768
854
|
|
|
@@ -785,28 +871,28 @@ var DefaultEndpointResolver = class extends EndpointResolver {
|
|
|
785
871
|
};
|
|
786
872
|
|
|
787
873
|
// src/core/agents/agent.ts
|
|
788
|
-
var
|
|
789
|
-
constructor(
|
|
874
|
+
var MozaikAgent = class {
|
|
875
|
+
constructor(request) {
|
|
790
876
|
this.gateway = new RequestGateway(new DefaultEndpointResolver());
|
|
791
|
-
this.
|
|
877
|
+
this.request = request;
|
|
792
878
|
}
|
|
793
879
|
setModel(model) {
|
|
794
|
-
this.
|
|
880
|
+
this.request.model = model;
|
|
795
881
|
}
|
|
796
882
|
setMessages(messages) {
|
|
797
|
-
this.
|
|
883
|
+
this.request.messages = messages;
|
|
798
884
|
}
|
|
799
885
|
setTask(task) {
|
|
800
|
-
this.
|
|
886
|
+
this.request.task = task;
|
|
801
887
|
}
|
|
802
888
|
setStructuredOutput(schema) {
|
|
803
|
-
this.
|
|
889
|
+
this.request.structuredOutput = schema;
|
|
804
890
|
}
|
|
805
891
|
async act(task) {
|
|
806
892
|
if (task) {
|
|
807
|
-
this.
|
|
893
|
+
this.request.task = task;
|
|
808
894
|
}
|
|
809
|
-
return await this.gateway.invoke(this.
|
|
895
|
+
return await this.gateway.invoke(this.request);
|
|
810
896
|
}
|
|
811
897
|
};
|
|
812
898
|
|
|
@@ -938,11 +1024,11 @@ var Task = class extends WorkUnit {
|
|
|
938
1024
|
}
|
|
939
1025
|
async execute(hook = DEFAULT_CLUSTER_HOOK) {
|
|
940
1026
|
hook.beforeTask(this);
|
|
941
|
-
const
|
|
1027
|
+
const request = {
|
|
942
1028
|
model: this.model,
|
|
943
1029
|
task: this.task
|
|
944
1030
|
};
|
|
945
|
-
const agent = new
|
|
1031
|
+
const agent = new MozaikAgent(request);
|
|
946
1032
|
const result = await agent.act(this.task);
|
|
947
1033
|
hook.afterTask(this, result);
|
|
948
1034
|
return result;
|
|
@@ -977,9 +1063,9 @@ Rules:
|
|
|
977
1063
|
- Keep prompts actionable.
|
|
978
1064
|
- Don't ask user for any input, just do the thing with available data you have.
|
|
979
1065
|
`;
|
|
980
|
-
var PlanningAgent = class extends
|
|
981
|
-
constructor(
|
|
982
|
-
super(
|
|
1066
|
+
var PlanningAgent = class extends MozaikAgent {
|
|
1067
|
+
constructor(request) {
|
|
1068
|
+
super(request);
|
|
983
1069
|
}
|
|
984
1070
|
async planFromGoal(goal) {
|
|
985
1071
|
this.setStructuredOutput(PlanSchema);
|
|
@@ -989,7 +1075,7 @@ Goal: ${goal}`);
|
|
|
989
1075
|
}
|
|
990
1076
|
};
|
|
991
1077
|
export {
|
|
992
|
-
|
|
1078
|
+
MozaikAgent,
|
|
993
1079
|
PlanningAgent,
|
|
994
1080
|
Task,
|
|
995
1081
|
WorkUnit,
|