#[derive(Debug)]
pub enum QueryIntent {
FindCallers { symbol: String },
FindSymbol { name: String, kind: Option<String> },
FindFile { hint: String },
FindChanges,
Understand { concept: String },
UnderstandSymbol { symbol: String },
UnderstandImplementations { name: String },
SearchCode { pattern: String },
FindDependents { target: String },
FindImplementations { name: String },
Explore { query: String },
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RouteConfidence {
Exact,
Inferred,
Fallback,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RouteAssessment {
pub confidence: RouteConfidence,
pub rationale: &'static str,
pub suggested_next_step: Option<&'static str>,
}
pub fn classify_intent(query: &str) -> QueryIntent {
let q = query.trim();
let lower = q.to_ascii_lowercase();
if let Some(sym) = strip_prefix_phrase(
&lower,
&[
"who calls ",
"what calls ",
"callers of ",
"callers for ",
"references to ",
"references for ",
"find references ",
"usages of ",
"who uses ",
],
) {
return QueryIntent::FindCallers {
symbol: clean_symbol_name(sym, q),
};
}
if let Some(target) = strip_prefix_phrase(
&lower,
&[
"what depends on ",
"depends on ",
"dependents of ",
"dependents for ",
"who imports ",
"what imports ",
],
) {
return QueryIntent::FindDependents {
target: clean_symbol_name(target, q),
};
}
if let Some(name) = strip_prefix_phrase(
&lower,
&[
"implementations of ",
"implementors of ",
"who implements ",
"what implements ",
"implementations for ",
],
) {
return QueryIntent::FindImplementations {
name: clean_symbol_name(name, q),
};
}
if let Some(name) = strip_prefix_phrase(
&lower,
&[
"where is ",
"find symbol ",
"definition of ",
"show me ",
"go to ",
"jump to ",
"locate ",
"find definition ",
"find function ",
"find struct ",
"find class ",
"find type ",
"find method ",
"find enum ",
"find trait ",
"find interface ",
],
) {
let name = name
.trim_end_matches(" defined")
.trim_end_matches(" declaration");
let (kind, clean_name) = extract_kind_hint(name);
return QueryIntent::FindSymbol {
name: clean_symbol_name(clean_name, q),
kind,
};
}
if let Some(hint) = strip_prefix_phrase(
&lower,
&[
"find file ",
"path to ",
"where is file ",
"locate file ",
"which file ",
],
) {
return QueryIntent::FindFile {
hint: hint.to_string(),
};
}
if lower.starts_with("what changed")
|| lower.starts_with("recent changes")
|| lower.starts_with("uncommitted")
|| lower == "changes"
|| lower.starts_with("what's changed")
|| lower.starts_with("show changes")
|| lower.starts_with("git status")
|| lower.starts_with("what did i change")
{
return QueryIntent::FindChanges;
}
if let Some(concept) = strip_prefix_phrase(
&lower,
&[
"how does ",
"how do ",
"explain ",
"understand ",
"what is ",
"what are ",
"describe ",
"tell me about ",
"help me understand ",
"walk me through ",
],
) {
let concept = concept
.trim_end_matches(" work")
.trim_end_matches(" works")
.trim_end_matches("?");
return QueryIntent::Understand {
concept: concept.to_string(),
};
}
if let Some(pattern) = strip_prefix_phrase(
&lower,
&[
"search for ",
"search ",
"grep ",
"find in code ",
"look for ",
"find text ",
"find string ",
],
) {
return QueryIntent::SearchCode {
pattern: pattern.trim_matches('"').trim_matches('\'').to_string(),
};
}
if looks_like_path(q) {
return QueryIntent::FindFile {
hint: q.to_string(),
};
}
if looks_like_symbol(q) {
return QueryIntent::FindSymbol {
name: q.to_string(),
kind: None,
};
}
if looks_like_code_pattern(q) {
return QueryIntent::SearchCode {
pattern: q.to_string(),
};
}
QueryIntent::Explore {
query: q.to_string(),
}
}
pub fn assess_route(query: &str, intent: &QueryIntent) -> RouteAssessment {
let lower = query.trim().to_ascii_lowercase();
match intent {
QueryIntent::FindCallers { .. } => {
if strip_prefix_phrase(
&lower,
&[
"who calls ",
"what calls ",
"callers of ",
"callers for ",
"references to ",
"references for ",
"find references ",
"usages of ",
"who uses ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit caller/reference phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred a symbol-centric caller query from the input shape",
suggested_next_step: Some(
"If this route looks wrong, ask with explicit phrasing like `who calls X` or `references to X`.",
),
}
}
}
QueryIntent::FindDependents { .. } => {
if strip_prefix_phrase(
&lower,
&[
"what depends on ",
"depends on ",
"dependents of ",
"dependents for ",
"who imports ",
"what imports ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit dependent/import phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred a dependency-path question from the input shape",
suggested_next_step: Some(
"If this route looks wrong, ask with explicit phrasing like `what depends on X` or call `find_dependents` directly.",
),
}
}
}
QueryIntent::FindImplementations { .. } => {
if strip_prefix_phrase(
&lower,
&[
"implementations of ",
"implementors of ",
"who implements ",
"what implements ",
"implementations for ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit implementation phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred an implementation query from the symbol-like input",
suggested_next_step: Some(
"If this route looks wrong, ask with explicit phrasing like `implementations of X`.",
),
}
}
}
QueryIntent::FindSymbol { .. } => {
if strip_prefix_phrase(
&lower,
&[
"where is ",
"find symbol ",
"definition of ",
"show me ",
"go to ",
"jump to ",
"locate ",
"find definition ",
"find function ",
"find struct ",
"find class ",
"find type ",
"find method ",
"find enum ",
"find trait ",
"find interface ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit definition/lookup phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred a symbol lookup from a symbol-like query",
suggested_next_step: Some(
"If this route looks wrong, call `search_files` for paths or `search_text` for literal text instead.",
),
}
}
}
QueryIntent::FindFile { .. } => {
if strip_prefix_phrase(
&lower,
&[
"find file ",
"path to ",
"where is file ",
"locate file ",
"which file ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit file/path phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred a file lookup from path-like input",
suggested_next_step: Some(
"If this route looks wrong, call `search_text` for literal content or `search_symbols` for code names.",
),
}
}
}
QueryIntent::FindChanges => RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit change/status phrasing",
suggested_next_step: None,
},
QueryIntent::Understand { .. } => {
if strip_prefix_phrase(
&lower,
&[
"how does ",
"how do ",
"explain ",
"understand ",
"what is ",
"what are ",
"describe ",
"tell me about ",
"help me understand ",
"walk me through ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit explanation/understanding phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred a conceptual exploration from the query wording",
suggested_next_step: Some(
"If this route looks too broad, ask for a specific symbol, file, or caller relationship.",
),
}
}
}
QueryIntent::UnderstandSymbol { .. } => RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "detected an exact indexed symbol inside a broad explanation query",
suggested_next_step: Some(
"If this route is too narrow, ask about the wider concept explicitly or call `explore` directly.",
),
},
QueryIntent::UnderstandImplementations { .. } => RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "detected a distinctive trait-like symbol inside a broad explanation query about implementations or types",
suggested_next_step: Some(
"If this route is too narrow, ask about the wider concept explicitly or call `explore` directly.",
),
},
QueryIntent::SearchCode { .. } => {
if strip_prefix_phrase(
&lower,
&[
"search for ",
"search ",
"grep ",
"find in code ",
"look for ",
"find text ",
"find string ",
],
)
.is_some()
{
RouteAssessment {
confidence: RouteConfidence::Exact,
rationale: "matched explicit code-search phrasing",
suggested_next_step: None,
}
} else {
RouteAssessment {
confidence: RouteConfidence::Inferred,
rationale: "inferred literal or pattern search from code-like syntax",
suggested_next_step: Some(
"If this route looks wrong, call `search_symbols` for names or rephrase with `search for ...`.",
),
}
}
}
QueryIntent::Explore { .. } => RouteAssessment {
confidence: RouteConfidence::Fallback,
rationale: "no stronger route matched, so SymForge fell back to conceptual exploration",
suggested_next_step: Some(
"If this is too broad, rephrase with a direct intent like `who calls`, `find symbol`, `find file`, or `search for`.",
),
},
}
}
pub fn route_confidence_label(confidence: RouteConfidence) -> &'static str {
match confidence {
RouteConfidence::Exact => "exact",
RouteConfidence::Inferred => "inferred",
RouteConfidence::Fallback => "fallback",
}
}
pub fn route_invocation(intent: &QueryIntent) -> String {
match intent {
QueryIntent::FindCallers { symbol } => {
format!("find_references(name=\"{symbol}\")")
}
QueryIntent::FindSymbol { name, kind } => {
if let Some(k) = kind {
format!("search_symbols(query=\"{name}\", kind=\"{k}\")")
} else {
format!("search_symbols(query=\"{name}\")")
}
}
QueryIntent::FindFile { hint } => {
format!("search_files(query=\"{hint}\")")
}
QueryIntent::FindChanges => "what_changed(uncommitted=true)".to_string(),
QueryIntent::Understand { concept } => {
format!("explore(query=\"{concept}\", depth=2)")
}
QueryIntent::UnderstandSymbol { symbol } => {
format!("get_symbol_context(name=\"{symbol}\")")
}
QueryIntent::UnderstandImplementations { name } => {
format!("find_references(name=\"{name}\", mode=\"implementations\")")
}
QueryIntent::SearchCode { pattern } => {
format!("search_text(query=\"{pattern}\")")
}
QueryIntent::FindDependents { target } => {
format!("find_dependents(path=\"{target}\")")
}
QueryIntent::FindImplementations { name } => {
format!("find_references(name=\"{name}\", mode=\"implementations\")")
}
QueryIntent::Explore { query } => {
format!("explore(query=\"{query}\")")
}
}
}
pub fn route_tool_name(intent: &QueryIntent) -> &'static str {
match intent {
QueryIntent::FindCallers { .. } => "find_references",
QueryIntent::FindSymbol { .. } => "search_symbols",
QueryIntent::FindFile { .. } => "search_files",
QueryIntent::FindChanges => "what_changed",
QueryIntent::Understand { .. } => "explore",
QueryIntent::UnderstandSymbol { .. } => "get_symbol_context",
QueryIntent::UnderstandImplementations { .. } => "find_references",
QueryIntent::SearchCode { .. } => "search_text",
QueryIntent::FindDependents { .. } => "find_dependents",
QueryIntent::FindImplementations { .. } => "find_references",
QueryIntent::Explore { .. } => "explore",
}
}
fn strip_prefix_phrase<'a>(lower: &'a str, prefixes: &[&str]) -> Option<&'a str> {
for prefix in prefixes {
if lower.starts_with(prefix) {
let rest = &lower[prefix.len()..];
let rest = rest.trim();
if !rest.is_empty() {
return Some(rest);
}
}
}
None
}
fn extract_kind_hint(name: &str) -> (Option<String>, &str) {
let kind_prefixes = [
("function ", "fn"),
("fn ", "fn"),
("struct ", "struct"),
("class ", "class"),
("type ", "type"),
("method ", "method"),
("enum ", "enum"),
("trait ", "trait"),
("interface ", "interface"),
("constant ", "constant"),
("const ", "constant"),
("variable ", "variable"),
("var ", "variable"),
];
for (prefix, kind) in &kind_prefixes {
if name.starts_with(prefix) {
return (Some(kind.to_string()), &name[prefix.len()..]);
}
}
(None, name)
}
fn clean_symbol_name(lower_match: &str, original: &str) -> String {
let lower_original = original.to_ascii_lowercase();
if let Some(pos) = lower_original.find(lower_match) {
return original[pos..pos + lower_match.len()].trim().to_string();
}
lower_match.trim().to_string()
}
fn looks_like_path(q: &str) -> bool {
(q.contains('/') || q.contains('\\'))
|| q.ends_with(".rs")
|| q.ends_with(".py")
|| q.ends_with(".ts")
|| q.ends_with(".js")
|| q.ends_with(".go")
|| q.ends_with(".java")
|| q.ends_with(".toml")
|| q.ends_with(".yaml")
|| q.ends_with(".yml")
|| q.ends_with(".json")
|| q.ends_with(".md")
}
fn looks_like_symbol(q: &str) -> bool {
if q.contains(' ') || q.is_empty() {
return false;
}
let has_camel = q.chars().skip(1).any(|c| c.is_uppercase());
let has_snake = q.contains('_');
let has_colons = q.contains("::");
let all_alnum = q
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == ':');
all_alnum && (has_camel || has_snake || has_colons)
}
fn looks_like_code_pattern(q: &str) -> bool {
q.contains("==")
|| q.contains("!=")
|| q.contains("->")
|| q.contains("=>")
|| q.contains("fn ")
|| q.contains("pub ")
|| q.contains("let ")
|| q.contains("def ")
|| q.contains("class ")
|| q.contains("impl ")
|| q.contains("struct ")
|| (q.contains('(') && q.contains(')'))
|| (q.contains('{') && q.contains('}'))
}
pub fn route_description(intent: &QueryIntent) -> String {
format!("[Routed to: {}]", route_invocation(intent))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_classify_callers() {
match classify_intent("who calls optimize_deterministic") {
QueryIntent::FindCallers { symbol } => assert_eq!(symbol, "optimize_deterministic"),
other => panic!("Expected FindCallers, got {:?}", other),
}
}
#[test]
fn test_classify_callers_references() {
match classify_intent("references to LiveIndex") {
QueryIntent::FindCallers { symbol } => assert_eq!(symbol, "LiveIndex"),
other => panic!("Expected FindCallers, got {:?}", other),
}
}
#[test]
fn test_classify_find_symbol() {
match classify_intent("where is optimize_deterministic defined") {
QueryIntent::FindSymbol { name, kind } => {
assert_eq!(name, "optimize_deterministic");
assert!(kind.is_none());
}
other => panic!("Expected FindSymbol, got {:?}", other),
}
}
#[test]
fn test_classify_find_symbol_with_kind() {
match classify_intent("find struct LiveIndex") {
QueryIntent::FindSymbol { name, kind } => {
assert_eq!(name, "LiveIndex");
assert_eq!(kind, None);
}
other => panic!("Expected FindSymbol, got {:?}", other),
}
}
#[test]
fn test_classify_find_file() {
match classify_intent("find file tools.rs") {
QueryIntent::FindFile { hint } => assert_eq!(hint, "tools.rs"),
other => panic!("Expected FindFile, got {:?}", other),
}
}
#[test]
fn test_classify_path_heuristic() {
match classify_intent("src/protocol/mod.rs") {
QueryIntent::FindFile { hint } => assert_eq!(hint, "src/protocol/mod.rs"),
other => panic!("Expected FindFile, got {:?}", other),
}
}
#[test]
fn test_classify_symbol_heuristic() {
match classify_intent("LiveIndex") {
QueryIntent::FindSymbol { name, .. } => assert_eq!(name, "LiveIndex"),
other => panic!("Expected FindSymbol, got {:?}", other),
}
}
#[test]
fn test_classify_snake_case_heuristic() {
match classify_intent("search_symbols_with_options") {
QueryIntent::FindSymbol { name, .. } => assert_eq!(name, "search_symbols_with_options"),
other => panic!("Expected FindSymbol, got {:?}", other),
}
}
#[test]
fn test_classify_changes() {
match classify_intent("what changed") {
QueryIntent::FindChanges => {}
other => panic!("Expected FindChanges, got {:?}", other),
}
}
#[test]
fn test_classify_understand() {
match classify_intent("how does the synergy pipeline work") {
QueryIntent::Understand { concept } => assert_eq!(concept, "the synergy pipeline"),
other => panic!("Expected Understand, got {:?}", other),
}
}
#[test]
fn test_classify_search_code() {
match classify_intent("search for TODO") {
QueryIntent::SearchCode { pattern } => assert_eq!(pattern, "todo"),
other => panic!("Expected SearchCode, got {:?}", other),
}
}
#[test]
fn test_classify_dependents() {
match classify_intent("what depends on src/protocol/mod.rs") {
QueryIntent::FindDependents { target } => assert_eq!(target, "src/protocol/mod.rs"),
other => panic!("Expected FindDependents, got {:?}", other),
}
}
#[test]
fn test_classify_implementations() {
match classify_intent("implementations of LlmClient") {
QueryIntent::FindImplementations { name } => assert_eq!(name, "LlmClient"),
other => panic!("Expected FindImplementations, got {:?}", other),
}
}
#[test]
fn test_classify_explore_fallback() {
match classify_intent("error handling patterns") {
QueryIntent::Explore { query } => assert_eq!(query, "error handling patterns"),
other => panic!("Expected Explore, got {:?}", other),
}
}
#[test]
fn test_route_description() {
let intent = classify_intent("who calls optimize_deterministic");
let desc = route_description(&intent);
assert!(desc.contains("find_references"));
assert!(desc.contains("optimize_deterministic"));
}
#[test]
fn test_assess_route_exact() {
let intent = classify_intent("who calls optimize_deterministic");
let assessment = assess_route("who calls optimize_deterministic", &intent);
assert_eq!(assessment.confidence, RouteConfidence::Exact);
assert_eq!(assessment.suggested_next_step, None);
}
#[test]
fn test_assess_route_inferred() {
let intent = classify_intent("LiveIndex");
let assessment = assess_route("LiveIndex", &intent);
assert_eq!(assessment.confidence, RouteConfidence::Inferred);
assert!(assessment.suggested_next_step.is_some());
}
#[test]
fn test_assess_route_fallback() {
let intent = classify_intent("error handling patterns");
let assessment = assess_route("error handling patterns", &intent);
assert_eq!(assessment.confidence, RouteConfidence::Fallback);
assert!(assessment.suggested_next_step.is_some());
}
#[test]
fn test_route_invocation() {
let intent = classify_intent("src/protocol/mod.rs");
let invocation = route_invocation(&intent);
assert!(invocation.contains("search_files"));
assert!(invocation.contains("src/protocol/mod.rs"));
}
#[test]
fn test_assess_route_understand_implementations() {
let intent = QueryIntent::UnderstandImplementations {
name: "Actor".to_string(),
};
let assessment = assess_route("what are the main actor types", &intent);
assert_eq!(assessment.confidence, RouteConfidence::Inferred);
assert!(assessment.rationale.contains("trait-like symbol"));
assert!(assessment.suggested_next_step.is_some());
}
#[test]
fn test_route_invocation_understand_implementations() {
let intent = QueryIntent::UnderstandImplementations {
name: "Actor".to_string(),
};
let invocation = route_invocation(&intent);
assert_eq!(
invocation,
"find_references(name=\"Actor\", mode=\"implementations\")"
);
assert_eq!(route_tool_name(&intent), "find_references");
}
}