@getmikk/ai-context 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +327 -0
- package/package.json +31 -27
- package/src/claude-md-generator.ts +265 -265
- package/src/context-builder.ts +398 -398
- package/src/index.ts +4 -4
- package/src/providers.ts +154 -154
- package/src/types.ts +69 -69
- package/tests/claude-md.test.ts +137 -137
- package/tests/smoke.test.ts +5 -5
- package/tsconfig.json +15 -15
package/README.md
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
# @getmikk/ai-context
|
|
2
|
+
|
|
3
|
+
> Intelligent context distillation for AI coding agents — builds token-budgeted, relevance-scored context payloads from the dependency graph, plus generates `claude.md` / `AGENTS.md` files.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@getmikk/ai-context)
|
|
6
|
+
[](../../LICENSE)
|
|
7
|
+
|
|
8
|
+
`@getmikk/ai-context` solves the "context window" problem for AI coding assistants. Instead of dumping your entire codebase into a prompt, it uses the dependency graph to trace only the relevant functions, files, and constraints for a given task — then packs them into a token-budgeted payload.
|
|
9
|
+
|
|
10
|
+
It also generates tiered `claude.md` and `AGENTS.md` files that give AI agents a structured understanding of your project architecture.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @getmikk/ai-context
|
|
18
|
+
# or
|
|
19
|
+
bun add @getmikk/ai-context
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Peer dependencies:** `@getmikk/core`, `@getmikk/intent-engine`
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
### Context Queries
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { ContextBuilder, getProvider } from '@getmikk/ai-context'
|
|
32
|
+
import { ContractReader, LockReader } from '@getmikk/core'
|
|
33
|
+
|
|
34
|
+
const contract = await new ContractReader().read('./mikk.json')
|
|
35
|
+
const lock = await new LockReader().read('./mikk.lock.json')
|
|
36
|
+
|
|
37
|
+
const builder = new ContextBuilder(contract, lock)
|
|
38
|
+
const context = builder.build({
|
|
39
|
+
task: 'Add rate limiting to the authentication endpoints',
|
|
40
|
+
tokenBudget: 8000,
|
|
41
|
+
maxHops: 3,
|
|
42
|
+
includeCallGraph: true,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// Format for a specific AI provider
|
|
46
|
+
const provider = getProvider('claude')
|
|
47
|
+
const formatted = provider.formatContext(context)
|
|
48
|
+
// Send `formatted` as part of your AI prompt
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Claude.md / AGENTS.md Generation
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { ClaudeMdGenerator } from '@getmikk/ai-context'
|
|
55
|
+
|
|
56
|
+
const generator = new ClaudeMdGenerator(contract, lock, /* tokenBudget */ 4000)
|
|
57
|
+
const markdown = generator.generate()
|
|
58
|
+
|
|
59
|
+
// Write to project root
|
|
60
|
+
await writeFile('./claude.md', markdown)
|
|
61
|
+
await writeFile('./AGENTS.md', markdown)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## How Context Building Works
|
|
67
|
+
|
|
68
|
+
The `ContextBuilder` uses a 6-step algorithm:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
1. Resolve Seeds
|
|
72
|
+
└─ Parse task → extract keywords → match against lock file functions/modules
|
|
73
|
+
|
|
74
|
+
2. BFS Proximity Walk
|
|
75
|
+
└─ Walk the call graph outward from seed functions (up to maxHops)
|
|
76
|
+
|
|
77
|
+
3. Score Functions
|
|
78
|
+
└─ Each function gets a relevance score:
|
|
79
|
+
• Proximity score (closer to seed = higher)
|
|
80
|
+
• Keyword match score (task keywords in function name)
|
|
81
|
+
• Entry-point bonus (exported functions score higher)
|
|
82
|
+
|
|
83
|
+
4. Sort by Score
|
|
84
|
+
└─ Descending relevance
|
|
85
|
+
|
|
86
|
+
5. Fill Token Budget
|
|
87
|
+
└─ Greedily add functions until budget is exhausted
|
|
88
|
+
(each function's token cost ≈ line count × 4)
|
|
89
|
+
|
|
90
|
+
6. Group by Module
|
|
91
|
+
└─ Organize selected functions into module-level context
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## API Reference
|
|
97
|
+
|
|
98
|
+
### ContextBuilder
|
|
99
|
+
|
|
100
|
+
The main entry point for building task-specific context.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { ContextBuilder } from '@getmikk/ai-context'
|
|
104
|
+
|
|
105
|
+
const builder = new ContextBuilder(contract, lock)
|
|
106
|
+
const context = builder.build(query)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**`ContextQuery`:**
|
|
110
|
+
|
|
111
|
+
| Field | Type | Default | Description |
|
|
112
|
+
|-------|------|---------|-------------|
|
|
113
|
+
| `task` | `string` | — | Natural-language description of the task |
|
|
114
|
+
| `focusFiles` | `string[]` | `[]` | Specific files to prioritize |
|
|
115
|
+
| `focusModules` | `string[]` | `[]` | Specific modules to prioritize |
|
|
116
|
+
| `maxFunctions` | `number` | `50` | Maximum functions to include |
|
|
117
|
+
| `maxHops` | `number` | `3` | BFS depth limit from seed functions |
|
|
118
|
+
| `tokenBudget` | `number` | `8000` | Maximum estimated tokens |
|
|
119
|
+
| `includeCallGraph` | `boolean` | `true` | Include `calls[]` and `calledBy[]` per function |
|
|
120
|
+
|
|
121
|
+
**`AIContext` (returned):**
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
type AIContext = {
|
|
125
|
+
project: string // Project name from contract
|
|
126
|
+
modules: ContextModule[] // Relevant modules with their functions
|
|
127
|
+
constraints: string[] // Active architectural constraints
|
|
128
|
+
decisions: string[] // Relevant ADRs
|
|
129
|
+
prompt: string // Original task
|
|
130
|
+
meta: {
|
|
131
|
+
seedCount: number // How many seed functions were found
|
|
132
|
+
totalFunctionsConsidered: number
|
|
133
|
+
selectedFunctions: number // Functions that fit in the budget
|
|
134
|
+
estimatedTokens: number // Approximate token count
|
|
135
|
+
keywords: string[] // Extracted keywords from the task
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**`ContextModule`:**
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
type ContextModule = {
|
|
144
|
+
id: string
|
|
145
|
+
name: string
|
|
146
|
+
description?: string
|
|
147
|
+
intent?: string // Module's purpose from mikk.json
|
|
148
|
+
functions: ContextFunction[]
|
|
149
|
+
files: string[]
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**`ContextFunction`:**
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
type ContextFunction = {
|
|
157
|
+
name: string
|
|
158
|
+
file: string
|
|
159
|
+
startLine: number
|
|
160
|
+
endLine: number
|
|
161
|
+
calls: string[] // Functions this one calls
|
|
162
|
+
calledBy: string[] // Functions that call this one
|
|
163
|
+
purpose?: string // Inferred purpose
|
|
164
|
+
errorHandling?: string // Error patterns detected
|
|
165
|
+
edgeCases?: string // Edge cases noted
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
### ClaudeMdGenerator
|
|
172
|
+
|
|
173
|
+
Generates tiered markdown documentation files for AI agents.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { ClaudeMdGenerator } from '@getmikk/ai-context'
|
|
177
|
+
|
|
178
|
+
const generator = new ClaudeMdGenerator(contract, lock, tokenBudget)
|
|
179
|
+
const markdown = generator.generate()
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Tiered output structure:**
|
|
183
|
+
|
|
184
|
+
| Tier | Content | Budget |
|
|
185
|
+
|------|---------|--------|
|
|
186
|
+
| **Tier 1** | Project summary — name, module count, total functions, architecture overview | ~500 tokens |
|
|
187
|
+
| **Tier 2** | Module details — each module's intent, public API, file list, key functions | ~300 tokens/module |
|
|
188
|
+
| **Tier 3** | Constraints & decisions — all architectural rules and ADRs | Remaining budget |
|
|
189
|
+
|
|
190
|
+
**All data is sourced from the AST-derived lock file** — no hallucinated descriptions. Module intents come from `mikk.json`, function lists and call graphs come from `mikk.lock.json`.
|
|
191
|
+
|
|
192
|
+
**Example output:**
|
|
193
|
+
|
|
194
|
+
```markdown
|
|
195
|
+
# Project: my-app
|
|
196
|
+
|
|
197
|
+
## Architecture Overview
|
|
198
|
+
- **Modules:** 5
|
|
199
|
+
- **Total Functions:** 87
|
|
200
|
+
- **Total Files:** 23
|
|
201
|
+
|
|
202
|
+
## Modules
|
|
203
|
+
|
|
204
|
+
### auth
|
|
205
|
+
**Intent:** Handle user authentication and session management
|
|
206
|
+
**Public API:** `login`, `logout`, `validateToken`, `refreshSession`
|
|
207
|
+
**Files:** auth/login.ts, auth/session.ts, auth/middleware.ts
|
|
208
|
+
**Key Functions:**
|
|
209
|
+
- `validateToken` (auth/middleware.ts:15-42) → calls: `decodeJWT`, `checkExpiry`
|
|
210
|
+
- `login` (auth/login.ts:8-35) → calls: `validateCredentials`, `createSession`
|
|
211
|
+
|
|
212
|
+
### payments
|
|
213
|
+
...
|
|
214
|
+
|
|
215
|
+
## Constraints
|
|
216
|
+
- auth: no-import from payments
|
|
217
|
+
- payments: must-use stripe-sdk
|
|
218
|
+
|
|
219
|
+
## Decisions
|
|
220
|
+
- ADR-001: Use JWT for stateless auth (2024-01-15)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
### Providers
|
|
226
|
+
|
|
227
|
+
Providers format the `AIContext` object into a string optimized for a specific AI model.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import { getProvider, ClaudeProvider, GenericProvider } from '@getmikk/ai-context'
|
|
231
|
+
|
|
232
|
+
// Factory
|
|
233
|
+
const provider = getProvider('claude') // or 'generic'
|
|
234
|
+
|
|
235
|
+
// Format context for the provider
|
|
236
|
+
const formatted = provider.formatContext(context)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### ClaudeProvider
|
|
240
|
+
|
|
241
|
+
Formats with structured XML tags optimized for Anthropic Claude:
|
|
242
|
+
|
|
243
|
+
```xml
|
|
244
|
+
<architecture>
|
|
245
|
+
<module name="auth" intent="Handle authentication">
|
|
246
|
+
<function name="validateToken" file="auth/middleware.ts" lines="15-42">
|
|
247
|
+
<calls>decodeJWT, checkExpiry</calls>
|
|
248
|
+
<calledBy>authMiddleware</calledBy>
|
|
249
|
+
</function>
|
|
250
|
+
</module>
|
|
251
|
+
</architecture>
|
|
252
|
+
<constraints>
|
|
253
|
+
auth: no-import from payments
|
|
254
|
+
</constraints>
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### GenericProvider
|
|
258
|
+
|
|
259
|
+
Clean plain-text format for any AI model:
|
|
260
|
+
|
|
261
|
+
```
|
|
262
|
+
## Module: auth
|
|
263
|
+
Intent: Handle authentication
|
|
264
|
+
|
|
265
|
+
### validateToken (auth/middleware.ts:15-42)
|
|
266
|
+
Calls: decodeJWT, checkExpiry
|
|
267
|
+
Called by: authMiddleware
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## Usage Examples
|
|
273
|
+
|
|
274
|
+
### "What breaks if I change this file?"
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
const context = builder.build({
|
|
278
|
+
task: `Impact analysis for changes to src/auth/login.ts`,
|
|
279
|
+
focusFiles: ['src/auth/login.ts'],
|
|
280
|
+
maxHops: 5,
|
|
281
|
+
tokenBudget: 4000,
|
|
282
|
+
})
|
|
283
|
+
// Returns all functions transitively affected by login.ts
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### "Get context for adding a feature"
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
const context = builder.build({
|
|
290
|
+
task: 'Add two-factor authentication to the login flow',
|
|
291
|
+
focusModules: ['auth'],
|
|
292
|
+
tokenBudget: 12000,
|
|
293
|
+
includeCallGraph: true,
|
|
294
|
+
})
|
|
295
|
+
// Returns auth module functions + related cross-module calls
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### "Focused context for a specific module"
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
const context = builder.build({
|
|
302
|
+
task: 'Refactor the payment processing pipeline',
|
|
303
|
+
focusModules: ['payments'],
|
|
304
|
+
maxFunctions: 30,
|
|
305
|
+
tokenBudget: 6000,
|
|
306
|
+
})
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
## Types
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import type {
|
|
315
|
+
AIContext,
|
|
316
|
+
ContextModule,
|
|
317
|
+
ContextFunction,
|
|
318
|
+
ContextQuery,
|
|
319
|
+
ContextProvider,
|
|
320
|
+
} from '@getmikk/ai-context'
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## License
|
|
326
|
+
|
|
327
|
+
[Apache-2.0](../../LICENSE)
|
package/package.json
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@getmikk/ai-context",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"@
|
|
26
|
-
}
|
|
27
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@getmikk/ai-context",
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/Ansh-dhanani/mikk"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"dev": "tsc --watch"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@getmikk/core": "^1.3.1",
|
|
25
|
+
"@getmikk/intent-engine": "^1.3.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.7.0",
|
|
29
|
+
"@types/node": "^22.0.0"
|
|
30
|
+
}
|
|
31
|
+
}
|