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
2 changes: 2 additions & 0 deletions frontend/src/app/data-providers/inspector-data-provider.tsx

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

12 changes: 5 additions & 7 deletions rivetkit-typescript/packages/rivetkit/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class Runtime<A extends RegistryActors> {
configureDefaultLogger(config.logging?.level);
}

// This should be unreachable: Zod defaults serveManager to false when
// spawnEngine is enabled (since endpoint gets set to ENGINE_ENDPOINT).
// We check anyway as a safety net for explicit misconfiguration.
invariant(
!(config.serverless.spawnEngine && config.serveManager),
"cannot specify both spawnEngine and serveManager",
Expand All @@ -90,11 +93,6 @@ export class Runtime<A extends RegistryActors> {
const driver = chooseDefaultDriver(config);
const managerDriver = driver.manager(config);

invariant(
!(config.serverless.spawnEngine && config.serveManager),
"cannot specify spawnEngine and serveManager together",
);

// Start main server. This is either:
// - Manager: Run a server in-process on port 6420 that mimics the
// engine's API for development.
Expand Down Expand Up @@ -215,11 +213,11 @@ export class Runtime<A extends RegistryActors> {
this.#config.serveManager ||
this.#config.serverless.spawnEngine;
if (
this.#config.serverless.advertiseEndpoint &&
this.#config.publicEndpoint &&
shouldShowEndpoint
) {
console.log(
` - Endpoint: ${this.#config.serverless.advertiseEndpoint}`,
` - Endpoint: ${this.#config.publicEndpoint}`,
);
}
if (this.#config.serverless.spawnEngine) {
Expand Down
102 changes: 54 additions & 48 deletions rivetkit-typescript/packages/rivetkit/src/client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,59 @@ import {
getRivetRunner,
} from "@/utils/env-vars";
import type { RegistryConfig } from "@/registry/config";
import {
EndpointSchema,
type ParsedEndpoint,
zodCheckDuplicateCredentials,
} from "@/utils/endpoint-parser";
import { tryParseEndpoint } from "@/utils/endpoint-parser";

/**
* Gets the default endpoint for the client.
*
* In browser: uses current origin + /api/rivet
*
* Server-side: uses localhost:6420
*/
function getDefaultEndpoint(): string {
if (typeof window !== "undefined" && window.location?.origin) {
return `${window.location.origin}/api/rivet`;
}
return "http://127.0.0.1:6420";
}

/**
* Base client config schema without transforms so it can be merged in to other schemas.
*/
export const ClientConfigSchemaBase = z.object({
/** Endpoint to connect to for Rivet Engine or RivetKit manager API. */
endpoint: EndpointSchema.optional(),
/**
* Endpoint to connect to for Rivet Engine or RivetKit manager API.
*
* Supports URL auth syntax for namespace and token:
* - `https://namespace:token@api.rivet.dev`
* - `https://namespace@api.rivet.dev`
*
* Can also be set via RIVET_ENDPOINT environment variables.
*
* Defaults to current origin + /api/rivet in browser, or localhost:6420 server-side.
*/
endpoint: z
.string()
.optional()
.transform(
(val) =>
val ??
getRivetEngine() ??
getRivetEndpoint() ??
getDefaultEndpoint(),
),

/** Token to use to authenticate with the API. */
token: z.string().optional(),
token: z
.string()
.optional()
.transform((val) => val ?? getRivetToken()),

/** Namespace to connect to. */
namespace: z.string().optional(),
namespace: z
.string()
.optional()
.transform((val) => val ?? getRivetNamespace()),

/** Name of the runner. This is used to group together runners in to different pools. */
runnerName: z.string().default(() => getRivetRunner() ?? "default"),
Expand Down Expand Up @@ -65,51 +100,22 @@ export type ClientConfig = z.infer<typeof ClientConfigSchema>;

export type ClientConfigInput = z.input<typeof ClientConfigSchema>;

export function resolveEndpoint(
parsedEndpoint: ParsedEndpoint | undefined,
): ParsedEndpoint | undefined {
if (parsedEndpoint) {
return parsedEndpoint;
}

const envEndpoint = getRivetEngine() ?? getRivetEndpoint();
if (envEndpoint) {
return EndpointSchema.parse(envEndpoint);
}

return undefined;
}

export function validateClientConfig(
resolvedEndpoint: ParsedEndpoint | undefined,
config: z.infer<typeof ClientConfigSchemaBase>,
ctx: z.RefinementCtx,
) {
if (resolvedEndpoint) {
zodCheckDuplicateCredentials(resolvedEndpoint, config, ctx);
}
}

export function transformClientConfig(
config: z.infer<typeof ClientConfigSchemaBase>,
ctx?: z.RefinementCtx,
ctx: z.RefinementCtx,
) {
const resolvedEndpoint = resolveEndpoint(config.endpoint);

// Validate if context is provided (when called from Zod transform)
if (ctx) {
validateClientConfig(resolvedEndpoint, config, ctx);
}
const parsedEndpoint = tryParseEndpoint(ctx, {
endpoint: config.endpoint,
path: ["endpoint"],
namespace: config.namespace,
token: config.token,
});

return {
...config,
endpoint: resolvedEndpoint?.endpoint,
namespace:
resolvedEndpoint?.namespace ??
config.namespace ??
getRivetNamespace() ??
"default",
token: resolvedEndpoint?.token ?? config.token ?? getRivetToken(),
endpoint: parsedEndpoint?.endpoint,
namespace: parsedEndpoint?.namespace ?? config.namespace ?? "default",
token: parsedEndpoint?.token ?? config.token,
};
}

Expand Down
12 changes: 12 additions & 0 deletions rivetkit-typescript/packages/rivetkit/src/common/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,23 @@ export interface MetadataResponse {
* This is also helpful for setting up clean redirects as needed.
**/
clientEndpoint?: string;
/**
* Namespace that the client should use when connecting.
**/
clientNamespace?: string;
/**
* Token that the client should use when connecting.
**/
clientToken?: string;
}

export function handleMetadataRequest(
c: HonoContext,
config: RegistryConfig,
runnerKind: MetadataRunnerKind,
clientEndpoint: string | undefined,
clientNamespace: string | undefined,
clientToken: string | undefined,
) {
const response: MetadataResponse = {
runtime: "rivetkit",
Expand All @@ -146,6 +156,8 @@ export function handleMetadataRequest(
},
actorNames: buildActorNames(config),
clientEndpoint,
clientNamespace,
clientToken,
};

return c.json(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { getRivetRunnerKey } from "@/utils/env-vars";
*/
export const EngineConfigSchemaBase = ClientConfigSchemaBase.extend({
/** Unique key for this runner. Runners connecting a given key will replace any other runner connected with the same key. */
runnerKey: z.string().optional(),
runnerKey: z
.string()
.optional()
.transform((val) => val ?? getRivetRunnerKey()),

/** How many actors this runner can run. */
totalSlots: z.number().default(100_000),
Expand All @@ -31,10 +34,10 @@ export type EngineConfigInput = z.input<typeof EngineConfigSchema>;

export function transformEngineConfig(
config: z.infer<typeof EngineConfigSchemaBase>,
ctx?: z.RefinementCtx,
ctx: z.RefinementCtx,
) {
return {
...transformClientConfig(config, ctx),
runnerKey: config.runnerKey ?? getRivetRunnerKey(),
runnerKey: config.runnerKey,
};
}
1 change: 1 addition & 0 deletions rivetkit-typescript/packages/rivetkit/src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare global {
interface Window {
location?: {
hostname?: string;
origin?: string;
};
__rivetkit?: unknown[];
}
Expand Down
9 changes: 8 additions & 1 deletion rivetkit-typescript/packages/rivetkit/src/manager/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,14 @@ export function buildManagerRouter(
router.get("/health", (c) => handleHealthRequest(c));

router.get("/metadata", (c) =>
handleMetadataRequest(c, config, { normal: {} }, undefined),
handleMetadataRequest(
c,
config,
{ normal: {} },
config.publicEndpoint,
config.publicNamespace,
config.publicToken,
),
);

managerDriver.modifyManagerRouter?.(config, router as unknown as Hono);
Expand Down
Loading
Loading