Skip to content

Commit

Permalink
chore: 编译相关优化
Browse files Browse the repository at this point in the history
- 修改函数入口目录为entry
- 分离vite编译配置
- 简化docker编译流程
- 分离next注入逻辑
  • Loading branch information
TBXark committed Nov 13, 2024
1 parent fe9ef45 commit 9e1cda2
Show file tree
Hide file tree
Showing 23 changed files with 928 additions and 542 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.git
.env
.DS_Store
.github
.vercel
.wrangler
dist
doc
node_modules
plugins
scripts
wrangler.toml
config.json
18 changes: 5 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
FROM node:alpine as DEV
FROM node:alpine

WORKDIR /app
COPY package.json vite.config.ts tsconfig.json ./
COPY src ./src
RUN npm install && npm run build:local

FROM node:alpine as PROD

WORKDIR /app
COPY --from=DEV /app/dist/index.js /app/dist/index.js
COPY --from=DEV /app/package.json /app/
RUN npm install --only=production --omit=dev
RUN apk add --no-cache sqlite
COPY package.json tsconfig.json ./
COPY src src
RUN npm install
EXPOSE 8787
CMD ["npm", "run", "start:dist"]
CMD ["npm", "run", "start:local"]
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 22 additions & 18 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion doc/cn/VERCEL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 使用Vercel部署 (实验性)

`/src/adapter/vercel`中提供了示例代码,可以完成Vercel部署,和基础的功能测试。但是无法保证所有功能都能正常工作。
`/src/entry/vercel`中提供了示例代码,可以完成Vercel部署,和基础的功能测试。但是无法保证所有功能都能正常工作。

### 自动部署

Expand Down
2 changes: 1 addition & 1 deletion doc/en/VERCEL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Deploy using Vercel (experimental)

The `/src/adapter/vercel` provides sample code that can complete Vercel deployment and basic functional testing. However, it cannot guarantee that all functions will work properly.
The `/src/entry/vercel` provides sample code that can complete Vercel deployment and basic functional testing. However, it cannot guarantee that all functions will work properly.


### Automatic deployment
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
- "8787:8787"
volumes:
- ./config.json:/app/config.json:ro # change `./config.json` to your local path
- ./wrangler.toml:/app/config.toml:ro # change `./wrangler.toml` to your local path
- ./wrangler.toml:/app/wrangler.toml:ro # change `./wrangler.toml` to your local path
48 changes: 24 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,50 @@
"lint": "eslint --fix *.js *.ts src plugins scripts",
"version": "tsx ./scripts/plugins/version/main.ts",
"build": "vite build",
"build:local": "BUILD_MODE=local vite build",
"build:docker": "npm run build:local && cd dist && docker build -t chatgpt-telegram-workers:latest .",
"build:dockerx": "npm run build:local && cd dist && docker build --platform linux/amd64,linux/arm64 -t tbxark/chatgpt-telegram-workers:latest --push .",
"build:vercel": "BUILD_MODE=vercel vite build",
"build:pack": "BUILD_MODE=pack vite build",
"build:local": "vite build",
"build:docker": "docker build -t chatgpt-telegram-workers:latest .",
"build:dockerx": "docker build --platform linux/amd64,linux/arm64 -t tbxark/chatgpt-telegram-workers:latest --push .",
"build:vercel": "ENTRY=src/entry/vercel/index.ts vite build",
"deploy:dist": "wrangler deploy",
"deploy:build": "npm run build && wrangler deploy",
"deploy:vercel": "vercel deploy --prod",
"deploy:plugin": "BUILD_MODE=plugins-page vite build && wrangler pages deploy plugins --project-name=interpolate-test --branch=main",
"deploy:plugin": "vite build -c plugins/vite.config.ts && wrangler pages deploy plugins --project-name=interpolate-test --branch=main",
"start:dist": "node dist/index.js",
"start:local": "CONFIG_PATH=./config.json TOML_PATH=./wrangler.toml tsx src/adapter/local/index.ts",
"start:local": "CONFIG_PATH=./config.json TOML_PATH=./wrangler.toml tsx src/entry/local/index.ts",
"start:debug": "wrangler dev --local",
"prepare:vercel": "tsx ./scripts/plugins/vercel/setenv.ts",
"wrangler": "wrangler",
"test": "tsx ./src/agent/index.test.ts"
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.56",
"@ai-sdk/azure": "^0.0.52",
"@ai-sdk/cohere": "^0.0.28",
"@ai-sdk/google": "^0.0.55",
"@ai-sdk/mistral": "^0.0.46",
"@ai-sdk/openai": "^0.0.72",
"ai": "^3.4.33",
"cloudflare-worker-adapter": "^1.3.3",
"@ai-sdk/anthropic": "^0.0.51",
"@ai-sdk/azure": "^0.0.48",
"@ai-sdk/cohere": "^0.0.25",
"@ai-sdk/google": "^0.0.52",
"@ai-sdk/mistral": "^0.0.44",
"@ai-sdk/openai": "^0.0.68"
"cloudflare-worker-adapter": "^1.3.3"
},
"devDependencies": {
"@antfu/eslint-config": "^3.6.2",
"@antfu/eslint-config": "^3.8.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^22.5.5",
"@types/node": "^22.9.0",
"@types/react": "^18.3.11",
"@vercel/node": "^3.2.14",
"eslint": "^9.10.0",
"@vercel/node": "^3.2.24",
"eslint": "^9.14.0",
"eslint-plugin-format": "^0.1.2",
"openai": "^4.68.1",
"react-dom": "^18.3.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-node-externals": "^7.1.3",
"telegram-bot-api-types": "^7.9.12",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"vite": "^5.4.3",
"stylelint": "^16.10.0",
"telegram-bot-api-types": "^7.11.0",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
"vite": "^5.4.11",
"vite-plugin-checker": "^0.8.0",
"vite-plugin-dts": "^4.2.1",
"wrangler": "^3.78.5"
"vite-plugin-dts": "^4.3.0",
"wrangler": "^3.86.1"
}
}
32 changes: 32 additions & 0 deletions plugins/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as path from 'node:path';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import cleanup from 'rollup-plugin-cleanup';
import nodeExternals from 'rollup-plugin-node-externals';
import { defineConfig } from 'vite';
import checker from 'vite-plugin-checker';

export default defineConfig({
plugins: [
nodeResolve({
preferBuiltins: true,
}),
cleanup({
comments: 'none',
extensions: ['js', 'ts'],
}),
checker({
typescript: true,
}),
nodeExternals(),
],
build: {
target: 'esnext',
lib: {
entry: path.resolve(__dirname, '../src/plugins/interpolate.ts'),
fileName: 'interpolate',
formats: ['es'],
},
minify: false,
outDir: path.resolve(__dirname, 'dist'),
},
});
41 changes: 0 additions & 41 deletions scripts/plugins/docker/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/adapter/next/index.ts

This file was deleted.

17 changes: 13 additions & 4 deletions src/adapter/next/next.ts → src/agent/next/next.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ProviderV1 } from '@ai-sdk/provider';
import type { LanguageModelV1 } from 'ai';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, HistoryItem, LLMChatParams } from '../../agent/types';
import type { AgentUserConfig } from '../../config/env';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, HistoryItem, LLMChatParams } from '../types';
import { createAnthropic } from '@ai-sdk/anthropic';
import { createAzure } from '@ai-sdk/azure';
import { createCohere } from '@ai-sdk/cohere';
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { createMistral } from '@ai-sdk/mistral';
import { createOpenAI } from '@ai-sdk/openai';
import { generateText, streamText } from 'ai';
import { streamHandler } from '../../agent/request';
import { streamHandler } from '../request';

export async function requestChatCompletionsV2(params: { model: LanguageModelV1; prompt?: string; messages: HistoryItem[] }, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> {
async function requestChatCompletionsV2(params: { model: LanguageModelV1; prompt?: string; messages: HistoryItem[] }, onStream: ChatStreamTextHandler | null): Promise<ChatAgentResponse> {
if (onStream !== null) {
const stream = await streamText({
model: params.model,
Expand All @@ -38,7 +38,7 @@ export async function requestChatCompletionsV2(params: { model: LanguageModelV1;

type ProviderCreator = (context: AgentUserConfig) => ProviderV1;

export class NextChatAgent implements ChatAgent {
class NextChatAgent implements ChatAgent {
readonly name: string;
readonly modelKey = 'NEXT_CHAT_MODEL';
readonly adapter: ChatAgent;
Expand Down Expand Up @@ -119,3 +119,12 @@ export class NextChatAgent implements ChatAgent {
return this.adapter.modelList(context);
};
}

export function injectNextChatAgent(agents: ChatAgent[]) {
for (let i = 0; i < agents.length; i++) {
const next = NextChatAgent.from(agents[i]);
if (next) {
agents[i] = next;
}
}
}
Loading

0 comments on commit 9e1cda2

Please sign in to comment.