Add trigger effects and conditions

This commit is contained in:
kwaroran
2024-04-10 10:19:10 +09:00
parent 1a2da8224e
commit 1a750dc6c6
4 changed files with 141 additions and 60 deletions

View File

@@ -417,6 +417,9 @@ export const languageEnglish = {
triggerEffSetVar: 'Modify Variable',
triggerEffImperson: 'Send Chat',
triggerEffCommand: 'Run Command',
triggerEffRunTrigger: 'Run Trigger',
triggerEffStop: 'Stop Sending Prompt',
triggerEffCall: 'Call Trigger',
varableName: "Variable Name",
role: "Role",
location: 'Location',
@@ -435,6 +438,7 @@ export const languageEnglish = {
isNull: "is not set",
ifChatIndex: "If chat index",
ifRandom: "If random",
ifValue: "If Value",
hideRealm: "Hide RisuRealm",
popularityLevel: "{} Popularity",
colorScheme: "Color Scheme",

View File

@@ -59,10 +59,10 @@
<span class="text-textcolor mt-4">Conditions
<button aria-labelledby="Add Conditions" class="float-right text-textcolor2 hover:text-green-500" on:click={() => {
value.conditions.push({
type: 'exists',
type: 'value',
value: '',
type2: 'loose',
depth: 3
operator: '=',
var: ''
})
value.conditions = value.conditions
@@ -93,9 +93,9 @@
depth: 3
}
}
if(cond.type === 'var'){
if(cond.type === 'var' || cond.type === 'value'){
cond = {
type: 'var',
type: cond.type,
var: '',
value: '',
operator: '='
@@ -108,8 +108,8 @@
operator: '='
}
}
}}>
<OptionInput value="value">{language.ifValue}</OptionInput>
<OptionInput value="exists">{language.triggerCondExists}</OptionInput>
<OptionInput value="var">{language.triggerCondVar}</OptionInput>
<OptionInput value="chatindex">{language.ifChatIndex}</OptionInput>
@@ -127,11 +127,14 @@
<span class="text-textcolor2 text-sm">{language.searchDepth}</span>
<NumberInput size="sm" bind:value={cond.depth} />
{/if}
{#if cond.type === 'var' || cond.type === 'chatindex'}
{#if cond.type === 'var' || cond.type === 'chatindex' || cond.type === 'value'}
{#if cond.type === 'var'}
<span class="text-textcolor2 text-sm">{language.varableName}</span>
<TextInput size="sm" bind:value={cond.var} />
{/if}
{#if cond.type === 'value'}
<TextInput size="sm" bind:value={cond.var} />
{/if}
<span class="text-textcolor2 text-sm">{language.value}</span>
<SelectInput bind:value={cond.operator} size="sm">
<OptionInput value="=">{language.equal}</OptionInput>
@@ -217,19 +220,34 @@
value: ''
}
}
if(effect.type === 'stop'){
effect = {
type: 'stop',
}
}
if(effect.type === 'runtrigger'){
effect = {
type: 'runtrigger',
value: ''
}
}
}}>
{#if effect.type === 'systemprompt' || value.type === 'start'}
<OptionInput value="systemprompt">{language.triggerEffSysPrompt}</OptionInput>
{/if}
<OptionInput value="setvar">{language.triggerEffSetVar}</OptionInput>
<OptionInput value="impersonate">{language.triggerEffImperson}</OptionInput>
<OptionInput value="command">{language.triggerEffCommand}</OptionInput>
{#if effect.type === 'systemprompt' || value.type === 'start'}
<OptionInput value="systemprompt">{language.triggerEffSysPrompt}</OptionInput>
{/if}
{#if effect.type === 'stop' || value.type === 'start'}
<OptionInput value="stop">{language.triggerEffStop}</OptionInput>
{/if}
<OptionInput value="runtrigger">{language.triggerEffRunTrigger}</OptionInput>
</SelectInput>
{#if value.type !== 'start' && (effect.type === 'systemprompt' || effect.type === 'stop')}
<span class="text-red-400 text-sm">{language.invaildTriggerEffect}</span>
{/if}
{#if effect.type === 'systemprompt'}
{#if value.type !== 'start'}
<span class="text-red-400 text-sm">{language.invaildTriggerEffect}</span>
{/if}
<span class="text-textcolor2 text-sm">{language.location}</span>
<SelectInput bind:value={effect.location} size="sm">
<OptionInput value="start">{language.promptstart}</OptionInput>
@@ -254,6 +272,10 @@
<TextInput size="sm" bind:value={effect.value} />
{/if}
{#if effect.type === 'runtrigger'}
<span class="text-textcolor2 text-sm">{language.name}</span>
<TextInput size="sm" bind:value={effect.value} />
{/if}
{#if effect.type === 'command'}
<span class="text-textcolor2 text-sm">{language.value}</span>
<TextAreaInput size="sm" bind:value={effect.value} />

View File

@@ -552,6 +552,10 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentChat = triggerResult.chat
ms = currentChat.message
currentTokens += triggerResult.tokens
if(triggerResult.stopSending){
doingChat.set(false)
return false
}
}
let index = 0

View File

@@ -16,10 +16,10 @@ export interface triggerscript{
export type triggerCondition = triggerConditionsVar|triggerConditionsExists|triggerConditionsChatIndex
export type triggerEffect = triggerEffectSetvar|triggerEffectSystemPrompt|triggerEffectImpersonate|triggerEffectCommand
export type triggerEffect = triggerEffectSetvar|triggerEffectSystemPrompt|triggerEffectImpersonate|triggerEffectCommand|triggerEffectStop|triggerEffectRunTrigger
export type triggerConditionsVar = {
type:'var'
type:'var'|'value'
var:string
value:string
operator:'='|'!='|'>'|'<'|'>='|'<='|'null'
@@ -62,6 +62,15 @@ export interface triggerEffectCommand{
value: string
}
export interface triggerEffectRunTrigger{
type: 'runtrigger',
value: string
}
export interface triggerEffectStop{
type: 'stop'
}
export type additonalSysPrompt = {
start:string,
historyend: string,
@@ -69,17 +78,23 @@ export type additonalSysPrompt = {
}
export async function runTrigger(char:character,mode:triggerMode, arg:{
chat: Chat
chat: Chat,
recursiveCount?: number
additonalSysPrompt?: additonalSysPrompt
stopSending?: boolean
manualName?: string
}){
arg.recursiveCount ??= 0
char = cloneDeep(char)
let varChanged = false
let additonalSysPrompt:additonalSysPrompt = {
let stopSending = arg.stopSending ?? false
let additonalSysPrompt:additonalSysPrompt = arg.additonalSysPrompt ?? {
start:'',
historyend: '',
promptend: ''
}
const triggers = char.triggerscript.concat(getModuleTriggers())
const chat = cloneDeep(arg.chat ?? char.chats[char.chatPage])
let chat = cloneDeep(arg.chat ?? char.chats[char.chatPage])
if((!triggers) || (triggers.length === 0)){
return null
}
@@ -96,14 +111,22 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
for(const trigger of triggers){
if(mode !== trigger.type){
if(arg.manualName){
if(trigger.comment !== arg.manualName){
continue
}
}
else if(mode !== trigger.type){
continue
}
let pass = true
for(const condition of trigger.conditions){
if(condition.type === 'var' || condition.type === 'chatindex'){
let varValue = (condition.type === 'var') ? (getVar(condition.var) ?? 'null') : (chat.message.length.toString())
if(condition.type === 'var' || condition.type === 'chatindex' || condition.type === 'value'){
let varValue = (condition.type === 'var') ? (getVar(condition.var) ?? 'null') :
(condition.type === 'chatindex') ? (chat.message.length.toString()) :
(condition.type === 'value') ? condition.var : null
if(varValue === undefined || varValue === null){
pass = false
break
@@ -172,48 +195,76 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
continue
}
for(const effect of trigger.effect){
if(effect.type === 'setvar'){
const effectValue = risuChatParser(effect.value,{chara:char})
const varKey = risuChatParser(effect.var,{chara:char})
switch(effect.operator){
case '=':{
setVar(varKey, effectValue)
break
}
case '+=':{
setVar(varKey, (Number(getVar(varKey)) + Number(effectValue)).toString())
break
}
case '-=':{
setVar(varKey, (Number(getVar(varKey)) - Number(effectValue)).toString())
break
}
case '*=':{
setVar(varKey, (Number(getVar(varKey)) * Number(effectValue)).toString())
break
}
case '/=':{
setVar(varKey, (Number(getVar(varKey)) / Number(effectValue)).toString())
break
switch(effect.type){
case'setvar': {
const effectValue = risuChatParser(effect.value,{chara:char})
const varKey = risuChatParser(effect.var,{chara:char})
switch(effect.operator){
case '=':{
setVar(varKey, effectValue)
break
}
case '+=':{
setVar(varKey, (Number(getVar(varKey)) + Number(effectValue)).toString())
break
}
case '-=':{
setVar(varKey, (Number(getVar(varKey)) - Number(effectValue)).toString())
break
}
case '*=':{
setVar(varKey, (Number(getVar(varKey)) * Number(effectValue)).toString())
break
}
case '/=':{
setVar(varKey, (Number(getVar(varKey)) / Number(effectValue)).toString())
break
}
}
break
}
}
else if(effect.type === 'systemprompt'){
const effectValue = risuChatParser(effect.value,{chara:char})
additonalSysPrompt[effect.location] += effectValue + "\n\n"
}
else if(effect.type === 'impersonate'){
const effectValue = risuChatParser(effect.value,{chara:char})
if(effect.role === 'user'){
chat.message.push({role: 'user', data: effectValue})
case 'systemprompt':{
const effectValue = risuChatParser(effect.value,{chara:char})
additonalSysPrompt[effect.location] += effectValue + "\n\n"
break
}
else if(effect.role === 'char'){
chat.message.push({role: 'char', data: effectValue})
case 'impersonate':{
const effectValue = risuChatParser(effect.value,{chara:char})
if(effect.role === 'user'){
chat.message.push({role: 'user', data: effectValue})
}
else if(effect.role === 'char'){
chat.message.push({role: 'char', data: effectValue})
}
break
}
case 'command':{
const effectValue = risuChatParser(effect.value,{chara:char})
await processMultiCommand(effectValue)
break
}
case 'stop':{
stopSending = true
break
}
case 'runtrigger':{
if(arg.recursiveCount < 10){
arg.recursiveCount++
const r = await runTrigger(char,'manual',{
chat,
recursiveCount: arg.recursiveCount,
additonalSysPrompt,
stopSending,
manualName: effect.value
})
if(r){
additonalSysPrompt = r.additonalSysPrompt
chat = r.chat
stopSending = r.stopSending
}
}
break
}
}
else if(effect.type === 'command'){
const effectValue = risuChatParser(effect.value,{chara:char})
await processMultiCommand(effectValue)
}
}
}
@@ -235,6 +286,6 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
console.log(chat)
return {additonalSysPrompt, chat, tokens:caculatedTokens}
return {additonalSysPrompt, chat, tokens:caculatedTokens, stopSending}
}