Skip to content

Commit bad44d8

Browse files
authored
perf: 支持文档格式的图片
2 parents 961b0ab + 7454920 commit bad44d8

40 files changed

+414
-495
lines changed

dist/buildinfo.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 228 additions & 264 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/core/src/agent/agent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { LLMChatParams } from './types';
2-
import { ENV } from '../config/env';
2+
import { ENV } from '../config';
33
import { loadChatLLM } from './agent';
44
import '../config/env.test';
55

packages/lib/core/src/agent/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { ChatAgent, ImageAgent } from './types';
33
import { Anthropic } from './anthropic';
44
import { AzureChatAI, AzureImageAI } from './azure';

packages/lib/core/src/agent/anthropic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { SseChatCompatibleOptions } from './request';
33
import type { SSEMessage, SSEParserResult } from './stream';
44
import type {
@@ -8,7 +8,7 @@ import type {
88
HistoryItem,
99
LLMChatParams,
1010
} from './types';
11-
import { ENV } from '../config/env';
11+
import { ENV } from '../config';
1212
import { imageToBase64String } from '../utils/image';
1313
import { requestChatCompletions } from './request';
1414
import { Stream } from './stream';

packages/lib/core/src/agent/azure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type {
33
ChatAgent,
44
ChatAgentResponse,

packages/lib/core/src/agent/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { WorkerContext } from '../config/context';
1+
import type { WorkerContext } from '../config';
22
import type { ChatAgent, HistoryItem, HistoryModifier, LLMChatParams, UserMessageItem } from './types';
3-
import { ENV } from '../config/env';
3+
import { ENV } from '../config';
44
import { extractTextContent } from './utils';
55

66
/**

packages/lib/core/src/agent/cohere.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { SseChatCompatibleOptions } from './request';
33
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
44
import { renderOpenAIMessages } from './openai';

packages/lib/core/src/agent/gemini.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
33
import { renderOpenAIMessages } from './openai';
44
import { requestChatCompletions } from './request';

packages/lib/core/src/agent/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './agent';
2+
export * from './chat';
23
export * from './request';
34
export * from './types';

packages/lib/core/src/agent/message.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
export type DataContent = string | Uint8Array | ArrayBuffer | Buffer;
22

3-
interface TextPart {
3+
export interface TextPart {
44
type: 'text';
55
text: string;
66
}
77

8-
interface ImagePart {
8+
export interface ImagePart {
99
type: 'image';
1010
image: DataContent | URL;
1111
mimeType?: string;
1212
}
1313

14-
interface ToolCallPart {
14+
export interface ToolCallPart {
1515
type: 'tool-call';
1616
toolCallId: string;
1717
toolName: string;
1818
args: unknown;
1919
}
2020

21-
interface FilePart {
21+
export interface FilePart {
2222
type: 'file';
2323
data: DataContent | URL;
2424
}
2525

26-
interface ToolResultPart {
26+
export interface ToolResultPart {
2727
type: 'tool-result';
2828
toolCallId: string;
2929
toolName: string;
3030
result: unknown;
3131
}
3232

33-
type AssistantContent = string | Array<TextPart | ToolCallPart>;
34-
type UserContent = string | Array<TextPart | ImagePart | FilePart>;
35-
type ToolContent = Array<ToolResultPart>;
33+
export type AssistantContent = string | Array<TextPart | ToolCallPart>;
34+
export type UserContent = string | Array<TextPart | ImagePart | FilePart>;
35+
export type ToolContent = Array<ToolResultPart>;
3636

3737
export interface CoreSystemMessage {
3838
role: 'system';

packages/lib/core/src/agent/mistralai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { ChatAgent, ChatAgentResponse, ChatStreamTextHandler, LLMChatParams } from './types';
33
import { renderOpenAIMessages } from './openai';
44
import { requestChatCompletions } from './request';

packages/lib/core/src/agent/openai.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type {
33
ChatAgent,
44
ChatAgentResponse,
@@ -7,7 +7,7 @@ import type {
77
ImageAgent,
88
LLMChatParams,
99
} from './types';
10-
import { ENV } from '../config/env';
10+
import { ENV } from '../config';
1111
import { imageToBase64String } from '../utils/image';
1212
import { requestChatCompletions } from './request';
1313
import { convertStringToResponseMessages, extractImageContent, loadModelsList } from './utils';

packages/lib/core/src/agent/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ChatStreamTextHandler } from './types';
2-
import { ENV } from '../config/env';
2+
import { ENV } from '../config';
33
import { Stream } from './stream';
44

55
export interface SseChatCompatibleOptions {

packages/lib/core/src/agent/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import type { AgentUserConfig } from '../config/env';
2-
import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from './message';
1+
import type { AgentUserConfig } from '../config';
2+
import type {
3+
CoreAssistantMessage,
4+
CoreSystemMessage,
5+
CoreToolMessage,
6+
CoreUserMessage,
7+
DataContent,
8+
FilePart,
9+
ImagePart,
10+
TextPart,
11+
} from './message';
312
// 当使用 `ai` 包时,取消注释以下行并注释掉上一行
413
// import type { CoreAssistantMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, DataContent } from 'ai';
514

615
export type DataItemContent = DataContent;
16+
export type UserContentPart = TextPart | ImagePart | FilePart;
717

818
export type SystemMessageItem = CoreSystemMessage;
919
export type UserMessageItem = CoreUserMessage;

packages/lib/core/src/agent/workersai.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentUserConfig } from '../config/env';
1+
import type { AgentUserConfig } from '../config';
22
import type { SseChatCompatibleOptions } from './request';
33
import type {
44
ChatAgent,

packages/lib/core/src/config/context.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as Telegram from 'telegram-bot-api-types';
22
import type { AgentUserConfig } from './env';
3-
import { ENV } from './env';
3+
import { ENV, ENV_KEY_MAPPER } from './env';
44
import { ConfigMerger } from './merger';
55

66
export class ShareContext {
@@ -84,6 +84,7 @@ export class WorkerContext {
8484
constructor(USER_CONFIG: AgentUserConfig, SHARE_CONTEXT: ShareContext) {
8585
this.USER_CONFIG = USER_CONFIG;
8686
this.SHARE_CONTEXT = SHARE_CONTEXT;
87+
this.execChangeAndSave = this.execChangeAndSave.bind(this);
8788
}
8889

8990
static async from(token: string, update: Telegram.Update): Promise<WorkerContext> {
@@ -98,6 +99,30 @@ export class WorkerContext {
9899
}
99100
return new WorkerContext(USER_CONFIG, SHARE_CONTEXT);
100101
}
102+
103+
async execChangeAndSave(values: Record<string, any>): Promise<void> {
104+
for (const ent of Object.entries(values || {})) {
105+
let [key, value] = ent;
106+
key = ENV_KEY_MAPPER[key] || key;
107+
if (ENV.LOCK_USER_CONFIG_KEYS.includes(key)) {
108+
throw new Error(`Key ${key} is locked`);
109+
}
110+
const configKeys = Object.keys(this.USER_CONFIG || {}) || [];
111+
if (!configKeys.includes(key)) {
112+
throw new Error(`Key ${key} is not allowed`);
113+
}
114+
this.USER_CONFIG.DEFINE_KEYS.push(key);
115+
ConfigMerger.merge(this.USER_CONFIG, {
116+
[key]: value,
117+
});
118+
console.log('Update user config: ', key, this.USER_CONFIG[key]);
119+
}
120+
this.USER_CONFIG.DEFINE_KEYS = Array.from(new Set(this.USER_CONFIG.DEFINE_KEYS));
121+
await ENV.DATABASE.put(
122+
this.SHARE_CONTEXT.configStoreKey,
123+
JSON.stringify(ConfigMerger.trim(this.USER_CONFIG, ENV.LOCK_USER_CONFIG_KEYS)),
124+
);
125+
}
101126
}
102127

103128
class UpdateContext {

packages/lib/core/src/config/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const ENV_KEY_MAPPER: Record<string, string> = {
5050
WORKERS_AI_MODEL: 'WORKERS_CHAT_MODEL',
5151
};
5252

53+
export type CustomMessageRender = (mode: string | null, message: string) => string;
54+
5355
class Environment extends EnvironmentConfig {
5456
// -- 版本数据 --
5557
//
@@ -68,6 +70,8 @@ class Environment extends EnvironmentConfig {
6870
DATABASE: KVNamespace = null as any;
6971
API_GUARD: APIGuard | null = null;
7072

73+
CUSTOM_MESSAGE_RENDER: CustomMessageRender | null = null;
74+
7175
constructor() {
7276
super();
7377
this.merge = this.merge.bind(this);

packages/lib/core/src/config/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './config';
2+
export * from './context';
3+
export * from './env';
4+
export * from './merger';
5+
export * from './types';
6+
export * from './version';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export const BUILD_TIMESTAMP = 1731639921;
2-
export const BUILD_VERSION = '5e2f72d';
1+
export const BUILD_TIMESTAMP = 1731649723;
2+
export const BUILD_VERSION = '76928f4';

packages/lib/core/src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { ENV } from './config/env';
1+
import { ENV } from './config';
22
import { createRouter } from './route';
33

44
export * from './agent';
5-
export * from './config/env';
5+
export * from './config';
66
export * from './route';
7-
export * from './telegram/api';
8-
export * from './telegram/handler';
9-
export * from '@chatgpt-telegram-workers/plugins';
7+
export * from './telegram';
108

119
export const Workers = {
1210
async fetch(request: Request, env: any): Promise<Response> {

packages/lib/core/src/route/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as Telegram from 'telegram-bot-api-types';
22
import type { RouterRequest } from '../utils/router';
3-
import { ENV } from '../config/env';
3+
import { ENV } from '../config';
44
import { createTelegramBotAPI } from '../telegram/api';
55
import { commandsBindScope, commandsDocument } from '../telegram/command';
66
import { handleUpdate } from '../telegram/handler';

packages/lib/core/src/telegram/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as Telegram from 'telegram-bot-api-types';
2-
import { ENV } from '../../config/env';
2+
import { ENV } from '../../config';
33

44
class APIClientBase {
55
readonly token: string;
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { ENV } from '../../config/env';
2-
import { isTelegramChatTypeGroup } from '../utils/utils';
1+
import { ENV } from '../../config';
32

43
export const TELEGRAM_AUTH_CHECKER = {
54
default(chatType: string): string[] | null {
6-
if (isTelegramChatTypeGroup(chatType)) {
5+
if (isGroupChat(chatType)) {
76
return ['administrator', 'creator'];
87
}
98
return null;
109
},
1110
shareModeGroup(chatType: string): string[] | null {
12-
if (isTelegramChatTypeGroup(chatType)) {
11+
if (isGroupChat(chatType)) {
1312
// 每个人在群里有上下文的时候,不限制
1413
if (!ENV.GROUP_CHAT_BOT_SHARE_MODE) {
1514
return null;
@@ -19,3 +18,7 @@ export const TELEGRAM_AUTH_CHECKER = {
1918
return null;
2019
},
2120
};
21+
22+
export function isGroupChat(type: string): boolean {
23+
return type === 'group' || type === 'supergroup';
24+
}

packages/lib/core/src/telegram/callback_query/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as Telegram from 'telegram-bot-api-types';
2-
import type { WorkerContext } from '../../config/context';
2+
import type { WorkerContext } from '../../config';
33
import { loadChatRoleWithContext } from '../command/auth';
4-
import { MessageSender } from '../utils/send';
4+
import { MessageSender } from '../sender';
55
import { AgentListCallbackQueryHandler, ModelChangeCallbackQueryHandler, ModelListCallbackQueryHandler } from './system';
66

77
const QUERY_HANDLERS = [

packages/lib/core/src/telegram/callback_query/system.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type * as Telegram from 'telegram-bot-api-types';
2-
import type { WorkerContext } from '../../config/context';
3-
import type { AgentUserConfig } from '../../config/env';
2+
import type { AgentUserConfig, WorkerContext } from '../../config';
43
import type { CallbackQueryHandler } from './types';
54
import { CHAT_AGENTS, loadChatLLM } from '../../agent';
6-
import { ENV } from '../../config/env';
7-
import { TELEGRAM_AUTH_CHECKER } from '../auth/auth';
8-
import { MessageSender } from '../utils/send';
9-
import { setUserConfig } from '../utils/utils';
5+
import { ENV } from '../../config';
6+
import { TELEGRAM_AUTH_CHECKER } from '../auth';
7+
import { MessageSender } from '../sender';
108

119
export class AgentListCallbackQueryHandler implements CallbackQueryHandler {
1210
prefix = 'al:';
@@ -145,10 +143,10 @@ export class ModelChangeCallbackQueryHandler implements CallbackQueryHandler {
145143
if (!chatAgent?.modelKey) {
146144
throw new Error(`modelKey not found: ${agent}`);
147145
}
148-
await setUserConfig({
146+
await context.execChangeAndSave({
149147
AI_PROVIDER: agent,
150148
[chatAgent.modelKey]: model,
151-
}, context);
149+
});
152150
console.log('Change model:', agent, model);
153151
const message: Telegram.EditMessageTextParams = {
154152
chat_id: query.message.chat.id,

packages/lib/core/src/telegram/callback_query/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as Telegram from 'telegram-bot-api-types';
2-
import type { WorkerContext } from '../../config/context';
2+
import type { WorkerContext } from '../../config';
33

44
export interface CallbackQueryHandler {
55
prefix: string;

0 commit comments

Comments
 (0)