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 | 10x 10x 10x 7x 2x 5x 2x 3x 2x 2x 1x 2x 9x 9x 9x 9x 7x 11x 11x 10x 10x 10x 7x 11x 11x 11x 11x 1x 1x 6x 6x 6x 6x 6x 5x 4x 1x 1x 102x 107x 6x 6x 6x 6x 4x 4x 6x 1x 1x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Generators/CommitteeHelpers
* @description Pure helpers for applying MCP committee data to a CommitteeData object.
* Stateless functions — no I/O or MCP calls; only JSON parsing + data assignment.
*/
import type { CommitteeData, MCPToolResult } from '../types/index.js';
/** Featured committees to include in committee reports */
export const FEATURED_COMMITTEES = ['ENVI', 'ECON', 'AFET', 'LIBE', 'AGRI'] as const;
/** Sentinel value used when no real chair data is available from MCP */
export const PLACEHOLDER_CHAIR = 'N/A';
/** Sentinel value used when no real member count data is available from MCP */
export const PLACEHOLDER_MEMBERS = 0;
/**
* Resolve a raw memberCount/members field to a numeric count.
* Handles arrays (EP API returns members as array), numbers, and numeric strings.
*
* @param memberCountRaw - Raw value from MCP response
* @returns Numeric member count, or 0 if not resolvable
*/
function resolveMemberCount(memberCountRaw: unknown): number {
if (Array.isArray(memberCountRaw)) {
return memberCountRaw.length;
}
if (typeof memberCountRaw === 'number' && Number.isFinite(memberCountRaw)) {
return memberCountRaw;
}
if (typeof memberCountRaw === 'string') {
const parsedNumber = Number(memberCountRaw);
if (Number.isFinite(parsedNumber)) {
return parsedNumber;
}
}
return 0;
}
/** Shape of a committee info record extracted from an MCP response */
interface CommitteeInfoRecord {
name?: string | undefined;
abbreviation?: string | undefined;
chair?: string | undefined;
memberCount?: unknown | undefined;
members?: unknown | undefined;
}
/**
* Extract the committee info record from a parsed MCP response.
* Supports both the wrapped `{ committee: {...} }` format and the flat
* EP Open Data Portal format `{ name, chair, ... }`.
*
* @param parsed - Parsed JSON object from MCP response
* @returns The info record, or null when the payload is unrecognised
*/
function extractCommitteeInfoRecord(parsed: Record<string, unknown>): CommitteeInfoRecord | null {
const hasWrapped = typeof parsed.committee === 'object' && parsed.committee !== null;
const info = (hasWrapped ? parsed.committee : parsed) as CommitteeInfoRecord;
Iif (!info || typeof info !== 'object') return null;
// For flat format, require at least a name/abbreviation to confirm real data
if (!hasWrapped && !info.name && !info.abbreviation) return null;
return info;
}
/**
* Apply committee info from MCP result to the data object
*
* @param result - MCP tool result
* @param data - Committee data to populate
* @param abbreviation - Fallback abbreviation
*/
export function applyCommitteeInfo(
result: MCPToolResult,
data: CommitteeData,
abbreviation: string
): void {
try {
if (!result?.content?.[0]) return;
const parsed = JSON.parse(result.content[0].text) as Record<string, unknown>;
const info = extractCommitteeInfoRecord(parsed);
if (!info) return;
data.name = info.name ?? data.name;
data.abbreviation =
typeof info.abbreviation === 'string' && !info.abbreviation.startsWith('org/')
? info.abbreviation
: abbreviation;
data.chair = info.chair && info.chair.length > 0 ? info.chair : PLACEHOLDER_CHAIR;
data.members = resolveMemberCount(info.memberCount ?? info.members);
console.log(` ✅ Committee info: ${data.name} (${data.members} members)`);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(' ⚠️ Failed to parse committee info:', message);
}
}
/**
* Apply documents from MCP result to the data object
*
* @param result - MCP tool result
* @param data - Committee data to populate
*/
export function applyDocuments(result: MCPToolResult, data: CommitteeData): void {
try {
Iif (!result?.content?.[0]) return;
const parsed = JSON.parse(result.content[0].text) as {
documents?:
| Array<{
title?: string | undefined;
type?: string | undefined;
documentType?: string | undefined;
date?: string | undefined;
}>
| undefined;
data?:
| Array<{
title?: string | undefined;
type?: string | undefined;
documentType?: string | undefined;
date?: string | undefined;
}>
| undefined;
};
// Support both { documents: [...] } and { data: [...] } response formats
const docs = parsed.documents ?? parsed.data;
if (!docs || docs.length === 0) return;
data.documents = docs.map((d) => ({
title: d.title ?? 'Untitled Document',
type: d.type ?? d.documentType ?? 'Document',
date: d.date ?? '',
}));
console.log(` ✅ Fetched ${data.documents.length} documents from MCP`);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(' ⚠️ Failed to parse documents:', message);
}
}
/**
* Detect whether a list of committee data entries are all fallback/default placeholders.
*
* A committee entry is considered a placeholder when the MCP fetch produced no real
* data — i.e. `chair` is still the sentinel `'N/A'`, `members` is 0, and the
* `documents` list is empty. This typically happens when the MCP server is
* unavailable and `fetchCommitteeData` returns the `defaultResult` unchanged.
*
* Returns `true` only when:
* - the list is non-empty **and**
* - every committee entry matches all three placeholder criteria.
*
* An empty list returns `false` so that a genuine zero-committee scenario is still
* rendered (the caller can decide how to handle it).
*
* @param committees - Committee data entries to inspect
* @returns `true` when all entries are default/placeholder data; `false` otherwise
*/
export function isPlaceholderCommitteeData(committees: readonly CommitteeData[]): boolean {
return (
committees.length > 0 &&
committees.every(
(c) =>
c.chair === PLACEHOLDER_CHAIR &&
c.members === PLACEHOLDER_MEMBERS &&
c.documents.length === 0
)
);
}
/**
* Apply effectiveness metrics from MCP result to the data object
*
* @param result - MCP tool result
* @param data - Committee data to populate
*/
export function applyEffectiveness(result: MCPToolResult, data: CommitteeData): void {
try {
Iif (!result?.content?.[0]) return;
const parsed = JSON.parse(result.content[0].text) as {
effectiveness?: { overallScore?: unknown | undefined; rank?: string } | undefined;
};
if (!parsed.effectiveness) return;
const score = parsed.effectiveness.overallScore;
const rank = parsed.effectiveness.rank ?? '';
data.effectiveness =
typeof score === 'number' && Number.isFinite(score)
? `Score: ${score.toFixed(1)} ${rank}`.trim()
: null;
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(' ⚠️ Failed to parse effectiveness:', message);
}
}
|