Skip to content

CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.

build: ship the new layout, and give the server one place for resource paths

codingFeng101e73abb090992
diff --git a/README.md b/README.md
index bb9e65d..b8f265a 100644
--- a/README.md
+++ b/README.md
@@ -185,7 +185,7 @@ npm run build
- AI configuration is read from local app settings; the text and image channels are configured separately.
- Local development depends on the API service for `/api/chat` and `/api/ppt-ai`.
- After changing local API route wiring in `vite.config.ts`, restart the dev server.
-- `src/server/**` and `src/ai/**` use relative imports: `vite.config.ts` imports the API routes and Node resolves that file without the `@/` path alias.
+- `server/**` uses relative imports: `vite.config.ts` imports the dev middleware so `npm run dev` serves the API in-process, and Vite loads its own config through plain Node, which does not know the `@/` alias.
- Set `PROMPT_LOG_DIR` to write assembled prompts to disk; it is off by default because the log contains the user's content.
## 📚 Docs
diff --git a/README.zh-CN.md b/README.zh-CN.md
index e6aec2c..b9e50d2 100644
--- a/README.zh-CN.md
+++ b/README.zh-CN.md
@@ -185,7 +185,7 @@ npm run build
- AI 配置读取本地应用设置;文本通道和生图通道分别配置。
- 本地开发依赖 API 服务提供 `/api/chat` 和 `/api/ppt-ai`。
- 修改 `vite.config.ts` 中的本地 API 路由后,需要重启开发服务。
-- `src/server/**` 和 `src/ai/**` 使用相对路径导入:`vite.config.ts` 会 import API 路由,而 Node 加载这个配置文件时不认 `@/` 别名。
+- `server/**` 使用相对路径导入:`vite.config.ts` 会 import 开发中间件(这样 `npm run dev` 一条命令就带 API),而 Vite 用纯 Node 加载自己的配置文件,不认 `@/` 别名。
- 设置 `PROMPT_LOG_DIR` 可把拼装好的提示词写入磁盘;默认关闭,因为日志里含有用户内容。
## 📚 文档
diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile
index a4c4497..c2b5418 100644
--- a/deploy/docker/Dockerfile
+++ b/deploy/docker/Dockerfile
@@ -14,8 +14,13 @@ WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
-COPY api ./api
-COPY src ./src
+# The API runs from source through tsx, so the server and its contracts ship
+# as-is. resources/ is read at request time — the agent prompts and the draw.io
+# shape libraries — so it has to be present too, not just bundled into dist.
+COPY server ./server
+COPY contracts ./contracts
+COPY resources ./resources
+COPY tsconfig.json ./tsconfig.json
COPY --from=build /app/dist ./dist
ENV NODE_ENV=production
diff --git a/docs/architecture.md b/docs/architecture.md
index 50ce534..e2d9b28 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -1,47 +1,69 @@
# Architecture
-CanvasAnvil is a Vite + React single-page app with a small Node API beside it.
-Three canvases — Flow, Interior Design (CAD), and PPT — share one shell, one UI
-kit, and one model client.
+CanvasAnvil is three programs in one repository: a Vite + React browser app, a
+small Node API, and the contract between them. Three canvases — Flow, Interior
+Design (CAD), and PPT — share one shell, one UI kit, and one model client.
## Layout
```
-src/
- app/ the shell: App, the workspace registry, error boundary
- ai/ the model client — one OpenAI-compatible transport
- shared/ everything more than one workspace uses
- ui/ the shadcn component kit (one copy)
- chat/ the chat components and plumbing the panels share
- files/ upload extraction: PDF, Word, LaTeX archives, figures
- storage/ the IndexedDB key/value store factory
- i18n/ language setting and the app-shell dictionary
- lib/ cn()
- workspaces/ flow / cad / ppt — each canvas's own logic
- features/ ppt-editor: the editable-slide bridge
- pages/ the portal / landing page
- server/ the API: routes, chat pipeline, telemetry
- components/ the settings dialog
-api/ thin route shims plus the Express entry point
-agent/ system prompts, as markdown, loaded with ?raw
-skill/ the per-canvas skill bundles
+client/ the browser app
+ app/ the shell: App, the workspace registry, settings, error boundary
+ ai/ the browser's model facade and where it stores settings
+ workspaces/<id>/ one directory per canvas, all the same shape:
+ <Id>Workspace.tsx the entry the registry lazy-loads
+ canvas/ the canvas half
+ chat/ the chat half
+ lib/ logic only that workspace uses
+ storage.ts its persisted keys
+ features/ppt-editor/ the editable-slide bridge
+ pages/portal/ the landing page
+ shared/ anything more than one workspace uses
+ ui/ the shadcn component kit
+ chat/ the chat components and plumbing the panels share
+ files/ upload extraction: PDF, Word, LaTeX archives, figures
+ storage/ the IndexedDB key/value store factory
+ i18n/ language setting and the app-shell dictionary
+ lib/ cn()
+
+server/ the API
+ routes/ one file per endpoint
+ chat/ the /api/chat pipeline, a module per stage
+ ai/ the OpenAI transport
+ telemetry/ Langfuse
+ http/ the route table, the Node<->Web adapter, the Vite middleware
+ index.ts the Express entry point
+
+contracts/ what the client and server agree on (AIConfig and its
+ normalisation); no browser or Node APIs
+resources/ data read at runtime
+ prompts/ agent prompts, as markdown
+ shape-libraries/ draw.io shape references the Flow agent can pull in
+
+public/ docs/ deploy/ skill/ tests/
```
## Rules the structure depends on
-**Nothing in `src/server/` imports from `src/workspaces/`.** The server is a
-separate program that happens to share a repository; a backend reaching into a
-frontend folder is what put the draw.io system prompt and the Langfuse client
-inside the Flow workspace for a while.
+**Nothing in `server/` imports from `client/`.** They are separate programs;
+the only thing they share is `contracts/`. A backend reaching into a frontend
+folder is what once put the draw.io system prompt and the Langfuse client
+inside the Flow workspace.
-**`src/ai/**` and `src/server/**` use relative imports, not the `@/` alias.**
-Vite loads `vite.config.ts` through plain Node, and that config imports the API
-route handlers — Node does not know about the tsconfig path alias.
+**`server/**` uses relative imports, not the `@/` alias.** `vite.config.ts`
+imports the dev middleware so `npm run dev` serves the API in-process, and Vite
+loads its own config through plain Node, which knows nothing about the tsconfig
+paths.
**Storage keys are load-bearing.** IndexedDB database names and localStorage
-keys carry users' saved work across releases. `src/workspaces/*/storage.ts` and
-`flow/next/lib/flow-storage-keys.ts` name them in one place each; renaming one
-orphans whatever was stored under the old name.
+keys carry users' saved work across releases. Each workspace names its own in
+`workspaces/<id>/storage.ts`; Flow's are in `workspaces/flow/lib/storage.ts`
+and keep their original `next-ai-draw-io-` prefix from before that workspace
+was folded in. Renaming one orphans whatever was stored under it.
+
+**`resources/` is data, not documentation or source.** The server reads it with
+`readFile` at request time, so a deployment has to ship it — see the Dockerfile.
+`server/chat/resources.ts` is the only place those paths are written down.
## The model client
@@ -64,19 +86,25 @@ fallback order is pinned by `tests/ai-request-shape.test.ts`.
Two paths reach the model:
- **`/api/ppt-ai`** — non-streaming, used by the CAD and PPT workspaces through
- `src/ai/client.ts`. The browser never talks to the provider directly, so the
- key stays out of cross-origin requests and the server can inline remote
+ `client/ai/client.ts`. The browser never talks to the provider directly, so
+ the key stays out of cross-origin requests and the server can inline remote
images the browser could not fetch.
- **`/api/chat`** — streaming with tool calls, used by Flow. Runs as a pipeline
- whose stages live in `src/server/chat/`: validate, extract attachments,
+ whose stages live in `server/chat/`: validate, extract attachments,
summarise, classify intent, optionally draft a reference image, assemble the
prompt, stream.
## Adding a canvas
-Add an entry to `WORKSPACES` in `src/app/workspaces.ts`. The header tabs, the
+Add an entry to `WORKSPACES` in `client/app/workspaces.ts`. The header tabs, the
persisted selection, and the rendered workspace all read from that array. Give
-the shell its own lazy import so it does not land in the initial bundle.
+the entry its own lazy import so it stays out of the initial bundle.
+
+## Adding an API route
+
+Add the handler under `server/routes/`, then one entry to `API_ROUTES` in
+`server/http/routes.ts`. Express and the Vite dev middleware both read that
+table, so nothing else needs touching.
## Bundle
diff --git a/server/chat/deep-thinking.ts b/server/chat/deep-thinking.ts
index 9ccd942..489947c 100644
--- a/server/chat/deep-thinking.ts
+++ b/server/chat/deep-thinking.ts
@@ -1,6 +1,7 @@
import { mkdir, readFile, writeFile } from "fs/promises"
import path from "path"
import { generateImage } from "../ai/image"
+import { promptsDir } from "./resources"
import type { AIChannel } from "../../contracts/ai"
import { parseDataUrl } from "./files"
@@ -94,12 +95,7 @@ export async function getFlowDeepThinkingImagePromptTemplate(): Promise<string>
return flowDeepThinkingImagePromptTemplateCache
}
- const promptPath = path.join(
- process.cwd(),
- "agent",
- "flow",
- "deep-thinking-image.md",
- )
+ const promptPath = path.join(promptsDir(), "flow", "deep-thinking-image.md")
const raw = await readFile(promptPath, "utf8")
flowDeepThinkingImagePromptTemplateCache = String(raw || "").trim()
return flowDeepThinkingImagePromptTemplateCache
diff --git a/server/chat/resources.ts b/server/chat/resources.ts
new file mode 100644
index 0000000..b5955e8
--- /dev/null
+++ b/server/chat/resources.ts
@@ -0,0 +1,19 @@
+import path from "node:path";
+
+/**
+ * Where the server finds the files it reads at request time.
+ *
+ * These are runtime data, not build inputs: the client bundles the same
+ * prompts with `?raw`, but the server reads them from disk, so a deployment
+ * has to ship resources/ alongside the code.
+ */
+
+const RESOURCES = "resources";
+
+export function promptsDir(): string {
+ return path.join(process.cwd(), RESOURCES, "prompts");
+}
+
+export function shapeLibraryDir(): string {
+ return path.join(process.cwd(), RESOURCES, "shape-libraries");
+}
diff --git a/server/chat/shape-library.ts b/server/chat/shape-library.ts
index 755e39c..f38f4a6 100644
--- a/server/chat/shape-library.ts
+++ b/server/chat/shape-library.ts
@@ -1,5 +1,3 @@
-import path from "node:path"
-
/**
* The draw.io shape libraries the Flow agent can pull in on demand.
*
@@ -45,8 +43,3 @@ export type ShapeLibraryName = (typeof AVAILABLE_SHAPE_LIBRARIES)[number];
export function formatAvailableShapeLibraries(): string {
return AVAILABLE_SHAPE_LIBRARIES.join(", ");
}
-
-/** Absolute path to the directory holding one markdown file per library. */
-export function shapeLibraryDir(): string {
- return path.join(process.cwd(), "resources", "shape-libraries");
-}
diff --git a/server/routes/chat.ts b/server/routes/chat.ts
index 4fe28cc..b051c87 100644
--- a/server/routes/chat.ts
+++ b/server/routes/chat.ts
@@ -12,7 +12,8 @@ import path from "path"
import { z } from "zod"
import { getImageChannel, getTextChannel, normalizeAIConfig } from "../../contracts/ai"
import { getChatModel } from "../ai/model"
-import { formatAvailableShapeLibraries, shapeLibraryDir } from "../chat/shape-library"
+import { formatAvailableShapeLibraries } from "../chat/shape-library"
+import { shapeLibraryDir } from "../chat/resources"
import { getSystemPrompt } from "../chat/system-prompt"
import {
RECURSIVE_THRESHOLD_TOKENS,