Repository navigation
Expand file tree
/
Copy pathmic.ts
More file actions
676 lines (618 loc) · 21.1 KB
/
Copy pathmic.ts
File metadata and controls
676 lines (618 loc) · 21.1 KB
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/**
* Moss Intelligence Core (MIC) consumer.
*
* Read-only access to signalmap canonical store + per-video analysis.
* Embeddings are stripped at consume time — UI never needs 1536d vectors.
*
* Path resolution: env `MIC_DATA_PATH` points to the directory holding
* `canonical-entities.json`, `canonical-topics.json`, `canonical-events.json`,
* and the per-video `yt-*.json` files emitted by the SignalMap pipeline
* (https://signalmap.moss.land — separate repo). Default: ./mic-data
*
* Caching: in-process LRU on canonical data (5 min TTL). Per-video reads
* are filesystem cached by Node's path resolution; with O(1k) videos that's
* fine. For large scale, swap to a real cache.
*/
import fs from "node:fs";
import path from "node:path";
const MIC_DATA_PATH =
process.env.MIC_DATA_PATH || path.join(process.cwd(), "mic-data");
// ─── types ────────────────────────────────────────────────────────────
export type EntityType = "person" | "org" | "country" | "concept" | "asset";
export type Entity = {
id: string;
label: string;
aliases: string[];
type: EntityType;
videoCount: number;
videoIds: string[];
createdAt: string;
updatedAt: string;
};
export type Topic = {
id: string;
label: string;
aliases: string[];
description?: string;
videoCount: number;
videoIds: string[];
createdAt: string;
updatedAt: string;
};
export type EventItem = {
id: string;
label: string;
aliases: string[];
dateHint: string | null;
relatedEntityIds: string[];
videoCount: number;
videoIds: string[];
createdAt: string;
updatedAt: string;
};
export type VideoSource = {
videoId: string;
url: string;
};
export type VideoMeta = {
title: string;
author_name?: string;
thumbnail_url?: string;
duration_s?: number;
published_at?: string;
view_count?: number;
channel_id?: string;
};
export type Stance = "agree" | "disagree" | "observe" | "neutral" | string;
export type VideoAnalysis = {
summary_oneline?: string;
claims?: string[] | { text?: string }[];
quotes?: { text: string; ts_seconds?: number }[];
topic_label?: string;
topic_description?: string;
stance?: Stance;
stance_reason?: string;
language?: "ko" | "en" | "mixed" | string;
entities?: { name: string; type?: string }[];
events?: { name: string; date_hint?: string | null }[];
};
export type VideoRecord = {
source: VideoSource;
meta: VideoMeta;
analysis?: VideoAnalysis;
canonical_topic_id?: string | null;
canonical_entity_ids?: string[];
canonical_event_ids?: string[];
};
export type PulseSource = {
url: string;
title?: string;
publisher?: string;
publishedAt?: string;
excerpt?: string;
};
export type Pulse = {
id: string;
asset: string;
assetLabel?: string;
direction: "up" | "down" | string;
magnitudePct: number;
windowMinutes: number;
detectedAt: string;
priceFrom?: number;
priceTo?: number;
priceUnit?: string;
summary: string;
confidence: "speculative" | "discussed" | "reported" | "confirmed" | string;
sources?: PulseSource[];
synthesisState?: "raw" | "pending" | "enriched" | "reviewed" | string;
verifiedAt?: string | null;
verifiedSummary?: string | null;
accuracy?: "matched" | "drifted" | "unconfirmed" | string;
};
// ─── canonical loaders (cached) ──────────────────────────────────────
type Cache<T> = { data: T; loadedAt: number };
export type PulseLoadDiagnostics = {
invalidFiles: string[];
duplicateIds: string[];
};
const TTL_MS = 5 * 60 * 1000;
let _entitiesCache: Cache<Entity[]> | null = null;
let _topicsCache: Cache<Topic[]> | null = null;
let _eventsCache: Cache<EventItem[]> | null = null;
let _pulsesCache: Cache<Pulse[]> | null = null;
let _videoIndexCache: Cache<VideoIndexEntry[]> | null = null;
let _pulseDiagnosticsCache: Cache<PulseLoadDiagnostics> | null = null;
function fresh<T>(c: Cache<T> | null): boolean {
return !!c && Date.now() - c.loadedAt < TTL_MS;
}
function readCanonical<T>(file: string, stripEmbeds = true): T[] {
const p = path.join(MIC_DATA_PATH, file);
if (!fs.existsSync(p)) return [];
const raw = JSON.parse(fs.readFileSync(p, "utf8"));
const items = (raw.items as Record<string, unknown>[]) || [];
if (stripEmbeds) {
return items.map((it) => {
const { embedding, centroid, ...rest } = it as {
embedding?: unknown;
centroid?: unknown;
[k: string]: unknown;
};
void embedding;
void centroid;
return rest as T;
});
}
return items as T[];
}
export function getAllEntities(): Entity[] {
if (fresh(_entitiesCache)) return _entitiesCache!.data;
const data = readCanonical<Entity>("canonical-entities.json");
_entitiesCache = { data, loadedAt: Date.now() };
return data;
}
export function getAllTopics(): Topic[] {
if (fresh(_topicsCache)) return _topicsCache!.data;
const data = readCanonical<Topic>("canonical-topics.json");
_topicsCache = { data, loadedAt: Date.now() };
return data;
}
export function getAllEvents(): EventItem[] {
if (fresh(_eventsCache)) return _eventsCache!.data;
const data = readCanonical<EventItem>("canonical-events.json");
_eventsCache = { data, loadedAt: Date.now() };
return data;
}
export function getEntity(id: string): Entity | null {
return getAllEntities().find((e) => e.id === id) || null;
}
export function getTopic(id: string): Topic | null {
return getAllTopics().find((t) => t.id === id) || null;
}
export function getEvent(id: string): EventItem | null {
return getAllEvents().find((e) => e.id === id) || null;
}
export function getEntitiesByType(type: EntityType): Entity[] {
return getAllEntities().filter((e) => e.type === type);
}
// ─── per-video reader ────────────────────────────────────────────────
const videoCache = new Map<string, { rec: VideoRecord; loadedAt: number }>();
const VIDEO_TTL_MS = 30 * 60 * 1000;
export function getVideo(videoId: string): VideoRecord | null {
const cached = videoCache.get(videoId);
if (cached && Date.now() - cached.loadedAt < VIDEO_TTL_MS) return cached.rec;
const p = path.join(MIC_DATA_PATH, `yt-${videoId}.json`);
if (!fs.existsSync(p)) return null;
try {
const raw = JSON.parse(fs.readFileSync(p, "utf8")) as VideoRecord & {
embedding?: unknown;
};
delete raw.embedding;
videoCache.set(videoId, { rec: raw, loadedAt: Date.now() });
return raw;
} catch {
return null;
}
}
/**
* One row per analysed video, from SignalMap's `_signalmap.json` index —
* title, channel, publishedAt, category and the analysis one-liner/claims/
* quotes, WITHOUT the transcript or embedding.
*
* This is the only way to ask "what was published on day X" without opening
* all 11k `yt-*.json` files (2+ GB). One 28 MB parse, cached like the
* canonical files.
*/
export type VideoIndexEntry = {
videoId: string;
channelName?: string;
channelId?: string;
category?: string;
videoTitle?: string;
publishedAt?: string;
topicLabel?: string;
summaryOneline?: string;
stance?: string;
claims?: unknown[];
quotes?: { text?: string; ts_seconds?: number }[];
};
export function getVideoIndex(): VideoIndexEntry[] {
if (fresh(_videoIndexCache)) return _videoIndexCache!.data;
const p = path.join(MIC_DATA_PATH, "_signalmap.json");
let data: VideoIndexEntry[] = [];
if (fs.existsSync(p)) {
try {
const raw = JSON.parse(fs.readFileSync(p, "utf8")) as { videos?: Record<string, unknown>[] };
data = (raw.videos ?? []).map((v) => {
const { embedding, ...rest } = v as { embedding?: unknown; [k: string]: unknown };
void embedding;
return rest as VideoIndexEntry;
});
} catch {
// A half-written file (SignalMap rewrites it) reads as no index this
// round; the next TTL expiry tries again.
data = [];
}
}
_videoIndexCache = { data, loadedAt: Date.now() };
return data;
}
/** Videos whose publishedAt falls in [start, end) (epoch ms). Newest first. */
export function getVideosPublishedBetween(start: number, end: number): VideoIndexEntry[] {
return getVideoIndex()
.filter((v) => {
if (!v.publishedAt) return false;
const t = Date.parse(v.publishedAt);
return Number.isFinite(t) && t >= start && t < end;
})
.sort((a, b) => Date.parse(b.publishedAt!) - Date.parse(a.publishedAt!));
}
/** Get videos for an entity, ordered by published_at desc when available. */
export function getVideosForEntity(entityId: string, limit = 12): VideoRecord[] {
const ent = getEntity(entityId);
if (!ent) return [];
const recs = ent.videoIds
.map((id) => getVideo(id))
.filter((v): v is VideoRecord => !!v);
recs.sort((a, b) => {
const ta = a.meta.published_at ? Date.parse(a.meta.published_at) : 0;
const tb = b.meta.published_at ? Date.parse(b.meta.published_at) : 0;
return tb - ta;
});
return recs.slice(0, limit);
}
export function getVideosForTopic(topicId: string, limit = 12): VideoRecord[] {
const t = getTopic(topicId);
if (!t) return [];
const recs = t.videoIds
.map((id) => getVideo(id))
.filter((v): v is VideoRecord => !!v);
recs.sort((a, b) => {
const ta = a.meta.published_at ? Date.parse(a.meta.published_at) : 0;
const tb = b.meta.published_at ? Date.parse(b.meta.published_at) : 0;
return tb - ta;
});
return recs.slice(0, limit);
}
export function getVideosForEvent(eventId: string, limit = 12): VideoRecord[] {
const e = getEvent(eventId);
if (!e) return [];
const recs = e.videoIds
.map((id) => getVideo(id))
.filter((v): v is VideoRecord => !!v);
recs.sort((a, b) => {
const ta = a.meta.published_at ? Date.parse(a.meta.published_at) : 0;
const tb = b.meta.published_at ? Date.parse(b.meta.published_at) : 0;
return tb - ta;
});
return recs.slice(0, limit);
}
// ─── stance distribution ─────────────────────────────────────────────
export type StanceDistribution = {
agree: number;
disagree: number;
observe: number;
neutral: number;
total: number;
/** 0-100, higher = more divergent (closer to 50/50 split). */
divergenceScore: number;
};
export function stanceDistribution(videos: VideoRecord[]): StanceDistribution {
const dist = { agree: 0, disagree: 0, observe: 0, neutral: 0, total: 0 };
for (const v of videos) {
const s = v.analysis?.stance;
if (s === "agree") dist.agree++;
else if (s === "disagree") dist.disagree++;
else if (s === "observe") dist.observe++;
else dist.neutral++;
dist.total++;
}
// divergence score: if agree+disagree are balanced and high vs total, it's
// divergent. Normalize to 0-100. Pure consensus → 0, perfect 50/50 → 100.
const agreePct = dist.total ? dist.agree / dist.total : 0;
const disagreePct = dist.total ? dist.disagree / dist.total : 0;
const opposed = Math.min(agreePct, disagreePct);
const opposedTotal = agreePct + disagreePct;
const divergenceScore = opposedTotal === 0
? 0
: Math.round((opposed / Math.max(opposedTotal, 0.0001)) * 200);
return { ...dist, divergenceScore: Math.min(100, divergenceScore) };
}
// ─── pulses ──────────────────────────────────────────────────────────
export function getAllPulses(): Pulse[] {
if (fresh(_pulsesCache)) return _pulsesCache!.data;
const dir = path.join(MIC_DATA_PATH, "pulses");
if (!fs.existsSync(dir)) {
const loadedAt = Date.now();
_pulsesCache = { data: [], loadedAt };
_pulseDiagnosticsCache = {
data: { invalidFiles: [], duplicateIds: [] },
loadedAt,
};
return [];
}
const files = fs
.readdirSync(dir)
.filter((f) => !f.startsWith(".") && f.endsWith(".json"));
const pulses: Pulse[] = [];
const invalidFiles: string[] = [];
const duplicateIds = new Set<string>();
const seenIds = new Set<string>();
for (const f of files) {
try {
const raw = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8"));
if (
!raw ||
typeof raw.id !== "string" ||
!raw.id.trim() ||
typeof raw.asset !== "string" ||
!raw.asset.trim() ||
typeof raw.detectedAt !== "string" ||
!Number.isFinite(Date.parse(raw.detectedAt)) ||
typeof raw.direction !== "string" ||
!raw.direction.trim() ||
typeof raw.magnitudePct !== "number" ||
!Number.isFinite(raw.magnitudePct) ||
typeof raw.summary !== "string" ||
!raw.summary.trim() ||
(raw.priceFrom != null &&
(typeof raw.priceFrom !== "number" ||
!Number.isFinite(raw.priceFrom))) ||
(raw.priceTo != null &&
(typeof raw.priceTo !== "number" || !Number.isFinite(raw.priceTo))) ||
(raw.sources != null &&
(!Array.isArray(raw.sources) ||
!raw.sources.every(
(source: unknown) =>
!!source &&
typeof source === "object" &&
typeof (source as { url?: unknown }).url === "string" &&
!!(source as { url: string }).url.trim()
)))
) {
invalidFiles.push(f);
continue;
}
if (seenIds.has(raw.id)) {
duplicateIds.add(raw.id);
continue;
}
seenIds.add(raw.id);
pulses.push(raw as Pulse);
} catch {
invalidFiles.push(f);
}
}
pulses.sort(
(a, b) => Date.parse(b.detectedAt) - Date.parse(a.detectedAt)
);
const loadedAt = Date.now();
const diagnostics: PulseLoadDiagnostics = {
invalidFiles,
duplicateIds: [...duplicateIds].sort(),
};
_pulsesCache = { data: pulses, loadedAt };
_pulseDiagnosticsCache = { data: diagnostics, loadedAt };
if (invalidFiles.length || duplicateIds.size) {
// Rejected files vanish from every pulse surface (/pulse, asset pages,
// /api/pulse/active.json, briefs). Say so once per (re)load so the web
// side is not silent; the generator additionally fails closed.
console.warn(
`[mic] pulses: dropped ${formatPulseLoadDiagnostics(diagnostics)}`
);
}
return pulses;
}
export function getPulseLoadDiagnostics(): PulseLoadDiagnostics {
getAllPulses();
const diagnostics = _pulseDiagnosticsCache?.data ?? {
invalidFiles: [],
duplicateIds: [],
};
return {
invalidFiles: [...diagnostics.invalidFiles],
duplicateIds: [...diagnostics.duplicateIds],
};
}
/** Human-readable summary of pulse loader rejections, capped at `max` names
* per category so a bad batch does not flood logs. */
export function formatPulseLoadDiagnostics(
d: PulseLoadDiagnostics,
max = 20
): string {
const list = (xs: string[]) =>
xs.length
? ` [${xs.slice(0, max).join(", ")}${xs.length > max ? `, +${xs.length - max} more` : ""}]`
: "";
return (
`invalid_files=${d.invalidFiles.length}${list(d.invalidFiles)} ` +
`duplicate_ids=${d.duplicateIds.length}${list(d.duplicateIds)}`
);
}
export function getPulse(id: string): Pulse | null {
return getAllPulses().find((p) => p.id === id) || null;
}
/** Active = detected within last N hours and synthesisState is enriched/reviewed. */
export function getActivePulses(withinHours = 24): Pulse[] {
const cutoff = Date.now() - withinHours * 3600_000;
return getAllPulses().filter((p) => Date.parse(p.detectedAt) >= cutoff);
}
// ─── helpers for asset symbol mapping ────────────────────────────────
// Asset entities — slug → entity id mapping.
// Convention: /asset/btc → entity id "bitcoin"; /asset/eth → "ethereum".
const ASSET_SLUG_MAP: Record<string, string> = {
btc: "bitcoin",
bitcoin: "bitcoin",
eth: "ethereum",
ethereum: "ethereum",
sol: "solana",
solana: "solana",
moc: "mossland",
mossland: "mossland",
doge: "dogecoin",
dogecoin: "dogecoin",
xrp: "xrp",
ada: "cardano",
cardano: "cardano",
};
/**
* Known asset stubs — display these even if signalmap canonical doesn't have
* them yet. Provides stable URLs (영구 URL 약속, alpha_dev_plan §2.4).
* `videoCount: 0` triggers noindex via quality_score gating.
*/
const ASSET_STUBS: Record<string, Pick<Entity, "id" | "label" | "aliases" | "type">> = {
ethereum: {
id: "ethereum",
label: "이더리움",
aliases: ["Ethereum", "ETH"],
type: "asset",
},
mossland: {
id: "mossland",
label: "Mossland (MOC)",
aliases: ["MOC", "Moss Coin", "모스랜드", "모스코인"],
type: "asset",
},
solana: {
id: "solana",
label: "솔라나",
aliases: ["Solana", "SOL"],
type: "asset",
},
dogecoin: {
id: "dogecoin",
label: "도지코인",
aliases: ["Dogecoin", "DOGE"],
type: "asset",
},
xrp: {
id: "xrp",
label: "XRP (리플)",
aliases: ["Ripple", "리플"],
type: "asset",
},
cardano: {
id: "cardano",
label: "카르다노",
aliases: ["Cardano", "ADA"],
type: "asset",
},
};
export function entityIdFromAssetSlug(slug: string): string {
const lower = slug.toLowerCase();
return ASSET_SLUG_MAP[lower] || lower;
}
export function assetSlugFromEntity(entity: Entity): string {
const lower = entity.id.toLowerCase();
for (const [slug, id] of Object.entries(ASSET_SLUG_MAP)) {
if (id === lower && slug.length <= 4) return slug;
}
return lower;
}
/**
* Resolve asset entity, falling back to stub if signalmap canonical
* doesn't have it yet. Stubs have videoCount=0 → noindex.
*/
export function getAssetOrStub(slug: string): Entity | null {
const id = entityIdFromAssetSlug(slug);
const real = getEntity(id);
if (real && real.type === "asset") return real;
const stub = ASSET_STUBS[id];
if (stub) {
const now = new Date().toISOString();
return {
...stub,
videoCount: 0,
videoIds: [],
createdAt: now,
updatedAt: now,
};
}
return null;
}
export function getAssetEntities(): Entity[] {
return getEntitiesByType("asset");
}
/**
* Asset entities that exist only as stubs — a live page (price, pulses,
* permanent URL) that the upstream canonical store has no row for yet.
*
* `getAllEntities()` reads canonical only, so anything callers build from it
* cannot see these at all. That is why the persona candidate pool never
* contained ethereum despite a dozen live pulses on its page: not a threshold
* problem, an enumeration one. Callers still decide whether a given stub has
* enough on the page to be worth using.
*/
export function getStubAssetEntities(): Entity[] {
const canonical = new Set(getAllEntities().map((e) => e.id));
return Object.keys(ASSET_STUBS)
.filter((id) => !canonical.has(id))
.map((id) => getAssetOrStub(id))
.filter((e): e is Entity => e !== null);
}
// ─── co-mention helpers ──────────────────────────────────────────────
/** Top N entities co-mentioned with the focal entity (excludes self). */
export function getCoMentionedEntities(
focalEntityId: string,
limit = 12
): { entity: Entity; count: number }[] {
const ent = getEntity(focalEntityId);
if (!ent) return [];
const counts = new Map<string, number>();
for (const vid of ent.videoIds) {
const v = getVideo(vid);
if (!v) continue;
for (const eid of v.canonical_entity_ids || []) {
if (eid === focalEntityId) continue;
counts.set(eid, (counts.get(eid) || 0) + 1);
}
}
return [...counts.entries()]
.map(([id, count]) => ({ entity: getEntity(id), count }))
.filter((x): x is { entity: Entity; count: number } => !!x.entity)
.sort((a, b) => b.count - a.count)
.slice(0, limit);
}
/** Top N topics co-mentioned with focal entity. */
export function getCoMentionedTopics(
focalEntityId: string,
limit = 6
): { topic: Topic; count: number }[] {
const ent = getEntity(focalEntityId);
if (!ent) return [];
const counts = new Map<string, number>();
for (const vid of ent.videoIds) {
const v = getVideo(vid);
if (!v?.canonical_topic_id) continue;
counts.set(
v.canonical_topic_id,
(counts.get(v.canonical_topic_id) || 0) + 1
);
}
return [...counts.entries()]
.map(([id, count]) => ({ topic: getTopic(id), count }))
.filter((x): x is { topic: Topic; count: number } => !!x.topic)
.sort((a, b) => b.count - a.count)
.slice(0, limit);
}
/** Top N events co-mentioned with focal entity. */
export function getCoMentionedEvents(
focalEntityId: string,
limit = 6
): { event: EventItem; count: number }[] {
const ent = getEntity(focalEntityId);
if (!ent) return [];
const counts = new Map<string, number>();
for (const vid of ent.videoIds) {
const v = getVideo(vid);
if (!v) continue;
for (const eid of v.canonical_event_ids || []) {
counts.set(eid, (counts.get(eid) || 0) + 1);
}
}
return [...counts.entries()]
.map(([id, count]) => ({ event: getEvent(id), count }))
.filter((x): x is { event: EventItem; count: number } => !!x.event)
.sort((a, b) => b.count - a.count)
.slice(0, limit);
}