@mastra/code-sdk 1.15.0-alpha.9 → 1.15.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/dist/acp/agent.js +2 -1
  2. package/dist/acp/agent.js.map +1 -1
  3. package/dist/acp/event-mapper.js +0 -2
  4. package/dist/acp/event-mapper.js.map +1 -1
  5. package/dist/acp.js +1 -2
  6. package/dist/agent-connections/registry.js.map +1 -1
  7. package/dist/agent-connections/signal-provider.js.map +1 -1
  8. package/dist/agent-connections/state-processor.js.map +1 -1
  9. package/dist/agent-connections/tools.js +26 -24
  10. package/dist/agent-connections/tools.js.map +1 -1
  11. package/dist/agents/background-completion.d.ts +15 -2
  12. package/dist/agents/background-completion.d.ts.map +1 -1
  13. package/dist/agents/background-completion.js +19 -2
  14. package/dist/agents/background-completion.js.map +1 -1
  15. package/dist/agents/instructions.js +3 -2
  16. package/dist/agents/instructions.js.map +1 -1
  17. package/dist/agents/mastracode-gateway.js +35 -27
  18. package/dist/agents/mastracode-gateway.js.map +1 -1
  19. package/dist/agents/memory.d.ts.map +1 -1
  20. package/dist/agents/memory.js +17 -3
  21. package/dist/agents/memory.js.map +1 -1
  22. package/dist/agents/prompts/agent-instructions.js +1 -1
  23. package/dist/agents/prompts/agent-instructions.js.map +1 -1
  24. package/dist/agents/prompts/index.js +3 -2
  25. package/dist/agents/prompts/index.js.map +1 -1
  26. package/dist/agents/prompts/plan.js +2 -1
  27. package/dist/agents/prompts/plan.js.map +1 -1
  28. package/dist/agents/sandbox-filesystem.d.ts +28 -0
  29. package/dist/agents/sandbox-filesystem.d.ts.map +1 -1
  30. package/dist/agents/sandbox-filesystem.js +81 -2
  31. package/dist/agents/sandbox-filesystem.js.map +1 -1
  32. package/dist/agents/tools.d.ts.map +1 -1
  33. package/dist/agents/tools.js +18 -12
  34. package/dist/agents/tools.js.map +1 -1
  35. package/dist/agents/workspace.js +2 -1
  36. package/dist/agents/workspace.js.map +1 -1
  37. package/dist/auth/account-rotation-processor.js +2 -1
  38. package/dist/auth/account-rotation-processor.js.map +1 -1
  39. package/dist/auth/providers/anthropic.js +2 -2
  40. package/dist/auth/providers/anthropic.js.map +1 -1
  41. package/dist/auth/providers/github-copilot.js +1 -1
  42. package/dist/auth/providers/github-copilot.js.map +1 -1
  43. package/dist/auth/providers/openai-codex.js +3 -2
  44. package/dist/auth/providers/openai-codex.js.map +1 -1
  45. package/dist/auth/providers/xai.js +1 -1
  46. package/dist/auth/providers/xai.js.map +1 -1
  47. package/dist/clipboard/index.js +5 -4
  48. package/dist/clipboard/index.js.map +1 -1
  49. package/dist/evals/context-builder.js +1 -3
  50. package/dist/evals/context-builder.js.map +1 -1
  51. package/dist/evals/scorers/outcome.js +2 -1
  52. package/dist/evals/scorers/outcome.js.map +1 -1
  53. package/dist/factory/remote-controller.js.map +1 -1
  54. package/dist/factory/remote-session.d.ts +1 -1
  55. package/dist/factory/remote-session.d.ts.map +1 -1
  56. package/dist/factory/remote-session.js +15 -13
  57. package/dist/factory/remote-session.js.map +1 -1
  58. package/dist/headless/cli.js +2 -1
  59. package/dist/headless/cli.js.map +1 -1
  60. package/dist/headless/run-mc.js +9 -9
  61. package/dist/headless/run-mc.js.map +1 -1
  62. package/dist/hooks/executor.js +3 -1
  63. package/dist/hooks/executor.js.map +1 -1
  64. package/dist/hooks/manager.js +40 -26
  65. package/dist/hooks/manager.js.map +1 -1
  66. package/dist/index.d.ts +2 -2
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +56 -29
  69. package/dist/index.js.map +1 -1
  70. package/dist/knowledge-inspector.js +2 -2
  71. package/dist/knowledge-inspector.js.map +1 -1
  72. package/dist/lsp/server.js +4 -2
  73. package/dist/lsp/server.js.map +1 -1
  74. package/dist/mcp/manager.d.ts.map +1 -1
  75. package/dist/mcp/manager.js +14 -2
  76. package/dist/mcp/manager.js.map +1 -1
  77. package/dist/onboarding/settings.js +5 -2
  78. package/dist/onboarding/settings.js.map +1 -1
  79. package/dist/platform/index.js +5 -3
  80. package/dist/platform/index.js.map +1 -1
  81. package/dist/plugins/loader.js +3 -1
  82. package/dist/plugins/loader.js.map +1 -1
  83. package/dist/plugins/manager.js +2 -1
  84. package/dist/plugins/manager.js.map +1 -1
  85. package/dist/plugins/package-link.js.map +1 -1
  86. package/dist/plugins/scaffold.js +4 -2
  87. package/dist/plugins/scaffold.js.map +1 -1
  88. package/dist/plugins/signal-lane.js.map +1 -1
  89. package/dist/process-memory-diagnostics.js +2 -1
  90. package/dist/process-memory-diagnostics.js.map +1 -1
  91. package/dist/providers/amazon-bedrock-gateway.js +2 -1
  92. package/dist/providers/amazon-bedrock-gateway.js.map +1 -1
  93. package/dist/providers/amazon-bedrock.js +2 -2
  94. package/dist/providers/amazon-bedrock.js.map +1 -1
  95. package/dist/providers/claude-max.d.ts.map +1 -1
  96. package/dist/providers/claude-max.js +14 -10
  97. package/dist/providers/claude-max.js.map +1 -1
  98. package/dist/providers/github-copilot-gateway.js.map +1 -1
  99. package/dist/providers/github-copilot.js +13 -12
  100. package/dist/providers/github-copilot.js.map +1 -1
  101. package/dist/providers/openai-codex.js +14 -11
  102. package/dist/providers/openai-codex.js.map +1 -1
  103. package/dist/providers/provider-thinking.js.map +1 -1
  104. package/dist/providers/thinking-request.js +1 -1
  105. package/dist/providers/thinking-request.js.map +1 -1
  106. package/dist/schedules/run-script.js +1 -1
  107. package/dist/schedules/run-script.js.map +1 -1
  108. package/dist/schedules/scheduler.js.map +1 -1
  109. package/dist/tools/request-sandbox-access.js +7 -5
  110. package/dist/tools/request-sandbox-access.js.map +1 -1
  111. package/dist/tools/utils.js +3 -1
  112. package/dist/tools/utils.js.map +1 -1
  113. package/dist/tools/web-search.d.ts +13 -11
  114. package/dist/tools/web-search.d.ts.map +1 -1
  115. package/dist/tools/web-search.js +21 -15
  116. package/dist/tools/web-search.js.map +1 -1
  117. package/dist/utils/debug-log.js +2 -2
  118. package/dist/utils/debug-log.js.map +1 -1
  119. package/dist/utils/gateway-sync.js +1 -1
  120. package/dist/utils/gateway-sync.js.map +1 -1
  121. package/dist/utils/local-observability.d.ts +1 -1
  122. package/dist/utils/local-observability.d.ts.map +1 -1
  123. package/dist/utils/local-observability.js +1 -1
  124. package/dist/utils/local-observability.js.map +1 -1
  125. package/dist/utils/prune-cli.js +10 -8
  126. package/dist/utils/prune-cli.js.map +1 -1
  127. package/dist/utils/signals-pubsub.js.map +1 -1
  128. package/dist/utils/slash-command-processor.js +1 -1
  129. package/dist/utils/slash-command-processor.js.map +1 -1
  130. package/dist/utils/stdin-pipe.js +1 -1
  131. package/dist/utils/stdin-pipe.js.map +1 -1
  132. package/dist/utils/storage-factory.d.ts.map +1 -1
  133. package/dist/utils/storage-factory.js +1 -1
  134. package/dist/utils/storage-factory.js.map +1 -1
  135. package/dist/utils/storage-maintenance.js +1 -1
  136. package/dist/utils/storage-maintenance.js.map +1 -1
  137. package/dist/utils/update-check.js +2 -1
  138. package/dist/utils/update-check.js.map +1 -1
  139. package/dist/utils/wake-session.d.ts +1 -1
  140. package/dist/utils/wake-session.d.ts.map +1 -1
  141. package/dist/utils/wake-session.js +8 -1
  142. package/dist/utils/wake-session.js.map +1 -1
  143. package/dist/workflows/register-primitives.js +1 -1
  144. package/dist/workflows/register-primitives.js.map +1 -1
  145. package/package.json +22 -22
@@ -1 +1 @@
1
- {"version":3,"file":"github-copilot.js","names":[],"sources":["../../src/providers/github-copilot.ts"],"sourcesContent":["/**\n * GitHub Copilot OAuth Provider\n *\n * Uses OAuth tokens from AuthStorage to authenticate with GitHub Copilot's chat API.\n * The Copilot API speaks an OpenAI-compatible chat format, so we plug\n * `@ai-sdk/openai-compatible` into Copilot's API URL and use a custom fetch to inject\n * the bearer token and Copilot-specific headers.\n *\n * Inspired by:\n * - opencode: https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/plugin/github-copilot/copilot.ts\n * - pi-mono: https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/utils/oauth/github-copilot.ts\n */\n\nimport { createOpenAICompatible } from '@ai-sdk/openai-compatible';\nimport type { MastraModelConfig } from '@mastra/core/llm';\nimport type { JSONSchema7 } from '@mastra/schema-compat';\nimport { applyCompatLayer, GoogleSchemaCompatLayer } from '@mastra/schema-compat';\nimport { wrapLanguageModel } from 'ai';\nimport type { LanguageModelMiddleware } from 'ai';\nimport { ProviderAuthRequiredError, rejectUnauthorizedResponse } from '../auth/provider-auth-error.js';\nimport { COPILOT_HEADERS, fetchCopilotModels, getGitHubCopilotBaseUrl } from '../auth/providers/github-copilot.js';\nimport type { CopilotModelEntry, GitHubCopilotCredentials } from '../auth/providers/github-copilot.js';\nimport { AuthStorage } from '../auth/storage.js';\nimport type { CredentialStore, OAuthCredential } from '../auth/types.js';\n\nconst COPILOT_PROVIDER_ID = 'github-copilot';\n\n// Singleton auth storage instance (shared with claude-max.ts / openai-codex.ts when not overridden).\nlet authStorageInstance: AuthStorage | null = null;\n\n/** Get or create the shared AuthStorage instance. */\nexport function getAuthStorage(): AuthStorage {\n if (!authStorageInstance) {\n authStorageInstance = new AuthStorage();\n }\n return authStorageInstance;\n}\n\n/** Set a custom AuthStorage instance (useful for tests / TUI integration). */\nexport function setAuthStorage(storage: AuthStorage | undefined): void {\n authStorageInstance = storage ?? null;\n}\n\n/**\n * Heuristic: did this request come from the agent (e.g. tool result follow-ups) rather\n * than a fresh user turn? Mirrors opencode's `isAgent` logic — Copilot bills these\n * differently via the `x-initiator` header.\n */\nfunction detectIsAgent(body: unknown): boolean {\n if (!body || typeof body !== 'object') return false;\n const obj = body as Record<string, unknown>;\n\n const messages = obj.messages;\n if (Array.isArray(messages) && messages.length > 0) {\n const last = messages[messages.length - 1] as { role?: string; content?: unknown };\n if (last?.role && last.role !== 'user') return true;\n if (Array.isArray(last?.content)) {\n // If the last user turn carries any tool_result parts, treat it as an agent turn.\n const hasToolResult = last.content.some(\n (part: unknown) => part && typeof part === 'object' && (part as { type?: string }).type === 'tool_result',\n );\n if (hasToolResult) return true;\n }\n }\n\n const input = obj.input;\n if (Array.isArray(input) && input.length > 0) {\n const last = input[input.length - 1] as { role?: string };\n if (last?.role && last.role !== 'user') return true;\n }\n\n return false;\n}\n\n/** Detect image/vision content in a request body. */\nfunction detectIsVision(body: unknown): boolean {\n if (!body || typeof body !== 'object') return false;\n const obj = body as Record<string, unknown>;\n\n const matchPart = (part: unknown): boolean => {\n if (!part || typeof part !== 'object') return false;\n const t = (part as { type?: string }).type;\n return t === 'image' || t === 'image_url' || t === 'input_image';\n };\n\n const messages = obj.messages;\n if (Array.isArray(messages)) {\n return messages.some(\n (msg: unknown) =>\n msg &&\n typeof msg === 'object' &&\n Array.isArray((msg as { content?: unknown }).content) &&\n ((msg as { content: unknown[] }).content as unknown[]).some(matchPart),\n );\n }\n\n const input = obj.input;\n if (Array.isArray(input)) {\n return input.some(\n (item: unknown) =>\n item &&\n typeof item === 'object' &&\n Array.isArray((item as { content?: unknown }).content) &&\n ((item as { content: unknown[] }).content as unknown[]).some(matchPart),\n );\n }\n\n return false;\n}\n\n/**\n * Build a fetch wrapper that authenticates with GitHub Copilot OAuth.\n *\n * - Injects the short-lived Copilot bearer token (auto-refreshed by AuthStorage).\n * - Adds the VS Code-like Copilot headers required by the API.\n * - Rewrites the request URL onto the per-token API base when `rewriteUrl` is true.\n */\nexport function buildGitHubCopilotOAuthFetch(\n opts: { authStorage?: CredentialStore; rewriteUrl?: boolean } = {},\n): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n const storage = opts.authStorage ?? getAuthStorage();\n storage.reload();\n\n const cred = storage.get(COPILOT_PROVIDER_ID);\n if (!cred || cred.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to GitHub Copilot.');\n }\n\n let accessToken: string | undefined;\n let activeCred: OAuthCredential | undefined;\n if (storage.getOAuthCredential) {\n activeCred = await storage.getOAuthCredential(COPILOT_PROVIDER_ID);\n accessToken = activeCred?.access;\n } else {\n accessToken = await storage.getApiKey(COPILOT_PROVIDER_ID);\n storage.reload();\n const reloaded = storage.get(COPILOT_PROVIDER_ID);\n activeCred = reloaded?.type === 'oauth' ? { ...reloaded, access: accessToken ?? reloaded.access } : undefined;\n }\n if (!accessToken || !activeCred) {\n throw new ProviderAuthRequiredError('Failed to refresh the GitHub Copilot token.');\n }\n const enterpriseUrl = (activeCred as GitHubCopilotCredentials).enterpriseUrl;\n\n let parsedBody: unknown;\n try {\n const body = await request.clone().text();\n parsedBody = body ? JSON.parse(body) : undefined;\n } catch {\n parsedBody = undefined;\n }\n const isAgent = detectIsAgent(parsedBody);\n const isVision = detectIsVision(parsedBody);\n\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n headers.set('x-initiator', isAgent ? 'agent' : 'user');\n headers.set('Openai-Intent', 'conversation-edits');\n if (isVision) {\n headers.set('Copilot-Vision-Request', 'true');\n }\n for (const [key, value] of Object.entries(COPILOT_HEADERS)) {\n // Only set if caller didn't already provide it (allow overrides for tests).\n if (!headers.has(key)) {\n headers.set(key, value);\n }\n }\n\n const finalUrl =\n opts.rewriteUrl !== false ? rewriteToCopilotBase(request, accessToken, enterpriseUrl) : new URL(request.url);\n\n const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body;\n const finalRequest = new Request(finalUrl, {\n method: request.method,\n headers,\n body,\n signal: request.signal,\n redirect: request.redirect,\n integrity: request.integrity,\n ...(body ? ({ duplex: 'half' } as RequestInit) : {}),\n });\n try {\n return await rejectUnauthorizedResponse(await fetch(finalRequest), {\n providerName: 'GitHub Copilot',\n requestUrl: finalUrl.toString(),\n });\n } catch (error) {\n if (error && typeof error === 'object') {\n Object.assign(error as Record<string, unknown>, {\n requestUrl: finalUrl.toString(),\n });\n }\n throw error;\n }\n }) as typeof fetch;\n}\n\nfunction rewriteToCopilotBase(url: string | URL | Request, token: string, enterpriseDomain?: string): URL {\n const original = url instanceof URL ? url : new URL(typeof url === 'string' ? url : (url as Request).url);\n const base = new URL(getGitHubCopilotBaseUrl(token, enterpriseDomain));\n // Copilot's OpenAI-compatible API serves endpoints at the root of the base host\n // (`/chat/completions`, `/responses`, `/models`, ...) — not under a `/v1/` prefix\n // like api.openai.com does. The @ai-sdk/openai default baseURL is\n // `https://api.openai.com/v1`, so the SDK builds requests like\n // `https://api.openai.com/v1/chat/completions`. Strip the leading `/v1` segment\n // when rewriting onto the Copilot base or Copilot will return 404 Not Found.\n const pathname = original.pathname.replace(/^\\/v1(\\/|$)/, '/');\n return new URL(`${pathname}${original.search}`, base);\n}\n\nfunction isGeminiModel(modelId: string): boolean {\n return modelId.startsWith('gemini-');\n}\n\nfunction applyGeminiSchemaCompatToTools(modelId: string, tools: unknown): unknown {\n if (!Array.isArray(tools)) {\n return tools;\n }\n\n const compatLayer = new GoogleSchemaCompatLayer({\n provider: COPILOT_PROVIDER_ID,\n modelId,\n supportsStructuredOutputs: false,\n });\n\n return tools.map(tool => {\n if (!tool || typeof tool !== 'object' || (tool as { type?: unknown }).type !== 'function') {\n return tool;\n }\n\n const functionTool = tool as { inputSchema?: JSONSchema7 };\n if (!functionTool.inputSchema) {\n return tool;\n }\n\n return {\n ...functionTool,\n inputSchema: applyCompatLayer({\n schema: functionTool.inputSchema,\n compatLayers: [compatLayer],\n mode: 'aiSdkSchema',\n }).jsonSchema as JSONSchema7,\n };\n });\n}\n\n/** Middleware that prevents sending parameters Copilot's endpoint rejects. */\nfunction createCopilotMiddleware(modelId: string): LanguageModelMiddleware {\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n if (params.temperature !== undefined && params.temperature !== null) {\n delete params.topP;\n }\n\n if (isGeminiModel(modelId)) {\n (params as { tools?: unknown }).tools = applyGeminiSchemaCompatToTools(\n modelId,\n (params as { tools?: unknown }).tools,\n );\n }\n\n return params;\n },\n };\n}\n\n/**\n * Creates a model that talks to GitHub Copilot using OAuth credentials.\n *\n * Copilot's `/chat/completions` endpoint is OpenAI-compatible, but GitHub Copilot\n * is not OpenAI. Use the generic OpenAI-compatible adapter with Copilot's base URL\n * instead of the OpenAI provider plus URL rewriting.\n */\nexport function githubCopilotProvider(\n modelId: string = 'gpt-4.1',\n options?: { headers?: Record<string, string>; authStorage?: CredentialStore },\n): MastraModelConfig {\n const headers = options?.headers;\n const copilot = createOpenAICompatible({\n name: COPILOT_PROVIDER_ID,\n baseURL: 'https://api.githubcopilot.com',\n apiKey: process.env.NODE_ENV === 'test' || process.env.VITEST ? 'test-api-key' : 'oauth-placeholder',\n headers,\n fetch:\n process.env.NODE_ENV === 'test' || process.env.VITEST\n ? undefined\n : (buildGitHubCopilotOAuthFetch({ rewriteUrl: false, authStorage: options?.authStorage }) as any),\n });\n\n return wrapLanguageModel({\n model: copilot.chatModel(modelId),\n middleware: [createCopilotMiddleware(modelId)],\n });\n}\n\n// ---------------------------------------------------------------------------\n// Live model catalog\n// ---------------------------------------------------------------------------\n\n/**\n * Hard-coded fallback advertised when the live `/models` request fails (network\n * down, expired token, etc.). Keep this conservative because the live catalog is\n * the source of truth for the user's currently-enabled Copilot models.\n *\n * Available across all paid Copilot tiers and free of premium-request charges.\n */\nconst COPILOT_FALLBACK_MODELS: CopilotModelEntry[] = [\n {\n id: 'gpt-4.1',\n name: 'GPT-4.1',\n vendor: 'OpenAI',\n supportedEndpoints: ['/chat/completions'],\n isAnthropicShaped: false,\n supportsVision: true,\n supportsToolCalls: true,\n },\n];\n\nconst CATALOG_TTL_MS = 10 * 60 * 1000;\nconst CATALOG_FAILURE_TTL_MS = 60 * 1000;\nconst CATALOG_FETCH_TIMEOUT_MS = 5_000;\n\ninterface CatalogCacheEntry {\n fetchedAt: number;\n ttl: number;\n models: CopilotModelEntry[];\n}\n\nconst catalogCache = new Map<string, CatalogCacheEntry>();\nconst inflightFetches = new Map<string, Promise<CopilotModelEntry[]>>();\n\n/**\n * Resolve the account identity for a credential read through `getApiKey`, which\n * may await a refresh with a rotation landing inside it. The registry is\n * consulted on both sides of that await, and only an id that survived it names\n * the account that produced the token: an id that moved describes whichever\n * account is active *now*, so trusting it would file this fetch's models under\n * the wrong account and serve them to it. Unidentified is the safe answer — the\n * caller then skips the TTL cache, exactly as it does when the store exposes no\n * registry at all. (`AuthStorage` never needs this: its snapshots name their\n * account.)\n */\nfunction readStableAccountId(storage: CredentialStore, idBeforeToken: string | undefined): string | undefined {\n const idAfterToken = storage.getActiveAccount?.(COPILOT_PROVIDER_ID)?.id;\n return idBeforeToken !== undefined && idBeforeToken === idAfterToken ? idAfterToken : undefined;\n}\n\n/** Reset the in-process Copilot catalog cache (test seam, also useful after logout). */\nexport function clearCopilotCatalogCache(): void {\n catalogCache.clear();\n inflightFetches.clear();\n}\n\n/**\n * Return the user's currently-available Copilot models.\n *\n * - Returns `[]` when the user is not logged in to GitHub Copilot.\n * - Returns the cached list when a recent fetch succeeded.\n * - On the cache-miss / expired path, fetches `/models` with a 5s timeout, filters\n * to picker-enabled and non-policy-disabled models, then caches for 10 minutes.\n * - On fetch failure, returns a small hard-coded fallback (so packs still work\n * offline) and caches that for 1 minute to avoid hammering the API.\n *\n * Concurrent calls during a fetch share the inflight promise.\n */\nexport async function getCopilotModelCatalog(\n opts: { authStorage?: CredentialStore } = {},\n): Promise<CopilotModelEntry[]> {\n const storage = opts.authStorage ?? getAuthStorage();\n\n // Resolve one coherent credential snapshot before consulting the cache so a\n // token can never be paired with another account's enterprise endpoint.\n // `getOAuthCredential` is optional (deployed stores have no local registry),\n // so fall back to the always-present `getApiKey`, taking any enterprise\n // endpoint from the same slot via `get()`.\n let accessToken: string | undefined;\n let accountInstanceId: string | undefined;\n let enterpriseUrl: string | undefined;\n if (storage.getOAuthCredential) {\n const credential = await storage.getOAuthCredential(COPILOT_PROVIDER_ID);\n if (!credential || credential.type !== 'oauth') return [];\n accessToken = credential.access;\n // A snapshot that names its account is coherent by construction — the store\n // read the identity and the token in the same breath. One that does not\n // cannot be attributed to a registry entry we then read afterwards, because\n // a rotation may have landed in between; leave it unnamed so the caller\n // skips the TTL cache instead of guessing. (`AuthStorage` always stamps.)\n accountInstanceId = credential.accountInstanceId;\n enterpriseUrl = (credential as GitHubCopilotCredentials).enterpriseUrl;\n } else {\n // This store republishes its registry but not the OAuth snapshot API, so the\n // identity has to come from the registry. `getApiKey` may await a refresh and\n // a rotation can land inside it, so bracket the fetch: only an id that held\n // still names the account that produced this token.\n const idBeforeToken = storage.getActiveAccount?.(COPILOT_PROVIDER_ID)?.id;\n accessToken = await storage.getApiKey(COPILOT_PROVIDER_ID);\n if (!accessToken) return [];\n accountInstanceId = readStableAccountId(storage, idBeforeToken);\n const stored = storage.get(COPILOT_PROVIDER_ID);\n if (stored?.type === 'oauth') {\n enterpriseUrl = (stored as GitHubCopilotCredentials).enterpriseUrl;\n }\n // The id is non-secret, and keying per account is what keeps entitlements\n // (per account, per `github-copilot-catalog.test.ts`) from leaking across a\n // rotation. Deriving the key from the token is what `cba11389e7` removed.\n }\n const baseUrl = getGitHubCopilotBaseUrl(accessToken, enterpriseUrl);\n // No identity means the store cannot name the account it just served, so the\n // TTL cache cannot be keyed per account — one entry would outlive the request\n // and serve those models to every other account of the store. The in-flight\n // map is no safer: two callers that resolved *different* accounts of an\n // unnamed store would otherwise collide on one key and share whichever\n // catalog was fetched first. So both caches are identified-only, and an\n // unnamed store pays one `/models` request per call.\n const credentialKey = accountInstanceId === undefined ? undefined : `${accountInstanceId}\\0${baseUrl}`;\n const dedupeKey = credentialKey;\n\n const now = Date.now();\n const cached = credentialKey ? catalogCache.get(credentialKey) : undefined;\n if (cached && now - cached.fetchedAt < cached.ttl) return cached.models;\n\n const existingFetch = dedupeKey === undefined ? undefined : inflightFetches.get(dedupeKey);\n if (existingFetch) return existingFetch;\n\n const fetchPromise = (async (): Promise<CopilotModelEntry[]> => {\n try {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), CATALOG_FETCH_TIMEOUT_MS);\n try {\n const models = await fetchCopilotModels({\n baseUrl,\n bearerToken: accessToken,\n signal: controller.signal,\n });\n if (credentialKey) catalogCache.set(credentialKey, { fetchedAt: Date.now(), ttl: CATALOG_TTL_MS, models });\n return models;\n } finally {\n clearTimeout(timer);\n }\n } catch (error) {\n if (credentialKey) {\n catalogCache.set(credentialKey, {\n fetchedAt: Date.now(),\n ttl: CATALOG_FAILURE_TTL_MS,\n models: COPILOT_FALLBACK_MODELS,\n });\n }\n console.warn(\n 'Failed to fetch live GitHub Copilot models, using fallback list:',\n error instanceof Error ? error.message : error,\n );\n return COPILOT_FALLBACK_MODELS;\n } finally {\n if (dedupeKey !== undefined) inflightFetches.delete(dedupeKey);\n }\n })();\n if (dedupeKey !== undefined) inflightFetches.set(dedupeKey, fetchPromise);\n return fetchPromise;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyBA,MAAM,sBAAsB;AAG5B,IAAI,sBAA0C;;AAG9C,SAAgB,iBAA8B;CAC5C,IAAI,CAAC,qBACH,sBAAsB,IAAI,YAAY;CAExC,OAAO;AACT;;AAGA,SAAgB,eAAe,SAAwC;CACrE,sBAAsB,WAAW;AACnC;;;;;;AAOA,SAAS,cAAc,MAAwB;CAC7C,IAAI,CAAC,QAAQ,OAAO,SAAS,UAAU,OAAO;CAC9C,MAAM,MAAM;CAEZ,MAAM,WAAW,IAAI;CACrB,IAAI,MAAM,QAAQ,QAAQ,KAAK,SAAS,SAAS,GAAG;EAClD,MAAM,OAAO,SAAS,SAAS,SAAS;EACxC,IAAI,MAAM,QAAQ,KAAK,SAAS,QAAQ,OAAO;EAC/C,IAAI,MAAM,QAAQ,MAAM,OAAO,GAEP;OAAA,KAAK,QAAQ,MAChC,SAAkB,QAAQ,OAAO,SAAS,YAAa,KAA2B,SAAS,aAE9E,GAAG,OAAO;EAAA;CAE9B;CAEA,MAAM,QAAQ,IAAI;CAClB,IAAI,MAAM,QAAQ,K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AO;GACT,UAAU;IACR,aAAa,KAAK;GACpB;EACF,SAAS,OAAO;GACd,IAAI,eACF,aAAa,IAAI,eAAe;IAC9B,WAAW,KAAK,IAAI;IACpB,KAAK;IACL,QAAQ;GACV,CAAC;GAEH,QAAQ,KACN,oEACA,iBAAiB,QAAQ,MAAM,UAAU,KAC3C;GACA,OAAO;EACT,UAAU;GACR,IAAI,cAAc,KAAA,GAAW,gBAAgB,OAAO,SAAS;EAC/D;CACF,EAAA,CAAG;CACH,IAAI,cAAc,KAAA,GAAW,gBAAgB,IAAI,WAAW,YAAY;CACxE,OAAO;AACT"}
1
+ {"version":3,"file":"github-copilot.js","names":[],"sources":["../../src/providers/github-copilot.ts"],"sourcesContent":["/**\n * GitHub Copilot OAuth Provider\n *\n * Uses OAuth tokens from AuthStorage to authenticate with GitHub Copilot's chat API.\n * The Copilot API speaks an OpenAI-compatible chat format, so we plug\n * `@ai-sdk/openai-compatible` into Copilot's API URL and use a custom fetch to inject\n * the bearer token and Copilot-specific headers.\n *\n * Inspired by:\n * - opencode: https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/plugin/github-copilot/copilot.ts\n * - pi-mono: https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/utils/oauth/github-copilot.ts\n */\n\nimport { createOpenAICompatible } from '@ai-sdk/openai-compatible';\nimport type { MastraModelConfig } from '@mastra/core/llm';\nimport type { JSONSchema7 } from '@mastra/schema-compat';\nimport { applyCompatLayer, GoogleSchemaCompatLayer } from '@mastra/schema-compat';\nimport { wrapLanguageModel } from 'ai';\nimport type { LanguageModelMiddleware } from 'ai';\nimport { ProviderAuthRequiredError, rejectUnauthorizedResponse } from '../auth/provider-auth-error.js';\nimport { COPILOT_HEADERS, fetchCopilotModels, getGitHubCopilotBaseUrl } from '../auth/providers/github-copilot.js';\nimport type { CopilotModelEntry, GitHubCopilotCredentials } from '../auth/providers/github-copilot.js';\nimport { AuthStorage } from '../auth/storage.js';\nimport type { CredentialStore, OAuthCredential } from '../auth/types.js';\n\nconst COPILOT_PROVIDER_ID = 'github-copilot';\n\n// Singleton auth storage instance (shared with claude-max.ts / openai-codex.ts when not overridden).\nlet authStorageInstance: AuthStorage | null = null;\n\n/** Get or create the shared AuthStorage instance. */\nexport function getAuthStorage(): AuthStorage {\n if (!authStorageInstance) {\n authStorageInstance = new AuthStorage();\n }\n return authStorageInstance;\n}\n\n/** Set a custom AuthStorage instance (useful for tests / TUI integration). */\nexport function setAuthStorage(storage: AuthStorage | undefined): void {\n authStorageInstance = storage ?? null;\n}\n\n/**\n * Heuristic: did this request come from the agent (e.g. tool result follow-ups) rather\n * than a fresh user turn? Mirrors opencode's `isAgent` logic — Copilot bills these\n * differently via the `x-initiator` header.\n */\nfunction detectIsAgent(body: unknown): boolean {\n if (!body || typeof body !== 'object') return false;\n const obj = body as Record<string, unknown>;\n\n const messages = obj.messages;\n if (Array.isArray(messages) && messages.length > 0) {\n const last = messages[messages.length - 1] as { role?: string; content?: unknown };\n if (last?.role && last.role !== 'user') return true;\n if (Array.isArray(last?.content)) {\n // If the last user turn carries any tool_result parts, treat it as an agent turn.\n const hasToolResult = last.content.some(\n (part: unknown) => part && typeof part === 'object' && (part as { type?: string }).type === 'tool_result',\n );\n if (hasToolResult) return true;\n }\n }\n\n const input = obj.input;\n if (Array.isArray(input) && input.length > 0) {\n const last = input[input.length - 1] as { role?: string };\n if (last?.role && last.role !== 'user') return true;\n }\n\n return false;\n}\n\n/** Detect image/vision content in a request body. */\nfunction detectIsVision(body: unknown): boolean {\n if (!body || typeof body !== 'object') return false;\n const obj = body as Record<string, unknown>;\n\n const matchPart = (part: unknown): boolean => {\n if (!part || typeof part !== 'object') return false;\n const t = (part as { type?: string }).type;\n return t === 'image' || t === 'image_url' || t === 'input_image';\n };\n\n const messages = obj.messages;\n if (Array.isArray(messages)) {\n return messages.some(\n (msg: unknown) =>\n msg &&\n typeof msg === 'object' &&\n Array.isArray((msg as { content?: unknown }).content) &&\n ((msg as { content: unknown[] }).content as unknown[]).some(matchPart),\n );\n }\n\n const input = obj.input;\n if (Array.isArray(input)) {\n return input.some(\n (item: unknown) =>\n item &&\n typeof item === 'object' &&\n Array.isArray((item as { content?: unknown }).content) &&\n ((item as { content: unknown[] }).content as unknown[]).some(matchPart),\n );\n }\n\n return false;\n}\n\n/**\n * Build a fetch wrapper that authenticates with GitHub Copilot OAuth.\n *\n * - Injects the short-lived Copilot bearer token (auto-refreshed by AuthStorage).\n * - Adds the VS Code-like Copilot headers required by the API.\n * - Rewrites the request URL onto the per-token API base when `rewriteUrl` is true.\n */\nexport function buildGitHubCopilotOAuthFetch(\n opts: { authStorage?: CredentialStore; rewriteUrl?: boolean } = {},\n): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n const storage = opts.authStorage ?? getAuthStorage();\n storage.reload();\n\n const cred = storage.get(COPILOT_PROVIDER_ID);\n if (!cred || cred.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to GitHub Copilot.');\n }\n\n let accessToken: string | undefined;\n let activeCred: OAuthCredential | undefined;\n if (storage.getOAuthCredential) {\n activeCred = await storage.getOAuthCredential(COPILOT_PROVIDER_ID);\n accessToken = activeCred?.access;\n } else {\n accessToken = await storage.getApiKey(COPILOT_PROVIDER_ID);\n storage.reload();\n const reloaded = storage.get(COPILOT_PROVIDER_ID);\n activeCred = reloaded?.type === 'oauth' ? { ...reloaded, access: accessToken ?? reloaded.access } : undefined;\n }\n if (!accessToken || !activeCred) {\n throw new ProviderAuthRequiredError('Failed to refresh the GitHub Copilot token.');\n }\n const enterpriseUrl = (activeCred as GitHubCopilotCredentials).enterpriseUrl;\n\n let parsedBody: unknown;\n try {\n const body = await request.clone().text();\n parsedBody = body ? JSON.parse(body) : undefined;\n } catch {\n parsedBody = undefined;\n }\n const isAgent = detectIsAgent(parsedBody);\n const isVision = detectIsVision(parsedBody);\n\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n headers.set('x-initiator', isAgent ? 'agent' : 'user');\n headers.set('Openai-Intent', 'conversation-edits');\n if (isVision) {\n headers.set('Copilot-Vision-Request', 'true');\n }\n for (const [key, value] of Object.entries(COPILOT_HEADERS)) {\n // Only set if caller didn't already provide it (allow overrides for tests).\n if (!headers.has(key)) {\n headers.set(key, value);\n }\n }\n\n const finalUrl =\n opts.rewriteUrl !== false ? rewriteToCopilotBase(request, accessToken, enterpriseUrl) : new URL(request.url);\n\n const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body;\n const finalRequest = new Request(finalUrl, {\n method: request.method,\n headers,\n body,\n signal: request.signal,\n redirect: request.redirect,\n integrity: request.integrity,\n ...(body ? ({ duplex: 'half' } as RequestInit) : {}),\n });\n try {\n return await rejectUnauthorizedResponse(await fetch(finalRequest), {\n providerName: 'GitHub Copilot',\n requestUrl: finalUrl.toString(),\n });\n } catch (error) {\n if (error && typeof error === 'object') {\n Object.assign(error as Record<string, unknown>, {\n requestUrl: finalUrl.toString(),\n });\n }\n throw error;\n }\n }) as typeof fetch;\n}\n\nfunction rewriteToCopilotBase(url: string | URL | Request, token: string, enterpriseDomain?: string): URL {\n const original = url instanceof URL ? url : new URL(typeof url === 'string' ? url : (url as Request).url);\n const base = new URL(getGitHubCopilotBaseUrl(token, enterpriseDomain));\n // Copilot's OpenAI-compatible API serves endpoints at the root of the base host\n // (`/chat/completions`, `/responses`, `/models`, ...) — not under a `/v1/` prefix\n // like api.openai.com does. The @ai-sdk/openai default baseURL is\n // `https://api.openai.com/v1`, so the SDK builds requests like\n // `https://api.openai.com/v1/chat/completions`. Strip the leading `/v1` segment\n // when rewriting onto the Copilot base or Copilot will return 404 Not Found.\n const pathname = original.pathname.replace(/^\\/v1(\\/|$)/, '/');\n return new URL(`${pathname}${original.search}`, base);\n}\n\nfunction isGeminiModel(modelId: string): boolean {\n return modelId.startsWith('gemini-');\n}\n\nfunction applyGeminiSchemaCompatToTools(modelId: string, tools: unknown): unknown {\n if (!Array.isArray(tools)) {\n return tools;\n }\n\n const compatLayer = new GoogleSchemaCompatLayer({\n provider: COPILOT_PROVIDER_ID,\n modelId,\n supportsStructuredOutputs: false,\n });\n\n return tools.map(tool => {\n if (!tool || typeof tool !== 'object' || (tool as { type?: unknown }).type !== 'function') {\n return tool;\n }\n\n const functionTool = tool as { inputSchema?: JSONSchema7 };\n if (!functionTool.inputSchema) {\n return tool;\n }\n\n return {\n ...functionTool,\n inputSchema: applyCompatLayer({\n schema: functionTool.inputSchema,\n compatLayers: [compatLayer],\n mode: 'aiSdkSchema',\n }).jsonSchema as JSONSchema7,\n };\n });\n}\n\n/** Middleware that prevents sending parameters Copilot's endpoint rejects. */\nfunction createCopilotMiddleware(modelId: string): LanguageModelMiddleware {\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n if (params.temperature !== undefined && params.temperature !== null) {\n delete params.topP;\n }\n\n if (isGeminiModel(modelId)) {\n (params as { tools?: unknown }).tools = applyGeminiSchemaCompatToTools(\n modelId,\n (params as { tools?: unknown }).tools,\n );\n }\n\n return params;\n },\n };\n}\n\n/**\n * Creates a model that talks to GitHub Copilot using OAuth credentials.\n *\n * Copilot's `/chat/completions` endpoint is OpenAI-compatible, but GitHub Copilot\n * is not OpenAI. Use the generic OpenAI-compatible adapter with Copilot's base URL\n * instead of the OpenAI provider plus URL rewriting.\n */\nexport function githubCopilotProvider(\n modelId: string = 'gpt-4.1',\n options?: { headers?: Record<string, string>; authStorage?: CredentialStore },\n): MastraModelConfig {\n const headers = options?.headers;\n const copilot = createOpenAICompatible({\n name: COPILOT_PROVIDER_ID,\n baseURL: 'https://api.githubcopilot.com',\n apiKey: process.env.NODE_ENV === 'test' || process.env.VITEST ? 'test-api-key' : 'oauth-placeholder',\n headers,\n fetch:\n process.env.NODE_ENV === 'test' || process.env.VITEST\n ? undefined\n : (buildGitHubCopilotOAuthFetch({ rewriteUrl: false, authStorage: options?.authStorage }) as any),\n });\n\n return wrapLanguageModel({\n model: copilot.chatModel(modelId),\n middleware: [createCopilotMiddleware(modelId)],\n });\n}\n\n// ---------------------------------------------------------------------------\n// Live model catalog\n// ---------------------------------------------------------------------------\n\n/**\n * Hard-coded fallback advertised when the live `/models` request fails (network\n * down, expired token, etc.). Keep this conservative because the live catalog is\n * the source of truth for the user's currently-enabled Copilot models.\n *\n * Available across all paid Copilot tiers and free of premium-request charges.\n */\nconst COPILOT_FALLBACK_MODELS: CopilotModelEntry[] = [\n {\n id: 'gpt-4.1',\n name: 'GPT-4.1',\n vendor: 'OpenAI',\n supportedEndpoints: ['/chat/completions'],\n isAnthropicShaped: false,\n supportsVision: true,\n supportsToolCalls: true,\n },\n];\n\nconst CATALOG_TTL_MS = 10 * 60 * 1000;\nconst CATALOG_FAILURE_TTL_MS = 60 * 1000;\nconst CATALOG_FETCH_TIMEOUT_MS = 5_000;\n\ninterface CatalogCacheEntry {\n fetchedAt: number;\n ttl: number;\n models: CopilotModelEntry[];\n}\n\nconst catalogCache = new Map<string, CatalogCacheEntry>();\nconst inflightFetches = new Map<string, Promise<CopilotModelEntry[]>>();\n\n/**\n * Resolve the account identity for a credential read through `getApiKey`, which\n * may await a refresh with a rotation landing inside it. The registry is\n * consulted on both sides of that await, and only an id that survived it names\n * the account that produced the token: an id that moved describes whichever\n * account is active *now*, so trusting it would file this fetch's models under\n * the wrong account and serve them to it. Unidentified is the safe answer — the\n * caller then skips the TTL cache, exactly as it does when the store exposes no\n * registry at all. (`AuthStorage` never needs this: its snapshots name their\n * account.)\n */\nfunction readStableAccountId(storage: CredentialStore, idBeforeToken: string | undefined): string | undefined {\n const idAfterToken = storage.getActiveAccount?.(COPILOT_PROVIDER_ID)?.id;\n return idBeforeToken !== undefined && idBeforeToken === idAfterToken ? idAfterToken : undefined;\n}\n\n/** Reset the in-process Copilot catalog cache (test seam, also useful after logout). */\nexport function clearCopilotCatalogCache(): void {\n catalogCache.clear();\n inflightFetches.clear();\n}\n\n/**\n * Return the user's currently-available Copilot models.\n *\n * - Returns `[]` when the user is not logged in to GitHub Copilot.\n * - Returns the cached list when a recent fetch succeeded.\n * - On the cache-miss / expired path, fetches `/models` with a 5s timeout, filters\n * to picker-enabled and non-policy-disabled models, then caches for 10 minutes.\n * - On fetch failure, returns a small hard-coded fallback (so packs still work\n * offline) and caches that for 1 minute to avoid hammering the API.\n *\n * Concurrent calls during a fetch share the inflight promise.\n */\nexport async function getCopilotModelCatalog(\n opts: { authStorage?: CredentialStore } = {},\n): Promise<CopilotModelEntry[]> {\n const storage = opts.authStorage ?? getAuthStorage();\n\n // Resolve one coherent credential snapshot before consulting the cache so a\n // token can never be paired with another account's enterprise endpoint.\n // `getOAuthCredential` is optional (deployed stores have no local registry),\n // so fall back to the always-present `getApiKey`, taking any enterprise\n // endpoint from the same slot via `get()`.\n let accessToken: string | undefined;\n let accountInstanceId: string | undefined;\n let enterpriseUrl: string | undefined;\n if (storage.getOAuthCredential) {\n const credential = await storage.getOAuthCredential(COPILOT_PROVIDER_ID);\n if (!credential || credential.type !== 'oauth') return [];\n accessToken = credential.access;\n // A snapshot that names its account is coherent by construction — the store\n // read the identity and the token in the same breath. One that does not\n // cannot be attributed to a registry entry we then read afterwards, because\n // a rotation may have landed in between; leave it unnamed so the caller\n // skips the TTL cache instead of guessing. (`AuthStorage` always stamps.)\n accountInstanceId = credential.accountInstanceId;\n enterpriseUrl = (credential as GitHubCopilotCredentials).enterpriseUrl;\n } else {\n // This store republishes its registry but not the OAuth snapshot API, so the\n // identity has to come from the registry. `getApiKey` may await a refresh and\n // a rotation can land inside it, so bracket the fetch: only an id that held\n // still names the account that produced this token.\n const idBeforeToken = storage.getActiveAccount?.(COPILOT_PROVIDER_ID)?.id;\n accessToken = await storage.getApiKey(COPILOT_PROVIDER_ID);\n if (!accessToken) return [];\n accountInstanceId = readStableAccountId(storage, idBeforeToken);\n const stored = storage.get(COPILOT_PROVIDER_ID);\n if (stored?.type === 'oauth') {\n enterpriseUrl = (stored as GitHubCopilotCredentials).enterpriseUrl;\n }\n // The id is non-secret, and keying per account is what keeps entitlements\n // (per account, per `github-copilot-catalog.test.ts`) from leaking across a\n // rotation. Deriving the key from the token is what `cba11389e7` removed.\n }\n const baseUrl = getGitHubCopilotBaseUrl(accessToken, enterpriseUrl);\n // No identity means the store cannot name the account it just served, so the\n // TTL cache cannot be keyed per account — one entry would outlive the request\n // and serve those models to every other account of the store. The in-flight\n // map is no safer: two callers that resolved *different* accounts of an\n // unnamed store would otherwise collide on one key and share whichever\n // catalog was fetched first. So both caches are identified-only, and an\n // unnamed store pays one `/models` request per call.\n const credentialKey = accountInstanceId === undefined ? undefined : `${accountInstanceId}\\0${baseUrl}`;\n const dedupeKey = credentialKey;\n\n const now = Date.now();\n const cached = credentialKey ? catalogCache.get(credentialKey) : undefined;\n if (cached && now - cached.fetchedAt < cached.ttl) return cached.models;\n\n const existingFetch = dedupeKey === undefined ? undefined : inflightFetches.get(dedupeKey);\n if (existingFetch) return existingFetch;\n\n const fetchPromise = (async (): Promise<CopilotModelEntry[]> => {\n try {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), CATALOG_FETCH_TIMEOUT_MS);\n try {\n const models = await fetchCopilotModels({\n baseUrl,\n bearerToken: accessToken,\n signal: controller.signal,\n });\n if (credentialKey) catalogCache.set(credentialKey, { fetchedAt: Date.now(), ttl: CATALOG_TTL_MS, models });\n return models;\n } finally {\n clearTimeout(timer);\n }\n } catch (error) {\n if (credentialKey) {\n catalogCache.set(credentialKey, {\n fetchedAt: Date.now(),\n ttl: CATALOG_FAILURE_TTL_MS,\n models: COPILOT_FALLBACK_MODELS,\n });\n }\n console.warn(\n 'Failed to fetch live GitHub Copilot models, using fallback list:',\n error instanceof Error ? error.message : error,\n );\n return COPILOT_FALLBACK_MODELS;\n } finally {\n if (dedupeKey !== undefined) inflightFetches.delete(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AQ,IAAI,mBAAmB;EAC9C,IAAI,QAAQ,SAAS,SACnB,gBAAiB,OAAoC;CAKzD;CACA,MAAM,UAAU,wBAAwB,aAAa,aAAa;CAQlE,MAAM,gBAAgB,sBAAsB,KAAA,IAAY,KAAA,IAAY,GAAG,kBAAkB,IAAI;CAC7F,MAAM,YAAY;CAElB,MAAM,MAAM,KAAK,IAAI;CACrB,MAAM,SAAS,gBAAgB,aAAa,IAAI,aAAa,IAAI,KAAA;CACjE,IAAI,UAAU,MAAM,OAAO,YAAY,OAAO,KAAK,OAAO,OAAO;CAEjE,MAAM,gBAAgB,cAAc,KAAA,IAAY,KAAA,IAAY,gBAAgB,IAAI,SAAS;CACzF,IAAI,eAAe,OAAO;CAE1B,MAAM,gBAAgB,YAA0C;EAC9D,IAAI;GACF,MAAM,aAAa,IAAI,gBAAgB;GACvC,MAAM,QAAQ,iBAAiB,WAAW,MAAM,GAAG,wBAAwB;GAC3E,IAAI;IACF,MAAM,SAAS,MAAM,mBAAmB;KACtC;KACA,aAAa;KACb,QAAQ,WAAW;IACrB,CAAC;IACD,IAAI,eAAe,aAAa,IAAI,eAAe;KAAE,WAAW,KAAK,IAAI;KAAG,KAAK;KAAgB;IAAO,CAAC;IACzG,OAAO;GACT,UAAU;IACR,aAAa,KAAK;GACpB;EACF,SAAS,OAAO;GACd,IAAI,eACF,aAAa,IAAI,eAAe;IAC9B,WAAW,KAAK,IAAI;IACpB,KAAK;IACL,QAAQ;GACV,CAAC;GAEH,QAAQ,KACN,oEACA,iBAAiB,QAAQ,MAAM,UAAU,KAC3C;GACA,OAAO;EACT,UAAU;GACR,IAAI,cAAc,KAAA,GAAW,gBAAgB,OAAO,SAAS;EAC/D;CACF,EAAA,CAAG;CACH,IAAI,cAAc,KAAA,GAAW,gBAAgB,IAAI,WAAW,YAAY;CACxE,OAAO;AACT"}
@@ -271,7 +271,6 @@ async function aggregateCodexStream(response) {
271
271
  break;
272
272
  case "response.error":
273
273
  case "error": throw new Error(`Codex stream error: ${JSON.stringify(payload.error ?? payload)}`);
274
- default: break;
275
274
  }
276
275
  };
277
276
  const processChunk = (chunk) => {
@@ -326,21 +325,25 @@ function openaiCodexProvider(modelId = "codex-mini-latest", options) {
326
325
  const middleware = createCodexMiddleware(reasoningEffort);
327
326
  const headers = options?.headers;
328
327
  const baseURL = process.env.OPENAI_BASE_URL;
329
- if (process.env.NODE_ENV === "test" || process.env.VITEST) return wrapLanguageModel({
330
- model: createOpenAI({
328
+ if (process.env.NODE_ENV === "test" || process.env.VITEST) {
329
+ const openai = createOpenAI({
331
330
  apiKey: "test-api-key",
332
331
  baseURL,
333
332
  headers
334
- }).responses(modelId),
335
- middleware: [middleware]
333
+ });
334
+ return wrapLanguageModel({
335
+ model: openai.responses(modelId),
336
+ middleware: [middleware]
337
+ });
338
+ }
339
+ const openai = createOpenAI({
340
+ apiKey: "oauth-dummy-key",
341
+ baseURL,
342
+ headers,
343
+ fetch: buildOpenAICodexOAuthFetch({ authStorage: options?.authStorage })
336
344
  });
337
345
  return wrapLanguageModel({
338
- model: createOpenAI({
339
- apiKey: "oauth-dummy-key",
340
- baseURL,
341
- headers,
342
- fetch: buildOpenAICodexOAuthFetch({ authStorage: options?.authStorage })
343
- }).responses(modelId),
346
+ model: openai.responses(modelId),
344
347
  middleware: [middleware]
345
348
  });
346
349
  }
@@ -1 +1 @@
1
- {"version":3,"file":"openai-codex.js","names":[],"sources":["../../src/providers/openai-codex.ts"],"sourcesContent":["/**\n * OpenAI Codex OAuth Provider\n *\n * Uses OAuth tokens from AuthStorage to authenticate with ChatGPT Plus/Pro subscription.\n * This allows access to OpenAI models through the ChatGPT OAuth flow.\n *\n * Inspired by opencode's Codex plugin implementation:\n * https://github.com/sst/opencode/blob/main/packages/opencode/src/plugin/codex.ts\n */\n\nimport { createOpenAI } from '@ai-sdk/openai';\nimport type { MastraModelConfig } from '@mastra/core/llm';\nimport { wrapLanguageModel } from 'ai';\nimport type { LanguageModelMiddleware } from 'ai';\nimport { ProviderAuthRequiredError, rejectUnauthorizedResponse } from '../auth/provider-auth-error.js';\nimport { AuthStorage } from '../auth/storage.js';\nimport type { CredentialStore } from '../auth/types.js';\nimport { runCatalogThinkingLevel } from '../thinking-catalog.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nexport { supportsMaxReasoningEffort } from '../thinking.js';\n\n// Codex API endpoint (not standard OpenAI API)\nconst CODEX_API_ENDPOINT = 'https://chatgpt.com/backend-api/codex/responses';\nconst CODEX_ORIGINATOR = 'mastracode';\nconst CODEX_USER_AGENT = 'mastracode';\n\n// Singleton auth storage instance (shared with claude-max.ts)\nlet authStorageInstance: AuthStorage | null = null;\n\n/**\n * Get or create the shared AuthStorage instance\n */\nexport function getAuthStorage(): AuthStorage {\n if (!authStorageInstance) {\n authStorageInstance = new AuthStorage();\n }\n return authStorageInstance;\n}\n\n/**\n * Set a custom AuthStorage instance (useful for TUI integration)\n */\nexport function setAuthStorage(storage: AuthStorage | undefined): void {\n authStorageInstance = storage ?? null;\n}\n\n// Default instructions for Codex API (required)\nconst CODEX_INSTRUCTIONS = `You are an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user.\n\nIMPORTANT: You should be concise, direct, and helpful. Focus on solving the user's problem efficiently.`;\n\nexport type ThinkingLevel = ThinkingLevelSetting;\n\nconst GPT5_MODEL_RE = /^gpt-5(?:\\.|-|$)/;\n\nexport function getEffectiveThinkingLevel(modelId: string, level: ThinkingLevel): ThinkingLevel {\n // GPT-5.* models on Codex require at least low reasoning.\n const requestedLevel = GPT5_MODEL_RE.test(modelId) && level === 'off' ? 'low' : level;\n return runCatalogThinkingLevel(`openai/${modelId}`, requestedLevel);\n}\n\n// Map thinkingLevel state values to OpenAI reasoningEffort values.\n// undefined means omit the parameter (no reasoning). Model-dependent fitting\n// happens in getEffectiveThinkingLevel before this lookup.\nexport const THINKING_LEVEL_TO_REASONING_EFFORT: Record<ThinkingLevel, string | undefined> = {\n off: undefined,\n low: 'low',\n medium: 'medium',\n high: 'high',\n xhigh: 'xhigh',\n max: 'max',\n};\n\n/**\n * Create Codex middleware with the given reasoning effort level.\n */\nexport function createCodexMiddleware(reasoningEffort?: string): LanguageModelMiddleware {\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n // Remove topP if temperature is set (OpenAI doesn't like both)\n if (params.temperature !== undefined && params.temperature !== null) {\n delete params.topP;\n }\n\n // Codex API requires specific settings via providerOptions\n // Use type assertion to satisfy JSONValue constraints\n params.providerOptions = {\n ...params.providerOptions,\n openai: {\n ...(params.providerOptions?.openai ?? {}),\n instructions: CODEX_INSTRUCTIONS,\n // Codex API requires store to be false\n store: false,\n // Enable reasoning for Codex models — without this, the model\n // skips the reasoning/action phase and goes straight to final_answer,\n // resulting in narration instead of tool calls.\n ...(reasoningEffort ? { reasoningEffort } : {}),\n },\n } as typeof params.providerOptions;\n\n return params;\n },\n };\n}\n\n/**\n * Create middleware that forwards a resolved reasoning effort to an\n * OpenAI-compatible provider as `providerOptions[providerKey].reasoningEffort`.\n *\n * Returns `undefined` when `reasoningEffort` is unset so callers wrap nothing\n * (e.g. thinking level `off`), leaving the wire request without a\n * `reasoning_effort` field. Unlike {@link createCodexMiddleware}, it does not\n * inject Codex-only options (instructions/store); it only sets reasoning\n * effort, keyed by the caller-supplied provider id.\n */\nexport function createReasoningEffortMiddleware(\n providerKey: string,\n reasoningEffort?: string,\n): LanguageModelMiddleware | undefined {\n if (!reasoningEffort) return undefined;\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n params.providerOptions = {\n ...params.providerOptions,\n [providerKey]: {\n ...(params.providerOptions?.[providerKey] ?? {}),\n reasoningEffort,\n },\n } as typeof params.providerOptions;\n\n return params;\n },\n };\n}\n\n/**\n * Get a live OAuth bearer token for the Codex OAuth credential.\n *\n * Refreshes the token if it's expired, and returns the credential's\n * accountId alongside the access token. Throws if the user isn't logged in\n * or if the refresh fails.\n *\n * This is the only piece of Codex auth that is genuinely shared between\n * the main agent's fetch (`buildOpenAICodexOAuthFetch`) and the Stagehand\n * fetch (`buildCodexStagehandFetch`).\n */\nasync function getCodexBearer(\n authStorage?: CredentialStore,\n): Promise<{ accessToken: string; accountId: string | undefined }> {\n const storage = authStorage ?? getAuthStorage();\n storage.reload();\n\n let cred = storage.get('openai-codex');\n if (!cred || cred.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to OpenAI Codex.');\n }\n\n if (storage.getOAuthCredential) {\n cred = await storage.getOAuthCredential('openai-codex');\n if (!cred) throw new ProviderAuthRequiredError('Failed to refresh the OpenAI Codex token.');\n } else if (Date.now() >= cred.expires) {\n const refreshedToken = await storage.getApiKey('openai-codex');\n if (!refreshedToken) {\n throw new ProviderAuthRequiredError('Failed to refresh the OpenAI Codex token.');\n }\n storage.reload();\n const reloaded = storage.get('openai-codex');\n if (!reloaded || reloaded.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to OpenAI Codex.');\n }\n cred = { ...reloaded, access: refreshedToken };\n }\n\n return { accessToken: cred.access, accountId: (cred as any).accountId as string | undefined };\n}\n\n/**\n * Build a fetch function that handles OpenAI Codex OAuth.\n * Preserves non-authorization headers from init.\n * When rewriteUrl is true (default), rewrites /v1/responses and /chat/completions\n * to the Codex API endpoint. Set rewriteUrl: false for gateway usage where the\n * SDK already targets the correct URL.\n */\nexport function buildOpenAICodexOAuthFetch(\n opts: { authStorage?: CredentialStore; rewriteUrl?: boolean } = {},\n): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n const { accessToken, accountId } = await getCodexBearer(opts.authStorage);\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n\n // Preserve Request headers and let explicit init headers override them.\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n if (!headers.has('originator')) {\n headers.set('originator', CODEX_ORIGINATOR);\n }\n if (!headers.has('User-Agent')) {\n headers.set('User-Agent', CODEX_USER_AGENT);\n }\n if (accountId) {\n headers.set('ChatGPT-Account-ID', accountId);\n }\n\n // URL rewriting — only when rewriteUrl !== false\n const parsed = new URL(request.url);\n const shouldRewrite =\n opts.rewriteUrl !== false &&\n (parsed.pathname.includes('/v1/responses') || parsed.pathname.includes('/chat/completions'));\n const finalUrl = shouldRewrite ? new URL(CODEX_API_ENDPOINT) : parsed;\n\n const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body;\n const finalRequest = new Request(finalUrl, {\n method: request.method,\n headers,\n body,\n signal: request.signal,\n redirect: request.redirect,\n integrity: request.integrity,\n ...(body ? ({ duplex: 'half' } as RequestInit) : {}),\n });\n try {\n return await rejectUnauthorizedResponse(await fetch(finalRequest), {\n providerName: 'OpenAI Codex',\n requestUrl: finalUrl.toString(),\n });\n } catch (error) {\n if (error && typeof error === 'object') {\n Object.assign(error as Record<string, unknown>, {\n requestUrl: finalUrl.toString(),\n });\n }\n throw error;\n }\n }) as typeof fetch;\n}\n\n/**\n * Build a fetch function for Stagehand-on-Codex.\n *\n * The Codex backend has two requirements that AI SDK's non-streaming\n * `generateText` path doesn't naturally satisfy:\n *\n * 1. `stream: true` must be set on every request body.\n * 2. The response is delivered as Server-Sent Events; AI SDK's\n * non-streaming code path expects a single JSON body.\n *\n * This fetch forces streaming on the outgoing request, collects the SSE\n * events, and synthesizes the non-streaming JSON shape that\n * `@ai-sdk/openai`'s Responses API parser expects.\n *\n * Headers, OAuth refresh, and URL targeting are handled by the caller via\n * `baseURL` / `headers` on the AI SDK provider; this fetch only injects the\n * live OAuth bearer per call.\n */\nexport function buildCodexStagehandFetch(authStorage: AuthStorage): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n // Refresh + inject the OAuth bearer per call.\n const { accessToken } = await getCodexBearer(authStorage);\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n headers.set('Accept', 'text/event-stream');\n\n // Stagehand sends JSON; force stream: true while preserving Request bodies.\n let body = await request.clone().text();\n try {\n const parsed = JSON.parse(body) as Record<string, unknown>;\n parsed.stream = true;\n body = JSON.stringify(parsed);\n if (!headers.has('content-type')) headers.set('content-type', 'application/json');\n } catch {\n // Not JSON; leave as-is.\n }\n\n const upstream = await fetch(\n new Request(request, {\n headers,\n ...(request.method === 'GET' || request.method === 'HEAD' ? {} : { body }),\n }),\n );\n if (!upstream.ok) return upstream;\n\n // Aggregate SSE -> synthesized non-streaming Response\n const aggregated = await aggregateCodexStream(upstream);\n return new Response(aggregated, {\n status: 200,\n headers: { 'content-type': 'application/json' },\n });\n }) as typeof fetch;\n}\n\n/**\n * Read an SSE Response and reduce it to a single JSON string matching the\n * non-streaming OpenAI Responses-API shape.\n *\n * Event vocabulary we care about (per OpenAI Responses API streaming):\n * - response.created → carries `response` object (id, model, usage stub)\n * - response.output_item.added/done → output items (message, reasoning, etc.)\n * - response.output_text.delta → text chunks\n * - response.completed → final `response` snapshot incl. usage\n * - response.error / error → bubble up as a thrown body\n *\n * Reasoning events (`response.reasoning_summary.*`) are intentionally ignored\n * for the non-streaming text response.\n */\nasync function aggregateCodexStream(response: Response): Promise<string> {\n if (!response.body) {\n throw new Error('Codex streaming response had no body');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder('utf-8');\n let buffer = '';\n\n let finalResponse: any = null;\n let createdResponse: any = null;\n // Track output_items by index so we can rebuild the final array\n const items = new Map<number, any>();\n // Accumulate output_text deltas keyed by item_index + content_index\n const textBuffers = new Map<string, string>();\n\n const handleEvent = (event: { event?: string; data?: string }) => {\n if (!event.data || event.data === '[DONE]') return;\n let payload: any;\n try {\n payload = JSON.parse(event.data);\n } catch {\n return;\n }\n const type: string = payload.type ?? event.event ?? '';\n\n switch (type) {\n case 'response.created': {\n createdResponse = payload.response ?? createdResponse;\n break;\n }\n case 'response.output_item.added': {\n if (typeof payload.output_index === 'number' && payload.item) {\n items.set(payload.output_index, payload.item);\n }\n break;\n }\n case 'response.output_item.done': {\n if (typeof payload.output_index === 'number' && payload.item) {\n items.set(payload.output_index, payload.item);\n }\n break;\n }\n case 'response.output_text.delta': {\n const key = `${payload.output_index}:${payload.content_index ?? 0}`;\n textBuffers.set(key, (textBuffers.get(key) ?? '') + (payload.delta ?? ''));\n break;\n }\n case 'response.completed': {\n finalResponse = payload.response ?? finalResponse;\n break;\n }\n case 'response.error':\n case 'error': {\n throw new Error(`Codex stream error: ${JSON.stringify(payload.error ?? payload)}`);\n }\n default:\n // Ignore reasoning / unknown events\n break;\n }\n };\n\n // SSE parser: events separated by blank line; lines like \"event: x\" / \"data: y\"\n // Normalize CRLF→LF so \\r\\n\\r\\n event boundaries parse correctly (SSE spec allows CRLF).\n const processChunk = (chunk: string) => {\n buffer += chunk.replace(/\\r\\n/g, '\\n');\n let sepIdx: number;\n while ((sepIdx = buffer.indexOf('\\n\\n')) !== -1) {\n const raw = buffer.slice(0, sepIdx);\n buffer = buffer.slice(sepIdx + 2);\n const event: { event?: string; data?: string } = {};\n const dataLines: string[] = [];\n for (const line of raw.split('\\n')) {\n if (line.startsWith('event:')) {\n event.event = line.slice(6).trim();\n } else if (line.startsWith('data:')) {\n dataLines.push(line.slice(5).trimStart());\n }\n }\n if (dataLines.length > 0) {\n event.data = dataLines.join('\\n');\n }\n handleEvent(event);\n }\n };\n\n try {\n while (true) {\n const { value, done } = await reader.read();\n if (done) break;\n processChunk(decoder.decode(value, { stream: true }));\n }\n processChunk(decoder.decode());\n } finally {\n reader.releaseLock();\n }\n\n // Stitch accumulated text deltas back into their items\n const base = finalResponse ?? createdResponse ?? { output: [] };\n const finalItems = Array.from(items.entries())\n .sort(([a], [b]) => a - b)\n .map(([index, item]) => {\n // Patch message-type items' content text using buffered deltas\n if (item?.type === 'message' && Array.isArray(item.content)) {\n item.content = item.content.map((c: any, ci: number) => {\n const key = `${index}:${ci}`;\n if (textBuffers.has(key)) {\n return { ...c, text: textBuffers.get(key) };\n }\n return c;\n });\n }\n return item;\n });\n\n base.output = finalItems.length > 0 ? finalItems : (base.output ?? []);\n\n return JSON.stringify(base);\n}\n\n/**\n * Creates an OpenAI model using ChatGPT OAuth authentication\n * Uses OAuth tokens from AuthStorage (auto-refreshes when needed)\n *\n * IMPORTANT: This uses the Codex API endpoint, not the standard OpenAI API.\n * URLs are rewritten from /v1/responses or /chat/completions to the Codex endpoint.\n */\nexport function openaiCodexProvider(\n modelId: string = 'codex-mini-latest',\n options?: { thinkingLevel?: ThinkingLevel; headers?: Record<string, string>; authStorage?: CredentialStore },\n): MastraModelConfig {\n const requestedLevel: ThinkingLevel = options?.thinkingLevel ?? 'medium';\n const effectiveLevel = getEffectiveThinkingLevel(modelId, requestedLevel);\n const reasoningEffort = THINKING_LEVEL_TO_REASONING_EFFORT[effectiveLevel];\n const middleware = createCodexMiddleware(reasoningEffort);\n const headers = options?.headers;\n\n const baseURL = process.env.OPENAI_BASE_URL;\n\n // Test environment: use API key\n if (process.env.NODE_ENV === 'test' || process.env.VITEST) {\n const openai = createOpenAI({\n apiKey: 'test-api-key',\n baseURL,\n headers,\n });\n return wrapLanguageModel({\n model: openai.responses(modelId),\n middleware: [middleware],\n });\n }\n\n const openai = createOpenAI({\n apiKey: 'oauth-dummy-key',\n baseURL,\n headers,\n fetch: buildOpenAICodexOAuthFetch({ authStorage: options?.authStorage }) as any,\n });\n\n // Use the responses API for Codex models\n // Wrap with middleware\n return wrapLanguageModel({\n model: openai.responses(modelId),\n middleware: [middleware],\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAsBA,MAAM,qBAAqB;AAC3B,MAAM,mBAAmB;AACzB,MAAM,mBAAmB;AAGzB,IAAI,sBAA0C;;;;AAK9C,SA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* OpenAI Codex OAuth Provider\n *\n * Uses OAuth tokens from AuthStorage to authenticate with ChatGPT Plus/Pro subscription.\n * This allows access to OpenAI models through the ChatGPT OAuth flow.\n *\n * Inspired by opencode's Codex plugin implementation:\n * https://github.com/sst/opencode/blob/main/packages/opencode/src/plugin/codex.ts\n */\n\nimport { createOpenAI } from '@ai-sdk/openai';\nimport type { MastraModelConfig } from '@mastra/core/llm';\nimport { wrapLanguageModel } from 'ai';\nimport type { LanguageModelMiddleware } from 'ai';\nimport { ProviderAuthRequiredError, rejectUnauthorizedResponse } from '../auth/provider-auth-error.js';\nimport { AuthStorage } from '../auth/storage.js';\nimport type { CredentialStore } from '../auth/types.js';\nimport { runCatalogThinkingLevel } from '../thinking-catalog.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nexport { supportsMaxReasoningEffort } from '../thinking.js';\n\n// Codex API endpoint (not standard OpenAI API)\nconst CODEX_API_ENDPOINT = 'https://chatgpt.com/backend-api/codex/responses';\nconst CODEX_ORIGINATOR = 'mastracode';\nconst CODEX_USER_AGENT = 'mastracode';\n\n// Singleton auth storage instance (shared with claude-max.ts)\nlet authStorageInstance: AuthStorage | null = null;\n\n/**\n * Get or create the shared AuthStorage instance\n */\nexport function getAuthStorage(): AuthStorage {\n if (!authStorageInstance) {\n authStorageInstance = new AuthStorage();\n }\n return authStorageInstance;\n}\n\n/**\n * Set a custom AuthStorage instance (useful for TUI integration)\n */\nexport function setAuthStorage(storage: AuthStorage | undefined): void {\n authStorageInstance = storage ?? null;\n}\n\n// Default instructions for Codex API (required)\nconst CODEX_INSTRUCTIONS = `You are an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user.\n\nIMPORTANT: You should be concise, direct, and helpful. Focus on solving the user's problem efficiently.`;\n\nexport type ThinkingLevel = ThinkingLevelSetting;\n\nconst GPT5_MODEL_RE = /^gpt-5(?:\\.|-|$)/;\n\nexport function getEffectiveThinkingLevel(modelId: string, level: ThinkingLevel): ThinkingLevel {\n // GPT-5.* models on Codex require at least low reasoning.\n const requestedLevel = GPT5_MODEL_RE.test(modelId) && level === 'off' ? 'low' : level;\n return runCatalogThinkingLevel(`openai/${modelId}`, requestedLevel);\n}\n\n// Map thinkingLevel state values to OpenAI reasoningEffort values.\n// undefined means omit the parameter (no reasoning). Model-dependent fitting\n// happens in getEffectiveThinkingLevel before this lookup.\nexport const THINKING_LEVEL_TO_REASONING_EFFORT: Record<ThinkingLevel, string | undefined> = {\n off: undefined,\n low: 'low',\n medium: 'medium',\n high: 'high',\n xhigh: 'xhigh',\n max: 'max',\n};\n\n/**\n * Create Codex middleware with the given reasoning effort level.\n */\nexport function createCodexMiddleware(reasoningEffort?: string): LanguageModelMiddleware {\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n // Remove topP if temperature is set (OpenAI doesn't like both)\n if (params.temperature !== undefined && params.temperature !== null) {\n delete params.topP;\n }\n\n // Codex API requires specific settings via providerOptions\n // Use type assertion to satisfy JSONValue constraints\n params.providerOptions = {\n ...params.providerOptions,\n openai: {\n ...(params.providerOptions?.openai ?? {}),\n instructions: CODEX_INSTRUCTIONS,\n // Codex API requires store to be false\n store: false,\n // Enable reasoning for Codex models — without this, the model\n // skips the reasoning/action phase and goes straight to final_answer,\n // resulting in narration instead of tool calls.\n ...(reasoningEffort ? { reasoningEffort } : {}),\n },\n } as typeof params.providerOptions;\n\n return params;\n },\n };\n}\n\n/**\n * Create middleware that forwards a resolved reasoning effort to an\n * OpenAI-compatible provider as `providerOptions[providerKey].reasoningEffort`.\n *\n * Returns `undefined` when `reasoningEffort` is unset so callers wrap nothing\n * (e.g. thinking level `off`), leaving the wire request without a\n * `reasoning_effort` field. Unlike {@link createCodexMiddleware}, it does not\n * inject Codex-only options (instructions/store); it only sets reasoning\n * effort, keyed by the caller-supplied provider id.\n */\nexport function createReasoningEffortMiddleware(\n providerKey: string,\n reasoningEffort?: string,\n): LanguageModelMiddleware | undefined {\n if (!reasoningEffort) return undefined;\n return {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => {\n params.providerOptions = {\n ...params.providerOptions,\n [providerKey]: {\n ...(params.providerOptions?.[providerKey] ?? {}),\n reasoningEffort,\n },\n } as typeof params.providerOptions;\n\n return params;\n },\n };\n}\n\n/**\n * Get a live OAuth bearer token for the Codex OAuth credential.\n *\n * Refreshes the token if it's expired, and returns the credential's\n * accountId alongside the access token. Throws if the user isn't logged in\n * or if the refresh fails.\n *\n * This is the only piece of Codex auth that is genuinely shared between\n * the main agent's fetch (`buildOpenAICodexOAuthFetch`) and the Stagehand\n * fetch (`buildCodexStagehandFetch`).\n */\nasync function getCodexBearer(\n authStorage?: CredentialStore,\n): Promise<{ accessToken: string; accountId: string | undefined }> {\n const storage = authStorage ?? getAuthStorage();\n storage.reload();\n\n let cred = storage.get('openai-codex');\n if (!cred || cred.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to OpenAI Codex.');\n }\n\n if (storage.getOAuthCredential) {\n cred = await storage.getOAuthCredential('openai-codex');\n if (!cred) throw new ProviderAuthRequiredError('Failed to refresh the OpenAI Codex token.');\n } else if (Date.now() >= cred.expires) {\n const refreshedToken = await storage.getApiKey('openai-codex');\n if (!refreshedToken) {\n throw new ProviderAuthRequiredError('Failed to refresh the OpenAI Codex token.');\n }\n storage.reload();\n const reloaded = storage.get('openai-codex');\n if (!reloaded || reloaded.type !== 'oauth') {\n throw new ProviderAuthRequiredError('Not logged in to OpenAI Codex.');\n }\n cred = { ...reloaded, access: refreshedToken };\n }\n\n return { accessToken: cred.access, accountId: (cred as any).accountId as string | undefined };\n}\n\n/**\n * Build a fetch function that handles OpenAI Codex OAuth.\n * Preserves non-authorization headers from init.\n * When rewriteUrl is true (default), rewrites /v1/responses and /chat/completions\n * to the Codex API endpoint. Set rewriteUrl: false for gateway usage where the\n * SDK already targets the correct URL.\n */\nexport function buildOpenAICodexOAuthFetch(\n opts: { authStorage?: CredentialStore; rewriteUrl?: boolean } = {},\n): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n const { accessToken, accountId } = await getCodexBearer(opts.authStorage);\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n\n // Preserve Request headers and let explicit init headers override them.\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n if (!headers.has('originator')) {\n headers.set('originator', CODEX_ORIGINATOR);\n }\n if (!headers.has('User-Agent')) {\n headers.set('User-Agent', CODEX_USER_AGENT);\n }\n if (accountId) {\n headers.set('ChatGPT-Account-ID', accountId);\n }\n\n // URL rewriting — only when rewriteUrl !== false\n const parsed = new URL(request.url);\n const shouldRewrite =\n opts.rewriteUrl !== false &&\n (parsed.pathname.includes('/v1/responses') || parsed.pathname.includes('/chat/completions'));\n const finalUrl = shouldRewrite ? new URL(CODEX_API_ENDPOINT) : parsed;\n\n const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body;\n const finalRequest = new Request(finalUrl, {\n method: request.method,\n headers,\n body,\n signal: request.signal,\n redirect: request.redirect,\n integrity: request.integrity,\n ...(body ? ({ duplex: 'half' } as RequestInit) : {}),\n });\n try {\n return await rejectUnauthorizedResponse(await fetch(finalRequest), {\n providerName: 'OpenAI Codex',\n requestUrl: finalUrl.toString(),\n });\n } catch (error) {\n if (error && typeof error === 'object') {\n Object.assign(error as Record<string, unknown>, {\n requestUrl: finalUrl.toString(),\n });\n }\n throw error;\n }\n }) as typeof fetch;\n}\n\n/**\n * Build a fetch function for Stagehand-on-Codex.\n *\n * The Codex backend has two requirements that AI SDK's non-streaming\n * `generateText` path doesn't naturally satisfy:\n *\n * 1. `stream: true` must be set on every request body.\n * 2. The response is delivered as Server-Sent Events; AI SDK's\n * non-streaming code path expects a single JSON body.\n *\n * This fetch forces streaming on the outgoing request, collects the SSE\n * events, and synthesizes the non-streaming JSON shape that\n * `@ai-sdk/openai`'s Responses API parser expects.\n *\n * Headers, OAuth refresh, and URL targeting are handled by the caller via\n * `baseURL` / `headers` on the AI SDK provider; this fetch only injects the\n * live OAuth bearer per call.\n */\nexport function buildCodexStagehandFetch(authStorage: AuthStorage): typeof fetch {\n return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n // Refresh + inject the OAuth bearer per call.\n const { accessToken } = await getCodexBearer(authStorage);\n const { headers: initHeaders, ...requestInit } = init ?? {};\n const request = new Request(url, requestInit);\n const headers = new Headers(url instanceof Request ? url.headers : undefined);\n if (initHeaders) new Headers(initHeaders).forEach((value, key) => headers.set(key, value));\n headers.delete('authorization');\n headers.delete('x-api-key');\n headers.set('Authorization', `Bearer ${accessToken}`);\n headers.set('Accept', 'text/event-stream');\n\n // Stagehand sends JSON; force stream: true while preserving Request bodies.\n let body = await request.clone().text();\n try {\n const parsed = JSON.parse(body) as Record<string, unknown>;\n parsed.stream = true;\n body = JSON.stringify(parsed);\n if (!headers.has('content-type')) headers.set('content-type', 'application/json');\n } catch {\n // Not JSON; leave as-is.\n }\n\n const upstream = await fetch(\n new Request(request, {\n headers,\n ...(request.method === 'GET' || request.method === 'HEAD' ? {} : { body }),\n }),\n );\n if (!upstream.ok) return upstream;\n\n // Aggregate SSE -> synthesized non-streaming Response\n const aggregated = await aggregateCodexStream(upstream);\n return new Response(aggregated, {\n status: 200,\n headers: { 'content-type': 'application/json' },\n });\n }) as typeof fetch;\n}\n\n/**\n * Read an SSE Response and reduce it to a single JSON string matching the\n * non-streaming OpenAI Responses-API shape.\n *\n * Event vocabulary we care about (per OpenAI Responses API streaming):\n * - response.created → carries `response` object (id, model, usage stub)\n * - response.output_item.added/done → output items (message, reasoning, etc.)\n * - response.output_text.delta → text chunks\n * - response.completed → final `response` snapshot incl. usage\n * - response.error / error → bubble up as a thrown body\n *\n * Reasoning events (`response.reasoning_summary.*`) are intentionally ignored\n * for the non-streaming text response.\n */\nasync function aggregateCodexStream(response: Response): Promise<string> {\n if (!response.body) {\n throw new Error('Codex streaming response had no body');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder('utf-8');\n let buffer = '';\n\n let finalResponse: any = null;\n let createdResponse: any = null;\n // Track output_items by index so we can rebuild the final array\n const items = new Map<number, any>();\n // Accumulate output_text deltas keyed by item_index + content_index\n const textBuffers = new Map<string, string>();\n\n const handleEvent = (event: { event?: string; data?: string }) => {\n if (!event.data || event.data === '[DONE]') return;\n let payload: any;\n try {\n payload = JSON.parse(event.data);\n } catch {\n return;\n }\n const type: string = payload.type ?? event.event ?? '';\n\n switch (type) {\n case 'response.created': {\n createdResponse = payload.response ?? createdResponse;\n break;\n }\n case 'response.output_item.added': {\n if (typeof payload.output_index === 'number' && payload.item) {\n items.set(payload.output_index, payload.item);\n }\n break;\n }\n case 'response.output_item.done': {\n if (typeof payload.output_index === 'number' && payload.item) {\n items.set(payload.output_index, payload.item);\n }\n break;\n }\n case 'response.output_text.delta': {\n const key = `${payload.output_index}:${payload.content_index ?? 0}`;\n textBuffers.set(key, (textBuffers.get(key) ?? '') + (payload.delta ?? ''));\n break;\n }\n case 'response.completed': {\n finalResponse = payload.response ?? finalResponse;\n break;\n }\n case 'response.error':\n case 'error': {\n throw new Error(`Codex stream error: ${JSON.stringify(payload.error ?? payload)}`);\n }\n default:\n // Ignore reasoning / unknown events\n break;\n }\n };\n\n // SSE parser: events separated by blank line; lines like \"event: x\" / \"data: y\"\n // Normalize CRLF→LF so \\r\\n\\r\\n event boundaries parse correctly (SSE spec allows CRLF).\n const processChunk = (chunk: string) => {\n buffer += chunk.replace(/\\r\\n/g, '\\n');\n let sepIdx: number;\n while ((sepIdx = buffer.indexOf('\\n\\n')) !== -1) {\n const raw = buffer.slice(0, sepIdx);\n buffer = buffer.slice(sepIdx + 2);\n const event: { event?: string; data?: string } = {};\n const dataLines: string[] = [];\n for (const line of raw.split('\\n')) {\n if (line.startsWith('event:')) {\n event.event = line.slice(6).trim();\n } else if (line.startsWith('data:')) {\n dataLines.push(line.slice(5).trimStart());\n }\n }\n if (dataLines.length > 0) {\n event.data = dataLines.join('\\n');\n }\n handleEvent(event);\n }\n };\n\n try {\n while (true) {\n const { value, done } = await reader.read();\n if (done) break;\n processChunk(decoder.decode(value, { stream: true }));\n }\n processChunk(decoder.decode());\n } finally {\n reader.releaseLock();\n }\n\n // Stitch accumulated text deltas back into their items\n const base = finalResponse ?? createdResponse ?? { output: [] };\n const finalItems = Array.from(items.entries())\n .sort(([a], [b]) => a - b)\n .map(([index, item]) => {\n // Patch message-type items' content text using buffered deltas\n if (item?.type === 'message' && Array.isArray(item.content)) {\n item.content = item.content.map((c: any, ci: number) => {\n const key = `${index}:${ci}`;\n if (textBuffers.has(key)) {\n return { ...c, text: textBuffers.get(key) };\n }\n return c;\n });\n }\n return item;\n });\n\n base.output = finalItems.length > 0 ? finalItems : (base.output ?? []);\n\n return JSON.stringify(base);\n}\n\n/**\n * Creates an OpenAI model using ChatGPT OAuth authentication\n * Uses OAuth tokens from AuthStorage (auto-refreshes when needed)\n *\n * IMPORTANT: This uses the Codex API endpoint, not the standard OpenAI API.\n * URLs are rewritten from /v1/responses or /chat/completions to the Codex endpoint.\n */\nexport function openaiCodexProvider(\n modelId: string = 'codex-mini-latest',\n options?: { thinkingLevel?: ThinkingLevel; headers?: Record<string, string>; authStorage?: CredentialStore },\n): MastraModelConfig {\n const requestedLevel: ThinkingLevel = options?.thinkingLevel ?? 'medium';\n const effectiveLevel = getEffectiveThinkingLevel(modelId, requestedLevel);\n const reasoningEffort = THINKING_LEVEL_TO_REASONING_EFFORT[effectiveLevel];\n const middleware = createCodexMiddleware(reasoningEffort);\n const headers = options?.headers;\n\n const baseURL = process.env.OPENAI_BASE_URL;\n\n // Test environment: use API key\n if (process.env.NODE_ENV === 'test' || process.env.VITEST) {\n const openai = createOpenAI({\n apiKey: 'test-api-key',\n baseURL,\n headers,\n });\n return wrapLanguageModel({\n model: openai.responses(modelId),\n middleware: [middleware],\n });\n }\n\n const openai = createOpenAI({\n apiKey: 'oauth-dummy-key',\n baseURL,\n headers,\n fetch: buildOpenAICodexOAuthFetch({ authStorage: options?.authStorage }) as any,\n });\n\n // Use the responses API for Codex models\n // Wrap with middleware\n return wrapLanguageModel({\n model: openai.responses(modelId),\n middleware: [middleware],\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAsBA,MAAM,qBAAqB;AAC3B,MAAM,mBAAmB;AACzB,MAAM,mBAAmB;AAGzB,IAAI,sBAA0C;;;;AAK9C,SAAgB,iBAA8B;CAC5C,IAAI,CAAC,qBACH,sBAAsB,IAAI,YAAY;CAExC,OAAO;AACT;;;;AAKA,SAAgB,eAAe,SAAwC;CACrE,sBAAsB,WAAW;AACnC;AAGA,MAAM,qBAAqB;;;AAM3B,MAAM,gBAAgB;AAEtB,SAAgB,0BAA0B,SAAiB,OAAqC;CAE9F,MAAM,iBAAiB,cAAc,KAAK,OAAO,KAAK,UAAU,QAAQ,QAAQ;CAChF,OAAO,wBAAwB,UAAU,WAAW,cAAc;AACpE;AAKA,MAAa,qCAAgF;CAC3F,KAAK,KAAA;CACL,KAAK;CACL,QAAQ;CACR,MAAM;CACN,OAAO;CACP,KAAK;AACP;;;;AAKA,SAAgB,sBAAsB,iBAAmD;CACvF,OAAO;EACL,sBAAsB;EACtB,iBAAiB,OAAO,EAAE,aAAa;GAErC,IAAI,OAAO,gBAAgB,KAAA,KAAa,OAAO,gBAAgB,MAC7D,OAAO,OAAO;GAKhB,OAAO,kBAAkB;IACvB,GAAG,OAAO;IACV,QAAQ;KACN,GAAI,OAAO,iBAAiB,UAAU,CAAC;KACvC,cAAc;KAEd,OAAO;KAIP,GAAI,kBAAkB,EAAE,gBAAgB,IAAI,CAAC;IAC/C;GACF;GAEA,OAAO;EACT;CACF;AACF;;;;;;;;;;;AAYA,SAAgB,gCACd,aACA,iBACqC;CACrC,IAAI,CAAC,iBAAiB,OAAO,KAAA;CAC7B,OAAO;EACL,sBAAsB;EACtB,iBAAiB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B,OAAO,OAAO,UAAU,OAAO;GAC/B,YAAY,CAAC,UAAU;EACzB,CAAC;CACH;CAEA,MAAM,SAAS,aAAa;EAC1B,QAAQ;EACR;EACA;EACA,OAAO,2BAA2B,EAAE,aAAa,SAAS,YAAY,CAAC;CACzE,CAAC;CAID,OAAO,kBAAkB;EACvB,OAAO,OAAO,UAAU,OAAO;EAC/B,YAAY,CAAC,UAAU;CACzB,CAAC;AACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"provider-thinking.js","names":["#thinking"],"sources":["../../src/providers/provider-thinking.ts"],"sourcesContent":["import { getModelReasoningOptions, ModelRouterLanguageModel } from '@mastra/core/llm';\nimport { wrapLanguageModel } from 'ai';\nimport type { JSONValue } from 'ai';\nimport { requestableReasoningOptions, runThinkingLevel } from '../thinking.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nimport { thinkingRequestFormatFor } from './thinking-request.js';\n\ntype ProviderOptions = Record<string, Record<string, JSONValue | undefined>>;\n\nexport interface ProviderThinkingOptions {\n optionsKey: string;\n options: Record<string, JSONValue>;\n controlKeys: ReadonlySet<string>;\n}\n\nexport function providerThinkingOptions(\n routedModelId: string,\n level: ThinkingLevelSetting | undefined,\n): ProviderThinkingOptions | undefined {\n const format = thinkingRequestFormatFor(routedModelId);\n if (!format || !level) return undefined;\n const catalogOptions = getModelReasoningOptions(routedModelId);\n const requestable = requestableReasoningOptions(routedModelId, catalogOptions);\n const runLevel = runThinkingLevel(routedModelId, level, catalogOptions);\n if (!requestable?.length || runLevel === 'off') return undefined;\n const effortOptions = format.sendEffort?.(runLevel);\n const sendsEffort = requestable.some(option => option.type === 'effort');\n const options = sendsEffort ? effortOptions : format.enableThinking;\n if (!options) return undefined;\n const controlKeys = new Set([...Object.keys(effortOptions ?? {}), ...Object.keys(format.enableThinking ?? {})]);\n return { optionsKey: format.optionsKey, options, controlKeys };\n}\n\nfunction withThinkingOptions<CallOptions extends { providerOptions?: ProviderOptions }>(\n callOptions: CallOptions,\n thinking: ProviderThinkingOptions | undefined,\n): CallOptions {\n if (!thinking) return callOptions;\n const callerOptions = callOptions.providerOptions?.[thinking.optionsKey] ?? {};\n const callerSetThinking = Object.keys(callerOptions).some(key => thinking.controlKeys.has(key));\n if (callerSetThinking) return callOptions;\n return {\n ...callOptions,\n providerOptions: {\n ...callOptions.providerOptions,\n [thinking.optionsKey]: { ...callerOptions, ...thinking.options },\n },\n };\n}\n\ntype AiSdkLanguageModel = Parameters<typeof wrapLanguageModel>[0]['model'];\n\nexport function withThinkingOptionsModel(\n model: AiSdkLanguageModel,\n thinking: ProviderThinkingOptions | undefined,\n): AiSdkLanguageModel {\n if (!thinking) return model;\n return wrapLanguageModel({\n model,\n middleware: {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => withThinkingOptions(params, thinking),\n },\n });\n}\n\ntype RouterConfig = ConstructorParameters<typeof ModelRouterLanguageModel>[0];\ntype RouterCallOptions = Parameters<ModelRouterLanguageModel['doStream']>[0];\ntype RouterCallResult = ReturnType<ModelRouterLanguageModel['doStream']>;\n\nexport class ModelRouterLanguageModelWithThinking extends ModelRouterLanguageModel {\n readonly #thinking: ProviderThinkingOptions | undefined;\n\n constructor(config: RouterConfig, thinking: ProviderThinkingOptions | undefined) {\n super(config);\n this.#thinking = thinking;\n }\n\n override doGenerate(options: RouterCallOptions): RouterCallResult {\n return super.doGenerate(withThinkingOptions(options, this.#thinking));\n }\n\n override doStream(options: RouterCallOptions): RouterCallResult {\n return super.doStream(withThinkingOptions(options, this.#thinking));\n }\n}\n"],"mappings":";;;;;AAeA,SAAgB,wBACd,eACA,OACqC;CACrC,MAAM,SAAS,yBAAyB,aAAa;CACrD,IAAI,CAAC,UAAU,CAAC,OAAO,OAAO,KAAA;CAC9B,MAAM,iBAAiB,yBAAyB,aAAa;CAC7D,MAAM,cAAc,4BAA4B,eAAe,cAAc;CAC7E,MAAM,WAAW,iBAAiB,eAAe,OAAO,cAAc;CACtE,IAAI,CAAC,aAAa,UAAU,aAAa,OAAO,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Content":["import { getModelReasoningOptions, ModelRouterLanguageModel } from '@mastra/core/llm';\nimport { wrapLanguageModel } from 'ai';\nimport type { JSONValue } from 'ai';\nimport { requestableReasoningOptions, runThinkingLevel } from '../thinking.js';\nimport type { ThinkingLevelSetting } from '../thinking.js';\nimport { thinkingRequestFormatFor } from './thinking-request.js';\n\ntype ProviderOptions = Record<string, Record<string, JSONValue | undefined>>;\n\nexport interface ProviderThinkingOptions {\n optionsKey: string;\n options: Record<string, JSONValue>;\n controlKeys: ReadonlySet<string>;\n}\n\nexport function providerThinkingOptions(\n routedModelId: string,\n level: ThinkingLevelSetting | undefined,\n): ProviderThinkingOptions | undefined {\n const format = thinkingRequestFormatFor(routedModelId);\n if (!format || !level) return undefined;\n const catalogOptions = getModelReasoningOptions(routedModelId);\n const requestable = requestableReasoningOptions(routedModelId, catalogOptions);\n const runLevel = runThinkingLevel(routedModelId, level, catalogOptions);\n if (!requestable?.length || runLevel === 'off') return undefined;\n const effortOptions = format.sendEffort?.(runLevel);\n const sendsEffort = requestable.some(option => option.type === 'effort');\n const options = sendsEffort ? effortOptions : format.enableThinking;\n if (!options) return undefined;\n const controlKeys = new Set([...Object.keys(effortOptions ?? {}), ...Object.keys(format.enableThinking ?? {})]);\n return { optionsKey: format.optionsKey, options, controlKeys };\n}\n\nfunction withThinkingOptions<CallOptions extends { providerOptions?: ProviderOptions }>(\n callOptions: CallOptions,\n thinking: ProviderThinkingOptions | undefined,\n): CallOptions {\n if (!thinking) return callOptions;\n const callerOptions = callOptions.providerOptions?.[thinking.optionsKey] ?? {};\n const callerSetThinking = Object.keys(callerOptions).some(key => thinking.controlKeys.has(key));\n if (callerSetThinking) return callOptions;\n return {\n ...callOptions,\n providerOptions: {\n ...callOptions.providerOptions,\n [thinking.optionsKey]: { ...callerOptions, ...thinking.options },\n },\n };\n}\n\ntype AiSdkLanguageModel = Parameters<typeof wrapLanguageModel>[0]['model'];\n\nexport function withThinkingOptionsModel(\n model: AiSdkLanguageModel,\n thinking: ProviderThinkingOptions | undefined,\n): AiSdkLanguageModel {\n if (!thinking) return model;\n return wrapLanguageModel({\n model,\n middleware: {\n specificationVersion: 'v3',\n transformParams: async ({ params }) => withThinkingOptions(params, thinking),\n },\n });\n}\n\ntype RouterConfig = ConstructorParameters<typeof ModelRouterLanguageModel>[0];\ntype RouterCallOptions = Parameters<ModelRouterLanguageModel['doStream']>[0];\ntype RouterCallResult = ReturnType<ModelRouterLanguageModel['doStream']>;\n\nexport class ModelRouterLanguageModelWithThinking extends ModelRouterLanguageModel {\n readonly #thinking: ProviderThinkingOptions | undefined;\n\n constructor(config: RouterConfig, thinking: ProviderThinkingOptions | undefined) {\n super(config);\n this.#thinking = thinking;\n }\n\n override doGenerate(options: RouterCallOptions): RouterCallResult {\n return super.doGenerate(withThinkingOptions(options, this.#thinking));\n }\n\n override doStream(options: RouterCallOptions): RouterCallResult {\n return super.doStream(withThinkingOptions(options, this.#thinking));\n }\n}\n"],"mappings":";;;;;AAeA,SAAgB,wBACd,eACA,OACqC;CACrC,MAAM,SAAS,yBAAyB,aAAa;CACrD,IAAI,CAAC,UAAU,CAAC,OAAO,OAAO,KAAA;CAC9B,MAAM,iBAAiB,yBAAyB,aAAa;CAC7D,MAAM,cAAc,4BAA4B,eAAe,cAAc;CAC7E,MAAM,WAAW,iBAAiB,eAAe,OAAO,cAAc;CACtE,IAAI,CAAC,aAAa,UAAU,aAAa,OAAO,OAAO,KAAA;CACvD,MAAM,gBAAgB,OAAO,aAAa,QAAQ;CAElD,MAAM,UADc,YAAY,MAAK,WAAU,OAAO,SAAS,QACrC,IAAI,gBAAgB,OAAO;CACrD,IAAI,CAAC,SAAS,OAAO,KAAA;CACrB,MAAM,8BAAc,IAAI,IAAI,CAAC,GAAG,OAAO,KAAK,iBAAiB,CAAC,CAAC,GAAG,GAAG,OAAO,KAAK,OAAO,kBAAkB,CAAC,CAAC,CAAC,CAAC;CAC9G,OAAO;EAAE,YAAY,OAAO;EAAY;EAAS;CAAY;AAC/D;AAEA,SAAS,oBACP,aACA,UACa;CACb,IAAI,CAAC,UAAU,OAAO;CACtB,MAAM,gBAAgB,YAAY,kBAAkB,SAAS,eAAe,CAAC;CAE7E,IAD0B,OAAO,KAAK,aAAa,CAAC,CAAC,MAAK,QAAO,SAAS,YAAY,IAAI,GAAG,CACzE,GAAG,OAAO;CAC9B,OAAO;EACL,GAAG;EACH,iBAAiB;GACf,GAAG,YAAY;IACd,SAAS,aAAa;IAAE,GAAG;IAAe,GAAG,SAAS;GAAQ;EACjE;CACF;AACF;AAIA,SAAgB,yBACd,OACA,UACoB;CACpB,IAAI,CAAC,UAAU,OAAO;CACtB,OAAO,kBAAkB;EACvB;EACA,YAAY;GACV,sBAAsB;GACtB,iBAAiB,OAAO,EAAE,aAAa,oBAAoB,QAAQ,QAAQ;EAC7E;CACF,CAAC;AACH;AAMA,IAAa,uCAAb,cAA0D,yBAAyB;CACjF;CAEA,YAAY,QAAsB,UAA+C;EAC/E,MAAM,MAAM;EACZ,KAAK,YAAY;CACnB;CAEA,WAAoB,SAA8C;EAChE,OAAO,MAAM,WAAW,oBAAoB,SAAS,KAAK,SAAS,CAAC;CACtE;CAEA,SAAkB,SAA8C;EAC9D,OAAO,MAAM,SAAS,oBAAoB,SAAS,KAAK,SAAS,CAAC;CACpE;AACF"}
@@ -36,7 +36,7 @@ const REQUEST_FORMATS_BY_PROVIDER = {
36
36
  xai: {
37
37
  optionsKey: "xai",
38
38
  sendEffort: sendReasoningEffort,
39
- acceptedEfforts: LOW_TO_HIGH
39
+ acceptedEfforts: [...LOW_TO_HIGH, "xhigh"]
40
40
  },
41
41
  togetherai: {
42
42
  optionsKey: "togetherai",
@@ -1 +1 @@
1
- {"version":3,"file":"thinking-request.js","names":[],"sources":["../../src/providers/thinking-request.ts"],"sourcesContent":["import type { JSONValue } from 'ai';\nimport type { ActiveThinkingLevel } from '../thinking.js';\nimport { MASTRA_GATEWAY_PREFIX, stripMastraGatewayPrefix } from './model-ids.js';\n\ntype ThinkingProviderOptions = Record<string, JSONValue>;\n\nexport interface ThinkingRequestFormat {\n optionsKey: string;\n sendEffort?: (effort: ActiveThinkingLevel) => ThinkingProviderOptions;\n acceptedEfforts?: readonly ActiveThinkingLevel[];\n enableThinking?: ThinkingProviderOptions;\n}\n\nconst LOW_TO_HIGH: readonly ActiveThinkingLevel[] = ['low', 'medium', 'high'];\nconst sendReasoningEffort = (reasoningEffort: ActiveThinkingLevel): ThinkingProviderOptions => ({ reasoningEffort });\nconst ENABLE_REASONING: ThinkingProviderOptions = { reasoning: { enabled: true } };\n\nconst OPENROUTER_FORMAT: ThinkingRequestFormat = {\n optionsKey: 'openrouter',\n sendEffort: effort => ({ reasoning: { effort } }),\n enableThinking: ENABLE_REASONING,\n};\n\nconst REQUEST_FORMATS_BY_PROVIDER: Partial<Record<string, ThinkingRequestFormat>> = {\n deepseek: {\n optionsKey: 'deepseek',\n sendEffort: sendReasoningEffort,\n enableThinking: { thinking: { type: 'enabled' } },\n },\n openrouter: OPENROUTER_FORMAT,\n alibaba: { optionsKey: 'alibaba', enableThinking: { enableThinking: true } },\n groq: { optionsKey: 'groq', sendEffort: sendReasoningEffort, acceptedEfforts: LOW_TO_HIGH },\n mistral: { optionsKey: 'mistral', sendEffort: sendReasoningEffort, acceptedEfforts: ['high'] },\n xai: { optionsKey: 'xai', sendEffort: sendReasoningEffort, acceptedEfforts: LOW_TO_HIGH },\n togetherai: {\n optionsKey: 'togetherai',\n sendEffort: sendReasoningEffort,\n acceptedEfforts: LOW_TO_HIGH,\n enableThinking: ENABLE_REASONING,\n },\n deepinfra: {\n optionsKey: 'deepinfra',\n sendEffort: sendReasoningEffort,\n acceptedEfforts: LOW_TO_HIGH,\n enableThinking: ENABLE_REASONING,\n },\n cerebras: { optionsKey: 'cerebras', sendEffort: sendReasoningEffort },\n perplexity: { optionsKey: 'perplexity', sendEffort: effort => ({ reasoning_effort: effort }) },\n};\n\nconst THINKING_ADAPTER_PROVIDERS: ReadonlySet<string> = new Set(['anthropic', 'openai', 'google']);\n\nexport function isThinkingAdapterProvider(provider: string): boolean {\n return THINKING_ADAPTER_PROVIDERS.has(provider);\n}\n\nconst PROVIDERS_ROUTED_WITHOUT_THINKING: ReadonlySet<string> = new Set([\n 'github-copilot',\n 'kimi-for-coding',\n 'moonshotai',\n 'amazon-bedrock',\n]);\n\nexport function isRoutedWithoutThinking(modelId: string): boolean {\n const [provider = ''] = stripMastraGatewayPrefix(modelId).split('/');\n return PROVIDERS_ROUTED_WITHOUT_THINKING.has(provider);\n}\n\nexport function thinkingRequestFormatFor(modelId: string): ThinkingRequestFormat | undefined {\n const routedThroughMastraGateway = modelId.startsWith(MASTRA_GATEWAY_PREFIX);\n const [provider = ''] = stripMastraGatewayPrefix(modelId).split('/');\n if (isThinkingAdapterProvider(provider)) return undefined;\n if (routedThroughMastraGateway) return OPENROUTER_FORMAT;\n return REQUEST_FORMATS_BY_PROVIDER[provider.includes('alibaba') ? 'alibaba' : provider];\n}\n"],"mappings":";;AAaA,MAAM,cAA8C;CAAC;CAAO;CAAU;AAAM;AAC5E,MAAM,uBAAuB,qBAAmE,EAAE,gBAAgB;AAClH,MAAM,mBAA4C,EAAE,WAAW,EAAE,SAAS,KAAK,EAAE;AAEjF,MAAM,oBAA2C;CAC/C,YAAY;CACZ,aAAY,YAAW,EAAE,WAAW,EAAE,OAAO,EAAE;CAC/C,gBAAgB;AAClB;AAEA,MAAM,8BAA8E;CAClF,UAAU;EACR,YAAY;EACZ,YAAY;EACZ,gBAAgB,EAAE,UAAU,EAAE,MAAM,UAAU,EAAE;CAClD;CACA,YAAY;CACZ,SAAS;EAAE,YAAY;EAAW,gBAAgB,EAAE,gBAAgB,KAAK;CAAE;CAC3E,MAAM;EAAE,YAAY;EAAQ,YAAY;EAAqB,iBAAiB;CAAY;CAC1F,SAAS;EAAE,YAAY;EAAW,YAAY;EAAqB,iBAAiB,CAAC,MAAM;CAAE;CAC7F,KAAK;EAAE,YAAY;EAAO,YAAY;EAAqB,iBAAiB;CAAY;CACxF,YAAY;EACV,YAAY;EACZ,YAAY;EACZ,iBAAiB;EACjB,gBAAgB;CAClB;CACA,WAAW;EACT,YAAY;EACZ,YAAY;EACZ,iBAAiB;EACjB,gBAAgB;CAClB;CACA,UAAU;EAAE,YAAY;EAAY,YAAY;CAAoB;CACpE,YAAY;EAAE,YAAY;EAAc,aAAY,YAAW,EAAE,kBAAkB,OAAO;CAAG;AAC/F;AAEA,MAAM,6CAAkD,IAAI,IAAI;CAAC;CAAa;CAAU;AAAQ,CAAC;AAEjG,SAAgB,0BAA0B,UAA2B;CACnE,OAAO,2BAA2B,IAAI,QAAQ;AAChD;AAEA,MAAM,oDAAyD,IAAI,IAAI;CACrE;CACA;CACA;CACA;AACF,CAAC;AAED,SAAgB,wBAAwB,SAA0B;CAChE,MAAM,CAAC,WAAW,MAAM,yBAAyB,OAAO,CAAC,CAAC,MAAM,GAAG;CACnE,OAAO,kCAAkC,IAAI,QAAQ;AACvD;AAEA,SAAgB,yBAAyB,SAAoD;CAC3F,MAAM,6BAA6B,QAAQ,WAAW,qBAAqB;CAC3E,MAAM,CAAC,WAAW,MAAM,yBAAyB,OAAO,CAAC,CAAC,MAAM,GAAG;CACnE,IAAI,0BAA0B,QAAQ,GAAG,OAAO,KAAA;CAChD,IAAI,4BAA4B,OAAO;CACvC,OAAO,4BAA4B,SAAS,SAAS,SAAS,IAAI,YAAY;AAChF"}
1
+ {"version":3,"file":"thinking-request.js","names":[],"sources":["../../src/providers/thinking-request.ts"],"sourcesContent":["import type { JSONValue } from 'ai';\nimport type { ActiveThinkingLevel } from '../thinking.js';\nimport { MASTRA_GATEWAY_PREFIX, stripMastraGatewayPrefix } from './model-ids.js';\n\ntype ThinkingProviderOptions = Record<string, JSONValue>;\n\nexport interface ThinkingRequestFormat {\n optionsKey: string;\n sendEffort?: (effort: ActiveThinkingLevel) => ThinkingProviderOptions;\n acceptedEfforts?: readonly ActiveThinkingLevel[];\n enableThinking?: ThinkingProviderOptions;\n}\n\nconst LOW_TO_HIGH: readonly ActiveThinkingLevel[] = ['low', 'medium', 'high'];\nconst sendReasoningEffort = (reasoningEffort: ActiveThinkingLevel): ThinkingProviderOptions => ({ reasoningEffort });\nconst ENABLE_REASONING: ThinkingProviderOptions = { reasoning: { enabled: true } };\n\nconst OPENROUTER_FORMAT: ThinkingRequestFormat = {\n optionsKey: 'openrouter',\n sendEffort: effort => ({ reasoning: { effort } }),\n enableThinking: ENABLE_REASONING,\n};\n\nconst REQUEST_FORMATS_BY_PROVIDER: Partial<Record<string, ThinkingRequestFormat>> = {\n deepseek: {\n optionsKey: 'deepseek',\n sendEffort: sendReasoningEffort,\n enableThinking: { thinking: { type: 'enabled' } },\n },\n openrouter: OPENROUTER_FORMAT,\n alibaba: { optionsKey: 'alibaba', enableThinking: { enableThinking: true } },\n groq: { optionsKey: 'groq', sendEffort: sendReasoningEffort, acceptedEfforts: LOW_TO_HIGH },\n mistral: { optionsKey: 'mistral', sendEffort: sendReasoningEffort, acceptedEfforts: ['high'] },\n xai: { optionsKey: 'xai', sendEffort: sendReasoningEffort, acceptedEfforts: [...LOW_TO_HIGH, 'xhigh'] },\n togetherai: {\n optionsKey: 'togetherai',\n sendEffort: sendReasoningEffort,\n acceptedEfforts: LOW_TO_HIGH,\n enableThinking: ENABLE_REASONING,\n },\n deepinfra: {\n optionsKey: 'deepinfra',\n sendEffort: sendReasoningEffort,\n acceptedEfforts: LOW_TO_HIGH,\n enableThinking: ENABLE_REASONING,\n },\n cerebras: { optionsKey: 'cerebras', sendEffort: sendReasoningEffort },\n perplexity: { optionsKey: 'perplexity', sendEffort: effort => ({ reasoning_effort: effort }) },\n};\n\nconst THINKING_ADAPTER_PROVIDERS: ReadonlySet<string> = new Set(['anthropic', 'openai', 'google']);\n\nexport function isThinkingAdapterProvider(provider: string): boolean {\n return THINKING_ADAPTER_PROVIDERS.has(provider);\n}\n\nconst PROVIDERS_ROUTED_WITHOUT_THINKING: ReadonlySet<string> = new Set([\n 'github-copilot',\n 'kimi-for-coding',\n 'moonshotai',\n 'amazon-bedrock',\n]);\n\nexport function isRoutedWithoutThinking(modelId: string): boolean {\n const [provider = ''] = stripMastraGatewayPrefix(modelId).split('/');\n return PROVIDERS_ROUTED_WITHOUT_THINKING.has(provider);\n}\n\nexport function thinkingRequestFormatFor(modelId: string): ThinkingRequestFormat | undefined {\n const routedThroughMastraGateway = modelId.startsWith(MASTRA_GATEWAY_PREFIX);\n const [provider = ''] = stripMastraGatewayPrefix(modelId).split('/');\n if (isThinkingAdapterProvider(provider)) return undefined;\n if (routedThroughMastraGateway) return OPENROUTER_FORMAT;\n return REQUEST_FORMATS_BY_PROVIDER[provider.includes('alibaba') ? 'alibaba' : provider];\n}\n"],"mappings":";;AAaA,MAAM,cAA8C;CAAC;CAAO;CAAU;AAAM;AAC5E,MAAM,uBAAuB,qBAAmE,EAAE,gBAAgB;AAClH,MAAM,mBAA4C,EAAE,WAAW,EAAE,SAAS,KAAK,EAAE;AAEjF,MAAM,oBAA2C;CAC/C,YAAY;CACZ,aAAY,YAAW,EAAE,WAAW,EAAE,OAAO,EAAE;CAC/C,gBAAgB;AAClB;AAEA,MAAM,8BAA8E;CAClF,UAAU;EACR,YAAY;EACZ,YAAY;EACZ,gBAAgB,EAAE,UAAU,EAAE,MAAM,UAAU,EAAE;CAClD;CACA,YAAY;CACZ,SAAS;EAAE,YAAY;EAAW,gBAAgB,EAAE,gBAAgB,KAAK;CAAE;CAC3E,MAAM;EAAE,YAAY;EAAQ,YAAY;EAAqB,iBAAiB;CAAY;CAC1F,SAAS;EAAE,YAAY;EAAW,YAAY;EAAqB,iBAAiB,CAAC,MAAM;CAAE;CAC7F,KAAK;EAAE,YAAY;EAAO,YAAY;EAAqB,iBAAiB,CAAC,GAAG,aAAa,OAAO;CAAE;CACtG,YAAY;EACV,YAAY;EACZ,YAAY;EACZ,iBAAiB;EACjB,gBAAgB;CAClB;CACA,WAAW;EACT,YAAY;EACZ,YAAY;EACZ,iBAAiB;EACjB,gBAAgB;CAClB;CACA,UAAU;EAAE,YAAY;EAAY,YAAY;CAAoB;CACpE,YAAY;EAAE,YAAY;EAAc,aAAY,YAAW,EAAE,kBAAkB,OAAO;CAAG;AAC/F;AAEA,MAAM,6CAAkD,IAAI,IAAI;CAAC;CAAa;CAAU;AAAQ,CAAC;AAEjG,SAAgB,0BAA0B,UAA2B;CACnE,OAAO,2BAA2B,IAAI,QAAQ;AAChD;AAEA,MAAM,oDAAyD,IAAI,IAAI;CACrE;CACA;CACA;CACA;AACF,CAAC;AAED,SAAgB,wBAAwB,SAA0B;CAChE,MAAM,CAAC,WAAW,MAAM,yBAAyB,OAAO,CAAC,CAAC,MAAM,GAAG;CACnE,OAAO,kCAAkC,IAAI,QAAQ;AACvD;AAEA,SAAgB,yBAAyB,SAAoD;CAC3F,MAAM,6BAA6B,QAAQ,WAAW,qBAAqB;CAC3E,MAAM,CAAC,WAAW,MAAM,yBAAyB,OAAO,CAAC,CAAC,MAAM,GAAG;CACnE,IAAI,0BAA0B,QAAQ,GAAG,OAAO,KAAA;CAChD,IAAI,4BAA4B,OAAO;CACvC,OAAO,4BAA4B,SAAS,SAAS,SAAS,IAAI,YAAY;AAChF"}
@@ -18,7 +18,7 @@ const INTERPRETERS = {
18
18
  ".ts": () => process.execPath,
19
19
  ".py": () => "python3"
20
20
  };
21
- const MAX_OUTPUT_BYTES = 1024 * 1024;
21
+ const MAX_OUTPUT_BYTES = 1048576;
22
22
  function isExecutableFile(absPath) {
23
23
  try {
24
24
  fs$1.accessSync(absPath, fs$1.constants.X_OK);
@@ -1 +1 @@
1
- {"version":3,"file":"run-script.js","names":["fs","path"],"sources":["../../src/schedules/run-script.ts"],"sourcesContent":["/**\n * Runs a schedule's script file at fire time.\n *\n * The file path is always passed as the literal command (or as the single\n * argument to a fixed interpreter) — never through a shell string — so the\n * user's extra prompt can never be interpolated into a command line.\n */\nimport { execFile } from 'node:child_process';\nimport * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport type { RunScript, ScriptResult } from './prompt.js';\n\n/** Interpreter for scripts that lack an executable bit, keyed by extension. */\nconst INTERPRETERS: Record<string, () => string> = {\n '.sh': () => '/bin/sh',\n '.js': () => process.execPath,\n '.mjs': () => process.execPath,\n '.cjs': () => process.execPath,\n '.ts': () => process.execPath,\n '.py': () => 'python3',\n};\n\nconst MAX_OUTPUT_BYTES = 1024 * 1024;\n\nexport function isExecutableFile(absPath: string): boolean {\n try {\n fs.accessSync(absPath, fs.constants.X_OK);\n return fs.statSync(absPath).isFile();\n } catch {\n return false;\n }\n}\n\nexport function resolveScriptCommand(absPath: string): { command: string; args: string[] } {\n if (isExecutableFile(absPath)) return { command: absPath, args: [] };\n const interpreter = INTERPRETERS[path.extname(absPath).toLowerCase()];\n if (!interpreter) {\n throw new Error(`${absPath} is not executable and has no known interpreter`);\n }\n return { command: interpreter(), args: [absPath] };\n}\n\nexport const runScript: RunScript = async (absPath, { cwd, timeoutMs }) => {\n const { command, args } = resolveScriptCommand(absPath);\n return new Promise<ScriptResult>((resolve, reject) => {\n execFile(\n command,\n args,\n { cwd, timeout: timeoutMs, maxBuffer: MAX_OUTPUT_BYTES, windowsHide: true },\n (error, stdout, stderr) => {\n if (!error) {\n resolve({ stdout, stderr, exitCode: 0 });\n return;\n }\n const spawnError = error as NodeJS.ErrnoException & { code?: number | string; killed?: boolean };\n if (typeof spawnError.code === 'number') {\n resolve({ stdout, stderr, exitCode: spawnError.code });\n return;\n }\n if (spawnError.killed) {\n resolve({ stdout, stderr: `${stderr}\\n[timed out after ${timeoutMs}ms]`.trim(), exitCode: null });\n return;\n }\n reject(error);\n },\n );\n });\n};\n"],"mappings":";;;;;;;;;;;;AAaA,MAAM,eAA6C;CACjD,aAAa;CACb,aAAa,QAAQ;CACrB,cAAc,QAAQ;CACtB,cAAc,QAAQ;CACtB,aAAa,QAAQ;CACrB,aAAa;AACf;AAEA,MAAM,mBAAmB,OAAO;AAEhC,SAAgB,iBAAiB,SAA0B;CACzD,IAAI;EACF,KAAG,WAAW,SAASA,KAAG,UAAU,IAAI;EACxC,OAAOA,KAAG,SAAS,OAAO,CAAC,CAAC,OAAO;CACrC,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAgB,qBAAqB,SAAsD;CACzF,IAAI,iBAAiB,OAAO,GAAG,OAAO;EAAE,SAAS;EAAS,MAAM,CAAC;CAAE;CACnE,MAAM,cAAc,aAAaC,OAAK,QAAQ,OAAO,CAAC,CAAC,YAAY;CACnE,IAAI,CAAC,aACH,MAAM,IAAI,MAAM,GAAG,QAAQ,gDAAgD;CAE7E,OAAO;EAAE,SAAS,YAAY;EAAG,MAAM,CAAC,OAAO;CAAE;AACnD;AAEA,MAAa,YAAuB,OAAO,SAAS,EAAE,KAAK,gBAAgB;CACzE,MAAM,EAAE,SAAS,SAAS,qBAAqB,OAAO;CACtD,OAAO,IAAI,SAAuB,SAAS,WAAW;EACpD,SACE,SACA,MACA;GAAE;GAAK,SAAS;GAAW,WAAW;GAAkB,aAAa;EAAK,IACzE,OAAO,QAAQ,WAAW;GACzB,IAAI,CAAC,OAAO;IACV,QAAQ;KAAE;KAAQ;KAAQ,UAAU;IAAE,CAAC;IACvC;GACF;GACA,MAAM,aAAa;GACnB,IAAI,OAAO,WAAW,SAAS,UAAU;IACvC,QAAQ;KAAE;KAAQ;KAAQ,UAAU,WAAW;IAAK,CAAC;IACrD;GACF;GACA,IAAI,WAAW,QAAQ;IACrB,QAAQ;KAAE;KAAQ,QAAQ,GAAG,OAAO,qBAAqB,UAAU,KAAK,KAAK;KAAG,UAAU;IAAK,CAAC;IAChG;GACF;GACA,OAAO,KAAK;EACd,CACF;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"run-script.js","names":["fs","path"],"sources":["../../src/schedules/run-script.ts"],"sourcesContent":["/**\n * Runs a schedule's script file at fire time.\n *\n * The file path is always passed as the literal command (or as the single\n * argument to a fixed interpreter) — never through a shell string — so the\n * user's extra prompt can never be interpolated into a command line.\n */\nimport { execFile } from 'node:child_process';\nimport * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport type { RunScript, ScriptResult } from './prompt.js';\n\n/** Interpreter for scripts that lack an executable bit, keyed by extension. */\nconst INTERPRETERS: Record<string, () => string> = {\n '.sh': () => '/bin/sh',\n '.js': () => process.execPath,\n '.mjs': () => process.execPath,\n '.cjs': () => process.execPath,\n '.ts': () => process.execPath,\n '.py': () => 'python3',\n};\n\nconst MAX_OUTPUT_BYTES = 1024 * 1024;\n\nexport function isExecutableFile(absPath: string): boolean {\n try {\n fs.accessSync(absPath, fs.constants.X_OK);\n return fs.statSync(absPath).isFile();\n } catch {\n return false;\n }\n}\n\nexport function resolveScriptCommand(absPath: string): { command: string; args: string[] } {\n if (isExecutableFile(absPath)) return { command: absPath, args: [] };\n const interpreter = INTERPRETERS[path.extname(absPath).toLowerCase()];\n if (!interpreter) {\n throw new Error(`${absPath} is not executable and has no known interpreter`);\n }\n return { command: interpreter(), args: [absPath] };\n}\n\nexport const runScript: RunScript = async (absPath, { cwd, timeoutMs }) => {\n const { command, args } = resolveScriptCommand(absPath);\n return new Promise<ScriptResult>((resolve, reject) => {\n execFile(\n command,\n args,\n { cwd, timeout: timeoutMs, maxBuffer: MAX_OUTPUT_BYTES, windowsHide: true },\n (error, stdout, stderr) => {\n if (!error) {\n resolve({ stdout, stderr, exitCode: 0 });\n return;\n }\n const spawnError = error as NodeJS.ErrnoException & { code?: number | string; killed?: boolean };\n if (typeof spawnError.code === 'number') {\n resolve({ stdout, stderr, exitCode: spawnError.code });\n return;\n }\n if (spawnError.killed) {\n resolve({ stdout, stderr: `${stderr}\\n[timed out after ${timeoutMs}ms]`.trim(), exitCode: null });\n return;\n }\n reject(error);\n },\n );\n });\n};\n"],"mappings":";;;;;;;;;;;;AAaA,MAAM,eAA6C;CACjD,aAAa;CACb,aAAa,QAAQ;CACrB,cAAc,QAAQ;CACtB,cAAc,QAAQ;CACtB,aAAa,QAAQ;CACrB,aAAa;AACf;AAEA,MAAM,mBAAmB;AAEzB,SAAgB,iBAAiB,SAA0B;CACzD,IAAI;EACF,KAAG,WAAW,SAASA,KAAG,UAAU,IAAI;EACxC,OAAOA,KAAG,SAAS,OAAO,CAAC,CAAC,OAAO;CACrC,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAgB,qBAAqB,SAAsD;CACzF,IAAI,iBAAiB,OAAO,GAAG,OAAO;EAAE,SAAS;EAAS,MAAM,CAAC;CAAE;CACnE,MAAM,cAAc,aAAaC,OAAK,QAAQ,OAAO,CAAC,CAAC,YAAY;CACnE,IAAI,CAAC,aACH,MAAM,IAAI,MAAM,GAAG,QAAQ,gDAAgD;CAE7E,OAAO;EAAE,SAAS,YAAY;EAAG,MAAM,CAAC,OAAO;CAAE;AACnD;AAEA,MAAa,YAAuB,OAAO,SAAS,EAAE,KAAK,gBAAgB;CACzE,MAAM,EAAE,SAAS,SAAS,qBAAqB,OAAO;CACtD,OAAO,IAAI,SAAuB,SAAS,WAAW;EACpD,SACE,SACA,MACA;GAAE;GAAK,SAAS;GAAW,WAAW;GAAkB,aAAa;EAAK,IACzE,OAAO,QAAQ,WAAW;GACzB,IAAI,CAAC,OAAO;IACV,QAAQ;KAAE;KAAQ;KAAQ,UAAU;IAAE,CAAC;IACvC;GACF;GACA,MAAM,aAAa;GACnB,IAAI,OAAO,WAAW,SAAS,UAAU;IACvC,QAAQ;KAAE;KAAQ;KAAQ,UAAU,WAAW;IAAK,CAAC;IACrD;GACF;GACA,IAAI,WAAW,QAAQ;IACrB,QAAQ;KAAE;KAAQ,QAAQ,GAAG,OAAO,qBAAqB,UAAU,KAAK,KAAK;KAAG,UAAU;IAAK,CAAC;IAChG;GACF;GACA,OAAO,KAAK;EACd,CACF;CACF,CAAC;AACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"scheduler.js","names":["#options","#now","#entries","#arm","#fire"],"sources":["../../src/schedules/scheduler.ts"],"sourcesContent":["/**\n * Process-local scheduler behind `/schedules`.\n *\n * Schedules live only in this process: timers fire them and they die with it.\n * Nothing is written to shared storage, so other Mastra Code processes on the\n * same database never see — or fire — this session's schedules.\n */\nimport { randomUUID } from 'node:crypto';\nimport { nextFireTime, validateInterval } from './interval.js';\nimport type { ParsedInterval } from './interval.js';\nimport type { AssembledPrompt } from './prompt.js';\n\n/** `source` attribute on fired signals; the TUI labels these turns `schedule`. */\nexport const SCHEDULE_SIGNAL_SOURCE = 'schedule';\n\n/** When a schedule fires. Only recurring `every` triggers exist today. */\nexport type ScheduleTrigger = { kind: 'every'; interval: ParsedInterval };\n\n/** A file a schedule runs as a script, or re-reads and sends as the prompt, on every fire. */\nexport type ScheduleFile = { path: string; displayPath: string; mode: 'script' | 'prompt' };\n\n/** What to send on each fire: `prompt` text, or a `file` (plus `extraPrompt` appended after it). */\nexport type ThreadScheduleSource = { prompt: string } | { file: ScheduleFile; extraPrompt?: string };\n\nexport type ThreadScheduleSpec = ThreadScheduleSource & {\n trigger: ScheduleTrigger;\n /** Who is creating it: the user via `/schedules`, or the agent via its schedule tools. Defaults to `user`. */\n createdBy?: 'user' | 'agent';\n};\n\nexport type ThreadSchedule = {\n id: string;\n threadId: string;\n resourceId: string;\n trigger: ScheduleTrigger;\n prompt?: string;\n file?: ScheduleFile;\n extraPrompt?: string;\n createdBy: 'user' | 'agent';\n status: 'active' | 'paused';\n /** Next boundary this schedule fires at; only meaningful while active. */\n nextFireAt: number;\n createdAt: number;\n};\n\nexport type ThreadSchedulerOptions = {\n /** Build the prompt for one fire (runs scripts / reads files). */\n assemblePrompt: (schedule: ThreadSchedule) => Promise<AssembledPrompt>;\n /** Hand the prompt to the agent for the schedule's thread. */\n deliver: (schedule: ThreadSchedule, assembled: AssembledPrompt) => Promise<void>;\n /** A fire failed before reaching the agent. */\n onError?: (error: unknown, schedule: ThreadSchedule) => void;\n now?: () => number;\n};\n\n/** How late a timer may run before its fire counts as missed (sleep, stalled event loop). */\nexport const LATE_FIRE_GRACE_MS = 60_000;\n\n/**\n * Outcome of a manual `run()`. `failed` means nothing reached the agent: the\n * prompt could not be built or the agent refused it.\n */\nexport type ScheduleRunResult =\n | { status: 'fired' }\n | { status: 'failed'; error: unknown }\n | { status: 'busy' }\n | { status: 'not-found' };\n\ntype FireOutcome = { kind: 'delivered' } | { kind: 'deleted' } | { kind: 'failed'; error: unknown };\n\ntype Entry = { schedule: ThreadSchedule; timer?: ReturnType<typeof setTimeout>; inFlight: number };\n\nexport class ThreadScheduler {\n #entries = new Map<string, Entry>();\n #options: ThreadSchedulerOptions;\n\n constructor(options: ThreadSchedulerOptions) {\n this.#options = options;\n }\n\n #now(): number {\n return this.#options.now?.() ?? Date.now();\n }\n\n /** Throws when the interval is not a supported wall-clock cadence (see `validateInterval`) or the prompt is empty. */\n create(spec: ThreadScheduleSpec, target: { threadId: string; resourceId: string }): ThreadSchedule {\n const { interval } = spec.trigger;\n const check = validateInterval(interval);\n if ('error' in check) {\n throw new Error(\n `Invalid schedule interval: ${check.error}${check.suggestion ? ` Try ${check.suggestion}.` : ''}`,\n );\n }\n if ('prompt' in spec && !spec.prompt.trim()) throw new Error('Schedule prompt is empty.');\n const now = this.#now();\n const schedule: ThreadSchedule = {\n id: randomUUID(),\n threadId: target.threadId,\n resourceId: target.resourceId,\n trigger: { kind: 'every', interval: { ...interval } },\n ...('file' in spec\n ? { file: { ...spec.file }, ...(spec.extraPrompt ? { extraPrompt: spec.extraPrompt } : {}) }\n : { prompt: spec.prompt }),\n createdBy: spec.createdBy ?? 'user',\n status: 'active',\n nextFireAt: nextFireTime(interval.ms, now),\n createdAt: now,\n };\n const entry: Entry = { schedule, inFlight: 0 };\n this.#entries.set(schedule.id, entry);\n this.#arm(entry);\n return { ...schedule };\n }\n\n list(filter: { threadId?: string } = {}): ThreadSchedule[] {\n return [...this.#entries.values()]\n .map(entry => ({ ...entry.schedule }))\n .filter(schedule => !filter.threadId || schedule.threadId === filter.threadId)\n .sort((a, b) => a.createdAt - b.createdAt);\n }\n\n delete(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n clearTimeout(entry.timer);\n this.#entries.delete(id);\n return true;\n }\n\n pause(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n clearTimeout(entry.timer);\n entry.timer = undefined;\n entry.schedule.status = 'paused';\n return true;\n }\n\n resume(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n if (entry.schedule.status === 'active') return true;\n entry.schedule.status = 'active';\n entry.schedule.nextFireAt = nextFireTime(entry.schedule.trigger.interval.ms, this.#now());\n this.#arm(entry);\n return true;\n }\n\n /** Whether a fire of this schedule (timer or manual) is still assembling or delivering. */\n isFiring(id: string): boolean {\n return (this.#entries.get(id)?.inFlight ?? 0) > 0;\n }\n\n /**\n * Fire once now, outside the cadence. Returns `busy` without firing when a\n * fire of this schedule is still running (a slow script, say), so the same\n * script never runs twice at once. Resolves once the fire finished; a fire\n * that never reached the agent resolves `failed` instead of calling `onError`.\n */\n async run(id: string): Promise<ScheduleRunResult> {\n const entry = this.#entries.get(id);\n if (!entry) return { status: 'not-found' };\n if (entry.inFlight > 0) return { status: 'busy' };\n const result = await this.#fire(entry);\n if (result.kind === 'failed') return { status: 'failed', error: result.error };\n return result.kind === 'delivered' ? { status: 'fired' } : { status: 'not-found' };\n }\n\n /** Drop every schedule and timer (process shutdown). */\n stop(): void {\n for (const entry of this.#entries.values()) clearTimeout(entry.timer);\n this.#entries.clear();\n }\n\n #arm(entry: Entry): void {\n clearTimeout(entry.timer);\n const scheduledAt = entry.schedule.nextFireAt;\n entry.timer = setTimeout(\n () => {\n if (this.#entries.get(entry.schedule.id) !== entry || entry.schedule.status !== 'active') return;\n // Timers can fire a hair early; step from the later of now and the\n // intended boundary so the same boundary is never fired twice.\n const now = this.#now();\n entry.schedule.nextFireAt = nextFireTime(entry.schedule.trigger.interval.ms, Math.max(now, scheduledAt));\n this.#arm(entry);\n // A timer that ran long after its boundary (machine asleep) is a missed fire, not a late one.\n if (now - scheduledAt > LATE_FIRE_GRACE_MS) return;\n // A slow script can outlast a short interval; don't stack fires.\n if (entry.inFlight === 0) {\n void this.#fire(entry).then(result => {\n if (result.kind === 'failed') this.#options.onError?.(result.error, { ...entry.schedule });\n });\n }\n },\n Math.max(0, scheduledAt - this.#now()),\n );\n entry.timer.unref?.();\n }\n\n async #fire(entry: Entry): Promise<FireOutcome> {\n const schedule = { ...entry.schedule };\n entry.inFlight++;\n try {\n const assembled = await this.#options.assemblePrompt(schedule);\n // Deleted while its prompt was being built: drop the fire quietly.\n if (this.#entries.get(schedule.id) !== entry) return { kind: 'deleted' };\n await this.#options.deliver(schedule, assembled);\n return { kind: 'delivered' };\n } catch (error) {\n return { kind: 'failed', error };\n } finally {\n entry.inFlight--;\n }\n }\n}\n\nexport function shortScheduleId(id: string): string {\n return id.slice(0, 8);\n}\n\n/** One-line human summary of what a schedule sends: `run ./check.sh + \"extra\"`, `read notes.md`, `\"prompt\"`. */\nexport function describeScheduleSource(schedule: Pick<ThreadSchedule, 'prompt' | 'file' | 'extraPrompt'>): string {\n if (!schedule.file) return `\"${schedule.prompt ?? ''}\"`;\n const verb = schedule.file.mode === 'script' ? 'run' : 'read';\n const extra = schedule.extraPrompt ? ` + \"${schedule.extraPrompt}\"` : '';\n return `${verb} ${schedule.file.displayPath}${extra}`;\n}\n\n/**\n * Attributes on a fired schedule signal. They carry everything the transcript\n * header needs, so reloaded history renders without the process-local entry.\n */\nexport function scheduleSignalAttributes(\n schedule: ThreadSchedule,\n assembled: Pick<AssembledPrompt, 'outcome'> = {},\n): Record<string, string> {\n return {\n source: SCHEDULE_SIGNAL_SOURCE,\n scheduleId: schedule.id,\n scheduleCadence: schedule.trigger.interval.label,\n scheduleSource: describeScheduleSource(schedule),\n scheduleCreatedBy: schedule.createdBy,\n ...(assembled.outcome ? { scheduleOutcome: assembled.outcome } : {}),\n };\n}\n"],"mappings":";;;;;;;;;;;AAaA,MAAa,yBAAyB;;AA2CtC,MAAa,qBAAqB;AAgBlC,IAAa,kBAAb,MAA6B;CAC3B,2BAAW,IAAI,IAAmB;CAClC;CAEA,YAAY,S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,SAAS,YAAY,KAAK;CACtE,OAAO,GAAG,KAAK,GAAG,SAAS,KAAK,cAAc;AAChD;;;;;AAMA,SAAgB,yBACd,UACA,YAA8C,CAAC,GACvB;CACxB,OAAO;EACL,QAAQ;EACR,YAAY,SAAS;EACrB,iBAAiB,SAAS,QAAQ,SAAS;EAC3C,gBAAgB,uBAAuB,QAAQ;EAC/C,mBAAmB,SAAS;EAC5B,GAAI,UAAU,UAAU,EAAE,iBAAiB,UAAU,QAAQ,IAAI,CAAC;CACpE;AACF"}
1
+ {"version":3,"file":"scheduler.js","names":[],"sources":["../../src/schedules/scheduler.ts"],"sourcesContent":["/**\n * Process-local scheduler behind `/schedules`.\n *\n * Schedules live only in this process: timers fire them and they die with it.\n * Nothing is written to shared storage, so other Mastra Code processes on the\n * same database never see — or fire — this session's schedules.\n */\nimport { randomUUID } from 'node:crypto';\nimport { nextFireTime, validateInterval } from './interval.js';\nimport type { ParsedInterval } from './interval.js';\nimport type { AssembledPrompt } from './prompt.js';\n\n/** `source` attribute on fired signals; the TUI labels these turns `schedule`. */\nexport const SCHEDULE_SIGNAL_SOURCE = 'schedule';\n\n/** When a schedule fires. Only recurring `every` triggers exist today. */\nexport type ScheduleTrigger = { kind: 'every'; interval: ParsedInterval };\n\n/** A file a schedule runs as a script, or re-reads and sends as the prompt, on every fire. */\nexport type ScheduleFile = { path: string; displayPath: string; mode: 'script' | 'prompt' };\n\n/** What to send on each fire: `prompt` text, or a `file` (plus `extraPrompt` appended after it). */\nexport type ThreadScheduleSource = { prompt: string } | { file: ScheduleFile; extraPrompt?: string };\n\nexport type ThreadScheduleSpec = ThreadScheduleSource & {\n trigger: ScheduleTrigger;\n /** Who is creating it: the user via `/schedules`, or the agent via its schedule tools. Defaults to `user`. */\n createdBy?: 'user' | 'agent';\n};\n\nexport type ThreadSchedule = {\n id: string;\n threadId: string;\n resourceId: string;\n trigger: ScheduleTrigger;\n prompt?: string;\n file?: ScheduleFile;\n extraPrompt?: string;\n createdBy: 'user' | 'agent';\n status: 'active' | 'paused';\n /** Next boundary this schedule fires at; only meaningful while active. */\n nextFireAt: number;\n createdAt: number;\n};\n\nexport type ThreadSchedulerOptions = {\n /** Build the prompt for one fire (runs scripts / reads files). */\n assemblePrompt: (schedule: ThreadSchedule) => Promise<AssembledPrompt>;\n /** Hand the prompt to the agent for the schedule's thread. */\n deliver: (schedule: ThreadSchedule, assembled: AssembledPrompt) => Promise<void>;\n /** A fire failed before reaching the agent. */\n onError?: (error: unknown, schedule: ThreadSchedule) => void;\n now?: () => number;\n};\n\n/** How late a timer may run before its fire counts as missed (sleep, stalled event loop). */\nexport const LATE_FIRE_GRACE_MS = 60_000;\n\n/**\n * Outcome of a manual `run()`. `failed` means nothing reached the agent: the\n * prompt could not be built or the agent refused it.\n */\nexport type ScheduleRunResult =\n | { status: 'fired' }\n | { status: 'failed'; error: unknown }\n | { status: 'busy' }\n | { status: 'not-found' };\n\ntype FireOutcome = { kind: 'delivered' } | { kind: 'deleted' } | { kind: 'failed'; error: unknown };\n\ntype Entry = { schedule: ThreadSchedule; timer?: ReturnType<typeof setTimeout>; inFlight: number };\n\nexport class ThreadScheduler {\n #entries = new Map<string, Entry>();\n #options: ThreadSchedulerOptions;\n\n constructor(options: ThreadSchedulerOptions) {\n this.#options = options;\n }\n\n #now(): number {\n return this.#options.now?.() ?? Date.now();\n }\n\n /** Throws when the interval is not a supported wall-clock cadence (see `validateInterval`) or the prompt is empty. */\n create(spec: ThreadScheduleSpec, target: { threadId: string; resourceId: string }): ThreadSchedule {\n const { interval } = spec.trigger;\n const check = validateInterval(interval);\n if ('error' in check) {\n throw new Error(\n `Invalid schedule interval: ${check.error}${check.suggestion ? ` Try ${check.suggestion}.` : ''}`,\n );\n }\n if ('prompt' in spec && !spec.prompt.trim()) throw new Error('Schedule prompt is empty.');\n const now = this.#now();\n const schedule: ThreadSchedule = {\n id: randomUUID(),\n threadId: target.threadId,\n resourceId: target.resourceId,\n trigger: { kind: 'every', interval: { ...interval } },\n ...('file' in spec\n ? { file: { ...spec.file }, ...(spec.extraPrompt ? { extraPrompt: spec.extraPrompt } : {}) }\n : { prompt: spec.prompt }),\n createdBy: spec.createdBy ?? 'user',\n status: 'active',\n nextFireAt: nextFireTime(interval.ms, now),\n createdAt: now,\n };\n const entry: Entry = { schedule, inFlight: 0 };\n this.#entries.set(schedule.id, entry);\n this.#arm(entry);\n return { ...schedule };\n }\n\n list(filter: { threadId?: string } = {}): ThreadSchedule[] {\n return [...this.#entries.values()]\n .map(entry => ({ ...entry.schedule }))\n .filter(schedule => !filter.threadId || schedule.threadId === filter.threadId)\n .sort((a, b) => a.createdAt - b.createdAt);\n }\n\n delete(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n clearTimeout(entry.timer);\n this.#entries.delete(id);\n return true;\n }\n\n pause(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n clearTimeout(entry.timer);\n entry.timer = undefined;\n entry.schedule.status = 'paused';\n return true;\n }\n\n resume(id: string): boolean {\n const entry = this.#entries.get(id);\n if (!entry) return false;\n if (entry.schedule.status === 'active') return true;\n entry.schedule.status = 'active';\n entry.schedule.nextFireAt = nextFireTime(entry.schedule.trigger.interval.ms, this.#now());\n this.#arm(entry);\n return true;\n }\n\n /** Whether a fire of this schedule (timer or manual) is still assembling or delivering. */\n isFiring(id: string): boolean {\n return (this.#entries.get(id)?.inFlight ?? 0) > 0;\n }\n\n /**\n * Fire once now, outside the cadence. Returns `busy` without firing when a\n * fire of this schedule is still running (a slow script, say), so the same\n * script never runs twice at once. Resolves once the fire finished; a fire\n * that never reached the agent resolves `failed` instead of calling `onError`.\n */\n async run(id: string): Promise<ScheduleRunResult> {\n const entry = this.#entries.get(id);\n if (!entry) return { status: 'not-found' };\n if (entry.inFlight > 0) return { status: 'busy' };\n const result = await this.#fire(entry);\n if (result.kind === 'failed') return { status: 'failed', error: result.error };\n return result.kind === 'delivered' ? { status: 'fired' } : { status: 'not-found' };\n }\n\n /** Drop every schedule and timer (process shutdown). */\n stop(): void {\n for (const entry of this.#entries.values()) clearTimeout(entry.timer);\n this.#entries.clear();\n }\n\n #arm(entry: Entry): void {\n clearTimeout(entry.timer);\n const scheduledAt = entry.schedule.nextFireAt;\n entry.timer = setTimeout(\n () => {\n if (this.#entries.get(entry.schedule.id) !== entry || entry.schedule.status !== 'active') return;\n // Timers can fire a hair early; step from the later of now and the\n // intended boundary so the same boundary is never fired twice.\n const now = this.#now();\n entry.schedule.nextFireAt = nextFireTime(entry.schedule.trigger.interval.ms, Math.max(now, scheduledAt));\n this.#arm(entry);\n // A timer that ran long after its boundary (machine asleep) is a missed fire, not a late one.\n if (now - scheduledAt > LATE_FIRE_GRACE_MS) return;\n // A slow script can outlast a short interval; don't stack fires.\n if (entry.inFlight === 0) {\n void this.#fire(entry).then(result => {\n if (result.kind === 'failed') this.#options.onError?.(result.error, { ...entry.schedule });\n });\n }\n },\n Math.max(0, scheduledAt - this.#now()),\n );\n entry.timer.unref?.();\n }\n\n async #fire(entry: Entry): Promise<FireOutcome> {\n const schedule = { ...entry.schedule };\n entry.inFlight++;\n try {\n const assembled = await this.#options.assemblePrompt(schedule);\n // Deleted while its prompt was being built: drop the fire quietly.\n if (this.#entries.get(schedule.id) !== entry) return { kind: 'deleted' };\n await this.#options.deliver(schedule, assembled);\n return { kind: 'delivered' };\n } catch (error) {\n return { kind: 'failed', error };\n } finally {\n entry.inFlight--;\n }\n }\n}\n\nexport function shortScheduleId(id: string): string {\n return id.slice(0, 8);\n}\n\n/** One-line human summary of what a schedule sends: `run ./check.sh + \"extra\"`, `read notes.md`, `\"prompt\"`. */\nexport function describeScheduleSource(schedule: Pick<ThreadSchedule, 'prompt' | 'file' | 'extraPrompt'>): string {\n if (!schedule.file) return `\"${schedule.prompt ?? ''}\"`;\n const verb = schedule.file.mode === 'script' ? 'run' : 'read';\n const extra = schedule.extraPrompt ? ` + \"${schedule.extraPrompt}\"` : '';\n return `${verb} ${schedule.file.displayPath}${extra}`;\n}\n\n/**\n * Attributes on a fired schedule signal. They carry everything the transcript\n * header needs, so reloaded history renders without the process-local entry.\n */\nexport function scheduleSignalAttributes(\n schedule: ThreadSchedule,\n assembled: Pick<AssembledPrompt, 'outcome'> = {},\n): Record<string, string> {\n return {\n source: SCHEDULE_SIGNAL_SOURCE,\n scheduleId: schedule.id,\n scheduleCadence: schedule.trigger.interval.label,\n scheduleSource: describeScheduleSource(schedule),\n scheduleCreatedBy: schedule.createdBy,\n ...(assembled.outcome ? { scheduleOutcome: assembled.outcome } : {}),\n };\n}\n"],"mappings":";;;;;;;;;;;AAaA,MAAa,yBAAyB;;AA2CtC,MAAa,qBAAqB;AAgBlC,IAAa,kBAAb,MAA6B;CAC3B,2BAAW,IAAI,IAAmB;CAClC;CAEA,YAAY,SAAiC;EAC3C,KAAK,WAAW;CAClB;CAEA,OAAe;EACb,OAAO,KAAK,SAAS,MAAM,KAAK,KAAK,IAAI;CAC3C;;CAGA,OAAO,MAA0B,QAAkE;EACjG,MAAM,EAAE,aAAa,KAAK;EAC1B,MAAM,QAAQ,iBAAiB,QAAQ;EACvC,IAAI,WAAW,OACb,MAAM,IAAI,MACR,8BAA8B,MAAM,QAAQ,MAAM,aAAa,QAAQ,MAAM,WAAW,KAAK,IAC/F;EAEF,IAAI,YAAY,QAAQ,CAAC,KAAK,OAAO,KAAK,GAAG,MAAM,IAAI,MAAM,2BAA2B;EACxF,MAAM,MAAM,KAAK,KAAK;EACtB,MAAM,WAA2B;GAC/B,IAAI,WAAW;GACf,UAAU,OAAO;GACjB,YAAY,OAAO;GACnB,SAAS;IAAE,MAAM;IAAS,UAAU,EAAE,GAAG,SAAS;GAAE;GACpD,GAAI,UAAU,OACV;IAAE,MAAM,EAAE,GAAG,KAAK,KAAK;IAAG,GAAI,KAAK,cAAc,EAAE,aAAa,KAAK,YAAY,IAAI,CAAC;GAAG,IACzF,EAAE,QAAQ,KAAK,OAAO;GAC1B,WAAW,KAAK,aAAa;GAC7B,QAAQ;GACR,YAAY,aAAa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OAAO,OAAO,EAAE,MAAM,UAAU;GACvE,MAAM,KAAK,SAAS,QAAQ,UAAU,SAAS;GAC/C,OAAO,EAAE,MAAM,YAAY;EAC7B,SAAS,OAAO;GACd,OAAO;IAAE,MAAM;IAAU;GAAM;EACjC,UAAU;GACR,MAAM;EACR;CACF;AACF;AAEA,SAAgB,gBAAgB,IAAoB;CAClD,OAAO,GAAG,MAAM,GAAG,CAAC;AACtB;;AAGA,SAAgB,uBAAuB,UAA2E;CAChH,IAAI,CAAC,SAAS,MAAM,OAAO,IAAI,SAAS,UAAU,GAAG;CACrD,MAAM,OAAO,SAAS,KAAK,SAAS,WAAW,QAAQ;CACvD,MAAM,QAAQ,SAAS,cAAc,OAAO,SAAS,YAAY,KAAK;CACtE,OAAO,GAAG,KAAK,GAAG,SAAS,KAAK,cAAc;AAChD;;;;;AAMA,SAAgB,yBACd,UACA,YAA8C,CAAC,GACvB;CACxB,OAAO;EACL,QAAQ;EACR,YAAY,SAAS;EACrB,iBAAiB,SAAS,QAAQ,SAAS;EAC3C,gBAAgB,uBAAuB,QAAQ;EAC/C,mBAAmB,SAAS;EAC5B,GAAI,UAAU,UAAU,EAAE,iBAAiB,UAAU,QAAQ,IAAI,CAAC;CACpE;AACF"}
@@ -14,13 +14,14 @@ function expandTilde(p) {
14
14
  if (p.startsWith("~/") || p.startsWith("~\\")) return path$1.join(os$1.homedir(), p.slice(2));
15
15
  return p;
16
16
  }
17
+ const requestSandboxAccessInputSchema = z.object({
18
+ path: z.string().min(1).describe("The absolute path to the directory you need access to."),
19
+ reason: z.string().min(1).describe("Brief explanation of why you need access to this directory.")
20
+ });
17
21
  const requestSandboxAccessTool = createTool({
18
22
  id: "request_access",
19
23
  description: `Request permission to access a directory outside the current project. Use this when you need to read or write files in a directory that is not within the project root. The user will be prompted to approve or deny the request.`,
20
- inputSchema: z.object({
21
- path: z.string().min(1).describe("The absolute path to the directory you need access to."),
22
- reason: z.string().min(1).describe("Brief explanation of why you need access to this directory.")
23
- }),
24
+ inputSchema: requestSandboxAccessInputSchema,
24
25
  suspendSchema: z.object({
25
26
  kind: z.literal("sandbox_access_request"),
26
27
  path: z.string(),
@@ -33,7 +34,8 @@ const requestSandboxAccessTool = createTool({
33
34
  const projectRoot = agentControllerCtx?.getState()?.projectPath ?? process.cwd();
34
35
  const expanded = expandTilde(requestedPath);
35
36
  const absolutePath = path$1.isAbsolute(expanded) ? expanded : path$1.resolve(projectRoot, expanded);
36
- if (isPathAllowed(absolutePath, projectRoot, getAllowedPathsFromContext(context))) return {
37
+ const allowedPaths = getAllowedPathsFromContext(context);
38
+ if (isPathAllowed(absolutePath, projectRoot, allowedPaths)) return {
37
39
  content: `Access already granted: "${absolutePath}" is within the project root or allowed paths.`,
38
40
  isError: false
39
41
  };
@@ -1 +1 @@
1
- {"version":3,"file":"request-sandbox-access.js","names":["os","path"],"sources":["../../src/tools/request-sandbox-access.ts"],"sourcesContent":["/**\n * request_access tool — requests permission to access a directory outside the project root.\n * The user can approve or deny the request via TUI dialog.\n */\n\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport type { AgentControllerRequestContext } from '@mastra/core/agent-controller';\nimport { createTool } from '@mastra/core/tools';\nimport { LocalFilesystem } from '@mastra/core/workspace';\nimport { z } from 'zod';\nimport type { MastraCodeState } from '../schema.js';\nimport { isPathAllowed, getAllowedPathsFromContext } from './utils.js';\n\nfunction expandTilde(p: string): string {\n if (p === '~') return os.homedir();\n if (p.startsWith('~/') || p.startsWith('~\\\\')) return path.join(os.homedir(), p.slice(2));\n return p;\n}\n\ntype RequestSandboxAccessInput = {\n path: string;\n reason: string;\n};\n\nconst requestSandboxAccessInputSchema = z.object({\n path: z.string().min(1).describe('The absolute path to the directory you need access to.'),\n reason: z.string().min(1).describe('Brief explanation of why you need access to this directory.'),\n});\n\nexport const requestSandboxAccessTool = createTool({\n id: 'request_access',\n description: `Request permission to access a directory outside the current project. Use this when you need to read or write files in a directory that is not within the project root. The user will be prompted to approve or deny the request.`,\n inputSchema: requestSandboxAccessInputSchema,\n suspendSchema: z.object({\n kind: z.literal('sandbox_access_request'),\n path: z.string(),\n reason: z.string(),\n }),\n resumeSchema: z.union([z.string(), z.array(z.string())]),\n execute: async ({ path: requestedPath, reason }: RequestSandboxAccessInput, context: any) => {\n try {\n const agentControllerCtx = context?.requestContext?.get('controller') as\n | AgentControllerRequestContext<MastraCodeState>\n | undefined;\n\n // Resolve to absolute path (expand ~ first since Node path APIs don't handle it)\n const projectRoot = agentControllerCtx?.getState()?.projectPath ?? process.cwd();\n const expanded = expandTilde(requestedPath);\n const absolutePath = path.isAbsolute(expanded) ? expanded : path.resolve(projectRoot, expanded);\n\n // Check if already allowed\n const allowedPaths = getAllowedPathsFromContext(context);\n if (isPathAllowed(absolutePath, projectRoot, allowedPaths)) {\n return {\n content: `Access already granted: \"${absolutePath}\" is within the project root or allowed paths.`,\n isError: false,\n };\n }\n\n const suspend = context?.agent?.suspend ?? context?.suspend;\n const resumeData = context?.agent?.resumeData ?? context?.resumeData;\n\n // First pass: pause via the native tool-suspension primitive so the host can\n // prompt the user. The suspend payload carries the request details; the host\n // resumes with the user's answer as resume data.\n if (resumeData === undefined) {\n if (!suspend) {\n return {\n content: `Cannot request sandbox access: interactive context not available. The user should manually run /sandbox add ${absolutePath}`,\n isError: true,\n };\n }\n await suspend({ kind: 'sandbox_access_request', path: absolutePath, reason });\n return;\n }\n\n const answerText = Array.isArray(resumeData) ? resumeData.join(', ') : String(resumeData);\n const approved = answerText.toLowerCase().startsWith('y') || answerText.toLowerCase() === 'approve';\n if (approved) {\n // Persist to controller state first (and await it) so the value is\n // committed before the next tool call re-derives the workspace's\n // allowed paths from state. The workspace factory rebuilds the\n // filesystem allowlist from `sandboxAllowedPaths` on every call\n // (getDynamicWorkspace), so an unawaited setState would let that\n // rebuild clobber the in-turn widen below before the grant lands.\n const controllerState = agentControllerCtx?.getState();\n const currentAllowed = (controllerState?.sandboxAllowedPaths as string[] | undefined) ?? [];\n const nextAllowed = currentAllowed.includes(absolutePath) ? currentAllowed : [...currentAllowed, absolutePath];\n if (!currentAllowed.includes(absolutePath)) {\n await agentControllerCtx?.setState({\n sandboxAllowedPaths: nextAllowed,\n });\n }\n\n // Also update the live workspace filesystem immediately so tools in\n // the same turn (e.g. `view`) can access the path without waiting for\n // a fresh workspace build. The resolved workspace is carried on the\n // controller request context — the tool-execution context does not expose\n // it — so read the filesystem from there.\n const fs = agentControllerCtx?.workspace?.filesystem ?? context?.workspace?.filesystem;\n if (fs instanceof LocalFilesystem) {\n fs.setAllowedPaths((prev: readonly string[]) => [...prev, absolutePath]);\n }\n\n return {\n content: `Access granted: \"${absolutePath}\" has been added to allowed paths. You can now access files in this directory.`,\n isError: false,\n };\n } else {\n return {\n content: `Access denied: The user declined access to \"${absolutePath}\".`,\n isError: false,\n };\n }\n } catch (error) {\n const msg = error instanceof Error ? error.message : 'Unknown error';\n return {\n content: `Failed to request sandbox access: ${msg}`,\n isError: true,\n };\n }\n },\n} as any);\n"],"mappings":";;;;;;;;;;;AAcA,SAAS,YAAY,GAAmB;CACtC,IAAI,MAAM,KAAK,OAAOA,KAAG,QAAQ;CACjC,IAAI,EAAE,WAAW,IAAI,KAAK,EAAE,WAAW,KAAK,GAAG,OAAOC,OAAK,KAAKD,KAAG,QAAQ,GAAG,EAAE,MAAM,CAAC,CAAC;CACxF,OAAO;AACT;AAYA,MAAa,2BAA2B,WAAW;CACjD,IAAI;CACJ,aAAa;CACb,aARsC,EAAE,OAAO;EAC/C,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,wDAAwD;EACzF,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,6DAA6D;CAClG,CAKe;CACb,eAAe,EAAE,OAAO;EACtB,MAAM,EAAE,QAAQ,wBAAwB;EACxC,MAAM,EAAE,OAAO;EACf,QAAQ,EAAE,OAAO;CACnB,CAAC;CACD,cAAc,EAAE,MAAM,CAAC,EAAE,OAAO,G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S,oBAAoB,aAAa;KAC1C,SAAS;IACX;GACF,OACE,OAAO;IACL,SAAS,+CAA+C,aAAa;IACrE,SAAS;GACX;EAEJ,SAAS,OAAO;GAEd,OAAO;IACL,SAAS,qCAFC,iBAAiB,QAAQ,MAAM,UAAU;IAGnD,SAAS;GACX;EACF;CACF;AACF,CAAQ"}
1
+ {"version":3,"file":"request-sandbox-access.js","names":["os","path"],"sources":["../../src/tools/request-sandbox-access.ts"],"sourcesContent":["/**\n * request_access tool — requests permission to access a directory outside the project root.\n * The user can approve or deny the request via TUI dialog.\n */\n\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport type { AgentControllerRequestContext } from '@mastra/core/agent-controller';\nimport { createTool } from '@mastra/core/tools';\nimport { LocalFilesystem } from '@mastra/core/workspace';\nimport { z } from 'zod';\nimport type { MastraCodeState } from '../schema.js';\nimport { isPathAllowed, getAllowedPathsFromContext } from './utils.js';\n\nfunction expandTilde(p: string): string {\n if (p === '~') return os.homedir();\n if (p.startsWith('~/') || p.startsWith('~\\\\')) return path.join(os.homedir(), p.slice(2));\n return p;\n}\n\ntype RequestSandboxAccessInput = {\n path: string;\n reason: string;\n};\n\nconst requestSandboxAccessInputSchema = z.object({\n path: z.string().min(1).describe('The absolute path to the directory you need access to.'),\n reason: z.string().min(1).describe('Brief explanation of why you need access to this directory.'),\n});\n\nexport const requestSandboxAccessTool = createTool({\n id: 'request_access',\n description: `Request permission to access a directory outside the current project. Use this when you need to read or write files in a directory that is not within the project root. The user will be prompted to approve or deny the request.`,\n inputSchema: requestSandboxAccessInputSchema,\n suspendSchema: z.object({\n kind: z.literal('sandbox_access_request'),\n path: z.string(),\n reason: z.string(),\n }),\n resumeSchema: z.union([z.string(), z.array(z.string())]),\n execute: async ({ path: requestedPath, reason }: RequestSandboxAccessInput, context: any) => {\n try {\n const agentControllerCtx = context?.requestContext?.get('controller') as\n | AgentControllerRequestContext<MastraCodeState>\n | undefined;\n\n // Resolve to absolute path (expand ~ first since Node path APIs don't handle it)\n const projectRoot = agentControllerCtx?.getState()?.projectPath ?? process.cwd();\n const expanded = expandTilde(requestedPath);\n const absolutePath = path.isAbsolute(expanded) ? expanded : path.resolve(projectRoot, expanded);\n\n // Check if already allowed\n const allowedPaths = getAllowedPathsFromContext(context);\n if (isPathAllowed(absolutePath, projectRoot, allowedPaths)) {\n return {\n content: `Access already granted: \"${absolutePath}\" is within the project root or allowed paths.`,\n isError: false,\n };\n }\n\n const suspend = context?.agent?.suspend ?? context?.suspend;\n const resumeData = context?.agent?.resumeData ?? context?.resumeData;\n\n // First pass: pause via the native tool-suspension primitive so the host can\n // prompt the user. The suspend payload carries the request details; the host\n // resumes with the user's answer as resume data.\n if (resumeData === undefined) {\n if (!suspend) {\n return {\n content: `Cannot request sandbox access: interactive context not available. The user should manually run /sandbox add ${absolutePath}`,\n isError: true,\n };\n }\n await suspend({ kind: 'sandbox_access_request', path: absolutePath, reason });\n return;\n }\n\n const answerText = Array.isArray(resumeData) ? resumeData.join(', ') : String(resumeData);\n const approved = answerText.toLowerCase().startsWith('y') || answerText.toLowerCase() === 'approve';\n if (approved) {\n // Persist to controller state first (and await it) so the value is\n // committed before the next tool call re-derives the workspace's\n // allowed paths from state. The workspace factory rebuilds the\n // filesystem allowlist from `sandboxAllowedPaths` on every call\n // (getDynamicWorkspace), so an unawaited setState would let that\n // rebuild clobber the in-turn widen below before the grant lands.\n const controllerState = agentControllerCtx?.getState();\n const currentAllowed = (controllerState?.sandboxAllowedPaths as string[] | undefined) ?? [];\n const nextAllowed = currentAllowed.includes(absolutePath) ? currentAllowed : [...currentAllowed, absolutePath];\n if (!currentAllowed.includes(absolutePath)) {\n await agentControllerCtx?.setState({\n sandboxAllowedPaths: nextAllowed,\n });\n }\n\n // Also update the live workspace filesystem immediately so tools in\n // the same turn (e.g. `view`) can access the path without waiting for\n // a fresh workspace build. The resolved workspace is carried on the\n // controller request context — the tool-execution context does not expose\n // it — so read the filesystem from there.\n const fs = agentControllerCtx?.workspace?.filesystem ?? context?.workspace?.filesystem;\n if (fs instanceof LocalFilesystem) {\n fs.setAllowedPaths((prev: readonly string[]) => [...prev, absolutePath]);\n }\n\n return {\n content: `Access granted: \"${absolutePath}\" has been added to allowed paths. You can now access files in this directory.`,\n isError: false,\n };\n } else {\n return {\n content: `Access denied: The user declined access to \"${absolutePath}\".`,\n isError: false,\n };\n }\n } catch (error) {\n const msg = error instanceof Error ? error.message : 'Unknown error';\n return {\n content: `Failed to request sandbox access: ${msg}`,\n isError: true,\n };\n }\n },\n} as any);\n"],"mappings":";;;;;;;;;;;AAcA,SAAS,YAAY,GAAmB;CACtC,IAAI,MAAM,KAAK,OAAOA,KAAG,QAAQ;CACjC,IAAI,EAAE,WAAW,IAAI,KAAK,EAAE,WAAW,KAAK,GAAG,OAAOC,OAAK,KAAKD,KAAG,QAAQ,GAAG,EAAE,MAAM,CAAC,CAAC;CACxF,OAAO;AACT;AAOA,MAAM,kCAAkC,EAAE,OAAO;CAC/C,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,wDAAwD;CACzF,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,6DAA6D;AAClG,CAAC;AAED,MAAa,2BAA2B,WAAW;CACjD,IAAI;CACJ,aAAa;CACb,aAAa;CACb,eAAe,EAAE,OAAO;EACtB,MAAM,EAAE,QAAQ,wBAAwB;EACxC,MAAM,EAAE,OAAO;EACf,QAAQ,EAAE,OAAO;CACnB,CAAC;CACD,cAAc,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACvD,SAAS,OAAO,EAAE,MAAM,eAAe,UAAqC,YAAiB;EAC3F,IAAI;GACF,MAAM,qBAAqB,SAAS,gBAAgB,IAAI,YAAY;GAKpE,MAAM,cAAc,oBAAoB,SAAS,CAAC,EAAE,eAAe,QAAQ,IAAI;GAC/E,MAAM,WAAW,YAAY,aAAa;GAC1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owed(targetPath, projectRoot, allowedPaths = []) {
22
22
  function getAllowedPathsFromContext(toolContext) {
23
23
  const agentControllerCtx = toolContext?.requestContext?.get("controller");
24
24
  const state = agentControllerCtx?.getState?.() ?? agentControllerCtx?.session?.state?.get?.();
25
- const skillPaths = buildSkillPaths(state?.projectPath ? path$1.resolve(state.projectPath) : process.cwd(), state?.configDir ?? ".mastracode");
25
+ const projectPath = state?.projectPath ? path$1.resolve(state.projectPath) : process.cwd();
26
+ const configDir = state?.configDir ?? ".mastracode";
27
+ const skillPaths = buildSkillPaths(projectPath, configDir);
26
28
  const sandboxPaths = state?.sandboxAllowedPaths ?? [];
27
29
  return [...skillPaths, ...sandboxPaths];
28
30
  }
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":["path"],"sources":["../../src/tools/utils.ts"],"sourcesContent":["import * as path from 'node:path';\nimport { buildSkillPaths } from '../agents/workspace.js';\nimport { DEFAULT_CONFIG_DIR } from '../constants.js';\n\n/**\n * Check whether `targetPath` falls inside `projectRoot` or any of the\n * additional `allowedPaths`. All arguments are expected to be absolute.\n *\n * Returns `true` when access should be **allowed**.\n */\nexport function isPathAllowed(targetPath: string, projectRoot: string, allowedPaths: string[] = []): boolean {\n const resolved = path.resolve(targetPath);\n const roots = [projectRoot, ...allowedPaths].map(p => path.resolve(p));\n\n return roots.some(root => resolved === root || resolved.startsWith(root + path.sep));\n}\n\n/**\n * Read allowed paths from the Mastra controller runtime context.\n * Combines skill paths (computed dynamically from projectPath and configDir)\n * with user-approved sandbox paths from controller state so that both parent\n * and subagent tools have the same access.\n * Returns default skill paths when the context is unavailable (e.g. in tests).\n */\nexport function getAllowedPathsFromContext(\n toolContext: { requestContext?: { get: (key: string) => unknown } } | undefined,\n): string[] {\n const agentControllerCtx = toolContext?.requestContext?.get('controller') as\n | {\n getState?: () => { sandboxAllowedPaths?: string[]; projectPath?: string; configDir?: string };\n session?: {\n state?: { get?: () => { sandboxAllowedPaths?: string[]; projectPath?: string; configDir?: string } };\n };\n }\n | undefined;\n const state = agentControllerCtx?.getState?.() ?? agentControllerCtx?.session?.state?.get?.();\n const projectPath = state?.projectPath ? path.resolve(state.projectPath) : process.cwd();\n const configDir = state?.configDir ?? DEFAULT_CONFIG_DIR;\n const skillPaths = buildSkillPaths(projectPath, configDir);\n const sandboxPaths = state?.sandboxAllowedPaths ?? [];\n\n return [...skillPaths, ...sandboxPaths];\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,cAAc,YAAoB,aAAqB,eAAyB,CAAC,GAAY;CAC3G,MAAM,WAAWA,OAAK,QAAQ,UAAU;CAGxC,OAFc,CAAC,aAAa,GAAG,YAAY,CAAC,CAAC,KAAI,MAAKA,OAAK,QAAQ,CAAC,CAEzD,CAAC,CAAC,MAAK,SAAQ,aAAa,QAAQ,SAAS,WAAW,OAAOA,OAAK,GAAG,CAAC;AACrF;;;;;;;;AASA,SAAgB,2BACd,aACU;CACV,MAAM,qBAAqB,aAAa,gBAAgB,IAAI,YAAY;CAQxE,MAAM,QAAQ,oBAAoB,WAAW,KAAK,oBAAoB,SAAS,OAAO,MAAM;CAG5F,MAAM,aAAa,gBAFC,OAAO,cAAcA,OAAK,QAAQ,MAAM,WAAW,IAAI,QAAQ,IAAI,GACrE,OAAO,aAAA,aACgC;CACzD,MAAM,eAAe,OAAO,uBAAuB,CAAC;CAEpD,OAAO,CAAC,GAAG,YAAY,GAAG,YAAY;AACxC"}
1
+ {"version":3,"file":"utils.js","names":["path"],"sources":["../../src/tools/utils.ts"],"sourcesContent":["import * as path from 'node:path';\nimport { buildSkillPaths } from '../agents/workspace.js';\nimport { DEFAULT_CONFIG_DIR } from '../constants.js';\n\n/**\n * Check whether `targetPath` falls inside `projectRoot` or any of the\n * additional `allowedPaths`. All arguments are expected to be absolute.\n *\n * Returns `true` when access should be **allowed**.\n */\nexport function isPathAllowed(targetPath: string, projectRoot: string, allowedPaths: string[] = []): boolean {\n const resolved = path.resolve(targetPath);\n const roots = [projectRoot, ...allowedPaths].map(p => path.resolve(p));\n\n return roots.some(root => resolved === root || resolved.startsWith(root + path.sep));\n}\n\n/**\n * Read allowed paths from the Mastra controller runtime context.\n * Combines skill paths (computed dynamically from projectPath and configDir)\n * with user-approved sandbox paths from controller state so that both parent\n * and subagent tools have the same access.\n * Returns default skill paths when the context is unavailable (e.g. in tests).\n */\nexport function getAllowedPathsFromContext(\n toolContext: { requestContext?: { get: (key: string) => unknown } } | undefined,\n): string[] {\n const agentControllerCtx = toolContext?.requestContext?.get('controller') as\n | {\n getState?: () => { sandboxAllowedPaths?: string[]; projectPath?: string; configDir?: string };\n session?: {\n state?: { get?: () => { sandboxAllowedPaths?: string[]; projectPath?: string; configDir?: string } };\n };\n }\n | undefined;\n const state = agentControllerCtx?.getState?.() ?? agentControllerCtx?.session?.state?.get?.();\n const projectPath = state?.projectPath ? path.resolve(state.projectPath) : process.cwd();\n const configDir = state?.configDir ?? DEFAULT_CONFIG_DIR;\n const skillPaths = buildSkillPaths(projectPath, configDir);\n const sandboxPaths = state?.sandboxAllowedPaths ?? [];\n\n return [...skillPaths, ...sandboxPaths];\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,cAAc,YAAoB,aAAqB,eAAyB,CAAC,GAAY;CAC3G,MAAM,WAAWA,OAAK,QAAQ,UAAU;CAGxC,OAFc,CAAC,aAAa,GAAG,YAAY,CAAC,CAAC,KAAI,MAAKA,OAAK,QAAQ,CAAC,CAEzD,CAAC,CAAC,MAAK,SAAQ,aAAa,QAAQ,SAAS,WAAW,OAAOA,OAAK,GAAG,CAAC;AACrF;;;;;;;;AASA,SAAgB,2BACd,aACU;CACV,MAAM,qBAAqB,aAAa,gBAAgB,IAAI,YAAY;CAQxE,MAAM,QAAQ,oBAAoB,WAAW,KAAK,oBAAoB,SAAS,OAAO,MAAM;CAC5F,MAAM,cAAc,OAAO,cAAcA,OAAK,QAAQ,MAAM,WAAW,IAAI,QAAQ,IAAI;CACvF,MAAM,YAAY,OAAO,aAAA;CACzB,MAAM,aAAa,gBAAgB,aAAa,SAAS;CACzD,MAAM,eAAe,OAAO,uBAAuB,CAAC;CAEpD,OAAO,CAAC,GAAG,YAAY,GAAG,YAAY;AACxC"}