Skip to content

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

refactor(ai): share mutation layout reconciliation

Danila Poyarkov0f070f9f97a0
diff --git a/packages/core/src/tools/ai-adapter.ts b/packages/core/src/tools/ai-adapter.ts
index d3232045b..3ceb28661 100644
--- a/packages/core/src/tools/ai-adapter.ts
+++ b/packages/core/src/tools/ai-adapter.ts
@@ -51,6 +51,7 @@ export interface StepBudget {
export interface AIAdapterOptions {
getFigma: () => FigmaAPI
onBeforeExecute?: (def: ToolDef) => void
+ executeTool?: (def: ToolDef, figma: FigmaAPI, args: Record<string, unknown>) => Promise<unknown>
onAfterExecute?: (def: ToolDef) => Promise<void> | void
onFlashNodes?: (nodeIds: string[]) => void
onToolLog?: (entry: ToolLogEntry) => void
@@ -167,7 +168,9 @@ export function toolsToAI(
options.onBeforeExecute?.(def)
try {
- let execResult = await def.execute(options.getFigma(), args)
+ let execResult = options.executeTool
+ ? await options.executeTool(def, figma, args)
+ : await def.execute(figma, args)
if (def.mutates && options.onFlashNodes) {
const ids = extractNodeIds(execResult)
if (ids.length > 0) options.onFlashNodes(ids)
diff --git a/src/app/ai/tools/index.ts b/src/app/ai/tools/index.ts
index c0ab27f0c..18d12681e 100644
--- a/src/app/ai/tools/index.ts
+++ b/src/app/ai/tools/index.ts
@@ -2,7 +2,6 @@ import { valibotSchema } from '@ai-sdk/valibot'
import { tool } from 'ai'
import * as v from 'valibot'
-import { computeAllLayouts } from '@open-pencil/core/layout'
import {
CORE_TOOLS,
EXTENDED_TOOLS,
@@ -85,17 +84,17 @@ export function createAITools(store: EditorStore) {
],
{
getFigma: () => makeFigmaFromStore(store),
- onBeforeExecute: (def) => {
- if (def.mutates) {
- beforeSnapshot = store.snapshotPage()
- }
+ executeTool: async (def, figma, args) => {
+ if (def.mutates) beforeSnapshot = store.snapshotPage()
+ return def.mutates
+ ? store.runMutationWithLayout(() => def.execute(figma, args), figma.currentPageId)
+ : def.execute(figma, args)
},
onAfterExecute: async (def) => {
if (def.mutates) {
const pageId = store.state.currentPageId
const pageNode = store.graph.getNode(pageId)
if (pageNode) await ensureGraphFonts(store.graph, pageNode.childIds, store.renderer)
- computeAllLayouts(store.graph, pageId)
store.requestRender()
if (beforeSnapshot) {
const before = beforeSnapshot
diff --git a/tests/engine/tools/ai-adapter.test.ts b/tests/engine/tools/ai-adapter.test.ts
index f9cbc8478..da3453934 100644
--- a/tests/engine/tools/ai-adapter.test.ts
+++ b/tests/engine/tools/ai-adapter.test.ts
@@ -103,6 +103,29 @@ describe('AI adapter', () => {
expect(result.children.length).toBeGreaterThan(0)
})
+ test('uses an execution wrapper when provided', async () => {
+ const calls: string[] = []
+ const graph = new SceneGraph()
+ const figma = new FigmaAPI(graph)
+ const createShape = ALL_TOOLS.find((candidate) => candidate.name === 'create_shape')
+ if (!createShape) throw new Error('create_shape tool missing')
+ const tools = toolsToAI(
+ [createShape],
+ {
+ getFigma: () => figma,
+ executeTool: async (def, target, args) => {
+ calls.push(def.name)
+ return def.execute(target, args)
+ }
+ },
+ { v, valibotSchema, tool }
+ )
+
+ await adapterTool(tools, 'create_shape').execute({ type: 'RECTANGLE' })
+
+ expect(calls).toEqual(['create_shape'])
+ })
+
test('onBeforeExecute and onAfterExecute are called', async () => {
const graph = new SceneGraph()
const figma = new FigmaAPI(graph)