birdclaw 0.8.2 → 0.8.4
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/CHANGELOG.md +33 -0
- package/README.md +10 -8
- package/bin/birdclaw.mjs +4 -66
- package/dist/cli/birdclaw.js +28165 -0
- package/dist/client/assets/AvatarChip-DLoqM5xD.js +1 -0
- package/dist/client/assets/ProfilePreview-BejAuYsJ.js +1 -0
- package/dist/client/assets/SavedTimelineView-BIS7HKpp.js +1 -0
- package/dist/client/assets/SmartTimestamp-BR0-9se2.js +1 -0
- package/dist/client/assets/TimelineRouteFrame-CyGPj0eM.js +1 -0
- package/dist/client/assets/blocks-7UDKQp8H.js +1 -0
- package/dist/client/assets/bookmarks-CbBkMvXk.js +1 -0
- package/dist/client/assets/chunk-C0phanGV.js +1 -0
- package/dist/client/assets/circle-check-C_Vq3ElD.js +1 -0
- package/dist/client/assets/client-stream-contracts-ys3XhWG4.js +5 -0
- package/dist/client/assets/createLucideIcon-JV8isFFR.js +1 -0
- package/dist/client/assets/data-sources-Cm2bpNvg.js +1 -0
- package/dist/client/assets/discuss-DTjaOugw.js +1 -0
- package/dist/client/assets/dms-CGd6Eogq.js +1 -0
- package/dist/client/assets/inbox-BHlGuOy2.js +1 -0
- package/dist/client/assets/index-CEIG7S6s.js +31 -0
- package/dist/client/assets/index-bpFrQ5yG.css +1 -0
- package/dist/client/assets/likes-cLrcAQem.js +1 -0
- package/dist/client/assets/links-CM9QO1LC.js +1 -0
- package/dist/client/assets/mapbox-BxTlqXt9.js +2 -0
- package/dist/client/assets/mapbox-gl-dgf_hX3a.js +3215 -0
- package/dist/client/assets/mentions-CxKCgzIf.js +1 -0
- package/dist/client/assets/network-map-B5a3whqj.js +2 -0
- package/dist/client/assets/profile-analyze-CRtNEwlb.js +1 -0
- package/dist/client/assets/profile-hydration-client-YEtJsNTv.js +1 -0
- package/dist/client/assets/profiles._handle-C82_HoKX.js +1 -0
- package/dist/client/assets/query-client-fYu2rzRC.js +17 -0
- package/dist/client/assets/rate-limits-ScaEwdVK.js +1 -0
- package/dist/client/assets/react-dom-DHl6Ug7F.js +1 -0
- package/dist/client/assets/react-nBylq1U5.js +1 -0
- package/dist/client/assets/repeat-2-xCwHM9hD.js +1 -0
- package/dist/client/assets/routes-q7ZaW4En.js +1 -0
- package/dist/client/assets/schemas-I9r6F6ya.js +64 -0
- package/dist/client/assets/search-Dq5kYsxA.js +1 -0
- package/dist/client/assets/sparkles-CQVlvHsZ.js +1 -0
- package/dist/client/assets/styles-B8s3tt3r.css +2 -0
- package/dist/client/assets/today-CeG1O7Py.js +1 -0
- package/dist/client/assets/triangle-alert-CBL7s2pf.js +1 -0
- package/dist/client/assets/useTimelineRouteData-1HsqgvUi.js +1 -0
- package/dist/client/assets/users-CHbKlqUr.js +1 -0
- package/dist/server/assets/AvatarChip-zUDAEmjL.js +105 -0
- package/dist/server/assets/FeedState-ntW0Q2GB.js +214 -0
- package/dist/server/assets/ProfileAnalysisStream-BFvhVdhn.js +198 -0
- package/dist/server/assets/ProfilePreview-C6gAor_j.js +752 -0
- package/dist/server/assets/SavedTimelineView-BG1jnBp1.js +64 -0
- package/dist/server/assets/SmartTimestamp-DfwE1NGf.js +45 -0
- package/dist/server/assets/SyncNowButton-BOn_IBXS.js +76 -0
- package/dist/server/assets/TimelineRouteFrame-CU7ilM7U.js +85 -0
- package/dist/server/assets/TweetRichText-DhCVv1-6.js +74 -0
- package/dist/server/assets/_tanstack-start-manifest_v-C-ZRnG_n.js +148 -0
- package/dist/server/assets/account-selection-CTdFGWRk.js +44 -0
- package/dist/server/assets/blocks-C1mgGTB3.js +299 -0
- package/dist/server/assets/blocks-DSgQghqP.js +17 -0
- package/dist/server/assets/bookmarks-BJN8tykY.js +14 -0
- package/dist/server/assets/client-stream-contracts-C2xbWVjm.js +769 -0
- package/dist/server/assets/data-sources-BnA1Q94p.js +180 -0
- package/dist/server/assets/discuss-CJ1dau1j.js +15 -0
- package/dist/server/assets/discuss-QTo08NCN.js +338 -0
- package/dist/server/assets/dms-BuLj4bp8.js +368 -0
- package/dist/server/assets/dms-DAyQrVM2.js +240 -0
- package/dist/server/assets/empty-plugin-adapters-DzrEkdBF.js +5 -0
- package/dist/server/assets/inbox-CkGOaW1q.js +214 -0
- package/dist/server/assets/inbox-DpWd1EzC.js +146 -0
- package/dist/server/assets/likes-DBOAtsJU.js +14 -0
- package/dist/server/assets/links-DAhSjynu.js +240 -0
- package/dist/server/assets/links-DmH5lxHO.js +468 -0
- package/dist/server/assets/mentions-BH2MzKtB.js +26 -0
- package/dist/server/assets/network-map-D3CzRcC4.js +224 -0
- package/dist/server/assets/network-map-D5DoLOwR.js +582 -0
- package/dist/server/assets/profile-analyze-BP65PED0.js +90 -0
- package/dist/server/assets/profile-analyze-C1NoM0tV.js +9 -0
- package/dist/server/assets/profile-hydration-client-GJ4kwJS8.js +133 -0
- package/dist/server/assets/profiles._handle-DjwlN_ac.js +184 -0
- package/dist/server/assets/profiles._handle-DsLNj2Ca.js +12 -0
- package/dist/server/assets/query-client-BdauHBgd.js +923 -0
- package/dist/server/assets/rate-limits-VAr2qzYD.js +262 -0
- package/dist/server/assets/route-search-BUlQ5lGM.js +117 -0
- package/dist/server/assets/router-DqkwSPiM.js +17026 -0
- package/dist/server/assets/routes-A9c9ZhwN.js +26 -0
- package/dist/server/assets/start-FaDOTmto.js +4 -0
- package/dist/server/assets/today-B5hVM57Z.js +17 -0
- package/dist/server/assets/today-fvGiZ7ud.js +303 -0
- package/dist/server/assets/ui-qbmcMSSX.js +137 -0
- package/{src/components/useDebouncedValue.ts → dist/server/assets/useDebouncedValue-DOnA6Xcy.js} +4 -4
- package/dist/server/assets/useTimelineRouteData-27cxynxa.js +1101 -0
- package/dist/server/server.js +5520 -0
- package/package.json +27 -34
- package/playwright.config.ts +0 -35
- package/scripts/browser-perf.mjs +0 -427
- package/scripts/build-docs-site.mjs +0 -967
- package/scripts/docs-site-assets.mjs +0 -311
- package/scripts/run-vitest.mjs +0 -21
- package/scripts/sanitize-node-options.mjs +0 -23
- package/scripts/start-test-server.mjs +0 -42
- package/src/cli-moderation.ts +0 -210
- package/src/cli.ts +0 -2336
- package/src/components/AccountSwitcher.tsx +0 -186
- package/src/components/AppNav.tsx +0 -118
- package/src/components/AvatarChip.tsx +0 -58
- package/src/components/BrandMark.tsx +0 -67
- package/src/components/ConversationThread.tsx +0 -131
- package/src/components/DmWorkspace.tsx +0 -286
- package/src/components/EmbeddedTweetCard.tsx +0 -55
- package/src/components/FeedState.tsx +0 -147
- package/src/components/FloatingPreview.tsx +0 -382
- package/src/components/InboxCard.tsx +0 -152
- package/src/components/LinkPreviewCard.tsx +0 -292
- package/src/components/MarkdownViewer.tsx +0 -777
- package/src/components/ProfileAnalysisStream.tsx +0 -428
- package/src/components/ProfilePreview.tsx +0 -129
- package/src/components/SavedTimelineView.tsx +0 -155
- package/src/components/SmartTimestamp.tsx +0 -72
- package/src/components/SyncNowButton.tsx +0 -140
- package/src/components/ThemeSlider.tsx +0 -80
- package/src/components/TimelineCard.tsx +0 -533
- package/src/components/TimelineRouteFrame.tsx +0 -186
- package/src/components/TweetArticleCard.tsx +0 -66
- package/src/components/TweetMediaGrid.tsx +0 -185
- package/src/components/TweetRichText.tsx +0 -159
- package/src/components/account-selection.ts +0 -64
- package/src/components/useTimelineRouteData.ts +0 -323
- package/src/lib/account-sync-job.ts +0 -666
- package/src/lib/actions-transport.ts +0 -300
- package/src/lib/api-client.ts +0 -419
- package/src/lib/archive-finder.ts +0 -185
- package/src/lib/archive-import.ts +0 -2764
- package/src/lib/authored-live.ts +0 -1072
- package/src/lib/avatar-cache.ts +0 -349
- package/src/lib/backup.ts +0 -2741
- package/src/lib/bird-actions.ts +0 -294
- package/src/lib/bird-command.ts +0 -130
- package/src/lib/bird.ts +0 -1140
- package/src/lib/blocklist.ts +0 -136
- package/src/lib/blocks-write.ts +0 -167
- package/src/lib/blocks.ts +0 -389
- package/src/lib/bookmark-sync-job.ts +0 -536
- package/src/lib/client-cache.ts +0 -109
- package/src/lib/config.ts +0 -224
- package/src/lib/conversation-surface.ts +0 -205
- package/src/lib/data-sources.ts +0 -219
- package/src/lib/db.ts +0 -1135
- package/src/lib/dms-live.ts +0 -933
- package/src/lib/effect-runtime.ts +0 -45
- package/src/lib/follow-graph.ts +0 -1097
- package/src/lib/geocoding.ts +0 -296
- package/src/lib/http-effect.ts +0 -222
- package/src/lib/identity-search-index.ts +0 -369
- package/src/lib/inbox.ts +0 -241
- package/src/lib/link-index.ts +0 -641
- package/src/lib/link-insights.ts +0 -860
- package/src/lib/link-preview-metadata.ts +0 -754
- package/src/lib/location.ts +0 -137
- package/src/lib/media-fetch.ts +0 -860
- package/src/lib/media-includes.ts +0 -165
- package/src/lib/mention-threads-live.ts +0 -917
- package/src/lib/mentions-export.ts +0 -147
- package/src/lib/mentions-live.ts +0 -1238
- package/src/lib/moderation-target.ts +0 -216
- package/src/lib/moderation-write.ts +0 -131
- package/src/lib/mutes-write.ts +0 -167
- package/src/lib/mutes.ts +0 -84
- package/src/lib/ndjson-stream.ts +0 -106
- package/src/lib/network-map.ts +0 -382
- package/src/lib/openai.ts +0 -117
- package/src/lib/period-digest.ts +0 -1552
- package/src/lib/present.ts +0 -118
- package/src/lib/profile-affiliation-hydration.ts +0 -228
- package/src/lib/profile-affiliations.ts +0 -262
- package/src/lib/profile-analysis.ts +0 -1272
- package/src/lib/profile-bio-entities.ts +0 -318
- package/src/lib/profile-history.ts +0 -164
- package/src/lib/profile-hydration.ts +0 -180
- package/src/lib/profile-replies.ts +0 -77
- package/src/lib/profile-resolver.ts +0 -650
- package/src/lib/queries.ts +0 -2381
- package/src/lib/research.ts +0 -652
- package/src/lib/search-discussion.ts +0 -1016
- package/src/lib/seed.ts +0 -613
- package/src/lib/sqlite.ts +0 -147
- package/src/lib/sync-cache.ts +0 -65
- package/src/lib/theme-transition.ts +0 -113
- package/src/lib/theme.tsx +0 -165
- package/src/lib/timeline-collections-live.ts +0 -581
- package/src/lib/timeline-live.ts +0 -452
- package/src/lib/tweet-account-edges.ts +0 -48
- package/src/lib/tweet-lookup.ts +0 -46
- package/src/lib/tweet-render.ts +0 -430
- package/src/lib/tweet-search-live.ts +0 -566
- package/src/lib/types.ts +0 -723
- package/src/lib/ui.ts +0 -529
- package/src/lib/url-expansion-store.ts +0 -110
- package/src/lib/url-expansion.ts +0 -343
- package/src/lib/url-safety.ts +0 -220
- package/src/lib/web-sync.ts +0 -516
- package/src/lib/whois.ts +0 -1079
- package/src/lib/x-profile.ts +0 -365
- package/src/lib/x-web.ts +0 -199
- package/src/lib/xurl-rate-limits.ts +0 -267
- package/src/lib/xurl.ts +0 -1677
- package/src/routeTree.gen.ts +0 -723
- package/src/router.tsx +0 -20
- package/src/routes/__root.tsx +0 -79
- package/src/routes/api/action.tsx +0 -147
- package/src/routes/api/avatar.tsx +0 -50
- package/src/routes/api/blocks.tsx +0 -35
- package/src/routes/api/conversation.tsx +0 -45
- package/src/routes/api/data-sources.tsx +0 -24
- package/src/routes/api/inbox.tsx +0 -43
- package/src/routes/api/link-insights.tsx +0 -85
- package/src/routes/api/link-preview.tsx +0 -47
- package/src/routes/api/network-map.tsx +0 -55
- package/src/routes/api/period-digest.tsx +0 -101
- package/src/routes/api/profile-analysis.tsx +0 -97
- package/src/routes/api/profile-hydrate.tsx +0 -54
- package/src/routes/api/query.tsx +0 -115
- package/src/routes/api/search-discussion.tsx +0 -113
- package/src/routes/api/status.tsx +0 -28
- package/src/routes/api/sync.tsx +0 -105
- package/src/routes/api/xurl-rate-limits.tsx +0 -24
- package/src/routes/blocks.tsx +0 -377
- package/src/routes/bookmarks.tsx +0 -18
- package/src/routes/data-sources.tsx +0 -257
- package/src/routes/discuss.tsx +0 -419
- package/src/routes/dms.tsx +0 -432
- package/src/routes/inbox.tsx +0 -226
- package/src/routes/index.tsx +0 -32
- package/src/routes/likes.tsx +0 -18
- package/src/routes/links.tsx +0 -994
- package/src/routes/mentions.tsx +0 -32
- package/src/routes/network-map.tsx +0 -1035
- package/src/routes/profile-analyze.tsx +0 -112
- package/src/routes/profiles.$handle.tsx +0 -228
- package/src/routes/rate-limits.tsx +0 -309
- package/src/routes/today.tsx +0 -511
- package/src/styles.css +0 -257
- package/tsconfig.json +0 -29
- package/vite.config.ts +0 -29
- /package/{public → dist/client}/birdclaw-mark.png +0 -0
- /package/{public → dist/client}/favicon.ico +0 -0
- /package/{public → dist/client}/logo192.png +0 -0
- /package/{public → dist/client}/logo512.png +0 -0
- /package/{public → dist/client}/manifest.json +0 -0
- /package/{public → dist/client}/robots.txt +0 -0
package/src/lib/whois.ts
DELETED
|
@@ -1,1079 +0,0 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
|
-
import { getNativeDb } from "./db";
|
|
3
|
-
import { runEffectPromise } from "./effect-runtime";
|
|
4
|
-
import {
|
|
5
|
-
ensureIdentitySearchIndexForDmProfiles,
|
|
6
|
-
syncIdentitySearchIndexForProfileIds,
|
|
7
|
-
} from "./identity-search-index";
|
|
8
|
-
import { fetchProfileBioEntities } from "./profile-bio-entities";
|
|
9
|
-
import { fetchProfileSnapshots } from "./profile-history";
|
|
10
|
-
import { resolveProfilesForIdsEffect } from "./profile-resolver";
|
|
11
|
-
import type { ProfileResolveResult } from "./profile-resolver";
|
|
12
|
-
import { listDmConversations, listTimelineItems } from "./queries";
|
|
13
|
-
import { expandUrlsFromTextsEffect } from "./url-expansion";
|
|
14
|
-
import type {
|
|
15
|
-
DmConversationItem,
|
|
16
|
-
ProfileAffiliation,
|
|
17
|
-
ProfileBioEntity,
|
|
18
|
-
ProfileRecord,
|
|
19
|
-
ProfileSnapshot,
|
|
20
|
-
TimelineItem,
|
|
21
|
-
UrlExpansionItem,
|
|
22
|
-
} from "./types";
|
|
23
|
-
|
|
24
|
-
export interface WhoisOptions {
|
|
25
|
-
account?: string;
|
|
26
|
-
dms?: boolean;
|
|
27
|
-
tweets?: boolean;
|
|
28
|
-
resolveProfiles?: boolean;
|
|
29
|
-
expandUrls?: boolean;
|
|
30
|
-
refreshProfileCache?: boolean;
|
|
31
|
-
refreshUrlCache?: boolean;
|
|
32
|
-
xurlFallback?: boolean;
|
|
33
|
-
context?: number;
|
|
34
|
-
limit?: number;
|
|
35
|
-
affiliation?: string;
|
|
36
|
-
currentAffiliation?: string;
|
|
37
|
-
excludeDomainOnly?: boolean;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface WhoisCandidate {
|
|
41
|
-
conversation: DmConversationItem;
|
|
42
|
-
confidence: number;
|
|
43
|
-
category: WhoisCandidateCategory;
|
|
44
|
-
reasons: string[];
|
|
45
|
-
profileEvidence: WhoisEvidenceSignal[];
|
|
46
|
-
evidence: Array<{
|
|
47
|
-
messageId: string;
|
|
48
|
-
createdAt: string;
|
|
49
|
-
direction: string;
|
|
50
|
-
text: string;
|
|
51
|
-
urlExpansions?: UrlExpansionItem[];
|
|
52
|
-
}>;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type WhoisCandidateCategory =
|
|
56
|
-
| "likely_affiliated"
|
|
57
|
-
| "ecosystem"
|
|
58
|
-
| "profile_or_link"
|
|
59
|
-
| "dm_context"
|
|
60
|
-
| "other";
|
|
61
|
-
|
|
62
|
-
export interface WhoisResult {
|
|
63
|
-
query: string;
|
|
64
|
-
candidates: WhoisCandidate[];
|
|
65
|
-
relatedTweets: TimelineItem[];
|
|
66
|
-
urlExpansions: UrlExpansionItem[];
|
|
67
|
-
profileResolution?: ProfileResolveResult[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface WhoisEvidenceSignal {
|
|
71
|
-
kind:
|
|
72
|
-
| "profile_handle"
|
|
73
|
-
| "profile_name"
|
|
74
|
-
| "profile_bio"
|
|
75
|
-
| "profile_location"
|
|
76
|
-
| "profile_url"
|
|
77
|
-
| "profile_bio_url"
|
|
78
|
-
| "profile_verified_type"
|
|
79
|
-
| "affiliation"
|
|
80
|
-
| "bio_handle"
|
|
81
|
-
| "bio_domain"
|
|
82
|
-
| "bio_company"
|
|
83
|
-
| "profile_history"
|
|
84
|
-
| "dm_context"
|
|
85
|
-
| "expanded_url";
|
|
86
|
-
value: string;
|
|
87
|
-
source: "profile" | "affiliation" | "bio_entity" | "history" | "dm" | "url";
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function trySync<T>(try_: () => T) {
|
|
91
|
-
return Effect.try({
|
|
92
|
-
try: try_,
|
|
93
|
-
catch: (cause) => cause,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface WhoisQueryIntent {
|
|
98
|
-
raw: string;
|
|
99
|
-
normalized: string;
|
|
100
|
-
terms: string[];
|
|
101
|
-
handles: string[];
|
|
102
|
-
domains: string[];
|
|
103
|
-
wantsPerson: boolean;
|
|
104
|
-
wantsAffiliation: boolean;
|
|
105
|
-
wantsDomain: boolean;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function normalizeQuery(query: string) {
|
|
109
|
-
return query.trim().toLowerCase();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function getSignificantQueryTerms(query: string) {
|
|
113
|
-
const stopwords = new Set([
|
|
114
|
-
"a",
|
|
115
|
-
"an",
|
|
116
|
-
"and",
|
|
117
|
-
"at",
|
|
118
|
-
"for",
|
|
119
|
-
"from",
|
|
120
|
-
"guy",
|
|
121
|
-
"is",
|
|
122
|
-
"of",
|
|
123
|
-
"person",
|
|
124
|
-
"the",
|
|
125
|
-
"who",
|
|
126
|
-
"with",
|
|
127
|
-
]);
|
|
128
|
-
return Array.from(
|
|
129
|
-
new Set(
|
|
130
|
-
normalizeQuery(query)
|
|
131
|
-
.split(/[^a-z0-9_@.-]+/)
|
|
132
|
-
.map((term) => term.replace(/^@/, ""))
|
|
133
|
-
.filter((term) => term.length >= 3 && !stopwords.has(term)),
|
|
134
|
-
),
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function getQueryDomains(query: string) {
|
|
139
|
-
return Array.from(
|
|
140
|
-
new Set(
|
|
141
|
-
query
|
|
142
|
-
.toLowerCase()
|
|
143
|
-
.match(/\b(?:[a-z0-9-]+\.)+[a-z]{2,}\b/g)
|
|
144
|
-
?.map((domain) => domain.replace(/^www\./, "")) ?? [],
|
|
145
|
-
),
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function getQueryHandles(query: string) {
|
|
150
|
-
return Array.from(
|
|
151
|
-
new Set(
|
|
152
|
-
query
|
|
153
|
-
.match(/@[A-Za-z0-9_]{1,15}\b/g)
|
|
154
|
-
?.map((handle) => handle.toLowerCase()) ?? [],
|
|
155
|
-
),
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function getQueryIntent(query: string): WhoisQueryIntent {
|
|
160
|
-
const normalized = normalizeQuery(query);
|
|
161
|
-
const domains = getQueryDomains(query);
|
|
162
|
-
const handles = getQueryHandles(query);
|
|
163
|
-
const terms = getSignificantQueryTerms(query);
|
|
164
|
-
const wantsPerson =
|
|
165
|
-
/\b(guy|person|people|who|someone|staff|employee|devrel|founder|founders)\b/i.test(
|
|
166
|
-
query,
|
|
167
|
-
);
|
|
168
|
-
const wantsDomain =
|
|
169
|
-
domains.length > 0 ||
|
|
170
|
-
(/\b(link|url|domain|repo|repository|github\.com)\b/i.test(query) &&
|
|
171
|
-
!wantsPerson);
|
|
172
|
-
const wantsAffiliation =
|
|
173
|
-
handles.length > 0 ||
|
|
174
|
-
wantsPerson ||
|
|
175
|
-
/\b(at|from|works?|employee|staff|affiliation|company|org|devrel)\b/i.test(
|
|
176
|
-
query,
|
|
177
|
-
);
|
|
178
|
-
return {
|
|
179
|
-
raw: query,
|
|
180
|
-
normalized,
|
|
181
|
-
terms,
|
|
182
|
-
handles,
|
|
183
|
-
domains,
|
|
184
|
-
wantsPerson,
|
|
185
|
-
wantsAffiliation,
|
|
186
|
-
wantsDomain,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function matchesQueryText(query: string, value: string | undefined | null) {
|
|
191
|
-
if (!value) {
|
|
192
|
-
return false;
|
|
193
|
-
}
|
|
194
|
-
const normalizedValue = value.toLowerCase();
|
|
195
|
-
if (normalizedValue.includes(query)) {
|
|
196
|
-
return true;
|
|
197
|
-
}
|
|
198
|
-
return getSignificantQueryTerms(query).some((term) =>
|
|
199
|
-
normalizedValue.includes(term),
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function getDmSearchQueries(query: string) {
|
|
204
|
-
const trimmed = query.trim();
|
|
205
|
-
const values = [trimmed, ...getSignificantQueryTerms(trimmed)];
|
|
206
|
-
return Array.from(new Set(values.filter((value) => value.length > 0)));
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function getMessageTexts(conversation: DmConversationItem) {
|
|
210
|
-
return (conversation.matches ?? []).flatMap((match) => [
|
|
211
|
-
...match.before.map((message) => message.text),
|
|
212
|
-
match.message.text,
|
|
213
|
-
...match.after.map((message) => message.text),
|
|
214
|
-
]);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function getUrlEntityExpandedUrl(entity: unknown) {
|
|
218
|
-
if (!entity || typeof entity !== "object") {
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
const record = entity as Record<string, unknown>;
|
|
222
|
-
const expanded = record.expandedUrl ?? record.expanded_url ?? record.url;
|
|
223
|
-
return typeof expanded === "string" && expanded.length > 0
|
|
224
|
-
? expanded
|
|
225
|
-
: undefined;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function getProfileBioUrls(profile: ProfileRecord) {
|
|
229
|
-
const description = profile.entities?.description;
|
|
230
|
-
if (!description || typeof description !== "object") {
|
|
231
|
-
return [];
|
|
232
|
-
}
|
|
233
|
-
const urls = (description as { urls?: unknown }).urls;
|
|
234
|
-
if (!Array.isArray(urls)) {
|
|
235
|
-
return [];
|
|
236
|
-
}
|
|
237
|
-
return urls
|
|
238
|
-
.map(getUrlEntityExpandedUrl)
|
|
239
|
-
.filter((url): url is string => Boolean(url));
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function getAffiliationTexts(affiliation: ProfileAffiliation) {
|
|
243
|
-
return [
|
|
244
|
-
affiliation.organizationName,
|
|
245
|
-
affiliation.organizationHandle,
|
|
246
|
-
affiliation.label,
|
|
247
|
-
affiliation.url,
|
|
248
|
-
affiliation.organizationProfileId,
|
|
249
|
-
].filter((item): item is string => Boolean(item));
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function pushMatchEvidence(
|
|
253
|
-
signals: WhoisEvidenceSignal[],
|
|
254
|
-
query: string,
|
|
255
|
-
kind: WhoisEvidenceSignal["kind"],
|
|
256
|
-
value: string | undefined | null,
|
|
257
|
-
source: WhoisEvidenceSignal["source"],
|
|
258
|
-
) {
|
|
259
|
-
if (!matchesQueryText(query, value)) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
signals.push({ kind, value: String(value), source });
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function getSnapshotAffiliationTexts(snapshot: ProfileSnapshot) {
|
|
266
|
-
return snapshot.affiliations.flatMap((affiliation) => {
|
|
267
|
-
if (!affiliation || typeof affiliation !== "object") {
|
|
268
|
-
return [];
|
|
269
|
-
}
|
|
270
|
-
const record = affiliation as Record<string, unknown>;
|
|
271
|
-
return [
|
|
272
|
-
record.organizationName,
|
|
273
|
-
record.organizationHandle,
|
|
274
|
-
record.label,
|
|
275
|
-
record.url,
|
|
276
|
-
].filter(
|
|
277
|
-
(item): item is string => typeof item === "string" && item.length > 0,
|
|
278
|
-
);
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function getHistoricalSnapshotTexts(
|
|
283
|
-
snapshot: ProfileSnapshot,
|
|
284
|
-
profile: ProfileRecord,
|
|
285
|
-
) {
|
|
286
|
-
const texts: string[] = [];
|
|
287
|
-
if (snapshot.handle !== profile.handle) {
|
|
288
|
-
texts.push(`previous handle: @${snapshot.handle}`);
|
|
289
|
-
}
|
|
290
|
-
if (snapshot.displayName !== profile.displayName) {
|
|
291
|
-
texts.push(`previous name: ${snapshot.displayName}`);
|
|
292
|
-
}
|
|
293
|
-
if (snapshot.bio !== profile.bio) {
|
|
294
|
-
texts.push(`previous bio: ${snapshot.bio}`);
|
|
295
|
-
}
|
|
296
|
-
if (snapshot.location && snapshot.location !== (profile.location ?? null)) {
|
|
297
|
-
texts.push(`previous location: ${snapshot.location}`);
|
|
298
|
-
}
|
|
299
|
-
if (snapshot.url && snapshot.url !== (profile.url ?? null)) {
|
|
300
|
-
texts.push(`previous url: ${snapshot.url}`);
|
|
301
|
-
}
|
|
302
|
-
if (
|
|
303
|
-
snapshot.verifiedType &&
|
|
304
|
-
snapshot.verifiedType !== (profile.verifiedType ?? null)
|
|
305
|
-
) {
|
|
306
|
-
texts.push(`previous verified: ${snapshot.verifiedType}`);
|
|
307
|
-
}
|
|
308
|
-
const currentAffiliationTexts = new Set(
|
|
309
|
-
(profile.affiliations ?? [])
|
|
310
|
-
.flatMap((affiliation) => [
|
|
311
|
-
affiliation.organizationName,
|
|
312
|
-
affiliation.organizationHandle,
|
|
313
|
-
affiliation.label,
|
|
314
|
-
affiliation.url,
|
|
315
|
-
])
|
|
316
|
-
.filter((item): item is string => Boolean(item))
|
|
317
|
-
.map((item) => item.toLowerCase()),
|
|
318
|
-
);
|
|
319
|
-
const previousAffiliationTexts = getSnapshotAffiliationTexts(snapshot);
|
|
320
|
-
if (
|
|
321
|
-
previousAffiliationTexts.some(
|
|
322
|
-
(text) => !currentAffiliationTexts.has(text.toLowerCase()),
|
|
323
|
-
)
|
|
324
|
-
) {
|
|
325
|
-
texts.push(
|
|
326
|
-
`previous affiliations: ${JSON.stringify(snapshot.affiliations)}`,
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
return texts;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function collectProfileEvidence(
|
|
333
|
-
query: string,
|
|
334
|
-
profile: ProfileRecord,
|
|
335
|
-
bioEntities: ProfileBioEntity[] = [],
|
|
336
|
-
snapshots: ProfileSnapshot[] = [],
|
|
337
|
-
) {
|
|
338
|
-
const signals: WhoisEvidenceSignal[] = [];
|
|
339
|
-
pushMatchEvidence(
|
|
340
|
-
signals,
|
|
341
|
-
query,
|
|
342
|
-
"profile_handle",
|
|
343
|
-
profile.handle,
|
|
344
|
-
"profile",
|
|
345
|
-
);
|
|
346
|
-
pushMatchEvidence(
|
|
347
|
-
signals,
|
|
348
|
-
query,
|
|
349
|
-
"profile_name",
|
|
350
|
-
profile.displayName,
|
|
351
|
-
"profile",
|
|
352
|
-
);
|
|
353
|
-
pushMatchEvidence(signals, query, "profile_bio", profile.bio, "profile");
|
|
354
|
-
pushMatchEvidence(
|
|
355
|
-
signals,
|
|
356
|
-
query,
|
|
357
|
-
"profile_location",
|
|
358
|
-
profile.location,
|
|
359
|
-
"profile",
|
|
360
|
-
);
|
|
361
|
-
pushMatchEvidence(signals, query, "profile_url", profile.url, "profile");
|
|
362
|
-
pushMatchEvidence(
|
|
363
|
-
signals,
|
|
364
|
-
query,
|
|
365
|
-
"profile_verified_type",
|
|
366
|
-
profile.verifiedType,
|
|
367
|
-
"profile",
|
|
368
|
-
);
|
|
369
|
-
for (const url of getProfileBioUrls(profile)) {
|
|
370
|
-
pushMatchEvidence(signals, query, "profile_bio_url", url, "profile");
|
|
371
|
-
}
|
|
372
|
-
for (const affiliation of profile.affiliations ?? []) {
|
|
373
|
-
for (const text of getAffiliationTexts(affiliation)) {
|
|
374
|
-
pushMatchEvidence(signals, query, "affiliation", text, "affiliation");
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
for (const entity of bioEntities) {
|
|
378
|
-
const kind =
|
|
379
|
-
entity.kind === "handle"
|
|
380
|
-
? "bio_handle"
|
|
381
|
-
: entity.kind === "domain"
|
|
382
|
-
? "bio_domain"
|
|
383
|
-
: "bio_company";
|
|
384
|
-
pushMatchEvidence(signals, query, kind, entity.value, "bio_entity");
|
|
385
|
-
}
|
|
386
|
-
for (const snapshot of snapshots) {
|
|
387
|
-
for (const text of getHistoricalSnapshotTexts(snapshot, profile)) {
|
|
388
|
-
pushMatchEvidence(
|
|
389
|
-
signals,
|
|
390
|
-
query,
|
|
391
|
-
"profile_history",
|
|
392
|
-
`${snapshot.lastSeenAt}: ${text}`,
|
|
393
|
-
"history",
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
return signals;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const DOMAIN_ONLY_EVIDENCE = new Set<WhoisEvidenceSignal["kind"]>([
|
|
401
|
-
"profile_url",
|
|
402
|
-
"profile_bio_url",
|
|
403
|
-
"bio_domain",
|
|
404
|
-
"expanded_url",
|
|
405
|
-
]);
|
|
406
|
-
|
|
407
|
-
function getEvidenceWeight(
|
|
408
|
-
signal: WhoisEvidenceSignal,
|
|
409
|
-
intent: WhoisQueryIntent,
|
|
410
|
-
profile: ProfileRecord,
|
|
411
|
-
) {
|
|
412
|
-
const bio = profile.bio.toLowerCase();
|
|
413
|
-
const ecosystemPenalty =
|
|
414
|
-
signal.kind === "bio_handle" || signal.kind === "bio_company"
|
|
415
|
-
? isEcosystemMention(bio, intent)
|
|
416
|
-
: false;
|
|
417
|
-
switch (signal.kind) {
|
|
418
|
-
case "affiliation":
|
|
419
|
-
return 72;
|
|
420
|
-
case "bio_handle":
|
|
421
|
-
return ecosystemPenalty ? 20 : 56;
|
|
422
|
-
case "bio_company":
|
|
423
|
-
return ecosystemPenalty ? 16 : 46;
|
|
424
|
-
case "profile_history":
|
|
425
|
-
return 36;
|
|
426
|
-
case "profile_handle":
|
|
427
|
-
return intent.handles.length > 0 ? 44 : 24;
|
|
428
|
-
case "profile_name":
|
|
429
|
-
case "profile_bio":
|
|
430
|
-
return 24;
|
|
431
|
-
case "dm_context":
|
|
432
|
-
return intent.wantsAffiliation ? 12 : 22;
|
|
433
|
-
case "expanded_url":
|
|
434
|
-
return intent.wantsDomain ? 26 : 8;
|
|
435
|
-
case "profile_url":
|
|
436
|
-
case "profile_bio_url":
|
|
437
|
-
case "bio_domain":
|
|
438
|
-
return intent.wantsDomain ? 30 : 8;
|
|
439
|
-
case "profile_location":
|
|
440
|
-
case "profile_verified_type":
|
|
441
|
-
return 8;
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function isEcosystemMention(bio: string, intent: WhoisQueryIntent) {
|
|
446
|
-
const terms = new Set([
|
|
447
|
-
...intent.terms,
|
|
448
|
-
...intent.handles.map((handle) => handle.replace(/^@/, "")),
|
|
449
|
-
]);
|
|
450
|
-
for (const term of terms) {
|
|
451
|
-
const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
452
|
-
if (
|
|
453
|
-
new RegExp(
|
|
454
|
-
`\\b${escaped}\\s+(star|stars|sponsor|sponsors|campus expert|expert|partner|alumni)\\b`,
|
|
455
|
-
"i",
|
|
456
|
-
).test(bio)
|
|
457
|
-
) {
|
|
458
|
-
return true;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
return false;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
function hasCurrentAffiliationMatch(
|
|
465
|
-
profile: ProfileRecord,
|
|
466
|
-
affiliationQuery: string | undefined,
|
|
467
|
-
) {
|
|
468
|
-
if (!affiliationQuery?.trim()) {
|
|
469
|
-
return true;
|
|
470
|
-
}
|
|
471
|
-
const normalized = normalizeQuery(affiliationQuery);
|
|
472
|
-
return (profile.affiliations ?? []).some((affiliation) =>
|
|
473
|
-
getAffiliationTexts(affiliation).some((text) =>
|
|
474
|
-
matchesQueryText(normalized, text),
|
|
475
|
-
),
|
|
476
|
-
);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
function hasAffiliationEvidenceMatch(
|
|
480
|
-
profile: ProfileRecord,
|
|
481
|
-
profileEvidence: WhoisEvidenceSignal[],
|
|
482
|
-
affiliationQuery: string | undefined,
|
|
483
|
-
) {
|
|
484
|
-
if (!affiliationQuery?.trim()) {
|
|
485
|
-
return true;
|
|
486
|
-
}
|
|
487
|
-
const normalized = normalizeQuery(affiliationQuery);
|
|
488
|
-
if (hasCurrentAffiliationMatch(profile, affiliationQuery)) {
|
|
489
|
-
return true;
|
|
490
|
-
}
|
|
491
|
-
return profileEvidence.some(
|
|
492
|
-
(signal) =>
|
|
493
|
-
(signal.kind === "affiliation" ||
|
|
494
|
-
signal.kind === "bio_handle" ||
|
|
495
|
-
signal.kind === "bio_company" ||
|
|
496
|
-
signal.kind === "profile_history") &&
|
|
497
|
-
matchesQueryText(normalized, signal.value),
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
function hasNonDomainEvidence(profileEvidence: WhoisEvidenceSignal[]) {
|
|
502
|
-
return profileEvidence.some(
|
|
503
|
-
(signal) => !DOMAIN_ONLY_EVIDENCE.has(signal.kind),
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
function getCandidateCategory(
|
|
508
|
-
profile: ProfileRecord,
|
|
509
|
-
profileEvidence: WhoisEvidenceSignal[],
|
|
510
|
-
intent: WhoisQueryIntent,
|
|
511
|
-
messageMatched: boolean,
|
|
512
|
-
): WhoisCandidateCategory {
|
|
513
|
-
if (profileEvidence.some((signal) => signal.kind === "affiliation")) {
|
|
514
|
-
return "likely_affiliated";
|
|
515
|
-
}
|
|
516
|
-
if (
|
|
517
|
-
profileEvidence.some(
|
|
518
|
-
(signal) =>
|
|
519
|
-
signal.kind === "bio_handle" ||
|
|
520
|
-
signal.kind === "bio_company" ||
|
|
521
|
-
signal.kind === "profile_history",
|
|
522
|
-
)
|
|
523
|
-
) {
|
|
524
|
-
return isEcosystemMention(profile.bio.toLowerCase(), intent)
|
|
525
|
-
? "ecosystem"
|
|
526
|
-
: "likely_affiliated";
|
|
527
|
-
}
|
|
528
|
-
if (profileEvidence.some((signal) => DOMAIN_ONLY_EVIDENCE.has(signal.kind))) {
|
|
529
|
-
return "profile_or_link";
|
|
530
|
-
}
|
|
531
|
-
return messageMatched ? "dm_context" : "other";
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
function scoreCandidate(
|
|
535
|
-
query: string,
|
|
536
|
-
conversation: DmConversationItem,
|
|
537
|
-
expansions: UrlExpansionItem[],
|
|
538
|
-
bioEntities: ProfileBioEntity[] = [],
|
|
539
|
-
snapshots: ProfileSnapshot[] = [],
|
|
540
|
-
) {
|
|
541
|
-
const normalized = normalizeQuery(query);
|
|
542
|
-
const intent = getQueryIntent(query);
|
|
543
|
-
const profile = conversation.participant;
|
|
544
|
-
const profileEvidence = collectProfileEvidence(
|
|
545
|
-
normalized,
|
|
546
|
-
profile,
|
|
547
|
-
bioEntities,
|
|
548
|
-
snapshots,
|
|
549
|
-
);
|
|
550
|
-
const affiliationTexts = (profile.affiliations ?? []).flatMap(
|
|
551
|
-
getAffiliationTexts,
|
|
552
|
-
);
|
|
553
|
-
const profileBioUrls = getProfileBioUrls(profile);
|
|
554
|
-
const bioEntityTexts = bioEntities.map((entity) => entity.value);
|
|
555
|
-
const profileHistoryTexts = snapshots.flatMap((snapshot) =>
|
|
556
|
-
getHistoricalSnapshotTexts(snapshot, profile),
|
|
557
|
-
);
|
|
558
|
-
const messageTexts = getMessageTexts(conversation);
|
|
559
|
-
const haystack = [
|
|
560
|
-
conversation.title,
|
|
561
|
-
profile.handle,
|
|
562
|
-
profile.displayName,
|
|
563
|
-
profile.bio,
|
|
564
|
-
profile.location,
|
|
565
|
-
profile.url,
|
|
566
|
-
profile.verifiedType,
|
|
567
|
-
...profileBioUrls,
|
|
568
|
-
...affiliationTexts,
|
|
569
|
-
...bioEntityTexts,
|
|
570
|
-
...profileHistoryTexts,
|
|
571
|
-
...messageTexts,
|
|
572
|
-
]
|
|
573
|
-
.filter(Boolean)
|
|
574
|
-
.join("\n")
|
|
575
|
-
.toLowerCase();
|
|
576
|
-
const reasons: string[] = [];
|
|
577
|
-
let confidence = 0;
|
|
578
|
-
|
|
579
|
-
if (!/^id\d+$/.test(conversation.participant.handle)) {
|
|
580
|
-
confidence += 18;
|
|
581
|
-
reasons.push("resolved profile");
|
|
582
|
-
}
|
|
583
|
-
if (
|
|
584
|
-
matchesQueryText(normalized, profile.handle) ||
|
|
585
|
-
matchesQueryText(normalized, profile.displayName) ||
|
|
586
|
-
matchesQueryText(normalized, profile.bio)
|
|
587
|
-
) {
|
|
588
|
-
confidence += 18;
|
|
589
|
-
reasons.push("profile matches query");
|
|
590
|
-
}
|
|
591
|
-
if (
|
|
592
|
-
matchesQueryText(normalized, profile.url) ||
|
|
593
|
-
profileBioUrls.some((url) => matchesQueryText(normalized, url))
|
|
594
|
-
) {
|
|
595
|
-
confidence += intent.wantsDomain ? 28 : 8;
|
|
596
|
-
reasons.push("profile URL matches query");
|
|
597
|
-
}
|
|
598
|
-
if (profileEvidence.some((signal) => signal.kind === "affiliation")) {
|
|
599
|
-
confidence += 40;
|
|
600
|
-
reasons.push("affiliation matches query");
|
|
601
|
-
}
|
|
602
|
-
if (profileEvidence.some((signal) => signal.source === "bio_entity")) {
|
|
603
|
-
confidence += 24;
|
|
604
|
-
reasons.push("bio entity matches query");
|
|
605
|
-
}
|
|
606
|
-
if (profileEvidence.some((signal) => signal.source === "history")) {
|
|
607
|
-
confidence += 20;
|
|
608
|
-
reasons.push("profile history matches query");
|
|
609
|
-
}
|
|
610
|
-
if (haystack.includes("co-founder") || haystack.includes("cofounder")) {
|
|
611
|
-
confidence += 25;
|
|
612
|
-
reasons.push("cofounder language");
|
|
613
|
-
}
|
|
614
|
-
const messageMatched = messageTexts.some((text) =>
|
|
615
|
-
matchesQueryText(normalized, text),
|
|
616
|
-
);
|
|
617
|
-
if (messageMatched) {
|
|
618
|
-
confidence += intent.wantsAffiliation ? 10 : 18;
|
|
619
|
-
reasons.push("message text matches query");
|
|
620
|
-
profileEvidence.push({
|
|
621
|
-
kind: "dm_context",
|
|
622
|
-
value: query,
|
|
623
|
-
source: "dm",
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
if (expansions.some((item) => matchesQueryText(normalized, item.finalUrl))) {
|
|
627
|
-
confidence += intent.wantsDomain ? 20 : 8;
|
|
628
|
-
reasons.push("expanded URL matches query");
|
|
629
|
-
for (const item of expansions) {
|
|
630
|
-
pushMatchEvidence(
|
|
631
|
-
profileEvidence,
|
|
632
|
-
normalized,
|
|
633
|
-
"expanded_url",
|
|
634
|
-
item.finalUrl,
|
|
635
|
-
"url",
|
|
636
|
-
);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
for (const signal of profileEvidence) {
|
|
641
|
-
confidence += getEvidenceWeight(signal, intent, profile);
|
|
642
|
-
}
|
|
643
|
-
const category = getCandidateCategory(
|
|
644
|
-
profile,
|
|
645
|
-
profileEvidence,
|
|
646
|
-
intent,
|
|
647
|
-
messageMatched,
|
|
648
|
-
);
|
|
649
|
-
if (category === "ecosystem") {
|
|
650
|
-
confidence -= 24;
|
|
651
|
-
reasons.push("ecosystem mention, not direct affiliation");
|
|
652
|
-
}
|
|
653
|
-
if (category === "profile_or_link" && intent.wantsAffiliation) {
|
|
654
|
-
confidence -= 18;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
return {
|
|
658
|
-
confidence: Math.max(0, Math.min(100, confidence)),
|
|
659
|
-
category,
|
|
660
|
-
reasons: reasons.length > 0 ? reasons : ["local DM match"],
|
|
661
|
-
profileEvidence,
|
|
662
|
-
};
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
function attachExpansionsToMatches(
|
|
666
|
-
conversation: DmConversationItem,
|
|
667
|
-
expansions: UrlExpansionItem[],
|
|
668
|
-
) {
|
|
669
|
-
for (const match of conversation.matches ?? []) {
|
|
670
|
-
const matchUrls = new Set(
|
|
671
|
-
[...match.before, match.message, ...match.after].flatMap((message) =>
|
|
672
|
-
expansions
|
|
673
|
-
.filter((item) => message.text.includes(item.url))
|
|
674
|
-
.map((item) => item.url),
|
|
675
|
-
),
|
|
676
|
-
);
|
|
677
|
-
if (matchUrls.size > 0) {
|
|
678
|
-
match.urlExpansions = expansions.filter((item) =>
|
|
679
|
-
matchUrls.has(item.url),
|
|
680
|
-
);
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
function mergeConversations(
|
|
686
|
-
target: Map<string, DmConversationItem>,
|
|
687
|
-
conversations: DmConversationItem[],
|
|
688
|
-
) {
|
|
689
|
-
for (const conversation of conversations) {
|
|
690
|
-
if (!target.has(conversation.id)) {
|
|
691
|
-
target.set(conversation.id, conversation);
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
function findProfileEvidenceConversationIds(
|
|
697
|
-
query: string,
|
|
698
|
-
account: string | undefined,
|
|
699
|
-
limit: number,
|
|
700
|
-
) {
|
|
701
|
-
const terms = [normalizeQuery(query), ...getSignificantQueryTerms(query)]
|
|
702
|
-
.filter((term) => term.length > 0)
|
|
703
|
-
.filter((term, index, array) => array.indexOf(term) === index);
|
|
704
|
-
if (terms.length === 0) {
|
|
705
|
-
return [];
|
|
706
|
-
}
|
|
707
|
-
const db = getNativeDb();
|
|
708
|
-
ensureIdentitySearchIndexForDmProfiles(db, account);
|
|
709
|
-
const clauses: string[] = [];
|
|
710
|
-
const params: Array<string | number> = [];
|
|
711
|
-
for (const term of terms) {
|
|
712
|
-
const pattern = `%${term}%`;
|
|
713
|
-
clauses.push("isi.normalized_value like ?");
|
|
714
|
-
params.push(pattern);
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
let accountClause = "";
|
|
718
|
-
if (account && account !== "all") {
|
|
719
|
-
accountClause = "and c.account_id = ?";
|
|
720
|
-
params.push(account);
|
|
721
|
-
}
|
|
722
|
-
params.push(limit);
|
|
723
|
-
|
|
724
|
-
const rows = db
|
|
725
|
-
.prepare(
|
|
726
|
-
`
|
|
727
|
-
select c.id, max(isi.weight) as max_weight
|
|
728
|
-
from dm_conversations c
|
|
729
|
-
join identity_search_index isi on isi.profile_id = c.participant_profile_id
|
|
730
|
-
where (${clauses.map((clause) => `(${clause})`).join(" or ")})
|
|
731
|
-
${accountClause}
|
|
732
|
-
group by c.id
|
|
733
|
-
order by max_weight desc, c.last_message_at desc
|
|
734
|
-
limit ?
|
|
735
|
-
`,
|
|
736
|
-
)
|
|
737
|
-
.all(...params) as Array<{ id: string }>;
|
|
738
|
-
return rows.map((row) => row.id);
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
function loadWhoisConversations(
|
|
742
|
-
query: string,
|
|
743
|
-
options: WhoisOptions,
|
|
744
|
-
includeDms: boolean,
|
|
745
|
-
context: number,
|
|
746
|
-
limit: number,
|
|
747
|
-
) {
|
|
748
|
-
if (!includeDms) {
|
|
749
|
-
return [];
|
|
750
|
-
}
|
|
751
|
-
const merged = new Map<string, DmConversationItem>();
|
|
752
|
-
const batchLimit = Math.max(limit * 3, 20);
|
|
753
|
-
for (const search of getDmSearchQueries(query)) {
|
|
754
|
-
mergeConversations(
|
|
755
|
-
merged,
|
|
756
|
-
listDmConversations({
|
|
757
|
-
account: options.account,
|
|
758
|
-
search,
|
|
759
|
-
context,
|
|
760
|
-
limit: batchLimit,
|
|
761
|
-
}),
|
|
762
|
-
);
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
const profileEvidenceIds = findProfileEvidenceConversationIds(
|
|
766
|
-
query,
|
|
767
|
-
options.account,
|
|
768
|
-
Math.max(limit * 5, 50),
|
|
769
|
-
);
|
|
770
|
-
if (profileEvidenceIds.length > 0) {
|
|
771
|
-
mergeConversations(
|
|
772
|
-
merged,
|
|
773
|
-
listDmConversations({
|
|
774
|
-
account: options.account,
|
|
775
|
-
conversationIds: profileEvidenceIds,
|
|
776
|
-
limit: profileEvidenceIds.length,
|
|
777
|
-
}),
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
return [...merged.values()];
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
export function runWhoisEffect(
|
|
785
|
-
query: string,
|
|
786
|
-
options: WhoisOptions = {},
|
|
787
|
-
): Effect.Effect<WhoisResult, unknown> {
|
|
788
|
-
return Effect.gen(function* () {
|
|
789
|
-
const includeDms = options.dms ?? true;
|
|
790
|
-
const includeTweets = options.tweets ?? false;
|
|
791
|
-
const limit = options.limit ?? 10;
|
|
792
|
-
const context = options.context ?? 4;
|
|
793
|
-
let conversations = yield* trySync(() =>
|
|
794
|
-
loadWhoisConversations(query, options, includeDms, context, limit),
|
|
795
|
-
);
|
|
796
|
-
let profileResolution: WhoisResult["profileResolution"];
|
|
797
|
-
|
|
798
|
-
if (options.resolveProfiles ?? true) {
|
|
799
|
-
profileResolution = yield* resolveProfilesForIdsEffect(
|
|
800
|
-
conversations.map((item) => item.participant.id),
|
|
801
|
-
{
|
|
802
|
-
refresh: options.refreshProfileCache,
|
|
803
|
-
xurlFallback: options.xurlFallback ?? true,
|
|
804
|
-
},
|
|
805
|
-
);
|
|
806
|
-
conversations = yield* trySync(() =>
|
|
807
|
-
loadWhoisConversations(query, options, includeDms, context, limit),
|
|
808
|
-
);
|
|
809
|
-
}
|
|
810
|
-
yield* trySync(() =>
|
|
811
|
-
syncIdentitySearchIndexForProfileIds(
|
|
812
|
-
getNativeDb(),
|
|
813
|
-
conversations.map((item) => item.participant.id),
|
|
814
|
-
),
|
|
815
|
-
);
|
|
816
|
-
|
|
817
|
-
const relatedTweets = includeTweets
|
|
818
|
-
? yield* trySync(() => [
|
|
819
|
-
...listTimelineItems({
|
|
820
|
-
resource: "home",
|
|
821
|
-
account: options.account,
|
|
822
|
-
search: query,
|
|
823
|
-
limit,
|
|
824
|
-
}),
|
|
825
|
-
...listTimelineItems({
|
|
826
|
-
resource: "mentions",
|
|
827
|
-
account: options.account,
|
|
828
|
-
search: query,
|
|
829
|
-
limit,
|
|
830
|
-
}),
|
|
831
|
-
])
|
|
832
|
-
: [];
|
|
833
|
-
|
|
834
|
-
const texts = yield* trySync(() => [
|
|
835
|
-
...conversations.flatMap(getMessageTexts),
|
|
836
|
-
...conversations.flatMap((conversation) =>
|
|
837
|
-
[
|
|
838
|
-
conversation.participant.bio,
|
|
839
|
-
conversation.participant.url ?? "",
|
|
840
|
-
...getProfileBioUrls(conversation.participant),
|
|
841
|
-
].filter((text) => text.includes("https://t.co/")),
|
|
842
|
-
),
|
|
843
|
-
...relatedTweets.map((tweet) => tweet.text),
|
|
844
|
-
]);
|
|
845
|
-
const urlExpansions =
|
|
846
|
-
(options.expandUrls ?? true)
|
|
847
|
-
? yield* expandUrlsFromTextsEffect(texts, {
|
|
848
|
-
refresh: options.refreshUrlCache,
|
|
849
|
-
})
|
|
850
|
-
: [];
|
|
851
|
-
yield* trySync(() => {
|
|
852
|
-
for (const conversation of conversations) {
|
|
853
|
-
attachExpansionsToMatches(conversation, urlExpansions);
|
|
854
|
-
}
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
const candidates = yield* trySync(() => {
|
|
858
|
-
const profileIds = conversations.map(
|
|
859
|
-
(conversation) => conversation.participant.id,
|
|
860
|
-
);
|
|
861
|
-
const db = getNativeDb();
|
|
862
|
-
const bioEntitiesByProfile = fetchProfileBioEntities(db, profileIds);
|
|
863
|
-
const snapshotsByProfile = fetchProfileSnapshots(db, profileIds);
|
|
864
|
-
return conversations
|
|
865
|
-
.map((conversation): WhoisCandidate | null => {
|
|
866
|
-
const conversationExpansions = urlExpansions.filter((item) =>
|
|
867
|
-
getMessageTexts(conversation).some((text) =>
|
|
868
|
-
text.includes(item.url),
|
|
869
|
-
),
|
|
870
|
-
);
|
|
871
|
-
const profileId = conversation.participant.id;
|
|
872
|
-
const score = scoreCandidate(
|
|
873
|
-
query,
|
|
874
|
-
conversation,
|
|
875
|
-
conversationExpansions,
|
|
876
|
-
bioEntitiesByProfile.get(profileId) ?? [],
|
|
877
|
-
snapshotsByProfile.get(profileId) ?? [],
|
|
878
|
-
);
|
|
879
|
-
if (
|
|
880
|
-
!hasCurrentAffiliationMatch(
|
|
881
|
-
conversation.participant,
|
|
882
|
-
options.currentAffiliation,
|
|
883
|
-
)
|
|
884
|
-
) {
|
|
885
|
-
return null;
|
|
886
|
-
}
|
|
887
|
-
if (
|
|
888
|
-
!hasAffiliationEvidenceMatch(
|
|
889
|
-
conversation.participant,
|
|
890
|
-
score.profileEvidence,
|
|
891
|
-
options.affiliation,
|
|
892
|
-
)
|
|
893
|
-
) {
|
|
894
|
-
return null;
|
|
895
|
-
}
|
|
896
|
-
if (
|
|
897
|
-
options.excludeDomainOnly &&
|
|
898
|
-
!hasNonDomainEvidence(score.profileEvidence)
|
|
899
|
-
) {
|
|
900
|
-
return null;
|
|
901
|
-
}
|
|
902
|
-
return {
|
|
903
|
-
conversation,
|
|
904
|
-
confidence: score.confidence,
|
|
905
|
-
category: score.category,
|
|
906
|
-
reasons: score.reasons,
|
|
907
|
-
profileEvidence: score.profileEvidence,
|
|
908
|
-
evidence: (conversation.matches ?? []).map((match) => ({
|
|
909
|
-
messageId: match.message.id,
|
|
910
|
-
createdAt: match.message.createdAt,
|
|
911
|
-
direction: match.message.direction,
|
|
912
|
-
text: match.message.text,
|
|
913
|
-
...(match.urlExpansions
|
|
914
|
-
? { urlExpansions: match.urlExpansions }
|
|
915
|
-
: {}),
|
|
916
|
-
})),
|
|
917
|
-
};
|
|
918
|
-
})
|
|
919
|
-
.filter((candidate): candidate is WhoisCandidate => candidate !== null)
|
|
920
|
-
.sort((left, right) => {
|
|
921
|
-
if (right.confidence !== left.confidence) {
|
|
922
|
-
return right.confidence - left.confidence;
|
|
923
|
-
}
|
|
924
|
-
return (
|
|
925
|
-
new Date(right.conversation.lastMessageAt).getTime() -
|
|
926
|
-
new Date(left.conversation.lastMessageAt).getTime()
|
|
927
|
-
);
|
|
928
|
-
})
|
|
929
|
-
.slice(0, limit);
|
|
930
|
-
});
|
|
931
|
-
|
|
932
|
-
return {
|
|
933
|
-
query,
|
|
934
|
-
candidates,
|
|
935
|
-
relatedTweets,
|
|
936
|
-
urlExpansions,
|
|
937
|
-
...(profileResolution ? { profileResolution } : {}),
|
|
938
|
-
};
|
|
939
|
-
});
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
export function runWhois(
|
|
943
|
-
query: string,
|
|
944
|
-
options: WhoisOptions = {},
|
|
945
|
-
): Promise<WhoisResult> {
|
|
946
|
-
return runEffectPromise(runWhoisEffect(query, options));
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
const CATEGORY_LABELS: Record<WhoisCandidateCategory, string> = {
|
|
950
|
-
likely_affiliated: "Likely affiliated",
|
|
951
|
-
ecosystem: "Ecosystem / role mentions",
|
|
952
|
-
profile_or_link: "Profile or link matches",
|
|
953
|
-
dm_context: "DM context matches",
|
|
954
|
-
other: "Other local matches",
|
|
955
|
-
};
|
|
956
|
-
|
|
957
|
-
function summarizeSignal(signal: WhoisEvidenceSignal) {
|
|
958
|
-
switch (signal.kind) {
|
|
959
|
-
case "affiliation":
|
|
960
|
-
return `current affiliation ${signal.value}`;
|
|
961
|
-
case "bio_handle":
|
|
962
|
-
return `bio handle ${signal.value}`;
|
|
963
|
-
case "bio_company":
|
|
964
|
-
return `bio company ${signal.value}`;
|
|
965
|
-
case "profile_history":
|
|
966
|
-
return `profile history ${signal.value}`;
|
|
967
|
-
case "profile_url":
|
|
968
|
-
case "profile_bio_url":
|
|
969
|
-
case "bio_domain":
|
|
970
|
-
return `domain/link ${signal.value}`;
|
|
971
|
-
case "dm_context":
|
|
972
|
-
return `DM context`;
|
|
973
|
-
case "expanded_url":
|
|
974
|
-
return `expanded URL ${signal.value}`;
|
|
975
|
-
default:
|
|
976
|
-
return `${signal.kind.replace(/^profile_/, "profile ")} ${signal.value}`;
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
function signalSummaryRank(signal: WhoisEvidenceSignal) {
|
|
981
|
-
switch (signal.kind) {
|
|
982
|
-
case "affiliation":
|
|
983
|
-
return 0;
|
|
984
|
-
case "bio_handle":
|
|
985
|
-
return 1;
|
|
986
|
-
case "bio_company":
|
|
987
|
-
return 2;
|
|
988
|
-
case "profile_history":
|
|
989
|
-
return 3;
|
|
990
|
-
case "dm_context":
|
|
991
|
-
return 4;
|
|
992
|
-
case "expanded_url":
|
|
993
|
-
return 5;
|
|
994
|
-
case "profile_url":
|
|
995
|
-
case "profile_bio_url":
|
|
996
|
-
case "bio_domain":
|
|
997
|
-
return 6;
|
|
998
|
-
default:
|
|
999
|
-
return 7;
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
function explainCandidate(candidate: WhoisCandidate) {
|
|
1004
|
-
const evidence = candidate.profileEvidence
|
|
1005
|
-
.slice()
|
|
1006
|
-
.sort((left, right) => signalSummaryRank(left) - signalSummaryRank(right))
|
|
1007
|
-
.slice(0, 4)
|
|
1008
|
-
.map(summarizeSignal);
|
|
1009
|
-
return evidence.length > 0
|
|
1010
|
-
? evidence.join(" + ")
|
|
1011
|
-
: candidate.reasons.join(", ");
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
export function formatWhois(result: WhoisResult) {
|
|
1015
|
-
const lines = [`Whois: ${result.query}`];
|
|
1016
|
-
if (result.candidates.length === 0) {
|
|
1017
|
-
lines.push("No matching DM candidates.");
|
|
1018
|
-
} else {
|
|
1019
|
-
let activeCategory: WhoisCandidateCategory | undefined;
|
|
1020
|
-
for (const candidate of result.candidates) {
|
|
1021
|
-
const profile = candidate.conversation.participant;
|
|
1022
|
-
if (activeCategory !== candidate.category) {
|
|
1023
|
-
activeCategory = candidate.category;
|
|
1024
|
-
lines.push("");
|
|
1025
|
-
lines.push(CATEGORY_LABELS[candidate.category]);
|
|
1026
|
-
}
|
|
1027
|
-
lines.push("");
|
|
1028
|
-
lines.push(
|
|
1029
|
-
`${candidate.confidence}% @${profile.handle} (${profile.displayName})`,
|
|
1030
|
-
);
|
|
1031
|
-
lines.push(`Why: ${explainCandidate(candidate)}`);
|
|
1032
|
-
lines.push(`Reasons: ${candidate.reasons.join(", ")}`);
|
|
1033
|
-
lines.push(`Conversation: ${candidate.conversation.id}`);
|
|
1034
|
-
for (const signal of candidate.profileEvidence.slice(0, 5)) {
|
|
1035
|
-
lines.push(`Evidence: ${signal.kind}: ${signal.value}`);
|
|
1036
|
-
}
|
|
1037
|
-
for (const evidence of candidate.evidence.slice(0, 3)) {
|
|
1038
|
-
lines.push(
|
|
1039
|
-
`- ${evidence.createdAt} ${evidence.direction}: ${evidence.text}`,
|
|
1040
|
-
);
|
|
1041
|
-
for (const expansion of evidence.urlExpansions ?? []) {
|
|
1042
|
-
lines.push(` ${expansion.url} -> ${expansion.finalUrl}`);
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
if (result.relatedTweets.length > 0) {
|
|
1049
|
-
lines.push("");
|
|
1050
|
-
lines.push(`Related tweets: ${result.relatedTweets.length}`);
|
|
1051
|
-
for (const tweet of result.relatedTweets.slice(0, 5)) {
|
|
1052
|
-
lines.push(`- ${tweet.createdAt} @${tweet.author.handle}: ${tweet.text}`);
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
return lines.join("\n");
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
export const __test__ = {
|
|
1060
|
-
getSignificantQueryTerms,
|
|
1061
|
-
getQueryDomains,
|
|
1062
|
-
getQueryHandles,
|
|
1063
|
-
getQueryIntent,
|
|
1064
|
-
matchesQueryText,
|
|
1065
|
-
getDmSearchQueries,
|
|
1066
|
-
getUrlEntityExpandedUrl,
|
|
1067
|
-
getProfileBioUrls,
|
|
1068
|
-
getAffiliationTexts,
|
|
1069
|
-
getSnapshotAffiliationTexts,
|
|
1070
|
-
getHistoricalSnapshotTexts,
|
|
1071
|
-
collectProfileEvidence,
|
|
1072
|
-
hasCurrentAffiliationMatch,
|
|
1073
|
-
hasAffiliationEvidenceMatch,
|
|
1074
|
-
hasNonDomainEvidence,
|
|
1075
|
-
scoreCandidate,
|
|
1076
|
-
explainCandidate,
|
|
1077
|
-
attachExpansionsToMatches,
|
|
1078
|
-
mergeConversations,
|
|
1079
|
-
};
|