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
96 changes: 96 additions & 0 deletions site/public/llms-full.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions site/src/content/docs/actors/ephemeral-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,35 @@ Use `state` when:

- The data must be preserved across actor sleeps, restarts, updates, or crashes
- The information is essential to the actor's core functionality and business logic

## Advanced

### Accessing Driver Context

The `createVars` function receives a second parameter that provides access to driver-specific context. This allows you to access driver-specific functionality.

For example, the Redis driver exposes access to the Redis instance:

```typescript
import { actor, ActorInitContext } from "@rivetkit/actor";
import { DriverContext } from "@rivetkit/redis";

const myActor = actor({
state: { count: 0 },

// The second parameter provides driver-specific context
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({ driver }),

actions: {
accessDriverFeatures: (c) => {
// Access Redis
const redis = c.vars.driver.redis;
const keyPrefix = c.vars.driver.keyPrefix;
// ...etc...
}
}
});
```

Consult the documentation for each driver to learn more about their respective `DriverContext` types.

39 changes: 39 additions & 0 deletions site/src/content/docs/drivers/redis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,42 @@ Example using Redis driver with Hono web framework.
Basic Redis driver setup and configuration example.
</Card>
</CardGroup>

## Advanced

### Driver Context

The Redis driver provides access to the underlying Redis connection through the driver context in `createVars`.

```typescript
import { actor, ActorInitContext } from "@rivetkit/actor";
import type { DriverContext } from "@rivetkit/redis";

const myActor = actor({
state: { count: 0 },

// Save the Redis driver context
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({ redis: driver.redis }),

actions: {
// Example: Access Redis directly (not recommended in practice)
getRedisValue: async (c, key: string) => {
// Use the Redis client from driver context
return await c.vars.redis.get(key);
},
}
});
```

The Redis driver context type is exported as `DriverContext` from `@rivetkit/redis`:

```typescript
interface DriverContext {
redis: ioredis.Redis;
keyPrefix: string; // Key prefix that all RivetKit data is stored under
}
```

<Warning>
While you have access to the Redis client, be cautious when directly modifying keys under the `keyPrefix`, as this may interfere with RivetKit's internal operations and potentially break actor functionality.
</Warning>
68 changes: 68 additions & 0 deletions site/src/content/docs/hosting-providers/cloudflare-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,71 @@ Example using Cloudflare Workers with Hono web framework.
Basic Cloudflare Workers setup and configuration example.
</Card>
</CardGroup>

## Advanced

### Accessing Environment Bindings

You can access Cloudflare Workers environment bindings directly using the importable `env`:

```typescript
import { env } from "cloudflare:workers";

// Access environment variables and secrets in top-level scope
const API_KEY = env.API_KEY;
const LOG_LEVEL = env.LOG_LEVEL || "info";

// Use bindings in your actor
const myActor = actor({
state: { count: 0 },

actions: {
// Access KV, D1, or other bindings during request handling
getFromKV: async (c, key: string) => {
// Access additional KV namespaces defined in wrangler.json
if (env.MY_CACHE_KV) {
return await env.MY_CACHE_KV.get(key);
}
}
}
});
```

### Driver Context

The Cloudflare Workers driver provides access to the Durable Object state and environment through the driver context in `createVars`.

```typescript
import { actor, ActorInitContext } from "@rivetkit/actor";
import type { DriverContext } from "@rivetkit/cloudflare-workers";

const myActor = actor({
state: { count: 0 },

// Save the Cloudflare driver context
createVars: (ctx: ActorInitContext, driver: DriverContext) => ({
state: driver.state,
}),

actions: {
// Example: Access Durable Object info (not recommended in practice)
kvGet: (c, key: string) => {
const doState = c.vars.state;
return await doState.storage.get(key)
},
Comment on lines +192 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kvGet function is using await but is not declared as async. This will cause a syntax error. The function signature should be updated to:

kvGet: async (c, key: string) => {
  const doState = c.vars.state;
  return await doState.storage.get(key);
}
Suggested change
kvGet: (c, key: string) => {
const doState = c.vars.state;
return await doState.storage.get(key)
},
kvGet: async (c, key: string) => {
const doState = c.vars.state;
return await doState.storage.get(key)
},

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

}
});
```

The Cloudflare Workers driver context type is exported as `DriverContext` from `@rivetkit/cloudflare-workers`:

```typescript
interface DriverContext {
state: DurableObjectState;
}
```

<Warning>
While you have access to the Durable Object state, be cautious when directly modifying KV storage or alarms, as this may interfere with RivetKit's internal operations and potentially break actor functionality.
</Warning>

Loading