Skip to content

Commit

Permalink
Merge pull request elizaOS#2400 from elizaOS/fixes
Browse files Browse the repository at this point in the history
fix: lint errors
  • Loading branch information
shakkernerd authored Jan 16, 2025
2 parents 1eacc8d + 0c55316 commit 5ae8feb
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 402 deletions.
3 changes: 3 additions & 0 deletions packages/client-instagram/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
90 changes: 46 additions & 44 deletions packages/client-instagram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,52 @@ import { InstagramInteractionService } from "./services/interaction";
import { InstagramPostService } from "./services/post";

export const InstagramClientInterface: Client = {
async start(runtime: IAgentRuntime) {
try {
// Validate configuration
const config = await validateInstagramConfig(runtime);
elizaLogger.log("Instagram client configuration validated");

// Initialize client and get initial state
const state = await initializeClient(runtime, config);
elizaLogger.log("Instagram client initialized");

// Create services
const postService = new InstagramPostService(runtime, state);
const interactionService = new InstagramInteractionService(runtime, state);

// Start services
if (!config.INSTAGRAM_DRY_RUN) {
await postService.start();
elizaLogger.log("Instagram post service started");

if (config.INSTAGRAM_ENABLE_ACTION_PROCESSING) {
await interactionService.start();
elizaLogger.log("Instagram interaction service started");
async start(runtime: IAgentRuntime) {
try {
// Validate configuration
const config = await validateInstagramConfig(runtime);
elizaLogger.log("Instagram client configuration validated");

// Initialize client and get initial state
const state = await initializeClient(runtime, config);
elizaLogger.log("Instagram client initialized");

// Create services
const postService = new InstagramPostService(runtime, state);
const interactionService = new InstagramInteractionService(
runtime,
state
);

// Start services
if (!config.INSTAGRAM_DRY_RUN) {
await postService.start();
elizaLogger.log("Instagram post service started");

if (config.INSTAGRAM_ENABLE_ACTION_PROCESSING) {
await interactionService.start();
elizaLogger.log("Instagram interaction service started");
}
} else {
elizaLogger.log("Instagram client running in dry-run mode");
}

// Return manager instance
return {
post: postService,
interaction: interactionService,
state,
};
} catch (error) {
elizaLogger.error("Failed to start Instagram client:", error);
throw error;
}
} else {
elizaLogger.log("Instagram client running in dry-run mode");
}

// Return manager instance
return {
post: postService,
interaction: interactionService,
state
};

} catch (error) {
elizaLogger.error("Failed to start Instagram client:", error);
throw error;
}
},

async stop(runtime: IAgentRuntime) {
elizaLogger.log("Stopping Instagram client services...");
// Cleanup will be handled by the services themselves
}
},

async stop(_runtime: IAgentRuntime) {
elizaLogger.log("Stopping Instagram client services...");
// Cleanup will be handled by the services themselves
},
};

export default InstagramClientInterface;
export default InstagramClientInterface;
132 changes: 65 additions & 67 deletions packages/client-instagram/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/lib/auth.ts
import { IAgentRuntime, elizaLogger } from "@elizaos/core";
import { IgLoginTwoFactorRequiredError } from 'instagram-private-api';
import { IgLoginTwoFactorRequiredError } from "instagram-private-api";
import { InstagramConfig } from "../environment";
import { InstagramState } from "../types";
import { fetchProfile } from "./profile";
Expand All @@ -10,96 +10,94 @@ import { createInitialState, getIgClient } from "./state";
* Authenticates with Instagram
*/
async function authenticate(
runtime: IAgentRuntime,
config: InstagramConfig
runtime: IAgentRuntime,
config: InstagramConfig
): Promise<InstagramState> {
const ig = getIgClient();
let state = createInitialState();
const ig = getIgClient();
const state = createInitialState();

try {
// Generate device ID
ig.state.generateDevice(config.INSTAGRAM_USERNAME);

// Attempt to load cached session
const cachedSession = await runtime.cacheManager.get('instagram/session');
if (cachedSession) {
try {
await ig.state.deserialize(cachedSession);
const profile = await fetchProfile(runtime, config);
return {
...state,
isInitialized: true,
profile
};
} catch (error) {
elizaLogger.warn('Cached session invalid, proceeding with fresh login');
}
}

// Proceed with fresh login
try {
await ig.account.login(
config.INSTAGRAM_USERNAME,
config.INSTAGRAM_PASSWORD
);
// Generate device ID
ig.state.generateDevice(config.INSTAGRAM_USERNAME);

// Attempt to load cached session
const cachedSession =
await runtime.cacheManager.get("instagram/session");
if (cachedSession) {
try {
await ig.state.deserialize(cachedSession);
const profile = await fetchProfile(runtime, config);
return {
...state,
isInitialized: true,
profile,
};
} catch {
elizaLogger.warn(
`Cached session invalid, proceeding with fresh login`
);
}
}

// Cache the session
const serialized = await ig.state.serialize();
await runtime.cacheManager.set('instagram/session', serialized);
// Proceed with fresh login
try {
await ig.account.login(
config.INSTAGRAM_USERNAME,
config.INSTAGRAM_PASSWORD
);

const profile = await fetchProfile(runtime, config);
// Cache the session
const serialized = await ig.state.serialize();
await runtime.cacheManager.set("instagram/session", serialized);

return {
...state,
isInitialized: true,
profile
};
const profile = await fetchProfile(runtime, config);

return {
...state,
isInitialized: true,
profile,
};
} catch (error) {
if (error instanceof IgLoginTwoFactorRequiredError) {
// Handle 2FA if needed - would need to implement 2FA code generation
throw new Error("2FA authentication not yet implemented");
}
throw error;
}
} catch (error) {
if (error instanceof IgLoginTwoFactorRequiredError) {
// Handle 2FA if needed - would need to implement 2FA code generation
throw new Error('2FA authentication not yet implemented');
}
throw error;
elizaLogger.error("Authentication failed:", error);
throw error;
}

} catch (error) {
elizaLogger.error('Authentication failed:', error);
throw error;
}
}

/**
* Sets up webhooks for real-time updates if needed
*/
async function setupWebhooks() {
// Implement webhook setup
// This is a placeholder for future implementation
// Implement webhook setup
// This is a placeholder for future implementation
}

/**
* Initializes the Instagram client
*/
export async function initializeClient(
runtime: IAgentRuntime,
config: InstagramConfig
runtime: IAgentRuntime,
config: InstagramConfig
): Promise<InstagramState> {
try {
// Authenticate and get initial state
const state = await authenticate(runtime, config);
try {
// Authenticate and get initial state
const state = await authenticate(runtime, config);

// Set up webhook handlers if needed
await setupWebhooks();
// Set up webhook handlers if needed
await setupWebhooks();

return state;
} catch (error) {
elizaLogger.error('Failed to initialize Instagram client:', error);
throw error;
}
return state;
} catch (error) {
elizaLogger.error("Failed to initialize Instagram client:", error);
throw error;
}
}

// Export other authentication related functions if needed
export {
authenticate,
setupWebhooks
};
export { authenticate, setupWebhooks };
Loading

0 comments on commit 5ae8feb

Please sign in to comment.