npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@agentbrain/core

v1.4.3

Published

Shared intelligence layer for AgentBrain - smart context generation for coding agents

Readme

@agentbrain/core

Core intelligence layer for AgentBrain - shared library for repository analysis and AI-powered documentation generation.

Features

  • 🤖 Provider-agnostic AI client - Works with both Anthropic (Claude) and OpenAI (GPT)
  • 📁 Intelligent file crawler - Scans repositories with relevance scoring
  • 🧠 Context generation - Creates comprehensive codebase documentation
  • 📋 Standards generation - Generates coding standards for different AI agents
  • 🔄 Handoff generation - Creates session handoff documents from git diffs
  • 💾 Smart caching - Git-hash based cache invalidation for zero-cost repeat runs
  • 🔐 Secure config - Encrypted API key storage with proper permissions

Installation

npm install @agentbrain/core

Usage

Generate Context Documentation

import { generateContext, loadAIConfig } from '@agentbrain/core'

const aiConfig = await loadAIConfig()

const result = await generateContext({
  repoPath: '/path/to/repo',
  aiConfig,
  maxFiles: 100,
  useCache: true,
  onProgress: (msg) => console.log(msg),
})

console.log('Generated docs:', result.docs)
console.log('Total tokens:', result.totalTokens)
console.log('Cost:', result.cost)

Scan Repository

import { scanRepository } from '@agentbrain/core'

const scanResult = await scanRepository('/path/to/repo', {
  maxFiles: 100,
  onProgress: (msg) => console.log(msg),
})

console.log('Total files:', scanResult.totalFiles)
console.log('Relevant files:', scanResult.relevantFiles.length)
console.log('Git hash:', scanResult.gitHash)

AI Client

import { AIClient, loadAIConfig } from '@agentbrain/core'

const config = await loadAIConfig()
const client = new AIClient(config)

const response = await client.generate(
  [{ role: 'user', content: 'Explain this code...' }],
  'mid',  // tier: 'fast' | 'mid' | 'smart'
  { temperature: 0.5, maxTokens: 2000 }
)

console.log(response.content)
console.log('Tokens used:', response.tokenCount)

Cache Management

import { loadCache, saveCache, isCacheValid } from '@agentbrain/core'

// Check if cache is valid
const isValid = await isCacheValid('/path/to/repo', currentGitHash)

// Load cache
const cache = await loadCache('/path/to/repo')

// Save to cache
await saveCachedDoc('/path/to/repo', gitHash, doc)

API Reference

Types

type AIProvider = 'anthropic' | 'openai'
type ModelTier = 'fast' | 'mid' | 'smart'

interface AIConfig {
  provider: AIProvider
  apiKey: string
  models: {
    fast: string
    mid: string
    smart: string
  }
}

interface ContextDoc {
  type: 'context' | 'dependency-map' | 'patterns' | 'handoff' | 'standards'
  content: string
  generatedAt: string
  gitHash: string
  tokenCount: number
}

Main Functions

generateContext(options: GenerateContextOptions)

Generates complete context documentation for a repository.

generateStandards(options: GenerateStandardsOptions)

Generates coding standards files for AI agents (CLAUDE.md, .cursorrules, .windsurfrules).

generateHandoff(options: GenerateHandoffOptions)

Generates session handoff document from git diff.

scanRepository(repoPath: string, options?)

Scans repository and returns relevant files with scoring.

loadAIConfig(apiKeyOverride?: string)

Loads AI configuration from environment or stored config.

saveAPIKey(apiKey: string)

Saves API key to secure config file.

Architecture

Chunk + Merge Strategy

AgentBrain uses a tiered approach to context generation:

  1. Scan - Analyze file tree with no AI calls
  2. Score - Calculate relevance scores for files
  3. Chunk - Summarize each file independently with fast models
  4. Merge - Synthesize summaries into comprehensive docs with mid models

This approach:

  • ✅ Prevents context overflow
  • ✅ Optimizes costs (uses cheap models where possible)
  • ✅ Scales to large repositories

Cache Strategy

Cache is keyed by git commit hash:

  • Same hash = instant return (0 tokens)
  • Different hash = regenerate
  • Cache stored at {repoPath}/.agentbrain/cache.json

File Scoring

Files are scored based on:

  • +100 - Always-include files (README.md, package.json, etc.)
  • +50 - Entry points (index, main, app, server)
  • +30 - Config files
  • -3 per level - Directory depth penalty
  • -10 - Test files

Files with score < 0 are excluded.

Configuration

API Keys

Set via environment variables (highest priority):

export ANTHROPIC_API_KEY="sk-ant-..."
# or
export OPENAI_API_KEY="sk-..."

Or store persistently:

import { saveAPIKey } from '@agentbrain/core'
await saveAPIKey('sk-ant-...')

Stored at ~/.agentbrain/config.json with 0600 permissions.

Model Selection

Default models by provider:

Anthropic:

  • Fast: claude-haiku-4-5-20251001
  • Mid: claude-sonnet-4-6
  • Smart: claude-opus-4-6

OpenAI:

  • Fast: gpt-4o-mini
  • Mid: gpt-4o
  • Smart: gpt-4.1

License

MIT

Related Packages