@pairdraw/core 0.17.0 → 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.
- package/dist/canvas/outline-stroke.d.ts.map +1 -1
- package/dist/canvas/outline-stroke.js.map +1 -1
- package/dist/canvas/overlays/prototype.d.ts +2 -5
- package/dist/canvas/overlays/prototype.d.ts.map +1 -1
- package/dist/canvas/overlays/prototype.js +3 -19
- package/dist/canvas/overlays/prototype.js.map +1 -1
- package/dist/canvas/renderer/fonts.js +2 -1
- package/dist/canvas/renderer/fonts.js.map +1 -1
- package/dist/canvas/renderer.d.ts +4 -0
- package/dist/canvas/renderer.d.ts.map +1 -1
- package/dist/canvas/renderer.js +4 -0
- package/dist/canvas/renderer.js.map +1 -1
- package/dist/canvas/scene.d.ts.map +1 -1
- package/dist/canvas/scene.js +61 -37
- package/dist/canvas/scene.js.map +1 -1
- package/dist/canvas/text/font-families.d.ts +4 -3
- package/dist/canvas/text/font-families.d.ts.map +1 -1
- package/dist/canvas/text/font-families.js +8 -7
- package/dist/canvas/text/font-families.js.map +1 -1
- package/dist/canvas/text/index.d.ts.map +1 -1
- package/dist/canvas/text/index.js +1 -1
- package/dist/canvas/text/index.js.map +1 -1
- package/dist/canvas/text/paragraph/index.js +2 -2
- package/dist/canvas/text/paragraph/index.js.map +1 -1
- package/dist/canvas/text/placed.js +2 -2
- package/dist/canvas/text/placed.js.map +1 -1
- package/dist/canvas/text/renderer.d.ts +2 -0
- package/dist/canvas/text/renderer.d.ts.map +1 -1
- package/dist/design-jsx/renderer.d.ts.map +1 -1
- package/dist/editor/bridges/undo.d.ts +1 -1
- package/dist/editor/create.d.ts +1 -1
- package/dist/figma-api/index.d.ts +43 -8
- package/dist/figma-api/index.d.ts.map +1 -1
- package/dist/figma-api/index.js +48 -6
- package/dist/figma-api/index.js.map +1 -1
- package/dist/figma-api/serialization.js +10 -0
- package/dist/figma-api/serialization.js.map +1 -1
- package/dist/geometry/connectors.js +2 -14
- package/dist/geometry/connectors.js.map +1 -1
- package/dist/geometry/index.d.ts +2 -2
- package/dist/geometry/scene.d.ts +3 -1
- package/dist/geometry/scene.d.ts.map +1 -1
- package/dist/geometry/scene.js +16 -1
- package/dist/geometry/scene.js.map +1 -1
- package/dist/geometry/types.d.ts +7 -1
- package/dist/geometry/types.d.ts.map +1 -1
- package/dist/io/formats/drawio/export.d.ts.map +1 -1
- package/dist/io/formats/drawio/export.js +10 -0
- package/dist/io/formats/drawio/export.js.map +1 -1
- package/dist/io/formats/drawio/index.d.ts.map +1 -1
- package/dist/io/formats/drawio/index.js +32 -1
- package/dist/io/formats/drawio/index.js.map +1 -1
- package/dist/io/formats/drawio/shapes.d.ts +10 -5
- package/dist/io/formats/drawio/shapes.d.ts.map +1 -1
- package/dist/io/formats/drawio/shapes.js +10 -6
- package/dist/io/formats/drawio/shapes.js.map +1 -1
- package/dist/io/formats/drawio/warnings.d.ts +11 -0
- package/dist/io/formats/drawio/warnings.d.ts.map +1 -0
- package/dist/io/formats/drawio/warnings.js +23 -0
- package/dist/io/formats/drawio/warnings.js.map +1 -0
- package/dist/io/formats/pdf/dom.d.ts +10 -1
- package/dist/io/formats/pdf/dom.d.ts.map +1 -1
- package/dist/io/formats/pdf/dom.js +31 -1
- package/dist/io/formats/pdf/dom.js.map +1 -1
- package/dist/io/formats/pdf/export.js +3 -2
- package/dist/io/formats/pdf/export.js.map +1 -1
- package/dist/io/formats/raster/headless.d.ts +2 -2
- package/dist/io/formats/raster/headless.d.ts.map +1 -1
- package/dist/io/formats/raster/headless.js +2 -2
- package/dist/io/formats/raster/headless.js.map +1 -1
- package/dist/io/formats/raster/render.d.ts +1 -1
- package/dist/io/formats/raster/render.d.ts.map +1 -1
- package/dist/io/formats/raster/render.js +2 -2
- package/dist/io/formats/raster/render.js.map +1 -1
- package/dist/io/formats.d.ts.map +1 -1
- package/dist/io/formats.js +3 -1
- package/dist/io/formats.js.map +1 -1
- package/dist/io/registry.d.ts +2 -0
- package/dist/io/registry.d.ts.map +1 -1
- package/dist/io/registry.js +5 -0
- package/dist/io/registry.js.map +1 -1
- package/dist/io/types.d.ts +2 -0
- package/dist/io/types.d.ts.map +1 -1
- package/dist/library/snapshot.d.ts.map +1 -1
- package/dist/library/snapshot.js +1 -0
- package/dist/library/snapshot.js.map +1 -1
- package/dist/lint/linter.d.ts.map +1 -1
- package/dist/lint/linter.js +11 -2
- package/dist/lint/linter.js.map +1 -1
- package/dist/rpc/variables-command.d.ts.map +1 -1
- package/dist/rpc/variables-command.js +4 -2
- package/dist/rpc/variables-command.js.map +1 -1
- package/dist/text/coverage.d.ts.map +1 -1
- package/dist/text/coverage.js +6 -3
- package/dist/text/coverage.js.map +1 -1
- package/dist/text/fallbacks.d.ts +8 -2
- package/dist/text/fallbacks.d.ts.map +1 -1
- package/dist/text/fallbacks.js +73 -7
- package/dist/text/fallbacks.js.map +1 -1
- package/dist/text/font/manager.d.ts +14 -6
- package/dist/text/font/manager.d.ts.map +1 -1
- package/dist/text/font/manager.js +40 -14
- package/dist/text/font/manager.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +2 -2
- package/dist/text/outlines.js +1 -1
- package/dist/text/outlines.js.map +1 -1
- package/dist/text/path/caret.d.ts +2 -4
- package/dist/text/path/caret.d.ts.map +1 -1
- package/dist/text/path/caret.js.map +1 -1
- package/dist/text/prepare.d.ts +5 -1
- package/dist/text/prepare.d.ts.map +1 -1
- package/dist/text/prepare.js +19 -2
- package/dist/text/prepare.js.map +1 -1
- package/dist/text/web-font/assets.d.ts.map +1 -1
- package/dist/text/web-font/assets.js +13 -21
- package/dist/text/web-font/assets.js.map +1 -1
- package/dist/text/web-font/resolver.d.ts.map +1 -1
- package/dist/text/web-font/resolver.js +4 -1
- package/dist/text/web-font/resolver.js.map +1 -1
- package/dist/tools/create/components.d.ts +2 -1
- package/dist/tools/create/components.d.ts.map +1 -1
- package/dist/tools/create/components.js +27 -1
- package/dist/tools/create/components.js.map +1 -1
- package/dist/tools/create/icons.d.ts.map +1 -1
- package/dist/tools/create/icons.js +11 -5
- package/dist/tools/create/icons.js.map +1 -1
- package/dist/tools/create/vector.d.ts.map +1 -1
- package/dist/tools/create/vector.js +9 -6
- package/dist/tools/create/vector.js.map +1 -1
- package/dist/tools/create.d.ts +2 -2
- package/dist/tools/create.js +2 -2
- package/dist/tools/modify/effects.d.ts.map +1 -1
- package/dist/tools/modify/effects.js +5 -3
- package/dist/tools/modify/effects.js.map +1 -1
- package/dist/tools/modify/matching.d.ts.map +1 -1
- package/dist/tools/modify/matching.js +9 -6
- package/dist/tools/modify/matching.js.map +1 -1
- package/dist/tools/modify/paint.d.ts.map +1 -1
- package/dist/tools/modify/paint.js +7 -5
- package/dist/tools/modify/paint.js.map +1 -1
- package/dist/tools/modify/text.d.ts.map +1 -1
- package/dist/tools/modify/text.js +28 -8
- package/dist/tools/modify/text.js.map +1 -1
- package/dist/tools/read/libraries.d.ts.map +1 -1
- package/dist/tools/read/libraries.js +16 -4
- package/dist/tools/read/libraries.js.map +1 -1
- package/dist/tools/read/pages.d.ts.map +1 -1
- package/dist/tools/read/pages.js.map +1 -1
- package/dist/tools/registry/extended.d.ts.map +1 -1
- package/dist/tools/registry/extended.js +2 -1
- package/dist/tools/registry/extended.js.map +1 -1
- package/dist/tools/schema.d.ts +24 -5
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +80 -6
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/structure/batch.d.ts.map +1 -1
- package/dist/tools/structure/batch.js +4 -3
- package/dist/tools/structure/batch.js.map +1 -1
- package/dist/tools/variables/values.d.ts.map +1 -1
- package/dist/tools/variables/values.js +4 -2
- package/dist/tools/variables/values.js.map +1 -1
- package/dist/tools/vector/export.d.ts.map +1 -1
- package/dist/tools/vector/export.js +5 -2
- package/dist/tools/vector/export.js.map +1 -1
- package/package.json +9 -9
- package/src/canvas/outline-stroke.ts +2 -6
- package/src/canvas/overlays/prototype.ts +5 -27
- package/src/canvas/renderer/fonts.ts +2 -1
- package/src/canvas/renderer.ts +4 -0
- package/src/canvas/scene.ts +92 -63
- package/src/canvas/text/font-families.ts +8 -7
- package/src/canvas/text/index.ts +3 -1
- package/src/canvas/text/paragraph/index.ts +2 -2
- package/src/canvas/text/placed.ts +2 -2
- package/src/canvas/text/renderer.ts +2 -0
- package/src/figma-api/index.ts +90 -9
- package/src/figma-api/serialization.ts +14 -0
- package/src/geometry/connectors.ts +2 -12
- package/src/geometry/index.ts +1 -1
- package/src/geometry/scene.ts +19 -0
- package/src/geometry/types.ts +8 -0
- package/src/io/formats/drawio/export.ts +11 -0
- package/src/io/formats/drawio/index.ts +36 -1
- package/src/io/formats/drawio/shapes.ts +13 -9
- package/src/io/formats/drawio/warnings.ts +27 -0
- package/src/io/formats/pdf/dom.ts +29 -0
- package/src/io/formats/pdf/export.ts +3 -2
- package/src/io/formats/raster/headless.ts +10 -3
- package/src/io/formats/raster/render.ts +4 -2
- package/src/io/formats.ts +3 -1
- package/src/io/registry.ts +12 -0
- package/src/io/types.ts +2 -0
- package/src/library/snapshot.ts +1 -0
- package/src/lint/linter.ts +12 -2
- package/src/rpc/variables-command.ts +6 -5
- package/src/text/coverage.ts +17 -2
- package/src/text/fallbacks.ts +81 -7
- package/src/text/font/manager.ts +50 -17
- package/src/text/outlines.ts +1 -1
- package/src/text/path/caret.ts +3 -4
- package/src/text/prepare.ts +34 -2
- package/src/text/web-font/assets.ts +11 -15
- package/src/text/web-font/resolver.ts +7 -1
- package/src/tools/create/components.ts +33 -0
- package/src/tools/create/icons.ts +13 -5
- package/src/tools/create/vector.ts +16 -6
- package/src/tools/create.ts +1 -0
- package/src/tools/modify/effects.ts +11 -4
- package/src/tools/modify/matching.ts +10 -6
- package/src/tools/modify/paint.ts +13 -5
- package/src/tools/modify/text.ts +53 -20
- package/src/tools/read/libraries.ts +21 -4
- package/src/tools/read/pages.ts +1 -0
- package/src/tools/registry/extended.ts +2 -0
- package/src/tools/schema.ts +109 -5
- package/src/tools/structure/batch.ts +10 -5
- package/src/tools/variables/values.ts +6 -3
- package/src/tools/vector/export.ts +5 -1
package/src/tools/modify/text.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
|
-
import {
|
|
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.
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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 (
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import { isPlainObject } from 'es-toolkit'
|
|
1
|
+
import { groupBy, isPlainObject } from 'es-toolkit'
|
|
2
2
|
import * as v from 'valibot'
|
|
3
3
|
|
|
4
4
|
import { getComponentCatalog } from '#core/tools/component-catalog'
|
|
@@ -14,14 +14,31 @@ const variantValuesSchema = v.pipe(
|
|
|
14
14
|
|
|
15
15
|
export const listLibraries = defineTool({
|
|
16
16
|
name: 'list_libraries',
|
|
17
|
-
description:
|
|
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.',
|
|
18
19
|
execution: { kind: 'async', mutation: 'none' },
|
|
19
20
|
exposure: { webmcp: false },
|
|
20
21
|
input: v.strictObject({}),
|
|
21
22
|
execute: async (figma) => {
|
|
22
23
|
const catalog = getComponentCatalog(figma.graph)
|
|
23
|
-
|
|
24
|
-
|
|
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
|
+
}
|
|
25
42
|
}
|
|
26
43
|
})
|
|
27
44
|
|
package/src/tools/read/pages.ts
CHANGED
|
@@ -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,
|
package/src/tools/schema.ts
CHANGED
|
@@ -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,
|
|
94
|
-
* tool and lists each problem with its argument
|
|
95
|
-
* the
|
|
96
|
-
* strict objects, so a misspelled or mis-nested
|
|
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(
|
|
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.
|
|
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
|
|
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
|
|
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 {
|
|
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
|
|
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(
|
|
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(
|