Skip to content

Commit

Permalink
perf: 支持文档格式的图片
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark authored Nov 15, 2024
2 parents 961b0ab + 7454920 commit bad44d8
Show file tree
Hide file tree
Showing 40 changed files with 414 additions and 495 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.

492 changes: 228 additions & 264 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';
16 changes: 8 additions & 8 deletions packages/lib/core/src/agent/message.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
export type DataContent = string | Uint8Array | ArrayBuffer | Buffer;

interface TextPart {
export interface TextPart {
type: 'text';
text: string;
}

interface ImagePart {
export interface ImagePart {
type: 'image';
image: DataContent | URL;
mimeType?: string;
}

interface ToolCallPart {
export interface ToolCallPart {
type: 'tool-call';
toolCallId: string;
toolName: string;
args: unknown;
}

interface FilePart {
export interface FilePart {
type: 'file';
data: DataContent | URL;
}

interface ToolResultPart {
export interface ToolResultPart {
type: 'tool-result';
toolCallId: string;
toolName: string;
result: unknown;
}

type AssistantContent = string | Array<TextPart | ToolCallPart>;
type UserContent = string | Array<TextPart | ImagePart | FilePart>;
type ToolContent = Array<ToolResultPart>;
export type AssistantContent = string | Array<TextPart | ToolCallPart>;
export type UserContent = string | Array<TextPart | ImagePart | FilePart>;
export type ToolContent = Array<ToolResultPart>;

export interface CoreSystemMessage {
role: 'system';
Expand Down
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 = 1731639921;
export const BUILD_VERSION = '5e2f72d';
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
Loading

0 comments on commit bad44d8

Please sign in to comment.