@odla-ai/kg 0.4.1 → 0.6.0
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/README.md +174 -0
- package/dist/index.cjs +683 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +555 -2
- package/dist/index.d.ts +555 -2
- package/dist/index.js +683 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -345,6 +345,15 @@ function findConnectors(fragment, config) {
|
|
|
345
345
|
function slugify(input) {
|
|
346
346
|
return input.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60).replace(/-+$/g, "");
|
|
347
347
|
}
|
|
348
|
+
var LEGAL_SUFFIXES = /* @__PURE__ */ new Set(["co", "company", "corp", "corporation", "inc", "incorporated", "llc", "llp", "ltd", "limited", "pllc"]);
|
|
349
|
+
function identityKey(value) {
|
|
350
|
+
const tokens = value.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/&/g, " and ").match(/[a-z0-9]+/g) ?? [];
|
|
351
|
+
while (tokens.length > 1 && LEGAL_SUFFIXES.has(tokens.at(-1) ?? "")) {
|
|
352
|
+
tokens.pop();
|
|
353
|
+
if (tokens.at(-1) === "and") tokens.pop();
|
|
354
|
+
}
|
|
355
|
+
return tokens.join("");
|
|
356
|
+
}
|
|
348
357
|
function normalizeUrl(raw) {
|
|
349
358
|
try {
|
|
350
359
|
const u = new URL(raw);
|
|
@@ -487,6 +496,213 @@ function locationFragment(slug, name, fips) {
|
|
|
487
496
|
};
|
|
488
497
|
}
|
|
489
498
|
|
|
499
|
+
// src/enrich/verify.ts
|
|
500
|
+
var WRITABLE_VERDICTS = ["confirmed", "probable"];
|
|
501
|
+
var ALL = ["confirmed", "probable", "ambiguous", "not_found", "wrong_entity"];
|
|
502
|
+
function isVerdict(value) {
|
|
503
|
+
return typeof value === "string" && ALL.includes(value);
|
|
504
|
+
}
|
|
505
|
+
function isWritable(verdict) {
|
|
506
|
+
return WRITABLE_VERDICTS.includes(verdict);
|
|
507
|
+
}
|
|
508
|
+
function normalizeVerification(draft, retrievedAt) {
|
|
509
|
+
const d = draft ?? {};
|
|
510
|
+
const verdict = isVerdict(d.verdict) ? d.verdict : "ambiguous";
|
|
511
|
+
const raw = typeof d.confidence === "number" && Number.isFinite(d.confidence) ? d.confidence : 0;
|
|
512
|
+
const confidence = Math.min(1, Math.max(0, raw));
|
|
513
|
+
const evidence = [];
|
|
514
|
+
for (const item of Array.isArray(d.evidence) ? d.evidence.slice(0, 12) : []) {
|
|
515
|
+
const e = item ?? {};
|
|
516
|
+
const sourceUrl = typeof e.sourceUrl === "string" ? e.sourceUrl.trim() : "";
|
|
517
|
+
const claim = typeof e.claim === "string" ? e.claim.trim() : "";
|
|
518
|
+
if (!claim || !isHttpUrl(sourceUrl)) continue;
|
|
519
|
+
evidence.push({ claim: claim.slice(0, 300), sourceUrl, ...retrievedAt ? { retrievedAt } : {} });
|
|
520
|
+
}
|
|
521
|
+
if (evidence.length === 0 && isWritable(verdict)) return { verdict: "ambiguous", confidence, evidence };
|
|
522
|
+
return { verdict, confidence, evidence };
|
|
523
|
+
}
|
|
524
|
+
function isHttpUrl(value) {
|
|
525
|
+
try {
|
|
526
|
+
const u = new URL(value);
|
|
527
|
+
return u.protocol === "https:" || u.protocol === "http:";
|
|
528
|
+
} catch {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/enrich/person.ts
|
|
534
|
+
var LINK_KINDS = ["website", "blog", "substack", "linkedin", "x", "bluesky", "github", "other"];
|
|
535
|
+
var CANONICAL_HOSTS = {
|
|
536
|
+
linkedin: ["linkedin.com"],
|
|
537
|
+
github: ["github.com"],
|
|
538
|
+
x: ["x.com", "twitter.com"],
|
|
539
|
+
bluesky: ["bsky.app"]
|
|
540
|
+
};
|
|
541
|
+
var PLACEHOLDER_HOSTS = ["example.com", "example.org", "example.net", "yourdomain.com", "domain.com"];
|
|
542
|
+
var PERSON_SEARCH_SYSTEM = "You are a research assistant with web search, confirming the identity of a specific named person and finding their PUBLIC PROFESSIONAL presence. Look for: a personal website, blog, or Substack; LinkedIn, X/Bluesky, or GitHub profiles; their current employer and role; and a business email or phone that they or their employer have published openly (a company contact page, a conference bio). Do NOT look for or report home addresses, personal phone numbers, or personal email addresses. Also report disambiguating facts: city or region, alma mater, and other companies named alongside them. Names are not unique. If the results describe more than one person with this name and you cannot tell which is meant, say so explicitly. If you find a different person with the same name, say that too. Cite the URL for every claim. Never guess a profile URL from a name pattern \u2014 report only URLs you actually saw. If you found nothing, say so plainly.";
|
|
543
|
+
var PERSON_EXTRACT_SYSTEM = "Extract the structured profile from the research notes. Use null for anything the notes do not establish. Set verdict to 'confirmed' only when the notes identify this exact person beyond doubt; 'probable' when the evidence is strong but circumstantial; 'ambiguous' when several same-named people cannot be told apart; 'not_found' when nothing was established; 'wrong_entity' when the notes describe a different person with the same name. Every claim in evidence must carry the source URL it came from.";
|
|
544
|
+
var PERSON_EXTRACT_TOOL = {
|
|
545
|
+
name: "person_profile",
|
|
546
|
+
description: "Verified identity plus public professional profile for one person",
|
|
547
|
+
parameters: {
|
|
548
|
+
type: "object",
|
|
549
|
+
properties: {
|
|
550
|
+
verdict: { type: "string", enum: ["confirmed", "probable", "ambiguous", "not_found", "wrong_entity"] },
|
|
551
|
+
confidence: { type: "number", description: "0..1 \u2014 how sure you are this is the right person" },
|
|
552
|
+
links: {
|
|
553
|
+
type: "array",
|
|
554
|
+
description: "Only URLs actually seen in the results; never a guessed profile pattern.",
|
|
555
|
+
items: {
|
|
556
|
+
type: "object",
|
|
557
|
+
properties: {
|
|
558
|
+
kind: { type: "string", enum: [...LINK_KINDS] },
|
|
559
|
+
url: { type: "string" }
|
|
560
|
+
},
|
|
561
|
+
required: ["kind", "url"]
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
employer: { type: ["string", "null"], description: "Current employer name" },
|
|
565
|
+
role: { type: ["string", "null"], description: "Current title at that employer" },
|
|
566
|
+
businessEmail: { type: ["string", "null"], description: "Publicly published work email only" },
|
|
567
|
+
businessPhone: { type: ["string", "null"], description: "Publicly published work phone only" },
|
|
568
|
+
city: { type: ["string", "null"] },
|
|
569
|
+
region: { type: ["string", "null"], description: "State, province, or country" },
|
|
570
|
+
almaMater: { type: ["string", "null"] },
|
|
571
|
+
otherCompanies: { type: "array", items: { type: "string" }, description: "Companies named alongside them" },
|
|
572
|
+
evidence: {
|
|
573
|
+
type: "array",
|
|
574
|
+
items: {
|
|
575
|
+
type: "object",
|
|
576
|
+
properties: { claim: { type: "string" }, sourceUrl: { type: "string" } },
|
|
577
|
+
required: ["claim", "sourceUrl"]
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
required: ["verdict", "confidence", "links", "evidence"]
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
function personPrompt(person) {
|
|
585
|
+
const at = person.companyName ? ` (${person.role ? `${person.role}, ` : ""}${person.companyName})` : "";
|
|
586
|
+
const where = [person.city, person.region].filter(Boolean).join(", ");
|
|
587
|
+
return `Person: ${person.name}${at}${where ? `
|
|
588
|
+
Known location: ${where}` : ""}
|
|
589
|
+
|
|
590
|
+
Confirm this is a real, identifiable person and find their public professional presence.`;
|
|
591
|
+
}
|
|
592
|
+
function validateLinks(raw, limit = 8) {
|
|
593
|
+
const out = [];
|
|
594
|
+
const seen = /* @__PURE__ */ new Set();
|
|
595
|
+
for (const item of (raw ?? []).slice(0, 24)) {
|
|
596
|
+
const kind = LINK_KINDS.includes(String(item?.kind)) ? item.kind : "other";
|
|
597
|
+
const url = String(item?.url ?? "").trim();
|
|
598
|
+
const parsed = httpsUrl(url);
|
|
599
|
+
if (!parsed) continue;
|
|
600
|
+
const host = parsed.hostname.toLowerCase().replace(/^www\./, "");
|
|
601
|
+
if (PLACEHOLDER_HOSTS.includes(host)) continue;
|
|
602
|
+
const required = CANONICAL_HOSTS[kind];
|
|
603
|
+
if (required && !required.some((h) => host === h || host.endsWith(`.${h}`))) continue;
|
|
604
|
+
if (seen.has(url)) continue;
|
|
605
|
+
seen.add(url);
|
|
606
|
+
out.push({ kind, url });
|
|
607
|
+
if (out.length >= limit) break;
|
|
608
|
+
}
|
|
609
|
+
return out;
|
|
610
|
+
}
|
|
611
|
+
function personToFragment(person, draft, retrievedAt) {
|
|
612
|
+
const verification = normalizeVerification(draft, retrievedAt);
|
|
613
|
+
const links = validateLinks(draft.links);
|
|
614
|
+
if (!isWritable(verification.verdict)) return { verification, links, fragment: { nodes: [], edges: [] } };
|
|
615
|
+
const attrs = { name: person.name };
|
|
616
|
+
for (const link of links) if (!attrs[link.kind]) attrs[link.kind] = link.url;
|
|
617
|
+
const city = str(draft.city, 60);
|
|
618
|
+
if (city && !person.city) attrs.city = city;
|
|
619
|
+
const region = str(draft.region, 60);
|
|
620
|
+
if (region && !person.region) attrs.region = region;
|
|
621
|
+
const email = str(draft.businessEmail, 120);
|
|
622
|
+
if (email && /^[^@\s]+@[^@\s.]+\.[^@\s]+$/.test(email)) attrs.businessEmail = email;
|
|
623
|
+
const phone = str(draft.businessPhone, 40);
|
|
624
|
+
if (phone) attrs.businessPhone = phone;
|
|
625
|
+
const almaMater = str(draft.almaMater, 120);
|
|
626
|
+
if (almaMater) attrs.almaMater = almaMater;
|
|
627
|
+
const nodes = [{ type: "person", key: person.slug, attrs }];
|
|
628
|
+
const edges = [];
|
|
629
|
+
const employer = str(draft.employer, 120);
|
|
630
|
+
const known = person.companySlug && person.companyName && identityKey(person.companyName) === identityKey(employer);
|
|
631
|
+
const companySlug = employer ? known ? person.companySlug : slugify(employer) : "";
|
|
632
|
+
if (employer && companySlug) {
|
|
633
|
+
const key = `${person.slug}~${companySlug}`;
|
|
634
|
+
nodes.push({ type: "company", key: companySlug, attrs: { name: known ? person.companyName : employer } });
|
|
635
|
+
nodes.push({ type: "affiliation", key, attrs: { role: str(draft.role, 60) || person.role || "" } });
|
|
636
|
+
edges.push({ from: { type: "affiliation", key }, to: { type: "person", key: person.slug }, label: "of_person" });
|
|
637
|
+
edges.push({ from: { type: "affiliation", key }, to: { type: "company", key: companySlug }, label: "of_company" });
|
|
638
|
+
}
|
|
639
|
+
return { verification, links, fragment: { nodes, edges } };
|
|
640
|
+
}
|
|
641
|
+
function str(value, max) {
|
|
642
|
+
return typeof value === "string" ? value.trim().slice(0, max) : "";
|
|
643
|
+
}
|
|
644
|
+
function httpsUrl(value) {
|
|
645
|
+
try {
|
|
646
|
+
const url = new URL(value);
|
|
647
|
+
return url.protocol === "https:" ? url : null;
|
|
648
|
+
} catch {
|
|
649
|
+
return null;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// src/enrich/grade.ts
|
|
654
|
+
var DEFAULT_STANDARD = {
|
|
655
|
+
minimumSources: 2,
|
|
656
|
+
minimumFacts: 3,
|
|
657
|
+
anchors: ["first-party", "authoritative"],
|
|
658
|
+
anchorRequiredForMedium: false
|
|
659
|
+
};
|
|
660
|
+
function independentHosts(sources) {
|
|
661
|
+
const hosts = /* @__PURE__ */ new Set();
|
|
662
|
+
for (const source of sources) {
|
|
663
|
+
try {
|
|
664
|
+
hosts.add(new URL(source.sourceUrl).hostname.toLowerCase().replace(/^www\./, ""));
|
|
665
|
+
} catch {
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return [...hosts];
|
|
669
|
+
}
|
|
670
|
+
function gradeSources(sources, standard = DEFAULT_STANDARD) {
|
|
671
|
+
const supporting = sources.filter((s) => s.corroboration === "confirmed" || s.corroboration === "name-only");
|
|
672
|
+
const contradicting = sources.filter((s) => s.corroboration === "absent");
|
|
673
|
+
const unreadable = sources.filter((s) => s.corroboration === "unreachable" || s.corroboration === "unchecked");
|
|
674
|
+
const facts = [...new Set(supporting.flatMap((s) => s.facts))];
|
|
675
|
+
const hosts = independentHosts(supporting);
|
|
676
|
+
const hasAnchor = supporting.some((s) => standard.anchors.includes(s.sourceClass));
|
|
677
|
+
const base = {
|
|
678
|
+
independentSources: hosts.length,
|
|
679
|
+
factsCorroborated: facts,
|
|
680
|
+
hasAnchor,
|
|
681
|
+
contradictions: contradicting.map((s) => s.sourceUrl)
|
|
682
|
+
};
|
|
683
|
+
if (supporting.length === 0) {
|
|
684
|
+
return unreadable.length > 0 && contradicting.length === 0 ? { ...base, grade: "unverifiable", reason: `no source could be read (${unreadable.length} unreachable)` } : { ...base, grade: "unresolved", reason: "no source corroborated the subject" };
|
|
685
|
+
}
|
|
686
|
+
if (base.contradictions.length > 0) {
|
|
687
|
+
return { ...base, grade: "unresolved", reason: `${base.contradictions.length} source(s) named someone else` };
|
|
688
|
+
}
|
|
689
|
+
if (hosts.length >= standard.minimumSources && hasAnchor && facts.length >= standard.minimumFacts) {
|
|
690
|
+
return { ...base, grade: "high", reason: `${hosts.length} independent sources, anchored, agreeing on ${facts.length} facts` };
|
|
691
|
+
}
|
|
692
|
+
if (hosts.length >= standard.minimumSources && (hasAnchor || !standard.anchorRequiredForMedium)) {
|
|
693
|
+
return {
|
|
694
|
+
...base,
|
|
695
|
+
grade: "medium",
|
|
696
|
+
reason: hasAnchor ? `${hosts.length} sources but only ${facts.length} corroborated facts` : `${hosts.length} sources, none first-party or authoritative`
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
return { ...base, grade: "unresolved", reason: hosts.length < standard.minimumSources ? "only one independent source" : "no anchoring source" };
|
|
700
|
+
}
|
|
701
|
+
var ACTIONABLE = ["high", "medium"];
|
|
702
|
+
function isActionable(value) {
|
|
703
|
+
return ACTIONABLE.includes(value);
|
|
704
|
+
}
|
|
705
|
+
|
|
490
706
|
// src/enrich/run.ts
|
|
491
707
|
async function enrichCompany(ai, models, company) {
|
|
492
708
|
const where = `${company.city ? `, ${company.city}` : ""}${company.region ? ` (${company.region})` : ""}`;
|
|
@@ -510,6 +726,27 @@ ${text}`,
|
|
|
510
726
|
const { value } = await extractOnce().catch(extractOnce);
|
|
511
727
|
return factsToFragment(company, value);
|
|
512
728
|
}
|
|
729
|
+
async function enrichPerson(ai, models, person, retrievedAt) {
|
|
730
|
+
const { text, citations } = await ai.search({
|
|
731
|
+
model: models.search,
|
|
732
|
+
system: PERSON_SEARCH_SYSTEM,
|
|
733
|
+
user: personPrompt(person),
|
|
734
|
+
maxTokens: 1200
|
|
735
|
+
});
|
|
736
|
+
const extractOnce = () => ai.extract({
|
|
737
|
+
model: models.extract,
|
|
738
|
+
system: PERSON_EXTRACT_SYSTEM,
|
|
739
|
+
user: `Person: ${person.name}
|
|
740
|
+
|
|
741
|
+
Notes:
|
|
742
|
+
${text}`,
|
|
743
|
+
tool: PERSON_EXTRACT_TOOL,
|
|
744
|
+
maxTokens: 4e3
|
|
745
|
+
// reasoning headroom before the forced tool call
|
|
746
|
+
});
|
|
747
|
+
const { value } = await extractOnce().catch(extractOnce);
|
|
748
|
+
return { ...personToFragment(person, value, retrievedAt), citations, notes: text };
|
|
749
|
+
}
|
|
513
750
|
async function resolveCounties(ai, model, rows, gazetteer) {
|
|
514
751
|
const { value } = await ai.extract({
|
|
515
752
|
model,
|
|
@@ -527,6 +764,409 @@ async function resolveCounties(ai, model, rows, gazetteer) {
|
|
|
527
764
|
return out;
|
|
528
765
|
}
|
|
529
766
|
|
|
767
|
+
// src/enrich/match.ts
|
|
768
|
+
function fold(value) {
|
|
769
|
+
return value.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
|
|
770
|
+
}
|
|
771
|
+
function mentionsName(haystack, name) {
|
|
772
|
+
const tokens = fold(name).split(" ").filter((t) => t.length > 2);
|
|
773
|
+
if (tokens.length < 2) return false;
|
|
774
|
+
const words = haystack.split(" ");
|
|
775
|
+
const positions = /* @__PURE__ */ new Map();
|
|
776
|
+
for (const [i, word] of words.entries()) {
|
|
777
|
+
if (tokens.includes(word)) positions.set(word, [...positions.get(word) ?? [], i]);
|
|
778
|
+
}
|
|
779
|
+
if (tokens.some((t) => !positions.has(t))) return false;
|
|
780
|
+
const span = tokens.length;
|
|
781
|
+
const anchors = positions.get(tokens[0]) ?? [];
|
|
782
|
+
return anchors.some(
|
|
783
|
+
(anchor) => tokens.every((token) => (positions.get(token) ?? []).some((at) => Math.abs(at - anchor) <= span))
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
function snippetAround(text, name, width = 240) {
|
|
787
|
+
const hay = fold(text);
|
|
788
|
+
const first = fold(name).split(" ")[0] ?? "";
|
|
789
|
+
const at = hay.indexOf(first);
|
|
790
|
+
if (at < 0) return "";
|
|
791
|
+
const start = Math.max(0, at - width / 2);
|
|
792
|
+
return text.slice(start, start + width).replace(/\s+/g, " ").trim();
|
|
793
|
+
}
|
|
794
|
+
function stripMarkup(html) {
|
|
795
|
+
return html.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<style[\s\S]*?<\/style>/gi, " ").replace(/<[^>]+>/g, " ").replace(/ /gi, " ").replace(/&/gi, "&").replace(/\s+/g, " ").slice(0, 2e5);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// src/enrich/strategy.ts
|
|
799
|
+
async function investigatePerson(context, strategies, standard = DEFAULT_STANDARD) {
|
|
800
|
+
const skipped = [];
|
|
801
|
+
const runnable = [];
|
|
802
|
+
for (const strategy of strategies) {
|
|
803
|
+
if (strategy.applicable(context)) runnable.push(strategy);
|
|
804
|
+
else skipped.push(strategy.name);
|
|
805
|
+
}
|
|
806
|
+
const findings = await Promise.all(
|
|
807
|
+
runnable.map(async (strategy) => {
|
|
808
|
+
try {
|
|
809
|
+
return await strategy.run(context);
|
|
810
|
+
} catch (err) {
|
|
811
|
+
return {
|
|
812
|
+
strategy: strategy.name,
|
|
813
|
+
sources: [],
|
|
814
|
+
links: [],
|
|
815
|
+
notes: "",
|
|
816
|
+
error: err instanceof Error ? err.message : String(err)
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
})
|
|
820
|
+
);
|
|
821
|
+
const sources = dedupeBy(findings.flatMap((f) => f.sources), (s) => s.sourceUrl);
|
|
822
|
+
const links = dedupeBy(findings.flatMap((f) => f.links), (l) => l.url);
|
|
823
|
+
const report = gradeSources(sources, standard);
|
|
824
|
+
return { person: context.person, grade: report.grade, report, sources, links, findings, skipped };
|
|
825
|
+
}
|
|
826
|
+
function dedupeBy(items, key) {
|
|
827
|
+
const seen = /* @__PURE__ */ new Map();
|
|
828
|
+
for (const item of items) {
|
|
829
|
+
const at = key(item);
|
|
830
|
+
if (!seen.has(at)) seen.set(at, item);
|
|
831
|
+
}
|
|
832
|
+
return [...seen.values()];
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// src/enrich/strategies/org-site.ts
|
|
836
|
+
var TEAM_PATHS = [
|
|
837
|
+
"/team",
|
|
838
|
+
"/about",
|
|
839
|
+
"/about-us",
|
|
840
|
+
"/our-team",
|
|
841
|
+
"/leadership",
|
|
842
|
+
"/people",
|
|
843
|
+
"/staff",
|
|
844
|
+
"/who-we-are",
|
|
845
|
+
"/company/team",
|
|
846
|
+
"/press",
|
|
847
|
+
"/news"
|
|
848
|
+
];
|
|
849
|
+
var PEOPLE_WORDS = [
|
|
850
|
+
"team",
|
|
851
|
+
"about",
|
|
852
|
+
"leadership",
|
|
853
|
+
"staff",
|
|
854
|
+
"people",
|
|
855
|
+
"who we are",
|
|
856
|
+
"our story",
|
|
857
|
+
"founders",
|
|
858
|
+
"management",
|
|
859
|
+
"board",
|
|
860
|
+
"press",
|
|
861
|
+
"news",
|
|
862
|
+
"advisors",
|
|
863
|
+
"bios"
|
|
864
|
+
];
|
|
865
|
+
function rootOf(website) {
|
|
866
|
+
try {
|
|
867
|
+
return new URL(website.startsWith("http") ? website : `https://${website}`).origin;
|
|
868
|
+
} catch {
|
|
869
|
+
return null;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
function candidatePages(website, limit = 4) {
|
|
873
|
+
const origin = rootOf(website);
|
|
874
|
+
if (!origin) return [];
|
|
875
|
+
return TEAM_PATHS.map((p) => `${origin}${p}`).slice(0, limit);
|
|
876
|
+
}
|
|
877
|
+
function peopleLinks(html, origin, limit = 5) {
|
|
878
|
+
const found = /* @__PURE__ */ new Map();
|
|
879
|
+
const anchor = /<a\b[^>]*href=["']([^"']+)["'][^>]*>([\s\S]{0,200}?)<\/a>/gi;
|
|
880
|
+
for (const match of html.matchAll(anchor)) {
|
|
881
|
+
let url;
|
|
882
|
+
try {
|
|
883
|
+
url = new URL(match[1] ?? "", origin);
|
|
884
|
+
} catch {
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
if (url.origin !== origin) continue;
|
|
888
|
+
const text = (match[2] ?? "").replace(/<[^>]+>/g, " ").toLowerCase();
|
|
889
|
+
const rank = PEOPLE_WORDS.findIndex((w) => `${text} ${url.pathname.toLowerCase()}`.includes(w));
|
|
890
|
+
if (rank < 0) continue;
|
|
891
|
+
const clean = `${url.origin}${url.pathname}`.replace(/\/$/, "");
|
|
892
|
+
if (clean === origin) continue;
|
|
893
|
+
const prior = found.get(clean);
|
|
894
|
+
if (prior === void 0 || rank < prior) found.set(clean, rank);
|
|
895
|
+
}
|
|
896
|
+
return [...found.entries()].sort((a, b) => a[1] - b[1]).slice(0, limit).map(([url]) => url);
|
|
897
|
+
}
|
|
898
|
+
function judgeOrgPage(name, url, text) {
|
|
899
|
+
const named = mentionsName(fold(text), name);
|
|
900
|
+
return { url, named, snippet: named ? snippetAround(text, name) : "" };
|
|
901
|
+
}
|
|
902
|
+
var orgSiteStrategy = {
|
|
903
|
+
name: "org-site",
|
|
904
|
+
applicable(context) {
|
|
905
|
+
return Boolean(context.readPage && rootOf(context.person.companyWebsite ?? ""));
|
|
906
|
+
},
|
|
907
|
+
async run(context) {
|
|
908
|
+
const { person, readPage, retrievedAt } = context;
|
|
909
|
+
const origin = rootOf(person.companyWebsite ?? "");
|
|
910
|
+
if (!origin || !readPage) {
|
|
911
|
+
return { strategy: "org-site", sources: [], links: [], notes: "no employer website to read" };
|
|
912
|
+
}
|
|
913
|
+
const hits = [];
|
|
914
|
+
let read = 0;
|
|
915
|
+
const visit = async (url) => {
|
|
916
|
+
const html = await readPage(url);
|
|
917
|
+
if (html === null) return null;
|
|
918
|
+
read += 1;
|
|
919
|
+
const hit = judgeOrgPage(person.name, url, stripMarkup(html));
|
|
920
|
+
if (hit.named) hits.push(hit);
|
|
921
|
+
return html;
|
|
922
|
+
};
|
|
923
|
+
const home = await visit(origin);
|
|
924
|
+
const followable = home && hits.length === 0 ? peopleLinks(home, origin) : [];
|
|
925
|
+
const fallback = read <= 1 ? candidatePages(person.companyWebsite ?? "") : [];
|
|
926
|
+
for (const url of [...followable, ...fallback]) {
|
|
927
|
+
if (hits.length > 0) break;
|
|
928
|
+
await visit(url);
|
|
929
|
+
}
|
|
930
|
+
const sources = hits.map((hit) => ({
|
|
931
|
+
sourceUrl: hit.url,
|
|
932
|
+
sourceClass: "first-party",
|
|
933
|
+
facts: ["employer", "affiliation"],
|
|
934
|
+
verifiedOn: retrievedAt,
|
|
935
|
+
corroboration: "confirmed"
|
|
936
|
+
}));
|
|
937
|
+
return {
|
|
938
|
+
strategy: "org-site",
|
|
939
|
+
sources,
|
|
940
|
+
links: [],
|
|
941
|
+
notes: hits[0] ? `named on ${hits[0].url}
|
|
942
|
+
|
|
943
|
+
${hits[0].snippet}` : read > 0 ? `read ${read} page(s) on the employer's site; none named them` : "no page on the employer's site could be read"
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
// src/enrich/strategies/research.ts
|
|
949
|
+
var SELF_ASSERTED_HOSTS = ["linkedin.com", "x.com", "twitter.com", "bsky.app", "github.com", "substack.com"];
|
|
950
|
+
function hostOf(url) {
|
|
951
|
+
try {
|
|
952
|
+
return new URL(url).hostname.toLowerCase().replace(/^www\./, "");
|
|
953
|
+
} catch {
|
|
954
|
+
return "";
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
function classifySource(url, companyWebsite) {
|
|
958
|
+
const host = hostOf(url);
|
|
959
|
+
if (!host) return "third-party";
|
|
960
|
+
const company = companyWebsite ? hostOf(companyWebsite.startsWith("http") ? companyWebsite : `https://${companyWebsite}`) : "";
|
|
961
|
+
if (company && (host === company || host.endsWith(`.${company}`))) return "first-party";
|
|
962
|
+
if (SELF_ASSERTED_HOSTS.some((h) => host === h || host.endsWith(`.${h}`))) return "self-asserted";
|
|
963
|
+
return "third-party";
|
|
964
|
+
}
|
|
965
|
+
function isAttributed(citation) {
|
|
966
|
+
return Boolean(citation.citedText || citation.startIndex !== void 0 || citation.endIndex !== void 0);
|
|
967
|
+
}
|
|
968
|
+
var researchStrategy = {
|
|
969
|
+
name: "research",
|
|
970
|
+
applicable(context) {
|
|
971
|
+
return Boolean(context.ai && context.models?.search && context.models?.extract);
|
|
972
|
+
},
|
|
973
|
+
async run(context) {
|
|
974
|
+
const { ai, models, person, retrievedAt } = context;
|
|
975
|
+
if (!ai || !models) return { strategy: "research", sources: [], links: [], notes: "no models available" };
|
|
976
|
+
const result = await enrichPerson(ai, models, person, retrievedAt);
|
|
977
|
+
const sources = result.citations.map((citation) => {
|
|
978
|
+
const corroboration = isAttributed(citation) ? "name-only" : "unchecked";
|
|
979
|
+
return {
|
|
980
|
+
sourceUrl: citation.url,
|
|
981
|
+
sourceClass: classifySource(citation.url, person.companyWebsite),
|
|
982
|
+
facts: ["identity"],
|
|
983
|
+
verifiedOn: retrievedAt,
|
|
984
|
+
corroboration
|
|
985
|
+
};
|
|
986
|
+
});
|
|
987
|
+
return {
|
|
988
|
+
strategy: "research",
|
|
989
|
+
sources,
|
|
990
|
+
links: result.links,
|
|
991
|
+
notes: `[${result.verification.verdict}] ${result.notes}`
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
// src/enrich/strategies/targeted-profile.ts
|
|
997
|
+
var PROFILE_HOSTS = {
|
|
998
|
+
"linkedin.com": /\/in\/[^/?#]+/i,
|
|
999
|
+
"github.com": /^\/[^/?#]+\/?$/i,
|
|
1000
|
+
"bsky.app": /\/profile\/[^/?#]+/i,
|
|
1001
|
+
"x.com": /^\/[^/?#]+\/?$/i,
|
|
1002
|
+
"twitter.com": /^\/[^/?#]+\/?$/i
|
|
1003
|
+
};
|
|
1004
|
+
function kindOf(host) {
|
|
1005
|
+
if (host.startsWith("linkedin")) return "linkedin";
|
|
1006
|
+
if (host.startsWith("github")) return "github";
|
|
1007
|
+
if (host.startsWith("bsky")) return "bluesky";
|
|
1008
|
+
if (host.startsWith("x.") || host.startsWith("twitter")) return "x";
|
|
1009
|
+
return "other";
|
|
1010
|
+
}
|
|
1011
|
+
function targetedQuery(person, platform = "linkedin") {
|
|
1012
|
+
const company = person.companyName ? ` "${person.companyName}"` : "";
|
|
1013
|
+
return `"${person.name}"${company} ${platform}`;
|
|
1014
|
+
}
|
|
1015
|
+
function candidatesFrom(citations) {
|
|
1016
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1017
|
+
for (const citation of citations) {
|
|
1018
|
+
let url;
|
|
1019
|
+
try {
|
|
1020
|
+
url = new URL(citation.url);
|
|
1021
|
+
} catch {
|
|
1022
|
+
continue;
|
|
1023
|
+
}
|
|
1024
|
+
const host = url.hostname.toLowerCase().replace(/^www\./, "").replace(/^[a-z]{2}\./, "");
|
|
1025
|
+
const match = PROFILE_HOSTS[host]?.exec(url.pathname);
|
|
1026
|
+
if (!match) continue;
|
|
1027
|
+
const handle = match[0].replace(/\/+$/, "").toLowerCase();
|
|
1028
|
+
if (!seen.has(`${host}${handle}`)) seen.set(`${host}${handle}`, { url: citation.url, host, handle });
|
|
1029
|
+
}
|
|
1030
|
+
return [...seen.values()];
|
|
1031
|
+
}
|
|
1032
|
+
var targetedProfileStrategy = {
|
|
1033
|
+
name: "targeted-profile",
|
|
1034
|
+
applicable(context) {
|
|
1035
|
+
return Boolean(context.ai && context.models?.search);
|
|
1036
|
+
},
|
|
1037
|
+
async run(context) {
|
|
1038
|
+
const { ai, models, person, retrievedAt } = context;
|
|
1039
|
+
if (!ai || !models) return { strategy: "targeted-profile", sources: [], links: [], notes: "no search model" };
|
|
1040
|
+
const query = targetedQuery(person);
|
|
1041
|
+
const { citations } = await ai.search({
|
|
1042
|
+
model: models.search,
|
|
1043
|
+
system: "Search for the exact query given and report what you find. Do not guess a profile URL that did not appear in the results.",
|
|
1044
|
+
user: query,
|
|
1045
|
+
maxTokens: 600
|
|
1046
|
+
});
|
|
1047
|
+
const candidates = candidatesFrom(citations);
|
|
1048
|
+
if (candidates.length !== 1) {
|
|
1049
|
+
return {
|
|
1050
|
+
strategy: "targeted-profile",
|
|
1051
|
+
sources: [],
|
|
1052
|
+
links: [],
|
|
1053
|
+
notes: candidates.length === 0 ? `${query}
|
|
1054
|
+
|
|
1055
|
+
no profile matched` : `${query}
|
|
1056
|
+
|
|
1057
|
+
${candidates.length} distinct profiles matched \u2014 the name is ambiguous`
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
const only = candidates[0];
|
|
1061
|
+
const link = { kind: kindOf(only.host), url: only.url };
|
|
1062
|
+
const source = {
|
|
1063
|
+
sourceUrl: only.url,
|
|
1064
|
+
sourceClass: "self-asserted",
|
|
1065
|
+
facts: ["profile"],
|
|
1066
|
+
verifiedOn: retrievedAt,
|
|
1067
|
+
corroboration: "confirmed"
|
|
1068
|
+
};
|
|
1069
|
+
return {
|
|
1070
|
+
strategy: "targeted-profile",
|
|
1071
|
+
sources: [source],
|
|
1072
|
+
links: [link],
|
|
1073
|
+
notes: `${query}
|
|
1074
|
+
|
|
1075
|
+
exactly one distinct profile matched the name and company`
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
// src/identity/person.ts
|
|
1081
|
+
var DURABLE_KINDS = ["linkedin", "github", "bluesky", "x"];
|
|
1082
|
+
function settled(config) {
|
|
1083
|
+
return {
|
|
1084
|
+
aliasType: config.aliasType,
|
|
1085
|
+
aliasKeyAttr: config.aliasKeyAttr ?? "alias",
|
|
1086
|
+
personKeyAttr: config.personKeyAttr ?? "personKey",
|
|
1087
|
+
personType: config.personType ?? "person",
|
|
1088
|
+
edgeLabel: config.edgeLabel ?? "of_person"
|
|
1089
|
+
};
|
|
1090
|
+
}
|
|
1091
|
+
function mintPersonKey(random = () => crypto.randomUUID()) {
|
|
1092
|
+
return `p_${random()}`;
|
|
1093
|
+
}
|
|
1094
|
+
function isMintedKey(key) {
|
|
1095
|
+
return key.startsWith("p_");
|
|
1096
|
+
}
|
|
1097
|
+
function profileAlias(kind, url) {
|
|
1098
|
+
try {
|
|
1099
|
+
const parsed = new URL(url);
|
|
1100
|
+
const handle = parsed.pathname.replace(/\/+$/, "").toLowerCase();
|
|
1101
|
+
return handle && handle !== "/" ? `${kind}:${handle}` : null;
|
|
1102
|
+
} catch {
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
function nameAlias(name, companyName) {
|
|
1107
|
+
const person = identityKey(name);
|
|
1108
|
+
const company = companyName ? identityKey(companyName) : "";
|
|
1109
|
+
return person && company ? `name:${person}~${company}` : null;
|
|
1110
|
+
}
|
|
1111
|
+
function aliasesFor(person, links = []) {
|
|
1112
|
+
const out = [];
|
|
1113
|
+
for (const kind of DURABLE_KINDS) {
|
|
1114
|
+
for (const link of links.filter((l) => l.kind === kind)) {
|
|
1115
|
+
const alias = profileAlias(kind, link.url);
|
|
1116
|
+
if (alias && !out.includes(alias)) out.push(alias);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const byName = nameAlias(person.name, person.companyName);
|
|
1120
|
+
if (byName) out.push(byName);
|
|
1121
|
+
return out;
|
|
1122
|
+
}
|
|
1123
|
+
async function resolvePersonKey(db, config, aliases) {
|
|
1124
|
+
const { aliasType, aliasKeyAttr, personKeyAttr } = settled(config);
|
|
1125
|
+
if (aliases.length === 0) return null;
|
|
1126
|
+
const result = await db.query({
|
|
1127
|
+
[aliasType]: { $: { where: { [aliasKeyAttr]: { $in: [...aliases] } } } }
|
|
1128
|
+
});
|
|
1129
|
+
const found = /* @__PURE__ */ new Map();
|
|
1130
|
+
for (const row of result[aliasType] ?? []) {
|
|
1131
|
+
const alias = row[aliasKeyAttr];
|
|
1132
|
+
const personKey = row[personKeyAttr];
|
|
1133
|
+
if (typeof alias === "string" && typeof personKey === "string" && personKey) found.set(alias, personKey);
|
|
1134
|
+
}
|
|
1135
|
+
for (const alias of aliases) {
|
|
1136
|
+
const hit = found.get(alias);
|
|
1137
|
+
if (hit) return hit;
|
|
1138
|
+
}
|
|
1139
|
+
return null;
|
|
1140
|
+
}
|
|
1141
|
+
function applyPersonIdentity(fragment, from, to, aliases, config) {
|
|
1142
|
+
const { aliasType, aliasKeyAttr, personKeyAttr, personType, edgeLabel } = settled(config);
|
|
1143
|
+
const rekey = (type, key) => type === personType && key === from ? to : key;
|
|
1144
|
+
const nodes = fragment.nodes.map((n) => ({ ...n, key: rekey(n.type, n.key) }));
|
|
1145
|
+
const edges = fragment.edges.map((e) => ({
|
|
1146
|
+
from: { type: e.from.type, key: rekey(e.from.type, e.from.key) },
|
|
1147
|
+
to: { type: e.to.type, key: rekey(e.to.type, e.to.key) },
|
|
1148
|
+
label: e.label
|
|
1149
|
+
}));
|
|
1150
|
+
for (const alias of [...new Set(aliases)]) {
|
|
1151
|
+
nodes.push({ type: aliasType, key: alias, attrs: { [personKeyAttr]: to } });
|
|
1152
|
+
edges.push({ from: { type: aliasType, key: alias }, to: { type: personType, key: to }, label: edgeLabel });
|
|
1153
|
+
}
|
|
1154
|
+
return { nodes, edges };
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
// src/identity/identify.ts
|
|
1158
|
+
async function identifyPerson(db, config, person, links, fragment) {
|
|
1159
|
+
const aliases = aliasesFor(person, links);
|
|
1160
|
+
const existing = await resolvePersonKey(db, config, aliases);
|
|
1161
|
+
const personKey = existing ?? mintPersonKey();
|
|
1162
|
+
return {
|
|
1163
|
+
personKey,
|
|
1164
|
+
minted: existing === null,
|
|
1165
|
+
aliases,
|
|
1166
|
+
fragment: applyPersonIdentity(fragment, person.slug, personKey, aliases, config)
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
|
|
530
1170
|
// src/llm/provider.ts
|
|
531
1171
|
import { init, extractText } from "@odla-ai/ai";
|
|
532
1172
|
var MODEL_TIERS = {
|
|
@@ -1206,7 +1846,9 @@ async function ingestFresh(step, deps, fresh, kind, resolve = false) {
|
|
|
1206
1846
|
);
|
|
1207
1847
|
}
|
|
1208
1848
|
export {
|
|
1849
|
+
ACTIONABLE,
|
|
1209
1850
|
COMMIT_STEP_CONFIG,
|
|
1851
|
+
DEFAULT_STANDARD,
|
|
1210
1852
|
FACTS_EXTRACT_SYSTEM,
|
|
1211
1853
|
FACTS_EXTRACT_TOOL,
|
|
1212
1854
|
FACTS_SEARCH_SYSTEM,
|
|
@@ -1215,17 +1857,28 @@ export {
|
|
|
1215
1857
|
LOCATE_SYSTEM,
|
|
1216
1858
|
LOCATE_TOOL,
|
|
1217
1859
|
MODEL_TIERS,
|
|
1860
|
+
PERSON_EXTRACT_SYSTEM,
|
|
1861
|
+
PERSON_EXTRACT_TOOL,
|
|
1862
|
+
PERSON_SEARCH_SYSTEM,
|
|
1218
1863
|
Store,
|
|
1864
|
+
TEAM_PATHS,
|
|
1865
|
+
WRITABLE_VERDICTS,
|
|
1219
1866
|
addUsage,
|
|
1867
|
+
aliasesFor,
|
|
1220
1868
|
allJobs,
|
|
1869
|
+
applyPersonIdentity,
|
|
1221
1870
|
asStepLike,
|
|
1871
|
+
candidatePages,
|
|
1872
|
+
candidatesFrom,
|
|
1222
1873
|
canonicalizeFragment,
|
|
1874
|
+
classifySource,
|
|
1223
1875
|
connectorIds,
|
|
1224
1876
|
countyKey,
|
|
1225
1877
|
createProvider,
|
|
1226
1878
|
emptyUsage,
|
|
1227
1879
|
enabledSources,
|
|
1228
1880
|
enrichCompany,
|
|
1881
|
+
enrichPerson,
|
|
1229
1882
|
exportFragment,
|
|
1230
1883
|
extractJson,
|
|
1231
1884
|
extractJsonArray,
|
|
@@ -1233,29 +1886,59 @@ export {
|
|
|
1233
1886
|
fetchText,
|
|
1234
1887
|
fetchUrl,
|
|
1235
1888
|
findConnectors,
|
|
1889
|
+
fold,
|
|
1236
1890
|
fragmentToOps,
|
|
1237
1891
|
getConnector,
|
|
1238
1892
|
getProvider,
|
|
1893
|
+
gradeSources,
|
|
1239
1894
|
htmlToText,
|
|
1240
1895
|
humanAttrsOf,
|
|
1896
|
+
identifyPerson,
|
|
1897
|
+
identityKey,
|
|
1898
|
+
independentHosts,
|
|
1241
1899
|
ingestFresh,
|
|
1900
|
+
investigatePerson,
|
|
1901
|
+
isActionable,
|
|
1902
|
+
isAttributed,
|
|
1903
|
+
isHttpUrl,
|
|
1242
1904
|
isLikelyPersonName,
|
|
1905
|
+
isMintedKey,
|
|
1906
|
+
isVerdict,
|
|
1907
|
+
isWritable,
|
|
1243
1908
|
jobsForHour,
|
|
1909
|
+
judgeOrgPage,
|
|
1244
1910
|
locatePrompt,
|
|
1245
1911
|
locationFragment,
|
|
1246
1912
|
makeContext,
|
|
1913
|
+
mentionsName,
|
|
1247
1914
|
mergeAttrs,
|
|
1248
1915
|
mergeRulesOf,
|
|
1249
1916
|
mergeScalar,
|
|
1917
|
+
mintPersonKey,
|
|
1918
|
+
nameAlias,
|
|
1250
1919
|
naturalKeysOf,
|
|
1251
1920
|
normalizeUrl,
|
|
1921
|
+
normalizeVerification,
|
|
1922
|
+
orgSiteStrategy,
|
|
1923
|
+
peopleLinks,
|
|
1924
|
+
personPrompt,
|
|
1925
|
+
personToFragment,
|
|
1926
|
+
profileAlias,
|
|
1927
|
+
researchStrategy,
|
|
1252
1928
|
resilientStep,
|
|
1253
1929
|
resolveCounties,
|
|
1254
1930
|
resolveName,
|
|
1931
|
+
resolvePersonKey,
|
|
1932
|
+
rootOf,
|
|
1255
1933
|
searchEntities,
|
|
1256
1934
|
slugify,
|
|
1935
|
+
snippetAround,
|
|
1936
|
+
stripMarkup,
|
|
1937
|
+
targetedProfileStrategy,
|
|
1938
|
+
targetedQuery,
|
|
1257
1939
|
toSerializedSchema,
|
|
1258
1940
|
urlHash,
|
|
1941
|
+
validateLinks,
|
|
1259
1942
|
writeFragment
|
|
1260
1943
|
};
|
|
1261
1944
|
//# sourceMappingURL=index.js.map
|