@binblink/wasmnest-plugin-sdk-mobile 0.0.0-stage → 0.0.1

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.
Files changed (114) hide show
  1. package/README.md +363 -2
  2. package/README_en.md +353 -0
  3. package/bin/build.js +269 -0
  4. package/bin/cli.js +96 -0
  5. package/bin/lib.js +349 -0
  6. package/bin/manifest-gen.js +324 -0
  7. package/bin/package.js +150 -0
  8. package/bin/scaffold.js +285 -0
  9. package/dev-shell/README.md +151 -0
  10. package/dev-shell/index.html +27 -0
  11. package/dev-shell/package.json +27 -0
  12. package/dev-shell/pnpm-workspace.yaml +9 -0
  13. package/dev-shell/postcss.config.js +6 -0
  14. package/dev-shell/src/App.vue +90 -0
  15. package/dev-shell/src/apps/mock-terminal/MockTerminalSlotCard.vue +51 -0
  16. package/dev-shell/src/apps/mock-terminal/MockTerminalView.vue +168 -0
  17. package/dev-shell/src/apps/mock-terminal/index.ts +50 -0
  18. package/dev-shell/src/components/DevToolbar.vue +100 -0
  19. package/dev-shell/src/components/DialogHost.vue +119 -0
  20. package/dev-shell/src/components/LogPanel.vue +80 -0
  21. package/dev-shell/src/i18n.ts +22 -0
  22. package/dev-shell/src/loader.ts +239 -0
  23. package/dev-shell/src/locale.ts +29 -0
  24. package/dev-shell/src/locales/en-shell.ts +328 -0
  25. package/dev-shell/src/locales/index.ts +70 -0
  26. package/dev-shell/src/locales/shell.ts +328 -0
  27. package/dev-shell/src/main.ts +58 -0
  28. package/dev-shell/src/mock/dialog-service.ts +58 -0
  29. package/dev-shell/src/mock/file-transfer.ts +390 -0
  30. package/dev-shell/src/mock/mobile-api.ts +369 -0
  31. package/dev-shell/src/mock/preset-tasks.ts +186 -0
  32. package/dev-shell/src/mock/session.ts +121 -0
  33. package/dev-shell/src/mock-context.ts +351 -0
  34. package/dev-shell/src/registry.ts +151 -0
  35. package/dev-shell/src/shared-runtime.ts +44 -0
  36. package/dev-shell/src/shell/ShellView.vue +52 -0
  37. package/dev-shell/src/shell/adapters/devAppSource.ts +155 -0
  38. package/dev-shell/src/shell/components/PluginContextHost.vue +40 -0
  39. package/dev-shell/src/shell/components/ShellAppCell.vue +75 -0
  40. package/dev-shell/src/shell/components/ShellAppIcon.vue +111 -0
  41. package/dev-shell/src/shell/components/ShellAppSurface.vue +46 -0
  42. package/dev-shell/src/shell/components/ShellBrandTile.vue +27 -0
  43. package/dev-shell/src/shell/components/ShellCapsuleMenu.vue +123 -0
  44. package/dev-shell/src/shell/components/ShellChip.vue +49 -0
  45. package/dev-shell/src/shell/components/ShellGroupCard.vue +17 -0
  46. package/dev-shell/src/shell/components/ShellHost.vue +75 -0
  47. package/dev-shell/src/shell/components/ShellNotificationShade.vue +496 -0
  48. package/dev-shell/src/shell/components/ShellPermissionPrompt.vue +127 -0
  49. package/dev-shell/src/shell/components/ShellPlaceholderSurface.vue +33 -0
  50. package/dev-shell/src/shell/components/ShellSection.vue +27 -0
  51. package/dev-shell/src/shell/components/ShellSheet.vue +74 -0
  52. package/dev-shell/src/shell/components/ShellStatusbar.vue +84 -0
  53. package/dev-shell/src/shell/components/screens/PluginRoutePage.vue +98 -0
  54. package/dev-shell/src/shell/components/screens/ShellAppDetailScreen.vue +378 -0
  55. package/dev-shell/src/shell/components/screens/ShellAppRunScreen.vue +90 -0
  56. package/dev-shell/src/shell/components/screens/ShellAppsScreen.vue +195 -0
  57. package/dev-shell/src/shell/components/screens/ShellHomeScreen.vue +316 -0
  58. package/dev-shell/src/shell/components/screens/ShellPermissionsScreen.vue +376 -0
  59. package/dev-shell/src/shell/components/screens/ShellSettingsScreen.vue +240 -0
  60. package/dev-shell/src/shell/components/screens/ShellSwitcherScreen.vue +559 -0
  61. package/dev-shell/src/shell/components/ui/ConfirmDialog.vue +137 -0
  62. package/dev-shell/src/shell/components/ui/Toggle.vue +53 -0
  63. package/dev-shell/src/shell/composables/useShellApps.ts +415 -0
  64. package/dev-shell/src/shell/composables/useShellNavigation.ts +197 -0
  65. package/dev-shell/src/shell/composables/useShellNotifications.ts +115 -0
  66. package/dev-shell/src/shell/composables/useShellOpen.ts +35 -0
  67. package/dev-shell/src/shell/composables/useShellOverlays.ts +101 -0
  68. package/dev-shell/src/shell/composables/useShellPreferences.ts +143 -0
  69. package/dev-shell/src/shell/composables/useShellRecent.ts +84 -0
  70. package/dev-shell/src/shell/composables/useToast.ts +33 -0
  71. package/dev-shell/src/shell/index.ts +88 -0
  72. package/dev-shell/src/shell/logger.ts +45 -0
  73. package/dev-shell/src/shell/permissions.ts +145 -0
  74. package/dev-shell/src/shell/registry.ts +220 -0
  75. package/dev-shell/src/shell/styles/shell.css +841 -0
  76. package/dev-shell/src/shell/types.ts +137 -0
  77. package/dev-shell/src/shell/utils.ts +54 -0
  78. package/dev-shell/src/styles/main.css +153 -0
  79. package/dev-shell/src/styles/mobile.css +884 -0
  80. package/dev-shell/src/styles/wallpaper.css +130 -0
  81. package/dev-shell/src/theme.ts +71 -0
  82. package/dev-shell/src/vite-env.d.ts +20 -0
  83. package/dev-shell/tailwind.config.js +101 -0
  84. package/dev-shell/tsconfig.json +16 -0
  85. package/dev-shell/vite.config.ts +130 -0
  86. package/dist/index.d.ts +84 -0
  87. package/dist/index.js +161 -0
  88. package/dist/types.d.ts +637 -0
  89. package/dist/types.js +0 -0
  90. package/dist/vite-plugin.d.ts +16 -0
  91. package/dist/vite-plugin.js +95 -0
  92. package/package.json +80 -3
  93. package/src/constants.ts +6 -0
  94. package/src/global-dialog.ts +168 -0
  95. package/src/index.ts +87 -0
  96. package/src/runtime.ts +44 -0
  97. package/src/types.ts +715 -0
  98. package/src/ui/PluginGlobalDialog.vue +388 -0
  99. package/src/ui/Select.d.ts +37 -0
  100. package/src/ui/Select.vue +307 -0
  101. package/src/ui/plugin-global-dialog.d.ts +11 -0
  102. package/src/ui/useSwipeTabs.d.ts +20 -0
  103. package/src/ui/useSwipeTabs.ts +53 -0
  104. package/src/vite-plugin.ts +132 -0
  105. package/template/.gitignore +4 -0
  106. package/template/README.md +46 -0
  107. package/template/package.json +23 -0
  108. package/template/plugin.json +13 -0
  109. package/template/rust/Cargo.toml +19 -0
  110. package/template/rust/src/lib.rs +33 -0
  111. package/template/src/env.d.ts +24 -0
  112. package/template/src/index.ts +37 -0
  113. package/template/tsconfig.json +17 -0
  114. package/template/vite.config.ts +21 -0
@@ -0,0 +1,33 @@
1
+ /**
2
+ * 壳内 Toast 反馈(与宿主 shell/composables/useToast.ts 同款 API)
3
+ * -----------------------------------------------------------------------------
4
+ * 宿主用 vue-sonner 渲染 toast,dev-shell 没有该依赖,且已有自己的对话框宿主
5
+ * (`mock/dialog-service.ts` + `components/DialogHost.vue`)在渲染同款队列与 toast。
6
+ * 因此这里只做一层薄适配:壳内组件用宿主同款 API 调用,落点换成 dev-shell 的
7
+ * DialogHost——不为预览环境引入第二套 toast 实现。
8
+ *
9
+ * API 形状(show / success / error / warning / info / dismiss)保持一致,便于
10
+ * 「同款代码在两边跑」。
11
+ */
12
+
13
+ import { dialogService } from '../../mock/dialog-service'
14
+
15
+ /** Toast 选项(与宿主 useToast 的 ToastOptions 同形) */
16
+ export interface ToastOptions {
17
+ message: string
18
+ type?: 'success' | 'error' | 'warning' | 'info'
19
+ }
20
+
21
+ export function useToast() {
22
+ function show(options: ToastOptions) {
23
+ dialogService.showToast(options.message, options.type ?? 'info')
24
+ }
25
+
26
+ return {
27
+ show,
28
+ success: (message: string) => show({ message, type: 'success' }),
29
+ error: (message: string) => show({ message, type: 'error' }),
30
+ warning: (message: string) => show({ message, type: 'warning' }),
31
+ info: (message: string) => show({ message, type: 'info' }),
32
+ }
33
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Dev Shell 宿主壳公共入口
3
+ * -----------------------------------------------------------------------------
4
+ * 对外暴露三类东西:契约类型、注册表、以及给内置应用 / 调试代码用的注册 API。
5
+ *
6
+ * 内置应用接入只需这里的注册 API:
7
+ *
8
+ * ```ts
9
+ * import { registerSurface, registerSlot } from './shell'
10
+ *
11
+ * registerSurface('dev.my-app', { component: MyAppView }) // 运行面
12
+ * registerSlot('dev.my-app', { id: 'status', component: MyCard }) // 首页快捷卡片
13
+ * ```
14
+ *
15
+ * 被调试插件走的是 `context.ui.*`(mock-context 内的同款实现),appId 由插件 id 代填。
16
+ *
17
+ * 返回值都是 Disposable:应用停用时 dispose 即摘除,不留残桩。
18
+ */
19
+
20
+ export type {
21
+ Disposable,
22
+ ShellApp,
23
+ ShellAppState,
24
+ ShellAppContributions,
25
+ ShellAppSource,
26
+ ShellCapsuleItem,
27
+ ShellPermissionGrant,
28
+ ShellSlotContribution,
29
+ ShellSurfaceContribution,
30
+ } from './types'
31
+
32
+ export { getShellRegistry, type ShellRegistryClass } from './registry'
33
+ export {
34
+ SHELL_PERMISSION_GROUPS,
35
+ isLockedPermission,
36
+ permissionTitleKey,
37
+ groupPermissions,
38
+ } from './permissions'
39
+ export { formatBytes, initialOf, iconKindOf, type IconKind } from './utils'
40
+ export { logger } from './logger'
41
+
42
+ export { useShellApps, type ShellApps, type ShellStats } from './composables/useShellApps'
43
+ export {
44
+ useShellNavigation,
45
+ resetShellNavigation,
46
+ type ShellNavigation,
47
+ type ShellScreenId,
48
+ type ShellScreenEntry,
49
+ type ShellNavDirection,
50
+ } from './composables/useShellNavigation'
51
+ export {
52
+ useShellOverlays,
53
+ type ShellOverlays,
54
+ type ShellPermissionRequest,
55
+ } from './composables/useShellOverlays'
56
+ export { useShellRecent, pushRecent, clearRecent } from './composables/useShellRecent'
57
+ export { useShellOpen, openShellApp } from './composables/useShellOpen'
58
+ export { useToast, type ToastOptions } from './composables/useToast'
59
+
60
+ export {
61
+ createDevAppSource,
62
+ DEV_APP_SOURCE_ID,
63
+ toShellApp,
64
+ type DevAppSourceDeps,
65
+ } from './adapters/devAppSource'
66
+
67
+ import type {
68
+ Disposable,
69
+ ShellCapsuleItem,
70
+ ShellSlotContribution,
71
+ ShellSurfaceContribution,
72
+ } from './types'
73
+ import { getShellRegistry } from './registry'
74
+
75
+ /** 注册应用运行面 */
76
+ export function registerSurface(appId: string, surface: ShellSurfaceContribution): Disposable {
77
+ return getShellRegistry().registerSurface(appId, surface)
78
+ }
79
+
80
+ /** 注册首页快捷卡片 */
81
+ export function registerSlot(appId: string, slot: ShellSlotContribution): Disposable {
82
+ return getShellRegistry().registerSlot(appId, slot)
83
+ }
84
+
85
+ /** 注册胶囊菜单附加项 */
86
+ export function registerCapsuleItem(appId: string, item: ShellCapsuleItem): Disposable {
87
+ return getShellRegistry().registerCapsuleItem(appId, item)
88
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 壳层日志桥
3
+ * -----------------------------------------------------------------------------
4
+ * dev-shell 的壳复用宿主的 `src/utils/frontendLogger` 同款职责(带上下文的统一日志),
5
+ * 但落到 dev-shell 自己的日志通道——即 `registry.ts` 的 `pushLog`,它同时写
6
+ * 浏览器 console 与右下角日志面板。
7
+ *
8
+ * 为什么不直接用 console:壳内出问题时,用户的第一现场是那个日志面板;
9
+ * 只进 console 等于把最需要被看见的错误藏起来。
10
+ *
11
+ * 与宿主 frontendLogger 的差异:宿主的实参是 (message, error?),这里同样接受可选
12
+ * 错误对象并拼进 message(日志面板只显示一条 message 字段)——这样从宿主移植过来的
13
+ * 两参调用无需逐个改写,且错误细节不丢。
14
+ */
15
+
16
+ import { pushLog } from '../registry'
17
+
18
+ /** 壳层日志级别(与 DevLogEntry 对齐) */
19
+ type Level = 'debug' | 'info' | 'warn' | 'error'
20
+
21
+ /** 稳定低熵的错误折叠:Error 取 message,其他取 String,避免大对象污染日志 */
22
+ function foldError(error: unknown): string {
23
+ if (error === undefined) return ''
24
+ if (error instanceof Error) return error.message
25
+ return String(error)
26
+ }
27
+
28
+ /**
29
+ * 壳层 logger
30
+ *
31
+ * `scope` 是壳自身的固定标识(`Shell`),插件侧日志仍走 `context.logger`,
32
+ * 两条通道在日志面板里可区分。
33
+ */
34
+ export const logger = {
35
+ debug: (message: string, error?: unknown) =>
36
+ pushLog('debug', 'Shell', error === undefined ? message : `${message} :: ${foldError(error)}`),
37
+ info: (message: string, error?: unknown) =>
38
+ pushLog('info', 'Shell', error === undefined ? message : `${message} :: ${foldError(error)}`),
39
+ warn: (message: string, error?: unknown) =>
40
+ pushLog('warn', 'Shell', error === undefined ? message : `${message} :: ${foldError(error)}`),
41
+ error: (message: string, error?: unknown) =>
42
+ pushLog('error', 'Shell', error === undefined ? message : `${message} :: ${foldError(error)}`),
43
+ } satisfies Record<Level, (message: string, error?: unknown) => void>
44
+
45
+ export type { Level }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Dev Shell 权限展示元数据(与宿主 shell/permissions.ts 逐字同构)
3
+ * -----------------------------------------------------------------------------
4
+ * 权限词汇显示层两件事:
5
+ * 1. 把权限词映射成 i18n 文案 key(dev-shell 自有 `shell.permission.*` 命名空间)
6
+ * 2. 按能力域分组,让「应用详情 → 权限」是可读清单而不是一串冒号字符串
7
+ *
8
+ * 授权裁决在插件侧 / 宿主后端执行,前端开关只是 UX(docs/security.md §1)——因此这里
9
+ * 的分组与文案不参与任何安全判定。
10
+ *
11
+ * 同构纪律:词表与分组与宿主 `wasmnest-mobile/src/shell/permissions.ts` 保持一致;
12
+ * 新增权限词时两端要同步补词表与双语 locale(缺一条的代价是「权限总览」页直接
13
+ * 裸显英文 code)。
14
+ */
15
+
16
+ import type { ShellPermissionGrant } from './types'
17
+
18
+ /** 权限分组定义(展示顺序即数组顺序) */
19
+ export interface ShellPermissionGroup {
20
+ id: string
21
+ /** 分组标题的 i18n key */
22
+ titleKey: string
23
+ keys: string[]
24
+ }
25
+
26
+ /** 权限词 → 本地化文案 key 的后缀(前缀统一 shell.permission.perm.;
27
+ * terminal:input 已随票 15 阶段 B 整面退役,不在展示词表) */
28
+ const PERMISSION_I18N_KEY: Record<string, string> = {
29
+ storage: 'storage',
30
+ 'terminal:output': 'terminalOutput',
31
+ 'session:read': 'sessionRead',
32
+ 'session:write': 'sessionWrite',
33
+ 'ui:toolbox': 'uiToolbox',
34
+ 'ui:navtab': 'uiNavtab',
35
+ 'ui:settings': 'uiSettings',
36
+ 'ui:input': 'uiInput',
37
+ 'ui:route': 'uiRoute',
38
+ 'ui:dialog': 'uiDialog',
39
+ 'ui:back': 'uiBack',
40
+ 'network:http': 'networkHttp',
41
+ 'fs:read': 'fsRead',
42
+ 'fs:write': 'fsWrite',
43
+ 'fs:pick': 'fsPick',
44
+ fileservice: 'fileservice',
45
+ 'system:open': 'systemOpen',
46
+ bus: 'bus',
47
+ broadcast: 'broadcast',
48
+ transfer: 'transfer',
49
+ mdns: 'mdns',
50
+ peer: 'peer',
51
+ 'ws:client': 'wsClient',
52
+ auth: 'auth',
53
+ notify: 'notify',
54
+ 'metrics:read': 'metricsRead',
55
+ }
56
+
57
+ /** 能力域分组:与移动端权限词汇的能力域划分一致 */
58
+ export const SHELL_PERMISSION_GROUPS: ShellPermissionGroup[] = [
59
+ {
60
+ id: 'terminal',
61
+ titleKey: 'shell.permission.group.terminal',
62
+ keys: ['terminal:output', 'session:read', 'session:write'],
63
+ },
64
+ {
65
+ id: 'data',
66
+ titleKey: 'shell.permission.group.data',
67
+ keys: ['fs:read', 'fs:write', 'fs:pick', 'fileservice', 'transfer', 'network:http'],
68
+ },
69
+ {
70
+ id: 'device',
71
+ titleKey: 'shell.permission.group.device',
72
+ keys: ['peer', 'mdns', 'ws:client', 'auth', 'notify'],
73
+ },
74
+ {
75
+ id: 'system',
76
+ titleKey: 'shell.permission.group.system',
77
+ keys: ['metrics:read'],
78
+ },
79
+ {
80
+ id: 'interface',
81
+ titleKey: 'shell.permission.group.interface',
82
+ keys: [
83
+ 'ui:toolbox',
84
+ 'ui:navtab',
85
+ 'ui:settings',
86
+ 'ui:input',
87
+ 'ui:route',
88
+ 'ui:dialog',
89
+ 'ui:back',
90
+ 'bus',
91
+ 'broadcast',
92
+ 'system:open',
93
+ 'storage',
94
+ ],
95
+ },
96
+ ]
97
+
98
+ /**
99
+ * 默认授予且不可关闭的权限。
100
+ * storage 是插件底座能力(私有库 / KV),关掉等于让应用无法工作,因此不给开关。
101
+ */
102
+ export const SHELL_LOCKED_PERMISSIONS: ReadonlySet<string> = new Set(['storage'])
103
+
104
+ /** 权限标题的 i18n key(未知权限返回 undefined,由调用方回退原始权限词) */
105
+ export function permissionTitleKey(key: string): string | undefined {
106
+ const suffix = PERMISSION_I18N_KEY[key]
107
+ return suffix ? `shell.permission.perm.${suffix}.title` : undefined
108
+ }
109
+
110
+ /** 权限说明的 i18n key(未知权限返回 undefined) */
111
+ export function permissionDescKey(key: string): string | undefined {
112
+ const suffix = PERMISSION_I18N_KEY[key]
113
+ return suffix ? `shell.permission.perm.${suffix}.desc` : undefined
114
+ }
115
+
116
+ /** 权限是否默认授予且不可关闭 */
117
+ export function isLockedPermission(key: string): boolean {
118
+ return SHELL_LOCKED_PERMISSIONS.has(key)
119
+ }
120
+
121
+ /**
122
+ * 按能力域分组权限授予项
123
+ *
124
+ * 分组外的权限(新增词表尚未归类)归入末尾的「其他」组,保证新权限不会因
125
+ * 未归类而从 UI 上消失——消失比归类难看更危险。
126
+ */
127
+ export function groupPermissions(
128
+ grants: ShellPermissionGrant[],
129
+ fallbackTitleKey: string,
130
+ ): Array<ShellPermissionGroup & { items: ShellPermissionGrant[] }> {
131
+ const grouped = SHELL_PERMISSION_GROUPS.map((g) => ({ ...g, items: [] as ShellPermissionGrant[] }))
132
+ const rest: ShellPermissionGrant[] = []
133
+
134
+ for (const grant of grants) {
135
+ const target = grouped.find((g) => g.keys.includes(grant.key))
136
+ if (target) target.items.push(grant)
137
+ else rest.push(grant)
138
+ }
139
+
140
+ const nonEmpty = grouped.filter((g) => g.items.length > 0)
141
+ if (rest.length > 0) {
142
+ nonEmpty.push({ id: 'other', titleKey: fallbackTitleKey, keys: [], items: rest })
143
+ }
144
+ return nonEmpty
145
+ }
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Dev Shell 宿主壳注册表
3
+ * -----------------------------------------------------------------------------
4
+ * 结构与宿主 `wasmnest-mobile/src/shell/registry.ts` 同构(只做注册/撤销/查询,
5
+ * 不做 UI 决策、不感知数据来源)。
6
+ *
7
+ * 与宿主的唯一差异是数据源:宿主的数据源是插件系统(真后端),dev-shell 的数据源
8
+ * 是 `adapters/devAppSource.ts`(把被调试插件记录投影成应用)。壳内组件对两者无感——
9
+ * 这正是这套分层要保证的事:换真源只换数据源实现。
10
+ *
11
+ * 同构纪律:改这里之前先看宿主那份,两边结构应保持一致;字段级漂移由
12
+ * `__tests__/shell/contractDrift.test.ts` 兜底。
13
+ */
14
+
15
+ import { ref, markRaw, type Ref } from 'vue'
16
+ import type {
17
+ Disposable,
18
+ ShellApp,
19
+ ShellAppSource,
20
+ ShellCapsuleItem,
21
+ ShellSlotContribution,
22
+ ShellSurfaceContribution,
23
+ } from './types'
24
+
25
+ /** 单个应用的增量贡献集合(键为贡献项 id,重复注册即覆盖) */
26
+ interface ContributionStore {
27
+ slots: Map<string, ShellSlotContribution>
28
+ capsuleItems: Map<string, ShellCapsuleItem>
29
+ surface?: ShellSurfaceContribution
30
+ }
31
+
32
+ /** 应用记录归属:哪个数据源写入的,清理时按源整体回收 */
33
+ interface AppRecord {
34
+ sourceId: string
35
+ app: ShellApp
36
+ }
37
+
38
+ function emptyContributions(): ContributionStore {
39
+ return { slots: new Map(), capsuleItems: new Map() }
40
+ }
41
+
42
+ /** 宿主壳注册表 */
43
+ class ShellRegistryClass {
44
+ private readonly apps = new Map<string, AppRecord>()
45
+ private readonly sources = new Map<string, ShellAppSource>()
46
+ private readonly contributions = new Map<string, ContributionStore>()
47
+
48
+ /** 响应式应用清单(供壳内组件消费) */
49
+ readonly appsRef: Ref<ShellApp[]> = ref([])
50
+
51
+ // ==================== 数据源 ====================
52
+
53
+ /** 注册应用数据源;重复注册同一 id 覆盖旧源 */
54
+ registerSource(source: ShellAppSource): Disposable {
55
+ this.sources.set(source.id, source)
56
+ return {
57
+ dispose: () => {
58
+ this.sources.delete(source.id)
59
+ },
60
+ }
61
+ }
62
+
63
+ /** 已注册的数据源(按注册顺序) */
64
+ listSources(): ShellAppSource[] {
65
+ return [...this.sources.values()]
66
+ }
67
+
68
+ // ==================== 应用清单 ====================
69
+
70
+ /**
71
+ * 全量覆盖某数据源的应用清单
72
+ *
73
+ * 清单自带的 contributions 会并入增量贡献表(同 id 覆盖),因此「清单下发」与
74
+ * 「运行时注册」两条路径最终合并为同一份视图。
75
+ */
76
+ upsertApps(sourceId: string, apps: ShellApp[]): void {
77
+ for (const app of apps) {
78
+ this.apps.set(app.id, { sourceId, app })
79
+ this.mergeContributions(app.id, app.contributions)
80
+ }
81
+ // 该数据源本次未返回的应用视为已卸载:回收其记录与贡献
82
+ const alive = new Set(apps.map((a) => a.id))
83
+ for (const [id, rec] of [...this.apps.entries()]) {
84
+ if (rec.sourceId === sourceId && !alive.has(id)) this.apps.delete(id)
85
+ }
86
+ this.publish()
87
+ }
88
+
89
+ /** 单个应用的状态补丁(启动/停止/失败后回写,避免整表重拉) */
90
+ patchApp(appId: string, patch: Partial<ShellApp>): void {
91
+ const rec = this.apps.get(appId)
92
+ if (!rec) return
93
+ rec.app = { ...rec.app, ...patch }
94
+ this.publish()
95
+ }
96
+
97
+ /** 获取应用(含合并后的贡献) */
98
+ getApp(appId: string): ShellApp | undefined {
99
+ const rec = this.apps.get(appId)
100
+ if (!rec) return undefined
101
+ return this.toView(rec.app)
102
+ }
103
+
104
+ /** 清理单个应用(记录 + 贡献) */
105
+ clearApp(appId: string): void {
106
+ this.apps.delete(appId)
107
+ this.contributions.delete(appId)
108
+ this.publish()
109
+ }
110
+
111
+ /** 清理某数据源写入的全部应用 */
112
+ clearSource(sourceId: string): void {
113
+ for (const [id, rec] of [...this.apps.entries()]) {
114
+ if (rec.sourceId === sourceId) {
115
+ this.apps.delete(id)
116
+ this.contributions.delete(id)
117
+ }
118
+ }
119
+ this.publish()
120
+ }
121
+
122
+ /** 应用所属数据源(按来源解析延迟能力,如运行面组件) */
123
+ getSourceOf(appId: string): ShellAppSource | undefined {
124
+ const rec = this.apps.get(appId)
125
+ if (!rec) return undefined
126
+ return this.sources.get(rec.sourceId)
127
+ }
128
+
129
+ // ==================== 界面贡献 ====================
130
+
131
+ /** 注册首页快捷卡片 */
132
+ registerSlot(appId: string, slot: ShellSlotContribution): Disposable {
133
+ const store = this.storeFor(appId)
134
+ store.slots.set(slot.id, { ...slot, component: markRaw(slot.component) })
135
+ this.publish()
136
+ return { dispose: () => this.drop(store.slots, slot.id) }
137
+ }
138
+
139
+ /** 注册应用运行面 */
140
+ registerSurface(appId: string, surface: ShellSurfaceContribution): Disposable {
141
+ const store = this.storeFor(appId)
142
+ store.surface = { ...surface, component: markRaw(surface.component) }
143
+ this.publish()
144
+ return {
145
+ dispose: () => {
146
+ if (store.surface?.component === surface.component) store.surface = undefined
147
+ this.publish()
148
+ },
149
+ }
150
+ }
151
+
152
+ /** 注册胶囊菜单附加项 */
153
+ registerCapsuleItem(appId: string, item: ShellCapsuleItem): Disposable {
154
+ const store = this.storeFor(appId)
155
+ store.capsuleItems.set(item.id, item)
156
+ this.publish()
157
+ return { dispose: () => this.drop(store.capsuleItems, item.id) }
158
+ }
159
+
160
+ // ==================== 内部 ====================
161
+
162
+ private storeFor(appId: string): ContributionStore {
163
+ let store = this.contributions.get(appId)
164
+ if (!store) {
165
+ store = emptyContributions()
166
+ this.contributions.set(appId, store)
167
+ }
168
+ return store
169
+ }
170
+
171
+ private mergeContributions(appId: string, c: ShellApp['contributions']): void {
172
+ if (!c) return
173
+ const store = this.storeFor(appId)
174
+ for (const slot of c.slots ?? []) {
175
+ store.slots.set(slot.id, { ...slot, component: markRaw(slot.component) })
176
+ }
177
+ for (const item of c.capsuleItems ?? []) store.capsuleItems.set(item.id, item)
178
+ if (c.surface) {
179
+ store.surface = { ...c.surface, component: markRaw(c.surface.component) }
180
+ }
181
+ }
182
+
183
+ private drop(map: Map<string, unknown>, key: string): void {
184
+ map.delete(key)
185
+ this.publish()
186
+ }
187
+
188
+ /** 把存储模型投影为「含合并贡献」的视图模型 */
189
+ private toView(app: ShellApp): ShellApp {
190
+ const store = this.contributions.get(app.id)
191
+ if (!store) return { ...app, contributions: {} }
192
+ return {
193
+ ...app,
194
+ contributions: {
195
+ slots: [...store.slots.values()].sort(byOrder),
196
+ surface: store.surface,
197
+ capsuleItems: [...store.capsuleItems.values()].sort(byOrder),
198
+ },
199
+ }
200
+ }
201
+
202
+ /** 重算响应式清单 */
203
+ private publish(): void {
204
+ this.appsRef.value = [...this.apps.values()].map((rec) => this.toView(rec.app))
205
+ }
206
+ }
207
+
208
+ function byOrder<T extends { order?: number }>(a: T, b: T): number {
209
+ return (a.order ?? 100) - (b.order ?? 100)
210
+ }
211
+
212
+ /** 全局单例 */
213
+ const registry = new ShellRegistryClass()
214
+
215
+ /** 获取宿主壳注册表 */
216
+ export function getShellRegistry(): ShellRegistryClass {
217
+ return registry
218
+ }
219
+
220
+ export type { ShellRegistryClass }