@mastra/code-sdk 1.0.3-alpha.0 → 1.1.0-alpha.2
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/CHANGELOG.md +49 -0
- package/dist/acp/agent.js +155 -167
- package/dist/acp/agent.js.map +1 -1
- package/dist/acp/event-mapper.js +193 -168
- package/dist/acp/event-mapper.js.map +1 -1
- package/dist/acp/index.js +60 -49
- package/dist/acp/index.js.map +1 -1
- package/dist/acp/server.js +31 -30
- package/dist/acp/server.js.map +1 -1
- package/dist/acp.js +1 -4
- package/dist/agents/credential-resolver.js +40 -29
- package/dist/agents/credential-resolver.js.map +1 -1
- package/dist/agents/custom-provider-source.js +16 -9
- package/dist/agents/custom-provider-source.js.map +1 -1
- package/dist/agents/instructions.js +27 -36
- package/dist/agents/instructions.js.map +1 -1
- package/dist/agents/mastracode-gateway.js +355 -428
- package/dist/agents/mastracode-gateway.js.map +1 -1
- package/dist/agents/memory.js +87 -78
- package/dist/agents/memory.js.map +1 -1
- package/dist/agents/model.js +114 -119
- package/dist/agents/model.js.map +1 -1
- package/dist/agents/modes/build.js +12 -13
- package/dist/agents/modes/build.js.map +1 -1
- package/dist/agents/modes/explore.js +14 -13
- package/dist/agents/modes/explore.js.map +1 -1
- package/dist/agents/modes/plan.js +14 -15
- package/dist/agents/modes/plan.js.map +1 -1
- package/dist/agents/prompts/agent-instructions.js +78 -69
- package/dist/agents/prompts/agent-instructions.js.map +1 -1
- package/dist/agents/prompts/build.js +19 -16
- package/dist/agents/prompts/build.js.map +1 -1
- package/dist/agents/prompts/fast.js +8 -4
- package/dist/agents/prompts/fast.js.map +1 -1
- package/dist/agents/prompts/index.js +48 -47
- package/dist/agents/prompts/index.js.map +1 -1
- package/dist/agents/prompts/model.js +6 -5
- package/dist/agents/prompts/model.js.map +1 -1
- package/dist/agents/prompts/plan.js +20 -16
- package/dist/agents/prompts/plan.js.map +1 -1
- package/dist/agents/prompts/tool-guidance.js +77 -109
- package/dist/agents/prompts/tool-guidance.js.map +1 -1
- package/dist/agents/sandbox-filesystem.js +245 -259
- package/dist/agents/sandbox-filesystem.js.map +1 -1
- package/dist/agents/sandbox-reattach.js +9 -11
- package/dist/agents/sandbox-reattach.js.map +1 -1
- package/dist/agents/thread-caveman-state.js +92 -81
- package/dist/agents/thread-caveman-state.js.map +1 -1
- package/dist/agents/tool-availability.js +62 -80
- package/dist/agents/tool-availability.js.map +1 -1
- package/dist/agents/tools.js +99 -139
- package/dist/agents/tools.js.map +1 -1
- package/dist/agents/workspace.js +232 -225
- package/dist/agents/workspace.js.map +1 -1
- package/dist/analytics.js +139 -128
- package/dist/analytics.js.map +1 -1
- package/dist/auth/authorization-input.js +39 -25
- package/dist/auth/authorization-input.js.map +1 -1
- package/dist/auth/device-code.js +112 -80
- package/dist/auth/device-code.js.map +1 -1
- package/dist/auth/index.js +2 -9
- package/dist/auth/pkce.js +27 -16
- package/dist/auth/pkce.js.map +1 -1
- package/dist/auth/providers/anthropic.js +107 -93
- package/dist/auth/providers/anthropic.js.map +1 -1
- package/dist/auth/providers/github-copilot.js +304 -293
- package/dist/auth/providers/github-copilot.js.map +1 -1
- package/dist/auth/providers/openai-codex.js +442 -460
- package/dist/auth/providers/openai-codex.js.map +1 -1
- package/dist/auth/providers/xai.js +179 -145
- package/dist/auth/providers/xai.js.map +1 -1
- package/dist/auth/storage.js +182 -175
- package/dist/auth/storage.js.map +1 -1
- package/dist/auth/types.js +1 -1
- package/dist/clipboard/index.js +206 -172
- package/dist/clipboard/index.js.map +1 -1
- package/dist/constants.js +11 -16
- package/dist/constants.js.map +1 -1
- package/dist/error-classification.js +19 -11
- package/dist/error-classification.js.map +1 -1
- package/dist/evals/context-builder.js +145 -104
- package/dist/evals/context-builder.js.map +1 -1
- package/dist/evals/index.js +4 -7
- package/dist/evals/scorers/classify-command.js +62 -41
- package/dist/evals/scorers/classify-command.js.map +1 -1
- package/dist/evals/scorers/efficiency.js +239 -181
- package/dist/evals/scorers/efficiency.js.map +1 -1
- package/dist/evals/scorers/extract-tools.js +51 -47
- package/dist/evals/scorers/extract-tools.js.map +1 -1
- package/dist/evals/scorers/index.js +1 -5
- package/dist/evals/scorers/outcome.js +321 -212
- package/dist/evals/scorers/outcome.js.map +1 -1
- package/dist/goal-manager.js +266 -261
- package/dist/goal-manager.js.map +1 -1
- package/dist/goal-signal.js +14 -13
- package/dist/goal-signal.js.map +1 -1
- package/dist/headless/cli.js +153 -154
- package/dist/headless/cli.js.map +1 -1
- package/dist/headless/flags.js +180 -178
- package/dist/headless/flags.js.map +1 -1
- package/dist/headless/format.js +72 -71
- package/dist/headless/format.js.map +1 -1
- package/dist/headless/index.js +5 -31
- package/dist/headless/policy.js +35 -31
- package/dist/headless/policy.js.map +1 -1
- package/dist/headless/run-mc.js +332 -334
- package/dist/headless/run-mc.js.map +1 -1
- package/dist/headless/types.js +16 -7
- package/dist/headless/types.js.map +1 -1
- package/dist/hooks/config.js +56 -61
- package/dist/hooks/config.js.map +1 -1
- package/dist/hooks/executor.js +118 -116
- package/dist/hooks/executor.js.map +1 -1
- package/dist/hooks/index.js +4 -15
- package/dist/hooks/manager.js +221 -280
- package/dist/hooks/manager.js.map +1 -1
- package/dist/hooks/types.js +5 -4
- package/dist/hooks/types.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +632 -712
- package/dist/index.js.map +1 -1
- package/dist/ipc/ipc-reporter.js +19 -22
- package/dist/ipc/ipc-reporter.js.map +1 -1
- package/dist/lsp/client.js +248 -306
- package/dist/lsp/client.js.map +1 -1
- package/dist/lsp/index.js +1 -6
- package/dist/lsp/language.js +39 -43
- package/dist/lsp/language.js.map +1 -1
- package/dist/lsp/manager.js +78 -93
- package/dist/lsp/manager.js.map +1 -1
- package/dist/lsp/server.js +151 -120
- package/dist/lsp/server.js.map +1 -1
- package/dist/lsp/workspace.js +50 -47
- package/dist/lsp/workspace.js.map +1 -1
- package/dist/mcp/config.js +210 -199
- package/dist/mcp/config.js.map +1 -1
- package/dist/mcp/index.js +2 -9
- package/dist/mcp/manager.js +482 -475
- package/dist/mcp/manager.js.map +1 -1
- package/dist/mcp/types.js +1 -1
- package/dist/onboarding/custom-packs.js +14 -17
- package/dist/onboarding/custom-packs.js.map +1 -1
- package/dist/onboarding/custom-providers.js +22 -20
- package/dist/onboarding/custom-providers.js.map +1 -1
- package/dist/onboarding/index.d.ts +1 -1
- package/dist/onboarding/index.d.ts.map +1 -1
- package/dist/onboarding/index.js +3 -21
- package/dist/onboarding/om-settings.d.ts +5 -0
- package/dist/onboarding/om-settings.d.ts.map +1 -1
- package/dist/onboarding/om-settings.js +41 -20
- package/dist/onboarding/om-settings.js.map +1 -1
- package/dist/onboarding/packs.d.ts +4 -6
- package/dist/onboarding/packs.d.ts.map +1 -1
- package/dist/onboarding/packs.js +154 -105
- package/dist/onboarding/packs.js.map +1 -1
- package/dist/onboarding/settings.js +575 -517
- package/dist/onboarding/settings.js.map +1 -1
- package/dist/permissions.js +112 -99
- package/dist/permissions.js.map +1 -1
- package/dist/plugin.js +21 -23
- package/dist/plugin.js.map +1 -1
- package/dist/plugins/dependencies.js +64 -87
- package/dist/plugins/dependencies.js.map +1 -1
- package/dist/plugins/install.js +159 -169
- package/dist/plugins/install.js.map +1 -1
- package/dist/plugins/loader.js +170 -185
- package/dist/plugins/loader.js.map +1 -1
- package/dist/plugins/manager.js +374 -380
- package/dist/plugins/manager.js.map +1 -1
- package/dist/plugins/manifest.js +38 -63
- package/dist/plugins/manifest.js.map +1 -1
- package/dist/plugins/package-link.js +48 -64
- package/dist/plugins/package-link.js.map +1 -1
- package/dist/plugins/paths.js +18 -19
- package/dist/plugins/paths.js.map +1 -1
- package/dist/plugins/registry.js +97 -81
- package/dist/plugins/registry.js.map +1 -1
- package/dist/plugins/scaffold.js +62 -98
- package/dist/plugins/scaffold.js.map +1 -1
- package/dist/plugins/types.js +1 -1
- package/dist/processors/plan-rejection-abort.js +45 -44
- package/dist/processors/plan-rejection-abort.js.map +1 -1
- package/dist/providers/amazon-bedrock-gateway.js +134 -125
- package/dist/providers/amazon-bedrock-gateway.js.map +1 -1
- package/dist/providers/amazon-bedrock.js +68 -47
- package/dist/providers/amazon-bedrock.js.map +1 -1
- package/dist/providers/claude-max.js +153 -133
- package/dist/providers/claude-max.js.map +1 -1
- package/dist/providers/github-copilot.js +232 -230
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/openai-codex.js +271 -260
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/providers/xai.js +57 -50
- package/dist/providers/xai.js.map +1 -1
- package/dist/schema.js +91 -89
- package/dist/schema.js.map +1 -1
- package/dist/theme-palette.js +18 -14
- package/dist/theme-palette.js.map +1 -1
- package/dist/tool-names.js +46 -37
- package/dist/tool-names.js.map +1 -1
- package/dist/tools/index.js +2 -8
- package/dist/tools/request-sandbox-access.js +71 -80
- package/dist/tools/request-sandbox-access.js.map +1 -1
- package/dist/tools/utils.js +26 -16
- package/dist/tools/utils.js.map +1 -1
- package/dist/tools/web-search.js +51 -55
- package/dist/tools/web-search.js.map +1 -1
- package/dist/utils/binaries.js +65 -61
- package/dist/utils/binaries.js.map +1 -1
- package/dist/utils/debug-log.js +51 -47
- package/dist/utils/debug-log.js.map +1 -1
- package/dist/utils/errors.js +157 -192
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/gateway-sync.js +49 -37
- package/dist/utils/gateway-sync.js.map +1 -1
- package/dist/utils/path-security.js +7 -6
- package/dist/utils/path-security.js.map +1 -1
- package/dist/utils/plan-diff.js +58 -39
- package/dist/utils/plan-diff.js.map +1 -1
- package/dist/utils/plans.js +92 -74
- package/dist/utils/plans.js.map +1 -1
- package/dist/utils/project.js +318 -256
- package/dist/utils/project.js.map +1 -1
- package/dist/utils/signals-pubsub.js +117 -99
- package/dist/utils/signals-pubsub.js.map +1 -1
- package/dist/utils/slash-command-loader.js +118 -154
- package/dist/utils/slash-command-loader.js.map +1 -1
- package/dist/utils/slash-command-processor.js +92 -83
- package/dist/utils/slash-command-processor.js.map +1 -1
- package/dist/utils/stdin-pipe.js +87 -63
- package/dist/utils/stdin-pipe.js.map +1 -1
- package/dist/utils/storage-factory.js +104 -84
- package/dist/utils/storage-factory.js.map +1 -1
- package/dist/utils/storage-maintenance.js +249 -211
- package/dist/utils/storage-maintenance.js.map +1 -1
- package/dist/utils/thread-lock.js +96 -92
- package/dist/utils/thread-lock.js.map +1 -1
- package/dist/utils/token-estimator.js +12 -12
- package/dist/utils/token-estimator.js.map +1 -1
- package/dist/utils/update-check.d.ts +1 -1
- package/dist/utils/update-check.js +341 -259
- package/dist/utils/update-check.js.map +1 -1
- package/dist/voice/stt-registry.js +131 -113
- package/dist/voice/stt-registry.js.map +1 -1
- package/package.json +11 -11
- package/dist/acp.js.map +0 -1
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/types.js.map +0 -1
- package/dist/evals/index.js.map +0 -1
- package/dist/evals/scorers/index.js.map +0 -1
- package/dist/headless/index.js.map +0 -1
- package/dist/hooks/index.js.map +0 -1
- package/dist/lsp/index.js.map +0 -1
- package/dist/mcp/index.js.map +0 -1
- package/dist/mcp/types.js.map +0 -1
- package/dist/onboarding/index.js.map +0 -1
- package/dist/plugins/types.js.map +0 -1
- package/dist/tools/index.js.map +0 -1
|
@@ -1,473 +1,400 @@
|
|
|
1
|
-
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
2
|
-
import { createOpenAI } from "@ai-sdk/openai";
|
|
3
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
4
|
-
import {
|
|
5
|
-
GATEWAY_AUTH_HEADER,
|
|
6
|
-
MastraGateway,
|
|
7
|
-
MastraModelGateway,
|
|
8
|
-
ModelRouterLanguageModel,
|
|
9
|
-
PROVIDER_REGISTRY
|
|
10
|
-
} from "@mastra/core/llm";
|
|
11
|
-
import { wrapLanguageModel } from "ai";
|
|
12
1
|
import { AuthStorage } from "../auth/storage.js";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
opencodeClaudeMaxProvider,
|
|
18
|
-
promptCacheMiddleware
|
|
19
|
-
} from "../providers/claude-max.js";
|
|
2
|
+
import { THINKING_LEVEL_TO_REASONING_EFFORT, buildOpenAICodexOAuthFetch, createCodexMiddleware, getEffectiveThinkingLevel, openaiCodexProvider } from "../providers/openai-codex.js";
|
|
3
|
+
import { getCustomProviderId, loadSettings } from "../onboarding/settings.js";
|
|
4
|
+
import { resolveCustomProviders } from "./custom-provider-source.js";
|
|
5
|
+
import { buildAnthropicOAuthFetch, claudeCodeMiddleware, opencodeClaudeMaxProvider, promptCacheMiddleware } from "../providers/claude-max.js";
|
|
20
6
|
import { getCopilotModelCatalog, githubCopilotProvider } from "../providers/github-copilot.js";
|
|
21
|
-
import {
|
|
22
|
-
buildOpenAICodexOAuthFetch,
|
|
23
|
-
createCodexMiddleware,
|
|
24
|
-
getEffectiveThinkingLevel,
|
|
25
|
-
openaiCodexProvider,
|
|
26
|
-
THINKING_LEVEL_TO_REASONING_EFFORT
|
|
27
|
-
} from "../providers/openai-codex.js";
|
|
28
7
|
import { xaiProvider } from "../providers/xai.js";
|
|
29
|
-
import {
|
|
8
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
9
|
+
import { wrapLanguageModel } from "ai";
|
|
10
|
+
import { GATEWAY_AUTH_HEADER, MastraGateway, MastraModelGateway, ModelRouterLanguageModel, PROVIDER_REGISTRY } from "@mastra/core/llm";
|
|
11
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
12
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
13
|
+
//#region src/agents/mastracode-gateway.ts
|
|
30
14
|
const OPENAI_PREFIX = "openai/";
|
|
31
15
|
const MASTRA_GATEWAY_PREFIX = "mastra/";
|
|
32
16
|
const MASTRACODE_GATEWAY_ID = "mastracode";
|
|
33
17
|
const CODEX_OPENAI_MODEL_REMAPS = {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
18
|
+
"gpt-5.3": "gpt-5.3-codex",
|
|
19
|
+
"gpt-5.2": "gpt-5.2-codex",
|
|
20
|
+
"gpt-5.1": "gpt-5.1-codex",
|
|
21
|
+
"gpt-5.1-mini": "gpt-5.1-codex-mini",
|
|
22
|
+
"gpt-5": "gpt-5-codex"
|
|
39
23
|
};
|
|
40
24
|
const authStorage = new AuthStorage();
|
|
41
25
|
function reloadAuthStorage() {
|
|
42
|
-
|
|
26
|
+
authStorage.reload();
|
|
43
27
|
}
|
|
44
28
|
function stripMastraGatewayPrefix(modelId) {
|
|
45
|
-
|
|
29
|
+
return modelId.startsWith("mastra/") ? modelId.substring(7) : modelId;
|
|
46
30
|
}
|
|
47
31
|
function normalizeAnthropicModelId(modelId) {
|
|
48
|
-
|
|
32
|
+
return modelId.replace(/\.(?=\d)/g, "-");
|
|
49
33
|
}
|
|
50
34
|
function remapOpenAIModelForCodexOAuth(modelId) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const codexModelId = CODEX_OPENAI_MODEL_REMAPS[openaiModelId];
|
|
60
|
-
if (!codexModelId) {
|
|
61
|
-
return modelId;
|
|
62
|
-
}
|
|
63
|
-
const remappedModelId = `${OPENAI_PREFIX}${codexModelId}`;
|
|
64
|
-
return modelId.startsWith(MASTRA_GATEWAY_PREFIX) ? `${MASTRA_GATEWAY_PREFIX}${remappedModelId}` : remappedModelId;
|
|
35
|
+
const normalizedModelId = stripMastraGatewayPrefix(modelId);
|
|
36
|
+
if (!normalizedModelId.startsWith("openai/")) return modelId;
|
|
37
|
+
const openaiModelId = normalizedModelId.substring(7);
|
|
38
|
+
if (openaiModelId.includes("-codex")) return modelId;
|
|
39
|
+
const codexModelId = CODEX_OPENAI_MODEL_REMAPS[openaiModelId];
|
|
40
|
+
if (!codexModelId) return modelId;
|
|
41
|
+
const remappedModelId = `${OPENAI_PREFIX}${codexModelId}`;
|
|
42
|
+
return modelId.startsWith("mastra/") ? `${MASTRA_GATEWAY_PREFIX}${remappedModelId}` : remappedModelId;
|
|
65
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolve the Anthropic API key.
|
|
46
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
47
|
+
*/
|
|
66
48
|
function getAnthropicApiKey(credentials = authStorage) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (dedicatedKey) return dedicatedKey;
|
|
73
|
-
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.ANTHROPIC_API_KEY?.trim() || void 0;
|
|
49
|
+
const storedCred = credentials.get("anthropic");
|
|
50
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
51
|
+
const dedicatedKey = credentials.getStoredApiKey("anthropic")?.trim();
|
|
52
|
+
if (dedicatedKey) return dedicatedKey;
|
|
53
|
+
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.ANTHROPIC_API_KEY?.trim() || void 0;
|
|
74
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Resolve the OpenAI API key.
|
|
57
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
58
|
+
*/
|
|
75
59
|
function getOpenAIApiKey(credentials = authStorage) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (dedicatedKey) return dedicatedKey;
|
|
82
|
-
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.OPENAI_API_KEY?.trim() || void 0;
|
|
60
|
+
const storedCred = credentials.get("openai-codex");
|
|
61
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
62
|
+
const dedicatedKey = credentials.getStoredApiKey("openai-codex")?.trim();
|
|
63
|
+
if (dedicatedKey) return dedicatedKey;
|
|
64
|
+
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.OPENAI_API_KEY?.trim() || void 0;
|
|
83
65
|
}
|
|
84
66
|
function anthropicApiKeyProvider(modelId, apiKey, headers) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
67
|
+
return wrapLanguageModel({
|
|
68
|
+
model: createAnthropic({
|
|
69
|
+
apiKey,
|
|
70
|
+
headers
|
|
71
|
+
})(modelId),
|
|
72
|
+
middleware: [promptCacheMiddleware]
|
|
73
|
+
});
|
|
90
74
|
}
|
|
91
75
|
function openaiApiKeyProvider(modelId, apiKey, headers) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
76
|
+
return wrapLanguageModel({
|
|
77
|
+
model: createOpenAI({
|
|
78
|
+
apiKey,
|
|
79
|
+
baseURL: process.env.OPENAI_BASE_URL,
|
|
80
|
+
headers
|
|
81
|
+
}).responses(modelId),
|
|
82
|
+
middleware: []
|
|
83
|
+
});
|
|
97
84
|
}
|
|
98
85
|
function getAuthProviderId(providerId) {
|
|
99
|
-
|
|
86
|
+
return providerId === "openai" ? "openai-codex" : providerId;
|
|
100
87
|
}
|
|
101
88
|
function getProviderAuthKey(providerId, credentials = authStorage) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
return credentials.getStoredApiKey(authProviderId)?.trim() || void 0;
|
|
89
|
+
const authProviderId = getAuthProviderId(providerId);
|
|
90
|
+
const storedCred = credentials.get(authProviderId);
|
|
91
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
92
|
+
return credentials.getStoredApiKey(authProviderId)?.trim() || void 0;
|
|
108
93
|
}
|
|
109
94
|
function resolveAuth(request, mastraGatewayApiKey) {
|
|
110
|
-
|
|
95
|
+
return MastraCodeGateway.resolveProviderAuth(request, mastraGatewayApiKey);
|
|
111
96
|
}
|
|
112
97
|
function getGatewayProviderKey(gatewayId, providerId) {
|
|
113
|
-
|
|
114
|
-
|
|
98
|
+
if (gatewayId === "models.dev") return providerId;
|
|
99
|
+
return providerId === gatewayId ? gatewayId : `${gatewayId}/${providerId}`;
|
|
115
100
|
}
|
|
116
101
|
function parseGatewayRouterId(routerId, gateway) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
modelId: [secondPart, ...restParts].filter(Boolean).join("/")
|
|
130
|
-
};
|
|
102
|
+
const [firstPart = "", secondPart = "", ...restParts] = routerId.split("/");
|
|
103
|
+
const gatewayId = gateway.id;
|
|
104
|
+
if (firstPart === gatewayId && secondPart) return {
|
|
105
|
+
gatewayId,
|
|
106
|
+
providerId: secondPart,
|
|
107
|
+
modelId: restParts.join("/")
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
gatewayId,
|
|
111
|
+
providerId: firstPart,
|
|
112
|
+
modelId: [secondPart, ...restParts].filter(Boolean).join("/")
|
|
113
|
+
};
|
|
131
114
|
}
|
|
132
115
|
function hasResolvedAuth(auth) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
116
|
+
if (!auth) return false;
|
|
117
|
+
if (auth.apiKey || auth.bearerToken) return true;
|
|
118
|
+
return auth.headers ? Object.keys(auth.headers).length > 0 : false;
|
|
136
119
|
}
|
|
137
120
|
async function resolveGatewayProviderAuth(gateway, routerId) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
121
|
+
if (!gateway.resolveAuth) return void 0;
|
|
122
|
+
const parsed = parseGatewayRouterId(routerId, gateway);
|
|
123
|
+
try {
|
|
124
|
+
const result = await gateway.resolveAuth({
|
|
125
|
+
gatewayId: parsed.gatewayId,
|
|
126
|
+
providerId: parsed.providerId,
|
|
127
|
+
modelId: parsed.modelId,
|
|
128
|
+
routerId
|
|
129
|
+
});
|
|
130
|
+
return hasResolvedAuth(result) ? result : void 0;
|
|
131
|
+
} catch {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
151
134
|
}
|
|
152
135
|
async function getMastraCodeProviderConfigs(gateway) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
return providers;
|
|
136
|
+
const providers = { ...PROVIDER_REGISTRY };
|
|
137
|
+
try {
|
|
138
|
+
const gatewayProviders = await gateway.fetchProviders();
|
|
139
|
+
for (const [providerId, config] of Object.entries(gatewayProviders)) providers[getGatewayProviderKey(gateway.id, providerId)] = {
|
|
140
|
+
...config,
|
|
141
|
+
gateway: gateway.id
|
|
142
|
+
};
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.warn(`Failed to load providers from gateway ${gateway.id}:`, error);
|
|
145
|
+
}
|
|
146
|
+
return providers;
|
|
166
147
|
}
|
|
167
148
|
function getApiKeyEnvVar(providerConfig) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
class MastraCodeGateway extends MastraModelGateway {
|
|
172
|
-
id = MASTRACODE_GATEWAY_ID;
|
|
173
|
-
name = "MastraCode Gateway";
|
|
174
|
-
#mastraGateway;
|
|
175
|
-
#mastraGatewayBaseUrl;
|
|
176
|
-
#mastraGatewayApiKey;
|
|
177
|
-
#routeThroughMastraGateway;
|
|
178
|
-
#thinkingLevel;
|
|
179
|
-
#customProviders;
|
|
180
|
-
#settingsPath;
|
|
181
|
-
#credentials;
|
|
182
|
-
constructor({
|
|
183
|
-
mastraGatewayBaseUrl,
|
|
184
|
-
mastraGatewayApiKey,
|
|
185
|
-
routeThroughMastraGateway,
|
|
186
|
-
thinkingLevel,
|
|
187
|
-
customProviders,
|
|
188
|
-
settingsPath,
|
|
189
|
-
credentialStore
|
|
190
|
-
}) {
|
|
191
|
-
super();
|
|
192
|
-
this.#mastraGateway = new MastraGateway({ baseUrl: mastraGatewayBaseUrl });
|
|
193
|
-
this.#mastraGatewayBaseUrl = mastraGatewayBaseUrl;
|
|
194
|
-
this.#mastraGatewayApiKey = mastraGatewayApiKey;
|
|
195
|
-
this.#routeThroughMastraGateway = routeThroughMastraGateway;
|
|
196
|
-
this.#thinkingLevel = thinkingLevel;
|
|
197
|
-
this.#customProviders = customProviders;
|
|
198
|
-
this.#settingsPath = settingsPath;
|
|
199
|
-
this.#credentials = credentialStore ?? authStorage;
|
|
200
|
-
}
|
|
201
|
-
static getMastraGatewayApiKey() {
|
|
202
|
-
return authStorage.getStoredApiKey(MASTRA_GATEWAY_PROVIDER) ?? process.env["MASTRA_GATEWAY_API_KEY"];
|
|
203
|
-
}
|
|
204
|
-
/** @deprecated Renamed to {@link MastraCodeGateway.getMastraGatewayApiKey}. */
|
|
205
|
-
static getMemoryGatewayApiKey() {
|
|
206
|
-
return MastraCodeGateway.getMastraGatewayApiKey();
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Claim an unprefixed model id (e.g. a bare `anthropic/...`) when this gateway
|
|
210
|
-
* can authenticate it via OAuth or a stored/env credential. This lets the
|
|
211
|
-
* shared gateway router select MastraCode for auth resolution, so the status
|
|
212
|
-
* bar and `/api-keys` reflect OAuth logins instead of only checking env vars.
|
|
213
|
-
*/
|
|
214
|
-
handlesModel(modelId) {
|
|
215
|
-
const parsed = parseGatewayRouterId(modelId, this);
|
|
216
|
-
if (!parsed.providerId || !parsed.modelId) return false;
|
|
217
|
-
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) return true;
|
|
218
|
-
const customProvider = this.#getCustomProviders().find(
|
|
219
|
-
(provider) => parsed.providerId === getCustomProviderId(provider.name)
|
|
220
|
-
);
|
|
221
|
-
if (customProvider?.apiKey) return true;
|
|
222
|
-
return hasResolvedAuth(
|
|
223
|
-
MastraCodeGateway.resolveProviderAuth(
|
|
224
|
-
{
|
|
225
|
-
gatewayId: this.id,
|
|
226
|
-
providerId: parsed.providerId,
|
|
227
|
-
modelId: parsed.modelId,
|
|
228
|
-
routerId: modelId
|
|
229
|
-
},
|
|
230
|
-
void 0,
|
|
231
|
-
this.#credentials
|
|
232
|
-
)
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
static resolveProviderAuth(request, mastraGatewayApiKey, credentials = authStorage) {
|
|
236
|
-
if (request.gatewayId === "mastra" && mastraGatewayApiKey) {
|
|
237
|
-
return { apiKey: mastraGatewayApiKey, source: "gateway" };
|
|
238
|
-
}
|
|
239
|
-
const storedCred = credentials.get(getAuthProviderId(request.providerId));
|
|
240
|
-
if (storedCred?.type === "oauth") {
|
|
241
|
-
return { bearerToken: "oauth", source: "gateway" };
|
|
242
|
-
}
|
|
243
|
-
const apiKey = getProviderAuthKey(request.providerId, credentials);
|
|
244
|
-
return apiKey ? { apiKey, source: "gateway" } : void 0;
|
|
245
|
-
}
|
|
246
|
-
static createModelCatalogProvider(gateway) {
|
|
247
|
-
return async () => {
|
|
248
|
-
const registry = await getMastraCodeProviderConfigs(gateway);
|
|
249
|
-
const models = [];
|
|
250
|
-
for (const [provider, providerConfig] of Object.entries(registry)) {
|
|
251
|
-
const apiKeyEnvVar = getApiKeyEnvVar(providerConfig);
|
|
252
|
-
const hasEnvKey = apiKeyEnvVar ? Boolean(process.env[apiKeyEnvVar]) : false;
|
|
253
|
-
const modelNames = providerConfig.models;
|
|
254
|
-
if (!Array.isArray(modelNames)) continue;
|
|
255
|
-
const gatewayAuth = modelNames[0] ? await resolveGatewayProviderAuth(gateway, `${provider}/${modelNames[0]}`) : void 0;
|
|
256
|
-
for (const modelName of modelNames) {
|
|
257
|
-
const id = `${provider}/${modelName}`;
|
|
258
|
-
models.push({
|
|
259
|
-
id,
|
|
260
|
-
provider,
|
|
261
|
-
modelName,
|
|
262
|
-
hasApiKey: hasEnvKey || Boolean(gatewayAuth),
|
|
263
|
-
apiKeyEnvVar: apiKeyEnvVar || void 0
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
return models;
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
createModelCatalogProvider() {
|
|
271
|
-
return MastraCodeGateway.createModelCatalogProvider(this);
|
|
272
|
-
}
|
|
273
|
-
#getCustomProviders() {
|
|
274
|
-
return this.#customProviders ?? resolveCustomProviders() ?? loadSettings(this.#settingsPath).customProviders;
|
|
275
|
-
}
|
|
276
|
-
async fetchProviders() {
|
|
277
|
-
const providers = {};
|
|
278
|
-
for (const provider of this.#getCustomProviders()) {
|
|
279
|
-
const models = provider.models ?? [];
|
|
280
|
-
if (!models.length) continue;
|
|
281
|
-
providers[getCustomProviderId(provider.name)] = {
|
|
282
|
-
name: provider.name,
|
|
283
|
-
url: provider.url,
|
|
284
|
-
apiKeyEnvVar: "",
|
|
285
|
-
apiKeyHeader: "Authorization",
|
|
286
|
-
gateway: this.id,
|
|
287
|
-
models
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
try {
|
|
291
|
-
const copilotModels = await getCopilotModelCatalog({ authStorage });
|
|
292
|
-
providers["github-copilot"] = {
|
|
293
|
-
name: "GitHub Copilot",
|
|
294
|
-
apiKeyEnvVar: "",
|
|
295
|
-
apiKeyHeader: "Authorization",
|
|
296
|
-
gateway: this.id,
|
|
297
|
-
models: copilotModels.map((model) => model.id)
|
|
298
|
-
};
|
|
299
|
-
} catch (error) {
|
|
300
|
-
console.warn("Failed to load GitHub Copilot model catalog:", error);
|
|
301
|
-
}
|
|
302
|
-
return providers;
|
|
303
|
-
}
|
|
304
|
-
buildUrl(modelId) {
|
|
305
|
-
return this.#routeThroughMastraGateway ? this.#mastraGateway.buildUrl(modelId) : modelId;
|
|
306
|
-
}
|
|
307
|
-
async getApiKey(modelId) {
|
|
308
|
-
const providerId = stripMastraGatewayPrefix(modelId).split("/", 1)[0];
|
|
309
|
-
if (this.#routeThroughMastraGateway) return this.#mastraGatewayApiKey ?? "";
|
|
310
|
-
return providerId ? getProviderAuthKey(providerId, this.#credentials) ?? "" : "";
|
|
311
|
-
}
|
|
312
|
-
resolveAuth(request) {
|
|
313
|
-
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) {
|
|
314
|
-
return { apiKey: this.#mastraGatewayApiKey, source: "gateway" };
|
|
315
|
-
}
|
|
316
|
-
const customProvider = this.#getCustomProviders().find(
|
|
317
|
-
(provider) => request.providerId === getCustomProviderId(provider.name)
|
|
318
|
-
);
|
|
319
|
-
if (customProvider?.apiKey) {
|
|
320
|
-
return { apiKey: customProvider.apiKey, source: "gateway" };
|
|
321
|
-
}
|
|
322
|
-
return MastraCodeGateway.resolveProviderAuth(request, void 0, this.#credentials);
|
|
323
|
-
}
|
|
324
|
-
resolveLanguageModel(args) {
|
|
325
|
-
const customProvider = this.#getCustomProviders().find(
|
|
326
|
-
(provider) => args.providerId === getCustomProviderId(provider.name)
|
|
327
|
-
);
|
|
328
|
-
if (customProvider) {
|
|
329
|
-
const provider = createOpenAICompatible({
|
|
330
|
-
name: args.providerId,
|
|
331
|
-
baseURL: customProvider.url,
|
|
332
|
-
apiKey: args.apiKey,
|
|
333
|
-
headers: args.headers
|
|
334
|
-
});
|
|
335
|
-
return provider.chatModel(args.modelId);
|
|
336
|
-
}
|
|
337
|
-
if (args.providerId === "github-copilot") {
|
|
338
|
-
return githubCopilotProvider(args.modelId, {
|
|
339
|
-
headers: args.headers,
|
|
340
|
-
authStorage: this.#credentials
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
if (args.providerId === "moonshotai") {
|
|
344
|
-
const apiKey = args.apiKey?.trim() || process.env.MOONSHOT_API_KEY?.trim() || process.env.MOONSHOT_AI_API_KEY?.trim();
|
|
345
|
-
if (!apiKey) {
|
|
346
|
-
throw new Error("Need MOONSHOT_API_KEY");
|
|
347
|
-
}
|
|
348
|
-
return createAnthropic({
|
|
349
|
-
apiKey,
|
|
350
|
-
baseURL: "https://api.moonshot.ai/anthropic/v1",
|
|
351
|
-
name: "moonshotai.anthropicv1",
|
|
352
|
-
headers: args.headers
|
|
353
|
-
})(args.modelId);
|
|
354
|
-
}
|
|
355
|
-
if (args.providerId === "anthropic") {
|
|
356
|
-
return this.#resolveAnthropicModel(args);
|
|
357
|
-
}
|
|
358
|
-
if (args.providerId === "openai") {
|
|
359
|
-
const openaiModel = this.#resolveOpenAIModel(args);
|
|
360
|
-
if (openaiModel) return openaiModel;
|
|
361
|
-
}
|
|
362
|
-
if (args.providerId === "xai" && this.#credentials.get("xai")?.type === "oauth") {
|
|
363
|
-
return xaiProvider(args.modelId, {
|
|
364
|
-
headers: args.headers,
|
|
365
|
-
authStorage: this.#credentials
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
if (this.#routeThroughMastraGateway) {
|
|
369
|
-
return this.#mastraGateway.resolveLanguageModel(args);
|
|
370
|
-
}
|
|
371
|
-
return new ModelRouterLanguageModel({
|
|
372
|
-
id: `${args.providerId}/${args.modelId}`,
|
|
373
|
-
apiKey: args.apiKey,
|
|
374
|
-
headers: args.headers
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
#resolveAnthropicModel(args) {
|
|
378
|
-
const bareModelId = normalizeAnthropicModelId(args.modelId);
|
|
379
|
-
const storedCred = this.#credentials.get("anthropic");
|
|
380
|
-
if (this.#routeThroughMastraGateway) {
|
|
381
|
-
if (storedCred?.type === "oauth") {
|
|
382
|
-
const anthropic = createAnthropic({
|
|
383
|
-
apiKey: "oauth-gateway-placeholder",
|
|
384
|
-
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
385
|
-
headers: {
|
|
386
|
-
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
387
|
-
...args.headers
|
|
388
|
-
},
|
|
389
|
-
fetch: buildAnthropicOAuthFetch({ authStorage: this.#credentials })
|
|
390
|
-
});
|
|
391
|
-
return wrapLanguageModel({
|
|
392
|
-
model: anthropic(bareModelId),
|
|
393
|
-
middleware: [claudeCodeMiddleware, promptCacheMiddleware]
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
return this.#mastraGateway.resolveLanguageModel({ ...args, modelId: bareModelId });
|
|
397
|
-
}
|
|
398
|
-
if (storedCred?.type === "oauth") {
|
|
399
|
-
return opencodeClaudeMaxProvider(bareModelId, {
|
|
400
|
-
headers: args.headers,
|
|
401
|
-
authStorage: this.#credentials
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) {
|
|
405
|
-
return anthropicApiKeyProvider(
|
|
406
|
-
bareModelId,
|
|
407
|
-
storedCred.key.trim(),
|
|
408
|
-
args.headers
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
const apiKey = getAnthropicApiKey(this.#credentials);
|
|
412
|
-
if (apiKey) {
|
|
413
|
-
return anthropicApiKeyProvider(bareModelId, apiKey, args.headers);
|
|
414
|
-
}
|
|
415
|
-
return opencodeClaudeMaxProvider(bareModelId, {
|
|
416
|
-
headers: args.headers,
|
|
417
|
-
authStorage: this.#credentials
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
#resolveOpenAIModel(args) {
|
|
421
|
-
const storedCred = this.#credentials.get("openai-codex");
|
|
422
|
-
if (this.#routeThroughMastraGateway) {
|
|
423
|
-
if (storedCred?.type === "oauth") {
|
|
424
|
-
const resolvedModelId = remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`);
|
|
425
|
-
const resolvedBareModelId = resolvedModelId.substring(OPENAI_PREFIX.length);
|
|
426
|
-
const requestedLevel = this.#thinkingLevel ?? "medium";
|
|
427
|
-
const effectiveLevel = getEffectiveThinkingLevel(resolvedBareModelId, requestedLevel);
|
|
428
|
-
const reasoningEffort = THINKING_LEVEL_TO_REASONING_EFFORT[effectiveLevel];
|
|
429
|
-
const middleware = createCodexMiddleware(reasoningEffort);
|
|
430
|
-
const openai = createOpenAI({
|
|
431
|
-
apiKey: "oauth-gateway-placeholder",
|
|
432
|
-
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
433
|
-
headers: {
|
|
434
|
-
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
435
|
-
...args.headers
|
|
436
|
-
},
|
|
437
|
-
fetch: buildOpenAICodexOAuthFetch({ authStorage: this.#credentials, rewriteUrl: false })
|
|
438
|
-
});
|
|
439
|
-
return wrapLanguageModel({
|
|
440
|
-
model: openai.responses(resolvedBareModelId),
|
|
441
|
-
middleware: [middleware]
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
return this.#mastraGateway.resolveLanguageModel(args);
|
|
445
|
-
}
|
|
446
|
-
if (storedCred?.type === "oauth") {
|
|
447
|
-
const resolvedModelId = remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`);
|
|
448
|
-
return openaiCodexProvider(resolvedModelId.substring(OPENAI_PREFIX.length), {
|
|
449
|
-
thinkingLevel: this.#thinkingLevel,
|
|
450
|
-
headers: args.headers,
|
|
451
|
-
authStorage: this.#credentials
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
const apiKey = getOpenAIApiKey(this.#credentials);
|
|
455
|
-
if (apiKey) {
|
|
456
|
-
return openaiApiKeyProvider(args.modelId, apiKey, args.headers);
|
|
457
|
-
}
|
|
458
|
-
return void 0;
|
|
459
|
-
}
|
|
149
|
+
const envVars = providerConfig?.apiKeyEnvVar;
|
|
150
|
+
return Array.isArray(envVars) ? envVars[0] : envVars;
|
|
460
151
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
152
|
+
var MastraCodeGateway = class MastraCodeGateway extends MastraModelGateway {
|
|
153
|
+
id = MASTRACODE_GATEWAY_ID;
|
|
154
|
+
name = "MastraCode Gateway";
|
|
155
|
+
#mastraGateway;
|
|
156
|
+
#mastraGatewayBaseUrl;
|
|
157
|
+
#mastraGatewayApiKey;
|
|
158
|
+
#routeThroughMastraGateway;
|
|
159
|
+
#thinkingLevel;
|
|
160
|
+
#customProviders;
|
|
161
|
+
#settingsPath;
|
|
162
|
+
#credentials;
|
|
163
|
+
constructor({ mastraGatewayBaseUrl, mastraGatewayApiKey, routeThroughMastraGateway, thinkingLevel, customProviders, settingsPath, credentialStore }) {
|
|
164
|
+
super();
|
|
165
|
+
this.#mastraGateway = new MastraGateway({ baseUrl: mastraGatewayBaseUrl });
|
|
166
|
+
this.#mastraGatewayBaseUrl = mastraGatewayBaseUrl;
|
|
167
|
+
this.#mastraGatewayApiKey = mastraGatewayApiKey;
|
|
168
|
+
this.#routeThroughMastraGateway = routeThroughMastraGateway;
|
|
169
|
+
this.#thinkingLevel = thinkingLevel;
|
|
170
|
+
this.#customProviders = customProviders;
|
|
171
|
+
this.#settingsPath = settingsPath;
|
|
172
|
+
this.#credentials = credentialStore ?? authStorage;
|
|
173
|
+
}
|
|
174
|
+
static getMastraGatewayApiKey() {
|
|
175
|
+
return authStorage.getStoredApiKey("mastra-gateway") ?? process.env["MASTRA_GATEWAY_API_KEY"];
|
|
176
|
+
}
|
|
177
|
+
/** @deprecated Renamed to {@link MastraCodeGateway.getMastraGatewayApiKey}. */
|
|
178
|
+
static getMemoryGatewayApiKey() {
|
|
179
|
+
return MastraCodeGateway.getMastraGatewayApiKey();
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Claim an unprefixed model id (e.g. a bare `anthropic/...`) when this gateway
|
|
183
|
+
* can authenticate it via OAuth or a stored/env credential. This lets the
|
|
184
|
+
* shared gateway router select MastraCode for auth resolution, so the status
|
|
185
|
+
* bar and `/api-keys` reflect OAuth logins instead of only checking env vars.
|
|
186
|
+
*/
|
|
187
|
+
handlesModel(modelId) {
|
|
188
|
+
const parsed = parseGatewayRouterId(modelId, this);
|
|
189
|
+
if (!parsed.providerId || !parsed.modelId) return false;
|
|
190
|
+
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) return true;
|
|
191
|
+
if (this.#getCustomProviders().find((provider) => parsed.providerId === getCustomProviderId(provider.name))?.apiKey) return true;
|
|
192
|
+
return hasResolvedAuth(MastraCodeGateway.resolveProviderAuth({
|
|
193
|
+
gatewayId: this.id,
|
|
194
|
+
providerId: parsed.providerId,
|
|
195
|
+
modelId: parsed.modelId,
|
|
196
|
+
routerId: modelId
|
|
197
|
+
}, void 0, this.#credentials));
|
|
198
|
+
}
|
|
199
|
+
static resolveProviderAuth(request, mastraGatewayApiKey, credentials = authStorage) {
|
|
200
|
+
if (request.gatewayId === "mastra" && mastraGatewayApiKey) return {
|
|
201
|
+
apiKey: mastraGatewayApiKey,
|
|
202
|
+
source: "gateway"
|
|
203
|
+
};
|
|
204
|
+
if (credentials.get(getAuthProviderId(request.providerId))?.type === "oauth") return {
|
|
205
|
+
bearerToken: "oauth",
|
|
206
|
+
source: "gateway"
|
|
207
|
+
};
|
|
208
|
+
const apiKey = getProviderAuthKey(request.providerId, credentials);
|
|
209
|
+
return apiKey ? {
|
|
210
|
+
apiKey,
|
|
211
|
+
source: "gateway"
|
|
212
|
+
} : void 0;
|
|
213
|
+
}
|
|
214
|
+
static createModelCatalogProvider(gateway) {
|
|
215
|
+
return async () => {
|
|
216
|
+
const registry = await getMastraCodeProviderConfigs(gateway);
|
|
217
|
+
const models = [];
|
|
218
|
+
for (const [provider, providerConfig] of Object.entries(registry)) {
|
|
219
|
+
const apiKeyEnvVar = getApiKeyEnvVar(providerConfig);
|
|
220
|
+
const hasEnvKey = apiKeyEnvVar ? Boolean(process.env[apiKeyEnvVar]) : false;
|
|
221
|
+
const modelNames = providerConfig.models;
|
|
222
|
+
if (!Array.isArray(modelNames)) continue;
|
|
223
|
+
const gatewayAuth = modelNames[0] ? await resolveGatewayProviderAuth(gateway, `${provider}/${modelNames[0]}`) : void 0;
|
|
224
|
+
for (const modelName of modelNames) {
|
|
225
|
+
const id = `${provider}/${modelName}`;
|
|
226
|
+
models.push({
|
|
227
|
+
id,
|
|
228
|
+
provider,
|
|
229
|
+
modelName,
|
|
230
|
+
hasApiKey: hasEnvKey || Boolean(gatewayAuth),
|
|
231
|
+
apiKeyEnvVar: apiKeyEnvVar || void 0
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return models;
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
createModelCatalogProvider() {
|
|
239
|
+
return MastraCodeGateway.createModelCatalogProvider(this);
|
|
240
|
+
}
|
|
241
|
+
#getCustomProviders() {
|
|
242
|
+
return this.#customProviders ?? resolveCustomProviders() ?? loadSettings(this.#settingsPath).customProviders;
|
|
243
|
+
}
|
|
244
|
+
async fetchProviders() {
|
|
245
|
+
const providers = {};
|
|
246
|
+
for (const provider of this.#getCustomProviders()) {
|
|
247
|
+
const models = provider.models ?? [];
|
|
248
|
+
if (!models.length) continue;
|
|
249
|
+
providers[getCustomProviderId(provider.name)] = {
|
|
250
|
+
name: provider.name,
|
|
251
|
+
url: provider.url,
|
|
252
|
+
apiKeyEnvVar: "",
|
|
253
|
+
apiKeyHeader: "Authorization",
|
|
254
|
+
gateway: this.id,
|
|
255
|
+
models
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
const copilotModels = await getCopilotModelCatalog({ authStorage });
|
|
260
|
+
providers["github-copilot"] = {
|
|
261
|
+
name: "GitHub Copilot",
|
|
262
|
+
apiKeyEnvVar: "",
|
|
263
|
+
apiKeyHeader: "Authorization",
|
|
264
|
+
gateway: this.id,
|
|
265
|
+
models: copilotModels.map((model) => model.id)
|
|
266
|
+
};
|
|
267
|
+
} catch (error) {
|
|
268
|
+
console.warn("Failed to load GitHub Copilot model catalog:", error);
|
|
269
|
+
}
|
|
270
|
+
return providers;
|
|
271
|
+
}
|
|
272
|
+
buildUrl(modelId) {
|
|
273
|
+
return this.#routeThroughMastraGateway ? this.#mastraGateway.buildUrl(modelId) : modelId;
|
|
274
|
+
}
|
|
275
|
+
async getApiKey(modelId) {
|
|
276
|
+
const providerId = stripMastraGatewayPrefix(modelId).split("/", 1)[0];
|
|
277
|
+
if (this.#routeThroughMastraGateway) return this.#mastraGatewayApiKey ?? "";
|
|
278
|
+
return providerId ? getProviderAuthKey(providerId, this.#credentials) ?? "" : "";
|
|
279
|
+
}
|
|
280
|
+
resolveAuth(request) {
|
|
281
|
+
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) return {
|
|
282
|
+
apiKey: this.#mastraGatewayApiKey,
|
|
283
|
+
source: "gateway"
|
|
284
|
+
};
|
|
285
|
+
const customProvider = this.#getCustomProviders().find((provider) => request.providerId === getCustomProviderId(provider.name));
|
|
286
|
+
if (customProvider?.apiKey) return {
|
|
287
|
+
apiKey: customProvider.apiKey,
|
|
288
|
+
source: "gateway"
|
|
289
|
+
};
|
|
290
|
+
return MastraCodeGateway.resolveProviderAuth(request, void 0, this.#credentials);
|
|
291
|
+
}
|
|
292
|
+
resolveLanguageModel(args) {
|
|
293
|
+
const customProvider = this.#getCustomProviders().find((provider) => args.providerId === getCustomProviderId(provider.name));
|
|
294
|
+
if (customProvider) return createOpenAICompatible({
|
|
295
|
+
name: args.providerId,
|
|
296
|
+
baseURL: customProvider.url,
|
|
297
|
+
apiKey: args.apiKey,
|
|
298
|
+
headers: args.headers
|
|
299
|
+
}).chatModel(args.modelId);
|
|
300
|
+
if (args.providerId === "github-copilot") return githubCopilotProvider(args.modelId, {
|
|
301
|
+
headers: args.headers,
|
|
302
|
+
authStorage: this.#credentials
|
|
303
|
+
});
|
|
304
|
+
if (args.providerId === "moonshotai") {
|
|
305
|
+
const apiKey = args.apiKey?.trim() || process.env.MOONSHOT_API_KEY?.trim() || process.env.MOONSHOT_AI_API_KEY?.trim();
|
|
306
|
+
if (!apiKey) throw new Error("Need MOONSHOT_API_KEY");
|
|
307
|
+
return createAnthropic({
|
|
308
|
+
apiKey,
|
|
309
|
+
baseURL: "https://api.moonshot.ai/anthropic/v1",
|
|
310
|
+
name: "moonshotai.anthropicv1",
|
|
311
|
+
headers: args.headers
|
|
312
|
+
})(args.modelId);
|
|
313
|
+
}
|
|
314
|
+
if (args.providerId === "anthropic") return this.#resolveAnthropicModel(args);
|
|
315
|
+
if (args.providerId === "openai") {
|
|
316
|
+
const openaiModel = this.#resolveOpenAIModel(args);
|
|
317
|
+
if (openaiModel) return openaiModel;
|
|
318
|
+
}
|
|
319
|
+
if (args.providerId === "xai" && this.#credentials.get("xai")?.type === "oauth") return xaiProvider(args.modelId, {
|
|
320
|
+
headers: args.headers,
|
|
321
|
+
authStorage: this.#credentials
|
|
322
|
+
});
|
|
323
|
+
if (this.#routeThroughMastraGateway) return this.#mastraGateway.resolveLanguageModel(args);
|
|
324
|
+
return new ModelRouterLanguageModel({
|
|
325
|
+
id: `${args.providerId}/${args.modelId}`,
|
|
326
|
+
apiKey: args.apiKey,
|
|
327
|
+
headers: args.headers
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
#resolveAnthropicModel(args) {
|
|
331
|
+
const bareModelId = normalizeAnthropicModelId(args.modelId);
|
|
332
|
+
const storedCred = this.#credentials.get("anthropic");
|
|
333
|
+
if (this.#routeThroughMastraGateway) {
|
|
334
|
+
if (storedCred?.type === "oauth") return wrapLanguageModel({
|
|
335
|
+
model: createAnthropic({
|
|
336
|
+
apiKey: "oauth-gateway-placeholder",
|
|
337
|
+
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
338
|
+
headers: {
|
|
339
|
+
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
340
|
+
...args.headers
|
|
341
|
+
},
|
|
342
|
+
fetch: buildAnthropicOAuthFetch({ authStorage: this.#credentials })
|
|
343
|
+
})(bareModelId),
|
|
344
|
+
middleware: [claudeCodeMiddleware, promptCacheMiddleware]
|
|
345
|
+
});
|
|
346
|
+
return this.#mastraGateway.resolveLanguageModel({
|
|
347
|
+
...args,
|
|
348
|
+
modelId: bareModelId
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
if (storedCred?.type === "oauth") return opencodeClaudeMaxProvider(bareModelId, {
|
|
352
|
+
headers: args.headers,
|
|
353
|
+
authStorage: this.#credentials
|
|
354
|
+
});
|
|
355
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return anthropicApiKeyProvider(bareModelId, storedCred.key.trim(), args.headers);
|
|
356
|
+
const apiKey = getAnthropicApiKey(this.#credentials);
|
|
357
|
+
if (apiKey) return anthropicApiKeyProvider(bareModelId, apiKey, args.headers);
|
|
358
|
+
return opencodeClaudeMaxProvider(bareModelId, {
|
|
359
|
+
headers: args.headers,
|
|
360
|
+
authStorage: this.#credentials
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
#resolveOpenAIModel(args) {
|
|
364
|
+
const storedCred = this.#credentials.get("openai-codex");
|
|
365
|
+
if (this.#routeThroughMastraGateway) {
|
|
366
|
+
if (storedCred?.type === "oauth") {
|
|
367
|
+
const resolvedBareModelId = remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`).substring(7);
|
|
368
|
+
const reasoningEffort = THINKING_LEVEL_TO_REASONING_EFFORT[getEffectiveThinkingLevel(resolvedBareModelId, this.#thinkingLevel ?? "medium")];
|
|
369
|
+
const middleware = createCodexMiddleware(reasoningEffort);
|
|
370
|
+
return wrapLanguageModel({
|
|
371
|
+
model: createOpenAI({
|
|
372
|
+
apiKey: "oauth-gateway-placeholder",
|
|
373
|
+
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
374
|
+
headers: {
|
|
375
|
+
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
376
|
+
...args.headers
|
|
377
|
+
},
|
|
378
|
+
fetch: buildOpenAICodexOAuthFetch({
|
|
379
|
+
authStorage: this.#credentials,
|
|
380
|
+
rewriteUrl: false
|
|
381
|
+
})
|
|
382
|
+
}).responses(resolvedBareModelId),
|
|
383
|
+
middleware: [middleware]
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
return this.#mastraGateway.resolveLanguageModel(args);
|
|
387
|
+
}
|
|
388
|
+
if (storedCred?.type === "oauth") return openaiCodexProvider(remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`).substring(7), {
|
|
389
|
+
thinkingLevel: this.#thinkingLevel,
|
|
390
|
+
headers: args.headers,
|
|
391
|
+
authStorage: this.#credentials
|
|
392
|
+
});
|
|
393
|
+
const apiKey = getOpenAIApiKey(this.#credentials);
|
|
394
|
+
if (apiKey) return openaiApiKeyProvider(args.modelId, apiKey, args.headers);
|
|
395
|
+
}
|
|
472
396
|
};
|
|
397
|
+
//#endregion
|
|
398
|
+
export { MASTRACODE_GATEWAY_ID, MASTRA_GATEWAY_PREFIX, MastraCodeGateway, OPENAI_PREFIX, getAnthropicApiKey, getOpenAIApiKey, reloadAuthStorage, remapOpenAIModelForCodexOAuth, resolveAuth, stripMastraGatewayPrefix };
|
|
399
|
+
|
|
473
400
|
//# sourceMappingURL=mastracode-gateway.js.map
|