fix(vector): default missing tangents to {x:0, y:0} in create_vector

Vectors created via MCP with straight-line segments (no tangent
handles) crashed the .fig serializer on save. Segments without
tangentStart/tangentEnd are now normalized to zero vectors at
creation time, producing straight line segments as intended.

Fixes #206
This commit is contained in:
Danila Poyarkov 2026-04-22 14:29:25 +03:00
parent 405a3f2ecf
commit 66b2e7c487
3 changed files with 23 additions and 1 deletions

View file

@ -26,6 +26,7 @@ export {
SceneGraph,
generateId,
cloneVectorNetwork,
normalizeVectorNetwork,
type SceneNode,
type NodeType,
type Fill,

View file

@ -78,6 +78,25 @@ export function cloneVectorNetwork(vn: VectorNetwork): VectorNetwork {
}
}
/**
* Ensure every segment has tangentStart/tangentEnd.
* Missing tangents default to {x:0, y:0} (straight line segments).
* Use at system boundaries where input may come from JSON/MCP.
*/
export function normalizeVectorNetwork(vn: VectorNetwork): VectorNetwork {
const ZERO: Vector = { x: 0, y: 0 }
return {
vertices: vn.vertices,
segments: vn.segments.map((s) => ({
start: s.start,
end: s.end,
tangentStart: (s as Partial<VectorSegment>).tangentStart ?? { ...ZERO },
tangentEnd: (s as Partial<VectorSegment>).tangentEnd ?? { ...ZERO }
})),
regions: vn.regions
}
}
export interface GeometryPath {
windingRule: WindingRule
commandsBlob: Uint8Array

View file

@ -1,6 +1,7 @@
import { parseColor } from '../color'
import { fetchIcons, searchIconsBatch } from '../icons'
import { createIconFromPaths } from '../icons/render'
import { normalizeVectorNetwork } from '../scene-graph'
import { defineTool, nodeSummary } from './schema'
import type { FigmaNodeProxy } from '../figma-api'
@ -169,7 +170,8 @@ export const createVector = defineTool({
node.y = args.y
if (args.name) node.name = args.name
if (args.path) {
figma.graph.updateNode(node.id, { vectorNetwork: JSON.parse(args.path) as VectorNetwork })
const network = normalizeVectorNetwork(JSON.parse(args.path) as VectorNetwork)
figma.graph.updateNode(node.id, { vectorNetwork: network })
}
if (args.fill) {
node.fills = [{ type: 'SOLID', color: parseColor(args.fill), opacity: 1, visible: true }]