|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example script demonstrating PicoCode API usage. |
| 4 | +This shows how to integrate PicoCode with a PyCharm plugin or other IDE. |
| 5 | +""" |
| 6 | +import requests |
| 7 | +import json |
| 8 | +import time |
| 9 | +from typing import Optional, Dict, Any |
| 10 | + |
| 11 | +class PicoCodeClient: |
| 12 | + """Client for interacting with PicoCode API.""" |
| 13 | + |
| 14 | + def __init__(self, base_url: str = "http://127.0.0.1:8000"): |
| 15 | + self.base_url = base_url |
| 16 | + self.api_base = f"{base_url}/api" |
| 17 | + |
| 18 | + def health_check(self) -> Dict[str, Any]: |
| 19 | + """Check if the server is running and healthy.""" |
| 20 | + response = requests.get(f"{self.api_base}/health") |
| 21 | + response.raise_for_status() |
| 22 | + return response.json() |
| 23 | + |
| 24 | + def create_project(self, path: str, name: Optional[str] = None) -> Dict[str, Any]: |
| 25 | + """Create or get a project.""" |
| 26 | + response = requests.post( |
| 27 | + f"{self.api_base}/projects", |
| 28 | + json={"path": path, "name": name} |
| 29 | + ) |
| 30 | + response.raise_for_status() |
| 31 | + return response.json() |
| 32 | + |
| 33 | + def list_projects(self) -> list: |
| 34 | + """List all projects.""" |
| 35 | + response = requests.get(f"{self.api_base}/projects") |
| 36 | + response.raise_for_status() |
| 37 | + return response.json() |
| 38 | + |
| 39 | + def get_project(self, project_id: str) -> Dict[str, Any]: |
| 40 | + """Get project details.""" |
| 41 | + response = requests.get(f"{self.api_base}/projects/{project_id}") |
| 42 | + response.raise_for_status() |
| 43 | + return response.json() |
| 44 | + |
| 45 | + def delete_project(self, project_id: str) -> Dict[str, Any]: |
| 46 | + """Delete a project.""" |
| 47 | + response = requests.delete(f"{self.api_base}/projects/{project_id}") |
| 48 | + response.raise_for_status() |
| 49 | + return response.json() |
| 50 | + |
| 51 | + def index_project(self, project_id: str) -> Dict[str, Any]: |
| 52 | + """Start indexing a project.""" |
| 53 | + response = requests.post( |
| 54 | + f"{self.api_base}/projects/index", |
| 55 | + json={"project_id": project_id} |
| 56 | + ) |
| 57 | + response.raise_for_status() |
| 58 | + return response.json() |
| 59 | + |
| 60 | + def query(self, project_id: str, query: str, top_k: int = 5) -> Dict[str, Any]: |
| 61 | + """Perform semantic search.""" |
| 62 | + response = requests.post( |
| 63 | + f"{self.api_base}/query", |
| 64 | + json={ |
| 65 | + "project_id": project_id, |
| 66 | + "query": query, |
| 67 | + "top_k": top_k |
| 68 | + } |
| 69 | + ) |
| 70 | + response.raise_for_status() |
| 71 | + return response.json() |
| 72 | + |
| 73 | + def get_code_suggestion( |
| 74 | + self, |
| 75 | + project_id: str, |
| 76 | + prompt: str, |
| 77 | + context: str = "", |
| 78 | + use_rag: bool = True, |
| 79 | + top_k: int = 5 |
| 80 | + ) -> Dict[str, Any]: |
| 81 | + """Get code suggestions using RAG + LLM.""" |
| 82 | + response = requests.post( |
| 83 | + f"{self.api_base}/code", |
| 84 | + json={ |
| 85 | + "project_id": project_id, |
| 86 | + "prompt": prompt, |
| 87 | + "context": context, |
| 88 | + "use_rag": use_rag, |
| 89 | + "top_k": top_k |
| 90 | + } |
| 91 | + ) |
| 92 | + response.raise_for_status() |
| 93 | + return response.json() |
| 94 | + |
| 95 | + |
| 96 | +def example_workflow(): |
| 97 | + """Example workflow for IDE integration.""" |
| 98 | + client = PicoCodeClient() |
| 99 | + |
| 100 | + print("=" * 60) |
| 101 | + print("PicoCode API Example Workflow") |
| 102 | + print("=" * 60) |
| 103 | + |
| 104 | + # 1. Health check |
| 105 | + print("\n1. Checking server health...") |
| 106 | + try: |
| 107 | + health = client.health_check() |
| 108 | + print(f" ✓ Server is healthy: {health}") |
| 109 | + except Exception as e: |
| 110 | + print(f" ✗ Server is not running: {e}") |
| 111 | + print(" Please start the server with: python main.py") |
| 112 | + return |
| 113 | + |
| 114 | + # 2. Create a project |
| 115 | + print("\n2. Creating/getting project...") |
| 116 | + project_path = "/tmp/example_project" |
| 117 | + try: |
| 118 | + project = client.create_project(project_path, "Example Project") |
| 119 | + project_id = project["id"] |
| 120 | + print(f" ✓ Project ID: {project_id}") |
| 121 | + print(f" ✓ Status: {project['status']}") |
| 122 | + except Exception as e: |
| 123 | + print(f" ✗ Failed to create project: {e}") |
| 124 | + return |
| 125 | + |
| 126 | + # 3. List all projects |
| 127 | + print("\n3. Listing all projects...") |
| 128 | + try: |
| 129 | + projects = client.list_projects() |
| 130 | + print(f" ✓ Found {len(projects)} project(s)") |
| 131 | + for p in projects[:3]: # Show first 3 |
| 132 | + print(f" - {p['name']}: {p['path']} ({p['status']})") |
| 133 | + except Exception as e: |
| 134 | + print(f" ✗ Failed to list projects: {e}") |
| 135 | + |
| 136 | + # 4. Index the project (this would take time in real use) |
| 137 | + print("\n4. Starting project indexing...") |
| 138 | + print(" Note: This starts background indexing.") |
| 139 | + print(" In a real project, you would poll for completion.") |
| 140 | + try: |
| 141 | + index_result = client.index_project(project_id) |
| 142 | + print(f" ✓ Indexing started: {index_result}") |
| 143 | + except Exception as e: |
| 144 | + print(f" ✗ Failed to start indexing: {e}") |
| 145 | + |
| 146 | + # 5. Query example (would fail if not indexed yet) |
| 147 | + print("\n5. Semantic search example...") |
| 148 | + print(" Note: This requires the project to be indexed first.") |
| 149 | + print(" Skipping in this demo as indexing takes time.") |
| 150 | + |
| 151 | + # 6. Code suggestion example (would fail if not indexed yet) |
| 152 | + print("\n6. Code suggestion example...") |
| 153 | + print(" Note: This requires the project to be indexed first.") |
| 154 | + print(" Skipping in this demo as indexing takes time.") |
| 155 | + |
| 156 | + print("\n" + "=" * 60) |
| 157 | + print("Example workflow completed!") |
| 158 | + print("=" * 60) |
| 159 | + print("\nFor full functionality:") |
| 160 | + print("1. Start the server: python main.py") |
| 161 | + print("2. Create a project with a real codebase path") |
| 162 | + print("3. Index the project: POST /api/projects/index") |
| 163 | + print("4. Wait for indexing to complete (poll /api/projects/{id})") |
| 164 | + print("5. Use /api/query and /api/code for RAG queries") |
| 165 | + |
| 166 | + |
| 167 | +def print_api_reference(): |
| 168 | + """Print API reference.""" |
| 169 | + print("\n" + "=" * 60) |
| 170 | + print("PicoCode API Reference") |
| 171 | + print("=" * 60) |
| 172 | + print(""" |
| 173 | + Base URL: http://127.0.0.1:8000/api |
| 174 | + |
| 175 | + Endpoints: |
| 176 | + |
| 177 | + 1. Health Check |
| 178 | + GET /api/health |
| 179 | + Returns: {"status": "ok", "version": "0.2.0", ...} |
| 180 | + |
| 181 | + 2. Create/Get Project |
| 182 | + POST /api/projects |
| 183 | + Body: {"path": "/path/to/project", "name": "Optional Name"} |
| 184 | + Returns: Project object |
| 185 | + |
| 186 | + 3. List Projects |
| 187 | + GET /api/projects |
| 188 | + Returns: Array of project objects |
| 189 | + |
| 190 | + 4. Get Project |
| 191 | + GET /api/projects/{project_id} |
| 192 | + Returns: Project object |
| 193 | + |
| 194 | + 5. Delete Project |
| 195 | + DELETE /api/projects/{project_id} |
| 196 | + Returns: {"success": true} |
| 197 | + |
| 198 | + 6. Index Project |
| 199 | + POST /api/projects/index |
| 200 | + Body: {"project_id": "..."} |
| 201 | + Returns: {"status": "indexing", ...} |
| 202 | + |
| 203 | + 7. Semantic Search |
| 204 | + POST /api/query |
| 205 | + Body: {"project_id": "...", "query": "...", "top_k": 5} |
| 206 | + Returns: {"results": [...], ...} |
| 207 | + |
| 208 | + 8. Code Suggestions |
| 209 | + POST /api/code |
| 210 | + Body: {"project_id": "...", "prompt": "...", "use_rag": true} |
| 211 | + Returns: {"response": "...", "used_context": [...], ...} |
| 212 | + |
| 213 | + For more details, see PYCHARM_INTEGRATION.md |
| 214 | + """) |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + import sys |
| 219 | + |
| 220 | + if len(sys.argv) > 1 and sys.argv[1] == "--help": |
| 221 | + print_api_reference() |
| 222 | + else: |
| 223 | + example_workflow() |
0 commit comments