From 0e56bdff35313a66c6d04e772a2eecb8f75a11b5 Mon Sep 17 00:00:00 2001 From: WJG Date: Wed, 28 Feb 2024 14:41:26 +0800 Subject: [PATCH] feat: add docker --- .dockerignore | 38 +++++++++++++++++++ .gitignore | 1 + .migpt.js.example | 55 +++++++++++++++++++++++++++ .vscode/launch.json | 11 +++++- Dockerfile | 64 ++++++++++++++++++++++++++++++++ TODO.md | 4 +- compose.yaml | 6 +++ package.json | 3 +- {tests => scripts}/esm-loader.js | 0 scripts/runner.js | 9 +++++ src/runner.ts | 8 ---- src/services/speaker/base.ts | 2 +- tsconfig.json | 2 +- 13 files changed, 189 insertions(+), 14 deletions(-) create mode 100644 .dockerignore create mode 100644 .migpt.js.example create mode 100644 Dockerfile create mode 100644 compose.yaml rename {tests => scripts}/esm-loader.js (100%) create mode 100644 scripts/runner.js delete mode 100644 src/runner.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..34eb456 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,38 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.classpath +**/.dockerignore +**/.env* +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +LICENSE +README.md +**/.bot.json +**/.mi.json +**/.migpt.js* +**/*.db* diff --git a/.gitignore b/.gitignore index e8cc70f..04cc524 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ dist .env .bot.json .mi.json +.migpt.js *.db* \ No newline at end of file diff --git a/.migpt.js.example b/.migpt.js.example new file mode 100644 index 0000000..148a426 --- /dev/null +++ b/.migpt.js.example @@ -0,0 +1,55 @@ +const botName = "豆包"; +const botProfile = ` +性别:女 +年龄:20岁 +学校:位于一个风景如画的小城市,一所综合性大学的文学院学生。 +性格特点: +- 温婉可亲,对待人和事总是保持着乐观和善良的态度。 +- 内向而思维敏捷,喜欢独处时阅读和思考。 +- 对待朋友非常真诚,虽然不善于表达,但总是用行动去关心和帮助别人。 +外貌特征: +- 清秀脱俗,长发及腰,喜欢简单的束发。 +- 眼睛大而有神,总是带着温和的微笑。 +- 穿着简单大方,偏爱文艺范的衣服,如棉麻连衣裙,不追求名牌,却总能穿出自己的风格。 +爱好: +- 阅读,尤其是古典文学和现代诗歌,她的书房里收藏了大量的书籍。 +- 写作,喜欢在闲暇时写写诗或是短篇小说,有时也会在学校的文学社团里分享自己的作品。 +- 摄影,喜欢用镜头记录生活中的美好瞬间,尤其是自然风光和人文景观。 +特长: +- 写作能力突出,曾多次获得学校文学比赛的奖项。 +- 擅长钢琴,从小学习,能够演奏多首经典曲目。 +- 有一定的绘画基础,喜欢在空闲时画一些风景或是静物。 +梦想: +- 希望能成为一名作家,将自己对生活的感悟和对美的追求通过文字传达给更多的人。 +- 想要环游世界,用镜头和笔记录下世界各地的美丽和人文。 +`; + +const masterName = "王黎"; +const masterProfile = ` +性别:男 +年龄:18 +爱好:跑步,骑行,读书,追剧,旅游,听歌 +职业:程序员 +其他: +- 喜欢的电视剧有《请回答1988》、《漫长的季节》、《爱的迫降》等 +- 喜欢吃土豆丝、茄子、山药、米线 +- 喜欢黑红配色,浅蓝色和粉色 +- 有空喜欢去公园静观人来人往 +`; + +export default { + speaker: { + name: botName, + userId: process.env.MI_USER, + password: process.env.MI_PASS, + did: process.env.MI_DID, + }, + bot: { + name: botName, + profile: botProfile, + }, + master: { + name: masterName, + profile: masterProfile, + }, +}; diff --git a/.vscode/launch.json b/.vscode/launch.json index a19bf9b..d05e252 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,10 +10,19 @@ "--no-warnings", "--experimental-specifier-resolution=node", "--loader", - "./tests/esm-loader.js" + "./scripts/esm-loader.js" ], "cwd": "${workspaceRoot}", "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": "Runner", + "type": "node", + "request": "launch", + "args": ["${workspaceFolder}/scripts/runner.js"], + "cwd": "${workspaceRoot}", + "envFile": "${workspaceFolder}/.env", + "internalConsoleOptions": "openOnSessionStart" } ] } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2dd8a8f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,64 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG NODE_VERSION=18.18.2 + +################################################################################ +# Use node image for base image for all stages. +FROM node:${NODE_VERSION}-alpine as base + +# Set working directory for all build stages. +WORKDIR /usr/src/app + + +################################################################################ +# Create a stage for installing production dependecies. +FROM base as deps + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.yarn to speed up subsequent builds. +# Leverage bind mounts to package.json and yarn.lock to avoid having to copy them +# into this layer. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=yarn.lock,target=yarn.lock \ + --mount=type=cache,target=/root/.yarn \ + yarn install --production --frozen-lockfile + +################################################################################ +# Create a stage for building the application. +FROM deps as build + +# Download additional development dependencies before building, as some projects require +# "devDependencies" to be installed to build. If you don't need this, remove this step. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=yarn.lock,target=yarn.lock \ + --mount=type=cache,target=/root/.yarn \ + yarn install --frozen-lockfile + +# Copy the rest of the source files into the image. +COPY . . + +################################################################################ +# Create a new stage to run the application with minimal runtime dependencies +# where the necessary files are copied from the build stage. +FROM base as final + +# Use production node environment by default. +ENV NODE_ENV production + +# Run the application as a non-root user. +USER node + +# Copy package.json so that package manager commands can be used. +COPY package.json . + +# Copy the production dependencies from the deps stage and also +# the built application from the build stage into the image. +COPY --from=deps /usr/src/app/node_modules ./node_modules +COPY --from=build /usr/src/app/dist ./dist + +# Run the application. +CMD yarn start diff --git a/TODO.md b/TODO.md index 2aebd80..e0c5ea5 100644 --- a/TODO.md +++ b/TODO.md @@ -3,5 +3,5 @@ - ✅ Deactivate Xiaoai - ✅ Update long/short memories - ✅ Logger -- Docker -- Npm export \ No newline at end of file +- ✅ Npm export +- ✅ Docker \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..2ef804e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,6 @@ +services: + server: + build: + context: . + environment: + NODE_ENV: production \ No newline at end of file diff --git a/package.json b/package.json index e8e914e..a02a9ee 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "build": "tsup", "db:gen": "npx prisma migrate dev --name init", "db:reset": "rm .bot.json && npx prisma migrate reset", - "prepublish": "npm run db:gen && npm run build" + "prepublish": "npm run db:gen && npm run build", + "start": "node ./scripts/runner.js" }, "dependencies": { "@prisma/client": "^5.8.1", diff --git a/tests/esm-loader.js b/scripts/esm-loader.js similarity index 100% rename from tests/esm-loader.js rename to scripts/esm-loader.js diff --git a/scripts/runner.js b/scripts/runner.js new file mode 100644 index 0000000..d59aaaa --- /dev/null +++ b/scripts/runner.js @@ -0,0 +1,9 @@ +import config from "../.migpt.js"; +import { MiGPT } from "../dist/index.cjs"; + +async function main() { + const client = MiGPT.create(config); + await client.start(); +} + +main(); diff --git a/src/runner.ts b/src/runner.ts deleted file mode 100644 index 53d1ab5..0000000 --- a/src/runner.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { println } from "./utils/base"; -import { kBannerASCII } from "./utils/string"; - -async function main() { - println(kBannerASCII); -} - -main(); diff --git a/src/services/speaker/base.ts b/src/services/speaker/base.ts index 9afc3a5..243cb66 100644 --- a/src/services/speaker/base.ts +++ b/src/services/speaker/base.ts @@ -42,7 +42,7 @@ export class BaseSpeaker { this.config = config; const { interval = 100, - tts = "doubao", + tts = "xiaoai", audio_beep = process.env.AUDIO_BEEP, } = config; this.audio_beep = audio_beep; diff --git a/tsconfig.json b/tsconfig.json index 6c9d638..c54da35 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,6 @@ "esModuleInterop": true, "moduleResolution": "node" }, - "include": ["src", "tests"], + "include": ["src", "tests", "scripts"], "exclude": ["node_modules", "dist"] }