@ai-agent-context/core

中文文档
1.0.0发布于 2026/10/18MITTypeScriptES Module零依赖近 30 天下载 4137

Core of ai-agent-context: repository knowledge graph, architecture detection, incremental scanning, deterministic .agent/ serialization. Local-first, zero runtime dependencies, no LLM required.

原始使用文档
文档目录26 节
  1. Installation
  2. Usage
  3. API
  4. AgentContext
  5. Scanner
  6. Knowledge Graph
  7. Architecture Detection
  8. Convention Detection
  9. Decision Detection
  10. Architecture Analysis
  11. Search
  12. Serialization
  13. Language Adapters
  14. TypeScript/JavaScript
  15. JSON
  16. Markdown
  17. Package.json
  18. tsconfig.json
  19. Utilities
  20. Path Utilities
  21. Text Utilities
  22. Pattern Matching
  23. Configuration
  24. Error Handling
  25. Security
  26. License

@ai-agent-context/core

Core analysis engine for ai-agent-context.

This package provides the repository analysis capabilities without any CLI or MCP dependencies. It's designed to be:

  • Local-first: 100% local execution, no external API calls
  • Deterministic: Same repository state → identical context
  • Extensible: Pluggable language adapters, detectors, and analyzers
  • Zero runtime dependencies: Works with only Node.js built-ins

Installation

npm install @ai-agent-context/core

Usage

import { AgentContext } from '@ai-agent-context/core';

const context = await AgentContext.load({ root: process.cwd() });

// Scan repository
await context.scan();

// Explain a module
const module = await context.explain('src/payments');

// Get architecture
const architecture = await context.getArchitecture();

// Get dependencies
const dependencies = await context.getDependencies();

// Search context
const results = await context.search('payment idempotency');

API

AgentContext

Main entry point for repository analysis.

class AgentContext {
  static async load(options?: AgentContextOptions): Promise<AgentContext>
  async scan(options?: ScanOptions): Promise<ScanReport>
  async getRepositoryContext(options?: RepositoryContextOptions): Promise<RepositoryContext>
  async getTaskContext(task: string, options?: { target?: string; maxModules?: number }): Promise<TaskContext>
  async getChangeImpact(target: string, options?: { maxFiles?: number }): Promise<ChangeImpact>
  async getModuleHistory(target: string, options?: { limit?: number }): Promise<ModuleHistoryEntry[]>
  async getRevisionDiff(revision: string, base?: string): Promise<RevisionDiff>
  async getRevisionSnapshot(revision: string): Promise<RevisionSnapshot>
  async explain(target: string): Promise<ModuleExplanation | null>
  async diff(): Promise<ContextDiff>
  async status(): Promise<StatusReport>
  async getRepository(): Promise<Repository>
  async getArchitecture(): Promise<ArchitectureDocument>
  async getDependencies(): Promise<DependenciesDocument>
  async getConventions(): Promise<Convention[]>
  async getDecisions(): Promise<Decision[]>
  async search(query: string, options?: SearchOptions): Promise<SearchResult[]>
  async init(options?: { force?: boolean }): Promise<InitResult>
}

Scanner

Repository scanner and file classification.

import { scanRepository, classifyFile } from '@ai-agent-context/core';

const output = await scanRepository(root, options);
const classification = classifyFile(filePath, language);

Knowledge Graph

Builds a repository knowledge graph from parsed files.

import { buildKnowledgeGraph, buildModules } from '@ai-agent-context/core';

const graph = buildKnowledgeGraph(input);
const modules = buildModules(files, basePath);

Architecture Detection

Detects module roles, entry points, and responsibilities.

import {
  detectEntryPoints,
  detectModuleRoles,
  detectResponsibilities,
  applyArchitectureDetection
} from '@ai-agent-context/core';

const entryPoints = detectEntryPoints(input);
const roles = detectModuleRoles(module);
const responsibilities = detectResponsibilities(module);
const enhanced = applyArchitectureDetection(graph);

Convention Detection

Detects observable coding conventions.

import { detectConventions, renderConventionsMarkdown } from '@ai-agent-context/core';

const conventions = detectConventions(input);
const markdown = renderConventionsMarkdown(conventions, repositoryName);

Decision Detection

Extracts architectural decisions from ADRs and configuration.

import { detectDecisions, parseAdrDocument } from '@ai-agent-context/core';

const decisions = detectDecisions(input);
const adr = parseAdrDocument(content, path);

Architecture Analysis

getArchitecture() includes summary.layeringViolations and layeringViolations with from, to, kind, confidence and evidence.

getModuleHistory(target, { limit }) returns bounded Git history for a module. getRevisionDiff(revision, base) returns changed paths and statuses, including renames.

import { GitAdapter, computeGitActivity } from '@ai-agent-context/core';

const adapter = new GitAdapter(root);
const availability = await adapter.checkAvailability();
const commits = await adapter.getRecentCommits(limit);
const activity = computeGitActivity(input);

Deterministic lexical search over repository context.

import { SearchService, Bm25Index } from '@ai-agent-context/core';

const service = SearchService.fromGraph({ graph, parsed });
const results = service.search(query, options);

Serialization

Reads and writes .agent/ context files.

import {
  readPersistedContext,
  writeContext,
  serializeContextDocuments
} from '@ai-agent-context/core';

const persisted = await readPersistedContext(root);
await writeContext(root, documents, options);

Language Adapters

TypeScript/JavaScript

import { TsJsAdapter } from '@ai-agent-context/core';

const adapter = new TsJsAdapter();
const parsed = await adapter.parse(filePath, content, language);

JSON

import { JsonAdapter } from '@ai-agent-context/core';

const adapter = new JsonAdapter();
const parsed = await adapter.parse(filePath, content, language);

Markdown

import { MarkdownAdapter } from '@ai-agent-context/core';

const adapter = new MarkdownAdapter();
const parsed = await adapter.parse(filePath, content, language);

Package.json

import { PackageJsonAdapter } from '@ai-agent-context/core';

const adapter = new PackageJsonAdapter();
const parsed = await adapter.parse(filePath, content, language);

tsconfig.json

import { TsConfigAdapter } from '@ai-agent-context/core';

const adapter = new TsConfigAdapter();
const parsed = await adapter.parse(filePath, content, language);

Utilities

Path Utilities

import {
  toPosix,
  normalizeRelative,
  dirname,
  basename,
  extname,
  join,
  isInside,
  ancestors,
  relativeTo
} from '@ai-agent-context/core';

Text Utilities

import {
  classifyIdentifierCase,
  splitIdentifier,
  truncate,
  formatDate,
  ratio,
  pluralize
} from '@ai-agent-context/core';

Pattern Matching

import { PatternSet, compileGlob, compileRule } from '@ai-agent-context/core';

Configuration

import {
  loadConfig,
  DEFAULT_CONFIG,
  DEFAULT_INCLUDE,
  DEFAULT_EXCLUDE
} from '@ai-agent-context/core';

const config = await loadConfig({ root: process.cwd() });

Error Handling

import {
  AgentContextError,
  ConfigError,
  AnalysisError,
  ContextStateError,
  isAgentContextError
} from '@ai-agent-context/core';

try {
  await context.scan();
} catch (error) {
  if (isAgentContextError(error)) {
    console.error(error.message);
    console.error(error.suggestions);
  }
}

Security

See the repository SECURITY.md. The core performs no network calls, excludes sensitive files by default, redacts secret-like excerpts, and does not follow symlinks unless explicitly configured.

License

MIT

版本与发布记录

@ai-agent-context/core 共发布 30 个版本,首次发布于 2026/09/19;过去 12 个月发布了 30 个版本。

版本发布日期距今
1.0.0latest2026/10/1819 天前
0.18.02026/10/1819 天前
0.17.02026/10/1819 天前
0.16.02026/10/1819 天前
0.15.02026/10/1819 天前
0.14.12026/10/1819 天前

关于 @ai-agent-context/core 的常见问题

@ai-agent-context/core 是什么?

Core of ai-agent-context: repository knowledge graph, architecture detection, incremental scanning, deterministic .agent/ serialization. Local-first, zero runtime dependencies, no LLM required.

如何安装 @ai-agent-context/core?

在项目目录运行 npm install @ai-agent-context/core,或使用 pnpm add / yarn add / bun add。

@ai-agent-context/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ai-agent-context/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ai-agent-context/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.11.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ai-agent-context/core 可以商用吗?

@ai-agent-context/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ai-agent-context/core 的下载量有多少?

根据 npm 官方下载统计,@ai-agent-context/core 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,137 次,日均约 138 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ai-agent-context/core 还在维护吗?

最新版本 1.0.0 发布于 2026/10/18(19 天前);至今共发布 30 个版本,其中过去 12 个月发布了 30 个;首次发布于 2026/09/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。