All files / agentflow/ConditionAgent matchScenario.ts

100% Statements 16/16
100% Branches 8/8
100% Functions 4/4
100% Lines 12/12

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    1x 6x   5x     12x     5x 8x       5x 2x   4x         5x 1x     5x    
export type ConditionScenario = { scenario: string }
 
export const findBestScenarioIndex = (scenarios: ConditionScenario[], calledOutputName: string): number => {
    if (!Array.isArray(scenarios) || scenarios.length === 0) return -1
 
    const normalizedOutput = calledOutputName.toLowerCase().trim()
 
    // try exact match first
    let matchedScenarioIndex = scenarios.findIndex((scenario) => scenario.scenario.toLowerCase() === normalizedOutput)
 
    // fallback: check if LLM returned a partial/abbreviated scenario name
    if (matchedScenarioIndex === -1) {
        matchedScenarioIndex = scenarios.findIndex((scenario) => scenario.scenario.toLowerCase().startsWith(normalizedOutput))
    }
 
    // further fallback: substring match in either direction
    if (matchedScenarioIndex === -1) {
        matchedScenarioIndex = scenarios.findIndex(
            (scenario) =>
                scenario.scenario.toLowerCase().includes(normalizedOutput) || normalizedOutput.includes(scenario.scenario.toLowerCase())
        )
    }
 
    // last resort: if still no match, use the last scenario as an "else" branch
    if (matchedScenarioIndex === -1) {
        matchedScenarioIndex = scenarios.length - 1
    }
 
    return matchedScenarioIndex
}