@acedatacloud/koishi-plugin-midjourney 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/index.js +81 -0
- package/package.json +47 -4
- package/product.json +22 -0
- package/request.js +13 -0
- package/services.js +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ace Data Cloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Midjourney for Koishi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Generate a Midjourney image and retrieve the original task in a private Koishi chat. Requires existing Midjourney API access.
|
|
6
|
+
|
|
7
|
+
In Koishi Console, search for `@acedatacloud/koishi-plugin-midjourney` in the Plugin Market and enable it. In the plugin settings, set `token` to your Ace Data Cloud application token for Midjourney.
|
|
8
|
+
|
|
9
|
+
Use these commands in a private chat with operator authority (3 or higher).
|
|
10
|
+
|
|
11
|
+
- `ace-midjourney.make --confirm A paper boat under a glass dome` submits one paid generation. Check your application task history before retrying an unconfirmed submission.
|
|
12
|
+
- `ace-midjourney.task <task-id>` queries that same task and lists the available terminal results.
|
|
13
|
+
- `ace-midjourney.task <task-id> 1` sends the first result.
|
|
14
|
+
|
|
15
|
+
[Midjourney OpenAPI specification](https://platform.acedata.cloud/api/v1/services/midjourney/?lang=en)
|
package/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const { h, Schema } = require('koishi')
|
|
2
|
+
const { services, makeRequest, readTask } = require('./services')
|
|
3
|
+
const request = require('./request')
|
|
4
|
+
const product = require('./product.json')
|
|
5
|
+
|
|
6
|
+
services.set(product.key, product.service)
|
|
7
|
+
const terminal = new Set(['complete', 'completed', 'success', 'succeeded', 'succeed'])
|
|
8
|
+
const credentialHelp = '请在此插件的设置里填写应用令牌。该应用需已有 Midjourney API 访问权限。'
|
|
9
|
+
const applicationLink = 'https://platform.acedata.cloud/console/applications?utm_source=koishi&utm_medium=referral&utm_campaign=evergreen'
|
|
10
|
+
|
|
11
|
+
exports.name = 'acedatacloud-midjourney'
|
|
12
|
+
exports.inject = ['database']
|
|
13
|
+
exports.usage = `<img src="${product.icon}" alt="Midjourney" width="48" height="48">\n\n从 [Ace Data Cloud 应用](${applicationLink}) 复制已有 Midjourney API 访问权限的应用令牌,填入下方的密钥字段。`
|
|
14
|
+
exports.Config = Schema.object({
|
|
15
|
+
token: Schema.string().role('secret').required().description('Ace Data Cloud application token with existing Midjourney API access.'),
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
function accessDenied(error) {
|
|
19
|
+
return error?.message === 'HTTP 403' || error?.message === 'HTTP 401'
|
|
20
|
+
? `请核对应用令牌及 Midjourney API 访问权限(${error.message})。`
|
|
21
|
+
: undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.apply = (ctx, config) => {
|
|
25
|
+
const chat = ctx.private()
|
|
26
|
+
const token = () => config.token?.trim()
|
|
27
|
+
const generation = `ace-midjourney.make --confirm ${product.example}`
|
|
28
|
+
const retrieval = 'ace-midjourney.task'
|
|
29
|
+
|
|
30
|
+
chat.command('ace-midjourney', 'Midjourney', { authority: 3 })
|
|
31
|
+
.action(() => `生成(计费):${generation}\n查询:${retrieval} <任务 ID> [编号]`)
|
|
32
|
+
|
|
33
|
+
chat.command('ace-midjourney.make <prompt:text>', '提交一次 Midjourney 图片任务(计费)', { authority: 3 })
|
|
34
|
+
.option('confirmed', '-y, --confirm 确认提交一次计费请求')
|
|
35
|
+
.example(generation)
|
|
36
|
+
.action(async ({ options }, prompt) => {
|
|
37
|
+
if (!token()) return credentialHelp
|
|
38
|
+
if (!prompt?.trim()) return `用法:${generation}`
|
|
39
|
+
if (!options.confirmed) return `生成会计费。确认提交请使用:${generation}`
|
|
40
|
+
|
|
41
|
+
let result
|
|
42
|
+
try {
|
|
43
|
+
result = await request(product.service.submit, makeRequest(product.key, prompt), token())
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return accessDenied(error) || '提交未确认。请先核对应用里的任务记录,以免重复计费。'
|
|
46
|
+
}
|
|
47
|
+
if (result?.success === false || typeof result?.task_id !== 'string' || !result.task_id.trim()) {
|
|
48
|
+
return '服务没有返回可查询的任务 ID。请先核对应用里的任务记录。'
|
|
49
|
+
}
|
|
50
|
+
return h.text(`Midjourney 已接收。查询:${retrieval} ${result.task_id}`)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
chat.command(`${retrieval} <taskId:string> [index:posint]`, '查询原 Midjourney 任务并发送图片', { authority: 3 })
|
|
54
|
+
.example(`${retrieval} YOUR_TASK_ID 1`)
|
|
55
|
+
.action(async (_, taskId, index) => {
|
|
56
|
+
if (!token()) return credentialHelp
|
|
57
|
+
if (!taskId) return `用法:${retrieval} <任务 ID> [编号]`
|
|
58
|
+
let task
|
|
59
|
+
try {
|
|
60
|
+
task = await request(product.service.tasks, { action: 'retrieve', id: taskId }, token())
|
|
61
|
+
} catch (error) {
|
|
62
|
+
return accessDenied(error) || '查询失败。请使用相同任务 ID 再查。'
|
|
63
|
+
}
|
|
64
|
+
if (!task || !Object.keys(task).length) return '没有找到该任务。'
|
|
65
|
+
if (task.id !== taskId || (task.response?.task_id != null && task.response.task_id !== taskId)) {
|
|
66
|
+
return '响应中的任务 ID 与查询不符。'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const options = { requireTerminalState: true }
|
|
70
|
+
let result = readTask(product.key, task, options)
|
|
71
|
+
if (!result.assets.length && task.response?.raw_image_url) {
|
|
72
|
+
const response = { ...task.response, image_url: task.response.raw_image_url }
|
|
73
|
+
result = readTask(product.key, { ...task, response }, options)
|
|
74
|
+
}
|
|
75
|
+
if (!terminal.has(result.state) || !result.assets.length) return h.text(`状态:${result.state}。暂无可发送的媒体。`)
|
|
76
|
+
const [asset] = result.assets
|
|
77
|
+
if (index === undefined) return h.text(`状态:${result.state}\n1. ${asset.label}\n发送:${retrieval} ${taskId} <编号>`)
|
|
78
|
+
if (index !== 1) return '请选择编号 1。'
|
|
79
|
+
return [h.text(asset.label), h.image(asset.url)]
|
|
80
|
+
})
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acedatacloud/koishi-plugin-midjourney",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generate a Midjourney image and retrieve the original task in a private Koishi chat. Requires existing Midjourney API access.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"product.json",
|
|
9
|
+
"services.js",
|
|
10
|
+
"request.js",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Ace Data Cloud",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/AceDataCloud/KoishiPluginAceMusic.git",
|
|
19
|
+
"directory": "products"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/AceDataCloud/KoishiPluginAceMusic/tree/main/products",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"koishi",
|
|
24
|
+
"plugin",
|
|
25
|
+
"midjourney",
|
|
26
|
+
"image"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"koishi": "^4.18.0"
|
|
33
|
+
},
|
|
34
|
+
"koishi": {
|
|
35
|
+
"description": {
|
|
36
|
+
"en": "Generate a Midjourney image and retrieve the original task in a private Koishi chat. Requires existing Midjourney API access.",
|
|
37
|
+
"zh": "在 Koishi 私聊中生成 Midjourney 图片并查询原任务。需要已有 Midjourney API 访问权限的应用密钥。"
|
|
38
|
+
},
|
|
39
|
+
"service": {
|
|
40
|
+
"required": [
|
|
41
|
+
"database"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"registry": "https://registry.npmjs.org"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/product.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"key": "midjourney",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"title": "Midjourney",
|
|
5
|
+
"description": "Generate a Midjourney image and retrieve the original task in a private Koishi chat. Requires existing Midjourney API access.",
|
|
6
|
+
"descriptionZh": "在 Koishi 私聊中生成 Midjourney 图片并查询原任务。需要已有 Midjourney API 访问权限的应用密钥。",
|
|
7
|
+
"kind": "image",
|
|
8
|
+
"icon": "https://cdn.acedata.cloud/wto43b.png",
|
|
9
|
+
"docs": "https://platform.acedata.cloud/api/v1/services/midjourney/?lang=en",
|
|
10
|
+
"example": "A paper boat under a glass dome",
|
|
11
|
+
"service": {
|
|
12
|
+
"label": "Midjourney",
|
|
13
|
+
"submit": "/midjourney/imagine",
|
|
14
|
+
"tasks": "/midjourney/tasks",
|
|
15
|
+
"defaults": {
|
|
16
|
+
"action": "generate"
|
|
17
|
+
},
|
|
18
|
+
"kind": "image",
|
|
19
|
+
"url": "image_url",
|
|
20
|
+
"flatResult": true
|
|
21
|
+
}
|
|
22
|
+
}
|
package/request.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
async function request(path, body, token) {
|
|
2
|
+
const reply = await fetch('https://api.acedata.cloud' + path, {
|
|
3
|
+
method: 'POST',
|
|
4
|
+
redirect: 'error',
|
|
5
|
+
signal: AbortSignal.timeout(30000),
|
|
6
|
+
headers: { Authorization: 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
7
|
+
body: JSON.stringify(body),
|
|
8
|
+
})
|
|
9
|
+
if (!reply.ok) throw new Error('HTTP ' + reply.status)
|
|
10
|
+
return reply.json()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = request
|
package/services.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const services = new Map([
|
|
2
|
+
['suno', { label: 'Suno', submit: '/suno/audios', defaults: { action: 'generate' }, kind: 'audio', url: 'audio_url' }],
|
|
3
|
+
['acemusic', { label: 'ACE Music', submit: '/acemusic/audios', defaults: { action: 'generate' }, kind: 'audio', url: 'audio_url' }],
|
|
4
|
+
['nano-banana', { label: 'Nano Banana', submit: '/nano-banana/images', defaults: { action: 'generate' }, kind: 'image', url: 'image_url' }],
|
|
5
|
+
['gpt-image', { label: 'GPT Image', submit: '/openai/images/generations', tasks: '/openai/tasks', defaults: { model: 'gpt-image-2', response_format: 'url' }, kind: 'image', url: 'url' }],
|
|
6
|
+
['seedream', { label: 'Seedream', submit: '/seedream/images', defaults: { model: 'doubao-seedream-5-0-lite-260128', response_format: 'url' }, kind: 'image', url: 'image_url' }],
|
|
7
|
+
['flux', { label: 'FLUX', submit: '/flux/images', defaults: { action: 'generate', size: '1024x1024' }, kind: 'image', url: 'image_url' }],
|
|
8
|
+
['seedance', { label: 'Seedance', submit: '/seedance/videos', defaults: { model: 'doubao-seedance-1-5-pro-251215' }, kind: 'video', url: 'video_url' }],
|
|
9
|
+
['kling', { label: 'Kling', submit: '/kling/videos', defaults: { action: 'text2video' }, kind: 'video', url: 'video_url' }],
|
|
10
|
+
['veo', { label: 'Veo', submit: '/veo/videos', defaults: { action: 'text2video' }, kind: 'video', url: 'video_url' }],
|
|
11
|
+
['hailuo', { label: 'Hailuo', submit: '/hailuo/videos', defaults: { action: 'generate', model: 'minimax-t2v' }, kind: 'video', url: 'video_url' }],
|
|
12
|
+
])
|
|
13
|
+
|
|
14
|
+
function makeRequest(service, prompt, model) {
|
|
15
|
+
const request = { ...services.get(service).defaults, async: true }
|
|
16
|
+
if (model) request.model = model
|
|
17
|
+
if (service === 'seedance') request.content = [{ type: 'text', text: prompt }]
|
|
18
|
+
else request.prompt = prompt
|
|
19
|
+
return request
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function decodeImage(encoded) {
|
|
23
|
+
if (typeof encoded !== 'string' || !encoded.length) return
|
|
24
|
+
const buffer = Buffer.from(encoded, 'base64')
|
|
25
|
+
let mime
|
|
26
|
+
if (buffer.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex'))) mime = 'image/png'
|
|
27
|
+
else if (buffer.subarray(0, 3).equals(Buffer.from('ffd8ff', 'hex'))) mime = 'image/jpeg'
|
|
28
|
+
else if (buffer.toString('ascii', 0, 4) === 'RIFF' && buffer.toString('ascii', 8, 12) === 'WEBP') mime = 'image/webp'
|
|
29
|
+
if (mime) return { buffer, mime }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readTask(service, task, { requireTerminalState = false } = {}) {
|
|
33
|
+
const spec = services.get(service)
|
|
34
|
+
const result = task.response
|
|
35
|
+
if (requireTerminalState && (task.success === false || task.error || result?.success === false || result?.error)) {
|
|
36
|
+
return { state: 'failed', assets: [] }
|
|
37
|
+
}
|
|
38
|
+
let rows = Array.isArray(result?.data) ? result.data : []
|
|
39
|
+
if (service === 'kling' || spec.flatResult) rows = [result]
|
|
40
|
+
if (service === 'seedance') rows = [result?.data?.content]
|
|
41
|
+
const assets = []
|
|
42
|
+
const states = new Set()
|
|
43
|
+
for (const row of rows) {
|
|
44
|
+
if (!row) continue
|
|
45
|
+
const state = row.state || row.status
|
|
46
|
+
if (state) states.add(state)
|
|
47
|
+
let url
|
|
48
|
+
if (typeof row[spec.url] === 'string') {
|
|
49
|
+
try { url = new URL(row[spec.url]) } catch {}
|
|
50
|
+
}
|
|
51
|
+
const label = String(row.title || spec.label)
|
|
52
|
+
if (url?.protocol === 'https:' && !url.username && !url.password) {
|
|
53
|
+
assets.push({ kind: spec.kind, label, url: url.href })
|
|
54
|
+
} else if (service === 'gpt-image' || service === 'seedream') {
|
|
55
|
+
const image = decodeImage(row.b64_json)
|
|
56
|
+
if (image) assets.push({ kind: 'image', label, ...image })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
let state = result?.status || result?.state || result?.data?.status || task.status || task.state
|
|
60
|
+
if (!state && (result?.success === false || result?.error)) state = 'failed'
|
|
61
|
+
if (!state && states.size) state = states.size === 1 ? [...states][0] : 'mixed'
|
|
62
|
+
if (!state) {
|
|
63
|
+
const finished = task.finished_at || result?.finished_at || (!requireTerminalState && result?.success === true)
|
|
64
|
+
state = finished ? 'completed' : 'pending'
|
|
65
|
+
}
|
|
66
|
+
return { state, assets }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { services, makeRequest, readTask }
|