Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

refactor: linkedin v6

Matthew Diakonov1bf6323a7c76
diff --git a/pipes/linkedin-ai-assistant/lib/chrome-session.ts b/pipes/linkedin-ai-assistant/lib/chrome-session.ts
index ed144bf0b..936e68922 100644
--- a/pipes/linkedin-ai-assistant/lib/chrome-session.ts
+++ b/pipes/linkedin-ai-assistant/lib/chrome-session.ts
@@ -1,13 +1,21 @@
+import fs from 'fs/promises';
+import path from 'path';
import { Page } from 'puppeteer-core';
-// Create a new file for managing Chrome session state
+const SESSION_FILE = path.join(process.cwd(), 'lib', 'storage', 'chrome-session.json');
+
export class ChromeSession {
private static instance: ChromeSession;
private wsUrl: string | null = null;
private isConnected: boolean = false;
private activePage: Page | null = null;
- private constructor() {}
+ private constructor() {
+ // Load saved state on instantiation
+ this.loadState().catch(err => {
+ console.log('failed to load chrome session state:', err);
+ });
+ }
static getInstance(): ChromeSession {
if (!ChromeSession.instance) {
@@ -16,9 +24,35 @@ export class ChromeSession {
return ChromeSession.instance;
}
+ private async saveState() {
+ const state = {
+ wsUrl: this.wsUrl,
+ isConnected: this.isConnected,
+ hasActivePage: !!this.activePage
+ };
+
+ try {
+ await fs.writeFile(SESSION_FILE, JSON.stringify(state, null, 2));
+ } catch (err) {
+ console.log('failed to save chrome session state:', err);
+ }
+ }
+
+ private async loadState() {
+ try {
+ const data = await fs.readFile(SESSION_FILE, 'utf-8');
+ const state = JSON.parse(data);
+ this.wsUrl = state.wsUrl;
+ this.isConnected = state.isConnected;
+ } catch (err) {
+ console.log('no saved chrome session state found');
+ }
+ }
+
setWsUrl(url: string) {
this.wsUrl = url;
this.isConnected = true;
+ this.saveState();
}
getWsUrl(): string | null {
@@ -27,6 +61,7 @@ export class ChromeSession {
setActivePage(page: Page) {
this.activePage = page;
+ this.saveState();
}
getActivePage(): Page | null {
@@ -41,5 +76,7 @@ export class ChromeSession {
this.wsUrl = null;
this.isConnected = false;
this.activePage = null;
+ this.saveState();
+ console.log('chrome session cleared');
}
}
\ No newline at end of file
diff --git a/pipes/linkedin-ai-assistant/lib/storage/storage.ts b/pipes/linkedin-ai-assistant/lib/storage/storage.ts
index fe1ec8ba8..2f91d9040 100644
--- a/pipes/linkedin-ai-assistant/lib/storage/storage.ts
+++ b/pipes/linkedin-ai-assistant/lib/storage/storage.ts
@@ -2,6 +2,7 @@ import fs from 'fs/promises';
import path from 'path';
import { ProfileVisit, State, Message, ProfileStore, ProfileDetails, MessageStore } from './types';
import { getActiveBrowser } from '../browser-setup';
+import { ChromeSession } from '../chrome-session';
const STORAGE_DIR = path.join(process.cwd(), 'lib', 'storage');
console.log('storage directory:', STORAGE_DIR);
@@ -416,26 +417,56 @@ export async function isStopRequested(): Promise<boolean> {
}
export async function saveToChrome(key: string, data: unknown) {
- const { page } = getActiveBrowser();
- if (!page) return;
-
- // Navigate to LinkedIn if we're not already there
- if (!page.url().includes('linkedin.com')) {
- await page.goto('https://www.linkedin.com');
+ const session = ChromeSession.getInstance();
+ const page = session.getActivePage();
+ if (!page) {
+ console.log('cannot save to chrome: no active page in session');
+ return;
}
- await page.evaluate((key: string, data: unknown) => {
- localStorage.setItem(key, JSON.stringify(data));
- }, key, data);
+ try {
+ console.log('current page url:', await page.url());
+
+ // Navigate to LinkedIn if we're not already there
+ if (!page.url().includes('linkedin.com')) {
+ console.log('navigating to linkedin.com...');
+ await page.goto('https://www.linkedin.com');
+ console.log('navigation complete, new url:', await page.url());
+ }
+
+ // console.log(`attempting to save ${key} to chrome storage...`);
+ await page.evaluate((key: string, data: unknown) => {
+ console.log('in page context, saving:', key, data);
+ localStorage.setItem(key, JSON.stringify(data));
+ console.log('storage after save:', localStorage.getItem(key));
+ }, key, data);
+ // console.log('chrome storage save complete');
+ } catch (err) {
+ console.log('failed to save to chrome storage:', err);
+ }
}
export async function loadFromChrome(key: string) {
- const { page } = getActiveBrowser();
- if (!page) return null;
-
- const data = await page.evaluate((key: string) => {
- return localStorage.getItem(key);
- }, key);
+ const session = ChromeSession.getInstance();
+ const page = session.getActivePage();
+ if (!page) {
+ console.log('cannot load from chrome: no active page in session');
+ return null;
+ }
- return data ? JSON.parse(data) : null;
+ try {
+ console.log('attempting to load', key, 'from chrome storage');
+ const data = await page.evaluate((key: string) => {
+ console.log('in page context, loading:', key);
+ const value = localStorage.getItem(key);
+ console.log('loaded value:', value);
+ return value;
+ }, key);
+
+ console.log('chrome storage load result:', data ? 'found' : 'not found');
+ return data ? JSON.parse(data) : null;
+ } catch (err) {
+ console.log('failed to load from chrome storage:', err);
+ return null;
+ }
}
\ No newline at end of file