Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ const dynamicFromUrl = dynamicActor(async () => {
});

const dynamicFromActor = dynamicActor(async (c) => {
const source = await c
const source = (await c
.client<any>()
.sourceCode.getOrCreate(["dynamic-source"])
.getCode();
.getCode()) as string;
return {
source,
nodeProcess: {
Expand Down
108 changes: 66 additions & 42 deletions rivetkit-typescript/packages/rivetkit/src/actor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,71 @@ export function getRunInspectorConfig(
// We don't use Zod generics with `z.custom` because:
// (a) there seems to be a weird bug in either Zod, tsup, or TSC that causese external packages to have different types from `z.infer` than from within the same package and
// (b) it makes the type definitions incredibly difficult to read as opposed to vanilla TypeScript.
const GlobalActorOptionsBaseSchema = z
.object({
/** Display name for the actor in the Inspector UI. */
name: z.string().optional(),
/** Icon for the actor in the Inspector UI. Can be an emoji or FontAwesome icon name. */
icon: z.string().optional(),
/**
* Can hibernate WebSockets for onWebSocket.
*
* WebSockets using actions/events are hibernatable by default.
*
* @experimental
**/
canHibernateWebSocket: z
.union([z.boolean(), zFunction<(request: Request) => boolean>()])
.default(false),
})
.strict();

export const GlobalActorOptionsSchema = GlobalActorOptionsBaseSchema.prefault(
() => ({}),
);

export type GlobalActorOptions = z.infer<typeof GlobalActorOptionsSchema>;
export type GlobalActorOptionsInput = z.input<typeof GlobalActorOptionsSchema>;

const InstanceActorOptionsBaseSchema = z
.object({
createVarsTimeout: z.number().positive().default(5000),
createConnStateTimeout: z.number().positive().default(5000),
onConnectTimeout: z.number().positive().default(5000),
onSleepTimeout: z.number().positive().default(5000),
onDestroyTimeout: z.number().positive().default(5000),
stateSaveInterval: z.number().positive().default(10_000),
actionTimeout: z.number().positive().default(60_000),
// Max time to wait for waitUntil background promises during shutdown
waitUntilTimeout: z.number().positive().default(15_000),
// Max time to wait for run handler to stop during shutdown
runStopTimeout: z.number().positive().default(15_000),
connectionLivenessTimeout: z.number().positive().default(2500),
connectionLivenessInterval: z.number().positive().default(5000),
noSleep: z.boolean().default(false),
sleepTimeout: z.number().positive().default(30_000),
maxQueueSize: z.number().positive().default(1000),
maxQueueMessageSize: z.number().positive().default(64 * 1024),
})
.strict();

export const InstanceActorOptionsSchema =
InstanceActorOptionsBaseSchema.prefault(() => ({}));

export type InstanceActorOptions = z.infer<typeof InstanceActorOptionsSchema>;
export type InstanceActorOptionsInput = z.input<
typeof InstanceActorOptionsSchema
>;

export const ActorOptionsSchema = GlobalActorOptionsBaseSchema.extend(
InstanceActorOptionsBaseSchema.shape,
)
.strict()
.prefault(() => ({}));

export type ActorOptions = z.infer<typeof ActorOptionsSchema>;
export type ActorOptionsInput = z.input<typeof ActorOptionsSchema>;

export const ActorConfigSchema = z
.object({
onCreate: zFunction().optional(),
Expand All @@ -199,48 +264,7 @@ export const ActorConfigSchema = z
vars: z.any().optional(),
db: z.any().optional(),
createVars: zFunction().optional(),
options: z
.object({
/** Display name for the actor in the Inspector UI. */
name: z.string().optional(),
/** Icon for the actor in the Inspector UI. Can be an emoji or FontAwesome icon name. */
icon: z.string().optional(),
createVarsTimeout: z.number().positive().default(5000),
createConnStateTimeout: z.number().positive().default(5000),
onConnectTimeout: z.number().positive().default(5000),
onSleepTimeout: z.number().positive().default(5000),
onDestroyTimeout: z.number().positive().default(5000),
stateSaveInterval: z.number().positive().default(10_000),
actionTimeout: z.number().positive().default(60_000),
// Max time to wait for waitUntil background promises during shutdown
waitUntilTimeout: z.number().positive().default(15_000),
// Max time to wait for run handler to stop during shutdown
runStopTimeout: z.number().positive().default(15_000),
connectionLivenessTimeout: z.number().positive().default(2500),
connectionLivenessInterval: z.number().positive().default(5000),
noSleep: z.boolean().default(false),
sleepTimeout: z.number().positive().default(30_000),
maxQueueSize: z.number().positive().default(1000),
maxQueueMessageSize: z
.number()
.positive()
.default(64 * 1024),
/**
* Can hibernate WebSockets for onWebSocket.
*
* WebSockets using actions/events are hibernatable by default.
*
* @experimental
**/
canHibernateWebSocket: z
.union([
z.boolean(),
zFunction<(request: Request) => boolean>(),
])
.default(false),
})
.strict()
.prefault(() => ({})),
options: ActorOptionsSchema,
})
.strict()
.refine(
Expand Down
10 changes: 5 additions & 5 deletions rivetkit-typescript/packages/rivetkit/src/actor/conn/driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnyConn } from "@/actor/conn/mod";
import type { AnyActorInstance } from "@/actor/instance/mod";
import type { AnyStaticActorInstance } from "@/actor/instance/mod";
import type { CachedSerializer } from "@/actor/protocol/serde";

export enum DriverReadyState {
Expand All @@ -24,7 +24,7 @@ export interface ConnDriver {
rivetKitProtocol?: {
/** Sends a RivetKit client message. */
sendMessage(
actor: AnyActorInstance,
actor: AnyStaticActorInstance,
conn: AnyConn,
message: CachedSerializer<any, any, any>,
): void;
Expand All @@ -42,20 +42,20 @@ export interface ConnDriver {
* This returns a promise since we commonly disconnect at the end of a program, and not waiting will cause the socket to not close cleanly.
*/
disconnect(
actor: AnyActorInstance,
actor: AnyStaticActorInstance,
conn: AnyConn,
reason?: string,
): Promise<void>;

/** Terminates the connection without graceful handling. */
terminate?(actor: AnyActorInstance, conn: AnyConn): void;
terminate?(actor: AnyStaticActorInstance, conn: AnyConn): void;

/**
* Returns the ready state of the connection.
* This is used to determine if the connection is ready to send messages, or if the connection is stale.
*/
getConnectionReadyState(
actor: AnyActorInstance,
actor: AnyStaticActorInstance,
conn: AnyConn,
): DriverReadyState | undefined;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnyConn } from "@/actor/conn/mod";
import type { AnyActorInstance } from "@/actor/instance/mod";
import type { AnyStaticActorInstance } from "@/actor/instance/mod";
import type { UniversalWebSocket } from "@/common/websocket-interface";
import { loggerWithoutContext } from "../../log";
import { type ConnDriver, DriverReadyState } from "../driver";
Expand All @@ -26,7 +26,7 @@ export function createRawWebSocketDriver(
// handle messages from the RivetKit protocol

disconnect: async (
_actor: AnyActorInstance,
_actor: AnyStaticActorInstance,
_conn: AnyConn,
reason?: string,
) => {
Expand All @@ -49,7 +49,7 @@ export function createRawWebSocketDriver(
},

getConnectionReadyState: (
_actor: AnyActorInstance,
_actor: AnyStaticActorInstance,
_conn: AnyConn,
): DriverReadyState | undefined => {
return websocket?.readyState ?? DriverReadyState.CONNECTING;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WSContext } from "hono/ws";
import type { AnyConn } from "@/actor/conn/mod";
import type { AnyActorInstance } from "@/actor/instance/mod";
import type { AnyStaticActorInstance } from "@/actor/instance/mod";
import type { CachedSerializer, Encoding } from "@/actor/protocol/serde";
import * as errors from "@/actor/errors";
import { loggerWithoutContext } from "../../log";
Expand All @@ -27,7 +27,7 @@ export function createWebSocketDriver(
hibernatable,
rivetKitProtocol: {
sendMessage: (
actor: AnyActorInstance,
actor: AnyStaticActorInstance,
conn: AnyConn,
message: CachedSerializer<any, any, any>,
) => {
Expand Down Expand Up @@ -105,7 +105,7 @@ export function createWebSocketDriver(
},

disconnect: async (
_actor: AnyActorInstance,
_actor: AnyStaticActorInstance,
_conn: AnyConn,
reason?: string,
) => {
Expand All @@ -128,7 +128,7 @@ export function createWebSocketDriver(
},

getConnectionReadyState: (
_actor: AnyActorInstance,
_actor: AnyStaticActorInstance,
_conn: AnyConn,
): DriverReadyState | undefined => {
return websocket?.readyState ?? DriverReadyState.CONNECTING;
Expand Down
8 changes: 4 additions & 4 deletions rivetkit-typescript/packages/rivetkit/src/actor/conn/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { bufferToArrayBuffer } from "@/utils";
import type { AnyDatabaseProvider } from "../database";
import { EventPayloadInvalid, InternalError } from "../errors";
import type { ActorInstance } from "../instance/mod";
import type { StaticActorInstance } from "../instance/mod";
import { CachedSerializer } from "../protocol/serde";
import {
type EventSchemaConfig,
Expand Down Expand Up @@ -52,9 +52,9 @@ export class Conn<
E extends EventSchemaConfig = Record<never, never>,
Q extends QueueSchemaConfig = Record<never, never>,
> {
#actor: ActorInstance<S, CP, CS, V, I, DB, E, Q>;
#actor: StaticActorInstance<S, CP, CS, V, I, DB, E, Q>;

get [CONN_ACTOR_SYMBOL](): ActorInstance<S, CP, CS, V, I, DB, E, Q> {
get [CONN_ACTOR_SYMBOL](): StaticActorInstance<S, CP, CS, V, I, DB, E, Q> {
return this.#actor;
}

Expand Down Expand Up @@ -139,7 +139,7 @@ export class Conn<
* @protected
*/
constructor(
actor: ActorInstance<S, CP, CS, V, I, DB, E, Q>,
actor: StaticActorInstance<S, CP, CS, V, I, DB, E, Q>,
data: ConnDataInput<CP, CS>,
) {
this.#actor = actor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { ActorDefinition, AnyActorDefinition } from "../../definition";
import * as errors from "../../errors";
import { ActorKv } from "../../instance/kv";
import type {
ActorInstance,
AnyActorInstance,
StaticActorInstance,
AnyStaticActorInstance,
SaveStateOptions,
} from "../../instance/mod";
import { ActorQueue } from "../../instance/queue";
Expand Down Expand Up @@ -40,8 +40,8 @@ export class ActorContext<
TEvents extends EventSchemaConfig = Record<never, never>,
TQueues extends QueueSchemaConfig = Record<never, never>,
> {
[ACTOR_CONTEXT_INTERNAL_SYMBOL]!: AnyActorInstance;
#actor: ActorInstance<
[ACTOR_CONTEXT_INTERNAL_SYMBOL]!: AnyStaticActorInstance;
#actor: StaticActorInstance<
TState,
TConnParams,
TConnState,
Expand All @@ -66,7 +66,7 @@ export class ActorContext<
| undefined;

constructor(
actor: ActorInstance<
actor: StaticActorInstance<
TState,
TConnParams,
TConnState,
Expand All @@ -78,7 +78,7 @@ export class ActorContext<
>,
) {
this.#actor = actor;
this[ACTOR_CONTEXT_INTERNAL_SYMBOL] = actor as AnyActorInstance;
this[ACTOR_CONTEXT_INTERNAL_SYMBOL] = actor as AnyStaticActorInstance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyDatabaseProvider } from "../../database";
import type { ActorDefinition, AnyActorDefinition } from "../../definition";
import type { ActorInstance } from "../../instance/mod";
import type { StaticActorInstance } from "../../instance/mod";
import type { EventSchemaConfig, QueueSchemaConfig } from "../../schema";
import { ActorContext } from "./actor";

Expand Down Expand Up @@ -35,7 +35,7 @@ export abstract class ConnInitContext<
* @internal
*/
constructor(
actor: ActorInstance<
actor: StaticActorInstance<
TState,
any,
any,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Conn } from "../../conn/mod";
import type { AnyDatabaseProvider } from "../../database";
import type { ActorDefinition, AnyActorDefinition } from "../../definition";
import type { ActorInstance } from "../../instance/mod";
import type { StaticActorInstance } from "../../instance/mod";
import type { EventSchemaConfig, QueueSchemaConfig } from "../../schema";
import { ActorContext } from "./actor";

Expand Down Expand Up @@ -32,7 +32,7 @@ export abstract class ConnContext<
* @internal
*/
constructor(
actor: ActorInstance<
actor: StaticActorInstance<
TState,
TConnParams,
TConnState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Conn } from "../conn/mod";
import type { AnyDatabaseProvider } from "../database";
import type { ActorDefinition, AnyActorDefinition } from "../definition";
import type { ActorInstance } from "../instance/mod";
import type { StaticActorInstance } from "../instance/mod";
import type { EventSchemaConfig, QueueSchemaConfig } from "../schema";
import { ConnContext } from "./base/conn";

Expand Down Expand Up @@ -37,7 +37,7 @@ export class RequestContext<
* @internal
*/
constructor(
actor: ActorInstance<
actor: StaticActorInstance<
TState,
TConnParams,
TConnState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Conn } from "../conn/mod";
import type { AnyDatabaseProvider } from "../database";
import type { ActorDefinition, AnyActorDefinition } from "../definition";
import type { ActorInstance } from "../instance/mod";
import type { StaticActorInstance } from "../instance/mod";
import type { EventSchemaConfig, QueueSchemaConfig } from "../schema";
import { ConnContext } from "./base/conn";

Expand Down Expand Up @@ -37,7 +37,7 @@ export class WebSocketContext<
* @internal
*/
constructor(
actor: ActorInstance<
actor: StaticActorInstance<
TState,
TConnParams,
TConnState,
Expand Down
Loading
Loading