view.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>E2E session rows</title>
  6. <style>
  7. :root {
  8. color-scheme: light dark;
  9. }
  10. body {
  11. margin: 0;
  12. padding: 16px;
  13. font:
  14. 13px/1.5 system-ui,
  15. sans-serif;
  16. color: var(--color-text-primary, #1f2328);
  17. background: var(--color-background-primary, transparent);
  18. }
  19. h1 {
  20. margin: 0 0 8px;
  21. font-size: 15px;
  22. }
  23. #meta {
  24. color: var(--color-text-secondary, #59636e);
  25. margin-bottom: 12px;
  26. }
  27. ol {
  28. margin: 0;
  29. padding-left: 20px;
  30. }
  31. li {
  32. padding: 4px 0;
  33. border-bottom: 1px solid var(--color-border-primary, rgba(127, 127, 127, 0.25));
  34. }
  35. .kind {
  36. color: var(--color-text-secondary, #59636e);
  37. margin-right: 8px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <h1>E2E session rows</h1>
  43. <div id="meta">loading</div>
  44. <ol id="rows"></ol>
  45. <script>
  46. window.__e2eErrors = [];
  47. window.addEventListener("error", (event) => window.__e2eErrors.push(String(event.message)));
  48. window.addEventListener("unhandledrejection", (event) =>
  49. window.__e2eErrors.push(String(event.reason?.message ?? event.reason)),
  50. );
  51. </script>
  52. <script>
  53. // 页面只经 window.zcode 读写:调视图工具 state、订阅 state 资源。
  54. const host = window.zcode;
  55. let renders = 0;
  56. let stateUri = null;
  57. const applyHostStyles = () => {
  58. const variables = host.hostContext?.styles?.variables ?? {};
  59. for (const [name, value] of Object.entries(variables)) {
  60. if (typeof value === "string") document.documentElement.style.setProperty(name, value);
  61. }
  62. document.documentElement.dataset.theme = host.theme ?? "";
  63. };
  64. async function refresh() {
  65. const result = await host.callTool("state", {});
  66. const data = result.structuredContent ?? {};
  67. renders += 1;
  68. const rows = data.rows ?? [];
  69. document.getElementById("meta").textContent =
  70. `session=${data.sessionId ?? "none"} phase=${data.phase ?? "none"} rows=${rows.length} events=${(data.events ?? []).join(",")}`;
  71. document.getElementById("rows").replaceChildren(
  72. ...rows.map((row) => {
  73. const item = document.createElement("li");
  74. item.dataset.kind = row.kind;
  75. const kind = document.createElement("span");
  76. kind.className = "kind";
  77. kind.textContent = row.kind;
  78. const text = document.createElement("span");
  79. text.textContent = row.text;
  80. item.append(kind, text);
  81. return item;
  82. }),
  83. );
  84. Object.assign(document.body.dataset, {
  85. sessionId: data.sessionId ?? "",
  86. rows: String(rows.length),
  87. events: (data.events ?? []).join(","),
  88. renders: String(renders),
  89. });
  90. }
  91. host.subscribe(applyHostStyles);
  92. host.onResourceUpdated((uri) => {
  93. if (uri === stateUri) void refresh();
  94. });
  95. host.ready().then(async () => {
  96. applyHostStyles();
  97. const resources = await host.listResources();
  98. stateUri =
  99. resources.resources.find((resource) => resource.uri.endsWith("/state"))?.uri ?? null;
  100. if (stateUri) await host.subscribeResource(stateUri);
  101. await refresh();
  102. });
  103. </script>
  104. </body>
  105. </html>