ilml-plugin-linkedin 1.14.0 → 1.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/visit.mjs CHANGED
@@ -1,7 +1,7 @@
1
- var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{for(var n in t)Kt(e,n,{get:t[n],enumerable:!0})};async function $(e){let t=Math.floor(Math.random()*2001),n=e+t;return new Promise(o=>setTimeout(o,n))}async function ot(e){return await e.evaluate(()=>{let t=document.querySelectorAll("div.artdeco-modal--is-open");for(let o of t){let r=o.innerText||"",s=r.includes("Premium")||r.includes("premium")||r.includes("Upgrade")||r.includes("upgrade"),i=r.includes("Try for")||r.includes("free trial")||r.includes("/month")||r.includes("per month")||r.includes("Start my")||r.includes("Subscribe"),c=r.includes("Get hired faster")||r.includes("Get hired 2x");if(s||i||c){let a=o.querySelector('button[aria-label="Dismiss"]')||o.querySelector("button.artdeco-modal__dismiss")||o.querySelector("button[data-test-modal-close-btn]");if(a)return a.click(),{dismissed:!0,matched:r.substring(0,80)};let m=o.querySelectorAll("button");for(let l of m){let y=l.innerText?.trim().toLowerCase()||"",u=l.getAttribute("aria-label")?.toLowerCase()||"";if(y==="not now"||y==="no thanks"||y==="dismiss"||u.includes("dismiss"))return l.click(),{dismissed:!0,matched:r.substring(0,80)}}}}let n=document.querySelector(".premium-upsell-link")||document.querySelector("[data-test-premium-upsell]")||document.querySelector(".premium-hub-upsell");if(n){let o=n.closest('[class*="modal"]')?.querySelector('button[aria-label="Dismiss"]')||n.closest('[class*="banner"]')?.querySelector('button[aria-label="Close"]');if(o)return o.click(),{dismissed:!0,matched:"premium-banner"}}return!1})}var se=nt(()=>{});var ht={};ge(ht,{extractCertificationsFromDetails:()=>_n,extractEducationFromDetails:()=>En,extractExperienceFromDetails:()=>xn,extractFeaturedFromDetails:()=>$n,extractRecommendationsFromDetails:()=>In,extractSkillsFromDetails:()=>An});async function xn(e,t){let n=t.replace(/\/+$/,"")+"/details/experience/";try{await e.goto(n,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let o=e.url();if(/\/(login|authwall|checkpoint)/i.test(o))throw new Error(`Session expired \u2014 bounced to ${o} when fetching /details/experience/`);for(let s=0;s<5;s++)await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(900);await $(1e3);let r=0;for(;r<5&&await e.evaluate(()=>{for(let i of document.querySelectorAll("button"))if(/^Load more$/i.test((i.textContent||"").trim()))return i.click(),!0;return!1});)r++,await $(1500),await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(700);return await e.evaluate(()=>{let s=document.querySelector("main");if(!s)return[];function i(l){let y=[];for(let u of l.querySelectorAll("div")){let f=Array.from(u.children).filter(v=>v.tagName==="DIV");if(f.length<3)continue;let C=f.filter(v=>(v.textContent||"").trim().length>50);C.length<3||(u.textContent||"").length<500||y.push({el:u,count:C.length,totalText:(u.textContent||"").length})}return y.sort((u,f)=>f.count-u.count||u.totalText-f.totalText),y[0]?.el}let c=i(s);if(!c)return[];let a=Array.from(c.children).filter(l=>l.tagName==="DIV").filter(l=>(l.textContent||"").trim().length>50);function m(l){let y=l[0]||null,u=null,f=null;if(l[1]){let b=l[1].split("\xB7").map(I=>I.trim());u=b[0]||null,b[1]&&(f=b[1])}let C=l.findIndex(b=>/(\d{4}|present)/i.test(b)&&/(-|–|—)/i.test(b)&&b.length<80),v=null,_=null;if(C>=0){let b=l[C].split("\xB7").map(I=>I.trim());v=b[0]||null,b[1]&&(_=b[1])}let h=null,g=-1;if(C>=0&&l[C+1]){let b=l[C+1],I=/^[>*\-•]|^&gt;/.test(b),te=/https?:\/\//.test(b),D=/\d{4}/.test(b),me=/,/.test(b)||/\bArea\b|\bRegion\b|\bProvince\b|\bCountry\b|, [A-Z]{2}\b/.test(b);!I&&!te&&!D&&b.length<100&&me&&(h=b,g=C+1)}let x=null,ee=-1,q=l[l.length-1];if(q&&/and\s+\+\d+\s+skills?$/i.test(q)){ee=l.length-1;let b=q.match(/^(.+?)\s+and\s+\+(\d+)\s+skills?$/i);b&&(x={listed:b[1].split(",").map(I=>I.trim()),additional:parseInt(b[2],10)})}let W=new Set([0,1,C,g,ee].filter(b=>b>=0)),T=l.filter((b,I)=>!W.has(I)),ve=T.length>0?T.join(`
1
+ var zt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{for(var n in t)zt(e,n,{get:t[n],enumerable:!0})};async function $(e){let t=Math.floor(Math.random()*2001),n=e+t;return new Promise(o=>setTimeout(o,n))}async function ot(e){return await e.evaluate(()=>{let t=document.querySelectorAll("div.artdeco-modal--is-open");for(let o of t){let r=o.innerText||"",s=r.includes("Premium")||r.includes("premium")||r.includes("Upgrade")||r.includes("upgrade"),i=r.includes("Try for")||r.includes("free trial")||r.includes("/month")||r.includes("per month")||r.includes("Start my")||r.includes("Subscribe"),c=r.includes("Get hired faster")||r.includes("Get hired 2x");if(s||i||c){let a=o.querySelector('button[aria-label="Dismiss"]')||o.querySelector("button.artdeco-modal__dismiss")||o.querySelector("button[data-test-modal-close-btn]");if(a)return a.click(),{dismissed:!0,matched:r.substring(0,80)};let m=o.querySelectorAll("button");for(let l of m){let y=l.innerText?.trim().toLowerCase()||"",u=l.getAttribute("aria-label")?.toLowerCase()||"";if(y==="not now"||y==="no thanks"||y==="dismiss"||u.includes("dismiss"))return l.click(),{dismissed:!0,matched:r.substring(0,80)}}}}let n=document.querySelector(".premium-upsell-link")||document.querySelector("[data-test-premium-upsell]")||document.querySelector(".premium-hub-upsell");if(n){let o=n.closest('[class*="modal"]')?.querySelector('button[aria-label="Dismiss"]')||n.closest('[class*="banner"]')?.querySelector('button[aria-label="Close"]');if(o)return o.click(),{dismissed:!0,matched:"premium-banner"}}return!1})}var se=nt(()=>{});var gt={};ge(gt,{extractCertificationsFromDetails:()=>In,extractEducationFromDetails:()=>An,extractExperienceFromDetails:()=>$n,extractFeaturedFromDetails:()=>En,extractRecommendationsFromDetails:()=>Tn,extractSkillsFromDetails:()=>_n});async function $n(e,t){let n=t.replace(/\/+$/,"")+"/details/experience/";try{await e.goto(n,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let o=e.url();if(/\/(login|authwall|checkpoint)/i.test(o))throw new Error(`Session expired \u2014 bounced to ${o} when fetching /details/experience/`);for(let s=0;s<5;s++)await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(900);await $(1e3);let r=0;for(;r<5&&await e.evaluate(()=>{for(let i of document.querySelectorAll("button"))if(/^Load more$/i.test((i.textContent||"").trim()))return i.click(),!0;return!1});)r++,await $(1500),await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(700);return await e.evaluate(()=>{let s=document.querySelector("main");if(!s)return[];function i(l){let y=[];for(let u of l.querySelectorAll("div")){let f=Array.from(u.children).filter(v=>v.tagName==="DIV");if(f.length<3)continue;let C=f.filter(v=>(v.textContent||"").trim().length>50);C.length<3||(u.textContent||"").length<500||y.push({el:u,count:C.length,totalText:(u.textContent||"").length})}return y.sort((u,f)=>f.count-u.count||u.totalText-f.totalText),y[0]?.el}let c=i(s);if(!c)return[];let a=Array.from(c.children).filter(l=>l.tagName==="DIV").filter(l=>(l.textContent||"").trim().length>50);function m(l){let y=l[0]||null,u=null,f=null;if(l[1]){let b=l[1].split("\xB7").map(I=>I.trim());u=b[0]||null,b[1]&&(f=b[1])}let C=l.findIndex(b=>/(\d{4}|present)/i.test(b)&&/(-|–|—)/i.test(b)&&b.length<80),v=null,_=null;if(C>=0){let b=l[C].split("\xB7").map(I=>I.trim());v=b[0]||null,b[1]&&(_=b[1])}let h=null,g=-1;if(C>=0&&l[C+1]){let b=l[C+1],I=/^[>*\-•]|^&gt;/.test(b),te=/https?:\/\//.test(b),D=/\d{4}/.test(b),me=/,/.test(b)||/\bArea\b|\bRegion\b|\bProvince\b|\bCountry\b|, [A-Z]{2}\b/.test(b);!I&&!te&&!D&&b.length<100&&me&&(h=b,g=C+1)}let x=null,ee=-1,q=l[l.length-1];if(q&&/and\s+\+\d+\s+skills?$/i.test(q)){ee=l.length-1;let b=q.match(/^(.+?)\s+and\s+\+(\d+)\s+skills?$/i);b&&(x={listed:b[1].split(",").map(I=>I.trim()),additional:parseInt(b[2],10)})}let W=new Set([0,1,C,g,ee].filter(b=>b>=0)),T=l.filter((b,I)=>!W.has(I)),ve=T.length>0?T.join(`
2
2
 
3
3
  `):null;return{title:y,company:u,employmentType:f,dates:v,duration:_,location:h,description:ve,skills:x,_raw:l}}return a.map(l=>{let u=(l.innerText||"").trim().split(`
4
- `).map(f=>f.trim()).filter(f=>f&&f.length<5e3);return m(u)}).filter(l=>l.title)})}catch(o){if(/session expired|bounced to|authwall|checkpoint/i.test(o.message||""))throw o;return console.error(`extractExperienceFromDetails failed for ${t}: ${o.message?.slice(0,120)}`),[]}}async function le(e,t,n){let o=t.replace(/\/+$/,"")+`/details/${n}/`;await e.goto(o,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let r=e.url();if(/\/(login|authwall|checkpoint)/i.test(r))throw new Error(`Session expired \u2014 bounced to ${r} when fetching /details/${n}/`);if(!r.toLowerCase().includes(`/details/${n.toLowerCase()}/`))throw new Error(`Section /details/${n}/ not available on profile (landed on ${r})`);for(let i=0;i<4;i++)await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(700);await $(800);let s=0;for(;s<5&&await e.evaluate(()=>{for(let c of document.querySelectorAll("button"))if(/^Load more$/i.test((c.textContent||"").trim()))return c.click(),!0;return!1});)s++,await $(1300),await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(600)}async function ce(e,t,n,o){try{return await o()}catch(r){let s=r.message||"";if(/session expired|bounced to|authwall|checkpoint/i.test(s))throw r;return/not available on profile/i.test(s)?(console.log(` (${t}: section not on profile \u2014 skipped)`),[]):(console.error(`${n} failed for ${e}: ${s.slice(0,120)}`),[])}}async function $n(e,t){return ce(t,"featured","extractFeaturedFromDetails",async()=>(await le(e,t,"featured"),await e.evaluate(`(() => {
4
+ `).map(f=>f.trim()).filter(f=>f&&f.length<5e3);return m(u)}).filter(l=>l.title)})}catch(o){if(/session expired|bounced to|authwall|checkpoint/i.test(o.message||""))throw o;return console.error(`extractExperienceFromDetails failed for ${t}: ${o.message?.slice(0,120)}`),[]}}async function le(e,t,n){let o=t.replace(/\/+$/,"")+`/details/${n}/`;await e.goto(o,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let r=e.url();if(/\/(login|authwall|checkpoint)/i.test(r))throw new Error(`Session expired \u2014 bounced to ${r} when fetching /details/${n}/`);if(!r.toLowerCase().includes(`/details/${n.toLowerCase()}/`))throw new Error(`Section /details/${n}/ not available on profile (landed on ${r})`);for(let i=0;i<4;i++)await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(700);await $(800);let s=0;for(;s<5&&await e.evaluate(()=>{for(let c of document.querySelectorAll("button"))if(/^Load more$/i.test((c.textContent||"").trim()))return c.click(),!0;return!1});)s++,await $(1300),await e.evaluate(()=>window.scrollTo(0,document.body.scrollHeight)),await $(600)}async function ce(e,t,n,o){try{return await o()}catch(r){let s=r.message||"";if(/session expired|bounced to|authwall|checkpoint/i.test(s))throw r;return/not available on profile/i.test(s)?(console.log(` (${t}: section not on profile \u2014 skipped)`),[]):(console.error(`${n} failed for ${e}: ${s.slice(0,120)}`),[])}}async function En(e,t){return ce(t,"featured","extractFeaturedFromDetails",async()=>(await le(e,t,"featured"),await e.evaluate(`(() => {
5
5
  ${ae}
6
6
 
7
7
  const main = document.querySelector('main');
@@ -42,7 +42,7 @@ var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{
42
42
  if (e.caption && /^\xB7\\s*\\d+(?:st|nd|rd|th)\\+?$/i.test(e.caption.trim())) return false;
43
43
  return true;
44
44
  });
45
- })()`)))}async function En(e,t){return ce(t,"education","extractEducationFromDetails",async()=>(await le(e,t,"education"),await e.evaluate(`(() => {
45
+ })()`)))}async function An(e,t){return ce(t,"education","extractEducationFromDetails",async()=>(await le(e,t,"education"),await e.evaluate(`(() => {
46
46
  ${ae}
47
47
 
48
48
  const main = document.querySelector('main');
@@ -79,7 +79,7 @@ var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{
79
79
  if (e.degree && /^\xB7\\s*\\d+(?:st|nd|rd|th)\\+?$/i.test(e.degree.trim())) return false;
80
80
  return true;
81
81
  });
82
- })()`)))}async function An(e,t){return ce(t,"skills","extractSkillsFromDetails",async()=>(await le(e,t,"skills"),await e.evaluate(`(() => {
82
+ })()`)))}async function _n(e,t){return ce(t,"skills","extractSkillsFromDetails",async()=>(await le(e,t,"skills"),await e.evaluate(`(() => {
83
83
  ${ae}
84
84
 
85
85
  const main = document.querySelector('main');
@@ -111,7 +111,7 @@ var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{
111
111
  skills.push({ name, endorsements, _raw: lines });
112
112
  }
113
113
  return skills;
114
- })()`)))}async function _n(e,t){return ce(t,"certifications","extractCertificationsFromDetails",async()=>(await le(e,t,"certifications"),await e.evaluate(`(() => {
114
+ })()`)))}async function In(e,t){return ce(t,"certifications","extractCertificationsFromDetails",async()=>(await le(e,t,"certifications"),await e.evaluate(`(() => {
115
115
  ${ae}
116
116
 
117
117
  const main = document.querySelector('main');
@@ -136,7 +136,7 @@ var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{
136
136
 
137
137
  return { name, issuer, issueDate, expiryDate, credentialId, _raw: lines };
138
138
  }).filter(e => e.name);
139
- })()`)))}async function In(e,t){return ce(t,"recommendations","extractRecommendationsFromDetails",async()=>(await le(e,t,"recommendations"),await e.evaluate(`(() => {
139
+ })()`)))}async function Tn(e,t){return ce(t,"recommendations","extractRecommendationsFromDetails",async()=>(await le(e,t,"recommendations"),await e.evaluate(`(() => {
140
140
  ${ae}
141
141
 
142
142
  const main = document.querySelector('main');
@@ -172,7 +172,7 @@ var Kt=Object.defineProperty;var nt=(e,t)=>()=>(e&&(t=e(e=0)),t);var ge=(e,t)=>{
172
172
  if (e.recommenderTitle && /^\xB7\\s*\\d+(?:st|nd|rd|th)\\+?$/i.test(e.recommenderTitle.trim())) return false;
173
173
  return true;
174
174
  });
175
- })()`)))}var ae,gt=nt(()=>{se();ae=`
175
+ })()`)))}var ae,yt=nt(()=>{se();ae=`
176
176
  function findEntryContainer(root) {
177
177
  const candidates = [];
178
178
  for (const div of root.querySelectorAll('div')) {
@@ -235,17 +235,17 @@ function findEntryContainer(root) {
235
235
  candidates.sort((a, b) => b.count - a.count || b.totalText - a.totalText);
236
236
  return candidates[0]?.el;
237
237
  }
238
- `});import on from"puppeteer";import{addExtra as rn}from"puppeteer-extra";import sn from"puppeteer-extra-plugin-stealth";se();import{getPluginConfig as zt}from"@ilivemylife/graph-sdk";var Qt=await zt();for(let[e,t]of Object.entries(Qt))process.env[e]===void 0&&(process.env[e]=t);var Zt=process.env.LINKEDIN_NAME,Y=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),Mo=Object.freeze(new Set([Y.DRAFT,Y.CONFIRMED,Y.NEEDS_REVIEW])),Xt=Object.freeze(new Set([Y.DRAFT,Y.CONFIRMED,Y.REJECTED,Y.NEEDS_REVIEW]));import rt from"fs";import en from"path";var tn="cookies.json";function nn(){let e=process.env.ILML_SCOPE_DIR;return e?en.join(e,"plugins-state","linkedin","cookies.json"):tn}function st(){let e=nn();if(!rt.existsSync(e))throw new Error(`Cookies not found at ${e}. Run: ilml linkedin login`);let t;try{t=rt.readFileSync(e,"utf-8")}catch(o){throw new Error(`Failed to read cookies at ${e}: ${o.message}. Run: ilml linkedin login`)}let n;try{n=JSON.parse(t)}catch{throw new Error(`Cookies file at ${e} is corrupted (invalid JSON). Run: ilml linkedin login`)}if(!Array.isArray(n)||n.length===0)throw new Error(`Cookies file at ${e} is empty or not an array. Run: ilml linkedin login`);return n}var it=rn(on);it.use(sn());async function lt(){let e=await it.launch({headless:!1,defaultViewport:null,protocolTimeout:3e5}),t=async()=>{console.log(`
238
+ `});import rn from"puppeteer";import{addExtra as sn}from"puppeteer-extra";import ln from"puppeteer-extra-plugin-stealth";se();import{getPluginConfig as Qt}from"@ilivemylife/graph-sdk";var Zt=await Qt();for(let[e,t]of Object.entries(Zt))process.env[e]===void 0&&(process.env[e]=t);var Xt=process.env.LINKEDIN_NAME,Y=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),Po=Object.freeze(new Set([Y.DRAFT,Y.CONFIRMED,Y.NEEDS_REVIEW])),en=Object.freeze(new Set([Y.DRAFT,Y.CONFIRMED,Y.REJECTED,Y.NEEDS_REVIEW]));import rt from"fs";import tn from"path";var nn="cookies.json";function on(){let e=process.env.ILML_SCOPE_DIR;return e?tn.join(e,"plugins-state","linkedin","cookies.json"):nn}function st(){let e=on();if(!rt.existsSync(e))throw new Error(`Cookies not found at ${e}. Run: ilml linkedin login`);let t;try{t=rt.readFileSync(e,"utf-8")}catch(o){throw new Error(`Failed to read cookies at ${e}: ${o.message}. Run: ilml linkedin login`)}let n;try{n=JSON.parse(t)}catch{throw new Error(`Cookies file at ${e} is corrupted (invalid JSON). Run: ilml linkedin login`)}if(!Array.isArray(n)||n.length===0)throw new Error(`Cookies file at ${e} is empty or not an array. Run: ilml linkedin login`);return n}var it=sn(rn);it.use(ln());async function lt(){let e=await it.launch({headless:!1,defaultViewport:null,protocolTimeout:3e5}),t=async()=>{console.log(`
239
239
  Closing browser...`);try{await e.close()}catch{}process.exit(0)};process.on("SIGINT",t),process.on("SIGTERM",t);let n=await e.newPage();await n.evaluateOnNewDocument(()=>{Object.defineProperty(navigator,"webdriver",{get:()=>{}})});try{let o=st();await n.setCookie(...o),console.log(" Browser launched, cookies loaded.")}catch(o){console.error(`
240
240
  [!] ${o.message}
241
- `),await e.close(),process.exit(1)}return{browser:e,page:n}}se();import"dotenv/config";import*as E from"fs";import*as A from"path";import{fileURLToPath as ln}from"url";var an=ln(import.meta.url),cn=A.dirname(A.dirname(an)),B=process.env.DATA_DIR?A.resolve(process.env.DATA_DIR,"collected-profiles"):A.join(cn,"collected-profiles"),xe=A.join(B,"people.json"),nr=A.join(B,"jobs.json"),or=A.join(B,"quota.json"),rr=A.join(B,"conversations.json"),sr=A.join(B,"sync-state.json"),ir=A.join(B,"application-questions.json"),$e=A.join(B,"profile-history");var ne=null,un=null,dn=null,fn=null,mn=null,pn=null;function ut(){ne=null,un=null,dn=null,fn=null,mn=null,pn=null}function dt(){E.existsSync(B)||E.mkdirSync(B,{recursive:!0})}function ft(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";E.writeFileSync(o,n);for(let r=0;r<3;r++)try{E.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function mt(e,t){if(!E.existsSync(e))return null;try{let n=JSON.parse(E.readFileSync(e,"utf-8"));try{E.copyFileSync(e,e+".backup")}catch{}return n}catch(n){console.error(`[!] ${t} is corrupted: ${n.message}`),console.error(` File: ${A.resolve(e)}`);let o=e+".backup";if(E.existsSync(o))try{let r=JSON.parse(E.readFileSync(o,"utf-8"));console.error(` Restored from ${o}`);try{E.copyFileSync(o,e)}catch{}return r}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{E.copyFileSync(e,e+".corrupted")}catch{}return null}}function hn(){return new Date().toISOString().split("T")[0]}var gn=["name","title","location","company","connectionDegree","connectionsCount","mutualConnections","photoUrl","hasVerifiedBadge","about","currentCompany","currentSchool","connectionsLevel","mutualConnectionsCount","followers","openToWork","hiring","topSkills","availableSections","experience","featuredLinks","_lastEnrichedAt","featured","education","skillsFull","recommendations","certifications"],yn=["name","title","location","currentCompany","currentSchool","connectionDegree","hasVerifiedBadge","openToWork","hiring","about","topSkills"];function Ee(e){if(!e)return null;let t=e.match(/\/in\/([^/?#]+)/);return t?t[1].toLowerCase():null}function wn(){E.existsSync($e)||E.mkdirSync($e,{recursive:!0})}function pt(e){let t=Ee(e);return t?A.join($e,`${t}.json`):null}function bn(e){let t=pt(e);return t?mt(t,`profile-history/${Ee(e)}.json`)||[]:[]}function Sn(e,t){let n=pt(e);if(!n)return;wn();let o=bn(e);o.unshift(t);try{ft(n,o)}catch(r){console.error(`[!] Failed to append profile-history for ${Ee(e)}: ${r.message}`)}}function at(e){return!!(e==null||Array.isArray(e)&&e.length===0||typeof e=="string"&&!e.trim())}function ct(e){return Array.isArray(e)?[...e].map(t=>String(t).trim()).sort().join("||"):typeof e=="string"?e.replace(/\s+/g," ").trim():e}function vn(e,t){if(!e)return[];let n=[];for(let o of yn){let r=e[o],s=t[o];at(r)||at(s)||ct(r)!==ct(s)&&n.push(o)}return n}function kn(e){let t=new Set(["seenCount","sources","firstSeen","lastSeen","lastUpdated","encounters","jobIds","lastFullVisitAt","lastSnapshotAt","messageSent","connectionStatus","connectionDate","connectionMethod"]),n={};for(let[o,r]of Object.entries(e))t.has(o)||(n[o]=r);return n}function ie(){return ne||(dt(),ne=mt(xe,"people.json")||{},ne)}function Cn(e){dt(),ne=e;try{ft(xe,e)}catch(t){console.error(`[!] Failed to save people.json: ${t.message}`),console.error(` Path: ${A.resolve(xe)}`)}}function Ae(e,t={}){if(!e)return null;let n=ie(),o=hn(),r=new Date().toISOString(),s=n[e];if(s){let i=vn(s,t);i.length>0&&(Sn(e,{capturedAt:r,previousLastUpdated:s.lastUpdated||null,changedFields:i,snapshot:kn(s)}),s.lastSnapshotAt=r),s.seenCount=(s.seenCount||1)+1,s.lastSeen=o,s.lastUpdated=r,t.source&&!s.sources?.includes(t.source)&&(s.sources=s.sources||[],s.sources.push(t.source));for(let c of gn)if(t[c]!=null){if(Array.isArray(t[c])&&t[c].length===0&&Array.isArray(s[c])&&s[c].length>0)continue;s[c]=t[c]}Array.isArray(t.availableSections)&&(s.lastFullVisitAt=r)}else n[e]={name:t.name||null,title:t.title||null,location:t.location||null,company:t.company||null,about:t.about||null,connectionDegree:t.connectionDegree||null,connectionsCount:t.connectionsCount||null,mutualConnections:t.mutualConnections||null,photoUrl:t.photoUrl||null,hasVerifiedBadge:t.hasVerifiedBadge||!1,currentCompany:t.currentCompany||null,currentSchool:t.currentSchool||null,connectionsLevel:t.connectionsLevel||null,mutualConnectionsCount:t.mutualConnectionsCount||null,followers:t.followers||null,openToWork:t.openToWork||!1,hiring:t.hiring||!1,topSkills:t.topSkills||null,availableSections:t.availableSections||null,experience:t.experience||null,featuredLinks:t.featuredLinks||null,_lastEnrichedAt:t._lastEnrichedAt||null,featured:t.featured||null,education:t.education||null,skillsFull:t.skillsFull||null,recommendations:t.recommendations||null,certifications:t.certifications||null,firstSeen:o,lastSeen:o,seenCount:1,sources:t.source?[t.source]:[],connectionStatus:null,connectionDate:null,connectionMethod:null,messageSent:null,jobIds:[],lastUpdated:r,lastSnapshotAt:null,lastFullVisitAt:Array.isArray(t.availableSections)?r:null};return Cn(n),n[e]}async function Tn(e,t){let n=e.url();if(n.includes("/in/"))return!1;if(n.includes("/login")||n.includes("/authwall")||n.includes("/checkpoint"))throw new Error("Session expired \u2014 redirected to login. Run: npm run login");console.log(` Off-track: ${n.slice(0,80)}`),console.log(" Navigating back to profile..."),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let o=e.url();return o.includes("/in/")||console.log(` Recovery failed \u2014 still on: ${o.slice(0,80)}`),!0}async function yt(e,t,n={}){let{extractExperience:o=!1,extractFeatured:r=!1,extractEducation:s=!1,extractSkills:i=!1,extractRecommendations:c=!1,extractCertifications:a=!1}=n;try{await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3),await ot(e)&&(console.log(" Dismissed Premium overlay"),await $(1500),await Tn(e,t));let l=0;for(let x=0;x<2;x++){let ee=await e.evaluate(()=>document.body.scrollHeight);for(let W=1;W<=6;W++)await e.evaluate(T=>window.scrollTo(0,T),ee*W/6),await $(900);let q=await e.evaluate(()=>document.body.scrollHeight);if(q===l)break;l=q}await $(800);let y=await e.evaluate(()=>{let x=new Set(["Highlights","About","Activity","Experience","Education","Skills","Recommendations","Languages","Interests","Volunteer experience","Featured","Honors & awards","Honors and awards","Projects","Publications","Patents","Certifications","Licenses & certifications","Courses","Test scores","Organizations"]),ee=new Set(["Ad Options","Don\u2019t want to see this","0 notifications","Explore Premium profiles","You might like","People you may know","Suggested for you","More profiles for you","Promoted"]),q=p=>{let w=(p||"").trim().replace(/^[·•]\s*/,"");return/^(1st|2nd|3rd|she\/her|he\/him|they\/them|their\/them)$/i.test(w)},W={name:null,title:null,location:null,currentCompany:null,currentSchool:null,connectionDegree:null,hasVerifiedBadge:!1,photoUrl:null,connectionsLevel:null,mutualConnectionsCount:null,followers:null,openToWork:!1,hiring:!1,about:null,topSkills:null,availableSections:[]},T=document.querySelector("main");if(!T)return W;let ve=[...Array.from(T.querySelectorAll("h1")),...Array.from(T.querySelectorAll("h2"))],b=Array.from(T.querySelectorAll("h2")),I=ve.find(p=>{let w=p.textContent&&p.textContent.trim(),S=(w||"").replace(/\s*\(\d+\)$/,"").trim();return w&&w.length>1&&w.length<100&&!x.has(S)&&!ee.has(S)}),te=I?I.textContent.trim():null,D=I?I.parentElement:null;for(;D&&D!==T&&!Array.from(D.children).filter(w=>w.tagName==="P").some(w=>{let S=w.textContent&&w.textContent.trim();return S&&S.length>5&&!q(S)});)D=D.parentElement;let me=null,Ve=null,He=null,ke=null;if(D&&D!==T){let p=Array.from(D.children).filter(S=>S.tagName==="P").filter(S=>{let V=S.textContent&&S.textContent.trim();return V&&V.length>5&&!q(V)});if(p[0]&&(me=p[0].textContent.trim()),p[1]){let S=p[1].textContent.trim().split(/\s*[·•]\s*/);Ve=S[0]||null,S.length>1&&(He=S.slice(1).join(" \xB7 "))}let w=Array.from(D.children).filter(S=>S.tagName==="DIV");for(let S=w.length-1;S>=0;S--){let V=w[S].textContent&&w[S].textContent.trim();if(V&&!(te&&V.startsWith(te))&&(ke=V.split(/[·•]/)[0].trim(),ke))break}}let Je=null;if(D){let p=D.querySelectorAll("p, span, div");for(let w of p){let S=(w.textContent||"").trim().replace(/^[·•]\s*/,"");if(S==="1st"||S==="2nd"||S==="3rd"){Je=S;break}}}let Ut=!!T.querySelector('svg[data-test-icon="verified"], [aria-label*="Verified" i]'),We=document.querySelector('main img[width="200"], button img[alt*="photo" i]'),Vt=We&&We.src||null,pe=T.textContent||"",Ye=null,Ge=pe.match(/(\d+\+?)\s+connections\b/i);Ge&&(Ye=Ge[1]+" connections");let he=null,Ke=pe.match(/(\d+)\s+mutual connection/i),ze=pe.match(/and\s+(\d+)\s+other mutual/i),Ht=pe.match(/([A-Z][\w'-]+(?:\s+[A-Z][\w'-]+)?)\s+and\s+([A-Z][\w'-]+(?:\s+[A-Z][\w'-]+)?)\s+are\s+mutual/);Ke?he=parseInt(Ke[1],10):ze?he=2+parseInt(ze[1],10):Ht&&(he=2);let Qe=null,Ze=null,Xe=b.find(p=>p.textContent&&p.textContent.trim()==="About");if(Xe){let p=Xe.parentElement;for(;p&&p.tagName!=="SECTION";)p=p.parentElement;if(p){let w=(p.textContent||"").trim();w=w.replace(/^About\s*/,"").trim();let S=w.match(/Top skills\s*(.+?)$/i);if(S){let Ce=S[1].split(/\b(?:see (?:more|less)|show (?:more|less))\b/i)[0].trim().split(/\s*[·•]\s*/).map(Gt=>Gt.trim()).filter(Boolean);Ce.length>0&&Ce.length<10&&(Ze=Ce)}w=w.split(/\n?…\s*more/i)[0].trim(),w=w.split(/\n?\.\.\.\s*more/i)[0].trim(),w=w.split(/\bTop skills\b/i)[0].trim(),w=w.split(/\b(?:see (?:more|less)|show (?:more|less))\b/i)[0].trim(),Qe=w||null}}let et=null,tt=b.find(p=>p.textContent&&p.textContent.trim()==="Activity");if(tt){let p=tt.parentElement;for(;p&&p.tagName!=="SECTION";)p=p.parentElement;if(p){let w=(p.textContent||"").slice(0,200).match(/([\d,]+)\s+followers/i);w&&(et=parseInt(w[1].replace(/,/g,""),10))}}let Jt=[...new Set(b.map(p=>(p.textContent||"").trim().replace(/\s*\(\d+\)$/,"").trim()).filter(p=>p&&p.length>1&&p.length<60&&p!==te).filter(p=>x.has(p)))],re=document.body.textContent||"",Wt=/#OpenToWork\b/i.test(re)||/\bOpen to work\b/.test(re),Yt=/#Hiring\b/i.test(re)||/\bI'?m hiring\b/i.test(re)||/\bWe are hiring\b/i.test(re);return{name:te,title:me,location:ke,currentCompany:Ve,currentSchool:He,connectionDegree:Je,hasVerifiedBadge:Ut,photoUrl:Vt,connectionsLevel:Ye,mutualConnectionsCount:he,followers:et,openToWork:Wt,hiring:Yt,about:Qe,topSkills:Ze,availableSections:Jt}});console.log(`Viewed: ${y.name} - ${y.title}`);let u=null,f=null,C=null,v=null,_=null,h=null;if(o||r||s||i||c||a){let x=await Promise.resolve().then(()=>(gt(),ht));o&&(u=await x.extractExperienceFromDetails(e,t),console.log(` + Experience: ${u.length} entries`)),r&&(f=await x.extractFeaturedFromDetails(e,t),console.log(` + Featured: ${f.length} entries`)),s&&(C=await x.extractEducationFromDetails(e,t),console.log(` + Education: ${C.length} entries`)),i&&(v=await x.extractSkillsFromDetails(e,t),console.log(` + Skills: ${v.length} entries`)),c&&(_=await x.extractRecommendationsFromDetails(e,t),console.log(` + Recommendations: ${_.length} entries`)),a&&(h=await x.extractCertificationsFromDetails(e,t),console.log(` + Certifications: ${h.length} entries`))}return{...y,profileUrl:t,...u!==null&&{experience:u},...f!==null&&{featured:f},...C!==null&&{education:C},...v!==null&&{skillsFull:v},..._!==null&&{recommendations:_},...h!==null&&{certifications:h}}}catch(m){console.error(`Error viewing profile ${t}:`,m.message);try{await e.goto("about:blank",{timeout:1e4}),await $(1e3)}catch{}if(/session expired|bounced to|authwall|checkpoint|redirected to login/i.test(m.message||""))throw m;return null}}import"dotenv/config";import*as O from"fs";import*as j from"path";import{fileURLToPath as Dn}from"url";var Rn=Dn(import.meta.url),jn=j.dirname(j.dirname(Rn)),K=process.env.DATA_DIR?j.resolve(process.env.DATA_DIR,"market-research"):j.join(jn,"market-research"),On=j.join(K,"scouted-jobs.json"),Nn=j.join(K,"discovered-people.json"),wt=j.join(K,"visit-log.json"),Sr=j.join(K,"companies.json"),vr=j.join(K,"job-decisions.json");function Ln(){O.existsSync(K)||O.mkdirSync(K,{recursive:!0})}function Fn(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";O.writeFileSync(o,n);for(let r=0;r<3;r++)try{O.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function _e(e){try{if(O.existsSync(e))return JSON.parse(O.readFileSync(e,"utf-8"))}catch{}return null}function bt(){ue=null,Pn=null,de=null,qn=null,G=null}var ue=null;function Mn(){return ue||(ue=_e(On)||{},ue)}var Pn=null;var de=null;function ye(){return de||(de=_e(Nn)||{},de)}var qn=null;var G=null;function oe(){return G||(G=_e(wt)||{visits:[]},Array.isArray(G.visits)||(G.visits=[]),G)}function St(e,t={}){if(!e)return;Ln();let n=oe();n.visits.push({date:new Date().toISOString(),profileUrl:e,name:t.name||null,source:t.source||"manual"});let o=Date.now()-90*24*60*60*1e3;n.visits=n.visits.filter(r=>new Date(r.date).getTime()>o);try{Fn(wt,n)}catch(r){console.error(`[!] Failed to save visit-log.json: ${r.message}`)}}function vt(e){let t=oe();for(let n=t.visits.length-1;n>=0;n--)if(t.visits[n].profileUrl===e)return t.visits[n].date;return null}function kt(){let e=Mn(),t=ye(),n=Object.values(e),o=Object.values(t),r={};for(let u of n)u.company&&(r[u.company]=(r[u.company]||0)+1);let s=Object.entries(r).sort((u,f)=>f[1]-u[1]).slice(0,5).map(([u,f])=>`${u} (${f})`),i={};for(let u of n)for(let f of u.techStack||[])i[f]=(i[f]||0)+1;let c=Object.entries(i).sort((u,f)=>f[1]-u[1]).slice(0,8).map(([u,f])=>`${u} (${f})`),a=oe(),m=new Date().toISOString().slice(0,10),l=a.visits.filter(u=>u.date.startsWith(m)).length,y=new Set(a.visits.map(u=>u.profileUrl)).size;return{totalJobs:n.length,totalRecruiters:o.length,topCompanies:s,topTech:c,totalVisited:y,todayVisited:l,lastScoutDate:n.length>0?n.sort((u,f)=>(f.scoutedAt||"").localeCompare(u.scoutedAt||""))[0]?.scoutedAt:null}}se();async function Ct(e,t,n={}){let{maxVisits:o=50,delayBetween:r=4e3,skipRecentDays:s=7,verbose:i=!1,extractExperience:c=!1}=n,a={visited:0,skipped:0,errors:0},m=Date.now()-s*24*60*60*1e3;for(let l of t){if(a.visited>=o)break;if(!l.profileUrl)continue;let y=vt(l.profileUrl);if(y&&new Date(y).getTime()>m){a.skipped++;continue}try{let u=await yt(e,l.profileUrl,{extractExperience:c}),f=l.source||"visit";if(u){let v={...u,source:f};c&&Array.isArray(u.experience)&&u.experience.length>0&&(v._lastEnrichedAt=new Date().toISOString()),Ae(l.profileUrl,v)}St(l.profileUrl,{name:u?.name||l.name||null,source:f}),a.visited++,i?console.log(` [${a.visited}] ${u?.name||l.profileUrl} \u2014 ${u?.title?.slice(0,40)||"?"}`):a.visited%10===0&&console.log(` Visited: ${a.visited}`);let C=Math.random()*4e3;await $(r+C)}catch(u){a.errors++,i&&console.error(` Error visiting ${l.name||l.profileUrl}: ${u.message?.slice(0,60)}`);let f=(u.message||"").toLowerCase();if(f.includes("detached")||f.includes("session closed")||f.includes("target closed")||f.includes("session expired")||f.includes("bounced to")||f.includes("authwall")||f.includes("checkpoint")||f.includes("redirected to login")){console.log(" Fatal browser/session error \u2014 stopping visits");break}}}return a}function xt(e={}){let{limit:t=150,revisitDays:n=14}=e,o=oe(),r=new Set(o.visits?.map(h=>h.profileUrl)||[]),s=new Map;for(let h of o.visits||[]){let g=s.get(h.profileUrl);(!g||h.date>g)&&s.set(h.profileUrl,h.date)}let i=Date.now()-7*24*60*60*1e3,c=Date.now()-n*24*60*60*1e3;function a(h){let g=s.get(h);return g&&new Date(g).getTime()>i}let m=ye(),l=Object.entries(m).filter(([h])=>!r.has(h)).map(([h,g])=>({profileUrl:h,name:g.name,source:"new-discovered",priority:1,jobCount:g.jobIds?.length||0})),y=ie(),u=Object.entries(y).filter(([h,g])=>g.connectionStatus==="connected"||a(h)?!1:(g.jobCount||g.jobIds?.length||0)>=2).map(([h,g])=>({profileUrl:h,name:g.name,source:"active-recruiter",priority:2,jobCount:g.jobCount||g.jobIds?.length||0})),f=Object.entries(y).filter(([h,g])=>{let x=g.connectionStatus;return!(!x||x==="connected"||x==="pending"||x==="sent_without_note"||x==="sent_with_note"||a(h)||(g.jobCount||g.jobIds?.length||0)>=2)}).map(([h,g])=>({profileUrl:h,name:g.name,source:"unconnected",priority:3,jobCount:g.jobCount||g.jobIds?.length||0})),C=[...s.entries()].filter(([h,g])=>new Date(g).getTime()<c).map(([h,g])=>({profileUrl:h,name:null,source:"revisit",priority:4,lastVisit:g})),v=h=>h.sort(()=>Math.random()-.5);return l.sort((h,g)=>g.jobCount-h.jobCount),u.sort((h,g)=>g.jobCount-h.jobCount),v(f),v(C),[...l,...u,...f,...C].slice(0,t)}function $t(){let e=ye();return Object.entries(e).map(([t,n])=>({profileUrl:t,name:n.name,source:"discovered"}))}function Ie(){let e=ie();return Object.entries(e).filter(([,t])=>{let n=t.connectionStatus;return n&&n!=="connected"&&n!=="pending"&&n!=="sent_without_note"&&n!=="sent_with_note"}).map(([t,n])=>({profileUrl:t,name:n.name,source:"unconnected"}))}function Te(e=14){let t=oe(),n=Date.now()-e*24*60*60*1e3,o=new Map;for(let r of t.visits){let s=o.get(r.profileUrl);(!s||r.date>s.date)&&o.set(r.profileUrl,r)}return[...o.entries()].filter(([,r])=>new Date(r.date).getTime()<n).map(([r,s])=>({profileUrl:r,name:s.name,source:"revisit"}))}import"dotenv/config";import*as N from"fs";import*as H from"path";import{fileURLToPath as Wn}from"url";import"dotenv/config";import{createGraphClient as Bn,resolveToken as Un}from"@ilivemylife/graph-sdk";import{config as Vn}from"dotenv";Vn();var Et=Un()||process.env.TOKEN;Et||(console.error(`
241
+ `),await e.close(),process.exit(1)}return{browser:e,page:n}}se();import"dotenv/config";import*as E from"fs";import*as A from"path";import{fileURLToPath as an}from"url";var cn=an(import.meta.url),un=A.dirname(A.dirname(cn)),B=process.env.DATA_DIR?A.resolve(process.env.DATA_DIR,"collected-profiles"):A.join(un,"collected-profiles"),xe=A.join(B,"people.json"),or=A.join(B,"jobs.json"),rr=A.join(B,"quota.json"),sr=A.join(B,"conversations.json"),ir=A.join(B,"sync-state.json"),lr=A.join(B,"application-questions.json"),$e=A.join(B,"profile-history"),ut=(e,t)=>{let n=parseInt(e,10);return Number.isInteger(n)&&n>=0?n:t},ar=ut(process.env.CONNECT_MAX_DAILY,20),cr=ut(process.env.CONNECT_MAX_WEEKLY,100),ne=null,dn=null,fn=null,mn=null,pn=null,hn=null;function dt(){ne=null,dn=null,fn=null,mn=null,pn=null,hn=null}function ft(){E.existsSync(B)||E.mkdirSync(B,{recursive:!0})}function mt(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";E.writeFileSync(o,n);for(let r=0;r<3;r++)try{E.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function pt(e,t){if(!E.existsSync(e))return null;try{let n=JSON.parse(E.readFileSync(e,"utf-8"));try{E.copyFileSync(e,e+".backup")}catch{}return n}catch(n){console.error(`[!] ${t} is corrupted: ${n.message}`),console.error(` File: ${A.resolve(e)}`);let o=e+".backup";if(E.existsSync(o))try{let r=JSON.parse(E.readFileSync(o,"utf-8"));console.error(` Restored from ${o}`);try{E.copyFileSync(o,e)}catch{}return r}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{E.copyFileSync(e,e+".corrupted")}catch{}return null}}function gn(){return new Date().toISOString().split("T")[0]}var yn=["name","title","location","company","connectionDegree","connectionsCount","mutualConnections","photoUrl","hasVerifiedBadge","about","currentCompany","currentSchool","connectionsLevel","mutualConnectionsCount","followers","openToWork","hiring","topSkills","availableSections","experience","featuredLinks","_lastEnrichedAt","featured","education","skillsFull","recommendations","certifications"],wn=["name","title","location","currentCompany","currentSchool","connectionDegree","hasVerifiedBadge","openToWork","hiring","about","topSkills"];function Ee(e){if(!e)return null;let t=e.match(/\/in\/([^/?#]+)/);return t?t[1].toLowerCase():null}function bn(){E.existsSync($e)||E.mkdirSync($e,{recursive:!0})}function ht(e){let t=Ee(e);return t?A.join($e,`${t}.json`):null}function Sn(e){let t=ht(e);return t?pt(t,`profile-history/${Ee(e)}.json`)||[]:[]}function vn(e,t){let n=ht(e);if(!n)return;bn();let o=Sn(e);o.unshift(t);try{mt(n,o)}catch(r){console.error(`[!] Failed to append profile-history for ${Ee(e)}: ${r.message}`)}}function at(e){return!!(e==null||Array.isArray(e)&&e.length===0||typeof e=="string"&&!e.trim())}function ct(e){return Array.isArray(e)?[...e].map(t=>String(t).trim()).sort().join("||"):typeof e=="string"?e.replace(/\s+/g," ").trim():e}function kn(e,t){if(!e)return[];let n=[];for(let o of wn){let r=e[o],s=t[o];at(r)||at(s)||ct(r)!==ct(s)&&n.push(o)}return n}function Cn(e){let t=new Set(["seenCount","sources","firstSeen","lastSeen","lastUpdated","encounters","jobIds","lastFullVisitAt","lastSnapshotAt","messageSent","connectionStatus","connectionDate","connectionMethod"]),n={};for(let[o,r]of Object.entries(e))t.has(o)||(n[o]=r);return n}function ie(){return ne||(ft(),ne=pt(xe,"people.json")||{},ne)}function xn(e){ft(),ne=e;try{mt(xe,e)}catch(t){console.error(`[!] Failed to save people.json: ${t.message}`),console.error(` Path: ${A.resolve(xe)}`)}}function Ae(e,t={}){if(!e)return null;let n=ie(),o=gn(),r=new Date().toISOString(),s=n[e];if(s){let i=kn(s,t);i.length>0&&(vn(e,{capturedAt:r,previousLastUpdated:s.lastUpdated||null,changedFields:i,snapshot:Cn(s)}),s.lastSnapshotAt=r),s.seenCount=(s.seenCount||1)+1,s.lastSeen=o,s.lastUpdated=r,t.source&&!s.sources?.includes(t.source)&&(s.sources=s.sources||[],s.sources.push(t.source));for(let c of yn)if(t[c]!=null){if(Array.isArray(t[c])&&t[c].length===0&&Array.isArray(s[c])&&s[c].length>0)continue;s[c]=t[c]}Array.isArray(t.availableSections)&&(s.lastFullVisitAt=r)}else n[e]={name:t.name||null,title:t.title||null,location:t.location||null,company:t.company||null,about:t.about||null,connectionDegree:t.connectionDegree||null,connectionsCount:t.connectionsCount||null,mutualConnections:t.mutualConnections||null,photoUrl:t.photoUrl||null,hasVerifiedBadge:t.hasVerifiedBadge||!1,currentCompany:t.currentCompany||null,currentSchool:t.currentSchool||null,connectionsLevel:t.connectionsLevel||null,mutualConnectionsCount:t.mutualConnectionsCount||null,followers:t.followers||null,openToWork:t.openToWork||!1,hiring:t.hiring||!1,topSkills:t.topSkills||null,availableSections:t.availableSections||null,experience:t.experience||null,featuredLinks:t.featuredLinks||null,_lastEnrichedAt:t._lastEnrichedAt||null,featured:t.featured||null,education:t.education||null,skillsFull:t.skillsFull||null,recommendations:t.recommendations||null,certifications:t.certifications||null,firstSeen:o,lastSeen:o,seenCount:1,sources:t.source?[t.source]:[],connectionStatus:null,connectionDate:null,connectionMethod:null,messageSent:null,jobIds:[],lastUpdated:r,lastSnapshotAt:null,lastFullVisitAt:Array.isArray(t.availableSections)?r:null};return xn(n),n[e]}async function Dn(e,t){let n=e.url();if(n.includes("/in/"))return!1;if(n.includes("/login")||n.includes("/authwall")||n.includes("/checkpoint"))throw new Error("Session expired \u2014 redirected to login. Run: npm run login");console.log(` Off-track: ${n.slice(0,80)}`),console.log(" Navigating back to profile..."),await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3);let o=e.url();return o.includes("/in/")||console.log(` Recovery failed \u2014 still on: ${o.slice(0,80)}`),!0}async function wt(e,t,n={}){let{extractExperience:o=!1,extractFeatured:r=!1,extractEducation:s=!1,extractSkills:i=!1,extractRecommendations:c=!1,extractCertifications:a=!1}=n;try{await e.goto(t,{waitUntil:"domcontentloaded",timeout:6e4}),await $(3e3),await ot(e)&&(console.log(" Dismissed Premium overlay"),await $(1500),await Dn(e,t));let l=0;for(let x=0;x<2;x++){let ee=await e.evaluate(()=>document.body.scrollHeight);for(let W=1;W<=6;W++)await e.evaluate(T=>window.scrollTo(0,T),ee*W/6),await $(900);let q=await e.evaluate(()=>document.body.scrollHeight);if(q===l)break;l=q}await $(800);let y=await e.evaluate(()=>{let x=new Set(["Highlights","About","Activity","Experience","Education","Skills","Recommendations","Languages","Interests","Volunteer experience","Featured","Honors & awards","Honors and awards","Projects","Publications","Patents","Certifications","Licenses & certifications","Courses","Test scores","Organizations"]),ee=new Set(["Ad Options","Don\u2019t want to see this","0 notifications","Explore Premium profiles","You might like","People you may know","Suggested for you","More profiles for you","Promoted"]),q=p=>{let w=(p||"").trim().replace(/^[·•]\s*/,"");return/^(1st|2nd|3rd|she\/her|he\/him|they\/them|their\/them)$/i.test(w)},W={name:null,title:null,location:null,currentCompany:null,currentSchool:null,connectionDegree:null,hasVerifiedBadge:!1,photoUrl:null,connectionsLevel:null,mutualConnectionsCount:null,followers:null,openToWork:!1,hiring:!1,about:null,topSkills:null,availableSections:[]},T=document.querySelector("main");if(!T)return W;let ve=[...Array.from(T.querySelectorAll("h1")),...Array.from(T.querySelectorAll("h2"))],b=Array.from(T.querySelectorAll("h2")),I=ve.find(p=>{let w=p.textContent&&p.textContent.trim(),S=(w||"").replace(/\s*\(\d+\)$/,"").trim();return w&&w.length>1&&w.length<100&&!x.has(S)&&!ee.has(S)}),te=I?I.textContent.trim():null,D=I?I.parentElement:null;for(;D&&D!==T&&!Array.from(D.children).filter(w=>w.tagName==="P").some(w=>{let S=w.textContent&&w.textContent.trim();return S&&S.length>5&&!q(S)});)D=D.parentElement;let me=null,Ve=null,He=null,ke=null;if(D&&D!==T){let p=Array.from(D.children).filter(S=>S.tagName==="P").filter(S=>{let V=S.textContent&&S.textContent.trim();return V&&V.length>5&&!q(V)});if(p[0]&&(me=p[0].textContent.trim()),p[1]){let S=p[1].textContent.trim().split(/\s*[·•]\s*/);Ve=S[0]||null,S.length>1&&(He=S.slice(1).join(" \xB7 "))}let w=Array.from(D.children).filter(S=>S.tagName==="DIV");for(let S=w.length-1;S>=0;S--){let V=w[S].textContent&&w[S].textContent.trim();if(V&&!(te&&V.startsWith(te))&&(ke=V.split(/[·•]/)[0].trim(),ke))break}}let Je=null;if(D){let p=D.querySelectorAll("p, span, div");for(let w of p){let S=(w.textContent||"").trim().replace(/^[·•]\s*/,"");if(S==="1st"||S==="2nd"||S==="3rd"){Je=S;break}}}let Vt=!!T.querySelector('svg[data-test-icon="verified"], [aria-label*="Verified" i]'),We=document.querySelector('main img[width="200"], button img[alt*="photo" i]'),Ht=We&&We.src||null,pe=T.textContent||"",Ye=null,Ge=pe.match(/(\d+\+?)\s+connections\b/i);Ge&&(Ye=Ge[1]+" connections");let he=null,Ke=pe.match(/(\d+)\s+mutual connection/i),ze=pe.match(/and\s+(\d+)\s+other mutual/i),Jt=pe.match(/([A-Z][\w'-]+(?:\s+[A-Z][\w'-]+)?)\s+and\s+([A-Z][\w'-]+(?:\s+[A-Z][\w'-]+)?)\s+are\s+mutual/);Ke?he=parseInt(Ke[1],10):ze?he=2+parseInt(ze[1],10):Jt&&(he=2);let Qe=null,Ze=null,Xe=b.find(p=>p.textContent&&p.textContent.trim()==="About");if(Xe){let p=Xe.parentElement;for(;p&&p.tagName!=="SECTION";)p=p.parentElement;if(p){let w=(p.textContent||"").trim();w=w.replace(/^About\s*/,"").trim();let S=w.match(/Top skills\s*(.+?)$/i);if(S){let Ce=S[1].split(/\b(?:see (?:more|less)|show (?:more|less))\b/i)[0].trim().split(/\s*[·•]\s*/).map(Kt=>Kt.trim()).filter(Boolean);Ce.length>0&&Ce.length<10&&(Ze=Ce)}w=w.split(/\n?…\s*more/i)[0].trim(),w=w.split(/\n?\.\.\.\s*more/i)[0].trim(),w=w.split(/\bTop skills\b/i)[0].trim(),w=w.split(/\b(?:see (?:more|less)|show (?:more|less))\b/i)[0].trim(),Qe=w||null}}let et=null,tt=b.find(p=>p.textContent&&p.textContent.trim()==="Activity");if(tt){let p=tt.parentElement;for(;p&&p.tagName!=="SECTION";)p=p.parentElement;if(p){let w=(p.textContent||"").slice(0,200).match(/([\d,]+)\s+followers/i);w&&(et=parseInt(w[1].replace(/,/g,""),10))}}let Wt=[...new Set(b.map(p=>(p.textContent||"").trim().replace(/\s*\(\d+\)$/,"").trim()).filter(p=>p&&p.length>1&&p.length<60&&p!==te).filter(p=>x.has(p)))],re=document.body.textContent||"",Yt=/#OpenToWork\b/i.test(re)||/\bOpen to work\b/.test(re),Gt=/#Hiring\b/i.test(re)||/\bI'?m hiring\b/i.test(re)||/\bWe are hiring\b/i.test(re);return{name:te,title:me,location:ke,currentCompany:Ve,currentSchool:He,connectionDegree:Je,hasVerifiedBadge:Vt,photoUrl:Ht,connectionsLevel:Ye,mutualConnectionsCount:he,followers:et,openToWork:Yt,hiring:Gt,about:Qe,topSkills:Ze,availableSections:Wt}});console.log(`Viewed: ${y.name} - ${y.title}`);let u=null,f=null,C=null,v=null,_=null,h=null;if(o||r||s||i||c||a){let x=await Promise.resolve().then(()=>(yt(),gt));o&&(u=await x.extractExperienceFromDetails(e,t),console.log(` + Experience: ${u.length} entries`)),r&&(f=await x.extractFeaturedFromDetails(e,t),console.log(` + Featured: ${f.length} entries`)),s&&(C=await x.extractEducationFromDetails(e,t),console.log(` + Education: ${C.length} entries`)),i&&(v=await x.extractSkillsFromDetails(e,t),console.log(` + Skills: ${v.length} entries`)),c&&(_=await x.extractRecommendationsFromDetails(e,t),console.log(` + Recommendations: ${_.length} entries`)),a&&(h=await x.extractCertificationsFromDetails(e,t),console.log(` + Certifications: ${h.length} entries`))}return{...y,profileUrl:t,...u!==null&&{experience:u},...f!==null&&{featured:f},...C!==null&&{education:C},...v!==null&&{skillsFull:v},..._!==null&&{recommendations:_},...h!==null&&{certifications:h}}}catch(m){console.error(`Error viewing profile ${t}:`,m.message);try{await e.goto("about:blank",{timeout:1e4}),await $(1e3)}catch{}if(/session expired|bounced to|authwall|checkpoint|redirected to login/i.test(m.message||""))throw m;return null}}import"dotenv/config";import*as O from"fs";import*as j from"path";import{fileURLToPath as Rn}from"url";var jn=Rn(import.meta.url),On=j.dirname(j.dirname(jn)),K=process.env.DATA_DIR?j.resolve(process.env.DATA_DIR,"market-research"):j.join(On,"market-research"),Nn=j.join(K,"scouted-jobs.json"),Ln=j.join(K,"discovered-people.json"),bt=j.join(K,"visit-log.json"),Cr=j.join(K,"companies.json"),xr=j.join(K,"job-decisions.json");function Fn(){O.existsSync(K)||O.mkdirSync(K,{recursive:!0})}function Mn(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";O.writeFileSync(o,n);for(let r=0;r<3;r++)try{O.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function _e(e){try{if(O.existsSync(e))return JSON.parse(O.readFileSync(e,"utf-8"))}catch{}return null}function St(){ue=null,qn=null,de=null,Bn=null,G=null}var ue=null;function Pn(){return ue||(ue=_e(Nn)||{},ue)}var qn=null;var de=null;function ye(){return de||(de=_e(Ln)||{},de)}var Bn=null;var G=null;function oe(){return G||(G=_e(bt)||{visits:[]},Array.isArray(G.visits)||(G.visits=[]),G)}function vt(e,t={}){if(!e)return;Fn();let n=oe();n.visits.push({date:new Date().toISOString(),profileUrl:e,name:t.name||null,source:t.source||"manual"});let o=Date.now()-90*24*60*60*1e3;n.visits=n.visits.filter(r=>new Date(r.date).getTime()>o);try{Mn(bt,n)}catch(r){console.error(`[!] Failed to save visit-log.json: ${r.message}`)}}function kt(e){let t=oe();for(let n=t.visits.length-1;n>=0;n--)if(t.visits[n].profileUrl===e)return t.visits[n].date;return null}function Ct(){let e=Pn(),t=ye(),n=Object.values(e),o=Object.values(t),r={};for(let u of n)u.company&&(r[u.company]=(r[u.company]||0)+1);let s=Object.entries(r).sort((u,f)=>f[1]-u[1]).slice(0,5).map(([u,f])=>`${u} (${f})`),i={};for(let u of n)for(let f of u.techStack||[])i[f]=(i[f]||0)+1;let c=Object.entries(i).sort((u,f)=>f[1]-u[1]).slice(0,8).map(([u,f])=>`${u} (${f})`),a=oe(),m=new Date().toISOString().slice(0,10),l=a.visits.filter(u=>u.date.startsWith(m)).length,y=new Set(a.visits.map(u=>u.profileUrl)).size;return{totalJobs:n.length,totalRecruiters:o.length,topCompanies:s,topTech:c,totalVisited:y,todayVisited:l,lastScoutDate:n.length>0?n.sort((u,f)=>(f.scoutedAt||"").localeCompare(u.scoutedAt||""))[0]?.scoutedAt:null}}se();async function xt(e,t,n={}){let{maxVisits:o=50,delayBetween:r=4e3,skipRecentDays:s=7,verbose:i=!1,extractExperience:c=!1}=n,a={visited:0,skipped:0,errors:0},m=Date.now()-s*24*60*60*1e3;for(let l of t){if(a.visited>=o)break;if(!l.profileUrl)continue;let y=kt(l.profileUrl);if(y&&new Date(y).getTime()>m){a.skipped++;continue}try{let u=await wt(e,l.profileUrl,{extractExperience:c}),f=l.source||"visit";if(u){let v={...u,source:f};c&&Array.isArray(u.experience)&&u.experience.length>0&&(v._lastEnrichedAt=new Date().toISOString()),Ae(l.profileUrl,v)}vt(l.profileUrl,{name:u?.name||l.name||null,source:f}),a.visited++,i?console.log(` [${a.visited}] ${u?.name||l.profileUrl} \u2014 ${u?.title?.slice(0,40)||"?"}`):a.visited%10===0&&console.log(` Visited: ${a.visited}`);let C=Math.random()*4e3;await $(r+C)}catch(u){a.errors++,i&&console.error(` Error visiting ${l.name||l.profileUrl}: ${u.message?.slice(0,60)}`);let f=(u.message||"").toLowerCase();if(f.includes("detached")||f.includes("session closed")||f.includes("target closed")||f.includes("session expired")||f.includes("bounced to")||f.includes("authwall")||f.includes("checkpoint")||f.includes("redirected to login")){console.log(" Fatal browser/session error \u2014 stopping visits");break}}}return a}function $t(e={}){let{limit:t=150,revisitDays:n=14}=e,o=oe(),r=new Set(o.visits?.map(h=>h.profileUrl)||[]),s=new Map;for(let h of o.visits||[]){let g=s.get(h.profileUrl);(!g||h.date>g)&&s.set(h.profileUrl,h.date)}let i=Date.now()-7*24*60*60*1e3,c=Date.now()-n*24*60*60*1e3;function a(h){let g=s.get(h);return g&&new Date(g).getTime()>i}let m=ye(),l=Object.entries(m).filter(([h])=>!r.has(h)).map(([h,g])=>({profileUrl:h,name:g.name,source:"new-discovered",priority:1,jobCount:g.jobIds?.length||0})),y=ie(),u=Object.entries(y).filter(([h,g])=>g.connectionStatus==="connected"||a(h)?!1:(g.jobCount||g.jobIds?.length||0)>=2).map(([h,g])=>({profileUrl:h,name:g.name,source:"active-recruiter",priority:2,jobCount:g.jobCount||g.jobIds?.length||0})),f=Object.entries(y).filter(([h,g])=>{let x=g.connectionStatus;return!(!x||x==="connected"||x==="pending"||x==="sent_without_note"||x==="sent_with_note"||a(h)||(g.jobCount||g.jobIds?.length||0)>=2)}).map(([h,g])=>({profileUrl:h,name:g.name,source:"unconnected",priority:3,jobCount:g.jobCount||g.jobIds?.length||0})),C=[...s.entries()].filter(([h,g])=>new Date(g).getTime()<c).map(([h,g])=>({profileUrl:h,name:null,source:"revisit",priority:4,lastVisit:g})),v=h=>h.sort(()=>Math.random()-.5);return l.sort((h,g)=>g.jobCount-h.jobCount),u.sort((h,g)=>g.jobCount-h.jobCount),v(f),v(C),[...l,...u,...f,...C].slice(0,t)}function Et(){let e=ye();return Object.entries(e).map(([t,n])=>({profileUrl:t,name:n.name,source:"discovered"}))}function Ie(){let e=ie();return Object.entries(e).filter(([,t])=>{let n=t.connectionStatus;return n&&n!=="connected"&&n!=="pending"&&n!=="sent_without_note"&&n!=="sent_with_note"}).map(([t,n])=>({profileUrl:t,name:n.name,source:"unconnected"}))}function Te(e=14){let t=oe(),n=Date.now()-e*24*60*60*1e3,o=new Map;for(let r of t.visits){let s=o.get(r.profileUrl);(!s||r.date>s.date)&&o.set(r.profileUrl,r)}return[...o.entries()].filter(([,r])=>new Date(r.date).getTime()<n).map(([r,s])=>({profileUrl:r,name:s.name,source:"revisit"}))}import"dotenv/config";import*as N from"fs";import*as H from"path";import{fileURLToPath as Yn}from"url";import"dotenv/config";import{createGraphClient as Un,resolveToken as Vn}from"@ilivemylife/graph-sdk";import{config as Hn}from"dotenv";Hn();var At=Vn()||process.env.TOKEN;At||(console.error(`
242
242
  [FATAL] No iLiveMyLife token found.`),console.error(" The bot uses Lifebot AI to decide which jobs to apply for"),console.error(` and how to fill application forms. It cannot work without it.
243
243
  `),console.error(" Easiest way:"),console.error(" 1. npm install -g @ilivemylife/graph-sdk"),console.error(` 2. ilml login your@email.com yourpassword
244
244
  `),console.error(` Or add ILML_TOKEN=... to .env
245
- `),process.exit(1));var At=Bn({token:Et});function Hn(e){if(!e||e<1e3)return`${e||0}ms`;let t=Math.round(e/1e3);if(t<60)return`${t}s`;let n=Math.floor(t/60),o=t%60;return o?`${n}m ${o}s`:`${n}m`}function Jn({scriptName:e,mode:t,status:n,error:o,duration:r}){let s=n==="error"?"\u2717":n==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),c=r?` ${Hn(r)}`:"",a=n==="error"?`error${o?`: ${String(o).slice(0,80)}`:""}`:n==="quota-reached"?"daily quota reached":"done",m=t&&t!=="default"?` (${t})`:"";return`${s} ${e}${m} \u2014 ${a} \u2014 ${i}${c}`}async function _t({scriptName:e,mode:t,status:n,summaryLines:o,error:r,duration:s}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(o)||o.length===0)return;let a=`**${Jn({scriptName:e,mode:t,status:n,error:r,duration:s})}**
245
+ `),process.exit(1));var _t=Un({token:At});function Jn(e){if(!e||e<1e3)return`${e||0}ms`;let t=Math.round(e/1e3);if(t<60)return`${t}s`;let n=Math.floor(t/60),o=t%60;return o?`${n}m ${o}s`:`${n}m`}function Wn({scriptName:e,mode:t,status:n,error:o,duration:r}){let s=n==="error"?"\u2717":n==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),c=r?` ${Jn(r)}`:"",a=n==="error"?`error${o?`: ${String(o).slice(0,80)}`:""}`:n==="quota-reached"?"daily quota reached":"done",m=t&&t!=="default"?` (${t})`:"";return`${s} ${e}${m} \u2014 ${a} \u2014 ${i}${c}`}async function It({scriptName:e,mode:t,status:n,summaryLines:o,error:r,duration:s}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(o)||o.length===0)return;let a=`**${Wn({scriptName:e,mode:t,status:n,error:r,duration:s})}**
246
246
 
247
247
  ${o.join(`
248
- `)}`;try{await At.addMessage(i,a)}catch(m){console.error(`[runReport] Failed to post end-of-session message: ${m.message}`)}}var Yn=Wn(import.meta.url),Gn=H.dirname(H.dirname(Yn)),De=process.env.DATA_DIR?H.resolve(process.env.DATA_DIR,"collected-profiles"):H.join(Gn,"collected-profiles"),Re=H.join(De,"run-log.json");function Kn(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";N.writeFileSync(o,n);for(let r=0;r<3;r++)try{N.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function It(){try{if(!N.existsSync(Re))return{runs:[]};let e=JSON.parse(N.readFileSync(Re,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function Tt(e){N.existsSync(De)||N.mkdirSync(De,{recursive:!0});try{Kn(Re,e)}catch(t){console.error(`[!] Failed to save run-log.json: ${t.message}`)}}function Dt(e,t="default"){let n=It();for(let i of n.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let o=new Date,r={timestamp:o.toISOString(),script:e,mode:t,status:"running",result:null,duration:null,stats:null,error:null};n.runs.push(r);let s=Date.now()-30*24*60*60*1e3;return n.runs=n.runs.filter(i=>new Date(i.timestamp).getTime()>s),Tt(n),{script:e,mode:t,startTime:o.getTime(),runIndex:n.runs.length-1}}function je(e,t,n=null,o=null){if(!e)return;let r=null;try{let i=It(),c=i.runs[e.runIndex];if(c&&c.status==="running"&&c.script===e.script)c.status=t,c.result=t,c.duration=Date.now()-e.startTime,c.stats=n,c.error=o,r=c.duration;else{let a=i.runs.find(m=>m.script===e.script&&m.status==="running"&&m.timestamp===new Date(e.startTime).toISOString());a&&(a.status=t,a.result=t,a.duration=Date.now()-e.startTime,a.stats=n,a.error=o,r=a.duration)}Tt(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let s=n&&Array.isArray(n.summaryLines)?n.summaryLines:null;if(s&&s.length>0&&process.env.NODE_RUN_REPORTS){let i=t==="error"?"error":n&&n.dailyLimitHit?"quota-reached":"done";_t({scriptName:e.script,mode:e.mode,status:i,summaryLines:s,error:o,duration:r??Date.now()-e.startTime}).catch(c=>console.error(`[runLog] postRunReport failed: ${c.message}`))}}import"dotenv/config";import*as d from"fs";import*as qe from"os";import*as k from"path";import{fileURLToPath as po}from"url";var Le={};ge(Le,{NAME:()=>Qn,VERSION:()=>zn,up:()=>eo});import*as L from"fs";import*as we from"path";var zn=1,Qn="normalize-jobs-and-questions",Zn=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],Xn=50;function Oe(e){return L.existsSync(e)?JSON.parse(L.readFileSync(e,"utf-8")):null}function Ne(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{L.writeFileSync(o,n)}catch(r){try{L.unlinkSync(o)}catch{}throw r}try{L.renameSync(o,e)}catch(r){try{L.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}async function eo(e,t){let n=we.join(e,"jobs.json"),o=we.join(t,"scouted-jobs.json"),r=we.join(e,"application-questions.json"),s=Oe(n)||{},i=Oe(o)||{},c=0,a=[];for(let l of Object.keys(s)){if(!i[l])continue;let y=i[l],u=s[l];for(let f of Zn)y[f]!=null&&u[f]==null&&(u[f]=y[f]);a.push(l),c++}c>0?(Ne(n,s),console.log(` Phase 1: Merged ${c} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let m=Oe(r);if(m&&Array.isArray(m.applications)){let l={},y=0;for(let u of m.applications){let f=u.jobId||"unknown";l[f]||(l[f]={applications:[]});let C=(u.questions||[]).map(v=>{let _={type:v.type||"input",question:v.question||"",answer:v.answer||""};return v.options&&(v.options.length<=Xn?_.options=v.options:(_.optionsOmitted=!0,y++)),v.page&&(_.page=v.page),v.wasPreFilled&&(_.wasPreFilled=!0),v.wasRetry&&(_.wasRetry=!0),_});l[f].applications.push({applicationId:u.applicationId,date:u.date,result:u.result||"submitted",formPages:u.formPages||null,duration:u.duration||null,retries:u.retries||0,failedField:u.failedField||null,questionCount:u.questionCount||C.length,questions:C})}Ne(r,l),console.log(` Phase 2: Rekeyed ${m.applications.length} applications by jobId (${Object.keys(l).length} unique jobs)`),y>0&&console.log(` Phase 2: Stripped bloated options from ${y} questions`)}else m&&!m.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(a.length>0){for(let l of a)delete i[l];Ne(o,i),console.log(` Phase 3: Removed ${a.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var Fe={};ge(Fe,{NAME:()=>no,VERSION:()=>to,up:()=>so});import*as F from"fs";import*as Rt from"path";var to=2,no="reset-unread-flags";function oo(e){return F.existsSync(e)?JSON.parse(F.readFileSync(e,"utf-8")):null}function ro(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{F.writeFileSync(o,n)}catch(r){try{F.unlinkSync(o)}catch{}throw r}try{F.renameSync(o,e)}catch(r){try{F.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}async function so(e){let t=Rt.join(e,"conversations.json"),n=oo(t);if(!n){console.log(" conversations.json not found \u2014 nothing to reset.");return}let o=0;for(let r of Object.keys(n))n[r].unread===!0&&(n[r].unread=!1,o++);if(o===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}ro(t,n),console.log(` Reset unread=false on ${o} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var Me={};ge(Me,{NAME:()=>lo,VERSION:()=>io,up:()=>mo});import*as M from"fs";import*as jt from"path";var io=3,lo="recompute-last-message-by",ao=new Set(["draft","confirmed","rejected","needs_review"]);function co(e){return M.existsSync(e)?JSON.parse(M.readFileSync(e,"utf-8")):null}function uo(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{M.writeFileSync(o,n)}catch(r){try{M.unlinkSync(o)}catch{}throw r}try{M.renameSync(o,e)}catch(r){try{M.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}function fo(e,t){if(!e||e.length===0)return"them";let n=e.filter(s=>!ao.has(s.status));if(n.length===0)return"them";let o=n.map(s=>({m:s,t:Date.parse(s.time)})).filter(s=>Number.isFinite(s.t)).sort((s,i)=>i.t-s.t).map(s=>s.m),r=o.length>0?o:[...n].reverse();for(let s of r){let i=s.sender;if(i===t)return"me";if(i!=="Unknown"&&i!=="")return"them"}return"them"}async function mo(e){let t=jt.join(e,"conversations.json"),n=co(t);if(!n){console.log(" conversations.json not found \u2014 nothing to recompute.");return}let o=process.env.LINKEDIN_NAME;if(!o||typeof o!="string"||!o.trim())throw new Error(`LINKEDIN_NAME env var is not set. Migration v003 needs your display name to determine which messages are yours vs theirs. Set LINKEDIN_NAME in your .env (or run "ilml plugin config linkedin set LINKEDIN_NAME 'Your Name'") and retry.`);let r=0,s=0,i=0;for(let c of Object.keys(n)){let a=n[c];if(!a||typeof a!="object")continue;Object.prototype.hasOwnProperty.call(a,"_cardSynced")&&(delete a._cardSynced,s++);let m=Array.isArray(a.messages)?a.messages:[],l=fo(m,o),y=l==="me"?"awaiting_reply":"unanswered_by_us";a.lastMessageBy!==l&&i++,a.lastMessageBy=l,a.conversationStatus=y,r++}if(r===0){console.log(" No conversations to recompute.");return}uo(t,n),console.log(` Recomputed lastMessageBy + conversationStatus on ${r} conversations.`),console.log(` ${i} had stale lastMessageBy (now corrected).`),s>0&&console.log(` Cleaned ${s} leftover _cardSynced flags.`),console.log(" Re-run 'ilml linkedin enrich' if you want actionNeeded / summary / autoTags to also update.")}var ho=po(import.meta.url),go=k.dirname(ho),yo=k.dirname(k.dirname(go));function wo(e){return e&&(e==="~"?qe.homedir():e.startsWith("~/")||e.startsWith("~\\")?k.join(qe.homedir(),e.slice(2)):e)}var P=process.env.DATA_DIR?k.resolve(wo(process.env.DATA_DIR)):yo,Q=k.join(P,"collected-profiles"),Z=k.join(P,"market-research"),R=k.join(P,".schema-version"),Ot=[k.join(Q,".schema-version"),k.join(Z,".schema-version")],J=3,U=[{version:1,module:Le},{version:2,module:Fe},{version:3,module:Me}];(function(){U.sort((o,r)=>o.version-r.version);let t=new Set;for(let o of U){if(!Number.isInteger(o.version)||o.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(o.version)} (must be positive integer)`);if(t.has(o.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${o.version}`);t.add(o.version);let r=o.module;if(!r||typeof r.up!="function")throw new Error(`MIGRATION_REGISTRY: v${o.version} module is missing up()`);if(r.VERSION!==void 0&&r.VERSION!==o.version)throw new Error(`MIGRATION_REGISTRY: v${o.version} entry does not match module VERSION (${r.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let n=U.length?U[U.length-1].version:0;if(n!==J)throw new Error(`MIGRATION_REGISTRY: highest version v${n} but CURRENT_SCHEMA_VERSION=${J}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let o=0;o<U.length;o++){let r=o+1;if(U[o].version!==r)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${r} at position ${o} but found v${U[o].version}`)}})();function Mt(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
248
+ `)}`;try{await _t.addMessage(i,a)}catch(m){console.error(`[runReport] Failed to post end-of-session message: ${m.message}`)}}var Gn=Yn(import.meta.url),Kn=H.dirname(H.dirname(Gn)),De=process.env.DATA_DIR?H.resolve(process.env.DATA_DIR,"collected-profiles"):H.join(Kn,"collected-profiles"),Re=H.join(De,"run-log.json");function zn(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";N.writeFileSync(o,n);for(let r=0;r<3;r++)try{N.renameSync(o,e);return}catch(s){if(r<2&&(s.code==="EPERM"||s.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw s}}function Tt(){try{if(!N.existsSync(Re))return{runs:[]};let e=JSON.parse(N.readFileSync(Re,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function Dt(e){N.existsSync(De)||N.mkdirSync(De,{recursive:!0});try{zn(Re,e)}catch(t){console.error(`[!] Failed to save run-log.json: ${t.message}`)}}function Rt(e,t="default"){let n=Tt();for(let i of n.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let o=new Date,r={timestamp:o.toISOString(),script:e,mode:t,status:"running",result:null,duration:null,stats:null,error:null};n.runs.push(r);let s=Date.now()-30*24*60*60*1e3;return n.runs=n.runs.filter(i=>new Date(i.timestamp).getTime()>s),Dt(n),{script:e,mode:t,startTime:o.getTime(),runIndex:n.runs.length-1}}function je(e,t,n=null,o=null){if(!e)return;let r=null;try{let i=Tt(),c=i.runs[e.runIndex];if(c&&c.status==="running"&&c.script===e.script)c.status=t,c.result=t,c.duration=Date.now()-e.startTime,c.stats=n,c.error=o,r=c.duration;else{let a=i.runs.find(m=>m.script===e.script&&m.status==="running"&&m.timestamp===new Date(e.startTime).toISOString());a&&(a.status=t,a.result=t,a.duration=Date.now()-e.startTime,a.stats=n,a.error=o,r=a.duration)}Dt(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let s=n&&Array.isArray(n.summaryLines)?n.summaryLines:null;if(s&&s.length>0&&process.env.NODE_RUN_REPORTS){let i=t==="error"?"error":n&&n.dailyLimitHit?"quota-reached":"done";It({scriptName:e.script,mode:e.mode,status:i,summaryLines:s,error:o,duration:r??Date.now()-e.startTime}).catch(c=>console.error(`[runLog] postRunReport failed: ${c.message}`))}}import"dotenv/config";import*as d from"fs";import*as qe from"os";import*as k from"path";import{fileURLToPath as ho}from"url";var Le={};ge(Le,{NAME:()=>Zn,VERSION:()=>Qn,up:()=>to});import*as L from"fs";import*as we from"path";var Qn=1,Zn="normalize-jobs-and-questions",Xn=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],eo=50;function Oe(e){return L.existsSync(e)?JSON.parse(L.readFileSync(e,"utf-8")):null}function Ne(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{L.writeFileSync(o,n)}catch(r){try{L.unlinkSync(o)}catch{}throw r}try{L.renameSync(o,e)}catch(r){try{L.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}async function to(e,t){let n=we.join(e,"jobs.json"),o=we.join(t,"scouted-jobs.json"),r=we.join(e,"application-questions.json"),s=Oe(n)||{},i=Oe(o)||{},c=0,a=[];for(let l of Object.keys(s)){if(!i[l])continue;let y=i[l],u=s[l];for(let f of Xn)y[f]!=null&&u[f]==null&&(u[f]=y[f]);a.push(l),c++}c>0?(Ne(n,s),console.log(` Phase 1: Merged ${c} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let m=Oe(r);if(m&&Array.isArray(m.applications)){let l={},y=0;for(let u of m.applications){let f=u.jobId||"unknown";l[f]||(l[f]={applications:[]});let C=(u.questions||[]).map(v=>{let _={type:v.type||"input",question:v.question||"",answer:v.answer||""};return v.options&&(v.options.length<=eo?_.options=v.options:(_.optionsOmitted=!0,y++)),v.page&&(_.page=v.page),v.wasPreFilled&&(_.wasPreFilled=!0),v.wasRetry&&(_.wasRetry=!0),_});l[f].applications.push({applicationId:u.applicationId,date:u.date,result:u.result||"submitted",formPages:u.formPages||null,duration:u.duration||null,retries:u.retries||0,failedField:u.failedField||null,questionCount:u.questionCount||C.length,questions:C})}Ne(r,l),console.log(` Phase 2: Rekeyed ${m.applications.length} applications by jobId (${Object.keys(l).length} unique jobs)`),y>0&&console.log(` Phase 2: Stripped bloated options from ${y} questions`)}else m&&!m.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(a.length>0){for(let l of a)delete i[l];Ne(o,i),console.log(` Phase 3: Removed ${a.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var Fe={};ge(Fe,{NAME:()=>oo,VERSION:()=>no,up:()=>io});import*as F from"fs";import*as jt from"path";var no=2,oo="reset-unread-flags";function ro(e){return F.existsSync(e)?JSON.parse(F.readFileSync(e,"utf-8")):null}function so(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{F.writeFileSync(o,n)}catch(r){try{F.unlinkSync(o)}catch{}throw r}try{F.renameSync(o,e)}catch(r){try{F.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}async function io(e){let t=jt.join(e,"conversations.json"),n=ro(t);if(!n){console.log(" conversations.json not found \u2014 nothing to reset.");return}let o=0;for(let r of Object.keys(n))n[r].unread===!0&&(n[r].unread=!1,o++);if(o===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}so(t,n),console.log(` Reset unread=false on ${o} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var Me={};ge(Me,{NAME:()=>ao,VERSION:()=>lo,up:()=>po});import*as M from"fs";import*as Ot from"path";var lo=3,ao="recompute-last-message-by",co=new Set(["draft","confirmed","rejected","needs_review"]);function uo(e){return M.existsSync(e)?JSON.parse(M.readFileSync(e,"utf-8")):null}function fo(e,t){let n=JSON.stringify(t,null,2),o=e+".tmp";try{M.writeFileSync(o,n)}catch(r){try{M.unlinkSync(o)}catch{}throw r}try{M.renameSync(o,e)}catch(r){try{M.unlinkSync(o)}catch{}if(r.code==="EPERM"||r.code==="EBUSY"){let s=new Error(`File locked: ${e} (${r.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw s.code=r.code,s}throw r}}function mo(e,t){if(!e||e.length===0)return"them";let n=e.filter(s=>!co.has(s.status));if(n.length===0)return"them";let o=n.map(s=>({m:s,t:Date.parse(s.time)})).filter(s=>Number.isFinite(s.t)).sort((s,i)=>i.t-s.t).map(s=>s.m),r=o.length>0?o:[...n].reverse();for(let s of r){let i=s.sender;if(i===t)return"me";if(i!=="Unknown"&&i!=="")return"them"}return"them"}async function po(e){let t=Ot.join(e,"conversations.json"),n=uo(t);if(!n){console.log(" conversations.json not found \u2014 nothing to recompute.");return}let o=process.env.LINKEDIN_NAME;if(!o||typeof o!="string"||!o.trim())throw new Error(`LINKEDIN_NAME env var is not set. Migration v003 needs your display name to determine which messages are yours vs theirs. Set LINKEDIN_NAME in your .env (or run "ilml plugin config linkedin set LINKEDIN_NAME 'Your Name'") and retry.`);let r=0,s=0,i=0;for(let c of Object.keys(n)){let a=n[c];if(!a||typeof a!="object")continue;Object.prototype.hasOwnProperty.call(a,"_cardSynced")&&(delete a._cardSynced,s++);let m=Array.isArray(a.messages)?a.messages:[],l=mo(m,o),y=l==="me"?"awaiting_reply":"unanswered_by_us";a.lastMessageBy!==l&&i++,a.lastMessageBy=l,a.conversationStatus=y,r++}if(r===0){console.log(" No conversations to recompute.");return}fo(t,n),console.log(` Recomputed lastMessageBy + conversationStatus on ${r} conversations.`),console.log(` ${i} had stale lastMessageBy (now corrected).`),s>0&&console.log(` Cleaned ${s} leftover _cardSynced flags.`),console.log(" Re-run 'ilml linkedin enrich' if you want actionNeeded / summary / autoTags to also update.")}var go=ho(import.meta.url),yo=k.dirname(go),wo=k.dirname(k.dirname(yo));function bo(e){return e&&(e==="~"?qe.homedir():e.startsWith("~/")||e.startsWith("~\\")?k.join(qe.homedir(),e.slice(2)):e)}var P=process.env.DATA_DIR?k.resolve(bo(process.env.DATA_DIR)):wo,Q=k.join(P,"collected-profiles"),Z=k.join(P,"market-research"),R=k.join(P,".schema-version"),Nt=[k.join(Q,".schema-version"),k.join(Z,".schema-version")],J=3,U=[{version:1,module:Le},{version:2,module:Fe},{version:3,module:Me}];(function(){U.sort((o,r)=>o.version-r.version);let t=new Set;for(let o of U){if(!Number.isInteger(o.version)||o.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(o.version)} (must be positive integer)`);if(t.has(o.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${o.version}`);t.add(o.version);let r=o.module;if(!r||typeof r.up!="function")throw new Error(`MIGRATION_REGISTRY: v${o.version} module is missing up()`);if(r.VERSION!==void 0&&r.VERSION!==o.version)throw new Error(`MIGRATION_REGISTRY: v${o.version} entry does not match module VERSION (${r.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let n=U.length?U[U.length-1].version:0;if(n!==J)throw new Error(`MIGRATION_REGISTRY: highest version v${n} but CURRENT_SCHEMA_VERSION=${J}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let o=0;o<U.length;o++){let r=o+1;if(U[o].version!==r)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${r} at position ${o} but found v${U[o].version}`)}})();function Pt(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
249
249
  [FATAL] DATA_DIR is set to whitespace ("${process.env.DATA_DIR}"). Either unset it or set a real path.
250
250
  `),process.exit(1));try{d.mkdirSync(P,{recursive:!0})}catch(t){t.code!=="EEXIST"&&(console.error(`
251
251
  [FATAL] Cannot create DATA_DIR (${P}): ${t.message}`),console.error(` Fix the path or permissions and try again.
@@ -255,43 +255,43 @@ ${o.join(`
255
255
  [FATAL] DATA_DIR (${P}) exists but is NOT a directory.`),console.error(` Looks like a typo \u2014 your DATA_DIR points at a file. Fix the path and try again.
256
256
  `),process.exit(1));for(let t of[Q,Z]){let n;try{n=d.lstatSync(t)}catch(o){if(o.code==="ENOENT")continue;throw o}n.isSymbolicLink()&&(console.error(`
257
257
  [FATAL] Data subdirectory is a symbolic link: ${t}`),console.error(" Refusing to follow \u2014 backup code would copy whatever the link points at."),console.error(` If this is intentional, replace the symlink with the real directory.
258
- `),process.exit(1))}}var z=k.join(P,".migration-lock");function bo(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return d.writeFileSync(z,e,{flag:"wx"}),!0}catch(o){if(o.code!=="EEXIST")throw o}let t;try{t=JSON.parse(d.readFileSync(z,"utf-8"))}catch{t=null}if(t&&Number.isInteger(t.pid)){let o=!1;try{process.kill(t.pid,0),o=!0}catch(i){i.code==="EPERM"&&(o=!0)}let r=24*60*60*1e3,s=1/0;if(t.startedAt){let i=Date.parse(t.startedAt);Number.isNaN(i)||(s=Date.now()-i)}o&&s<r&&(console.error(`
258
+ `),process.exit(1))}}var z=k.join(P,".migration-lock");function So(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return d.writeFileSync(z,e,{flag:"wx"}),!0}catch(o){if(o.code!=="EEXIST")throw o}let t;try{t=JSON.parse(d.readFileSync(z,"utf-8"))}catch{t=null}if(t&&Number.isInteger(t.pid)){let o=!1;try{process.kill(t.pid,0),o=!0}catch(i){i.code==="EPERM"&&(o=!0)}let r=24*60*60*1e3,s=1/0;if(t.startedAt){let i=Date.parse(t.startedAt);Number.isNaN(i)||(s=Date.now()-i)}o&&s<r&&(console.error(`
259
259
  [FATAL] Another plugin process is currently running migrations:`),console.error(` PID: ${t.pid}`),console.error(` Started at: ${t.startedAt||"(unknown)"}`),console.error(` Lock file: ${z}
260
260
  `),console.error(" Wait for it to finish, or if you're sure it's not actually running,"),console.error(` delete the lock file manually and re-run.
261
- `),process.exit(1)),o&&s>=r&&console.log(` Lock holder PID ${t.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${t?.pid??"?"} found \u2014 taking over.`);let n=z+".tmp";try{return d.writeFileSync(n,e),d.renameSync(n,z),!0}catch(o){try{d.unlinkSync(n)}catch{}throw new Error(`Failed to take over stale migration lock: ${o.message}`)}}function Be(){try{if(JSON.parse(d.readFileSync(z,"utf-8")).pid!==process.pid)return;d.unlinkSync(z)}catch{}}var Nt=!1;function So(){if(Nt)return;Nt=!0;let e=()=>{try{Be()}catch{}process.exit(1)};for(let t of["SIGINT","SIGTERM","SIGHUP"])try{process.on(t,e)}catch{}}function vo(){if(d.existsSync(R))return;let e=[],t=[];for(let s of Ot)if(d.existsSync(s))try{e.push({file:s,schema:be(s)})}catch(i){t.push({file:s,err:i.message})}if(t.length>0){console.error(`
261
+ `),process.exit(1)),o&&s>=r&&console.log(` Lock holder PID ${t.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${t?.pid??"?"} found \u2014 taking over.`);let n=z+".tmp";try{return d.writeFileSync(n,e),d.renameSync(n,z),!0}catch(o){try{d.unlinkSync(n)}catch{}throw new Error(`Failed to take over stale migration lock: ${o.message}`)}}function Be(){try{if(JSON.parse(d.readFileSync(z,"utf-8")).pid!==process.pid)return;d.unlinkSync(z)}catch{}}var Lt=!1;function vo(){if(Lt)return;Lt=!0;let e=()=>{try{Be()}catch{}process.exit(1)};for(let t of["SIGINT","SIGTERM","SIGHUP"])try{process.on(t,e)}catch{}}function ko(){if(d.existsSync(R))return;let e=[],t=[];for(let s of Nt)if(d.existsSync(s))try{e.push({file:s,schema:be(s)})}catch(i){t.push({file:s,err:i.message})}if(t.length>0){console.error(`
262
262
  [FATAL] Legacy .schema-version file(s) corrupted \u2014 cannot determine current schema version:`);for(let s of t)console.error(` ${s.file}: ${s.err}`);console.error(`
263
263
  Repair the file(s) (likely just an integer 'version' field \u2014 see another working DATA_DIR`),console.error(` or default to {"version":1,"migrations":[]} if you're sure you're on schema v1) and try again.
264
264
  `),process.exit(1)}if(e.length===0)return;let n=e.map(s=>s.schema.version);if(new Set(n).size>1){console.error(`
265
265
  [FATAL] Legacy .schema-version files disagree:`);for(let s of e)console.error(` ${s.file}: v${s.schema.version}`);console.error(" This means a previous migration crashed between writing the two files."),console.error(` Reconcile manually (delete the wrong one, keep the right one), then re-run.
266
- `),process.exit(1)}let o=e[0].schema,r=R+".tmp";try{d.writeFileSync(r,JSON.stringify(o,null,2)),d.renameSync(r,R)}catch(s){try{d.unlinkSync(r)}catch{}throw s}for(let s of Ot)if(d.existsSync(s))try{d.unlinkSync(s)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${R}`)}function ko(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let t=Number(e.version);return!Number.isFinite(t)||t<0||!Number.isInteger(t)?null:(e.version=t,Array.isArray(e.migrations)||(e.migrations=[]),e)}function be(e){let t=d.readFileSync(e,"utf-8");return t.charCodeAt(0)===65279&&(t=t.slice(1)),JSON.parse(t)}function Lt(){if(vo(),!d.existsSync(R))return{version:0,migrations:[]};let e;try{e=be(R)}catch(n){if(n.code==="ENOENT")return{version:0,migrations:[]};console.error(`
266
+ `),process.exit(1)}let o=e[0].schema,r=R+".tmp";try{d.writeFileSync(r,JSON.stringify(o,null,2)),d.renameSync(r,R)}catch(s){try{d.unlinkSync(r)}catch{}throw s}for(let s of Nt)if(d.existsSync(s))try{d.unlinkSync(s)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${R}`)}function Co(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let t=Number(e.version);return!Number.isFinite(t)||t<0||!Number.isInteger(t)?null:(e.version=t,Array.isArray(e.migrations)||(e.migrations=[]),e)}function be(e){let t=d.readFileSync(e,"utf-8");return t.charCodeAt(0)===65279&&(t=t.slice(1)),JSON.parse(t)}function Ft(){if(ko(),!d.existsSync(R))return{version:0,migrations:[]};let e;try{e=be(R)}catch(n){if(n.code==="ENOENT")return{version:0,migrations:[]};console.error(`
267
267
  [FATAL] ${R} is not valid JSON: ${n.message}`),console.error(" Refusing to proceed \u2014 silently treating it as 'fresh install' would re-apply"),console.error(" every migration on top of already-migrated data and corrupt it."),console.error(' Repair the file (set it to {"version":N,"migrations":[...]} matching your real state)'),console.error(` or restore from the *.pre-vNNN.backup files if you have them.
268
- `),process.exit(1)}let t=ko(e);return t===null&&(console.error(`
268
+ `),process.exit(1)}let t=Co(e);return t===null&&(console.error(`
269
269
  [FATAL] ${R} has invalid structure:`),console.error(` ${JSON.stringify(e)?.slice(0,200)||"(unparseable)"}`),console.error(' Expected shape: { "version": <integer>, "migrations": [...] }'),console.error(` Repair the file or restore from a backup.
270
- `),process.exit(1)),t}function Pt(e){try{d.chmodSync(e,384)}catch{}}function Co(e){if(Mt(),d.existsSync(R))try{if(d.lstatSync(R).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${R}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(n){if(n.message?.includes("symbolic link"))throw n}let t=R+".tmp";try{d.writeFileSync(t,JSON.stringify(e,null,2)),Pt(t),d.renameSync(t,R)}catch(n){try{d.unlinkSync(t)}catch{}throw n}}function Ft(e,t){if(!d.existsSync(e))return;let n=`.pre-v${String(t).padStart(3,"0")}.backup`,o=d.readdirSync(e).filter(c=>{let a=c.toLowerCase();if(a.includes(".backup")||a===".schema-version"||a.endsWith(".tmp"))return!1;try{return d.statSync(k.join(e,c)).isFile()}catch{return!1}}),r=0,s=0,i=[];for(let c of o){let a=k.join(e,c),m=k.join(e,c+n);if(d.existsSync(m)){s++;continue}try{Bt(a,m),r++}catch(l){i.push({file:c,err:l.message})}}if(i.length>0){let c=new Error(`Failed to back up ${i.length} file(s) in ${k.basename(e)}/: `+i.map(a=>`${a.file} (${a.err})`).join(", "));throw c.code="BACKUP_INCOMPLETE",c}if(r>0||s>0){let c=s>0?` (${s} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${r} files in ${k.basename(e)}/ (${n})${c}`)}}var Pe=!1;async function qt(){if(Pe)return;Mt(),_o();let t=Lt().version;if(t===J){Pe=!0;return}So(),bo(),process.on("exit",Be),t>J&&(console.error(`
270
+ `),process.exit(1)),t}function qt(e){try{d.chmodSync(e,384)}catch{}}function xo(e){if(Pt(),d.existsSync(R))try{if(d.lstatSync(R).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${R}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(n){if(n.message?.includes("symbolic link"))throw n}let t=R+".tmp";try{d.writeFileSync(t,JSON.stringify(e,null,2)),qt(t),d.renameSync(t,R)}catch(n){try{d.unlinkSync(t)}catch{}throw n}}function Mt(e,t){if(!d.existsSync(e))return;let n=`.pre-v${String(t).padStart(3,"0")}.backup`,o=d.readdirSync(e).filter(c=>{let a=c.toLowerCase();if(a.includes(".backup")||a===".schema-version"||a.endsWith(".tmp"))return!1;try{return d.statSync(k.join(e,c)).isFile()}catch{return!1}}),r=0,s=0,i=[];for(let c of o){let a=k.join(e,c),m=k.join(e,c+n);if(d.existsSync(m)){s++;continue}try{Ut(a,m),r++}catch(l){i.push({file:c,err:l.message})}}if(i.length>0){let c=new Error(`Failed to back up ${i.length} file(s) in ${k.basename(e)}/: `+i.map(a=>`${a.file} (${a.err})`).join(", "));throw c.code="BACKUP_INCOMPLETE",c}if(r>0||s>0){let c=s>0?` (${s} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${r} files in ${k.basename(e)}/ (${n})${c}`)}}var Pe=!1;async function Bt(){if(Pe)return;Pt(),Io();let t=Ft().version;if(t===J){Pe=!0;return}vo(),So(),process.on("exit",Be),t>J&&(console.error(`
271
271
  [FATAL] Your data schema is at v${t}, but this plugin build only understands up to v${J}.`),console.error(" This usually means you downgraded the plugin without rolling back data."),console.error(` Operating on the newer-format data with this build would risk corrupting it.
272
272
  `),console.error(" Options:"),console.error(" A) Re-install the newer plugin version that produced this schema, then"),console.error(" (if you really want to downgrade) run 'rollback-data --confirm' first,"),console.error(" then re-install the older plugin version."),console.error(` B) Manually restore your data from the *.pre-vNNN.backup files matching v${J}.
273
273
  `),process.exit(1)),console.log(`
274
- \u{1F4E6} Schema migration: v${t} \u2192 v${J}`);let n=Io();if(!n.ok){console.error(`
274
+ \u{1F4E6} Schema migration: v${t} \u2192 v${J}`);let n=To();if(!n.ok){console.error(`
275
275
  [FATAL] Cannot migrate \u2014 current data is corrupted:`);for(let r of n.broken)console.error(` ${r.dir}/${r.file}: ${r.err}`);console.error(`
276
276
  Migration ABORTED. No backups taken, no files modified.`),console.error(" Repair the file(s) above (restore from your own backup, or fix manually)"),console.error(` and try again. To inspect: 'ilml linkedin migrate-status'.
277
277
  `),process.exit(1)}let o=U.filter(r=>r.version>t);for(let r of o){console.log(`
278
- Running migration v${String(r.version).padStart(3,"0")}...`);let s=`v${String(r.version).padStart(3,"0")}`,i=`.pre-${s}.backup`;try{Ft(Q,r.version),Ft(Z,r.version)}catch(a){console.error(`
278
+ Running migration v${String(r.version).padStart(3,"0")}...`);let s=`v${String(r.version).padStart(3,"0")}`,i=`.pre-${s}.backup`;try{Mt(Q,r.version),Mt(Z,r.version)}catch(a){console.error(`
279
279
  [FATAL] Pre-migration backup failed for ${s}: ${a.message}`),console.error(" Migration NOT applied. Repair the underlying issue (disk space, permissions, locked files)"),console.error(` and re-run the plugin command.
280
280
  `),process.exit(1)}let c=r.module;try{await c.up(Q,Z)}catch(a){console.error(`
281
- [FATAL] Migration ${s} failed: ${a.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let m=To(i);m.ok?(console.error(` \u2713 Auto-rollback restored ${m.restored} files. Schema stays at v${t}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
281
+ [FATAL] Migration ${s} failed: ${a.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let m=Do(i);m.ok?(console.error(` \u2713 Auto-rollback restored ${m.restored} files. Schema stays at v${t}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
282
282
  `)):(console.error(` [!] Auto-rollback INCOMPLETE: ${m.error}`),console.error(` Manual recovery: copy *${i} files back over the originals.
283
283
  `))}catch(m){console.error(` [!] Auto-rollback CRASHED while restoring: ${m.message}`),console.error(` Manual recovery: copy *${i} files back over the originals.
284
- `)}process.exit(1)}try{let a=new Date().toISOString(),m={version:r.version,name:c.NAME||s,appliedAt:a},l=Lt();l.version=r.version,l.migratedAt=a,l.migrations=l.migrations||[],l.migrations.push(m),Co(l)}catch(a){console.error(`
284
+ `)}process.exit(1)}try{let a=new Date().toISOString(),m={version:r.version,name:c.NAME||s,appliedAt:a},l=Ft();l.version=r.version,l.migratedAt=a,l.migrations=l.migrations||[],l.migrations.push(m),xo(l)}catch(a){console.error(`
285
285
  [FATAL] Migration ${s} applied successfully but schema version file could NOT be updated: ${a.message}`),console.error(` Your data is in the v${r.version} state but .schema-version still shows v${t}.`),console.error(` On next plugin command the runner will try to re-apply ${s}. If that migration is`),console.error(` idempotent (most are), it will succeed harmlessly. If you're unsure, run 'data-cleanup' and contact support.
286
- `),process.exit(1)}console.log(` \u2713 Migration ${s} complete`)}try{ut()}catch{}try{bt()}catch{}Pe=!0,Be(),console.log(`
286
+ `),process.exit(1)}console.log(` \u2713 Migration ${s} complete`)}try{dt()}catch{}try{St()}catch{}Pe=!0,Be(),console.log(`
287
287
  \u2713 Schema is now at v${J}
288
- `)}var xo=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function $o(e){let t=e.toLowerCase();return xo.some(n=>n.test(t))}var Eo=6e4;function Ao(){let e=Date.now(),t=[];for(let n of[P,Q,Z]){if(!d.existsSync(n))continue;let o;try{o=d.readdirSync(n)}catch{continue}for(let r of o){if(!$o(r))continue;let s=k.join(n,r);try{let i=d.statSync(s);if(!i.isFile()||e-i.mtimeMs<Eo)continue;t.push({dir:n,file:r,fullPath:s})}catch{}}}return t}function _o({verbose:e=!1}={}){let t=Ao();if(t.length===0)return e&&console.log(" No orphan .tmp files found."),0;let n=0;for(let o of t)try{d.unlinkSync(o.fullPath),n++,e&&console.log(` Removed orphan: ${k.basename(o.dir)}/${o.file}`)}catch(r){console.warn(` [!] Could not remove orphan ${o.fullPath}: ${r.message}`)}return!e&&n>0&&console.log(` Cleaned up ${n} orphan .tmp file(s) from previous run.`),n}function Io(){let e=[];for(let t of[Q,Z]){if(!d.existsSync(t))continue;let n=d.readdirSync(t).filter(o=>{let r=o.toLowerCase();return r.endsWith(".json")&&!r.includes(".backup")});for(let o of n){let r=k.join(t,o);try{be(r)}catch(s){e.push({dir:k.basename(t),file:o,err:s.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function To(e){let t=[],n=[];for(let s of[Q,Z]){if(!d.existsSync(s))continue;let i=d.readdirSync(s).filter(c=>c.endsWith(e));for(let c of i){let a=k.join(s,c),m=c.slice(0,-e.length),l=k.join(s,m);try{m.toLowerCase().endsWith(".json")?be(a):d.accessSync(a,d.constants.R_OK),t.push({backupPath:a,originalPath:l,fileName:c,dir:s})}catch(y){n.push(`${k.basename(s)}/${c}: ${y.message}`)}}}if(n.length>0)return{ok:!1,error:n.join("; "),restored:0};let o=0,r=[];for(let{backupPath:s,originalPath:i,fileName:c,dir:a}of t)try{Bt(s,i),o++}catch(m){r.push(`${k.basename(a)}/${c}: ${m.message}`)}return r.length>0?{ok:!1,error:r.join("; "),restored:o}:{ok:!0,restored:o}}function Bt(e,t){let n=t+".tmp";try{d.copyFileSync(e,n),Pt(n)}catch(o){try{d.unlinkSync(n)}catch{}throw o}try{d.renameSync(n,t)}catch(o){try{d.unlinkSync(n)}catch{}if(o.code==="EPERM"||o.code==="EBUSY"){let r=new Error(`File locked: ${t} (${o.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw r.code=o.code,r}throw o}}await qt();var fe=process.argv.slice(2),Se=fe.find(e=>e.startsWith("--source="))?.split("=")[1]||"scouted",Ue=parseInt(fe.find(e=>e.startsWith("--max="))?.split("=")[1]||"50"),X=parseInt(fe.find(e=>e.startsWith("--revisit="))?.split("=")[1]||"0"),Do=fe.includes("--list"),Ro=fe.includes("--verbose");function jo(){let e=kt();console.log(`
288
+ `)}var $o=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function Eo(e){let t=e.toLowerCase();return $o.some(n=>n.test(t))}var Ao=6e4;function _o(){let e=Date.now(),t=[];for(let n of[P,Q,Z]){if(!d.existsSync(n))continue;let o;try{o=d.readdirSync(n)}catch{continue}for(let r of o){if(!Eo(r))continue;let s=k.join(n,r);try{let i=d.statSync(s);if(!i.isFile()||e-i.mtimeMs<Ao)continue;t.push({dir:n,file:r,fullPath:s})}catch{}}}return t}function Io({verbose:e=!1}={}){let t=_o();if(t.length===0)return e&&console.log(" No orphan .tmp files found."),0;let n=0;for(let o of t)try{d.unlinkSync(o.fullPath),n++,e&&console.log(` Removed orphan: ${k.basename(o.dir)}/${o.file}`)}catch(r){console.warn(` [!] Could not remove orphan ${o.fullPath}: ${r.message}`)}return!e&&n>0&&console.log(` Cleaned up ${n} orphan .tmp file(s) from previous run.`),n}function To(){let e=[];for(let t of[Q,Z]){if(!d.existsSync(t))continue;let n=d.readdirSync(t).filter(o=>{let r=o.toLowerCase();return r.endsWith(".json")&&!r.includes(".backup")});for(let o of n){let r=k.join(t,o);try{be(r)}catch(s){e.push({dir:k.basename(t),file:o,err:s.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function Do(e){let t=[],n=[];for(let s of[Q,Z]){if(!d.existsSync(s))continue;let i=d.readdirSync(s).filter(c=>c.endsWith(e));for(let c of i){let a=k.join(s,c),m=c.slice(0,-e.length),l=k.join(s,m);try{m.toLowerCase().endsWith(".json")?be(a):d.accessSync(a,d.constants.R_OK),t.push({backupPath:a,originalPath:l,fileName:c,dir:s})}catch(y){n.push(`${k.basename(s)}/${c}: ${y.message}`)}}}if(n.length>0)return{ok:!1,error:n.join("; "),restored:0};let o=0,r=[];for(let{backupPath:s,originalPath:i,fileName:c,dir:a}of t)try{Ut(s,i),o++}catch(m){r.push(`${k.basename(a)}/${c}: ${m.message}`)}return r.length>0?{ok:!1,error:r.join("; "),restored:o}:{ok:!0,restored:o}}function Ut(e,t){let n=t+".tmp";try{d.copyFileSync(e,n),qt(n)}catch(o){try{d.unlinkSync(n)}catch{}throw o}try{d.renameSync(n,t)}catch(o){try{d.unlinkSync(n)}catch{}if(o.code==="EPERM"||o.code==="EBUSY"){let r=new Error(`File locked: ${t} (${o.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw r.code=o.code,r}throw o}}await Bt();var fe=process.argv.slice(2),Se=fe.find(e=>e.startsWith("--source="))?.split("=")[1]||"scouted",Ue=parseInt(fe.find(e=>e.startsWith("--max="))?.split("=")[1]||"50"),X=parseInt(fe.find(e=>e.startsWith("--revisit="))?.split("=")[1]||"0"),Ro=fe.includes("--list"),jo=fe.includes("--verbose");function Oo(){let e=Ct();console.log(`
289
289
  \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(" PROFILE VISIT STATS"),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),console.log(` Unique profiles visited: ${e.totalVisited}`),console.log(` Today: ${e.todayVisited}`),console.log(` Discovered recruiters: ${e.totalRecruiters} (potential visits)`);let t=Ie();if(console.log(` Unconnected recruiters: ${t.length}`),X>0){let n=Te(X);console.log(` Revisit (>${X}d ago): ${n.length}`)}console.log(`\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
290
- `)}async function Oo(){if(console.log(`
290
+ `)}async function No(){if(console.log(`
291
291
  LinkedIn Profile Visitor
292
- `),Do){jo();return}let e=[],t="";if(X>0?(e=Te(X),t=`revisit (>${X} days)`):Se==="people"?(e=Ie(),t="unconnected recruiters (people.json)"):Se==="scouted"||Se==="discovered"?(e=$t(),t="discovered recruiters (market-research)"):(e=xt({limit:Ue}),t="smart (new \u2192 active \u2192 unconnected \u2192 revisit)"),console.log(` Source: ${t}`),console.log(` Candidates: ${e.length}`),console.log(` Max visits: ${Ue}
292
+ `),Ro){Oo();return}let e=[],t="";if(X>0?(e=Te(X),t=`revisit (>${X} days)`):Se==="people"?(e=Ie(),t="unconnected recruiters (people.json)"):Se==="scouted"||Se==="discovered"?(e=Et(),t="discovered recruiters (market-research)"):(e=$t({limit:Ue}),t="smart (new \u2192 active \u2192 unconnected \u2192 revisit)"),console.log(` Source: ${t}`),console.log(` Candidates: ${e.length}`),console.log(` Max visits: ${Ue}
293
293
  `),e.length===0){console.log(` No profiles to visit. Run npm run scout first.
294
- `);return}let n=Dt("visit",Se),{browser:o,page:r}=await lt();try{let s=await Ct(r,e,{maxVisits:Ue,verbose:Ro,skipRecentDays:X>0?0:7});console.log(`
294
+ `);return}let n=Rt("visit",Se),{browser:o,page:r}=await lt();try{let s=await xt(r,e,{maxVisits:Ue,verbose:jo,skipRecentDays:X>0?0:7});console.log(`
295
295
  \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(" Visit Complete"),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),console.log(` Visited: ${s.visited}`),console.log(` Skipped: ${s.skipped} (visited recently)`),console.log(` Errors: ${s.errors}`),console.log(`\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
296
296
  `),je(n,"success",s)}catch(s){console.error(`
297
- Error:`,s.message),je(n,"error",null,s.message)}finally{await o.close().catch(()=>{})}}Oo().catch(e=>{console.error("Visit failed:",e),process.exit(1)});
297
+ Error:`,s.message),je(n,"error",null,s.message)}finally{await o.close().catch(()=>{})}}No().catch(e=>{console.error("Visit failed:",e),process.exit(1)});
package/dist/warmScan.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import"dotenv/config";import*as j from"fs";import*as a from"path";import{fileURLToPath as N}from"url";import{getPluginConfig as T}from"@ilivemylife/graph-sdk";var _=await T();for(let[e,n]of Object.entries(_))process.env[e]===void 0&&(process.env[e]=n);var z=process.env.LINKEDIN_NAME,p=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),Y=Object.freeze(new Set([p.DRAFT,p.CONFIRMED,p.NEEDS_REVIEW])),G=Object.freeze(new Set([p.DRAFT,p.CONFIRMED,p.REJECTED,p.NEEDS_REVIEW]));import"dotenv/config";import*as s from"fs";import*as t from"path";import{fileURLToPath as k}from"url";var F=k(import.meta.url),R=t.dirname(t.dirname(F)),f=process.env.DATA_DIR?t.resolve(process.env.DATA_DIR,"collected-profiles"):t.join(R,"collected-profiles"),ee=t.join(f,"people.json"),ne=t.join(f,"jobs.json"),te=t.join(f,"quota.json"),oe=t.join(f,"conversations.json"),se=t.join(f,"sync-state.json"),re=t.join(f,"application-questions.json"),ie=t.join(f,"profile-history");function b(e,n){if(!s.existsSync(e))return null;try{let c=JSON.parse(s.readFileSync(e,"utf-8"));try{s.copyFileSync(e,e+".backup")}catch{}return c}catch(c){console.error(`[!] ${n} is corrupted: ${c.message}`),console.error(` File: ${t.resolve(e)}`);let i=e+".backup";if(s.existsSync(i))try{let o=JSON.parse(s.readFileSync(i,"utf-8"));console.error(` Restored from ${i}`);try{s.copyFileSync(i,e)}catch{}return o}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{s.copyFileSync(e,e+".corrupted")}catch{}return null}}var L=a.dirname(N(import.meta.url)),I=process.env.DATA_DIR?a.resolve(process.env.DATA_DIR,"collected-profiles"):a.join(L,"collected-profiles"),g={};for(let e of process.argv.slice(2)){let n=e.match(/^--([^=]+)(?:=(.*))?$/);n&&(g[n[1]]=n[2]??!0)}var d=g.group||null,C=typeof g["min-seniority"]=="string"?g["min-seniority"]:null,D=!!g["show-all"],u={},h=process.env.DATA_DIR&&a.resolve(process.env.DATA_DIR,"target-companies.json");if(h&&j.existsSync(h))try{u=JSON.parse(j.readFileSync(h,"utf8"))}catch(e){console.error(`Ignoring malformed ${h}: ${e.message}`)}(!u||typeof u!="object"||Array.isArray(u))&&(u={});var A=typeof g.companies=="string"?g.companies.split(","):d?u[d]?.companies:null,y=Array.isArray(A)?A.filter(e=>typeof e=="string"&&e.trim()).map(e=>e.trim()):null;if(!y||!y.length){console.error("Specify which companies to scan:"),console.error(' --companies="Google,Meta,Stripe"');let e=Object.keys(u);d&&!u[d]&&console.error(` (group "${d}" not found)`),e.length?console.error(` --group=<name> your groups: ${e.join(", ")}`):console.error(` or define named groups in ${h||"<DATA_DIR>/target-companies.json"}`),process.exit(1)}var v=b(a.join(I,"people.json"),"people.json")||{},E=b(a.join(I,"conversations.json"),"conversations.json")||{};Object.keys(v).length||(console.error("No contacts found yet \u2014 run `ilml linkedin sync` first to build your network, then warm-scan."),process.exit(1));var w=new Map;for(let[e,n]of Object.entries(E))typeof n.isConnection=="boolean"&&w.set(e,{isConnection:n.isConnection,conv:n});function M(e){if(e.name&&w.has(e.name)){let n=w.get(e.name);return{is:n.isConnection,src:"A.convInbox",conv:n.conv}}return e.connectionDegree==="1st"?{is:!0,src:"B.profileScrape"}:e.connectionDegree==="2nd"||e.connectionDegree==="3rd"?{is:!1,src:"B.profileScrape"}:e.connectionStatus==="connected"?{is:!0,src:"C.botStatus"}:["sent_without_note","sent_with_note","withdrawn","rejected"].includes(e.connectionStatus)?{is:!1,src:"C.botStatus"}:{is:null,src:null}}var O={intern:0,junior:1,mid:2,senior:3,lead:4,staff:4,principal:5,director:5,vp:6};function P(e,n){if(!n)return!0;if(!e)return!1;let c=e.toLowerCase(),i=O[n.toLowerCase()];if(i==null)return!0;for(let[o,r]of Object.entries(O))if(c.includes(o)&&r>=i)return!0;return!1}function J(e,n){return[e.company,e.title,n?.personContext?.title,n?.personContext?.company].filter(Boolean).join(" | ").toLowerCase()}function B(e){let n=e.toLowerCase().replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`\\b${n}\\b`,"i")}var U=y.map(e=>({name:e,re:B(e)})),S={};for(let e of y)S[e]={connected:[],notConnected:[],unknown:[]};for(let[e,n]of Object.entries(v)){let c=n.name?E[n.name]:null,i=J(n,c);if(i)for(let{name:o,re:r}of U){if(!r.test(i))continue;if(!P(n.title||c?.personContext?.title,C))break;let l=M(n),m={name:n.name||e.slice(-40),title:n.title||c?.personContext?.title||"",company:n.company,degree:n.connectionDegree,via:l.src,convStatus:l.conv?.conversationStatus,lastMsg:l.conv?.lastMessageTime,lastBy:l.conv?.lastMessageBy,category:l.conv?.category||n.category};l.is===!0?S[o].connected.push(m):l.is===!1?S[o].notConnected.push(m):S[o].unknown.push(m);break}}var q=C?` (min-seniority=${C})`:"",V=d?`group "${d}"`:`${y.length} target companies`;console.log(`
2
- \u2550\u2550 Warm-scan: ${V}${q} \u2550\u2550
3
- `);var Q=Object.entries(S).sort((e,n)=>n[1].connected.length-e[1].connected.length),$=0;for(let[e,n]of Q){let c=n.connected.length+n.notConnected.length+n.unknown.length;$+=n.connected.length;let i=`${e.padEnd(18)} connected: ${n.connected.length}, other: ${n.notConnected.length+n.unknown.length}, total seen: ${c}`;if(console.log(i),n.connected.length>0)for(let r of n.connected){let l=(r.title||"").slice(0,55),m=r.convStatus?` [${r.convStatus}]`:"",x=r.lastMsg?` (${r.lastMsg.slice(0,10)})`:"";console.log(` \xB7 ${r.name.padEnd(28)} ${l}${m}${x}`)}let o=[...n.notConnected,...n.unknown];if(o.length>0&&(D||o.length<=5)){console.log(" seen but not 1st-degree:");for(let r of o.slice(0,D?o.length:5))console.log(` ${r.name.padEnd(28)} ${(r.title||"").slice(0,55)}`)}else o.length>5&&console.log(` seen but not 1st-degree: ${o.length} \u2014 add --show-all or --companies="${e}" to expand`);console.log("")}console.log(`Total 1st-degree at target companies: ${$}`);
1
+ import"dotenv/config";import*as j from"fs";import*as a from"path";import{fileURLToPath as L}from"url";import{getPluginConfig as _}from"@ilivemylife/graph-sdk";var k=await _();for(let[e,n]of Object.entries(k))process.env[e]===void 0&&(process.env[e]=n);var z=process.env.LINKEDIN_NAME,p=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),G=Object.freeze(new Set([p.DRAFT,p.CONFIRMED,p.NEEDS_REVIEW])),H=Object.freeze(new Set([p.DRAFT,p.CONFIRMED,p.REJECTED,p.NEEDS_REVIEW]));import"dotenv/config";import*as r from"fs";import*as o from"path";import{fileURLToPath as F}from"url";var R=F(import.meta.url),N=o.dirname(o.dirname(R)),f=process.env.DATA_DIR?o.resolve(process.env.DATA_DIR,"collected-profiles"):o.join(N,"collected-profiles"),ne=o.join(f,"people.json"),te=o.join(f,"jobs.json"),oe=o.join(f,"quota.json"),se=o.join(f,"conversations.json"),re=o.join(f,"sync-state.json"),ie=o.join(f,"application-questions.json"),ce=o.join(f,"profile-history"),A=(e,n)=>{let t=parseInt(e,10);return Number.isInteger(t)&&t>=0?t:n},le=A(process.env.CONNECT_MAX_DAILY,20),ae=A(process.env.CONNECT_MAX_WEEKLY,100);function b(e,n){if(!r.existsSync(e))return null;try{let t=JSON.parse(r.readFileSync(e,"utf-8"));try{r.copyFileSync(e,e+".backup")}catch{}return t}catch(t){console.error(`[!] ${n} is corrupted: ${t.message}`),console.error(` File: ${o.resolve(e)}`);let c=e+".backup";if(r.existsSync(c))try{let s=JSON.parse(r.readFileSync(c,"utf-8"));console.error(` Restored from ${c}`);try{r.copyFileSync(c,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{r.copyFileSync(e,e+".corrupted")}catch{}return null}}var M=a.dirname(L(import.meta.url)),O=process.env.DATA_DIR?a.resolve(process.env.DATA_DIR,"collected-profiles"):a.join(M,"collected-profiles"),g={};for(let e of process.argv.slice(2)){let n=e.match(/^--([^=]+)(?:=(.*))?$/);n&&(g[n[1]]=n[2]??!0)}var d=g.group||null,C=typeof g["min-seniority"]=="string"?g["min-seniority"]:null,D=!!g["show-all"],u={},h=process.env.DATA_DIR&&a.resolve(process.env.DATA_DIR,"target-companies.json");if(h&&j.existsSync(h))try{u=JSON.parse(j.readFileSync(h,"utf8"))}catch(e){console.error(`Ignoring malformed ${h}: ${e.message}`)}(!u||typeof u!="object"||Array.isArray(u))&&(u={});var E=typeof g.companies=="string"?g.companies.split(","):d?u[d]?.companies:null,y=Array.isArray(E)?E.filter(e=>typeof e=="string"&&e.trim()).map(e=>e.trim()):null;if(!y||!y.length){console.error("Specify which companies to scan:"),console.error(' --companies="Google,Meta,Stripe"');let e=Object.keys(u);d&&!u[d]&&console.error(` (group "${d}" not found)`),e.length?console.error(` --group=<name> your groups: ${e.join(", ")}`):console.error(` or define named groups in ${h||"<DATA_DIR>/target-companies.json"}`),process.exit(1)}var v=b(a.join(O,"people.json"),"people.json")||{},$=b(a.join(O,"conversations.json"),"conversations.json")||{};Object.keys(v).length||(console.error("No contacts found yet \u2014 run `ilml linkedin sync` first to build your network, then warm-scan."),process.exit(1));var w=new Map;for(let[e,n]of Object.entries($))typeof n.isConnection=="boolean"&&w.set(e,{isConnection:n.isConnection,conv:n});function P(e){if(e.name&&w.has(e.name)){let n=w.get(e.name);return{is:n.isConnection,src:"A.convInbox",conv:n.conv}}return e.connectionDegree==="1st"?{is:!0,src:"B.profileScrape"}:e.connectionDegree==="2nd"||e.connectionDegree==="3rd"?{is:!1,src:"B.profileScrape"}:e.connectionStatus==="connected"?{is:!0,src:"C.botStatus"}:["sent_without_note","sent_with_note","withdrawn","rejected"].includes(e.connectionStatus)?{is:!1,src:"C.botStatus"}:{is:null,src:null}}var I={intern:0,junior:1,mid:2,senior:3,lead:4,staff:4,principal:5,director:5,vp:6};function J(e,n){if(!n)return!0;if(!e)return!1;let t=e.toLowerCase(),c=I[n.toLowerCase()];if(c==null)return!0;for(let[s,i]of Object.entries(I))if(t.includes(s)&&i>=c)return!0;return!1}function B(e,n){return[e.company,e.title,n?.personContext?.title,n?.personContext?.company].filter(Boolean).join(" | ").toLowerCase()}function U(e){let n=e.toLowerCase().replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`\\b${n}\\b`,"i")}var q=y.map(e=>({name:e,re:U(e)})),S={};for(let e of y)S[e]={connected:[],notConnected:[],unknown:[]};for(let[e,n]of Object.entries(v)){let t=n.name?$[n.name]:null,c=B(n,t);if(c)for(let{name:s,re:i}of q){if(!i.test(c))continue;if(!J(n.title||t?.personContext?.title,C))break;let l=P(n),m={name:n.name||e.slice(-40),title:n.title||t?.personContext?.title||"",company:n.company,degree:n.connectionDegree,via:l.src,convStatus:l.conv?.conversationStatus,lastMsg:l.conv?.lastMessageTime,lastBy:l.conv?.lastMessageBy,category:l.conv?.category||n.category};l.is===!0?S[s].connected.push(m):l.is===!1?S[s].notConnected.push(m):S[s].unknown.push(m);break}}var V=C?` (min-seniority=${C})`:"",W=d?`group "${d}"`:`${y.length} target companies`;console.log(`
2
+ \u2550\u2550 Warm-scan: ${W}${V} \u2550\u2550
3
+ `);var Q=Object.entries(S).sort((e,n)=>n[1].connected.length-e[1].connected.length),x=0;for(let[e,n]of Q){let t=n.connected.length+n.notConnected.length+n.unknown.length;x+=n.connected.length;let c=`${e.padEnd(18)} connected: ${n.connected.length}, other: ${n.notConnected.length+n.unknown.length}, total seen: ${t}`;if(console.log(c),n.connected.length>0)for(let i of n.connected){let l=(i.title||"").slice(0,55),m=i.convStatus?` [${i.convStatus}]`:"",T=i.lastMsg?` (${i.lastMsg.slice(0,10)})`:"";console.log(` \xB7 ${i.name.padEnd(28)} ${l}${m}${T}`)}let s=[...n.notConnected,...n.unknown];if(s.length>0&&(D||s.length<=5)){console.log(" seen but not 1st-degree:");for(let i of s.slice(0,D?s.length:5))console.log(` ${i.name.padEnd(28)} ${(i.title||"").slice(0,55)}`)}else s.length>5&&console.log(` seen but not 1st-degree: ${s.length} \u2014 add --show-all or --companies="${e}" to expand`);console.log("")}console.log(`Total 1st-degree at target companies: ${x}`);
package/ilml-plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "linkedin",
3
- "version": "1.14.0",
3
+ "version": "1.14.2",
4
4
  "description": "Reference ilml plugin: keep a local mirror of your LinkedIn data with AI-assisted workflows",
5
5
  "commands": {
6
6
  "help": { "run": "node dist/help.mjs", "description": "List all commands with descriptions; or `help <cmd>` for detailed info on one command (usage, flags, ban-risk, when-to-use). Reads ilml-plugin.json directly.", "usage": "ilml linkedin help [<cmd>]", "useWhen": "You forgot what a command does, or you're an AI agent picking the right tool." },
@@ -49,18 +49,19 @@
49
49
  },
50
50
  "connect": {
51
51
  "run": "node dist/connect.mjs",
52
- "description": "Connect queue: a reviewable, prioritised list of people to send connection requests to. Drop a list of profiles, review and approve them, choose who gets a personal note (LinkedIn limits those), then send the approved ones in one browser session under the daily/weekly quota. The queue is one file with a status on each entry (queued → approved → sent) — approving, reordering and sending all update it in place. Rich profile data stays in your contacts DB; the queue just references people by URL.",
52
+ "description": "Connect queue: a reviewable, prioritised list of people to send connection requests to. Drop a list of profiles, review and approve them, then send the approved ones in one browser session under the daily/weekly quota. By default sends go WITHOUT a note (LinkedIn caps custom notes to a few per month); mark the deliberate few with `connect note` and send those with `connect run --with-notes`. It stops itself after 3 failures in a row (often LinkedIn's own invite limit) and saves what LinkedIn showed. The queue is one file with a status on each entry (queued → approved → sent) — approving, reordering and sending all update it in place.",
53
53
  "usage": "ilml linkedin connect <add|fill|list|approve|note|no-note|skip|priority|run> [...]",
54
54
  "flags": {
55
55
  "add <file.json>": "Add profiles from a JSON array [{url, note?, name?}] to the queue. Dedupes against the queue and skips people already connected/pending. Combine with --tag=X and --priority=high|normal|low.",
56
- "fill --category=...": "Fill the queue from people already categorised in your contacts DB (not yet connected). e.g. --category=founder,investor,hiring_manager (or --valuable for the standard high-value set). Each entry is tagged with its category so `quota` shows acceptance per category. --min-posts=N keeps only people who appeared in at least N of your job posts (the repeat-recruiter signal, useful with --category=recruiter). --max=N keeps the best N (ranked by priority, mutual connections, repeat count). Respects your excluded-locations setting. No browser.",
56
+ "fill --category=...": "Fill the queue from people already categorised in your contacts DB (not yet connected). e.g. --category=founder,investor,hiring_manager (or --valuable for the standard high-value set). Each entry is tagged with its category so `quota` shows acceptance per category. By default it VALIDATES that each person's stored title actually confirms the category (drops people whose title contradicts the tag — the category field is sometimes mis-set by enrichment; people with no stored title are held rather than visited, to avoid ban risk). --no-validate keeps the old trust-the-tag behaviour. --min-posts=N keeps only people who appeared in at least N of your job posts (the repeat-recruiter signal, useful with --category=recruiter). --max=N keeps the best N (ranked by priority, mutual connections, repeat count). Respects your excluded-locations setting. No browser.",
57
57
  "list": "Show the queue (status, priority, note flag, name/title joined from your contacts). No browser. Optional --status=queued|approved|sent|skipped|failed.",
58
58
  "approve <url>": "Move an entry from queued to approved. Or approve a whole batch with --all or --tag=X. Only approved entries are sent.",
59
- "note <url> \"text\"": "Attach a personal note to an entry (it will send WITH a note). LinkedIn caps notes at 300 characters and limits how many you can send — allocate deliberately.",
60
- "no-note <url>": "Remove the note from an entry (sends without a note, frees your note budget).",
59
+ "note <url> \"text\"": "Save a personal note on an entry (max 300 chars). It is sent ONLY when you run `connect run --with-notes` — by default connect sends plain requests, because LinkedIn caps custom notes to a handful per month. Shown with 📝 in `connect list`.",
60
+ "no-note <url>": "Remove the saved note from an entry.",
61
61
  "skip <url>": "Drop an entry from sending (queued/approved → skipped).",
62
62
  "priority <url> <lvl>": "Set priority high|normal|low. Higher priority is sent first.",
63
- "run": "Send approved entries, highest priority first, under the daily/weekly quota (browser). --max=N caps this run, --dry-run previews without sending, --yes skips the confirmation prompt (for schedulers).",
63
+ "run": "Send approved entries, highest priority first, under the daily/weekly quota (browser). By default sends WITHOUT notes; add --with-notes to send the saved notes (spends your scarce monthly LinkedIn notes). --max=N caps this run, --dry-run previews without sending, --yes skips the confirmation prompt (for schedulers). Stops itself after 3 failures in a row and saves what LinkedIn showed to connect-diagnostics/.",
64
+ "--with-notes": "With `run`: actually send the per-entry saved notes (opt-in; default is no notes).",
64
65
  "--tag=X": "Label a batch (used by add/approve). Shows up as the source in `ilml linkedin quota` acceptance stats.",
65
66
  "--priority=LEVEL": "high|normal|low (used by add)."
66
67
  },
@@ -298,6 +299,13 @@
298
299
  { "key": "FUNNEL_MESSAGE", "label": "Connection request message", "type": "text", "required": false, "hint": "Use {name} placeholder, e.g., 'Hi {name}, I just applied for a role at your company'" },
299
300
  { "key": "FUNNEL_EXCLUDE_LOCATIONS", "label": "Exclude these locations", "type": "text", "required": false, "hint": "Comma-separated list" }
300
301
  ]
302
+ },
303
+ {
304
+ "title": "Connection limits (connect / funnel quota)",
305
+ "fields": [
306
+ { "key": "CONNECT_MAX_DAILY", "label": "Max connection requests per day", "type": "number", "required": false, "default": 20, "hint": "Your OWN conservative daily cap across every connection path. LinkedIn's real limit is dynamic and unknown — keep this below it. Raise it deliberately only to surface LinkedIn's own limit: connect stops itself after 3 failures in a row and saves what LinkedIn showed to connect-diagnostics/." },
307
+ { "key": "CONNECT_MAX_WEEKLY", "label": "Max connection requests per week", "type": "number", "required": false, "default": 100, "hint": "Rolling 7-day cap. LinkedIn free accounts are often limited around 100-200/week and adjust it based on your acceptance rate." }
308
+ ]
301
309
  }
302
310
  ]
303
311
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ilml-plugin-linkedin",
3
- "version": "1.14.0",
3
+ "version": "1.14.2",
4
4
  "description": "Reference ilml plugin: keep a local mirror of your LinkedIn data and run AI-assisted workflows under your direction.",
5
5
  "type": "module",
6
6
  "files": [