Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
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
2 changes: 1 addition & 1 deletion examples/cloudflare-workers/src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActorContext, actor, setup } from "@rivetkit/actor";
import { actor, setup } from "@rivetkit/actor";

export const counter = actor({
onAuth: () => {
Expand Down
21 changes: 21 additions & 0 deletions internal-docs/CONFIG_TYPES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Config Types

## Inferred types

- All types must be included in `ActorTypes` so the user can hardcode types

- If using input parameters for inferring types, they must be raw parameters. e.g.:

```typescript
// It's hard for users to infer TConnParams
onAuth: (opts: OnAuthOpts<TConnParams>) => TAuthData,
// Because you would have to import & use an extra type
onAuth: (opts: OnAuthOpts<MyConnParam>) => TAuthData,
```

- When inferring via return data, you must use a union. e.g.:

```typescript
{ state: TState } | { createState: () => TState } | undefined
```

4 changes: 2 additions & 2 deletions packages/core/fixtures/driver-test-suite/action-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export interface State {
// Test actor that can capture input during creation
export const inputActor = actor({
onAuth: () => {},
createState: (c, { input }): State => {
createState: (c, input): State => {
return {
initialInput: input,
onCreateInput: undefined,
};
},

onCreate: (c, { input }) => {
onCreate: (c, input) => {
c.state.onCreateInput = input;
},

Expand Down
10 changes: 3 additions & 7 deletions packages/core/fixtures/driver-test-suite/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { actor, UserError } from "@rivetkit/core";
// Basic auth actor - requires API key
export const authActor = actor({
state: { requests: 0 },
onAuth: (opts) => {
const { request, intents, params } = opts;
onAuth: (params) => {
const apiKey = (params as any)?.apiKey;
if (!apiKey) {
throw new UserError("API key required", { code: "missing_auth" });
Expand All @@ -28,8 +27,7 @@ export const authActor = actor({
// Intent-specific auth actor - checks different permissions for different intents
export const intentAuthActor = actor({
state: { value: 0 },
onAuth: (opts) => {
const { request, intents, params } = opts;
onAuth: (params, { request, intents }) => {
console.log("intents", intents, params);
const role = (params as any)?.role;

Expand Down Expand Up @@ -82,9 +80,7 @@ export const noAuthActor = actor({
// Async auth actor - tests promise-based authentication
export const asyncAuthActor = actor({
state: { count: 0 },
onAuth: async (opts) => {
const { params } = opts;

onAuth: async (params) => {
const token = (params as any)?.token;
if (!token) {
throw new UserError("Token required", { code: "missing_token" });
Expand Down
11 changes: 5 additions & 6 deletions packages/core/fixtures/driver-test-suite/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { actor } from "@rivetkit/core";

type ConnParams = { trackLifecycle?: boolean } | undefined;

export const counterWithLifecycle = actor({
onAuth: () => {},
state: {
count: 0,
events: [] as string[],
},
createConnState: (
c,
opts: { params: { trackLifecycle?: boolean } | undefined },
) => ({
createConnState: (c, params: ConnParams) => ({
joinTime: Date.now(),
}),
onStart: (c) => {
c.state.events.push("onStart");
},
onBeforeConnect: (c, conn) => {
if (conn.params?.trackLifecycle) c.state.events.push("onBeforeConnect");
onBeforeConnect: (c, params) => {
if (params?.trackLifecycle) c.state.events.push("onBeforeConnect");
},
onConnect: (c, conn) => {
if (conn.params?.trackLifecycle) c.state.events.push("onConnect");
Expand Down
3 changes: 1 addition & 2 deletions packages/core/fixtures/driver-test-suite/raw-http-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export const rawHttpAuthActor = actor({
state: {
requestCount: 0,
},
onAuth: (opts) => {
const { params } = opts;
onAuth: (params) => {
const apiKey = (params as any)?.apiKey;
if (!apiKey) {
throw new UserError("API key required", { code: "missing_auth" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export const rawWebSocketAuthActor = actor({
connectionCount: 0,
messageCount: 0,
},
onAuth: (opts) => {
const { params } = opts;
onAuth: (params) => {
const apiKey = (params as any)?.apiKey;
if (!apiKey) {
throw new UserError("API key required", { code: "missing_auth" });
Expand Down
4 changes: 2 additions & 2 deletions packages/core/fixtures/driver-test-suite/raw-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export const rawWebSocketActor = actor({
connectionCount: 0,
messageCount: 0,
},
onAuth(opts) {
onAuth(params) {
// Allow all connections and pass through connection params
return { connParams: opts.params };
return { connParams: params };
},
onWebSocket(ctx, websocket, opts) {
ctx.state.connectionCount = ctx.state.connectionCount + 1;
Expand Down
55 changes: 41 additions & 14 deletions packages/core/src/actor/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ import type { Schedule } from "./schedule";
* @typeParam A Actor this action belongs to
*/
export class ActionContext<
S,
CP,
CS,
V,
I,
AD,
DB extends AnyDatabaseProvider,
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase extends AnyDatabaseProvider,
> {
#actorContext: ActorContext<S, CP, CS, V, I, AD, DB>;
#actorContext: ActorContext<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase
>;

/**
* Should not be called directly.
Expand All @@ -31,23 +39,39 @@ export class ActionContext<
* @param conn - The connection associated with the action
*/
constructor(
actorContext: ActorContext<S, CP, CS, V, I, AD, DB>,
public readonly conn: Conn<S, CP, CS, V, I, AD, DB>,
actorContext: ActorContext<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase
>,
public readonly conn: Conn<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TAuthData,
TDatabase
>,
) {
this.#actorContext = actorContext;
}

/**
* Get the actor state
*/
get state(): S {
get state(): TState {
return this.#actorContext.state;
}

/**
* Get the actor variables
*/
get vars(): V {
get vars(): TVars {
return this.#actorContext.vars;
}

Expand Down Expand Up @@ -103,7 +127,10 @@ export class ActionContext<
/**
* Gets the map of connections.
*/
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD, DB>> {
get conns(): Map<
ConnId,
Conn<TState, TConnParams, TConnState, TVars, TInput, TAuthData, TDatabase>
> {
return this.#actorContext.conns;
}

Expand All @@ -117,7 +144,7 @@ export class ActionContext<
/**
* @experimental
*/
get db(): InferDatabaseClient<DB> {
get db(): InferDatabaseClient<TDatabase> {
return this.#actorContext.db;
}

Expand Down
Loading
Loading