Skip to content

Commit

Permalink
chore: 合并 extractImageFileID和findPhotoFileID函数
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 15, 2024
1 parent 2977d62 commit e919fa5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 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.

23 changes: 8 additions & 15 deletions dist/index.js

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

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 = 1731642094;
export const BUILD_VERSION = 'ce77d5f';
export const BUILD_TIMESTAMP = 1731643971;
export const BUILD_VERSION = '2977d62';
20 changes: 6 additions & 14 deletions packages/lib/core/src/telegram/handler/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ export async function chatWithLLM(message: Telegram.Message, params: UserMessage
}
}

function findPhotoFileID(photos: Telegram.PhotoSize[], offset: number): string {
let sizeIndex = 0;
if (offset >= 0) {
sizeIndex = offset;
} else if (offset < 0) {
sizeIndex = photos.length + offset;
}
sizeIndex = Math.max(0, Math.min(sizeIndex, photos.length - 1));
return photos[sizeIndex].file_id;
}

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

function extractImageFieldID(message: Telegram.Message): string | null {
function extractImageFileID(message: Telegram.Message): string | null {
if (message.photo && message.photo.length > 0) {
return findPhotoFileID(message.photo, ENV.TELEGRAM_PHOTO_SIZE_OFFSET);
const offset = ENV.TELEGRAM_PHOTO_SIZE_OFFSET;
const length = message.photo.length;
const sizeIndex = Math.max(0, Math.min(offset >= 0 ? offset : length + offset, length - 1));
return message.photo[sizeIndex]?.file_id;
} else if (message.document && message.document.thumbnail) {
return message.document.thumbnail.file_id;
}
Expand All @@ -120,7 +112,7 @@ export class ChatHandler implements MessageHandler {
role: 'user',
content: text,
};
const url = await extractImageURL(extractImageFieldID(message), context);
const url = await extractImageURL(extractImageFileID(message), context);
if (url) {
const contents = new Array<TextPart | ImagePart | FilePart>();
if (text) {
Expand Down

0 comments on commit e919fa5

Please sign in to comment.