@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,232 @@
|
|
|
1
|
+
import { ProviderAuthRequiredError, rejectUnauthorizedResponse } from "../auth/provider-auth-error.js";
|
|
2
|
+
import { AuthStorage } from "../auth/storage.js";
|
|
3
|
+
import { runCatalogThinkingLevel } from "../thinking-catalog.js";
|
|
4
|
+
import "./anthropic-prompt-cache.js";
|
|
5
|
+
import { ANTHROPIC_THINKING_BUDGET_TOKENS, getAnthropicThinkingCapability } from "./anthropic-thinking.js";
|
|
6
|
+
import { wrapLanguageModel } from "ai";
|
|
7
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
8
|
+
// src/providers/claude-max.ts
|
|
9
|
+
/**
|
|
10
|
+
* Claude Max OAuth Provider
|
|
11
|
+
*
|
|
12
|
+
* Uses OAuth tokens from AuthStorage to authenticate with Claude Max plan.
|
|
13
|
+
* The OAuth endpoint requires a specific system message to be present.
|
|
14
|
+
*/
|
|
15
|
+
const PROVIDER_NAME = "Anthropic";
|
|
16
|
+
const claudeCodeIdentity = "You are Claude Code, Anthropic's official CLI for Claude.";
|
|
17
|
+
const OAUTH_REQUIRED_BETAS = [
|
|
18
|
+
"oauth-2025-04-20",
|
|
19
|
+
"claude-code-20250219",
|
|
20
|
+
"interleaved-thinking-2025-05-14",
|
|
21
|
+
"fine-grained-tool-streaming-2025-05-14"
|
|
22
|
+
];
|
|
23
|
+
let authStorageInstance = null;
|
|
24
|
+
/**
|
|
25
|
+
* Get or create the shared AuthStorage instance
|
|
26
|
+
*/
|
|
27
|
+
function getAuthStorage() {
|
|
28
|
+
if (!authStorageInstance) authStorageInstance = new AuthStorage();
|
|
29
|
+
return authStorageInstance;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Set a custom AuthStorage instance (useful for TUI integration)
|
|
33
|
+
*/
|
|
34
|
+
function setAuthStorage(storage) {
|
|
35
|
+
authStorageInstance = storage ?? null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Middleware that injects the Claude Code identity system message
|
|
39
|
+
* Required for Claude Max OAuth authentication
|
|
40
|
+
*/
|
|
41
|
+
const claudeCodeMiddleware = {
|
|
42
|
+
specificationVersion: "v4",
|
|
43
|
+
transformParams: async ({ params }) => {
|
|
44
|
+
const systemMessage = {
|
|
45
|
+
role: "system",
|
|
46
|
+
content: claudeCodeIdentity
|
|
47
|
+
};
|
|
48
|
+
if (params.temperature) delete params.topP;
|
|
49
|
+
return {
|
|
50
|
+
...params,
|
|
51
|
+
prompt: [systemMessage, ...params.prompt]
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Prompt caching middleware for Anthropic
|
|
57
|
+
*
|
|
58
|
+
* Adds cache breakpoints at strategic locations:
|
|
59
|
+
* 1. Last system message (end of static instructions + dynamic memory)
|
|
60
|
+
* 2. Most recent user/assistant message (conversation context), `conversation` scope only
|
|
61
|
+
*
|
|
62
|
+
* This allows Anthropic to cache:
|
|
63
|
+
* - System prompts and instructions (rarely change)
|
|
64
|
+
* - Conversation history up to the last message
|
|
65
|
+
*/
|
|
66
|
+
const createPromptCacheMiddleware = (scope) => ({
|
|
67
|
+
specificationVersion: "v4",
|
|
68
|
+
transformParams: async ({ params }) => {
|
|
69
|
+
const prompt = [...params.prompt];
|
|
70
|
+
const cacheControl = {
|
|
71
|
+
type: "ephemeral",
|
|
72
|
+
ttl: scope === "conversation" ? "1h" : "5m"
|
|
73
|
+
};
|
|
74
|
+
const addCacheToMessage = (msg) => {
|
|
75
|
+
if (typeof msg.content === "string") return {
|
|
76
|
+
...msg,
|
|
77
|
+
providerOptions: {
|
|
78
|
+
...msg.providerOptions,
|
|
79
|
+
anthropic: {
|
|
80
|
+
...msg.providerOptions?.anthropic,
|
|
81
|
+
cacheControl
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
if (Array.isArray(msg.content) && msg.content.length > 0) {
|
|
86
|
+
const content = [...msg.content];
|
|
87
|
+
const lastPart = content[content.length - 1];
|
|
88
|
+
content[content.length - 1] = {
|
|
89
|
+
...lastPart,
|
|
90
|
+
providerOptions: {
|
|
91
|
+
...lastPart.providerOptions,
|
|
92
|
+
anthropic: {
|
|
93
|
+
...lastPart.providerOptions?.anthropic,
|
|
94
|
+
cacheControl
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
...msg,
|
|
100
|
+
content
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return msg;
|
|
104
|
+
};
|
|
105
|
+
let lastSystemIdx = -1;
|
|
106
|
+
for (let i = prompt.length - 1; i >= 0; i--) if (prompt[i].role === "system") {
|
|
107
|
+
lastSystemIdx = i;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (lastSystemIdx >= 0) prompt[lastSystemIdx] = addCacheToMessage(prompt[lastSystemIdx]);
|
|
111
|
+
const lastIdx = prompt.length - 1;
|
|
112
|
+
if (scope === "conversation" && lastIdx >= 0 && lastIdx !== lastSystemIdx) prompt[lastIdx] = addCacheToMessage(prompt[lastIdx]);
|
|
113
|
+
return {
|
|
114
|
+
...params,
|
|
115
|
+
prompt
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
function runAnthropicRequestLevel(modelId, level) {
|
|
120
|
+
return runCatalogThinkingLevel(`anthropic/${modelId}`, level);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Middleware that maps the session thinking level onto Anthropic extended
|
|
124
|
+
* thinking. Returns `undefined` (no middleware) when the level is `off` or the
|
|
125
|
+
* model doesn't support thinking, preserving the previous request shape.
|
|
126
|
+
*
|
|
127
|
+
* - Adaptive-era models (Sonnet 4.6+/Opus 4.6+): `thinking: adaptive` plus
|
|
128
|
+
* `output_config.effort`, the closest effort the model accepts.
|
|
129
|
+
* - Budget-era models (Claude 3.7 – Opus 4.5): `thinking: enabled` with a
|
|
130
|
+
* `budget_tokens` value derived from the level.
|
|
131
|
+
*/
|
|
132
|
+
function createAnthropicThinkingMiddleware(modelId, thinkingLevel) {
|
|
133
|
+
if (!thinkingLevel || thinkingLevel === "off") return void 0;
|
|
134
|
+
const capability = getAnthropicThinkingCapability(modelId);
|
|
135
|
+
const level = runAnthropicRequestLevel(modelId, thinkingLevel);
|
|
136
|
+
if (level === "off") return void 0;
|
|
137
|
+
return {
|
|
138
|
+
specificationVersion: "v4",
|
|
139
|
+
transformParams: async ({ params }) => {
|
|
140
|
+
const anthropicOptions = params.providerOptions?.anthropic ?? {};
|
|
141
|
+
if (anthropicOptions.thinking !== void 0 || anthropicOptions.effort !== void 0) return params;
|
|
142
|
+
delete params.temperature;
|
|
143
|
+
delete params.topP;
|
|
144
|
+
delete params.topK;
|
|
145
|
+
params.providerOptions = {
|
|
146
|
+
...params.providerOptions,
|
|
147
|
+
anthropic: {
|
|
148
|
+
...anthropicOptions,
|
|
149
|
+
...capability === "adaptive" ? {
|
|
150
|
+
thinking: {
|
|
151
|
+
type: "adaptive",
|
|
152
|
+
display: "summarized"
|
|
153
|
+
},
|
|
154
|
+
effort: level
|
|
155
|
+
} : { thinking: {
|
|
156
|
+
type: "enabled",
|
|
157
|
+
budgetTokens: ANTHROPIC_THINKING_BUDGET_TOKENS[level]
|
|
158
|
+
} }
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
return params;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Build a fetch function that handles Anthropic OAuth.
|
|
167
|
+
* Preserves non-auth headers from init (critical for gateway auth header to survive
|
|
168
|
+
* when used with the gateway). Strips `authorization` and `x-api-key`.
|
|
169
|
+
*/
|
|
170
|
+
function buildAnthropicOAuthFetch(opts = {}) {
|
|
171
|
+
return (async (url, init) => {
|
|
172
|
+
const storage = opts.authStorage ?? getAuthStorage();
|
|
173
|
+
storage.reload();
|
|
174
|
+
if (storage.get("anthropic")?.type === "api_key") throw new Error("Anthropic API key credential is configured, but OAuth is required.");
|
|
175
|
+
const accessToken = await storage.getApiKey("anthropic");
|
|
176
|
+
if (!accessToken) throw storage.get("anthropic")?.type === "oauth" ? ProviderAuthRequiredError.signInExpired(PROVIDER_NAME) : ProviderAuthRequiredError.notSignedIn(PROVIDER_NAME);
|
|
177
|
+
const headers = new Headers(url instanceof Request ? url.headers : void 0);
|
|
178
|
+
if (init?.headers) new Headers(init.headers).forEach((value, key) => headers.set(key, value));
|
|
179
|
+
headers.delete("authorization");
|
|
180
|
+
headers.delete("x-api-key");
|
|
181
|
+
headers.set("Authorization", `Bearer ${accessToken}`);
|
|
182
|
+
const requestBetas = (headers.get("anthropic-beta") ?? "").split(",").map((beta) => beta.trim()).filter(Boolean);
|
|
183
|
+
headers.set("anthropic-beta", Array.from(/* @__PURE__ */ new Set([...OAUTH_REQUIRED_BETAS, ...requestBetas])).join(","));
|
|
184
|
+
headers.set("anthropic-version", "2023-06-01");
|
|
185
|
+
const requestUrl = url instanceof URL ? url.toString() : typeof url === "string" ? url : url.url;
|
|
186
|
+
try {
|
|
187
|
+
return await rejectUnauthorizedResponse(await fetch(url, {
|
|
188
|
+
...init,
|
|
189
|
+
headers
|
|
190
|
+
}), {
|
|
191
|
+
providerName: PROVIDER_NAME,
|
|
192
|
+
requestUrl
|
|
193
|
+
});
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (error && typeof error === "object") Object.assign(error, { requestUrl });
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Creates an Anthropic model using Claude Max OAuth authentication
|
|
202
|
+
* Uses OAuth tokens from AuthStorage (auto-refreshes when needed)
|
|
203
|
+
*/
|
|
204
|
+
function opencodeClaudeMaxProvider(modelId = "claude-sonnet-5-5", options) {
|
|
205
|
+
const headers = options?.headers;
|
|
206
|
+
const thinkingMiddleware = createAnthropicThinkingMiddleware(modelId, options?.thinkingLevel);
|
|
207
|
+
const middleware = [
|
|
208
|
+
claudeCodeMiddleware,
|
|
209
|
+
createPromptCacheMiddleware(options?.promptCacheScope ?? "conversation"),
|
|
210
|
+
...thinkingMiddleware ? [thinkingMiddleware] : []
|
|
211
|
+
];
|
|
212
|
+
if (process.env.NODE_ENV === "test" || process.env.VITEST) {
|
|
213
|
+
const anthropic = createAnthropic({
|
|
214
|
+
apiKey: "test-api-key",
|
|
215
|
+
headers
|
|
216
|
+
});
|
|
217
|
+
return wrapLanguageModel({
|
|
218
|
+
model: anthropic(modelId),
|
|
219
|
+
middleware
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const anthropic = createAnthropic({
|
|
223
|
+
apiKey: "oauth-placeholder",
|
|
224
|
+
headers,
|
|
225
|
+
fetch: buildAnthropicOAuthFetch({ authStorage: options?.authStorage })
|
|
226
|
+
});
|
|
227
|
+
return wrapLanguageModel({
|
|
228
|
+
model: anthropic(modelId),
|
|
229
|
+
middleware
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
export { buildAnthropicOAuthFetch, claudeCodeMiddleware, createAnthropicThinkingMiddleware, createPromptCacheMiddleware, getAuthStorage, opencodeClaudeMaxProvider, setAuthStorage };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MastraModelGateway } from '@mastra/core/llm';
|
|
2
|
+
import type { GatewayAuthRequest, GatewayAuthResult, GatewayLanguageModel, ProviderConfig } from '@mastra/core/llm';
|
|
3
|
+
import type { CredentialStore } from '../auth/types.js';
|
|
4
|
+
export declare const GITHUB_COPILOT_GATEWAY_ID = "github-copilot";
|
|
5
|
+
/**
|
|
6
|
+
* Standalone GitHub Copilot gateway.
|
|
7
|
+
*
|
|
8
|
+
* models.dev excludes Copilot from the shared registry, so Mastra Code supplies
|
|
9
|
+
* the provider itself. It is its own gateway so catalog ids are the canonical
|
|
10
|
+
* `github-copilot/<model>` rather than `mastracode/github-copilot/<model>`.
|
|
11
|
+
*/
|
|
12
|
+
export declare class GitHubCopilotGateway extends MastraModelGateway {
|
|
13
|
+
#private;
|
|
14
|
+
readonly id = "github-copilot";
|
|
15
|
+
readonly name = "GitHub Copilot";
|
|
16
|
+
constructor(options?: {
|
|
17
|
+
credentialStore?: CredentialStore;
|
|
18
|
+
});
|
|
19
|
+
fetchProviders(): Promise<Record<string, ProviderConfig>>;
|
|
20
|
+
buildUrl(_modelId: string): string | undefined;
|
|
21
|
+
getApiKey(_modelId: string): Promise<string>;
|
|
22
|
+
resolveAuth(_request: GatewayAuthRequest): GatewayAuthResult | undefined;
|
|
23
|
+
resolveLanguageModel(args: {
|
|
24
|
+
modelId: string;
|
|
25
|
+
providerId: string;
|
|
26
|
+
apiKey: string;
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
transport?: any;
|
|
29
|
+
responsesWebSocket?: any;
|
|
30
|
+
}): GatewayLanguageModel;
|
|
31
|
+
}
|
|
32
|
+
export declare function createGitHubCopilotGateway(options?: {
|
|
33
|
+
credentialStore?: CredentialStore;
|
|
34
|
+
}): GitHubCopilotGateway;
|
|
35
|
+
//# sourceMappingURL=github-copilot-gateway.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getAuthStorage, getCopilotModelCatalog, githubCopilotProvider } from "./github-copilot.js";
|
|
2
|
+
import { MastraModelGateway } from "@mastra/core/llm";
|
|
3
|
+
// src/providers/github-copilot-gateway.ts
|
|
4
|
+
const GITHUB_COPILOT_GATEWAY_ID = "github-copilot";
|
|
5
|
+
/**
|
|
6
|
+
* Standalone GitHub Copilot gateway.
|
|
7
|
+
*
|
|
8
|
+
* models.dev excludes Copilot from the shared registry, so Mastra Code supplies
|
|
9
|
+
* the provider itself. It is its own gateway so catalog ids are the canonical
|
|
10
|
+
* `github-copilot/<model>` rather than `mastracode/github-copilot/<model>`.
|
|
11
|
+
*/
|
|
12
|
+
var GitHubCopilotGateway = class extends MastraModelGateway {
|
|
13
|
+
id = GITHUB_COPILOT_GATEWAY_ID;
|
|
14
|
+
name = "GitHub Copilot";
|
|
15
|
+
#credentials;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
super();
|
|
18
|
+
this.#credentials = options.credentialStore;
|
|
19
|
+
}
|
|
20
|
+
get #store() {
|
|
21
|
+
return this.#credentials ?? getAuthStorage();
|
|
22
|
+
}
|
|
23
|
+
async fetchProviders() {
|
|
24
|
+
try {
|
|
25
|
+
const models = await getCopilotModelCatalog({ authStorage: this.#store });
|
|
26
|
+
return { [GITHUB_COPILOT_GATEWAY_ID]: {
|
|
27
|
+
name: "GitHub Copilot",
|
|
28
|
+
apiKeyEnvVar: "",
|
|
29
|
+
apiKeyHeader: "Authorization",
|
|
30
|
+
gateway: this.id,
|
|
31
|
+
models: models.map((model) => model.id)
|
|
32
|
+
} };
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn("Failed to load GitHub Copilot model catalog:", error);
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
buildUrl(_modelId) {}
|
|
39
|
+
async getApiKey(_modelId) {
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
resolveAuth(_request) {
|
|
43
|
+
return this.#store.get("github-copilot")?.type === "oauth" ? {
|
|
44
|
+
bearerToken: "oauth",
|
|
45
|
+
source: "gateway"
|
|
46
|
+
} : void 0;
|
|
47
|
+
}
|
|
48
|
+
resolveLanguageModel(args) {
|
|
49
|
+
return githubCopilotProvider(args.modelId, {
|
|
50
|
+
headers: args.headers,
|
|
51
|
+
authStorage: this.#store
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function createGitHubCopilotGateway(options) {
|
|
56
|
+
return new GitHubCopilotGateway(options);
|
|
57
|
+
}
|
|
58
|
+
export { GITHUB_COPILOT_GATEWAY_ID, GitHubCopilotGateway, createGitHubCopilotGateway };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Copilot OAuth Provider
|
|
3
|
+
*
|
|
4
|
+
* Uses OAuth tokens from AuthStorage to authenticate with GitHub Copilot's chat API.
|
|
5
|
+
* The Copilot API speaks an OpenAI-compatible chat format, so we plug
|
|
6
|
+
* `@ai-sdk/openai-compatible` into Copilot's API URL and use a custom fetch to inject
|
|
7
|
+
* the bearer token and Copilot-specific headers.
|
|
8
|
+
*
|
|
9
|
+
* Inspired by:
|
|
10
|
+
* - opencode: https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/plugin/github-copilot/copilot.ts
|
|
11
|
+
* - pi-mono: https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/utils/oauth/github-copilot.ts
|
|
12
|
+
*/
|
|
13
|
+
import type { MastraModelConfig } from '@mastra/core/llm';
|
|
14
|
+
import type { CopilotModelEntry } from '../auth/providers/github-copilot.js';
|
|
15
|
+
import { AuthStorage } from '../auth/storage.js';
|
|
16
|
+
import type { CredentialStore } from '../auth/types.js';
|
|
17
|
+
/** Get or create the shared AuthStorage instance. */
|
|
18
|
+
export declare function getAuthStorage(): AuthStorage;
|
|
19
|
+
/** Set a custom AuthStorage instance (useful for tests / TUI integration). */
|
|
20
|
+
export declare function setAuthStorage(storage: AuthStorage | undefined): void;
|
|
21
|
+
/**
|
|
22
|
+
* Build a fetch wrapper that authenticates with GitHub Copilot OAuth.
|
|
23
|
+
*
|
|
24
|
+
* - Injects the short-lived Copilot bearer token (auto-refreshed by AuthStorage).
|
|
25
|
+
* - Adds the VS Code-like Copilot headers required by the API.
|
|
26
|
+
* - Rewrites the request URL onto the per-token API base when `rewriteUrl` is true.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildGitHubCopilotOAuthFetch(opts?: {
|
|
29
|
+
authStorage?: CredentialStore;
|
|
30
|
+
rewriteUrl?: boolean;
|
|
31
|
+
}): typeof fetch;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a model that talks to GitHub Copilot using OAuth credentials.
|
|
34
|
+
*
|
|
35
|
+
* Copilot's `/chat/completions` endpoint is OpenAI-compatible, but GitHub Copilot
|
|
36
|
+
* is not OpenAI. Use the generic OpenAI-compatible adapter with Copilot's base URL
|
|
37
|
+
* instead of the OpenAI provider plus URL rewriting.
|
|
38
|
+
*/
|
|
39
|
+
export declare function githubCopilotProvider(modelId?: string, options?: {
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
authStorage?: CredentialStore;
|
|
42
|
+
}): MastraModelConfig;
|
|
43
|
+
/** Reset the in-process Copilot catalog cache (test seam, also useful after logout). */
|
|
44
|
+
export declare function clearCopilotCatalogCache(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Return the user's currently-available Copilot models.
|
|
47
|
+
*
|
|
48
|
+
* - Returns `[]` when the user is not logged in to GitHub Copilot.
|
|
49
|
+
* - Returns the cached list when a recent fetch succeeded.
|
|
50
|
+
* - On the cache-miss / expired path, fetches `/models` with a 5s timeout, filters
|
|
51
|
+
* to picker-enabled and non-policy-disabled models, then caches for 10 minutes.
|
|
52
|
+
* - On fetch failure, returns a small hard-coded fallback (so packs still work
|
|
53
|
+
* offline) and caches that for 1 minute to avoid hammering the API.
|
|
54
|
+
*
|
|
55
|
+
* Concurrent calls during a fetch share the inflight promise.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getCopilotModelCatalog(opts?: {
|
|
58
|
+
authStorage?: CredentialStore;
|
|
59
|
+
}): Promise<CopilotModelEntry[]>;
|
|
60
|
+
//# sourceMappingURL=github-copilot.d.ts.map
|