From da4a0c8f690f7af45fed182dcd176b3e38953298 Mon Sep 17 00:00:00 2001 From: tbxark Date: Thu, 14 Nov 2024 23:21:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20vercel=20=E7=89=88=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20NextChatAgent=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 22 +++++++++++----------- packages/apps/vercel/package.json | 5 ++++- packages/apps/vercel/src/index.ts | 4 +++- packages/apps/vercel/tsconfig.json | 3 ++- packages/apps/workers-next/package.json | 2 +- packages/apps/workers/package.json | 2 +- pnpm-lock.yaml | 9 +++++++++ wrangler-example.toml | 8 +++++--- 8 files changed, 36 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 35fd61c3..c3f8b20e 100644 --- a/package.json +++ b/package.json @@ -23,22 +23,22 @@ "lint": "eslint --fix *.js *.ts packages plugins scripts", "version": "tsx scripts/gen-version.ts", "build": "pnpm -r run build", - "build:plugins": "pnpm --filter @chatgpt-telegram-workers/plugins... build", - "build:core": "pnpm --filter @chatgpt-telegram-workers/core... build", - "build:next": "pnpm --filter @chatgpt-telegram-workers/next... build", - "build:local": "pnpm --filter @chatgpt-telegram-workers/local... build", - "build:vercel": "pnpm --filter @chatgpt-telegram-workers/vercel... build", - "build:workers": "pnpm --filter @chatgpt-telegram-workers/workers... build", - "build:workersnext": "pnpm --filter @chatgpt-telegram-workers/workers-next... build", - "build:interpolate": "pnpm --filter @chatgpt-telegram-workers/interpolate... build", + "build:plugins": "pnpm run --filter @chatgpt-telegram-workers/plugins... build", + "build:core": "pnpm run --filter @chatgpt-telegram-workers/core... build", + "build:next": "pnpm run --filter @chatgpt-telegram-workers/next... build", + "build:local": "pnpm run --filter @chatgpt-telegram-workers/local... build", + "build:vercel": "pnpm run --filter @chatgpt-telegram-workers/vercel... build", + "build:workers": "pnpm run --filter @chatgpt-telegram-workers/workers... build", + "build:workersnext": "pnpm run --filter @chatgpt-telegram-workers/workers-next... build", + "build:interpolate": "pnpm run --filter @chatgpt-telegram-workers/interpolate... build", "build:docker": "docker build -t chatgpt-telegram-workers:latest .", "build:dockerx": "docker buildx build --platform linux/amd64,linux/arm64 -t chatgpt-telegram-workers:latest .", "build:dist": "pnpm run version && pnpm run build:workers && cp -r packages/apps/workers/dist/index.js dist/", "deploy:dist": "pnpm run build:dist && wrangler deploy", - "deploy:workers": "pnpm run build:workers && TOML_PATH=$INIT_CWD/wrangler.toml pnpm --filter @chatgpt-telegram-workers/workers deploy", - "deploy:workersnext": "pnpm run build:workersnext && TOML_PATH=$INIT_CWD/wrangler.toml pnpm --filter @chatgpt-telegram-workers/workers-next deploy", + "deploy:workers": "pnpm run build:workers && && TOML_PATH=$INIT_CWD/wrangler.toml pnpm run --filter @chatgpt-telegram-workers/workers deploy", + "deploy:workersnext": "pnpm run build:workersnext && TOML_PATH=$INIT_CWD/wrangler.toml pnpm run --filter @chatgpt-telegram-workers/workers-next deploy", "deploy:vercel": "pnpm run build:vercel && vercel --prod", - "start:local": "pnpm run build:local && CONFIG_PATH=$INIT_CWD/config.json TOML_PATH=$INIT_CWD/wrangler.toml pnpm --filter @chatgpt-telegram-workers/local start", + "start:local": "pnpm run build:local && CONFIG_PATH=$INIT_CWD/config.json TOML_PATH=$INIT_CWD/wrangler.toml pnpm run --filter @chatgpt-telegram-workers/local start", "vercel:syncenv": "tsx scripts/vercel-sync-env.ts && vercel --prod", "clean": "pnpm -r run clean", "wrangler": "wrangler" diff --git a/packages/apps/vercel/package.json b/packages/apps/vercel/package.json index 1b6c97d3..ddc686c7 100644 --- a/packages/apps/vercel/package.json +++ b/packages/apps/vercel/package.json @@ -10,10 +10,13 @@ }, "dependencies": { "@chatgpt-telegram-workers/core": "workspace:*", + "@chatgpt-telegram-workers/next": "workspace:*", "cloudflare-worker-adapter": "^1.3.3" }, "devDependencies": { "@types/react": "^18.3.12", - "@vercel/node": "^3.2.24" + "@vercel/node": "^3.2.24", + "openai": "^4.72.0", + "react-dom": "^18.3.1" } } diff --git a/packages/apps/vercel/src/index.ts b/packages/apps/vercel/src/index.ts index a752e3e1..86dc22df 100644 --- a/packages/apps/vercel/src/index.ts +++ b/packages/apps/vercel/src/index.ts @@ -1,6 +1,7 @@ import type { VercelRequest, VercelResponse } from '@vercel/node'; import * as process from 'node:process'; -import { createRouter, ENV } from '@chatgpt-telegram-workers/core'; +import { CHAT_AGENTS, createRouter, ENV } from '@chatgpt-telegram-workers/core'; +import { injectNextChatAgent } from '@chatgpt-telegram-workers/next'; import { UpStashRedis } from 'cloudflare-worker-adapter'; export default async function (request: VercelRequest, response: VercelResponse) { @@ -24,6 +25,7 @@ export default async function (request: VercelRequest, response: VercelResponse) ...process.env, DATABASE: cache, }); + injectNextChatAgent(CHAT_AGENTS); // remove this line if you don't use vercel ai sdk const router = createRouter(); let body: any | null = null; if (request.body) { diff --git a/packages/apps/vercel/tsconfig.json b/packages/apps/vercel/tsconfig.json index ccbf41f3..2ff50670 100644 --- a/packages/apps/vercel/tsconfig.json +++ b/packages/apps/vercel/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../../../tsconfig.json", "compilerOptions": { "rootDir": "./src", - "outDir": "./dist" + "outDir": "./dist", + "types": ["node", "react"] }, "references": [ { "path": "../../lib/core" } diff --git a/packages/apps/workers-next/package.json b/packages/apps/workers-next/package.json index 36f4407e..cfb50376 100644 --- a/packages/apps/workers-next/package.json +++ b/packages/apps/workers-next/package.json @@ -7,7 +7,7 @@ "scripts": { "build": "pnpm vite build", "clean": "rm -rf dist", - "deploy": "wrangler deploy --config=${TOML_PATH}" + "deploy": "wrangler deploy --config ${TOML_PATH}" }, "dependencies": { "@chatgpt-telegram-workers/core": "workspace:*", diff --git a/packages/apps/workers/package.json b/packages/apps/workers/package.json index 05b55a3d..f9a380ac 100644 --- a/packages/apps/workers/package.json +++ b/packages/apps/workers/package.json @@ -7,7 +7,7 @@ "scripts": { "build": "pnpm vite build", "clean": "rm -rf dist", - "deploy": "wrangler deploy --config=${TOML_PATH}" + "deploy": "wrangler deploy --config ${TOML_PATH}" }, "dependencies": { "@chatgpt-telegram-workers/core": "workspace:*" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ab747f0..46084057 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -87,6 +87,9 @@ importers: '@chatgpt-telegram-workers/core': specifier: workspace:* version: link:../../lib/core + '@chatgpt-telegram-workers/next': + specifier: workspace:* + version: link:../../lib/next cloudflare-worker-adapter: specifier: ^1.3.3 version: 1.3.3 @@ -97,6 +100,12 @@ importers: '@vercel/node': specifier: ^3.2.24 version: 3.2.24(encoding@0.1.13) + openai: + specifier: ^4.72.0 + version: 4.72.0(encoding@0.1.13)(zod@3.23.8) + react-dom: + specifier: ^18.3.1 + version: 18.3.1(react@18.3.1) packages/apps/workers: dependencies: diff --git a/wrangler-example.toml b/wrangler-example.toml index cc132dcf..8bbcb3f7 100644 --- a/wrangler-example.toml +++ b/wrangler-example.toml @@ -3,10 +3,12 @@ name = 'chatgpt-telegram-workers' compatibility_date = '2023-10-07' main = './dist/index.js' # 先使用vite编译 然后再使用编译产物部署 -# 修改 main = './dist/index.js' 为对应模块index.ts -# 然后在对应目录下执行 pnpm run deploy:workers 或者 pnpm run deploy:workersnext -# 即可直接使用wrangler编译部署,不使用vite编译 +# 修改 main = './dist/index.js' 为对应模块dist/index.js 或 src/index.ts, +# pnpm run deploy:workers +#main = './packages/apps/workers/dist/index.js' #main = './packages/apps/workers/src/index.ts' +# pnpm run deploy:workersnext +#main = './packages/apps/workers-next/dist/index.js' #main = './packages/apps/workers-next/src/index.ts' workers_dev = true