Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: lint errors #2400

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading