Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(linkedin_enrichment): split monolithic module into smaller submodules

Steven Enamakelc24921ebbe24
diff --git a/crates/openhuman-core/src/agent/learning/linkedin_enrichment.rs b/crates/openhuman-core/src/agent/learning/linkedin_enrichment.rs
index 9766cabcac..a1d8a23b8b 100644
--- a/crates/openhuman-core/src/agent/learning/linkedin_enrichment.rs
+++ b/crates/openhuman-core/src/agent/learning/linkedin_enrichment.rs
@@ -11,8 +11,277 @@
//! pass. Each stage logs progress so the caller (or a future frontend
//! progress UI) can observe what happened.
+mod gmail_discovery;
+mod memory_persistence;
+mod profile_markdown;
+
+use crate::config::Config;
+use crate::integrations::build_client;
+use regex::Regex;
+use serde_json::json;
+use std::sync::LazyLock;
+
+use gmail_discovery::search_gmail_for_linkedin;
+pub use gmail_discovery::scrape_linkedin_profile;
+use memory_persistence::{persist_linkedin_profile, persist_linkedin_url_only, profile_memory_writer};
+use profile_markdown::{write_profile_md, write_profile_md_url_only};
+pub use profile_markdown::{render_profile_markdown, summarise_profile_with_llm};
+
#[cfg(test)]
#[path = "linkedin_enrichment_tests.rs"]
mod tests;
-include!("linkedin_enrichment_part_01.rs");
-include!("linkedin_enrichment_part_02.rs");
+
+/// Apify actor slug for the LinkedIn profile scraper.
+pub(super) const LINKEDIN_SCRAPER_ACTOR: &str = "dev_fusion/linkedin-profile-scraper";
+
+/// Regex that captures a LinkedIn username from profile URLs.
+///
+/// Matches both the canonical form (`linkedin.com/in/<slug>`) and the
+/// notification-email form (`linkedin.com/comm/in/<slug>`). The username
+/// is captured in group 1 so we can reconstruct a clean canonical URL.
+pub(super) static LINKEDIN_USERNAME_RE: LazyLock<Regex> = LazyLock::new(|| {
+ Regex::new(r"https?://(?:www\.)?linkedin\.com/(?:comm/)?in/([a-zA-Z0-9_-]+)").unwrap()
+});
+
+/// Build the canonical profile URL from a username slug.
+pub(super) fn canonical_linkedin_url(username: &str) -> String {
+ format!("https://www.linkedin.com/in/{username}")
+}
+
+/// Typed status for a pipeline stage.
+#[derive(Debug, Clone, serde::Serialize)]
+#[serde(rename_all = "lowercase")]
+pub enum StageStatus {
+ Success,
+ Failed,
+ Skipped,
+}
+
+/// A single pipeline stage result, suitable for structured RPC responses.
+#[derive(Debug, Clone, serde::Serialize)]
+pub struct EnrichmentStage {
+ pub id: String,
+ pub status: StageStatus,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub detail: Option<String>,
+}
+
+/// Outcome of the full enrichment pipeline.
+#[derive(Debug)]
+pub struct LinkedInEnrichmentResult {
+ /// The LinkedIn profile URL found in Gmail, if any.
+ pub profile_url: Option<String>,
+ /// Raw scraped profile JSON from Apify, if the scrape succeeded.
+ pub profile_data: Option<serde_json::Value>,
+ /// Typed stage results for structured consumption by the frontend.
+ pub stages: Vec<EnrichmentStage>,
+ /// Human-readable log lines for display.
+ pub log: Vec<String>,
+}
+
+/// Run the full Gmail → LinkedIn → Apify enrichment pipeline.
+///
+/// `preset_profile_url` lets callers skip the Gmail-search stage and
+/// supply a profile URL they already discovered out-of-band — currently
+/// the frontend obtains one via the webview-driven
+/// `gmail_find_linkedin_profile_url` Tauri command, which uses the
+/// logged-in Gmail webview's CDP session instead of a Composio token.
+/// When `None`, the function falls back to the Composio-driven Gmail
+/// search at [`search_gmail_for_linkedin`] (which currently errors
+/// because Composio Gmail was removed; callers should pass `Some` until
+/// a Composio-free fallback ships).
+///
+/// Returns `Ok` with a result struct even if individual stages fail —
+/// partial progress is still useful. Only returns `Err` if we can't
+/// even build the integration client (i.e. user isn't signed in).
+pub async fn run_linkedin_enrichment(
+ config: &Config,
+ preset_profile_url: Option<String>,
+) -> anyhow::Result<LinkedInEnrichmentResult> {
+ let mut result = LinkedInEnrichmentResult {
+ profile_url: None,
+ profile_data: None,
+ stages: Vec::new(),
+ log: Vec::new(),
+ };
+
+ // Short-circuit: if PROFILE.md is already on disk from a previous
+ // enrichment run, skip the entire pipeline. The welcome agent reads
+ // PROFILE.md straight from the workspace, so re-running stages 1-3
+ // would just churn quota for the same output.
+ let profile_path = config.workspace_dir.join("PROFILE.md");
+ if profile_path.is_file() {
+ tracing::info!(
+ path = %profile_path.display(),
+ "[linkedin_enrichment] PROFILE.md already exists — skipping pipeline"
+ );
+ result
+ .log
+ .push("PROFILE.md already exists — skipping enrichment.".into());
+ for id in ["gmail-search", "apify-scrape", "build-profile"] {
+ result.stages.push(EnrichmentStage {
+ id: id.into(),
+ status: StageStatus::Skipped,
+ detail: Some("PROFILE.md already on disk".into()),
+ });
+ }
+ return Ok(result);
+ }
+
+ let client = build_client(config)
+ .ok_or_else(|| anyhow::anyhow!("no integration client — user not signed in"))?;
+
+ // ── Stage 1: search Gmail for LinkedIn emails ───────────────────
+ let profile_url = if let Some(url) = preset_profile_url {
+ tracing::info!(url = %url, "[linkedin_enrichment] stage 1: using preset profile URL");
+ result
+ .log
+ .push(format!("Using preset LinkedIn profile: {url}"));
+ result.stages.push(EnrichmentStage {
+ id: "gmail-search".into(),
+ status: StageStatus::Success,
+ detail: Some(url.clone()),
+ });
+ Some(url)
+ } else {
+ tracing::info!("[linkedin_enrichment] stage 1: searching Gmail for LinkedIn emails");
+ result
+ .log
+ .push("Searching Gmail for LinkedIn emails...".into());
+ match search_gmail_for_linkedin(config).await {
+ Ok(Some(url)) => {
+ tracing::info!(url = %url, "[linkedin_enrichment] found LinkedIn profile URL");
+ result.log.push(format!("Found LinkedIn profile: {url}"));
+ result.stages.push(EnrichmentStage {
+ id: "gmail-search".into(),
+ status: StageStatus::Success,
+ detail: Some(url.clone()),
+ });
+ Some(url)
+ }
+ Ok(None) => {
+ tracing::info!("[linkedin_enrichment] no LinkedIn profile URL found in emails");
+ result
+ .log
+ .push("No LinkedIn profile URL found in emails.".into());
+ result.stages.push(EnrichmentStage {
+ id: "gmail-search".into(),
+ status: StageStatus::Skipped,
+ detail: Some("No LinkedIn profile URL found in emails".into()),
+ });
+ None
+ }
+ Err(e) => {
+ tracing::warn!(error = %e, "[linkedin_enrichment] Gmail search failed");
+ result.log.push(format!("Gmail search failed: {e}"));
+ result.stages.push(EnrichmentStage {
+ id: "gmail-search".into(),
+ status: StageStatus::Failed,
+ detail: Some(format!("Gmail search failed: {e}")),
+ });
+ None
+ }
+ }
+ };
+
+ result.profile_url = profile_url.clone();
+
+ // ── Stage 2: scrape the LinkedIn profile via Apify ───────────────
+ let Some(url) = profile_url else {
+ result
+ .log
+ .push("Skipping LinkedIn scrape — no profile URL.".into());
+ result.stages.push(EnrichmentStage {
+ id: "apify-scrape".into(),
+ status: StageStatus::Skipped,
+ detail: Some("No profile URL to scrape".into()),
+ });
+ result.stages.push(EnrichmentStage {
+ id: "build-profile".into(),
+ status: StageStatus::Skipped,
+ detail: Some("No profile data".into()),
+ });
+ return Ok(result);
+ };
+
+ tracing::info!(url = %url, "[linkedin_enrichment] stage 2: scraping LinkedIn profile via Apify");
+ result.log.push("Scraping LinkedIn profile...".into());
+
+ // Resolve the guarded memory driver once for all persist calls.
+ let memory = match profile_memory_writer().await {
+ Ok(m) => Some(m),
+ Err(e) => {
+ tracing::warn!(
+ error = %e,
+ "[linkedin_enrichment] memory driver unavailable, skipping memory persistence"
+ );
+ None
+ }
+ };
+
+ match scrape_linkedin_profile(&client, &url).await {
+ Ok(data) => {
+ tracing::info!("[linkedin_enrichment] Apify scrape succeeded");
+ result
+ .log
+ .push("LinkedIn profile scraped successfully.".into());
+ result.stages.push(EnrichmentStage {
+ id: "apify-scrape".into(),
+ status: StageStatus::Success,
+ detail: None,
+ });
+
+ // ── Stage 3: write PROFILE.md to workspace ──────────────
+ tracing::info!("[linkedin_enrichment] stage 3: writing PROFILE.md");
+ if let Err(e) = write_profile_md(config, &url, &data).await {
+ tracing::warn!(error = %e, "[linkedin_enrichment] failed to write PROFILE.md");
+ result.log.push(format!("Failed to write PROFILE.md: {e}"));
+ result.stages.push(EnrichmentStage {
+ id: "build-profile".into(),
+ status: StageStatus::Failed,
+ detail: Some(format!("{e}")),
+ });
+ } else {
+ result.log.push("PROFILE.md written to workspace.".into());
+ result.stages.push(EnrichmentStage {
+ id: "build-profile".into(),
+ status: StageStatus::Success,
+ detail: Some("PROFILE.md written".into()),
+ });
+ }
+
+ // Also persist to memory store for RAG retrieval.
+ if let Some(ref mem) = memory {
+ if let Err(e) = persist_linkedin_profile(mem, &url, &data).await {
+ tracing::warn!(error = %e, "[linkedin_enrichment] failed to persist to memory");
+ }
+ }
+
+ result.profile_data = Some(data);
+ }
+ Err(e) => {
+ tracing::warn!(error = %e, "[linkedin_enrichment] Apify scrape failed");
+ result.log.push(format!("LinkedIn scrape failed: {e}"));
+ result.stages.push(EnrichmentStage {
+ id: "apify-scrape".into(),
+ status: StageStatus::Failed,
+ detail: Some(format!("{e}")),
+ });
+ result.stages.push(EnrichmentStage {
+ id: "build-profile".into(),
+ status: StageStatus::Skipped,
+ detail: Some("Scrape failed".into()),
+ });
+
+ // Still write a minimal PROFILE.md with just the URL.
+ if let Err(e) = write_profile_md_url_only(config, &url) {
+ tracing::warn!(error = %e, "[linkedin_enrichment] failed to write PROFILE.md");
+ }
+ if let Some(ref mem) = memory {
+ let _ = persist_linkedin_url_only(mem, &url).await;
+ }
+ }
+ }
+
+ Ok(result)
+}
diff --git a/crates/openhuman-core/src/agent/learning/linkedin_enrichment/gmail_discovery.rs b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/gmail_discovery.rs
new file mode 100644
index 0000000000..4190ffb207
--- /dev/null
+++ b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/gmail_discovery.rs
@@ -0,0 +1,185 @@
+//! Profile discovery: Gmail search for the user's own LinkedIn URL and the
+//! Apify profile scrape.
+
+use crate::config::Config;
+use crate::integrations::IntegrationClient;
+use regex::Regex;
+use serde_json::json;
+use std::sync::{Arc, LazyLock};
+
+use super::{canonical_linkedin_url, LINKEDIN_SCRAPER_ACTOR, LINKEDIN_USERNAME_RE};
+
+/// Search Gmail via Composio for emails from linkedin.com and extract
+/// the user's own LinkedIn username.
+///
+/// LinkedIn notification emails embed `comm/in/<username>` links in the
+/// **HTML body** — which Gmail returns as base64-encoded data inside
+/// `payload.parts[].body.data`. We must decode those parts before
+/// regex-matching; searching the raw JSON alone misses them.
+pub(super) async fn search_gmail_for_linkedin(config: &Config) -> anyhow::Result<Option<String>> {
+ use crate::integrations::composio::client::{
+ create_composio_client, direct_execute, ComposioClientKind,
+ };
+ use base64::engine::general_purpose::URL_SAFE_NO_PAD;
+ use base64::Engine;
+
+ // Resolve through the mode-aware factory so a direct-mode user
+ // with a stored API key can still drive Gmail enrichment from the
+ // personal Composio tenant (#1710 Wave 2). Pre-fix this path used
+ // `build_composio_client` and returned early for any user without
+ // a backend session, silently disabling LinkedIn enrichment for
+ // direct-mode users even when their LinkedIn/Gmail connections
+ // were healthy on app.composio.dev.
+ let client_kind = create_composio_client(config)
+ .map_err(|e| anyhow::anyhow!("composio client unavailable: {e}"))?;
+
+ // `comm/in/<username>` — LinkedIn's own notification emails always use
+ // this form to refer to the email *recipient's* profile.
+ static COMM_RE: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"linkedin\.com/comm/in/([a-zA-Z0-9_-]+)").unwrap());
+
+ let args = json!({
+ "query": "from:linkedin.com",
+ "max_results": 10,
+ });
+ let resp = match &client_kind {
+ ComposioClientKind::Backend(client) => client
+ .execute_tool("GMAIL_FETCH_EMAILS", Some(args))
+ .await
+ .map_err(|e| anyhow::anyhow!("GMAIL_FETCH_EMAILS failed: {e:#}"))?,
+ ComposioClientKind::Direct(direct) => {
+ tracing::debug!(
+ "[linkedin_enrichment][composio-direct] GMAIL_FETCH_EMAILS via direct tenant"
+ );
+ direct_execute(
+ direct,
+ "GMAIL_FETCH_EMAILS",
+ Some(args),
+ &config.composio.entity_id,
+ None,
+ )
+ .await
+ .map_err(|e| anyhow::anyhow!("GMAIL_FETCH_EMAILS (direct) failed: {e:#}"))?
+ }
+ };
+
+ if !resp.successful {
+ let err = resp.error.unwrap_or_else(|| "unknown error".into());
+ anyhow::bail!("GMAIL_FETCH_EMAILS error: {err}");
+ }
+
+ // Walk the messages, decode HTML parts, and search for profile URLs.
+ let messages = resp
+ .data
+ .get("messages")
+ .and_then(|v| v.as_array())
+ .cloned()
+ .unwrap_or_default();
+
+ for msg in &messages {
+ // Collect all text to search: plain messageText + decoded HTML parts.
+ let mut searchable = String::new();
+
+ // Plain text body (already decoded by Composio).
+ if let Some(text) = msg.get("messageText").and_then(|v| v.as_str()) {
+ searchable.push_str(text);
+ searchable.push('\n');
+ }
+
+ // Decode base64 HTML parts from payload.parts[].body.data.
+ if let Some(parts) = msg.pointer("/payload/parts").and_then(|v| v.as_array()) {
+ for part in parts {
+ let is_html = part
+ .get("mimeType")
+ .and_then(|v| v.as_str())
+ .is_some_and(|m| m.contains("html"));
+ if !is_html {
+ continue;
+ }
+ if let Some(b64) = part.pointer("/body/data").and_then(|v| v.as_str()) {
+ if let Ok(bytes) = URL_SAFE_NO_PAD.decode(b64) {
+ if let Ok(html) = String::from_utf8(bytes) {
+ searchable.push_str(&html);
+ searchable.push('\n');
+ }
+ }
+ }
+ }
+ }
+
+ // Priority 1: comm/in/<username> — always the recipient's own profile.
+ if let Some(caps) = COMM_RE.captures(&searchable) {
+ let username = caps[1].to_string();
+ let url = canonical_linkedin_url(&username);
+ tracing::info!(
+ username = %username,
+ url = %url,
+ "[linkedin_enrichment] found own username via comm/in/ in HTML body"
+ );
+ return Ok(Some(url));
+ }
+
+ // Priority 2: canonical /in/<username> (some notification types).
+ if let Some(caps) = LINKEDIN_USERNAME_RE.captures(&searchable) {
+ let username = caps[1].to_string();
+ let url = canonical_linkedin_url(&username);
+ tracing::info!(
+ username = %username,
+ url = %url,
+ "[linkedin_enrichment] found username via /in/ in email body"
+ );
+ return Ok(Some(url));
+ }
+ }
+
+ Ok(None)
+}
+
+/// Call the Apify LinkedIn profile scraper synchronously and return the
+/// first profile item from the dataset.
+pub async fn scrape_linkedin_profile(
+ client: &Arc<IntegrationClient>,
+ profile_url: &str,
+) -> anyhow::Result<serde_json::Value> {
+ let body = json!({
+ "actorId": LINKEDIN_SCRAPER_ACTOR,
+ "input": {
+ "profileUrls": [profile_url],
+ },
+ "sync": true,
+ "timeoutSecs": 120,
+ });
+
+ tracing::debug!(
+ actor = LINKEDIN_SCRAPER_ACTOR,
+ url_len = profile_url.len(),
+ "[linkedin_enrichment] invoking Apify actor"
+ );
+
+ // The backend wraps the Apify response in its standard envelope.
+ // `IntegrationClient::post` already unwraps `{ success, data }`.
+ let resp: serde_json::Value = client
+ .post("/agent-integrations/apify/run", &body)
+ .await
+ .map_err(|e| anyhow::anyhow!("Apify run failed: {e:#}"))?;
+
+ let status = resp
+ .get("status")
+ .and_then(|v| v.as_str())
+ .unwrap_or("UNKNOWN");
+
+ if status != "SUCCEEDED" {
+ anyhow::bail!("Apify run finished with status: {status}");
+ }
+
+ // Extract the first item from the inline results array.
+ let items = resp
+ .get("items")
+ .and_then(|v| v.as_array())
+ .ok_or_else(|| anyhow::anyhow!("Apify run returned no items array"))?;
+
+ items
+ .first()
+ .cloned()
+ .ok_or_else(|| anyhow::anyhow!("Apify run returned an empty items array"))
+}
diff --git a/crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_02.rs b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/memory_persistence.rs
similarity index 84%
rename from crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_02.rs
rename to crates/openhuman-core/src/agent/learning/linkedin_enrichment/memory_persistence.rs
index 461403a575..59121a82a3 100644
--- a/crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_02.rs
+++ b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/memory_persistence.rs
@@ -1,3 +1,19 @@
+//! Persistence of the scraped profile into the user-profile memory
+//! namespace through the guarded documents family.
+
+use crate::memory::api::provider::MemoryProvider;
+use crate::memory::api::types::{MemoryTaint, NamespaceDocumentInput};
+use serde_json::json;
+
+use super::LINKEDIN_SCRAPER_ACTOR;
+
+/// The namespace scraped LinkedIn profiles are persisted into.
+///
+/// `MemoryClient::store_skill_sync("user-profile", ...)` derived this by
+/// prefixing the skill id with `skill-`; that derivation is one line of host
+/// policy, so it moves here rather than needing a contract member (#5560). The
+/// literal must not drift — it is what onboarding RAG recalls against.
+const PROFILE_MEMORY_NAMESPACE: &str = "skill-user-profile";
/// The guarded memory driver's documents family, for profile persistence.
///
@@ -20,7 +36,7 @@
///
/// The caller still treats an error as "skip persistence and warn", for the
/// genuine failures that remain.
-async fn profile_memory_writer(
+pub(super) async fn profile_memory_writer(
) -> anyhow::Result<std::sync::Arc<crate::memory::guard::MemoryGuard>> {
let guard = crate::memory::ops::guard::active_memory_guard()
.await
@@ -87,7 +103,7 @@ async fn put_profile_document(
/// Persist the full scraped LinkedIn profile to the user-profile memory
/// namespace so the agent has rich context about the user.
-async fn persist_linkedin_profile(
+pub(super) async fn persist_linkedin_profile(
memory: &crate::memory::guard::MemoryGuard,
url: &str,
data: &serde_json::Value,
@@ -113,7 +129,7 @@ async fn persist_linkedin_profile(
}
/// Fallback: persist just the LinkedIn URL when the full scrape fails.
-async fn persist_linkedin_url_only(
+pub(super) async fn persist_linkedin_url_only(
memory: &crate::memory::guard::MemoryGuard,
url: &str,
) -> anyhow::Result<()> {
diff --git a/crates/openhuman-core/src/agent/learning/linkedin_enrichment/profile_markdown.rs b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/profile_markdown.rs
new file mode 100644
index 0000000000..a8bfa54d89
--- /dev/null
+++ b/crates/openhuman-core/src/agent/learning/linkedin_enrichment/profile_markdown.rs
@@ -0,0 +1,230 @@
+//! `PROFILE.md` generation: rendering the scraped LinkedIn payload as
+//! Markdown and distilling it through the backend LLM.
+
+use crate::config::Config;
+
+use super::LINKEDIN_SCRAPER_ACTOR;
+
+// ── PROFILE.md generation ────────────────────────────────────────────
+
+/// Summarise the scraped LinkedIn data with an LLM, then write the
+/// result to `{workspace_dir}/PROFILE.md`. The prompt system picks this
+/// file up automatically on the next agent turn.
+pub(super) async fn write_profile_md(
+ config: &Config,
+ url: &str,
+ data: &serde_json::Value,
+) -> anyhow::Result<()> {
+ // First render a full Markdown draft from the raw data.
+ let raw_md = render_profile_markdown(url, data);
+
+ // Then compress it through the LLM.
+ let md = match summarise_profile_with_llm(config, &raw_md).await {
+ Ok(summary) => summary,
+ Err(e) => {
+ tracing::warn!(
+ error = %e,
+ "[linkedin_enrichment] LLM summarisation failed, falling back to raw markdown"
+ );
+ raw_md
+ }
+ };
+
+ let path = config.workspace_dir.join("PROFILE.md");
+ if let Some(parent) = path.parent() {
+ std::fs::create_dir_all(parent)?;
+ }
+ std::fs::write(&path, &md)?;
+ tracing::info!(path = %path.display(), len = md.len(), "[linkedin_enrichment] wrote PROFILE.md");
+ Ok(())
+}
+
+/// Ask the backend LLM to distil the raw LinkedIn Markdown into a
+/// concise, high-signal profile document suitable for agent context.
+pub async fn summarise_profile_with_llm(config: &Config, raw_md: &str) -> anyhow::Result<String> {
+ let (model_chat, _) =
+ crate::inference::provider::create_chat_model_from_string_with_model_id(
+ "summarization",
+ "openhuman",
+ config,
+ 0.3,
+ )?;
+
+ let system = "\
+You are a profile analyst. You will receive a user's LinkedIn profile in Markdown format. \
+Your job is to produce a concise PROFILE.md that an AI assistant will read to understand \
+who this user is.\n\n\
+Rules:\n\
+- Output clean Markdown with a `# User Profile` heading.\n\
+- Lead with name, headline, location, and LinkedIn URL.\n\
+- Summarise the About section in 2-3 sentences max.\n\
+- List only the most notable experiences (founder roles, leadership positions) — skip \
+ short stints and minor roles.\n\
+- Include education, languages, and any standout achievements.\n\
+- Add a short `## Key facts for the assistant` section with 5-8 bullet points the AI \
+ should know (e.g. expertise areas, industries, current focus, communication style hints).\n\
+- Keep the entire output under 400 words.\n\
+- Do not invent information — only use what is in the input.";
+
+ let model = "summarization-v1";
+
+ tracing::debug!(
+ model = model,
+ input_len = raw_md.len(),
+ "[linkedin_enrichment] sending profile to LLM for summarisation"
+ );
+
+ use tinyinference::message::Message;
+ use tinyinference::model::ModelRequest;
+ let summary = model_chat
+ .invoke(
+ &(),
+ ModelRequest::new(vec![
+ Message::system(system),
+ Message::user(raw_md.to_string()),
+ ]),
+ )
+ .await?
+ .text();
+
+ tracing::debug!(
+ output_len = summary.len(),
+ "[linkedin_enrichment] LLM summarisation complete"
+ );
+
+ Ok(summary)
+}
+
+/// Minimal fallback when the Apify scrape failed but we have the URL.
+pub(super) fn write_profile_md_url_only(config: &Config, url: &str) -> anyhow::Result<()> {
+ let md = format!(
+ "# User Profile\n\n\
+ LinkedIn: {url}\n\n\
+ _Full profile data was not available at onboarding time._\n"
+ );
+ let path = config.workspace_dir.join("PROFILE.md");
+ if let Some(parent) = path.parent() {
+ std::fs::create_dir_all(parent)?;
+ }
+ std::fs::write(&path, md)?;
+ Ok(())
+}
+
+/// Turn the Apify scrape JSON into clean Markdown.
+pub fn render_profile_markdown(url: &str, data: &serde_json::Value) -> String {
+ let s = |key: &str| {
+ data.get(key)
+ .and_then(|v| v.as_str())
+ .unwrap_or("")
+ .to_string()
+ };
+
+ let full_name = s("fullName");
+ let headline = s("headline");
+ let location = s("addressWithCountry");
+ let about = s("about");
+ let connections = data.get("connections").and_then(|v| v.as_u64());
+ let followers = data.get("followers").and_then(|v| v.as_u64());
+
+ let mut md = format!("# User Profile — {full_name}\n\n");
+
+ if !headline.is_empty() {
+ md.push_str(&format!("**{headline}**\n\n"));
+ }
+ if !location.is_empty() {
+ md.push_str(&format!("Location: {location}\n\n"));
+ }
+ md.push_str(&format!("LinkedIn: {url}\n\n"));
+ if let (Some(c), Some(f)) = (connections, followers) {
+ md.push_str(&format!("Connections: {c} | Followers: {f}\n\n"));
+ }
+
+ if !about.is_empty() {
+ md.push_str("## About\n\n");
+ md.push_str(&about);
+ md.push_str("\n\n");
+ }
+
+ // Experience
+ if let Some(exps) = data.get("experiences").and_then(|v| v.as_array()) {
+ if !exps.is_empty() {
+ md.push_str("## Experience\n\n");
+ for exp in exps {
+ let title = exp.get("title").and_then(|v| v.as_str()).unwrap_or("");
+ let company = exp.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
+ let duration = exp.get("duration").and_then(|v| v.as_str()).unwrap_or("");
+ let caption = exp.get("caption").and_then(|v| v.as_str()).unwrap_or("");
+ let desc = exp
+ .get("description")
+ .and_then(|v| v.as_str())
+ .unwrap_or("");
+ md.push_str(&format!("- **{title}**"));
+ if !company.is_empty() {
+ md.push_str(&format!(" at {company}"));
+ }
+ if !duration.is_empty() {
+ md.push_str(&format!(" ({duration})"));
+ }
+ if !caption.is_empty() {
+ md.push_str(&format!(" — {caption}"));
+ }
+ md.push('\n');
+ if !desc.is_empty() {
+ md.push_str(&format!(" {desc}\n"));
+ }
+ }
+ md.push('\n');
+ }
+ }
+
+ // Education
+ if let Some(edus) = data.get("educations").and_then(|v| v.as_array()) {
+ if !edus.is_empty() {
+ md.push_str("## Education\n\n");
+ for edu in edus {
+ let school = edu.get("title").and_then(|v| v.as_str()).unwrap_or("");
+ let degree = edu.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
+ md.push_str(&format!("- **{school}**"));
+ if !degree.is_empty() {
+ md.push_str(&format!(" — {degree}"));
+ }
+ md.push('\n');
+ }
+ md.push('\n');
+ }
+ }
+
+ // Languages
+ if let Some(langs) = data.get("languages").and_then(|v| v.as_array()) {
+ if !langs.is_empty() {
+ let names: Vec<&str> = langs
+ .iter()
+ .filter_map(|l| l.get("name").and_then(|v| v.as_str()))
+ .collect();
+ if !names.is_empty() {
+ md.push_str(&format!("Languages: {}\n\n", names.join(", ")));
+ }
+ }
+ }
+
+ // Volunteering
+ if let Some(vols) = data.get("volunteering").and_then(|v| v.as_array()) {
+ if !vols.is_empty() {
+ md.push_str("## Volunteering\n\n");
+ for vol in vols {
+ let title = vol.get("title").and_then(|v| v.as_str()).unwrap_or("");
+ let org = vol.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
+ md.push_str(&format!("- {title}"));
+ if !org.is_empty() {
+ md.push_str(&format!(" at {org}"));
+ }
+ md.push('\n');
+ }
+ md.push('\n');
+ }
+ }
+
+ md
+}
+
+// ── Internal helpers ─────────────────────────────────────────────────
diff --git a/crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_01.rs b/crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_01.rs
deleted file mode 100644
index 0a4801a0ef..0000000000
--- a/crates/openhuman-core/src/agent/learning/linkedin_enrichment_part_01.rs
+++ /dev/null
@@ -1,669 +0,0 @@
-use crate::config::Config;
-use crate::integrations::{build_client, IntegrationClient};
-use crate::memory::api::provider::MemoryProvider;
-use crate::memory::api::types::{MemoryTaint, NamespaceDocumentInput};
-use regex::Regex;
-use serde_json::json;
-use std::sync::{Arc, LazyLock};
-
-/// Apify actor slug for the LinkedIn profile scraper.
-const LINKEDIN_SCRAPER_ACTOR: &str = "dev_fusion/linkedin-profile-scraper";
-
-/// Regex that captures a LinkedIn username from profile URLs.
-///
-/// Matches both the canonical form (`linkedin.com/in/<slug>`) and the
-/// notification-email form (`linkedin.com/comm/in/<slug>`). The username
-/// is captured in group 1 so we can reconstruct a clean canonical URL.
-static LINKEDIN_USERNAME_RE: LazyLock<Regex> = LazyLock::new(|| {
- Regex::new(r"https?://(?:www\.)?linkedin\.com/(?:comm/)?in/([a-zA-Z0-9_-]+)").unwrap()
-});
-
-/// Build the canonical profile URL from a username slug.
-fn canonical_linkedin_url(username: &str) -> String {
- format!("https://www.linkedin.com/in/{username}")
-}
-
-/// Typed status for a pipeline stage.
-#[derive(Debug, Clone, serde::Serialize)]
-#[serde(rename_all = "lowercase")]
-pub enum StageStatus {
- Success,
- Failed,
- Skipped,
-}
-
-/// A single pipeline stage result, suitable for structured RPC responses.
-#[derive(Debug, Clone, serde::Serialize)]
-pub struct EnrichmentStage {
- pub id: String,
- pub status: StageStatus,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub detail: Option<String>,
-}
-
-/// Outcome of the full enrichment pipeline.
-#[derive(Debug)]
-pub struct LinkedInEnrichmentResult {
- /// The LinkedIn profile URL found in Gmail, if any.
- pub profile_url: Option<String>,
- /// Raw scraped profile JSON from Apify, if the scrape succeeded.
- pub profile_data: Option<serde_json::Value>,
- /// Typed stage results for structured consumption by the frontend.
- pub stages: Vec<EnrichmentStage>,
- /// Human-readable log lines for display.
- pub log: Vec<String>,
-}
-
-/// Run the full Gmail → LinkedIn → Apify enrichment pipeline.
-///
-/// `preset_profile_url` lets callers skip the Gmail-search stage and
-/// supply a profile URL they already discovered out-of-band — currently
-/// the frontend obtains one via the webview-driven
-/// `gmail_find_linkedin_profile_url` Tauri command, which uses the
-/// logged-in Gmail webview's CDP session instead of a Composio token.
-/// When `None`, the function falls back to the Composio-driven Gmail
-/// search at [`search_gmail_for_linkedin`] (which currently errors
-/// because Composio Gmail was removed; callers should pass `Some` until
-/// a Composio-free fallback ships).
-///
-/// Returns `Ok` with a result struct even if individual stages fail —
-/// partial progress is still useful. Only returns `Err` if we can't
-/// even build the integration client (i.e. user isn't signed in).
-pub async fn run_linkedin_enrichment(
- config: &Config,
- preset_profile_url: Option<String>,
-) -> anyhow::Result<LinkedInEnrichmentResult> {
- let mut result = LinkedInEnrichmentResult {
- profile_url: None,
- profile_data: None,
- stages: Vec::new(),
- log: Vec::new(),
- };
-
- // Short-circuit: if PROFILE.md is already on disk from a previous
- // enrichment run, skip the entire pipeline. The welcome agent reads
- // PROFILE.md straight from the workspace, so re-running stages 1-3
- // would just churn quota for the same output.
- let profile_path = config.workspace_dir.join("PROFILE.md");
- if profile_path.is_file() {
- tracing::info!(
- path = %profile_path.display(),
- "[linkedin_enrichment] PROFILE.md already exists — skipping pipeline"
- );
- result
- .log
- .push("PROFILE.md already exists — skipping enrichment.".into());
- for id in ["gmail-search", "apify-scrape", "build-profile"] {
- result.stages.push(EnrichmentStage {
- id: id.into(),
- status: StageStatus::Skipped,
- detail: Some("PROFILE.md already on disk".into()),
- });
- }
- return Ok(result);
- }
-
- let client = build_client(config)
- .ok_or_else(|| anyhow::anyhow!("no integration client — user not signed in"))?;
-
- // ── Stage 1: search Gmail for LinkedIn emails ───────────────────
- let profile_url = if let Some(url) = preset_profile_url {
- tracing::info!(url = %url, "[linkedin_enrichment] stage 1: using preset profile URL");
- result
- .log
- .push(format!("Using preset LinkedIn profile: {url}"));
- result.stages.push(EnrichmentStage {
- id: "gmail-search".into(),
- status: StageStatus::Success,
- detail: Some(url.clone()),
- });
- Some(url)
- } else {
- tracing::info!("[linkedin_enrichment] stage 1: searching Gmail for LinkedIn emails");
- result
- .log
- .push("Searching Gmail for LinkedIn emails...".into());
- match search_gmail_for_linkedin(config).await {
- Ok(Some(url)) => {
- tracing::info!(url = %url, "[linkedin_enrichment] found LinkedIn profile URL");
- result.log.push(format!("Found LinkedIn profile: {url}"));
- result.stages.push(EnrichmentStage {
- id: "gmail-search".into(),
- status: StageStatus::Success,
- detail: Some(url.clone()),
- });
- Some(url)
- }
- Ok(None) => {
- tracing::info!("[linkedin_enrichment] no LinkedIn profile URL found in emails");
- result
- .log
- .push("No LinkedIn profile URL found in emails.".into());
- result.stages.push(EnrichmentStage {
- id: "gmail-search".into(),
- status: StageStatus::Skipped,
- detail: Some("No LinkedIn profile URL found in emails".into()),
- });
- None
- }
- Err(e) => {
- tracing::warn!(error = %e, "[linkedin_enrichment] Gmail search failed");
- result.log.push(format!("Gmail search failed: {e}"));
- result.stages.push(EnrichmentStage {
- id: "gmail-search".into(),
- status: StageStatus::Failed,
- detail: Some(format!("Gmail search failed: {e}")),
- });
- None
- }
- }
- };
-
- result.profile_url = profile_url.clone();
-
- // ── Stage 2: scrape the LinkedIn profile via Apify ───────────────
- let Some(url) = profile_url else {
- result
- .log
- .push("Skipping LinkedIn scrape — no profile URL.".into());
- result.stages.push(EnrichmentStage {
- id: "apify-scrape".into(),
- status: StageStatus::Skipped,
- detail: Some("No profile URL to scrape".into()),
- });
- result.stages.push(EnrichmentStage {
- id: "build-profile".into(),
- status: StageStatus::Skipped,
- detail: Some("No profile data".into()),
- });
- return Ok(result);
- };
-
- tracing::info!(url = %url, "[linkedin_enrichment] stage 2: scraping LinkedIn profile via Apify");
- result.log.push("Scraping LinkedIn profile...".into());
-
- // Resolve the guarded memory driver once for all persist calls.
- let memory = match profile_memory_writer().await {
- Ok(m) => Some(m),
- Err(e) => {
- tracing::warn!(
- error = %e,
- "[linkedin_enrichment] memory driver unavailable, skipping memory persistence"
- );
- None
- }
- };
-
- match scrape_linkedin_profile(&client, &url).await {
- Ok(data) => {
- tracing::info!("[linkedin_enrichment] Apify scrape succeeded");
- result
- .log
- .push("LinkedIn profile scraped successfully.".into());
- result.stages.push(EnrichmentStage {
- id: "apify-scrape".into(),
- status: StageStatus::Success,
- detail: None,
- });
-
- // ── Stage 3: write PROFILE.md to workspace ──────────────
- tracing::info!("[linkedin_enrichment] stage 3: writing PROFILE.md");
- if let Err(e) = write_profile_md(config, &url, &data).await {
- tracing::warn!(error = %e, "[linkedin_enrichment] failed to write PROFILE.md");
- result.log.push(format!("Failed to write PROFILE.md: {e}"));
- result.stages.push(EnrichmentStage {
- id: "build-profile".into(),
- status: StageStatus::Failed,
- detail: Some(format!("{e}")),
- });
- } else {
- result.log.push("PROFILE.md written to workspace.".into());
- result.stages.push(EnrichmentStage {
- id: "build-profile".into(),
- status: StageStatus::Success,
- detail: Some("PROFILE.md written".into()),
- });
- }
-
- // Also persist to memory store for RAG retrieval.
- if let Some(ref mem) = memory {
- if let Err(e) = persist_linkedin_profile(mem, &url, &data).await {
- tracing::warn!(error = %e, "[linkedin_enrichment] failed to persist to memory");
- }
- }
-
- result.profile_data = Some(data);
- }
- Err(e) => {
- tracing::warn!(error = %e, "[linkedin_enrichment] Apify scrape failed");
- result.log.push(format!("LinkedIn scrape failed: {e}"));
- result.stages.push(EnrichmentStage {
- id: "apify-scrape".into(),
- status: StageStatus::Failed,
- detail: Some(format!("{e}")),
- });
- result.stages.push(EnrichmentStage {
- id: "build-profile".into(),
- status: StageStatus::Skipped,
- detail: Some("Scrape failed".into()),
- });
-
- // Still write a minimal PROFILE.md with just the URL.
- if let Err(e) = write_profile_md_url_only(config, &url) {
- tracing::warn!(error = %e, "[linkedin_enrichment] failed to write PROFILE.md");
- }
- if let Some(ref mem) = memory {
- let _ = persist_linkedin_url_only(mem, &url).await;
- }
- }
- }
-
- Ok(result)
-}
-
-// ── PROFILE.md generation ────────────────────────────────────────────
-
-/// Summarise the scraped LinkedIn data with an LLM, then write the
-/// result to `{workspace_dir}/PROFILE.md`. The prompt system picks this
-/// file up automatically on the next agent turn.
-async fn write_profile_md(
- config: &Config,
- url: &str,
- data: &serde_json::Value,
-) -> anyhow::Result<()> {
- // First render a full Markdown draft from the raw data.
- let raw_md = render_profile_markdown(url, data);
-
- // Then compress it through the LLM.
- let md = match summarise_profile_with_llm(config, &raw_md).await {
- Ok(summary) => summary,
- Err(e) => {
- tracing::warn!(
- error = %e,
- "[linkedin_enrichment] LLM summarisation failed, falling back to raw markdown"
- );
- raw_md
- }
- };
-
- let path = config.workspace_dir.join("PROFILE.md");
- if let Some(parent) = path.parent() {
- std::fs::create_dir_all(parent)?;
- }
- std::fs::write(&path, &md)?;
- tracing::info!(path = %path.display(), len = md.len(), "[linkedin_enrichment] wrote PROFILE.md");
- Ok(())
-}
-
-/// Ask the backend LLM to distil the raw LinkedIn Markdown into a
-/// concise, high-signal profile document suitable for agent context.
-pub async fn summarise_profile_with_llm(config: &Config, raw_md: &str) -> anyhow::Result<String> {
- let (model_chat, _) =
- crate::inference::provider::create_chat_model_from_string_with_model_id(
- "summarization",
- "openhuman",
- config,
- 0.3,
- )?;
-
- let system = "\
-You are a profile analyst. You will receive a user's LinkedIn profile in Markdown format. \
-Your job is to produce a concise PROFILE.md that an AI assistant will read to understand \
-who this user is.\n\n\
-Rules:\n\
-- Output clean Markdown with a `# User Profile` heading.\n\
-- Lead with name, headline, location, and LinkedIn URL.\n\
-- Summarise the About section in 2-3 sentences max.\n\
-- List only the most notable experiences (founder roles, leadership positions) — skip \
- short stints and minor roles.\n\
-- Include education, languages, and any standout achievements.\n\
-- Add a short `## Key facts for the assistant` section with 5-8 bullet points the AI \
- should know (e.g. expertise areas, industries, current focus, communication style hints).\n\
-- Keep the entire output under 400 words.\n\
-- Do not invent information — only use what is in the input.";
-
- let model = "summarization-v1";
-
- tracing::debug!(
- model = model,
- input_len = raw_md.len(),
- "[linkedin_enrichment] sending profile to LLM for summarisation"
- );
-
- use tinyinference::message::Message;
- use tinyinference::model::ModelRequest;
- let summary = model_chat
- .invoke(
- &(),
- ModelRequest::new(vec![
- Message::system(system),
- Message::user(raw_md.to_string()),
- ]),
- )
- .await?
- .text();
-
- tracing::debug!(
- output_len = summary.len(),
- "[linkedin_enrichment] LLM summarisation complete"
- );
-
- Ok(summary)
-}
-
-/// Minimal fallback when the Apify scrape failed but we have the URL.
-fn write_profile_md_url_only(config: &Config, url: &str) -> anyhow::Result<()> {
- let md = format!(
- "# User Profile\n\n\
- LinkedIn: {url}\n\n\
- _Full profile data was not available at onboarding time._\n"
- );
- let path = config.workspace_dir.join("PROFILE.md");
- if let Some(parent) = path.parent() {
- std::fs::create_dir_all(parent)?;
- }
- std::fs::write(&path, md)?;
- Ok(())
-}
-
-/// Turn the Apify scrape JSON into clean Markdown.
-pub fn render_profile_markdown(url: &str, data: &serde_json::Value) -> String {
- let s = |key: &str| {
- data.get(key)
- .and_then(|v| v.as_str())
- .unwrap_or("")
- .to_string()
- };
-
- let full_name = s("fullName");
- let headline = s("headline");
- let location = s("addressWithCountry");
- let about = s("about");
- let connections = data.get("connections").and_then(|v| v.as_u64());
- let followers = data.get("followers").and_then(|v| v.as_u64());
-
- let mut md = format!("# User Profile — {full_name}\n\n");
-
- if !headline.is_empty() {
- md.push_str(&format!("**{headline}**\n\n"));
- }
- if !location.is_empty() {
- md.push_str(&format!("Location: {location}\n\n"));
- }
- md.push_str(&format!("LinkedIn: {url}\n\n"));
- if let (Some(c), Some(f)) = (connections, followers) {
- md.push_str(&format!("Connections: {c} | Followers: {f}\n\n"));
- }
-
- if !about.is_empty() {
- md.push_str("## About\n\n");
- md.push_str(&about);
- md.push_str("\n\n");
- }
-
- // Experience
- if let Some(exps) = data.get("experiences").and_then(|v| v.as_array()) {
- if !exps.is_empty() {
- md.push_str("## Experience\n\n");
- for exp in exps {
- let title = exp.get("title").and_then(|v| v.as_str()).unwrap_or("");
- let company = exp.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
- let duration = exp.get("duration").and_then(|v| v.as_str()).unwrap_or("");
- let caption = exp.get("caption").and_then(|v| v.as_str()).unwrap_or("");
- let desc = exp
- .get("description")
- .and_then(|v| v.as_str())
- .unwrap_or("");
- md.push_str(&format!("- **{title}**"));
- if !company.is_empty() {
- md.push_str(&format!(" at {company}"));
- }
- if !duration.is_empty() {
- md.push_str(&format!(" ({duration})"));
- }
- if !caption.is_empty() {
- md.push_str(&format!(" — {caption}"));
- }
- md.push('\n');
- if !desc.is_empty() {
- md.push_str(&format!(" {desc}\n"));
- }
- }
- md.push('\n');
- }
- }
-
- // Education
- if let Some(edus) = data.get("educations").and_then(|v| v.as_array()) {
- if !edus.is_empty() {
- md.push_str("## Education\n\n");
- for edu in edus {
- let school = edu.get("title").and_then(|v| v.as_str()).unwrap_or("");
- let degree = edu.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
- md.push_str(&format!("- **{school}**"));
- if !degree.is_empty() {
- md.push_str(&format!(" — {degree}"));
- }
- md.push('\n');
- }
- md.push('\n');
- }
- }
-
- // Languages
- if let Some(langs) = data.get("languages").and_then(|v| v.as_array()) {
- if !langs.is_empty() {
- let names: Vec<&str> = langs
- .iter()
- .filter_map(|l| l.get("name").and_then(|v| v.as_str()))
- .collect();
- if !names.is_empty() {
- md.push_str(&format!("Languages: {}\n\n", names.join(", ")));
- }
- }
- }
-
- // Volunteering
- if let Some(vols) = data.get("volunteering").and_then(|v| v.as_array()) {
- if !vols.is_empty() {
- md.push_str("## Volunteering\n\n");
- for vol in vols {
- let title = vol.get("title").and_then(|v| v.as_str()).unwrap_or("");
- let org = vol.get("subtitle").and_then(|v| v.as_str()).unwrap_or("");
- md.push_str(&format!("- {title}"));
- if !org.is_empty() {
- md.push_str(&format!(" at {org}"));
- }
- md.push('\n');
- }
- md.push('\n');
- }
- }
-
- md
-}
-
-// ── Internal helpers ─────────────────────────────────────────────────
-
-/// Search Gmail via Composio for emails from linkedin.com and extract
-/// the user's own LinkedIn username.
-///
-/// LinkedIn notification emails embed `comm/in/<username>` links in the
-/// **HTML body** — which Gmail returns as base64-encoded data inside
-/// `payload.parts[].body.data`. We must decode those parts before
-/// regex-matching; searching the raw JSON alone misses them.
-async fn search_gmail_for_linkedin(config: &Config) -> anyhow::Result<Option<String>> {
- use crate::integrations::composio::client::{
- create_composio_client, direct_execute, ComposioClientKind,
- };
- use base64::engine::general_purpose::URL_SAFE_NO_PAD;
- use base64::Engine;
-
- // Resolve through the mode-aware factory so a direct-mode user
- // with a stored API key can still drive Gmail enrichment from the
- // personal Composio tenant (#1710 Wave 2). Pre-fix this path used
- // `build_composio_client` and returned early for any user without
- // a backend session, silently disabling LinkedIn enrichment for
- // direct-mode users even when their LinkedIn/Gmail connections
- // were healthy on app.composio.dev.
- let client_kind = create_composio_client(config)
- .map_err(|e| anyhow::anyhow!("composio client unavailable: {e}"))?;
-
- // `comm/in/<username>` — LinkedIn's own notification emails always use
- // this form to refer to the email *recipient's* profile.
- static COMM_RE: LazyLock<Regex> =
- LazyLock::new(|| Regex::new(r"linkedin\.com/comm/in/([a-zA-Z0-9_-]+)").unwrap());
-
- let args = json!({
- "query": "from:linkedin.com",
- "max_results": 10,
- });
- let resp = match &client_kind {
- ComposioClientKind::Backend(client) => client
- .execute_tool("GMAIL_FETCH_EMAILS", Some(args))
- .await
- .map_err(|e| anyhow::anyhow!("GMAIL_FETCH_EMAILS failed: {e:#}"))?,
- ComposioClientKind::Direct(direct) => {
- tracing::debug!(
- "[linkedin_enrichment][composio-direct] GMAIL_FETCH_EMAILS via direct tenant"
- );
- direct_execute(
- direct,
- "GMAIL_FETCH_EMAILS",
- Some(args),
- &config.composio.entity_id,
- None,
- )
- .await
- .map_err(|e| anyhow::anyhow!("GMAIL_FETCH_EMAILS (direct) failed: {e:#}"))?
- }
- };
-
- if !resp.successful {
- let err = resp.error.unwrap_or_else(|| "unknown error".into());
- anyhow::bail!("GMAIL_FETCH_EMAILS error: {err}");
- }
-
- // Walk the messages, decode HTML parts, and search for profile URLs.
- let messages = resp
- .data
- .get("messages")
- .and_then(|v| v.as_array())
- .cloned()
- .unwrap_or_default();
-
- for msg in &messages {
- // Collect all text to search: plain messageText + decoded HTML parts.
- let mut searchable = String::new();
-
- // Plain text body (already decoded by Composio).
- if let Some(text) = msg.get("messageText").and_then(|v| v.as_str()) {
- searchable.push_str(text);
- searchable.push('\n');
- }
-
- // Decode base64 HTML parts from payload.parts[].body.data.
- if let Some(parts) = msg.pointer("/payload/parts").and_then(|v| v.as_array()) {
- for part in parts {
- let is_html = part
- .get("mimeType")
- .and_then(|v| v.as_str())
- .is_some_and(|m| m.contains("html"));
- if !is_html {
- continue;
- }
- if let Some(b64) = part.pointer("/body/data").and_then(|v| v.as_str()) {
- if let Ok(bytes) = URL_SAFE_NO_PAD.decode(b64) {
- if let Ok(html) = String::from_utf8(bytes) {
- searchable.push_str(&html);
- searchable.push('\n');
- }
- }
- }
- }
- }
-
- // Priority 1: comm/in/<username> — always the recipient's own profile.
- if let Some(caps) = COMM_RE.captures(&searchable) {
- let username = caps[1].to_string();
- let url = canonical_linkedin_url(&username);
- tracing::info!(
- username = %username,
- url = %url,
- "[linkedin_enrichment] found own username via comm/in/ in HTML body"
- );
- return Ok(Some(url));
- }
-
- // Priority 2: canonical /in/<username> (some notification types).
- if let Some(caps) = LINKEDIN_USERNAME_RE.captures(&searchable) {
- let username = caps[1].to_string();
- let url = canonical_linkedin_url(&username);
- tracing::info!(
- username = %username,
- url = %url,
- "[linkedin_enrichment] found username via /in/ in email body"
- );
- return Ok(Some(url));
- }
- }
-
- Ok(None)
-}
-
-/// Call the Apify LinkedIn profile scraper synchronously and return the
-/// first profile item from the dataset.
-pub async fn scrape_linkedin_profile(
- client: &Arc<IntegrationClient>,
- profile_url: &str,
-) -> anyhow::Result<serde_json::Value> {
- let body = json!({
- "actorId": LINKEDIN_SCRAPER_ACTOR,
- "input": {
- "profileUrls": [profile_url],
- },
- "sync": true,
- "timeoutSecs": 120,
- });
-
- tracing::debug!(
- actor = LINKEDIN_SCRAPER_ACTOR,
- url_len = profile_url.len(),
- "[linkedin_enrichment] invoking Apify actor"
- );
-
- // The backend wraps the Apify response in its standard envelope.
- // `IntegrationClient::post` already unwraps `{ success, data }`.
- let resp: serde_json::Value = client
- .post("/agent-integrations/apify/run", &body)
- .await
- .map_err(|e| anyhow::anyhow!("Apify run failed: {e:#}"))?;
-
- let status = resp
- .get("status")
- .and_then(|v| v.as_str())
- .unwrap_or("UNKNOWN");
-
- if status != "SUCCEEDED" {
- anyhow::bail!("Apify run finished with status: {status}");
- }
-
- // Extract the first item from the inline results array.
- let items = resp
- .get("items")
- .and_then(|v| v.as_array())
- .ok_or_else(|| anyhow::anyhow!("Apify run returned no items array"))?;
-
- items
- .first()
- .cloned()
- .ok_or_else(|| anyhow::anyhow!("Apify run returned an empty items array"))
-}
-
-/// The namespace scraped LinkedIn profiles are persisted into.
-///
-/// `MemoryClient::store_skill_sync("user-profile", ...)` derived this by
-/// prefixing the skill id with `skill-`; that derivation is one line of host
-/// policy, so it moves here rather than needing a contract member (#5560). The
-/// literal must not drift — it is what onboarding RAG recalls against.
-const PROFILE_MEMORY_NAMESPACE: &str = "skill-user-profile";