|
| 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 { readFileSync } from "node:fs"; |
| 15 | +import { join } from "node:path"; |
| 16 | +import { DirectContext } from "@augmentcode/auggie-sdk"; |
| 17 | + |
| 18 | +async function main() { |
| 19 | + console.log("=== Direct Context Sample ===\n"); |
| 20 | + |
| 21 | + // Create a Direct Context instance |
| 22 | + // Authentication is automatic via: |
| 23 | + // 1. AUGMENT_API_TOKEN / AUGMENT_API_URL env vars, or |
| 24 | + // 2. ~/.augment/session.json (created by `auggie login`) |
| 25 | + console.log("Creating Direct Context..."); |
| 26 | + const context = await DirectContext.create({ debug: true }); |
| 27 | + |
| 28 | + // Add some sample files to the index |
| 29 | + console.log("\nAdding files to index..."); |
| 30 | + const files = [ |
| 31 | + { |
| 32 | + path: "sample/calculator.ts", |
| 33 | + contents: `export class Calculator { |
| 34 | + add(a: number, b: number): number { |
| 35 | + return a + b; |
| 36 | + } |
| 37 | +
|
| 38 | + subtract(a: number, b: number): number { |
| 39 | + return a - b; |
| 40 | + } |
| 41 | +
|
| 42 | + multiply(a: number, b: number): number { |
| 43 | + return a * b; |
| 44 | + } |
| 45 | +
|
| 46 | + divide(a: number, b: number): number { |
| 47 | + if (b === 0) throw new Error("Division by zero"); |
| 48 | + return a / b; |
| 49 | + } |
| 50 | +}`, |
| 51 | + }, |
| 52 | + { |
| 53 | + path: "sample/utils.ts", |
| 54 | + contents: `export function formatNumber(num: number): string { |
| 55 | + return num.toLocaleString(); |
| 56 | +} |
| 57 | +
|
| 58 | +export function isEven(num: number): boolean { |
| 59 | + return num % 2 === 0; |
| 60 | +} |
| 61 | +
|
| 62 | +export function clamp(value: number, min: number, max: number): number { |
| 63 | + return Math.min(Math.max(value, min), max); |
| 64 | +}`, |
| 65 | + }, |
| 66 | + { |
| 67 | + path: "sample/main.ts", |
| 68 | + contents: `import { Calculator } from "./calculator"; |
| 69 | +import { formatNumber } from "./utils"; |
| 70 | +
|
| 71 | +const calc = new Calculator(); |
| 72 | +const result = calc.add(10, 20); |
| 73 | +console.log("Result:", formatNumber(result));`, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + |
| 77 | + const result = await context.addToIndex(files); |
| 78 | + console.log("\nIndexing result:"); |
| 79 | + console.log(" Newly indexed:", result.newlyIndexed); |
| 80 | + console.log(" Already indexed:", result.alreadyIndexed); |
| 81 | + |
| 82 | + // Search the codebase - returns formatted string ready for LLM use or display |
| 83 | + console.log("\n--- Search 1: Find calculator functions ---"); |
| 84 | + const results1 = await context.search("calculator functions for arithmetic"); |
| 85 | + console.log("Search results:"); |
| 86 | + console.log(results1); |
| 87 | + |
| 88 | + console.log("\n--- Search 2: Find utility functions ---"); |
| 89 | + const results2 = await context.search("utility functions"); |
| 90 | + console.log("Search results:"); |
| 91 | + console.log(results2); |
| 92 | + |
| 93 | + // Use searchAndAsk to ask questions about the indexed code |
| 94 | + console.log("\n--- searchAndAsk Example 1: Ask questions about the code ---"); |
| 95 | + const question = "How does the Calculator class handle division by zero?"; |
| 96 | + console.log(`Question: ${question}`); |
| 97 | + |
| 98 | + const answer = await context.searchAndAsk( |
| 99 | + "division by zero error handling", |
| 100 | + question |
| 101 | + ); |
| 102 | + |
| 103 | + console.log(`\nAnswer: ${answer}`); |
| 104 | + |
| 105 | + // Use searchAndAsk to generate documentation |
| 106 | + console.log("\n--- searchAndAsk Example 2: Generate documentation ---"); |
| 107 | + const documentation = await context.searchAndAsk( |
| 108 | + "Calculator class methods", |
| 109 | + "Generate API documentation in markdown format for this code" |
| 110 | + ); |
| 111 | + |
| 112 | + console.log("\nGenerated Documentation:"); |
| 113 | + console.log(documentation); |
| 114 | + |
| 115 | + // Use searchAndAsk to explain code patterns |
| 116 | + console.log("\n--- searchAndAsk Example 3: Explain code patterns ---"); |
| 117 | + const explanation = await context.searchAndAsk( |
| 118 | + "utility functions", |
| 119 | + "Explain what these utility functions do and when they would be useful" |
| 120 | + ); |
| 121 | + |
| 122 | + console.log(`\nExplanation: ${explanation}`); |
| 123 | + |
| 124 | + // Export state to a file |
| 125 | + const stateFile = join("/tmp", "direct-context-state.json"); |
| 126 | + console.log(`\nExporting state to ${stateFile}...`); |
| 127 | + await context.exportToFile(stateFile); |
| 128 | + console.log("State exported successfully"); |
| 129 | + |
| 130 | + // Show the exported state |
| 131 | + const exportedState = JSON.parse(readFileSync(stateFile, "utf-8")); |
| 132 | + console.log("\nExported state:"); |
| 133 | + console.log(JSON.stringify(exportedState, null, 2)); |
| 134 | + |
| 135 | + // Import state in a new context |
| 136 | + console.log("\n--- Testing state import ---"); |
| 137 | + const context2 = await DirectContext.create({ debug: false }); |
| 138 | + await context2.importFromFile(stateFile); |
| 139 | + console.log("State imported successfully"); |
| 140 | + |
| 141 | + // Verify we can still search |
| 142 | + const results3 = await context2.search("division by zero"); |
| 143 | + console.log("\nSearch after importing state:"); |
| 144 | + console.log(results3); |
| 145 | + |
| 146 | + console.log("\n=== Sample Complete ==="); |
| 147 | +} |
| 148 | + |
| 149 | +main().catch((error) => { |
| 150 | + console.error("Error:", error); |
| 151 | + process.exit(1); |
| 152 | +}); |
0 commit comments