fix: guard against IndexError on empty query string in SPARQL generator

Issue #870: query.split() on an empty or whitespace-only string returns
an empty list, and accessing [0] raises IndexError. Although the existing
startswith guard on line 205 already prevents this in practice (empty
strings won't match SPARQL keywords), the direct indexing is fragile.

Refactor to split first and check the result before indexing, making the
code resilient to future refactoring of the upstream guard.
This commit is contained in:
Baima (Hermes Agent) 2026-05-08 07:14:10 +08:00
parent 1ffae12559
commit 4f2445bf71

View file

@ -203,10 +203,12 @@ ASK {{
if response and isinstance(response, dict):
query = response.get('query', '').strip()
if query.upper().startswith(('SELECT', 'ASK', 'CONSTRUCT', 'DESCRIBE')):
parts = query.split()
query_type = parts[0].upper() if parts else 'UNKNOWN'
return SPARQLQuery(
query=query,
variables=self._extract_variables(query),
query_type=query.split()[0].upper(),
query_type=query_type,
explanation=response.get('explanation', 'Generated by LLM'),
complexity_score=self._calculate_complexity(query)
)