1- // Removed imports for matchRequest, extractParameters, detectIntent, and extractContextParameters
1+ // Reconnecting pattern matching for improved NLP accuracy
2+ import { matchRequest , extractParameters } from "../matching-service/index.js" ;
23import { MatchResult } from "../../types/tools.js" ;
34import { processWithSequentialThinking } from "../../tools/sequential-thinking.js" ;
45import { OpenRouterConfig } from "../../types/workflow.js" ;
@@ -21,9 +22,10 @@ export interface EnhancedMatchResult extends MatchResult {
2122
2223/**
2324 * Main hybrid matching function that implements the fallback flow
24- * 1. Try Semantic Matching
25- * 2. Fall back to Sequential Thinking
26- * 3. Default Fallback
25+ * 1. Try Pattern Matching (fastest, most accurate for defined patterns)
26+ * 2. Try Semantic Matching (embeddings-based similarity)
27+ * 3. Fall back to Sequential Thinking (LLM-based)
28+ * 4. Default Fallback
2729 *
2830 * @param request The user request to match
2931 * @param config OpenRouter configuration for sequential thinking
@@ -35,12 +37,62 @@ export async function hybridMatch(
3537) : Promise < EnhancedMatchResult > {
3638 let matchResult : MatchResult | null = null ;
3739 let parameters : Record < string , string > = { } ;
38- // Default to sequential, semantic will override if successful
39- let matchMethod : "semantic" | "sequential" = "sequential" ;
40- let requiresConfirmation = true ; // Default to true, semantic match might override
40+ // Default to sequential, will be overridden if pattern or semantic match succeeds
41+ let matchMethod : "rule" | "semantic" | "sequential" = "sequential" ;
42+ let requiresConfirmation = true ; // Default to true, pattern/semantic match might override
43+
44+ // Step 0: Try Keyword Prefiltering (ultra-fast path for obvious matches)
45+ const lowerRequest = request . toLowerCase ( ) ;
46+ const toolKeywords : Record < string , string [ ] > = {
47+ 'prd-generator' : [ 'prd' , 'product requirement' ] ,
48+ 'user-stories-generator' : [ 'user stor' , 'acceptance criteria' ] ,
49+ 'task-list-generator' : [ 'task list' , 'todo' , 'tasks' ] ,
50+ 'vibe-task-manager' : [ 'vibe' , 'task manager' , 'decompose' , 'atomic' ] ,
51+ 'map-codebase' : [ 'map' , 'codebase' , 'structure' , 'architecture' ] ,
52+ 'research-manager' : [ 'research' , 'investigate' , 'explore' ] ,
53+ 'curate-context' : [ 'curate' , 'context' , 'meta-prompt' ] ,
54+ 'run-workflow' : [ 'workflow' , 'run workflow' ] ,
55+ 'rules-generator' : [ 'rules' , 'standards' , 'guidelines' , 'style guide' ] ,
56+ 'fullstack-starter-kit-generator' : [ 'starter kit' , 'scaffold' , 'boilerplate' ] ,
57+ 'get-job-result' : [ 'job result' , 'job status' , 'check job' ] ,
58+ 'register-agent' : [ 'register agent' , 'add agent' ] ,
59+ 'get-agent-tasks' : [ 'agent tasks' , 'poll tasks' ] ,
60+ 'submit-task-response' : [ 'submit response' , 'task completion' ] ,
61+ 'process-request' : [ 'process request' , 'route request' ]
62+ } ;
63+
64+ for ( const [ tool , keywords ] of Object . entries ( toolKeywords ) ) {
65+ if ( keywords . some ( kw => lowerRequest . includes ( kw ) ) ) {
66+ logger . info ( `Keyword match for ${ tool } ` ) ;
67+ return {
68+ toolName : tool ,
69+ confidence : 0.85 ,
70+ matchedPattern : 'keyword_match' ,
71+ parameters : { } ,
72+ matchMethod : 'rule' as const ,
73+ requiresConfirmation : false
74+ } ;
75+ }
76+ }
77+
78+ // Step 1: Try Pattern Matching (fastest, most accurate for defined patterns)
79+ logger . debug ( 'No keyword match found. Trying pattern matching...' ) ;
80+ const patternMatch = matchRequest ( request ) ;
81+ if ( patternMatch && patternMatch . confidence >= 0.8 ) {
82+ logger . info ( `Pattern match found: ${ patternMatch . toolName } (${ patternMatch . confidence } )` ) ;
83+ const extractedParams = patternMatch . matchedPattern
84+ ? extractParameters ( request , patternMatch . matchedPattern )
85+ : { } ;
86+ return {
87+ ...patternMatch ,
88+ parameters : extractedParams ,
89+ matchMethod : 'rule' as const ,
90+ requiresConfirmation : patternMatch . confidence < 0.9
91+ } ;
92+ }
4193
42- // Step 1 : Try Semantic Matching
43- logger . debug ( 'Trying semantic matching...' ) ;
94+ // Step 2 : Try Semantic Matching (if pattern matching didn't succeed)
95+ logger . debug ( 'Pattern matching did not yield a confident result. Trying semantic matching...' ) ;
4496 const semanticMatchResult = await findBestSemanticMatch ( request ) ;
4597
4698 if ( semanticMatchResult ) {
@@ -65,13 +117,13 @@ export async function hybridMatch(
65117 logger . debug ( 'Semantic matching did not yield a confident result. Falling back to sequential thinking...' ) ;
66118 }
67119
68- // Step 2 : Fall back to sequential thinking for ambiguous requests (Only if semantic match failed)
69- // Note: matchMethod remains 'sequential' if semantic match failed
120+ // Step 3 : Fall back to sequential thinking for ambiguous requests (Only if pattern and semantic match failed)
121+ // Note: matchMethod remains 'sequential' if both pattern and semantic match failed
70122 try {
71123 // Use sequential thinking to determine the most likely tool
72124 const sequentialResult = await performSequentialThinking (
73125 request ,
74- "What tool should I use for this request? Options are: research-manager, prd-generator, user-stories-generator, task-list-generator, rules-generator, workflow- manager." ,
126+ "What tool should I use for this request? Options are: research-manager, prd-generator, user-stories-generator, task-list-generator, rules-generator, run- workflow, get-job-result, map-codebase, vibe-task- manager, curate-context, fullstack-starter-kit-generator, register-agent, get-agent-tasks, submit-task-response, process-request ." ,
75127 config
76128 ) ;
77129
@@ -81,7 +133,15 @@ export async function hybridMatch(
81133 . split ( "\n" ) [ 0 ]
82134 . replace ( / ^ .* ?: / , "" ) ;
83135
84- if ( toolName && ( toolName . includes ( "generator" ) || toolName . includes ( "manager" ) ) ) {
136+ // Check if the tool name is valid (exists in our tool list)
137+ const validTools = [
138+ "research-manager" , "prd-generator" , "user-stories-generator" , "task-list-generator" ,
139+ "rules-generator" , "run-workflow" , "get-job-result" , "map-codebase" ,
140+ "vibe-task-manager" , "curate-context" , "fullstack-starter-kit-generator" ,
141+ "register-agent" , "get-agent-tasks" , "submit-task-response" , "process-request"
142+ ] ;
143+
144+ if ( toolName && validTools . includes ( toolName ) ) {
85145 matchResult = {
86146 toolName : toolName ,
87147 confidence : 0.5 , // Medium confidence for sequential thinking
0 commit comments