Main Packages in the Kimi-Code Repository: Complete Monorepo Architecture
The Kimi-Code repository organizes its TypeScript architecture into fourteen workspace-scoped packages under packages/, spanning core agent engines (@moonshot-ai/agent-core and agent-core-v2), the Kap server backend, Node.js and client SDKs, LLM abstractions, execution runtimes, and shared protocols.
Kimi-Code, developed by MoonshotAI, is an agentic coding system constructed as a TypeScript monorepo. Understanding the main packages in the kimi-code repository is essential for developers extending the engine, building integrations, or contributing to the core infrastructure. Each package resides under the packages/ directory and publishes as a scoped npm module under the @moonshot-ai namespace.
Core Engine Packages
The engine packages define the fundamental agent runtime, session management, and execution loops that power the Kimi-Code applications.
@moonshot-ai/agent-core
The foundational engine package exposes the unified agent architecture. In packages/agent-core/src/agent/index.ts, the main Agent class coordinates tool execution, planning, and RPC communication. The package also defines Session management in packages/agent-core/src/session/index.ts, which handles activity lifecycle and state persistence. This package exports its public API from src/index.ts and remains the primary entry point for understanding the agent runtime.
@moonshot-ai/agent-core-v2
This updated engine implements a new dependency injection (DI) model and serves as the runtime for newer server and client implementations. While it supersedes the original core for modern applications, both engines coexist during migration periods. The package entry point is src/index.ts.
Backend and Server Packages
@moonshot-ai/kap-server
The Kimi-Code backend, located in packages/kap-server, exposes REST and WebSocket APIs for the entire system. The server bootstrap logic resides in src/start.ts, which initializes the HTTP and WebSocket services. Key services include the transcript operation batch handling in src/transcript/service.ts, which implements the sequence contract for turn-based interactions, global search endpoints, and debug interfaces.
Client SDK and API Packages
@moonshot-ai/node-sdk
The public TypeScript SDK enables Node.js scripts and CLI tools to consume the engine directly. The high-level API wrapper lives in packages/node-sdk/src/client.ts, exposing methods like session.create() and agent.run(). This package abstracts the RPC complexity for server-side automation.
@moonshot-ai/klient
A low-level client façade wrapping the server RPC surface, used by UI applications and the Node SDK. Agent operations are exposed through packages/klient/src/agent/api.ts, while transcript retrieval is handled in packages/klient/src/transcript/api.ts. The search functionality resides in packages/klient/src/search/api.ts, providing access to the global search index.
Runtime and Infrastructure Packages
@moonshot-ai/kosong
This provider-agnostic LLM abstraction layer manages model catalogs, token usage tracking, and provider adapters. The model discovery interface is implemented in packages/kosong/src/catalog/index.ts, supporting configuration layering across different LLM backends.
@moonshot-ai/kaos
The execution environment package supplies sandboxed runtime primitives. File system operations, process spawning, and worker-pool utilities are managed here, with process execution safety implemented in packages/kaos/src/process.ts.
@moonshot-ai/telemetry
Shared client-side telemetry infrastructure handling metrics collection, event batching, and reporting across all client applications.
@moonshot-ai/minidb
An embedded JSON document store powering the global search feature. The inverted full-text index implementation is located in packages/minidb/src/text-index.ts.
Protocol and Support Libraries
@moonshot-ai/protocol
Contains shared TypeScript type definitions for the wire protocol between client and server, ensuring type safety across the RPC boundary.
@moonshot-ai/oauth
Provides authentication flow utilities for Kimi-Code's OAuth integration.
@moonshot-ai/migration-legacy
Legacy data translation helpers that convert older session data formats to current engine structures. The translation logic is implemented in packages/migration-legacy/src/translator.ts.
@moonshot-ai/tree-sitter-bash
A pure-TypeScript Bash parser built on tree-sitter, used for tool-permission analysis. The main parsing implementation is in packages/tree-sitter-bash/src/parser.ts.
@moonshot-ai/pi-tui
Text-user-interface primitives for CLI and TUI front-ends, providing reusable components rendered in packages/pi-tui/src/ui.tsx.
Practical Code Examples
The following examples demonstrate how to interact with the main packages in the kimi-code repository.
Creating an Agent with agent-core
import { Agent } from '#/agent'; // from @moonshot-ai/agent-core
import { Session } from '#/session';
import { createLogger } from '#/logger';
const session = new Session({ sessionId: 'demo-session' });
const agent = new Agent({
session,
model: 'gpt-4o-mini', // model name from the kosong catalog
logger: createLogger(),
});
await agent.runPrompt('Explain the planetary model of the atom.');
(Source: packages/agent-core/src/agent/index.ts)
Using the Node SDK Client
import { KimiClient } from '@moonshot-ai/node-sdk';
const client = new KimiClient({ serverUrl: 'http://localhost:58627' });
const session = await client.session.create({ name: 'my-session' });
await client.agent.run({
sessionId: session.id,
prompt: 'Summarize the latest news about AI research.',
});
(Source: packages/node-sdk/src/client.ts)
Accessing Transcripts via Klient
import { Klient } from '@moonshot-ai/klient';
const klient = new Klient({ apiBase: '/api/v1' });
const transcript = await klient.transcript.get({
sessionId: 'my-session',
beforeTurn: 0,
});
console.log(transcript.turns.map(t => t.assistant?.text).join('\n---\n'));
(Source: packages/klient/src/transcript/api.ts)
Querying Global Search
import { Klient } from '@moonshot-ai/klient';
const client = new Klient({ apiBase: '/api/v1' });
const results = await client.search.global({
query: 'bash parsing',
mode: 'terms', // or 'literal'
});
for (const hit of results.hits) {
console.log(`[${hit.sessionId}] ${hit.text}`);
}
(Source: packages/klient/src/search/api.ts)
Summary
- Kimi-Code is structured as a TypeScript monorepo with fourteen scoped packages under
packages/. - agent-core and agent-core-v2 provide the dual engine architecture supporting legacy and DI-based runtimes.
- kap-server hosts the backend services via
packages/kap-server/src/start.ts. - node-sdk and klient offer high-level and low-level client access respectively.
- kosong, kaos, and minidb supply LLM abstraction, sandboxed execution, and search indexing.
- protocol, oauth, and migration-legacy handle cross-cutting concerns like type safety, authentication, and data migration.
Frequently Asked Questions
What is the difference between @moonshot-ai/agent-core and agent-core-v2?
@moonshot-ai/agent-core is the original unified engine defining the Agent and Session classes in packages/agent-core/src/agent/index.ts and packages/agent-core/src/session/index.ts. agent-core-v2 introduces a new dependency injection model used by modern server implementations and newer client code, offering improved modularity while maintaining compatibility with the core execution model.
Which package provides the global search functionality?
The @moonshot-ai/minidb package implements the embedded JSON document store with an inverted full-text index in packages/minidb/src/text-index.ts. The @moonshot-ai/klient package exposes this functionality to clients through packages/klient/src/search/api.ts, allowing queries against the global search index via the client.search.global() method.
How do I start the Kimi-Code backend server?
The @moonshot-ai/kap-server package contains the backend implementation. Run the server entry point at packages/kap-server/src/start.ts, which bootstraps the REST and WebSocket APIs, transcript services, and debug endpoints required for the Kimi-Code ecosystem.
What package handles LLM provider abstraction?
@moonshot-ai/kosong provides the provider-agnostic LLM layer, including model catalog management in packages/kosong/src/catalog/index.ts, token usage tracking, and provider adapters. This abstraction allows Kimi-Code to switch between different LLM backends without modifying the core engine code.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →