dsh-mc-server-ops 0.5.3 → 0.5.4
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/README.md +21 -0
- package/index.js +173 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
- **配方**:`kubejs` 脚本备份 / 重载
|
|
37
37
|
- **授权**:动受保护区域(world、ops\data、白名单)要 2 位群友投票;**凑满 2 票之前任何人反对就作废**
|
|
38
38
|
- **人设**:默认「小铜」,名字和语气都能在设置里改
|
|
39
|
+
- **游戏内 AI**:装上配套 Forge mod 后,玩家在游戏里 `/ai <内容>` 就能和「小铜」对话(只监听 `127.0.0.1`,按玩家 UUID 复用独立会话)
|
|
39
40
|
|
|
40
41
|
## 安装
|
|
41
42
|
|
|
@@ -106,6 +107,9 @@ dsh plugin add /path/to/dsh-mc-server-ops
|
|
|
106
107
|
| `bridge.coalesce.fastMs` | `50000` | 相邻两条发言间隔小于这个值算「话密」 |
|
|
107
108
|
| `bridge.maxPerDay` | `900` | 每群每天发送上限(官方上限 1000) |
|
|
108
109
|
| `bridge.appId` / `appSecret` | 空 | 留空自动从本机 qqbot 插件配置读取 |
|
|
110
|
+
| `ai.enabled` | `true` | 是否开游戏内 `/ai` 对话(需要配套 Forge mod;只监听 `127.0.0.1`) |
|
|
111
|
+
| `ai.port` | `8765` | mod 连接的本地端口 |
|
|
112
|
+
| `ai.timeoutSeconds` | `60` | 等 AI 回复的超时秒数 |
|
|
109
113
|
|
|
110
114
|
## 目录结构
|
|
111
115
|
|
|
@@ -238,6 +242,20 @@ powershell -File <服务器目录>\ops\mcadmin.ps1 -Action owner-add -Arg <openi
|
|
|
238
242
|
|
|
239
243
|
群 openid 不知道怎么办:官方没有「列出机器人加入的群」的稳定接口(`/v2/users/@me/groups` 直接 404),只能从一条真实的群消息里抓一次。详细过程写在 [docs/qq-bridge.md](https://github.com/mogutofu/dsh-mc-server-ops/blob/main/docs/qq-bridge.md)。
|
|
240
244
|
|
|
245
|
+
## 游戏内 AI 对话(/ai)
|
|
246
|
+
|
|
247
|
+
配套 Forge mod:[mogutofu/dsh-mc-ai-chat](https://github.com/mogutofu/dsh-mc-ai-chat)(服务端 Forge 1.20.1-47.x)。玩家在游戏里输入 `/ai 你好`,mod 会把请求 POST 到本机 `127.0.0.1:8765/mc-ai`,插件开一个 agent 回合,再把回复贴回玩家聊天栏。
|
|
248
|
+
|
|
249
|
+
| 项 | 说明 |
|
|
250
|
+
| --- | --- |
|
|
251
|
+
| 监听 | 只听 `127.0.0.1`,公网进不来;端口默认 8765 |
|
|
252
|
+
| 会话 | 每个玩家 UUID 一个会话 `mc-ai:<uuid>`,上下文连续;不是本机界面会话,按「群友」权限管 |
|
|
253
|
+
| 协议 | `POST /mc-ai`,请求 JSON `{"player":"名字","uuid":"UUID","text":"内容"}` → 回复 `{"ok":true,"reply":"…"}`;HTTP 200 也可能是 `ok:false`,以 `ok` 字段为准 |
|
|
254
|
+
| 配置 | `ai.enabled` / `ai.port` / `ai.timeoutSeconds`,改完立刻生效 |
|
|
255
|
+
| 排障 | 回复「(小铜没说话)」通常是 agent 没拿到可用的模型路由:检查 DSH 的默认模型(`agentDefaultModel`)是否指向有效 provider;`<服务器目录>\ops\data\mc-ai.log` 会记录每次请求和真实错误 |
|
|
256
|
+
|
|
257
|
+
游戏内玩家和群友一样受权限守卫限制:只读查询可以,写文件、执行命令需要服主授权;mod 侧默认每玩家 5 秒冷却,避免刷屏。
|
|
258
|
+
|
|
241
259
|
## 安全模型
|
|
242
260
|
|
|
243
261
|
- 仓库和 npm 包里**不含任何口令、QQ 号、记忆数据或整合包**。
|
|
@@ -323,6 +341,9 @@ Paper / Spigot / Purpur / Folia / Leaves、Forge / NeoForge / Fabric / Quilt、A
|
|
|
323
341
|
**群服互通会消耗 token 吗?**
|
|
324
342
|
不会。桥直接读日志、直接调 QQ 开放平台,不创建 agent 会话、不调模型。只有群友真的问问题时才走 AI。
|
|
325
343
|
|
|
344
|
+
**游戏内 /ai 需要什么?**
|
|
345
|
+
这个插件 + 配套的 Forge mod [dsh-mc-ai-chat](https://github.com/mogutofu/dsh-mc-ai-chat),服务端装 mod 后重启即可。`/ai` 会创建 agent 会话、调用 DSH 的模型,按模型计费;玩家和群友受同一套权限守卫,不能借它绕过服务器管理规则。
|
|
346
|
+
|
|
326
347
|
## License
|
|
327
348
|
|
|
328
349
|
MIT
|
package/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { createServer } from 'node:http'
|
|
2
4
|
import { appendFileSync, copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, lstatSync } from 'node:fs'
|
|
3
5
|
import { dirname, join } from 'node:path'
|
|
4
6
|
import { fileURLToPath } from 'node:url'
|
|
@@ -294,6 +296,13 @@ export const Config = Schema.object({
|
|
|
294
296
|
}).description('合并窗口自动调整:人少话稀就快发,人多话密就多攒一会儿'),
|
|
295
297
|
maxPerDay: Schema.number().default(900).description('每个群每天最多发几条(官方上限 1000,留余量)'),
|
|
296
298
|
}).description('群服互通'),
|
|
299
|
+
|
|
300
|
+
ai: Schema.object({
|
|
301
|
+
enabled: Schema.boolean().default(true)
|
|
302
|
+
.description('允许游戏内玩家用 /ai 和小铜对话(需要配套的 Forge mod;只监听 127.0.0.1)'),
|
|
303
|
+
port: Schema.number().default(8765).description('mod 连接的端口,默认 8765'),
|
|
304
|
+
timeoutSeconds: Schema.number().default(60).description('等待 AI 回复的超时时间(秒)'),
|
|
305
|
+
}).description('游戏内 AI 对话'),
|
|
297
306
|
})
|
|
298
307
|
|
|
299
308
|
// ─────────────────────── 检查更新 / 自更新 ───────────────────────
|
|
@@ -569,6 +578,7 @@ export function apply(ctx, config) {
|
|
|
569
578
|
},
|
|
570
579
|
maxPerDay: 900,
|
|
571
580
|
}, (config && config.bridge) || {}),
|
|
581
|
+
ai: Object.assign({ enabled: true, port: 8765, timeoutSeconds: 60 }, (config && config.ai) || {}),
|
|
572
582
|
}
|
|
573
583
|
const memoryConfig = {
|
|
574
584
|
get: () => liveConfig,
|
|
@@ -624,6 +634,169 @@ export function apply(ctx, config) {
|
|
|
624
634
|
})
|
|
625
635
|
}
|
|
626
636
|
|
|
637
|
+
// ── 游戏内 AI 对话:给 Forge mod 的本地 HTTP 入口 ──
|
|
638
|
+
// 玩家在游戏里 /ai 内容 → mod POST 到 127.0.0.1:<port>/mc-ai → 这里开一个 agent 回合
|
|
639
|
+
// → 拿回复回给 mod。会话 id 形如 mc-ai:<uuid>,不是本机会话,所以走「群友」权限。
|
|
640
|
+
const aiCfg = Object.assign({ enabled: true, port: 8765, timeoutSeconds: 60 }, (liveConfig && liveConfig.ai) || {})
|
|
641
|
+
if (aiCfg.enabled) {
|
|
642
|
+
const aiSessions = new Map()
|
|
643
|
+
const aiQueues = new Map()
|
|
644
|
+
const aiLog = (m) => {
|
|
645
|
+
try { appendFileSync(join(opsDir, 'data', 'mc-ai.log'), new Date().toISOString() + ' ' + m + '\n') } catch { }
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const askAgent = async (key, text) => {
|
|
649
|
+
const agents = ctx.get('agents')
|
|
650
|
+
if (!agents || typeof agents.create !== 'function') throw new Error('DSH 没有可用的 agents 服务')
|
|
651
|
+
let agent = aiSessions.get(key)
|
|
652
|
+
if (!agent || !agents.get(key)) {
|
|
653
|
+
// agents.create 不会自动套用部署默认模型;不传 agentOptions 时,第一轮
|
|
654
|
+
// prepareStep 会抛 "has no provider/model",turn/end 之后回复是空的。
|
|
655
|
+
// 所以从 dsh-agent-default-model 取一份 provider/model 路由传进去。
|
|
656
|
+
const defaultModel = ctx.get('agentDefaultModel')
|
|
657
|
+
const route = defaultModel && typeof defaultModel.currentSelection === 'function'
|
|
658
|
+
? defaultModel.currentSelection()
|
|
659
|
+
: undefined
|
|
660
|
+
let handle
|
|
661
|
+
try {
|
|
662
|
+
handle = await agents.create({ sessionId: key, agentOptions: route, meta: { cwd: serverDir } })
|
|
663
|
+
} catch (createError) {
|
|
664
|
+
// 玩家之前聊过、会话已经落盘(DSH 重启后内存里的 agent 不在了):
|
|
665
|
+
// create 会因 session id 已存在而失败,这时恢复旧会话继续聊。
|
|
666
|
+
try {
|
|
667
|
+
handle = await agents.resume({ resumeSessionId: key, agentOptions: route })
|
|
668
|
+
} catch {
|
|
669
|
+
throw createError
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
agent = handle.agent
|
|
673
|
+
aiSessions.set(key, agent)
|
|
674
|
+
// 恢复出来的会话可能记着一条旧的(甚至已经失效的)模型路由,AgentModel 会
|
|
675
|
+
// 优先用记录值。这里把当前默认路由显式写进选择,覆盖旧记录。
|
|
676
|
+
if (route && agent.model && typeof agent.model.select === 'function') {
|
|
677
|
+
try { agent.model.select(route) } catch (error) {
|
|
678
|
+
aiLog('[ai] ' + key + ' 设置模型路由失败:' + (error && error.message ? error.message : String(error)))
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
const timeoutMs = Math.max(5, Number(aiCfg.timeoutSeconds) || 60) * 1000
|
|
683
|
+
// 优先用 DSH 自己的构造函数,拿不到就手搓一个同形状的消息
|
|
684
|
+
let message = null
|
|
685
|
+
try {
|
|
686
|
+
const llm = await import('@deepseek-ai/dsh-llm')
|
|
687
|
+
if (llm && typeof llm.createUserMessage === 'function') {
|
|
688
|
+
message = llm.createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } })
|
|
689
|
+
}
|
|
690
|
+
} catch { /* 拿不到就退回手搓 */ }
|
|
691
|
+
if (!message) {
|
|
692
|
+
message = {
|
|
693
|
+
id: 'mcai-' + randomUUID(),
|
|
694
|
+
role: 'user',
|
|
695
|
+
content: [{ type: 'text', text }],
|
|
696
|
+
source: { kind: 'user' },
|
|
697
|
+
time: Date.now(),
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const parts = []
|
|
701
|
+
let endReason = null
|
|
702
|
+
const reply = await new Promise((resolve, reject) => {
|
|
703
|
+
let off = () => { }
|
|
704
|
+
const timer = setTimeout(() => {
|
|
705
|
+
try { off() } catch { }
|
|
706
|
+
try { agent.cancel({ kind: 'hook', reason: 'in-game timeout' }) } catch { }
|
|
707
|
+
reject(new Error('AI 回复超时'))
|
|
708
|
+
}, timeoutMs)
|
|
709
|
+
off = ctx.on('session/event', (session, event) => {
|
|
710
|
+
const id = session && session.header ? String(session.header.id ?? '') : ''
|
|
711
|
+
if (id !== key) return
|
|
712
|
+
if (event.type === 'assistant/message') {
|
|
713
|
+
const content = event.data && event.data.message ? event.data.message.content : null
|
|
714
|
+
for (const block of content || []) {
|
|
715
|
+
if (block && block.type === 'text' && typeof block.text === 'string') parts.push(block.text)
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
if (event.type === 'turn/end') {
|
|
719
|
+
endReason = event.data && event.data.reason ? event.data.reason : null
|
|
720
|
+
clearTimeout(timer)
|
|
721
|
+
try { off() } catch { }
|
|
722
|
+
resolve(parts.join('\n').trim())
|
|
723
|
+
}
|
|
724
|
+
}, { global: true })
|
|
725
|
+
try { agent.followup(message) } catch (error) {
|
|
726
|
+
clearTimeout(timer)
|
|
727
|
+
try { off() } catch { }
|
|
728
|
+
reject(error)
|
|
729
|
+
}
|
|
730
|
+
})
|
|
731
|
+
if (!reply && endReason && endReason.kind === 'error') {
|
|
732
|
+
// 不要把模型/鉴权错误吞成「小铜没说话」:真实原因记进日志,也让 mod 显示出来
|
|
733
|
+
const failure = endReason.error || {}
|
|
734
|
+
const detail = String(failure.message || failure.code || '未知错误')
|
|
735
|
+
aiLog('[ai] ' + key + ' 回合失败:' + detail)
|
|
736
|
+
throw new Error('AI 请求失败:' + detail)
|
|
737
|
+
}
|
|
738
|
+
return reply
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** 同一玩家串行,避免并发打断同一个会话 */
|
|
742
|
+
const enqueue = (key, task) => {
|
|
743
|
+
const prev = aiQueues.get(key) || Promise.resolve()
|
|
744
|
+
const next = prev.then(task, task)
|
|
745
|
+
aiQueues.set(key, next.catch(() => { }))
|
|
746
|
+
return next
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const aiServer = createServer((req, res) => {
|
|
750
|
+
const send = (code, obj) => {
|
|
751
|
+
try {
|
|
752
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8' })
|
|
753
|
+
res.end(JSON.stringify(obj))
|
|
754
|
+
} catch { }
|
|
755
|
+
}
|
|
756
|
+
const remote = String((req.socket && req.socket.remoteAddress) || '')
|
|
757
|
+
if (!/^(127\.0\.0\.1|::1|::ffff:127\.0\.0\.1)$/.test(remote)) {
|
|
758
|
+
return send(403, { ok: false, error: '只接受本机请求' })
|
|
759
|
+
}
|
|
760
|
+
if (req.method !== 'POST' || !String(req.url || '').startsWith('/mc-ai')) {
|
|
761
|
+
return send(404, { ok: false, error: 'not found' })
|
|
762
|
+
}
|
|
763
|
+
const chunks = []
|
|
764
|
+
req.on('data', (c) => chunks.push(Buffer.from(c)))
|
|
765
|
+
req.on('end', () => {
|
|
766
|
+
let body = null
|
|
767
|
+
try { body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}') } catch { body = null }
|
|
768
|
+
if (!body) return send(400, { ok: false, error: '请求体不是合法 JSON' })
|
|
769
|
+
const player = String(body.player || '').slice(0, 32)
|
|
770
|
+
const uuid = String(body.uuid || player).slice(0, 64)
|
|
771
|
+
const text = String(body.text || '').trim().slice(0, 1000)
|
|
772
|
+
if (!text) return send(400, { ok: false, error: '内容不能为空' })
|
|
773
|
+
const key = 'mc-ai:' + (uuid || player)
|
|
774
|
+
const startedAt = Date.now()
|
|
775
|
+
enqueue(key, async () => {
|
|
776
|
+
try {
|
|
777
|
+
const reply = await askAgent(key, text)
|
|
778
|
+
aiLog('[ai] ' + player + ' → ' + text.slice(0, 60) + ' ⇒ ' + String(reply || '').slice(0, 60))
|
|
779
|
+
send(200, { ok: true, reply: reply || '(小铜没说话)', elapsedMs: Date.now() - startedAt })
|
|
780
|
+
} catch (error) {
|
|
781
|
+
const message = error && error.message ? error.message : String(error)
|
|
782
|
+
aiLog('[ai] ' + player + ' 失败:' + message)
|
|
783
|
+
send(200, { ok: false, error: message })
|
|
784
|
+
}
|
|
785
|
+
})
|
|
786
|
+
})
|
|
787
|
+
req.on('error', () => { try { send(400, { ok: false, error: '请求读取失败' }) } catch { } })
|
|
788
|
+
})
|
|
789
|
+
|
|
790
|
+
ctx.effect(() => {
|
|
791
|
+
aiServer.on('error', (error) => aiLog('[ai] 监听失败:' + (error && error.message ? error.message : String(error))))
|
|
792
|
+
try { aiServer.listen(aiCfg.port, '127.0.0.1') } catch (error) {
|
|
793
|
+
aiLog('[ai] 启动失败:' + (error && error.message ? error.message : String(error)))
|
|
794
|
+
}
|
|
795
|
+
aiLog('[ai] 已监听 127.0.0.1:' + aiCfg.port)
|
|
796
|
+
return () => { try { aiServer.close() } catch { } }
|
|
797
|
+
})
|
|
798
|
+
}
|
|
799
|
+
|
|
627
800
|
// 依赖可能比本插件晚就绪,用 ctx.inject 等它们可用再挂路由。
|
|
628
801
|
if (typeof ctx.inject === 'function') {
|
|
629
802
|
ctx.inject(['settings', 'webServer'], (scope) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mc-server-ops",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "在 DeepSeek Harness 里托管 Minecraft 服务器的插件:状态/启停/日志/白名单/投票授权,静态命令拦截,本地/Pterodactyl 可插拔适配器,游戏聊天直通 QQ 群(不经过 AI、不耗 token
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"description": "在 DeepSeek Harness 里托管 Minecraft 服务器的插件:状态/启停/日志/白名单/投票授权,静态命令拦截,本地/Pterodactyl 可插拔适配器,游戏聊天直通 QQ 群(不经过 AI、不耗 token),把人设注入 QQ 会话,并让游戏内玩家用 /ai 和「小铜」对话(配套 Forge mod)。需要先安装 QQ 机器人插件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"exports": {
|