process-local-miniapp-application.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import type { MiniAppSurfaceSummary } from '@mavis/shared/miniapp-surface';
  2. import {
  3. miniAppBusyReason,
  4. type MiniAppStatus,
  5. type MiniAppSupervisor,
  6. } from '../../service/miniapp/index.js';
  7. import type {
  8. AvailableMiniAppDefinition,
  9. MiniAppPluginControl,
  10. } from '../../service/plugin-system/index.js';
  11. import type {
  12. LocalRuntimeApplication,
  13. ProcessLocalMiniAppLaunch,
  14. } from './process-local-application-contract.js';
  15. type ProcessLocalMiniAppApplication = NonNullable<LocalRuntimeApplication['miniApps']>;
  16. /** Projects installed MiniApp definitions and Supervisor state for the Electron bridge. */
  17. export function createProcessLocalMiniAppApplication(
  18. plugins: MiniAppPluginControl,
  19. supervisor: Pick<MiniAppSupervisor, 'inspect'> | undefined,
  20. ): ProcessLocalMiniAppApplication | undefined {
  21. if (!supervisor) return undefined;
  22. const requireDefinition = async (pluginId: string): Promise<AvailableMiniAppDefinition> => {
  23. const miniApp = (await plugins.listAvailableMiniApps()).find(
  24. (candidate) => candidate.pluginId === pluginId,
  25. );
  26. if (!miniApp) throw applicationError('NOT_FOUND');
  27. return miniApp;
  28. };
  29. const launch = (
  30. miniApp: AvailableMiniAppDefinition,
  31. status: MiniAppStatus | undefined,
  32. ): ProcessLocalMiniAppLaunch => {
  33. const active = status?.active;
  34. if (
  35. status?.phase !== 'active' ||
  36. !active?.origin ||
  37. !active.processGeneration ||
  38. active.pluginId !== miniApp.pluginId
  39. ) {
  40. throw applicationError(status?.failureCode ?? 'RUNTIME_FAILED');
  41. }
  42. return {
  43. miniApp: summary(miniApp, status),
  44. launch: {
  45. origin: active.origin,
  46. surfacePath: active.surfacePath,
  47. runId: active.processGeneration,
  48. },
  49. };
  50. };
  51. const open = async (
  52. pluginId: string,
  53. signal?: AbortSignal,
  54. ): Promise<ProcessLocalMiniAppLaunch> => {
  55. signal?.throwIfAborted();
  56. const miniApp = await requireDefinition(pluginId);
  57. try {
  58. await plugins.activateMiniApp({ pluginId, ...(signal ? { signal } : {}) });
  59. } catch (error) {
  60. const busyReason = miniAppBusyReason(error);
  61. if (!busyReason) throw error;
  62. throw Object.assign(applicationError('BUSY', { cause: error }), { busyReason });
  63. }
  64. signal?.throwIfAborted();
  65. return launch(miniApp, supervisor.inspect(pluginId)[0]);
  66. };
  67. return {
  68. list: async () => {
  69. const definitions = await plugins.listAvailableMiniApps();
  70. const statuses = new Map(supervisor.inspect().map((status) => [status.pluginId, status]));
  71. return {
  72. miniApps: definitions.map((miniApp) => summary(miniApp, statuses.get(miniApp.pluginId))),
  73. };
  74. },
  75. open: ({ pluginId, signal }) => open(pluginId, signal),
  76. stop: async ({ pluginId, signal }) => {
  77. await requireDefinition(pluginId);
  78. await plugins.stopMiniApp({ pluginId, ...(signal ? { signal } : {}) });
  79. },
  80. };
  81. }
  82. function summary(
  83. miniApp: AvailableMiniAppDefinition,
  84. status: MiniAppStatus | undefined,
  85. ): MiniAppSurfaceSummary {
  86. const runtimeStatus = mapRuntimeStatus(status?.phase);
  87. return {
  88. pluginId: miniApp.pluginId,
  89. ...(miniApp.displayName ? { displayName: miniApp.displayName } : {}),
  90. ...(miniApp.description ? { description: miniApp.description } : {}),
  91. ...(miniApp.iconPath ? { iconUrl: localIconUrl(miniApp.iconPath) } : {}),
  92. runtime: {
  93. kind: 'process',
  94. status: runtimeStatus,
  95. ...(runtimeStatus === 'failed' && status?.failureCode
  96. ? { errorCode: status.failureCode }
  97. : {}),
  98. },
  99. };
  100. }
  101. function localIconUrl(iconPath: string): string {
  102. return `/mavis/api/file/preview?path=${encodeURIComponent(iconPath)}`;
  103. }
  104. function mapRuntimeStatus(
  105. phase: MiniAppStatus['phase'] | undefined,
  106. ): MiniAppSurfaceSummary['runtime']['status'] {
  107. if (phase === 'active') return 'running';
  108. if (phase === 'starting' || phase === 'preparing') return 'starting';
  109. if (phase === 'failed' || phase === 'quarantined') return 'failed';
  110. return 'stopped';
  111. }
  112. function applicationError(code: string, options?: ErrorOptions): Error & { readonly code: string } {
  113. return Object.assign(new Error('Mini App surface action failed', options), { code });
  114. }