This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
npm run build- Build the TypeScript project to dist/npm run dev- Watch mode for development (rebuilds on file changes)npm run clean- Remove the dist/ directory
npm test- Run all tests using Vitestnpm test -- tests/claude-code.test.ts- Run a specific test filenpm test -- --coverage- Run tests with coverage report
npm run lint- Run ESLint on all TypeScript filesnpm run typecheck- Run TypeScript type checking without emitting filesnpm run format- Format code using Prettier
npm run prepublishOnly- Automatically runs clean and build before publishing
This is a TypeScript SDK wrapper for the claude-code CLI. The architecture follows a clean separation of concerns:
-
ClaudeCode Class (
src/claude-code.ts): Main entry point that wraps the claude CLI- Handles command construction with proper JSON output format
- Manages API key, model, and working directory options
- Provides methods:
chat(),newSession(),version() - Uses execa for process execution
-
Session Class (
src/session.ts): Manages multi-turn conversations- Tracks session IDs and message history
- Maintains conversation context across multiple prompts
- Automatically resumes the last session ID when continuing conversations
-
Command Utilities (
src/commands.ts): Low-level process executionexecuteCommand()- Executes commands and returns full outputstreamCommand()- Provides real-time streaming of command output- Both use execa under the hood for robust process management
-
Type Definitions (
src/types.ts): TypeScript interfaces for type safety- Defines all request/response shapes
- Provides proper typing for options, messages, and errors
- Command Pattern: All claude CLI interactions go through command construction with JSON output parsing
- Session Management: Sessions automatically track and resume conversations using session IDs
- Error Handling: Consistent error wrapping with ExecaError details preserved
- Flexible Input: Prompts accept either simple strings or structured objects with system prompts
Tests are located in tests/ and use Vitest. The test suite includes:
- Unit tests for ClaudeCode class methods
- Session management tests
- Command execution tests
- Error handling scenarios
Coverage reporting is configured to exclude type definitions and index files.