All files server/ai/suggestion.ts

92.59% Statements 200/216
84.46% Branches 87/103
86.84% Functions 33/38
92.23% Lines 190/206

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 552 553 554                                                                        288x 288x     288x     288x 288x 288x 12x 12x   288x 8x 8x 8x     288x       4x             4x 4x 4x 4x             4x             4x 4x 4x 4x 4x             20x             8x 8x 8x             4x             4x 4x 4x 4x 4x             4x             4x 4x 4x                                                                                                   216x                   388x   388x 52x   336x 40x   296x 40x     256x             132x   132x   52x           40x           40x                     288x 288x   288x 3492x 3492x   88x 28x 28x 28x 28x   28x 4x 4x 4x   24x 4x 4x 4x   20x 4x 4x 4x         76x 16x 16x 16x 16x 16x 16x 32x 16x     60x       200x             288x   288x               288x 1144x 104x       184x             288x 288x     288x   288x 48x       288x 288x 4x       288x 288x 4x     288x                     388x 388x 388x     388x       388x 32x 16x 12x 12x   4x 4x     372x 372x 516x 356x   372x 356x       388x     100x 100x     100x                       288x 288x     288x 288x 104x 104x       288x 288x     288x 1112x     1112x     656x     1112x 2720x 96x 96x 96x 96x           288x   108x 24x 24x 24x                           288x 168x 168x 168x     288x     288x 288x   288x 56x 56x 40x 40x 40x         288x 368x 1032x   1032x     784x     1032x 2800x   24x 12x 12x 12x 12x             288x 52x       288x 288x   256x 88x 84x       84x 84x   84x 84x 84x   4x         288x     288x 48x 48x                         240x                
/**
 * AI Query Suggestion Engine
 * Parse natural language intent and generate query structure
 */
 
import type { CubeMetadata } from '../types/metadata'
import type { SemanticQuery, TimeDimension } from '../types/query'
import { discoverCubes, findBestFieldMatch } from './discovery'
 
/**
 * Suggested query result
 */
export interface QuerySuggestion {
  query: Partial<SemanticQuery>
  confidence: number
  reasoning: string[]
  warnings?: string[]
  /** Detected analysis mode */
  analysisMode: 'query' | 'funnel' | 'flow' | 'retention'
  /** Next steps when mode != 'query' */
  nextSteps?: string[]
}
 
/**
 * Time expression patterns
 */
interface TimeExpression {
  pattern: RegExp
  getDateRange: () => [string, string]
  granularity?: string
}
 
/**
 * Get time expressions with current date context
 */
function getTimeExpressions(): TimeExpression[] {
  const now = new Date()
  const today = now.toISOString().split('T')[0]
 
  // Helper to format date
  const formatDate = (d: Date): string => d.toISOString().split('T')[0]
 
  // Helper to get start of period
  const startOfMonth = (d: Date): Date => new Date(d.getFullYear(), d.getMonth(), 1)
  const startOfYear = (d: Date): Date => new Date(d.getFullYear(), 0, 1)
  const startOfQuarter = (d: Date): Date => {
    const quarter = Math.floor(d.getMonth() / 3)
    return new Date(d.getFullYear(), quarter * 3, 1)
  }
  const startOfWeek = (d: Date): Date => {
    const day = d.getDay()
    const diff = d.getDate() - day + (day === 0 ? -6 : 1)
    return new Date(d.getFullYear(), d.getMonth(), diff)
  }
 
  return [
    // Today
    {
      pattern: /\btoday\b/i,
      getDateRange: () => [today, today],
      granularity: 'day'
    },
    // Yesterday
    {
      pattern: /\byesterday\b/i,
      getDateRange: () => {
        const yesterday = new Date(now)
        yesterday.setDate(yesterday.getDate() - 1)
        const d = formatDate(yesterday)
        return [d, d]
      },
      granularity: 'day'
    },
    // This week
    {
      pattern: /\bthis week\b/i,
      getDateRange: () => [formatDate(startOfWeek(now)), today],
      granularity: 'day'
    },
    // Last week
    {
      pattern: /\blast week\b/i,
      getDateRange: () => {
        const lastWeekStart = new Date(startOfWeek(now))
        lastWeekStart.setDate(lastWeekStart.getDate() - 7)
        const lastWeekEnd = new Date(lastWeekStart)
        lastWeekEnd.setDate(lastWeekEnd.getDate() + 6)
        return [formatDate(lastWeekStart), formatDate(lastWeekEnd)]
      },
      granularity: 'day'
    },
    // This month
    {
      pattern: /\bthis month\b/i,
      getDateRange: () => [formatDate(startOfMonth(now)), today],
      granularity: 'day'
    },
    // Last month
    {
      pattern: /\blast month\b/i,
      getDateRange: () => {
        const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1)
        const lastMonthEnd = new Date(now.getFullYear(), now.getMonth(), 0)
        return [formatDate(lastMonth), formatDate(lastMonthEnd)]
      },
      granularity: 'day'
    },
    // This quarter
    {
      pattern: /\bthis quarter\b/i,
      getDateRange: () => [formatDate(startOfQuarter(now)), today],
      granularity: 'month'
    },
    // Last quarter
    {
      pattern: /\blast quarter\b/i,
      getDateRange: () => {
        const lastQuarterStart = new Date(startOfQuarter(now))
        lastQuarterStart.setMonth(lastQuarterStart.getMonth() - 3)
        const lastQuarterEnd = new Date(startOfQuarter(now))
        lastQuarterEnd.setDate(lastQuarterEnd.getDate() - 1)
        return [formatDate(lastQuarterStart), formatDate(lastQuarterEnd)]
      },
      granularity: 'month'
    },
    // This year
    {
      pattern: /\bthis year\b/i,
      getDateRange: () => [formatDate(startOfYear(now)), today],
      granularity: 'month'
    },
    // Last year
    {
      pattern: /\blast year\b/i,
      getDateRange: () => {
        const lastYear = new Date(now.getFullYear() - 1, 0, 1)
        const lastYearEnd = new Date(now.getFullYear() - 1, 11, 31)
        return [formatDate(lastYear), formatDate(lastYearEnd)]
      },
      granularity: 'month'
    },
    // Last N days
    {
      pattern: /\blast (\d+) days?\b/i,
      getDateRange: () => {
        // Default to 7 days, actual value extracted separately
        const start = new Date(now)
        start.setDate(start.getDate() - 7)
        return [formatDate(start), today]
      },
      granularity: 'day'
    },
    // Last N weeks
    {
      pattern: /\blast (\d+) weeks?\b/i,
      getDateRange: () => {
        const start = new Date(now)
        start.setDate(start.getDate() - 7 * 4)
        return [formatDate(start), today]
      },
      granularity: 'week'
    },
    // Last N months
    {
      pattern: /\blast (\d+) months?\b/i,
      getDateRange: () => {
        const start = new Date(now)
        start.setMonth(start.getMonth() - 3)
        return [formatDate(start), today]
      },
      granularity: 'month'
    },
    // Q1, Q2, Q3, Q4 patterns
    {
      pattern: /\bq([1-4])\b/i,
      getDateRange: () => {
        // Default Q1 of current year
        return [formatDate(new Date(now.getFullYear(), 0, 1)), formatDate(new Date(now.getFullYear(), 2, 31))]
      },
      granularity: 'month'
    }
  ]
}
 
/**
 * Analysis mode detection patterns
 */
const ANALYSIS_MODE_PATTERNS = {
  funnel: /\b(funnel|conversion|drop.?off|steps?|journey|pipeline|stages?)\b/i,
  flow: /\b(flows?|paths?|sequence|before|after|next|previous|user.?journey)\b/i,
  retention: /\b(retention|cohort|return|churn|comeback|retained|day.?\d+)\b/i
}
 
/**
 * Detect analysis mode from natural language
 */
function detectAnalysisMode(text: string): 'query' | 'funnel' | 'flow' | 'retention' {
  const lowerText = text.toLowerCase()
 
  if (ANALYSIS_MODE_PATTERNS.funnel.test(lowerText)) {
    return 'funnel'
  }
  if (ANALYSIS_MODE_PATTERNS.flow.test(lowerText)) {
    return 'flow'
  }
  if (ANALYSIS_MODE_PATTERNS.retention.test(lowerText)) {
    return 'retention'
  }
 
  return 'query'
}
 
/**
 * Generate next steps for analysis modes
 */
function generateNextSteps(mode: 'funnel' | 'flow' | 'retention', cubeName?: string): string[] {
  const cubeRef = cubeName ? cubeName : 'the relevant cube'
 
  switch (mode) {
    case 'funnel':
      return [
        `Use /mcp/discover to get ${cubeRef} funnel configuration and schema`,
        `Query the event dimension to discover available event types for funnel steps`,
        'Build funnel query with discovered values using the schema from discover'
      ]
    case 'flow':
      return [
        `Use /mcp/discover to get ${cubeRef} flow configuration and schema`,
        `Query the event dimension to discover available event types`,
        'Build flow query specifying the starting event and steps before/after'
      ]
    case 'retention':
      return [
        `Use /mcp/discover to get ${cubeRef} retention configuration and schema`,
        'Build retention query specifying granularity (day/week/month) and number of periods'
      ]
  }
}
 
/**
 * Parse time expression from natural language
 */
function parseTimeExpression(text: string): { dateRange: [string, string]; granularity?: string } | null {
  const expressions = getTimeExpressions()
  const lowerText = text.toLowerCase()
 
  for (const expr of expressions) {
    const match = lowerText.match(expr.pattern)
    if (match) {
      // Handle patterns with numeric capture groups
      if (match[1] && /^\d+$/.test(match[1])) {
        const n = parseInt(match[1], 10)
        const now = new Date()
        const today = now.toISOString().split('T')[0]
        const formatDate = (d: Date): string => d.toISOString().split('T')[0]
 
        if (/days?/.test(lowerText)) {
          const start = new Date(now)
          start.setDate(start.getDate() - n)
          return { dateRange: [formatDate(start), today], granularity: 'day' }
        }
        if (/weeks?/.test(lowerText)) {
          const start = new Date(now)
          start.setDate(start.getDate() - n * 7)
          return { dateRange: [formatDate(start), today], granularity: n <= 4 ? 'day' : 'week' }
        }
        if (/months?/.test(lowerText)) {
          const start = new Date(now)
          start.setMonth(start.getMonth() - n)
          return { dateRange: [formatDate(start), today], granularity: n <= 3 ? 'day' : 'month' }
        }
      }
 
      // Handle Q1-Q4
      if (/^q[1-4]$/i.test(match[0])) {
        const quarter = parseInt(match[1], 10)
        const now = new Date()
        const year = now.getFullYear()
        const startMonth = (quarter - 1) * 3
        const start = new Date(year, startMonth, 1)
        const end = new Date(year, startMonth + 3, 0)
        const formatDate = (d: Date): string => d.toISOString().split('T')[0]
        return { dateRange: [formatDate(start), formatDate(end)], granularity: 'month' }
      }
 
      return { dateRange: expr.getDateRange(), granularity: expr.granularity }
    }
  }
 
  return null
}
 
/**
 * Detect aggregation intent from natural language
 */
function detectAggregationIntent(text: string): { type: 'sum' | 'count' | 'avg' | 'max' | 'min'; confidence: number } | null {
  const lowerText = text.toLowerCase()
 
  const patterns: Array<{ pattern: RegExp; type: 'sum' | 'count' | 'avg' | 'max' | 'min' }> = [
    { pattern: /\b(total|sum|combined)\b/i, type: 'sum' },
    { pattern: /\b(count|number of|how many)\b/i, type: 'count' },
    { pattern: /\b(average|avg|mean)\b/i, type: 'avg' },
    { pattern: /\b(maximum|max|highest|top)\b/i, type: 'max' },
    { pattern: /\b(minimum|min|lowest|bottom)\b/i, type: 'min' }
  ]
 
  for (const { pattern, type } of patterns) {
    if (pattern.test(lowerText)) {
      return { type, confidence: 0.8 }
    }
  }
 
  return null
}
 
/**
 * Detect grouping/breakdown intent
 */
function detectGroupingIntent(text: string): string[] {
  const lowerText = text.toLowerCase()
  const groupingKeywords: string[] = []
 
  // Look for "by X" patterns
  const byPattern = /\bby\s+(\w+(?:\s+\w+)?)/gi
  let match
  while ((match = byPattern.exec(lowerText)) !== null) {
    groupingKeywords.push(match[1].trim())
  }
 
  // Look for "per X" patterns
  const perPattern = /\bper\s+(\w+)/gi
  while ((match = perPattern.exec(lowerText)) !== null) {
    groupingKeywords.push(match[1].trim())
  }
 
  // Look for "for each X" patterns
  const forEachPattern = /\bfor each\s+(\w+)/gi
  while ((match = forEachPattern.exec(lowerText)) !== null) {
    groupingKeywords.push(match[1].trim())
  }
 
  return groupingKeywords
}
 
/**
 * Suggest a query based on natural language input
 */
export function suggestQuery(
  metadata: CubeMetadata[],
  naturalLanguage: string,
  targetCube?: string
): QuerySuggestion {
  const reasoning: string[] = []
  const warnings: string[] = []
  const query: Partial<SemanticQuery> = {}
 
  // Detect analysis mode
  const analysisMode = detectAnalysisMode(naturalLanguage)
 
  // Step 1: Discover relevant cubes if not specified
  let relevantCubes: CubeMetadata[]
  if (targetCube) {
    const cube = metadata.find(c => c.name === targetCube)
    if (cube) {
      relevantCubes = [cube]
      reasoning.push(`Using specified cube: ${targetCube}`)
    } else {
      warnings.push(`Specified cube '${targetCube}' not found`)
      relevantCubes = []
    }
  } else {
    const discoveryResults = discoverCubes(metadata, { intent: naturalLanguage, limit: 3 })
    relevantCubes = discoveryResults
      .map(r => metadata.find(c => c.name === r.cube))
      .filter((c): c is CubeMetadata => c !== undefined)
 
    if (relevantCubes.length > 0) {
      reasoning.push(`Identified relevant cubes: ${relevantCubes.map(c => c.name).join(', ')}`)
    }
  }
 
  if (relevantCubes.length === 0) {
    // For analysis modes, still provide nextSteps guidance even without cubes
    // Return confidence 0.7 for analysis modes (mode was detected), 0 for query mode
    const isAnalysisMode = analysisMode !== 'query'
    const nextSteps = isAnalysisMode
      ? generateNextSteps(analysisMode, undefined)
      : undefined
    return {
      query: {},
      confidence: isAnalysisMode ? 0.7 : 0,
      reasoning: isAnalysisMode
        ? [`Detected ${analysisMode} intent from natural language`]
        : ['Could not identify relevant cubes for this query'],
      warnings,
      analysisMode,
      nextSteps
    }
  }
 
  const primaryCube = relevantCubes[0]
  let confidence = 0.5 // Base confidence
 
  // Step 2: Detect aggregation intent
  const aggregationIntent = detectAggregationIntent(naturalLanguage)
  if (aggregationIntent) {
    reasoning.push(`Detected ${aggregationIntent.type} aggregation intent`)
    confidence += 0.1
  }
 
  // Step 3: Find matching measures
  const measures: string[] = []
  const lowerText = naturalLanguage.toLowerCase()
 
  // Look for measure keywords in the text
  for (const measure of primaryCube.measures) {
    const measureName = measure.name.split('.').pop() || measure.name
 
    // Check name, title, and synonyms
    const namesToCheck = [
      measureName.toLowerCase(),
      measure.title.toLowerCase(),
      ...(measure.synonyms || []).map(s => s.toLowerCase())
    ]
 
    for (const name of namesToCheck) {
      if (lowerText.includes(name)) {
        measures.push(measure.name)
        reasoning.push(`Matched measure '${measure.name}' via keyword '${name}'`)
        confidence += 0.15
        break
      }
    }
  }
 
  // If no specific measures found, suggest based on aggregation intent
  if (measures.length === 0 && aggregationIntent) {
    // Find measures matching the aggregation type
    const matchingMeasures = primaryCube.measures.filter(m => m.type === aggregationIntent.type)
    if (matchingMeasures.length > 0) {
      measures.push(matchingMeasures[0].name)
      reasoning.push(`Suggested ${matchingMeasures[0].name} based on ${aggregationIntent.type} intent`)
    E} else if (aggregationIntent.type === 'count') {
      // For count, find any count or countDistinct measure
      const countMeasure = primaryCube.measures.find(m =>
        m.type === 'count' || m.type === 'countDistinct'
      )
      if (countMeasure) {
        measures.push(countMeasure.name)
        reasoning.push(`Suggested ${countMeasure.name} for counting`)
      }
    }
  }
 
  // If still no measures, use first available
  if (measures.length === 0 && primaryCube.measures.length > 0) {
    measures.push(primaryCube.measures[0].name)
    reasoning.push(`Using default measure: ${primaryCube.measures[0].name}`)
    warnings.push('Could not determine specific measure from query, using default')
  }
 
  query.measures = measures
 
  // Step 4: Detect and resolve grouping dimensions
  const groupingKeywords = detectGroupingIntent(naturalLanguage)
  const dimensions: string[] = []
 
  for (const keyword of groupingKeywords) {
    const match = findBestFieldMatch(relevantCubes, keyword, 'dimension')
    if (match) {
      dimensions.push(match.field)
      reasoning.push(`Matched dimension '${match.field}' from grouping keyword '${keyword}'`)
      confidence += 0.1
    }
  }
 
  // Also check for dimension keywords in the text
  for (const cube of relevantCubes) {
    for (const dimension of cube.dimensions) {
      const dimName = dimension.name.split('.').pop() || dimension.name
 
      const namesToCheck = [
        dimName.toLowerCase(),
        dimension.title.toLowerCase(),
        ...(dimension.synonyms || []).map(s => s.toLowerCase())
      ]
 
      for (const name of namesToCheck) {
        if (lowerText.includes(name) && !dimensions.includes(dimension.name)) {
          // Check if this is likely a grouping dimension
          if (lowerText.includes(`by ${name}`) || lowerText.includes(`per ${name}`)) {
            dimensions.push(dimension.name)
            reasoning.push(`Matched dimension '${dimension.name}' as grouping`)
            confidence += 0.1
            break
          }
        }
      }
    }
  }
 
  if (dimensions.length > 0) {
    query.dimensions = dimensions
  }
 
  // Step 5: Parse time expressions
  const timeExpr = parseTimeExpression(naturalLanguage)
  if (timeExpr) {
    // Find a time dimension in the primary cube
    const timeDimension = primaryCube.dimensions.find(d => d.type === 'time')
    if (timeDimension) {
      const td: TimeDimension = {
        dimension: timeDimension.name,
        dateRange: timeExpr.dateRange
      }
      Eif (timeExpr.granularity) {
        td.granularity = timeExpr.granularity as any
      }
      query.timeDimensions = [td]
      reasoning.push(`Applied time filter: ${timeExpr.dateRange[0]} to ${timeExpr.dateRange[1]}`)
      confidence += 0.15
    } else {
      warnings.push('Time expression found but no time dimension in cube')
    }
  }
 
  // Normalize confidence to 0-1
  confidence = Math.min(1, confidence)
 
  // For analysis modes, return guidance instead of building query
  if (analysisMode !== 'query') {
    const primaryCubeName = relevantCubes.length > 0 ? relevantCubes[0].name : undefined
    return {
      query: {},
      confidence: 0.7,
      reasoning: [
        `Detected ${analysisMode} intent from natural language`,
        ...(primaryCubeName ? [`Found relevant cube: ${primaryCubeName}`] : [])
      ],
      warnings: warnings.length > 0 ? warnings : undefined,
      analysisMode,
      nextSteps: generateNextSteps(analysisMode, primaryCubeName)
    }
  }
 
  return {
    query,
    confidence,
    reasoning,
    warnings: warnings.length > 0 ? warnings : undefined,
    analysisMode: 'query'
  }
}