UNPKG

contextual-agent-sdk

Version:

SDK for building AI agents with seamless voice-text context switching

155 lines 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolProviderUtils = exports.BaseToolProvider = void 0; class BaseToolProvider { _isAuthenticated = false; _lastHealthCheck; _healthStatus = 'unhealthy'; async testConnection(credentials) { const startTime = Date.now(); try { if (credentials) { const authResult = await this.authenticate(credentials); if (!authResult) { return { success: false, responseTime: Date.now() - startTime, error: 'Authentication failed' }; } } return { success: true, responseTime: Date.now() - startTime, metadata: { providerId: this.id, testType: 'basic_connectivity' } }; } catch (error) { return { success: false, responseTime: Date.now() - startTime, error: error instanceof Error ? error.message : 'Unknown error' }; } } getHealthStatus() { return { status: this._healthStatus, lastCheck: this._lastHealthCheck || new Date(), details: { apiConnectivity: this._isAuthenticated, authentication: this._isAuthenticated, rateLimitStatus: 'ok', errorRate: 0, averageResponseTime: 0 } }; } setAuthenticationStatus(isAuthenticated) { this._isAuthenticated = isAuthenticated; this._lastHealthCheck = new Date(); this._healthStatus = isAuthenticated ? 'healthy' : 'unhealthy'; } setHealthStatus(status) { this._healthStatus = status; this._lastHealthCheck = new Date(); } async cleanup() { this._isAuthenticated = false; this._healthStatus = 'unhealthy'; console.log(`[${this.id}] Provider cleanup completed`); } isToolAvailableForSubscription(tool, subscriptionContext) { if (!subscriptionContext) { return true; } const tierOrder = { 'BASIC': 1, 'PRO': 2, 'ENTERPRISE': 3 }; if (tierOrder[subscriptionContext.tier] < tierOrder[tool.minimumTier]) { return false; } const allowedCategories = subscriptionContext.limits.toolCategoriesAllowed; if (!allowedCategories.includes('*') && !allowedCategories.includes(tool.category)) { return false; } return true; } validateExecutionContext(context) { if (!context) { return false; } return !!(context.agentId && context.organizationId); } createErrorResult(error, errorCode) { return { success: false, error, errorCode, metadata: { executionTime: 0, providerVersion: this.version } }; } createSuccessResult(data, metadata) { return { success: true, data, metadata: { executionTime: 0, providerVersion: this.version, ...metadata } }; } } exports.BaseToolProvider = BaseToolProvider; class ToolProviderUtils { static validateParameters(tool, params) { const errors = []; for (const param of tool.parameters) { if (param.required && !(param.name in params)) { errors.push(`Required parameter '${param.name}' is missing`); continue; } if (param.name in params) { const value = params[param.name]; const expectedType = param.type; const actualType = Array.isArray(value) ? 'array' : typeof value; if (expectedType !== actualType) { errors.push(`Parameter '${param.name}' expected ${expectedType} but got ${actualType}`); } } } return { isValid: errors.length === 0, errors }; } static applyParameterDefaults(tool, params) { const result = { ...params }; for (const param of tool.parameters) { if (!(param.name in result) && param.default !== undefined) { result[param.name] = param.default; } } return result; } static checkTierRequirement(toolTier, userTier) { const tierOrder = { 'BASIC': 1, 'PRO': 2, 'ENTERPRISE': 3 }; return tierOrder[userTier] >= tierOrder[toolTier]; } } exports.ToolProviderUtils = ToolProviderUtils; //# sourceMappingURL=ToolProvider.js.map