Skip to content

Commit

Permalink
feat: support config maxSteps and maxResolution
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 30, 2022
1 parent 4d9b49d commit 891b315
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
36 changes: 25 additions & 11 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ API 服务器地址。如果你搭建了私服,可以将此项设置为你的

要附加的额外请求头。如果你的 `endpoint` 是第三方服务器,你可能需要设置正确的请求头,否则请求可能会被拒绝。

## 功能设置
## 参数设置

### model

Expand All @@ -78,19 +78,19 @@ API 服务器地址。如果你搭建了私服,可以将此项设置为你的

默认的采样器。

### output
### maxSteps

- 类型:`'minimal' | 'default' | 'verbose'`
- 默认值:`'default'`
- 类型:`number`

输出方式。`minimal` 表示只发送图片,`default` 表示发送图片和关键信息,`verbose` 表示发送全部信息
选项 `--steps` 的最大值

### allowAnlas
### maxResolution

- 类型:`boolean`
- 默认值:`true`
- 类型:`number`

是否允许使用点数。禁用后部分功能 (如图片增强和步数设置) 将无法使用。
选项 `--resolution` 中边长的最大值。

## 输入设置

### basePrompt

Expand Down Expand Up @@ -120,19 +120,33 @@ API 服务器地址。如果你搭建了私服,可以将此项设置为你的

## 高级设置

### output

- 类型:`'minimal' | 'default' | 'verbose'`
- 默认值:`'default'`

输出方式。`minimal` 表示只发送图片,`default` 表示发送图片和关键信息,`verbose` 表示发送全部信息。

### allowAnlas

- 类型:`boolean`
- 默认值:`true`

是否允许使用点数。禁用后部分功能 (如图片增强和步数设置) 将无法使用。

### requestTimeout

- 类型:`number`
- 默认值:`30000`

当请求超过这个时间时会中止并提示超时。

### recallTimeout
<!-- ### recallTimeout
- 类型:`number`
- 默认值:`0`
图片发送后自动撤回的时间 (设置为 `0` 禁用此功能)。
图片发送后自动撤回的时间 (设置为 `0` 禁用此功能)。 -->

### maxConcurrency

Expand Down
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 NovelAI",
"version": "1.12.3",
"version": "1.12.4",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
30 changes: 20 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export interface Options {

export interface Config {
type: 'token' | 'login' | 'naifu' | 'sd-webui'
token: string
email: string
password: string
token?: string
email?: string
password?: string
model?: Model
orient?: Orient
sampler?: string
maxSteps?: number
maxResolution?: number
anatomy?: boolean
output?: 'minimal' | 'default' | 'verbose'
allowAnlas?: boolean | number
Expand Down Expand Up @@ -150,32 +152,40 @@ export const Config = Schema.intersect([
Schema.object({
type: Schema.const('sd-webui'),
sampler: sampler.createSchema(sampler.sd),
}).description('功能设置'),
}).description('参数设置'),
Schema.object({
type: Schema.const('naifu'),
sampler: sampler.createSchema(sampler.nai),
}).description('功能设置'),
}).description('参数设置'),
Schema.object({
model: Schema.union(models).description('默认的生成模型。').default('nai'),
sampler: sampler.createSchema(sampler.nai),
}).description('功能设置'),
}).description('参数设置'),
] as const),

Schema.object({
orient: Schema.union(orients).description('默认的图片方向。').default('portrait'),
maxSteps: Schema.natural().description('允许的最大迭代步数。').default(0),
maxResolution: Schema.natural().description('生成图片的最大尺寸。').default(0),
}),

Schema.object({
basePrompt: Schema.string().role('textarea').description('默认附加的标签。').default('masterpiece, best quality'),
negativePrompt: Schema.string().role('textarea').description('默认附加的反向标签。').default(ucPreset),
forbidden: Schema.string().role('textarea').description('违禁词列表。含有违禁词的请求将被拒绝。').default(''),
}).description('输入设置'),

Schema.object({
output: Schema.union([
Schema.const('minimal').description('只发送图片'),
Schema.const('default').description('发送图片和关键信息'),
Schema.const('verbose').description('发送全部信息'),
]).description('输出方式。').default('default'),
basePrompt: Schema.string().role('textarea').description('默认附加的标签。').default('masterpiece, best quality'),
negativePrompt: Schema.string().role('textarea').description('默认附加的反向标签。').default(ucPreset),
forbidden: Schema.string().role('textarea').description('违禁词列表。含有违禁词的请求将被拒绝。').default(''),
maxRetryCount: Schema.natural().description('连接失败时最大的重试次数。').default(3),
requestTimeout: Schema.number().role('time').description('当请求超过这个时间时会中止并提示超时。').default(Time.minute),
recallTimeout: Schema.number().role('time').description('图片发送后自动撤回的时间 (设置为 0 以禁用此功能)。').default(0),
maxConcurrency: Schema.number().description('单个频道下的最大并发数量 (设置为 0 以禁用此功能)。').default(0),
}),
}).description('高级设置'),
]) as Schema<Config>

interface Forbidden {
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,22 @@ export function apply(ctx: Context, config: Config) {
}
}

const step = (source: string) => {
const value = +source
if (value * 0 === 0 && Math.floor(value) === value && value > 0 && value <= (config.maxSteps || Infinity)) return value
throw new Error()
}

const resolution = (source: string, session: Session<'authority'>): Size => {
if (source in orientMap) return orientMap[source]
if (restricted(session)) throw new Error()
const cap = source.match(/^(\d+)[x×](\d+)$/)
if (!cap) throw new Error()
const width = closestMultiple(+cap[1])
const height = closestMultiple(+cap[2])
if (Math.max(width, height) > (config.maxResolution || Infinity)) {
throw new Error()
}
return { width, height }
}

Expand All @@ -82,11 +91,11 @@ export function apply(ctx: Context, config: Config) {
.shortcut('增強', { fuzzy: true, options: { enhance: true } })
.option('enhance', '-e', { hidden: restricted })
.option('model', '-m <model>', { type: models, hidden: thirdParty })
.option('resolution', '-o, -r <resolution>', { type: resolution })
.option('resolution', '-r <resolution>', { type: resolution })
.option('override', '-O')
.option('sampler', '-s <sampler>')
.option('seed', '-x <seed:number>')
.option('steps', '-t <step:number>', { hidden: restricted })
.option('steps', '-t <step>', { type: step, hidden: restricted })
.option('scale', '-c <scale:number>')
.option('noise', '-n <noise:number>', { hidden: restricted })
.option('strength', '-N <strength:number>', { hidden: restricted })
Expand Down

0 comments on commit 891b315

Please sign in to comment.