Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 20x 20x 60x 60x 40x 20x 20x 4x 16x 20x 20x 20x 18x 15x 3x 2x 1x 20x 20x 20x 20x 1x 16x 2x 14x 20x 20x 20x 20x 5x 8x 8x 8x 3x 3x 2x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x | #!/usr/bin/env tsx
/**
* @module pre-article-analysis
* @description Pre-article data download and deep analysis pipeline.
*
* Orchestrates all analysis steps before article generation:
* 1. Download all relevant parliamentary documents from riksdag-regering-mcp
* 2. Political classification — Classify each document by significance, impact, domain
* 3. Risk assessment — Assess political risks (coalition stability, anomaly detection)
* 4. SWOT analysis — Generate political SWOT for relevant actors
* 5. Threat analysis — Identify threats from SWOT contributions
* 6. Stakeholder perspective analysis — Run all 6 lenses
* 7. Significance scoring — Score all documents (0–10)
* 8. Cross-reference mapping — Identify relationships between documents
* 9. Synthesis — Combined analysis summary integrating all methods
* 10. Persist — Write structured markdown to analysis/daily/YYYY-MM-DD/
*
* Usage:
* npx tsx scripts/pre-article-analysis.ts [--date YYYY-MM-DD] [--limit N]
* npx tsx scripts/pre-article-analysis.ts --aggregate weekly [--date YYYY-WNN]
*
* @author Hack23 AB
* @license Apache-2.0
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { MCPClient } from './mcp-client/client.js';
import { analyzeDocuments } from './analysis-framework/index.js';
import { calculateCoalitionRiskIndex, detectAnomalousPatterns } from './data-transformers/risk-analysis.js';
import type { RawDocument, CIAContext } from './data-transformers/types.js';
import { loadCIAContext } from './news-types/weekly-review/index.js';
import { normalizedCIAContext } from './news-types/weekly-review/data-loader.js';
import {
downloadAllDocuments,
flattenDocuments,
} from './pre-article-analysis/data-downloader.js';
import type {
SerializationContext,
SignificanceEntry,
RiskAssessmentResult,
SwotSummary,
CrossReferenceSummary,
SynthesisSummary,
} from './pre-article-analysis/markdown-serializer.js';
import {
serializeDataManifest,
serializeClassificationResults,
serializeRiskAssessment,
serializeSwotAnalysis,
serializeThreatAnalysis,
serializeStakeholderPerspectives,
serializeSignificanceScoring,
serializeCrossReferenceMap,
serializeSynthesisSummary,
} from './pre-article-analysis/markdown-serializer.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(__dirname, '..');
const ANALYSIS_DIR = path.join(REPO_ROOT, 'analysis');
function formatTimestampForMarkdown(date: Date = new Date()): string {
return date.toISOString().slice(0, 16).replace('T', ' ') + ' UTC';
}
// ---------------------------------------------------------------------------
// CLI helpers
// ---------------------------------------------------------------------------
export function parseArgs(argv: string[]): {
date: string;
aggregate: boolean;
limit: number;
weekLabel: string | null;
rm: string | null;
} {
const args = argv.slice(2);
const get = (flag: string): string | null => {
const idx = args.indexOf(flag);
if (idx === -1) {
return null;
}
const next = args[idx + 1];
if (!next || next.startsWith('--')) {
throw new Error(`Missing value for ${flag}.`);
}
return next;
};
const dateArg = get('--date');
const aggregateArg = get('--aggregate');
const aggregate = (() => {
if (aggregateArg === null) {
return false;
}
if (aggregateArg === 'weekly') {
return true;
}
throw new Error(`Invalid --aggregate value: ${aggregateArg}. Supported value: 'weekly'.`);
})();
const now = new Date();
const todayIso = now.toISOString().slice(0, 10);
// When aggregate weekly, --date supplies the week label (YYYY-WNN), not a
// calendar date. `date` is always a YYYY-MM-DD value (defaults to today).
const weekLabel = aggregate
? (dateArg || `${now.getUTCFullYear()}-W${isoWeekNumber(now).toString().padStart(2, '0')}`)
: null;
if (aggregate && weekLabel && !parseIsoWeekLabel(weekLabel)) {
throw new Error(`Invalid weekly --date value: ${weekLabel}. Expected YYYY-WNN.`);
}
if (dateArg && dateArg !== 'today' && !aggregate && !parseAndValidateIsoDate(dateArg)) {
throw new Error(`Invalid --date value: ${dateArg}. Expected YYYY-MM-DD or 'today'.`);
}
// In aggregate mode, date is always today; the week-specific field is weekLabel.
const isoDate = aggregate
? todayIso
: (dateArg === 'today' || !dateArg ? todayIso : dateArg);
const limitArg = get('--limit');
const DEFAULT_LIMIT = 20;
const parsedLimit = limitArg ? Number(limitArg) : DEFAULT_LIMIT;
if (!Number.isInteger(parsedLimit) || parsedLimit <= 0) {
throw new Error(`Invalid --limit value: ${limitArg}. Expected a positive integer.`);
}
const limit = parsedLimit;
const rm = get('--rm');
return { date: isoDate, aggregate, limit, weekLabel, rm };
}
function isoWeekNumber(date: Date): number {
const d = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
const dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d.getTime() - yearStart.getTime()) / 86400000) + 1) / 7);
}
function parseAndValidateIsoDate(dateStr: string): Date | null {
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateStr);
if (!m) return null;
const year = Number(m[1]);
const month = Number(m[2]);
const day = Number(m[3]);
const d = new Date(Date.UTC(year, month - 1, day));
Iif (Number.isNaN(d.getTime())) return null;
if (d.getUTCFullYear() !== year || d.getUTCMonth() + 1 !== month || d.getUTCDate() !== day) return null;
return d;
}
function riksMoteFromDate(dateStr: string): string {
const parsed = parseAndValidateIsoDate(dateStr) ?? new Date();
const year = parsed.getUTCFullYear();
const month = parsed.getUTCMonth() + 1;
if (month >= 10) return `${year}/${String(year + 1).slice(-2)}`;
return `${year - 1}/${String(year).slice(-2)}`;
}
function parseIsoWeekLabel(label: string): { year: number; week: number } | null {
const match = /^(\d{4})-W(\d{2})$/.exec(label);
if (!match) return null;
const year = Number(match[1]);
const week = Number(match[2]);
Iif (week < 1 || week > 53) return null;
return { year, week };
}
function isDateInIsoWeek(dateStr: string, weekLabel: string): boolean {
const parsedDate = parseAndValidateIsoDate(dateStr);
const parsedWeek = parseIsoWeekLabel(weekLabel);
if (!parsedDate || !parsedWeek) return false;
const isoThursday = new Date(parsedDate);
const dayNum = isoThursday.getUTCDay() || 7;
isoThursday.setUTCDate(isoThursday.getUTCDate() + 4 - dayNum);
const isoYear = isoThursday.getUTCFullYear();
const isoWeek = isoWeekNumber(parsedDate);
return isoYear === parsedWeek.year && isoWeek === parsedWeek.week;
}
// ---------------------------------------------------------------------------
// File utilities
// ---------------------------------------------------------------------------
function ensureDir(dir: string): void {
fs.mkdirSync(dir, { recursive: true });
}
function writeAnalysis(dir: string, filename: string, content: string): void {
const filePath = path.join(dir, filename);
fs.writeFileSync(filePath, content, 'utf8');
console.log(` ✅ Written: ${path.relative(REPO_ROOT, filePath)}`);
}
// ---------------------------------------------------------------------------
// SWOT extraction from analysis results
// ---------------------------------------------------------------------------
function extractSwotSummaries(results: ReturnType<typeof analyzeDocuments>['results']): SwotSummary[] {
const map = new Map<string, SwotSummary>();
for (const result of results) {
for (const p of result.perspectives) {
for (const swotC of p.swotContribution) {
const key = swotC.forStakeholder;
if (!map.has(key)) {
map.set(key, { forStakeholder: key, strengths: [], weaknesses: [], opportunities: [], threats: [] });
}
const entry = map.get(key)!;
switch (swotC.quadrant) {
case 'strength': entry.strengths.push(swotC.text); break;
case 'weakness': entry.weaknesses.push(swotC.text); break;
case 'opportunity': entry.opportunities.push(swotC.text); break;
case 'threat': entry.threats.push(swotC.text); break;
}
}
}
}
// Deduplicate and cap
return [...map.values()].map(s => ({
...s,
strengths: [...new Set(s.strengths)].slice(0, 5),
weaknesses: [...new Set(s.weaknesses)].slice(0, 5),
opportunities: [...new Set(s.opportunities)].slice(0, 5),
threats: [...new Set(s.threats)].slice(0, 5),
}));
}
// ---------------------------------------------------------------------------
// Significance entries
// ---------------------------------------------------------------------------
function buildSignificanceEntries(results: ReturnType<typeof analyzeDocuments>['results']): SignificanceEntry[] {
return results.map(r => ({
dok_id: r.document.dok_id || 'N/A',
title: r.document.titel || r.document.title || r.document.dok_id || 'Unknown',
score: r.overallSignificance,
doctype: r.document.doktyp || 'unknown',
}));
}
// ---------------------------------------------------------------------------
// Risk assessment
// ---------------------------------------------------------------------------
function buildRiskAssessment(docs: RawDocument[], ciaContext: CIAContext): RiskAssessmentResult {
const normalizedContext = normalizedCIAContext(ciaContext);
// Attempt to derive basic coalition signals from document data
const riskIndex = calculateCoalitionRiskIndex(normalizedContext);
const anomalies = detectAnomalousPatterns(normalizedContext);
// Derive implications from document significance
const highSignificance = docs.filter(d => {
const titleText = (d.titel || d.title || '').toLowerCase();
return (
d.doktyp === 'prop' ||
titleText.includes('budget') ||
titleText.includes('försvar') ||
titleText.includes('nato')
);
});
const implications: string[] = [
`${docs.length} documents analyzed for risk indicators`,
`${highSignificance.length} high-significance documents identified`,
riskIndex.level !== 'LOW'
? `Coalition stability at ${riskIndex.level} risk — monitor upcoming votes`
: 'Coalition stability appears stable based on available data',
];
return {
coalitionRiskScore: riskIndex.score,
riskLevel: riskIndex.level,
riskSummary: riskIndex.summary,
anomalyFlags: anomalies.map(a => ({
type: a.type,
severity: a.severity,
description: a.description,
})),
implications,
};
}
// ---------------------------------------------------------------------------
// Synthesis
// ---------------------------------------------------------------------------
function buildSynthesis(
docs: RawDocument[],
significanceEntries: SignificanceEntry[],
riskResult: RiskAssessmentResult,
): SynthesisSummary {
const totalDocs = docs.length;
const topDocs = [...significanceEntries].sort((a, b) => b.score - a.score).slice(0, 10);
const avgScore = significanceEntries.length > 0
? significanceEntries.reduce((s, e) => s + e.score, 0) / significanceEntries.length
: 0;
const overallConfidence: 'HIGH' | 'MEDIUM' | 'LOW' =
totalDocs >= 20 ? 'HIGH' : totalDocs >= 10 ? 'MEDIUM' : 'LOW';
const keyFindings: string[] = [
`Analyzed ${totalDocs} parliamentary documents with avg significance ${avgScore.toFixed(1)}/10`,
`Coalition risk level: ${riskResult.riskLevel} (score: ${riskResult.coalitionRiskScore}/100)`,
`Top document: "${topDocs[0]?.title || 'N/A'}" (significance: ${topDocs[0]?.score ?? 0}/10)`,
];
if (riskResult.anomalyFlags.length > 0) {
keyFindings.push(`${riskResult.anomalyFlags.length} anomaly flag(s) detected requiring editorial attention`);
}
const executiveSummary = [
`Pre-article analysis completed for ${totalDocs} documents.`,
`Overall political risk: ${riskResult.riskLevel}.`,
`Average document significance: ${avgScore.toFixed(1)}/10.`,
overallConfidence === 'HIGH'
? 'High data coverage — analysis results are reliable for article generation.'
: 'Partial data coverage — treat analysis as directional guidance.',
].join(' ');
return {
totalDocs,
executiveSummary,
keyFindings,
topDocuments: topDocs,
overallConfidence,
aggregateRiskLevel: riskResult.riskLevel,
};
}
// ---------------------------------------------------------------------------
// Weekly aggregation
// ---------------------------------------------------------------------------
function runWeeklyAggregation(weekLabel: string): void {
const weekDir = path.join(ANALYSIS_DIR, 'weekly', weekLabel);
ensureDir(weekDir);
const dailyRoot = path.join(ANALYSIS_DIR, 'daily');
let allSyntheses = '';
let includedDays = 0;
let aggregatedDocumentsAnalyzed = 0;
const parsedWeek = parseIsoWeekLabel(weekLabel);
if (!parsedWeek) {
throw new Error(`Invalid ISO week label: ${weekLabel}. Expected format YYYY-WNN`);
}
if (fs.existsSync(dailyRoot)) {
const dailyDirs = fs.readdirSync(dailyRoot).sort();
for (const dir of dailyDirs) {
if (!isDateInIsoWeek(dir, weekLabel)) continue;
const synthPath = path.join(dailyRoot, dir, 'synthesis-summary.md');
if (fs.existsSync(synthPath)) {
const dailySynthesis = fs.readFileSync(synthPath, 'utf8');
allSyntheses += `\n\n---\n\n## Day: ${dir}\n\n${dailySynthesis}`;
includedDays += 1;
const docsMatch = /(?:^|\n)\*\*Documents Analyzed\*\*:\s*(\d+)/.exec(dailySynthesis);
if (docsMatch?.[1]) {
aggregatedDocumentsAnalyzed += Number(docsMatch[1]);
} else {
console.warn(`[pre-analysis] Could not parse Documents Analyzed from ${synthPath}`);
}
}
}
}
const weeklyContent = buildWeeklySynthesisMarkdown({
weekLabel,
generatedAt: formatTimestampForMarkdown(),
documentsAnalyzed: aggregatedDocumentsAnalyzed,
daysIncluded: includedDays,
allSyntheses,
});
writeAnalysis(weekDir, 'weekly-synthesis.md', weeklyContent);
console.log(`\n✅ Weekly aggregation written to analysis/weekly/${weekLabel}/`);
}
export function buildWeeklySynthesisMarkdown(opts: {
weekLabel: string;
generatedAt: string;
documentsAnalyzed: number;
daysIncluded: number;
allSyntheses: string;
}): string {
const confidence = opts.documentsAnalyzed >= 20
? 'HIGH'
: (opts.documentsAnalyzed >= 8 ? 'MEDIUM' : 'LOW');
return [
`# Weekly Analysis Aggregation — ${opts.weekLabel}`,
'',
`**Generated**: ${opts.generatedAt}`,
'**Data Sources**: Aggregated from daily synthesis summaries',
`**Documents Analyzed**: ${opts.documentsAnalyzed}`,
`**Confidence**: ${confidence}`,
`**Period**: ${opts.weekLabel}`,
`**Days Included**: ${opts.daysIncluded}`,
'',
'## Summary',
'',
'Aggregation of daily analysis synthesis results for the week.',
'',
opts.allSyntheses || '_No daily analysis results found for this week._',
].join('\n');
}
// ---------------------------------------------------------------------------
// Main pipeline
// ---------------------------------------------------------------------------
async function runPreArticleAnalysis(opts: {
date: string;
limit: number;
aggregate: boolean;
weekLabel: string | null;
rm: string | null;
}): Promise<void> {
const { date, limit, aggregate, weekLabel, rm } = opts;
if (aggregate && weekLabel) {
console.log(`\n📅 Running weekly aggregation for: ${weekLabel}`);
runWeeklyAggregation(weekLabel);
return;
}
console.log(`\n🚀 Pre-Article Analysis Pipeline — ${date}`);
console.log('='.repeat(50));
// Output directory
const outputDir = path.join(ANALYSIS_DIR, 'daily', date);
ensureDir(outputDir);
const generatedAt = formatTimestampForMarkdown();
// ── Step 1: Download data ─────────────────────────────────────────────────
console.log('\n📥 Step 1: Downloading documents from riksdag-regering-mcp...');
const client = new MCPClient();
const resolvedRm = rm ?? riksMoteFromDate(date);
const { data, manifest } = await downloadAllDocuments(client, { limit, rm: resolvedRm });
const flattenedDocs = flattenDocuments(data);
const allDocs = flattenedDocs.filter((doc: RawDocument) => {
// Only keep documents whose datum matches the requested analysis date (YYYY-MM-DD).
if (doc.datum && typeof doc.datum === 'string') {
return doc.datum.slice(0, 10) === date;
}
return false;
});
const excludedDocsCount = flattenedDocs.length - allDocs.length;
console.log(` Downloaded ${flattenedDocs.length} unique documents from ${manifest.dataSources.length} MCP tools`);
console.log(
` Selected ${allDocs.length} documents for analysis for ${date} (${excludedDocsCount} with missing or non-matching dates excluded)`,
);
console.log(` Duration: ${manifest.durationMs}ms`);
console.log(` Riksmöte: ${resolvedRm}`);
const ctx: SerializationContext = {
date,
generatedAt,
dataSources: manifest.dataSources,
};
writeAnalysis(outputDir, 'data-download-manifest.md', serializeDataManifest(ctx, manifest.docCounts, allDocs.length));
if (allDocs.length === 0) {
console.warn('\n⚠️ No documents downloaded. Analysis will be minimal.');
}
// ── Step 2: Classification ────────────────────────────────────────────────
console.log('\n🏷️ Step 2: Classifying documents...');
const ciaContext = loadCIAContext();
const batchResult = analyzeDocuments(allDocs, ciaContext, 'en');
writeAnalysis(outputDir, 'classification-results.md', serializeClassificationResults(ctx, batchResult.results));
// ── Step 3: Risk assessment ───────────────────────────────────────────────
console.log('\n⚠️ Step 3: Assessing political risks...');
const riskResult = buildRiskAssessment(allDocs, ciaContext);
writeAnalysis(outputDir, 'risk-assessment.md', serializeRiskAssessment(ctx, allDocs.length, riskResult));
// ── Step 4: SWOT analysis ─────────────────────────────────────────────────
console.log('\n📊 Step 4: Generating SWOT analysis...');
const swots = extractSwotSummaries(batchResult.results);
writeAnalysis(outputDir, 'swot-analysis.md', serializeSwotAnalysis(ctx, allDocs.length, swots));
// ── Step 5: Threat analysis ───────────────────────────────────────────────
console.log('\n🔴 Step 5: Analyzing threats...');
writeAnalysis(outputDir, 'threat-analysis.md', serializeThreatAnalysis(ctx, batchResult.results));
// ── Step 6: Stakeholder perspectives ─────────────────────────────────────
console.log('\n👥 Step 6: Running stakeholder perspective analysis...');
writeAnalysis(outputDir, 'stakeholder-perspectives.md', serializeStakeholderPerspectives(ctx, batchResult.results));
// ── Step 7: Significance scoring ─────────────────────────────────────────
console.log('\n📈 Step 7: Scoring document significance...');
const significanceEntries = buildSignificanceEntries(batchResult.results);
writeAnalysis(outputDir, 'significance-scoring.md', serializeSignificanceScoring(ctx, significanceEntries));
// ── Step 8: Cross-reference mapping ──────────────────────────────────────
console.log('\n🔗 Step 8: Mapping cross-document references...');
const crossRefSummary: CrossReferenceSummary = {
docCount: allDocs.length,
totalLinks: batchResult.crossDocumentLinks.length,
links: batchResult.crossDocumentLinks,
};
writeAnalysis(outputDir, 'cross-reference-map.md', serializeCrossReferenceMap(ctx, crossRefSummary));
// ── Step 9: Synthesis ─────────────────────────────────────────────────────
console.log('\n🧩 Step 9: Synthesizing all analysis...');
const synthesis = buildSynthesis(allDocs, significanceEntries, riskResult);
writeAnalysis(outputDir, 'synthesis-summary.md', serializeSynthesisSummary(ctx, synthesis));
// ── Summary ───────────────────────────────────────────────────────────────
console.log(`\n✅ Analysis complete! Results in: analysis/daily/${date}/`);
console.log(` 📄 9 analysis files written`);
console.log(` 📊 ${allDocs.length} documents analyzed`);
console.log(` 🎯 Overall confidence: ${synthesis.overallConfidence}`);
console.log(` ⚠️ Risk level: ${synthesis.aggregateRiskLevel}`);
}
// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------
Iif (path.resolve(fileURLToPath(import.meta.url)) === path.resolve(process.argv[1] ?? '')) {
const args = parseArgs(process.argv);
runPreArticleAnalysis(args).catch((err: unknown) => {
console.error('[pre-article-analysis] Fatal error:', err instanceof Error ? err.message : String(err));
process.exit(1);
});
}
|