Skip to content

feat(server): migrate db to node:sqlite and harden local APIs#13

Merged
wynxing merged 7 commits into
mainfrom
feat/back
Jun 23, 2026
Merged

feat(server): migrate db to node:sqlite and harden local APIs#13
wynxing merged 7 commits into
mainfrom
feat/back

Conversation

@wynxing

@wynxing wynxing commented Jun 23, 2026

Copy link
Copy Markdown
Owner

背景

本 PR 继续后端基础设施整理:将数据库层迁移到 Node 24 的 node:sqlite,补齐 server-local 路由校验、安全边界和错误处理,并修复知识库/embedding 相关问题。

主要变更

  • 数据库层从 better-sqlite3/Drizzle 迁移到 node:sqlite,新增手写 row types,并更新 schema 初始化、查询映射和测试。
  • server-local 引入 TypeBox/Fastify type provider、统一错误响应、helmet/rate-limit,以及更明确的路由模块边界。
  • 共享包新增运行时可校验的请求/响应 schema,/api/tasks 响应改为 { items, total },desktop API 端保持调用方仍拿到 Task[]
  • 修复知识库任务重排时无列任务的 NULL 语义,以及 embedding 配置避免回写明文 API key。
  • 补充 node:sqlite spike 文档、架构/开发文档和相关验证脚本说明。

验证

  • git diff --check origin/main...HEAD
  • pnpm typecheck
  • pnpm --filter @neo-companion/db test
  • pnpm --filter @neo-companion/server-local test
  • pnpm --filter @neo-companion/shared test
  • GitHub CI Check:包含 pnpm typecheckpnpm test./scripts/verify-docs.sh
  • GitHub CI Build:Windows、macOS、Ubuntu

备注

  • 需要 Node.js >=24.0.0,因为数据库层使用 node:sqlite
  • 本地 Windows 工作区下 pnpm test 可能因 markdown fixture 被 checkout 为 CRLF 而复现 3 个 byte-for-byte 换行失败;GitHub CI 使用 LF checkout,当前 PR 的 CI 已全绿。

wynxing added 7 commits June 22, 2026 20:58
P0 基础设施:
- shared 包新增 TypeBox schemas(tasks/tts/ai/focus/hooks/knowledge/errors)
- 新增 typed error 层级(HttpError + BadRequest/NotFound/Unauthorized/Forbidden/Stale/ServiceUnavailable)
- app.ts 接入 setErrorHandler,统一错误响应为 { error, code?, details? }
- 所有路由用手写 if 校验改为 schema + typed error,清理 as Type 断言
- WsMessage 新增 tts:error / server:shutdown 类型

P1-A 安全与健壮性:
- 接入 @fastify/helmet(禁用 CSP/HSTS,sidecar 只吐 JSON)
- 接入 @fastify/rate-limit(200 req/min,IP 维度)
- 显式 bodyLimit 1MB
- SIGINT/SIGTERM 优雅退出:app.close() → exit 0
- WsHub.close() 广播 server:shutdown 并关闭所有 WS(1001)

P1-B WebSocket 加固:
- WsHub 重写:30s 心跳 ping/pong,无响应连接 terminate
- 背压保护:bufferedAmount > 1MB 跳过,连续 5 次丢弃则 terminate
- WS 路由:消息大小限制 64KB(超限 close 1009)
- 每连接 token bucket 频率限制 30 msg/s(超限 close 1008)

P1-C 业务增强:
- Tasks 分页:list({limit,offset}) 返回 {items,total},前端 listTasks 适配
- Weather in-memory 缓存(TTL 10min,NEO_WEATHER_CACHE_TTL_MS 可配)
- TTS 失败广播 tts:error(路由 try/catch + focus 自动播报失败)

验证:typecheck 全绿;server-local 53/53、db 20/20、ai 8/8、tts 1/1 测试通过。
@wynxing wynxing marked this pull request as ready for review June 23, 2026 01:20
@fennoai

fennoai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Important

Fenno AI is not enabled for this organization

Fenno AI is currently in internal testing and is not yet open for external organizations.

If you would like to request access, sign in to Fenno AI Console, choose Give Feedback in the lower-left corner, and tell us what you need. We will follow up promptly.

@wynxing wynxing merged commit 44bfdfa into main Jun 23, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant