@pairdraw/core 0.16.2 → 0.18.0

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 (257) hide show
  1. package/dist/canvas/outline-stroke.d.ts.map +1 -1
  2. package/dist/canvas/outline-stroke.js.map +1 -1
  3. package/dist/canvas/overlays/prototype.d.ts +2 -5
  4. package/dist/canvas/overlays/prototype.d.ts.map +1 -1
  5. package/dist/canvas/overlays/prototype.js +3 -19
  6. package/dist/canvas/overlays/prototype.js.map +1 -1
  7. package/dist/canvas/renderer/fonts.js +2 -1
  8. package/dist/canvas/renderer/fonts.js.map +1 -1
  9. package/dist/canvas/renderer.d.ts +4 -0
  10. package/dist/canvas/renderer.d.ts.map +1 -1
  11. package/dist/canvas/renderer.js +4 -0
  12. package/dist/canvas/renderer.js.map +1 -1
  13. package/dist/canvas/rulers.js +1 -1
  14. package/dist/canvas/rulers.js.map +1 -1
  15. package/dist/canvas/scene.d.ts.map +1 -1
  16. package/dist/canvas/scene.js +61 -37
  17. package/dist/canvas/scene.js.map +1 -1
  18. package/dist/canvas/text/font-families.d.ts +4 -3
  19. package/dist/canvas/text/font-families.d.ts.map +1 -1
  20. package/dist/canvas/text/font-families.js +8 -7
  21. package/dist/canvas/text/font-families.js.map +1 -1
  22. package/dist/canvas/text/index.d.ts.map +1 -1
  23. package/dist/canvas/text/index.js +1 -1
  24. package/dist/canvas/text/index.js.map +1 -1
  25. package/dist/canvas/text/paragraph/index.js +2 -2
  26. package/dist/canvas/text/paragraph/index.js.map +1 -1
  27. package/dist/canvas/text/placed.js +2 -2
  28. package/dist/canvas/text/placed.js.map +1 -1
  29. package/dist/canvas/text/renderer.d.ts +2 -0
  30. package/dist/canvas/text/renderer.d.ts.map +1 -1
  31. package/dist/clipboard/pairdraw.d.ts.map +1 -1
  32. package/dist/clipboard/pairdraw.js +53 -7
  33. package/dist/clipboard/pairdraw.js.map +1 -1
  34. package/dist/design-jsx/preview/picture.js +6 -1
  35. package/dist/design-jsx/preview/picture.js.map +1 -1
  36. package/dist/design-jsx/renderer.d.ts.map +1 -1
  37. package/dist/editor/alignment.d.ts.map +1 -1
  38. package/dist/editor/alignment.js +35 -27
  39. package/dist/editor/alignment.js.map +1 -1
  40. package/dist/editor/bridges/undo.d.ts +1 -1
  41. package/dist/editor/clipboard/detach.d.ts.map +1 -1
  42. package/dist/editor/clipboard/detach.js +3 -3
  43. package/dist/editor/clipboard/detach.js.map +1 -1
  44. package/dist/editor/clipboard/paste/properties.d.ts.map +1 -1
  45. package/dist/editor/clipboard/paste/properties.js +3 -1
  46. package/dist/editor/clipboard/paste/properties.js.map +1 -1
  47. package/dist/editor/create.d.ts +1 -1
  48. package/dist/editor/structure.d.ts.map +1 -1
  49. package/dist/editor/structure.js +3 -1
  50. package/dist/editor/structure.js.map +1 -1
  51. package/dist/editor/viewport.js +2 -2
  52. package/dist/editor/viewport.js.map +1 -1
  53. package/dist/figma-api/index.d.ts +43 -8
  54. package/dist/figma-api/index.d.ts.map +1 -1
  55. package/dist/figma-api/index.js +48 -6
  56. package/dist/figma-api/index.js.map +1 -1
  57. package/dist/figma-api/serialization.js +10 -0
  58. package/dist/figma-api/serialization.js.map +1 -1
  59. package/dist/figma-api/text/bindings.d.ts.map +1 -1
  60. package/dist/figma-api/text/bindings.js +20 -8
  61. package/dist/figma-api/text/bindings.js.map +1 -1
  62. package/dist/figma-api/text/index.js +1 -1
  63. package/dist/geometry/connectors.js +2 -14
  64. package/dist/geometry/connectors.js.map +1 -1
  65. package/dist/geometry/index.d.ts +2 -2
  66. package/dist/geometry/scene.d.ts +3 -1
  67. package/dist/geometry/scene.d.ts.map +1 -1
  68. package/dist/geometry/scene.js +16 -1
  69. package/dist/geometry/scene.js.map +1 -1
  70. package/dist/geometry/types.d.ts +7 -1
  71. package/dist/geometry/types.d.ts.map +1 -1
  72. package/dist/io/formats/drawio/export.d.ts.map +1 -1
  73. package/dist/io/formats/drawio/export.js +10 -0
  74. package/dist/io/formats/drawio/export.js.map +1 -1
  75. package/dist/io/formats/drawio/index.d.ts.map +1 -1
  76. package/dist/io/formats/drawio/index.js +32 -1
  77. package/dist/io/formats/drawio/index.js.map +1 -1
  78. package/dist/io/formats/drawio/shapes.d.ts +10 -5
  79. package/dist/io/formats/drawio/shapes.d.ts.map +1 -1
  80. package/dist/io/formats/drawio/shapes.js +10 -6
  81. package/dist/io/formats/drawio/shapes.js.map +1 -1
  82. package/dist/io/formats/drawio/warnings.d.ts +11 -0
  83. package/dist/io/formats/drawio/warnings.d.ts.map +1 -0
  84. package/dist/io/formats/drawio/warnings.js +23 -0
  85. package/dist/io/formats/drawio/warnings.js.map +1 -0
  86. package/dist/io/formats/pdf/dom.d.ts +10 -1
  87. package/dist/io/formats/pdf/dom.d.ts.map +1 -1
  88. package/dist/io/formats/pdf/dom.js +31 -1
  89. package/dist/io/formats/pdf/dom.js.map +1 -1
  90. package/dist/io/formats/pdf/export.js +3 -2
  91. package/dist/io/formats/pdf/export.js.map +1 -1
  92. package/dist/io/formats/raster/headless.d.ts +2 -2
  93. package/dist/io/formats/raster/headless.d.ts.map +1 -1
  94. package/dist/io/formats/raster/headless.js +2 -2
  95. package/dist/io/formats/raster/headless.js.map +1 -1
  96. package/dist/io/formats/raster/render.d.ts +1 -1
  97. package/dist/io/formats/raster/render.d.ts.map +1 -1
  98. package/dist/io/formats/raster/render.js +2 -2
  99. package/dist/io/formats/raster/render.js.map +1 -1
  100. package/dist/io/formats.d.ts.map +1 -1
  101. package/dist/io/formats.js +6 -6
  102. package/dist/io/formats.js.map +1 -1
  103. package/dist/io/registry.d.ts +2 -0
  104. package/dist/io/registry.d.ts.map +1 -1
  105. package/dist/io/registry.js +5 -0
  106. package/dist/io/registry.js.map +1 -1
  107. package/dist/io/types.d.ts +2 -0
  108. package/dist/io/types.d.ts.map +1 -1
  109. package/dist/library/snapshot.d.ts.map +1 -1
  110. package/dist/library/snapshot.js +1 -0
  111. package/dist/library/snapshot.js.map +1 -1
  112. package/dist/lint/linter.d.ts.map +1 -1
  113. package/dist/lint/linter.js +11 -2
  114. package/dist/lint/linter.js.map +1 -1
  115. package/dist/rpc/variables-command.d.ts.map +1 -1
  116. package/dist/rpc/variables-command.js +4 -2
  117. package/dist/rpc/variables-command.js.map +1 -1
  118. package/dist/text/character-style.d.ts.map +1 -1
  119. package/dist/text/character-style.js +2 -1
  120. package/dist/text/character-style.js.map +1 -1
  121. package/dist/text/coverage.d.ts.map +1 -1
  122. package/dist/text/coverage.js +6 -3
  123. package/dist/text/coverage.js.map +1 -1
  124. package/dist/text/fallbacks.d.ts +8 -2
  125. package/dist/text/fallbacks.d.ts.map +1 -1
  126. package/dist/text/fallbacks.js +73 -7
  127. package/dist/text/fallbacks.js.map +1 -1
  128. package/dist/text/font/manager.d.ts +14 -6
  129. package/dist/text/font/manager.d.ts.map +1 -1
  130. package/dist/text/font/manager.js +40 -14
  131. package/dist/text/font/manager.js.map +1 -1
  132. package/dist/text/index.d.ts +2 -2
  133. package/dist/text/index.js +3 -3
  134. package/dist/text/outlines.js +1 -1
  135. package/dist/text/outlines.js.map +1 -1
  136. package/dist/text/path/caret.d.ts +2 -4
  137. package/dist/text/path/caret.d.ts.map +1 -1
  138. package/dist/text/path/caret.js.map +1 -1
  139. package/dist/text/prepare.d.ts +5 -1
  140. package/dist/text/prepare.d.ts.map +1 -1
  141. package/dist/text/prepare.js +19 -2
  142. package/dist/text/prepare.js.map +1 -1
  143. package/dist/text/web-font/assets.d.ts.map +1 -1
  144. package/dist/text/web-font/assets.js +13 -21
  145. package/dist/text/web-font/assets.js.map +1 -1
  146. package/dist/text/web-font/resolver.d.ts.map +1 -1
  147. package/dist/text/web-font/resolver.js +4 -1
  148. package/dist/text/web-font/resolver.js.map +1 -1
  149. package/dist/tools/create/components.d.ts +2 -1
  150. package/dist/tools/create/components.d.ts.map +1 -1
  151. package/dist/tools/create/components.js +27 -1
  152. package/dist/tools/create/components.js.map +1 -1
  153. package/dist/tools/create/icons.d.ts.map +1 -1
  154. package/dist/tools/create/icons.js +11 -5
  155. package/dist/tools/create/icons.js.map +1 -1
  156. package/dist/tools/create/vector.d.ts.map +1 -1
  157. package/dist/tools/create/vector.js +9 -6
  158. package/dist/tools/create/vector.js.map +1 -1
  159. package/dist/tools/create.d.ts +2 -2
  160. package/dist/tools/create.js +2 -2
  161. package/dist/tools/modify/effects.d.ts.map +1 -1
  162. package/dist/tools/modify/effects.js +5 -3
  163. package/dist/tools/modify/effects.js.map +1 -1
  164. package/dist/tools/modify/matching.d.ts.map +1 -1
  165. package/dist/tools/modify/matching.js +9 -6
  166. package/dist/tools/modify/matching.js.map +1 -1
  167. package/dist/tools/modify/paint.d.ts.map +1 -1
  168. package/dist/tools/modify/paint.js +7 -5
  169. package/dist/tools/modify/paint.js.map +1 -1
  170. package/dist/tools/modify/text.d.ts.map +1 -1
  171. package/dist/tools/modify/text.js +28 -8
  172. package/dist/tools/modify/text.js.map +1 -1
  173. package/dist/tools/read/libraries.d.ts.map +1 -1
  174. package/dist/tools/read/libraries.js +20 -8
  175. package/dist/tools/read/libraries.js.map +1 -1
  176. package/dist/tools/read/pages.d.ts.map +1 -1
  177. package/dist/tools/read/pages.js.map +1 -1
  178. package/dist/tools/registry/extended.d.ts.map +1 -1
  179. package/dist/tools/registry/extended.js +2 -1
  180. package/dist/tools/registry/extended.js.map +1 -1
  181. package/dist/tools/schema.d.ts +24 -5
  182. package/dist/tools/schema.d.ts.map +1 -1
  183. package/dist/tools/schema.js +80 -6
  184. package/dist/tools/schema.js.map +1 -1
  185. package/dist/tools/structure/batch.d.ts.map +1 -1
  186. package/dist/tools/structure/batch.js +4 -3
  187. package/dist/tools/structure/batch.js.map +1 -1
  188. package/dist/tools/variables/values.d.ts.map +1 -1
  189. package/dist/tools/variables/values.js +4 -2
  190. package/dist/tools/variables/values.js.map +1 -1
  191. package/dist/tools/vector/export.d.ts.map +1 -1
  192. package/dist/tools/vector/export.js +5 -2
  193. package/dist/tools/vector/export.js.map +1 -1
  194. package/package.json +9 -9
  195. package/src/canvas/outline-stroke.ts +2 -6
  196. package/src/canvas/overlays/prototype.ts +5 -27
  197. package/src/canvas/renderer/fonts.ts +2 -1
  198. package/src/canvas/renderer.ts +4 -0
  199. package/src/canvas/rulers.ts +1 -1
  200. package/src/canvas/scene.ts +92 -63
  201. package/src/canvas/text/font-families.ts +8 -7
  202. package/src/canvas/text/index.ts +3 -1
  203. package/src/canvas/text/paragraph/index.ts +2 -2
  204. package/src/canvas/text/placed.ts +2 -2
  205. package/src/canvas/text/renderer.ts +2 -0
  206. package/src/clipboard/pairdraw.ts +51 -7
  207. package/src/design-jsx/preview/picture.ts +8 -2
  208. package/src/editor/alignment.ts +33 -29
  209. package/src/editor/clipboard/detach.ts +9 -3
  210. package/src/editor/clipboard/paste/properties.ts +3 -1
  211. package/src/editor/structure.ts +3 -1
  212. package/src/editor/viewport.ts +2 -2
  213. package/src/figma-api/index.ts +90 -9
  214. package/src/figma-api/serialization.ts +14 -0
  215. package/src/figma-api/text/bindings.ts +26 -8
  216. package/src/geometry/connectors.ts +2 -12
  217. package/src/geometry/index.ts +1 -1
  218. package/src/geometry/scene.ts +19 -0
  219. package/src/geometry/types.ts +8 -0
  220. package/src/io/formats/drawio/export.ts +11 -0
  221. package/src/io/formats/drawio/index.ts +36 -1
  222. package/src/io/formats/drawio/shapes.ts +13 -9
  223. package/src/io/formats/drawio/warnings.ts +27 -0
  224. package/src/io/formats/pdf/dom.ts +29 -0
  225. package/src/io/formats/pdf/export.ts +3 -2
  226. package/src/io/formats/raster/headless.ts +10 -3
  227. package/src/io/formats/raster/render.ts +4 -2
  228. package/src/io/formats.ts +8 -10
  229. package/src/io/registry.ts +12 -0
  230. package/src/io/types.ts +2 -0
  231. package/src/library/snapshot.ts +1 -0
  232. package/src/lint/linter.ts +12 -2
  233. package/src/rpc/variables-command.ts +6 -5
  234. package/src/text/character-style.ts +8 -4
  235. package/src/text/coverage.ts +17 -2
  236. package/src/text/fallbacks.ts +81 -7
  237. package/src/text/font/manager.ts +50 -17
  238. package/src/text/outlines.ts +1 -1
  239. package/src/text/path/caret.ts +3 -4
  240. package/src/text/prepare.ts +34 -2
  241. package/src/text/web-font/assets.ts +11 -15
  242. package/src/text/web-font/resolver.ts +7 -1
  243. package/src/tools/create/components.ts +33 -0
  244. package/src/tools/create/icons.ts +13 -5
  245. package/src/tools/create/vector.ts +16 -6
  246. package/src/tools/create.ts +1 -0
  247. package/src/tools/modify/effects.ts +11 -4
  248. package/src/tools/modify/matching.ts +10 -6
  249. package/src/tools/modify/paint.ts +13 -5
  250. package/src/tools/modify/text.ts +53 -20
  251. package/src/tools/read/libraries.ts +35 -11
  252. package/src/tools/read/pages.ts +1 -0
  253. package/src/tools/registry/extended.ts +2 -0
  254. package/src/tools/schema.ts +109 -5
  255. package/src/tools/structure/batch.ts +10 -5
  256. package/src/tools/variables/values.ts +6 -3
  257. package/src/tools/vector/export.ts +5 -1
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { parseColor } from '@pairdraw/scene-graph/color'
3
+ import type { Color } from '@pairdraw/scene-graph/primitives'
4
4
 
5
5
  import type { FigmaAPI, FigmaNodeProxy } from '#core/figma-api'
6
6
  import type { FigmaFontName, FigmaFontNameInput } from '#core/figma-api/fonts/names'
@@ -9,6 +9,7 @@ import { PairdrawAPI } from '#core/pairdraw-api'
9
9
  import { normalizeLinkURL } from '#core/text/hyperlink'
10
10
  import { collectNodeFontFaces } from '#core/text/requirements/collect'
11
11
  import {
12
+ parseOptionalToolColor,
12
13
  toolNumber,
13
14
  nodeIdInputSchema,
14
15
  defineTool,
@@ -90,9 +91,11 @@ export const setText = defineTool({
90
91
  })
91
92
 
92
93
  /** Variable font axes a font tool can set, as Figma's `variationSettings`. */
94
+ // An object with a value for every key rather than a record: a record publishes `propertyNames`,
95
+ // which OpenAI refuses, and a JSON object's keys are strings anyway.
93
96
  const variationsInputSchema = v.optional(
94
97
  v.pipe(
95
- v.record(v.pipe(v.string(), v.length(4)), v.number()),
98
+ v.objectWithRest({}, v.number()),
96
99
  v.description(
97
100
  'Variable font axis values by OpenType tag (e.g. {"wght": 550, "wdth": 75}); axes left out keep the style\'s values. Without a style, the nearest named style is picked.'
98
101
  )
@@ -162,6 +165,8 @@ export const setFont = defineTool({
162
165
  execute: (figma, args) => {
163
166
  const node = figma.getNodeById(args.id)
164
167
  if (!node) return nodeNotFound(args.id)
168
+ const badTag = badVariationTag(args.variations)
169
+ if (badTag) return badTag
165
170
  const font = requestedFont(node, args)
166
171
  try {
167
172
  if (font && 'whole' in font) node.fontName = font.whole
@@ -185,6 +190,46 @@ function rangeFont(
185
190
  return fontNameInput(args, typeof current === 'symbol' ? null : current)
186
191
  }
187
192
 
193
+ type RangeEdit = {
194
+ hyperlink: { link: { type: 'URL'; value: string } | null } | null
195
+ color: Color | undefined
196
+ }
197
+
198
+ /** Why a variation setting cannot apply: an axis tag is not four letters. */
199
+ function badVariationTag(variations: FigmaVariationSettings | undefined): { error: string } | null {
200
+ const tag = Object.keys(variations ?? {}).find((key) => key.length !== 4)
201
+ return tag === undefined ? null : { error: `"${tag}" is not a four-letter OpenType axis tag` }
202
+ }
203
+
204
+ /** What a range edit sets, or why it cannot run: nothing named, a bad colour, or a bad link. */
205
+ function rangeEdit(args: {
206
+ family?: string
207
+ size?: number
208
+ style?: string
209
+ variations?: unknown
210
+ color?: string
211
+ hyperlink?: string | null
212
+ }): RangeEdit | { error: string } {
213
+ if (
214
+ !args.family &&
215
+ !args.size &&
216
+ !args.style &&
217
+ !args.variations &&
218
+ !args.color &&
219
+ args.hyperlink === undefined
220
+ ) {
221
+ return {
222
+ error:
223
+ 'set_font_range needs at least one of family, size, style, variations, color, or hyperlink'
224
+ }
225
+ }
226
+ const hyperlink = requestedHyperlink(args.hyperlink)
227
+ if (hyperlink && 'error' in hyperlink) return hyperlink
228
+ const color = parseOptionalToolColor(args.color)
229
+ if ('error' in color) return color
230
+ return { hyperlink: hyperlink ?? null, color: color.color }
231
+ }
232
+
188
233
  export const setFontRange = defineTool({
189
234
  name: 'set_font_range',
190
235
 
@@ -213,30 +258,18 @@ export const setFontRange = defineTool({
213
258
  const node = figma.getNodeById(args.id)
214
259
  if (!node) return nodeNotFound(args.id)
215
260
  const { start, end } = args
216
- if (
217
- !args.family &&
218
- !args.size &&
219
- !args.style &&
220
- !args.variations &&
221
- !args.color &&
222
- args.hyperlink === undefined
223
- ) {
224
- return {
225
- error:
226
- 'set_font_range needs at least one of family, size, style, variations, color, or hyperlink'
227
- }
228
- }
229
- const hyperlink = requestedHyperlink(args.hyperlink)
230
- if (hyperlink && 'error' in hyperlink) return hyperlink
261
+ const badTag = badVariationTag(args.variations)
262
+ if (badTag) return badTag
263
+ const edit = rangeEdit(args)
264
+ if ('error' in edit) return edit
265
+ const { hyperlink, color } = edit
231
266
  // The range methods reject an empty range or one past the text, before changing anything.
232
267
  try {
233
268
  if (args.size) node.setRangeFontSize(start, end, args.size)
234
269
  if (args.family || args.style || args.variations) {
235
270
  node.setRangeFontName(start, end, rangeFont(node, args))
236
271
  }
237
- if (args.color) {
238
- node.setRangeFills(start, end, [{ type: 'SOLID', color: parseColor(args.color) }])
239
- }
272
+ if (color) node.setRangeFills(start, end, [{ type: 'SOLID', color }])
240
273
  if (hyperlink) node.setRangeHyperlink(start, end, hyperlink.link)
241
274
  } catch (error) {
242
275
  return toolFailure(error)
@@ -1,18 +1,44 @@
1
+ import { groupBy, isPlainObject } from 'es-toolkit'
1
2
  import * as v from 'valibot'
2
3
 
3
4
  import { getComponentCatalog } from '#core/tools/component-catalog'
4
5
  import { toolNumber, defineTool } from '#core/tools/schema'
5
6
 
7
+ const variantValuesSchema = v.pipe(
8
+ v.string(),
9
+ v.parseJson(),
10
+ // A record also takes an array, as its indices.
11
+ v.check(isPlainObject, 'Expected a JSON object'),
12
+ v.record(v.string(), v.string())
13
+ )
14
+
6
15
  export const listLibraries = defineTool({
7
16
  name: 'list_libraries',
8
- description: 'List available published component libraries.',
17
+ description:
18
+ 'List available published component libraries and each component asset key, name, and type. Pass a key to insert_library_component. A library does not have to be enabled to be listed.',
9
19
  execution: { kind: 'async', mutation: 'none' },
10
20
  exposure: { webmcp: false },
11
21
  input: v.strictObject({}),
12
22
  execute: async (figma) => {
13
23
  const catalog = getComponentCatalog(figma.graph)
14
- const libraries = catalog ? await catalog.listLibraries() : []
15
- return { count: libraries.length, libraries }
24
+ if (!catalog) return { count: 0, libraries: [] }
25
+ const [libraries, components] = await Promise.all([
26
+ catalog.listLibraries(),
27
+ // Enabled-only would hide the keys of a library the document has not turned on.
28
+ catalog.listComponents({ enabledOnly: false })
29
+ ])
30
+ const byLibrary = groupBy(components, (component) => component.libraryId)
31
+ return {
32
+ count: libraries.length,
33
+ libraries: libraries.map((library) => ({
34
+ ...library,
35
+ assets: (byLibrary[library.libraryId] ?? []).map((component) => ({
36
+ key: component.asset.key,
37
+ name: component.asset.name,
38
+ type: component.asset.type
39
+ }))
40
+ }))
41
+ }
16
42
  }
17
43
  })
18
44
 
@@ -38,15 +64,13 @@ export const insertLibraryComponent = defineTool({
38
64
  if (!catalog) throw new Error('No component library catalog is configured')
39
65
  let variantValues: Record<string, string> | undefined
40
66
  if (args.variant_values) {
41
- const parsed: unknown = JSON.parse(args.variant_values)
42
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
43
- throw new TypeError('variant_values must be a JSON object')
44
- }
45
- const entries = Object.entries(parsed)
46
- if (entries.some(([, value]) => typeof value !== 'string')) {
47
- throw new TypeError('variant_values values must be strings')
67
+ const parsed = v.safeParse(variantValuesSchema, args.variant_values)
68
+ if (!parsed.success) {
69
+ throw new TypeError(
70
+ `variant_values must be a JSON object of strings: ${v.summarize(parsed.issues)}`
71
+ )
48
72
  }
49
- variantValues = Object.fromEntries(entries)
73
+ variantValues = parsed.output
50
74
  }
51
75
  return catalog.insertComponent({
52
76
  libraryId: args.library_id,
@@ -30,6 +30,7 @@ export const switchPage = defineTool({
30
30
  const target =
31
31
  figma.root.children.find((candidate) => candidate.name === page) ?? figma.getNodeById(page)
32
32
  if (!target) return { error: `Page "${page}" not found` }
33
+ // The host shows the page from here, and reads it back on the next call.
33
34
  figma.currentPage = target
34
35
  return { page: target.name, id: target.id }
35
36
  }
@@ -19,6 +19,7 @@ import {
19
19
  createShape,
20
20
  createSlice,
21
21
  createVector,
22
+ detachInstance,
22
23
  exposeInstanceSwap,
23
24
  combineAsVariants,
24
25
  createSlot,
@@ -159,6 +160,7 @@ export const EXTENDED_TOOLS: ToolDef[] = [
159
160
  combineAsVariants,
160
161
  createComponent,
161
162
  createInstance,
163
+ detachInstance,
162
164
  exposeInstanceSwap,
163
165
  createSlot,
164
166
  getBehaviour,
@@ -8,6 +8,8 @@
8
8
  import * as v from 'valibot'
9
9
 
10
10
  import type { SceneNode } from '@pairdraw/scene-graph'
11
+ import { tryParseColor } from '@pairdraw/scene-graph/color'
12
+ import type { Color } from '@pairdraw/scene-graph/primitives'
11
13
 
12
14
  import type { FigmaAPI, FigmaNodeProxy } from '#core/figma-api'
13
15
  import type { FigmaFontNameInput } from '#core/figma-api/fonts/names'
@@ -90,10 +92,11 @@ export function defineTool<P extends v.ObjectEntries, R>(
90
92
  }
91
93
 
92
94
  /**
93
- * Every tool run goes through here, so a wrong call from AI chat, the CLI, or WebMCP names the
94
- * tool and lists each problem with its argument, as `v.summarize` formats them. MCP clients get
95
- * the MCP SDK's own report, which validates the same schema before the handler runs. Inputs are
96
- * strict objects, so a misspelled or mis-nested argument fails here instead of being dropped.
95
+ * Every tool run goes through here, so a wrong call from AI chat, the CLI, WebMCP, and MCP names
96
+ * the tool and lists each problem with its argument. An unknown or missing argument names the
97
+ * arguments the tool takes, and a picklist names the values it accepts, read from the schema, so
98
+ * the caller does not have to guess. Inputs are strict objects, so a misspelled or mis-nested
99
+ * argument fails here instead of being dropped.
97
100
  */
98
101
  export function parseToolArgs<S extends v.GenericSchema>(
99
102
  name: string,
@@ -102,7 +105,82 @@ export function parseToolArgs<S extends v.GenericSchema>(
102
105
  ): v.InferOutput<S> {
103
106
  const result = v.safeParse(schema, args)
104
107
  if (result.success) return result.output
105
- throw new Error(`Invalid arguments for ${name}:\n${v.summarize(result.issues)}`)
108
+ throw new Error(formatToolArgumentError(name, schema, result.issues))
109
+ }
110
+
111
+ /** One refusal for every surface: the tool, then each problem with where it is. */
112
+ function formatToolArgumentError(
113
+ name: string,
114
+ schema: v.GenericSchema,
115
+ issues: readonly v.BaseIssue<unknown>[]
116
+ ): string {
117
+ const problems = issues.map((issue) => {
118
+ const keys = issueKeys(issue)
119
+ const place = keys.length > 0 ? `\n → at ${keys.join('.')}` : ''
120
+ return `× ${formatToolArgIssue(schema, issue)}${place}`
121
+ })
122
+ return `Invalid arguments for ${name}:\n${problems.join('\n')}`
123
+ }
124
+
125
+ /** The parts of a Valibot schema this module reads to name what a call should have sent. */
126
+ type SchemaParts = {
127
+ readonly entries?: v.ObjectEntries
128
+ readonly item?: v.GenericSchema
129
+ readonly wrapped?: v.GenericSchema
130
+ readonly options?: readonly unknown[]
131
+ }
132
+
133
+ function schemaParts(schema: unknown): SchemaParts {
134
+ return typeof schema === 'object' && schema !== null ? schema : {}
135
+ }
136
+
137
+ /** The schema a wrapper such as `optional` or `pipe` holds, down to the one with the rules. */
138
+ function unwrappedSchema(schema: v.GenericSchema): v.GenericSchema {
139
+ let current = schema
140
+ for (;;) {
141
+ const wrapped = schemaParts(current).wrapped
142
+ if (!wrapped) return current
143
+ current = wrapped
144
+ }
145
+ }
146
+
147
+ function issueKeys(issue: v.BaseIssue<unknown>): string[] {
148
+ return (issue.path ?? []).flatMap((item) => ('key' in item ? [String(item.key)] : []))
149
+ }
150
+
151
+ function schemaAtPath(root: v.GenericSchema, keys: readonly string[]): v.GenericSchema | null {
152
+ let current = unwrappedSchema(root)
153
+ for (const key of keys) {
154
+ const parts = schemaParts(current)
155
+ const next = parts.entries && key in parts.entries ? parts.entries[key] : parts.item
156
+ if (!next) return null
157
+ current = unwrappedSchema(next)
158
+ }
159
+ return current
160
+ }
161
+
162
+ function formatToolArgIssue(schema: v.GenericSchema, issue: v.BaseIssue<unknown>): string {
163
+ const keys = issueKeys(issue)
164
+ const key = keys.at(-1)
165
+ if (key !== undefined && (issue.type === 'object' || issue.type === 'strict_object')) {
166
+ if (issue.expected === 'never') {
167
+ const parent = schemaAtPath(schema, keys.slice(0, -1))
168
+ const accepted = parent ? Object.keys(schemaParts(parent).entries ?? {}) : []
169
+ return accepted.length > 0
170
+ ? `Unknown argument "${key}". Accepted: ${accepted.join(', ')}`
171
+ : `Unknown argument "${key}"`
172
+ }
173
+ if (issue.received === 'undefined') return `Missing required argument "${key}"`
174
+ }
175
+ if (issue.type === 'picklist' && key !== undefined) {
176
+ const resolved = schemaAtPath(schema, keys)
177
+ const options = resolved ? schemaParts(resolved).options : undefined
178
+ if (options && options.length > 0) {
179
+ const accepted = options.map((option) => JSON.stringify(option)).join(', ')
180
+ return `Invalid value ${issue.received} for argument "${key}". Accepted: ${accepted}`
181
+ }
182
+ }
183
+ return issue.message
106
184
  }
107
185
 
108
186
  export function toolChangesDocument(def: Pick<ToolDef, 'execution'>): boolean {
@@ -165,6 +243,32 @@ export function nodeSummary(node: FigmaNodeProxy): { id: string; name: string; t
165
243
  return { id: node.id, name: node.name, type: node.type }
166
244
  }
167
245
 
246
+ /**
247
+ * A tool colour, refusing text that would otherwise land as black: `set_fill { color: "#GGG" }`
248
+ * used to paint black and report success. Read through `tryParseColor`, since a published schema
249
+ * cannot express what the parser accepts.
250
+ */
251
+ export function parseToolColor(value: string): Color | { error: string } {
252
+ return (
253
+ tryParseColor(value) ?? {
254
+ error: `"${value}" is not a color; use a hex value, a color name, or a color function`
255
+ }
256
+ )
257
+ }
258
+
259
+ /**
260
+ * A colour the caller may leave out. Missing stays missing; text that is not a colour is the
261
+ * same refusal `parseToolColor` gives, so each tool does not repeat that check.
262
+ */
263
+ export function parseOptionalToolColor(
264
+ value: string | undefined
265
+ ): { color: Color | undefined } | { error: string } {
266
+ if (value === undefined) return { color: undefined }
267
+ const parsed = parseToolColor(value)
268
+ if ('error' in parsed) return parsed
269
+ return { color: parsed }
270
+ }
271
+
168
272
  export const nodeIdInputSchema = v.pipe(v.string(), v.description('Node ID'))
169
273
  export const nodeInputSchema = v.strictObject({ id: nodeIdInputSchema })
170
274
  export const nodeComparisonInputSchema = v.strictObject({
@@ -5,12 +5,17 @@ import { textFonts } from '#core/tools/modify/text'
5
5
  import { defineTool } from '#core/tools/schema'
6
6
 
7
7
  const batchOpsSchema = v.array(
8
- v.object({ id: v.string(), props: v.record(v.string(), v.unknown()) })
8
+ v.object({ id: v.string(), props: v.objectWithRest({}, v.unknown()) })
9
9
  )
10
10
  type BatchOp = v.InferOutput<typeof batchOpsSchema>[number]
11
11
 
12
- /** The operations a call names, or why they cannot be read. */
13
- function parseOperations(operations: string): BatchOp[] | { error: string } {
12
+ /** The operations a call names, as an array or as the JSON string a client may send. */
13
+ function parseOperations(operations: BatchOp[] | string): BatchOp[] | { error: string } {
14
+ if (Array.isArray(operations)) {
15
+ return v.safeParse(batchOpsSchema, operations).success
16
+ ? operations
17
+ : { error: 'operations must be an array of {id, props}' }
18
+ }
14
19
  const json = v.safeParse(v.pipe(v.string(), v.parseJson()), operations)
15
20
  if (!json.success) return { error: 'Invalid JSON in operations' }
16
21
  const ops = v.safeParse(batchOpsSchema, json.output)
@@ -107,9 +112,9 @@ export const batchUpdate = defineTool({
107
112
  execution: { kind: 'sync', mutation: 'document' },
108
113
  input: v.strictObject({
109
114
  operations: v.pipe(
110
- v.string(),
115
+ v.union([batchOpsSchema, v.string()]),
111
116
  v.description(
112
- 'JSON array: [{"id":"0:5","props":{"spacing":8}},{"id":"0:6","props":{"sizing_horizontal":"FILL","grow":1}}]'
117
+ 'Array of {"id","props"}, or the same array as a JSON string: [{"id":"0:5","props":{"spacing":8}},{"id":"0:6","props":{"sizing_horizontal":"FILL","grow":1}}]'
113
118
  )
114
119
  )
115
120
  }),
@@ -1,7 +1,7 @@
1
1
  import * as v from 'valibot'
2
2
 
3
3
  import type { VariableType } from '@pairdraw/scene-graph'
4
- import { parseColor } from '@pairdraw/scene-graph/color'
4
+ import { tryParseColor } from '@pairdraw/scene-graph/color'
5
5
  import type { Color } from '@pairdraw/scene-graph/primitives'
6
6
 
7
7
  import type { FigmaVariableValue } from '#core/figma-api'
@@ -14,8 +14,8 @@ function figmaValue(value: ToolValue): FigmaVariableValue {
14
14
 
15
15
  type ToolValue = Color | number | string | boolean
16
16
 
17
- function parseVariableValue(type: VariableType, value: string): ToolValue {
18
- if (type === 'COLOR') return parseColor(value)
17
+ function parseVariableValue(type: VariableType, value: string): ToolValue | null {
18
+ if (type === 'COLOR') return tryParseColor(value)
19
19
  if (type === 'FLOAT') return Number(value)
20
20
  if (type === 'BOOLEAN') return value === 'true'
21
21
  return value
@@ -41,6 +41,7 @@ export const createVariable = defineTool({
41
41
  const type = args.type
42
42
  // The value is checked first, so a value the variable cannot hold creates nothing.
43
43
  const value = args.value === undefined ? undefined : parseVariableValue(type, args.value)
44
+ if (value === null) return { error: `"${args.value}" is not a ${type} value` }
44
45
  if (typeof value === 'number' && !Number.isFinite(value))
45
46
  return { error: `"${args.value}" is not a number` }
46
47
  const variable = figma.variables.createVariable(args.name, args.collection_id, type)
@@ -66,6 +67,8 @@ export const setVariable = defineTool({
66
67
  const variable = figma.variables.getVariableById(args.id)
67
68
  if (!variable) return { error: `Variable "${args.id}" not found` }
68
69
  const parsedValue = parseVariableValue(variable.resolvedType, args.value)
70
+ if (parsedValue === null)
71
+ return { error: `"${args.value}" is not a ${variable.resolvedType} value` }
69
72
  variable.setValueForMode(args.mode, figmaValue(parsedValue))
70
73
  return { id: args.id, mode: args.mode, value: parsedValue }
71
74
  }
@@ -163,8 +163,12 @@ export const exportImage = defineTool({
163
163
  exposure: { webmcp: false },
164
164
  input: v.strictObject({
165
165
  ...exportInputs,
166
+ // Letter case does not matter here, as it does not for the other tools' options.
166
167
  format: v.optional(
167
- v.pipe(v.picklist(['PNG', 'JPG', 'WEBP']), v.description('Image format')),
168
+ v.pipe(
169
+ v.picklist(['PNG', 'JPG', 'WEBP', 'png', 'jpg', 'webp']),
170
+ v.description('Image format: PNG, JPG, or WEBP')
171
+ ),
168
172
  'PNG'
169
173
  ),
170
174
  quality: v.optional(