dsh-lovart-mcp 0.12.2 → 0.12.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.
Binary file
package/assets/icon.png CHANGED
Binary file
package/client.js CHANGED
@@ -153,6 +153,46 @@ window.__ModuleLoader__.load({
153
153
  setBusy('')
154
154
  }
155
155
  }
156
+ const addAccount = async (id, label) => {
157
+ setBusy('add')
158
+ try {
159
+ const response = await fetch(`${API}/accounts/add`, {
160
+ method: 'POST',
161
+ headers: { 'content-type': 'application/json' },
162
+ body: JSON.stringify({ account: id, label }),
163
+ })
164
+ const body = await response.json().catch(() => ({}))
165
+ if (!response.ok) {
166
+ fail(body.error || `add failed (${response.status})`)
167
+ return false
168
+ }
169
+ setAction({ kind: 'add', id: body.id, label: body.label, saved: body.saved })
170
+ await load()
171
+ return true
172
+ } catch (error) {
173
+ fail(String((error && error.message) || error))
174
+ return false
175
+ } finally {
176
+ setBusy('')
177
+ }
178
+ }
179
+ const removeAccount = async (id) => {
180
+ setBusy(`remove:${id}`)
181
+ try {
182
+ const response = await fetch(`${API}/accounts/remove?account=${encodeURIComponent(id)}`, { method: 'POST' })
183
+ const body = await response.json().catch(() => ({}))
184
+ if (!response.ok) {
185
+ fail(body.error || `remove failed (${response.status})`)
186
+ return
187
+ }
188
+ setAction({ kind: 'remove', id: body.id, saved: body.saved })
189
+ await load()
190
+ } catch (error) {
191
+ fail(String((error && error.message) || error))
192
+ } finally {
193
+ setBusy('')
194
+ }
195
+ }
156
196
  const saveProxy = async (value) => {
157
197
  setBusy('proxy')
158
198
  try {
@@ -174,10 +214,10 @@ window.__ModuleLoader__.load({
174
214
  setBusy('')
175
215
  }
176
216
  }
177
- return { data, busy, action, load, login, exportAccount, importBundle, exportState, importStateText, saveProxy }
217
+ return { data, busy, action, load, login, addAccount, removeAccount, exportAccount, importBundle, exportState, importStateText, saveProxy }
178
218
  }
179
219
 
180
- function AccountCard({ account, busy, onLogin, onExport }) {
220
+ function AccountCard({ account, busy, onLogin, onExport, onRemove }) {
181
221
  const days = typeof account.expiresInSeconds === 'number' ? Math.round(account.expiresInSeconds / 86400) : undefined
182
222
  return h('div', { style: cardStyle },
183
223
  h('div', { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px' } },
@@ -202,7 +242,75 @@ window.__ModuleLoader__.load({
202
242
  h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '8px', marginTop: '2px' } },
203
243
  button(busy === account.id ? '正在打开…' : account.authorized ? '重新登录' : '登录', () => onLogin(account.id), !account.authorized),
204
244
  account.authorized ? button(busy === `export:${account.id}` ? '导出中…' : '导出登录', () => onExport(account.id)) : null,
245
+ busy === `remove:${account.id}` ? note('移除中…') : button('移除', () => {
246
+ const ok = typeof window !== 'undefined' && typeof window.confirm === 'function'
247
+ ? window.confirm(`移除账号 ${account.id}?会清除本机令牌并从插件配置里删除。`)
248
+ : false
249
+ if (ok) onRemove(account.id)
250
+ }),
251
+ ),
252
+ )
253
+ }
254
+
255
+ function AddAccountBox({ onAdd, busy }) {
256
+ const idRef = React.useRef(null)
257
+ const labelRef = React.useRef(null)
258
+ const [message, setMessage] = React.useState('')
259
+ const submit = async () => {
260
+ const id = idRef.current ? String(idRef.current.value || '').trim() : ''
261
+ if (!id) {
262
+ setMessage('先填一个账号 id,例如 c。')
263
+ return
264
+ }
265
+ setMessage('添加中…')
266
+ const label = labelRef.current ? String(labelRef.current.value || '').trim() : ''
267
+ const ok = await onAdd(id, label)
268
+ if (ok && idRef.current) idRef.current.value = ''
269
+ if (ok && labelRef.current) labelRef.current.value = ''
270
+ setMessage(ok ? '' : '添加失败,看上面的提示。')
271
+ }
272
+ return h('div', {
273
+ style: {
274
+ border: '1px dashed var(--dsw-alias-border-subtle, rgba(127,127,127,.3))',
275
+ borderRadius: '10px',
276
+ padding: '10px 12px',
277
+ display: 'flex',
278
+ flexDirection: 'column',
279
+ gap: '8px',
280
+ },
281
+ },
282
+ h('div', { style: { fontSize: '12px', fontWeight: 600 } }, '新增账号'),
283
+ h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '10px', alignItems: 'center' } },
284
+ h('input', {
285
+ ref: idRef,
286
+ placeholder: '账号 id,如 c',
287
+ style: {
288
+ fontSize: '11px',
289
+ padding: '4px 8px',
290
+ borderRadius: '6px',
291
+ border: '1px solid var(--dsw-alias-border-subtle, rgba(127,127,127,.3))',
292
+ background: 'transparent',
293
+ color: 'inherit',
294
+ width: '130px',
295
+ },
296
+ }),
297
+ h('input', {
298
+ ref: labelRef,
299
+ placeholder: '显示名(可留空)',
300
+ style: {
301
+ fontSize: '11px',
302
+ padding: '4px 8px',
303
+ borderRadius: '6px',
304
+ border: '1px solid var(--dsw-alias-border-subtle, rgba(127,127,127,.3))',
305
+ background: 'transparent',
306
+ color: 'inherit',
307
+ width: '170px',
308
+ },
309
+ }),
310
+ button(busy === 'add' ? '添加中…' : '添加账号', submit, true),
205
311
  ),
312
+ h('div', { style: { fontSize: '11px', opacity: 0.6 } }, '加好后点那张卡片上的「登录」,仪表里授权对应账号。'),
313
+ message ? note(message) : null,
206
314
  )
207
315
  }
208
316
 
@@ -302,7 +410,7 @@ window.__ModuleLoader__.load({
302
410
  }
303
411
 
304
412
  function Panel() {
305
- const { data, busy, action, load, login, exportAccount, importBundle, exportState, importStateText, saveProxy } = useStatus()
413
+ const { data, busy, action, load, login, addAccount, removeAccount, exportAccount, importBundle, exportState, importStateText, saveProxy } = useStatus()
306
414
  const [proxyDraft, setProxyDraft] = React.useState(null)
307
415
  const shownProxy = proxyDraft ?? data.proxy ?? ''
308
416
  return h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px', padding: '4px 0 12px' } },
@@ -335,15 +443,23 @@ window.__ModuleLoader__.load({
335
443
  data.error ? h('div', { style: { fontSize: '12px', color: 'var(--dsw-alias-text-danger, #e03131)' } }, data.error) : null,
336
444
  h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '10px' } },
337
445
  data.accounts.length === 0 && !data.loading
338
- ? h('div', { style: { fontSize: '12px', opacity: 0.6 } }, '还没有配置账号。用 lovart_add_account 添加。')
446
+ ? h('div', { style: { fontSize: '12px', opacity: 0.6 } }, '还没有配置账号,在下面「新增账号」里加一个再登录。')
339
447
  : data.accounts.map((account) => h(AccountCard, {
340
448
  key: account.id,
341
449
  account,
342
450
  busy,
343
451
  onLogin: login,
344
452
  onExport: exportAccount,
453
+ onRemove: removeAccount,
345
454
  })),
346
455
  ),
456
+ h(AddAccountBox, { onAdd: addAccount, busy }),
457
+ action && action.kind === 'add'
458
+ ? note(`已新增账号 ${action.id}(${action.label})${action.saved ? ',并写入插件配置' : ',仅本次会话有效'}。点上面的「登录」去授权。`)
459
+ : null,
460
+ action && action.kind === 'remove'
461
+ ? note(`已移除账号 ${action.id}${action.saved ? ',配置已更新' : ''},本机令牌已清除。`)
462
+ : null,
347
463
  action && action.kind === 'proxy'
348
464
  ? note(action.proxy ? `代理已设为 ${action.proxy},所有 Lovart 请求将走它。` : '代理已清除,改为直连。')
349
465
  : null,
package/lib/index.js CHANGED
@@ -528,6 +528,40 @@ export async function apply(ctx, config = {}) {
528
528
  },
529
529
  })))
530
530
 
531
+ async function addAccount(rawId, rawLabel) {
532
+ const raw = String(rawId ?? '').trim()
533
+ if (!/^[A-Za-z0-9_-]{1,32}$/.test(raw)) {
534
+ throw new Error(`Invalid account id "${raw}": use 1-32 characters of A-Z, a-z, 0-9, _ or -.`)
535
+ }
536
+ const id = raw.toLowerCase()
537
+ if (findAccount(id) !== undefined) throw new Error(`Account "${id}" already exists.`)
538
+ const label = typeof rawLabel === 'string' && rawLabel.trim() !== '' ? rawLabel.trim() : `Lovart ${id.toUpperCase()}`
539
+ const account = normalizeAccounts([{ id, label }])[0]
540
+ accounts.push(account)
541
+ const persisted = await persistAccounts()
542
+ return { account, persisted }
543
+ }
544
+
545
+ async function removeAccount(rawId) {
546
+ const account = findAccount(String(rawId ?? ''))
547
+ if (account === undefined) {
548
+ throw new Error(`Unknown account "${rawId}". Known: ${accounts.map((entry) => entry.id).join(', ')}`)
549
+ }
550
+ closePending(account.id, 'cancelled')
551
+ const dispose = mounted.get(account.id)
552
+ if (dispose !== undefined) {
553
+ mounted.delete(account.id)
554
+ try { dispose() } catch { /* already disposed */ }
555
+ }
556
+ for (const ref of [account.refs.access, account.refs.refresh, account.refs.expires]) {
557
+ await credentials.unset(ref)
558
+ }
559
+ const position = accounts.indexOf(account)
560
+ if (position >= 0) accounts.splice(position, 1)
561
+ const persisted = await persistAccounts()
562
+ return { account, persisted }
563
+ }
564
+
531
565
  disposers.push(ctx.tools.register(defineTool({
532
566
  name: 'lovart_add_account',
533
567
  description: 'Add another Lovart account slot at runtime and persist it in the plugin config. Follow with lovart_login for that account id.',
@@ -537,17 +571,13 @@ export async function apply(ctx, config = {}) {
537
571
  },
538
572
  output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] },
539
573
  async execute(args) {
540
- const raw = String(args.account ?? '').trim()
541
- if (!/^[A-Za-z0-9_-]{1,32}$/.test(raw)) {
542
- return `Invalid account id "${raw}": use 1-32 characters of A-Z, a-z, 0-9, _ or -.`
574
+ let added
575
+ try {
576
+ added = await addAccount(args.account, args.label)
577
+ } catch (error) {
578
+ return error.message
543
579
  }
544
- const id = raw.toLowerCase()
545
- if (findAccount(id) !== undefined) return `Account "${id}" already exists.`
546
- const label = typeof args.label === 'string' && args.label.trim() !== '' ? args.label.trim() : `Lovart ${id.toUpperCase()}`
547
- const account = normalizeAccounts([{ id, label }])[0]
548
- accounts.push(account)
549
- const persisted = await persistAccounts()
550
- return `Added lovart-${id}${persisted ? ' and saved it to the plugin config' : ' for this session only'}. Next: lovart_login(account: "${id}").`
580
+ return `Added lovart-${added.account.id}${added.persisted ? ' and saved it to the plugin config' : ' for this session only'}. Next: lovart_login(account: "${added.account.id}").`
551
581
  },
552
582
  })))
553
583
 
@@ -559,21 +589,13 @@ export async function apply(ctx, config = {}) {
559
589
  },
560
590
  output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] },
561
591
  async execute(args) {
562
- const account = findAccount(String(args.account ?? ''))
563
- if (account === undefined) return `Unknown account "${args.account}". Known: ${accounts.map((entry) => entry.id).join(', ')}`
564
- closePending(account.id, 'cancelled')
565
- const dispose = mounted.get(account.id)
566
- if (dispose !== undefined) {
567
- mounted.delete(account.id)
568
- try { dispose() } catch { /* already disposed */ }
569
- }
570
- for (const ref of [account.refs.access, account.refs.refresh, account.refs.expires]) {
571
- await credentials.unset(ref)
592
+ let removed
593
+ try {
594
+ removed = await removeAccount(args.account)
595
+ } catch (error) {
596
+ return error.message
572
597
  }
573
- const index = accounts.indexOf(account)
574
- if (index >= 0) accounts.splice(index, 1)
575
- const persisted = await persistAccounts()
576
- return `Removed lovart-${account.id}${persisted ? ' and saved the config' : ' for this session only'}.`
598
+ return `Removed lovart-${removed.account.id}${removed.persisted ? ' and saved the config' : ' for this session only'}.`
577
599
  },
578
600
  })))
579
601
 
@@ -1085,6 +1107,8 @@ export async function apply(ctx, config = {}) {
1085
1107
  openBrowser(url)
1086
1108
  return url
1087
1109
  },
1110
+ addAccount: (id, label) => addAccount(id, label),
1111
+ removeAccount: (id) => removeAccount(id),
1088
1112
  exportAccount: (accountId) => exportAccountBundle(accountId),
1089
1113
  importAccount: (input) => importAccountBundle(input),
1090
1114
  exportState: (accountId) => exportStateFile(accountId),
package/lib/panel.mjs CHANGED
@@ -73,7 +73,7 @@ function readBody(req, limitBytes = 200_000) {
73
73
  })
74
74
  }
75
75
 
76
- export function registerPanel(ctx, { accounts, credentials, login, exportAccount, importAccount, exportState, importState, getProxy, setProxyValue }) {
76
+ export function registerPanel(ctx, { accounts, credentials, login, addAccount, removeAccount, exportAccount, importAccount, exportState, importState, getProxy, setProxyValue }) {
77
77
  const server = ctx.get?.('webServer')
78
78
  if (server === undefined || typeof server.register !== 'function') return undefined
79
79
  const connection = ctx.get?.('connection')
@@ -162,6 +162,35 @@ export function registerPanel(ctx, { accounts, credentials, login, exportAccount
162
162
  return writeJson(res, 500, { error: String(error instanceof Error ? error.message : error).slice(0, 300) })
163
163
  }
164
164
  }
165
+ if (route === 'accounts/add' && req.method === 'POST') {
166
+ if (typeof addAccount !== 'function') return writeJson(res, 501, { error: 'add account is not available' })
167
+ let body
168
+ try {
169
+ body = JSON.parse(await readBody(req))
170
+ } catch (error) {
171
+ return writeJson(res, 400, { error: String(error instanceof Error ? error.message : error).slice(0, 200) })
172
+ }
173
+ if (typeof body?.account !== 'string' || body.account.trim() === '') {
174
+ return writeJson(res, 400, { error: 'account is required' })
175
+ }
176
+ try {
177
+ const added = await addAccount(body.account, typeof body.label === 'string' ? body.label : undefined)
178
+ return writeJson(res, 200, { ok: true, id: added.account.id, label: added.account.label, saved: added.persisted })
179
+ } catch (error) {
180
+ return writeJson(res, 400, { error: String(error instanceof Error ? error.message : error).slice(0, 300) })
181
+ }
182
+ }
183
+ if (route === 'accounts/remove' && req.method === 'POST') {
184
+ if (typeof removeAccount !== 'function') return writeJson(res, 501, { error: 'remove account is not available' })
185
+ const id = url.searchParams.get('account')
186
+ if (typeof id !== 'string' || id.length === 0) return writeJson(res, 400, { error: 'missing account' })
187
+ try {
188
+ const removed = await removeAccount(id)
189
+ return writeJson(res, 200, { ok: true, id: removed.account.id, saved: removed.persisted })
190
+ } catch (error) {
191
+ return writeJson(res, 404, { error: String(error instanceof Error ? error.message : error).slice(0, 300) })
192
+ }
193
+ }
165
194
  if (route === 'export' && req.method === 'POST') {
166
195
  if (typeof exportAccount !== 'function') return writeJson(res, 501, { error: 'export is not available' })
167
196
  const id = url.searchParams.get('account')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lovart-mcp",
3
- "version": "0.12.2",
3
+ "version": "0.12.4",
4
4
  "type": "module",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {