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
8 changes: 3 additions & 5 deletions packages/actor-core/src/driver-helpers/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ToServer } from "@/actor/protocol/message/to-server";

export { type DriverConfig, DriverConfigSchema } from "./config";
export type { ActorInstance, AnyActorInstance } from "@/actor/instance";
export {
Expand All @@ -13,9 +11,9 @@ export {
export { ActorDriver } from "@/actor/driver";
export {
ManagerDriver,
CreateActorInput,
CreateActorOutput,
GetActorOutput,
CreateInput,
GetForIdInput,
GetWithKeyInput,
GetOrCreateWithKeyInput,
ActorOutput,
} from "@/manager/driver";
4 changes: 4 additions & 0 deletions packages/actor-core/src/driver-test-suite/tests/actor-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export function runActorConnTests(driverTestConfig: DriverTestConfig) {
const conn1 = handle1.connect();
const conn2 = handle2.connect();

// HACK: Call an action to wait for the connections to be established
await conn1.getInitializers();
await conn2.getInitializers();

// Get initializers to verify connection params were used
const initializers = await conn1.getInitializers();

Expand Down
19 changes: 10 additions & 9 deletions packages/actor-core/src/manager/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { ManagerInspector } from "@/inspector/manager";
import type { Env, Context as HonoContext } from "hono";

export interface ManagerDriver {
getForId(input: GetForIdInput): Promise<GetActorOutput | undefined>;
getWithKey(input: GetWithKeyInput): Promise<GetActorOutput | undefined>;
createActor(input: CreateActorInput): Promise<CreateActorOutput>;
getForId(input: GetForIdInput): Promise<ActorOutput | undefined>;
getWithKey(input: GetWithKeyInput): Promise<ActorOutput | undefined>;
getOrCreateWithKey(input: GetOrCreateWithKeyInput): Promise<ActorOutput>;
createActor(input: CreateInput): Promise<ActorOutput>;

inspector?: ManagerInspector;
}
Expand All @@ -20,23 +21,23 @@ export interface GetWithKeyInput<E extends Env = any> {
key: ActorKey;
}

export interface GetActorOutput<E extends Env = any> {
export interface GetOrCreateWithKeyInput<E extends Env = any> {
c?: HonoContext<E>;
actorId: string;
name: string;
key: ActorKey;
meta?: unknown;
region?: string;
}

export interface CreateActorInput<E extends Env = any> {
export interface CreateInput<E extends Env = any> {
c?: HonoContext<E>;
name: string;
key: ActorKey;
region?: string;
}

export interface CreateActorOutput {
export interface ActorOutput {
actorId: string;
name: string;
key: ActorKey;
meta?: unknown;
}

25 changes: 7 additions & 18 deletions packages/actor-core/src/manager/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,27 +426,16 @@ export async function queryActor(
}
actorOutput = existingActor;
} else if ("getOrCreateForKey" in query) {
const existingActor = await driver.getWithKey({
const getOrCreateOutput = await driver.getOrCreateWithKey({
c,
name: query.getOrCreateForKey.name,
key: query.getOrCreateForKey.key,
region: query.getOrCreateForKey.region,
});
if (existingActor) {
// Actor exists
actorOutput = existingActor;
} else {
// Create if needed
const createOutput = await driver.createActor({
c,
name: query.getOrCreateForKey.name,
key: query.getOrCreateForKey.key,
region: query.getOrCreateForKey.region,
});
actorOutput = {
actorId: createOutput.actorId,
meta: createOutput.meta,
};
}
actorOutput = {
actorId: getOrCreateOutput.actorId,
meta: getOrCreateOutput.meta,
};
} else if ("create" in query) {
const createOutput = await driver.createActor({
c,
Expand Down Expand Up @@ -737,7 +726,7 @@ async function handleMessageRequest(
);
} else if ("custom" in handler.proxyMode) {
logger().debug("using custom proxy mode for connection message");
const url = new URL(`http://actor/connections/${connId}/message`);
const url = new URL(`http://actor/connections/message`);

const proxyRequest = new Request(url, c.req.raw);
proxyRequest.headers.set(HEADER_ENCODING, encoding);
Expand Down
23 changes: 9 additions & 14 deletions packages/actor-core/src/test/driver/global-state.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import type { ActorKey } from "@/mod";

/**
* Class representing an actor's state
* Interface representing an actor's state
*/
export class ActorState {
// Basic actor information
initialized = true;
export interface ActorState {
id: string;
name: string;
key: ActorKey;

// Persisted data
persistedData: unknown = undefined;

constructor(id: string, name: string, key: ActorKey) {
this.id = id;
this.name = name;
this.key = key;
}
persistedData: unknown;
}

/**
Expand Down Expand Up @@ -45,7 +35,12 @@ export class TestGlobalState {
createActor(actorId: string, name: string, key: ActorKey): void {
// Create actor state if it doesn't exist
if (!this.#actors.has(actorId)) {
this.#actors.set(actorId, new ActorState(actorId, name, key));
this.#actors.set(actorId, {
id: actorId,
name,
key,
persistedData: undefined
});
} else {
throw new Error(`Actor already exists for ID: ${actorId}`);
}
Expand Down
30 changes: 19 additions & 11 deletions packages/actor-core/src/test/driver/manager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type {
CreateActorInput,
CreateActorOutput,
GetActorOutput,
GetForIdInput,
GetWithKeyInput,
GetOrCreateWithKeyInput,
ManagerDriver,
CreateInput,
} from "@/driver-helpers/mod";
import { ActorAlreadyExists } from "@/actor/errors";
import type { TestGlobalState } from "./global-state";
import * as crypto from "node:crypto";
import { ManagerInspector } from "@/inspector/manager";
import type { ActorCoreApp } from "@/app/mod";
import { ActorOutput } from "@/manager/driver";

export class TestManagerDriver implements ManagerDriver {
#state: TestGlobalState;
Expand All @@ -30,9 +30,7 @@ export class TestManagerDriver implements ManagerDriver {
this.#state = state;
}

async getForId({
actorId,
}: GetForIdInput): Promise<GetActorOutput | undefined> {
async getForId({ actorId }: GetForIdInput): Promise<ActorOutput | undefined> {
// Validate the actor exists
const actor = this.#state.getActor(actorId);
if (!actor) {
Expand All @@ -49,7 +47,7 @@ export class TestManagerDriver implements ManagerDriver {
async getWithKey({
name,
key,
}: GetWithKeyInput): Promise<GetActorOutput | undefined> {
}: GetWithKeyInput): Promise<ActorOutput | undefined> {
// NOTE: This is a slow implementation that checks each actor individually.
// This can be optimized with an index in the future.

Expand Down Expand Up @@ -115,10 +113,18 @@ export class TestManagerDriver implements ManagerDriver {
return undefined;
}

async createActor({
name,
key,
}: CreateActorInput): Promise<CreateActorOutput> {
async getOrCreateWithKey(
input: GetOrCreateWithKeyInput,
): Promise<ActorOutput> {
const getOutput = await this.getWithKey(input);
if (getOutput) {
return getOutput;
} else {
return await this.createActor(input);
}
}

async createActor({ name, key }: CreateInput): Promise<ActorOutput> {
// Check if actor with the same name and key already exists
const existingActor = await this.getWithKey({ name, key });
if (existingActor) {
Expand All @@ -132,6 +138,8 @@ export class TestManagerDriver implements ManagerDriver {

return {
actorId,
name,
key,
};
}
}
Loading
Loading