renderer.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { createGenUiPage } from "../../../ui/src/gen-ui/adapters/page.js";
  2. import { registerPluginUiSessionActions } from "../../../ui/src/plugin-ui/hostPrimitives.js";
  3. import type { GenUiPage } from "../../../ui/src/gen-ui/contract.js";
  4. import type { IGenUiService } from "@zcode/services";
  5. import type { GenUiStateChange } from "@zcode/shared/gen-ui";
  6. import type { PluginSandboxPortsEvent } from "@zcode/shared/mcp-apps";
  7. const harness = (window as any).harness;
  8. let portListener: ((event: PluginSandboxPortsEvent) => void) | undefined;
  9. let stateListener: ((event: GenUiStateChange) => void) | undefined;
  10. window.addEventListener("message", (event) => {
  11. if (event.data?.type === "zcode:plugin-sandbox-ports")
  12. portListener?.({ ...event.data, port: event.ports[0] });
  13. if (event.data?.type === "fixture-notification") stateListener?.(event.data.payload);
  14. });
  15. const service = new Proxy(
  16. {},
  17. {
  18. get: (_, key) =>
  19. key === "onStateChanged"
  20. ? (fn: typeof stateListener) => {
  21. stateListener = fn;
  22. return {
  23. dispose() {
  24. stateListener = undefined;
  25. },
  26. };
  27. }
  28. : (input: unknown) => harness.bridge(key, input),
  29. },
  30. ) as IGenUiService;
  31. void (async () => {
  32. const config = await harness.config();
  33. let page: GenUiPage, off: (() => void) | undefined;
  34. let confirmations = 0,
  35. messages: unknown[] = [],
  36. bound = true;
  37. const target = {
  38. workspacePath: config.root,
  39. sessionId: "fixture",
  40. path: config.path,
  41. instanceKey: "row:0",
  42. workspaceIdentity: "ssh:fixture:/workspace",
  43. };
  44. const unregister = registerPluginUiSessionActions(target, {
  45. async sendFollowUp(message) {
  46. messages.push(message);
  47. },
  48. });
  49. const waitReady = () =>
  50. new Promise<void>((resolve, reject) => {
  51. const read = () => {
  52. const s = page.getSnapshot();
  53. if (
  54. s.phase === "ready" &&
  55. s.groups.length === 2 &&
  56. s.groups[0]?.controls.length === 4 &&
  57. s.groups[1]?.controls.length === 2
  58. ) {
  59. stop();
  60. resolve();
  61. }
  62. if (s.phase === "error") {
  63. stop();
  64. reject(new Error(s.error));
  65. }
  66. };
  67. const stop = page.subscribe(read);
  68. read();
  69. });
  70. async function start() {
  71. page = createGenUiPage({
  72. target,
  73. files: service,
  74. state: service,
  75. openExternal: () => {
  76. throw new Error("External links belong to the React fixture");
  77. },
  78. confirm: async (prompt) => {
  79. confirmations++;
  80. return prompt;
  81. },
  82. platform: {
  83. supportsRetainedMove: window.zcodePluginSandbox?.supportsRetainedMove,
  84. copyImage: (request) => window.zcodePluginSandbox!.copyImage!(request),
  85. getOwnerWebContentsId: () => harness.owner(),
  86. disposeSandbox: (id, init) => harness.dispose(id, init),
  87. consumeUserGesture: () => harness.gesture(),
  88. onPorts(fn) {
  89. portListener = fn;
  90. return () => {
  91. portListener = undefined;
  92. };
  93. },
  94. },
  95. });
  96. off = page.bind(document.getElementById("inline")!, false);
  97. await waitReady();
  98. }
  99. Object.assign(window, {
  100. harnessReady: start(),
  101. stop: () => page.dispose(),
  102. copy: () => page.copyImage(),
  103. switchPage: async (expanded: boolean) => {
  104. off?.();
  105. off = page.bind(document.getElementById(expanded ? "expanded" : "inline")!, expanded);
  106. await new Promise(requestAnimationFrame);
  107. return page.getSnapshot();
  108. },
  109. restore: async () => {
  110. off?.();
  111. await page.dispose();
  112. await start();
  113. },
  114. inspect: () => ({ confirmations, messages, bound, snapshot: page.getSnapshot() }),
  115. setProbeTweaks: (values: Record<string, number>) => page.setTweaks(values),
  116. unbind: () => {
  117. unregister();
  118. bound = false;
  119. },
  120. adjust: () =>
  121. page.setTweaks(
  122. Object.fromEntries(
  123. page
  124. .getSnapshot()
  125. .groups[0]!.controls.map((control, index) => [
  126. control.id,
  127. [35, "#ff0000", false, "compact"][index]!,
  128. ]),
  129. ),
  130. ),
  131. submit: () => page.submitTweaks({ [page.getSnapshot().groups[0]!.controls[0]!.id]: 35 }),
  132. });
  133. })();