|
| 1 | +import { BailianError } from "bailian-cli-core"; |
| 2 | +import { mcpMarketplaceDetailPage } from "bailian-cli-runtime"; |
| 3 | + |
| 4 | +/** Detect MCP-not-activated / invalid 404 errors (CLI-wrapped server message). */ |
| 5 | +export function isMcpNotActivated(error: unknown): boolean { |
| 6 | + if (!(error instanceof BailianError)) return false; |
| 7 | + const message = error.message; |
| 8 | + if (!/MCP request failed:\s*404\b/i.test(message)) return false; |
| 9 | + return /未开通|MCP不存在|MCP_IS_INVALID/i.test(message); |
| 10 | +} |
| 11 | + |
| 12 | +/** Activation hint; URL from runtime/urls.ts. */ |
| 13 | +export function mcpActivateHint(serverCode: string): string { |
| 14 | + const lines = [ |
| 15 | + `Activate (or re-activate) the ${serverCode} MCP in the Bailian MCP marketplace, then retry.`, |
| 16 | + ]; |
| 17 | + if (serverCode === "WebSearch") { |
| 18 | + lines.push( |
| 19 | + "If it was previously on SSE, cancel and activate again to upgrade to Streamable HTTP.", |
| 20 | + ); |
| 21 | + } |
| 22 | + lines.push(`Open: ${mcpMarketplaceDetailPage(serverCode)}`); |
| 23 | + return lines.join("\n"); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * For not-activated errors, keep the original message / exitCode and append a hint only. |
| 28 | + * Do not replace the server error message. |
| 29 | + */ |
| 30 | +export function rethrowWithMcpActivateHint(error: unknown, serverCode: string): never { |
| 31 | + if (isMcpNotActivated(error) && error instanceof BailianError && !error.hint) { |
| 32 | + throw new BailianError(error.message, error.exitCode, mcpActivateHint(serverCode), { |
| 33 | + cause: error, |
| 34 | + api: error.api, |
| 35 | + rawResponse: error.rawResponse, |
| 36 | + }); |
| 37 | + } |
| 38 | + throw error; |
| 39 | +} |
0 commit comments