@openconductor/mcp-sdk 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -288,13 +288,167 @@ async function getCheckoutUrl(pack) {
288
288
  const session = await createCreditsCheckout(pack);
289
289
  return session?.url ?? null;
290
290
  }
291
+ async function generateUpgradePrompt(options = {}) {
292
+ const { required = 0, available = 0, toolName, includeLinks = true } = options;
293
+ const needed = Math.max(0, required - available);
294
+ let recommendedPack = "starter";
295
+ if (needed > 100) recommendedPack = "pro";
296
+ if (needed > 500) recommendedPack = "business";
297
+ const packUrls = {
298
+ starter: null,
299
+ pro: null,
300
+ business: null
301
+ };
302
+ if (includeLinks) {
303
+ const [starterUrl, proUrl, businessUrl] = await Promise.all([
304
+ getCheckoutUrl("starter"),
305
+ getCheckoutUrl("pro"),
306
+ getCheckoutUrl("business")
307
+ ]);
308
+ packUrls.starter = starterUrl;
309
+ packUrls.pro = proUrl;
310
+ packUrls.business = businessUrl;
311
+ }
312
+ const toolPart = toolName ? ` to use ${toolName}` : "";
313
+ const shortMessage = `Insufficient credits: need ${required}, have ${available}`;
314
+ let message = `You need ${needed} more credits${toolPart}.
315
+
316
+ `;
317
+ message += `\u{1F4B3} Quick top-up options:
318
+ `;
319
+ message += ` \u2022 Starter: 100 credits for $9.99${packUrls.starter ? ` \u2192 ${packUrls.starter}` : ""}
320
+ `;
321
+ message += ` \u2022 Pro: 500 credits for $39.99 (20% off)${packUrls.pro ? ` \u2192 ${packUrls.pro}` : ""}
322
+ `;
323
+ message += ` \u2022 Business: 2,000 credits for $119.99 (40% off)${packUrls.business ? ` \u2192 ${packUrls.business}` : ""}
324
+ `;
325
+ if (recommendedPack !== "starter") {
326
+ message += `
327
+ \u2728 Recommended: ${recommendedPack.charAt(0).toUpperCase() + recommendedPack.slice(1)} Pack`;
328
+ }
329
+ return {
330
+ message,
331
+ shortMessage,
332
+ recommendedPack,
333
+ packs: {
334
+ starter: { credits: 100, price: 9.99, url: packUrls.starter },
335
+ pro: { credits: 500, price: 39.99, url: packUrls.pro },
336
+ business: { credits: 2e3, price: 119.99, url: packUrls.business }
337
+ }
338
+ };
339
+ }
340
+ function getDashboardCheckoutUrl(options = {}) {
341
+ const params = new URLSearchParams();
342
+ params.set("action", "buy-credits");
343
+ if (options.required) params.set("required", String(options.required));
344
+ if (options.available) params.set("available", String(options.available));
345
+ if (options.pack) params.set("pack", options.pack);
346
+ return `https://dashboard.openconductor.ai?${params.toString()}`;
347
+ }
348
+ function createUpgradeErrorHandler(handlers) {
349
+ return async (error) => {
350
+ if (error instanceof InsufficientCreditsError && handlers.onInsufficientCredits) {
351
+ const prompt = await generateUpgradePrompt({
352
+ required: error.data?.required,
353
+ available: error.data?.available
354
+ });
355
+ return handlers.onInsufficientCredits(prompt);
356
+ }
357
+ if (error instanceof SubscriptionRequiredError && handlers.onSubscriptionRequired) {
358
+ return handlers.onSubscriptionRequired(error);
359
+ }
360
+ if (error instanceof PaymentRequiredError && handlers.onPaymentRequired) {
361
+ return handlers.onPaymentRequired(error);
362
+ }
363
+ if (handlers.onOtherError) {
364
+ return handlers.onOtherError(error);
365
+ }
366
+ throw error;
367
+ };
368
+ }
369
+ async function getUserAnalytics(userId, period = "30d") {
370
+ const config = paymentConfig;
371
+ if (!config) {
372
+ console.error("[payment] Payment not initialized. Call initPayment() first.");
373
+ return null;
374
+ }
375
+ if (config.testMode) {
376
+ return {
377
+ period,
378
+ balance: 9999,
379
+ summary: {
380
+ totalUsed: 100,
381
+ totalPurchased: 500,
382
+ netChange: 400,
383
+ burnRate: 3.33,
384
+ daysRemaining: 3e3,
385
+ toolCount: 5,
386
+ transactionCount: 30
387
+ },
388
+ topTools: [
389
+ { tool: "test-tool", calls: 10, credits: 50 }
390
+ ],
391
+ usageTimeline: [],
392
+ recentTransactions: []
393
+ };
394
+ }
395
+ try {
396
+ const response = await fetch(
397
+ `${config.apiUrl}/functions/v1/usage-analytics/${userId}?period=${period}`,
398
+ {
399
+ headers: {
400
+ "Authorization": `Bearer ${config.apiKey}`
401
+ }
402
+ }
403
+ );
404
+ if (!response.ok) return null;
405
+ return await response.json();
406
+ } catch (error) {
407
+ console.error("[payment] Analytics fetch error:", error);
408
+ return null;
409
+ }
410
+ }
411
+ async function checkLowBalance(userId, thresholdDays = 7) {
412
+ const analytics = await getUserAnalytics(userId, "7d");
413
+ if (!analytics) return null;
414
+ const { balance, summary } = analytics;
415
+ const { burnRate, daysRemaining } = summary;
416
+ const isLow = daysRemaining !== null && daysRemaining <= thresholdDays;
417
+ let recommendedPack = "starter";
418
+ const weeklyBurn = burnRate * 7;
419
+ if (weeklyBurn > 100) recommendedPack = "pro";
420
+ if (weeklyBurn > 400) recommendedPack = "business";
421
+ let message = "";
422
+ if (isLow) {
423
+ if (daysRemaining === 0) {
424
+ message = `\u26A0\uFE0F Credits depleted! Balance: ${balance}. Buy more to continue.`;
425
+ } else if (daysRemaining <= 1) {
426
+ message = `\u26A0\uFE0F Running low! ~${daysRemaining} day of credits left at current usage.`;
427
+ } else {
428
+ message = `\u{1F4CA} ~${daysRemaining} days of credits remaining at current burn rate (${burnRate.toFixed(1)}/day).`;
429
+ }
430
+ }
431
+ return {
432
+ isLow,
433
+ daysRemaining,
434
+ balance,
435
+ burnRate,
436
+ message,
437
+ recommendedPack
438
+ };
439
+ }
291
440
 
292
441
  exports.canUserAccess = canUserAccess;
442
+ exports.checkLowBalance = checkLowBalance;
293
443
  exports.createCreditsCheckout = createCreditsCheckout;
294
444
  exports.createPaidTool = createPaidTool;
445
+ exports.createUpgradeErrorHandler = createUpgradeErrorHandler;
446
+ exports.generateUpgradePrompt = generateUpgradePrompt;
295
447
  exports.getCheckoutUrl = getCheckoutUrl;
296
448
  exports.getCreditPacks = getCreditPacks;
449
+ exports.getDashboardCheckoutUrl = getDashboardCheckoutUrl;
297
450
  exports.getPaymentConfig = getPaymentConfig;
451
+ exports.getUserAnalytics = getUserAnalytics;
298
452
  exports.getUserBillingStatus = getUserBillingStatus;
299
453
  exports.initPayment = initPayment;
300
454
  exports.requirePayment = requirePayment;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts","../../src/payment/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA,EAkBxB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,oBAAA,EAAsB,MAAA;AAAA,EACtB,qBAAA,EAAuB;AACzB,CAAA;;;AClBO,IAAM,QAAA,GAAN,cAAuB,KAAA,CAAM;AAAA,EAClB,IAAA;AAAA,EACA,IAAA;AAAA,EAEhB,WAAA,CACE,IAAA,EACA,OAAA,EACA,IAAA,EACA;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAGZ,IAAA,IAAI,MAAM,iBAAA,EAAmB;AAC3B,MAAA,KAAA,CAAM,iBAAA,CAAkB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAAA,IAChD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAA,GAAS;AACP,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,SAAS,IAAA,CAAK,OAAA;AAAA,MACd,GAAI,IAAA,CAAK,IAAA,IAAQ,EAAE,IAAA,EAAM,KAAK,IAAA;AAAK,KACrC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,UAAA,CAAW,KAA6B,IAAA,EAAM;AAC5C,IAAA,OAAO;AAAA,MACL,OAAA,EAAS,KAAA;AAAA,MACT,EAAA;AAAA,MACA,KAAA,EAAO,KAAK,MAAA;AAAO,KACrB;AAAA,EACF;AACF,CAAA;AAyHO,IAAM,kBAAA,GAAN,cAAiC,QAAA,CAAS;AAAA,EAC/C,WAAA,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\nexport const ErrorCodes = {\n // JSON-RPC 2.0 Standard Errors\n PARSE_ERROR: -32700,\n INVALID_REQUEST: -32600,\n METHOD_NOT_FOUND: -32601,\n INVALID_PARAMS: -32602,\n INTERNAL_ERROR: -32603,\n\n // MCP-Specific Errors (-32000 to -32099 reserved for implementation)\n TOOL_NOT_FOUND: -32001,\n TOOL_EXECUTION_ERROR: -32002,\n RESOURCE_NOT_FOUND: -32003,\n AUTHENTICATION_ERROR: -32004,\n AUTHORIZATION_ERROR: -32005,\n RATE_LIMIT_ERROR: -32006,\n TIMEOUT_ERROR: -32007,\n VALIDATION_ERROR: -32008,\n DEPENDENCY_ERROR: -32009,\n CONFIGURATION_ERROR: -32010,\n PAYMENT_REQUIRED: -32011,\n INSUFFICIENT_CREDITS: -32012,\n SUBSCRIPTION_REQUIRED: -32013,\n} as const\n\nexport type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]\n","import { ErrorCodes, type ErrorCode } from './codes'\n\nexport { ErrorCodes, type ErrorCode } from './codes'\n\n/**\n * Base error class for MCP servers\n * Formats errors according to JSON-RPC 2.0 specification\n */\nexport class MCPError extends Error {\n public readonly code: ErrorCode\n public readonly data?: Record<string, unknown>\n\n constructor(\n code: ErrorCode,\n message: string,\n data?: Record<string, unknown>\n ) {\n super(message)\n this.name = 'MCPError'\n this.code = code\n this.data = data\n\n // Maintains proper stack trace in V8 environments\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor)\n }\n }\n\n /**\n * Returns JSON-RPC 2.0 formatted error object\n */\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n ...(this.data && { data: this.data }),\n }\n }\n\n /**\n * Create error response for JSON-RPC\n */\n toResponse(id: string | number | null = null) {\n return {\n jsonrpc: '2.0' as const,\n id,\n error: this.toJSON(),\n }\n }\n}\n\n/**\n * Thrown when tool input validation fails\n */\nexport class ValidationError extends MCPError {\n constructor(field: string, reason: string, value?: unknown) {\n super(ErrorCodes.INVALID_PARAMS, `Validation failed for '${field}': ${reason}`, {\n field,\n reason,\n ...(value !== undefined && { value }),\n })\n this.name = 'ValidationError'\n }\n}\n\n/**\n * Thrown when a requested tool doesn't exist\n */\nexport class ToolNotFoundError extends MCPError {\n constructor(toolName: string) {\n super(ErrorCodes.TOOL_NOT_FOUND, `Tool '${toolName}' not found`, {\n tool: toolName,\n })\n this.name = 'ToolNotFoundError'\n }\n}\n\n/**\n * Thrown when tool execution fails\n */\nexport class ToolExecutionError extends MCPError {\n constructor(toolName: string, reason: string, cause?: Error) {\n super(ErrorCodes.TOOL_EXECUTION_ERROR, `Tool '${toolName}' failed: ${reason}`, {\n tool: toolName,\n reason,\n ...(cause && { cause: cause.message }),\n })\n this.name = 'ToolExecutionError'\n }\n}\n\n/**\n * Thrown when a requested resource doesn't exist\n */\nexport class ResourceNotFoundError extends MCPError {\n constructor(resourceUri: string) {\n super(ErrorCodes.RESOURCE_NOT_FOUND, `Resource '${resourceUri}' not found`, {\n uri: resourceUri,\n })\n this.name = 'ResourceNotFoundError'\n }\n}\n\n/**\n * Thrown when authentication fails\n */\nexport class AuthenticationError extends MCPError {\n constructor(reason: string = 'Authentication required') {\n super(ErrorCodes.AUTHENTICATION_ERROR, reason)\n this.name = 'AuthenticationError'\n }\n}\n\n/**\n * Thrown when authorization fails (authenticated but not permitted)\n */\nexport class AuthorizationError extends MCPError {\n constructor(action: string, resource?: string) {\n const msg = resource\n ? `Not authorized to ${action} on '${resource}'`\n : `Not authorized to ${action}`\n super(ErrorCodes.AUTHORIZATION_ERROR, msg, {\n action,\n ...(resource && { resource }),\n })\n this.name = 'AuthorizationError'\n }\n}\n\n/**\n * Thrown when rate limits are exceeded\n */\nexport class RateLimitError extends MCPError {\n constructor(retryAfterMs?: number) {\n super(ErrorCodes.RATE_LIMIT_ERROR, 'Rate limit exceeded', {\n ...(retryAfterMs && { retryAfterMs }),\n })\n this.name = 'RateLimitError'\n }\n}\n\n/**\n * Thrown when an operation times out\n */\nexport class TimeoutError extends MCPError {\n constructor(operation: string, timeoutMs: number) {\n super(ErrorCodes.TIMEOUT_ERROR, `Operation '${operation}' timed out after ${timeoutMs}ms`, {\n operation,\n timeoutMs,\n })\n this.name = 'TimeoutError'\n }\n}\n\n/**\n * Thrown when a required dependency is unavailable\n */\nexport class DependencyError extends MCPError {\n constructor(dependency: string, reason: string) {\n super(ErrorCodes.DEPENDENCY_ERROR, `Dependency '${dependency}' unavailable: ${reason}`, {\n dependency,\n reason,\n })\n this.name = 'DependencyError'\n }\n}\n\n/**\n * Thrown when server configuration is invalid\n */\nexport class ConfigurationError extends MCPError {\n constructor(setting: string, reason: string) {\n super(ErrorCodes.CONFIGURATION_ERROR, `Invalid configuration '${setting}': ${reason}`, {\n setting,\n reason,\n })\n this.name = 'ConfigurationError'\n }\n}\n\n/**\n * Thrown when payment is required to access a tool\n */\nexport class PaymentRequiredError extends MCPError {\n constructor(toolName: string, options?: { upgradeUrl?: string; priceId?: string }) {\n super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {\n tool: toolName,\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n ...(options?.priceId && { priceId: options.priceId }),\n })\n this.name = 'PaymentRequiredError'\n }\n}\n\n/**\n * Thrown when user doesn't have enough credits\n */\nexport class InsufficientCreditsError extends MCPError {\n constructor(required: number, available: number, options?: { purchaseUrl?: string }) {\n super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {\n required,\n available,\n ...(options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }),\n })\n this.name = 'InsufficientCreditsError'\n }\n}\n\n/**\n * Thrown when a subscription tier is required\n */\nexport class SubscriptionRequiredError extends MCPError {\n constructor(requiredTier: string, currentTier?: string, options?: { upgradeUrl?: string }) {\n const msg = currentTier \n ? `Subscription '${requiredTier}' required (current: '${currentTier}')`\n : `Subscription '${requiredTier}' required`\n super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {\n requiredTier,\n ...(currentTier && { currentTier }),\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n })\n this.name = 'SubscriptionRequiredError'\n }\n}\n","import {\n PaymentRequiredError,\n InsufficientCreditsError,\n SubscriptionRequiredError,\n ConfigurationError,\n} from '../errors'\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PaymentConfig {\n /** OpenConductor API key for billing */\n apiKey: string\n /** Base URL for billing API (default: https://api.openconductor.ai) */\n apiUrl?: string\n /** Default upgrade URL to show users */\n upgradeUrl?: string\n /** Enable test mode (skips actual billing) */\n testMode?: boolean\n}\n\nexport interface CreditRequirement {\n /** Number of credits to deduct per call */\n credits: number\n}\n\nexport interface SubscriptionRequirement {\n /** Required subscription tier (e.g., 'pro', 'enterprise') */\n tier: string\n /** Alternative tiers that also grant access */\n allowedTiers?: string[]\n}\n\nexport interface StripeRequirement {\n /** Stripe Price ID for per-call billing */\n priceId: string\n}\n\nexport type PaymentRequirement = \n | CreditRequirement \n | SubscriptionRequirement \n | StripeRequirement\n\nexport interface UserContext {\n /** User ID for billing lookup */\n userId: string\n /** Optional API key override */\n apiKey?: string\n}\n\nexport interface BillingStatus {\n /** Whether the user can proceed */\n allowed: boolean\n /** Current credit balance (if applicable) */\n credits?: number\n /** Current subscription tier (if applicable) */\n tier?: string\n /** Reason for denial (if not allowed) */\n reason?: string\n /** URL for upgrade/purchase */\n actionUrl?: string\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\nlet paymentConfig: PaymentConfig | null = null\n\n/**\n * Initialize payment/billing configuration\n * \n * @example\n * ```typescript\n * initPayment({\n * apiKey: process.env.OPENCONDUCTOR_API_KEY!,\n * upgradeUrl: 'https://myapp.com/upgrade'\n * })\n * ```\n */\nexport function initPayment(config: PaymentConfig): void {\n paymentConfig = {\n apiUrl: 'https://api.openconductor.ai',\n testMode: false,\n ...config,\n }\n}\n\n/**\n * Get current payment configuration\n */\nexport function getPaymentConfig(): PaymentConfig | null {\n return paymentConfig\n}\n\n// ============================================================================\n// Billing Client\n// ============================================================================\n\ninterface CheckBillingParams {\n userId: string\n requirement: PaymentRequirement\n toolName: string\n}\n\ninterface DeductCreditsParams {\n userId: string\n credits: number\n toolName: string\n callId?: string\n}\n\nasync function checkBilling(params: CheckBillingParams): Promise<BillingStatus> {\n const config = paymentConfig\n if (!config) {\n throw new ConfigurationError('payment', 'Payment not initialized. Call initPayment() first.')\n }\n\n // Test mode - always allow\n if (config.testMode) {\n return { allowed: true, credits: 9999, tier: 'test' }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-check`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n requirement: params.requirement,\n tool: params.toolName,\n }),\n })\n\n if (!response.ok) {\n // Non-2xx response - treat as billing check failed\n const errorBody = await response.text()\n console.error('[payment] Billing check failed:', response.status, errorBody)\n return {\n allowed: false,\n reason: 'Billing service unavailable',\n actionUrl: config.upgradeUrl,\n }\n }\n\n return await response.json() as BillingStatus\n } catch (error) {\n // Network error - fail open or closed based on config\n console.error('[payment] Billing check error:', error)\n return {\n allowed: false,\n reason: 'Unable to verify billing status',\n actionUrl: config.upgradeUrl,\n }\n }\n}\n\nasync function deductCredits(params: DeductCreditsParams): Promise<boolean> {\n const config = paymentConfig\n if (!config) return false\n\n // Test mode - don't actually deduct\n if (config.testMode) return true\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-deduct`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n credits: params.credits,\n tool: params.toolName,\n callId: params.callId,\n }),\n })\n\n return response.ok\n } catch (error) {\n console.error('[payment] Credit deduction error:', error)\n return false\n }\n}\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\nfunction isCreditRequirement(req: PaymentRequirement): req is CreditRequirement {\n return 'credits' in req && typeof req.credits === 'number'\n}\n\nfunction isSubscriptionRequirement(req: PaymentRequirement): req is SubscriptionRequirement {\n return 'tier' in req && typeof req.tier === 'string'\n}\n\nfunction isStripeRequirement(req: PaymentRequirement): req is StripeRequirement {\n return 'priceId' in req && typeof req.priceId === 'string'\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\nexport interface RequirePaymentOptions {\n /** Tool name for billing records */\n toolName?: string\n /** Function to extract user ID from input */\n getUserId?: (input: unknown) => string | undefined\n /** Custom error handler */\n onPaymentError?: (error: Error) => void\n}\n\n/**\n * One-line payment middleware for MCP tools\n * \n * @example Credits-based billing\n * ```typescript\n * const paidTool = requirePayment({ credits: 10 })(myHandler)\n * ```\n * \n * @example Subscription tier requirement\n * ```typescript\n * const premiumTool = requirePayment({ tier: 'pro' })(myHandler)\n * ```\n * \n * @example With wrapTool\n * ```typescript\n * const safePaidTool = wrapTool(\n * requirePayment({ credits: 5 })(myHandler),\n * { name: 'premium-analysis' }\n * )\n * ```\n */\nexport function requirePayment<TInput extends { userId?: string }, TOutput>(\n requirement: PaymentRequirement,\n options: RequirePaymentOptions = {}\n) {\n return (\n handler: (input: TInput) => TOutput | Promise<TOutput>\n ): ((input: TInput) => Promise<TOutput>) => {\n \n const { toolName = 'unknown', getUserId } = options\n\n return async (input: TInput): Promise<TOutput> => {\n // Extract user ID\n const userId = getUserId?.(input) ?? input.userId\n \n if (!userId) {\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: paymentConfig?.upgradeUrl,\n })\n }\n\n // Check billing status\n const status = await checkBilling({\n userId,\n requirement,\n toolName,\n })\n\n if (!status.allowed) {\n // Throw appropriate error based on requirement type\n if (isCreditRequirement(requirement)) {\n throw new InsufficientCreditsError(\n requirement.credits,\n status.credits ?? 0,\n { purchaseUrl: status.actionUrl }\n )\n }\n\n if (isSubscriptionRequirement(requirement)) {\n throw new SubscriptionRequiredError(\n requirement.tier,\n status.tier,\n { upgradeUrl: status.actionUrl }\n )\n }\n\n // Generic payment required\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: status.actionUrl,\n ...(isStripeRequirement(requirement) && { priceId: requirement.priceId }),\n })\n }\n\n // Execute the handler\n const result = await handler(input)\n\n // Deduct credits after successful execution (if credit-based)\n if (isCreditRequirement(requirement)) {\n await deductCredits({\n userId,\n credits: requirement.credits,\n toolName,\n })\n }\n\n return result\n }\n }\n}\n\n// ============================================================================\n// Convenience Functions\n// ============================================================================\n\n/**\n * Create a paid tool with built-in payment verification\n * \n * @example\n * ```typescript\n * const analyzeData = createPaidTool({\n * name: 'analyze-data',\n * payment: { credits: 10 },\n * handler: async (input) => {\n * // Your tool logic\n * return result\n * }\n * })\n * ```\n */\nexport function createPaidTool<TInput extends { userId?: string }, TOutput>(config: {\n name: string\n payment: PaymentRequirement\n handler: (input: TInput) => TOutput | Promise<TOutput>\n getUserId?: (input: TInput) => string | undefined\n}): (input: TInput) => Promise<TOutput> {\n return requirePayment<TInput, TOutput>(config.payment, {\n toolName: config.name,\n getUserId: config.getUserId as (input: unknown) => string | undefined,\n })(config.handler)\n}\n\n/**\n * Check if a user can access a paid feature without executing it\n * Useful for UI gating\n * \n * @example\n * ```typescript\n * const canAccess = await canUserAccess('user_123', { credits: 10 }, 'premium-tool')\n * if (!canAccess.allowed) {\n * showUpgradePrompt(canAccess.actionUrl)\n * }\n * ```\n */\nexport async function canUserAccess(\n userId: string,\n requirement: PaymentRequirement,\n toolName: string = 'check'\n): Promise<BillingStatus> {\n return checkBilling({ userId, requirement, toolName })\n}\n\n/**\n * Get user's current billing status\n */\nexport async function getUserBillingStatus(userId: string): Promise<{\n credits: number\n tier: string\n active: boolean\n} | null> {\n const config = paymentConfig\n if (!config) return null\n\n if (config.testMode) {\n return { credits: 9999, tier: 'test', active: true }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-status/${userId}`, {\n headers: {\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n })\n\n if (!response.ok) return null\n return await response.json()\n } catch {\n return null\n }\n}\n\n// ============================================================================\n// Checkout Helpers\n// ============================================================================\n\nexport type CreditPack = 'starter' | 'pro' | 'business'\n\nexport interface CreditPackInfo {\n name: string\n credits: number\n price: number\n perCredit: number\n savings: number\n bestFor: string\n popular?: boolean\n}\n\nexport interface CheckoutSession {\n sessionId: string\n url: string\n pack: CreditPack\n credits: number\n price: number\n perCredit: number\n}\n\n/**\n * Get available credit packs and pricing\n * \n * @example\n * ```typescript\n * const packs = await getCreditPacks()\n * console.log(packs.pro) // { name: 'Pro Pack', credits: 500, price: 39.99, ... }\n * ```\n */\nexport async function getCreditPacks(): Promise<Record<CreditPack, CreditPackInfo> | null> {\n const config = paymentConfig\n if (!config) return null\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/credit-packs`)\n if (!response.ok) return null\n const data = await response.json()\n return data.packs\n } catch {\n return null\n }\n}\n\n/**\n * Create a Stripe checkout session for purchasing credits\n * Returns a URL to redirect the user to for payment\n * \n * @example\n * ```typescript\n * const checkout = await createCreditsCheckout('pro', {\n * successUrl: 'https://myapp.com/success',\n * cancelUrl: 'https://myapp.com/pricing',\n * })\n * \n * // Redirect user to checkout\n * window.location.href = checkout.url\n * ```\n */\nexport async function createCreditsCheckout(\n pack: CreditPack,\n options: {\n successUrl?: string\n cancelUrl?: string\n } = {}\n): Promise<CheckoutSession | null> {\n const config = paymentConfig\n if (!config) {\n console.error('[payment] Payment not initialized. Call initPayment() first.')\n return null\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/stripe-checkout-credits`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n pack,\n successUrl: options.successUrl,\n cancelUrl: options.cancelUrl,\n }),\n })\n\n if (!response.ok) {\n const error = await response.text()\n console.error('[payment] Checkout creation failed:', error)\n return null\n }\n\n return await response.json() as CheckoutSession\n } catch (error) {\n console.error('[payment] Checkout error:', error)\n return null\n }\n}\n\n/**\n * Generate a direct checkout URL for embedding in upgrade prompts\n * \n * @example In an error handler:\n * ```typescript\n * catch (error) {\n * if (error instanceof InsufficientCreditsError) {\n * const checkoutUrl = await getCheckoutUrl('pro')\n * return { error: `Insufficient credits. Buy more: ${checkoutUrl}` }\n * }\n * }\n * ```\n */\nexport async function getCheckoutUrl(pack: CreditPack): Promise<string | null> {\n const session = await createCreditsCheckout(pack)\n return session?.url ?? null\n}\n"]}
1
+ {"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts","../../src/payment/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA,EAkBxB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,oBAAA,EAAsB,MAAA;AAAA,EACtB,qBAAA,EAAuB;AACzB,CAAA;;;AClBO,IAAM,QAAA,GAAN,cAAuB,KAAA,CAAM;AAAA,EAClB,IAAA;AAAA,EACA,IAAA;AAAA,EAEhB,WAAA,CACE,IAAA,EACA,OAAA,EACA,IAAA,EACA;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAGZ,IAAA,IAAI,MAAM,iBAAA,EAAmB;AAC3B,MAAA,KAAA,CAAM,iBAAA,CAAkB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAAA,IAChD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAA,GAAS;AACP,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,SAAS,IAAA,CAAK,OAAA;AAAA,MACd,GAAI,IAAA,CAAK,IAAA,IAAQ,EAAE,IAAA,EAAM,KAAK,IAAA;AAAK,KACrC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,UAAA,CAAW,KAA6B,IAAA,EAAM;AAC5C,IAAA,OA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I,CAAA;AACrD,EAAA,IAAI,CAAC,WAAW,OAAO,IAAA;AAEvB,EAAA,MAAM,EAAE,OAAA,EAAS,OAAA,EAAQ,GAAI,SAAA;AAC7B,EAAA,MAAM,EAAE,QAAA,EAAU,aAAA,EAAc,GAAI,OAAA;AAEpC,EAAA,MAAM,KAAA,GAAQ,aAAA,KAAkB,IAAA,IAAQ,aAAA,IAAiB,aAAA;AAGzD,EAAA,IAAI,eAAA,GAA8B,SAAA;AAClC,EAAA,MAAM,aAAa,QAAA,GAAW,CAAA;AAC9B,EAAA,IAAI,UAAA,GAAa,KAAK,eAAA,GAAkB,KAAA;AACxC,EAAA,IAAI,UAAA,GAAa,KAAK,eAAA,GAAkB,UAAA;AAExC,EAAA,IAAI,OAAA,GAAU,EAAA;AACd,EAAA,IAAI,KAAA,EAAO;AACT,IAAA,IAAI,kBAAkB,CAAA,EAAG;AACvB,MAAA,OAAA,GAAU,2CAAiC,OAAO,CAAA,uBAAA,CAAA;AAAA,IACpD,CAAA,MAAA,IAAW,iBAAiB,CAAA,EAAG;AAC7B,MAAA,OAAA,GAAU,8BAAoB,aAAa,CAAA,sCAAA,CAAA;AAAA,IAC7C,CAAA,MAAO;AACL,MAAA,OAAA,GAAU,cAAO,aAAa,CAAA,iDAAA,EAAoD,QAAA,CAAS,OAAA,CAAQ,CAAC,CAAC,CAAA,MAAA,CAAA;AAAA,IACvG;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,KAAA;AAAA,IACA,aAAA;AAAA,IACA,OAAA;AAAA,IACA,QAAA;AAAA,IACA,OAAA;AAAA,IACA;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * JSON-RPC 2.0 Standard Error Codes\n * https://www.jsonrpc.org/specification#error_object\n */\nexport const ErrorCodes = {\n // JSON-RPC 2.0 Standard Errors\n PARSE_ERROR: -32700,\n INVALID_REQUEST: -32600,\n METHOD_NOT_FOUND: -32601,\n INVALID_PARAMS: -32602,\n INTERNAL_ERROR: -32603,\n\n // MCP-Specific Errors (-32000 to -32099 reserved for implementation)\n TOOL_NOT_FOUND: -32001,\n TOOL_EXECUTION_ERROR: -32002,\n RESOURCE_NOT_FOUND: -32003,\n AUTHENTICATION_ERROR: -32004,\n AUTHORIZATION_ERROR: -32005,\n RATE_LIMIT_ERROR: -32006,\n TIMEOUT_ERROR: -32007,\n VALIDATION_ERROR: -32008,\n DEPENDENCY_ERROR: -32009,\n CONFIGURATION_ERROR: -32010,\n PAYMENT_REQUIRED: -32011,\n INSUFFICIENT_CREDITS: -32012,\n SUBSCRIPTION_REQUIRED: -32013,\n} as const\n\nexport type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]\n","import { ErrorCodes, type ErrorCode } from './codes'\n\nexport { ErrorCodes, type ErrorCode } from './codes'\n\n/**\n * Base error class for MCP servers\n * Formats errors according to JSON-RPC 2.0 specification\n */\nexport class MCPError extends Error {\n public readonly code: ErrorCode\n public readonly data?: Record<string, unknown>\n\n constructor(\n code: ErrorCode,\n message: string,\n data?: Record<string, unknown>\n ) {\n super(message)\n this.name = 'MCPError'\n this.code = code\n this.data = data\n\n // Maintains proper stack trace in V8 environments\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor)\n }\n }\n\n /**\n * Returns JSON-RPC 2.0 formatted error object\n */\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n ...(this.data && { data: this.data }),\n }\n }\n\n /**\n * Create error response for JSON-RPC\n */\n toResponse(id: string | number | null = null) {\n return {\n jsonrpc: '2.0' as const,\n id,\n error: this.toJSON(),\n }\n }\n}\n\n/**\n * Thrown when tool input validation fails\n */\nexport class ValidationError extends MCPError {\n constructor(field: string, reason: string, value?: unknown) {\n super(ErrorCodes.INVALID_PARAMS, `Validation failed for '${field}': ${reason}`, {\n field,\n reason,\n ...(value !== undefined && { value }),\n })\n this.name = 'ValidationError'\n }\n}\n\n/**\n * Thrown when a requested tool doesn't exist\n */\nexport class ToolNotFoundError extends MCPError {\n constructor(toolName: string) {\n super(ErrorCodes.TOOL_NOT_FOUND, `Tool '${toolName}' not found`, {\n tool: toolName,\n })\n this.name = 'ToolNotFoundError'\n }\n}\n\n/**\n * Thrown when tool execution fails\n */\nexport class ToolExecutionError extends MCPError {\n constructor(toolName: string, reason: string, cause?: Error) {\n super(ErrorCodes.TOOL_EXECUTION_ERROR, `Tool '${toolName}' failed: ${reason}`, {\n tool: toolName,\n reason,\n ...(cause && { cause: cause.message }),\n })\n this.name = 'ToolExecutionError'\n }\n}\n\n/**\n * Thrown when a requested resource doesn't exist\n */\nexport class ResourceNotFoundError extends MCPError {\n constructor(resourceUri: string) {\n super(ErrorCodes.RESOURCE_NOT_FOUND, `Resource '${resourceUri}' not found`, {\n uri: resourceUri,\n })\n this.name = 'ResourceNotFoundError'\n }\n}\n\n/**\n * Thrown when authentication fails\n */\nexport class AuthenticationError extends MCPError {\n constructor(reason: string = 'Authentication required') {\n super(ErrorCodes.AUTHENTICATION_ERROR, reason)\n this.name = 'AuthenticationError'\n }\n}\n\n/**\n * Thrown when authorization fails (authenticated but not permitted)\n */\nexport class AuthorizationError extends MCPError {\n constructor(action: string, resource?: string) {\n const msg = resource\n ? `Not authorized to ${action} on '${resource}'`\n : `Not authorized to ${action}`\n super(ErrorCodes.AUTHORIZATION_ERROR, msg, {\n action,\n ...(resource && { resource }),\n })\n this.name = 'AuthorizationError'\n }\n}\n\n/**\n * Thrown when rate limits are exceeded\n */\nexport class RateLimitError extends MCPError {\n constructor(retryAfterMs?: number) {\n super(ErrorCodes.RATE_LIMIT_ERROR, 'Rate limit exceeded', {\n ...(retryAfterMs && { retryAfterMs }),\n })\n this.name = 'RateLimitError'\n }\n}\n\n/**\n * Thrown when an operation times out\n */\nexport class TimeoutError extends MCPError {\n constructor(operation: string, timeoutMs: number) {\n super(ErrorCodes.TIMEOUT_ERROR, `Operation '${operation}' timed out after ${timeoutMs}ms`, {\n operation,\n timeoutMs,\n })\n this.name = 'TimeoutError'\n }\n}\n\n/**\n * Thrown when a required dependency is unavailable\n */\nexport class DependencyError extends MCPError {\n constructor(dependency: string, reason: string) {\n super(ErrorCodes.DEPENDENCY_ERROR, `Dependency '${dependency}' unavailable: ${reason}`, {\n dependency,\n reason,\n })\n this.name = 'DependencyError'\n }\n}\n\n/**\n * Thrown when server configuration is invalid\n */\nexport class ConfigurationError extends MCPError {\n constructor(setting: string, reason: string) {\n super(ErrorCodes.CONFIGURATION_ERROR, `Invalid configuration '${setting}': ${reason}`, {\n setting,\n reason,\n })\n this.name = 'ConfigurationError'\n }\n}\n\n/**\n * Thrown when payment is required to access a tool\n */\nexport class PaymentRequiredError extends MCPError {\n constructor(toolName: string, options?: { upgradeUrl?: string; priceId?: string }) {\n super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {\n tool: toolName,\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n ...(options?.priceId && { priceId: options.priceId }),\n })\n this.name = 'PaymentRequiredError'\n }\n}\n\n/**\n * Thrown when user doesn't have enough credits\n */\nexport class InsufficientCreditsError extends MCPError {\n constructor(required: number, available: number, options?: { purchaseUrl?: string }) {\n super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {\n required,\n available,\n ...(options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }),\n })\n this.name = 'InsufficientCreditsError'\n }\n}\n\n/**\n * Thrown when a subscription tier is required\n */\nexport class SubscriptionRequiredError extends MCPError {\n constructor(requiredTier: string, currentTier?: string, options?: { upgradeUrl?: string }) {\n const msg = currentTier \n ? `Subscription '${requiredTier}' required (current: '${currentTier}')`\n : `Subscription '${requiredTier}' required`\n super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {\n requiredTier,\n ...(currentTier && { currentTier }),\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n })\n this.name = 'SubscriptionRequiredError'\n }\n}\n","import {\n PaymentRequiredError,\n InsufficientCreditsError,\n SubscriptionRequiredError,\n ConfigurationError,\n} from '../errors'\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PaymentConfig {\n /** OpenConductor API key for billing */\n apiKey: string\n /** Base URL for billing API (default: https://api.openconductor.ai) */\n apiUrl?: string\n /** Default upgrade URL to show users */\n upgradeUrl?: string\n /** Enable test mode (skips actual billing) */\n testMode?: boolean\n}\n\nexport interface CreditRequirement {\n /** Number of credits to deduct per call */\n credits: number\n}\n\nexport interface SubscriptionRequirement {\n /** Required subscription tier (e.g., 'pro', 'enterprise') */\n tier: string\n /** Alternative tiers that also grant access */\n allowedTiers?: string[]\n}\n\nexport interface StripeRequirement {\n /** Stripe Price ID for per-call billing */\n priceId: string\n}\n\nexport type PaymentRequirement = \n | CreditRequirement \n | SubscriptionRequirement \n | StripeRequirement\n\nexport interface UserContext {\n /** User ID for billing lookup */\n userId: string\n /** Optional API key override */\n apiKey?: string\n}\n\nexport interface BillingStatus {\n /** Whether the user can proceed */\n allowed: boolean\n /** Current credit balance (if applicable) */\n credits?: number\n /** Current subscription tier (if applicable) */\n tier?: string\n /** Reason for denial (if not allowed) */\n reason?: string\n /** URL for upgrade/purchase */\n actionUrl?: string\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\nlet paymentConfig: PaymentConfig | null = null\n\n/**\n * Initialize payment/billing configuration\n * \n * @example\n * ```typescript\n * initPayment({\n * apiKey: process.env.OPENCONDUCTOR_API_KEY!,\n * upgradeUrl: 'https://myapp.com/upgrade'\n * })\n * ```\n */\nexport function initPayment(config: PaymentConfig): void {\n paymentConfig = {\n apiUrl: 'https://api.openconductor.ai',\n testMode: false,\n ...config,\n }\n}\n\n/**\n * Get current payment configuration\n */\nexport function getPaymentConfig(): PaymentConfig | null {\n return paymentConfig\n}\n\n// ============================================================================\n// Billing Client\n// ============================================================================\n\ninterface CheckBillingParams {\n userId: string\n requirement: PaymentRequirement\n toolName: string\n}\n\ninterface DeductCreditsParams {\n userId: string\n credits: number\n toolName: string\n callId?: string\n}\n\nasync function checkBilling(params: CheckBillingParams): Promise<BillingStatus> {\n const config = paymentConfig\n if (!config) {\n throw new ConfigurationError('payment', 'Payment not initialized. Call initPayment() first.')\n }\n\n // Test mode - always allow\n if (config.testMode) {\n return { allowed: true, credits: 9999, tier: 'test' }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-check`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n requirement: params.requirement,\n tool: params.toolName,\n }),\n })\n\n if (!response.ok) {\n // Non-2xx response - treat as billing check failed\n const errorBody = await response.text()\n console.error('[payment] Billing check failed:', response.status, errorBody)\n return {\n allowed: false,\n reason: 'Billing service unavailable',\n actionUrl: config.upgradeUrl,\n }\n }\n\n return await response.json() as BillingStatus\n } catch (error) {\n // Network error - fail open or closed based on config\n console.error('[payment] Billing check error:', error)\n return {\n allowed: false,\n reason: 'Unable to verify billing status',\n actionUrl: config.upgradeUrl,\n }\n }\n}\n\nasync function deductCredits(params: DeductCreditsParams): Promise<boolean> {\n const config = paymentConfig\n if (!config) return false\n\n // Test mode - don't actually deduct\n if (config.testMode) return true\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-deduct`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n credits: params.credits,\n tool: params.toolName,\n callId: params.callId,\n }),\n })\n\n return response.ok\n } catch (error) {\n console.error('[payment] Credit deduction error:', error)\n return false\n }\n}\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\nfunction isCreditRequirement(req: PaymentRequirement): req is CreditRequirement {\n return 'credits' in req && typeof req.credits === 'number'\n}\n\nfunction isSubscriptionRequirement(req: PaymentRequirement): req is SubscriptionRequirement {\n return 'tier' in req && typeof req.tier === 'string'\n}\n\nfunction isStripeRequirement(req: PaymentRequirement): req is StripeRequirement {\n return 'priceId' in req && typeof req.priceId === 'string'\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\nexport interface RequirePaymentOptions {\n /** Tool name for billing records */\n toolName?: string\n /** Function to extract user ID from input */\n getUserId?: (input: unknown) => string | undefined\n /** Custom error handler */\n onPaymentError?: (error: Error) => void\n}\n\n/**\n * One-line payment middleware for MCP tools\n * \n * @example Credits-based billing\n * ```typescript\n * const paidTool = requirePayment({ credits: 10 })(myHandler)\n * ```\n * \n * @example Subscription tier requirement\n * ```typescript\n * const premiumTool = requirePayment({ tier: 'pro' })(myHandler)\n * ```\n * \n * @example With wrapTool\n * ```typescript\n * const safePaidTool = wrapTool(\n * requirePayment({ credits: 5 })(myHandler),\n * { name: 'premium-analysis' }\n * )\n * ```\n */\nexport function requirePayment<TInput extends { userId?: string }, TOutput>(\n requirement: PaymentRequirement,\n options: RequirePaymentOptions = {}\n) {\n return (\n handler: (input: TInput) => TOutput | Promise<TOutput>\n ): ((input: TInput) => Promise<TOutput>) => {\n \n const { toolName = 'unknown', getUserId } = options\n\n return async (input: TInput): Promise<TOutput> => {\n // Extract user ID\n const userId = getUserId?.(input) ?? input.userId\n \n if (!userId) {\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: paymentConfig?.upgradeUrl,\n })\n }\n\n // Check billing status\n const status = await checkBilling({\n userId,\n requirement,\n toolName,\n })\n\n if (!status.allowed) {\n // Throw appropriate error based on requirement type\n if (isCreditRequirement(requirement)) {\n throw new InsufficientCreditsError(\n requirement.credits,\n status.credits ?? 0,\n { purchaseUrl: status.actionUrl }\n )\n }\n\n if (isSubscriptionRequirement(requirement)) {\n throw new SubscriptionRequiredError(\n requirement.tier,\n status.tier,\n { upgradeUrl: status.actionUrl }\n )\n }\n\n // Generic payment required\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: status.actionUrl,\n ...(isStripeRequirement(requirement) && { priceId: requirement.priceId }),\n })\n }\n\n // Execute the handler\n const result = await handler(input)\n\n // Deduct credits after successful execution (if credit-based)\n if (isCreditRequirement(requirement)) {\n await deductCredits({\n userId,\n credits: requirement.credits,\n toolName,\n })\n }\n\n return result\n }\n }\n}\n\n// ============================================================================\n// Convenience Functions\n// ============================================================================\n\n/**\n * Create a paid tool with built-in payment verification\n * \n * @example\n * ```typescript\n * const analyzeData = createPaidTool({\n * name: 'analyze-data',\n * payment: { credits: 10 },\n * handler: async (input) => {\n * // Your tool logic\n * return result\n * }\n * })\n * ```\n */\nexport function createPaidTool<TInput extends { userId?: string }, TOutput>(config: {\n name: string\n payment: PaymentRequirement\n handler: (input: TInput) => TOutput | Promise<TOutput>\n getUserId?: (input: TInput) => string | undefined\n}): (input: TInput) => Promise<TOutput> {\n return requirePayment<TInput, TOutput>(config.payment, {\n toolName: config.name,\n getUserId: config.getUserId as (input: unknown) => string | undefined,\n })(config.handler)\n}\n\n/**\n * Check if a user can access a paid feature without executing it\n * Useful for UI gating\n * \n * @example\n * ```typescript\n * const canAccess = await canUserAccess('user_123', { credits: 10 }, 'premium-tool')\n * if (!canAccess.allowed) {\n * showUpgradePrompt(canAccess.actionUrl)\n * }\n * ```\n */\nexport async function canUserAccess(\n userId: string,\n requirement: PaymentRequirement,\n toolName: string = 'check'\n): Promise<BillingStatus> {\n return checkBilling({ userId, requirement, toolName })\n}\n\n/**\n * Get user's current billing status\n */\nexport async function getUserBillingStatus(userId: string): Promise<{\n credits: number\n tier: string\n active: boolean\n} | null> {\n const config = paymentConfig\n if (!config) return null\n\n if (config.testMode) {\n return { credits: 9999, tier: 'test', active: true }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-status/${userId}`, {\n headers: {\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n })\n\n if (!response.ok) return null\n return await response.json()\n } catch {\n return null\n }\n}\n\n// ============================================================================\n// Checkout Helpers\n// ============================================================================\n\nexport type CreditPack = 'starter' | 'pro' | 'business'\n\nexport interface CreditPackInfo {\n name: string\n credits: number\n price: number\n perCredit: number\n savings: number\n bestFor: string\n popular?: boolean\n}\n\nexport interface CheckoutSession {\n sessionId: string\n url: string\n pack: CreditPack\n credits: number\n price: number\n perCredit: number\n}\n\n/**\n * Get available credit packs and pricing\n * \n * @example\n * ```typescript\n * const packs = await getCreditPacks()\n * console.log(packs.pro) // { name: 'Pro Pack', credits: 500, price: 39.99, ... }\n * ```\n */\nexport async function getCreditPacks(): Promise<Record<CreditPack, CreditPackInfo> | null> {\n const config = paymentConfig\n if (!config) return null\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/credit-packs`)\n if (!response.ok) return null\n const data = await response.json()\n return data.packs\n } catch {\n return null\n }\n}\n\n/**\n * Create a Stripe checkout session for purchasing credits\n * Returns a URL to redirect the user to for payment\n * \n * @example\n * ```typescript\n * const checkout = await createCreditsCheckout('pro', {\n * successUrl: 'https://myapp.com/success',\n * cancelUrl: 'https://myapp.com/pricing',\n * })\n * \n * // Redirect user to checkout\n * window.location.href = checkout.url\n * ```\n */\nexport async function createCreditsCheckout(\n pack: CreditPack,\n options: {\n successUrl?: string\n cancelUrl?: string\n } = {}\n): Promise<CheckoutSession | null> {\n const config = paymentConfig\n if (!config) {\n console.error('[payment] Payment not initialized. Call initPayment() first.')\n return null\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/stripe-checkout-credits`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n pack,\n successUrl: options.successUrl,\n cancelUrl: options.cancelUrl,\n }),\n })\n\n if (!response.ok) {\n const error = await response.text()\n console.error('[payment] Checkout creation failed:', error)\n return null\n }\n\n return await response.json() as CheckoutSession\n } catch (error) {\n console.error('[payment] Checkout error:', error)\n return null\n }\n}\n\n/**\n * Generate a direct checkout URL for embedding in upgrade prompts\n * \n * @example In an error handler:\n * ```typescript\n * catch (error) {\n * if (error instanceof InsufficientCreditsError) {\n * const checkoutUrl = await getCheckoutUrl('pro')\n * return { error: `Insufficient credits. Buy more: ${checkoutUrl}` }\n * }\n * }\n * ```\n */\nexport async function getCheckoutUrl(pack: CreditPack): Promise<string | null> {\n const session = await createCreditsCheckout(pack)\n return session?.url ?? null\n}\n\n// ============================================================================\n// Upgrade Prompts\n// ============================================================================\n\nexport interface UpgradePromptOptions {\n /** Required credits for the blocked action */\n required?: number\n /** Currently available credits */\n available?: number\n /** Tool that was blocked */\n toolName?: string\n /** Include direct checkout links */\n includeLinks?: boolean\n}\n\n/**\n * Generate a user-friendly upgrade prompt message\n * Use this in your error handlers to show helpful messages\n * \n * @example\n * ```typescript\n * catch (error) {\n * if (error instanceof InsufficientCreditsError) {\n * const prompt = await generateUpgradePrompt({\n * required: error.data.required,\n * available: error.data.available,\n * toolName: 'analyze-data'\n * })\n * console.log(prompt.message)\n * // \"You need 10 more credits to use analyze-data. \n * // Buy the Pro Pack (500 credits) for $39.99 → https://...\"\n * }\n * }\n * ```\n */\nexport async function generateUpgradePrompt(\n options: UpgradePromptOptions = {}\n): Promise<{\n message: string\n shortMessage: string\n recommendedPack: CreditPack\n packs: Record<CreditPack, { credits: number; price: number; url: string | null }>\n}> {\n const { required = 0, available = 0, toolName, includeLinks = true } = options\n const needed = Math.max(0, required - available)\n \n // Determine recommended pack based on needed credits\n let recommendedPack: CreditPack = 'starter'\n if (needed > 100) recommendedPack = 'pro'\n if (needed > 500) recommendedPack = 'business'\n \n // Get pack URLs if requested\n const packUrls: Record<CreditPack, string | null> = {\n starter: null,\n pro: null,\n business: null,\n }\n \n if (includeLinks) {\n const [starterUrl, proUrl, businessUrl] = await Promise.all([\n getCheckoutUrl('starter'),\n getCheckoutUrl('pro'),\n getCheckoutUrl('business'),\n ])\n packUrls.starter = starterUrl\n packUrls.pro = proUrl\n packUrls.business = businessUrl\n }\n \n // Build message\n const toolPart = toolName ? ` to use ${toolName}` : ''\n const shortMessage = `Insufficient credits: need ${required}, have ${available}`\n \n let message = `You need ${needed} more credits${toolPart}.\\n\\n`\n message += `💳 Quick top-up options:\\n`\n message += ` • Starter: 100 credits for $9.99${packUrls.starter ? ` → ${packUrls.starter}` : ''}\\n`\n message += ` • Pro: 500 credits for $39.99 (20% off)${packUrls.pro ? ` → ${packUrls.pro}` : ''}\\n`\n message += ` • Business: 2,000 credits for $119.99 (40% off)${packUrls.business ? ` → ${packUrls.business}` : ''}\\n`\n \n if (recommendedPack !== 'starter') {\n message += `\\n✨ Recommended: ${recommendedPack.charAt(0).toUpperCase() + recommendedPack.slice(1)} Pack`\n }\n \n return {\n message,\n shortMessage,\n recommendedPack,\n packs: {\n starter: { credits: 100, price: 9.99, url: packUrls.starter },\n pro: { credits: 500, price: 39.99, url: packUrls.pro },\n business: { credits: 2000, price: 119.99, url: packUrls.business },\n },\n }\n}\n\n/**\n * Get a simple checkout URL for the dashboard\n * Useful when you don't want to create a full Stripe session\n */\nexport function getDashboardCheckoutUrl(options: {\n required?: number\n available?: number\n pack?: CreditPack\n} = {}): string {\n const params = new URLSearchParams()\n params.set('action', 'buy-credits')\n if (options.required) params.set('required', String(options.required))\n if (options.available) params.set('available', String(options.available))\n if (options.pack) params.set('pack', options.pack)\n return `https://dashboard.openconductor.ai?${params.toString()}`\n}\n\n/**\n * Create an error handler that automatically generates upgrade prompts\n * \n * @example\n * ```typescript\n * const handleError = createUpgradeErrorHandler({\n * onInsufficientCredits: (prompt) => {\n * return { error: prompt.shortMessage, upgradeUrl: prompt.packs.pro.url }\n * }\n * })\n * \n * // In your tool:\n * try {\n * return await paidHandler(input)\n * } catch (error) {\n * return handleError(error)\n * }\n * ```\n */\nexport function createUpgradeErrorHandler(handlers: {\n onInsufficientCredits?: (prompt: Awaited<ReturnType<typeof generateUpgradePrompt>>) => unknown\n onSubscriptionRequired?: (error: SubscriptionRequiredError) => unknown\n onPaymentRequired?: (error: PaymentRequiredError) => unknown\n onOtherError?: (error: Error) => unknown\n}) {\n return async (error: Error): Promise<unknown> => {\n if (error instanceof InsufficientCreditsError && handlers.onInsufficientCredits) {\n const prompt = await generateUpgradePrompt({\n required: error.data?.required as number,\n available: error.data?.available as number,\n })\n return handlers.onInsufficientCredits(prompt)\n }\n \n if (error instanceof SubscriptionRequiredError && handlers.onSubscriptionRequired) {\n return handlers.onSubscriptionRequired(error)\n }\n \n if (error instanceof PaymentRequiredError && handlers.onPaymentRequired) {\n return handlers.onPaymentRequired(error)\n }\n \n if (handlers.onOtherError) {\n return handlers.onOtherError(error)\n }\n \n throw error\n }\n}\n\n// ============================================================================\n// Usage Analytics\n// ============================================================================\n\nexport type AnalyticsPeriod = '24h' | '7d' | '30d' | 'all'\n\nexport interface UsageAnalytics {\n period: AnalyticsPeriod\n balance: number\n summary: {\n totalUsed: number\n totalPurchased: number\n netChange: number\n burnRate: number\n daysRemaining: number | null\n toolCount: number\n transactionCount: number\n }\n topTools: Array<{\n tool: string\n calls: number\n credits: number\n }>\n usageTimeline: Array<{\n date: string\n used: number\n purchased: number\n }>\n recentTransactions: Array<{\n id: string\n amount: number\n type: string\n tool: string | null\n createdAt: string\n }>\n}\n\n/**\n * Get usage analytics for a user\n * \n * @example\n * ```typescript\n * const analytics = await getUserAnalytics('user_123', '30d')\n * console.log(`Burn rate: ${analytics.summary.burnRate} credits/day`)\n * console.log(`Days remaining: ${analytics.summary.daysRemaining}`)\n * ```\n */\nexport async function getUserAnalytics(\n userId: string,\n period: AnalyticsPeriod = '30d'\n): Promise<UsageAnalytics | null> {\n const config = paymentConfig\n if (!config) {\n console.error('[payment] Payment not initialized. Call initPayment() first.')\n return null\n }\n\n if (config.testMode) {\n // Return mock data in test mode\n return {\n period,\n balance: 9999,\n summary: {\n totalUsed: 100,\n totalPurchased: 500,\n netChange: 400,\n burnRate: 3.33,\n daysRemaining: 3000,\n toolCount: 5,\n transactionCount: 30,\n },\n topTools: [\n { tool: 'test-tool', calls: 10, credits: 50 },\n ],\n usageTimeline: [],\n recentTransactions: [],\n }\n }\n\n try {\n const response = await fetch(\n `${config.apiUrl}/functions/v1/usage-analytics/${userId}?period=${period}`,\n {\n headers: {\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n }\n )\n\n if (!response.ok) return null\n return await response.json() as UsageAnalytics\n } catch (error) {\n console.error('[payment] Analytics fetch error:', error)\n return null\n }\n}\n\n/**\n * Check if user's credits are running low based on burn rate\n * Returns recommended action if low\n * \n * @example\n * ```typescript\n * const warning = await checkLowBalance('user_123')\n * if (warning) {\n * sendNotification(warning.message)\n * }\n * ```\n */\nexport async function checkLowBalance(\n userId: string,\n thresholdDays: number = 7\n): Promise<{\n isLow: boolean\n daysRemaining: number | null\n balance: number\n burnRate: number\n message: string\n recommendedPack: CreditPack\n} | null> {\n const analytics = await getUserAnalytics(userId, '7d')\n if (!analytics) return null\n\n const { balance, summary } = analytics\n const { burnRate, daysRemaining } = summary\n \n const isLow = daysRemaining !== null && daysRemaining <= thresholdDays\n \n // Recommend pack based on burn rate\n let recommendedPack: CreditPack = 'starter'\n const weeklyBurn = burnRate * 7\n if (weeklyBurn > 100) recommendedPack = 'pro'\n if (weeklyBurn > 400) recommendedPack = 'business'\n \n let message = ''\n if (isLow) {\n if (daysRemaining === 0) {\n message = `⚠️ Credits depleted! Balance: ${balance}. Buy more to continue.`\n } else if (daysRemaining <= 1) {\n message = `⚠️ Running low! ~${daysRemaining} day of credits left at current usage.`\n } else {\n message = `📊 ~${daysRemaining} days of credits remaining at current burn rate (${burnRate.toFixed(1)}/day).`\n }\n }\n \n return {\n isLow,\n daysRemaining,\n balance,\n burnRate,\n message,\n recommendedPack,\n }\n}\n"]}
@@ -286,7 +286,156 @@ async function getCheckoutUrl(pack) {
286
286
  const session = await createCreditsCheckout(pack);
287
287
  return session?.url ?? null;
288
288
  }
289
+ async function generateUpgradePrompt(options = {}) {
290
+ const { required = 0, available = 0, toolName, includeLinks = true } = options;
291
+ const needed = Math.max(0, required - available);
292
+ let recommendedPack = "starter";
293
+ if (needed > 100) recommendedPack = "pro";
294
+ if (needed > 500) recommendedPack = "business";
295
+ const packUrls = {
296
+ starter: null,
297
+ pro: null,
298
+ business: null
299
+ };
300
+ if (includeLinks) {
301
+ const [starterUrl, proUrl, businessUrl] = await Promise.all([
302
+ getCheckoutUrl("starter"),
303
+ getCheckoutUrl("pro"),
304
+ getCheckoutUrl("business")
305
+ ]);
306
+ packUrls.starter = starterUrl;
307
+ packUrls.pro = proUrl;
308
+ packUrls.business = businessUrl;
309
+ }
310
+ const toolPart = toolName ? ` to use ${toolName}` : "";
311
+ const shortMessage = `Insufficient credits: need ${required}, have ${available}`;
312
+ let message = `You need ${needed} more credits${toolPart}.
313
+
314
+ `;
315
+ message += `\u{1F4B3} Quick top-up options:
316
+ `;
317
+ message += ` \u2022 Starter: 100 credits for $9.99${packUrls.starter ? ` \u2192 ${packUrls.starter}` : ""}
318
+ `;
319
+ message += ` \u2022 Pro: 500 credits for $39.99 (20% off)${packUrls.pro ? ` \u2192 ${packUrls.pro}` : ""}
320
+ `;
321
+ message += ` \u2022 Business: 2,000 credits for $119.99 (40% off)${packUrls.business ? ` \u2192 ${packUrls.business}` : ""}
322
+ `;
323
+ if (recommendedPack !== "starter") {
324
+ message += `
325
+ \u2728 Recommended: ${recommendedPack.charAt(0).toUpperCase() + recommendedPack.slice(1)} Pack`;
326
+ }
327
+ return {
328
+ message,
329
+ shortMessage,
330
+ recommendedPack,
331
+ packs: {
332
+ starter: { credits: 100, price: 9.99, url: packUrls.starter },
333
+ pro: { credits: 500, price: 39.99, url: packUrls.pro },
334
+ business: { credits: 2e3, price: 119.99, url: packUrls.business }
335
+ }
336
+ };
337
+ }
338
+ function getDashboardCheckoutUrl(options = {}) {
339
+ const params = new URLSearchParams();
340
+ params.set("action", "buy-credits");
341
+ if (options.required) params.set("required", String(options.required));
342
+ if (options.available) params.set("available", String(options.available));
343
+ if (options.pack) params.set("pack", options.pack);
344
+ return `https://dashboard.openconductor.ai?${params.toString()}`;
345
+ }
346
+ function createUpgradeErrorHandler(handlers) {
347
+ return async (error) => {
348
+ if (error instanceof InsufficientCreditsError && handlers.onInsufficientCredits) {
349
+ const prompt = await generateUpgradePrompt({
350
+ required: error.data?.required,
351
+ available: error.data?.available
352
+ });
353
+ return handlers.onInsufficientCredits(prompt);
354
+ }
355
+ if (error instanceof SubscriptionRequiredError && handlers.onSubscriptionRequired) {
356
+ return handlers.onSubscriptionRequired(error);
357
+ }
358
+ if (error instanceof PaymentRequiredError && handlers.onPaymentRequired) {
359
+ return handlers.onPaymentRequired(error);
360
+ }
361
+ if (handlers.onOtherError) {
362
+ return handlers.onOtherError(error);
363
+ }
364
+ throw error;
365
+ };
366
+ }
367
+ async function getUserAnalytics(userId, period = "30d") {
368
+ const config = paymentConfig;
369
+ if (!config) {
370
+ console.error("[payment] Payment not initialized. Call initPayment() first.");
371
+ return null;
372
+ }
373
+ if (config.testMode) {
374
+ return {
375
+ period,
376
+ balance: 9999,
377
+ summary: {
378
+ totalUsed: 100,
379
+ totalPurchased: 500,
380
+ netChange: 400,
381
+ burnRate: 3.33,
382
+ daysRemaining: 3e3,
383
+ toolCount: 5,
384
+ transactionCount: 30
385
+ },
386
+ topTools: [
387
+ { tool: "test-tool", calls: 10, credits: 50 }
388
+ ],
389
+ usageTimeline: [],
390
+ recentTransactions: []
391
+ };
392
+ }
393
+ try {
394
+ const response = await fetch(
395
+ `${config.apiUrl}/functions/v1/usage-analytics/${userId}?period=${period}`,
396
+ {
397
+ headers: {
398
+ "Authorization": `Bearer ${config.apiKey}`
399
+ }
400
+ }
401
+ );
402
+ if (!response.ok) return null;
403
+ return await response.json();
404
+ } catch (error) {
405
+ console.error("[payment] Analytics fetch error:", error);
406
+ return null;
407
+ }
408
+ }
409
+ async function checkLowBalance(userId, thresholdDays = 7) {
410
+ const analytics = await getUserAnalytics(userId, "7d");
411
+ if (!analytics) return null;
412
+ const { balance, summary } = analytics;
413
+ const { burnRate, daysRemaining } = summary;
414
+ const isLow = daysRemaining !== null && daysRemaining <= thresholdDays;
415
+ let recommendedPack = "starter";
416
+ const weeklyBurn = burnRate * 7;
417
+ if (weeklyBurn > 100) recommendedPack = "pro";
418
+ if (weeklyBurn > 400) recommendedPack = "business";
419
+ let message = "";
420
+ if (isLow) {
421
+ if (daysRemaining === 0) {
422
+ message = `\u26A0\uFE0F Credits depleted! Balance: ${balance}. Buy more to continue.`;
423
+ } else if (daysRemaining <= 1) {
424
+ message = `\u26A0\uFE0F Running low! ~${daysRemaining} day of credits left at current usage.`;
425
+ } else {
426
+ message = `\u{1F4CA} ~${daysRemaining} days of credits remaining at current burn rate (${burnRate.toFixed(1)}/day).`;
427
+ }
428
+ }
429
+ return {
430
+ isLow,
431
+ daysRemaining,
432
+ balance,
433
+ burnRate,
434
+ message,
435
+ recommendedPack
436
+ };
437
+ }
289
438
 
290
- export { canUserAccess, createCreditsCheckout, createPaidTool, getCheckoutUrl, getCreditPacks, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment };
439
+ export { canUserAccess, checkLowBalance, createCreditsCheckout, createPaidTool, createUpgradeErrorHandler, generateUpgradePrompt, getCheckoutUrl, getCreditPacks, getDashboardCheckoutUrl, getPaymentConfig, getUserAnalytics, getUserBillingStatus, initPayment, requirePayment };
291
440
  //# sourceMappingURL=index.mjs.map
292
441
  //# sourceMappingURL=index.mjs.map