Skip to content

Commit e457946

Browse files
committed
Examples for the TypeScript context SDK
1 parent ad5318a commit e457946

File tree

25 files changed

+5471
-0
lines changed

25 files changed

+5471
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Context Examples
2+
3+
Examples demonstrating the Auggie SDK's context modes and AI-powered code analysis.
4+
5+
## Examples
6+
7+
### [Direct Context](./direct-context/)
8+
API-based indexing with semantic search and AI Q&A.
9+
10+
```bash
11+
auggie login
12+
npx tsx examples/context/direct-context/index.ts
13+
```
14+
15+
### [FileSystem Context](./filesystem-context/)
16+
Local directory search via MCP protocol.
17+
18+
```bash
19+
npx tsx examples/context/filesystem-context/index.ts
20+
```
21+
22+
### [File Search Server](./file-search-server/)
23+
REST API for semantic file search with AI summarization.
24+
25+
```bash
26+
export AUGMENT_API_TOKEN="your-token"
27+
npx tsx examples/context/file-search-server/index.ts .
28+
curl "http://localhost:3000/search?q=typescript"
29+
```
30+
31+
### [Prompt Enhancer Server](./prompt-enhancer-server/)
32+
HTTP server that enhances prompts with codebase context.
33+
34+
```bash
35+
npx tsx examples/context/prompt-enhancer-server/index.ts .
36+
curl -X POST http://localhost:3001/enhance \
37+
-H "Content-Type: application/json" \
38+
-d '{"prompt": "fix the login bug"}'
39+
```
40+
41+
## Setup
42+
43+
1. Build the SDK: `npm run build`
44+
2. Authenticate: `auggie login` or set `AUGMENT_API_TOKEN`
45+
3. For FileSystem Context: Install `auggie` CLI
46+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Direct Context Example
2+
3+
API-based indexing with semantic search, AI Q&A, and state persistence.
4+
5+
## Usage
6+
7+
```bash
8+
# Authenticate
9+
auggie login
10+
11+
# Run the example
12+
npx tsx examples/context/direct-context/index.ts
13+
```
14+
15+
## What It Does
16+
17+
- Creates a Direct Context instance
18+
- Adds sample files to the index
19+
- Performs semantic searches
20+
- Uses `searchAndAsk()` for AI-powered Q&A
21+
- Generates documentation
22+
- Exports/imports context state
23+
24+
## Key Features
25+
26+
- **`search()`**: Semantic search returning formatted code snippets
27+
- **`searchAndAsk()`**: One-step AI Q&A about indexed code
28+
- **State persistence**: Export/import index for reuse
29+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# File Search Server Example
2+
3+
REST API for semantic file search with AI-powered summarization and code explanation.
4+
5+
## Prerequisites
6+
7+
Install the `auggie` CLI:
8+
```bash
9+
auggie --version
10+
```
11+
12+
## Usage
13+
14+
```bash
15+
export AUGMENT_API_TOKEN="your-token"
16+
npx tsx examples/context/file-search-server/index.ts .
17+
```
18+
19+
## API Endpoints
20+
21+
### Search Files
22+
```bash
23+
curl "http://localhost:3000/search?q=typescript"
24+
curl "http://localhost:3000/search?q=package.json&format=text"
25+
```
26+
27+
### Summarize (AI)
28+
```bash
29+
curl -X POST http://localhost:3000/summarize \
30+
-H "Content-Type: application/json" \
31+
-d '{"query":"authentication logic"}'
32+
```
33+
34+
### Explain Code (AI)
35+
```bash
36+
curl -X POST http://localhost:3000/explain \
37+
-H "Content-Type: application/json" \
38+
-d '{"path":"src/auth.ts"}'
39+
```
40+
41+
### Health Check
42+
```bash
43+
curl "http://localhost:3000/health"
44+
```
45+

0 commit comments

Comments
 (0)