🎨 添加文档 && 新增正则脚本模块

Signed-off-by: Echo <1711788888@qq.com>
This commit is contained in:
2026-02-28 15:11:12 +08:00
parent a6234e7bb0
commit 3bfa59cf3e
17 changed files with 26980 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
package app
import (
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
// RegexScript 正则脚本模型(完全兼容 SillyTavern 格式)
type RegexScript struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
UserID uint `gorm:"index;not null" json:"userId"`
Name string `gorm:"type:varchar(100);not null" json:"name"` // 脚本名称
// 正则表达式
FindRegex string `gorm:"type:text;not null" json:"findRegex"` // 查找的正则表达式
ReplaceWith string `gorm:"type:text" json:"replaceWith"` // 替换的内容
TrimStrings datatypes.JSON `gorm:"type:jsonb" json:"trimStrings"` // 要修剪的字符串列表 []string
// 执行阶段
// 0=输入(input), 1=输出(output), 2=世界书(world_info), 3=推理(display)
Placement int `gorm:"default:1" json:"placement"`
// 执行选项
Disabled bool `gorm:"default:false" json:"disabled"` // 是否禁用
MarkdownOnly bool `gorm:"default:false" json:"markdownOnly"` // 仅在 Markdown 模式下执行
RunOnEdit bool `gorm:"default:false" json:"runOnEdit"` // 编辑消息时执行
PromptOnly bool `gorm:"default:false" json:"promptOnly"` // 仅在 prompt 中执行
// 宏替换
SubstituteRegex bool `gorm:"default:true" json:"substituteRegex"` // 是否替换宏变量 {{user}}/{{char}}
// 深度控制
MinDepth *int `gorm:"type:int" json:"minDepth"` // 最小深度null 表示不限制)
MaxDepth *int `gorm:"type:int" json:"maxDepth"` // 最大深度null 表示不限制)
// 作用域
// 0=全局(global), 1=角色(character), 2=预设(preset)
Scope int `gorm:"default:0" json:"scope"`
OwnerCharID *uint `gorm:"type:int" json:"ownerCharId"` // 角色IDscope=1时有效
OwnerPresetID *uint `gorm:"type:int" json:"ownerPresetId"` // 预设IDscope=2时有效
// 执行顺序
Order int `gorm:"default:100" json:"order"` // 执行顺序,数字越小越先执行
// 扩展字段
Extensions datatypes.JSON `gorm:"type:jsonb" json:"extensions"`
}
func (RegexScript) TableName() string {
return "regex_scripts"
}

View File

@@ -0,0 +1,54 @@
package request
// CreateRegexScriptRequest 创建正则脚本请求
type CreateRegexScriptRequest struct {
Name string `json:"name" binding:"required,max=100"`
FindRegex string `json:"findRegex" binding:"required"`
ReplaceWith string `json:"replaceWith"`
TrimStrings []string `json:"trimStrings"`
Placement int `json:"placement"`
Disabled bool `json:"disabled"`
MarkdownOnly bool `json:"markdownOnly"`
RunOnEdit bool `json:"runOnEdit"`
PromptOnly bool `json:"promptOnly"`
SubstituteRegex bool `json:"substituteRegex"`
MinDepth *int `json:"minDepth"`
MaxDepth *int `json:"maxDepth"`
Scope int `json:"scope"`
OwnerCharID *uint `json:"ownerCharId"`
OwnerPresetID *uint `json:"ownerPresetId"`
Order int `json:"order"`
}
// UpdateRegexScriptRequest 更新正则脚本请求
type UpdateRegexScriptRequest struct {
Name *string `json:"name" binding:"omitempty,max=100"`
FindRegex *string `json:"findRegex"`
ReplaceWith *string `json:"replaceWith"`
TrimStrings []string `json:"trimStrings"`
Placement *int `json:"placement"`
Disabled *bool `json:"disabled"`
MarkdownOnly *bool `json:"markdownOnly"`
RunOnEdit *bool `json:"runOnEdit"`
PromptOnly *bool `json:"promptOnly"`
SubstituteRegex *bool `json:"substituteRegex"`
MinDepth *int `json:"minDepth"`
MaxDepth *int `json:"maxDepth"`
Scope *int `json:"scope"`
OwnerCharID *uint `json:"ownerCharId"`
OwnerPresetID *uint `json:"ownerPresetId"`
Order *int `json:"order"`
}
// GetRegexScriptListRequest 获取正则脚本列表请求
type GetRegexScriptListRequest struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
Keyword string `json:"keyword"`
Scope *int `json:"scope"`
}
// TestRegexScriptRequest 测试正则脚本请求
type TestRegexScriptRequest struct {
TestString string `json:"testString" binding:"required"`
}

View File

@@ -0,0 +1,78 @@
package response
import (
"encoding/json"
"time"
"git.echol.cn/loser/st/server/model/app"
)
// RegexScriptResponse 正则脚本响应
type RegexScriptResponse struct {
ID uint `json:"id"`
UserID uint `json:"userId"`
Name string `json:"name"`
FindRegex string `json:"findRegex"`
ReplaceWith string `json:"replaceWith"`
TrimStrings []string `json:"trimStrings"`
Placement int `json:"placement"`
Disabled bool `json:"disabled"`
MarkdownOnly bool `json:"markdownOnly"`
RunOnEdit bool `json:"runOnEdit"`
PromptOnly bool `json:"promptOnly"`
SubstituteRegex bool `json:"substituteRegex"`
MinDepth *int `json:"minDepth"`
MaxDepth *int `json:"maxDepth"`
Scope int `json:"scope"`
OwnerCharID *uint `json:"ownerCharId"`
OwnerPresetID *uint `json:"ownerPresetId"`
Order int `json:"order"`
Extensions map[string]interface{} `json:"extensions"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// TestRegexScriptResponse 测试正则脚本响应
type TestRegexScriptResponse struct {
Original string `json:"original"`
Result string `json:"result"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
// ToRegexScriptResponse 转换为正则脚本响应结构
func ToRegexScriptResponse(script *app.RegexScript) RegexScriptResponse {
var trimStrings []string
if len(script.TrimStrings) > 0 {
json.Unmarshal(script.TrimStrings, &trimStrings)
}
var extensions map[string]interface{}
if len(script.Extensions) > 0 {
json.Unmarshal(script.Extensions, &extensions)
}
return RegexScriptResponse{
ID: script.ID,
UserID: script.UserID,
Name: script.Name,
FindRegex: script.FindRegex,
ReplaceWith: script.ReplaceWith,
TrimStrings: trimStrings,
Placement: script.Placement,
Disabled: script.Disabled,
MarkdownOnly: script.MarkdownOnly,
RunOnEdit: script.RunOnEdit,
PromptOnly: script.PromptOnly,
SubstituteRegex: script.SubstituteRegex,
MinDepth: script.MinDepth,
MaxDepth: script.MaxDepth,
Scope: script.Scope,
OwnerCharID: script.OwnerCharID,
OwnerPresetID: script.OwnerPresetID,
Order: script.Order,
Extensions: extensions,
CreatedAt: script.CreatedAt,
UpdatedAt: script.UpdatedAt,
}
}