[feat] trigger runner2

This commit is contained in:
kwaroran
2023-07-28 05:53:12 +09:00
parent 0e9f3cbce0
commit 7b768d3e73
7 changed files with 186 additions and 70 deletions

View File

@@ -305,7 +305,7 @@ function wppParser(data:string){
const rgx = /(?:{{|<)(.+?)(?:}}|>)/gm
type matcherArg = {chatID:number,db:Database,chara:character|string,rmVar:boolean}
type matcherArg = {chatID:number,db:Database,chara:character|string,rmVar:boolean,var?:{[key:string]:string}}
const matcher = (p1:string,matcherArg:matcherArg) => {
if(p1.length > 10000){
return ''
@@ -448,7 +448,7 @@ const matcher = (p1:string,matcherArg:matcherArg) => {
const v = arra[1]
switch(arra[0]){
case 'getvar':{
const d =getVarChat(chatID)
const d = matcherArg.var ?? getVarChat(chatID)
return d[v] ?? "[Null]"
}
case 'calc':{
@@ -568,7 +568,8 @@ export function risuChatParser(da:string, arg:{
chatID?:number
db?:Database
chara?:string|character|groupChat
rmVar?:boolean
rmVar?:boolean,
var?:{[key:string]:string}
} = {}):string{
const chatID = arg.chatID ?? -1
const db = arg.db ?? get(DataBase)
@@ -597,7 +598,8 @@ export function risuChatParser(da:string, arg:{
chatID: chatID,
chara: chara,
rmVar: arg.rmVar ?? false,
db: db
db: db,
var: arg.var ?? null
}
while(pointer < da.length){
switch(da[pointer]){

View File

@@ -15,6 +15,7 @@ import { supaMemory } from "./memory/supaMemory";
import { v4 } from "uuid";
import { cloneDeep } from "lodash";
import { groupOrder } from "./group";
import { runTrigger, type additonalSysPrompt } from "./triggers";
export interface OpenAIChat{
role: 'system'|'user'|'assistant'|'function'
@@ -185,7 +186,6 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
chatObjects.push({ role, content });
}
console.log(chatObjects)
return chatObjects;
}
@@ -288,7 +288,14 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentTokens += await tokenizer.tokenizeChat(chat)
}
const ms = currentChat.message
let ms = currentChat.message
const triggerResult = runTrigger(currentChar, 'start', {chat: currentChat})
if(triggerResult){
currentChat = triggerResult.chat
ms = currentChat.message
}
for(const msg of ms){
let formedChat = processScript(nowChatroom,risuChatParser(msg.data, {chara: currentChar, rmVar: true}), 'editprocess')
let name = ''
@@ -368,6 +375,29 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
unformated.chats = chats
if(triggerResult){
if(triggerResult.additonalSysPrompt.promptend){
unformated.postEverything.push({
role: 'system',
content: triggerResult.additonalSysPrompt.promptend
})
}
if(triggerResult.additonalSysPrompt.historyend){
unformated.lastChat.push({
role: 'system',
content: triggerResult.additonalSysPrompt.historyend
})
}
if(triggerResult.additonalSysPrompt.start){
unformated.lastChat.unshift({
role: 'system',
content: triggerResult.additonalSysPrompt.start
})
}
}
//make into one
let formated:OpenAIChat[] = []
@@ -462,6 +492,11 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
}
}
const triggerResult = runTrigger(currentChar, 'output', {chat:currentChat})
if(triggerResult){
db.characters[selectedChar].chats[selectedChat] = triggerResult.chat
setDatabase(db)
}
await sayTTS(currentChar, result)
}
else{
@@ -477,6 +512,10 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
saying: currentChar.chaId
})
db.characters[selectedChar].reloadKeys += 1
const triggerResult = runTrigger(currentChar, 'output', {chat:currentChat})
if(triggerResult){
db.characters[selectedChar].chats[selectedChat] = triggerResult.chat
}
await sayTTS(currentChar, result)
setDatabase(db)
}
@@ -646,7 +685,6 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
const msgs = db.characters[selectedChar].chats[selectedChat].message
let msgStr = ''
for(let i = (msgs.length - 1);i>=0;i--){
console.log(i,msgs.length,msgs[i])
if(msgs[i].role === 'char'){
msgStr = `character: ${msgs[i].data.replace(/\n/, ' ')} \n` + msgStr
}
@@ -663,5 +701,6 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
setDatabase(db)
}
}
return true
}

View File

@@ -1,5 +1,6 @@
import { getVarChat } from "../parser";
import type { character } from "../storage/database";
import { cloneDeep } from "lodash";
import { getVarChat, risuChatParser } from "../parser";
import type { Chat, character } from "../storage/database";
export interface triggerscript{
comment: string;
@@ -8,7 +9,7 @@ export interface triggerscript{
effect:triggerEffect[]
}
export type triggerCondition = triggerConditionsVar|triggerConditionsExists
export type triggerCondition = triggerConditionsVar|triggerConditionsExists|triggerConditionsChatIndex
export type triggerEffect = triggerEffectSetvar|triggerEffectSystemPrompt|triggerEffectImpersonate
@@ -16,7 +17,13 @@ export type triggerConditionsVar = {
type:'var'
var:string
value:string
operator:'='|'!='|'>'|'<'|'>='|'<='
operator:'='|'!='|'>'|'<'|'>='|'<='|'null'
}
export type triggerConditionsChatIndex = {
type:'chatindex'
value:string
operator:'='|'!='|'>'|'<'|'>='|'<='|'null'
}
export type triggerConditionsExists ={
@@ -45,8 +52,17 @@ export interface triggerEffectImpersonate{
value:string
}type triggerMode = 'start'|'manual'|'output'|'input'
export function runTrigger(char:character,mode:triggerMode){
let additonalSysPrompt = {
export type additonalSysPrompt = {
start:string,
historyend: string,
promptend: string
}
export function runTrigger(char:character,mode:triggerMode, arg:{
chat?: Chat
} = {}){
char = cloneDeep(char)
let additonalSysPrompt:additonalSysPrompt = {
start:'',
historyend: '',
promptend: ''
@@ -54,9 +70,9 @@ export function runTrigger(char:character,mode:triggerMode){
let varValues = getVarChat(-1, char)
let varValuesChanged = false
const triggers = char.triggerscript
const chat = char.chats[char.chatPage]
const chat = arg.chat ?? char.chats[char.chatPage]
if(!triggers){
return {additonalSysPrompt, char}
return null
}
for(const trigger of triggers){
@@ -66,61 +82,63 @@ export function runTrigger(char:character,mode:triggerMode){
let pass = true
for(const condition of trigger.conditions){
if(condition.type === 'var'){
const varValue = varValues[condition.var]
if(condition.type === 'var' || condition.type === 'chatindex'){
const varValue = (condition.type === 'var') ? (varValues[condition.var] ?? '[Null]') : (chat.message.length)
if(varValue === undefined || varValue === null){
pass = false
break
}
else{
if(condition.operator === '='){
if(varValue !== condition.value){
pass = false
switch(condition.operator){
case '=':
if(varValue !== condition.value){
pass = false
}
break
}
}
else if(condition.operator === '!='){
if(varValue === condition.value){
pass = false
case '!=':
if(varValue === condition.value){
pass = false
}
break
}
}
else if(condition.operator === '>'){
if(Number(varValue) > Number(condition.value)){
pass = false
case '>':
if(Number(varValue) > Number(condition.value)){
pass = false
}
break
}
}
else if(condition.operator === '<'){
if(Number(varValue) < Number(condition.value)){
pass = false
case '<':
if(Number(varValue) < Number(condition.value)){
pass = false
}
break
}
}
else if(condition.operator === '>='){
if(Number(varValue) >= Number(condition.value)){
pass = false
case '>=':
if(Number(varValue) >= Number(condition.value)){
pass = false
}
break
}
}
else if(condition.operator === '<='){
if(Number(varValue) <= Number(condition.value)){
pass = false
case '<=':
if(Number(varValue) <= Number(condition.value)){
pass = false
}
break
case 'null':
if(varValue !== '[Null]'){
pass = false
}
break
}
}
}
}
else if(condition.type === 'exists'){
const val = risuChatParser(condition.value,{chara:char, var:varValues})
let da = chat.message.slice(0-condition.depth).map((v)=>v.data).join(' ')
if(condition.type2 === 'strict'){
pass = da.split(' ').includes(condition.value)
pass = da.split(' ').includes(val)
}
else if(condition.type2 === 'loose'){
pass = da.toLowerCase().includes(condition.value.toLowerCase())
pass = da.toLowerCase().includes(val.toLowerCase())
}
else if(condition.type2 === 'regex'){
pass = new RegExp(condition.value).test(da)
pass = new RegExp(val).test(da)
}
}
if(!pass){
@@ -169,8 +187,9 @@ export function runTrigger(char:character,mode:triggerMode){
chat.message[chat.message.length-1].data = chat.message.at(-1).data.replaceAll(/{{(setvar|getvar)::.+?}}/gis,'') + Object.keys(varValues).map((v)=>`{{setvar::${v}::${varValues[v]}}}`).join('')
}
char.chats[char.chatPage] = chat
return {additonalSysPrompt, char}
if(arg.chat !== undefined && arg.chat !== null){
char.chats[char.chatPage] = chat
}
return {additonalSysPrompt, chat}
}