Fix triggerscript and commands
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
import { DataBase, setDatabase } from "../storage/database";
|
import { DataBase, setDatabase } from "../storage/database";
|
||||||
import { selectedCharID } from "../stores";
|
import { selectedCharID } from "../stores";
|
||||||
import { alertInput, alertMd, alertSelect, alertToast } from "../alert";
|
import { alertInput, alertMd, alertNormal, alertSelect, alertToast } from "../alert";
|
||||||
import { sayTTS } from "./tts";
|
import { sayTTS } from "./tts";
|
||||||
import { risuChatParser } from "../parser";
|
import { risuChatParser } from "../parser";
|
||||||
import { sendChat } from ".";
|
import { sendChat } from ".";
|
||||||
@@ -22,8 +22,10 @@ export async function processMultiCommand(command:string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
splited.push(command.slice(lastIndex))
|
splited.push(command.slice(lastIndex))
|
||||||
|
console.log(splited)
|
||||||
for(let i = 0; i<splited.length; i++){
|
for(let i = 0; i<splited.length; i++){
|
||||||
const result = await processCommand(splited[i].trim(), pipe)
|
const result = await processCommand(splited[i].trim(), pipe)
|
||||||
|
console.log(pipe)
|
||||||
if(result === false){
|
if(result === false){
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -41,6 +43,10 @@ async function processCommand(command:string, pipe:string):Promise<false | strin
|
|||||||
const currentChat = currentChar.chats[currentChar.chatPage]
|
const currentChat = currentChar.chats[currentChar.chatPage]
|
||||||
let {commandName, arg, namedArg} = commandParser(command, pipe)
|
let {commandName, arg, namedArg} = commandParser(command, pipe)
|
||||||
|
|
||||||
|
if(!arg){
|
||||||
|
arg = pipe
|
||||||
|
}
|
||||||
|
|
||||||
arg = risuChatParser(arg, {
|
arg = risuChatParser(arg, {
|
||||||
chara: currentChar.type === 'character' ? currentChar : null
|
chara: currentChar.type === 'character' ? currentChar : null
|
||||||
})
|
})
|
||||||
@@ -58,12 +64,9 @@ async function processCommand(command:string, pipe:string):Promise<false | strin
|
|||||||
pipe = await alertInput(arg)
|
pipe = await alertInput(arg)
|
||||||
return pipe
|
return pipe
|
||||||
}
|
}
|
||||||
case 'echo':{
|
case 'echo':
|
||||||
alertToast(arg)
|
|
||||||
return pipe
|
|
||||||
}
|
|
||||||
case 'popup':{
|
case 'popup':{
|
||||||
alertMd(arg)
|
alertNormal(arg)
|
||||||
return pipe
|
return pipe
|
||||||
}
|
}
|
||||||
case 'pass':{
|
case 'pass':{
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { risuChatParser } from "../parser";
|
|||||||
import type { Chat, character } from "../storage/database";
|
import type { Chat, character } from "../storage/database";
|
||||||
import { tokenize } from "../tokenizer";
|
import { tokenize } from "../tokenizer";
|
||||||
import { getModuleTriggers } from "./modules";
|
import { getModuleTriggers } from "./modules";
|
||||||
|
import { get } from "svelte/store";
|
||||||
|
import { CurrentChat } from "../stores";
|
||||||
|
|
||||||
export interface triggerscript{
|
export interface triggerscript{
|
||||||
comment: string;
|
comment: string;
|
||||||
@@ -64,6 +66,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
chat: Chat
|
chat: Chat
|
||||||
}){
|
}){
|
||||||
char = cloneDeep(char)
|
char = cloneDeep(char)
|
||||||
|
let varChanged = false
|
||||||
let additonalSysPrompt:additonalSysPrompt = {
|
let additonalSysPrompt:additonalSysPrompt = {
|
||||||
start:'',
|
start:'',
|
||||||
historyend: '',
|
historyend: '',
|
||||||
@@ -74,15 +77,15 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
if((!triggers) || (triggers.length === 0)){
|
if((!triggers) || (triggers.length === 0)){
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const charVars = chat.scriptstate
|
|
||||||
let varValues:{[key:string]:string} = {}
|
|
||||||
let varValuesChanged = false
|
|
||||||
|
|
||||||
for(const key in charVars){
|
function getVar(key:string){
|
||||||
if(!key.startsWith('$')){
|
return chat.scriptstate?.['$' + key]
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
varValues[key] = charVars[key.substring(1)].toString()
|
|
||||||
|
function setVar(key:string, value:string){
|
||||||
|
varChanged = true
|
||||||
|
chat.scriptstate ??= {}
|
||||||
|
chat.scriptstate['$' + key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +97,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
let pass = true
|
let pass = true
|
||||||
for(const condition of trigger.conditions){
|
for(const condition of trigger.conditions){
|
||||||
if(condition.type === 'var' || condition.type === 'chatindex'){
|
if(condition.type === 'var' || condition.type === 'chatindex'){
|
||||||
const varValue = (condition.type === 'var') ? (varValues[condition.var] ?? '[Null]') : (chat.message.length)
|
const varValue = (condition.type === 'var') ? (getVar(condition.var) ?? 'null') : (chat.message.length)
|
||||||
if(varValue === undefined || varValue === null){
|
if(varValue === undefined || varValue === null){
|
||||||
pass = false
|
pass = false
|
||||||
break
|
break
|
||||||
@@ -140,7 +143,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(condition.type === 'exists'){
|
else if(condition.type === 'exists'){
|
||||||
const val = risuChatParser(condition.value,{chara:char, var:varValues})
|
const val = risuChatParser(condition.value,{chara:char})
|
||||||
let da = chat.message.slice(0-condition.depth).map((v)=>v.data).join(' ')
|
let da = chat.message.slice(0-condition.depth).map((v)=>v.data).join(' ')
|
||||||
if(condition.type2 === 'strict'){
|
if(condition.type2 === 'strict'){
|
||||||
pass = da.split(' ').includes(val)
|
pass = da.split(' ').includes(val)
|
||||||
@@ -162,23 +165,27 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
for(const effect of trigger.effect){
|
for(const effect of trigger.effect){
|
||||||
if(effect.type === 'setvar'){
|
if(effect.type === 'setvar'){
|
||||||
switch(effect.operator){
|
switch(effect.operator){
|
||||||
case '=':
|
case '=':{
|
||||||
varValues[effect.var] = effect.value
|
setVar(effect.var, effect.value)
|
||||||
break
|
|
||||||
case '+=':
|
|
||||||
varValues[effect.var] = (Number(varValues[effect.var]) + Number(effect.value)).toString()
|
|
||||||
break
|
|
||||||
case '-=':
|
|
||||||
varValues[effect.var] = (Number(varValues[effect.var]) - Number(effect.value)).toString()
|
|
||||||
break
|
|
||||||
case '*=':
|
|
||||||
varValues[effect.var] = (Number(varValues[effect.var]) * Number(effect.value)).toString()
|
|
||||||
break
|
|
||||||
case '/=':
|
|
||||||
varValues[effect.var] = (Number(varValues[effect.var]) / Number(effect.value)).toString()
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
varValuesChanged = true
|
case '+=':{
|
||||||
|
setVar(effect.var, (Number(getVar(effect.var)) + Number(effect.value)).toString())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case '-=':{
|
||||||
|
setVar(effect.var, (Number(getVar(effect.var)) - Number(effect.value)).toString())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case '*=':{
|
||||||
|
setVar(effect.var, (Number(getVar(effect.var)) * Number(effect.value)).toString())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case '/=':{
|
||||||
|
setVar(effect.var, (Number(getVar(effect.var)) / Number(effect.value)).toString())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(effect.type === 'systemprompt'){
|
else if(effect.type === 'systemprompt'){
|
||||||
additonalSysPrompt[effect.location] += effect.value + "\n\n"
|
additonalSysPrompt[effect.location] += effect.value + "\n\n"
|
||||||
@@ -193,6 +200,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let caculatedTokens = 0
|
let caculatedTokens = 0
|
||||||
if(additonalSysPrompt.start){
|
if(additonalSysPrompt.start){
|
||||||
caculatedTokens += await tokenize(additonalSysPrompt.start)
|
caculatedTokens += await tokenize(additonalSysPrompt.start)
|
||||||
@@ -203,10 +211,9 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
|||||||
if(additonalSysPrompt.promptend){
|
if(additonalSysPrompt.promptend){
|
||||||
caculatedTokens += await tokenize(additonalSysPrompt.promptend)
|
caculatedTokens += await tokenize(additonalSysPrompt.promptend)
|
||||||
}
|
}
|
||||||
if(varValuesChanged){
|
if(varChanged){
|
||||||
for(const key in varValues){
|
const currentChat = get(CurrentChat)
|
||||||
chat.scriptstate['$' + key] = varValues[key]
|
currentChat.scriptstate = chat.scriptstate
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {additonalSysPrompt, chat, tokens:caculatedTokens}
|
return {additonalSysPrompt, chat, tokens:caculatedTokens}
|
||||||
|
|||||||
Reference in New Issue
Block a user