按钮改成右下角悬浮胶囊(shell.overlay),新增 /server 斜杠命令
由 dsh-go 自动上传 · 2026/11/18 01:35:56
This commit is contained in:
1 parent
e22647cb47
commit
2964c92d83
3 files changed
+112
-11
No files matched your search
@@ -20,9 +20,10 @@ window.__ModuleLoader__.load({
|
||||
|
||||
const s = {
|
||||
trigger: {
|
||||
display: 'inline-flex', alignItems: 'center', gap: 6, height: 30, padding: '0 10px',
|
||||
borderRadius: 8, border: `1px solid ${T.line}`, background: T.soft, color: 'inherit',
|
||||
font: 'inherit', fontSize: 13, cursor: 'pointer', whiteSpace: 'nowrap'
|
||||
display: 'inline-flex', alignItems: 'center', gap: 6, height: 26, padding: '0 11px',
|
||||
borderRadius: 999, border: '1px solid rgba(91,140,255,.45)', background: 'rgba(91,140,255,.16)',
|
||||
color: 'inherit', font: 'inherit', fontSize: 12.5, fontWeight: 550, cursor: 'pointer',
|
||||
whiteSpace: 'nowrap', letterSpacing: '.01em'
|
||||
},
|
||||
mask: {
|
||||
position: 'fixed', inset: 0, zIndex: 9000, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
@@ -338,9 +339,24 @@ window.__ModuleLoader__.load({
|
||||
function Trigger() {
|
||||
const [open, setOpen] = React.useState(false)
|
||||
return h(React.Fragment, null,
|
||||
h('button', { style: s.trigger, onClick: () => setOpen(true), title: '连接服务器' },
|
||||
h('span', { style: { width: 7, height: 7, borderRadius: 4, background: GOOD } }),
|
||||
'服务器'
|
||||
h('div', {
|
||||
style: { position: 'fixed', right: 14, bottom: 92, zIndex: 8000, pointerEvents: 'auto' }
|
||||
},
|
||||
h('button', {
|
||||
style: {
|
||||
display: 'inline-flex', alignItems: 'center', gap: 7, height: 34, padding: '0 14px',
|
||||
borderRadius: 999, border: '1px solid rgba(91,140,255,.5)',
|
||||
background: 'linear-gradient(180deg, rgba(91,140,255,.30), rgba(91,140,255,.18))',
|
||||
color: '#eef3ff', fontSize: 13, fontWeight: 600, cursor: 'pointer',
|
||||
boxShadow: '0 8px 24px rgba(0,0,0,.35), inset 0 1px 0 rgba(255,255,255,.12)',
|
||||
backdropFilter: 'blur(8px)'
|
||||
},
|
||||
onClick: () => setOpen(true),
|
||||
title: '连接服务器(也可以直接输入 /server)'
|
||||
},
|
||||
h('span', { style: { width: 7, height: 7, borderRadius: 4, background: GOOD, boxShadow: '0 0 0 3px rgba(56,211,159,.20)' } }),
|
||||
'服务器'
|
||||
)
|
||||
),
|
||||
open && h(Panel, { onClose: () => setOpen(false) })
|
||||
)
|
||||
@@ -349,10 +365,10 @@ window.__ModuleLoader__.load({
|
||||
return {
|
||||
inject: ['slots'],
|
||||
apply(ctx) {
|
||||
ctx.slots.inject('conversation.input.left', () => ctx.slots.register({
|
||||
name: 'conversation.input.left',
|
||||
id: 'dsh-server-manager-button',
|
||||
order: 20
|
||||
ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
||||
name: 'shell.overlay',
|
||||
id: 'dsh-server-manager-fab',
|
||||
order: 40
|
||||
}, Trigger))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export const name = 'dsh-server-manager'
|
||||
|
||||
export const inject = ['tools', 'webServer']
|
||||
export const inject = ['tools', 'webServer', 'commands']
|
||||
|
||||
// 本地 bundle 解析不到 @deepseek-ai/*,直接用主机上的真实文件路径拿 defineTool。
|
||||
const TOOLS_ENTRY = '/opt/dsh/node_modules/@deepseek-ai/dsh-tools/lib/index.js'
|
||||
@@ -40,6 +40,65 @@ export async function apply(ctx) {
|
||||
|
||||
registerRoutes(ctx, { ensureSeed, saveConfig, resolveServer, describeServer, execRemote, json, guard, readJson, OVERVIEW_SCRIPT, parseOverview, humanBytes })
|
||||
|
||||
const serverCommand = async (inv) => {
|
||||
const raw = String(inv?.rawInput ?? inv?.input ?? '').trim()
|
||||
const parts = raw.split(/\s+/).filter(Boolean)
|
||||
const verb = (parts[0] || 'status').toLowerCase()
|
||||
try {
|
||||
if (verb === 'ls' || verb === 'list') {
|
||||
const { cfg } = listServers()
|
||||
if (!cfg.servers.length) return { kind: 'success', text: '还没有服务器,发 /server add <地址> <用户名> 加一台。' }
|
||||
return {
|
||||
kind: 'success',
|
||||
text: cfg.servers.map((v) => `${v.id === cfg.default ? '★ ' : '· '}${v.id} — ${v.label || ''} · ${v.user}@${v.host}:${v.port || 22}`).join('\n')
|
||||
}
|
||||
}
|
||||
if (verb === 'add') {
|
||||
const [, host, user, port, key] = parts
|
||||
if (!host || !user) return { kind: 'error', text: '用法:/server add 1.2.3.4 root [端口] [私钥路径]' }
|
||||
const { addServer } = await import('./lib/config.js')
|
||||
const entry = addServer({ host, user, port: port ? Number(port) : 22, key })
|
||||
const probe = await execRemote(entry, 'hostname; (. /etc/os-release 2>/dev/null && echo "$PRETTY_NAME")')
|
||||
if (!probe.ok) {
|
||||
return { kind: 'error', text: `已写进配置(id: ${entry.id}),但试连失败:${(probe.stderr || probe.stdout || '').trim().split('\n')[0]}\n多半是私钥没装到那台机器的 authorized_keys。` }
|
||||
}
|
||||
return { kind: 'success', text: `加好了:${entry.id}(${entry.user}@${entry.host}:${entry.port})\n${probe.stdout.trim().split('\n').join(' · ')}` }
|
||||
}
|
||||
// status
|
||||
const { cfg } = listServers()
|
||||
const resolved = resolveServer(cfg, parts[1])
|
||||
if (resolved.error) return { kind: 'error', text: resolved.error }
|
||||
const row = resolved.server
|
||||
const r = await execRemote(row, OVERVIEW_SCRIPT)
|
||||
if (!r.ok) return { kind: 'error', text: `连不上 ${row.label || row.id}:${(r.stderr || '').trim().split('\n').slice(-1)[0] || '未知错误'}` }
|
||||
const o = parseOverview(r.stdout)
|
||||
return {
|
||||
kind: 'success',
|
||||
text: [
|
||||
`${o.host} · ${o.os}`,
|
||||
`开机 ${o.uptime} · ${o.cores} 核 · 负载 ${o.load}`,
|
||||
`内存 ${humanBytes(o.mem.used)} / ${humanBytes(o.mem.total)}(${o.mem.pct}%)`,
|
||||
`磁盘 ${humanBytes(o.disk.used)} / ${humanBytes(o.disk.total)}(${o.disk.pct}%)`,
|
||||
Object.entries(o.services).map(([k, v]) => `${k}:${v === 'active' ? '运行中' : '未运行'}`).join(' '),
|
||||
o.giteaVersion ? `Gitea ${o.giteaVersion}` : ''
|
||||
].filter(Boolean).join('\n')
|
||||
}
|
||||
} catch (error) {
|
||||
return { kind: 'error', text: String(error?.message || error) }
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.commands?.register) {
|
||||
ctx.commands.register({
|
||||
definitionId: '@local/dsh-server-manager',
|
||||
name: 'server',
|
||||
description: '看服务器状态、加服务器(/server status | ls | add <地址> <用户名>)',
|
||||
input: { hint: 'status | ls | add <地址> <用户名> [端口] [私钥]' },
|
||||
recordInput: false,
|
||||
handler: serverCommand
|
||||
})
|
||||
}
|
||||
|
||||
ctx.tools.register(
|
||||
defineTool({
|
||||
name: 'server_add',
|
||||
|
||||
@@ -70,6 +70,32 @@ export function resolveServer(cfg, wanted) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 新增一台服务器:补全 id/label/port/key,id 冲突自动加序号;返回写入的条目。 */
|
||||
export function addServer(input) {
|
||||
const cfg = ensureSeed()
|
||||
const host = String(input?.host || '').trim()
|
||||
if (!host) throw new Error('缺少服务器地址(host)')
|
||||
const user = String(input?.user || '').trim() || 'root'
|
||||
const port = Number(input?.port) > 0 ? Number(input.port) : 22
|
||||
const base = String(input?.id || '').trim() ||
|
||||
host.replace(/\./g, '-').replace(/[^A-Za-z0-9_-]/g, '').slice(0, 24) || 'server'
|
||||
let id = base
|
||||
let n = 2
|
||||
while (cfg.servers.some((s) => s.id === id)) id = `${base}${n++}`
|
||||
const entry = {
|
||||
id,
|
||||
label: String(input?.label || '').trim() || id,
|
||||
host,
|
||||
port,
|
||||
user,
|
||||
key: String(input?.key || '').trim()
|
||||
}
|
||||
cfg.servers.push(entry)
|
||||
if (!cfg.default) cfg.default = entry.id
|
||||
saveConfig({ default: cfg.default, servers: cfg.servers })
|
||||
return entry
|
||||
}
|
||||
|
||||
export function describeServer(s) {
|
||||
return `${s.label} · ${s.user}@${s.host}:${s.port}`
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user