Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 182 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,185 @@ Always include a README.md for new packages. The `README.md` should always follo
Apache 2.0
```

[... rest of the existing content remains unchanged ...]
## Common Terminology

- **Actor**: A stateful, long-lived entity that processes messages and maintains state
- **Manager**: Component responsible for creating, routing, and managing actor instances
- **Remote Procedure Call (RPC)**: Method for an actor to expose callable functions to clients
- **Event**: Asynchronous message sent from an actor to connected clients
- **Alarm**: Scheduled callback that triggers at a specific time

### Coordinated Topology Terminology

- **Peer**: Individual actor instance in a coordinated network
- **Node**: Physical or logical host running one or more actor peers

## Build Commands

Run these commands from the root of the project. They depend on Turborepo, so you cannot run the commands within the package itself. Running these commands are important in order to ensure that all dependencies are automatically built.

- **Type Check:** `pnpm check-types` - Verify TypeScript types
- **Check specific package:** `pnpm check-types -F rivetkit` - Check only specified package
- **Build:** `pnpm build` - Production build using Turbopack
- **Build specific package:** `pnpm build -F rivetkit` - Build only specified package
- **Format:** `pnpm fmt` - Format code with Biome
- Do not run the format command automatically.

## Core Concepts

### Topologies

rivetkit supports three topologies that define how actors communicate and scale:

- **Singleton:** A single instance of an actor running in one location
- **Partition:** Multiple instances of an actor type partitioned by ID, useful for horizontal scaling
- **Coordinate:** Actors connected in a peer-to-peer network, sharing state between instances

### Driver Interfaces

Driver interfaces define the contract between rivetkit and various backends:

- **ActorDriver:** Manages actor state, lifecycle, and persistence
- **ManagerDriver:** Manages actor discovery, routing, and scaling
- **CoordinateDriver:** Handles peer-to-peer communication between actor instances
- Only applicable in coordinate topologies

### Driver Implementations

Located in `packages/drivers/`, these implement the driver interfaces:

- **Memory:** In-memory implementation for development and testing
- **Redis:** Production-ready implementation using Redis for persistence and pub/sub

### Platforms

Located in `packages/platforms/`, these adapt rivetkit to specific runtime environments:

- **NodeJS:** Standard Node.js server environment
- **Cloudflare Workers:** Edge computing environment
- **Bun:** Fast JavaScript runtime alternative to Node.js
- **Rivet:** Cloud platform with built-in scaling and management

## Package Import Resolution

When importing from workspace packages, always check the package's `package.json` file under the `exports` field to determine the correct import paths:

1. Locate the package's `package.json` file
2. Find the `exports` object which maps subpath patterns to their file locations
3. Use these defined subpaths in your imports rather than direct file paths
4. For example, if you need to import from a package, check its exports to find if it exposes specific subpaths for different modules

This ensures imports resolve correctly across different build environments and prevents errors from direct file path imports that might change.

## Code Style Guidelines

- **Formatting:** Uses Biome for consistent formatting
- See biome.json for reference on formatting rules
- **Imports:** Organized imports enforced, unused imports warned
- **TypeScript:** Strict mode enabled, target ESNext
- **Naming:**
- camelCase for variables, functions
- PascalCase for classes, interfaces, types
- UPPER_CASE for constants
- Use `#` prefix for private class members (not `private` keyword)
- **Error Handling:**
- Extend from `ActorError` base class (packages/core/src/actor/errors.ts)
- Use `UserError` for client-safe errors
- Use `InternalError` for internal errors
- Don't try to fix type issues by casting to unknown or any. If you need to do this, then stop and ask me to manually intervene.
- Write log messages in lowercase
- Use `logger()` to log messages
- Do not store `logger()` as a variable, always call it using `logger().info("...")`
- Use structured logging where it makes sense, for example: `logger().info("foo", { bar: 5, baz: 10 })`
- Supported logging methods are: trace, debug, info, warn, error, critical
- Instead of returning errors as raw HTTP responses with c.json, use or write an error in packages/rivetkit/src/actor/errors.ts and throw that instead. The middleware will automatically serialize the response for you.

## Project Structure

- Monorepo with pnpm workspaces and Turborepo
- Core code in `packages/core/`
- Platform implementations in `packages/platforms/`
- Driver implementations in `packages/drivers/`

## Development Notes

- Use zod for runtime type validation
- Use `assertUnreachable(x: never)` for exhaustive type checking in switch statements
- Add proper JSDoc comments for public APIs
- Ensure proper error handling with descriptive messages
- Run `pnpm check-types` regularly during development to catch type errors early. Prefer `pnpm check-types` instead of `pnpm build`.
- Use `tsx` CLI to execute TypeScript scripts directly (e.g., `tsx script.ts` instead of `node script.js`).
- Do not auto-commit changes

## Test Guidelines

- Do not check if errors are an instanceOf ActorError in tests. Many error types do not have the same prototype chain when sent over the network, but still have the same properties so you can safely cast with `as`.

## Examples

Examples live in the `examples/` folder.

### Example Configuration

- All examples should have the turbo.json:

```json
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
```

### `examples/*/package.json`

- Always name the example `example-{name}`
- Always use `workspace:*` for dependencies
- Use `tsx` unless otherwise instructed
- Always have a `dev` and `check-types` scripts
- `dev` should use `tsx --watch` unless otherwise instructed
- `check-types` should use `tsc --noEmit`

### `examples/*/README.md`

Always include a README.md. The `README.md` should always follow this structure:

```md
# {human readable title} for RivetKit

Example project demonstrating {specific feature} with [RivetKit](https://rivetkit.org).

[Learn More →](https://github.com/rivet-gg/rivetkit)

[Discord](https://rivet.gg/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-gg/rivetkit/issues)

## Getting Started

### Prerequisites

- {node or bun based on demo}
- {any other related services if this integrates with external SaaS}

### Installation

```sh
git clone https://github.com/rivet-gg/rivetkit
cd rivetkit/examples/{name}
npm install
```

### Development

```sh
npm run dev
```

{instructions to either open browser or run script to test it}

## License

Apache 2.0
```

## Test Notes

- Using setTimeout in tests & test actors will not work unless you call `await waitFor(driverTestConfig, <ts>)`
3 changes: 1 addition & 2 deletions examples/better-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"vitest": "^3.1.1"
},
"dependencies": {
"@hono/node-server": "^1.14.4",
"@rivetkit/react": "workspace:0.9.0-rc.1",
"@rivetkit/react": "workspace:*",
"better-auth": "^1.0.1",
"hono": "^4.7.0",
"react": "^18.2.0",
Expand Down
4 changes: 4 additions & 0 deletions examples/better-auth/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/chat-room/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/cloudflare-workers-hono/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/cloudflare-workers/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/counter/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
2 changes: 1 addition & 1 deletion examples/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@rivetkit/db": "workspace:0.9.0-rc.1",
"@rivetkit/db": "workspace:*",
"drizzle-kit": "^0.31.2",
"drizzle-orm": "^0.44.2"
},
Expand Down
4 changes: 4 additions & 0 deletions examples/drizzle/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
2 changes: 1 addition & 1 deletion examples/elysia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@rivetkit/react": "workspace:0.9.0-rc.1",
"@rivetkit/react": "workspace:*",
"elysia": "^1.3.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
4 changes: 4 additions & 0 deletions examples/elysia/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
2 changes: 1 addition & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@rivetkit/react": "workspace:0.9.0-rc.1",
"@rivetkit/react": "workspace:*",
"express": "^5.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
4 changes: 4 additions & 0 deletions examples/express/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
3 changes: 1 addition & 2 deletions examples/hono-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"vitest": "^3.1.1"
},
"dependencies": {
"@hono/node-server": "^1.14.4",
"@rivetkit/react": "workspace:0.9.0-rc.1",
"@rivetkit/react": "workspace:*",
"hono": "^4.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
4 changes: 4 additions & 0 deletions examples/hono-react/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
1 change: 0 additions & 1 deletion examples/hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@hono/node-server": "^1.14.4",
"hono": "^4.7.0"
},
"stableVersion": "0.8.0"
Expand Down
4 changes: 4 additions & 0 deletions examples/hono/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
2 changes: 1 addition & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"vitest": "^3.1.1"
},
"dependencies": {
"@rivetkit/react": "workspace:0.9.0-rc.1",
"@rivetkit/react": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 4 additions & 0 deletions examples/react/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/snippets/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/starter/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
4 changes: 4 additions & 0 deletions examples/trpc/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
Loading
Loading