Kimi Code Documentation

repository·main·Indexed 25 days ago

https://github.com/moonshotai/kimi-code

An AI coding agent available as a CLI, VS Code extension, and web client. Kimi Code can read/edit code, execute shell commands, and search files. It supports the Model Context Protocol (MCP) for extending capabilities and provides a unified agent engine via @moonshot-ai/agent-core, including a dependency injection container and an ACP adapter for exposing agent sessions via the Agent Client Protocol.

Tokens
117.4K
Snippets
209
Records
610
Agent score
88%

What's inside Kimi Code

  1. Overview of @moonshot-ai/agent-core Dependency Injection Container

    main

    The @moonshot-ai/agent-core DI container is a VSCode-style dependency injection system designed for the agent-core/server stack. It provides service identifiers, registration, resolution, and lifecycle management with zero runtime dependencies.

    Key features include:

    • Service Identifiers: Branded callable values created via createDecorator<T>().
    • Resolution: InstantiationService handles singleton-per-container semantics, scoped child containers via createChild, and automatic constructor-parameter injection.
    • Delayed Instantiation: Services can be lazily materialized behind a Proxy using SyncDescriptor.
    • Cycle Detection: Uses both graph-based walks and construction stack tracking to prevent cyclic dependencies.
    • Testing Support: A specialized TestInstantiationService is available via the @moonshot-ai/agent-core/di/test subpath for easy stubbing and resolution.
  2. Overview of Kimi Code Built-in Tools

    main

    Kimi Code CLI provides a set of built-in tools managed directly by the runtime. These tools do not require MCP server installation and are automatically selected by the Agent.

    Approval Mechanism:

    • Read-only tools (e.g., Read, Grep, Glob) are auto-allowed by default.
    • Write and execution tools (e.g., Write, Edit, Bash) require user approval by default.
    • In YOLO mode, approval for regular tool calls is skipped.
    • Plan mode exit approval is always required regardless of mode.
  3. Understand the Unified Read-Write Model Design for agent-core-v2

    main

    The agent-core-v2 unified read-write model is designed to provide a single source of truth for views, topics, write operations, and subscriptions. It aims to resolve issues like event visibility confusion, inconsistent definition methods, and feedback loops.

    Key Design Constraints:

    • Persistence is frozen: The design does not change the physical storage layout. wire.jsonl paths, the 'one physical log file per agent' rule, and the PersistedWireRecord data structure remain unchanged.
    • API-level unification: Unification occurs only at the in-process API level, specifically for write entry points, read models, subscriptions, phases, and the type registry.
    • Compatibility: The design maintains existing protocol_version and migration chain (1.0→1.5) mechanisms and does not introduce new log format migrations.
  4. Understand the Kimi Code CLI Operating Environment

    main

    The Kimi Code CLI operates in a non-sandboxed environment on the user's machine. Actions taken by the agent (such as file reads, writes, or command executions) immediately affect the host system.

    Key Environment Variables & Context:

    • {{ KIMI_OS }}: The operating system being used.
    • {{ KIMI_SHELL }}: The shell used for executing Bash tool commands.
    • {{ KIMI_WORK_DIR }}: The current working directory, which serves as the project root. Use absolute paths for tool parameters when required.
    • {{ KIMI_NOW }}: The ISO format date/time captured at session start. Note that this value is static and may become stale; for time-sensitive tasks, use the date command via a shell tool to get the current time.
  5. Use Kimi Code features and slash commands

    main

    Kimi Code provides several capabilities for autonomous coding and context management:

    • Autonomous Actions: Explores codebases, reads/writes code, and runs terminal commands (requires permission).
    • Thinking Controls: Toggle reasoning or select specific model-supported thinking effort levels.
    • Provider-aware Models: Select specific models across different configured providers even if they share the same name.
    • Native Editor Integration: Review AI changes directly using the VS Code diff viewer.
    • MCP Support: Extend capabilities using Model Context Protocol servers.
    • Slash Commands:
      • /init: Analyzes your current project.
      • /compact: Manages and optimizes the current context.
  6. Understand the Kimi Code Agent System Instructions

    main

    The Kimi Code agent is an interactive AI agent designed for software engineering tasks. It operates directly on the user's system (not in a sandbox) and uses tools to perform actions like reading files, searching code, and executing commands.

    Key Operational Principles:

    • Task-Oriented: If a request can be interpreted as a task (e.g., "change methodName to snake_case"), the agent defaults to using tools to perform the change rather than just describing it.
    • Language Consistency: The agent communicates in the user's language but maintains original forms for code, commands, identifiers, and file paths. Technical artifacts (comments, commits, PRs) follow project conventions.
    • Tool Preference: The agent prefers dedicated tools (Read, Glob, Grep) over raw shell commands to respect workspace access policies and manage output size.
    • Parallelism: For efficiency, the agent is encouraged to issue multiple non-interfering tool calls (like Read, Grep, and Glob) in parallel, especially during investigation phases.
  7. Understand the Kimi Code Unified Read/Write Model

    main

    The system is built on a unified model consisting of 5 Primitives, 1 Stream Structure, and 1 State Machine. This design separates decision-making (Commands) from state (Views) and persistent history (Facts).

    The 5 Primitives

    PrimitiveDefinitionPurpose
    FactA persistent, replayable event that has occurred."What changed the state"
    CommandA stateless decision-making unit that produces 0..n Facts."Who decided to change something"
    ViewA pure function that folds the Fact stream into a state."What the current state is"
    SignalA transient, non-persistent, typed event."Current progress/volatile status"
    EffectA strategy that reacts to Fact/View changes via Commands."What happens after a fact occurs"
    HookAn interceptor for write operations."Intercepting/vetoing an operation"

    Core Concepts

    • Facts are committed to the stream and are essential for resuming state.
    • Commands are never replayed; they only run in the live phase.
    • Views are the sole carriers of state, derived by folding Facts.
    • Signals are volatile and do not participate in state folding.
  8. Understand the VS Code Node SDK Migration (v0.6.0+)

    main

    Starting with version 0.6.0, the Kimi Code VS Code extension migrated from a Python-based CLI architecture to an in-process Node SDK architecture using @moonshot-ai/kimi-code-sdk.

    Key Changes:

    • Runtime: Replaced the Python/stdio host with the TypeScript v1 engine running directly in the VS Code Extension Host.
    • Identity: The extension identifies itself with the User-Agent kimi-code-vscode/[version].
    • Data Sharing: The extension and the TUI (Terminal User Interface) now share the same configuration, authentication, MCP configuration, and sessions, provided they resolve to the same Kimi Code home directory.
    • Environment Variables: The legacy kimi.environmentVariables setting has been removed. Provider-specific variables must now be placed in config.toml, and MCP server variables in mcp.json.