|
| 1 | +/** |
| 2 | + * Response formatting utilities for LLM-optimized output |
| 3 | + * |
| 4 | + * Addresses: |
| 5 | + * - Issue #1: Truncate numeric precision |
| 6 | + * - Issue #2: Strip redundant URL prefixes |
| 7 | + * - Issue #5: Compact response format |
| 8 | + */ |
| 9 | + |
| 10 | +import type { SearchAnalyticsRow } from '../../types.js'; |
| 11 | + |
| 12 | +export interface FormatOptions { |
| 13 | + /** Response format: 'full' (default) or 'compact' (LLM-optimized) */ |
| 14 | + format?: 'full' | 'compact'; |
| 15 | + /** Site URL for stripping prefixes (issue #2) */ |
| 16 | + siteUrl?: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Round CTR to 4 decimal places (or format as percentage string in compact mode) |
| 21 | + */ |
| 22 | +export function formatCtr(ctr: number, compact = false): number | string { |
| 23 | + if (compact) { |
| 24 | + return `${(ctr * 100).toFixed(2)}%`; |
| 25 | + } |
| 26 | + return Math.round(ctr * 10000) / 10000; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Round position to 1 decimal place |
| 31 | + */ |
| 32 | +export function formatPosition(position: number): number { |
| 33 | + return Math.round(position * 10) / 10; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Strip the siteUrl prefix from a page URL |
| 38 | + */ |
| 39 | +export function stripUrlPrefix(url: string, siteUrl?: string): string { |
| 40 | + if (!siteUrl || !url) return url; |
| 41 | + |
| 42 | + // Normalize siteUrl - handle both domain property and URL prefix formats |
| 43 | + let prefix = siteUrl; |
| 44 | + |
| 45 | + // Handle sc-domain: format (e.g., "sc-domain:example.com") |
| 46 | + if (prefix.startsWith('sc-domain:')) { |
| 47 | + const domain = prefix.replace('sc-domain:', ''); |
| 48 | + // Try stripping https://domain, https://www.domain, http://domain, http://www.domain |
| 49 | + const prefixes = [ |
| 50 | + `https://${domain}`, |
| 51 | + `https://www.${domain}`, |
| 52 | + `http://${domain}`, |
| 53 | + `http://www.${domain}`, |
| 54 | + ]; |
| 55 | + for (const p of prefixes) { |
| 56 | + if (url.startsWith(p)) { |
| 57 | + return url.slice(p.length) || '/'; |
| 58 | + } |
| 59 | + } |
| 60 | + return url; |
| 61 | + } |
| 62 | + |
| 63 | + // Handle URL prefix format (e.g., "https://example.com/") |
| 64 | + // Remove trailing slash for matching |
| 65 | + prefix = prefix.replace(/\/$/, ''); |
| 66 | + |
| 67 | + if (url.startsWith(prefix)) { |
| 68 | + return url.slice(prefix.length) || '/'; |
| 69 | + } |
| 70 | + |
| 71 | + return url; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Format a single analytics row with truncated precision |
| 76 | + */ |
| 77 | +export function formatRow( |
| 78 | + row: SearchAnalyticsRow, |
| 79 | + options: FormatOptions = {} |
| 80 | +): Record<string, unknown> { |
| 81 | + const { format = 'full', siteUrl } = options; |
| 82 | + const compact = format === 'compact'; |
| 83 | + |
| 84 | + // Process keys (strip URL prefixes for page dimension) |
| 85 | + let keys = row.keys; |
| 86 | + if (keys && siteUrl) { |
| 87 | + keys = keys.map(key => { |
| 88 | + // If it looks like a URL, strip the prefix |
| 89 | + if (key.startsWith('http://') || key.startsWith('https://')) { |
| 90 | + return stripUrlPrefix(key, siteUrl); |
| 91 | + } |
| 92 | + return key; |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + if (compact) { |
| 97 | + // Compact format with short keys (issue #5) |
| 98 | + const result: Record<string, unknown> = {}; |
| 99 | + |
| 100 | + if (keys && keys.length > 0) { |
| 101 | + // Use short key names based on what the key represents |
| 102 | + // For single-dimension queries, just use the value directly |
| 103 | + if (keys.length === 1) { |
| 104 | + result.key = keys[0]; |
| 105 | + } else { |
| 106 | + result.keys = keys; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + result.clicks = row.clicks; |
| 111 | + result.imp = row.impressions; |
| 112 | + result.ctr = formatCtr(row.ctr, true); |
| 113 | + result.pos = formatPosition(row.position); |
| 114 | + |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + // Full format with truncated precision (issue #1) |
| 119 | + return { |
| 120 | + keys, |
| 121 | + clicks: row.clicks, |
| 122 | + impressions: row.impressions, |
| 123 | + ctr: formatCtr(row.ctr, false), |
| 124 | + position: formatPosition(row.position), |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Format an array of analytics rows |
| 130 | + */ |
| 131 | +export function formatRows( |
| 132 | + rows: SearchAnalyticsRow[] | undefined, |
| 133 | + options: FormatOptions = {} |
| 134 | +): Record<string, unknown>[] { |
| 135 | + if (!rows) return []; |
| 136 | + return rows.map(row => formatRow(row, options)); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Generate a natural language summary for compact format |
| 141 | + */ |
| 142 | +export function generateSummary( |
| 143 | + rows: SearchAnalyticsRow[] | undefined, |
| 144 | + dimensions?: string[] |
| 145 | +): string | undefined { |
| 146 | + if (!rows || rows.length === 0) return undefined; |
| 147 | + |
| 148 | + const topRow = rows[0]; |
| 149 | + const dimensionType = dimensions?.[0] || 'item'; |
| 150 | + |
| 151 | + let keyDescription = ''; |
| 152 | + if (topRow.keys && topRow.keys.length > 0) { |
| 153 | + keyDescription = `'${topRow.keys[0]}'`; |
| 154 | + } |
| 155 | + |
| 156 | + return `Top ${dimensionType} ${keyDescription} got ${topRow.clicks} clicks from ${topRow.impressions} impressions at position ${formatPosition(topRow.position)}`; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Default row limits (issue #4) |
| 161 | + */ |
| 162 | +export const DEFAULT_ROW_LIMIT = 25; |
| 163 | +export const DEFAULT_ROW_LIMIT_LARGE = 100; |
| 164 | + |
| 165 | +/** |
| 166 | + * Extract format options from tool arguments |
| 167 | + */ |
| 168 | +export function extractFormatOptions(args: Record<string, unknown>): FormatOptions { |
| 169 | + return { |
| 170 | + format: (args.format as 'full' | 'compact') || 'full', |
| 171 | + siteUrl: args.siteUrl as string | undefined, |
| 172 | + }; |
| 173 | +} |
0 commit comments