@hiper2d/ai-agents 0.3.0 → 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 +10 -1
- package/dist/index.d.mts +101 -5
- package/dist/index.d.ts +101 -5
- package/dist/index.js +282 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +275 -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,
|
|
@@ -143,6 +148,7 @@ __export(index_exports, {
|
|
|
143
148
|
periodEnd: () => periodEnd,
|
|
144
149
|
periodKey: () => periodKey,
|
|
145
150
|
safeValidateResponse: () => safeValidateResponse,
|
|
151
|
+
setBeforeAskHook: () => setBeforeAskHook,
|
|
146
152
|
setLlmLogger: () => setLlmLogger,
|
|
147
153
|
stableHashHex: () => stableHashHex,
|
|
148
154
|
stripInlineThinking: () => stripInlineThinking,
|
|
@@ -152,6 +158,7 @@ __export(index_exports, {
|
|
|
152
158
|
toFuguEffort: () => toFuguEffort,
|
|
153
159
|
toGeminiEffort: () => toGeminiEffort,
|
|
154
160
|
toGlmEffort: () => toGlmEffort,
|
|
161
|
+
toMetaEffort: () => toMetaEffort,
|
|
155
162
|
toOpenAIEffort: () => toOpenAIEffort,
|
|
156
163
|
transcribeWithGemini: () => transcribeWithGemini,
|
|
157
164
|
transcribeWithOpenAi: () => transcribeWithOpenAi,
|
|
@@ -919,7 +926,8 @@ var API_KEY_CONSTANTS = {
|
|
|
919
926
|
Z_AI: "Z_AI_API_KEY",
|
|
920
927
|
FUGU: "FUGU_API_KEY",
|
|
921
928
|
QWEN: "QWEN_API_KEY",
|
|
922
|
-
MINIMAX: "MINIMAX_API_KEY"
|
|
929
|
+
MINIMAX: "MINIMAX_API_KEY",
|
|
930
|
+
META: "META_API_KEY"
|
|
923
931
|
};
|
|
924
932
|
var SupportedAiKeyNames = {
|
|
925
933
|
[API_KEY_CONSTANTS.OPENAI]: "OpenAI",
|
|
@@ -932,7 +940,8 @@ var SupportedAiKeyNames = {
|
|
|
932
940
|
[API_KEY_CONSTANTS.Z_AI]: "Z.AI",
|
|
933
941
|
[API_KEY_CONSTANTS.FUGU]: "Sakana Fugu",
|
|
934
942
|
[API_KEY_CONSTANTS.QWEN]: "Qwen",
|
|
935
|
-
[API_KEY_CONSTANTS.MINIMAX]: "MiniMax"
|
|
943
|
+
[API_KEY_CONSTANTS.MINIMAX]: "MiniMax",
|
|
944
|
+
[API_KEY_CONSTANTS.META]: "Meta"
|
|
936
945
|
};
|
|
937
946
|
var LLM_CONSTANTS = {
|
|
938
947
|
// Thinking-only catalog since 2026-08-05: models whose API offers a thinking toggle used to
|
|
@@ -969,7 +978,9 @@ var LLM_CONSTANTS = {
|
|
|
969
978
|
QWEN_MAX: "qwen-max",
|
|
970
979
|
QWEN_FLASH: "qwen-flash",
|
|
971
980
|
// MiniMax. Single M3 entry; stable id without the version for the same repoint reason.
|
|
972
|
-
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"
|
|
973
984
|
};
|
|
974
985
|
var DEFAULT_MAX_OUTPUT_TOKENS = 8192;
|
|
975
986
|
var SupportedAiModels = {
|
|
@@ -1254,6 +1265,22 @@ var SupportedAiModels = {
|
|
|
1254
1265
|
hasThinking: true,
|
|
1255
1266
|
temperature: 1,
|
|
1256
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"
|
|
1257
1284
|
}
|
|
1258
1285
|
};
|
|
1259
1286
|
function createCatalog(overrides = {}) {
|
|
@@ -1521,6 +1548,14 @@ var MODEL_PRICING = {
|
|
|
1521
1548
|
extendedContextOutputPrice: 2.4,
|
|
1522
1549
|
extendedContextCacheHitPrice: 0.12,
|
|
1523
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
|
|
1524
1559
|
}
|
|
1525
1560
|
};
|
|
1526
1561
|
var HYBRID_THINKING_API_NAMES = /* @__PURE__ */ new Set([
|
|
@@ -1584,6 +1619,9 @@ function getProviderSignatureFields(aiType, signature) {
|
|
|
1584
1619
|
if (aiType.startsWith("grok")) {
|
|
1585
1620
|
return { grokEncryptedReasoning: signature };
|
|
1586
1621
|
}
|
|
1622
|
+
if (aiType.startsWith("muse-")) {
|
|
1623
|
+
return { metaEncryptedReasoning: signature };
|
|
1624
|
+
}
|
|
1587
1625
|
return {};
|
|
1588
1626
|
}
|
|
1589
1627
|
|
|
@@ -1595,6 +1633,7 @@ var GEMINI_REASONING_EFFORTS = ["minimal", "low", "medium", "high"];
|
|
|
1595
1633
|
var GLM_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1596
1634
|
var DEEPSEEK_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1597
1635
|
var FUGU_REASONING_EFFORTS = ["high", "xhigh"];
|
|
1636
|
+
var META_REASONING_EFFORTS = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
|
1598
1637
|
function clampReasoningEffort(effort, allowed) {
|
|
1599
1638
|
const rank = REASONING_EFFORT_SCALE.indexOf(effort);
|
|
1600
1639
|
let best = allowed[0];
|
|
@@ -1613,6 +1652,7 @@ var toAnthropicEffort = (effort) => clampReasoningEffort(effort, ANTHROPIC_REASO
|
|
|
1613
1652
|
var toGeminiEffort = (effort) => clampReasoningEffort(effort, GEMINI_REASONING_EFFORTS);
|
|
1614
1653
|
var toGlmEffort = (effort) => clampReasoningEffort(effort, GLM_REASONING_EFFORTS);
|
|
1615
1654
|
var toDeepSeekEffort = (effort) => clampReasoningEffort(effort, DEEPSEEK_REASONING_EFFORTS);
|
|
1655
|
+
var toMetaEffort = (effort) => clampReasoningEffort(effort, META_REASONING_EFFORTS);
|
|
1616
1656
|
var toFuguEffort = (effort) => clampReasoningEffort(effort, FUGU_REASONING_EFFORTS);
|
|
1617
1657
|
|
|
1618
1658
|
// src/pricing/token-usage-utils.ts
|
|
@@ -1667,6 +1707,9 @@ function extractKimiTokenUsage(response) {
|
|
|
1667
1707
|
function extractGrokTokenUsage(response) {
|
|
1668
1708
|
return extractTokenUsage(response);
|
|
1669
1709
|
}
|
|
1710
|
+
function extractMetaTokenUsage(response) {
|
|
1711
|
+
return extractTokenUsage(response);
|
|
1712
|
+
}
|
|
1670
1713
|
function extractAnthropicTokenUsage(response) {
|
|
1671
1714
|
if (!response?.usage) {
|
|
1672
1715
|
return null;
|
|
@@ -1755,11 +1798,19 @@ function extractTokenUsageFromResponse4(response) {
|
|
|
1755
1798
|
return extractGrokTokenUsage(response);
|
|
1756
1799
|
}
|
|
1757
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
|
+
|
|
1758
1809
|
// src/pricing/anthropic-pricing.ts
|
|
1759
1810
|
function calculateAnthropicCost(model, inputTokens, outputTokens, cacheHitTokens = 0) {
|
|
1760
1811
|
return calculateCost(model, inputTokens, outputTokens, { cacheHitTokens });
|
|
1761
1812
|
}
|
|
1762
|
-
function
|
|
1813
|
+
function extractTokenUsageFromResponse6(response) {
|
|
1763
1814
|
return extractAnthropicTokenUsage(response);
|
|
1764
1815
|
}
|
|
1765
1816
|
|
|
@@ -1767,7 +1818,7 @@ function extractTokenUsageFromResponse5(response) {
|
|
|
1767
1818
|
function calculateGoogleCost(model, inputTokens, outputTokens, options = {}) {
|
|
1768
1819
|
return calculateCost(model, inputTokens, outputTokens, options);
|
|
1769
1820
|
}
|
|
1770
|
-
function
|
|
1821
|
+
function extractTokenUsageFromResponse7(response) {
|
|
1771
1822
|
return extractGoogleTokenUsage(response);
|
|
1772
1823
|
}
|
|
1773
1824
|
|
|
@@ -1775,7 +1826,7 @@ function extractTokenUsageFromResponse6(response) {
|
|
|
1775
1826
|
function calculateMistralCost(model, inputTokens, outputTokens, cacheHitTokens = 0) {
|
|
1776
1827
|
return calculateCost(model, inputTokens, outputTokens, { cacheHitTokens });
|
|
1777
1828
|
}
|
|
1778
|
-
function
|
|
1829
|
+
function extractTokenUsageFromResponse8(response) {
|
|
1779
1830
|
return extractMistralTokenUsage(response);
|
|
1780
1831
|
}
|
|
1781
1832
|
|
|
@@ -2183,6 +2234,10 @@ var BudgetController = class {
|
|
|
2183
2234
|
};
|
|
2184
2235
|
|
|
2185
2236
|
// src/agents/abstract-agent.ts
|
|
2237
|
+
var beforeAskHook;
|
|
2238
|
+
function setBeforeAskHook(hook) {
|
|
2239
|
+
beforeAskHook = hook;
|
|
2240
|
+
}
|
|
2186
2241
|
var AbstractAgent = class {
|
|
2187
2242
|
name;
|
|
2188
2243
|
gameId;
|
|
@@ -2234,6 +2289,7 @@ var AbstractAgent = class {
|
|
|
2234
2289
|
* must NOT override these.
|
|
2235
2290
|
*/
|
|
2236
2291
|
async askWithZodSchema(zodSchema, messages) {
|
|
2292
|
+
if (beforeAskHook) await beforeAskHook(this);
|
|
2237
2293
|
const startedAt = Date.now();
|
|
2238
2294
|
try {
|
|
2239
2295
|
const [result, thinking, usage, signature] = await this.doAskWithZodSchema(zodSchema, messages);
|
|
@@ -2244,6 +2300,7 @@ var AbstractAgent = class {
|
|
|
2244
2300
|
}
|
|
2245
2301
|
}
|
|
2246
2302
|
async askText(messages) {
|
|
2303
|
+
if (beforeAskHook) await beforeAskHook(this);
|
|
2247
2304
|
const startedAt = Date.now();
|
|
2248
2305
|
try {
|
|
2249
2306
|
const [content, thinking, usage, signature] = await this.doAskText(messages);
|
|
@@ -4839,6 +4896,212 @@ ${openAIMessages[0].content}`;
|
|
|
4839
4896
|
}
|
|
4840
4897
|
};
|
|
4841
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
|
+
|
|
4842
5105
|
// src/agents/agent-factory.ts
|
|
4843
5106
|
var AgentFactory = class {
|
|
4844
5107
|
static createAgent(name, instruction, llmType, apiKeys, enableThinking = false) {
|
|
@@ -4892,6 +5155,9 @@ var AgentFactory = class {
|
|
|
4892
5155
|
// MiniMax M3 — adaptive thinking (the model decides per-request)
|
|
4893
5156
|
case LLM_CONSTANTS.MINIMAX:
|
|
4894
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);
|
|
4895
5161
|
default:
|
|
4896
5162
|
throw new Error(`Unknown Key: ${modelName}`);
|
|
4897
5163
|
}
|
|
@@ -4934,7 +5200,9 @@ var AgentFactory = class {
|
|
|
4934
5200
|
KimiAgent,
|
|
4935
5201
|
LLM_CONSTANTS,
|
|
4936
5202
|
MESSAGE_ROLE,
|
|
5203
|
+
META_REASONING_EFFORTS,
|
|
4937
5204
|
MODEL_PRICING,
|
|
5205
|
+
MetaAgent,
|
|
4938
5206
|
MiniMaxAgent,
|
|
4939
5207
|
MistralAgent,
|
|
4940
5208
|
ModelAuthenticationError,
|
|
@@ -4967,6 +5235,7 @@ var AgentFactory = class {
|
|
|
4967
5235
|
calculateGoogleCost,
|
|
4968
5236
|
calculateGrokCost,
|
|
4969
5237
|
calculateKimiCost,
|
|
5238
|
+
calculateMetaCost,
|
|
4970
5239
|
calculateMistralCost,
|
|
4971
5240
|
calculateModelCost,
|
|
4972
5241
|
calculateOpenAICost,
|
|
@@ -4988,6 +5257,8 @@ var AgentFactory = class {
|
|
|
4988
5257
|
extractGrokTokenUsageFromResponse,
|
|
4989
5258
|
extractKimiTokenUsage,
|
|
4990
5259
|
extractKimiTokenUsageFromResponse,
|
|
5260
|
+
extractMetaTokenUsage,
|
|
5261
|
+
extractMetaTokenUsageFromResponse,
|
|
4991
5262
|
extractMistralTokenUsage,
|
|
4992
5263
|
extractMistralTokenUsageFromResponse,
|
|
4993
5264
|
extractOpenAITokenUsage,
|
|
@@ -5019,6 +5290,7 @@ var AgentFactory = class {
|
|
|
5019
5290
|
periodEnd,
|
|
5020
5291
|
periodKey,
|
|
5021
5292
|
safeValidateResponse,
|
|
5293
|
+
setBeforeAskHook,
|
|
5022
5294
|
setLlmLogger,
|
|
5023
5295
|
stableHashHex,
|
|
5024
5296
|
stripInlineThinking,
|
|
@@ -5028,6 +5300,7 @@ var AgentFactory = class {
|
|
|
5028
5300
|
toFuguEffort,
|
|
5029
5301
|
toGeminiEffort,
|
|
5030
5302
|
toGlmEffort,
|
|
5303
|
+
toMetaEffort,
|
|
5031
5304
|
toOpenAIEffort,
|
|
5032
5305
|
transcribeWithGemini,
|
|
5033
5306
|
transcribeWithOpenAi,
|