Skip to content

Commit

Permalink
refactor: 为core中每个子模块添加index.ts 减少import层级
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 15, 2024
1 parent 76928f4 commit 7454920
Show file tree
Hide file tree
Showing 39 changed files with 359 additions and 462 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.

454 changes: 204 additions & 250 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LLMChatParams } from './types';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { loadChatLLM } from './agent';
import '../config/env.test';

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { ChatAgent, ImageAgent } from './types';
import { Anthropic } from './anthropic';
import { AzureChatAI, AzureImageAI } from './azure';
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/core/src/agent/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { SseChatCompatibleOptions } from './request';
import type { SSEMessage, SSEParserResult } from './stream';
import type {
Expand All @@ -8,7 +8,7 @@ import type {
HistoryItem,
LLMChatParams,
} from './types';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { imageToBase64String } from '../utils/image';
import { requestChatCompletions } from './request';
import { Stream } from './stream';
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/azure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type {
ChatAgent,
ChatAgentResponse,
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/core/src/agent/chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WorkerContext } from '../config/context';
import type { WorkerContext } from '../config';
import type { ChatAgent, HistoryItem, HistoryModifier, LLMChatParams, UserMessageItem } from './types';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { extractTextContent } from './utils';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/cohere.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { SseChatCompatibleOptions } from './request';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import { renderOpenAIMessages } from './openai';
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import { renderOpenAIMessages } from './openai';
import { requestChatCompletions } from './request';
Expand Down
1 change: 1 addition & 0 deletions packages/lib/core/src/agent/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './agent';
export * from './chat';
export * from './request';
export * from './types';
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/mistralai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
import { renderOpenAIMessages } from './openai';
import { requestChatCompletions } from './request';
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/core/src/agent/openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type {
ChatAgent,
ChatAgentResponse,
Expand All @@ -7,7 +7,7 @@ import type {
ImageAgent,
LLMChatParams,
} from './types';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { imageToBase64String } from '../utils/image';
import { requestChatCompletions } from './request';
import { convertStringToResponseMessages, extractImageContent, loadModelsList } from './utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChatStreamTextHandler } from './types';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { Stream } from './stream';

export interface SseChatCompatibleOptions {
Expand Down
14 changes: 12 additions & 2 deletions packages/lib/core/src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import type { AgentUserConfig } from '../config/env';
import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from './message';
import type { AgentUserConfig } from '../config';
import type {
CoreAssistantMessage,
CoreSystemMessage,
CoreToolMessage,
CoreUserMessage,
DataContent,
FilePart,
ImagePart,
TextPart,
} from './message';
// 当使用 `ai` 包时,取消注释以下行并注释掉上一行
// import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from 'ai';

export type DataItemContent = DataContent;
export type UserContentPart = TextPart | ImagePart | FilePart;

export type SystemMessageItem = CoreSystemMessage;
export type UserMessageItem = CoreUserMessage;
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/agent/workersai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AgentUserConfig } from '../config/env';
import type { AgentUserConfig } from '../config';
import type { SseChatCompatibleOptions } from './request';
import type {
ChatAgent,
Expand Down
27 changes: 26 additions & 1 deletion packages/lib/core/src/config/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { AgentUserConfig } from './env';
import { ENV } from './env';
import { ENV, ENV_KEY_MAPPER } from './env';
import { ConfigMerger } from './merger';

export class ShareContext {
Expand Down Expand Up @@ -84,6 +84,7 @@ export class WorkerContext {
constructor(USER_CONFIG: AgentUserConfig, SHARE_CONTEXT: ShareContext) {
this.USER_CONFIG = USER_CONFIG;
this.SHARE_CONTEXT = SHARE_CONTEXT;
this.execChangeAndSave = this.execChangeAndSave.bind(this);
}

static async from(token: string, update: Telegram.Update): Promise<WorkerContext> {
Expand All @@ -98,6 +99,30 @@ export class WorkerContext {
}
return new WorkerContext(USER_CONFIG, SHARE_CONTEXT);
}

async execChangeAndSave(values: Record<string, any>): Promise<void> {
for (const ent of Object.entries(values || {})) {
let [key, value] = ent;
key = ENV_KEY_MAPPER[key] || key;
if (ENV.LOCK_USER_CONFIG_KEYS.includes(key)) {
throw new Error(`Key ${key} is locked`);
}
const configKeys = Object.keys(this.USER_CONFIG || {}) || [];
if (!configKeys.includes(key)) {
throw new Error(`Key ${key} is not allowed`);
}
this.USER_CONFIG.DEFINE_KEYS.push(key);
ConfigMerger.merge(this.USER_CONFIG, {
[key]: value,
});
console.log('Update user config: ', key, this.USER_CONFIG[key]);
}
this.USER_CONFIG.DEFINE_KEYS = Array.from(new Set(this.USER_CONFIG.DEFINE_KEYS));
await ENV.DATABASE.put(
this.SHARE_CONTEXT.configStoreKey,
JSON.stringify(ConfigMerger.trim(this.USER_CONFIG, ENV.LOCK_USER_CONFIG_KEYS)),
);
}
}

class UpdateContext {
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/core/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const ENV_KEY_MAPPER: Record<string, string> = {
WORKERS_AI_MODEL: 'WORKERS_CHAT_MODEL',
};

export type CustomMessageRender = (mode: string | null, message: string) => string;

class Environment extends EnvironmentConfig {
// -- 版本数据 --
//
Expand All @@ -68,6 +70,8 @@ class Environment extends EnvironmentConfig {
DATABASE: KVNamespace = null as any;
API_GUARD: APIGuard | null = null;

CUSTOM_MESSAGE_RENDER: CustomMessageRender | null = null;

constructor() {
super();
this.merge = this.merge.bind(this);
Expand Down
6 changes: 6 additions & 0 deletions packages/lib/core/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './config';
export * from './context';
export * from './env';
export * from './merger';
export * from './types';
export * from './version';
4 changes: 2 additions & 2 deletions packages/lib/core/src/config/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const BUILD_TIMESTAMP = 1731643971;
export const BUILD_VERSION = '2977d62';
export const BUILD_TIMESTAMP = 1731649723;
export const BUILD_VERSION = '76928f4';
8 changes: 3 additions & 5 deletions packages/lib/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ENV } from './config/env';
import { ENV } from './config';
import { createRouter } from './route';

export * from './agent';
export * from './config/env';
export * from './config';
export * from './route';
export * from './telegram/api';
export * from './telegram/handler';
export * from '@chatgpt-telegram-workers/plugins';
export * from './telegram';

export const Workers = {
async fetch(request: Request, env: any): Promise<Response> {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/route/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { RouterRequest } from '../utils/router';
import { ENV } from '../config/env';
import { ENV } from '../config';
import { createTelegramBotAPI } from '../telegram/api';
import { commandsBindScope, commandsDocument } from '../telegram/command';
import { handleUpdate } from '../telegram/handler';
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/telegram/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as Telegram from 'telegram-bot-api-types';
import { ENV } from '../../config/env';
import { ENV } from '../../config';

class APIClientBase {
readonly token: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ENV } from '../../config/env';
import { isTelegramChatTypeGroup } from '../utils/utils';
import { ENV } from '../../config';

export const TELEGRAM_AUTH_CHECKER = {
default(chatType: string): string[] | null {
if (isTelegramChatTypeGroup(chatType)) {
if (isGroupChat(chatType)) {
return ['administrator', 'creator'];
}
return null;
},
shareModeGroup(chatType: string): string[] | null {
if (isTelegramChatTypeGroup(chatType)) {
if (isGroupChat(chatType)) {
// 每个人在群里有上下文的时候,不限制
if (!ENV.GROUP_CHAT_BOT_SHARE_MODE) {
return null;
Expand All @@ -19,3 +18,7 @@ export const TELEGRAM_AUTH_CHECKER = {
return null;
},
};

export function isGroupChat(type: string): boolean {
return type === 'group' || type === 'supergroup';
}
4 changes: 2 additions & 2 deletions packages/lib/core/src/telegram/callback_query/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { WorkerContext } from '../../config/context';
import type { WorkerContext } from '../../config';
import { loadChatRoleWithContext } from '../command/auth';
import { MessageSender } from '../utils/send';
import { MessageSender } from '../sender';
import { AgentListCallbackQueryHandler, ModelChangeCallbackQueryHandler, ModelListCallbackQueryHandler } from './system';

const QUERY_HANDLERS = [
Expand Down
14 changes: 6 additions & 8 deletions packages/lib/core/src/telegram/callback_query/system.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { WorkerContext } from '../../config/context';
import type { AgentUserConfig } from '../../config/env';
import type { AgentUserConfig, WorkerContext } from '../../config';
import type { CallbackQueryHandler } from './types';
import { CHAT_AGENTS, loadChatLLM } from '../../agent';
import { ENV } from '../../config/env';
import { TELEGRAM_AUTH_CHECKER } from '../auth/auth';
import { MessageSender } from '../utils/send';
import { setUserConfig } from '../utils/utils';
import { ENV } from '../../config';
import { TELEGRAM_AUTH_CHECKER } from '../auth';
import { MessageSender } from '../sender';

export class AgentListCallbackQueryHandler implements CallbackQueryHandler {
prefix = 'al:';
Expand Down Expand Up @@ -145,10 +143,10 @@ export class ModelChangeCallbackQueryHandler implements CallbackQueryHandler {
if (!chatAgent?.modelKey) {
throw new Error(`modelKey not found: ${agent}`);
}
await setUserConfig({
await context.execChangeAndSave({
AI_PROVIDER: agent,
[chatAgent.modelKey]: model,
}, context);
});
console.log('Change model:', agent, model);
const message: Telegram.EditMessageTextParams = {
chat_id: query.message.chat.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/core/src/telegram/callback_query/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { WorkerContext } from '../../config/context';
import type { WorkerContext } from '../../config';

export interface CallbackQueryHandler {
prefix: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { HistoryModifier, UserMessageItem } from '../../agent';
import type { StreamResultHandler } from '../../agent/chat';
import type { FilePart, ImagePart, TextPart } from '../../agent/message';
import type { WorkerContext } from '../../config/context';
import type { MessageHandler } from './types';
import { loadChatLLM } from '../../agent';
import { requestCompletionsFromLLM } from '../../agent/chat';
import { ENV } from '../../config/env';
import type { HistoryModifier, StreamResultHandler, UserContentPart, UserMessageItem } from '../../agent';
import type { WorkerContext } from '../../config';
import { loadChatLLM, requestCompletionsFromLLM } from '../../agent';
import { ENV } from '../../config';
import { createTelegramBotAPI } from '../api';
import { MessageSender } from '../utils/send';
import { MessageSender } from '../sender';

export async function chatWithLLM(message: Telegram.Message, params: UserMessageItem | null, context: WorkerContext, modifier: HistoryModifier | null): Promise<Response> {
export async function chatWithMessage(message: Telegram.Message, params: UserMessageItem | null, context: WorkerContext, modifier: HistoryModifier | null): Promise<Response> {
const sender = MessageSender.fromMessage(context.SHARE_CONTEXT.botToken, message);
try {
try {
Expand Down Expand Up @@ -77,7 +73,7 @@ export async function chatWithLLM(message: Telegram.Message, params: UserMessage
}
}

async function extractImageURL(fileId: string | null, context: WorkerContext): Promise<URL | null> {
export async function extractImageURL(fileId: string | null, context: WorkerContext): Promise<URL | null> {
if (!fileId) {
return null;
}
Expand All @@ -93,7 +89,7 @@ async function extractImageURL(fileId: string | null, context: WorkerContext): P
return null;
}

function extractImageFileID(message: Telegram.Message): string | null {
export function extractImageFileID(message: Telegram.Message): string | null {
if (message.photo && message.photo.length > 0) {
const offset = ENV.TELEGRAM_PHOTO_SIZE_OFFSET;
const length = message.photo.length;
Expand All @@ -105,22 +101,20 @@ function extractImageFileID(message: Telegram.Message): string | null {
return null;
}

export class ChatHandler implements MessageHandler {
handle = async (message: Telegram.Message, context: WorkerContext): Promise<Response | null> => {
const text = message.text || message.caption || '';
const params: UserMessageItem = {
role: 'user',
content: text,
};
const url = await extractImageURL(extractImageFileID(message), context);
if (url) {
const contents = new Array<TextPart | ImagePart | FilePart>();
if (text) {
contents.push({ type: 'text', text });
}
contents.push({ type: 'image', image: url });
params.content = contents;
}
return chatWithLLM(message, params, context, null);
export async function extractUserMessageItem(message: Telegram.Message, context: WorkerContext): Promise<UserMessageItem> {
const text = message.text || message.caption || '';
const params: UserMessageItem = {
role: 'user',
content: text,
};
const url = await extractImageURL(extractImageFileID(message), context);
if (url) {
const contents = new Array<UserContentPart>();
if (text) {
contents.push({ type: 'text', text });
}
contents.push({ type: 'image', image: url });
params.content = contents;
}
return params;
}
4 changes: 2 additions & 2 deletions packages/lib/core/src/telegram/command/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { WorkerContext } from '../../config/context';
import { ENV } from '../../config/env';
import type { WorkerContext } from '../../config';
import { ENV } from '../../config';
import { createTelegramBotAPI } from '../api';

export async function loadChatRoleWithContext(chatId: number, speakerId: number, context: WorkerContext): Promise<string | null> {
Expand Down
Loading

0 comments on commit 7454920

Please sign in to comment.