feat: add triggercode

This commit is contained in:
kwaroran
2024-06-29 00:45:57 +09:00
parent 39e7a8df8b
commit 8a94ef938e
5 changed files with 67 additions and 5 deletions

View File

@@ -663,4 +663,7 @@ export const languageEnglish = {
doNotTranslate: "Do Not Translate", doNotTranslate: "Do Not Translate",
includePersonaName: "Include Persona Name", includePersonaName: "Include Persona Name",
hidePersonaName: "Hide Persona Name", hidePersonaName: "Hide Persona Name",
triggerSwitchWarn: "If you change the trigger type, current triggers will be lost. do you want to continue?",
codeMode: "Code Mode",
blockMode: "Block Mode",
} }

View File

@@ -580,7 +580,44 @@
}}><PlusIcon /></button> }}><PlusIcon /></button>
<span class="text-textcolor mt-4">{language.triggerScript} <Help key="triggerScript"/></span> <span class="text-textcolor mt-4">{language.triggerScript} <Help key="triggerScript"/></span>
<TriggerList bind:value={currentChar.data.triggerscript} lowLevelAble={currentChar.data.lowLevelAccess} /> <div class="flex items-start mt-2 gap-2">
<button class="bg-bgcolor py-2 rounded-lg px-4" class:ring-1={currentChar.data?.triggerscript?.[0]?.effect?.[0]?.type !== 'triggercode'} on:click|stopPropagation={async () => {
if(currentChar.type === 'character' && currentChar.data?.triggerscript?.[0]?.effect?.[0]?.type === 'triggercode'){
const codeTrigger = currentChar.data?.triggerscript?.[0]?.effect?.[0]?.code
if(codeTrigger){
const t = await alertConfirm(language.triggerSwitchWarn)
if(!t){
return
}
}
currentChar.data.triggerscript = []
}
}}>{language.blockMode}</button>
<button class="bg-bgcolor py-2 rounded-lg px-4" class:ring-1={currentChar.data?.triggerscript?.[0]?.effect?.[0]?.type === 'triggercode'} on:click|stopPropagation={async () => {
if(currentChar.type === 'character' && currentChar.data?.triggerscript?.[0]?.effect?.[0]?.type !== 'triggercode'){
if(currentChar.data?.triggerscript && currentChar.data?.triggerscript.length > 0){
const t = await alertConfirm(language.triggerSwitchWarn)
if(!t){
return
}
}
currentChar.data.triggerscript = [{
comment: "",
type: "start",
conditions: [],
effect: [{
type: "triggercode",
code: ""
}]
}]
}
}}>{language.codeMode}</button>
</div>
{#if currentChar.data?.triggerscript?.[0]?.effect?.[0]?.type === 'triggercode'}
<TextAreaInput highlight margin="both" autocomplete="off" bind:value={currentChar.data.triggerscript[0].effect[0].code}></TextAreaInput>
{:else}
<TriggerList bind:value={currentChar.data.triggerscript} lowLevelAble={currentChar.data.lowLevelAccess} />
{/if}
<button class="font-medium cursor-pointer hover:text-green-500 mb-2" on:click={() => { <button class="font-medium cursor-pointer hover:text-green-500 mb-2" on:click={() => {
if(currentChar.type === 'character'){ if(currentChar.type === 'character'){
let script = currentChar.data.triggerscript let script = currentChar.data.triggerscript

View File

@@ -73,6 +73,7 @@
onInput() onInput()
}} }}
translate="no" translate="no"
>{value ?? ''}</div> >{value ?? ''}</div>
{/if} {/if}
</div> </div>

View File

@@ -2026,6 +2026,7 @@ export async function risuCommandParser(da:string, arg:{
funcName?:string funcName?:string
passed?:string[], passed?:string[],
recursiveCount?:number recursiveCount?:number
lowLevelAccess?:boolean
} = {}):Promise<{[key:string]:string}>{ } = {}):Promise<{[key:string]:string}>{
const db = arg.db ?? get(DataBase) const db = arg.db ?? get(DataBase)
const aChara = arg.chara const aChara = arg.chara
@@ -2068,7 +2069,8 @@ export async function risuCommandParser(da:string, arg:{
consistantChar: false, consistantChar: false,
funcName: arg.funcName ?? null, funcName: arg.funcName ?? null,
text: da, text: da,
recursiveCount: recursiveCount recursiveCount: recursiveCount,
lowLevelAccess: arg.lowLevelAccess ?? false
} }
let tempVar:{[key:string]:string} = {} let tempVar:{[key:string]:string} = {}

View File

@@ -1,4 +1,4 @@
import { risuChatParser } from "../parser"; import { risuChatParser, risuCommandParser } from "../parser";
import { DataBase, type Chat, type character } from "../storage/database"; import { DataBase, type Chat, type character } from "../storage/database";
import { tokenize } from "../tokenizer"; import { tokenize } from "../tokenizer";
import { getModuleTriggers } from "./modules"; import { getModuleTriggers } from "./modules";
@@ -23,7 +23,7 @@ export interface triggerscript{
export type triggerCondition = triggerConditionsVar|triggerConditionsExists|triggerConditionsChatIndex export type triggerCondition = triggerConditionsVar|triggerConditionsExists|triggerConditionsChatIndex
export type triggerEffect = triggerEffectCutChat|triggerEffectModifyChat|triggerEffectImgGen|triggerEffectRegex|triggerEffectRunLLM|triggerEffectCheckSimilarity|triggerEffectSendAIprompt|triggerEffectShowAlert|triggerEffectSetvar|triggerEffectSystemPrompt|triggerEffectImpersonate|triggerEffectCommand|triggerEffectStop|triggerEffectRunTrigger export type triggerEffect = triggerCode|triggerEffectCutChat|triggerEffectModifyChat|triggerEffectImgGen|triggerEffectRegex|triggerEffectRunLLM|triggerEffectCheckSimilarity|triggerEffectSendAIprompt|triggerEffectShowAlert|triggerEffectSetvar|triggerEffectSystemPrompt|triggerEffectImpersonate|triggerEffectCommand|triggerEffectStop|triggerEffectRunTrigger
export type triggerConditionsVar = { export type triggerConditionsVar = {
type:'var'|'value' type:'var'|'value'
@@ -32,6 +32,11 @@ export type triggerConditionsVar = {
operator:'='|'!='|'>'|'<'|'>='|'<='|'null'|'true' operator:'='|'!='|'>'|'<'|'>='|'<='|'null'|'true'
} }
export type triggerCode = {
type: 'triggercode',
code: string
}
export type triggerConditionsChatIndex = { export type triggerConditionsChatIndex = {
type:'chatindex' type:'chatindex'
value:string value:string
@@ -202,7 +207,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
continue continue
} }
} }
else if(mode !== trigger.type){ else if(mode !== trigger.type && trigger.effect[0]?.type !== 'triggercode'){
continue continue
} }
@@ -518,6 +523,20 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
setVar(effect.inputVar, res) setVar(effect.inputVar, res)
break break
} }
case 'triggercode':{
const triggerCodeResult = await risuCommandParser(effect.code,{
chara:char,
lowLevelAccess: trigger.lowLevelAccess,
funcName: mode
})
if(triggerCodeResult['__stop_chat__'] === '1'){
stopSending = true
}
break
}
} }
} }
} }