@hiper2d/ai-agents 0.3.1 → 0.4.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 +1 -1
- package/dist/index.d.mts +92 -5
- package/dist/index.d.ts +92 -5
- package/dist/index.js +274 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +268 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -58,7 +58,9 @@ __export(index_exports, {
|
|
|
58
58
|
KimiAgent: () => KimiAgent,
|
|
59
59
|
LLM_CONSTANTS: () => LLM_CONSTANTS,
|
|
60
60
|
MESSAGE_ROLE: () => MESSAGE_ROLE,
|
|
61
|
+
META_REASONING_EFFORTS: () => META_REASONING_EFFORTS,
|
|
61
62
|
MODEL_PRICING: () => MODEL_PRICING,
|
|
63
|
+
MetaAgent: () => MetaAgent,
|
|
62
64
|
MiniMaxAgent: () => MiniMaxAgent,
|
|
63
65
|
MistralAgent: () => MistralAgent,
|
|
64
66
|
ModelAuthenticationError: () => ModelAuthenticationError,
|
|
@@ -91,6 +93,7 @@ __export(index_exports, {
|
|
|
91
93
|
calculateGoogleCost: () => calculateGoogleCost,
|
|
92
94
|
calculateGrokCost: () => calculateGrokCost,
|
|
93
95
|
calculateKimiCost: () => calculateKimiCost,
|
|
96
|
+
calculateMetaCost: () => calculateMetaCost,
|
|
94
97
|
calculateMistralCost: () => calculateMistralCost,
|
|
95
98
|
calculateModelCost: () => calculateModelCost,
|
|
96
99
|
calculateOpenAICost: () => calculateOpenAICost,
|
|
@@ -102,18 +105,20 @@ __export(index_exports, {
|
|
|
102
105
|
createVoiceAgent: () => createVoiceAgent,
|
|
103
106
|
evaluateBudget: () => evaluateBudget,
|
|
104
107
|
extractAnthropicTokenUsage: () => extractAnthropicTokenUsage,
|
|
105
|
-
extractAnthropicTokenUsageFromResponse: () =>
|
|
108
|
+
extractAnthropicTokenUsageFromResponse: () => extractTokenUsageFromResponse6,
|
|
106
109
|
extractDeepSeekTokenUsage: () => extractDeepSeekTokenUsage,
|
|
107
110
|
extractDeepSeekTokenUsageFromResponse: () => extractTokenUsageFromResponse2,
|
|
108
111
|
extractFirstJsonObject: () => extractFirstJsonObject,
|
|
109
112
|
extractGoogleTokenUsage: () => extractGoogleTokenUsage,
|
|
110
|
-
extractGoogleTokenUsageFromResponse: () =>
|
|
113
|
+
extractGoogleTokenUsageFromResponse: () => extractTokenUsageFromResponse7,
|
|
111
114
|
extractGrokTokenUsage: () => extractGrokTokenUsage,
|
|
112
115
|
extractGrokTokenUsageFromResponse: () => extractTokenUsageFromResponse4,
|
|
113
116
|
extractKimiTokenUsage: () => extractKimiTokenUsage,
|
|
114
117
|
extractKimiTokenUsageFromResponse: () => extractTokenUsageFromResponse3,
|
|
118
|
+
extractMetaTokenUsage: () => extractMetaTokenUsage,
|
|
119
|
+
extractMetaTokenUsageFromResponse: () => extractTokenUsageFromResponse5,
|
|
115
120
|
extractMistralTokenUsage: () => extractMistralTokenUsage,
|
|
116
|
-
extractMistralTokenUsageFromResponse: () =>
|
|
121
|
+
extractMistralTokenUsageFromResponse: () => extractTokenUsageFromResponse8,
|
|
117
122
|
extractOpenAITokenUsage: () => extractOpenAITokenUsage,
|
|
118
123
|
extractOpenAITokenUsageFromResponse: () => extractTokenUsageFromResponse,
|
|
119
124
|
extractTokenUsage: () => extractTokenUsage,
|
|
@@ -153,6 +158,7 @@ __export(index_exports, {
|
|
|
153
158
|
toFuguEffort: () => toFuguEffort,
|
|
154
159
|
toGeminiEffort: () => toGeminiEffort,
|
|
155
160
|
toGlmEffort: () => toGlmEffort,
|
|
161
|
+
toMetaEffort: () => toMetaEffort,
|
|
156
162
|
toOpenAIEffort: () => toOpenAIEffort,
|
|
157
163
|
transcribeWithGemini: () => transcribeWithGemini,
|
|
158
164
|
transcribeWithOpenAi: () => transcribeWithOpenAi,
|
|
@@ -920,7 +926,8 @@ var API_KEY_CONSTANTS = {
|
|
|
920
926
|
Z_AI: "Z_AI_API_KEY",
|
|
921
927
|
FUGU: "FUGU_API_KEY",
|
|
922
928
|
QWEN: "QWEN_API_KEY",
|
|
923
|
-
MINIMAX: "MINIMAX_API_KEY"
|
|
929
|
+
MINIMAX: "MINIMAX_API_KEY",
|
|
930
|
+
META: "META_API_KEY"
|
|
924
931
|
};
|
|
925
932
|
var SupportedAiKeyNames = {
|
|
926
933
|
[API_KEY_CONSTANTS.OPENAI]: "OpenAI",
|
|
@@ -933,7 +940,8 @@ var SupportedAiKeyNames = {
|
|
|
933
940
|
[API_KEY_CONSTANTS.Z_AI]: "Z.AI",
|
|
934
941
|
[API_KEY_CONSTANTS.FUGU]: "Sakana Fugu",
|
|
935
942
|
[API_KEY_CONSTANTS.QWEN]: "Qwen",
|
|
936
|
-
[API_KEY_CONSTANTS.MINIMAX]: "MiniMax"
|
|
943
|
+
[API_KEY_CONSTANTS.MINIMAX]: "MiniMax",
|
|
944
|
+
[API_KEY_CONSTANTS.META]: "Meta"
|
|
937
945
|
};
|
|
938
946
|
var LLM_CONSTANTS = {
|
|
939
947
|
// Thinking-only catalog since 2026-08-05: models whose API offers a thinking toggle used to
|
|
@@ -970,7 +978,9 @@ var LLM_CONSTANTS = {
|
|
|
970
978
|
QWEN_MAX: "qwen-max",
|
|
971
979
|
QWEN_FLASH: "qwen-flash",
|
|
972
980
|
// MiniMax. Single M3 entry; stable id without the version for the same repoint reason.
|
|
973
|
-
MINIMAX: "minimax"
|
|
981
|
+
MINIMAX: "minimax",
|
|
982
|
+
// Meta Model API (api.meta.ai). Muse Spark; version-free id so a 1.3 → 1.4 repoint is entry-only.
|
|
983
|
+
MUSE_SPARK: "muse-spark"
|
|
974
984
|
};
|
|
975
985
|
var DEFAULT_MAX_OUTPUT_TOKENS = 8192;
|
|
976
986
|
var SupportedAiModels = {
|
|
@@ -1255,6 +1265,22 @@ var SupportedAiModels = {
|
|
|
1255
1265
|
hasThinking: true,
|
|
1256
1266
|
temperature: 1,
|
|
1257
1267
|
tags: ["very-slow", "cheap"]
|
|
1268
|
+
},
|
|
1269
|
+
// Meta Muse Spark 1.3 (added 2026-09-12) on Meta's own Model API — Standard tier, i.e.
|
|
1270
|
+
// the private model id (the `-contributor` id is a quarter of the price but Meta trains
|
|
1271
|
+
// on the prompts). Always-on reasoning with an effort dial (minimal … max, "none" is
|
|
1272
|
+
// rejected); the chain of thought is never returned, only an optional summary, plus
|
|
1273
|
+
// encrypted reasoning items replayed across turns like Grok. 'medium' is pinned as the
|
|
1274
|
+
// game default: turns are short and every reasoning token bills as output.
|
|
1275
|
+
// Temperature: Meta documents the model as tuned to its 1.0 default.
|
|
1276
|
+
// Speed/tags: unmeasured until the first live run — no tag rather than a guess.
|
|
1277
|
+
[LLM_CONSTANTS.MUSE_SPARK]: {
|
|
1278
|
+
displayName: "Muse Spark 1.3",
|
|
1279
|
+
modelApiName: "muse-spark-1.3",
|
|
1280
|
+
apiKeyName: API_KEY_CONSTANTS.META,
|
|
1281
|
+
hasThinking: true,
|
|
1282
|
+
temperature: 1,
|
|
1283
|
+
reasoningEffort: "medium"
|
|
1258
1284
|
}
|
|
1259
1285
|
};
|
|
1260
1286
|
function createCatalog(overrides = {}) {
|
|
@@ -1522,6 +1548,14 @@ var MODEL_PRICING = {
|
|
|
1522
1548
|
extendedContextOutputPrice: 2.4,
|
|
1523
1549
|
extendedContextCacheHitPrice: 0.12,
|
|
1524
1550
|
extendedContextThresholdTokens: 512e3
|
|
1551
|
+
},
|
|
1552
|
+
// Meta Muse Spark 1.3, Standard tier. Rates from ai.developer.meta.com/docs/pricing-rate-limits
|
|
1553
|
+
// (2026-09-12): no long-context premium at any point of the 1M window; reasoning tokens bill
|
|
1554
|
+
// as output; caching is automatic, hits reported in input_tokens_details.cached_tokens.
|
|
1555
|
+
[SupportedAiModels[LLM_CONSTANTS.MUSE_SPARK].modelApiName]: {
|
|
1556
|
+
inputPrice: 1.25,
|
|
1557
|
+
outputPrice: 4.25,
|
|
1558
|
+
cacheHitPrice: 0.15
|
|
1525
1559
|
}
|
|
1526
1560
|
};
|
|
1527
1561
|
var HYBRID_THINKING_API_NAMES = /* @__PURE__ */ new Set([
|
|
@@ -1585,6 +1619,9 @@ function getProviderSignatureFields(aiType, signature) {
|
|
|
1585
1619
|
if (aiType.startsWith("grok")) {
|
|
1586
1620
|
return { grokEncryptedReasoning: signature };
|
|
1587
1621
|
}
|
|
1622
|
+
if (aiType.startsWith("muse-")) {
|
|
1623
|
+
return { metaEncryptedReasoning: signature };
|
|
1624
|
+
}
|
|
1588
1625
|
return {};
|
|
1589
1626
|
}
|
|
1590
1627
|
|
|
@@ -1596,6 +1633,7 @@ var GEMINI_REASONING_EFFORTS = ["minimal", "low", "medium", "high"];
|
|
|
1596
1633
|
var GLM_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1597
1634
|
var DEEPSEEK_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1598
1635
|
var FUGU_REASONING_EFFORTS = ["high", "xhigh"];
|
|
1636
|
+
var META_REASONING_EFFORTS = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
|
1599
1637
|
function clampReasoningEffort(effort, allowed) {
|
|
1600
1638
|
const rank = REASONING_EFFORT_SCALE.indexOf(effort);
|
|
1601
1639
|
let best = allowed[0];
|
|
@@ -1614,6 +1652,7 @@ var toAnthropicEffort = (effort) => clampReasoningEffort(effort, ANTHROPIC_REASO
|
|
|
1614
1652
|
var toGeminiEffort = (effort) => clampReasoningEffort(effort, GEMINI_REASONING_EFFORTS);
|
|
1615
1653
|
var toGlmEffort = (effort) => clampReasoningEffort(effort, GLM_REASONING_EFFORTS);
|
|
1616
1654
|
var toDeepSeekEffort = (effort) => clampReasoningEffort(effort, DEEPSEEK_REASONING_EFFORTS);
|
|
1655
|
+
var toMetaEffort = (effort) => clampReasoningEffort(effort, META_REASONING_EFFORTS);
|
|
1617
1656
|
var toFuguEffort = (effort) => clampReasoningEffort(effort, FUGU_REASONING_EFFORTS);
|
|
1618
1657
|
|
|
1619
1658
|
// src/pricing/token-usage-utils.ts
|
|
@@ -1668,6 +1707,9 @@ function extractKimiTokenUsage(response) {
|
|
|
1668
1707
|
function extractGrokTokenUsage(response) {
|
|
1669
1708
|
return extractTokenUsage(response);
|
|
1670
1709
|
}
|
|
1710
|
+
function extractMetaTokenUsage(response) {
|
|
1711
|
+
return extractTokenUsage(response);
|
|
1712
|
+
}
|
|
1671
1713
|
function extractAnthropicTokenUsage(response) {
|
|
1672
1714
|
if (!response?.usage) {
|
|
1673
1715
|
return null;
|
|
@@ -1756,11 +1798,19 @@ function extractTokenUsageFromResponse4(response) {
|
|
|
1756
1798
|
return extractGrokTokenUsage(response);
|
|
1757
1799
|
}
|
|
1758
1800
|
|
|
1801
|
+
// src/pricing/meta-pricing.ts
|
|
1802
|
+
function calculateMetaCost(model, inputTokens, outputTokens, cacheHitTokens = 0) {
|
|
1803
|
+
return calculateCost(model, inputTokens, outputTokens, { cacheHitTokens });
|
|
1804
|
+
}
|
|
1805
|
+
function extractTokenUsageFromResponse5(response) {
|
|
1806
|
+
return extractMetaTokenUsage(response);
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1759
1809
|
// src/pricing/anthropic-pricing.ts
|
|
1760
1810
|
function calculateAnthropicCost(model, inputTokens, outputTokens, cacheHitTokens = 0) {
|
|
1761
1811
|
return calculateCost(model, inputTokens, outputTokens, { cacheHitTokens });
|
|
1762
1812
|
}
|
|
1763
|
-
function
|
|
1813
|
+
function extractTokenUsageFromResponse6(response) {
|
|
1764
1814
|
return extractAnthropicTokenUsage(response);
|
|
1765
1815
|
}
|
|
1766
1816
|
|
|
@@ -1768,7 +1818,7 @@ function extractTokenUsageFromResponse5(response) {
|
|
|
1768
1818
|
function calculateGoogleCost(model, inputTokens, outputTokens, options = {}) {
|
|
1769
1819
|
return calculateCost(model, inputTokens, outputTokens, options);
|
|
1770
1820
|
}
|
|
1771
|
-
function
|
|
1821
|
+
function extractTokenUsageFromResponse7(response) {
|
|
1772
1822
|
return extractGoogleTokenUsage(response);
|
|
1773
1823
|
}
|
|
1774
1824
|
|
|
@@ -1776,7 +1826,7 @@ function extractTokenUsageFromResponse6(response) {
|
|
|
1776
1826
|
function calculateMistralCost(model, inputTokens, outputTokens, cacheHitTokens = 0) {
|
|
1777
1827
|
return calculateCost(model, inputTokens, outputTokens, { cacheHitTokens });
|
|
1778
1828
|
}
|
|
1779
|
-
function
|
|
1829
|
+
function extractTokenUsageFromResponse8(response) {
|
|
1780
1830
|
return extractMistralTokenUsage(response);
|
|
1781
1831
|
}
|
|
1782
1832
|
|
|
@@ -4846,6 +4896,212 @@ ${openAIMessages[0].content}`;
|
|
|
4846
4896
|
}
|
|
4847
4897
|
};
|
|
4848
4898
|
|
|
4899
|
+
// src/agents/meta-agent.ts
|
|
4900
|
+
var import_openai11 = require("openai");
|
|
4901
|
+
var MetaAgent = class extends AbstractAgent {
|
|
4902
|
+
client;
|
|
4903
|
+
promptCacheKey;
|
|
4904
|
+
// Log message templates
|
|
4905
|
+
logTemplates = {
|
|
4906
|
+
error: (name, error) => `Error in ${name} agent: ${error}`
|
|
4907
|
+
};
|
|
4908
|
+
// Error message templates
|
|
4909
|
+
errorMessages = {
|
|
4910
|
+
emptyResponse: "Empty or undefined response from Meta API",
|
|
4911
|
+
invalidFormat: "Invalid response format from Meta API",
|
|
4912
|
+
apiError: (error) => `Failed to get response from Meta API: ${error instanceof Error ? error.message : String(error)}`
|
|
4913
|
+
};
|
|
4914
|
+
constructor(name, instruction, model, apiKey, temperature, enableThinking = false, agentLoggingConfig = DEFAULT_LOGGING_CONFIG.agents) {
|
|
4915
|
+
super(name, instruction, model, temperature, enableThinking, agentLoggingConfig);
|
|
4916
|
+
this.promptCacheKey = stableHashHex(`${name}
|
|
4917
|
+
${instruction}`);
|
|
4918
|
+
this.client = new import_openai11.OpenAI({
|
|
4919
|
+
apiKey,
|
|
4920
|
+
baseURL: "https://api.meta.ai/v1",
|
|
4921
|
+
timeout: 12e5
|
|
4922
|
+
});
|
|
4923
|
+
}
|
|
4924
|
+
/**
|
|
4925
|
+
* Structured output: json_schema format on the Responses API plus the schema described
|
|
4926
|
+
* in the prompt, parsed leniently and validated with Zod.
|
|
4927
|
+
*/
|
|
4928
|
+
async doAskWithZodSchema(zodSchema, messages) {
|
|
4929
|
+
try {
|
|
4930
|
+
const schemaDescription = ZodSchemaConverter.toPromptDescription(zodSchema);
|
|
4931
|
+
const input = this.buildResponsesInput(this.prepareMessages(messages));
|
|
4932
|
+
const lastMessage = input[input.length - 1];
|
|
4933
|
+
if (lastMessage && typeof lastMessage.content === "string") {
|
|
4934
|
+
lastMessage.content += `
|
|
4935
|
+
|
|
4936
|
+
Your response must be a valid JSON object matching this schema:
|
|
4937
|
+
${schemaDescription}`;
|
|
4938
|
+
}
|
|
4939
|
+
this.logAsking(messages);
|
|
4940
|
+
this.logMessages(messages);
|
|
4941
|
+
const response = await this.createResponse(input, zodSchema);
|
|
4942
|
+
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4943
|
+
if (!text) {
|
|
4944
|
+
throw new Error(this.errorMessages.emptyResponse);
|
|
4945
|
+
}
|
|
4946
|
+
this.logger(`Meta Agent - Found reasoning summary: ${!!reasoningSummary}, encrypted reasoning: ${!!encryptedReasoning}`);
|
|
4947
|
+
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m));
|
|
4948
|
+
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4949
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
4950
|
+
if (parsedData) {
|
|
4951
|
+
this.logReply(parsedData, reasoningSummary, tokenUsage);
|
|
4952
|
+
}
|
|
4953
|
+
return [parsedData, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4954
|
+
} catch (error) {
|
|
4955
|
+
this.logger(this.logTemplates.error(this.name, error));
|
|
4956
|
+
throw new Error(this.errorMessages.apiError(error));
|
|
4957
|
+
}
|
|
4958
|
+
}
|
|
4959
|
+
/**
|
|
4960
|
+
* Plain-text ask: no JSON mode and no schema appended to the prompt.
|
|
4961
|
+
* Reasoning extraction and token accounting are identical to askWithZodSchema.
|
|
4962
|
+
*/
|
|
4963
|
+
async doAskText(messages) {
|
|
4964
|
+
try {
|
|
4965
|
+
const input = this.buildResponsesInput(this.prepareMessages(messages));
|
|
4966
|
+
this.logAsking(messages);
|
|
4967
|
+
this.logMessages(messages);
|
|
4968
|
+
const response = await this.createResponse(input);
|
|
4969
|
+
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4970
|
+
if (!text) {
|
|
4971
|
+
throw new Error(this.errorMessages.emptyResponse);
|
|
4972
|
+
}
|
|
4973
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
4974
|
+
this.logReply(text, reasoningSummary, tokenUsage);
|
|
4975
|
+
return [text, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4976
|
+
} catch (error) {
|
|
4977
|
+
this.logger(this.logTemplates.error(this.name, error));
|
|
4978
|
+
throw new Error(this.errorMessages.apiError(error));
|
|
4979
|
+
}
|
|
4980
|
+
}
|
|
4981
|
+
createResponse(input, zodSchema) {
|
|
4982
|
+
return this.client.responses.create({
|
|
4983
|
+
model: this.model,
|
|
4984
|
+
temperature: this.temperature,
|
|
4985
|
+
input,
|
|
4986
|
+
// Reasoning bills against the output budget on top of the visible answer, so this
|
|
4987
|
+
// has to cover both. Raise it with a catalog `maxOutputTokens` override if Muse
|
|
4988
|
+
// ever starts truncating.
|
|
4989
|
+
max_output_tokens: this.maxOutputTokens,
|
|
4990
|
+
// Effort read at request time so a per-instance override (story generation runs
|
|
4991
|
+
// deeper) is honored; omitted entirely when nothing is pinned, leaving Meta's default.
|
|
4992
|
+
reasoning: {
|
|
4993
|
+
...this.reasoningEffort ? { effort: toMetaEffort(this.reasoningEffort) } : {},
|
|
4994
|
+
summary: "auto"
|
|
4995
|
+
},
|
|
4996
|
+
// We manage conversation state ourselves; encrypted reasoning is only
|
|
4997
|
+
// returned for unstored responses.
|
|
4998
|
+
store: false,
|
|
4999
|
+
include: ["reasoning.encrypted_content"],
|
|
5000
|
+
prompt_cache_key: this.promptCacheKey,
|
|
5001
|
+
...zodSchema ? {
|
|
5002
|
+
text: {
|
|
5003
|
+
format: {
|
|
5004
|
+
type: "json_schema",
|
|
5005
|
+
name: "response_schema",
|
|
5006
|
+
schema: ZodSchemaConverter.toJsonSchema(zodSchema),
|
|
5007
|
+
strict: false
|
|
5008
|
+
}
|
|
5009
|
+
}
|
|
5010
|
+
} : {}
|
|
5011
|
+
});
|
|
5012
|
+
}
|
|
5013
|
+
/**
|
|
5014
|
+
* Converts history to Responses API input items. The system instruction is merged into
|
|
5015
|
+
* the leading system message; assistant messages carrying stored encrypted reasoning get
|
|
5016
|
+
* their reasoning items replayed right before them.
|
|
5017
|
+
*/
|
|
5018
|
+
buildResponsesInput(messages) {
|
|
5019
|
+
const input = [];
|
|
5020
|
+
for (const msg of messages) {
|
|
5021
|
+
if (msg.role === "assistant" && msg.metaEncryptedReasoning) {
|
|
5022
|
+
try {
|
|
5023
|
+
const reasoningItems = JSON.parse(msg.metaEncryptedReasoning);
|
|
5024
|
+
if (Array.isArray(reasoningItems)) {
|
|
5025
|
+
input.push(...reasoningItems);
|
|
5026
|
+
}
|
|
5027
|
+
} catch {
|
|
5028
|
+
this.logger(`Failed to parse stored encrypted reasoning, replaying message without it`);
|
|
5029
|
+
}
|
|
5030
|
+
}
|
|
5031
|
+
input.push({ role: msg.role, content: msg.content });
|
|
5032
|
+
}
|
|
5033
|
+
if (input.length > 0 && input[0].role !== "system") {
|
|
5034
|
+
input.unshift({ role: "system", content: this.instruction });
|
|
5035
|
+
} else if (input.length > 0 && input[0].role === "system") {
|
|
5036
|
+
input[0].content = `${this.instruction}
|
|
5037
|
+
|
|
5038
|
+
${input[0].content}`;
|
|
5039
|
+
}
|
|
5040
|
+
return input;
|
|
5041
|
+
}
|
|
5042
|
+
/**
|
|
5043
|
+
* Walks the response output items: reasoning items yield the human-readable summary
|
|
5044
|
+
* plus the encrypted items (serialized for storage/replay); message items yield text.
|
|
5045
|
+
*/
|
|
5046
|
+
extractResponseParts(response) {
|
|
5047
|
+
const textParts = [];
|
|
5048
|
+
const summaryParts = [];
|
|
5049
|
+
const encryptedItems = [];
|
|
5050
|
+
for (const item of response?.output ?? []) {
|
|
5051
|
+
if (!item) {
|
|
5052
|
+
continue;
|
|
5053
|
+
}
|
|
5054
|
+
if (item.type === "reasoning") {
|
|
5055
|
+
for (const summary of item.summary ?? []) {
|
|
5056
|
+
if (typeof summary?.text === "string" && summary.text) {
|
|
5057
|
+
summaryParts.push(summary.text);
|
|
5058
|
+
}
|
|
5059
|
+
}
|
|
5060
|
+
if (item.encrypted_content) {
|
|
5061
|
+
encryptedItems.push(item);
|
|
5062
|
+
}
|
|
5063
|
+
} else if (item.type === "message") {
|
|
5064
|
+
for (const part of item.content ?? []) {
|
|
5065
|
+
if (part?.type === "output_text" && typeof part.text === "string") {
|
|
5066
|
+
textParts.push(part.text);
|
|
5067
|
+
}
|
|
5068
|
+
}
|
|
5069
|
+
}
|
|
5070
|
+
}
|
|
5071
|
+
return {
|
|
5072
|
+
text: textParts.join("\n").trim(),
|
|
5073
|
+
reasoningSummary: summaryParts.join("\n").trim(),
|
|
5074
|
+
encryptedReasoning: encryptedItems.length > 0 ? JSON.stringify(encryptedItems) : void 0
|
|
5075
|
+
};
|
|
5076
|
+
}
|
|
5077
|
+
extractTokenUsage(response) {
|
|
5078
|
+
const usage = response?.usage;
|
|
5079
|
+
if (!usage) {
|
|
5080
|
+
return void 0;
|
|
5081
|
+
}
|
|
5082
|
+
const inputTokens = usage.input_tokens || 0;
|
|
5083
|
+
const outputTokens = usage.output_tokens || 0;
|
|
5084
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens || 0;
|
|
5085
|
+
const cachedTokens = usage.input_tokens_details?.cached_tokens || 0;
|
|
5086
|
+
const cost = calculateMetaCost(this.model, inputTokens, outputTokens, cachedTokens);
|
|
5087
|
+
if (reasoningTokens > 0) {
|
|
5088
|
+
this.logger(`Output breakdown: ${reasoningTokens} reasoning tokens, ${outputTokens - reasoningTokens} final answer tokens, ${outputTokens} total output tokens`);
|
|
5089
|
+
}
|
|
5090
|
+
if (cachedTokens > 0) {
|
|
5091
|
+
this.logger(`Input breakdown: ${cachedTokens} cached tokens of ${inputTokens} input tokens`);
|
|
5092
|
+
}
|
|
5093
|
+
return {
|
|
5094
|
+
inputTokens,
|
|
5095
|
+
outputTokens,
|
|
5096
|
+
totalTokens: inputTokens + outputTokens,
|
|
5097
|
+
costUSD: cost,
|
|
5098
|
+
// Omitted when zero so we never hand Firestore an undefined value.
|
|
5099
|
+
...reasoningTokens > 0 ? { reasoningTokens } : {},
|
|
5100
|
+
...cachedTokens > 0 ? { cachedInputTokens: cachedTokens } : {}
|
|
5101
|
+
};
|
|
5102
|
+
}
|
|
5103
|
+
};
|
|
5104
|
+
|
|
4849
5105
|
// src/agents/agent-factory.ts
|
|
4850
5106
|
var AgentFactory = class {
|
|
4851
5107
|
static createAgent(name, instruction, llmType, apiKeys, enableThinking = false) {
|
|
@@ -4899,6 +5155,9 @@ var AgentFactory = class {
|
|
|
4899
5155
|
// MiniMax M3 — adaptive thinking (the model decides per-request)
|
|
4900
5156
|
case LLM_CONSTANTS.MINIMAX:
|
|
4901
5157
|
return new MiniMaxAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
5158
|
+
// Meta Muse Spark — Responses API with encrypted reasoning replay, effort from the catalog
|
|
5159
|
+
case LLM_CONSTANTS.MUSE_SPARK:
|
|
5160
|
+
return new MetaAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
4902
5161
|
default:
|
|
4903
5162
|
throw new Error(`Unknown Key: ${modelName}`);
|
|
4904
5163
|
}
|
|
@@ -4941,7 +5200,9 @@ var AgentFactory = class {
|
|
|
4941
5200
|
KimiAgent,
|
|
4942
5201
|
LLM_CONSTANTS,
|
|
4943
5202
|
MESSAGE_ROLE,
|
|
5203
|
+
META_REASONING_EFFORTS,
|
|
4944
5204
|
MODEL_PRICING,
|
|
5205
|
+
MetaAgent,
|
|
4945
5206
|
MiniMaxAgent,
|
|
4946
5207
|
MistralAgent,
|
|
4947
5208
|
ModelAuthenticationError,
|
|
@@ -4974,6 +5235,7 @@ var AgentFactory = class {
|
|
|
4974
5235
|
calculateGoogleCost,
|
|
4975
5236
|
calculateGrokCost,
|
|
4976
5237
|
calculateKimiCost,
|
|
5238
|
+
calculateMetaCost,
|
|
4977
5239
|
calculateMistralCost,
|
|
4978
5240
|
calculateModelCost,
|
|
4979
5241
|
calculateOpenAICost,
|
|
@@ -4995,6 +5257,8 @@ var AgentFactory = class {
|
|
|
4995
5257
|
extractGrokTokenUsageFromResponse,
|
|
4996
5258
|
extractKimiTokenUsage,
|
|
4997
5259
|
extractKimiTokenUsageFromResponse,
|
|
5260
|
+
extractMetaTokenUsage,
|
|
5261
|
+
extractMetaTokenUsageFromResponse,
|
|
4998
5262
|
extractMistralTokenUsage,
|
|
4999
5263
|
extractMistralTokenUsageFromResponse,
|
|
5000
5264
|
extractOpenAITokenUsage,
|
|
@@ -5036,6 +5300,7 @@ var AgentFactory = class {
|
|
|
5036
5300
|
toFuguEffort,
|
|
5037
5301
|
toGeminiEffort,
|
|
5038
5302
|
toGlmEffort,
|
|
5303
|
+
toMetaEffort,
|
|
5039
5304
|
toOpenAIEffort,
|
|
5040
5305
|
transcribeWithGemini,
|
|
5041
5306
|
transcribeWithOpenAi,
|