|
| 1 | +import { DirectContext } from '@augmentcode/auggie-sdk'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Example 1: Simple Usage |
| 5 | + * Upload files and ask questions immediately |
| 6 | + */ |
| 7 | +async function simpleExample() { |
| 8 | + console.log('\n=== Example 1: Simple Usage ===\n'); |
| 9 | + |
| 10 | + // Create context (authentication is automatic) |
| 11 | + const context = await DirectContext.create(); |
| 12 | + |
| 13 | + // Add files and wait for indexing (default behavior) |
| 14 | + const result = await context.addToIndex([ |
| 15 | + { path: 'src/auth.ts', contents: 'export function login(user: string, pass: string) { /* auth logic */ }' }, |
| 16 | + { path: 'src/user.ts', contents: 'export class User { constructor(public id: string) { /* user logic */ } }' }, |
| 17 | + { path: 'README.md', contents: '# My Project\nAuthentication system' } |
| 18 | + ]); |
| 19 | + |
| 20 | + console.log(`Newly uploaded: ${result.newlyUploaded.length}`); |
| 21 | + |
| 22 | + // Ask questions about the code - files are already indexed |
| 23 | + const answer = await context.searchAndAsk( |
| 24 | + 'How does the login system work?' |
| 25 | + ); |
| 26 | + console.log('Answer:', answer); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Example 2: Persistent Index |
| 31 | + * Persist state between sessions to avoid re-indexing |
| 32 | + */ |
| 33 | +async function persistentIndexExample() { |
| 34 | + console.log('\n=== Example 2: Persistent Index ===\n'); |
| 35 | + |
| 36 | + // First session: Upload and save |
| 37 | + console.log('Session 1: Uploading and saving...'); |
| 38 | + const context1 = await DirectContext.create(); |
| 39 | + |
| 40 | + await context1.addToIndex([ |
| 41 | + { path: 'src/auth.ts', contents: 'export function login() { /* auth logic */ }' }, |
| 42 | + { path: 'src/user.ts', contents: 'export class User { /* user logic */ }' }, |
| 43 | + { path: 'src/database.ts', contents: 'export function connect() { /* db connection */ }' } |
| 44 | + ]); |
| 45 | + |
| 46 | + // Save state for later use |
| 47 | + await context1.exportToFile('./my-project-context.json'); |
| 48 | + console.log('Context saved!'); |
| 49 | + |
| 50 | + // Second session: Load and update |
| 51 | + console.log('\nSession 2: Loading and updating...'); |
| 52 | + const context2 = await DirectContext.importFromFile('./my-project-context.json'); |
| 53 | + console.log('Context loaded from previous session'); |
| 54 | + |
| 55 | + // Make incremental changes |
| 56 | + await context2.removeFromIndex(['src/user.ts']); |
| 57 | + await context2.addToIndex([ |
| 58 | + { path: 'src/profile.ts', contents: 'export class Profile { /* profile logic */ }' }, |
| 59 | + { path: 'src/settings.ts', contents: 'export function updateSettings() { /* settings */ }' } |
| 60 | + ]); |
| 61 | + console.log('Index updated incrementally'); |
| 62 | + |
| 63 | + // Save updated state |
| 64 | + await context2.exportToFile('./my-project-context.json'); |
| 65 | + console.log('Updated context saved!'); |
| 66 | + |
| 67 | + // Third session: Load and search |
| 68 | + console.log('\nSession 3: Loading and searching...'); |
| 69 | + const context3 = await DirectContext.importFromFile('./my-project-context.json'); |
| 70 | + console.log('Updated context loaded'); |
| 71 | + |
| 72 | + const answer = await context3.searchAndAsk( |
| 73 | + 'What user management features are implemented?' |
| 74 | + ); |
| 75 | + console.log('Answer:', answer); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Example 3: Batch Upload Then Wait |
| 80 | + * Optimized for multiple uploads before searching |
| 81 | + */ |
| 82 | +async function batchExample() { |
| 83 | + console.log('\n=== Example 3: Batch Upload ===\n'); |
| 84 | + |
| 85 | + const context = await DirectContext.create(); |
| 86 | + |
| 87 | + // Upload multiple batches without waiting for indexing |
| 88 | + await context.addToIndex([ |
| 89 | + { path: 'src/auth.ts', contents: 'export function login() { /* auth logic */ }' }, |
| 90 | + { path: 'src/user.ts', contents: 'export class User { /* user logic */ }' } |
| 91 | + ], { waitForIndexing: false }); |
| 92 | + |
| 93 | + await context.addToIndex([ |
| 94 | + { path: 'src/database.ts', contents: 'export function connect() { /* db connection */ }' }, |
| 95 | + { path: 'src/api.ts', contents: 'export function createServer() { /* api logic */ }' } |
| 96 | + ], { waitForIndexing: false }); |
| 97 | + |
| 98 | + // Wait for all files to be indexed |
| 99 | + console.log('Waiting for indexing to complete...'); |
| 100 | + await context.waitForIndexing(); |
| 101 | + console.log('All files indexed!'); |
| 102 | + |
| 103 | + // Now search - all files are guaranteed to be indexed |
| 104 | + const answer = await context.searchAndAsk( |
| 105 | + 'How do the database and auth systems work together?' |
| 106 | + ); |
| 107 | + console.log('Answer:', answer); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Example 4: External LLM Integration |
| 112 | + * Use search() results with external LLM APIs |
| 113 | + */ |
| 114 | +async function externalLLMExample() { |
| 115 | + console.log('\n=== Example 4: External LLM Integration ===\n'); |
| 116 | + |
| 117 | + const context = await DirectContext.create(); |
| 118 | + |
| 119 | + // Upload code |
| 120 | + await context.addToIndex([ |
| 121 | + { path: 'src/api.ts', contents: 'export function handleRequest() { /* API logic */ }' }, |
| 122 | + { path: 'src/auth.ts', contents: 'export function authenticate() { /* auth */ }' }, |
| 123 | + { path: 'src/database.ts', contents: 'export function query() { /* db query */ }' } |
| 124 | + ]); |
| 125 | + |
| 126 | + // Get formatted search results for external LLMs |
| 127 | + const codeContext = await context.search('API request handling'); |
| 128 | + |
| 129 | + console.log('Code context for LLM (first 500 chars):'); |
| 130 | + console.log(codeContext.substring(0, 500) + '...'); |
| 131 | + console.log('\nThis context can be used with Anthropic, OpenAI, or other LLM APIs'); |
| 132 | +} |
| 133 | + |
| 134 | +// Run all examples |
| 135 | +async function main() { |
| 136 | + try { |
| 137 | + await simpleExample(); |
| 138 | + await persistentIndexExample(); |
| 139 | + await batchExample(); |
| 140 | + await externalLLMExample(); |
| 141 | + } catch (error) { |
| 142 | + console.error('Error running examples:', error); |
| 143 | + process.exit(1); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +main(); |
| 148 | + |
0 commit comments