|
| 1 | +""" |
| 2 | +Sample: Direct Context - API-based indexing with import/export state |
| 3 | +
|
| 4 | +This sample demonstrates: |
| 5 | +- Creating a Direct Context instance |
| 6 | +- Adding files to the index |
| 7 | +- Searching the indexed files |
| 8 | +- Using Generation API to ask questions about indexed code |
| 9 | +- Generating documentation from indexed code |
| 10 | +- Exporting state to a file |
| 11 | +- Importing state from a file |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from auggie_sdk.context import DirectContext, File |
| 20 | + |
| 21 | +# Sample files are in the samples/ subdirectory |
| 22 | +SAMPLES_DIR = Path(__file__).parent / "samples" |
| 23 | + |
| 24 | + |
| 25 | +def load_sample_files() -> list[File]: |
| 26 | + """Load sample Python files from the samples directory.""" |
| 27 | + files = [] |
| 28 | + for file_path in SAMPLES_DIR.rglob("*.py"): |
| 29 | + relative_path = file_path.relative_to(SAMPLES_DIR) |
| 30 | + contents = file_path.read_text() |
| 31 | + files.append(File(path=str(relative_path), contents=contents)) |
| 32 | + return files |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + print("=== Direct Context Sample ===\n") |
| 37 | + |
| 38 | + # Create a Direct Context instance |
| 39 | + # Authentication is automatic via: |
| 40 | + # 1. AUGMENT_API_TOKEN / AUGMENT_API_URL env vars, or |
| 41 | + # 2. ~/.augment/session.json (created by `auggie login`) |
| 42 | + print("Creating Direct Context...") |
| 43 | + context = DirectContext.create(debug=True) |
| 44 | + |
| 45 | + # Load sample files from the samples/ directory |
| 46 | + print("\nAdding files to index...") |
| 47 | + file_objects = load_sample_files() |
| 48 | + print(f" Found {len(file_objects)} sample files") |
| 49 | + result = context.add_to_index(file_objects) |
| 50 | + print("\nIndexing result:") |
| 51 | + print(f" Newly uploaded: {result.newly_uploaded}") |
| 52 | + print(f" Already uploaded: {result.already_uploaded}") |
| 53 | + |
| 54 | + # Search the codebase - returns formatted string ready for LLM use or display |
| 55 | + # Using queries that work well with our realistic content |
| 56 | + print("\n--- Search 1: Find string utility functions ---") |
| 57 | + results1 = context.search("string utility functions for text formatting") |
| 58 | + print("Search results:") |
| 59 | + print(results1) |
| 60 | + |
| 61 | + print("\n--- Search 2: Find user management service ---") |
| 62 | + results2 = context.search("user management service with CRUD operations") |
| 63 | + print("Search results:") |
| 64 | + print(results2) |
| 65 | + |
| 66 | + print("\n--- Search 3: Find HTTP client for API requests ---") |
| 67 | + http_results = context.search("HTTP client for making API requests") |
| 68 | + print("Search results:") |
| 69 | + print(http_results) |
| 70 | + |
| 71 | + # Use search_and_ask to ask questions about the indexed code |
| 72 | + print("\n--- search_and_ask Example 1: Ask questions about the code ---") |
| 73 | + question = "How does the UserService class handle user creation and validation?" |
| 74 | + print(f"Question: {question}") |
| 75 | + |
| 76 | + answer = context.search_and_ask( |
| 77 | + "user creation and validation in UserService", |
| 78 | + question, |
| 79 | + ) |
| 80 | + |
| 81 | + print(f"\nAnswer: {answer}") |
| 82 | + |
| 83 | + # Use search_and_ask to generate documentation |
| 84 | + print("\n--- search_and_ask Example 2: Generate documentation ---") |
| 85 | + documentation = context.search_and_ask( |
| 86 | + "string utility functions", |
| 87 | + "Generate API documentation in markdown format for the string utility functions", |
| 88 | + ) |
| 89 | + |
| 90 | + print("\nGenerated Documentation:") |
| 91 | + print(documentation) |
| 92 | + |
| 93 | + # Use search_and_ask to explain code patterns |
| 94 | + print("\n--- search_and_ask Example 3: Explain code patterns ---") |
| 95 | + explanation = context.search_and_ask( |
| 96 | + "utility functions", |
| 97 | + "Explain what these utility functions do and when they would be useful", |
| 98 | + ) |
| 99 | + |
| 100 | + print(f"\nExplanation: {explanation}") |
| 101 | + |
| 102 | + # Export state to a file |
| 103 | + state_file = Path(tempfile.gettempdir()) / "direct-context-state.json" |
| 104 | + print(f"\nExporting state to {state_file}...") |
| 105 | + context.export_to_file(state_file) |
| 106 | + print("State exported successfully") |
| 107 | + |
| 108 | + # Show the exported state |
| 109 | + with open(state_file, "r") as f: |
| 110 | + exported_state = json.load(f) |
| 111 | + print("\nExported state:") |
| 112 | + print(json.dumps(exported_state, indent=2)) |
| 113 | + |
| 114 | + # Import state in a new context |
| 115 | + print("\n--- Testing state import ---") |
| 116 | + context2 = DirectContext.import_from_file(state_file, debug=False) |
| 117 | + print("State imported successfully") |
| 118 | + |
| 119 | + # Verify we can still search |
| 120 | + results3 = context2.search("string utility functions") |
| 121 | + print("\nSearch after importing state:") |
| 122 | + print(results3) |
| 123 | + |
| 124 | + print("\n=== Sample Complete ===") |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + try: |
| 129 | + main() |
| 130 | + except Exception as error: |
| 131 | + print(f"Error: {error}") |
| 132 | + sys.exit(1) |
| 133 | + |
0 commit comments