Skip to content

Commit

Permalink
Merge pull request #220 from TBXark/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TBXark authored Oct 17, 2023
2 parents 3cce4ce + 28a5fe4 commit 44f84a5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 58 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ TIMESTAMP_FILE := ./dist/timestamp # 兼容旧版更新逻辑
BUILD_INFO_JSON := ./dist/buildinfo.json
OUTPUT_FILE := ./dist/index.js
ENTRY_FILE := main.js

ifeq ($(shell if [ -d "./node_modules/.bin" ]; then echo "yes"; else echo "no"; fi),yes)
PATH := ./node_modules/.bin:$(PATH)
endif
ESLINT := ./node_modules/.bin/eslint
ESBUILD := ./node_modules/.bin/esbuild

.PHONY: build
build: clean
COMMIT_HASH=$$(git rev-parse --short HEAD) && \
TIMESTAMP=$$(date +%s) && \
echo "$$TIMESTAMP" > $(TIMESTAMP_FILE) && \
echo "{\"sha\": \"$$COMMIT_HASH\", \"timestamp\": $$TIMESTAMP}" > $(BUILD_INFO_JSON) && \
esbuild $(ENTRY_FILE) --bundle --outfile=$(OUTPUT_FILE) --format=esm --define:process.env.BUILD_VERSION="'$$COMMIT_HASH'" --define:process.env.BUILD_TIMESTAMP="$$TIMESTAMP"
$(ESBUILD) $(ENTRY_FILE) --bundle --outfile=$(OUTPUT_FILE) --format=esm --define:process.env.BUILD_VERSION="'$$COMMIT_HASH'" --define:process.env.BUILD_TIMESTAMP="$$TIMESTAMP"

.PHONY: clean
clean:
rm -f $(TIMESTAMP_FILE) $(BUILD_INFO_JSON) $(OUTPUT_FILE)

.PHONY: lint
lint:
eslint --fix --ext .js,.jsx,.mjs main.js src adapter
$(ESLINT) --fix --ext .js,.jsx,.mjs main.js src adapter
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "cca7cf8", "timestamp": 1696754346}
{"sha": "3713d4d", "timestamp": 1697520630}
13 changes: 9 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ var ENV = {
// 检查更新的分支
UPDATE_BRANCH: "master",
// 当前版本
BUILD_TIMESTAMP: 1696754346,
BUILD_TIMESTAMP: 1697520630,
// 当前版本 commit id
BUILD_VERSION: "cca7cf8",
BUILD_VERSION: "3713d4d",
I18N: null,
LANGUAGE: "zh-cn",
// 使用流模式
Expand All @@ -58,6 +58,8 @@ var ENV = {
TELEGRAM_API_DOMAIN: "https://api.telegram.org",
// OpenAI API Domain 可替换兼容openai api的其他服务商
OPENAI_API_DOMAIN: "https://api.openai.com",
// OpenAI API BASE `https://api.openai.com/v1`
OPENAI_API_BASE: "",
// Azure API Key
AZURE_API_KEY: null,
// Azure Completions API
Expand Down Expand Up @@ -121,6 +123,9 @@ function initEnv(env, i18n2) {
}
ENV.TELEGRAM_AVAILABLE_TOKENS.push(env.TELEGRAM_TOKEN);
}
if (!ENV.OPENAI_API_BASE) {
ENV.OPENAI_API_BASE = `${ENV.OPENAI_API_DOMAIN}/v1`;
}
}
ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase());
ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message;
Expand Down Expand Up @@ -741,7 +746,7 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
const { signal } = controller;
const timeout = 1e3 * 60 * 5;
setTimeout(() => controller.abort(), timeout);
let url = `${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`;
let url = `${ENV.OPENAI_API_BASE}/chat/completions`;
const header = {
"Content-Type": "application/json",
"Authorization": `Bearer ${key}`
Expand Down Expand Up @@ -802,7 +807,7 @@ async function requestImageFromOpenAI(prompt, context) {
n: 1,
size: "512x512"
};
const resp = await fetch(`${ENV.OPENAI_API_DOMAIN}/v1/images/generations`, {
const resp = await fetch(`${ENV.OPENAI_API_BASE}/images/generations`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1696754346
1697520630
2 changes: 1 addition & 1 deletion src/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export async function chatWithLLM(text, context, modifier) {

const llm = loadLLM(context);
if (llm === null) {
return sendMessageToTelegramWithContext(context)('LLM is not enable');
return sendMessageToTelegramWithContext(context)('LLM is not enable');
}
const answer = await requestCompletionsFromLLM(text, context, llm, modifier, onStream);
context.CURRENT_CHAT_CONTEXT.parse_mode = parseMode;
Expand Down
3 changes: 2 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CONST, DATABASE, ENV} from './env.js';
// import {TelegramMessage} from './type.d.ts';
// eslint-disable-next-line no-unused-vars
import './type.js';

/**
* 上下文信息
Expand Down
7 changes: 7 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export const ENV = {
TELEGRAM_API_DOMAIN: 'https://api.telegram.org',
// OpenAI API Domain 可替换兼容openai api的其他服务商
OPENAI_API_DOMAIN: 'https://api.openai.com',
// OpenAI API BASE `https://api.openai.com/v1`
OPENAI_API_BASE: '',

// Azure API Key
AZURE_API_KEY: null,
Expand Down Expand Up @@ -196,6 +198,11 @@ export function initEnv(env, i18n) {
}
ENV.TELEGRAM_AVAILABLE_TOKENS.push(env.TELEGRAM_TOKEN);
}

// AUTO SET VALUES
if (!ENV.OPENAI_API_BASE) {
ENV.OPENAI_API_BASE=`${ENV.OPENAI_API_DOMAIN}/v1`;
}
}
ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase());
ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message;
Expand Down
3 changes: 2 additions & 1 deletion src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {sendMessageToTelegramWithContext} from './telegram.js';
import {handleCommandMessage} from './command.js';
import {errorToString} from './utils.js';
import {chatWithLLM} from './chat.js';
// import {TelegramMessage, TelegramWebhookRequest} from './type.d.ts';
// eslint-disable-next-line no-unused-vars
import './type.js';


/**
Expand Down
4 changes: 2 additions & 2 deletions src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function requestCompletionsFromOpenAI(message, history, context, on
const timeout = 1000 * 60 * 5;
setTimeout(() => controller.abort(), timeout);

let url = `${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`;
let url = `${ENV.OPENAI_API_BASE}/chat/completions`;
const header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${key}`,
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function requestImageFromOpenAI(prompt, context) {
n: 1,
size: '512x512',
};
const resp = await fetch(`${ENV.OPENAI_API_DOMAIN}/v1/images/generations`, {
const resp = await fetch(`${ENV.OPENAI_API_BASE}/images/generations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
41 changes: 0 additions & 41 deletions src/type.d.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @typedef {Object} TelegramUser
* @property {number} id - The ID of the user.
* @property {boolean} is_bot - True, if the user is a bot.
* @property {string} first_name - The first name of the user.
* @property {string} [last_name] - The last name of the user.
* @property {string} [username] - The username of the user.
* @property {string} [language_code] - The language code of the user.
*/

/**
* @typedef {Object} TelegramChat
* @property {number} id - The ID of the chat.
* @property {string} type - The type of the chat.
*/

/**
* @typedef {Object} TelegramMessageEntity
* @property {string} type - Type of the entity.
* @property {number} offset - Offset in UTF-16 code units to the start of the entity.
* @property {number} length - Length of the entity in UTF-16 code units.
* @property {string} [url] - URL of the entity.
* @property {TelegramUser} [user] - The user this entity refers to.
*/

/**
* @typedef {Object} TelegramMessage
* @property {number} message_id - The message's unique identifier.
* @property {TelegramUser} from - The user that sent the message.
* @property {TelegramChat} chat - The chat where the message was sent.
* @property {number} date - The date the message was sent.
* @property {string} text - The text of the message.
* @property {TelegramMessageEntity[]} [entities] - An array of message entities.
* @property {TelegramMessage} [reply_to_message] - The message that this message is a reply to.
*/

/**
* @typedef {Object} TelegramWebhookRequest
* @property {number} update_id - The update's unique identifier.
* @property {TelegramMessage} message - The message
* @property {TelegramMessage} edited_message - The edited message
*/

0 comments on commit 44f84a5

Please sign in to comment.