Skip to content

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

feat(tools): return text layer content from query_nodes and get_page_tree (#1083)

Danila Poyarkov2f4957db7cb1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2a9c247d3..14ba41dd0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -123,6 +123,7 @@
- Be in several shared rooms at once, each in its own tab and syncing in the background. Join a room from Home with **Join room…**, and from a browser on a computer open it in the desktop app with **Open in desktop app** (`openpencil://join?room=<id>`).
- See how auto layout arranges layers, as in Figma: hovering a horizontal or vertical auto layout frame outlines its visible direct children with dotted lines, and selecting a single layer dots the border of its auto layout parent, in purple for components and instances. The outlines, and the padding and gap markers of a selected frame, hide while layers move, resize, or rotate.
- Join a shared room right away under a generated name such as *Teal Fox*, and set the one name every room shows in Settings or the share panel.
+- Read what a frame says without rendering it: the `query_nodes` and `get_page_tree` AI and MCP tools return the text of text layers, and XPath selectors match `@id`, so `//*[@id='1:2']//TEXT[not(ancestor-or-self::*[@visible='false'])]` lists a frame's visible text in layer order with each layer's ID (#861).
- Choose an accent color in **Settings → General → Appearance**: one of eight presets or any custom color. Buttons, toggles, selected layers, focus rings, the active tool, and the canvas selection, handles, size labels, and ruler badges follow it in both themes, and text on accent surfaces turns dark when the color is too light for white. The CLI and MCP `settings` commands read and set it as `appearance.accent`.
- Customize the toolbar in **Settings → Toolbar**, also opened from **Customize toolbar…** when you right-click it: hide tools, reorder them, and choose which share a flyout, on desktop and mobile alike. Hidden tools keep their shortcuts, **Move** always stays, and tools added in later versions join their default flyout in a customized toolbar.
diff --git a/packages/core/src/tools/read/nodes.ts b/packages/core/src/tools/read/nodes.ts
index 7cca84ca9..d870867b9 100644
--- a/packages/core/src/tools/read/nodes.ts
+++ b/packages/core/src/tools/read/nodes.ts
@@ -10,6 +10,7 @@ interface TreeEntry {
name: string
w: number
h: number
+ text?: string
children?: TreeEntry[]
}
@@ -37,6 +38,7 @@ function nodeToTreeEntry(
w: node.width,
h: node.height
}
+ if (node.type === 'TEXT') entry.text = node.characters
if (children.length > 0) entry.children = children
return entry
}
@@ -44,7 +46,7 @@ function nodeToTreeEntry(
export const getPageTree = defineTool({
name: 'get_page_tree',
description:
- 'Get the node tree of the current page. Returns lightweight hierarchy: id, type, name, size. Use depth, root_id, or node_types to keep large pages small. Use get_node for full properties of a specific node.',
+ 'Get the node tree of the current page. Returns lightweight hierarchy: id, type, name, size, and text for TEXT nodes. Use depth, root_id, or node_types to keep large pages small. Use get_node for full properties of a specific node.',
execution: { kind: 'sync', mutation: 'none' },
input: v.strictObject({
depth: v.optional(
diff --git a/packages/core/src/tools/read/query.ts b/packages/core/src/tools/read/query.ts
index 8bed09fd6..b59776140 100644
--- a/packages/core/src/tools/read/query.ts
+++ b/packages/core/src/tools/read/query.ts
@@ -6,7 +6,7 @@ import { queryByXPath } from '#core/xpath'
export const queryNodes = defineTool({
name: 'query_nodes',
- description: `Query nodes using XPath selectors. Node types are element names (FRAME, TEXT, RECTANGLE, ELLIPSE, etc.). Attributes: name, width, height, x, y, visible, opacity, cornerRadius, fontSize, fontFamily, fontWeight, layoutMode, itemSpacing, paddingTop/Right/Bottom/Left, strokeWeight, rotation, locked, blendMode, text, lineHeight, letterSpacing.
+ description: `Query nodes using XPath selectors. Returns id, name, and type, plus text for TEXT nodes. Node types are element names (FRAME, TEXT, RECTANGLE, ELLIPSE, etc.). Attributes: id, name, width, height, x, y, visible, opacity, cornerRadius, fontSize, fontFamily, fontWeight, layoutMode, itemSpacing, paddingTop/Right/Bottom/Left, strokeWeight, rotation, locked, blendMode, text, lineHeight, letterSpacing.
Examples:
//FRAME — all frames
@@ -15,7 +15,8 @@ Examples:
//SECTION/FRAME — direct frame children of sections
//SECTION//TEXT — all text nodes inside sections
//*[@cornerRadius > 0] — any node with corner radius
-//TEXT[contains(@text, 'Hello')] — text nodes containing "Hello"`,
+//TEXT[contains(@text, 'Hello')] — text nodes containing "Hello"
+//*[@id='1:2']//TEXT[not(ancestor-or-self::*[@visible='false'])] — visible text inside a frame, in layer order`,
execution: { kind: 'async', mutation: 'none' },
exposure: { webmcp: false },
input: v.strictObject({
@@ -31,7 +32,12 @@ Examples:
})
return {
count: nodes.length,
- nodes: nodes.map((node) => ({ id: node.id, name: node.name, type: node.type }))
+ nodes: nodes.map((node) => ({
+ id: node.id,
+ name: node.name,
+ type: node.type,
+ ...(node.type === 'TEXT' ? { text: node.text } : {})
+ }))
}
} catch (err) {
return { error: `XPath error: ${err instanceof Error ? err.message : String(err)}` }
diff --git a/packages/core/src/xpath.ts b/packages/core/src/xpath.ts
index 175eb2ba0..a265b2f93 100644
--- a/packages/core/src/xpath.ts
+++ b/packages/core/src/xpath.ts
@@ -10,6 +10,7 @@ const NODE_TYPES = {
}
const QUERYABLE_ATTRS = [
+ 'id',
'name',
'width',
'height',
diff --git a/tests/engine/tools/read.test.ts b/tests/engine/tools/read.test.ts
index 3bc38f072..2d03ed727 100644
--- a/tests/engine/tools/read.test.ts
+++ b/tests/engine/tools/read.test.ts
@@ -107,6 +107,42 @@ describe('query_nodes', () => {
expect(result.count).toBe(3)
})
+ test('returns the text of visible TEXT nodes inside a frame in layer order', async () => {
+ const { figma } = setupToolTest()
+ const card = figma.createFrame()
+ const title = figma.createText()
+ title.characters = 'Balance'
+ const hiddenGroup = figma.createFrame()
+ hiddenGroup.visible = false
+ const hiddenText = figma.createText()
+ hiddenText.characters = 'Draft'
+ hiddenGroup.appendChild(hiddenText)
+ const amount = figma.createText()
+ amount.characters = '$12,450\nAvailable'
+ card.appendChild(title)
+ card.appendChild(hiddenGroup)
+ card.appendChild(amount)
+ figma.createText().characters = 'Outside'
+
+ const tool = getTool('query_nodes')
+ const result = (await tool.execute(figma, {
+ selector: `//*[@id='${card.id}']//TEXT[not(ancestor-or-self::*[@visible='false'])]`
+ })) as ToolResult
+ expect(result.nodes?.map((node) => [node.id, node.text])).toEqual([
+ [title.id, 'Balance'],
+ [amount.id, '$12,450\nAvailable']
+ ])
+ })
+
+ test('omits text for non-text nodes', async () => {
+ const { figma } = setupToolTest()
+ figma.createRectangle()
+
+ const tool = getTool('query_nodes')
+ const result = (await tool.execute(figma, { selector: '//RECTANGLE' })) as ToolResult
+ expect(expectDefined(result.nodes, 'queried nodes')[0]).not.toHaveProperty('text')
+ })
+
test('returns empty array when nothing matches', async () => {
const { figma } = setupToolTest()
figma.createRectangle()
@@ -169,6 +205,24 @@ describe('get_node', () => {
})
})
+describe('get_page_tree', () => {
+ test('includes the text of TEXT nodes', () => {
+ const { figma } = setupToolTest()
+ const card = figma.createFrame()
+ const label = figma.createText()
+ label.characters = 'Saldo disponível'
+ card.appendChild(label)
+
+ const tool = getTool('get_page_tree')
+ const result = tool.execute(figma, { root_id: card.id, node_types: ['TEXT'] })
+ expect(result).toMatchObject({
+ root: card.id,
+ tree: { id: card.id, children: [{ id: label.id, text: 'Saldo disponível' }] }
+ })
+ expect(result).not.toHaveProperty('tree.text')
+ })
+})
+
describe('page tools', () => {
test('list_pages returns pages', () => {
const { figma } = setupToolTest()
diff --git a/tests/helpers/tools.ts b/tests/helpers/tools.ts
index e31683e7c..2f85ea1a3 100644
--- a/tests/helpers/tools.ts
+++ b/tests/helpers/tools.ts
@@ -10,7 +10,7 @@ export interface ToolResult {
type?: string
error?: string
count?: number
- nodes?: Array<{ id?: string; name: string; type: string }>
+ nodes?: Array<{ id?: string; name: string; type: string; text?: string }>
[key: string]: unknown
}