fix: escape for prompt and uc, fix #219

This commit is contained in:
Shigma 2024-02-04 00:25:37 +08:00
parent 86d422f9fe
commit 4d4e222d20
No known key found for this signature in database
GPG Key ID: 414458B84ACF8744
4 changed files with 22 additions and 8 deletions

View File

@ -1,7 +1,7 @@
{
"name": "koishi-plugin-novelai",
"description": "Generate images by diffusion models",
"version": "1.20.0",
"version": "1.20.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [

View File

@ -387,7 +387,7 @@ export function parseForbidden(input: string) {
const backslash = /@@__BACKSLASH__@@/g
export function parseInput(session: Session, input: string, config: Config, override: boolean, addDefault: boolean): string[] {
export function parseInput(session: Session, input: string, config: Config, override: boolean): string[] {
if (!input) {
return [
null,
@ -401,6 +401,8 @@ export function parseInput(session: Session, input: string, config: Config, over
.replace(//g, ',')
.replace(//g, '(')
.replace(//g, ')')
.replace(/《/g, '<')
.replace(/》/g, '>')
if (config.type === 'sd-webui') {
input = input
@ -470,7 +472,7 @@ export function parseInput(session: Session, input: string, config: Config, over
if (!override) {
appendToList(positive, session.resolve(config.basePrompt))
appendToList(negative, session.resolve(config.negativePrompt))
if (addDefault) appendToList(positive, session.resolve(config.defaultPrompt))
if (config.defaultPromptSw) appendToList(positive, session.resolve(config.defaultPrompt))
}
return [null, positive.join(', '), negative.join(', ')]

View File

@ -188,9 +188,7 @@ export function apply(ctx: Context, config: Config) {
}
}
const [errPath, prompt, uc] = parseInput(
session, input, config, options.override, config.defaultPromptSw,
)
const [errPath, prompt, uc] = parseInput(session, input, config, options.override)
if (errPath) return session.text(errPath)
let token: string
@ -486,9 +484,9 @@ export function apply(ctx: Context, config: Config) {
}
}
result.children.push(h('message', attrs, lines.join('\n')))
result.children.push(h('message', attrs, `prompt = ${finalPrompt}`))
result.children.push(h('message', attrs, `prompt = ${h.escape(finalPrompt)}`))
if (output === 'verbose') {
result.children.push(h('message', attrs, `undesired = ${uc}`))
result.children.push(h('message', attrs, `undesired = ${h.escape(uc)}`))
}
result.children.push(h('message', attrs, h.image(dataUrl)))
return result

14
tests/index.spec.ts Normal file
View File

@ -0,0 +1,14 @@
import { describe, test } from 'node:test'
import * as novelai from '../src'
import { Context } from 'koishi'
import mock from '@koishijs/plugin-mock'
describe('koishi-plugin-novelai', () => {
test('parse input', () => {
const ctx = new Context()
ctx.plugin(mock)
const session = ctx.bots[0].session({})
const fork = ctx.plugin(novelai)
console.log(novelai.parseInput(session, '<lora:skr2:1>,1girl', fork.config, false))
})
})