@xiaobuyu/nodesign 0.0.1 → 0.0.2

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 (282) hide show
  1. package/README.md +156 -106
  2. package/bin/nodesign.js +0 -0
  3. package/package.json +1 -1
  4. package/server/api/README.md +45 -45
  5. package/server/api/_guard.js +75 -75
  6. package/server/api/admin.js +187 -187
  7. package/server/api/assets/docx-page.js +103 -103
  8. package/server/api/assets/helpers.js +22 -22
  9. package/server/api/assets/notes.js +144 -144
  10. package/server/api/assets.js +896 -896
  11. package/server/api/board.js +57 -57
  12. package/server/api/browse.js +138 -138
  13. package/server/api/canvas.js +444 -444
  14. package/server/api/chatai.js +324 -324
  15. package/server/api/exports/build-standalone.js +980 -980
  16. package/server/api/exports/cards.js +118 -118
  17. package/server/api/exports/docx-pdf.js +46 -46
  18. package/server/api/exports/export-page.js +142 -142
  19. package/server/api/exports/handoff.js +147 -147
  20. package/server/api/exports.js +690 -690
  21. package/server/api/instruction.js +75 -75
  22. package/server/api/me.js +116 -116
  23. package/server/api/memory.js +185 -185
  24. package/server/api/pending-changes.js +366 -366
  25. package/server/api/plugins.js +164 -164
  26. package/server/api/projects.js +220 -220
  27. package/server/api/publish.js +64 -64
  28. package/server/api/recent.js +73 -73
  29. package/server/api/sessions.js +589 -589
  30. package/server/api/skills.js +77 -77
  31. package/server/api/standalone-fit.js +168 -168
  32. package/server/api/turn-compose.js +180 -180
  33. package/server/api/turn-inflight.js +44 -44
  34. package/server/auth/README.md +17 -17
  35. package/server/auth/middleware.js +138 -138
  36. package/server/auth/origin-guard.js +71 -71
  37. package/server/auth/session.js +108 -108
  38. package/server/auth/tier.js +113 -113
  39. package/server/auth/users-store.js +348 -348
  40. package/server/edit/README.md +29 -29
  41. package/server/engine/README.md +113 -113
  42. package/server/engine/agent/agent-shared.js +550 -550
  43. package/server/engine/agent/auto-mode-default-hard-deny.txt +4 -4
  44. package/server/engine/agent/auto-mode-rules.js +78 -78
  45. package/server/engine/agent/context.js +418 -418
  46. package/server/engine/agent/events.js +464 -464
  47. package/server/engine/agent/hooks/canvas-validate.js +199 -199
  48. package/server/engine/agent/hooks/failure.js +120 -120
  49. package/server/engine/agent/hooks/file-events.js +75 -75
  50. package/server/engine/agent/hooks/lifecycle.js +233 -233
  51. package/server/engine/agent/hooks/post-canvas-focus.js +80 -80
  52. package/server/engine/agent/hooks/post-guidance.js +206 -206
  53. package/server/engine/agent/hooks/post-subagent-report.js +104 -104
  54. package/server/engine/agent/hooks/post-trim.js +58 -58
  55. package/server/engine/agent/hooks/pre-board-neighborhood.js +34 -34
  56. package/server/engine/agent/hooks/pre-defaults.js +84 -84
  57. package/server/engine/agent/hooks/pre-injectors.js +277 -277
  58. package/server/engine/agent/hooks/pre-performance-log-guard.js +78 -78
  59. package/server/engine/agent/hooks/pre-starter-files.js +61 -61
  60. package/server/engine/agent/hooks/pre-workspace-scope-guard.js +78 -78
  61. package/server/engine/agent/hooks/site-validate.js +94 -94
  62. package/server/engine/agent/hooks/tool-prompts.js +31 -31
  63. package/server/engine/agent/hooks/turn-state-memory.js +56 -56
  64. package/server/engine/agent/hooks/user-prompt-submit.js +215 -215
  65. package/server/engine/agent/hooks.js +354 -354
  66. package/server/engine/agent/init-contract.js +69 -69
  67. package/server/engine/agent/isolation.js +159 -159
  68. package/server/engine/agent/model-table.js +292 -292
  69. package/server/engine/agent/plugin-loader.js +266 -266
  70. package/server/engine/agent/prompts/nodesign-prelude.md +336 -336
  71. package/server/engine/agent/prompts/tools/ask-user-question-protocol.md +71 -71
  72. package/server/engine/agent/prompts/tools/direct-edit-protocol.md +98 -98
  73. package/server/engine/agent/prompts/tools/generate-image-cookbook.gemini-gateway.md +583 -583
  74. package/server/engine/agent/prompts/tools/generate-image-cookbook.md +483 -483
  75. package/server/engine/agent/prompts/tools/hybrid-reference.md +100 -100
  76. package/server/engine/agent/prompts/tools/paint-still-cookbook.md +270 -270
  77. package/server/engine/agent/prompts/tools/roll-film-cookbook.md +70 -70
  78. package/server/engine/agent/prompts/tools/site-reference.md +106 -106
  79. package/server/engine/agent/prompts/tools/tweaks-syntax.md +125 -125
  80. package/server/engine/agent/prompts/tools/vision-checker-dispatch.md +48 -48
  81. package/server/engine/agent/session-loop.js +1024 -1024
  82. package/server/engine/agent/session-model.js +164 -164
  83. package/server/engine/agent/skill.js +204 -204
  84. package/server/engine/agent/task-events.js +78 -78
  85. package/server/engine/agents/ds-extractor.md +132 -132
  86. package/server/engine/agents/explorer.md +190 -190
  87. package/server/engine/agents/index.js +230 -230
  88. package/server/engine/agents/schemas/design-system.json +156 -156
  89. package/server/engine/agents/schemas/tweak-schema.json +113 -113
  90. package/server/engine/agents/tweak-proposer.md +127 -127
  91. package/server/engine/agents/vision-checker.md +254 -254
  92. package/server/engine/browse/capture.js +422 -422
  93. package/server/engine/browse/card.js +137 -137
  94. package/server/engine/browse/handover.js +59 -59
  95. package/server/engine/browse/page-digest.js +119 -119
  96. package/server/engine/browse/refs.js +196 -196
  97. package/server/engine/browse/registry.js +307 -307
  98. package/server/engine/browse/screencast.js +170 -170
  99. package/server/engine/browse/state.js +121 -121
  100. package/server/engine/chatai/chat-log.js +79 -79
  101. package/server/engine/chatai/index.js +193 -193
  102. package/server/engine/chatai/openai-compat.js +152 -152
  103. package/server/engine/chatai/orchestrate.js +309 -309
  104. package/server/engine/chatai/perform.js +40 -40
  105. package/server/engine/chatai/summarize.js +79 -79
  106. package/server/engine/mcp/capability-gate.js +52 -52
  107. package/server/engine/mcp/index.js +339 -339
  108. package/server/engine/mcp/param-sanitizer.js +142 -142
  109. package/server/engine/mcp/tools/arrange-on-board.js +124 -124
  110. package/server/engine/mcp/tools/artifact-session.js +278 -278
  111. package/server/engine/mcp/tools/browse-computer.js +421 -421
  112. package/server/engine/mcp/tools/browse-find-batch.js +183 -183
  113. package/server/engine/mcp/tools/browse-screenshot.js +157 -157
  114. package/server/engine/mcp/tools/browse.js +480 -480
  115. package/server/engine/mcp/tools/build-docx.js +101 -101
  116. package/server/engine/mcp/tools/clear-pending-changes.js +99 -99
  117. package/server/engine/mcp/tools/create-on-board.js +155 -155
  118. package/server/engine/mcp/tools/crystallize-skill.js +168 -168
  119. package/server/engine/mcp/tools/explain-style.js +237 -237
  120. package/server/engine/mcp/tools/export-handoff.js +133 -133
  121. package/server/engine/mcp/tools/expose-tweaks.js +149 -149
  122. package/server/engine/mcp/tools/generate-image.js +842 -842
  123. package/server/engine/mcp/tools/get-computed-styles.js +164 -164
  124. package/server/engine/mcp/tools/get-pending-changes.js +205 -205
  125. package/server/engine/mcp/tools/h3box-ssh.js +84 -84
  126. package/server/engine/mcp/tools/helpers/acquire-page.js +82 -82
  127. package/server/engine/mcp/tools/helpers/motion-lab.js +554 -554
  128. package/server/engine/mcp/tools/helpers/motion-scroll.js +112 -112
  129. package/server/engine/mcp/tools/helpers/perception-page.js +215 -215
  130. package/server/engine/mcp/tools/helpers/reference-download.js +103 -103
  131. package/server/engine/mcp/tools/helpers/rembg-bridge.py +80 -80
  132. package/server/engine/mcp/tools/helpers/shot-pipeline.js +280 -280
  133. package/server/engine/mcp/tools/highlight.js +68 -68
  134. package/server/engine/mcp/tools/list-pages.js +222 -222
  135. package/server/engine/mcp/tools/lookup-tags.js +59 -59
  136. package/server/engine/mcp/tools/navigate-to-page.js +60 -60
  137. package/server/engine/mcp/tools/organize-board.js +64 -64
  138. package/server/engine/mcp/tools/paint-still.js +434 -434
  139. package/server/engine/mcp/tools/pin-to-board.js +130 -130
  140. package/server/engine/mcp/tools/preview-deck.js +130 -130
  141. package/server/engine/mcp/tools/profile-scroll.js +305 -305
  142. package/server/engine/mcp/tools/publish-site.js +102 -102
  143. package/server/engine/mcp/tools/query-elements.js +195 -195
  144. package/server/engine/mcp/tools/read-board.js +103 -103
  145. package/server/engine/mcp/tools/read-document.js +93 -93
  146. package/server/engine/mcp/tools/read-page.js +264 -264
  147. package/server/engine/mcp/tools/read-tavern-json.js +143 -143
  148. package/server/engine/mcp/tools/record-decision.js +140 -140
  149. package/server/engine/mcp/tools/relate-on-board.js +110 -110
  150. package/server/engine/mcp/tools/report-issue.js +115 -115
  151. package/server/engine/mcp/tools/roll-film.js +266 -266
  152. package/server/engine/mcp/tools/screenshot-docx.js +138 -138
  153. package/server/engine/mcp/tools/screenshot-url.js +195 -195
  154. package/server/engine/mcp/tools/screenshot.js +583 -583
  155. package/server/engine/mcp/tools/tier-gate.js +96 -96
  156. package/server/engine/mcp/tools/trace-motion.js +215 -215
  157. package/server/engine/mcp/tools/web-search.js +548 -548
  158. package/server/engine/motion/inventory.js +349 -349
  159. package/server/engine/perception/session.js +235 -235
  160. package/server/engine/plugins/nodesign/.claude-plugin/plugin.json +5 -5
  161. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/SKILL.md +192 -192
  162. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/canvas.template.html +667 -667
  163. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/hybrid-grid.md +22 -22
  164. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/image-led-cover.md +25 -25
  165. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/portrait.md +23 -23
  166. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/quote-backdrop.md +21 -21
  167. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/section-divider.md +23 -23
  168. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/text-led.md +21 -21
  169. package/server/engine/plugins/nodesign/skills/docx-craft/SKILL.md +162 -162
  170. package/server/engine/plugins/nodesign/skills/docx-craft/references/token-schema.md +407 -407
  171. package/server/engine/plugins/nodesign/skills/docx-craft//346/226/207/346/241/243.template.json +80 -80
  172. package/server/engine/plugins/nodesign/skills/rp-craft/SKILL.md +228 -228
  173. package/server/engine/plugins/nodesign/skills/rp-craft/patterns//346/274/224/345/207/272/351/241/265-/346/234/200/345/260/217/345/256/236/347/216/260.html +78 -78
  174. package/server/engine/plugins/nodesign/skills/rp-craft//346/274/224/345/207/272.template.js +268 -268
  175. package/server/engine/plugins/nodesign/skills/site-craft/SKILL.md +323 -323
  176. package/server/engine/plugins/nodesign/skills/site-craft/patterns/build-lane.md +134 -134
  177. package/server/engine/plugins/nodesign/skills/site-craft/patterns/cutout-collage.md +61 -61
  178. package/server/engine/plugins/nodesign/skills/site-craft/patterns/mock-app.md +163 -163
  179. package/server/engine/plugins/nodesign/skills/site-craft/patterns/page-transitions.md +86 -86
  180. package/server/engine/runs/active-runs.js +698 -698
  181. package/server/engine/runs/live-turn.js +252 -252
  182. package/server/engine/runs/store.js +364 -364
  183. package/server/engine/runs/turn-relay.js +217 -217
  184. package/server/engine/runtime/workspace.js +164 -164
  185. package/server/lib/artifact-file-path.js +80 -80
  186. package/server/lib/artifact-target.js +284 -284
  187. package/server/lib/asset-refs.js +239 -239
  188. package/server/lib/async-queue.js +104 -104
  189. package/server/lib/auto-relations.js +130 -130
  190. package/server/lib/binding-types.js +60 -60
  191. package/server/lib/board-kind-sizes.js +62 -62
  192. package/server/lib/board-relations.js +133 -133
  193. package/server/lib/browse-proxy.js +180 -180
  194. package/server/lib/canvas-id.js +36 -36
  195. package/server/lib/cover.js +227 -227
  196. package/server/lib/danbooru-tags.js +291 -291
  197. package/server/lib/doc-extract.js +156 -156
  198. package/server/lib/docx/build-from-source.js +260 -260
  199. package/server/lib/docx/build.js +490 -490
  200. package/server/lib/docx/dump-styles.js +287 -287
  201. package/server/lib/docx/fonts/nodesign-cjk.conf +164 -164
  202. package/server/lib/docx/merge-runs.js +129 -129
  203. package/server/lib/docx/numbering.js +218 -218
  204. package/server/lib/docx/order.js +152 -152
  205. package/server/lib/docx/rawzip.js +172 -172
  206. package/server/lib/docx/render.js +92 -92
  207. package/server/lib/docx/text-lint.js +225 -225
  208. package/server/lib/docx/tokens.js +335 -335
  209. package/server/lib/docx/units.js +29 -29
  210. package/server/lib/docx/xml.js +291 -291
  211. package/server/lib/docx-pages.js +187 -187
  212. package/server/lib/export-collect.js +289 -289
  213. package/server/lib/export-package.js +210 -210
  214. package/server/lib/html-srcset.js +145 -145
  215. package/server/lib/image-variant.js +460 -460
  216. package/server/lib/ingress/forward-openai-chat.js +305 -305
  217. package/server/lib/ingress/openai-chat.js +491 -491
  218. package/server/lib/ingress/session-notice.js +66 -66
  219. package/server/lib/ingress/session-routes.js +68 -68
  220. package/server/lib/ingress/slot-probe.js +130 -130
  221. package/server/lib/ingress/upstream-billing.js +52 -52
  222. package/server/lib/ingress/upstream-fail-streak.js +72 -72
  223. package/server/lib/ingress/upstream-truncation.js +48 -48
  224. package/server/lib/issues-store.js +182 -182
  225. package/server/lib/kinds/deck.js +72 -72
  226. package/server/lib/kinds/docx.js +283 -283
  227. package/server/lib/kinds/file-kinds.js +113 -113
  228. package/server/lib/kinds/index.js +202 -202
  229. package/server/lib/kinds/site.js +161 -161
  230. package/server/lib/model-ingress.js +543 -543
  231. package/server/lib/moderation.js +255 -255
  232. package/server/lib/notice-store.js +103 -103
  233. package/server/lib/plugin-install.js +104 -104
  234. package/server/lib/plugin-validator.js +721 -721
  235. package/server/lib/publish-store.js +115 -115
  236. package/server/lib/quick-summary.js +52 -52
  237. package/server/lib/quota.js +270 -270
  238. package/server/lib/rate-window.js +29 -29
  239. package/server/lib/reference-assets.js +92 -92
  240. package/server/lib/region-shot.js +135 -135
  241. package/server/lib/safe-path.js +73 -73
  242. package/server/lib/sdk-session.js +33 -33
  243. package/server/lib/showcase-store.js +92 -92
  244. package/server/lib/site-publish.js +465 -465
  245. package/server/lib/ssrf-guard.js +432 -432
  246. package/server/lib/task-scan.js +158 -158
  247. package/server/lib/tavern-json.js +154 -154
  248. package/server/lib/video-variant.js +237 -237
  249. package/server/lib/workspace-path.js +33 -33
  250. package/server/ops/fix-sdk-musl.mjs +40 -40
  251. package/server/ops/install-macos-fonts.sh +114 -114
  252. package/server/ops/macos-fonts.conf +336 -336
  253. package/server/ops/sandbox-shim/bwrap +57 -57
  254. package/server/projects/assets-summary.js +119 -119
  255. package/server/projects/auto-name.js +53 -53
  256. package/server/projects/board-store.js +640 -640
  257. package/server/projects/move-entry.js +103 -103
  258. package/server/projects/store.js +270 -270
  259. package/server/projects/ui-config.js +54 -54
  260. package/server/projects/workspace-templates.js +53 -53
  261. package/server/projects/workspace.js +1025 -1025
  262. package/server/runtime/local-config.js +177 -177
  263. package/server/runtime/profile.js +72 -72
  264. package/server/services/rembg-service.py +334 -334
  265. package/server/shared/README.md +25 -25
  266. package/server/shared/deck.js +54 -54
  267. package/server/shared/time.js +49 -49
  268. package/server/style-pipeline/README.md +53 -53
  269. package/server/ws/broker.js +36 -36
  270. package/server/ws/browse-channel.js +177 -177
  271. package/server/ws/index.js +386 -386
  272. package/web/README.md +89 -89
  273. package/web/dist/assets/BrowserWindow-BYCgizHw.js +11 -0
  274. package/web/dist/assets/{DeckWindow-4G0xX7P3.js → DeckWindow-pNydEM4L.js} +1 -1
  275. package/web/dist/assets/{DocxWindow-BXQsoPkh.js → DocxWindow-PPgzh3Gq.js} +1 -1
  276. package/web/dist/assets/{SiteWindow-D6eS-2gM.js → SiteWindow-DM0LQPCO.js} +1 -1
  277. package/web/dist/assets/{index-Cbl3ATvp.js → index-BHTBJlpY.js} +32 -32
  278. package/web/dist/assets/{pending-edit-apply-DTjo2geW.js → pending-edit-apply-C0I13CbD.js} +1 -1
  279. package/web/dist/assets/{rotate-cw-LMT3x1Zj.js → rotate-cw-B1UQ8Hld.js} +1 -1
  280. package/web/dist/assets/{square-dashed-mouse-pointer-CBoAkfhi.js → square-dashed-mouse-pointer-8lrgqmfv.js} +1 -1
  281. package/web/dist/index.html +17 -17
  282. package/web/dist/assets/BrowserWindow-Dc7J7CYc.js +0 -11
@@ -1,237 +1,237 @@
1
- /**
2
- * mcp/tools/explain-style.js — explain_style(2026-08-18)
3
- *
4
- * ## 这个洞是什么
5
- *
6
- * `get_computed_styles` 告诉你**结果**(marginBottom: 0px),不告诉你**为什么**。
7
- * 浏览器 devtools 的 Styles 面板会把败下阵的声明划掉,一眼就看得出是谁赢了 ——
8
- * agent 手里恰恰没有 devtools。
9
- *
10
- * 真实案例:用户圈出"这里太挤了、很贴底边"。真因是 `.wrap { margin: 0 auto }`
11
- * 这条**简写** —— 类选择器优先级高于 `main > section { margin-bottom: 108px }`,
12
- * 把所有带 .wrap 的节的下边距清成了 0,全站每一节都在吃这个亏。
13
- * 定位过程:computed 报 0px(对的,但只告诉结果)→ 怀疑外边距塌陷 → 怀疑媒体
14
- * 查询 → 怀疑变量没解析 → grep 整个 CSS → 都排除后才想到是简写覆盖。
15
- * **五六轮工具调用,而 devtools 里这是一眼的事。**
16
- *
17
- * 这类 bug 在"一份手写 CSS + 后加的工具类"的站点上非常常见 —— 也就是这个平台上
18
- * 绝大多数站点。
19
- *
20
- * ## 实现
21
- *
22
- * 走 CDP 的 `CSS.getMatchedStylesForNode`:它按级联次序返回命中该元素的全部规则,
23
- * 我们从里面挑出跟目标属性有关的声明(**含简写展开**),标出谁赢、谁被压掉、
24
- * 各自来自哪条规则。
25
- */
26
-
27
- import { tool } from '@anthropic-ai/claude-agent-sdk';
28
- import { z } from 'zod';
29
- import { resolveCanvasTarget, CANVAS_PATH_DESC, requireBrowsable } from '../../../lib/artifact-target.js';
30
- import { degradedNote, SITE_DEVICE_W } from './helpers/perception-page.js';
31
- import { acquireArtifactPage, LIVE_PARAM_DESC } from './helpers/acquire-page.js';
32
-
33
- /** 简写 → 它能设置的长手属性(只列常踩的那些,够覆盖真实事故) */
34
- const SHORTHAND_OF = {
35
- margin: ['margin-top', 'margin-right', 'margin-bottom', 'margin-left'],
36
- padding: ['padding-top', 'padding-right', 'padding-bottom', 'padding-left'],
37
- inset: ['top', 'right', 'bottom', 'left'],
38
- border: ['border-width', 'border-style', 'border-color'],
39
- 'border-width': ['border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'],
40
- 'border-radius': ['border-top-left-radius', 'border-top-right-radius', 'border-bottom-right-radius', 'border-bottom-left-radius'],
41
- background: ['background-color', 'background-image', 'background-position', 'background-size', 'background-repeat'],
42
- font: ['font-family', 'font-size', 'font-weight', 'font-style', 'line-height'],
43
- flex: ['flex-grow', 'flex-shrink', 'flex-basis'],
44
- gap: ['row-gap', 'column-gap'],
45
- overflow: ['overflow-x', 'overflow-y'],
46
- transition: ['transition-property', 'transition-duration', 'transition-timing-function', 'transition-delay'],
47
- animation: ['animation-name', 'animation-duration', 'animation-timing-function'],
48
- 'grid-template': ['grid-template-rows', 'grid-template-columns'],
49
- 'place-items': ['align-items', 'justify-items'],
50
- 'place-content': ['align-content', 'justify-content'],
51
- };
52
-
53
- /** 一条声明是否影响目标属性:直接同名,或者是能设置它的简写 */
54
- function affects(declName, prop) {
55
- if (declName === prop) return { hit: true, viaShorthand: null };
56
- const longhands = SHORTHAND_OF[declName];
57
- if (longhands && longhands.includes(prop)) return { hit: true, viaShorthand: declName };
58
- return { hit: false };
59
- }
60
-
61
- export function makeExplainStyleTool({ workspaceRoot, projectId, sessionId }) {
62
- return tool(
63
- 'explain_style',
64
- `Explain WHY one CSS property has the value it has: every declaration that
65
- matched the element, in cascade order, with the winner marked and the losers
66
- listed — including declarations that came from a SHORTHAND.
67
-
68
- get_computed_styles gives you the result; this gives you the reason. Reach for it
69
- the moment a value is not what your CSS says it should be, instead of guessing
70
- through margin collapse / media queries / unresolved variables one at a time.
71
-
72
- The case this was built for: a section's margin-bottom computed to 0px even
73
- though the stylesheet said 108px. Cause was ".wrap { margin: 0 auto }" — a
74
- shorthand on a class selector, higher specificity than "main > section", zeroing
75
- the bottom margin of every .wrap section on the site. Five or six tool calls to
76
- find by elimination; one call here.
77
-
78
- Note it reports the FIRST element matching the selector.`,
79
- {
80
- selector: z.string().min(1)
81
- .describe('CSS selector — the first match is explained. Plain CSS only (no :has-text, >>, nth=).'),
82
- property: z.string().min(1)
83
- .describe('The CSS property to explain, e.g. "margin-bottom", "color", "display". Use the longhand name when you can — asking about "margin" tells you less than asking about "margin-bottom".'),
84
- path: z.string().optional().describe(CANVAS_PATH_DESC),
85
- device: z.enum(['desktop', 'tablet', 'mobile']).optional()
86
- .describe('Viewport width, so media queries resolve the way they do at that breakpoint (default desktop 1440).'),
87
- live: z.boolean().optional().describe(LIVE_PARAM_DESC),
88
- },
89
- async ({ selector, property, path: relPath, device, live }) => {
90
- const asText = (text, isError = false) => ({ content: [{ type: 'text', text }], ...(isError ? { isError: true } : {}) });
91
- const target = await resolveCanvasTarget(workspaceRoot, relPath, sessionId);
92
- if (!target) return asText('No page found.', true);
93
- const guard = requireBrowsable(target);
94
- if (guard) return asText(guard, true);
95
-
96
- const W = SITE_DEVICE_W[device || 'desktop'];
97
- const prop = property.trim().toLowerCase();
98
-
99
- let acq;
100
- try {
101
- acq = await acquireArtifactPage({ projectId, workspaceRoot, target, live, viewport: { width: W, height: 900 } });
102
- const page = acq.page;
103
- const opened = acq;
104
-
105
- const computed = await page.evaluate(([sel, p]) => {
106
- const el = document.querySelector(sel);
107
- if (!el) return null;
108
- return getComputedStyle(el).getPropertyValue(p);
109
- }, [selector, prop]);
110
- if (computed === null) return asText(`Selector matched no elements: ${selector}`, true);
111
-
112
- const cdp = await page.context().newCDPSession(page);
113
- // styleSheetId → 文件名。同一个 selector 出现在两个文件里时(reset + 组件,
114
- // 最常见的那种),光打 `.card` 两行人分不出谁是谁。**必须在 CSS.enable
115
- // 之前挂监听** —— 已有的样式表是在 enable 的那一刻补发的。
116
- const sheetName = new Map();
117
- cdp.on('CSS.styleSheetAdded', (ev) => {
118
- // ⚠️ `isInline` 要**先**判:`<style>` 块的 sourceURL 是文档自己的地址,
119
- // 按 URL 取名会打出 `page.html?nd=raw` 这种(连我们内部的 raw 参数都漏给
120
- // agent 看),而它其实是"页面里那段 <style>"
121
- const url = ev?.header?.sourceURL || '';
122
- const name = ev?.header?.isInline ? '<style> 内联'
123
- : (url ? decodeURIComponent((url.split('?')[0] || url).split('/').pop() || url) : '');
124
- if (ev?.header?.styleSheetId) sheetName.set(ev.header.styleSheetId, name);
125
- });
126
- await cdp.send('DOM.enable');
127
- await cdp.send('CSS.enable');
128
- const { root } = await cdp.send('DOM.getDocument', { depth: -1 });
129
- const { nodeId } = await cdp.send('DOM.querySelector', { nodeId: root.nodeId, selector });
130
- if (!nodeId) return asText(`Selector matched no elements: ${selector}`, true);
131
- const matched = await cdp.send('CSS.getMatchedStylesForNode', { nodeId });
132
-
133
- // 收集:CDP 的 matchedCSSRules 按**优先级递增**排(最后一条最强)
134
- const rows = [];
135
- const push = (where, style, extra = {}) => {
136
- for (const d of style?.cssProperties ?? []) {
137
- if (d.disabled) continue;
138
- const a = affects(String(d.name).toLowerCase(), prop);
139
- if (!a.hit) continue;
140
- // ⚠️ CDP 这一版的 `d.value` **本身就带着** `!important`,再拼一次会打出
141
- // `color: red !important !important` —— 看起来像源码里真写了两遍
142
- const val = String(d.value).replace(/\s*!\s*important\s*$/i, '');
143
- rows.push({
144
- where,
145
- decl: `${d.name}: ${val}${d.important ? ' !important' : ''}`,
146
- important: !!d.important,
147
- viaShorthand: a.viaShorthand,
148
- ...extra,
149
- });
150
- }
151
- };
152
-
153
- let ruleIdx = 0;
154
- for (const m of matched.matchedCSSRules ?? []) {
155
- const sel = m.rule?.selectorList?.text ?? '?';
156
- const sheet = m.rule?.origin === 'user-agent' ? 'browser default'
157
- : (matched.cssKeyframesRules ? null : null);
158
- const media = (m.rule?.media ?? []).map(x => x.text).filter(Boolean).join(' and ');
159
- const from = sheet || sheetName.get(m.rule?.styleSheetId) || '';
160
- push(`${sel}${media ? ` @media ${media}` : ''}${from ? ` [${from}]` : ''}`, m.rule?.style, {
161
- origin: m.rule?.origin,
162
- ruleIdx: ruleIdx++, // ⭐ 合并键,见下面那段注释
163
- });
164
- }
165
- if (matched.inlineStyle) push('element inline style="…"', matched.inlineStyle, { inline: true, ruleIdx: 'inline' });
166
-
167
- // 合并同一条规则里的重复条目。CDP 会把简写和它展开出来的长手**都**列一遍
168
- // (`.wrap { margin: 0 auto }` 出两条:`margin: 0 auto` + `margin-bottom: 0px`),
169
- // 直接打印就是四行里两行是同一件事。留长手那条(它给的是真正生效的值),
170
- // 把"来自哪个简写"作为标注挂上去。
171
- //
172
- // ⛔ 合并键必须是**规则本身**(ruleIdx),不能是 selector 文本。审查实测:
173
- // `reset.css` 里 `.card{color:red!important}` 加 `component.css` 里
174
- // `.card{color:blue}` —— 两条不同规则碰巧同 selector,按文本合并会折成一条,
175
- // `same.decl = r.decl` 留下后者的值(蓝)、`same.important ||=` 又把 red 的
176
- // important 标志过继给它,于是工具报「蓝赢」而浏览器画的是红,**真正的赢家
177
- // 一行都没出现**。正好命中它的目标人群:手写 CSS 上面又叠了 reset/工具类。
178
- const merged = [];
179
- for (const r of rows) {
180
- const same = merged.find(x => x.ruleIdx === r.ruleIdx);
181
- if (!same) { merged.push({ ...r }); continue; }
182
- const rIsLonghand = !r.viaShorthand;
183
- if (rIsLonghand) {
184
- same.decl = r.decl; // 长手的值才是生效值
185
- same.important = same.important || r.important;
186
- } else if (!same.viaShorthand) {
187
- same.viaShorthand = r.viaShorthand; // 把简写来历补到长手那条上
188
- }
189
- if (r.viaShorthand) same.viaShorthand = r.viaShorthand;
190
- }
191
- rows.length = 0;
192
- rows.push(...merged);
193
-
194
- if (!rows.length) {
195
- return asText([
196
- `${selector} → ${prop} computes to "${computed}"`,
197
- '',
198
- 'No declaration anywhere sets this property (directly or via a shorthand).',
199
- 'So the value is inherited or the property\'s initial value — if that surprises you,',
200
- 'the property you wrote is probably not the one you think controls this.',
201
- ].join('\n'));
202
- }
203
-
204
- // 赢家:!important 的最后一条 > 普通的最后一条(inline 已经排在最后)
205
- const importants = rows.filter(r => r.important);
206
- const winner = importants.length ? importants[importants.length - 1] : rows[rows.length - 1];
207
-
208
- const degraded = degradedNote(opened); // 契约:note 非空必须写进返回文本
209
- const lines = [
210
- ...(degraded ? [degraded, ''] : []),
211
- ...(acq.liveNote ? [acq.liveNote, ''] : []),
212
- `${selector} → ${prop} computes to "${computed}" (viewport ${acq.live ? acq.viewport.width : W}px)`,
213
- '',
214
- 'Declarations that matched, weakest first:',
215
- ];
216
- for (const r of rows) {
217
- const mark = r === winner ? '✅ WINS ' : ' lost ';
218
- const via = r.viaShorthand
219
- ? ` ⭐ via the "${r.viaShorthand}" SHORTHAND — it sets ${prop} even though it does not name it`
220
- : '';
221
- lines.push(`${mark}${r.decl}\n from ${r.where}${via}`);
222
- }
223
- if (winner.viaShorthand) {
224
- lines.push('', `⭐ The winning declaration is a shorthand ("${winner.viaShorthand}"). This is the`
225
- + ' single most missable cause of "my CSS says X but the page shows Y": a shorthand on a'
226
- + ' higher-specificity selector silently resets longhands you set elsewhere. Fix by using'
227
- + ` the longhand there, or by setting ${prop} again after it.`);
228
- }
229
- return asText(lines.join('\n'));
230
- } catch (err) {
231
- return asText(`explain_style failed: ${err?.message || String(err)}`, true);
232
- } finally {
233
- await acq?.release?.(); // 一次性:关浏览器;live:松会话锁
234
- }
235
- },
236
- );
237
- }
1
+ /**
2
+ * mcp/tools/explain-style.js — explain_style(2026-08-18)
3
+ *
4
+ * ## 这个洞是什么
5
+ *
6
+ * `get_computed_styles` 告诉你**结果**(marginBottom: 0px),不告诉你**为什么**。
7
+ * 浏览器 devtools 的 Styles 面板会把败下阵的声明划掉,一眼就看得出是谁赢了 ——
8
+ * agent 手里恰恰没有 devtools。
9
+ *
10
+ * 真实案例:用户圈出"这里太挤了、很贴底边"。真因是 `.wrap { margin: 0 auto }`
11
+ * 这条**简写** —— 类选择器优先级高于 `main > section { margin-bottom: 108px }`,
12
+ * 把所有带 .wrap 的节的下边距清成了 0,全站每一节都在吃这个亏。
13
+ * 定位过程:computed 报 0px(对的,但只告诉结果)→ 怀疑外边距塌陷 → 怀疑媒体
14
+ * 查询 → 怀疑变量没解析 → grep 整个 CSS → 都排除后才想到是简写覆盖。
15
+ * **五六轮工具调用,而 devtools 里这是一眼的事。**
16
+ *
17
+ * 这类 bug 在"一份手写 CSS + 后加的工具类"的站点上非常常见 —— 也就是这个平台上
18
+ * 绝大多数站点。
19
+ *
20
+ * ## 实现
21
+ *
22
+ * 走 CDP 的 `CSS.getMatchedStylesForNode`:它按级联次序返回命中该元素的全部规则,
23
+ * 我们从里面挑出跟目标属性有关的声明(**含简写展开**),标出谁赢、谁被压掉、
24
+ * 各自来自哪条规则。
25
+ */
26
+
27
+ import { tool } from '@anthropic-ai/claude-agent-sdk';
28
+ import { z } from 'zod';
29
+ import { resolveCanvasTarget, CANVAS_PATH_DESC, requireBrowsable } from '../../../lib/artifact-target.js';
30
+ import { degradedNote, SITE_DEVICE_W } from './helpers/perception-page.js';
31
+ import { acquireArtifactPage, LIVE_PARAM_DESC } from './helpers/acquire-page.js';
32
+
33
+ /** 简写 → 它能设置的长手属性(只列常踩的那些,够覆盖真实事故) */
34
+ const SHORTHAND_OF = {
35
+ margin: ['margin-top', 'margin-right', 'margin-bottom', 'margin-left'],
36
+ padding: ['padding-top', 'padding-right', 'padding-bottom', 'padding-left'],
37
+ inset: ['top', 'right', 'bottom', 'left'],
38
+ border: ['border-width', 'border-style', 'border-color'],
39
+ 'border-width': ['border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'],
40
+ 'border-radius': ['border-top-left-radius', 'border-top-right-radius', 'border-bottom-right-radius', 'border-bottom-left-radius'],
41
+ background: ['background-color', 'background-image', 'background-position', 'background-size', 'background-repeat'],
42
+ font: ['font-family', 'font-size', 'font-weight', 'font-style', 'line-height'],
43
+ flex: ['flex-grow', 'flex-shrink', 'flex-basis'],
44
+ gap: ['row-gap', 'column-gap'],
45
+ overflow: ['overflow-x', 'overflow-y'],
46
+ transition: ['transition-property', 'transition-duration', 'transition-timing-function', 'transition-delay'],
47
+ animation: ['animation-name', 'animation-duration', 'animation-timing-function'],
48
+ 'grid-template': ['grid-template-rows', 'grid-template-columns'],
49
+ 'place-items': ['align-items', 'justify-items'],
50
+ 'place-content': ['align-content', 'justify-content'],
51
+ };
52
+
53
+ /** 一条声明是否影响目标属性:直接同名,或者是能设置它的简写 */
54
+ function affects(declName, prop) {
55
+ if (declName === prop) return { hit: true, viaShorthand: null };
56
+ const longhands = SHORTHAND_OF[declName];
57
+ if (longhands && longhands.includes(prop)) return { hit: true, viaShorthand: declName };
58
+ return { hit: false };
59
+ }
60
+
61
+ export function makeExplainStyleTool({ workspaceRoot, projectId, sessionId }) {
62
+ return tool(
63
+ 'explain_style',
64
+ `Explain WHY one CSS property has the value it has: every declaration that
65
+ matched the element, in cascade order, with the winner marked and the losers
66
+ listed — including declarations that came from a SHORTHAND.
67
+
68
+ get_computed_styles gives you the result; this gives you the reason. Reach for it
69
+ the moment a value is not what your CSS says it should be, instead of guessing
70
+ through margin collapse / media queries / unresolved variables one at a time.
71
+
72
+ The case this was built for: a section's margin-bottom computed to 0px even
73
+ though the stylesheet said 108px. Cause was ".wrap { margin: 0 auto }" — a
74
+ shorthand on a class selector, higher specificity than "main > section", zeroing
75
+ the bottom margin of every .wrap section on the site. Five or six tool calls to
76
+ find by elimination; one call here.
77
+
78
+ Note it reports the FIRST element matching the selector.`,
79
+ {
80
+ selector: z.string().min(1)
81
+ .describe('CSS selector — the first match is explained. Plain CSS only (no :has-text, >>, nth=).'),
82
+ property: z.string().min(1)
83
+ .describe('The CSS property to explain, e.g. "margin-bottom", "color", "display". Use the longhand name when you can — asking about "margin" tells you less than asking about "margin-bottom".'),
84
+ path: z.string().optional().describe(CANVAS_PATH_DESC),
85
+ device: z.enum(['desktop', 'tablet', 'mobile']).optional()
86
+ .describe('Viewport width, so media queries resolve the way they do at that breakpoint (default desktop 1440).'),
87
+ live: z.boolean().optional().describe(LIVE_PARAM_DESC),
88
+ },
89
+ async ({ selector, property, path: relPath, device, live }) => {
90
+ const asText = (text, isError = false) => ({ content: [{ type: 'text', text }], ...(isError ? { isError: true } : {}) });
91
+ const target = await resolveCanvasTarget(workspaceRoot, relPath, sessionId);
92
+ if (!target) return asText('No page found.', true);
93
+ const guard = requireBrowsable(target);
94
+ if (guard) return asText(guard, true);
95
+
96
+ const W = SITE_DEVICE_W[device || 'desktop'];
97
+ const prop = property.trim().toLowerCase();
98
+
99
+ let acq;
100
+ try {
101
+ acq = await acquireArtifactPage({ projectId, workspaceRoot, target, live, viewport: { width: W, height: 900 } });
102
+ const page = acq.page;
103
+ const opened = acq;
104
+
105
+ const computed = await page.evaluate(([sel, p]) => {
106
+ const el = document.querySelector(sel);
107
+ if (!el) return null;
108
+ return getComputedStyle(el).getPropertyValue(p);
109
+ }, [selector, prop]);
110
+ if (computed === null) return asText(`Selector matched no elements: ${selector}`, true);
111
+
112
+ const cdp = await page.context().newCDPSession(page);
113
+ // styleSheetId → 文件名。同一个 selector 出现在两个文件里时(reset + 组件,
114
+ // 最常见的那种),光打 `.card` 两行人分不出谁是谁。**必须在 CSS.enable
115
+ // 之前挂监听** —— 已有的样式表是在 enable 的那一刻补发的。
116
+ const sheetName = new Map();
117
+ cdp.on('CSS.styleSheetAdded', (ev) => {
118
+ // ⚠️ `isInline` 要**先**判:`<style>` 块的 sourceURL 是文档自己的地址,
119
+ // 按 URL 取名会打出 `page.html?nd=raw` 这种(连我们内部的 raw 参数都漏给
120
+ // agent 看),而它其实是"页面里那段 <style>"
121
+ const url = ev?.header?.sourceURL || '';
122
+ const name = ev?.header?.isInline ? '<style> 内联'
123
+ : (url ? decodeURIComponent((url.split('?')[0] || url).split('/').pop() || url) : '');
124
+ if (ev?.header?.styleSheetId) sheetName.set(ev.header.styleSheetId, name);
125
+ });
126
+ await cdp.send('DOM.enable');
127
+ await cdp.send('CSS.enable');
128
+ const { root } = await cdp.send('DOM.getDocument', { depth: -1 });
129
+ const { nodeId } = await cdp.send('DOM.querySelector', { nodeId: root.nodeId, selector });
130
+ if (!nodeId) return asText(`Selector matched no elements: ${selector}`, true);
131
+ const matched = await cdp.send('CSS.getMatchedStylesForNode', { nodeId });
132
+
133
+ // 收集:CDP 的 matchedCSSRules 按**优先级递增**排(最后一条最强)
134
+ const rows = [];
135
+ const push = (where, style, extra = {}) => {
136
+ for (const d of style?.cssProperties ?? []) {
137
+ if (d.disabled) continue;
138
+ const a = affects(String(d.name).toLowerCase(), prop);
139
+ if (!a.hit) continue;
140
+ // ⚠️ CDP 这一版的 `d.value` **本身就带着** `!important`,再拼一次会打出
141
+ // `color: red !important !important` —— 看起来像源码里真写了两遍
142
+ const val = String(d.value).replace(/\s*!\s*important\s*$/i, '');
143
+ rows.push({
144
+ where,
145
+ decl: `${d.name}: ${val}${d.important ? ' !important' : ''}`,
146
+ important: !!d.important,
147
+ viaShorthand: a.viaShorthand,
148
+ ...extra,
149
+ });
150
+ }
151
+ };
152
+
153
+ let ruleIdx = 0;
154
+ for (const m of matched.matchedCSSRules ?? []) {
155
+ const sel = m.rule?.selectorList?.text ?? '?';
156
+ const sheet = m.rule?.origin === 'user-agent' ? 'browser default'
157
+ : (matched.cssKeyframesRules ? null : null);
158
+ const media = (m.rule?.media ?? []).map(x => x.text).filter(Boolean).join(' and ');
159
+ const from = sheet || sheetName.get(m.rule?.styleSheetId) || '';
160
+ push(`${sel}${media ? ` @media ${media}` : ''}${from ? ` [${from}]` : ''}`, m.rule?.style, {
161
+ origin: m.rule?.origin,
162
+ ruleIdx: ruleIdx++, // ⭐ 合并键,见下面那段注释
163
+ });
164
+ }
165
+ if (matched.inlineStyle) push('element inline style="…"', matched.inlineStyle, { inline: true, ruleIdx: 'inline' });
166
+
167
+ // 合并同一条规则里的重复条目。CDP 会把简写和它展开出来的长手**都**列一遍
168
+ // (`.wrap { margin: 0 auto }` 出两条:`margin: 0 auto` + `margin-bottom: 0px`),
169
+ // 直接打印就是四行里两行是同一件事。留长手那条(它给的是真正生效的值),
170
+ // 把"来自哪个简写"作为标注挂上去。
171
+ //
172
+ // ⛔ 合并键必须是**规则本身**(ruleIdx),不能是 selector 文本。审查实测:
173
+ // `reset.css` 里 `.card{color:red!important}` 加 `component.css` 里
174
+ // `.card{color:blue}` —— 两条不同规则碰巧同 selector,按文本合并会折成一条,
175
+ // `same.decl = r.decl` 留下后者的值(蓝)、`same.important ||=` 又把 red 的
176
+ // important 标志过继给它,于是工具报「蓝赢」而浏览器画的是红,**真正的赢家
177
+ // 一行都没出现**。正好命中它的目标人群:手写 CSS 上面又叠了 reset/工具类。
178
+ const merged = [];
179
+ for (const r of rows) {
180
+ const same = merged.find(x => x.ruleIdx === r.ruleIdx);
181
+ if (!same) { merged.push({ ...r }); continue; }
182
+ const rIsLonghand = !r.viaShorthand;
183
+ if (rIsLonghand) {
184
+ same.decl = r.decl; // 长手的值才是生效值
185
+ same.important = same.important || r.important;
186
+ } else if (!same.viaShorthand) {
187
+ same.viaShorthand = r.viaShorthand; // 把简写来历补到长手那条上
188
+ }
189
+ if (r.viaShorthand) same.viaShorthand = r.viaShorthand;
190
+ }
191
+ rows.length = 0;
192
+ rows.push(...merged);
193
+
194
+ if (!rows.length) {
195
+ return asText([
196
+ `${selector} → ${prop} computes to "${computed}"`,
197
+ '',
198
+ 'No declaration anywhere sets this property (directly or via a shorthand).',
199
+ 'So the value is inherited or the property\'s initial value — if that surprises you,',
200
+ 'the property you wrote is probably not the one you think controls this.',
201
+ ].join('\n'));
202
+ }
203
+
204
+ // 赢家:!important 的最后一条 > 普通的最后一条(inline 已经排在最后)
205
+ const importants = rows.filter(r => r.important);
206
+ const winner = importants.length ? importants[importants.length - 1] : rows[rows.length - 1];
207
+
208
+ const degraded = degradedNote(opened); // 契约:note 非空必须写进返回文本
209
+ const lines = [
210
+ ...(degraded ? [degraded, ''] : []),
211
+ ...(acq.liveNote ? [acq.liveNote, ''] : []),
212
+ `${selector} → ${prop} computes to "${computed}" (viewport ${acq.live ? acq.viewport.width : W}px)`,
213
+ '',
214
+ 'Declarations that matched, weakest first:',
215
+ ];
216
+ for (const r of rows) {
217
+ const mark = r === winner ? '✅ WINS ' : ' lost ';
218
+ const via = r.viaShorthand
219
+ ? ` ⭐ via the "${r.viaShorthand}" SHORTHAND — it sets ${prop} even though it does not name it`
220
+ : '';
221
+ lines.push(`${mark}${r.decl}\n from ${r.where}${via}`);
222
+ }
223
+ if (winner.viaShorthand) {
224
+ lines.push('', `⭐ The winning declaration is a shorthand ("${winner.viaShorthand}"). This is the`
225
+ + ' single most missable cause of "my CSS says X but the page shows Y": a shorthand on a'
226
+ + ' higher-specificity selector silently resets longhands you set elsewhere. Fix by using'
227
+ + ` the longhand there, or by setting ${prop} again after it.`);
228
+ }
229
+ return asText(lines.join('\n'));
230
+ } catch (err) {
231
+ return asText(`explain_style failed: ${err?.message || String(err)}`, true);
232
+ } finally {
233
+ await acq?.release?.(); // 一次性:关浏览器;live:松会话锁
234
+ }
235
+ },
236
+ );
237
+ }