dsh-miloco-bridge 0.1.4 → 0.1.5

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/lib/client.js CHANGED
@@ -1,13 +1,16 @@
1
1
  /**
2
2
  * dsh-miloco-bridge — browser half.
3
3
  *
4
- * A 「Miloco」 tab inside the Web GUI's 「设置 → 插件」 page:
4
+ * The settings half opens from the 「Miloco 家庭助手」 page (its 「配置」
5
+ * button) and covers:
5
6
  *
6
7
  * 1. whether Miloco is reachable; if not, one-click deploy or connect;
7
- * 2. household overview (account, devices, cameras, rules/tasks, token usage);
8
+ * 2. the household workspace directory and whether it stays registered;
8
9
  * 3. push destination: a conversation and an optional model;
9
10
  * 4. the dedicated agent's operating mode.
10
11
  *
12
+ * The household overview itself lives on the 「Miloco 家庭助手」 page.
13
+ *
11
14
  * Model selection deliberately has no "deployment default" pseudo-entry:
12
15
  * leaving it empty means the conversation keeps its own model.
13
16
  *
@@ -22,15 +25,20 @@ window.__ModuleLoader__.load({
22
25
  const React = require('react')
23
26
  const h = React.createElement
24
27
 
25
- /** Client services: the slot registry. */
26
- const inject = ['slots']
28
+ /**
29
+ * Client services this half declares.
30
+ *
31
+ * - `slots` — the slot registry every registration goes through.
32
+ * - `uiWorkspace` — session navigation (open/start).
33
+ * - `workspaces` — workspace snapshot source and idempotent `create`,
34
+ * used only when the host reports no household `workspaceId`.
35
+ * - `uiConversation` — conversation Definition registry.
36
+ */
37
+ const inject = ['slots', 'uiWorkspace', 'workspaces', 'uiConversation']
27
38
 
28
39
  /** Settings API prefix, mirrored from the host half. */
29
40
  const API = '/dsh-miloco-bridge/api'
30
41
 
31
- /** Tab identity inside 「设置 → 插件」. */
32
- const TAB_ID = 'dsh-miloco-bridge'
33
-
34
42
  const STYLE_ID = 'dsh-miloco-bridge-settings-style'
35
43
  const SESSION_LIST_ID = 'dsh-miloco-bridge-session-list'
36
44
 
@@ -85,6 +93,11 @@ window.__ModuleLoader__.load({
85
93
  .dsh-miloco-bridge-settings .field > label { color: var(--mc-text-2); font-size: 12px; }
86
94
  .dsh-miloco-bridge-settings .fieldRow { display: flex; align-items: center; gap: 8px; }
87
95
  .dsh-miloco-bridge-settings .fieldRow input:not([type="checkbox"]):not([type="radio"]) { flex: 1 1 auto; width: auto; min-width: 0; }
96
+ .dsh-miloco-bridge-settings .switchRow {
97
+ display: flex; align-items: center; gap: 8px; cursor: pointer;
98
+ color: var(--mc-text-1); font-size: 13px;
99
+ }
100
+ .dsh-miloco-bridge-settings .switchRow input { width: 15px; height: 15px; accent-color: var(--mc-accent, #4a7edc); }
88
101
  .dsh-miloco-bridge-settings .row > select,
89
102
  .dsh-miloco-bridge-settings .row > input:not([type="checkbox"]):not([type="radio"]) { flex: 1 1 220px; width: auto; }
90
103
  .dsh-miloco-bridge-settings .statusRow {
@@ -238,6 +251,182 @@ window.__ModuleLoader__.load({
238
251
  border: 1px solid var(--mc-border-soft); font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
239
252
  font-size: 11.5px; line-height: 1.5; white-space: pre-wrap;
240
253
  }
254
+
255
+ /* ---- Miloco 家庭概览(main 面板内容) ---- */
256
+ .dsh-miloco-dash {
257
+ max-width: 980px; margin: 0 auto; padding: 24px 28px 40px; color: var(--dsw-alias-label-primary, inherit);
258
+ font-size: var(--dsh-content-font-size-secondary, 13px); line-height: 1.6;
259
+ /* The host panel clips its column (overflow hidden), so the dashboard
260
+ scrolls itself instead of growing past the viewport. */
261
+ max-height: 100%; overflow-y: auto; overscroll-behavior: contain;
262
+ }
263
+ .dsh-miloco-dash-head { display: flex; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
264
+ .dsh-miloco-dash-head-main { flex: 1 1 260px; min-width: 0; }
265
+ .dsh-miloco-dash-title { font-size: 20px; font-weight: 600; line-height: 28px; letter-spacing: .01em; }
266
+ .dsh-miloco-dash-actions { flex: none; display: flex; gap: 8px; padding-top: 2px; }
267
+ .dsh-miloco-dash-actions button {
268
+ height: 30px; padding: 0 13px; border-radius: var(--dsw-radius-md, 8px);
269
+ border: 1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.24));
270
+ background: transparent; color: inherit; font: inherit; font-size: 12.5px; cursor: pointer;
271
+ transition: background-color .15s ease, border-color .15s ease, opacity .15s ease;
272
+ }
273
+ @media (hover: hover) {
274
+ .dsh-miloco-dash-actions button:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover, rgba(128,128,128,.12)); }
275
+ }
276
+ .dsh-miloco-dash-actions button.primary {
277
+ background: var(--dsw-alias-brand-primary, #4a7edc); border-color: transparent; color: #fff; font-weight: 500;
278
+ }
279
+ .dsh-miloco-dash-actions button:disabled { opacity: .45; cursor: default; }
280
+ .dsh-miloco-dash-chips { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin: 14px 0 2px; }
281
+ .dsh-miloco-dash-chip {
282
+ display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
283
+ padding: 3px 9px; border-radius: 999px; border: .5px solid var(--dsw-alias-border-l2, rgba(128,128,128,.24));
284
+ background: var(--dsw-alias-bg-l1, rgba(128,128,128,.04)); color: var(--dsw-alias-label-secondary, rgba(128,128,128,.95));
285
+ font-size: 12px; line-height: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
286
+ }
287
+ .dsh-miloco-dash-chip.warn { border-color: var(--dsw-alias-state-warning-primary, #c58a2a); color: var(--dsw-alias-state-warning-primary, #c58a2a); }
288
+ .dsh-miloco-dash-chipdot { flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); }
289
+ .dsh-miloco-dash-chip.ok .dsh-miloco-dash-chipdot { background: var(--dsw-alias-state-success-primary, #3c9a5a); }
290
+ .dsh-miloco-dash-chip.warn .dsh-miloco-dash-chipdot { background: currentColor; }
291
+ .dsh-miloco-dash-card {
292
+ border: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.14));
293
+ border-radius: var(--dsw-radius-xl, 12px); padding: 14px 16px 6px; margin-top: 12px;
294
+ background: var(--dsw-alias-bg-l1, rgba(128,128,128,.02));
295
+ }
296
+ .dsh-miloco-dash-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-bottom: 8px; }
297
+ .dsh-miloco-dash-card-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
298
+ .dsh-miloco-dash-card-head h3 { margin: 0; font-size: 14px; font-weight: 600; white-space: nowrap; }
299
+ .dsh-miloco-dash-meta { color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); font-size: 12px; white-space: nowrap; }
300
+ .dsh-miloco-dash-score {
301
+ flex: none; padding: 1px 8px; border-radius: 999px; font-size: 11.5px;
302
+ color: var(--dsw-alias-state-success-primary, #3c9a5a);
303
+ border: .5px solid var(--dsw-alias-state-success-primary, #3c9a5a);
304
+ }
305
+ .dsh-miloco-dash-rooms { columns: 210px 3; column-gap: 8px; padding-bottom: 10px; }
306
+ .dsh-miloco-dash-room {
307
+ break-inside: avoid; margin-bottom: 8px;
308
+ border: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.14));
309
+ border-radius: 10px; padding: 0 10px; background: transparent;
310
+ }
311
+ .dsh-miloco-dash-room > summary {
312
+ display: flex; align-items: center; gap: 8px; padding: 8px 0; cursor: pointer;
313
+ list-style: none; color: var(--dsw-alias-label-secondary, rgba(128,128,128,.95)); font-size: 12.5px;
314
+ user-select: none;
315
+ }
316
+ .dsh-miloco-dash-room > summary::-webkit-details-marker { display: none; }
317
+ .dsh-miloco-dash-room > summary:hover { color: var(--dsw-alias-label-primary, inherit); }
318
+ .dsh-miloco-dash-chev {
319
+ flex: none; width: 12px; text-align: center; font-size: 15px; line-height: 1;
320
+ color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); transition: transform .15s ease;
321
+ }
322
+ .dsh-miloco-dash-room[open] > summary .dsh-miloco-dash-chev { transform: rotate(90deg); }
323
+ .dsh-miloco-dash-room-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
324
+ .dsh-miloco-dash-room-count { flex: none; font-variant-numeric: tabular-nums; color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); }
325
+ .dsh-miloco-dash-room-count.ok { color: var(--dsw-alias-state-success-primary, #3c9a5a); }
326
+ .dsh-miloco-dash-room-count.off { color: var(--dsw-alias-state-error-primary, #c85a50); }
327
+ .dsh-miloco-dash-room-bar { flex: none; width: 54px; height: 4px; border-radius: 2px; overflow: hidden; background: var(--dsw-alias-border-l1, rgba(128,128,128,.14)); }
328
+ .dsh-miloco-dash-room-bar i { display: block; height: 100%; border-radius: 2px; background: var(--dsw-alias-state-success-primary, #3c9a5a); }
329
+ .dsh-miloco-dash-list { list-style: none; margin: 0 0 6px; padding: 0; max-height: 300px; overflow: auto; }
330
+ .dsh-miloco-dash-room .dsh-miloco-dash-list { padding-left: 0; max-height: 220px; border-top: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.1)); }
331
+ .dsh-miloco-dash-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.1)); min-width: 0; }
332
+ .dsh-miloco-dash-devdot { flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--dsw-alias-label-tertiary, rgba(128,128,128,.5)); }
333
+ .dsh-miloco-dash-devdot.ok { background: var(--dsw-alias-state-success-primary, #3c9a5a); }
334
+ .dsh-miloco-dash-main { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; }
335
+ .dsh-miloco-dash-tag {
336
+ flex: none; padding: 0 6px; border-radius: 999px; font-size: 10.5px; line-height: 16px;
337
+ color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72));
338
+ border: .5px solid var(--dsw-alias-border-l2, rgba(128,128,128,.24));
339
+ }
340
+ .dsh-miloco-dash-link { border: 0; background: transparent; color: inherit; font: inherit; padding: 0; cursor: pointer; text-align: left; }
341
+ @media (hover: hover) { .dsh-miloco-dash-link:hover { color: var(--dsw-alias-brand-primary, #4a7edc); } }
342
+ .dsh-miloco-dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; margin-top: 12px; }
343
+ .dsh-miloco-dash-grid .dsh-miloco-dash-card { margin-top: 0; }
344
+ .dsh-miloco-dash-summary .dsh-miloco-dash-chips { margin: 0 0 8px; }
345
+ .dsh-miloco-dash-stats {
346
+ display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 8px;
347
+ padding: 2px 0 12px;
348
+ }
349
+ .dsh-miloco-dash-stat {
350
+ display: flex; flex-direction: column; gap: 2px; padding: 8px 10px; border-radius: 10px;
351
+ border: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.14));
352
+ background: var(--dsw-alias-bg-l2, rgba(128,128,128,.04));
353
+ }
354
+ .dsh-miloco-dash-stat .k { color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); font-size: 11px; }
355
+ .dsh-miloco-dash-stat .v {
356
+ font-size: 15px; font-weight: 600; line-height: 1.4; overflow: hidden;
357
+ text-overflow: ellipsis; white-space: nowrap;
358
+ }
359
+ .dsh-miloco-dash-config-backdrop {
360
+ position: fixed; inset: 0; z-index: 1200; display: flex; align-items: center; justify-content: center;
361
+ padding: 24px; background: rgba(0, 0, 0, .38);
362
+ }
363
+ .dsh-miloco-dash-config {
364
+ width: min(920px, 94vw); max-height: 88vh; overflow: auto; border-radius: 14px;
365
+ border: .5px solid var(--dsw-alias-border-l2, rgba(128,128,128,.24));
366
+ background: var(--dsw-alias-bg-module-platform, var(--dsw-alias-bg-l1, #fff));
367
+ box-shadow: 0 18px 50px rgba(0, 0, 0, .28);
368
+ }
369
+ .dsh-miloco-dash-config-head {
370
+ position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between;
371
+ gap: 12px; padding: 12px 18px; border-bottom: .5px solid var(--dsw-alias-border-l1, rgba(128,128,128,.14));
372
+ background: inherit;
373
+ }
374
+ .dsh-miloco-dash-config-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
375
+ .dsh-miloco-dash-config-close {
376
+ border: 0; background: transparent; color: var(--dsw-alias-label-secondary, rgba(128,128,128,.95));
377
+ font-size: 20px; line-height: 1; padding: 2px 6px; border-radius: 6px; cursor: pointer;
378
+ }
379
+ @media (hover: hover) { .dsh-miloco-dash-config-close:hover { background: var(--dsw-alias-interactive-bg-hover, rgba(128,128,128,.12)); } }
380
+ .dsh-miloco-dash-config > .dsh-miloco-bridge-settings { max-width: none; padding: 16px 18px 22px; }
381
+ .dsh-miloco-dash-empty { padding: 18px 2px 20px; color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); font-size: 12.5px; }
382
+ .dsh-miloco-dash-loading { padding: 28px 2px; color: var(--dsw-alias-label-tertiary, rgba(128,128,128,.72)); font-size: 12.5px; }
383
+ .dsh-miloco-dash-error {
384
+ display: flex; align-items: center; justify-content: space-between; gap: 8px;
385
+ margin: 12px 0 0; padding: 8px 10px; border-radius: var(--dsw-radius-md, 8px);
386
+ border: .5px solid var(--dsw-alias-state-error-primary, #c85a50);
387
+ color: var(--dsw-alias-state-error-primary, #c85a50);
388
+ }
389
+
390
+ /* ---- 折叠推送卡片(照抄 DSH 定时任务触发通知卡片) ---- */
391
+ .dsh-miloco-push-root {
392
+ min-width: 0;
393
+ border: 0.5px solid var(--dsw-alias-border-l1);
394
+ border-radius: var(--dsw-radius-xl, var(--dsw-alias-radius-xl, 12px));
395
+ background: var(--dsw-alias-turn-trigger-bg, var(--dsw-alias-markdown-code-block));
396
+ transition: background-color 100ms ease;
397
+ }
398
+ @media (hover: hover) {
399
+ .dsh-miloco-push-root:hover {
400
+ background: var(--dsw-alias-turn-trigger-bg-hover, var(--dsw-alias-interactive-bg-hover));
401
+ }
402
+ }
403
+ .dsh-miloco-push-header {
404
+ display: flex; align-items: center; gap: 10px; width: 100%; padding: 12px 16px;
405
+ border: 0; background: none; color: var(--dsw-alias-label-tertiary);
406
+ transition: color 100ms ease; font: inherit; text-align: left; cursor: pointer;
407
+ }
408
+ @media (hover: hover) {
409
+ .dsh-miloco-push-header:hover { color: var(--dsw-alias-label-secondary); }
410
+ }
411
+ .dsh-miloco-push-icon { display: inline-flex; flex: none; color: inherit; }
412
+ .dsh-miloco-push-title {
413
+ flex: 1; min-width: 0; overflow-wrap: anywhere;
414
+ font-size: var(--dsh-content-font-size, 14px);
415
+ }
416
+ .dsh-miloco-push-time { flex: none; margin-left: auto; color: inherit; font: var(--dsw-font-xxs-12); }
417
+ .dsh-miloco-push-chevron { flex: none; color: inherit; }
418
+ .dsh-miloco-push-chevron.open { transform: rotate(180deg); }
419
+ .dsh-miloco-push-body { padding: 0 16px 12px 40px; }
420
+ .dsh-miloco-push-explanation {
421
+ margin: 8px 0; color: var(--dsw-alias-label-secondary); font: var(--dsw-font-xxs-12);
422
+ }
423
+ .dsh-miloco-push-content {
424
+ max-height: 240px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere;
425
+ font: var(--dsw-font-xxs-12);
426
+ }
427
+ @media (prefers-reduced-motion: reduce) {
428
+ .dsh-miloco-push-root, .dsh-miloco-push-header { transition: none; }
429
+ }
241
430
  `
242
431
  document.head.appendChild(style)
243
432
  }
@@ -499,6 +688,15 @@ window.__ModuleLoader__.load({
499
688
  const [upgradeConfirmOpen, setUpgradeConfirmOpen] = React.useState(false)
500
689
  const [upgradeBusy, setUpgradeBusy] = React.useState(false)
501
690
  const [upgradePolling, setUpgradePolling] = React.useState(false)
691
+ const [targetMode, setTargetMode] = React.useState('archive')
692
+ const [homePath, setHomePath] = React.useState('')
693
+ const [homeDraft, setHomeDraft] = React.useState('')
694
+ const [homeSource, setHomeSource] = React.useState('')
695
+ const [homeArchiveId, setHomeArchiveId] = React.useState('')
696
+ const [homeEnabled, setHomeEnabled] = React.useState(true)
697
+ const [homeBusy, setHomeBusy] = React.useState(false)
698
+ const [homeNotice, setHomeNotice] = React.useState(null)
699
+ const [homeError, setHomeError] = React.useState(null)
502
700
 
503
701
  const reload = React.useCallback(() => {
504
702
  setError(null)
@@ -516,8 +714,18 @@ window.__ModuleLoader__.load({
516
714
  setConnectUrl(payload.connection?.baseUrl ?? '')
517
715
  setMode(payload.mode?.id ?? 'readonly')
518
716
  setPerceptionLevel(payload.overlay?.push?.perception?.minLevel ?? payload.push?.perception?.minLevel ?? 'all')
717
+ setTargetMode(payload.overlay?.push?.target?.mode || payload.push?.target?.mode || 'archive')
519
718
  })
520
719
  .catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)))
720
+ call('/home')
721
+ .then((payload) => {
722
+ setHomePath(payload.path ?? '')
723
+ setHomeDraft(payload.path ?? '')
724
+ setHomeSource(payload.dirSource ?? '')
725
+ setHomeArchiveId(payload.archiveSessionId ?? '')
726
+ setHomeEnabled(payload.enabled !== false)
727
+ })
728
+ .catch(() => { /* 工作区不可用时保留空值,页面允许继续操作其它设置 */ })
521
729
  }, [])
522
730
 
523
731
  const loadVersion = React.useCallback((refresh = false) => {
@@ -618,6 +826,7 @@ window.__ModuleLoader__.load({
618
826
  model: modelValue ? model : '',
619
827
  reasoningEffort: effort,
620
828
  perceptionLevel,
829
+ mode: targetMode,
621
830
  }),
622
831
  })
623
832
  .then((payload) => {
@@ -626,7 +835,59 @@ window.__ModuleLoader__.load({
626
835
  })
627
836
  .catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)))
628
837
  .finally(() => setBusy(false))
629
- }, [sessionId, modelValue, reasoningEffort, perceptionLevel, status])
838
+ }, [sessionId, modelValue, reasoningEffort, perceptionLevel, targetMode, status])
839
+
840
+ const saveHome = React.useCallback(() => {
841
+ const dir = homeDraft.trim()
842
+ if (!dir) {
843
+ setHomeError('目录不能为空')
844
+ return
845
+ }
846
+ setHomeBusy(true)
847
+ setHomeError(null)
848
+ setHomeNotice(null)
849
+ call('/home', {
850
+ method: 'POST',
851
+ headers: { 'content-type': 'application/json' },
852
+ body: JSON.stringify({ dir }),
853
+ })
854
+ .then((payload) => {
855
+ setHomePath(payload.path ?? dir)
856
+ setHomeDraft(payload.path ?? dir)
857
+ setHomeSource(payload.dirSource ?? 'overlay')
858
+ setHomeArchiveId(payload.archiveSessionId ?? '')
859
+ setHomeEnabled(payload.enabled !== false)
860
+ setHomeNotice(payload.previousPath && payload.previousPath !== payload.path
861
+ ? `已切换目录;旧注册 ${payload.previousPath} 保留`
862
+ : '已保存')
863
+ })
864
+ .catch((cause) => setHomeError(cause instanceof Error ? cause.message : String(cause)))
865
+ .finally(() => setHomeBusy(false))
866
+ }, [homeDraft])
867
+
868
+ /**
869
+ * Turn the pinned household workspace on or off.
870
+ * @param {boolean} value - The wanted state.
871
+ * @returns {void}
872
+ */
873
+ const saveHomeEnabled = React.useCallback((value) => {
874
+ setHomeBusy(true)
875
+ setHomeError(null)
876
+ setHomeNotice(null)
877
+ call('/home', {
878
+ method: 'POST',
879
+ headers: { 'content-type': 'application/json' },
880
+ body: JSON.stringify({ enabled: value }),
881
+ })
882
+ .then((payload) => {
883
+ setHomeEnabled(payload.enabled !== false)
884
+ setHomeNotice(payload.enabled === false
885
+ ? '已关闭置顶工作区;家庭会话归到「未分组」'
886
+ : '已启用置顶工作区')
887
+ })
888
+ .catch((cause) => setHomeError(cause instanceof Error ? cause.message : String(cause)))
889
+ .finally(() => setHomeBusy(false))
890
+ }, [])
630
891
 
631
892
  const deploy = React.useCallback(() => {
632
893
  setBusy(true)
@@ -725,7 +986,6 @@ window.__ModuleLoader__.load({
725
986
  const backend = status.backend ?? {}
726
987
  const connection = status.connection ?? {}
727
988
  const push = status.push ?? {}
728
- const overview = status.overview ?? null
729
989
  const adapter = status.adapter ?? {}
730
990
  const reachable = Boolean(backend.reachable)
731
991
  const cards = []
@@ -778,37 +1038,6 @@ window.__ModuleLoader__.load({
778
1038
  deployLog ? h('pre', null, deployLog) : null))
779
1039
  }
780
1040
 
781
- // ---------------------------------------------------------------- 家庭概览
782
- if (reachable && overview) {
783
- cards.push(h('div', { className: 'card', key: 'overview' },
784
- h('h3', null, '家庭概览'),
785
- h('div', { className: 'grid' },
786
- h('div', { className: 'stat' },
787
- h('span', { className: 'k' }, '米家账号'),
788
- h('span', { className: 'v', title: overview.account?.nickname || '' }, overview.account?.nickname || (overview.accountBound ? '已绑定' : '未绑定'))),
789
- h('div', { className: 'stat' },
790
- h('span', { className: 'k' }, '设备'),
791
- h('span', { className: 'v' }, overview.deviceCount ?? '—')),
792
- h('div', { className: 'stat' },
793
- h('span', { className: 'k' }, '摄像头'),
794
- h('span', { className: 'v' }, overview.cameraCount ?? '—')),
795
- h('div', { className: 'stat' },
796
- h('span', { className: 'k' }, '规则 / 任务'),
797
- h('span', { className: 'v' }, `${overview.ruleCount ?? '—'} / ${overview.taskCount ?? '—'}`)),
798
- h('div', { className: 'stat' },
799
- h('span', { className: 'k' }, '感知引擎'),
800
- h('span', { className: 'v' }, overview.perceptionRunning ? '运行中' : '未运行')),
801
- h('div', { className: 'stat' },
802
- h('span', { className: 'k' }, '感知源'),
803
- h('span', { className: 'v' }, overview.perceptionSources ?? '—')),
804
- h('div', { className: 'stat' },
805
- h('span', { className: 'k' }, 'token 记录'),
806
- h('span', { className: 'v' }, overview.tokenEvents ?? '—')),
807
- h('div', { className: 'stat' },
808
- h('span', { className: 'k' }, 'token 合计'),
809
- h('span', { className: 'v' }, overview.tokenTotal ?? '—')))))
810
- }
811
-
812
1041
  // ---------------------------------------------------------------- 运行模式
813
1042
  const modes = status.modes ?? []
814
1043
  cards.push(h('div', { className: 'card', key: 'mode' },
@@ -848,7 +1077,7 @@ window.__ModuleLoader__.load({
848
1077
  h('ul', null,
849
1078
  h('li', null, '新建会话时选择「Miloco 家庭助手」;其他对话不会出现 miloco_* 工具。'),
850
1079
  h('li', null, '在本页选择运行模式;切换后重启 DSH 或重新挂载预设生效。'),
851
- h('li', null, '推送目标留空 = 投到上一次触发 Miloco 的会话;也可以指定任意会话。'),
1080
+ h('li', null, '推送目标:默认投到归档会话;切到「指定会话」后,会话留空 = 上一次触发 Miloco 的会话,也可以指定任意会话。'),
852
1081
  h('li', null, '模型留空 = 跟随该对话;选择模型后,该对话会用指定模型处理推送。')),
853
1082
  h('h4', null, '可以这样问'),
854
1083
  h('ul', null,
@@ -858,6 +1087,48 @@ window.__ModuleLoader__.load({
858
1087
  h('li', null, '“最近 token 用量多少?” / “为什么这条规则没触发?”')))))
859
1088
  }
860
1089
 
1090
+ // ---------------------------------------------------------------- 家庭工作区
1091
+ const sourceLabel = homeSource === 'overlay'
1092
+ ? '设置页覆盖'
1093
+ : homeSource === 'config'
1094
+ ? '插件配置'
1095
+ : '默认目录'
1096
+ cards.push(h('div', { className: 'card', key: 'workspace' },
1097
+ h('h3', null, '家庭工作区'),
1098
+ h('div', { className: 'field' },
1099
+ h('label', null, '目录'),
1100
+ h('div', { className: 'fieldRow' },
1101
+ h('input', {
1102
+ type: 'text',
1103
+ value: homeDraft,
1104
+ placeholder: homePath || '家庭工作区目录',
1105
+ onChange: (event) => setHomeDraft(event.target.value),
1106
+ }),
1107
+ h('button', { type: 'button', disabled: homeBusy, onClick: saveHome }, homeBusy ? '保存中…' : '保存'))),
1108
+ h('div', { className: 'field' },
1109
+ h('label', null, '启用置顶工作区'),
1110
+ h('label', { className: 'switchRow' },
1111
+ h('input', {
1112
+ type: 'checkbox',
1113
+ checked: homeEnabled,
1114
+ disabled: homeBusy,
1115
+ onChange: (event) => saveHomeEnabled(event.target.checked),
1116
+ }),
1117
+ h('span', null, homeEnabled ? '已启用:家庭工作区置顶显示' : '已关闭:家庭会话归到「未分组」'))),
1118
+ h('div', { className: 'muted' }, `当前:${homePath || '—'}(${sourceLabel})`),
1119
+ homeArchiveId ? h('div', { className: 'muted' }, `推送归档会话:${homeArchiveId}`) : null,
1120
+ h('div', { className: 'muted' },
1121
+ '修改目录会注册新的工作区;旧目录的工作区注册不会自动删除,目录与其中的会话不受影响。'),
1122
+ h('div', { className: 'muted' },
1123
+ '工作区从侧边栏被删除后,插件会在 30 秒内自动恢复。'),
1124
+ h('div', { className: 'muted' },
1125
+ '关闭「启用置顶工作区」后不再注册工作区,已经在里面的家庭会话会归到「未分组」。'),
1126
+ homeNotice || homeError
1127
+ ? h('div', { className: 'row' },
1128
+ homeNotice ? h('span', { className: 'pill ok' }, homeNotice) : null,
1129
+ homeError ? h('span', { className: 'pill bad' }, homeError) : null)
1130
+ : null))
1131
+
861
1132
  // ---------------------------------------------------------------- 推送目标
862
1133
  const lastSession = status.lastMilocoSession ?? ''
863
1134
  const selectedSession = (status.sessions ?? []).find(session => session.id === sessionId)
@@ -873,7 +1144,17 @@ window.__ModuleLoader__.load({
873
1144
  cards.push(h('div', { className: 'card', key: 'target' },
874
1145
  h('h3', null, '推送目标'),
875
1146
  h('div', { className: 'field' },
876
- h('label', null, '对话会话'),
1147
+ h('label', null, '投递方式'),
1148
+ h('select', {
1149
+ value: targetMode,
1150
+ onChange: (event) => setTargetMode(event.target.value),
1151
+ }, [
1152
+ h('option', { key: 'archive', value: 'archive' }, '归档会话'),
1153
+ h('option', { key: 'session', value: 'session' }, '指定会话'),
1154
+ ])),
1155
+ targetMode === 'session'
1156
+ ? h('div', { className: 'field' },
1157
+ h('label', null, '对话会话'),
877
1158
  h('div', { className: 'fieldRow' },
878
1159
  h('input', {
879
1160
  type: 'text',
@@ -906,10 +1187,13 @@ window.__ModuleLoader__.load({
906
1187
  },
907
1188
  }),
908
1189
  h('datalist', { id: SESSION_LIST_ID }, sessionOptions(status)),
909
- h('button', { type: 'button', disabled: busy, onClick: refreshSessions }, '刷新会话'))),
910
- h('div', { className: 'muted' },
911
- '留空 = 投到「上一次触发 miloco 的会话」,也就是最近一次调用过米家工具的对话。'
912
- + '也可以从下拉里选任意对话,或直接填会话 id。'),
1190
+ h('button', { type: 'button', disabled: busy, onClick: refreshSessions }, '刷新会话'))) : null,
1191
+ targetMode === 'archive'
1192
+ ? h('div', { className: 'muted' },
1193
+ `所有推送进入固定归档会话 ${homeArchiveId || 'miloco-archive'}(不可修改),可在家庭概览页直接打开。`)
1194
+ : h('div', { className: 'muted' },
1195
+ '留空 = 投到「上一次触发 miloco 的会话」,也就是最近一次调用过米家工具的对话。'
1196
+ + '也可以从下拉里选任意对话,或直接填会话 id。'),
913
1197
  h('div', { className: 'formGrid' },
914
1198
  h('div', { className: 'field' },
915
1199
  h('label', null, '模型'),
@@ -1219,27 +1503,668 @@ window.__ModuleLoader__.load({
1219
1503
  }, uninstallBusy ? '卸载中…' : '确认卸载')))
1220
1504
  : null))
1221
1505
 
1222
- // 运行状态固定排在第二格(概览之后;未连接时在部署卡之后)。
1223
- const order = ['deploy', 'overview', 'runtime', 'mode', 'guide', 'target']
1506
+ // 运行状态固定排在第二格(未连接时在部署卡之后)。
1507
+ const order = ['deploy', 'runtime', 'workspace', 'mode', 'guide', 'target']
1224
1508
  cards.sort((a, b) => order.indexOf(a.key) - order.indexOf(b.key))
1225
1509
 
1226
1510
  return h('div', { className: 'dsh-miloco-bridge-settings' }, cards)
1227
1511
  }
1228
1512
 
1513
+ /* ================================================================
1514
+ * 「Miloco 家庭助手」侧栏入口 + 专属工作区面板 + 折叠推送卡片
1515
+ * ================================================================ */
1516
+
1517
+ /** Sidebar row id and main panel key share one identity. */
1518
+ const PANEL_ID = 'miloco'
1519
+
1520
+ /** Message source kind carried by an inbound Miloco push. */
1521
+ const PUSH_SOURCE_KIND = 'miloco-push'
1522
+
1523
+ /** Agent preset the Miloco conversations run with. */
1524
+ const AGENT_PRESET = 'miloco'
1525
+
1526
+ /** Stable DOM id counter for the push card's disclosure body. */
1527
+ let pushBodySeq = 0
1528
+
1529
+ /**
1530
+ * Read the plain text of a message's content blocks.
1531
+ * @param {any} content - ContentBlock list of a user message.
1532
+ * @returns {string} The joined text blocks.
1533
+ */
1534
+ function contentText(content) {
1535
+ if (!Array.isArray(content)) return ''
1536
+ return content
1537
+ .filter(block => block && block.type === 'text' && typeof block.text === 'string')
1538
+ .map(block => block.text)
1539
+ .join('\n')
1540
+ }
1541
+
1542
+ /**
1543
+ * First non-empty line of a text blob.
1544
+ * @param {string} text - Source text.
1545
+ * @returns {string} The trimmed first line, or an empty string.
1546
+ */
1547
+ function firstLine(text) {
1548
+ const line = String(text ?? '')
1549
+ .split('\n')
1550
+ .map(part => part.trim())
1551
+ .find(part => part.length > 0)
1552
+ return line ?? ''
1553
+ }
1554
+
1555
+ /**
1556
+ * Short display form of a Miloco version.
1557
+ * @param {any} value - Version string.
1558
+ * @returns {string} The stable part, or ''.
1559
+ */
1560
+ function formatShortVersion(value) {
1561
+ const text = String(value ?? '').trim()
1562
+ if (!text) return ''
1563
+ return text.split('+')[0].replace(/\.(post|dev|rc)\d*.*$/, '')
1564
+ }
1565
+
1566
+ /**
1567
+ * Compact local message clock, matching DSH's message chrome.
1568
+ * @param {any} value - ISO string, epoch millis, or Date.
1569
+ * @param {number} [now] - Reference instant.
1570
+ * @returns {string} Local clock, or an empty string for an invalid instant.
1571
+ */
1572
+ function formatMessageClock(value, now = Date.now()) {
1573
+ if (value === undefined || value === null || value === '') return ''
1574
+ const date = value instanceof Date ? value : new Date(value)
1575
+ if (Number.isNaN(date.getTime())) return ''
1576
+ const reference = new Date(now)
1577
+ const pad = part => String(part).padStart(2, '0')
1578
+ const clock = `${pad(date.getHours())}:${pad(date.getMinutes())}`
1579
+ if (date.getFullYear() === reference.getFullYear()
1580
+ && date.getMonth() === reference.getMonth()
1581
+ && date.getDate() === reference.getDate()) return clock
1582
+ const md = `${date.getMonth() + 1}月${date.getDate()}日`
1583
+ return date.getFullYear() === reference.getFullYear()
1584
+ ? `${md} ${clock}`
1585
+ : `${date.getFullYear()}年${md} ${clock}`
1586
+ }
1587
+
1588
+ /**
1589
+ * Event time from the overview's normalised field, or the raw backend names.
1590
+ * @param {any} item - Event row.
1591
+ * @returns {string} The formatted clock, or ''.
1592
+ */
1593
+ function eventClock(item) {
1594
+ const raw = item?.timestamp ?? item?.ts ?? item?.created_at ?? item?.time
1595
+ if (typeof raw === 'number') return formatMessageClock(raw < 1e12 ? raw * 1000 : raw)
1596
+ return formatMessageClock(raw)
1597
+ }
1598
+
1599
+ /**
1600
+ * Event label from the overview's normalised field, or the raw names.
1601
+ * @param {any} item - Event row.
1602
+ * @returns {string} The event label.
1603
+ */
1604
+ function eventKind(item) {
1605
+ return String(item?.kind ?? item?.type ?? item?.event_type ?? '事件')
1606
+ }
1607
+
1608
+ /**
1609
+ * Event source from the overview's normalised field, or the raw names.
1610
+ * @param {any} item - Event row.
1611
+ * @returns {string} The source label.
1612
+ */
1613
+ function eventSource(item) {
1614
+ return String(item?.source ?? item?.device_name ?? item?.did ?? '—')
1615
+ }
1616
+
1617
+ /**
1618
+ * Event one-line text from the overview's normalised field.
1619
+ * @param {any} item - Event row.
1620
+ * @returns {string} The summary, or ''.
1621
+ */
1622
+ function eventSummary(item) {
1623
+ return String(item?.summary ?? '').trim()
1624
+ }
1625
+
1626
+ /**
1627
+ * Event urgency chip label, when the row carries one.
1628
+ * @param {any} item - Event row.
1629
+ * @returns {string} `高` / `中` / `低`, or '' when absent.
1630
+ */
1631
+ function eventPriority(item) {
1632
+ const value = String(item?.priority ?? '').trim().toLowerCase()
1633
+ return value === 'high' ? '高' : value === 'medium' ? '中' : value === 'low' ? '低' : ''
1634
+ }
1635
+
1636
+ /**
1637
+ * The sidebar row glyph: a house outline drawn at the requested edge.
1638
+ * The sidebar owns the row, its label, and the selected state, so the
1639
+ * glyph only inherits the row's color, exactly like the shipped panels.
1640
+ * @param {{size?: number, active?: boolean}} props - Sidebar icon share.
1641
+ * @returns {any} The SVG element.
1642
+ */
1643
+ function MilocoPanelIcon(props) {
1644
+ const size = typeof props?.size === 'number' && props.size > 0 ? props.size : 16
1645
+ return h('svg', {
1646
+ viewBox: '0 0 24 24',
1647
+ width: size,
1648
+ height: size,
1649
+ fill: 'none',
1650
+ stroke: 'currentColor',
1651
+ strokeWidth: 1.6,
1652
+ strokeLinecap: 'round',
1653
+ strokeLinejoin: 'round',
1654
+ 'aria-hidden': 'true',
1655
+ focusable: 'false',
1656
+ },
1657
+ h('path', { d: 'M3.6 10.4 12 4l8.4 6.4' }),
1658
+ h('path', { d: 'M5.6 9.3V19a1 1 0 0 0 1 1h10.8a1 1 0 0 0 1-1V9.3' }),
1659
+ h('path', { d: 'M9.6 20v-4.6h4.8V20' }))
1660
+ }
1661
+
1662
+ /**
1663
+ * The push card's source glyph: an external event (webhook family).
1664
+ * @param {number} size - Requested square edge.
1665
+ * @returns {any} The SVG element.
1666
+ */
1667
+ function MilocoPushSourceIcon(size) {
1668
+ return h('svg', {
1669
+ viewBox: '0 0 24 24',
1670
+ width: size,
1671
+ height: size,
1672
+ fill: 'none',
1673
+ stroke: 'currentColor',
1674
+ strokeWidth: 1.6,
1675
+ strokeLinecap: 'round',
1676
+ strokeLinejoin: 'round',
1677
+ 'aria-hidden': 'true',
1678
+ focusable: 'false',
1679
+ },
1680
+ h('circle', { cx: 12, cy: 12, r: 8.4 }),
1681
+ h('path', { d: 'M3.6 12h16.8' }),
1682
+ h('path', { d: 'M12 3.6c2.6 2.4 2.6 14.4 0 16.8' }),
1683
+ h('path', { d: 'M12 3.6c-2.6 2.4-2.6 14.4 0 16.8' }))
1684
+ }
1685
+
1686
+ /**
1687
+ * The push card's chevron.
1688
+ * @param {number} size - Requested square edge.
1689
+ * @returns {any} The SVG element.
1690
+ */
1691
+ function MilocoPushChevronIcon(size) {
1692
+ return h('svg', {
1693
+ viewBox: '0 0 24 24',
1694
+ width: size,
1695
+ height: size,
1696
+ fill: 'none',
1697
+ stroke: 'currentColor',
1698
+ strokeWidth: 1.8,
1699
+ strokeLinecap: 'round',
1700
+ strokeLinejoin: 'round',
1701
+ 'aria-hidden': 'true',
1702
+ focusable: 'false',
1703
+ },
1704
+ h('path', { d: 'M6 9.5 12 15.5 18 9.5' }))
1705
+ }
1706
+
1707
+ /**
1708
+ * Build the 「Miloco 家庭助手」 main panel bound to one plugin context.
1709
+ * The panel is a read-only household overview: workspace path and state,
1710
+ * devices by room, recent events, and recent pushes. Session management
1711
+ * (pin/rename/archive) deliberately lives nowhere else.
1712
+ * @param {any} ctx - Browser plugin context.
1713
+ * @returns {Function} The panel component.
1714
+ */
1715
+ function createMilocoHomePanel(ctx) {
1716
+ function MilocoHomePanel() {
1717
+ const [overview, setOverview] = React.useState(null)
1718
+ const [error, setError] = React.useState(null)
1719
+ const [configOpen, setConfigOpen] = React.useState(false)
1720
+ const inFlight = React.useRef(false)
1721
+ const mounted = React.useRef(false)
1722
+
1723
+ const load = React.useCallback(() => {
1724
+ if (inFlight.current) return
1725
+ inFlight.current = true
1726
+ call('/home/overview')
1727
+ .then((payload) => {
1728
+ if (!mounted.current) return
1729
+ setOverview(payload)
1730
+ setError(null)
1731
+ })
1732
+ .catch((cause) => {
1733
+ if (!mounted.current) return
1734
+ setError(cause instanceof Error ? cause.message : String(cause))
1735
+ })
1736
+ .finally(() => {
1737
+ inFlight.current = false
1738
+ })
1739
+ }, [])
1740
+
1741
+ React.useEffect(() => {
1742
+ mounted.current = true
1743
+ load()
1744
+ const timer = window.setInterval(() => {
1745
+ if (document.visibilityState === 'visible') load()
1746
+ }, 60_000)
1747
+ return () => {
1748
+ mounted.current = false
1749
+ window.clearInterval(timer)
1750
+ }
1751
+ }, [load])
1752
+
1753
+ React.useEffect(() => {
1754
+ if (!configOpen) return undefined
1755
+ const onKeyDown = (event) => {
1756
+ if (event.key === 'Escape') setConfigOpen(false)
1757
+ }
1758
+ window.addEventListener('keydown', onKeyDown)
1759
+ return () => window.removeEventListener('keydown', onKeyDown)
1760
+ }, [configOpen])
1761
+
1762
+ // Host without `workspaceRegistry`: adopt the path through the public API
1763
+ // so the page still gets a workspace id. Idempotent for a known path.
1764
+ const adopting = React.useRef(false)
1765
+ React.useEffect(() => {
1766
+ const homeState = overview?.home
1767
+ if (!homeState?.path || homeState.workspaceId || adopting.current) return
1768
+ if (!ctx.workspaces || typeof ctx.workspaces.create !== 'function') return
1769
+ adopting.current = true
1770
+ ctx.workspaces.create({ path: homeState.path })
1771
+ .then(async (workspace) => {
1772
+ const workspaceId = workspace?.workspaceId ?? ''
1773
+ if (!workspaceId) return
1774
+ if (homeState.title && typeof ctx.workspaces.rename === 'function') {
1775
+ await ctx.workspaces.rename(workspaceId, homeState.title).catch(() => {})
1776
+ }
1777
+ if (typeof ctx.workspaces.insertBefore === 'function') {
1778
+ const list = ctx.workspaces.list
1779
+ const items = typeof list?.getSnapshot === 'function'
1780
+ ? (list.getSnapshot()?.items ?? [])
1781
+ : []
1782
+ const first = items.find(item => item?.workspaceId !== workspaceId)
1783
+ if (first) await ctx.workspaces.insertBefore(workspaceId, first.workspaceId).catch(() => {})
1784
+ }
1785
+ if (!mounted.current) return
1786
+ setOverview(previous => previous
1787
+ ? { ...previous, home: { ...previous.home, workspaceId } }
1788
+ : previous)
1789
+ })
1790
+ .catch((cause) => {
1791
+ if (mounted.current) {
1792
+ setError(`无法注册家庭工作区:${cause instanceof Error ? cause.message : String(cause)}`)
1793
+ }
1794
+ })
1795
+ .finally(() => { adopting.current = false })
1796
+ }, [overview?.home?.path, overview?.home?.workspaceId, overview?.home?.title])
1797
+
1798
+ const home = overview?.home ?? null
1799
+ const status = overview?.status ?? null
1800
+ const version = overview?.version ?? null
1801
+ const devices = overview?.devices ?? null
1802
+ const events = overview?.events ?? null
1803
+ const pushes = overview?.pushes ?? null
1804
+ const push = overview?.push ?? null
1805
+ const stats = overview?.overview ?? null
1806
+
1807
+ const startSession = () => {
1808
+ if (!home?.workspaceId) return
1809
+ try {
1810
+ ctx.uiWorkspace.startSession(home.workspaceId, {
1811
+ agentPreset: home.agentPreset || AGENT_PRESET,
1812
+ })
1813
+ } catch (cause) {
1814
+ setError(cause instanceof Error ? cause.message : String(cause))
1815
+ }
1816
+ }
1817
+
1818
+ const openSession = (sessionId) => {
1819
+ if (typeof sessionId !== 'string' || !sessionId) return
1820
+ try {
1821
+ ctx.uiWorkspace.openSession(sessionId)
1822
+ } catch (cause) {
1823
+ setError(cause instanceof Error ? cause.message : String(cause))
1824
+ }
1825
+ }
1826
+
1827
+ const rooms = devices?.ok && Array.isArray(devices.rooms) ? devices.rooms : []
1828
+ const eventItems = events?.ok && Array.isArray(events.items) ? events.items : []
1829
+ const pushItems = pushes?.ok && Array.isArray(pushes.items) ? pushes.items.slice(0, 10) : []
1830
+
1831
+ const sectionError = text => h('div', { className: 'dsh-miloco-dash-error' },
1832
+ h('span', null, text || '读取失败'),
1833
+ h('button', { type: 'button', className: 'dsh-miloco-dash-link', onClick: load }, '重试'))
1834
+
1835
+ const chip = (key, tone, label, title) => h('span', {
1836
+ key,
1837
+ className: `dsh-miloco-dash-chip${tone ? ` ${tone}` : ''}`,
1838
+ title: title || undefined,
1839
+ },
1840
+ tone === 'ok' || tone === 'warn'
1841
+ ? h('i', { className: 'dsh-miloco-dash-chipdot', 'aria-hidden': 'true' })
1842
+ : null,
1843
+ h('span', null, label))
1844
+
1845
+ const stat = (key, label, value, title) => h('div', { className: 'dsh-miloco-dash-stat', key },
1846
+ h('span', { className: 'k' }, label),
1847
+ h('span', { className: 'v', title: title || undefined }, value))
1848
+
1849
+ const header = h('div', { className: 'dsh-miloco-dash-head' },
1850
+ h('div', { className: 'dsh-miloco-dash-head-main' },
1851
+ h('div', { className: 'dsh-miloco-dash-title' }, '家庭助手')),
1852
+ h('div', { className: 'dsh-miloco-dash-actions' },
1853
+ h('button', {
1854
+ type: 'button',
1855
+ className: 'primary',
1856
+ disabled: !home?.workspaceId,
1857
+ onClick: startSession,
1858
+ }, '新建家庭会话'),
1859
+ h('button', { type: 'button', onClick: () => setConfigOpen(true) }, '配置')))
1860
+
1861
+ if (!overview && !error) {
1862
+ return h('div', { className: 'dsh-miloco-dash' },
1863
+ header,
1864
+ h('div', { className: 'dsh-miloco-dash-loading' }, '正在读取家庭概览…'))
1865
+ }
1866
+
1867
+ return h('div', { className: 'dsh-miloco-dash' },
1868
+ header,
1869
+
1870
+ error ? sectionError(error) : null,
1871
+
1872
+ h('section', { className: 'dsh-miloco-dash-card dsh-miloco-dash-summary' },
1873
+ h('div', { className: 'dsh-miloco-dash-card-head' },
1874
+ h('div', { className: 'dsh-miloco-dash-card-title' },
1875
+ h('h3', null, '家庭概览'),
1876
+ status?.perceptionRunning
1877
+ ? h('span', { className: 'dsh-miloco-dash-meta' }, '感知运行中')
1878
+ : null)),
1879
+ h('div', { className: 'dsh-miloco-dash-chips' },
1880
+ chip('status', status?.reachable ? 'ok' : 'warn',
1881
+ status?.ok ? (status.reachable ? '已连接' : '未连接') : '状态不可用'),
1882
+ version?.current
1883
+ ? chip('version', '', `v${formatShortVersion(version.current)}`, version.current)
1884
+ : null,
1885
+ version?.hasUpdate ? chip('update', 'warn', '有更新') : null,
1886
+ status?.accountBound
1887
+ ? chip('account', '', '账号已绑定', status.account?.nickname || '')
1888
+ : chip('account', 'warn', '账号未绑定'),
1889
+ push
1890
+ ? chip('push', push.tokenSet ? 'ok' : 'warn',
1891
+ push.tokenSet ? '推送已启用' : '推送未配置', push.path)
1892
+ : null),
1893
+ h('div', { className: 'dsh-miloco-dash-stats' },
1894
+ stat('account', '米家账号',
1895
+ stats?.account?.nickname || (stats?.accountBound ? '已绑定' : '未绑定'),
1896
+ stats?.account?.nickname || ''),
1897
+ stat('devices', '设备', stats?.deviceCount ?? '—'),
1898
+ stat('cameras', '摄像头', stats?.cameraCount ?? '—'),
1899
+ stat('rules', '规则 / 任务', `${stats?.ruleCount ?? '—'} / ${stats?.taskCount ?? '—'}`),
1900
+ stat('engine', '感知引擎', stats?.perceptionRunning ? '运行中' : '未运行'),
1901
+ stat('sources', '感知源', stats?.perceptionSources ?? '—'),
1902
+ stat('tokenEvents', 'token 记录', stats?.tokenEvents ?? '—'),
1903
+ stat('tokenTotal', 'token 合计', stats?.tokenTotal ?? '—'))),
1904
+
1905
+ h('section', { className: 'dsh-miloco-dash-card' },
1906
+ h('div', { className: 'dsh-miloco-dash-card-head' },
1907
+ h('div', { className: 'dsh-miloco-dash-card-title' },
1908
+ h('h3', null, '设备与房间'),
1909
+ devices?.ok
1910
+ ? h('span', { className: 'dsh-miloco-dash-meta' },
1911
+ `${rooms.length} 个房间 · ${devices.total} 台设备`)
1912
+ : null),
1913
+ devices?.ok
1914
+ ? h('span', { className: 'dsh-miloco-dash-score' }, `${devices.online} 在线`)
1915
+ : null),
1916
+ devices?.ok
1917
+ ? (rooms.length > 0
1918
+ ? h('div', { className: 'dsh-miloco-dash-rooms' }, rooms.map(room => h('details', { className: 'dsh-miloco-dash-room', key: room.name },
1919
+ h('summary', { className: 'dsh-miloco-dash-room-head' },
1920
+ h('span', { className: 'dsh-miloco-dash-room-name' }, room.name),
1921
+ h('span', {
1922
+ className: `dsh-miloco-dash-room-count${room.online === room.total ? ' ok' : room.online === 0 ? ' off' : ''}`,
1923
+ }, `${room.online}/${room.total}`),
1924
+ h('span', { className: 'dsh-miloco-dash-room-bar', 'aria-hidden': 'true' },
1925
+ h('i', {
1926
+ style: { width: `${room.total ? Math.round((room.online / room.total) * 100) : 0}%` },
1927
+ })),
1928
+ h('span', { className: 'dsh-miloco-dash-chev', 'aria-hidden': 'true' }, '›')),
1929
+ h('ul', { className: 'dsh-miloco-dash-list' }, (room.devices ?? []).map((device, index) => h('li', {
1930
+ key: device.did || device.name || String(index),
1931
+ className: 'dsh-miloco-dash-row',
1932
+ },
1933
+ h('span', {
1934
+ className: `dsh-miloco-dash-devdot${device.online ? ' ok' : ''}`,
1935
+ 'aria-hidden': 'true',
1936
+ }),
1937
+ h('span', { className: 'dsh-miloco-dash-main', title: device.name || device.did || undefined }, device.name || device.did || '未命名设备'),
1938
+ device.online ? null : h('span', { className: 'dsh-miloco-dash-tag' }, '离线')))))))
1939
+ : h('div', { className: 'dsh-miloco-dash-empty' }, '没有设备'))
1940
+ : sectionError(devices?.error || '读取设备失败')),
1941
+
1942
+ h('div', { className: 'dsh-miloco-dash-grid' },
1943
+ h('section', { className: 'dsh-miloco-dash-card' },
1944
+ h('div', { className: 'dsh-miloco-dash-card-head' },
1945
+ h('div', { className: 'dsh-miloco-dash-card-title' },
1946
+ h('h3', null, '最近事件'),
1947
+ events?.ok && eventItems.length > 0
1948
+ ? h('span', { className: 'dsh-miloco-dash-meta' }, `${eventItems.length} 条`)
1949
+ : null)),
1950
+ events?.ok
1951
+ ? (eventItems.length > 0
1952
+ ? h('ul', { className: 'dsh-miloco-dash-list' }, eventItems.map((item, index) => {
1953
+ const summary = eventSummary(item)
1954
+ const source = eventSource(item)
1955
+ const priority = eventPriority(item)
1956
+ return h('li', {
1957
+ key: `${eventKind(item)}-${index}`,
1958
+ className: 'dsh-miloco-dash-row',
1959
+ },
1960
+ h('span', {
1961
+ className: 'dsh-miloco-dash-main',
1962
+ title: summary && source ? `${source} · ${summary}` : summary || source || undefined,
1963
+ }, summary || eventKind(item)),
1964
+ priority ? h('span', { className: 'dsh-miloco-dash-tag' }, priority) : null,
1965
+ h('span', { className: 'dsh-miloco-dash-meta' }, eventClock(item) || '—'))
1966
+ }))
1967
+ : h('div', { className: 'dsh-miloco-dash-empty' }, '最近没有事件'))
1968
+ : sectionError(events?.error || '读取事件失败')),
1969
+
1970
+ h('section', { className: 'dsh-miloco-dash-card' },
1971
+ h('div', { className: 'dsh-miloco-dash-card-head' },
1972
+ h('div', { className: 'dsh-miloco-dash-card-title' },
1973
+ h('h3', null, '推送记录'),
1974
+ pushItems.length > 0
1975
+ ? h('span', { className: 'dsh-miloco-dash-meta' }, `${pushItems.length} 条`)
1976
+ : null),
1977
+ home?.archive?.exists
1978
+ ? h('button', {
1979
+ type: 'button',
1980
+ className: 'dsh-miloco-dash-link dsh-miloco-dash-meta',
1981
+ onClick: () => openSession(home.archive.sessionId),
1982
+ }, '打开归档会话')
1983
+ : null),
1984
+ pushes?.ok
1985
+ ? (pushItems.length > 0
1986
+ ? h('ul', { className: 'dsh-miloco-dash-list' }, pushItems.map((item, index) => h('li', {
1987
+ key: item.runId || String(index),
1988
+ className: 'dsh-miloco-dash-row',
1989
+ },
1990
+ h('button', {
1991
+ type: 'button',
1992
+ className: 'dsh-miloco-dash-link dsh-miloco-dash-main',
1993
+ onClick: () => openSession(item.sessionId),
1994
+ }, item.title || item.summary || '推送'),
1995
+ h('span', { className: 'dsh-miloco-dash-meta' }, `${item.at ? formatMessageClock(item.at) : '—'}${item.status ? ` · ${item.status}` : ''}`))))
1996
+ : h('div', { className: 'dsh-miloco-dash-empty' }, '还没有推送记录'))
1997
+ : sectionError(pushes?.error || '读取推送失败'))),
1998
+
1999
+ configOpen
2000
+ ? h('div', {
2001
+ className: 'dsh-miloco-dash-config-backdrop',
2002
+ onClick: (event) => {
2003
+ if (event.target === event.currentTarget) setConfigOpen(false)
2004
+ },
2005
+ },
2006
+ h('div', { className: 'dsh-miloco-dash-config' },
2007
+ h('div', { className: 'dsh-miloco-dash-config-head' },
2008
+ h('h3', null, 'Miloco 配置'),
2009
+ h('button', {
2010
+ type: 'button',
2011
+ className: 'dsh-miloco-dash-config-close',
2012
+ onClick: () => setConfigOpen(false),
2013
+ 'aria-label': '关闭配置',
2014
+ }, '×')),
2015
+ h(MilocoSettingsTab)))
2016
+ : null)
2017
+ }
2018
+
2019
+ return MilocoHomePanel
2020
+ }
2021
+
2022
+ /**
2023
+ * The conversation Definition that claims an inbound Miloco push.
2024
+ *
2025
+ * `exclusive: true` claims the event on the `chat` target, so the
2026
+ * built-in non-exclusive `input-message` Definition never sees it and the
2027
+ * push is rendered exactly once, by our own renderer.
2028
+ */
2029
+ const milocoPushDefinition = {
2030
+ kind: PUSH_SOURCE_KIND,
2031
+ target: 'chat',
2032
+ match: (event) => {
2033
+ if (event?.type !== 'user/message') return null
2034
+ const source = event.data?.source
2035
+ if (source?.kind !== PUSH_SOURCE_KIND) return null
2036
+ return { id: String(event.data.id), role: 'start', exclusive: true }
2037
+ },
2038
+ start: (_context, match) => {
2039
+ const event = match.event
2040
+ const content = contentText(event.data?.content)
2041
+ const summary = typeof event.data?.source?.summary === 'string' ? event.data.source.summary : ''
2042
+ return {
2043
+ seq: event.seq,
2044
+ time: event.time,
2045
+ title: summary || firstLine(content) || 'Miloco 推送',
2046
+ summary,
2047
+ content,
2048
+ }
2049
+ },
2050
+ update: context => context.state,
2051
+ buildViewNode: (context) => {
2052
+ const state = context.state
2053
+ if (!state) return null
2054
+ const location = context.start?.location
2055
+ ?? context.matches[0]?.location
2056
+ ?? { kind: 'unresolved' }
2057
+ return {
2058
+ key: context.key,
2059
+ kind: PUSH_SOURCE_KIND,
2060
+ id: context.id,
2061
+ target: 'chat',
2062
+ anchorSeq: state.seq,
2063
+ location,
2064
+ visibility: 'visible',
2065
+ groupTree: 'input',
2066
+ data: {
2067
+ title: state.title,
2068
+ summary: state.summary,
2069
+ time: state.time,
2070
+ content: state.content,
2071
+ },
2072
+ }
2073
+ },
2074
+ }
2075
+
2076
+ /**
2077
+ * The collapsed push card. Its look copies DSH's scheduled-task trigger
2078
+ * notice: a rounded card with an icon, the push title, a local clock, and
2079
+ * a chevron; the detail body shows only after expanding.
2080
+ * @param {{node?: any}} props - The keyed Chat node renderer share.
2081
+ * @returns {any} The card element.
2082
+ */
2083
+ function MilocoPushNodeView(props) {
2084
+ const data = props?.node?.data ?? {}
2085
+ const [open, setOpen] = React.useState(false)
2086
+ const bodyId = React.useMemo(() => {
2087
+ pushBodySeq += 1
2088
+ return `dsh-miloco-push-body-${pushBodySeq}`
2089
+ }, [])
2090
+ const time = formatMessageClock(data.time)
2091
+ let iso = ''
2092
+ try {
2093
+ const date = data.time ? new Date(data.time) : null
2094
+ iso = date && !Number.isNaN(date.getTime()) ? date.toISOString() : ''
2095
+ } catch {
2096
+ iso = ''
2097
+ }
2098
+ return h('section', { className: 'dsh-miloco-push-root', 'data-miloco-push': '' },
2099
+ h('button', {
2100
+ className: 'dsh-miloco-push-header',
2101
+ type: 'button',
2102
+ 'aria-expanded': open,
2103
+ 'aria-controls': bodyId,
2104
+ onClick: () => setOpen(!open),
2105
+ },
2106
+ h('span', { className: 'dsh-miloco-push-icon', 'aria-hidden': 'true' }, MilocoPushSourceIcon(14)),
2107
+ h('span', { className: 'dsh-miloco-push-title' }, data.title || 'Miloco 推送'),
2108
+ time
2109
+ ? h('time', { className: 'dsh-miloco-push-time', dateTime: iso }, time)
2110
+ : null,
2111
+ h('span', {
2112
+ className: `dsh-miloco-push-chevron${open ? ' open' : ''}`,
2113
+ 'aria-hidden': 'true',
2114
+ }, MilocoPushChevronIcon(12))),
2115
+ open
2116
+ ? h('div', { id: bodyId, className: 'dsh-miloco-push-body' },
2117
+ h('p', { className: 'dsh-miloco-push-explanation' }, '这条通知触发了本轮回复。'),
2118
+ h('div', { className: 'dsh-miloco-push-content' }, data.content || ''))
2119
+ : null)
2120
+ }
2121
+
1229
2122
  function apply(ctx) {
1230
2123
  ensureStyles()
1231
2124
  installPresetGuide(ctx)
1232
- ctx.slots.inject('settings.plugins.tab', () => ctx.slots.register({
1233
- name: 'settings.plugins.tab',
1234
- id: TAB_ID,
1235
- order: 20,
1236
- label: 'Miloco',
1237
- }, MilocoSettingsTab))
2125
+
2126
+ // 配置入口在家庭助手页面(新建家庭会话右侧的「配置」按钮),不再注册
2127
+ // 内置「设置 → 插件」标签页。
2128
+ // 侧栏入口:面板列表的最后一行,正好在「更多」按钮的上一行。
2129
+ ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
2130
+ name: 'sidebar.panellist',
2131
+ id: PANEL_ID,
2132
+ order: 1000,
2133
+ label: () => 'Miloco 家庭助手',
2134
+ }, MilocoPanelIcon))
2135
+
2136
+ // 同名 main 面板:没有它,点击侧栏行会抛错。
2137
+ ctx.slots.inject('main', () => ctx.slots.register({
2138
+ name: 'main',
2139
+ key: PANEL_ID,
2140
+ }, createMilocoHomePanel(ctx)))
2141
+
2142
+ // 折叠推送卡片:先认领事件,再渲染它。
2143
+ // 注册随本 fiber 的生命周期结束,卸载后不会留下重复的 kind。
2144
+ ctx.effect(() => {
2145
+ let dispose = null
2146
+ try {
2147
+ dispose = ctx.uiConversation.events.register(milocoPushDefinition)
2148
+ } catch {
2149
+ // 上一个实例已经占用这个 kind;沿用它即可。
2150
+ dispose = null
2151
+ }
2152
+ return () => { if (dispose) dispose() }
2153
+ }, 'dsh-miloco: push conversation definition')
2154
+ ctx.slots.inject('conversation.chat.node', () => ctx.slots.register({
2155
+ name: 'conversation.chat.node',
2156
+ key: PUSH_SOURCE_KIND,
2157
+ }, MilocoPushNodeView))
1238
2158
  }
1239
2159
 
1240
2160
  exports.apply = apply
1241
2161
  exports.inject = inject
1242
2162
  exports.MilocoSettingsTab = MilocoSettingsTab
2163
+ exports.MilocoPanelIcon = MilocoPanelIcon
2164
+ exports.MilocoPushNodeView = MilocoPushNodeView
2165
+ exports.createMilocoHomePanel = createMilocoHomePanel
2166
+ exports.formatMessageClock = formatMessageClock
2167
+ exports.milocoPushDefinition = milocoPushDefinition
1243
2168
  exports.modelOptions = modelOptions
1244
2169
  exports.sessionOptions = sessionOptions
1245
2170
  exports.effortOptions = effortOptions