From 2a9a833cd7aa02fba41aac13cf7a1dfa460016c6 Mon Sep 17 00:00:00 2001 From: chensipeng Date: Wed, 13 May 2026 06:44:26 +0000 Subject: [PATCH] =?UTF-8?q?chore(dev):=20=E9=85=8D=E7=BD=AE=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=BC=80=E5=8F=91=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 引入 devenv 与 starship 配置,统一 Go/Node/Postgres 开发依赖与快捷命令。 同时让脚本在使用本地数据库环境时跳过 Docker 创建步骤。 --- .gitignore | 11 ++++++ devenv.lock | 65 +++++++++++++++++++++++++++++++ devenv.nix | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ devenv.yaml | 18 +++++++++ scripts/dev.sh | 6 ++- starship.toml | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 devenv.lock create mode 100644 devenv.nix create mode 100644 devenv.yaml create mode 100644 starship.toml diff --git a/.gitignore b/.gitignore index 35c0196..8313207 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,14 @@ coverage/ .idea .gitignore + +# Devenv +.devenv* +devenv.local.nix +devenv.local.yaml + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml diff --git a/devenv.lock b/devenv.lock new file mode 100644 index 0000000..9288a0d --- /dev/null +++ b/devenv.lock @@ -0,0 +1,65 @@ +{ + "nodes": { + "devenv": { + "locked": { + "dir": "src/modules", + "lastModified": 1778613747, + "narHash": "sha256-+FdF9iIvBQIC391Xkoso3IFIl/Iqp2NolSvCOgEIm78=", + "owner": "cachix", + "repo": "devenv", + "rev": "c9ee1d61986a6dde1cf45e738b01395cd5bce470", + "type": "github" + }, + "original": { + "dir": "src/modules", + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "nixpkgs": { + "inputs": { + "nixpkgs-src": "nixpkgs-src" + }, + "locked": { + "lastModified": 1778507786, + "narHash": "sha256-HzSQCKMsMr8r55LwM1JuzIOB+8bzk0FEv6sItKvsfoY=", + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "8f24a228a782e24576b155d1e39f0d914b380691", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, + "nixpkgs-src": { + "flake": false, + "locked": { + "lastModified": 1778274207, + "narHash": "sha256-I4puXmX1iovcCHZlRmztO3vW0mAbbRvq4F8wgIMQ1MM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b3da656039dc7a6240f27b2ef8cc6a3ef3bccae7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} \ No newline at end of file diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 0000000..216601e --- /dev/null +++ b/devenv.nix @@ -0,0 +1,104 @@ +{ + pkgs, + lib, + config, + inputs, + ... +}: + +{ + starship = { + enable = true; + config = { + enable = true; + path = ./starship.toml; + }; + }; + + env = { + AI_GATEWAY_DATABASE_NAME = "easyai_ai_gateway"; + AI_GATEWAY_DATABASE_URL = "host=${config.env.DEVENV_RUNTIME}/postgres dbname=easyai_ai_gateway sslmode=disable"; + AI_GATEWAY_SKIP_DB_CREATE = "1"; + }; + + packages = with pkgs; [ + curl + docker-client + git + jq + lsof + postgresql_18 + ripgrep + watchexec + ]; + + scripts = { + dev.exec = "pnpm dev"; + build.exec = "pnpm build"; + test-all.exec = "pnpm test"; + lint.exec = "pnpm lint"; + migrate.exec = "pnpm migrate"; + db-create.exec = "pnpm db:create"; + api-test.exec = "pnpm nx run api:test"; + web-build.exec = "pnpm nx run web:build"; + }; + + services.postgres = { + enable = true; + package = pkgs.postgresql_18.withPackages (postgresPackages: [ + postgresPackages.pgvector + ]); + listen_addresses = ""; + initialDatabases = [ + { + name = "easyai_ai_gateway"; + initialSQL = '' + CREATE EXTENSION IF NOT EXISTS pgcrypto; + CREATE EXTENSION IF NOT EXISTS vector; + ''; + } + ]; + }; + + # https://devenv.sh/languages/ + languages.go = { + enable = true; + package = pkgs.go; + }; + + languages.javascript = { + enable = true; + package = pkgs.nodejs_22; + nodejs.enable = true; + lsp.enable = true; + pnpm = { + enable = true; + install.enable = true; + }; + }; + + enterShell = '' + echo "" + echo "EasyAI AI Gateway 开发环境已就绪" + echo "当前目录:$PWD" + echo "" + echo "运行时版本:" + echo " go: $(go version | awk '{print $3}')" + echo " node: $(node --version)" + echo " pnpm: $(pnpm --version)" + echo " psql: $(psql --version | awk '{print $3}')" + echo "" + echo "常用命令:" + echo " dev 创建/迁移数据库,并启动 API 和 Web" + echo " test-all 运行 API 和 Web 测试目标" + echo " build 构建 API 和 Web" + echo " lint 运行 Web 与 contracts 类型检查" + echo " migrate 执行 API 数据库迁移" + echo " db-create 创建 AI Gateway 数据库" + echo " api-test 只运行 Go API 测试" + echo " web-build 只构建 Web 前端" + echo "" + echo "提示:根 package.json 是唯一脚本入口;上述短命令由 devenv scripts 转发。" + echo "" + ''; +} diff --git a/devenv.yaml b/devenv.yaml new file mode 100644 index 0000000..4da5c66 --- /dev/null +++ b/devenv.yaml @@ -0,0 +1,18 @@ +# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json +inputs: + nixpkgs: + url: github:cachix/devenv-nixpkgs/rolling + +# If you're using non-OSS software, you can set allowUnfree to true. +# allowUnfree: true + +# If you're not willing to allow unsupported packages: +# allowUnsupportedSystem: false + +# If you're willing to use a package that's vulnerable +# permittedInsecurePackages: +# - "openssl-1.1.1w" + +# If you have more than one devenv you can merge them +#imports: +# - ./backend diff --git a/scripts/dev.sh b/scripts/dev.sh index 167879d..2fbbc06 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -68,7 +68,11 @@ export AI_GATEWAY_DATABASE_URL="${AI_GATEWAY_DATABASE_URL:-postgresql://${AI_GAT echo "[ai-gateway] using database: ${AI_GATEWAY_DATABASE_URL}" -scripts/create-database.sh +if [[ "${AI_GATEWAY_SKIP_DB_CREATE:-}" == "1" ]]; then + echo "[ai-gateway] skipping Docker database creation" +else + scripts/create-database.sh +fi pnpm nx run api:migrate stop_stale_api_processes exec pnpm nx run-many -t dev -p api web --parallel=2 diff --git a/starship.toml b/starship.toml new file mode 100644 index 0000000..a0aa891 --- /dev/null +++ b/starship.toml @@ -0,0 +1,101 @@ +# --- 全局结构 (极致紧凑,高信息密度,AI友好) --- +format = """ +$directory\ +$git_branch\ +$git_status\ +$nix_shell\ +$nodejs\ +$bun\ +$rust\ +$golang\ +$cmd_duration\ +$memory_usage\ +$status\ +$line_break\ +$character""" + +# --- 目录 --- +[directory] +style = "bold cyan" +truncation_length = 3 +truncate_to_repo = false +truncation_symbol = ".../" +home_symbol = "~" +read_only = " [RO]" + +# --- Git 状态 (纯文本,避免解析错误与乱码) --- +[git_branch] +symbol = "git:" +style = "bold purple" +format = "[$symbol$branch]($style) " + +[git_status] +format = "[$all_status$ahead_behind]($style) " +style = "bold red" +conflicted = "=" +ahead = ">" +behind = "<" +diverged = "<>" +untracked = "?" +stashed = "*" +modified = "!" +staged = "+" +renamed = "»" +deleted = "x" + +# --- 编程语言与环境 (紧凑标签格式) --- +[nodejs] +symbol = "node:" +style = "bold green" +format = "[$symbol$version]($style) " +detect_files = ["package.json", ".node-version"] + +[bun] +symbol = "bun:" +style = "bold blue" +format = "[$symbol$version]($style) " + +[rust] +symbol = "rust:" +style = "bold 208" +format = "[$symbol$version]($style) " + +[golang] +symbol = "go:" +style = "bold cyan" +format = "[$symbol$version]($style) " + +[nix_shell] +symbol = "nix:" +style = "bold blue" +format = "[$symbol$state]($style) " +impure_msg = "impure" +pure_msg = "pure" + +# --- AI 辅助决策信息 (性能与状态反馈) --- +[cmd_duration] +min_time = 2_000 +format = "took [$duration]($style) " +style = "bold yellow" + +[memory_usage] +symbol = "mem:" +disabled = false +threshold = 75 +format = "[$symbol$ram_pct]($style) " +style = "bold dimmed white" + +[status] +disabled = false +format = "[ERR:$status]($style) " +style = "bold red" + +# --- 交互符号 (带明确状态码) --- +[character] +success_symbol = "[>](bold green)" +error_symbol = "[>](bold red)" +vicmd_symbol = "[<](bold green)" + +# --- 兼容性补充 --- +[package] +disabled = true