devika
devika copied to clipboard
feat: Add Morph codebase search integration
Summary
- Adds Morph as an optional codebase search backend, replacing the current approach of dumping the entire project into LLM context
- When a Morph API key is configured, follow-up actions (bug fixes, features, answers) use targeted code search instead of full-project reads, significantly reducing token usage on large codebases
- Gracefully falls back to the existing full-code-read behavior when no API key is set or when the search fails
Changes
| File | Change |
|---|---|
src/services/morph.py |
New Morph API client with multi-turn tool loop (ripgrep, read, list_directory, finish) |
src/services/morph_prompt.txt |
Search agent system prompt |
src/config.py |
get_morph_api_key() / set_morph_api_key() |
sample.config.toml |
MORPH key under [API_KEYS] |
src/services/__init__.py |
Export Morph |
src/filesystem/read_code.py |
search_code() method with fallback |
src/agents/agent.py |
Lazy code loading — determines action type before loading context |
ui/src/routes/settings/+page.svelte |
MORPH key in settings UI |
How it works
- User sends a follow-up message (bug fix, feature, question)
- The Action agent classifies the intent
- For
answer/feature/bugactions:ReadCode.search_code(prompt)calls the Morph WarpGrep API - WarpGrep searches the codebase in up to 4 turns using ripgrep, file reads, and directory listings
- Returns the complete relevant files (preserving existing prompt contracts)
- For
run/reportactions: falls back to full code read (these need broader context)
Notes
- No new pip dependencies — uses
requests(already in requirements.txt) and stdlib (xml.etree.ElementTree,subprocess) - Requires
rg(ripgrep) on the host for search tool execution; returns a clear message if unavailable - All file paths from the API are validated against the project root to prevent path traversal
- Users bring their own Morph API key (configured via the settings UI)
Test plan
- [x] All Python files pass
ast.parsesyntax validation - [x] Config getter/setter follow existing pattern exactly
- [x]
_file_treeexcludes.git,node_modules,__pycache__, binary files - [x]
_validate_pathrejects../../etc/passwdand absolute paths outside project root - [x]
_run_ripgrepfinds matches withrg, returns clear message when unavailable - [x]
_read_filehandles full reads, line ranges, single lines, and errors - [x]
_parse_tool_callshandles valid XML, malformed XML (regex fallback), and empty input - [x]
_read_full_filesreads valid files and rejects path traversal attempts - [x]
search()returns[]when no API key is set - [x] Full mock API e2e: ripgrep → finish flow returns correct files
- [x] ReadCode falls back to
code_set_to_markdown()on empty results or exceptions - [x] Agent lazy loading: action determined before code context loaded
🤖 Generated with Claude Code