Skip to content
Draft
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Rivet Dynamic Apps

> [!WARNING]
> Dynamic Apps currently supports only Rivet 2.x. Rivet 3.x support is coming
> soon.
> Dynamic Apps requires Rivet 3.x. Apps that use RivetKit run as an always-on
> envoy per app and do not scale to zero.

Build user-generated request handlers in a sandboxed deployment VM, keep their
release state in a Rivet actor, and serve cache-hit requests from bounded local
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/dynamic-apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"@rivet-dev/dynamic-apps": "workspace:*",
"@rivet-dev/dynamic-apps-core": "workspace:*",
"hono": "^4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
},
"devDependencies": {
"@rivetkit/engine-cli": "2.3.29",
"@rivetkit/engine-cli": "3.0.2",
"@types/node": "^22.19.15",
"get-port": "^7.1.0",
"tsx": "^4.20.6",
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/dynamic-apps/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function deployActorBenchmarkFixture(
main: "index.js",
dependencies: {
hono: "4.13.5",
rivetkit: "2.3.29",
rivetkit: "3.0.2",
},
}),
"index.js": `
Expand Down Expand Up @@ -100,9 +100,8 @@ const counter = actor({
},
});

const registry = setup({ use: { counter } });
export const registry = setup({ use: { counter } });
const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));
app.all("*", () => Response.json({ ok: true, workload: "actor-and-direct-http" }));
export default app;
`,
Expand Down
3 changes: 2 additions & 1 deletion docs/content/docs/connect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Each app gets its own Rivet namespace, which provides it:

- Complete per-tenant isolation
- Per-user billing
- Infrastructure that costs nothing when idle
- Its own node pool. Apps that define actors keep one agentOS VM running while
deployed, so they do not scale to zero.

## Connecting to Rivet

Expand Down
8 changes: 6 additions & 2 deletions docs/content/docs/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ skill: true

## Getting started

Mount it at any prefix and forward the private `/api/rivet/*` callback to it:
Mount it at any prefix. Forwarding `/api/rivet/*` to it exposes the host's
Rivet health, metadata, and metrics routes:

```ts
import { appsRouter } from "@rivet-dev/dynamic-apps";
import { appsRouter, startDynamicAppsHost } from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();

server.all("/api/rivet/*", (c) => appsRouter.fetch(c.req.raw));
server.route("/apps", appsRouter);

// Start running deployed apps' actors when the host boots.
await startDynamicAppsHost();
```

A request to `/apps/example/api/items` reaches that app's handler as
Expand Down
2 changes: 1 addition & 1 deletion docs/content/guides/host-ai-generated-apps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ The server has a route that deploys apps and a router that serves them:

- `POST /deploy/:name` checks that the prompt is a string of at most 4,000 characters, then returns what `deployApp` returns: the app's release, endpoint, namespace, pool, and a token when one is needed.
- `server.route("/apps", appsRouter)` serves every app under its own path. A request to `/apps/team-board/api/items` reaches the `team-board` app's fetch handler as `/api/items`. See [Route](/dynamic-apps/docs/routing).
- `/api/rivet/*` is a private callback that lets Rivet reach your server to manage deployments. Forward it to `appsRouter` as shown.
- `/api/rivet/*` exposes the host's Rivet health, metadata, and metrics routes. Forward it to `appsRouter` as shown.
- Requests run in a cached agentOS VM inside the same process, so there is no extra network hop between your server and the app.

<Note>
Expand Down
4 changes: 4 additions & 0 deletions examples/apps-ai-builder/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
appsRouter,
deployApp,
rivetActorsSkill,
startDynamicAppsHost,
webServerSkill,
} from "@rivet-dev/dynamic-apps";
import { generateObject } from "ai";
Expand Down Expand Up @@ -133,6 +134,9 @@ server.post("/deploy/:name", async (context) => {
server.route("/apps", appsRouter);
// docs:end routes

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

serve({
fetch: server.fetch,
port: 3000,
Expand Down
7 changes: 5 additions & 2 deletions examples/apps-hello-world/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { serve } from "@hono/node-server";
import { appsRouter } from "@rivet-dev/dynamic-apps";
import { appsRouter, startDynamicAppsHost } from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();

// This is how the Rivet control plane communicates with your backend.
// Exposes the host's Rivet health, metadata, and metrics routes.
server.all("/api/rivet/*", (c) => appsRouter.fetch(c.req.raw));

// Mount every deployed application at /apps/:appId.
server.route("/apps", appsRouter);

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

// Serve the host router over HTTP.
serve({
fetch: server.fetch,
Expand Down
2 changes: 1 addition & 1 deletion examples/apps-multiplayer/fixtures/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"main": "src/index.ts",
"dependencies": {
"hono": "4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
}
}
1 change: 0 additions & 1 deletion examples/apps-multiplayer/fixtures/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const room = actor({
export const registry = setup({ use: { room } });

const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));
app.get("/", (c) =>
c.json({ message: "Use the RivetKit client to join a room." }),
);
Expand Down
2 changes: 1 addition & 1 deletion examples/apps-multiplayer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@hono/node-server": "^2.1.1",
"@rivet-dev/dynamic-apps": "workspace:*",
"hono": "^4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
},
"devDependencies": {
"@types/node": "^22.20.1",
Expand Down
9 changes: 8 additions & 1 deletion examples/apps-multiplayer/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { serve } from "@hono/node-server";
import { appsRouter, deployApp } from "@rivet-dev/dynamic-apps";
import {
appsRouter,
deployApp,
startDynamicAppsHost,
} from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();
Expand All @@ -18,4 +22,7 @@ server.post("/deploy/:name", async (c) =>

server.route("/apps", appsRouter);

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

serve({ fetch: server.fetch, port: Number(process.env.PORT ?? 3000) });
2 changes: 1 addition & 1 deletion examples/apps-sqlite/fixtures/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"main": "src/index.ts",
"dependencies": {
"hono": "4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
}
}
1 change: 0 additions & 1 deletion examples/apps-sqlite/fixtures/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const notes = actor({
export const registry = setup({ use: { notes } });

const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));
app.get("/", (c) =>
c.json({ message: "Use the RivetKit client to add notes." }),
);
Expand Down
2 changes: 1 addition & 1 deletion examples/apps-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@hono/node-server": "^2.1.1",
"@rivet-dev/dynamic-apps": "workspace:*",
"hono": "^4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
},
"devDependencies": {
"@types/node": "^22.20.1",
Expand Down
9 changes: 8 additions & 1 deletion examples/apps-sqlite/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { serve } from "@hono/node-server";
import { appsRouter, deployApp } from "@rivet-dev/dynamic-apps";
import {
appsRouter,
deployApp,
startDynamicAppsHost,
} from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();
Expand All @@ -18,4 +22,7 @@ server.post("/deploy/:name", async (c) =>

server.route("/apps", appsRouter);

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

serve({ fetch: server.fetch, port: Number(process.env.PORT ?? 3000) });
9 changes: 8 additions & 1 deletion examples/apps-static-website/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { serve } from "@hono/node-server";
import { appsRouter, deployApp } from "@rivet-dev/dynamic-apps";
import {
appsRouter,
deployApp,
startDynamicAppsHost,
} from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();
Expand All @@ -14,5 +18,8 @@ await deployApp({
});
// docs:end deploy

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

serve({ fetch: server.fetch, port: Number(process.env.PORT ?? 3000) });
console.log("Open http://localhost:3000/apps/static-website/");
2 changes: 1 addition & 1 deletion examples/apps-workflows/fixtures/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"main": "src/index.ts",
"dependencies": {
"hono": "4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
}
}
1 change: 0 additions & 1 deletion examples/apps-workflows/fixtures/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const order = actor({
export const registry = setup({ use: { order } });

const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));
app.get("/", (c) =>
c.json({ message: "Use the RivetKit client to read orders." }),
);
Expand Down
2 changes: 1 addition & 1 deletion examples/apps-workflows/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@hono/node-server": "^2.1.1",
"@rivet-dev/dynamic-apps": "workspace:*",
"hono": "^4.13.5",
"rivetkit": "2.3.29"
"rivetkit": "3.0.2"
},
"devDependencies": {
"@types/node": "^22.20.1",
Expand Down
9 changes: 8 additions & 1 deletion examples/apps-workflows/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { serve } from "@hono/node-server";
import { appsRouter, deployApp } from "@rivet-dev/dynamic-apps";
import {
appsRouter,
deployApp,
startDynamicAppsHost,
} from "@rivet-dev/dynamic-apps";
import { Hono } from "hono";

const server = new Hono();
Expand All @@ -18,4 +22,7 @@ server.post("/deploy/:name", async (c) =>

server.route("/apps", appsRouter);

// Start running deployed apps' actors as soon as the host boots.
await startDynamicAppsHost();

serve({ fetch: server.fetch, port: Number(process.env.PORT ?? 3000) });
4 changes: 2 additions & 2 deletions packages/dynamic-apps-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
},
"dependencies": {
"@hono/node-server": "2.1.1",
"@rivetkit/rivetkit-wasm": "2.3.29",
"@rivetkit/rivetkit-wasm": "3.0.2",
"esbuild-wasm": "0.27.4",
"hono": "4.13.5",
"rivetkit": "2.3.29",
"rivetkit": "3.0.2",
"secure-exec": "0.2.1"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/dynamic-apps-builder/test/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ describe("apps-builder", () => {
PORT: String(port),
DYNAMIC_APPS_READY_NONCE: "builder-test",
RIVETKIT_RUNTIME: "wasm",
RIVETKIT_RUNTIME_MODE: "serverless",
},
});
let stderr = "";
Expand Down
1 change: 1 addition & 0 deletions packages/dynamic-apps-core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
DIRECT_ENTRYPOINT,
DIRECT_RUNTIME_FORMAT,
directRunnerSource,
ENVOY_SHUTDOWN_PATH,
GUEST_FORMAT_ERROR_SOURCE,
normalizeAppPath,
} from "./runtime.js";
Expand Down
66 changes: 49 additions & 17 deletions packages/dynamic-apps-core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function canonicalDeploymentHash(input: {
deploymentIdentity?: string;
}): string {
const hash = createHash("sha256");
hash.update("dynamic-apps-release-v19-mounted-hono-router\0");
hash.update("dynamic-apps-release-v20-rivet-v3-envoy\0");
const field = (value: string | Uint8Array) => {
const bytes = typeof value === "string" ? Buffer.from(value) : value;
const length = Buffer.allocUnsafe(8);
Expand Down Expand Up @@ -154,7 +154,17 @@ export const GUEST_FORMAT_ERROR_SOURCE = `function formatError(error) {
return lines.join("\\n");
}`;

/** Initializes RivetKit WASM and hosts the application's exported fetch handler. */
/** Path of the envoy runner's control endpoint that drains its registry. */
export const ENVOY_SHUTDOWN_PATH = "/.dynamic-apps/envoy/shutdown";

/**
* Initializes RivetKit WASM and runs the application in one of two modes.
*
* The default mode hosts the application's exported fetch handler for direct
* HTTP traffic. With `DYNAMIC_APPS_RUNNER_MODE=envoy` it instead starts the
* exported registry as a long-lived envoy that hosts the application's actors,
* and serves only a control endpoint that drains that envoy.
*/
export function actorRunnerSource(entrypointInput: string): string {
const entrypoint = `./${normalizeAppPath(entrypointInput)}`;
return `import { readFile } from "node:fs/promises";
Expand All @@ -164,23 +174,45 @@ import initializeRivetKit from "@rivetkit/rivetkit-wasm";
const wasmUrl = new URL(__AGENTOS_RIVETKIT_WASM_PATH__, import.meta.url);
await initializeRivetKit({ module_or_path: await readFile(wasmUrl) });
const applicationModule = await import(${JSON.stringify(entrypoint)});
const application = applicationModule.default;
const fetch = typeof application === "function"
? application
: typeof application?.fetch === "function"
? application.fetch.bind(application)
: undefined;
if (!fetch) {
throw new TypeError("Dynamic App entrypoint default export must be a fetch handler");
}
${GUEST_FORMAT_ERROR_SOURCE}
async function handle(request, ...rest) {
try {
return await fetch(request, ...rest);
} catch (error) {
console.error(formatError(error));
return new Response("Internal Server Error", { status: 500 });
let handle;
if (process.env.DYNAMIC_APPS_RUNNER_MODE === "envoy") {
const registry = applicationModule.registry;
if (typeof registry?.start !== "function" || typeof registry?.shutdown !== "function") {
throw new TypeError(
"Dynamic Apps using RivetKit must export the registry from the entrypoint: export const registry = setup(...)",
);
}
registry.start();
let shutdown;
handle = async (request) => {
const url = new URL(request.url);
if (request.method !== "POST" || url.pathname !== ${JSON.stringify(ENVOY_SHUTDOWN_PATH)}) {
return new Response("Not Found", { status: 404 });
}
// The guest exits once drained so the host can dispose its VM cleanly.
shutdown ??= registry.shutdown().finally(() => setTimeout(() => process.exit(0), 0));
await shutdown;
return new Response("drained");
};
} else {
const application = applicationModule.default;
const fetch = typeof application === "function"
? application
: typeof application?.fetch === "function"
? application.fetch.bind(application)
: undefined;
if (!fetch) {
throw new TypeError("Dynamic App entrypoint default export must be a fetch handler");
}
handle = async (request, ...rest) => {
try {
return await fetch(request, ...rest);
} catch (error) {
console.error(formatError(error));
return new Response("Internal Server Error", { status: 500 });
}
};
}
await new Promise((resolve, reject) => {
const server = serve({
Expand Down
Loading
Loading