feat: Open read-only access to persona name/desc from Lua (#847)

# PR Checklist
- [ ] Have you checked if it works normally in all models? *Ignore this
if it doesn't use models.*
- [x] Have you checked if it works normally in all web, local, and node
hosted versions? If it doesn't, have you blocked it in those versions?
- [x] Have you added type definitions?

# Description

This PR adds read-only persona name and description access from Lua by
adding `getPersonaName(triggerId)` and
`getPersonaDescription(triggerId)`. The description is parsed before
returning.
This commit is contained in:
kwaroran
2025-05-17 01:08:21 +09:00
committed by GitHub

View File

@@ -1,6 +1,6 @@
import { getChatVar, hasher, setChatVar, getGlobalChatVar, type simpleCharacterArgument } from "../parser.svelte";
import { getChatVar, hasher, setChatVar, getGlobalChatVar, type simpleCharacterArgument, risuChatParser } from "../parser.svelte";
import { LuaEngine, LuaFactory } from "wasmoon";
import { getCurrentCharacter, getCurrentChat, getDatabase, setCurrentChat, setDatabase, type Chat, type character, type groupChat } from "../storage/database.svelte";
import { getCurrentCharacter, getCurrentChat, getDatabase, setDatabase, type Chat, type character, type groupChat } from "../storage/database.svelte";
import { get } from "svelte/store";
import { ReloadGUIPointer, selectedCharID } from "../stores.svelte";
import { alertSelect, alertError, alertInput, alertNormal } from "../alert";
@@ -14,6 +14,7 @@ import { getModuleTriggers } from "./modules";
import { Mutex } from "../mutex";
import { tokenize } from "../tokenizer";
import { fetchNative } from "../globalApi.svelte";
import { getPersonaPrompt, getUserName } from '../util';
let luaFactory:LuaFactory
let LuaSafeIds = new Set<string>()
@@ -460,6 +461,26 @@ export async function runLua(code:string, arg:{
return char.firstMessage
})
luaEngine.global.set('getPersonaName', (id:string) => {
if(!LuaSafeIds.has(id)){
return
}
return getUserName()
})
luaEngine.global.set('getPersonaDescription', (id:string) => {
if(!LuaSafeIds.has(id)){
return
}
const db = getDatabase()
const selectedChar = get(selectedCharID)
const char = db.characters[selectedChar]
return risuChatParser(getPersonaPrompt(), { chara: char })
})
luaEngine.global.set('getBackgroundEmbedding', async (id:string) => {
if(!LuaSafeIds.has(id)){
return