create-next-imagicma 0.1.15 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +2 -0
  2. package/package.json +1 -1
  3. package/template-hono/.env.example +6 -14
  4. package/template-hono/.imagicma/AGENTS.md +7 -0
  5. package/template-hono/AGENTS.md +99 -128
  6. package/template-hono/README.md +86 -118
  7. package/template-hono/client/index.html +2 -1
  8. package/template-hono/client/public/favicon.png +0 -0
  9. package/template-hono/client/public/imagicma-picker-bridge.js +2 -6
  10. package/template-hono/client/public/imagicma-preview-feedback.js +0 -1
  11. package/template-hono/client/src/components/HelloClient.tsx +1 -1
  12. package/template-hono/client/src/globals.css +417 -1
  13. package/template-hono/client/src/lib/imagicma-preview-bridge.ts +1 -1
  14. package/template-hono/client/src/lib/imagicma-preview-picker.ts +28 -130
  15. package/template-hono/client/src/providers.tsx +1 -1
  16. package/template-hono/gitignore +2 -1
  17. package/template-hono/package.json +9 -9
  18. package/template-hono/pnpm-lock.yaml +646 -1173
  19. package/template-hono/server/app.ts +3 -2
  20. package/template-hono/server/controllers/greeting.controller.ts +22 -0
  21. package/template-hono/server/db/index.ts +129 -0
  22. package/template-hono/server/index.ts +3 -3
  23. package/template-hono/server/middlewares/error-handler.ts +8 -0
  24. package/template-hono/server/models/entities/message.entity.ts +13 -0
  25. package/template-hono/server/models/repositories/message.repository.ts +43 -0
  26. package/template-hono/server/models/services/greeting.service.ts +14 -0
  27. package/template-hono/server/models/types/message.ts +7 -0
  28. package/template-hono/server/routes/greeting.ts +1 -1
  29. package/template-hono/shared/constants/greeting.ts +1 -0
  30. package/template-hono/shared/contracts/routes.ts +14 -0
  31. package/template-hono/shared/routes.ts +1 -68
  32. package/template-hono/shared/schema.ts +5 -8
  33. package/template-hono/shared/types/greeting.ts +3 -0
  34. package/template-hono/tailwind.config.mjs +62 -111
  35. package/template-hono/tsconfig.json +3 -2
  36. package/template-hono/tsconfig.server.json +2 -0
  37. package/template-hono/vite.config.ts +4 -4
  38. package/template-hono/client/public/favicon.ico +0 -0
  39. package/template-hono/client/src/lib/ai-ui-stream.ts +0 -64
  40. package/template-hono/client/src/theme/default-theme.css +0 -482
  41. package/template-hono/drizzle.config.ts +0 -13
  42. package/template-hono/server/controllers/ai-chat-controller.ts +0 -49
  43. package/template-hono/server/controllers/greeting-controller.ts +0 -25
  44. package/template-hono/server/db/client.ts +0 -25
  45. package/template-hono/server/env.ts +0 -89
  46. package/template-hono/server/lib/ai-provider.ts +0 -74
  47. package/template-hono/server/lib/ai.ts +0 -205
  48. package/template-hono/server/middlewares/auth.ts +0 -69
  49. package/template-hono/server/middlewares/index.ts +0 -12
  50. package/template-hono/server/repositories/message-repository.ts +0 -13
  51. package/template-hono/server/routes/ai-chat.ts +0 -9
  52. package/template-hono/shared/schema/greeting.ts +0 -17
package/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  ## 模版源
6
6
 
7
+ - `../nextjs-app`
7
8
  - `../hono-app`
8
9
  如果要修改模版,请修改源头,然后执行 `pnpm run sync-template` 同步到当前模版目录,切记不可直接修改当前模版目录。
9
10
  ## 使用
@@ -49,6 +50,7 @@ pnpm run sync-template
49
50
 
50
51
  默认同步:
51
52
 
53
+ - `../nextjs-app` -> `template/`
52
54
  - `../hono-app` -> `template-hono/`
53
55
 
54
56
  可通过环境变量覆盖:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-next-imagicma",
3
- "version": "0.1.15",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "create-next-imagicma": "./bin/create-next-imagicma.mjs"
@@ -1,18 +1,10 @@
1
1
  # 复制为 .env.local 后按需覆盖(.env.local 不会被提交)
2
2
  #
3
- # 必填:PostgreSQL 连接串
4
- # DATABASE_URL="postgresql://postgres:password@127.0.0.1:5432/hono_app"
3
+ # 数据库类型:默认 sqlite,可切换为 postgres
4
+ # DB_TYPE="sqlite"
5
5
  #
6
- # AI 网关配置
7
- # 可选:提供商,支持 openai / openai-compatible / minimax / kimi
8
- # AI_PROVIDER="minimax"
9
- # 必填:模型访问密钥
10
- # AI_API_KEY="<your-api-key>"
11
- # 可选:自定义网关地址;minimax / kimi 留空时会自动使用官方兼容地址
12
- # AI_BASE_URL="https://api.minimaxi.com/v1"
13
- # 可选:模型 ID;未设置时默认 gpt-4o-mini
14
- # AI_MODEL="MiniMax-M2.7"
6
+ # DB_TYPE=sqlite 时,可选:自定义 SQLite 文件位置;默认使用 ./.data/app.db
7
+ # DATABASE_FILE="./.data/app.db"
15
8
  #
16
- # 兼容旧变量名:
17
- # OPENAI_API_KEY="<your-api-key>"
18
- # OPENAI_BASE_URL="https://api.openai.com/v1"
9
+ # 当 DB_TYPE=postgres 时,填写 Postgres 连接串
10
+ # DATABASE_URL="postgres://user:pass@localhost:5432/mydb"
@@ -0,0 +1,7 @@
1
+
2
+ ## 开发规则
3
+
4
+ - `/.imagicma/port.json` 是初始化生成的锁定端口文件,禁止修改。
5
+ - 禁止修改 `scripts/` 下的受保护启动文件:`imagicma-common.mjs`、`imagicma-guard.mjs`、`imagicma-dev.mjs`、`imagicma-start.mjs`。
6
+ - 禁止修改 `package.json` 中 `scripts.dev` 与 `scripts.start`(以及对应 `predev`、`prestart`)命令。
7
+ - 禁止直接执行 `vite` 或 `node dist/server/index.js` 启动项目;只能通过 `pnpm dev` / `pnpm start` 启动。
@@ -1,128 +1,99 @@
1
- # Repository Guidelines
2
-
3
- ## Project Structure & Module Organization
4
-
5
- This is a `Hono + Vite + React` app designed for enterprise-level product development. Keep browser code in `client/src`, server entrypoints in `server/`, and shared contracts in `shared/`.
6
-
7
- ### Documentation Structure (Design Phase)
8
- All design artifacts are stored under `docs/designer/`:
9
-
10
- - `docs/designer/project_state.json`: project state and version tracking
11
- - `docs/designer/prd/prd.md`: Product Requirements Document
12
- - `docs/designer/persona/persona_*.md`: user personas (one file per persona)
13
- - `docs/designer/feature_plan/`: feature module planning
14
- - `feature_modules.md`: module list, relationship diagrams, and shared/common components (may contain full details if ≤4 modules)
15
- - `feature_module_[name].md`: detailed module specifications for large apps (pages, layouts, components, business rules, and navigation)
16
- - `docs/designer/style_guide/*.style-guide.md`: visual design guidelines
17
- - `docs/designer/db/`: database schema documentation
18
- - `db_schema.md`: global ER diagram and module index
19
- - `db_schema_[module].md`: per-module database schemas
20
-
21
- ### Application Code Structure
22
- - `client/src/pages/`: route-level pages (e.g., `home.tsx`)
23
- - `client/src/components/`: reusable UI components, with shadcn/ui primitives under `components/ui/`
24
- - `client/src/lib/` and `client/src/hooks/`: client utilities and custom hooks
25
- - `client/src/main.tsx`: frontend entry point
26
- - `client/src/App.tsx`: React Router configuration
27
- - `client/src/globals.css`: global styles
28
- - `client/public/`: static assets
29
- - `server/app.ts`: Hono application entry point
30
- - `server/routes/`: route registration only; bind paths and hand off to controllers
31
- - `server/middlewares/`: cross-cutting HTTP middleware such as auth, request-id, logging, and rate-limit
32
- - `server/controllers/`: HTTP layer; parse requests, call repositories/lib, and map errors to responses
33
- - `server/repositories/`: data access only; wrap Drizzle/SQL queries
34
- - `server/db/`: database client and database-level infrastructure
35
- - `server/lib/`: external integrations and reusable backend helpers
36
- - `server/env.ts`: environment loading and config validation
37
- - `shared/routes.ts` and `shared/schema.ts`: Zod-backed API contracts and shared schema
38
- - `scripts/`: guarded dev/start scripts; do not bypass them
39
- - `.env.local`: local runtime secrets such as `DATABASE_URL`; do not commit it
40
-
41
- **Critical**: The homepage must never ship blank. `client/src/pages/home.tsx` should render visible, meaningful content on first load, not hidden placeholders or empty shells.
42
-
43
- ## Development Preparation
44
-
45
- Before starting development, agents must confirm the database connection with the user by using `question`. Ask for the PostgreSQL connection string that should be used locally, for example:
46
-
47
- - `DATABASE_URL="postgresql://postgres:password@127.0.0.1:5432/hono_app"`
48
-
49
- Do not assume the database host, port, username, password, or database name. After the user confirms the value, place it in `.env.local` (or ensure it is already available in the environment) before running database-related work such as `pnpm db:push`, server development, or any feature that depends on persistence.
50
-
51
- ## Build, Test, and Development Commands
52
-
53
- - `restart_workflow`: required agent entrypoint for restarting dev services; do not call `pnpm dev` directly
54
- - `pnpm build`: builds the client with Vite and compiles the server with `tsc`
55
- - `pnpm start`: runs the production server through the guarded startup script
56
- - `pnpm check`: TypeScript type-check for both client and server configs
57
- - `pnpm lint`: runs ESLint across the repository
58
- - `pnpm db:push`: applies Drizzle schema changes to the PostgreSQL database from `DATABASE_URL`
59
-
60
- ### AI Feature Rule
61
- - If you are developing AI-related features in this template, you must use the `ai-feature` skill before implementation.
62
-
63
- ## Coding Style & Naming Conventions
64
-
65
- Use TypeScript and ESM throughout. Follow the existing style: 2-space indentation, double quotes, semicolons, and trailing commas. Prefer:
66
-
67
- - PascalCase for React components (e.g., `HelloClient.tsx`)
68
- - kebab-case for route/page files (e.g., `not-found.tsx`)
69
- - snake_case for database schema files (e.g., `db_schema_user.md`)
70
- - Colocate logic in `hooks/`, `lib/`, and `routes/` before creating new top-level folders
71
-
72
- Reuse the `@/` import alias for client code where it improves readability.
73
-
74
- ### Backend Layering Rules
75
- - Backend request flow must follow `routes -> middlewares -> controllers -> repositories -> db`.
76
- - `routes/` may define paths and middleware, but must not contain business logic or direct database access.
77
- - `middlewares/` are for cross-cutting concerns such as auth, request-id, logging, and rate-limit; they may read/write Hono Context and short-circuit requests when needed.
78
- - Auth-related checks should be implemented as middleware and mounted in `routes/`, not reimplemented inside controllers.
79
- - `controllers/` may read/write HTTP details, call repositories or `lib/`, and map errors, but must not contain SQL.
80
- - `repositories/` own database reads/writes only and must not shape HTTP responses.
81
- - `db/` is limited to connection setup, schema wiring, and database infrastructure helpers.
82
- - `lib/` is for external providers or shared backend utilities; routes should reach it through middleware or controllers, not directly.
83
- - Controllers should consume auth context that middleware has already prepared instead of parsing tokens again.
84
- - New backend features should default to adding files along the same chain: route, middleware when needed, controller, repository.
85
- - Do not mock backend data for feature development. Implement against real backend flows, real repositories, and real interfaces unless the user explicitly asks for a temporary test double.
86
-
87
- ### Shared Schema Rules
88
- - Always split shared schema by business module in `shared/schema/*.ts`, not by individual table.
89
- - `shared/schema.ts` must remain the unified export entry so existing imports and Drizzle config stay stable.
90
- - Each schema module should contain the tables, Zod schema exports, and inferred TS types for that module.
91
- - Avoid one-file-per-table unless the user explicitly asks for it or a single module is exceptionally large.
92
-
93
- ### Design Documentation Standards
94
- - All Mermaid diagrams must NOT include styling elements (no style definitions, classDef, linkStyle or fill colors)
95
- - Use basic graph syntax only: `graph TD`/`graph TB` for architecture, and `erDiagram` for schemas
96
- - File naming must strictly follow the patterns defined in File Structure section
97
-
98
- ### Technical Selection Policy
99
- Technical selection is strictly an engineering responsibility, governed globally:
100
- - When evaluating complex system requirements (e.g., OCR, Payments, AI integrations, Auth), propose solutions contrasting self-built vs. commercial options.
101
- - Strip away meaningless basic scaffolding choices; focus squarely on domain-specific system blockers.
102
- - When human input is mandatory to finalize a route, translate technical jargon into business dimensions for the stakeholders: "Development Time, Cost, and Product Experience".
103
- - Final architectural decisions must be persisted into `docs/designer/tech_design/tech_design.md`.
104
-
105
- ## Testing & Quality Assurance
106
-
107
- Before opening a PR, complete manual verification to ensure the application is usable end to end.
108
-
109
- If you add tests, keep them as `*.test.ts` or `*.test.tsx`, or add a `tests/` folder and document the new command in `package.json`.
110
-
111
- ### Manual Verification Checklist
112
- - All API endpoints work correctly
113
- - All pages are accessible without 404 errors
114
- - All buttons are clickable and trigger the expected behavior
115
- - All links navigate to the correct destination
116
-
117
- ## Commit & Pull Request Guidelines
118
-
119
- Recent history uses Conventional Commit prefixes such as `feat:`, `fix:`, and `chore:`; keep that format and write concise subjects. PRs should include:
120
-
121
- - a short description of behavior changes
122
- - linked issue or task when available
123
- - screenshots or recordings for UI changes
124
- - notes for env, port, or database-impacting changes
125
-
126
- ## Runtime & Configuration Notes
127
-
128
- Agents must use `restart_workflow` instead of invoking `pnpm dev` manually. Do not pass a port on the command line or by prefixing `pnpm dev` with `PORT=...`; keep runtime port configuration in `.imagicma/runtime.env`. Database access must come from `DATABASE_URL` in `.env.local` (or process env), and secrets must never be committed.
1
+ # 项目 Agent 指南(Hono + Vite + React + TypeORM)
2
+
3
+ 默认使用中文沟通与输出(除非用户明确要求其他语言)。
4
+
5
+ ## 目标
6
+
7
+ 交付可运行、可预览、可验证、可演示的完整应用。
8
+
9
+ ## 技术栈与约束
10
+
11
+ - 框架:Hono(Node runtime)+ Vite(单进程开发)
12
+ - 前端:React 19 + React Router + Tailwind v4 + shadcn/ui
13
+ - 请求层:React Query + fetch
14
+ - 契约:Zod(`shared/contracts/routes.ts`,兼容 `shared/routes.ts` re-export)
15
+ - 数据库:TypeORM(默认 `DB_TYPE=sqlite` + `./.data/app.db`;切到 Postgres 时使用 `DB_TYPE=postgres` + `DATABASE_URL`)
16
+
17
+ ## 目录约定
18
+
19
+ ```text
20
+ client/
21
+ index.html # Vite 前端入口 HTML
22
+ public/ # 前端静态资源
23
+ src/ # 前端应用入口、页面、Provider、错误边界
24
+ components/ui/ # shadcn/ui 组件
25
+ hooks/ # 客户端 hooks
26
+ lib/ # 通用工具(含 app-metadata.ts,生成项目后优先修改应用名/title)
27
+
28
+ server/
29
+ app.ts # Hono 装配入口,仅负责注册 middleware、routes、静态资源
30
+ routes/ # 路由绑定层,只做 URL controller 的映射
31
+ controllers/ # HTTP 适配层,解析请求并返回响应
32
+ middlewares/ # 日志、异常等横切能力
33
+ models/
34
+ entities/ # TypeORM entity 等持久化模型
35
+ repositories/ # 数据库访问实现,只回答“怎么查、怎么存”
36
+ services/ # 业务规则与编排,只回答“为什么这么查、怎么处理结果”
37
+ db/ # DataSource 与数据库配置
38
+
39
+ shared/
40
+ contracts/ # 前后端共享 API path 与 Zod 契约
41
+ types/ # 前后端共享纯 TypeScript 类型
42
+ constants/ # 前后端共享纯常量
43
+ ```
44
+
45
+ - `shared/`:禁止放 TypeORM entity、Node API、Hono Context 与其他仅服务端可运行的代码
46
+
47
+ ## 常用命令
48
+
49
+ - `pnpm dev`:单进程开发(Vite + Hono dev middleware)
50
+ - `pnpm build`:构建前端并编译 server
51
+ - `pnpm start`:生产启动
52
+ - `pnpm check`:类型检查
53
+ - `pnpm lint`:ESLint
54
+
55
+ ## 执行硬规则
56
+
57
+ - 数据库 entity 与运行时连接配置必须保持一致,禁止手写临时 SQL ORM entity 并行漂移。
58
+ - 当任务涉及数据库选型且用户未明确指定时,必须优先使用 `question tool` 询问使用什么数据库;仅在无法询问或未获得明确答复时,才按默认 `sqlite`(`DB_TYPE=sqlite` + `./.data/app.db`)处理。
59
+ - 禁止修改 `scripts/` 下的受保护启动文件:`imagicma-common.mjs`、`imagicma-guard.mjs`、`imagicma-dev.mjs`、`imagicma-start.mjs`、`imagicma-runtime-logs.mjs`。
60
+ - 禁止修改 `package.json` 中 `scripts.dev` 与 `scripts.start`(以及对应 `predev`、`prestart`)命令。
61
+ - 禁止直接执行 `vite` `node dist/server/index.js` `pnpm dev` `pnpm start` 启动项目;只能通过 `restart_workflow` 启动。
62
+ - 禁止主动注入环境变量到 `process.env`。
63
+ - 端口契约只允许使用大写 `PORT`。
64
+ - 运行时端口读取顺序固定为:外部环境变量 `PORT` -> 项目内 `.imagicma/runtime.env`。
65
+ - 预览地址规则固定为 `https://{port}.preview.imagicma.cn`,其中 `port` 为实际启动端口。
66
+
67
+ ## 状态文件写入规则
68
+
69
+ - 若维护 `docs/project_state.json`,每次写入前先读取最新版本。
70
+ - 若写入报冲突(文件已变更),必须重新读取后重试,不得忽略。
71
+ - `quality_gates.typecheck=true` 仅在构建命令真实通过后设置。
72
+
73
+ ## UI 质量门禁(必须全部满足)
74
+
75
+ - `/` 路由必须可访问并落到首页组件。
76
+ - `client/src/pages/home.tsx` 不允许为空文件、空组件,或仅返回 `null` / 空白占位;首页必须渲染可见内容。
77
+ - 接入真实业务时,应将 `/` 首页替换为真实业务内容,不要长期保留模板默认文案或空白首页。
78
+ - 视觉风格必须明确:字体、颜色、间距、动效要统一。
79
+ - 必须定义可复用设计 token(颜色、圆角、阴影、间距等级)。
80
+ - 至少覆盖桌面与移动端关键断点,不允许内容溢出。
81
+ - 交互状态完整:hover/focus/disabled/loading/error/success。
82
+ - 列表增删改状态建议加入轻量动效(如 `framer-motion` 的入场/退出),并提供失败反馈(toast inline alert)。
83
+
84
+ ## 数据与安全
85
+
86
+ - API 响应必须经过 `shared/contracts/routes.ts`(或 `shared/routes.ts` re-export)的 Zod schema 校验。
87
+ - 优先复用 `client/src/components/ui` 与 `client/src/hooks`,避免重复造轮子。
88
+
89
+ ## 完成标准
90
+
91
+ 只有以下全部满足才允许结束:
92
+
93
+ - `pnpm build` 通过(无 pnpm 时 `npm run build` 通过)
94
+ - 页面可在本地端口访问并完成核心流程
95
+ - `/` 首页路由可访问
96
+ - 关键日志无阻塞级错误
97
+ - UI 达到可演示级(非“功能可用但观感粗糙”)
98
+ - 按钮可点击
99
+ - 页面跳转后正常打开,非404
@@ -1,153 +1,121 @@
1
1
  # hono-app
2
2
 
3
- 基于 **Hono + Vite + React** 的全栈应用。
3
+ 基于 **Hono + Vite + React** 的全栈模板,目标是在保留前端开发体验的同时,提供更轻量的 Node 运行时方案。
4
4
 
5
5
  ## 技术栈
6
6
 
7
7
  - 前端:React 19、React Router、Tailwind CSS v4、shadcn/ui、React Query
8
8
  - 后端:Hono(Node runtime)
9
- - 数据层:Drizzle ORM + PostgreSQL(默认通过 `DATABASE_URL` 连接)
10
- - AI 网关:Vercel AI SDK 5(`ai` + `@ai-sdk/openai` + `@ai-sdk/openai-compatible`)
11
- - 校验契约:Zod(`shared/routes.ts`)
12
-
13
- ## 目录结构(重点)
14
-
15
- - `client/index.html`:前端入口 HTML
16
- - `client/public/`:前端静态资源
17
- - `client/src/components/ui/`:shadcn/ui 组件
18
- - `client/src/hooks/`:前端 hooks
19
- - `client/src/lib/`:前端通用工具
20
- - `server/`:Hono 后端入口与路由
21
- - `shared/`:前后端共享契约与 schema
22
-
23
- ## AI 网关基础设施
24
-
25
- 模板已内置最薄的 AI 基础设施,不带默认业务页面:
26
-
27
- - 服务端统一入口:`POST /api/ai/chat`
28
- - 服务端默认架构:Hono API -> Vercel AI SDK -> OpenAI / OpenAI 兼容模型
29
- - 当前默认支持的小文件附件类型:`image/*`、`text/*`、`application/pdf`
30
- - 附件限制:最多 3 个文件,总大小不超过 5MB,且必须以内联 data URL 提交
9
+ - 数据层:TypeORM(默认 `sqlite`,支持切换 `postgres`)
10
+ - 契约校验:Zod(`shared/contracts/routes.ts`,兼容 `shared/routes.ts` re-export)
11
+
12
+ ## 目录结构
13
+
14
+ ```text
15
+ .
16
+ ├── .imagicma/
17
+ │ ├── AGENTS.md # 运行/修改约束补充说明
18
+ │ └── runtime.env # 项目运行端口契约,仅使用大写 PORT
19
+ ├── client/
20
+ │ ├── index.html # Vite 前端入口 HTML
21
+ │ ├── public/ # 前端静态资源
22
+ │ └── src/
23
+ │ ├── App.tsx # 前端应用根组件
24
+ │ ├── main.tsx # 前端挂载入口
25
+ │ ├── globals.css # 全局样式与设计 token
26
+ │ ├── components/ # 页面级与基础组件
27
+ │ │ └── ui/ # shadcn/ui 组件
28
+ │ ├── hooks/ # 客户端 hooks
29
+ │ ├── lib/ # 前端通用工具与预览桥接逻辑
30
+ │ └── pages/ # 路由页面
31
+ ├── server/
32
+ │ ├── app.ts # Hono 装配入口,仅注册 middleware、routes、静态资源
33
+ │ ├── dev-app.ts # 开发态 server 入口
34
+ │ ├── index.ts # 生产态 server 入口
35
+ │ ├── controllers/ # HTTP 适配层,解析请求并返回响应
36
+ │ ├── db/ # DataSource 与数据库配置
37
+ │ ├── middlewares/ # 日志、异常等横切能力
38
+ │ ├── models/
39
+ │ │ ├── entities/ # TypeORM entity 等持久化模型
40
+ │ │ ├── repositories/ # 数据访问实现
41
+ │ │ ├── services/ # 业务规则与编排
42
+ │ │ └── types/ # 仅服务端内部使用的类型
43
+ │ └── routes/ # 路由绑定层,只做 URL 到 controller 的映射
44
+ ├── shared/
45
+ │ ├── constants/ # 前后端共享纯常量
46
+ │ ├── contracts/ # 前后端共享 API path 与 Zod 契约
47
+ │ ├── routes.ts # contracts 的兼容 re-export
48
+ │ ├── schema.ts # 共享 schema 聚合导出
49
+ │ └── types/ # 前后端共享纯 TypeScript 类型
50
+ ├── scripts/ # 受保护的启动/守卫脚本
51
+ ├── .env.example # 环境变量示例
52
+ ├── package.json
53
+ ├── tsconfig.json
54
+ ├── tsconfig.server.json
55
+ └── vite.config.ts
56
+ ```
31
57
 
32
- 推荐做法:
58
+ ## 分层约束
33
59
 
34
- - 前端页面调用项目内 `/api/ai/chat`
35
- - 不要把模型密钥暴露到浏览器
36
- - 需要聊天 UI 时,优先使用 `@ai-sdk/react` 的 `useChat` / `DefaultChatTransport` 对接服务端 UI message stream
37
- - 如果暂时不引入 `@ai-sdk/react`,请复用 `client/src/lib/ai-ui-stream.ts`,不要手写 SSE 解析器
38
- - OpenAI 官方走 `AI_PROVIDER=openai`
39
- - MiniMax / Kimi 这类 OpenAI 兼容供应商,优先走 `AI_PROVIDER=minimax|kimi` 或 `openai-compatible`
60
+ - `server/routes` 只能依赖 `controllers`
61
+ - `server/controllers` 只负责 HTTP 适配,依赖 `models` 与 `shared`
62
+ - `server/models/repositories` 只负责数据读写,不返回 HTTP 响应结构
63
+ - `server/models/services` 负责业务规则、聚合与默认值,不接触 Hono `Context`
64
+ - `shared/` 只能放前后端都能安全复用的契约、常量和纯类型,禁止放 TypeORM entity、Node API、Hono 运行时代码
40
65
 
41
- ## 开发
66
+ ## 常用命令
42
67
 
43
68
  ```bash
44
69
  pnpm install
45
- start_app.sh
70
+ pnpm dev
71
+ pnpm build
72
+ pnpm start
73
+ pnpm check
74
+ pnpm lint
46
75
  ```
47
76
 
77
+ - `pnpm dev`:本地开发,使用受保护脚本拉起 Vite + Hono
78
+ - `pnpm build`:构建前端并编译服务端
79
+ - `pnpm start`:以生产模式启动构建产物
80
+ - `pnpm check`:执行前后端 TypeScript 类型检查
81
+ - `pnpm lint`:执行 ESLint
82
+
83
+ 如果你是在 imagicma 的 agent/workflow 环境中维护该模板,请继续遵循 [`AGENTS.md`](/Users/alexliu/Project/imagicma-all/imagicma-template/hono-app/AGENTS.md) 的约束,通过工作流提供的启动入口执行,不要绕过 `scripts/` 里的受保护脚本。
84
+
85
+ ## 端口与运行时文件
86
+
48
87
  启动时按以下顺序解析端口:
49
88
 
50
89
  1. 显式环境变量 `PORT`
51
90
  2. 项目内 `.imagicma/runtime.env`
52
91
 
53
- 如果两者都不存在,启动会直接失败。
54
- 项目内唯一允许的端口文件是 `.imagicma/runtime.env`,内容示例:
92
+ 如果两者都不存在,启动会直接失败。`.imagicma/runtime.env` 示例:
55
93
 
56
94
  ```bash
57
95
  PORT=6424
58
96
  ```
59
97
 
60
- 请勿直接执行 `vite` 或 `pnpm dev` 启动,必须使用 `restart_workflow`。
61
-
62
- - `build` 产出:
63
- - 前端:`dist/client`
64
- - 后端:`dist/server`
65
- - `start`:由受保护脚本启动服务(禁止直接 `node dist/server/index.js`),并由 Hono 承载 API + 静态资源 + SPA fallback。
66
- - 生产启动同样按 `PORT -> .imagicma/runtime.env` 的顺序解析端口。
67
-
68
- ## 数据库
69
-
70
- 首次运行前初始化结构:
71
-
72
- ```bash
73
- pnpm db:push
74
- ```
75
-
76
- - 请先复制 `.env.example` 为 `.env.local`
77
- - 在 `.env.local` 中设置 `DATABASE_URL`
78
- - `pnpm db:push` 会将 schema 推送到该 PostgreSQL 数据库
79
-
80
- ## AI 配置
81
-
82
- 请在 `.env.local` 中配置:
83
-
84
- - `AI_PROVIDER`:可选;支持 `openai`、`openai-compatible`、`minimax`、`kimi`
85
- - `AI_API_KEY`:必填
86
- - `AI_BASE_URL`:可选;接 OpenAI 兼容网关时填写。`minimax` / `kimi` 留空时自动走官方默认地址
87
- - `AI_MODEL`:可选;默认 `gpt-4o-mini`
88
-
89
- 兼容说明:
90
-
91
- - 旧变量 `OPENAI_API_KEY` / `OPENAI_BASE_URL` 仍然可用,但新项目建议改用通用命名的 `AI_*`
92
- - `minimax` 与 `kimi` 默认都走 OpenAI 兼容模式,不需要额外改代码
93
- - 如果接其它兼容供应商,使用 `AI_PROVIDER=openai-compatible` 并填写 `AI_BASE_URL`
94
-
95
- 模板内置的 AI 路由会优先读取这些环境变量,再由服务端统一调用模型供应商。
98
+ ## 构建产物
96
99
 
97
- 补充说明:
100
+ - 前端产物:`dist/client`
101
+ - 后端产物:`dist/server`
98
102
 
99
- - `/api/ai/chat` 返回的是 AI SDK 5 的 UI message stream(SSE `data:` 事件,不是旧的 `0:"..."` 文本流)
100
- - 如果模型网关不可达,优先检查 `AI_BASE_URL`、网络连通性与服务商侧配置
103
+ 生产启动时由 Hono 同时承载 API、静态资源与 SPA fallback。
101
104
 
102
- 最小请求示例:
103
-
104
- ```bash
105
- curl -N \
106
- -X POST http://127.0.0.1:6424/api/ai/chat \
107
- -H 'Content-Type: application/json' \
108
- -d '{
109
- "messages": [
110
- {
111
- "role": "user",
112
- "parts": [
113
- { "type": "text", "text": "请用一句话介绍这个项目的 AI 网关职责。" }
114
- ]
115
- }
116
- ]
117
- }'
118
- ```
119
-
120
- 常见配置示例:
105
+ ## 数据库
121
106
 
122
- ```bash
123
- # OpenAI 官方
124
- AI_PROVIDER=openai
125
- AI_API_KEY=<your-openai-key>
126
- AI_MODEL=gpt-4o-mini
127
-
128
- # MiniMax
129
- AI_PROVIDER=minimax
130
- AI_API_KEY=<your-minimax-key>
131
- AI_MODEL=MiniMax-M2.7
132
-
133
- # Kimi
134
- AI_PROVIDER=kimi
135
- AI_API_KEY=<your-kimi-key>
136
- AI_MODEL=kimi-k2.5
137
-
138
- # 其它 OpenAI 兼容供应商
139
- AI_PROVIDER=openai-compatible
140
- AI_API_KEY=<your-provider-key>
141
- AI_BASE_URL=https://api.provider.com/v1
142
- AI_MODEL=<provider-model-id>
143
- ```
107
+ 服务启动时会自动同步当前示例表结构。
144
108
 
145
- ## 运行时文件
109
+ - 默认数据库:`DB_TYPE=sqlite`
110
+ - 默认数据库文件:`./.data/app.db`
111
+ - 自定义 SQLite 路径:复制 `.env.example` 为 `.env.local` 后设置 `DATABASE_FILE`
112
+ - 切换 Postgres:
113
+ - `DB_TYPE=postgres`
114
+ - `DATABASE_URL=postgres://user:pass@host:5432/dbname`
146
115
 
147
- - `.imagicma/runtime.env`:项目运行端口契约,只使用大写 `PORT`
116
+ 数据库类型建议在项目首次启动前确定;初始化后默认按固定配置处理,不承诺无损切换。
148
117
 
149
118
  ## 验证路径
150
119
 
151
120
  - API:`GET /api/greeting`
152
- - AI:`POST /api/ai/chat`
153
- - 页面:`/hello`
121
+ - 页面:`/`
@@ -3,7 +3,8 @@
3
3
  <head>
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>hono-app</title>
6
+ <link rel="icon" type="image/png" href="/favicon.png" />
7
+ <title>Agentma App · Powered by agentma.cn</title>
7
8
  <script src="/imagicma-picker-bridge.js"></script>
8
9
  <script src="/imagicma-preview-feedback.js"></script>
9
10
  </head>
@@ -3,11 +3,7 @@
3
3
  var VERSION = 1;
4
4
  var MAX_TEXT_LENGTH = 240;
5
5
  var MAX_SELECTOR_LENGTH = 512;
6
- var PROD_PARENT_ORIGINS = {
7
- 'https://agentma.cn': true,
8
- 'https://imagicma.cn': true,
9
- 'https://nanocoda.com': true,
10
- };
6
+ var PROD_PARENT_ORIGIN = 'https://imagicma.cn';
11
7
  var LOCAL_PARENT_RE = /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/i;
12
8
  var LOCAL_IMAGICMA_PARENT_RE = /^https?:\/\/([a-z0-9-]+\.)?local\.imagicma\.cn(:\d+)?$/i;
13
9
 
@@ -25,7 +21,7 @@
25
21
 
26
22
  function isAllowedParentOrigin(origin) {
27
23
  if (!origin) return false;
28
- return !!PROD_PARENT_ORIGINS[origin] || LOCAL_PARENT_RE.test(origin) || LOCAL_IMAGICMA_PARENT_RE.test(origin);
24
+ return origin === PROD_PARENT_ORIGIN || LOCAL_PARENT_RE.test(origin) || LOCAL_IMAGICMA_PARENT_RE.test(origin);
29
25
  }
30
26
 
31
27
  function isRecord(value) {
@@ -4,7 +4,6 @@
4
4
  var PROD_PARENT_ORIGINS = {
5
5
  'https://agentma.cn': true,
6
6
  'https://imagicma.cn': true,
7
- 'https://nanocoda.com': true
8
7
  };
9
8
  var LOCAL_PARENT_RE = /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/i;
10
9
  var LOCAL_IMAGICMA_PARENT_RE = /^https?:\/\/([a-z0-9-]+\.)?local\.(agentma\.cn|imagicma\.cn)(:\d+)?$/i;
@@ -62,7 +62,7 @@ function EnvHint() {
62
62
  return (
63
63
  <div className="text-sm text-muted-foreground">
64
64
  <p>
65
- 默认使用 PostgreSQL。若 API 报错,请先在 <code>.env.local</code> 设置 <code>DATABASE_URL</code>,再执行 <code>pnpm db:push</code> 初始化数据库。
65
+ 默认使用 <code>DB_TYPE=sqlite</code> <code>./.data/app.db</code>,服务启动时会自动同步示例表。若要切到 Postgres,可在 <code>.env.local</code> 设置 <code>DB_TYPE=postgres</code> <code>DATABASE_URL</code>;数据库类型建议在首次启动前确定。
66
66
  </p>
67
67
  </div>
68
68
  );