@mastra/code-sdk 0.0.0-agent-tags-alpha-20261125185303
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/LICENSE.md +32 -0
- package/README.md +284 -0
- package/dist/acp/agent.d.ts +48 -0
- package/dist/acp/agent.js +414 -0
- package/dist/acp/errors.d.ts +3 -0
- package/dist/acp/errors.js +11 -0
- package/dist/acp/event-mapper.d.ts +28 -0
- package/dist/acp/event-mapper.js +274 -0
- package/dist/acp/index.d.ts +9 -0
- package/dist/acp/index.js +21 -0
- package/dist/acp/runtime.d.ts +5 -0
- package/dist/acp/runtime.js +91 -0
- package/dist/acp/server.d.ts +4 -0
- package/dist/acp/server.js +35 -0
- package/dist/acp/skills.d.ts +6 -0
- package/dist/acp/skills.js +30 -0
- package/dist/acp.d.ts +2 -0
- package/dist/acp.js +1 -0
- package/dist/agent-connections/messaging-processor.d.ts +17 -0
- package/dist/agent-connections/messaging-processor.js +131 -0
- package/dist/agent-connections/ownership.d.ts +24 -0
- package/dist/agent-connections/ownership.js +96 -0
- package/dist/agent-connections/registry.d.ts +20 -0
- package/dist/agent-connections/registry.js +167 -0
- package/dist/agent-connections/session-advertisement.d.ts +27 -0
- package/dist/agent-connections/session-advertisement.js +86 -0
- package/dist/agent-connections/signal-provider.d.ts +141 -0
- package/dist/agent-connections/signal-provider.js +35 -0
- package/dist/agent-connections/state-processor.d.ts +23 -0
- package/dist/agent-connections/state-processor.js +270 -0
- package/dist/agent-connections/thread-state.d.ts +41 -0
- package/dist/agent-connections/thread-state.js +157 -0
- package/dist/agent-connections/tools.d.ts +133 -0
- package/dist/agent-connections/tools.js +616 -0
- package/dist/agent-connections/types.d.ts +99 -0
- package/dist/agent-connections/types.js +4 -0
- package/dist/agent-connections/untrusted-text.d.ts +5 -0
- package/dist/agent-connections/untrusted-text.js +19 -0
- package/dist/agents/background-completion-events.d.ts +16 -0
- package/dist/agents/background-completion-events.js +16 -0
- package/dist/agents/background-completion.d.ts +22 -0
- package/dist/agents/background-completion.js +73 -0
- package/dist/agents/context-audit.d.ts +84 -0
- package/dist/agents/context-audit.js +115 -0
- package/dist/agents/credential-resolver.d.ts +74 -0
- package/dist/agents/credential-resolver.js +142 -0
- package/dist/agents/custom-provider-source.d.ts +35 -0
- package/dist/agents/custom-provider-source.js +22 -0
- package/dist/agents/fastembed-subprocess.d.ts +52 -0
- package/dist/agents/fastembed-subprocess.js +282 -0
- package/dist/agents/instructions.d.ts +42 -0
- package/dist/agents/instructions.js +69 -0
- package/dist/agents/mastracode-gateway.d.ts +85 -0
- package/dist/agents/mastracode-gateway.js +500 -0
- package/dist/agents/memory.d.ts +36 -0
- package/dist/agents/memory.js +248 -0
- package/dist/agents/model.d.ts +158 -0
- package/dist/agents/model.js +382 -0
- package/dist/agents/modes/build.d.ts +12 -0
- package/dist/agents/modes/build.js +33 -0
- package/dist/agents/modes/explore.d.ts +10 -0
- package/dist/agents/modes/explore.js +25 -0
- package/dist/agents/modes/plan.d.ts +8 -0
- package/dist/agents/modes/plan.js +43 -0
- package/dist/agents/prompts/agent-instructions.d.ts +122 -0
- package/dist/agents/prompts/agent-instructions.js +617 -0
- package/dist/agents/prompts/build.d.ts +12 -0
- package/dist/agents/prompts/build.js +73 -0
- package/dist/agents/prompts/fast.d.ts +5 -0
- package/dist/agents/prompts/fast.js +23 -0
- package/dist/agents/prompts/index.d.ts +61 -0
- package/dist/agents/prompts/index.js +141 -0
- package/dist/agents/prompts/model.d.ts +5 -0
- package/dist/agents/prompts/model.js +20 -0
- package/dist/agents/prompts/plan.d.ts +11 -0
- package/dist/agents/prompts/plan.js +132 -0
- package/dist/agents/prompts/tool-guidance.d.ts +18 -0
- package/dist/agents/prompts/tool-guidance.js +158 -0
- package/dist/agents/sandbox-filesystem.d.ts +232 -0
- package/dist/agents/sandbox-filesystem.js +851 -0
- package/dist/agents/subagents/execute.d.ts +3 -0
- package/dist/agents/subagents/execute.js +24 -0
- package/dist/agents/subagents/explore.d.ts +3 -0
- package/dist/agents/subagents/explore.js +15 -0
- package/dist/agents/subagents/index.d.ts +6 -0
- package/dist/agents/subagents/index.js +15 -0
- package/dist/agents/subagents/plan.d.ts +3 -0
- package/dist/agents/subagents/plan.js +15 -0
- package/dist/agents/thread-caveman-state.d.ts +17 -0
- package/dist/agents/thread-caveman-state.js +104 -0
- package/dist/agents/tool-availability.d.ts +17 -0
- package/dist/agents/tool-availability.js +134 -0
- package/dist/agents/tools.d.ts +37 -0
- package/dist/agents/tools.js +154 -0
- package/dist/agents/workflow-builder-agent.d.ts +2 -0
- package/dist/agents/workflow-builder-agent.js +79 -0
- package/dist/agents/workspace.d.ts +35 -0
- package/dist/agents/workspace.js +207 -0
- package/dist/analytics.d.ts +18 -0
- package/dist/analytics.js +159 -0
- package/dist/auth/account-rotation-processor.d.ts +210 -0
- package/dist/auth/account-rotation-processor.js +662 -0
- package/dist/auth/account-routing-context.d.ts +17 -0
- package/dist/auth/account-routing-context.js +38 -0
- package/dist/auth/authorization-input.d.ts +16 -0
- package/dist/auth/authorization-input.js +39 -0
- package/dist/auth/device-code.d.ts +93 -0
- package/dist/auth/device-code.js +126 -0
- package/dist/auth/index.d.ts +13 -0
- package/dist/auth/index.js +9 -0
- package/dist/auth/pkce.d.ts +13 -0
- package/dist/auth/pkce.js +29 -0
- package/dist/auth/provider-auth-error.d.ts +24 -0
- package/dist/auth/provider-auth-error.js +44 -0
- package/dist/auth/providers/anthropic.d.ts +40 -0
- package/dist/auth/providers/anthropic.js +117 -0
- package/dist/auth/providers/github-copilot.d.ts +154 -0
- package/dist/auth/providers/github-copilot.js +359 -0
- package/dist/auth/providers/kimi-coding.d.ts +38 -0
- package/dist/auth/providers/kimi-coding.js +225 -0
- package/dist/auth/providers/openai-codex.d.ts +141 -0
- package/dist/auth/providers/openai-codex.js +531 -0
- package/dist/auth/providers/xai.d.ts +64 -0
- package/dist/auth/providers/xai.js +223 -0
- package/dist/auth/storage.d.ts +222 -0
- package/dist/auth/storage.js +805 -0
- package/dist/auth/types.d.ts +164 -0
- package/dist/auth/types.js +1 -0
- package/dist/clipboard/index.d.ts +26 -0
- package/dist/clipboard/index.js +224 -0
- package/dist/constants.d.ts +29 -0
- package/dist/constants.js +34 -0
- package/dist/error-classification.d.ts +10 -0
- package/dist/error-classification.js +19 -0
- package/dist/evals/context-builder.d.ts +41 -0
- package/dist/evals/context-builder.js +156 -0
- package/dist/evals/index.d.ts +4 -0
- package/dist/evals/index.js +5 -0
- package/dist/evals/scorers/classify-command.d.ts +30 -0
- package/dist/evals/scorers/classify-command.js +61 -0
- package/dist/evals/scorers/efficiency.d.ts +44 -0
- package/dist/evals/scorers/efficiency.js +256 -0
- package/dist/evals/scorers/extract-tools.d.ts +25 -0
- package/dist/evals/scorers/extract-tools.js +57 -0
- package/dist/evals/scorers/index.d.ts +3 -0
- package/dist/evals/scorers/index.js +3 -0
- package/dist/evals/scorers/outcome.d.ts +49 -0
- package/dist/evals/scorers/outcome.js +360 -0
- package/dist/experimental-agent.d.ts +19 -0
- package/dist/experimental-agent.js +45 -0
- package/dist/factory/client.d.ts +44 -0
- package/dist/factory/client.js +110 -0
- package/dist/factory/remote-controller.d.ts +34 -0
- package/dist/factory/remote-controller.js +40 -0
- package/dist/factory/remote-session.d.ts +210 -0
- package/dist/factory/remote-session.js +415 -0
- package/dist/goal-manager.d.ts +114 -0
- package/dist/goal-manager.js +351 -0
- package/dist/goal-signal.d.ts +14 -0
- package/dist/goal-signal.js +14 -0
- package/dist/headless/cli.d.ts +55 -0
- package/dist/headless/cli.js +261 -0
- package/dist/headless/flags.d.ts +33 -0
- package/dist/headless/flags.js +207 -0
- package/dist/headless/format.d.ts +41 -0
- package/dist/headless/format.js +94 -0
- package/dist/headless/index.d.ts +25 -0
- package/dist/headless/index.js +6 -0
- package/dist/headless/policy.d.ts +17 -0
- package/dist/headless/policy.js +39 -0
- package/dist/headless/run-mc.d.ts +7 -0
- package/dist/headless/run-mc.js +438 -0
- package/dist/headless/types.d.ts +175 -0
- package/dist/headless/types.js +16 -0
- package/dist/hooks/config.d.ts +11 -0
- package/dist/hooks/config.js +77 -0
- package/dist/hooks/executor.d.ts +13 -0
- package/dist/hooks/executor.js +128 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/manager.d.ts +75 -0
- package/dist/hooks/manager.js +289 -0
- package/dist/hooks/types.d.ts +180 -0
- package/dist/hooks/types.js +5 -0
- package/dist/index.d.ts +537 -0
- package/dist/index.js +1399 -0
- package/dist/ipc/ipc-reporter.d.ts +14 -0
- package/dist/ipc/ipc-reporter.js +18 -0
- package/dist/knowledge-inspector.d.ts +139 -0
- package/dist/knowledge-inspector.js +533 -0
- package/dist/knowledge-scope-id.d.ts +7 -0
- package/dist/knowledge-scope-id.js +10 -0
- package/dist/knowledge-scope.d.ts +41 -0
- package/dist/knowledge-scope.js +85 -0
- package/dist/lsp/client.d.ts +56 -0
- package/dist/lsp/client.js +248 -0
- package/dist/lsp/index.d.ts +4 -0
- package/dist/lsp/index.js +4 -0
- package/dist/lsp/language.d.ts +9 -0
- package/dist/lsp/language.js +41 -0
- package/dist/lsp/manager.d.ts +34 -0
- package/dist/lsp/manager.js +79 -0
- package/dist/lsp/server.d.ts +27 -0
- package/dist/lsp/server.js +158 -0
- package/dist/lsp/workspace.d.ts +9 -0
- package/dist/lsp/workspace.js +52 -0
- package/dist/mcp/config.d.ts +77 -0
- package/dist/mcp/config.js +313 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/manager.d.ts +104 -0
- package/dist/mcp/manager.js +693 -0
- package/dist/mcp/state.d.ts +35 -0
- package/dist/mcp/state.js +94 -0
- package/dist/mcp/types.d.ts +139 -0
- package/dist/mcp/types.js +1 -0
- package/dist/onboarding/custom-packs.d.ts +7 -0
- package/dist/onboarding/custom-packs.js +17 -0
- package/dist/onboarding/custom-providers.d.ts +4 -0
- package/dist/onboarding/custom-providers.js +27 -0
- package/dist/onboarding/index.d.ts +5 -0
- package/dist/onboarding/index.js +3 -0
- package/dist/onboarding/om-settings.d.ts +47 -0
- package/dist/onboarding/om-settings.js +80 -0
- package/dist/onboarding/pack-activation.d.ts +43 -0
- package/dist/onboarding/pack-activation.js +105 -0
- package/dist/onboarding/packs.d.ts +106 -0
- package/dist/onboarding/packs.js +283 -0
- package/dist/onboarding/settings.d.ts +563 -0
- package/dist/onboarding/settings.js +1110 -0
- package/dist/permissions.d.ts +55 -0
- package/dist/permissions.js +139 -0
- package/dist/platform/index.d.ts +74 -0
- package/dist/platform/index.js +199 -0
- package/dist/plugin.d.ts +156 -0
- package/dist/plugin.js +27 -0
- package/dist/plugins/dependencies.d.ts +9 -0
- package/dist/plugins/dependencies.js +84 -0
- package/dist/plugins/install.d.ts +21 -0
- package/dist/plugins/install.js +187 -0
- package/dist/plugins/loader.d.ts +25 -0
- package/dist/plugins/loader.js +264 -0
- package/dist/plugins/manager.d.ts +115 -0
- package/dist/plugins/manager.js +507 -0
- package/dist/plugins/manifest.d.ts +14 -0
- package/dist/plugins/manifest.js +47 -0
- package/dist/plugins/package-link.d.ts +3 -0
- package/dist/plugins/package-link.js +58 -0
- package/dist/plugins/paths.d.ts +10 -0
- package/dist/plugins/paths.js +22 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +109 -0
- package/dist/plugins/scaffold.d.ts +10 -0
- package/dist/plugins/scaffold.js +115 -0
- package/dist/plugins/signal-lane.d.ts +59 -0
- package/dist/plugins/signal-lane.js +163 -0
- package/dist/plugins/types.d.ts +95 -0
- package/dist/plugins/types.js +1 -0
- package/dist/process-memory-diagnostics.d.ts +136 -0
- package/dist/process-memory-diagnostics.js +497 -0
- package/dist/processors/plan-rejection-abort.d.ts +16 -0
- package/dist/processors/plan-rejection-abort.js +62 -0
- package/dist/providers/amazon-bedrock-gateway.d.ts +47 -0
- package/dist/providers/amazon-bedrock-gateway.js +142 -0
- package/dist/providers/amazon-bedrock.d.ts +28 -0
- package/dist/providers/amazon-bedrock.js +99 -0
- package/dist/providers/anthropic-prompt-cache.d.ts +19 -0
- package/dist/providers/anthropic-prompt-cache.js +8 -0
- package/dist/providers/anthropic-thinking.d.ts +5 -0
- package/dist/providers/anthropic-thinking.js +22 -0
- package/dist/providers/claude-max.d.ts +67 -0
- package/dist/providers/claude-max.js +232 -0
- package/dist/providers/github-copilot-gateway.d.ts +35 -0
- package/dist/providers/github-copilot-gateway.js +58 -0
- package/dist/providers/github-copilot.d.ts +60 -0
- package/dist/providers/github-copilot.js +320 -0
- package/dist/providers/google-thinking.d.ts +31 -0
- package/dist/providers/google-thinking.js +64 -0
- package/dist/providers/google-vertex-gateway.d.ts +23 -0
- package/dist/providers/google-vertex-gateway.js +49 -0
- package/dist/providers/google-vertex.d.ts +6 -0
- package/dist/providers/google-vertex.js +51 -0
- package/dist/providers/kimi-coding.d.ts +14 -0
- package/dist/providers/kimi-coding.js +80 -0
- package/dist/providers/model-ids.d.ts +53 -0
- package/dist/providers/model-ids.js +132 -0
- package/dist/providers/openai-codex.d.ts +114 -0
- package/dist/providers/openai-codex.js +427 -0
- package/dist/providers/provider-thinking.d.ts +23 -0
- package/dist/providers/provider-thinking.js +61 -0
- package/dist/providers/thinking-request.d.ts +14 -0
- package/dist/providers/thinking-request.js +87 -0
- package/dist/providers/xai.d.ts +33 -0
- package/dist/providers/xai.js +72 -0
- package/dist/schedules/index.d.ts +10 -0
- package/dist/schedules/index.js +6 -0
- package/dist/schedules/interval.d.ts +49 -0
- package/dist/schedules/interval.js +212 -0
- package/dist/schedules/prompt.d.ts +36 -0
- package/dist/schedules/prompt.js +36 -0
- package/dist/schedules/run-script.d.ts +8 -0
- package/dist/schedules/run-script.js +80 -0
- package/dist/schedules/scheduler.d.ts +101 -0
- package/dist/schedules/scheduler.js +167 -0
- package/dist/schedules/tools.d.ts +57 -0
- package/dist/schedules/tools.js +217 -0
- package/dist/schema.d.ts +499 -0
- package/dist/schema.js +131 -0
- package/dist/theme-palette.d.ts +17 -0
- package/dist/theme-palette.js +18 -0
- package/dist/thinking-catalog.d.ts +4 -0
- package/dist/thinking-catalog.js +13 -0
- package/dist/thinking.d.ts +37 -0
- package/dist/thinking.js +134 -0
- package/dist/tool-names.d.ts +77 -0
- package/dist/tool-names.js +53 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/request-sandbox-access.d.ts +6 -0
- package/dist/tools/request-sandbox-access.js +80 -0
- package/dist/tools/utils.d.ts +20 -0
- package/dist/tools/utils.js +31 -0
- package/dist/tools/web-search.d.ts +116 -0
- package/dist/tools/web-search.js +158 -0
- package/dist/tools/workflows/create-workflow.d.ts +7 -0
- package/dist/tools/workflows/create-workflow.js +82 -0
- package/dist/tools/workflows/delete-workflow.d.ts +7 -0
- package/dist/tools/workflows/delete-workflow.js +18 -0
- package/dist/tools/workflows/ephemeral-memory.d.ts +28 -0
- package/dist/tools/workflows/ephemeral-memory.js +41 -0
- package/dist/tools/workflows/extract-json-schema.d.ts +12 -0
- package/dist/tools/workflows/extract-json-schema.js +19 -0
- package/dist/tools/workflows/get-workflow.d.ts +11 -0
- package/dist/tools/workflows/get-workflow.js +31 -0
- package/dist/tools/workflows/list-available-agents.d.ts +8 -0
- package/dist/tools/workflows/list-available-agents.js +29 -0
- package/dist/tools/workflows/list-available-classifiers.d.ts +19 -0
- package/dist/tools/workflows/list-available-classifiers.js +53 -0
- package/dist/tools/workflows/list-available-tools.d.ts +9 -0
- package/dist/tools/workflows/list-available-tools.js +37 -0
- package/dist/tools/workflows/list-available-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-available-workflows.js +29 -0
- package/dist/tools/workflows/list-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-workflows.js +30 -0
- package/dist/tools/workflows/run-workflow.d.ts +9 -0
- package/dist/tools/workflows/run-workflow.js +49 -0
- package/dist/tools/workflows/save-workflow.d.ts +1125 -0
- package/dist/tools/workflows/save-workflow.js +25 -0
- package/dist/tools/workflows/tool-ids.d.ts +16 -0
- package/dist/tools/workflows/tool-ids.js +17 -0
- package/dist/utils/binaries.d.ts +7 -0
- package/dist/utils/binaries.js +97 -0
- package/dist/utils/debug-log.d.ts +12 -0
- package/dist/utils/debug-log.js +56 -0
- package/dist/utils/discarding-scores-storage.d.ts +33 -0
- package/dist/utils/discarding-scores-storage.js +51 -0
- package/dist/utils/errors.d.ts +36 -0
- package/dist/utils/errors.js +215 -0
- package/dist/utils/gateway-sync.d.ts +26 -0
- package/dist/utils/gateway-sync.js +58 -0
- package/dist/utils/local-observability.d.ts +19 -0
- package/dist/utils/local-observability.js +54 -0
- package/dist/utils/maintenance-lock.d.ts +42 -0
- package/dist/utils/maintenance-lock.js +196 -0
- package/dist/utils/notification-dispatch.d.ts +74 -0
- package/dist/utils/notification-dispatch.js +128 -0
- package/dist/utils/open-url.d.ts +10 -0
- package/dist/utils/open-url.js +27 -0
- package/dist/utils/path-security.d.ts +2 -0
- package/dist/utils/path-security.js +7 -0
- package/dist/utils/plan-diff.d.ts +25 -0
- package/dist/utils/plan-diff.js +59 -0
- package/dist/utils/plans.d.ts +61 -0
- package/dist/utils/plans.js +120 -0
- package/dist/utils/project.d.ts +160 -0
- package/dist/utils/project.js +361 -0
- package/dist/utils/prune-cli.d.ts +24 -0
- package/dist/utils/prune-cli.js +124 -0
- package/dist/utils/signals-pubsub.d.ts +103 -0
- package/dist/utils/signals-pubsub.js +409 -0
- package/dist/utils/slash-command-loader.d.ts +52 -0
- package/dist/utils/slash-command-loader.js +139 -0
- package/dist/utils/slash-command-processor.d.ts +14 -0
- package/dist/utils/slash-command-processor.js +106 -0
- package/dist/utils/stdin-pipe.d.ts +33 -0
- package/dist/utils/stdin-pipe.js +93 -0
- package/dist/utils/storage-factory.d.ts +50 -0
- package/dist/utils/storage-factory.js +118 -0
- package/dist/utils/storage-maintenance.d.ts +121 -0
- package/dist/utils/storage-maintenance.js +352 -0
- package/dist/utils/thread-lock.d.ts +26 -0
- package/dist/utils/thread-lock.js +109 -0
- package/dist/utils/token-estimator.d.ts +3 -0
- package/dist/utils/token-estimator.js +19 -0
- package/dist/utils/update-check.d.ts +151 -0
- package/dist/utils/update-check.js +430 -0
- package/dist/utils/wake-session.d.ts +44 -0
- package/dist/utils/wake-session.js +83 -0
- package/dist/voice/stt-registry.d.ts +67 -0
- package/dist/voice/stt-registry.js +144 -0
- package/dist/voice/transcribe.d.ts +10 -0
- package/dist/voice/transcribe.js +75 -0
- package/dist/workflows/register-primitives.d.ts +52 -0
- package/dist/workflows/register-primitives.js +40 -0
- package/dist/workflows/service.d.ts +64 -0
- package/dist/workflows/service.js +50 -0
- package/package.json +117 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { CustomModelCatalogProvider } from '@mastra/core/agent-controller';
|
|
2
|
+
import { MastraModelGateway } from '@mastra/core/llm';
|
|
3
|
+
import type { GatewayAuthRequest, GatewayAuthResult, GatewayLanguageModel, MastraModelGatewayInterface, ProviderConfig } from '@mastra/core/llm';
|
|
4
|
+
import { AuthStorage } from '../auth/storage.js';
|
|
5
|
+
import type { CredentialStore } from '../auth/types.js';
|
|
6
|
+
import type { AnthropicPromptCacheScope } from '../providers/anthropic-prompt-cache.js';
|
|
7
|
+
import type { CodexServiceTier } from '../providers/model-ids.js';
|
|
8
|
+
import type { ThinkingLevel } from '../providers/openai-codex.js';
|
|
9
|
+
export { MASTRA_GATEWAY_PREFIX, OPENAI_PREFIX, remapOpenAIModelForCodexOAuth, stripMastraGatewayPrefix, } from '../providers/model-ids.js';
|
|
10
|
+
export declare const MASTRACODE_GATEWAY_ID = "mastracode";
|
|
11
|
+
export type MastraCodeCustomProvider = {
|
|
12
|
+
name: string;
|
|
13
|
+
url: string;
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
models?: string[];
|
|
16
|
+
};
|
|
17
|
+
export type MastraCodeGatewayOptions = {
|
|
18
|
+
mastraGatewayBaseUrl: string;
|
|
19
|
+
mastraGatewayApiKey?: string;
|
|
20
|
+
routeThroughMastraGateway: boolean;
|
|
21
|
+
thinkingLevel?: ThinkingLevel;
|
|
22
|
+
/** Anthropic prompt-cache breakpoints to write. Defaults to `conversation`. */
|
|
23
|
+
anthropicPromptCacheScope?: AnthropicPromptCacheScope;
|
|
24
|
+
customProviders?: MastraCodeCustomProvider[];
|
|
25
|
+
settingsPath?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Per-tenant credential source for this gateway instance. Defaults to the
|
|
28
|
+
* server-global file-backed `AuthStorage`; deployed web passes the calling
|
|
29
|
+
* tenant's store so model calls use the caller's own credentials.
|
|
30
|
+
*/
|
|
31
|
+
credentialStore?: CredentialStore;
|
|
32
|
+
};
|
|
33
|
+
export declare function getGlobalAuthStorage(): AuthStorage;
|
|
34
|
+
export declare function reloadAuthStorage(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Resolve the Anthropic API key.
|
|
37
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getAnthropicApiKey(credentials?: CredentialStore): string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Resolve the OpenAI API key.
|
|
42
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getOpenAIApiKey(credentials?: CredentialStore): string | undefined;
|
|
45
|
+
export declare function resolveAuth(request: GatewayAuthRequest, mastraGatewayApiKey?: string): GatewayAuthResult | undefined;
|
|
46
|
+
export declare function providerApiKeyEnvVar(providerId: string): string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Whether the process environment has a key for a models.dev provider. Checks every
|
|
49
|
+
* env var the registry lists, as the models.dev gateway does when `resolveModel`
|
|
50
|
+
* gets no gateway credential (e.g. `GOOGLE_API_KEY` or `GOOGLE_GENERATIVE_AI_API_KEY`).
|
|
51
|
+
*/
|
|
52
|
+
export declare function hasProviderEnvironmentKey(providerId: string): boolean;
|
|
53
|
+
export declare class MastraCodeGateway extends MastraModelGateway {
|
|
54
|
+
#private;
|
|
55
|
+
readonly id = "mastracode";
|
|
56
|
+
readonly name = "MastraCode Gateway";
|
|
57
|
+
constructor({ mastraGatewayBaseUrl, mastraGatewayApiKey, routeThroughMastraGateway, thinkingLevel, anthropicPromptCacheScope, customProviders, settingsPath, credentialStore, }: MastraCodeGatewayOptions);
|
|
58
|
+
static getMastraGatewayApiKey(): string | undefined;
|
|
59
|
+
/** @deprecated Renamed to {@link MastraCodeGateway.getMastraGatewayApiKey}. */
|
|
60
|
+
static getMemoryGatewayApiKey(): string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Claim an unprefixed model id (e.g. a bare `anthropic/...`) when this gateway
|
|
63
|
+
* can authenticate it via OAuth or a stored/env credential. This lets the
|
|
64
|
+
* shared gateway router select MastraCode for auth resolution, so the status
|
|
65
|
+
* bar and `/api-keys` reflect OAuth logins instead of only checking env vars.
|
|
66
|
+
*/
|
|
67
|
+
handlesModel(selectionId: string): boolean;
|
|
68
|
+
static resolveProviderAuth(request: GatewayAuthRequest, mastraGatewayApiKey?: string, credentials?: CredentialStore): GatewayAuthResult | undefined;
|
|
69
|
+
static createModelCatalogProvider(gateway: MastraModelGatewayInterface): CustomModelCatalogProvider;
|
|
70
|
+
createModelCatalogProvider(): CustomModelCatalogProvider;
|
|
71
|
+
fetchProviders(): Promise<Record<string, ProviderConfig>>;
|
|
72
|
+
buildUrl(modelId: string): string | undefined | Promise<string | undefined>;
|
|
73
|
+
getApiKey(modelId: string): Promise<string>;
|
|
74
|
+
resolveAuth(request: GatewayAuthRequest): GatewayAuthResult | undefined;
|
|
75
|
+
resolveLanguageModel(args: {
|
|
76
|
+
modelId: string;
|
|
77
|
+
providerId: string;
|
|
78
|
+
apiKey: string;
|
|
79
|
+
headers?: Record<string, string>;
|
|
80
|
+
transport?: any;
|
|
81
|
+
responsesWebSocket?: any;
|
|
82
|
+
codexServiceTier?: CodexServiceTier;
|
|
83
|
+
}): GatewayLanguageModel;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=mastracode-gateway.d.ts.map
|
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import { AuthStorage } from "../auth/storage.js";
|
|
2
|
+
import { getCustomProviderId, loadSettings } from "../onboarding/settings.js";
|
|
3
|
+
import { buildAnthropicOAuthFetch, claudeCodeMiddleware, createAnthropicThinkingMiddleware, createPromptCacheMiddleware, opencodeClaudeMaxProvider } from "../providers/claude-max.js";
|
|
4
|
+
import { githubCopilotProvider } from "../providers/github-copilot.js";
|
|
5
|
+
import { createGoogleThinkingMiddleware } from "../providers/google-thinking.js";
|
|
6
|
+
import { kimiCodingProvider } from "../providers/kimi-coding.js";
|
|
7
|
+
import { MASTRA_GATEWAY_PREFIX, OPENAI_PREFIX, getCredentialProviderId, normalizeAnthropicModelId, parseCodexModelSelection, remapOpenAIModelForCodexOAuth, stripMastraGatewayPrefix } from "../providers/model-ids.js";
|
|
8
|
+
import { THINKING_LEVEL_TO_REASONING_EFFORT, buildOpenAICodexOAuthFetch, createCodexMiddleware, createPromptCacheKeyMiddleware, createReasoningEffortMiddleware, getEffectiveThinkingLevel, getRequestThreadId, openaiCodexProvider } from "../providers/openai-codex.js";
|
|
9
|
+
import { ModelRouterLanguageModelWithThinking, providerThinkingOptions, withThinkingOptionsModel } from "../providers/provider-thinking.js";
|
|
10
|
+
import { xaiProvider } from "../providers/xai.js";
|
|
11
|
+
import { getAppDataDir } from "../utils/project.js";
|
|
12
|
+
import { resolveCustomProviders } from "./custom-provider-source.js";
|
|
13
|
+
import { GATEWAY_AUTH_HEADER, MastraGateway, MastraModelGateway, ModelRouterLanguageModel, PROVIDER_REGISTRY } from "@mastra/core/llm";
|
|
14
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
15
|
+
import { wrapLanguageModel } from "ai";
|
|
16
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
17
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
18
|
+
// src/agents/mastracode-gateway.ts
|
|
19
|
+
const PROVIDERS = PROVIDER_REGISTRY;
|
|
20
|
+
const MASTRACODE_GATEWAY_ID = "mastracode";
|
|
21
|
+
/** Gateway request header. `false` lets the gateway drop prompt cache controls no route can serve instead of refusing the request. */
|
|
22
|
+
const GATEWAY_STRICT_CAPABILITIES_HEADER = "x-gateway-strict-capabilities";
|
|
23
|
+
/** Organization keys reach the gateway that leaves memory, and so the prompt, to the client. */
|
|
24
|
+
function isOrganizationGatewayKey(apiKey) {
|
|
25
|
+
return apiKey.startsWith("msk-gateway-") || apiKey.startsWith("mskv2_");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Global file-backed credential store for local (TUI) model resolution.
|
|
29
|
+
*
|
|
30
|
+
* Constructed lazily, NOT at module scope: `AuthStorage`'s default path is
|
|
31
|
+
* resolved from `MASTRA_APP_DATA_DIR` at construction, and this module is
|
|
32
|
+
* routinely imported before test harnesses / embedders point that variable at
|
|
33
|
+
* an isolated directory — an import-time construction would permanently pin
|
|
34
|
+
* the developer's real app-data dir for every credential read.
|
|
35
|
+
*/
|
|
36
|
+
let authStorageSingleton;
|
|
37
|
+
let authStorageSingletonDir;
|
|
38
|
+
function getGlobalAuthStorage() {
|
|
39
|
+
const dir = getAppDataDir();
|
|
40
|
+
if (!authStorageSingleton || authStorageSingletonDir !== dir) {
|
|
41
|
+
authStorageSingleton = new AuthStorage();
|
|
42
|
+
authStorageSingletonDir = dir;
|
|
43
|
+
}
|
|
44
|
+
return authStorageSingleton;
|
|
45
|
+
}
|
|
46
|
+
function reloadAuthStorage() {
|
|
47
|
+
getGlobalAuthStorage().reload();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Resolve the Anthropic API key.
|
|
51
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
52
|
+
*/
|
|
53
|
+
function getAnthropicApiKey(credentials = getGlobalAuthStorage()) {
|
|
54
|
+
const storedCred = credentials.get("anthropic");
|
|
55
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
56
|
+
const dedicatedKey = credentials.getStoredApiKey("anthropic")?.trim();
|
|
57
|
+
if (dedicatedKey) return dedicatedKey;
|
|
58
|
+
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.ANTHROPIC_API_KEY?.trim() || void 0;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve the OpenAI API key.
|
|
62
|
+
* Main slot → dedicated apikey: slot → env var.
|
|
63
|
+
*/
|
|
64
|
+
function getOpenAIApiKey(credentials = getGlobalAuthStorage()) {
|
|
65
|
+
const storedCred = credentials.get("openai-codex");
|
|
66
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
67
|
+
const dedicatedKey = credentials.getStoredApiKey("openai-codex")?.trim();
|
|
68
|
+
if (dedicatedKey) return dedicatedKey;
|
|
69
|
+
return credentials.allowEnvironmentFallback === false ? void 0 : process.env.OPENAI_API_KEY?.trim() || void 0;
|
|
70
|
+
}
|
|
71
|
+
function anthropicApiKeyProvider(modelId, apiKey, headers, thinkingLevel, promptCacheScope) {
|
|
72
|
+
const anthropic = createAnthropic({
|
|
73
|
+
apiKey,
|
|
74
|
+
headers
|
|
75
|
+
});
|
|
76
|
+
const thinkingMiddleware = createAnthropicThinkingMiddleware(modelId, thinkingLevel);
|
|
77
|
+
return wrapLanguageModel({
|
|
78
|
+
model: anthropic(modelId),
|
|
79
|
+
middleware: [createPromptCacheMiddleware(promptCacheScope), ...thinkingMiddleware ? [thinkingMiddleware] : []]
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function openaiApiKeyProvider(modelId, apiKey, headers, thinkingLevel) {
|
|
83
|
+
const openai = createOpenAI({
|
|
84
|
+
apiKey,
|
|
85
|
+
baseURL: process.env.OPENAI_BASE_URL,
|
|
86
|
+
headers
|
|
87
|
+
});
|
|
88
|
+
const middleware = thinkingLevel && thinkingLevel !== "off" ? createReasoningEffortMiddleware("openai", THINKING_LEVEL_TO_REASONING_EFFORT[getEffectiveThinkingLevel(modelId, thinkingLevel)]) : void 0;
|
|
89
|
+
const promptCacheKeyMiddleware = createPromptCacheKeyMiddleware(getRequestThreadId(headers));
|
|
90
|
+
return wrapLanguageModel({
|
|
91
|
+
model: openai.responses(modelId),
|
|
92
|
+
middleware: [...middleware ? [middleware] : [], ...promptCacheKeyMiddleware ? [promptCacheKeyMiddleware] : []]
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function getProviderAuthKey(providerId, credentials = getGlobalAuthStorage()) {
|
|
96
|
+
const authProviderId = getCredentialProviderId(providerId);
|
|
97
|
+
const storedCred = credentials.get(authProviderId);
|
|
98
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return storedCred.key.trim();
|
|
99
|
+
const dedicatedKey = credentials.getStoredApiKey(authProviderId)?.trim();
|
|
100
|
+
if (dedicatedKey) return dedicatedKey;
|
|
101
|
+
const catalogKey = authProviderId !== providerId ? credentials.getStoredApiKey(providerId)?.trim() : void 0;
|
|
102
|
+
if (catalogKey) return catalogKey;
|
|
103
|
+
if (credentials.allowEnvironmentFallback === false) return void 0;
|
|
104
|
+
return authProviderId === "kimi-for-coding" ? process.env.KIMI_API_KEY?.trim() || void 0 : void 0;
|
|
105
|
+
}
|
|
106
|
+
function resolveAuth(request, mastraGatewayApiKey) {
|
|
107
|
+
return MastraCodeGateway.resolveProviderAuth(request, mastraGatewayApiKey);
|
|
108
|
+
}
|
|
109
|
+
function getGatewayProviderKey(gatewayId, providerId) {
|
|
110
|
+
if (gatewayId === "models.dev") return providerId;
|
|
111
|
+
return providerId === gatewayId ? gatewayId : `${gatewayId}/${providerId}`;
|
|
112
|
+
}
|
|
113
|
+
function parseGatewayRouterId(routerId, gateway) {
|
|
114
|
+
const [firstPart = "", secondPart = "", ...restParts] = routerId.split("/");
|
|
115
|
+
const gatewayId = gateway.id;
|
|
116
|
+
if (firstPart === gatewayId && secondPart) return {
|
|
117
|
+
gatewayId,
|
|
118
|
+
providerId: secondPart,
|
|
119
|
+
modelId: restParts.join("/")
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
gatewayId,
|
|
123
|
+
providerId: firstPart,
|
|
124
|
+
modelId: [secondPart, ...restParts].filter(Boolean).join("/")
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function hasResolvedAuth(auth) {
|
|
128
|
+
if (!auth) return false;
|
|
129
|
+
if (auth.apiKey || auth.bearerToken) return true;
|
|
130
|
+
return auth.headers ? Object.keys(auth.headers).length > 0 : false;
|
|
131
|
+
}
|
|
132
|
+
async function resolveGatewayProviderAuth(gateway, routerId) {
|
|
133
|
+
if (!gateway.resolveAuth) return void 0;
|
|
134
|
+
const parsed = parseGatewayRouterId(routerId, gateway);
|
|
135
|
+
try {
|
|
136
|
+
const result = await gateway.resolveAuth({
|
|
137
|
+
gatewayId: parsed.gatewayId,
|
|
138
|
+
providerId: parsed.providerId,
|
|
139
|
+
modelId: parsed.modelId,
|
|
140
|
+
routerId
|
|
141
|
+
});
|
|
142
|
+
return hasResolvedAuth(result) ? result : void 0;
|
|
143
|
+
} catch {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async function getMastraCodeProviderConfigs(gateway) {
|
|
148
|
+
const providers = { ...PROVIDERS };
|
|
149
|
+
try {
|
|
150
|
+
const gatewayProviders = await gateway.fetchProviders();
|
|
151
|
+
for (const [providerId, config] of Object.entries(gatewayProviders)) providers[getGatewayProviderKey(gateway.id, providerId)] = {
|
|
152
|
+
...config,
|
|
153
|
+
gateway: gateway.id
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.warn(`Failed to load providers from gateway ${gateway.id}:`, error);
|
|
157
|
+
}
|
|
158
|
+
return providers;
|
|
159
|
+
}
|
|
160
|
+
function getApiKeyEnvVar(providerConfig) {
|
|
161
|
+
const envVars = providerConfig?.apiKeyEnvVar;
|
|
162
|
+
return Array.isArray(envVars) ? envVars[0] : envVars;
|
|
163
|
+
}
|
|
164
|
+
function providerApiKeyEnvVar(providerId) {
|
|
165
|
+
return getApiKeyEnvVar(PROVIDERS[providerId]);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Whether the process environment has a key for a models.dev provider. Checks every
|
|
169
|
+
* env var the registry lists, as the models.dev gateway does when `resolveModel`
|
|
170
|
+
* gets no gateway credential (e.g. `GOOGLE_API_KEY` or `GOOGLE_GENERATIVE_AI_API_KEY`).
|
|
171
|
+
*/
|
|
172
|
+
function hasProviderEnvironmentKey(providerId) {
|
|
173
|
+
const envVars = PROVIDERS[providerId]?.apiKeyEnvVar;
|
|
174
|
+
if (!envVars) return false;
|
|
175
|
+
return (Array.isArray(envVars) ? envVars : [envVars]).some((envVar) => Boolean(process.env[envVar]?.trim()));
|
|
176
|
+
}
|
|
177
|
+
/** Servers call custom endpoints too: a redirect could aim that call at an address the saved URL never named. */
|
|
178
|
+
const fetchWithoutRedirects = (input, init) => fetch(input, {
|
|
179
|
+
...init,
|
|
180
|
+
redirect: "error"
|
|
181
|
+
});
|
|
182
|
+
var MastraCodeGateway = class MastraCodeGateway extends MastraModelGateway {
|
|
183
|
+
id = MASTRACODE_GATEWAY_ID;
|
|
184
|
+
name = "MastraCode Gateway";
|
|
185
|
+
#mastraGateway;
|
|
186
|
+
#mastraGatewayBaseUrl;
|
|
187
|
+
#mastraGatewayApiKey;
|
|
188
|
+
#routeThroughMastraGateway;
|
|
189
|
+
#thinkingLevel;
|
|
190
|
+
#anthropicPromptCacheScope;
|
|
191
|
+
#customProviders;
|
|
192
|
+
#settingsPath;
|
|
193
|
+
#credentials;
|
|
194
|
+
constructor({ mastraGatewayBaseUrl, mastraGatewayApiKey, routeThroughMastraGateway, thinkingLevel, anthropicPromptCacheScope, customProviders, settingsPath, credentialStore }) {
|
|
195
|
+
super();
|
|
196
|
+
this.#mastraGateway = new MastraGateway({ baseUrl: mastraGatewayBaseUrl });
|
|
197
|
+
this.#mastraGatewayBaseUrl = mastraGatewayBaseUrl;
|
|
198
|
+
this.#mastraGatewayApiKey = mastraGatewayApiKey;
|
|
199
|
+
this.#routeThroughMastraGateway = routeThroughMastraGateway;
|
|
200
|
+
this.#thinkingLevel = thinkingLevel;
|
|
201
|
+
this.#anthropicPromptCacheScope = anthropicPromptCacheScope ?? "conversation";
|
|
202
|
+
this.#customProviders = customProviders;
|
|
203
|
+
this.#settingsPath = settingsPath;
|
|
204
|
+
this.#credentials = credentialStore ?? getGlobalAuthStorage();
|
|
205
|
+
}
|
|
206
|
+
static getMastraGatewayApiKey() {
|
|
207
|
+
return getGlobalAuthStorage().getStoredApiKey("mastra-gateway") ?? process.env["MASTRA_GATEWAY_API_KEY"];
|
|
208
|
+
}
|
|
209
|
+
/** @deprecated Renamed to {@link MastraCodeGateway.getMastraGatewayApiKey}. */
|
|
210
|
+
static getMemoryGatewayApiKey() {
|
|
211
|
+
return MastraCodeGateway.getMastraGatewayApiKey();
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Claim an unprefixed model id (e.g. a bare `anthropic/...`) when this gateway
|
|
215
|
+
* can authenticate it via OAuth or a stored/env credential. This lets the
|
|
216
|
+
* shared gateway router select MastraCode for auth resolution, so the status
|
|
217
|
+
* bar and `/api-keys` reflect OAuth logins instead of only checking env vars.
|
|
218
|
+
*/
|
|
219
|
+
handlesModel(selectionId) {
|
|
220
|
+
const { modelId } = parseCodexModelSelection(selectionId);
|
|
221
|
+
const parsed = parseGatewayRouterId(modelId, this);
|
|
222
|
+
if (!parsed.providerId || !parsed.modelId) return false;
|
|
223
|
+
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) return true;
|
|
224
|
+
if (this.#customProvider(parsed.providerId)) return true;
|
|
225
|
+
return hasResolvedAuth(MastraCodeGateway.resolveProviderAuth({
|
|
226
|
+
gatewayId: this.id,
|
|
227
|
+
providerId: parsed.providerId,
|
|
228
|
+
modelId: parsed.modelId,
|
|
229
|
+
routerId: modelId
|
|
230
|
+
}, void 0, this.#credentials));
|
|
231
|
+
}
|
|
232
|
+
static resolveProviderAuth(request, mastraGatewayApiKey, credentials = getGlobalAuthStorage()) {
|
|
233
|
+
if (request.gatewayId === "mastra" && mastraGatewayApiKey) return {
|
|
234
|
+
apiKey: mastraGatewayApiKey,
|
|
235
|
+
source: "gateway"
|
|
236
|
+
};
|
|
237
|
+
const authProviderId = getCredentialProviderId(request.providerId);
|
|
238
|
+
if (credentials.get(authProviderId)?.type === "oauth") return {
|
|
239
|
+
bearerToken: "oauth",
|
|
240
|
+
source: "gateway"
|
|
241
|
+
};
|
|
242
|
+
const apiKey = getProviderAuthKey(request.providerId, credentials);
|
|
243
|
+
return apiKey ? {
|
|
244
|
+
apiKey,
|
|
245
|
+
source: "gateway"
|
|
246
|
+
} : void 0;
|
|
247
|
+
}
|
|
248
|
+
static createModelCatalogProvider(gateway) {
|
|
249
|
+
return async () => {
|
|
250
|
+
const registry = await getMastraCodeProviderConfigs(gateway);
|
|
251
|
+
const models = [];
|
|
252
|
+
for (const [provider, providerConfig] of Object.entries(registry)) {
|
|
253
|
+
const apiKeyEnvVar = getApiKeyEnvVar(providerConfig);
|
|
254
|
+
const hasEnvKey = apiKeyEnvVar ? Boolean(process.env[apiKeyEnvVar]) : false;
|
|
255
|
+
const modelNames = providerConfig.models;
|
|
256
|
+
if (!Array.isArray(modelNames)) continue;
|
|
257
|
+
const gatewayAuth = modelNames[0] ? await resolveGatewayProviderAuth(gateway, `${provider}/${modelNames[0]}`) : void 0;
|
|
258
|
+
for (const modelName of modelNames) {
|
|
259
|
+
const id = `${provider}/${modelName}`;
|
|
260
|
+
models.push({
|
|
261
|
+
id,
|
|
262
|
+
provider,
|
|
263
|
+
modelName,
|
|
264
|
+
hasApiKey: hasEnvKey || Boolean(gatewayAuth),
|
|
265
|
+
apiKeyEnvVar: apiKeyEnvVar || void 0
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return models;
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
createModelCatalogProvider() {
|
|
273
|
+
return MastraCodeGateway.createModelCatalogProvider(this);
|
|
274
|
+
}
|
|
275
|
+
#getCustomProviders() {
|
|
276
|
+
return this.#customProviders ?? resolveCustomProviders() ?? loadSettings(this.#settingsPath).customProviders;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The custom provider a provider id refers to, if any. Every method that
|
|
280
|
+
* resolves a model or its credentials goes through this lookup, so a custom
|
|
281
|
+
* endpoint only ever authenticates with its own key: it is never handed a
|
|
282
|
+
* credential saved for another provider, even when the ids collide. A model
|
|
283
|
+
* routed through the Mastra gateway is never a custom provider.
|
|
284
|
+
*/
|
|
285
|
+
#customProvider(providerId) {
|
|
286
|
+
if (this.#routeThroughMastraGateway) return void 0;
|
|
287
|
+
return this.#getCustomProviders().find((provider) => providerId === getCustomProviderId(provider.name));
|
|
288
|
+
}
|
|
289
|
+
async fetchProviders() {
|
|
290
|
+
this.#credentials.reload();
|
|
291
|
+
const providers = {};
|
|
292
|
+
for (const provider of this.#getCustomProviders()) {
|
|
293
|
+
const models = provider.models ?? [];
|
|
294
|
+
if (!models.length) continue;
|
|
295
|
+
providers[getCustomProviderId(provider.name)] = {
|
|
296
|
+
name: provider.name,
|
|
297
|
+
url: provider.url,
|
|
298
|
+
apiKeyEnvVar: "",
|
|
299
|
+
apiKeyHeader: "Authorization",
|
|
300
|
+
gateway: this.id,
|
|
301
|
+
models
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return providers;
|
|
305
|
+
}
|
|
306
|
+
buildUrl(modelId) {
|
|
307
|
+
return this.#routeThroughMastraGateway ? this.#mastraGateway.buildUrl(modelId) : modelId;
|
|
308
|
+
}
|
|
309
|
+
async getApiKey(modelId) {
|
|
310
|
+
const { providerId } = parseGatewayRouterId(stripMastraGatewayPrefix(modelId), this);
|
|
311
|
+
if (this.#routeThroughMastraGateway) return this.#mastraGatewayApiKey ?? "";
|
|
312
|
+
if (!providerId) return "";
|
|
313
|
+
const customProvider = this.#customProvider(providerId);
|
|
314
|
+
if (customProvider) return customProvider.apiKey ?? "";
|
|
315
|
+
return getProviderAuthKey(providerId, this.#credentials) ?? "";
|
|
316
|
+
}
|
|
317
|
+
resolveAuth(request) {
|
|
318
|
+
if (this.#routeThroughMastraGateway && this.#mastraGatewayApiKey) return {
|
|
319
|
+
apiKey: this.#mastraGatewayApiKey,
|
|
320
|
+
source: "gateway"
|
|
321
|
+
};
|
|
322
|
+
const customProvider = this.#customProvider(request.providerId);
|
|
323
|
+
if (customProvider) return customProvider.apiKey ? {
|
|
324
|
+
apiKey: customProvider.apiKey,
|
|
325
|
+
source: "gateway"
|
|
326
|
+
} : { source: "gateway" };
|
|
327
|
+
return MastraCodeGateway.resolveProviderAuth(request, void 0, this.#credentials);
|
|
328
|
+
}
|
|
329
|
+
resolveLanguageModel(args) {
|
|
330
|
+
const customProvider = this.#customProvider(args.providerId);
|
|
331
|
+
const selection = customProvider ? void 0 : parseCodexModelSelection(`${args.providerId}/${args.modelId}`);
|
|
332
|
+
if (selection && selection.modelId !== `${args.providerId}/${args.modelId}`) args = {
|
|
333
|
+
...args,
|
|
334
|
+
modelId: selection.modelId.slice(OPENAI_PREFIX.length),
|
|
335
|
+
...selection.codexServiceTier ? { codexServiceTier: selection.codexServiceTier } : {}
|
|
336
|
+
};
|
|
337
|
+
if (customProvider) {
|
|
338
|
+
const provider = createOpenAICompatible({
|
|
339
|
+
name: args.providerId,
|
|
340
|
+
baseURL: customProvider.url,
|
|
341
|
+
apiKey: args.apiKey,
|
|
342
|
+
headers: args.headers,
|
|
343
|
+
fetch: fetchWithoutRedirects
|
|
344
|
+
});
|
|
345
|
+
const reasoningEffort = this.#thinkingLevel ? THINKING_LEVEL_TO_REASONING_EFFORT[this.#thinkingLevel] : void 0;
|
|
346
|
+
const middleware = createReasoningEffortMiddleware(args.providerId, reasoningEffort);
|
|
347
|
+
if (!middleware) return provider.chatModel(args.modelId);
|
|
348
|
+
return wrapLanguageModel({
|
|
349
|
+
model: provider.chatModel(args.modelId),
|
|
350
|
+
middleware: [middleware]
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (args.providerId === "github-copilot") return githubCopilotProvider(args.modelId, {
|
|
354
|
+
headers: args.headers,
|
|
355
|
+
authStorage: this.#credentials
|
|
356
|
+
});
|
|
357
|
+
if (args.providerId === "moonshotai" && !this.#routeThroughMastraGateway) {
|
|
358
|
+
const apiKey = args.apiKey?.trim() || process.env.MOONSHOT_API_KEY?.trim() || process.env.MOONSHOT_AI_API_KEY?.trim();
|
|
359
|
+
if (!apiKey) throw new Error("Need MOONSHOT_API_KEY");
|
|
360
|
+
return createAnthropic({
|
|
361
|
+
apiKey,
|
|
362
|
+
baseURL: "https://api.moonshot.ai/anthropic/v1",
|
|
363
|
+
name: "moonshotai.anthropicv1",
|
|
364
|
+
headers: args.headers
|
|
365
|
+
})(args.modelId);
|
|
366
|
+
}
|
|
367
|
+
if (args.providerId === "anthropic") return this.#resolveAnthropicModel(args);
|
|
368
|
+
if (args.providerId === "openai") {
|
|
369
|
+
const openaiModel = this.#resolveOpenAIModel(args);
|
|
370
|
+
if (openaiModel) return openaiModel;
|
|
371
|
+
}
|
|
372
|
+
if (args.providerId === "xai" && this.#credentials.get("xai")?.type === "oauth") return withThinkingOptionsModel(xaiProvider(args.modelId, {
|
|
373
|
+
headers: args.headers,
|
|
374
|
+
authStorage: this.#credentials
|
|
375
|
+
}), providerThinkingOptions(`xai/${args.modelId}`, this.#thinkingLevel));
|
|
376
|
+
if (this.#routeThroughMastraGateway) return this.#mastraGateway.resolveLanguageModel(args);
|
|
377
|
+
if (args.providerId === "google") return this.#resolveGoogleModel(args);
|
|
378
|
+
if (args.providerId === "kimi-code-plan-cn" || args.providerId === "kimi-for-coding") return kimiCodingProvider(args.modelId, {
|
|
379
|
+
apiKey: args.apiKey,
|
|
380
|
+
headers: args.headers,
|
|
381
|
+
credentialStore: this.#credentials
|
|
382
|
+
});
|
|
383
|
+
const routedModelId = `${args.providerId}/${args.modelId}`;
|
|
384
|
+
return new ModelRouterLanguageModelWithThinking({
|
|
385
|
+
id: routedModelId,
|
|
386
|
+
apiKey: args.apiKey,
|
|
387
|
+
headers: args.headers
|
|
388
|
+
}, providerThinkingOptions(routedModelId, this.#thinkingLevel));
|
|
389
|
+
}
|
|
390
|
+
#resolveAnthropicModel(args) {
|
|
391
|
+
const bareModelId = normalizeAnthropicModelId(args.modelId);
|
|
392
|
+
const storedCred = this.#credentials.get("anthropic");
|
|
393
|
+
const thinkingMiddleware = createAnthropicThinkingMiddleware(bareModelId, this.#thinkingLevel);
|
|
394
|
+
if (this.#routeThroughMastraGateway) {
|
|
395
|
+
if (storedCred?.type === "oauth") {
|
|
396
|
+
const anthropic = createAnthropic({
|
|
397
|
+
apiKey: "oauth-gateway-placeholder",
|
|
398
|
+
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
399
|
+
headers: {
|
|
400
|
+
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
401
|
+
...args.headers
|
|
402
|
+
},
|
|
403
|
+
fetch: buildAnthropicOAuthFetch({ authStorage: this.#credentials })
|
|
404
|
+
});
|
|
405
|
+
return wrapLanguageModel({
|
|
406
|
+
model: anthropic(bareModelId),
|
|
407
|
+
middleware: [
|
|
408
|
+
claudeCodeMiddleware,
|
|
409
|
+
createPromptCacheMiddleware(this.#anthropicPromptCacheScope),
|
|
410
|
+
...thinkingMiddleware ? [thinkingMiddleware] : []
|
|
411
|
+
]
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
if (!isOrganizationGatewayKey(args.apiKey)) return this.#mastraGateway.resolveLanguageModel({
|
|
415
|
+
...args,
|
|
416
|
+
modelId: bareModelId
|
|
417
|
+
});
|
|
418
|
+
return wrapLanguageModel({
|
|
419
|
+
model: this.#mastraGateway.resolveLanguageModel({
|
|
420
|
+
...args,
|
|
421
|
+
modelId: bareModelId,
|
|
422
|
+
headers: {
|
|
423
|
+
...args.headers,
|
|
424
|
+
[GATEWAY_STRICT_CAPABILITIES_HEADER]: "false"
|
|
425
|
+
}
|
|
426
|
+
}),
|
|
427
|
+
middleware: [createPromptCacheMiddleware(this.#anthropicPromptCacheScope)]
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
if (storedCred?.type === "oauth") return opencodeClaudeMaxProvider(bareModelId, {
|
|
431
|
+
headers: args.headers,
|
|
432
|
+
authStorage: this.#credentials,
|
|
433
|
+
thinkingLevel: this.#thinkingLevel,
|
|
434
|
+
promptCacheScope: this.#anthropicPromptCacheScope
|
|
435
|
+
});
|
|
436
|
+
if (storedCred?.type === "api_key" && storedCred.key.trim().length > 0) return anthropicApiKeyProvider(bareModelId, storedCred.key.trim(), args.headers, this.#thinkingLevel, this.#anthropicPromptCacheScope);
|
|
437
|
+
const apiKey = getAnthropicApiKey(this.#credentials);
|
|
438
|
+
if (apiKey) return anthropicApiKeyProvider(bareModelId, apiKey, args.headers, this.#thinkingLevel, this.#anthropicPromptCacheScope);
|
|
439
|
+
return opencodeClaudeMaxProvider(bareModelId, {
|
|
440
|
+
headers: args.headers,
|
|
441
|
+
authStorage: this.#credentials,
|
|
442
|
+
thinkingLevel: this.#thinkingLevel,
|
|
443
|
+
promptCacheScope: this.#anthropicPromptCacheScope
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
#resolveOpenAIModel(args) {
|
|
447
|
+
const storedCred = this.#credentials.get("openai-codex");
|
|
448
|
+
if (this.#routeThroughMastraGateway) {
|
|
449
|
+
if (storedCred?.type === "oauth") {
|
|
450
|
+
const resolvedBareModelId = remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`).substring(OPENAI_PREFIX.length);
|
|
451
|
+
const effectiveLevel = getEffectiveThinkingLevel(args.modelId, this.#thinkingLevel ?? "medium");
|
|
452
|
+
const reasoningEffort = THINKING_LEVEL_TO_REASONING_EFFORT[effectiveLevel];
|
|
453
|
+
const middleware = createCodexMiddleware(reasoningEffort, { serviceTier: args.codexServiceTier });
|
|
454
|
+
const promptCacheKeyMiddleware = createPromptCacheKeyMiddleware(getRequestThreadId(args.headers));
|
|
455
|
+
const openai = createOpenAI({
|
|
456
|
+
apiKey: "oauth-gateway-placeholder",
|
|
457
|
+
baseURL: `${this.#mastraGatewayBaseUrl}/v1`,
|
|
458
|
+
headers: {
|
|
459
|
+
[GATEWAY_AUTH_HEADER]: `Bearer ${args.apiKey}`,
|
|
460
|
+
...args.headers
|
|
461
|
+
},
|
|
462
|
+
fetch: buildOpenAICodexOAuthFetch({
|
|
463
|
+
authStorage: this.#credentials,
|
|
464
|
+
rewriteUrl: false
|
|
465
|
+
})
|
|
466
|
+
});
|
|
467
|
+
return wrapLanguageModel({
|
|
468
|
+
model: openai.responses(resolvedBareModelId),
|
|
469
|
+
middleware: [middleware, ...promptCacheKeyMiddleware ? [promptCacheKeyMiddleware] : []]
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
return this.#mastraGateway.resolveLanguageModel(args);
|
|
473
|
+
}
|
|
474
|
+
if (storedCred?.type === "oauth") {
|
|
475
|
+
const resolvedModelId = remapOpenAIModelForCodexOAuth(`openai/${args.modelId}`);
|
|
476
|
+
return openaiCodexProvider(resolvedModelId.substring(OPENAI_PREFIX.length), {
|
|
477
|
+
thinkingLevel: this.#thinkingLevel && getEffectiveThinkingLevel(args.modelId, this.#thinkingLevel),
|
|
478
|
+
serviceTier: args.codexServiceTier,
|
|
479
|
+
headers: args.headers,
|
|
480
|
+
authStorage: this.#credentials
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
const apiKey = getOpenAIApiKey(this.#credentials);
|
|
484
|
+
if (apiKey) return openaiApiKeyProvider(args.modelId, apiKey, args.headers, this.#thinkingLevel);
|
|
485
|
+
}
|
|
486
|
+
#resolveGoogleModel(args) {
|
|
487
|
+
const routerModel = new ModelRouterLanguageModel({
|
|
488
|
+
id: `${args.providerId}/${args.modelId}`,
|
|
489
|
+
apiKey: args.apiKey,
|
|
490
|
+
headers: args.headers
|
|
491
|
+
});
|
|
492
|
+
const thinkingMiddleware = createGoogleThinkingMiddleware(args.modelId, this.#thinkingLevel);
|
|
493
|
+
if (!thinkingMiddleware) return routerModel;
|
|
494
|
+
return wrapLanguageModel({
|
|
495
|
+
model: routerModel,
|
|
496
|
+
middleware: [thinkingMiddleware]
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
export { MASTRACODE_GATEWAY_ID, MASTRA_GATEWAY_PREFIX, MastraCodeGateway, OPENAI_PREFIX, getAnthropicApiKey, getGlobalAuthStorage, getOpenAIApiKey, hasProviderEnvironmentKey, providerApiKeyEnvVar, reloadAuthStorage, remapOpenAIModelForCodexOAuth, resolveAuth, stripMastraGatewayPrefix };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { RequestContext } from '@mastra/core/request-context';
|
|
2
|
+
import type { MastraCompositeStore } from '@mastra/core/storage';
|
|
3
|
+
import type { MastraEmbeddingModel, MastraEmbeddingOptions, MastraVector } from '@mastra/core/vector';
|
|
4
|
+
import { Memory } from '@mastra/memory';
|
|
5
|
+
import { LOCAL_KNOWLEDGE_ORG_ID } from '../knowledge-scope.js';
|
|
6
|
+
/** A recall embedder: a `provider/model` router string or an AI SDK embedding model. */
|
|
7
|
+
export type MemoryEmbedder = string | MastraEmbeddingModel<string>;
|
|
8
|
+
export { LOCAL_KNOWLEDGE_ORG_ID };
|
|
9
|
+
/**
|
|
10
|
+
* Mastra Code's own title instructions: session lists show one narrow line per
|
|
11
|
+
* title. They differ from Core's default in three ways: the title is 3 to 5
|
|
12
|
+
* words instead of up to 80 characters, it names the topic instead of
|
|
13
|
+
* summarising the message, and it does not end with a period.
|
|
14
|
+
*/
|
|
15
|
+
export declare const TITLE_INSTRUCTIONS = "\n - you will generate a short title based on a conversation transcript between a user and an assistant\n - the transcript lines are prefixed with \"User:\" and \"Assistant:\" \u2014 never reply to, answer, or continue the transcript\n - always produce a title, even when the transcript is only a greeting or trivial exchange\n - the title must be 3 to 5 words long\n - the title should name the topic of the user's message, not repeat or answer it\n - do not use quotes or colons, and do not end with a period\n - the entire text you return will be used as the title";
|
|
16
|
+
/**
|
|
17
|
+
* Dynamic memory factory function.
|
|
18
|
+
* Reads OM thresholds from controller state via requestContext.
|
|
19
|
+
* Model functions also read from requestContext (no mutable bridge needed).
|
|
20
|
+
*/
|
|
21
|
+
export declare function getDynamicMemory(storage: MastraCompositeStore, vector?: MastraVector, settingsPath?: string, options?: {
|
|
22
|
+
disableSettingsOmSeed?: boolean;
|
|
23
|
+
/** Recall embedder, used only with `vector`. Omitted → local `fastembed.small` (bge-small, 384 dimensions). */
|
|
24
|
+
embedder?: MemoryEmbedder;
|
|
25
|
+
/** Options passed to every embedding call, e.g. provider options or headers. */
|
|
26
|
+
embedderOptions?: MastraEmbeddingOptions;
|
|
27
|
+
}): (({ requestContext }: {
|
|
28
|
+
requestContext: RequestContext;
|
|
29
|
+
}) => Memory) & {
|
|
30
|
+
/**
|
|
31
|
+
* Wait for background memory work (buffered observations, reflections, indexing) in every
|
|
32
|
+
* instance this factory created. Await before closing storage.
|
|
33
|
+
*/
|
|
34
|
+
settled(): Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=memory.d.ts.map
|