Add gettokens API

This commit is contained in:
kwaroran
2025-03-27 19:21:31 +09:00
parent 58f1cb4fcc
commit 9e00c9b17a

View File

@@ -12,6 +12,7 @@ import { requestChatData } from "./request";
import { v4 } from "uuid";
import { getModuleTriggers } from "./modules";
import { Mutex } from "../mutex";
import { tokenize } from "../tokenizer";
let luaFactory:LuaFactory
let LuaSafeIds = new Set<string>()
@@ -154,6 +155,12 @@ export async function runLua(code:string, arg:{
let roleData:'user'|'char' = role === 'user' ? 'user' : 'char'
luaEngineState.chat.message.splice(index, 0, {role: roleData, data: value})
})
luaEngine.global.set('getTokens', async (id:string, value:string) => {
if(!LuaSafeIds.has(id)){
return
}
return await tokenize(value)
})
luaEngine.global.set('getChatLength', (id:string) => {
if(!LuaSafeIds.has(id)){
return