plugin-connections
Version:
Connection management plugin for Twitter authentication and other services
180 lines (174 loc) • 5.66 kB
TypeScript
import { Plugin, UUID, Service, IAgentRuntime } from '@elizaos/core';
declare const plugin: Plugin;
/**
* Supported external services
*/
type ServiceName = "twitter" | "discord" | "telegram" | "github" | "google" | "facebook" | "linkedin" | "instagram" | "tiktok" | "youtube" | "other";
/**
* Service credential storage structure
*/
interface ServiceCredential {
id: UUID;
agentId: UUID;
serviceName: ServiceName;
credentials: Record<string, any>;
isActive: boolean;
expiresAt?: Date;
createdAt: Date;
updatedAt: Date;
}
/**
* OAuth session tracking
*/
interface OAuthSession {
id: UUID;
agentId: UUID;
serviceName: ServiceName;
state: string;
codeVerifier?: string;
returnUrl?: string;
expiresAt: Date;
createdAt: Date;
}
/**
* Twitter-specific credential structure
*/
interface TwitterCredentials {
apiKey: string;
apiSecretKey: string;
accessToken: string;
accessTokenSecret: string;
userId?: string;
username?: string;
}
/**
* OAuth flow initiation request
*/
interface OAuthInitiateRequest {
agentId: UUID;
serviceName: ServiceName;
returnUrl?: string;
}
/**
* OAuth flow completion request
*/
interface OAuthCompleteRequest {
agentId: UUID;
serviceName: ServiceName;
code: string;
state: string;
oauthToken?: string;
oauthVerifier?: string;
}
/**
* Connection status response
*/
interface ConnectionStatus {
serviceName: ServiceName;
isConnected: boolean;
isPending: boolean;
username?: string;
userId?: string;
expiresAt?: Date;
lastChecked: Date;
}
/**
* Auth service interface
*/
interface IAuthService {
getCredentials(agentId: UUID, service: ServiceName): Promise<Record<string, any> | null>;
revokeCredentials(agentId: UUID, service: ServiceName): Promise<void>;
storeCredentials(agentId: UUID, service: ServiceName, credentials: Record<string, any>): Promise<void>;
getConnectionStatus(agentId: UUID, service: ServiceName): Promise<ConnectionStatus>;
}
/**
* Encryption service interface
*/
interface IEncryptionService {
encrypt(data: string): string;
decrypt(encryptedData: string): string;
isEnabled(): boolean;
}
/**
* Authentication service for managing agent credentials to external services
*/
declare class AuthService extends Service implements IAuthService {
static serviceType: string;
private oauthCache;
private get databaseService();
constructor(runtime: IAgentRuntime);
get capabilityDescription(): string;
/**
* Generate a secure random state parameter for OAuth
*/
generateOAuthState(): string;
static start(runtime: IAgentRuntime): Promise<AuthService>;
static stop(runtime: IAgentRuntime): Promise<void>;
stop(): Promise<void>;
storeCredentials(agentId: UUID, service: ServiceName, credentials: Record<string, any>): Promise<void>;
getCredentials(agentId: UUID, service: ServiceName): Promise<Record<string, any> | null>;
revokeCredentials(agentId: UUID, service: ServiceName): Promise<void>;
getConnectionStatus(agentId: UUID, service: ServiceName): Promise<ConnectionStatus>;
storeTempCredentials(key: string, credentials: Record<string, any>): void;
getTempCredentials(key: string): Record<string, any> | null;
deleteTempCredentials(key: string): void;
createOAuthSession(agentId: UUID, service: ServiceName, state: string, returnUrl?: string): Promise<void>;
}
/**
* Manages database tables and operations for the connections plugin.
* Note: This service is currently not actively used as credentials are stored in agent settings.
*/
declare class DatabaseService extends Service {
protected runtime: IAgentRuntime;
static serviceType: string;
private tablesCreated;
constructor(runtime: IAgentRuntime);
get capabilityDescription(): string;
/**
* Direct access to the database via the runtime.
*/
private get db();
/**
* Creates and starts the database service.
*/
static start(runtime: IAgentRuntime): Promise<DatabaseService>;
/**
* Instance-specific stop method.
* This service does not manage any persistent connections or intervals,
* so no specific cleanup is required here.
*/
stop(): Promise<void>;
/**
* Initializes the database service and creates tables if they don't exist.
*/
initialize(): Promise<void>;
/**
* Creates all necessary tables for the connections plugin.
*/
private createTables;
/**
* Validate database connection.
*/
validateConnection(): Promise<void>;
/**
* Store encrypted credentials for a service.
*/
storeCredentials(agentId: UUID, service: ServiceName, credentials: Record<string, any>): Promise<void>;
/**
* Get encrypted credentials for a service.
*/
getCredentials(agentId: UUID, service: ServiceName): Promise<Record<string, any> | null>;
/**
* Delete credentials for a service.
*/
deleteCredentials(agentId: UUID, service: ServiceName): Promise<void>;
/**
* Get connection status for all services for an agent.
*/
getConnectionStatus(agentId: UUID): Promise<ConnectionStatus[]>;
/**
* Check if service has credentials.
*/
hasCredentials(agentId: UUID, service: ServiceName): Promise<boolean>;
}
export { AuthService, type ConnectionStatus, DatabaseService, type IAuthService, type IEncryptionService, type OAuthCompleteRequest, type OAuthInitiateRequest, type OAuthSession, type ServiceCredential, type ServiceName, type TwitterCredentials, plugin as default };