Skip to content

Commit

Permalink
fix: escape for prompt and uc, fix #219
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 3, 2024
1 parent 86d422f commit 4d4e222
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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(', ')]
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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))
})
})

0 comments on commit 4d4e222

Please sign in to comment.