Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

perf: lazy-load AI Worker runtime (refs #244)

tianma-ifaaab23d9c641
diff --git a/apps/api/src/ai-routes.ts b/apps/api/src/ai-routes.ts
index 0ea6e7be..cfb5ec68 100644
--- a/apps/api/src/ai-routes.ts
+++ b/apps/api/src/ai-routes.ts
@@ -510,7 +510,7 @@ export const registerAiRoutes = (app: Hono<AppEnv>, dependencies: AiRouteDepende
send({ type: "start" });
try {
if (input.stream) {
- const result = streamAiGeneration(generationInput);
+ const result = await streamAiGeneration(generationInput);
const normalizer = createAiGenerationStreamNormalizer(resultBoundary);
let hasContent = false;
for await (const part of result.stream) {
diff --git a/apps/api/src/ai-runtime.ts b/apps/api/src/ai-runtime.ts
new file mode 100644
index 00000000..443cbe42
--- /dev/null
+++ b/apps/api/src/ai-runtime.ts
@@ -0,0 +1,30 @@
+import { createAnthropic } from "@ai-sdk/anthropic";
+import { createGoogle } from "@ai-sdk/google";
+import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
+import type { AiProvider } from "@edgeever/shared";
+import { generateText, streamText } from "ai";
+
+export const createAiModel = (config: {
+ provider: AiProvider;
+ baseUrl: string;
+ apiKey: string;
+ modelId: string;
+}) => {
+ switch (config.provider) {
+ case "anthropic":
+ return createAnthropic({ baseURL: config.baseUrl, apiKey: config.apiKey })(config.modelId);
+ case "google":
+ return createGoogle({ baseURL: config.baseUrl, apiKey: config.apiKey })(config.modelId);
+ default:
+ return createOpenAICompatible({
+ name: "edgeever-openai-compatible",
+ baseURL: config.baseUrl,
+ apiKey: config.apiKey,
+ includeUsage: true,
+ })(config.modelId);
+ }
+};
+
+export const generateAiText = (...args: Parameters<typeof generateText>) => generateText(...args);
+
+export const streamAiText = (...args: Parameters<typeof streamText>) => streamText(...args);
diff --git a/apps/api/src/ai-service.test.mjs b/apps/api/src/ai-service.test.mjs
index e38cf078..6338c98f 100644
--- a/apps/api/src/ai-service.test.mjs
+++ b/apps/api/src/ai-service.test.mjs
@@ -75,7 +75,7 @@ describe("AI model service", () => {
test("streams plain text without forcing tool choice for thinking-model compatibility", async () => {
let request;
- const result = streamAiGeneration({
+ const result = await streamAiGeneration({
model: new MockLanguageModelV4({
doStream: async (options) => {
request = options;
@@ -179,7 +179,7 @@ describe("AI model service", () => {
});
},
})("deepseek-v4-flash");
- const result = streamAiGeneration({
+ const result = await streamAiGeneration({
model,
action: "summarize",
title: "测试",
diff --git a/apps/api/src/ai-service.ts b/apps/api/src/ai-service.ts
index 71a587f8..259f317e 100644
--- a/apps/api/src/ai-service.ts
+++ b/apps/api/src/ai-service.ts
@@ -1,6 +1,3 @@
-import { createAnthropic } from "@ai-sdk/anthropic";
-import { createGoogle } from "@ai-sdk/google";
-import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import type {
AiAction,
AiDiscoveredModel,
@@ -12,7 +9,6 @@ import type {
AiTone,
} from "@edgeever/shared";
import { getDefaultAiPromptSeed } from "@edgeever/shared";
-import { generateText, streamText } from "ai";
import { AppError } from "./app-error";
import { decryptSecret } from "./secret-encryption";
import type { DatabaseAdapter } from "./storage-contract";
@@ -183,26 +179,19 @@ export const getAiSettings = async (
export const normalizeAiBaseUrl = (value: string) => value.trim().replace(/\/+$/, "");
-export const createAiModel = (config: {
+const loadAiRuntime = () => import("./ai-runtime");
+
+export const createAiModel = async (config: {
provider: AiProvider;
baseUrl: string;
apiKey: string;
modelId: string;
}) => {
- const baseURL = normalizeAiBaseUrl(config.baseUrl);
- switch (config.provider) {
- case "anthropic":
- return createAnthropic({ baseURL, apiKey: config.apiKey })(config.modelId);
- case "google":
- return createGoogle({ baseURL, apiKey: config.apiKey })(config.modelId);
- default:
- return createOpenAICompatible({
- name: "edgeever-openai-compatible",
- baseURL,
- apiKey: config.apiKey,
- includeUsage: true,
- })(config.modelId);
- }
+ const runtime = await loadAiRuntime();
+ return runtime.createAiModel({
+ ...config,
+ baseUrl: normalizeAiBaseUrl(config.baseUrl),
+ });
};
export const loadDefaultAiModel = async (
@@ -319,13 +308,19 @@ export const testAiModel = async (config: {
baseUrl: string;
apiKey: string;
modelId: string;
-}) => generateText({
- model: createAiModel(config),
- system: "You are responding to an API connectivity check. Follow the user instruction exactly.",
- prompt: "Reply with only: OK",
- maxOutputTokens: 16,
- abortSignal: AbortSignal.timeout(20_000),
-});
+}) => {
+ const runtime = await loadAiRuntime();
+ return runtime.generateAiText({
+ model: runtime.createAiModel({
+ ...config,
+ baseUrl: normalizeAiBaseUrl(config.baseUrl),
+ }),
+ system: "You are responding to an API connectivity check. Follow the user instruction exactly.",
+ prompt: "Reply with only: OK",
+ maxOutputTokens: 16,
+ abortSignal: AbortSignal.timeout(20_000),
+ });
+};
/**
* Fallback instructions from the shared seed catalog (same text shown in the prompt library).
@@ -510,7 +505,7 @@ export const buildAiGenerationPrompt = (input: {
].filter(Boolean).join("\n\n");
type AiGenerationRequest = {
- model: ReturnType<typeof createAiModel>;
+ model: Awaited<ReturnType<typeof createAiModel>>;
action: AiAction;
title: string;
contentMarkdown: string;
@@ -534,6 +529,12 @@ const buildAiGenerationRequest = (input: AiGenerationRequest) => ({
abortSignal: input.abortSignal,
});
-export const generateAiGeneration = (input: AiGenerationRequest) => generateText(buildAiGenerationRequest(input));
+export const generateAiGeneration = async (input: AiGenerationRequest) => {
+ const runtime = await loadAiRuntime();
+ return runtime.generateAiText(buildAiGenerationRequest(input));
+};
-export const streamAiGeneration = (input: AiGenerationRequest) => streamText(buildAiGenerationRequest(input));
+export const streamAiGeneration = async (input: AiGenerationRequest) => {
+ const runtime = await loadAiRuntime();
+ return runtime.streamAiText(buildAiGenerationRequest(input));
+};
diff --git a/bun.lock b/bun.lock
index 569244e9..c7ab6afd 100644
--- a/bun.lock
+++ b/bun.lock
@@ -69,6 +69,7 @@
"@vitejs/plugin-react": "latest",
"autoprefixer": "latest",
"concurrently": "latest",
+ "esbuild": "0.28.1",
"fake-indexeddb": "^6.2.5",
"postcss": "latest",
"tailwindcss": "^3.4.17",
@@ -84,7 +85,7 @@
},
"apps/desktop": {
"name": "@edgeever/desktop",
- "version": "1.17.1",
+ "version": "1.23.0",
"dependencies": {
"electron-updater": "^6.6.2",
},
@@ -95,7 +96,7 @@
},
"apps/extension": {
"name": "@edgeever/extension",
- "version": "0.1.4",
+ "version": "0.1.5",
"dependencies": {
"@mozilla/readability": "^0.6.0",
"turndown": "^7.2.4",
diff --git a/package.json b/package.json
index 074067e0..17f37bdf 100644
--- a/package.json
+++ b/package.json
@@ -59,7 +59,8 @@
"test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/clipboard-write.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/local-data-reset.test.mjs apps/desktop/src/main/resource-request.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/tray-icon.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/desktop/src/main/window-visibility.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/clipboard.test.mjs apps/web/src/lib/desktop-repository.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/runtime.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
"verify:desktop-renderer": "bun scripts/verify-desktop-renderer.mjs",
"verify:desktop-package": "bun scripts/verify-desktop-package.mjs",
- "build:cloudflare": "bun run typecheck && bun run build",
+ "build:worker": "bun scripts/build-cloudflare-worker.mjs",
+ "build:cloudflare": "bun run typecheck && bun run build && bun run build:worker",
"build:site": "bun --cwd apps/site build",
"preview:web": "vite preview --config apps/web/vite.config.ts --host 127.0.0.1 --port 4173",
"preview:site": "bun --cwd apps/site preview",
@@ -175,6 +176,7 @@
"@vitejs/plugin-react": "latest",
"autoprefixer": "latest",
"concurrently": "latest",
+ "esbuild": "0.28.1",
"fake-indexeddb": "^6.2.5",
"postcss": "latest",
"tailwindcss": "^3.4.17",
diff --git a/scripts/build-cloudflare-worker.mjs b/scripts/build-cloudflare-worker.mjs
new file mode 100644
index 00000000..cd9dc7d8
--- /dev/null
+++ b/scripts/build-cloudflare-worker.mjs
@@ -0,0 +1,70 @@
+import { rm, stat } from "node:fs/promises";
+import { resolve, sep } from "node:path";
+import { build } from "esbuild";
+
+export const CLOUDFLARE_WORKER_OUTPUT_DIRECTORY = resolve(".wrangler/edgeever-worker");
+const normalizePath = (value) => value.replaceAll("\\", "/");
+
+export const buildCloudflareWorker = async () => {
+ const generatedRoot = resolve(".wrangler");
+ if (!CLOUDFLARE_WORKER_OUTPUT_DIRECTORY.startsWith(`${generatedRoot}${sep}`)) {
+ throw new Error(
+ `Refusing to clean unsafe Worker output path: ${CLOUDFLARE_WORKER_OUTPUT_DIRECTORY}`,
+ );
+ }
+ await rm(CLOUDFLARE_WORKER_OUTPUT_DIRECTORY, { recursive: true, force: true });
+
+ const result = await build({
+ entryPoints: [resolve("apps/api/src/index.ts")],
+ outdir: CLOUDFLARE_WORKER_OUTPUT_DIRECTORY,
+ entryNames: "index",
+ chunkNames: "modules/[name]-[hash]",
+ bundle: true,
+ splitting: true,
+ format: "esm",
+ platform: "browser",
+ target: "es2022",
+ conditions: ["workerd", "worker", "browser"],
+ minify: true,
+ keepNames: true,
+ legalComments: "none",
+ treeShaking: true,
+ metafile: true,
+ write: true,
+ allowOverwrite: true,
+ outExtension: { ".js": ".js" },
+ });
+
+ const outputs = await Promise.all(Object.entries(result.metafile.outputs).map(async ([path, metadata]) => ({
+ path,
+ bytes: (await stat(path)).size,
+ entryPoint: metadata.entryPoint,
+ imports: metadata.imports,
+ })));
+ const entry = outputs.find((output) =>
+ normalizePath(output.entryPoint ?? "").endsWith("apps/api/src/index.ts"));
+ const modules = outputs.filter((output) => normalizePath(output.path).includes("/modules/"));
+ const dynamicImportPaths = new Set(
+ entry?.imports.filter((item) => item.kind === "dynamic-import").map((item) => item.path) ?? [],
+ );
+ const lazyModules = modules.filter((module) =>
+ Array.from(dynamicImportPaths).some((path) =>
+ normalizePath(module.path).endsWith(normalizePath(path))));
+ const sharedModules = modules.filter((module) => !lazyModules.includes(module));
+ if (!entry || lazyModules.length === 0) {
+ throw new Error("Cloudflare Worker build must produce one entrypoint and at least one on-demand module.");
+ }
+
+ console.log([
+ `[worker-build] entry: ${(entry.bytes / 1024).toFixed(2)} KiB`,
+ ...sharedModules.map((module) =>
+ `[worker-build] shared module: ${(module.bytes / 1024).toFixed(2)} KiB`),
+ ...lazyModules.map((module) =>
+ `[worker-build] on-demand module: ${(module.bytes / 1024).toFixed(2)} KiB`),
+ ].join("\n"));
+ return { entry, sharedModules, lazyModules };
+};
+
+if (import.meta.main) {
+ await buildCloudflareWorker();
+}
diff --git a/scripts/build-cloudflare-worker.test.mjs b/scripts/build-cloudflare-worker.test.mjs
new file mode 100644
index 00000000..6bb4d911
--- /dev/null
+++ b/scripts/build-cloudflare-worker.test.mjs
@@ -0,0 +1,22 @@
+import { expect, test } from "bun:test";
+import { spawnSync } from "node:child_process";
+import { readdirSync, readFileSync, rmSync } from "node:fs";
+import { resolve } from "node:path";
+
+test("keeps the AI provider runtime out of the Cloudflare Worker entrypoint", () => {
+ const outputDirectory = resolve(import.meta.dir, "../.wrangler/edgeever-worker");
+ rmSync(outputDirectory, { force: true, recursive: true });
+ const result = spawnSync("bun", [resolve(import.meta.dir, "build-cloudflare-worker.mjs")], {
+ cwd: resolve(import.meta.dir, ".."),
+ encoding: "utf8",
+ });
+ expect(result.status).toBe(0);
+
+ const moduleNames = readdirSync(resolve(outputDirectory, "modules"));
+ const aiRuntimeName = moduleNames.find((name) => name.startsWith("ai-runtime-"));
+ expect(aiRuntimeName).toBeTruthy();
+ expect(readFileSync(resolve(outputDirectory, "index.js"), "utf8"))
+ .not.toContain("edgeever-openai-compatible");
+ expect(readFileSync(resolve(outputDirectory, "modules", aiRuntimeName), "utf8"))
+ .toContain("edgeever-openai-compatible");
+});
diff --git a/tests/deployment-entrypoints.test.ts b/tests/deployment-entrypoints.test.ts
index 9f77ca4b..fe03107c 100644
--- a/tests/deployment-entrypoints.test.ts
+++ b/tests/deployment-entrypoints.test.ts
@@ -99,6 +99,13 @@ describe("Cloudflare deployment entrypoints", () => {
expect(scripts["deploy:cloudflare-builds"]).toBe(
"EDGE_EVER_USE_EXISTING_AUTH_SECRET=true bun run deploy:ci",
);
+ expect(scripts["build:cloudflare"]).toContain("bun run build:worker");
+
+ const wranglerConfig = readRepositoryFile("wrangler.toml");
+ expect(wranglerConfig).toContain('main = ".wrangler/edgeever-worker/index.js"');
+ expect(wranglerConfig).toContain("no_bundle = true");
+ expect(wranglerConfig).toContain("find_additional_modules = true");
+ expect(wranglerConfig).toContain('globs = ["modules/*.js"]');
});
test("online deployment declares the required authentication Secret", () => {
diff --git a/wrangler.toml b/wrangler.toml
index 2d08dd08..bb676e5f 100644
--- a/wrangler.toml
+++ b/wrangler.toml
@@ -1,7 +1,19 @@
name = "edgeever"
-main = "apps/api/src/index.ts"
+main = ".wrangler/edgeever-worker/index.js"
compatibility_date = "2026-06-26"
workers_dev = true
+no_bundle = true
+find_additional_modules = true
+base_dir = ".wrangler/edgeever-worker"
+
+[[rules]]
+type = "ESModule"
+globs = ["modules/*.js"]
+fallthrough = true
+
+[build]
+command = "bun scripts/build-cloudflare-worker.mjs"
+watch_dir = ["apps/api/src", "packages/shared/src"]
[assets]
directory = "apps/web/dist"