view.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>E2E context breakdown</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. ul {
  28. list-style: none;
  29. margin: 0;
  30. padding: 0;
  31. }
  32. li {
  33. display: flex;
  34. justify-content: space-between;
  35. padding: 4px 0;
  36. border-bottom: 1px solid var(--color-border-primary, rgba(127, 127, 127, 0.25));
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <h1>E2E context breakdown</h1>
  42. <div id="meta">loading</div>
  43. <ul id="categories"></ul>
  44. <script>
  45. // 页面脚本的错误记进 DOM,E2E 失败时能直接读到原因。
  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(MCP App 页面层)读写:调视图工具、订阅资源。
  54. // 注意:window.zcode 是不可配置的全局属性,经典脚本顶层不能再声明同名的 const / let。
  55. const host = window.zcode;
  56. let renders = 0;
  57. let stateUri = null;
  58. const applyHostStyles = () => {
  59. const variables = host.hostContext?.styles?.variables ?? {};
  60. for (const [name, value] of Object.entries(variables)) {
  61. if (typeof value === "string") document.documentElement.style.setProperty(name, value);
  62. }
  63. document.documentElement.dataset.theme = host.theme ?? "";
  64. // 页面跟随宿主的 hostContext(主题、语言),E2E 读 body 上的标记。
  65. document.body.dataset.theme = host.theme === "dark" ? "dark" : "light";
  66. document.body.dataset.locale = host.locale ?? "";
  67. };
  68. async function refresh(reason) {
  69. const result = await host.callTool("snapshot", {});
  70. const data = result.structuredContent ?? {};
  71. const composition = data.composition ?? null;
  72. renders += 1;
  73. const categories = composition?.categories ?? [];
  74. document.getElementById("meta").textContent = composition
  75. ? `phase=${composition.phase} model=${composition.model.modelId} tokens=${composition.estimatedTokens}`
  76. : "composition=null";
  77. const list = document.getElementById("categories");
  78. list.replaceChildren(
  79. ...categories.map((category) => {
  80. const item = document.createElement("li");
  81. item.dataset.category = category.id;
  82. item.innerHTML = `<span></span><span></span>`;
  83. item.children[0].textContent = category.id;
  84. item.children[1].textContent = String(category.tokens);
  85. return item;
  86. }),
  87. );
  88. Object.assign(document.body.dataset, {
  89. // 与输入框用量圆环同一份服务商 usage 算出的值(context.get,§11);预演时没有。
  90. providerContextTokens:
  91. composition?.providerContextTokens === undefined
  92. ? ""
  93. : String(composition.providerContextTokens),
  94. // 各类别的条目数之和(工具名、技能名、指令文件),条目由宿主按 token 降序给出。
  95. items: String(
  96. categories.reduce((count, category) => count + (category.items?.length ?? 0), 0),
  97. ),
  98. phase: composition?.phase ?? "none",
  99. model: composition?.model.modelId ?? "",
  100. categories: categories.map((category) => category.id).join(","),
  101. renders: String(renders),
  102. scope: data.scope?.kind ?? "",
  103. sessionId: data.scope?.sessionId ?? "",
  104. });
  105. const tokens = Object.fromEntries(
  106. categories.map((category) => [category.id, category.tokens]),
  107. );
  108. await host.callTool("rendered", {
  109. reason,
  110. renders,
  111. phase: composition?.phase ?? null,
  112. model: composition?.model.modelId ?? null,
  113. sessionId: data.scope?.sessionId ?? null,
  114. categories: categories.map((category) => category.id),
  115. systemPromptTokens: tokens.system_prompt ?? 0,
  116. systemToolTokens: tokens.system_tool_schemas ?? 0,
  117. messagesTokens: tokens.messages ?? 0,
  118. });
  119. }
  120. host.subscribe(applyHostStyles);
  121. host.onResourceUpdated((uri) => {
  122. if (uri === stateUri) void refresh("updated");
  123. });
  124. host.ready().then(async () => {
  125. applyHostStyles();
  126. const resources = await host.listResources();
  127. stateUri =
  128. resources.resources.find((resource) => resource.uri.endsWith("/state"))?.uri ?? null;
  129. if (stateUri) await host.subscribeResource(stateUri);
  130. await refresh("initial");
  131. });
  132. </script>
  133. </body>
  134. </html>