Skip to content

AI-native design editor. Open-source Figma alternative.

Fix lint errors: split describe.ts, fix TS errors, clean up type guards

Anton A S4d3a5341cd20
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index af1eeef09..9dd41a54d 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -32,7 +32,7 @@ const GLYPH_WIDTH_FACTOR = 0.6
// this when available — this is only the fallback.
function estimateTextSize(node: SceneNode): { width: number; height: number } {
const fontSize = node.fontSize || 14
- const text = node.characters || node.text || ''
+ const text = node.text || ''
const charWidth = fontSize * GLYPH_WIDTH_FACTOR
return {
width: Math.ceil(text.length * charWidth),
diff --git a/packages/core/src/render/export-jsx.ts b/packages/core/src/render/export-jsx.ts
index a33ae0f75..027864683 100644
--- a/packages/core/src/render/export-jsx.ts
+++ b/packages/core/src/render/export-jsx.ts
@@ -347,11 +347,11 @@ function collectTextSizingProps(node: SceneNode, graph: SceneGraph, props: [stri
// gets emitted first and blocks the fill detection.
const isFillWidth = node.layoutAlignSelf === 'STRETCH' && (() => {
const parent = node.parentId ? graph.getNode(node.parentId) : null
- return parent && parent.layoutMode === 'VERTICAL'
+ return parent?.layoutMode === 'VERTICAL'
})()
const isGrowWidth = node.layoutGrow > 0 && (() => {
const parent = node.parentId ? graph.getNode(node.parentId) : null
- return parent && parent.layoutMode === 'HORIZONTAL'
+ return parent?.layoutMode === 'HORIZONTAL'
})()
const emitW = autoResize !== 'WIDTH_AND_HEIGHT' && !isFillWidth && !isGrowWidth
if (emitW && node.width > 0) props.push(['w', node.width])
diff --git a/packages/core/src/tools/ai-adapter.ts b/packages/core/src/tools/ai-adapter.ts
index f828310a3..690c33309 100644
--- a/packages/core/src/tools/ai-adapter.ts
+++ b/packages/core/src/tools/ai-adapter.ts
@@ -224,7 +224,7 @@ const ARG_TO_NODE_PROP: Record<string, string> = {
}
/** Args that are parameters to the tool, not node properties to track */
-const SKIP_ARGS: Record<string, Set<string>> = {
+const SKIP_ARGS: Partial<Record<string, Set<string>>> = {
set_effects: new Set(['type', 'color', 'offset_x', 'offset_y', 'radius', 'spread']),
set_fill: new Set(['type', 'color']),
set_stroke: new Set(['type', 'color']),
diff --git a/packages/core/src/tools/calc.ts b/packages/core/src/tools/calc.ts
index d43e33652..386f55043 100644
--- a/packages/core/src/tools/calc.ts
+++ b/packages/core/src/tools/calc.ts
@@ -5,16 +5,16 @@ import { defineTool } from './schema'
* Supports: + - * / % ** ( ) and Math.min/max/floor/ceil/round/abs/sqrt/pow
*/
-const MATH_FUNCTIONS: Record<string, (...args: number[]) => number> = {
- 'Math.min': (...args) => Math.min(...args),
- 'Math.max': (...args) => Math.max(...args),
- 'Math.floor': (n) => Math.floor(n),
- 'Math.ceil': (n) => Math.ceil(n),
- 'Math.round': (n) => Math.round(n),
- 'Math.abs': (n) => Math.abs(n),
- 'Math.sqrt': (n) => Math.sqrt(n),
- 'Math.pow': (base, exp) => Math.pow(base, exp),
-}
+const MATH_FUNCTIONS = new Map<string, (...args: number[]) => number>([
+ ['Math.min', (...args) => Math.min(...args)],
+ ['Math.max', (...args) => Math.max(...args)],
+ ['Math.floor', (n) => Math.floor(n)],
+ ['Math.ceil', (n) => Math.ceil(n)],
+ ['Math.round', (n) => Math.round(n)],
+ ['Math.abs', (n) => Math.abs(n)],
+ ['Math.sqrt', (n) => Math.sqrt(n)],
+ ['Math.pow', (base, exp) => Math.pow(base, exp)],
+])
class Parser {
private pos = 0
@@ -104,7 +104,7 @@ class Parser {
this.pos++
}
const name = this.input.slice(start, this.pos)
- const fn = MATH_FUNCTIONS[name]
+ const fn = MATH_FUNCTIONS.get(name)
if (!fn) throw new Error(`Unknown function: ${name}`)
this.skipWhitespace()
@@ -186,7 +186,7 @@ export const calc = defineTool({
}
return { expr, result }
} catch (e) {
- return { error: `${e instanceof Error ? e.message : String(e)}` }
+ return { error: e instanceof Error ? e.message : String(e) }
}
}
})
diff --git a/packages/core/src/tools/codegen.ts b/packages/core/src/tools/codegen.ts
index fb7ea2a7d..e4fe5fea1 100644
--- a/packages/core/src/tools/codegen.ts
+++ b/packages/core/src/tools/codegen.ts
@@ -21,11 +21,11 @@ function toCamelCase(name: string): string {
}
function isColor(value: VariableValue): value is Color {
- return typeof value === 'object' && value !== null && 'r' in value && 'g' in value && 'b' in value
+ return typeof value === 'object' && 'r' in value && 'g' in value && 'b' in value
}
function isAlias(value: VariableValue): value is { aliasId: string } {
- return typeof value === 'object' && value !== null && 'aliasId' in value
+ return typeof value === 'object' && 'aliasId' in value
}
function resolveValue(
@@ -57,7 +57,7 @@ interface TokenEntry {
name: string
cssVar: string
type: string
- values: Record<string, string>
+ values: Partial<Record<string, string>>
}
function buildTokens(
@@ -102,11 +102,11 @@ function renderCSS(
): string {
if (tokens.length === 0) return '/* No design tokens found */\n'
- const defaultModeId = modes[0]?.id
+ const defaultModeId = modes[0]?.id ?? ''
const lines: string[] = [':root {']
for (const t of tokens) {
- const val = t.values[defaultModeId ?? ''] ?? Object.values(t.values)[0] ?? ''
+ const val = t.values[defaultModeId] ?? Object.values(t.values)[0] ?? ''
lines.push(` ${t.cssVar}: ${val};`)
}
lines.push('}')
@@ -130,14 +130,14 @@ function renderTailwindTheme(
tokens: TokenEntry[],
modes: { id: string; name: string }[]
): string {
- const defaultModeId = modes[0]?.id
+ const defaultModeId = modes[0]?.id ?? ''
const colors: Record<string, string> = {}
const spacing: Record<string, string> = {}
const other: Record<string, string> = {}
for (const t of tokens) {
const key = toCamelCase(t.name)
- const val = t.values[defaultModeId ?? ''] ?? Object.values(t.values)[0] ?? ''
+ const val = t.values[defaultModeId] ?? Object.values(t.values)[0] ?? ''
if (t.type === 'COLOR') colors[key] = val
else if (t.type === 'FLOAT') spacing[key] = `${val}px`
else other[key] = val
diff --git a/packages/core/src/tools/describe-issues.ts b/packages/core/src/tools/describe-issues.ts
new file mode 100644
index 000000000..05069cf0f
--- /dev/null
+++ b/packages/core/src/tools/describe-issues.ts
@@ -0,0 +1,436 @@
+import { colorDistance, colorToHex } from '../color'
+
+import type { Color } from '../types'
+import type { SceneGraph, SceneNode } from '../scene-graph'
+
+const MIN_FILL_OPACITY = 0.15
+const MIN_STROKE_OPACITY = 0.20
+const LOW_CONTRAST_THRESHOLD = 15
+
+export interface DescribeIssue {
+ message: string
+ suggestion?: string
+}
+
+function findAncestorBackground(node: SceneNode, graph: SceneGraph): Color | null {
+ let current = node.parentId ? graph.getNode(node.parentId) : null
+ while (current) {
+ const solidFill = current.fills.find((f) => f.visible && f.type === 'SOLID' && f.opacity > 0.5)
+ if (solidFill) return solidFill.color
+ current = current.parentId ? graph.getNode(current.parentId) : null
+ }
+ return null
+}
+
+const CONTAINER_TYPES = new Set(['FRAME', 'COMPONENT', 'INSTANCE'])
+const SHAPE_TYPES = new Set(['RECTANGLE', 'ELLIPSE', 'STAR', 'POLYGON', 'LINE'])
+const ICON_MAX_SIZE = 48
+const BUTTON_MAX_WIDTH = 200
+const BUTTON_MAX_HEIGHT = 50
+const BUTTON_MIN_HEIGHT = 28
+const BUTTON_MIN_RADIUS = 2
+
+function looksLikeButton(node: SceneNode): boolean {
+ if (!CONTAINER_TYPES.has(node.type)) return false
+ return node.width <= BUTTON_MAX_WIDTH &&
+ node.height >= BUTTON_MIN_HEIGHT && node.height <= BUTTON_MAX_HEIGHT &&
+ node.cornerRadius >= BUTTON_MIN_RADIUS &&
+ node.childIds.length > 0
+}
+
+function checkEmptyIcon(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
+ if (!CONTAINER_TYPES.has(node.type)) return
+ if (node.width > ICON_MAX_SIZE || node.height > ICON_MAX_SIZE || node.childIds.length === 0) return
+ if (node.fills.some((f) => f.visible) || node.strokes.some((s) => s.visible)) return
+
+ const hasVisibleChild = node.childIds.some((id) => {
+ const c = graph.getNode(id)
+ return c?.visible && (c.fills.some((f) => f.visible) || c.strokes.some((s) => s.visible) || c.type === 'TEXT')
+ })
+ if (!hasVisibleChild) {
+ issues.push({
+ message: `Icon-sized frame "${node.name}" (${node.width}×${node.height}) has no visible content`,
+ suggestion: 'Add bg="#hex" or stroke="#hex" to the icon or its children'
+ })
+ }
+}
+
+function detectStructuralIssues(node: SceneNode, gridSize: number, graph: SceneGraph, issues: DescribeIssue[]): void {
+ if (node.x % 1 !== 0 || node.y % 1 !== 0) {
+ issues.push({
+ message: `Subpixel position (${node.x}, ${node.y})`,
+ suggestion: `(${Math.round(node.x)}, ${Math.round(node.y)})`
+ })
+ }
+ if (CONTAINER_TYPES.has(node.type) && node.fills.length === 0 && node.childIds.length === 0) {
+ issues.push({ message: 'Empty frame with no fill' })
+ }
+ const hasVisibleFill = node.fills.some((f) => f.visible)
+ const hasVisibleStroke = node.strokes.some((s) => s.visible)
+ if (SHAPE_TYPES.has(node.type) && !hasVisibleFill && !hasVisibleStroke) {
+ issues.push({
+ message: `${node.type} "${node.name}" has no fill and no stroke — invisible`,
+ suggestion: 'Add bg="#hex" or stroke="#hex"'
+ })
+ }
+ checkEmptyIcon(node, graph, issues)
+ if (looksLikeButton(node) && node.width < 44) {
+ issues.push({ message: `Touch target too small (${node.width}×${node.height})`, suggestion: 'Min 44×44' })
+ }
+ if (node.itemSpacing > 0 && node.itemSpacing % gridSize !== 0) {
+ const nearest = Math.round(node.itemSpacing / gridSize) * gridSize
+ issues.push({ message: `Gap ${node.itemSpacing} not on ${gridSize}px grid`, suggestion: `${nearest}` })
+ }
+}
+
+function detectVisibilityIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
+ for (const fill of node.fills) {
+ if (!fill.visible || fill.type !== 'SOLID') continue
+ if (fill.opacity < MIN_FILL_OPACITY) {
+ issues.push({
+ message: `Near-invisible fill ${colorToHex(fill.color)} at ${Math.round(fill.opacity * 100)}% opacity`,
+ suggestion: `Increase to at least ${Math.round(MIN_FILL_OPACITY * 100)}%`
+ })
+ }
+ }
+ for (const stroke of node.strokes) {
+ if (!stroke.visible || stroke.opacity >= MIN_STROKE_OPACITY) continue
+ issues.push({
+ message: `Near-invisible stroke at ${Math.round(stroke.opacity * 100)}% opacity`,
+ suggestion: `Increase to at least ${Math.round(MIN_STROKE_OPACITY * 100)}%`
+ })
+ }
+ if (node.type !== 'TEXT' || !node.parentId) return
+ const textFill = node.fills.find((f) => f.visible && f.type === 'SOLID')
+ if (!textFill) return
+ const parentBg = findAncestorBackground(node, graph)
+ if (!parentBg) return
+ const dist = colorDistance(textFill.color, parentBg)
+ if (dist < LOW_CONTRAST_THRESHOLD) {
+ issues.push({
+ message: `Low contrast: text ${colorToHex(textFill.color)} on ${colorToHex(parentBg)} (distance ${Math.round(dist)})`,
+ suggestion: 'Increase color difference between text and background'
+ })
+ }
+}
+
+const DARK_BG_LUMINANCE = 0.35
+
+function rgbLuminance(c: Color): number {
+ return 0.299 * c.r + 0.587 * c.g + 0.114 * c.b
+}
+
+interface LayoutContext {
+ node: SceneNode
+ graph: SceneGraph
+ isRow: boolean
+ children: SceneNode[]
+ issues: DescribeIssue[]
+}
+
+function checkDividerOrientation(ctx: LayoutContext): void {
+ for (const child of ctx.children) {
+ const isVerticalDivider = child.width <= 2 && child.height > 10 && child.type === 'RECTANGLE'
+ const isHorizontalDivider = child.height <= 2 && child.width > 10 && child.type === 'RECTANGLE'
+ if (isVerticalDivider && !ctx.isRow) {
+ ctx.issues.push({
+ message: `Vertical divider "${child.name}" (${child.width}×${child.height}) is inside a column layout — should be inside flex="row"`,
+ suggestion: 'Move the divider inside the row container it separates, or change to a horizontal divider'
+ })
+ }
+ if (isHorizontalDivider && ctx.isRow) {
+ ctx.issues.push({
+ message: `Horizontal divider "${child.name}" (${child.width}×${child.height}) is inside a row layout — should be inside flex="col"`,
+ suggestion: 'Move the divider inside the column container it separates'
+ })
+ }
+ }
+}
+
+function checkGrowInHug(ctx: LayoutContext): void {
+ const { node, isRow, children, issues } = ctx
+ if (node.primaryAxisSizing !== 'HUG') return
+ for (const child of children) {
+ if (child.layoutGrow > 0) {
+ issues.push({
+ message: `"${child.name}" has grow=${child.layoutGrow} but parent "${node.name}" is HUG on ${isRow ? 'horizontal' : 'vertical'} axis`,
+ suggestion: `Set parent to fixed size, or remove grow from "${child.name}"`
+ })
+ }
+ }
+}
+
+function checkGrowSizeConflict(ctx: LayoutContext): void {
+ for (const child of ctx.children) {
+ if (child.layoutGrow > 0 && child.layoutMode === 'NONE') {
+ const fixedDim = ctx.isRow ? child.width : child.height
+ if (fixedDim > 0 && fixedDim !== 100) {
+ ctx.issues.push({
+ message: `"${child.name}" has both fixed ${ctx.isRow ? 'width' : 'height'}=${fixedDim} and grow=${child.layoutGrow} — grow overrides the size`,
+ suggestion: 'Remove the fixed size or remove grow'
+ })
+ }
+ }
+ }
+}
+
+function checkChildOverflow(ctx: LayoutContext): void {
+ const { node, graph, isRow, children, issues } = ctx
+ for (const child of children) {
+ if (child.layoutPositioning === 'ABSOLUTE') continue
+ for (const grandchildId of child.childIds) {
+ const gc = graph.getNode(grandchildId)
+ if (!gc?.visible) continue
+ const gcDim = isRow ? gc.width : gc.height
+ const parentDim = isRow ? child.width : child.height
+ if (gcDim > parentDim + 1 && !child.clipsContent && parentDim > 0) {
+ issues.push({
+ message: `"${gc.name}" (${Math.round(gcDim)}px) overflows parent "${child.name}" (${Math.round(parentDim)}px)`,
+ suggestion: `Reduce size or add overflow="hidden" on "${child.name}"`
+ })
+ }
+ }
+ }
+
+ if (node.primaryAxisSizing === 'FIXED' && !node.clipsContent) {
+ const pad = isRow
+ ? node.paddingLeft + node.paddingRight
+ : node.paddingTop + node.paddingBottom
+ const spacing = children.length > 1 ? (children.length - 1) * node.itemSpacing : 0
+ const available = (isRow ? node.width : node.height) - pad - spacing
+ let totalChildren = 0
+ for (const child of children) {
+ totalChildren += isRow ? child.width : child.height
+ }
+ if (totalChildren > available + 1) {
+ issues.push({
+ message: `Children total ${Math.round(totalChildren)}px exceeds available ${Math.round(available)}px on ${isRow ? 'horizontal' : 'vertical'} axis`,
+ suggestion: 'Use grow/fill for flexible sizing, reduce child sizes, or set overflow="hidden"'
+ })
+ }
+ }
+}
+
+function checkHugCollapse(ctx: LayoutContext): void {
+ const { node, isRow, children, issues } = ctx
+ if (children.length === 0) return
+
+ if (node.primaryAxisSizing === 'HUG') {
+ const allGrow = children.every((c) => c.layoutGrow > 0)
+ if (allGrow) {
+ issues.push({
+ message: `"${node.name}" is HUG but all children use grow — no child provides a concrete size`,
+ suggestion: 'Give at least one child a fixed size, or set parent to fixed size'
+ })
+ }
+ }
+ if (node.counterAxisSizing === 'HUG') {
+ const allStretch = children.every(
+ (c) => c.layoutAlignSelf === 'STRETCH' || (node.counterAxisAlign === 'STRETCH' && c.layoutAlignSelf === 'AUTO')
+ )
+ const noConcreteChild = children.every((c) => {
+ const dim = isRow ? c.height : c.width
+ return dim <= 0
+ })
+ if (allStretch && noConcreteChild) {
+ issues.push({
+ message: `"${node.name}" is HUG on cross axis but all children stretch — no child provides a concrete size`,
+ suggestion: 'Give at least one child a fixed cross-axis size, or set parent cross-axis to fixed'
+ })
+ }
+ }
+}
+
+function checkTextVisibility(ctx: LayoutContext): void {
+ const { node, graph, issues } = ctx
+ for (const childId of node.childIds) {
+ const child = graph.getNode(childId)
+ if (!child?.visible || child.type !== 'TEXT') continue
+ const textFill = child.fills.find((f) => f.visible && f.type === 'SOLID')
+ if (!textFill) {
+ issues.push({
+ message: `"${child.name || child.text.slice(0, 20) || 'Text'}" has no color — invisible`,
+ suggestion: 'Add color="#hex" to the Text element'
+ })
+ continue
+ }
+ const textLum = rgbLuminance(textFill.color)
+ if (textLum > DARK_BG_LUMINANCE) continue
+ const bg = findAncestorBackground(child, graph)
+ if (!bg) continue
+ if (rgbLuminance(bg) < DARK_BG_LUMINANCE) {
+ issues.push({
+ message: `"${child.name || child.text.slice(0, 20) || 'Text'}" is dark text (${colorToHex(textFill.color)}) on dark background (${colorToHex(bg)})`,
+ suggestion: 'Set an explicit light color on the text'
+ })
+ }
+ }
+}
+
+function checkTextOverflow(ctx: LayoutContext): void {
+ const { node, children, issues } = ctx
+ const parentAvailableW = node.width - node.paddingLeft - node.paddingRight
+ for (const child of children) {
+ if (child.type !== 'TEXT' || !child.visible) continue
+ if (child.textAutoResize === 'WIDTH_AND_HEIGHT' && child.width > parentAvailableW + 1) {
+ issues.push({
+ message: `Text "${child.text.slice(0, 25)}..." is ${Math.round(child.width)}px wide but parent "${node.name}" has ${Math.round(parentAvailableW)}px available`,
+ suggestion: `Add w={${Math.round(parentAvailableW)}} to the Text or use w="fill"`
+ })
+ }
+ }
+}
+
+function checkCrossAxisOverflow(ctx: LayoutContext): void {
+ const { node, isRow, children, issues } = ctx
+ if (node.clipsContent) return
+ const crossPad = isRow
+ ? node.paddingTop + node.paddingBottom
+ : node.paddingLeft + node.paddingRight
+ const crossAvailable = (isRow ? node.height : node.width) - crossPad
+ for (const child of children) {
+ const childCross = isRow ? child.height : child.width
+ if (childCross > crossAvailable + 1 && child.layoutAlignSelf !== 'STRETCH') {
+ issues.push({
+ message: `"${child.name}" is ${Math.round(childCross)}px on cross axis but parent has ${Math.round(crossAvailable)}px available`,
+ suggestion: 'Reduce child size, use fill sizing, or set overflow="hidden" on parent'
+ })
+ }
+ }
+}
+
+function detectLayoutIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
+ if (!CONTAINER_TYPES.has(node.type)) return
+
+ const isAutoLayout = node.layoutMode !== 'NONE'
+ const isRow = node.layoutMode === 'HORIZONTAL'
+ const children = node.childIds
+ .map((id) => graph.getNode(id))
+ .filter((c): c is SceneNode => c?.visible === true && c.layoutPositioning !== 'ABSOLUTE')
+
+ const ctx: LayoutContext = { node, graph, isRow, children, issues }
+
+ checkTextVisibility(ctx)
+ if (!isAutoLayout) return
+
+ if (node.layoutWrap === 'WRAP' && node.counterAxisSpacing <= 0 && children.length > 1) {
+ issues.push({
+ message: `"${node.name}" uses wrap but has no rowGap — wrapped rows will stick together`,
+ suggestion: 'Add rowGap={8} or similar spacing'
+ })
+ }
+
+ checkDividerOrientation(ctx)
+ checkGrowInHug(ctx)
+ checkGrowSizeConflict(ctx)
+ checkChildOverflow(ctx)
+ checkHugCollapse(ctx)
+ checkTextOverflow(ctx)
+ checkCrossAxisOverflow(ctx)
+}
+
+const RADIUS_TOLERANCE = 2
+
+function detectRadiusIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
+ if (node.cornerRadius <= 0 || node.layoutMode === 'NONE') return
+
+ const minPad = Math.min(node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft)
+ if (minPad <= 0) return
+ const expectedInner = Math.max(0, node.cornerRadius - minPad)
+
+ for (const childId of node.childIds) {
+ const child = graph.getNode(childId)
+ if (!child?.visible || child.cornerRadius <= 0) continue
+ if (child.layoutPositioning === 'ABSOLUTE') continue
+ if (child.cornerRadius > node.cornerRadius + RADIUS_TOLERANCE) {
+ issues.push({
+ message: `"${child.name}" radius ${child.cornerRadius} exceeds parent "${node.name}" radius ${node.cornerRadius}`,
+ suggestion: `Use rounded={${expectedInner}} (parent ${node.cornerRadius} − padding ${minPad})`
+ })
+ } else if (child.cornerRadius > expectedInner + RADIUS_TOLERANCE && expectedInner < node.cornerRadius) {
+ issues.push({
+ message: `"${child.name}" radius ${child.cornerRadius} should be ${expectedInner} (parent ${node.cornerRadius} − padding ${minPad})`,
+ suggestion: `Use rounded={${expectedInner}}`
+ })
+ }
+ }
+}
+
+const UPPERCASE_MAX_SIZE = 13
+
+function detectTypographyIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
+ for (const childId of node.childIds) {
+ const child = graph.getNode(childId)
+ if (child?.type !== 'TEXT' || !child.visible) continue
+ const text = child.text
+
+ if (text === text.toUpperCase() && text.length > 1 && /[A-ZА-ЯЁ]/.test(text) && child.fontSize > UPPERCASE_MAX_SIZE) {
+ issues.push({
+ message: `"${text.slice(0, 30)}" is uppercase at ${child.fontSize}px — uppercase is for small labels (≤${UPPERCASE_MAX_SIZE}px)`,
+ suggestion: `Reduce size to ≤${UPPERCASE_MAX_SIZE}px or use mixed case`
+ })
+ }
+ }
+}
+
+const SPACING_GRID = 4
+
+function detectSpacingIssues(node: SceneNode, graph: SceneGraph, gridSize: number, issues: DescribeIssue[]): void {
+ if (node.layoutMode === 'NONE') return
+
+ const children = node.childIds
+ .map((id) => graph.getNode(id))
+ .filter((c): c is SceneNode => c?.visible === true && c.layoutPositioning !== 'ABSOLUTE')
+
+ const minPad = Math.min(node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft)
+ if (node.itemSpacing > 0 && minPad > 0 && node.itemSpacing > minPad * 2) {
+ issues.push({
+ message: `Gap ${node.itemSpacing} is much larger than padding ${minPad} in "${node.name}"`,
+ suggestion: 'Gap should usually be ≤ padding'
+ })
+ }
+
+ const spacingValues = [node.itemSpacing, node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft]
+ .filter((v) => v > 0)
+ for (const v of spacingValues) {
+ if (v % SPACING_GRID !== 0) {
+ const nearest = Math.round(v / SPACING_GRID) * SPACING_GRID
+ issues.push({
+ message: `Spacing value ${v} in "${node.name}" is not on ${SPACING_GRID}px grid`,
+ suggestion: `Use ${nearest}`
+ })
+ break
+ }
+ }
+
+ const flexChildren = children.filter((c) => c.layoutMode !== 'NONE')
+ if (flexChildren.length >= 2) {
+ const paddings = flexChildren.map((c) => c.paddingTop + c.paddingRight + c.paddingBottom + c.paddingLeft)
+ const gaps = flexChildren.map((c) => c.itemSpacing)
+ const uniquePads = new Set(paddings)
+ const uniqueGaps = new Set(gaps.filter((g) => g > 0))
+ if (uniquePads.size > 2) {
+ issues.push({
+ message: `Inconsistent padding across sibling containers in "${node.name}" (${[...uniquePads].join(', ')})`,
+ suggestion: 'Use the same padding for similar containers'
+ })
+ }
+ if (uniqueGaps.size > 2) {
+ issues.push({
+ message: `Inconsistent gaps across sibling containers in "${node.name}" (${[...uniqueGaps].join(', ')})`,
+ suggestion: 'Use the same gap for similar containers'
+ })
+ }
+ }
+}
+
+export function detectIssues(node: SceneNode, gridSize: number, graph: SceneGraph): DescribeIssue[] {
+ const issues: DescribeIssue[] = []
+ detectStructuralIssues(node, gridSize, graph, issues)
+ detectVisibilityIssues(node, graph, issues)
+ detectLayoutIssues(node, graph, issues)
+ detectRadiusIssues(node, graph, issues)
+ detectTypographyIssues(node, graph, issues)
+ detectSpacingIssues(node, graph, gridSize, issues)
+ return issues
+}
diff --git a/packages/core/src/tools/describe.ts b/packages/core/src/tools/describe.ts
index 22fe095aa..9c46ad7fb 100644
--- a/packages/core/src/tools/describe.ts
+++ b/packages/core/src/tools/describe.ts
@@ -1,8 +1,8 @@
-import { colorDistance, colorToHex } from '../color'
+import { colorToHex } from '../color'
+import { detectIssues } from './describe-issues'
import { defineTool } from './schema'
-import type { Color } from '../types'
import type { SceneGraph, SceneNode } from '../scene-graph'
const NAME_ROLE_PATTERNS: { pattern: RegExp; role: string }[] = [
@@ -113,408 +113,6 @@ function describeLayout(node: SceneNode): string | null {
return parts.join(', ')
}
-const MIN_FILL_OPACITY = 0.15
-const MIN_STROKE_OPACITY = 0.20
-const LOW_CONTRAST_THRESHOLD = 15
-
-interface DescribeIssue {
- message: string
- suggestion?: string
-}
-
-function findAncestorBackground(node: SceneNode, graph: SceneGraph): Color | null {
- let current = node.parentId ? graph.getNode(node.parentId) : null
- while (current) {
- const solidFill = current.fills.find((f) => f.visible && f.type === 'SOLID' && f.opacity > 0.5)
- if (solidFill) return solidFill.color
- current = current.parentId ? graph.getNode(current.parentId) : null
- }
- return null
-}
-
-function detectStructuralIssues(node: SceneNode, gridSize: number, issues: DescribeIssue[]): void {
- if (node.x % 1 !== 0 || node.y % 1 !== 0) {
- issues.push({
- message: `Subpixel position (${node.x}, ${node.y})`,
- suggestion: `(${Math.round(node.x)}, ${Math.round(node.y)})`
- })
- }
- const isContainer = node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
- if (isContainer && node.fills.length === 0 && node.childIds.length === 0) {
- issues.push({ message: 'Empty frame with no fill' })
- }
- const isShape = node.type === 'RECTANGLE' || node.type === 'ELLIPSE' ||
- node.type === 'STAR' || node.type === 'POLYGON' || node.type === 'LINE'
- const hasVisibleFill = node.fills.some((f) => f.visible)
- const hasVisibleStroke = node.strokes.some((s) => s.visible)
- if (isShape && !hasVisibleFill && !hasVisibleStroke) {
- issues.push({
- message: `${node.type} "${node.name}" has no fill and no stroke — invisible`,
- suggestion: 'Add bg="#hex" or stroke="#hex"'
- })
- }
- const ICON_MAX_SIZE = 48
- const looksLikeIcon = isContainer &&
- node.width <= ICON_MAX_SIZE && node.height <= ICON_MAX_SIZE &&
- node.childIds.length > 0
- if (looksLikeIcon && !hasVisibleFill && !hasVisibleStroke) {
- const hasVisibleChild = node.childIds.some((id) => {
- const c = graph.getNode(id)
- return c && c.visible && (c.fills.some((f) => f.visible) || c.strokes.some((s) => s.visible) || c.type === 'TEXT')
- })
- if (!hasVisibleChild) {
- issues.push({
- message: `Icon-sized frame "${node.name}" (${node.width}×${node.height}) has no visible content`,
- suggestion: 'Add bg="#hex" or stroke="#hex" to the icon or its children'
- })
- }
- }
- if (looksLikeButton(node) && node.width < 44) {
- issues.push({ message: `Touch target too small (${node.width}×${node.height})`, suggestion: 'Min 44×44' })
- }
- if (node.itemSpacing > 0 && node.itemSpacing % gridSize !== 0) {
- const nearest = Math.round(node.itemSpacing / gridSize) * gridSize
- issues.push({ message: `Gap ${node.itemSpacing} not on ${gridSize}px grid`, suggestion: `${nearest}` })
- }
-}
-
-function detectVisibilityIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
- for (const fill of node.fills) {
- if (!fill.visible || fill.type !== 'SOLID') continue
- if (fill.opacity < MIN_FILL_OPACITY) {
- issues.push({
- message: `Near-invisible fill ${colorToHex(fill.color)} at ${Math.round(fill.opacity * 100)}% opacity`,
- suggestion: `Increase to at least ${Math.round(MIN_FILL_OPACITY * 100)}%`
- })
- }
- }
- for (const stroke of node.strokes) {
- if (!stroke.visible || stroke.opacity >= MIN_STROKE_OPACITY) continue
- issues.push({
- message: `Near-invisible stroke at ${Math.round(stroke.opacity * 100)}% opacity`,
- suggestion: `Increase to at least ${Math.round(MIN_STROKE_OPACITY * 100)}%`
- })
- }
- if (node.type !== 'TEXT' || !node.parentId) return
- const textFill = node.fills.find((f) => f.visible && f.type === 'SOLID')
- if (!textFill) return
- const parentBg = findAncestorBackground(node, graph)
- if (!parentBg) return
- const dist = colorDistance(textFill.color, parentBg)
- if (dist < LOW_CONTRAST_THRESHOLD) {
- issues.push({
- message: `Low contrast: text ${colorToHex(textFill.color)} on ${colorToHex(parentBg)} (distance ${Math.round(dist)})`,
- suggestion: 'Increase color difference between text and background'
- })
- }
-}
-
-const DARK_BG_LUMINANCE = 0.35
-const DEFAULT_TEXT_LUMINANCE = 0.0
-
-function rgbLuminance(c: Color): number {
- return 0.299 * c.r + 0.587 * c.g + 0.114 * c.b
-}
-
-function detectLayoutIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
- const isContainer = node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
- if (!isContainer) return
-
- const isAutoLayout = node.layoutMode !== 'NONE'
- const isRow = node.layoutMode === 'HORIZONTAL'
- const children = node.childIds
- .map((id) => graph.getNode(id))
- .filter((c): c is SceneNode => c != null && c.visible && c.layoutPositioning !== 'ABSOLUTE')
-
- // Vertical divider (thin tall rectangle) outside a flex="row" parent
- if (isAutoLayout) {
- for (const child of children) {
- const isVerticalDivider = child.width <= 2 && child.height > 10 && child.type === 'RECTANGLE'
- const isHorizontalDivider = child.height <= 2 && child.width > 10 && child.type === 'RECTANGLE'
- if (isVerticalDivider && !isRow) {
- issues.push({
- message: `Vertical divider "${child.name}" (${child.width}×${child.height}) is inside a column layout — should be inside flex="row"`,
- suggestion: 'Move the divider inside the row container it separates, or change to a horizontal divider'
- })
- }
- if (isHorizontalDivider && isRow) {
- issues.push({
- message: `Horizontal divider "${child.name}" (${child.width}×${child.height}) is inside a row layout — should be inside flex="col"`,
- suggestion: 'Move the divider inside the column container it separates'
- })
- }
- }
- }
-
- if (isAutoLayout) {
- // wrap without rowGap — wrapped rows stick together
- if (node.layoutWrap === 'WRAP' && node.counterAxisSpacing <= 0 && children.length > 1) {
- issues.push({
- message: `"${node.name}" uses wrap but has no rowGap — wrapped rows will stick together`,
- suggestion: 'Add rowGap={8} or similar spacing'
- })
- }
-
- const primarySizing = node.primaryAxisSizing
- const primaryDim = isRow ? node.width : node.height
- const pad = isRow
- ? node.paddingLeft + node.paddingRight
- : node.paddingTop + node.paddingBottom
- const spacing = children.length > 1 ? (children.length - 1) * node.itemSpacing : 0
- const available = primaryDim - pad - spacing
-
- // grow inside HUG parent — grow has no effect
- if (primarySizing === 'HUG') {
- for (const child of children) {
- if (child.layoutGrow > 0) {
- issues.push({
- message: `"${child.name}" has grow=${child.layoutGrow} but parent "${node.name}" is HUG on ${isRow ? 'horizontal' : 'vertical'} axis`,
- suggestion: `Set parent to fixed size, or remove grow from "${child.name}"`
- })
- }
- }
- }
-
- // w and grow conflict — grow overrides width
- for (const child of children) {
- if (child.layoutGrow > 0 && child.layoutMode === 'NONE') {
- const fixedDim = isRow ? child.width : child.height
- if (fixedDim > 0 && fixedDim !== 100) {
- issues.push({
- message: `"${child.name}" has both fixed ${isRow ? 'width' : 'height'}=${fixedDim} and grow=${child.layoutGrow} — grow overrides the size`,
- suggestion: 'Remove the fixed size or remove grow'
- })
- }
- }
- }
-
- // Child Rectangle/fill overflows its parent (e.g. bar fill wider than bar bg)
- for (const child of children) {
- if (child.layoutPositioning === 'ABSOLUTE') continue
- for (const grandchildId of child.childIds) {
- const gc = graph.getNode(grandchildId)
- if (!gc || !gc.visible) continue
- const gcDim = isRow ? gc.width : gc.height
- const parentDim = isRow ? child.width : child.height
- if (gcDim > parentDim + 1 && !child.clipsContent && parentDim > 0) {
- issues.push({
- message: `"${gc.name}" (${Math.round(gcDim)}px) overflows parent "${child.name}" (${Math.round(parentDim)}px)`,
- suggestion: `Reduce size or add overflow="hidden" on "${child.name}"`
- })
- }
- }
- }
-
- // Children overflow parent on primary axis (compare actual computed sizes)
- if (primarySizing === 'FIXED' && !node.clipsContent) {
- let totalChildren = 0
- for (const child of children) {
- totalChildren += isRow ? child.width : child.height
- }
- if (totalChildren > available + 1) {
- issues.push({
- message: `Children total ${Math.round(totalChildren)}px exceeds available ${Math.round(available)}px on ${isRow ? 'horizontal' : 'vertical'} axis`,
- suggestion: 'Use grow/fill for flexible sizing, reduce child sizes, or set overflow="hidden"'
- })
- }
- }
-
- // HUG container where ALL children use fill → collapses
- const crossHug = node.counterAxisSizing === 'HUG'
- if (primarySizing === 'HUG' && children.length > 0) {
- const allGrow = children.every((c) => c.layoutGrow > 0)
- if (allGrow) {
- issues.push({
- message: `"${node.name}" is HUG but all children use grow — no child provides a concrete size`,
- suggestion: 'Give at least one child a fixed size, or set parent to fixed size'
- })
- }
- }
- if (crossHug && children.length > 0) {
- const allStretch = children.every(
- (c) => c.layoutAlignSelf === 'STRETCH' || (node.counterAxisAlign === 'STRETCH' && c.layoutAlignSelf === 'AUTO')
- )
- const noConcreteChild = children.every((c) => {
- const dim = isRow ? c.height : c.width
- return dim <= 0
- })
- if (allStretch && noConcreteChild) {
- issues.push({
- message: `"${node.name}" is HUG on cross axis but all children stretch — no child provides a concrete size`,
- suggestion: 'Give at least one child a fixed cross-axis size, or set parent cross-axis to fixed'
- })
- }
- }
- }
-
- // Text without any fill = invisible
- for (const childId of node.childIds) {
- const child = graph.getNode(childId)
- if (!child || child.type !== 'TEXT' || !child.visible) continue
- const textFill = child.fills.find((f) => f.visible && f.type === 'SOLID')
- if (!textFill) {
- issues.push({
- message: `"${child.name || child.characters?.slice(0, 20) || 'Text'}" has no color — invisible`,
- suggestion: 'Add color="#hex" to the Text element'
- })
- continue
- }
- const textLum = rgbLuminance(textFill.color)
- if (textLum > DARK_BG_LUMINANCE) continue
- const bg = findAncestorBackground(child, graph)
- if (!bg) continue
- if (rgbLuminance(bg) < DARK_BG_LUMINANCE) {
- issues.push({
- message: `"${child.name || child.characters?.slice(0, 20) || 'Text'}" is dark text (${colorToHex(textFill.color)}) on dark background (${colorToHex(bg)})`,
- suggestion: 'Set an explicit light color on the text'
- })
- }
- }
-
- // Text wider than its fixed-width parent
- if (isAutoLayout) {
- const parentAvailableW = isRow
- ? node.width - node.paddingLeft - node.paddingRight
- : node.width - node.paddingLeft - node.paddingRight
- for (const child of children) {
- if (child.type !== 'TEXT' || !child.visible) continue
- if (child.textAutoResize === 'WIDTH_AND_HEIGHT' && child.width > parentAvailableW + 1) {
- issues.push({
- message: `Text "${(child.characters ?? child.text ?? '').slice(0, 25)}..." is ${Math.round(child.width)}px wide but parent "${node.name}" has ${Math.round(parentAvailableW)}px available`,
- suggestion: `Add w={${Math.round(parentAvailableW)}} to the Text or use w="fill"`
- })
- }
- }
- }
-
- // Child with fixed size larger than parent (direct overflow)
- if (isAutoLayout && !node.clipsContent) {
- const crossPad = isRow
- ? node.paddingTop + node.paddingBottom
- : node.paddingLeft + node.paddingRight
- const crossAvailable = (isRow ? node.height : node.width) - crossPad
- for (const child of children) {
- const childCross = isRow ? child.height : child.width
- if (childCross > crossAvailable + 1 && child.layoutAlignSelf !== 'STRETCH') {
- issues.push({
- message: `"${child.name}" is ${Math.round(childCross)}px on cross axis but parent has ${Math.round(crossAvailable)}px available`,
- suggestion: 'Reduce child size, use fill sizing, or set overflow="hidden" on parent'
- })
- }
- }
- }
-}
-
-const RADIUS_TOLERANCE = 2
-
-function detectRadiusIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
- if (node.cornerRadius <= 0 || node.layoutMode === 'NONE') return
-
- const minPad = Math.min(node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft)
- if (minPad <= 0) return
- const expectedInner = Math.max(0, node.cornerRadius - minPad)
-
- for (const childId of node.childIds) {
- const child = graph.getNode(childId)
- if (!child || !child.visible || child.cornerRadius <= 0) continue
- if (child.layoutPositioning === 'ABSOLUTE') continue
- if (child.cornerRadius > node.cornerRadius + RADIUS_TOLERANCE) {
- issues.push({
- message: `"${child.name}" radius ${child.cornerRadius} exceeds parent "${node.name}" radius ${node.cornerRadius}`,
- suggestion: `Use rounded={${expectedInner}} (parent ${node.cornerRadius} − padding ${minPad})`
- })
- } else if (child.cornerRadius > expectedInner + RADIUS_TOLERANCE && expectedInner < node.cornerRadius) {
- issues.push({
- message: `"${child.name}" radius ${child.cornerRadius} should be ${expectedInner} (parent ${node.cornerRadius} − padding ${minPad})`,
- suggestion: `Use rounded={${expectedInner}}`
- })
- }
- }
-}
-
-const UPPERCASE_MAX_SIZE = 13
-
-function detectTypographyIssues(node: SceneNode, graph: SceneGraph, issues: DescribeIssue[]): void {
- for (const childId of node.childIds) {
- const child = graph.getNode(childId)
- if (!child || child.type !== 'TEXT' || !child.visible) continue
- const text = child.characters ?? child.text ?? ''
-
- // Uppercase body text — should only be used on small labels/overlines
- if (text === text.toUpperCase() && text.length > 1 && /[A-ZА-ЯЁ]/.test(text) && child.fontSize > UPPERCASE_MAX_SIZE) {
- issues.push({
- message: `"${text.slice(0, 30)}" is uppercase at ${child.fontSize}px — uppercase is for small labels (≤${UPPERCASE_MAX_SIZE}px)`,
- suggestion: `Reduce size to ≤${UPPERCASE_MAX_SIZE}px or use mixed case`
- })
- }
- }
-}
-
-const SPACING_GRID = 4
-
-function detectSpacingIssues(node: SceneNode, graph: SceneGraph, gridSize: number, issues: DescribeIssue[]): void {
- if (node.layoutMode === 'NONE') return
-
- const children = node.childIds
- .map((id) => graph.getNode(id))
- .filter((c): c is SceneNode => c != null && c.visible && c.layoutPositioning !== 'ABSOLUTE')
-
- // Padding larger than gap inside same container looks wrong
- const minPad = Math.min(node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft)
- if (node.itemSpacing > 0 && minPad > 0 && node.itemSpacing > minPad * 2) {
- issues.push({
- message: `Gap ${node.itemSpacing} is much larger than padding ${minPad} in "${node.name}"`,
- suggestion: 'Gap should usually be ≤ padding'
- })
- }
-
- // Off-grid spacing values
- const spacingValues = [node.itemSpacing, node.paddingTop, node.paddingRight, node.paddingBottom, node.paddingLeft]
- .filter((v) => v > 0)
- for (const v of spacingValues) {
- if (v % SPACING_GRID !== 0) {
- const nearest = Math.round(v / SPACING_GRID) * SPACING_GRID
- issues.push({
- message: `Spacing value ${v} in "${node.name}" is not on ${SPACING_GRID}px grid`,
- suggestion: `Use ${nearest}`
- })
- break
- }
- }
-
- // Inconsistent padding/gap among sibling containers of similar role
- const flexChildren = children.filter((c) => c.layoutMode !== 'NONE')
- if (flexChildren.length >= 2) {
- const paddings = flexChildren.map((c) => c.paddingTop + c.paddingRight + c.paddingBottom + c.paddingLeft)
- const gaps = flexChildren.map((c) => c.itemSpacing)
- const uniquePads = new Set(paddings)
- const uniqueGaps = new Set(gaps.filter((g) => g > 0))
- if (uniquePads.size > 2) {
- issues.push({
- message: `Inconsistent padding across sibling containers in "${node.name}" (${[...uniquePads].join(', ')})`,
- suggestion: 'Use the same padding for similar containers'
- })
- }
- if (uniqueGaps.size > 2) {
- issues.push({
- message: `Inconsistent gaps across sibling containers in "${node.name}" (${[...uniqueGaps].join(', ')})`,
- suggestion: 'Use the same gap for similar containers'
- })
- }
- }
-}
-
-function detectIssues(node: SceneNode, gridSize: number, graph: SceneGraph): DescribeIssue[] {
- const issues: DescribeIssue[] = []
- detectStructuralIssues(node, gridSize, issues)
- detectVisibilityIssues(node, graph, issues)
- detectLayoutIssues(node, graph, issues)
- detectRadiusIssues(node, graph, issues)
- detectTypographyIssues(node, graph, issues)
- detectSpacingIssues(node, graph, gridSize, issues)
- return issues
-}
-
function detectRole(node: SceneNode): string {
const nameDetected = detectRoleFromName(node.name)
if (nameDetected) return nameDetected
diff --git a/src/ai/chat-debug.ts b/src/ai/chat-debug.ts
index 178109087..3b2f18ae8 100644
--- a/src/ai/chat-debug.ts
+++ b/src/ai/chat-debug.ts
@@ -103,9 +103,9 @@ function formatDiagnostics(log: ToolDebugLog): string {
sections.push('⚠ NO-OP MUTATIONS (tool succeeded but node unchanged):')
for (const entry of log.noopMutations) {
sections.push(` ${entry.tool} on ${String(entry.args.id)}`)
- sections.push(` unchanged: ${entry.unchangedProps!.join(', ')}`)
- if (entry.nodeBefore) {
- for (const prop of entry.unchangedProps!) {
+ sections.push(` unchanged: ${entry.unchangedProps?.join(', ')}`)
+ if (entry.nodeBefore && entry.unchangedProps) {
+ for (const prop of entry.unchangedProps) {
sections.push(` ${prop} = ${JSON.stringify(entry.nodeBefore[prop])}`)
}
}
@@ -141,7 +141,7 @@ function formatMessageStats(messages: UIMessage[]): string {
for (const part of msg.parts) {
const p = part as Record<string, unknown>
if (p.type === 'text') {
- totalTextLength += String(p.text ?? '').length
+ totalTextLength += typeof p.text === 'string' ? p.text.length : 0
} else if (
p.type === 'tool-invocation' ||
p.type === 'dynamic-tool' ||
diff --git a/src/composables/use-chat.ts b/src/composables/use-chat.ts
index 8678caa03..5c56ef7ba 100644
--- a/src/composables/use-chat.ts
+++ b/src/composables/use-chat.ts
@@ -195,8 +195,8 @@ function createTransport() {
recordStepUsage({
inputTokens: usage.inputTokens ?? 0,
outputTokens: usage.outputTokens ?? 0,
- cacheReadTokens: usage.inputTokenDetails?.cacheReadTokens ?? 0,
- cacheWriteTokens: usage.inputTokenDetails?.cacheWriteTokens ?? 0,
+ cacheReadTokens: usage.inputTokenDetails.cacheReadTokens ?? 0,
+ cacheWriteTokens: usage.inputTokenDetails.cacheWriteTokens ?? 0,
timestamp: Date.now()
})
}