devika icon indicating copy to clipboard operation
devika copied to clipboard

feat: Add Morph codebase search integration

Open DhruvBhatia0 opened this issue 8 months ago • 0 comments

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

  1. User sends a follow-up message (bug fix, feature, question)
  2. The Action agent classifies the intent
  3. For answer/feature/bug actions: ReadCode.search_code(prompt) calls the Morph WarpGrep API
  4. WarpGrep searches the codebase in up to 4 turns using ripgrep, file reads, and directory listings
  5. Returns the complete relevant files (preserving existing prompt contracts)
  6. For run/report actions: 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.parse syntax validation
  • [x] Config getter/setter follow existing pattern exactly
  • [x] _file_tree excludes .git, node_modules, __pycache__, binary files
  • [x] _validate_path rejects ../../etc/passwd and absolute paths outside project root
  • [x] _run_ripgrep finds matches with rg, returns clear message when unavailable
  • [x] _read_file handles full reads, line ranges, single lines, and errors
  • [x] _parse_tool_calls handles valid XML, malformed XML (regex fallback), and empty input
  • [x] _read_full_files reads 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

DhruvBhatia0 avatar Mar 08 '26 02:03 DhruvBhatia0