openrich 0.0.0-stage → 0.1.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/.env.example +23 -0
- package/LICENSE +21 -0
- package/README.md +410 -2
- package/assets/logo.svg +41 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +144 -0
- package/dist/env.d.ts +5 -0
- package/dist/env.js +24 -0
- package/dist/eval.d.ts +30 -0
- package/dist/eval.js +78 -0
- package/dist/mcp/client.d.ts +31 -0
- package/dist/mcp/client.js +94 -0
- package/dist/mcp/oauth.d.ts +13 -0
- package/dist/mcp/oauth.js +93 -0
- package/dist/mcp/server.d.ts +87 -0
- package/dist/mcp/server.js +125 -0
- package/dist/merge.d.ts +7 -0
- package/dist/merge.js +39 -0
- package/dist/providers/devplatforms.d.ts +34 -0
- package/dist/providers/devplatforms.js +612 -0
- package/dist/providers/finders.d.ts +38 -0
- package/dist/providers/finders.js +708 -0
- package/dist/providers/freetier.d.ts +9 -0
- package/dist/providers/freetier.js +254 -0
- package/dist/providers/github.d.ts +8 -0
- package/dist/providers/github.js +109 -0
- package/dist/providers/identity.d.ts +5 -0
- package/dist/providers/identity.js +62 -0
- package/dist/providers/index.d.ts +10 -0
- package/dist/providers/index.js +68 -0
- package/dist/providers/knowledge.d.ts +52 -0
- package/dist/providers/knowledge.js +302 -0
- package/dist/providers/pattern.d.ts +6 -0
- package/dist/providers/pattern.js +99 -0
- package/dist/providers/registries.d.ts +36 -0
- package/dist/providers/registries.js +396 -0
- package/dist/providers/research.d.ts +30 -0
- package/dist/providers/research.js +471 -0
- package/dist/providers/signals.d.ts +15 -0
- package/dist/providers/signals.js +116 -0
- package/dist/providers/smtp.d.ts +25 -0
- package/dist/providers/smtp.js +97 -0
- package/dist/providers/social.d.ts +37 -0
- package/dist/providers/social.js +657 -0
- package/dist/providers/verifiers.d.ts +47 -0
- package/dist/providers/verifiers.js +455 -0
- package/dist/providers/websearch.d.ts +32 -0
- package/dist/providers/websearch.js +346 -0
- package/dist/router.d.ts +31 -0
- package/dist/router.js +214 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +52 -0
- package/dist/store.d.ts +32 -0
- package/dist/store.js +95 -0
- package/dist/types.d.ts +82 -0
- package/dist/types.js +1 -0
- package/dist/util/http.d.ts +19 -0
- package/dist/util/http.js +34 -0
- package/dist/util/match.d.ts +23 -0
- package/dist/util/match.js +72 -0
- package/dist/util/names.d.ts +11 -0
- package/dist/util/names.js +56 -0
- package/dist/util/patterns.d.ts +14 -0
- package/dist/util/patterns.js +61 -0
- package/package.json +55 -4
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { fetchKey, getJson, UA } from "../util/http.js";
|
|
2
|
+
import { mentionsCompany, sameName } from "../util/match.js";
|
|
3
|
+
/**
|
|
4
|
+
* Knowledge-base sources: Wikidata, Wikipedia, Google Knowledge Graph.
|
|
5
|
+
* They only know notable people, but what they know is curated. The hard part is the
|
|
6
|
+
* namesake problem, so every finding needs sameName() plus a company link (or, for
|
|
7
|
+
* Wikidata/Wikipedia with no company in the query, a single unambiguous human).
|
|
8
|
+
*/
|
|
9
|
+
const WD = 'https://www.wikidata.org/w/api.php';
|
|
10
|
+
const H = { 'User-Agent': UA };
|
|
11
|
+
const CONF_COMPANY = 0.85;
|
|
12
|
+
const CONF_NAME_ONLY = 0.6;
|
|
13
|
+
/** At most this many name-matching humans get the per-candidate reverse lookup. */
|
|
14
|
+
const MAX_CANDIDATES = 4;
|
|
15
|
+
const str = (v) => (typeof v === 'string' && v.trim() ? v.trim() : null);
|
|
16
|
+
const hasCompany = (q) => Boolean(q.company || q.domain);
|
|
17
|
+
const fullName = (q) => q.name ?? [q.firstName, q.lastName].filter(Boolean).join(' ');
|
|
18
|
+
/** Non-deprecated claims of a property, preferred rank first. */
|
|
19
|
+
function claims(e, p) {
|
|
20
|
+
const cs = (e.claims?.[p] ?? []).filter((c) => c.rank !== 'deprecated');
|
|
21
|
+
return [...cs.filter((c) => c.rank === 'preferred'), ...cs.filter((c) => c.rank !== 'preferred')];
|
|
22
|
+
}
|
|
23
|
+
const val = (c) => c.mainsnak?.datavalue?.value;
|
|
24
|
+
const itemId = (v) => (v && typeof v === 'object' && typeof v.id === 'string' ? v.id : null);
|
|
25
|
+
/** A claim with an end time (P582) is a past role. */
|
|
26
|
+
const isCurrent = (c) => !c.qualifiers?.P582?.length;
|
|
27
|
+
const label = (e) => e?.labels?.en?.value;
|
|
28
|
+
function names(e) {
|
|
29
|
+
return [label(e), ...(e.aliases?.en ?? []).map((a) => a.value)].filter((x) => Boolean(x));
|
|
30
|
+
}
|
|
31
|
+
async function getEntities(ctx, ids, props) {
|
|
32
|
+
const out = {};
|
|
33
|
+
// wbgetentities takes at most 50 ids per call.
|
|
34
|
+
for (let i = 0; i < ids.length; i += 50) {
|
|
35
|
+
const p = new URLSearchParams({ action: 'wbgetentities', ids: ids.slice(i, i + 50).join('|'), props, languages: 'en', sitefilter: 'enwiki', format: 'json' });
|
|
36
|
+
const r = await getJson(ctx.fetch, `${WD}?${p}`, { headers: H });
|
|
37
|
+
Object.assign(out, r.data?.entities ?? {});
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/** Items that name this person as founder (P112), CEO (P169) or chair (P488). Wikidata often stores the link only on the company side. */
|
|
42
|
+
async function ledOrgs(ctx, qid) {
|
|
43
|
+
const p = new URLSearchParams({
|
|
44
|
+
action: 'query',
|
|
45
|
+
list: 'search',
|
|
46
|
+
srsearch: `haswbstatement:P112=${qid}|P169=${qid}|P488=${qid}`,
|
|
47
|
+
srlimit: '20',
|
|
48
|
+
srprop: '',
|
|
49
|
+
format: 'json',
|
|
50
|
+
});
|
|
51
|
+
const r = await getJson(ctx.fetch, `${WD}?${p}`, { headers: H });
|
|
52
|
+
return (r.data?.query?.search ?? []).map((s) => String(s.title)).filter((t) => /^Q\d+$/.test(t));
|
|
53
|
+
}
|
|
54
|
+
/** Humans on Wikidata whose name matches the query, with their company evidence. */
|
|
55
|
+
async function wikidataCandidates(q, ctx) {
|
|
56
|
+
const name = fullName(q);
|
|
57
|
+
const sp = new URLSearchParams({ action: 'wbsearchentities', search: name, language: 'en', type: 'item', limit: '10', format: 'json' });
|
|
58
|
+
const s = await getJson(ctx.fetch, `${WD}?${sp}`, { headers: H });
|
|
59
|
+
const hits = (s.data?.search ?? [])
|
|
60
|
+
.filter((h) => [h.label, h.match?.text, ...(h.aliases ?? [])].some((n) => sameName(n, name)))
|
|
61
|
+
.map((h) => String(h.id));
|
|
62
|
+
if (!hits.length)
|
|
63
|
+
return [];
|
|
64
|
+
const ents = await getEntities(ctx, hits, 'labels|aliases|descriptions|claims|sitelinks');
|
|
65
|
+
const humans = Object.values(ents)
|
|
66
|
+
.filter((e) => claims(e, 'P31').some((c) => itemId(val(c)) === 'Q5'))
|
|
67
|
+
.filter((e) => names(e).some((n) => sameName(n, name)))
|
|
68
|
+
.slice(0, MAX_CANDIDATES);
|
|
69
|
+
if (!humans.length)
|
|
70
|
+
return [];
|
|
71
|
+
// Reverse links only matter when we have a company to check them against.
|
|
72
|
+
const led = new Map();
|
|
73
|
+
if (hasCompany(q))
|
|
74
|
+
for (const h of humans)
|
|
75
|
+
led.set(h.id, await ledOrgs(ctx, h.id));
|
|
76
|
+
// One label call for every item we might print or match on.
|
|
77
|
+
const refs = new Set();
|
|
78
|
+
for (const h of humans) {
|
|
79
|
+
for (const p of ['P108', 'P39', 'P106', 'P463']) {
|
|
80
|
+
for (const c of claims(h, p)) {
|
|
81
|
+
const id = itemId(val(c));
|
|
82
|
+
if (id)
|
|
83
|
+
refs.add(id);
|
|
84
|
+
for (const qs of Object.values(c.qualifiers ?? {}))
|
|
85
|
+
for (const x of qs) {
|
|
86
|
+
const qid = itemId(x.datavalue?.value);
|
|
87
|
+
if (qid)
|
|
88
|
+
refs.add(qid);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const o of led.get(h.id) ?? [])
|
|
93
|
+
refs.add(o);
|
|
94
|
+
}
|
|
95
|
+
const labelEnts = refs.size ? await getEntities(ctx, [...refs], 'labels') : {};
|
|
96
|
+
const labels = {};
|
|
97
|
+
for (const [id, e] of Object.entries(labelEnts)) {
|
|
98
|
+
const l = label(e);
|
|
99
|
+
if (l)
|
|
100
|
+
labels[id] = l;
|
|
101
|
+
}
|
|
102
|
+
return humans.map((e) => {
|
|
103
|
+
const hits = [];
|
|
104
|
+
const check = (why, text) => { if (text && mentionsCompany(text, q))
|
|
105
|
+
hits.push(`${why}: ${text}`); };
|
|
106
|
+
for (const p of ['P108', 'P39', 'P463']) {
|
|
107
|
+
const why = p === 'P108' ? 'employer' : p === 'P39' ? 'position' : 'member of';
|
|
108
|
+
for (const c of claims(e, p)) {
|
|
109
|
+
check(why, labels[itemId(val(c)) ?? '']);
|
|
110
|
+
// Qualifiers such as "of" (P642) or "employer" (P108) on a position statement.
|
|
111
|
+
for (const qs of Object.values(c.qualifiers ?? {}))
|
|
112
|
+
for (const x of qs)
|
|
113
|
+
check(why, labels[itemId(x.datavalue?.value) ?? '']);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const o of led.get(e.id) ?? [])
|
|
117
|
+
check('founder/leader of', labels[o]);
|
|
118
|
+
check('description', e.descriptions?.en?.value);
|
|
119
|
+
for (const c of claims(e, 'P856'))
|
|
120
|
+
check('website', str(val(c)) ?? undefined);
|
|
121
|
+
return { entity: e, labels, led: led.get(e.id) ?? [], companyHits: [...new Set(hits)] };
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The one candidate we are willing to call "this person", or null.
|
|
126
|
+
* With a company: exactly one candidate linked to it. Without: exactly one human by that name.
|
|
127
|
+
*/
|
|
128
|
+
function pick(q, cands) {
|
|
129
|
+
if (hasCompany(q)) {
|
|
130
|
+
const linked = cands.filter((c) => c.companyHits.length);
|
|
131
|
+
return linked.length === 1 ? { cand: linked[0], conf: CONF_COMPANY } : null;
|
|
132
|
+
}
|
|
133
|
+
return cands.length === 1 ? { cand: cands[0], conf: CONF_NAME_ONLY } : null;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The Wikidata and Wikipedia providers both need the same lookup (3-4 requests). Share it
|
|
137
|
+
* for a short while so running both costs one lookup. Keyed per fetch so tests stay isolated.
|
|
138
|
+
*/
|
|
139
|
+
const shared = new WeakMap();
|
|
140
|
+
const SHARE_MS = 60_000;
|
|
141
|
+
export function lookupWikidata(q, ctx) {
|
|
142
|
+
let m = shared.get(fetchKey(ctx.fetch));
|
|
143
|
+
if (!m) {
|
|
144
|
+
m = new Map();
|
|
145
|
+
shared.set(fetchKey(ctx.fetch), m);
|
|
146
|
+
}
|
|
147
|
+
const key = JSON.stringify([fullName(q), q.company ?? '', q.domain ?? '']);
|
|
148
|
+
const now = Date.now();
|
|
149
|
+
for (const [k, v] of m)
|
|
150
|
+
if (now - v.at > SHARE_MS)
|
|
151
|
+
m.delete(k);
|
|
152
|
+
const hit = m.get(key);
|
|
153
|
+
if (hit)
|
|
154
|
+
return hit.p;
|
|
155
|
+
const p = wikidataCandidates(q, ctx);
|
|
156
|
+
m.set(key, { at: now, p });
|
|
157
|
+
p.catch(() => m.delete(key));
|
|
158
|
+
return p;
|
|
159
|
+
}
|
|
160
|
+
const commonsFile = (f) => `https://commons.wikimedia.org/wiki/Special:FilePath/${encodeURIComponent(f.replace(/ /g, '_'))}`;
|
|
161
|
+
export const wikidataProvider = {
|
|
162
|
+
id: 'wikidata',
|
|
163
|
+
kind: 'free',
|
|
164
|
+
description: 'Wikidata (notable people: employer, position, social handles, ORCID, photo)',
|
|
165
|
+
provides: ['company', 'title', 'github', 'twitter', 'linkedin', 'website', 'orcid', 'photo', 'profile', 'bio'],
|
|
166
|
+
// Runs on a name alone too, but then only trusts a single unambiguous human (lower confidence).
|
|
167
|
+
needs: (q) => Boolean(q.firstName && q.lastName),
|
|
168
|
+
async run(q, ctx) {
|
|
169
|
+
const chosen = pick(q, await lookupWikidata(q, ctx));
|
|
170
|
+
if (!chosen)
|
|
171
|
+
return [];
|
|
172
|
+
const { cand, conf } = chosen;
|
|
173
|
+
const e = cand.entity;
|
|
174
|
+
const evidence = cand.companyHits.length ? cand.companyHits.join('; ') : 'only human with this name on Wikidata';
|
|
175
|
+
const out = [];
|
|
176
|
+
const add = (field, v, c = conf) => {
|
|
177
|
+
const x = str(v);
|
|
178
|
+
if (x && !out.some((f) => f.field === field && f.value === x))
|
|
179
|
+
out.push({ field, value: x, source: 'wikidata', confidence: c, evidence });
|
|
180
|
+
};
|
|
181
|
+
const lab = (c) => cand.labels[itemId(val(c)) ?? ''];
|
|
182
|
+
add('profile', `https://www.wikidata.org/wiki/${e.id}`);
|
|
183
|
+
// Company: current employers first, then an org they found/lead that matches the query.
|
|
184
|
+
for (const c of claims(e, 'P108').filter(isCurrent))
|
|
185
|
+
add('company', lab(c));
|
|
186
|
+
for (const o of cand.led)
|
|
187
|
+
if (mentionsCompany(cand.labels[o], q))
|
|
188
|
+
add('company', cand.labels[o]);
|
|
189
|
+
// Title: a current held position beats an occupation ("entrepreneur" is a weak title).
|
|
190
|
+
const pos = claims(e, 'P39').filter(isCurrent).map(lab).find(Boolean);
|
|
191
|
+
if (pos)
|
|
192
|
+
add('title', pos);
|
|
193
|
+
else
|
|
194
|
+
add('title', claims(e, 'P106').map(lab).find(Boolean), conf * 0.6);
|
|
195
|
+
for (const c of claims(e, 'P2037').slice(0, 1))
|
|
196
|
+
add('github', str(val(c)) && `https://github.com/${val(c)}`);
|
|
197
|
+
for (const c of claims(e, 'P2002').slice(0, 1))
|
|
198
|
+
add('twitter', str(val(c)) && `https://x.com/${val(c)}`);
|
|
199
|
+
for (const c of claims(e, 'P6634').slice(0, 1))
|
|
200
|
+
add('linkedin', str(val(c)) && `https://www.linkedin.com/in/${val(c)}`);
|
|
201
|
+
for (const c of claims(e, 'P856').slice(0, 1))
|
|
202
|
+
add('website', val(c));
|
|
203
|
+
for (const c of claims(e, 'P496').slice(0, 1))
|
|
204
|
+
add('orcid', str(val(c)) && `https://orcid.org/${val(c)}`);
|
|
205
|
+
for (const c of claims(e, 'P18').slice(0, 1))
|
|
206
|
+
add('photo', str(val(c)) && commonsFile(val(c)), conf * 0.9);
|
|
207
|
+
add('bio', e.descriptions?.en?.value, conf * 0.8);
|
|
208
|
+
return out;
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
// ---- Wikipedia ----
|
|
212
|
+
async function summary(ctx, title) {
|
|
213
|
+
const r = await getJson(ctx.fetch, `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title.replace(/ /g, '_'))}`, { headers: H });
|
|
214
|
+
return r.data?.type === 'standard' ? r.data : null;
|
|
215
|
+
}
|
|
216
|
+
const ABBR = /\b(?:[A-Z]|Jr|Sr|Dr|Mr|Mrs|Ms|Prof|St|Inc|Ltd|Corp|Co|vs|etc|No)\.$/;
|
|
217
|
+
/** First sentence. A stop only counts when a capital follows and it does not end an initial or abbreviation ("Jane Q. Doe Jr. is"). */
|
|
218
|
+
export function firstSentence(s) {
|
|
219
|
+
const re = /[.!?](?=\s+[A-Z(])/g;
|
|
220
|
+
for (let m = re.exec(s); m; m = re.exec(s)) {
|
|
221
|
+
const head = s.slice(0, m.index + 1);
|
|
222
|
+
if (!ABBR.test(head))
|
|
223
|
+
return head.trim();
|
|
224
|
+
}
|
|
225
|
+
return s.trim();
|
|
226
|
+
}
|
|
227
|
+
export const wikipediaProvider = {
|
|
228
|
+
id: 'wikipedia',
|
|
229
|
+
kind: 'free',
|
|
230
|
+
description: 'English Wikipedia summary of the matched Wikidata person (bio, photo, article URL)',
|
|
231
|
+
provides: ['bio', 'photo', 'profile'],
|
|
232
|
+
needs: (q) => Boolean(q.firstName && q.lastName && hasCompany(q)),
|
|
233
|
+
async run(q, ctx) {
|
|
234
|
+
const cands = (await lookupWikidata(q, ctx)).filter((c) => c.entity.sitelinks?.enwiki?.title);
|
|
235
|
+
let chosen = pick(q, cands);
|
|
236
|
+
let evidence = chosen?.cand.companyHits.join('; ') ?? '';
|
|
237
|
+
let page = chosen ? await summary(ctx, chosen.cand.entity.sitelinks.enwiki.title) : null;
|
|
238
|
+
// No Wikidata link to the company: the article text may still name it. Accept only one such article.
|
|
239
|
+
if (!page && hasCompany(q) && cands.length && cands.length <= 3) {
|
|
240
|
+
const linked = [];
|
|
241
|
+
for (const c of cands) {
|
|
242
|
+
const s = await summary(ctx, c.entity.sitelinks.enwiki.title);
|
|
243
|
+
if (s && mentionsCompany(s.extract, q))
|
|
244
|
+
linked.push({ cand: c, s });
|
|
245
|
+
}
|
|
246
|
+
if (linked.length === 1) {
|
|
247
|
+
page = linked[0].s;
|
|
248
|
+
chosen = { cand: linked[0].cand, conf: CONF_COMPANY };
|
|
249
|
+
evidence = 'article mentions the company';
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// Titles may carry a disambiguator: "John Smith (businessman)".
|
|
253
|
+
if (!page || !chosen || !sameName(String(page.title).replace(/\s*\(.*\)\s*$/, ''), fullName(q)))
|
|
254
|
+
return [];
|
|
255
|
+
const conf = chosen.conf;
|
|
256
|
+
const out = [];
|
|
257
|
+
const add = (field, v, c = conf) => { const x = str(v); if (x)
|
|
258
|
+
out.push({ field, value: x, source: 'wikipedia', confidence: c, evidence }); };
|
|
259
|
+
add('profile', page.content_urls?.desktop?.page);
|
|
260
|
+
add('bio', page.extract && firstSentence(page.extract));
|
|
261
|
+
// Strip Wikimedia's tracking query string from image URLs.
|
|
262
|
+
add('photo', (page.originalimage?.source ?? page.thumbnail?.source)?.replace(/\?utm_.*$/, ''), conf * 0.9);
|
|
263
|
+
return out;
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
// ---- Google Knowledge Graph ----
|
|
267
|
+
/**
|
|
268
|
+
* Google Knowledge Graph Search API. Free: 100,000 read calls/day per project (needs an API key
|
|
269
|
+
* from a Google Cloud project with the API enabled). Returns name + short description + a
|
|
270
|
+
* Wikipedia-derived detailedDescription; no employer field, so the company must appear in the text.
|
|
271
|
+
*/
|
|
272
|
+
export const googleKgProvider = {
|
|
273
|
+
id: 'google-kg',
|
|
274
|
+
kind: 'free-tier',
|
|
275
|
+
description: 'Google Knowledge Graph Search (free: 100,000 calls/day)',
|
|
276
|
+
provides: ['bio', 'website', 'profile'],
|
|
277
|
+
keyEnv: 'GOOGLE_KG_API_KEY',
|
|
278
|
+
quota: { limit: 100_000, period: 'day' },
|
|
279
|
+
needs: (q) => Boolean(q.firstName && q.lastName && hasCompany(q)),
|
|
280
|
+
async run(q, ctx) {
|
|
281
|
+
const name = fullName(q);
|
|
282
|
+
const p = new URLSearchParams({ query: name, types: 'Person', limit: '10', languages: 'en', key: ctx.env.GOOGLE_KG_API_KEY });
|
|
283
|
+
const r = await getJson(ctx.fetch, `https://kgsearch.googleapis.com/v1/entities:search?${p}`, { headers: H });
|
|
284
|
+
const matches = (r.data?.itemListElement ?? [])
|
|
285
|
+
.map((x) => x.result)
|
|
286
|
+
.filter((x) => x && sameName(x.name, name))
|
|
287
|
+
.filter((x) => mentionsCompany(x.description, q) || mentionsCompany(x.detailedDescription?.articleBody, q));
|
|
288
|
+
// Two people by this name both tied to the company: cannot tell which, say nothing.
|
|
289
|
+
if (matches.length !== 1)
|
|
290
|
+
return [];
|
|
291
|
+
const x = matches[0];
|
|
292
|
+
const evidence = `knowledge graph ${x['@id'] ?? ''}: ${x.description ?? ''}`.trim();
|
|
293
|
+
const out = [];
|
|
294
|
+
const add = (field, v, c = 0.8) => { const s = str(v); if (s)
|
|
295
|
+
out.push({ field, value: s, source: 'google-kg', confidence: c, evidence }); };
|
|
296
|
+
add('bio', x.detailedDescription?.articleBody ? firstSentence(x.detailedDescription.articleBody) : x.description, 0.7);
|
|
297
|
+
add('website', x.url);
|
|
298
|
+
add('profile', x.detailedDescription?.url);
|
|
299
|
+
return out;
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
export const KNOWLEDGE_PROVIDERS = [wikidataProvider, wikipediaProvider, googleKgProvider];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Provider } from '../types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Free email finder: guess addresses from name + domain, then confirm with free signals.
|
|
4
|
+
* Order of evidence: Reacher SMTP check (if REACHER_URL set) > OpenPGP verified key > Gravatar > learned domain pattern.
|
|
5
|
+
*/
|
|
6
|
+
export declare const patternProvider: Provider;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { hasNameAndDomain } from "../util/names.js";
|
|
2
|
+
import { candidates } from "../util/patterns.js";
|
|
3
|
+
import { gravatarExists, mxHosts, openpgpLookup, reacherCheck } from "./signals.js";
|
|
4
|
+
import { smtpCheck } from "./smtp.js";
|
|
5
|
+
const MAX_CANDIDATES = 8;
|
|
6
|
+
/**
|
|
7
|
+
* Free email finder: guess addresses from name + domain, then confirm with free signals.
|
|
8
|
+
* Order of evidence: Reacher SMTP check (if REACHER_URL set) > OpenPGP verified key > Gravatar > learned domain pattern.
|
|
9
|
+
*/
|
|
10
|
+
export const patternProvider = {
|
|
11
|
+
id: 'pattern',
|
|
12
|
+
kind: 'free',
|
|
13
|
+
description: 'Email pattern guess + free checks (MX, OpenPGP, Gravatar, optional self-hosted Reacher)',
|
|
14
|
+
provides: ['email'],
|
|
15
|
+
needs: (q) => hasNameAndDomain(q) && !q.email,
|
|
16
|
+
async run(q, ctx) {
|
|
17
|
+
const domain = q.domain;
|
|
18
|
+
let info = ctx.store.getDomainInfo(domain);
|
|
19
|
+
if (!info) {
|
|
20
|
+
info = { catchAll: null, mx: await mxHosts(domain) };
|
|
21
|
+
ctx.store.setDomainInfo(domain, info);
|
|
22
|
+
}
|
|
23
|
+
if (info.mx.length === 0) {
|
|
24
|
+
ctx.log(`pattern: ${domain} has no MX records, it cannot receive email`);
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
const known = ctx.store.knownPattern(domain);
|
|
28
|
+
const cands = candidates(q.firstName, q.lastName, domain, known).slice(0, MAX_CANDIDATES);
|
|
29
|
+
const out = [];
|
|
30
|
+
// Free existence signals, in parallel.
|
|
31
|
+
const signals = await Promise.all(cands.map(async (c) => {
|
|
32
|
+
const [pgp, grav] = await Promise.all([openpgpLookup(ctx.fetch, c.email), gravatarExists(ctx.fetch, c.email)]);
|
|
33
|
+
return { ...c, pgp, grav };
|
|
34
|
+
}));
|
|
35
|
+
for (const s of signals) {
|
|
36
|
+
if (s.pgp)
|
|
37
|
+
out.push({ field: 'email', value: s.email, source: 'openpgp', confidence: 0.9, evidence: `verified key: ${s.pgp[0] ?? ''}` });
|
|
38
|
+
if (s.grav)
|
|
39
|
+
out.push({ field: 'email', value: s.email, source: 'gravatar', confidence: 0.75, evidence: 'gravatar exists for this address' });
|
|
40
|
+
}
|
|
41
|
+
// SMTP verification through the user's own Reacher instance.
|
|
42
|
+
const reacher = ctx.env.REACHER_URL;
|
|
43
|
+
if (reacher && info.catchAll !== true) {
|
|
44
|
+
for (const c of cands) {
|
|
45
|
+
try {
|
|
46
|
+
const r = await reacherCheck(ctx.fetch, reacher, c.email);
|
|
47
|
+
if (r.catchAll !== null && info.catchAll === null) {
|
|
48
|
+
info.catchAll = r.catchAll;
|
|
49
|
+
ctx.store.setDomainInfo(domain, info);
|
|
50
|
+
}
|
|
51
|
+
if (r.catchAll)
|
|
52
|
+
break; // every address "exists": SMTP cannot tell them apart
|
|
53
|
+
if (r.reachable === 'safe') {
|
|
54
|
+
out.push({ field: 'email', value: c.email, source: 'smtp', confidence: 0.92, evidence: `SMTP accepted (${c.pattern})` });
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
ctx.log(`pattern: reacher failed: ${e.message}`);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else if (ctx.env.SMTP_CHECK === 'on' && info.catchAll !== true) {
|
|
65
|
+
// Built-in SMTP check against the first MX that answers.
|
|
66
|
+
for (const mx of info.mx.slice(0, 2)) {
|
|
67
|
+
try {
|
|
68
|
+
const r = await smtpCheck(mx, domain, cands.map((c) => c.email), { helo: ctx.env.SMTP_HELO, from: ctx.env.SMTP_FROM });
|
|
69
|
+
info.catchAll = r.catchAll;
|
|
70
|
+
ctx.store.setDomainInfo(domain, info);
|
|
71
|
+
const accepted = cands.filter((c) => r.verdicts.get(c.email) === true);
|
|
72
|
+
// One accepted address is strong evidence; several accepted means the server is lenient.
|
|
73
|
+
const conf = accepted.length === 1 ? 0.92 : 0.6;
|
|
74
|
+
for (const c of accepted.slice(0, 3)) {
|
|
75
|
+
out.push({ field: 'email', value: c.email, source: 'smtp', confidence: conf, evidence: `SMTP RCPT accepted (${c.pattern})` });
|
|
76
|
+
}
|
|
77
|
+
if (r.catchAll)
|
|
78
|
+
ctx.log(`pattern: ${domain} is catch-all, SMTP cannot confirm a mailbox`);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
ctx.log(`pattern: smtp ${mx}: ${e.message}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Unconfirmed best guess, so the caller always gets something. Low confidence on purpose.
|
|
87
|
+
if (out.length === 0 && cands[0]) {
|
|
88
|
+
const conf = known ? (info.catchAll ? 0.5 : 0.6) : 0.15;
|
|
89
|
+
out.push({
|
|
90
|
+
field: 'email',
|
|
91
|
+
value: cands[0].email,
|
|
92
|
+
source: 'guess',
|
|
93
|
+
confidence: conf,
|
|
94
|
+
evidence: known ? `learned pattern for ${domain}: ${known}` : `most common pattern ${cands[0].pattern}, unverified`,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PersonQuery, Provider } from '../types.ts';
|
|
2
|
+
type Strength = 'exact' | 'partial';
|
|
3
|
+
/** "Stripe Payments UK Ltd." -> "stripe payments uk" */
|
|
4
|
+
export declare function normCompany(s: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Does a registered company name match the query's company?
|
|
7
|
+
* exact: same name once legal suffixes are gone (or equal to the domain root, spaces ignored).
|
|
8
|
+
* partial: the registered name starts with the query name as whole words ("stripe payments uk" for "Stripe").
|
|
9
|
+
*/
|
|
10
|
+
export declare function companyMatch(registered: string | undefined | null, q: PersonQuery): Strength | null;
|
|
11
|
+
/**
|
|
12
|
+
* Person match. exact: sameName(). nick: same last name and one first name is a prefix of the other
|
|
13
|
+
* ("Tim" / "Timothy"), only trusted because we are already inside one company's officer list.
|
|
14
|
+
*/
|
|
15
|
+
export declare function personMatch(registered: string | undefined | null, q: PersonQuery): 'exact' | 'nick' | null;
|
|
16
|
+
/** UK Companies House (free key, basic auth "key:"; 600 requests / 5 min). */
|
|
17
|
+
export declare const ukCompaniesHouseProvider: Provider;
|
|
18
|
+
/** French company register via api.gouv.fr (free, no key, ~7 req/s). Titles are in French ("Président de SAS"). */
|
|
19
|
+
export declare const frRechercheEntreprisesProvider: Provider;
|
|
20
|
+
/** Norwegian Brønnøysund Register Centre (free, no key). */
|
|
21
|
+
export declare const noBrregProvider: Provider;
|
|
22
|
+
/** "COOK TIMOTHY D (CIK 0001214156)" -> { name: "COOK, TIMOTHY D", cik: "0001214156" }. Form 4 names are "LAST FIRST MIDDLE". */
|
|
23
|
+
export declare function edgarOwner(display: string): {
|
|
24
|
+
name: string;
|
|
25
|
+
cik: string;
|
|
26
|
+
} | null;
|
|
27
|
+
/**
|
|
28
|
+
* SEC EDGAR (free; SEC rejects requests without a contact User-Agent, so SEC_USER_AGENT="Name you@example.com" is required).
|
|
29
|
+
* company -> CIK via company_tickers.json (exact name or domain root only), then the person's latest Form 4 at that issuer
|
|
30
|
+
* in the last two years via full-text search, then officerTitle / isDirector from the Form 4 XML.
|
|
31
|
+
*/
|
|
32
|
+
export declare const secEdgarProvider: Provider;
|
|
33
|
+
/** UK Parliament Members API (free, no key). Only for queries about Parliament itself. */
|
|
34
|
+
export declare const ukParliamentProvider: Provider;
|
|
35
|
+
export declare const REGISTRY_PROVIDERS: Provider[];
|
|
36
|
+
export {};
|