File size: 19,457 Bytes
fa9c65f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | /**
* Focal Point Detector Core - Intelligence Synthesis Layer
*
* Correlates news entities with map signals to identify "main characters"
* that appear across multiple intelligence streams.
*
* Example: IRAN mentioned in 12 news clusters + 5 military flights + internet outage
* = CRITICAL focal point with rich narrative for AI
*
* Extracted from `src/services/focal-point-detector.ts`. Everything here is pure
* and dependency-free (no `src/`, no `@/`, no DOM) so it can be bundled for Edge
* runtimes and driven from server-side callers. The entity index is injected;
* this module never reaches for a singleton. `src/services/focal-point-detector.ts`
* keeps the stateful `focalPointDetector` singleton the dashboard uses.
*/
import {
extractEntityContexts,
type EntityIndex,
} from './entity-extraction-core.js';
import type { EntityType } from './entity-registry.js';
// ============================================================================
// Structural input shapes β the subset of the client types this core reads.
// The corresponding `src/` types are structurally assignable to these.
// ============================================================================
export type SignalType =
| 'internet_outage'
| 'military_flight'
| 'military_vessel'
| 'protest'
| 'ais_disruption'
| 'satellite_fire'
| 'radiation_anomaly'
| 'temporal_anomaly'
| 'sanctions_pressure'
| 'active_strike';
export interface GeoSignal {
type: SignalType;
severity: 'low' | 'medium' | 'high';
strikeCount?: number;
highSeverityStrikeCount?: number;
}
export interface CountrySignalCluster {
country: string;
signals: GeoSignal[];
signalTypes: Set<SignalType>;
totalCount: number;
highSeverityCount: number;
}
export interface SignalSummary {
topCountries: CountrySignalCluster[];
}
/** Minimal structural shape the detector reads off a news cluster. */
export interface FocalClusterInput {
id: string;
primaryTitle: string;
primaryLink: string;
allItems?: Array<{ title: string }>;
}
/**
* Minimal shape of a per-cluster news entity context. Both the shared
* `extractEntityContexts` and the client's `extractEntitiesFromClusters`
* produce maps that satisfy this.
*/
export interface EntityContextInput {
entities: Array<{ entityId: string; confidence: number }>;
}
// ============================================================================
// Output shapes
// ============================================================================
export type FocalPointUrgency = 'watch' | 'elevated' | 'critical';
export interface HeadlineWithUrl {
title: string;
url: string;
}
export interface EntityMention {
entityId: string;
entityType: EntityType;
displayName: string;
mentionCount: number;
avgConfidence: number;
clusterIds: string[];
topHeadlines: HeadlineWithUrl[];
}
export interface FocalPoint {
id: string;
entityId: string;
entityType: EntityType;
displayName: string;
// News dimension
newsMentions: number;
newsVelocity: number;
topHeadlines: HeadlineWithUrl[];
// Signal dimension
signalTypes: SignalType[];
signalCount: number;
highSeverityCount: number;
signalDescriptions: string[];
// Scoring
focalScore: number;
urgency: FocalPointUrgency;
// For AI context
narrative: string;
correlationEvidence: string[];
}
export interface FocalPointSummary {
timestamp: Date;
focalPoints: FocalPoint[];
aiContext: string;
topCountries: FocalPoint[];
topCompanies: FocalPoint[];
}
export const SIGNAL_TYPE_LABELS: Record<SignalType, string> = {
internet_outage: 'internet outage',
military_flight: 'military flights',
military_vessel: 'naval vessels',
protest: 'protests',
ais_disruption: 'shipping disruption',
satellite_fire: 'satellite fires',
radiation_anomaly: 'radiation anomalies',
temporal_anomaly: 'anomaly detection',
sanctions_pressure: 'sanctions pressure',
active_strike: 'active strikes',
};
export const SIGNAL_TYPE_ICONS: Record<SignalType, string> = {
internet_outage: 'π',
military_flight: 'βοΈ',
military_vessel: 'β',
protest: 'π’',
ais_disruption: 'π’',
satellite_fire: 'π₯',
radiation_anomaly: 'β’οΈ',
temporal_anomaly: 'π',
sanctions_pressure: 'π«',
active_strike: 'π₯',
};
/**
* Check if entity name/alias appears in headline title (case-insensitive)
* This ensures we only show headlines that are actually ABOUT the entity
*/
export function entityAppearsInTitle(entityId: string, title: string, index: EntityIndex): boolean {
const entity = index.byId.get(entityId);
if (!entity) return false;
const titleLower = title.toLowerCase();
// Check entity name
if (titleLower.includes(entity.name.toLowerCase())) return true;
// Check aliases
for (const alias of entity.aliases) {
if (titleLower.includes(alias.toLowerCase())) return true;
}
return false;
}
/**
* Aggregate entity mentions across all news clusters
*/
export function aggregateEntities(
entityContexts: Map<string, EntityContextInput>,
clusters: FocalClusterInput[],
index: EntityIndex
): Map<string, EntityMention> {
const mentions = new Map<string, EntityMention>();
const clustersById = new Map<string, FocalClusterInput>();
for (const cluster of clusters) {
if (!clustersById.has(cluster.id)) clustersById.set(cluster.id, cluster);
}
for (const [clusterId, context] of entityContexts) {
const cluster = clustersById.get(clusterId);
if (!cluster) continue;
for (const entity of context.entities) {
const entityEntry = index.byId.get(entity.entityId);
if (!entityEntry) continue;
// Only add headline if entity appears in the title (not just mentioned in body)
const titleHasEntity = entityAppearsInTitle(entity.entityId, cluster.primaryTitle, index);
const existing = mentions.get(entity.entityId);
if (existing) {
existing.mentionCount++;
existing.avgConfidence = (existing.avgConfidence * (existing.mentionCount - 1) + entity.confidence) / existing.mentionCount;
existing.clusterIds.push(clusterId);
// Only add headlines where entity is prominent in title
if (existing.topHeadlines.length < 3 && titleHasEntity) {
existing.topHeadlines.push({ title: cluster.primaryTitle, url: cluster.primaryLink });
}
} else {
mentions.set(entity.entityId, {
entityId: entity.entityId,
entityType: entityEntry.type,
displayName: entityEntry.name,
mentionCount: 1,
avgConfidence: entity.confidence,
clusterIds: [clusterId],
// Only include headline if entity appears in title
topHeadlines: titleHasEntity ? [{ title: cluster.primaryTitle, url: cluster.primaryLink }] : [],
});
}
}
}
return mentions;
}
/**
* Build focal points by correlating news entities with map signals
*/
export function buildFocalPoints(
entityMentions: Map<string, EntityMention>,
signalSummary: SignalSummary,
index: EntityIndex
): FocalPoint[] {
const focalPoints: FocalPoint[] = [];
const countrySignals = new Map<string, CountrySignalCluster>();
for (const cluster of signalSummary.topCountries) {
countrySignals.set(cluster.country, cluster);
}
for (const [entityId, mention] of entityMentions) {
const entityEntry = index.byId.get(entityId);
if (!entityEntry) continue;
let signals: CountrySignalCluster | undefined;
let signalCountry: string | undefined;
if (entityEntry.type === 'country') {
signals = countrySignals.get(entityId);
signalCountry = entityId;
} else if (entityEntry.related) {
for (const relatedId of entityEntry.related) {
const relatedEntity = index.byId.get(relatedId);
if (relatedEntity?.type === 'country') {
signals = countrySignals.get(relatedId);
if (signals) {
signalCountry = relatedId;
break;
}
}
}
}
const focalPoint = createFocalPoint(mention, signals, signalCountry);
focalPoints.push(focalPoint);
}
for (const [countryCode, signals] of countrySignals) {
if (!entityMentions.has(countryCode)) {
const countryEntity = index.byId.get(countryCode);
if (countryEntity) {
const mention: EntityMention = {
entityId: countryCode,
entityType: 'country',
displayName: countryEntity.name,
mentionCount: 0,
avgConfidence: 0,
clusterIds: [],
topHeadlines: [],
};
const focalPoint = createFocalPoint(mention, signals, countryCode);
if (focalPoint.focalScore > 20) {
focalPoints.push(focalPoint);
}
}
}
}
return focalPoints.sort((a, b) => b.focalScore - a.focalScore);
}
/**
* Create a focal point with scoring and narrative
*/
export function createFocalPoint(
mention: EntityMention,
signals: CountrySignalCluster | undefined,
_signalCountry: string | undefined
): FocalPoint {
const newsScore = calculateNewsScore(mention);
const signalScore = signals ? calculateSignalScore(signals) : 0;
const correlationBonus = calculateCorrelationBonus(mention, signals);
const conflictScore = signals ? calculateConflictScore(signals) : 0;
const rawScore = newsScore + signalScore + correlationBonus + conflictScore;
const signalTypes = signals ? Array.from(signals.signalTypes) : [];
const urgency = determineUrgency(rawScore, signalTypes.length);
const urgencyMultiplier = urgency === 'critical' ? 1.3 : urgency === 'elevated' ? 1.15 : 1.0;
const focalScore = Math.min(100, rawScore * urgencyMultiplier);
const signalDescriptions = signals
? signalTypes.map(type => {
const count = signals.signals.filter(s => s.type === type).length;
return `${count} ${SIGNAL_TYPE_LABELS[type]}`;
})
: [];
const narrative = generateNarrative(mention, signals, signalTypes);
const correlationEvidence = getCorrelationEvidence(mention, signals);
return {
id: `fp-${mention.entityId}`,
entityId: mention.entityId,
entityType: mention.entityType,
displayName: mention.displayName,
newsMentions: mention.mentionCount,
newsVelocity: mention.mentionCount / 24,
topHeadlines: mention.topHeadlines,
signalTypes,
signalCount: signals?.totalCount || 0,
highSeverityCount: signals?.highSeverityCount || 0,
signalDescriptions,
focalScore,
urgency,
narrative,
correlationEvidence,
};
}
export function calculateNewsScore(mention: Pick<EntityMention, 'mentionCount' | 'avgConfidence'>): number {
const base = Math.min(20, mention.mentionCount * 4);
const velocity = Math.min(10, (mention.mentionCount / 24) * 2);
const confidence = mention.avgConfidence * 10;
return base + velocity + confidence;
}
export function calculateSignalScore(signals: CountrySignalCluster): number {
const nonStrike = signals.signals.filter(s => s.type !== 'active_strike');
const types = new Set(nonStrike.map(s => s.type));
const typeBonus = types.size * 10;
const countBonus = Math.min(15, nonStrike.length * 3);
const severityBonus = nonStrike.filter(s => s.severity === 'high').length * 5;
return typeBonus + countBonus + severityBonus;
}
export function calculateConflictScore(signals: CountrySignalCluster): number {
const strikeSignals = signals.signals.filter(s => s.type === 'active_strike');
if (strikeSignals.length === 0) return 0;
let totalCount = 0;
let highSevCount = 0;
for (const s of strikeSignals) {
totalCount += s.strikeCount ?? 0;
highSevCount += s.highSeverityStrikeCount ?? 0;
}
const base = Math.min(30, totalCount * 1.5);
const severityBonus = Math.min(30, highSevCount * 3);
return base + severityBonus;
}
export function calculateCorrelationBonus(
mention: Pick<EntityMention, 'mentionCount' | 'topHeadlines'>,
signals: CountrySignalCluster | undefined
): number {
let bonus = 0;
if (mention.mentionCount > 0 && signals && signals.totalCount > 0) {
bonus += 10;
}
if (signals && mention.topHeadlines.some(h => {
const lower = h.title.toLowerCase();
return (signals.signalTypes.has('military_flight') && /military|troops|forces|army|air force/.test(lower)) ||
(signals.signalTypes.has('military_vessel') && /navy|naval|ships|fleet|carrier/.test(lower)) ||
(signals.signalTypes.has('protest') && /protest|demonstrat|unrest|riot/.test(lower)) ||
(signals.signalTypes.has('internet_outage') && /internet|blackout|outage|connectivity/.test(lower)) ||
(signals.signalTypes.has('sanctions_pressure') && /sanction|designation|ofac|treasury|embargo|blacklist/.test(lower)) ||
(signals.signalTypes.has('radiation_anomaly') && /nuclear|radiation|reactor|contamination|radnet/.test(lower)) ||
(signals.signalTypes.has('active_strike') && /strike|attack|bomb|missile|target|hit/.test(lower));
})) {
bonus += 5;
}
return bonus;
}
export function determineUrgency(score: number, signalTypeCount: number): FocalPointUrgency {
if (score > 70 || signalTypeCount >= 3) return 'critical';
if (score > 50 || signalTypeCount >= 2) return 'elevated';
return 'watch';
}
export function generateNarrative(
mention: Pick<EntityMention, 'mentionCount' | 'topHeadlines'>,
signals: CountrySignalCluster | undefined,
signalTypes: SignalType[]
): string {
const parts: string[] = [];
if (mention.mentionCount > 0) {
parts.push(`${mention.mentionCount} news mentions`);
}
if (signals && signalTypes.length > 0) {
const signalParts = signalTypes.map(type => {
const count = signals.signals.filter(s => s.type === type).length;
return `${count} ${SIGNAL_TYPE_LABELS[type]}`;
});
parts.push(signalParts.join(', '));
}
if (mention.topHeadlines.length > 0 && mention.topHeadlines[0]) {
const headline = mention.topHeadlines[0].title.slice(0, 60);
parts.push(`"${headline}..."`);
}
return parts.join(' | ');
}
export function getCorrelationEvidence(
mention: Pick<EntityMention, 'mentionCount' | 'displayName'>,
signals: CountrySignalCluster | undefined
): string[] {
const evidence: string[] = [];
if (mention.mentionCount > 0 && signals && signals.totalCount > 0) {
evidence.push(`${mention.displayName} appears in both news (${mention.mentionCount}) and map signals (${signals.totalCount})`);
}
if (signals && signals.signalTypes.size >= 2) {
const types = Array.from(signals.signalTypes).map(t => SIGNAL_TYPE_LABELS[t]);
evidence.push(`Multiple signal convergence: ${types.join(' + ')}`);
}
if (signals && signals.highSeverityCount > 0) {
evidence.push(`${signals.highSeverityCount} high-severity signals detected`);
}
return evidence;
}
/**
* Generate rich AI context for summarization
*/
export function generateAIContext(focalPoints: FocalPoint[]): string {
if (focalPoints.length === 0) {
return '';
}
const lines: string[] = ['[INTELLIGENCE SYNTHESIS]'];
const critical = focalPoints.filter(fp => fp.urgency === 'critical').slice(0, 3);
const elevated = focalPoints.filter(fp => fp.urgency === 'elevated').slice(0, 3);
const correlatedFPs = focalPoints.filter(fp => fp.newsMentions > 0 && fp.signalCount > 0).slice(0, 5);
if (critical.length > 0) {
lines.push('');
lines.push('CRITICAL FOCAL POINTS:');
for (const fp of critical) {
const icons = fp.signalTypes.map(t => SIGNAL_TYPE_ICONS[t as SignalType]).join('');
lines.push(`- ${fp.displayName} [CRITICAL] ${icons}: ${fp.narrative}`);
if (fp.correlationEvidence.length > 0) {
lines.push(` β ${fp.correlationEvidence[0]}`);
}
}
}
if (elevated.length > 0) {
lines.push('');
lines.push('ELEVATED WATCH:');
for (const fp of elevated) {
lines.push(`- ${fp.displayName}: ${fp.newsMentions} news, ${fp.signalCount} signals`);
}
}
if (correlatedFPs.length > 0) {
lines.push('');
lines.push('NEWS-SIGNAL CORRELATIONS:');
for (const fp of correlatedFPs) {
const signalDesc = fp.signalTypes.map(t => SIGNAL_TYPE_LABELS[t as SignalType]).join(', ');
lines.push(`- ${fp.displayName}: news coverage + ${signalDesc} detected`);
}
}
return lines.join('\n');
}
/**
* Generate application-authored context for agent consumers without copying
* source headlines, generated narratives, or correlation evidence.
*/
export function generateAgentSafeAIContext(focalPoints: FocalPoint[]): string {
if (focalPoints.length === 0) {
return '';
}
const lines: string[] = ['[INTELLIGENCE SYNTHESIS]'];
const critical = focalPoints.filter(fp => fp.urgency === 'critical').slice(0, 3);
const elevated = focalPoints.filter(fp => fp.urgency === 'elevated').slice(0, 3);
const correlatedFPs = focalPoints.filter(fp => fp.newsMentions > 0 && fp.signalCount > 0).slice(0, 5);
if (critical.length > 0) {
lines.push('', 'CRITICAL FOCAL POINTS:');
for (const fp of critical) {
const signalDesc = fp.signalTypes
.map(t => SIGNAL_TYPE_LABELS[t as SignalType])
.filter(Boolean)
.join(', ');
const signalSuffix = signalDesc ? ` (${signalDesc})` : '';
lines.push(
`- ${fp.displayName} [CRITICAL]: ${fp.newsMentions} news mentions, ${fp.signalCount} map signals${signalSuffix}`,
);
}
}
if (elevated.length > 0) {
lines.push('', 'ELEVATED WATCH:');
for (const fp of elevated) {
lines.push(`- ${fp.displayName}: ${fp.newsMentions} news, ${fp.signalCount} signals`);
}
}
if (correlatedFPs.length > 0) {
lines.push('', 'NEWS-SIGNAL CORRELATIONS:');
for (const fp of correlatedFPs) {
const signalDesc = fp.signalTypes.map(t => SIGNAL_TYPE_LABELS[t as SignalType]).join(', ');
lines.push(`- ${fp.displayName}: news coverage + ${signalDesc} detected`);
}
}
return lines.join('\n');
}
/**
* Get signal icons for UI display
*/
export function getSignalIcons(signalTypes: string[]): string {
return signalTypes.map(t => SIGNAL_TYPE_ICONS[t as SignalType] || '').join(' ');
}
/**
* Stateless focal point detector. Holds only the injected entity index β
* `analyze` is a pure function of its arguments.
*/
export class FocalPointCore {
constructor(private readonly index: EntityIndex) {}
/**
* Main analysis entry point - correlates news clusters with map signals.
*
* `entityContexts` is optional: server-side callers can omit it and let the
* core run its own extraction, while the dashboard passes the contexts it
* already computed via `src/services/entity-extraction.ts`.
*/
analyze(
clusters: FocalClusterInput[],
signalSummary: SignalSummary,
entityContexts?: Map<string, EntityContextInput>
): FocalPointSummary {
const contexts = entityContexts ?? extractEntityContexts(clusters, this.index);
const entityMentions = aggregateEntities(contexts, clusters, this.index);
const focalPoints = buildFocalPoints(entityMentions, signalSummary, this.index);
const aiContext = generateAIContext(focalPoints);
return {
timestamp: new Date(),
focalPoints,
aiContext,
topCountries: focalPoints.filter(fp => fp.entityType === 'country').slice(0, 5),
topCompanies: focalPoints.filter(fp => fp.entityType === 'company').slice(0, 3),
};
}
}
|