fix: post processors

This commit is contained in:
ikechan8370 2025-02-19 13:10:59 +08:00
parent 20195ecfdf
commit 5add41c982
4 changed files with 138 additions and 42 deletions

View file

@ -0,0 +1,72 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
export class AbstractPostProcessor {
name = ''
/**
* 类型
* @type {'pre' | 'post'}
*/
type = 'post'
/**
*
* @param {{
* text: string,
* thinking_text?: string
* }} input
* @returns {Promise<{
* text: string,
* thinking_text?: string
* }>}
*/
async processInner (input) {}
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
/**
* collect
* @param {'pre' | 'post' | undefined} type
* @return {Promise<AbstractPostProcessor[]>}
*/
export async function collectProcessors (type) {
const processors = []
const directoryPath = __dirname // 当前目录
// 读取目录中的所有文件
const files = fs.readdirSync(directoryPath)
// 遍历所有文件,筛选出.js文件
for (const file of files) {
if (file.endsWith('.js') && file !== 'BasicProcessor.js') { // 排除自己
const fullPath = path.join(directoryPath, file)
try {
// 动态导入模块
const module = await import(fullPath)
// 遍历模块的所有导出成员
for (const key of Object.keys(module)) {
const ExportedClass = module[key]
// 确保它是一个类,并且继承了 AbstractPostProcessor
if (typeof ExportedClass === 'function' &&
Object.getPrototypeOf(ExportedClass) !== null) {
const parent = Object.getPrototypeOf(ExportedClass)
if (parent.name === 'AbstractPostProcessor') {
let instance = new ExportedClass()
if (!type || instance.type === type) {
processors.push(instance)
}
}
}
}
} catch (err) {
// console.error(`Error processing file ${file}:`, err)
}
}
}
return processors
}

View file

@ -0,0 +1,55 @@
import { AbstractPostProcessor } from './BasicProcessor.js'
export class ReasonerProcessor extends AbstractPostProcessor {
constructor () {
super()
this.name = 'ReasonerPostProcessor'
this.type = 'post'
}
/**
*
* @param {{
* text: string,
* thinking_text?: string
* }} input
* @returns {Promise<{
* text: string,
* thinking_text?: string
* }>}
*/
async processInner (input) {
// eslint-disable-next-line camelcase
const { text, thinking_text } = extractThinkingTextAndText(input.text)
return {
text,
// eslint-disable-next-line camelcase
thinking_text: input.thinking_text + thinking_text
}
}
}
/**
* written by gpt-4o
* @param str
* @returns {{thinkingText: string, text: *}|{thinkingText: *, text: *}}
*/
const extractThinkingTextAndText = (str) => {
// 使用正则表达式提取think标签内容
const thinkRegex = /<think>(.*?)<\/think>/s
const match = str.match(thinkRegex)
// 如果找到了<think>标签内容
if (match) {
// thinking_text就是<think>标签内的内容
const thinkingText = match[1].trim()
// text就是</think>标签后的部分
const text = str.slice(match.index + match[0].length).trim()
return { thinkingText, text }
}
// 如果没有<think>标签内容,返回空或原始内容
return { thinkingText: '', text: str.trim() }
}

View file

@ -43,7 +43,7 @@ export class QueryStarRailTool extends AbstractTool {
e.user_id = qq
e.isSr = true
await ProfileList.render(e)
return 'the player panel of genshin impact has been sent to group. you don\'t need text version'
return 'the player panel of star rail has been sent to group. you don\'t need text version'
}
} catch (err) {
return `failed to query, error: ${err.toString()}`