Skip to content

Commit

Permalink
merge: Merge branch 'dev' into vercel-ai
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 5, 2024
2 parents 2e1b0f4 + 08366a5 commit b0d5cb7
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 76 deletions.
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.

58 changes: 15 additions & 43 deletions dist/index.js

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

1 change: 0 additions & 1 deletion dist/timestamp

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
"scripts": {
"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 .",
Expand All @@ -36,7 +37,7 @@
"start:debug": "wrangler dev --local",
"prepare:vercel": "tsx ./scripts/plugins/vercel/setenv.ts",
"wrangler": "wrangler",
"test": "tsx ./src/config/env.test.ts"
"test": "tsx ./src/agent/index.test.ts"
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.51",
Expand Down
46 changes: 30 additions & 16 deletions scripts/plugins/version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@ import { execSync } from 'node:child_process';
import * as fs from 'node:fs/promises';
import path from 'node:path';

let COMMIT_HASH = 'unknown';
const TIMESTAMP = Math.floor(Date.now() / 1000);
const COMMIT_HASH = ((): string => {
try {
return execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
console.warn(e);
}
return 'unknown';
})();

try {
COMMIT_HASH = execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
console.warn(e);
async function createVersionTs(outDir: string) {
await fs.writeFile(
path.resolve(outDir, 'src/config/version.ts'),
`export const BUILD_TIMESTAMP = ${TIMESTAMP};\nexport const BUILD_VERSION = '${COMMIT_HASH}';\n`,
);
}

async function createVersionJson(outDir: string) {
await fs.writeFile(path.resolve(outDir, 'dist/buildinfo.json'), JSON.stringify({
sha: COMMIT_HASH,
timestamp: TIMESTAMP,
}));
}

export async function createVersion(outDir: string) {
await createVersionTs(outDir);
await createVersionJson(outDir);
}

export function createVersionPlugin(targetDir: string) {
return {
name: 'buildInfo',
name: 'version',
async buildStart() {
await createVersionTs(targetDir);
},
async closeBundle() {
await fs.writeFile(path.resolve(targetDir, 'timestamp'), TIMESTAMP.toString());
await fs.writeFile(path.resolve(targetDir, 'buildinfo.json'), JSON.stringify({
sha: COMMIT_HASH,
timestamp: TIMESTAMP,
}));
await createVersionJson(targetDir);
},
};
}

export const versionDefine = {
__BUILD_VERSION__: JSON.stringify(COMMIT_HASH),
__BUILD_TIMESTAMP__: TIMESTAMP.toString(),
};
8 changes: 8 additions & 0 deletions scripts/plugins/version/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import process from 'node:process';
import { createVersion } from './index';

const {
OUT_DIR = '.',
} = process.env;

createVersion(OUT_DIR).catch(console.error);
2 changes: 1 addition & 1 deletion src/agent/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../config/env.test';
{
const agent = loadChatLLM({
...ENV.USER_CONFIG,
AI_PROVIDER: 'anthropic',
AI_PROVIDER: 'cohere',
});
const params: LLMChatParams = {
prompt: 'You are a useful assistant.',
Expand Down
2 changes: 1 addition & 1 deletion src/agent/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function defaultSSEJsonParser(sse: SSEMessage): SSEParserResult {
if (sse.data?.startsWith('[DONE]')) {
return { finish: true };
}
if (sse.event === null && sse.data) {
if (sse.data) {
try {
return { data: JSON.parse(sse.data) };
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class CohereConfig {
// cohere api key
COHERE_API_KEY: string | null = null;
// cohere api base
COHERE_API_BASE = 'https://api.cohere.com/v1';
COHERE_API_BASE = 'https://api.cohere.com/v2';
// cohere api model
COHERE_CHAT_MODEL = 'command-r-plus';
}
Expand Down
9 changes: 3 additions & 6 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
WorkersConfig,
} from './config';
import { ConfigMerger } from './merger';
import { BUILD_TIMESTAMP, BUILD_VERSION } from './version';

export type AgentUserConfig = Record<string, any> &
DefineKeys &
Expand Down Expand Up @@ -53,13 +54,9 @@ class Environment extends EnvironmentConfig {
// -- 版本数据 --
//
// 当前版本
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
BUILD_TIMESTAMP = typeof __BUILD_TIMESTAMP__ === 'number' ? __BUILD_TIMESTAMP__ : 0;
BUILD_TIMESTAMP = BUILD_TIMESTAMP;
// 当前版本 commit id
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
BUILD_VERSION = typeof __BUILD_VERSION__ === 'string' ? __BUILD_VERSION__ : 'unknown';
BUILD_VERSION = BUILD_VERSION;

// -- 基础配置 --
I18N = loadI18n();
Expand Down
2 changes: 2 additions & 0 deletions src/config/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const BUILD_TIMESTAMP = 1730790946;
export const BUILD_VERSION = '62b8147';
7 changes: 2 additions & 5 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { defineConfig } from 'vite';
import checker from 'vite-plugin-checker';
import dts from 'vite-plugin-dts';
import { createDockerPlugin } from './scripts/plugins/docker';
import { createVersionPlugin, versionDefine } from './scripts/plugins/version';
import { createVersionPlugin } from './scripts/plugins/version';

const { BUILD_MODE } = process.env;
const plugins: Plugin[] = [
Expand Down Expand Up @@ -53,10 +53,10 @@ switch (BUILD_MODE) {
break;
default:
entry = 'src/index.ts';
plugins.push(createVersionPlugin('dist'));
break;
}

plugins.push(createVersionPlugin(path.resolve(__dirname)));
export default defineConfig({
plugins,
build: {
Expand All @@ -69,7 +69,4 @@ export default defineConfig({
minify: false,
outDir,
},
define: {
...versionDefine,
},
});

0 comments on commit b0d5cb7

Please sign in to comment.