Skip to content

Commit

Permalink
feat: add separate protocol for actor inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
jog1t authored and NathanFlurry committed Jan 28, 2025
1 parent ac2e9de commit 1e1fc74
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export function ActorsActorDetails({
const { data } = useSuspenseQuery(actorQueryOptions(props));
return (
<ActorDetailsSettingsProvider>
<ActorWorkerContextProvider enabled={!data.destroyedAt} {...props}>
<ActorWorkerContextProvider
enabled={!data.destroyedAt}
endpoint={data.endpoint}
{...props}
>
<div className="flex flex-col h-full flex-1 pt-2">
<Tabs
value={tab || "logs"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ActorHandle, Client } from "@rivet-gg/actor-client";
import { ActorHandleRaw } from "@rivet-gg/actor-client";
import type { InspectRpcResponse } from "@rivet-gg/actor-protocol/ws/to_client";
import { fromJs } from "esast-util-from-js";
import { toJs } from "estree-util-to-js";
Expand Down Expand Up @@ -104,14 +104,7 @@ const createConsole = (id: string) => {
);
};

interface InspectableActor {
internal_setState(rpc: this, state: Record<string, unknown>): Promise<void>;
internal_inspect(): Promise<InspectRpcResponse>;
}

let init:
| null
| ({ handle: ActorHandle<InspectableActor> } & InspectRpcResponse);
let init: null | ({ handle: ActorHandleRaw } & InspectRpcResponse);

addEventListener("message", async (event) => {
const { success, data } = MessageSchema.safeParse(event.data);
Expand All @@ -130,11 +123,13 @@ addEventListener("message", async (event) => {
}

try {
const cl = new Client(data.managerUrl);
const handle = await Promise.race([
cl.getWithId<InspectableActor>(data.actorId, {}),
wait(5000).then(() => undefined),
]);
const handle = new ActorHandleRaw(
`${data.endpoint}/__inspect`,
undefined,
"cbor",
);

handle.connect();

if (!handle) {
respond({
Expand All @@ -146,7 +141,7 @@ addEventListener("message", async (event) => {
}

const inspect = await Promise.race([
handle.internal_inspect(),
handle.rpc<[], InspectRpcResponse>("inspect"),
wait(5000).then(() => undefined),
]);

Expand Down Expand Up @@ -208,21 +203,17 @@ addEventListener("message", async (event) => {
data: formatted,
});

const rpcs = Object.fromEntries(
actor.rpcs.map(
(rpc) =>
[
rpc,
actor.handle[rpc as keyof typeof actor.handle],
] as const,
),
const exposedActor = Object.fromEntries(
init?.rpcs.map((rpc) => [
rpc,
actor.handle.rpc.bind(actor.handle, rpc),
]) ?? [],
);

const evaluated = await evaluateCode(data.data, {
console: createConsole(data.id),
wait,
actor: actor.handle,
...rpcs,
actor: exposedActor,
});
return respond({
type: "result",
Expand Down Expand Up @@ -250,7 +241,7 @@ addEventListener("message", async (event) => {

try {
const state = JSON.parse(data.data);
await actor.handle.internal_setState(state);
await actor.handle.rpc("setState", state);
return respond({
type: "state-change",
data: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
actorManagerUrlQueryOptions,
actorQueryOptions,
} from "@/domains/project/queries";
import { actorQueryOptions } from "@/domains/project/queries";
import { queryClient } from "@/queries/global";
import ActorWorker from "./actor-repl.worker?worker";
import {
Expand Down Expand Up @@ -66,11 +63,13 @@ export class ActorWorkerContainer {
projectNameId,
environmentNameId,
actorId,
endpoint,
signal,
}: {
projectNameId: string;
environmentNameId: string;
actorId: string;
endpoint: string;
signal: AbortSignal;
}) {
this.terminate();
Expand All @@ -79,16 +78,6 @@ export class ActorWorkerContainer {
this.#state.status = { type: "pending" };
this.#update();
try {
// To check if the actor is supported, first we need to get the actor manager URL
// if there's no manager URL, we can't support the actor
const managerUrl = await queryClient.fetchQuery(
actorManagerUrlQueryOptions({
projectNameId,
environmentNameId,
}),
);
signal.throwIfAborted();

// If we have the manager URL, next we need to check actor's runtime
const { actor } = await queryClient.fetchQuery(
actorQueryOptions({
Expand Down Expand Up @@ -118,7 +107,7 @@ export class ActorWorkerContainer {
const worker = new ActorWorker({ name: `actor-${actorId}` });
signal.throwIfAborted();
// now worker needs to check if the actor is supported
this.#setupWorker(worker, { actorId, managerUrl });
this.#setupWorker(worker, { actorId, endpoint });
signal.throwIfAborted();
return worker;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface ActorWorkerContextProviderProps {
actorId: string;
projectNameId: string;
environmentNameId: string;
endpoint?: string;
enabled?: boolean;
children: ReactNode;
}
Expand All @@ -32,6 +33,7 @@ export const ActorWorkerContextProvider = ({
children,
actorId,
enabled,
endpoint,
projectNameId,
environmentNameId,
}: ActorWorkerContextProviderProps) => {
Expand All @@ -43,11 +45,12 @@ export const ActorWorkerContextProvider = ({
useEffect(() => {
const ctrl = new AbortController();

if (enabled) {
if (enabled && endpoint) {
container.init({
projectNameId,
environmentNameId,
actorId,
endpoint,
signal: ctrl.signal,
});
}
Expand All @@ -56,7 +59,7 @@ export const ActorWorkerContextProvider = ({
ctrl.abort();
container.terminate();
};
}, [actorId, projectNameId, environmentNameId, enabled]);
}, [actorId, projectNameId, environmentNameId, endpoint, enabled]);

return (
<ActorWorkerContext.Provider value={container}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CodeMessageSchema = z.object({
});
const InitMessageSchema = z.object({
type: z.literal("init"),
managerUrl: z.string(),
endpoint: z.string(),
actorId: z.string(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const actorQueryOptions = ({
(arg) => arg !== "",
),
},
endpoint: createActorEndpoint(data.actor.network),
}),
});
};
Expand Down Expand Up @@ -442,7 +443,9 @@ export const actorRegionQueryOptions = ({
});
};

const createActorEndpoint = (network: Rivet.actor.Network) => {
const createActorEndpoint = (
network: Rivet.actor.Network,
): string | undefined => {
const http = Object.values(network.ports).find(
(port) => port.protocol === "http" || port.protocol === "https",
);
Expand Down
6 changes: 3 additions & 3 deletions frontend/packages/components/src/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
) => {
const C = asChild ? Slot : "button";

const isIcon = size?.includes("icon");

return (
<C
className={cn(
Expand All @@ -93,9 +95,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
) : startIcon ? (
React.cloneElement(startIcon, startIcon.props)
) : null}
{!size?.includes("icon") && isLoading ? null : (
<Slottable>{children}</Slottable>
)}
{isIcon && isLoading ? null : <Slottable>{children}</Slottable>}
{endIcon ? React.cloneElement(endIcon, endIcon.props) : null}
</C>
);
Expand Down
Loading

0 comments on commit 1e1fc74

Please sign in to comment.