alita-sdk 0.3.428.post2__py3-none-any.whl → 0.3.429__py3-none-any.whl
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.
- alita_sdk/runtime/clients/client.py +12 -31
- alita_sdk/runtime/clients/mcp_discovery.py +342 -0
- alita_sdk/runtime/clients/mcp_manager.py +262 -0
- alita_sdk/runtime/langchain/constants.py +1 -1
- alita_sdk/runtime/models/mcp_models.py +57 -0
- alita_sdk/runtime/toolkits/__init__.py +24 -0
- alita_sdk/runtime/toolkits/mcp.py +667 -0
- alita_sdk/runtime/toolkits/tools.py +8 -1
- alita_sdk/runtime/tools/mcp_inspect_tool.py +248 -0
- alita_sdk/runtime/tools/mcp_server_tool.py +52 -2
- alita_sdk/runtime/utils/streamlit.py +34 -3
- alita_sdk/runtime/utils/toolkit_utils.py +5 -2
- alita_sdk/tools/__init__.py +11 -0
- alita_sdk/tools/chunkers/sematic/proposal_chunker.py +1 -1
- alita_sdk/tools/figma/__init__.py +3 -49
- alita_sdk/tools/figma/api_wrapper.py +123 -1157
- alita_sdk/tools/qtest/api_wrapper.py +52 -1236
- alita_sdk/tools/utils/content_parser.py +1 -36
- {alita_sdk-0.3.428.post2.dist-info → alita_sdk-0.3.429.dist-info}/METADATA +1 -1
- {alita_sdk-0.3.428.post2.dist-info → alita_sdk-0.3.429.dist-info}/RECORD +23 -20
- alita_sdk/tools/figma/figma_client.py +0 -73
- alita_sdk/tools/figma/toon_tools.py +0 -2748
- {alita_sdk-0.3.428.post2.dist-info → alita_sdk-0.3.429.dist-info}/WHEEL +0 -0
- {alita_sdk-0.3.428.post2.dist-info → alita_sdk-0.3.429.dist-info}/licenses/LICENSE +0 -0
- {alita_sdk-0.3.428.post2.dist-info → alita_sdk-0.3.429.dist-info}/top_level.txt +0 -0
|
@@ -2,107 +2,37 @@ import functools
|
|
|
2
2
|
import json
|
|
3
3
|
import logging
|
|
4
4
|
import re
|
|
5
|
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
6
5
|
from enum import Enum
|
|
7
6
|
from typing import Dict, List, Generator, Optional, Union
|
|
8
7
|
from urllib.parse import urlparse, parse_qs
|
|
9
8
|
|
|
10
9
|
import requests
|
|
10
|
+
from FigmaPy import FigmaPy
|
|
11
11
|
from langchain_core.documents import Document
|
|
12
12
|
from langchain_core.tools import ToolException
|
|
13
13
|
from pydantic import Field, PrivateAttr, create_model, model_validator, SecretStr
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
# User-friendly error messages for common Figma API errors
|
|
17
|
-
FIGMA_ERROR_MESSAGES = {
|
|
18
|
-
429: "Figma API rate limit exceeded. Please wait a moment and try again.",
|
|
19
|
-
403: "Access denied. Please check your Figma API token has access to this file.",
|
|
20
|
-
404: "File or node not found. Please verify the file key or node ID is correct.",
|
|
21
|
-
401: "Authentication failed. Please check your Figma API token is valid.",
|
|
22
|
-
500: "Figma server error. Please try again later.",
|
|
23
|
-
503: "Figma service temporarily unavailable. Please try again later.",
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def _handle_figma_error(e: ToolException) -> str:
|
|
28
|
-
"""
|
|
29
|
-
Convert a ToolException from Figma API into a user-friendly error message.
|
|
30
|
-
Returns a clean error string without technical details.
|
|
31
|
-
"""
|
|
32
|
-
error_str = str(e)
|
|
33
|
-
|
|
34
|
-
# Extract status code from error message
|
|
35
|
-
for code, message in FIGMA_ERROR_MESSAGES.items():
|
|
36
|
-
if f"error {code}:" in error_str.lower() or f"status\": {code}" in error_str:
|
|
37
|
-
return message
|
|
38
|
-
|
|
39
|
-
# Handle other common patterns
|
|
40
|
-
if "rate limit" in error_str.lower():
|
|
41
|
-
return FIGMA_ERROR_MESSAGES[429]
|
|
42
|
-
if "not found" in error_str.lower():
|
|
43
|
-
return FIGMA_ERROR_MESSAGES[404]
|
|
44
|
-
if "forbidden" in error_str.lower() or "access denied" in error_str.lower():
|
|
45
|
-
return FIGMA_ERROR_MESSAGES[403]
|
|
46
|
-
if "unauthorized" in error_str.lower():
|
|
47
|
-
return FIGMA_ERROR_MESSAGES[401]
|
|
48
|
-
|
|
49
|
-
# Fallback: return a generic but clean message
|
|
50
|
-
return f"Figma API request failed. Please try again or check your file key and permissions."
|
|
51
|
-
|
|
52
15
|
from ..non_code_indexer_toolkit import NonCodeIndexerToolkit
|
|
53
16
|
from ..utils.available_tools_decorator import extend_with_parent_available_tools
|
|
54
|
-
from ..utils.content_parser import
|
|
55
|
-
from .figma_client import AlitaFigmaPy
|
|
56
|
-
from .toon_tools import (
|
|
57
|
-
TOONSerializer,
|
|
58
|
-
process_page_to_toon_data,
|
|
59
|
-
process_frame_to_toon_data,
|
|
60
|
-
extract_text_by_role,
|
|
61
|
-
extract_components,
|
|
62
|
-
detect_sequences,
|
|
63
|
-
group_variants,
|
|
64
|
-
infer_cta_destination,
|
|
65
|
-
FrameDetailTOONSchema,
|
|
66
|
-
AnalyzeFileSchema,
|
|
67
|
-
)
|
|
17
|
+
from ..utils.content_parser import load_content_from_bytes
|
|
68
18
|
|
|
69
|
-
GLOBAL_LIMIT =
|
|
19
|
+
GLOBAL_LIMIT = 10000
|
|
70
20
|
GLOBAL_RETAIN = ['id', 'name', 'type', 'document', 'children']
|
|
71
21
|
GLOBAL_REMOVE = []
|
|
72
|
-
GLOBAL_DEPTH_START =
|
|
22
|
+
GLOBAL_DEPTH_START = 4
|
|
73
23
|
GLOBAL_DEPTH_END = 6
|
|
74
|
-
DEFAULT_NUMBER_OF_THREADS = 5 # valid range for number_of_threads is 1..5
|
|
75
|
-
# Default prompts for image analysis and summarization reused across toolkit and wrapper
|
|
76
|
-
DEFAULT_FIGMA_IMAGES_PROMPT: Dict[str, str] = {
|
|
77
|
-
"prompt": (
|
|
78
|
-
"You are an AI model for image analysis. For each image, first identify its type "
|
|
79
|
-
"(diagram, screenshot, photograph, illustration/drawing, text-centric, or mixed), "
|
|
80
|
-
"then describe all visible elements and extract any readable text. For diagrams, "
|
|
81
|
-
"capture titles, labels, legends, axes, and all numerical values, and summarize key "
|
|
82
|
-
"patterns or trends. For screenshots, describe the interface or page, key UI elements, "
|
|
83
|
-
"and any conversations or messages with participants and timestamps if visible. For "
|
|
84
|
-
"photos and illustrations, describe the setting, main objects/people, their actions, "
|
|
85
|
-
"style, colors, and composition. Be precise and thorough; when something is unclear or "
|
|
86
|
-
"illegible, state that explicitly instead of guessing."
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
DEFAULT_FIGMA_SUMMARY_PROMPT: Dict[str, str] = {
|
|
90
|
-
"prompt": (
|
|
91
|
-
"You are summarizing a visual design document exported from Figma as a sequence of images and text. "
|
|
92
|
-
"Provide a clear, concise overview of the main purpose, key elements, and notable changes or variations in the screens. "
|
|
93
|
-
"Infer a likely user flow or sequence of steps across the screens, calling out entry points, decisions, and outcomes. "
|
|
94
|
-
"Explain how this design could impact planning, development, testing, and review activities in a typical software lifecycle. "
|
|
95
|
-
"Return the result as structured Markdown with headings and bullet lists so it can be reused in SDLC documentation."
|
|
96
|
-
)
|
|
97
|
-
}
|
|
98
24
|
EXTRA_PARAMS = (
|
|
99
25
|
Optional[Dict[str, Union[str, int, List, None]]],
|
|
100
26
|
Field(
|
|
101
27
|
description=(
|
|
102
|
-
"
|
|
103
|
-
"`
|
|
104
|
-
"
|
|
105
|
-
"
|
|
28
|
+
"Additional parameters for customizing response processing:\n"
|
|
29
|
+
"- `limit`: Maximum size of the output in characters.\n"
|
|
30
|
+
"- `regexp`: Regex pattern to filter or clean the output.\n"
|
|
31
|
+
"- `fields_retain`: List of field names to always keep in the output, on levels starting from `depth_start`.\n"
|
|
32
|
+
"- `fields_remove`: List of field names to exclude from the output, unless also present in `fields_retain`.\n"
|
|
33
|
+
"- `depth_start`: The depth in the object hierarchy at which field filtering begins (fields are retained or removed).\n"
|
|
34
|
+
"- `depth_end`: The depth at which all fields are ignored and recursion stops.\n"
|
|
35
|
+
"Use these parameters to control the granularity and size of the returned data, especially for large or deeply nested objects."
|
|
106
36
|
),
|
|
107
37
|
default={
|
|
108
38
|
"limit": GLOBAL_LIMIT, "regexp": None,
|
|
@@ -249,52 +179,6 @@ class ArgsSchema(Enum):
|
|
|
249
179
|
),
|
|
250
180
|
extra_params=EXTRA_PARAMS,
|
|
251
181
|
)
|
|
252
|
-
FileSummary = create_model(
|
|
253
|
-
"FileSummary",
|
|
254
|
-
url=(
|
|
255
|
-
Optional[str],
|
|
256
|
-
Field(
|
|
257
|
-
description=(
|
|
258
|
-
"Full Figma URL with file key and optional node-id. "
|
|
259
|
-
"Example: 'https://www.figma.com/file/<FILE_KEY>/...?...node-id=<NODE_ID>'. "
|
|
260
|
-
"If provided and valid, URL is used and file_key/node_ids arguments are ignored."
|
|
261
|
-
),
|
|
262
|
-
default=None,
|
|
263
|
-
),
|
|
264
|
-
),
|
|
265
|
-
file_key=(
|
|
266
|
-
Optional[str],
|
|
267
|
-
Field(
|
|
268
|
-
description=(
|
|
269
|
-
"Explicit file key used only when URL is not provided."
|
|
270
|
-
),
|
|
271
|
-
default=None,
|
|
272
|
-
examples=["Fp24FuzPwH0L74ODSrCnQo"],
|
|
273
|
-
),
|
|
274
|
-
),
|
|
275
|
-
include_node_ids=(
|
|
276
|
-
Optional[str],
|
|
277
|
-
Field(
|
|
278
|
-
description=(
|
|
279
|
-
"Optional comma-separated top-level node ids (pages) to include when URL has no node-id and URL is not set. "
|
|
280
|
-
"Example: '8:6,1:7'."
|
|
281
|
-
),
|
|
282
|
-
default=None,
|
|
283
|
-
examples=["8:6,1:7"],
|
|
284
|
-
),
|
|
285
|
-
),
|
|
286
|
-
exclude_node_ids=(
|
|
287
|
-
Optional[str],
|
|
288
|
-
Field(
|
|
289
|
-
description=(
|
|
290
|
-
"Optional comma-separated top-level node ids (pages) to exclude when URL has no node-id and URL is not set. "
|
|
291
|
-
"Applied only when include_node_ids is not provided."
|
|
292
|
-
),
|
|
293
|
-
default=None,
|
|
294
|
-
examples=["8:6,1:7"],
|
|
295
|
-
),
|
|
296
|
-
),
|
|
297
|
-
)
|
|
298
182
|
|
|
299
183
|
|
|
300
184
|
class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
@@ -304,88 +188,39 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
304
188
|
global_regexp: Optional[str] = Field(default=None)
|
|
305
189
|
global_fields_retain: Optional[List[str]] = GLOBAL_RETAIN
|
|
306
190
|
global_fields_remove: Optional[List[str]] = GLOBAL_REMOVE
|
|
307
|
-
global_depth_start: Optional[int] =
|
|
308
|
-
global_depth_end: Optional[int] =
|
|
309
|
-
|
|
310
|
-
apply_images_prompt: Optional[bool] = Field(default=True)
|
|
311
|
-
images_prompt: Optional[Dict[str, str]] = Field(default=DEFAULT_FIGMA_IMAGES_PROMPT)
|
|
312
|
-
apply_summary_prompt: Optional[bool] = Field(default=True)
|
|
313
|
-
summary_prompt: Optional[Dict[str, str]] = Field(default=DEFAULT_FIGMA_SUMMARY_PROMPT)
|
|
314
|
-
# concurrency configuration, populated from toolkit config like images_prompt
|
|
315
|
-
number_of_threads: Optional[int] = Field(default=DEFAULT_NUMBER_OF_THREADS, ge=1, le=5)
|
|
316
|
-
_client: Optional[AlitaFigmaPy] = PrivateAttr()
|
|
317
|
-
|
|
318
|
-
def _parse_figma_url(self, url: str) -> tuple[str, Optional[List[str]]]:
|
|
319
|
-
"""Parse and validate a Figma URL.
|
|
320
|
-
|
|
321
|
-
Returns a tuple of (file_key, node_ids_from_url or None).
|
|
322
|
-
Raises ToolException with a clear message if the URL is malformed.
|
|
323
|
-
"""
|
|
324
|
-
try:
|
|
325
|
-
parsed = urlparse(url)
|
|
326
|
-
|
|
327
|
-
# Basic structural validation
|
|
328
|
-
if not parsed.scheme or not parsed.netloc:
|
|
329
|
-
raise ToolException(
|
|
330
|
-
"Figma URL must include protocol and host (e.g., https://www.figma.com/file/...). "
|
|
331
|
-
f"Got: {url}"
|
|
332
|
-
)
|
|
333
|
-
|
|
334
|
-
path_parts = parsed.path.strip('/').split('/') if parsed.path else []
|
|
335
|
-
|
|
336
|
-
# Supported URL patterns:
|
|
337
|
-
# - /file/<file_key>/...
|
|
338
|
-
# - /design/<file_key>/... (older / embedded variant)
|
|
339
|
-
if len(path_parts) < 2 or path_parts[0] not in {"file", "design"}:
|
|
340
|
-
raise ToolException(
|
|
341
|
-
"Unsupported Figma URL format. Expected path like '/file/<FILE_KEY>/...' or "
|
|
342
|
-
"'/design/<FILE_KEY>/...'. "
|
|
343
|
-
f"Got path: '{parsed.path}' from URL: {url}"
|
|
344
|
-
)
|
|
345
|
-
|
|
346
|
-
file_key = path_parts[1]
|
|
347
|
-
if not file_key:
|
|
348
|
-
raise ToolException(
|
|
349
|
-
"Figma URL is missing the file key segment after '/file/' or '/design/'. "
|
|
350
|
-
f"Got path: '{parsed.path}' from URL: {url}"
|
|
351
|
-
)
|
|
352
|
-
|
|
353
|
-
# Optional node-id is passed via query parameter
|
|
354
|
-
query_params = parse_qs(parsed.query or "")
|
|
355
|
-
node_ids_from_url = query_params.get("node-id", []) or None
|
|
356
|
-
|
|
357
|
-
return file_key, node_ids_from_url
|
|
358
|
-
|
|
359
|
-
except ToolException:
|
|
360
|
-
# Re-raise our own clear ToolException as-is
|
|
361
|
-
raise
|
|
362
|
-
except Exception as e:
|
|
363
|
-
# Catch any unexpected parsing issues and wrap them clearly
|
|
364
|
-
raise ToolException(
|
|
365
|
-
"Unexpected error while processing Figma URL. "
|
|
366
|
-
"Please provide a valid Figma file or page URL, for example: "
|
|
367
|
-
"'https://www.figma.com/file/<FILE_KEY>/...'? "
|
|
368
|
-
f"Original error: {e}"
|
|
369
|
-
)
|
|
191
|
+
global_depth_start: Optional[int] = GLOBAL_DEPTH_START
|
|
192
|
+
global_depth_end: Optional[int] = GLOBAL_DEPTH_END
|
|
193
|
+
_client: Optional[FigmaPy] = PrivateAttr()
|
|
370
194
|
|
|
371
195
|
def _base_loader(
|
|
372
196
|
self,
|
|
373
|
-
|
|
197
|
+
file_or_page_url: Optional[str] = None,
|
|
198
|
+
project_id: Optional[str] = None,
|
|
374
199
|
file_keys_include: Optional[List[str]] = None,
|
|
375
200
|
file_keys_exclude: Optional[List[str]] = None,
|
|
376
201
|
node_ids_include: Optional[List[str]] = None,
|
|
377
202
|
node_ids_exclude: Optional[List[str]] = None,
|
|
378
203
|
node_types_include: Optional[List[str]] = None,
|
|
379
204
|
node_types_exclude: Optional[List[str]] = None,
|
|
380
|
-
number_of_threads: Optional[int] = None,
|
|
381
205
|
**kwargs
|
|
382
206
|
) -> Generator[Document, None, None]:
|
|
383
|
-
if
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
207
|
+
if file_or_page_url:
|
|
208
|
+
# If URL is provided and valid, extract and override file_keys_include and node_ids_include
|
|
209
|
+
try:
|
|
210
|
+
parsed = urlparse(file_or_page_url)
|
|
211
|
+
path_parts = parsed.path.strip('/').split('/')
|
|
212
|
+
|
|
213
|
+
# Check if the path matches the expected format
|
|
214
|
+
if len(path_parts) >= 2 and path_parts[0] == 'design':
|
|
215
|
+
file_keys_include = [path_parts[1]]
|
|
216
|
+
if len(path_parts) == 3:
|
|
217
|
+
# To ensure url structure matches Figma's format with 3 path segments
|
|
218
|
+
query_params = parse_qs(parsed.query)
|
|
219
|
+
if "node-id" in query_params:
|
|
220
|
+
node_ids_include = query_params.get('node-id', [])
|
|
221
|
+
except Exception as e:
|
|
222
|
+
raise ToolException(
|
|
223
|
+
f"Unexpected error while processing Figma url {file_or_page_url}: {e}")
|
|
389
224
|
|
|
390
225
|
# If both include and exclude are provided, use only include
|
|
391
226
|
if file_keys_include:
|
|
@@ -395,7 +230,6 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
395
230
|
file = self._client.get_file(file_key, geometry='depth=1') # fetch only top-level structure (only pages without inner components)
|
|
396
231
|
if not file:
|
|
397
232
|
raise ToolException(f"Unexpected error while retrieving file {file_key}. Please try specifying the node-id of an inner page.")
|
|
398
|
-
# propagate per-call number_of_threads override via metadata so _process_document can respect it
|
|
399
233
|
metadata = {
|
|
400
234
|
'id': file_key,
|
|
401
235
|
'file_key': file_key,
|
|
@@ -404,15 +238,29 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
404
238
|
'figma_pages_include': node_ids_include or [],
|
|
405
239
|
'figma_pages_exclude': node_ids_exclude or [],
|
|
406
240
|
'figma_nodes_include': node_types_include or [],
|
|
407
|
-
'figma_nodes_exclude': node_types_exclude or []
|
|
241
|
+
'figma_nodes_exclude': node_types_exclude or []
|
|
408
242
|
}
|
|
409
|
-
if isinstance(number_of_threads, int) and 1 <= number_of_threads <= 5:
|
|
410
|
-
metadata['number_of_threads_override'] = number_of_threads
|
|
411
243
|
yield Document(page_content=json.dumps(metadata), metadata=metadata)
|
|
244
|
+
elif project_id:
|
|
245
|
+
self._log_tool_event(f"Loading project files from project `{project_id}`")
|
|
246
|
+
files = json.loads(self.get_project_files(project_id)).get('files', [])
|
|
247
|
+
for file in files:
|
|
248
|
+
if file_keys_exclude and file.get('key', '') in file_keys_exclude:
|
|
249
|
+
continue
|
|
250
|
+
yield Document(page_content=json.dumps(file), metadata={
|
|
251
|
+
'id': file.get('key', ''),
|
|
252
|
+
'file_key': file.get('key', ''),
|
|
253
|
+
'name': file.get('name', ''),
|
|
254
|
+
'updated_on': file.get('last_modified', ''),
|
|
255
|
+
'figma_pages_include': node_ids_include or [],
|
|
256
|
+
'figma_pages_exclude': node_ids_exclude or [],
|
|
257
|
+
'figma_nodes_include': node_types_include or [],
|
|
258
|
+
'figma_nodes_exclude': node_types_exclude or []
|
|
259
|
+
})
|
|
412
260
|
elif file_keys_exclude or node_ids_exclude:
|
|
413
|
-
raise ValueError("Excludes without parent (file_keys_include) do not make sense.")
|
|
261
|
+
raise ValueError("Excludes without parent (project_id or file_keys_include) do not make sense.")
|
|
414
262
|
else:
|
|
415
|
-
raise ValueError("You must provide
|
|
263
|
+
raise ValueError("You must provide at least project_id or file_keys_include.")
|
|
416
264
|
|
|
417
265
|
def has_image_representation(self, node):
|
|
418
266
|
node_type = node.get('type', '').lower()
|
|
@@ -446,11 +294,7 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
446
294
|
# try to fetch only specified pages/nodes in one request
|
|
447
295
|
file = self._get_file_nodes(file_key,','.join(node_ids_include)) # attempt to fetch only specified pages/nodes in one request
|
|
448
296
|
if file:
|
|
449
|
-
return [
|
|
450
|
-
node["document"]
|
|
451
|
-
for node in (file.get("nodes") or {}).values()
|
|
452
|
-
if node is not None and "document" in node
|
|
453
|
-
]
|
|
297
|
+
return [node['document'] for node in file.get('nodes', {}).values() if 'document' in node]
|
|
454
298
|
else:
|
|
455
299
|
#
|
|
456
300
|
file = self._client.get_file(file_key)
|
|
@@ -475,69 +319,7 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
475
319
|
result.append(page_res)
|
|
476
320
|
return result
|
|
477
321
|
|
|
478
|
-
def
|
|
479
|
-
self,
|
|
480
|
-
file_key: str,
|
|
481
|
-
document: Document,
|
|
482
|
-
node_id: str,
|
|
483
|
-
image_url: str,
|
|
484
|
-
prompt: str,
|
|
485
|
-
) -> Optional[Document]:
|
|
486
|
-
"""Download and process a single Figma image node.
|
|
487
|
-
This helper is used by `_process_document` (optionally in parallel via threads).
|
|
488
|
-
"""
|
|
489
|
-
if not image_url:
|
|
490
|
-
logging.warning(f"Image URL not found for node_id {node_id} in file {file_key}. Skipping.")
|
|
491
|
-
return None
|
|
492
|
-
|
|
493
|
-
logging.info(f"File {file_key}: downloading image node {node_id}.")
|
|
494
|
-
|
|
495
|
-
try:
|
|
496
|
-
response = requests.get(image_url)
|
|
497
|
-
except Exception as exc:
|
|
498
|
-
logging.warning(f"Failed to download image for node {node_id} in file {file_key}: {exc}")
|
|
499
|
-
return None
|
|
500
|
-
|
|
501
|
-
if response.status_code != 200:
|
|
502
|
-
logging.warning(
|
|
503
|
-
f"Unexpected status code {response.status_code} when downloading image "
|
|
504
|
-
f"for node {node_id} in file {file_key}."
|
|
505
|
-
)
|
|
506
|
-
return None
|
|
507
|
-
|
|
508
|
-
content_type = response.headers.get('Content-Type', '')
|
|
509
|
-
if 'text/html' in content_type.lower():
|
|
510
|
-
logging.warning(f"Received HTML instead of image content for node {node_id} in file {file_key}.")
|
|
511
|
-
return None
|
|
512
|
-
|
|
513
|
-
extension = (f".{content_type.split('/')[-1]}" if content_type.startswith('image') else '.txt')
|
|
514
|
-
logging.info(f"File {file_key}: processing image node {node_id}.")
|
|
515
|
-
page_content = _load_content_from_bytes_with_prompt(
|
|
516
|
-
file_content=response.content,
|
|
517
|
-
extension=extension,
|
|
518
|
-
llm=self.llm,
|
|
519
|
-
prompt=prompt,
|
|
520
|
-
)
|
|
521
|
-
|
|
522
|
-
logging.info(f"File {file_key}: finished image node {node_id}.")
|
|
523
|
-
|
|
524
|
-
return Document(
|
|
525
|
-
page_content=page_content,
|
|
526
|
-
metadata={
|
|
527
|
-
'id': node_id,
|
|
528
|
-
'updated_on': document.metadata.get('updated_on', ''),
|
|
529
|
-
'file_key': file_key,
|
|
530
|
-
'node_id': node_id,
|
|
531
|
-
'image_url': image_url,
|
|
532
|
-
'type': 'image',
|
|
533
|
-
},
|
|
534
|
-
)
|
|
535
|
-
|
|
536
|
-
def _process_document(
|
|
537
|
-
self,
|
|
538
|
-
document: Document,
|
|
539
|
-
prompt: str = "",
|
|
540
|
-
) -> Generator[Document, None, None]:
|
|
322
|
+
def _process_document(self, document: Document) -> Generator[Document, None, None]:
|
|
541
323
|
file_key = document.metadata.get('id', '')
|
|
542
324
|
self._log_tool_event(f"Loading details (images) for `{file_key}`")
|
|
543
325
|
figma_pages = self._load_pages(document)
|
|
@@ -561,105 +343,47 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
561
343
|
image_nodes.append(node['id'])
|
|
562
344
|
else:
|
|
563
345
|
text_nodes[node['id']] = self.get_texts_recursive(node)
|
|
564
|
-
|
|
565
|
-
# mutable counter so it can be updated from helper calls (even when used in threads)
|
|
566
|
-
counted_nodes_ref: Dict[str, int] = {"value": 0}
|
|
567
|
-
|
|
568
|
-
# Resolve number_of_threads override from document metadata, falling back to class field
|
|
569
|
-
override_threads = document.metadata.get('number_of_threads_override')
|
|
570
|
-
if isinstance(override_threads, int) and 1 <= override_threads <= 5:
|
|
571
|
-
number_of_threads = override_threads
|
|
572
|
-
else:
|
|
573
|
-
threads_cfg = getattr(self, "number_of_threads", DEFAULT_NUMBER_OF_THREADS)
|
|
574
|
-
if isinstance(threads_cfg, int) and 1 <= threads_cfg <= 5:
|
|
575
|
-
number_of_threads = threads_cfg
|
|
576
|
-
else:
|
|
577
|
-
number_of_threads = DEFAULT_NUMBER_OF_THREADS
|
|
578
|
-
|
|
579
|
-
# --- Process image nodes (potential bottleneck) with optional threading ---
|
|
346
|
+
# process image nodes
|
|
580
347
|
if image_nodes:
|
|
581
348
|
file_images = self._client.get_file_images(file_key, image_nodes)
|
|
582
349
|
images = self._client.get_file_images(file_key, image_nodes).images or {} if file_images else {}
|
|
583
350
|
total_images = len(images)
|
|
584
351
|
if total_images == 0:
|
|
585
352
|
logging.info(f"No images found for file {file_key}.")
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
353
|
+
return
|
|
354
|
+
progress_step = max(1, total_images // 10)
|
|
355
|
+
for idx, (node_id, image_url) in enumerate(images.items(), 1):
|
|
356
|
+
if not image_url:
|
|
357
|
+
logging.warning(f"Image URL not found for node_id {node_id} in file {file_key}. Skipping.")
|
|
358
|
+
continue
|
|
359
|
+
response = requests.get(image_url)
|
|
360
|
+
if response.status_code == 200:
|
|
361
|
+
content_type = response.headers.get('Content-Type', '')
|
|
362
|
+
if 'text/html' not in content_type.lower():
|
|
363
|
+
extension = f".{content_type.split('/')[-1]}" if content_type.startswith('image') else '.txt'
|
|
364
|
+
page_content = load_content_from_bytes(
|
|
365
|
+
file_content=response.content,
|
|
366
|
+
extension=extension, llm=self.llm)
|
|
367
|
+
yield Document(
|
|
368
|
+
page_content=page_content,
|
|
369
|
+
metadata={
|
|
370
|
+
'id': node_id,
|
|
371
|
+
'updated_on': document.metadata.get('updated_on', ''),
|
|
372
|
+
'file_key': file_key,
|
|
373
|
+
'node_id': node_id,
|
|
374
|
+
'image_url': image_url,
|
|
375
|
+
'type': 'image'
|
|
376
|
+
}
|
|
604
377
|
)
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
yield doc
|
|
612
|
-
else:
|
|
613
|
-
# Parallelize image download/processing with a thread pool
|
|
614
|
-
self._log_tool_event(
|
|
615
|
-
f"File {file_key}: using up to {max_workers} worker threads for image nodes."
|
|
616
|
-
)
|
|
617
|
-
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
|
618
|
-
future_to_node = {
|
|
619
|
-
executor.submit(
|
|
620
|
-
self._process_single_image,
|
|
621
|
-
file_key,
|
|
622
|
-
document,
|
|
623
|
-
node_id,
|
|
624
|
-
image_url,
|
|
625
|
-
prompt,
|
|
626
|
-
): node_id
|
|
627
|
-
for node_id, image_url in images.items()
|
|
628
|
-
}
|
|
629
|
-
for future in as_completed(future_to_node):
|
|
630
|
-
node_id = future_to_node[future]
|
|
631
|
-
try:
|
|
632
|
-
doc = future.result()
|
|
633
|
-
except Exception as exc: # safeguard
|
|
634
|
-
logging.warning(
|
|
635
|
-
f"File {file_key}: unexpected error while processing image node {node_id}: {exc}"
|
|
636
|
-
)
|
|
637
|
-
continue
|
|
638
|
-
finally:
|
|
639
|
-
# Count every attempted node, even if it failed or produced no doc,
|
|
640
|
-
# so that progress always reaches total_nodes.
|
|
641
|
-
counted_nodes_ref["value"] += 1
|
|
642
|
-
|
|
643
|
-
if doc is not None:
|
|
644
|
-
self._log_tool_event(
|
|
645
|
-
f"File {file_key}: processing image node {node_id} "
|
|
646
|
-
f"({counted_nodes_ref['value']}/{total_nodes} in {max_workers} threads)."
|
|
647
|
-
)
|
|
648
|
-
yield doc
|
|
649
|
-
|
|
650
|
-
logging.info(
|
|
651
|
-
f"File {file_key}: completed processing of {total_images} image nodes."
|
|
652
|
-
)
|
|
653
|
-
|
|
654
|
-
# --- Process text nodes (fast) ---
|
|
378
|
+
if idx % progress_step == 0 or idx == total_images:
|
|
379
|
+
percent = int((idx / total_images) * 100)
|
|
380
|
+
msg = f"Processed {idx}/{total_images} images ({percent}%) for file {file_key}."
|
|
381
|
+
logging.info(msg)
|
|
382
|
+
self._log_tool_event(msg)
|
|
383
|
+
# process text nodes
|
|
655
384
|
if text_nodes:
|
|
656
385
|
for node_id, texts in text_nodes.items():
|
|
657
|
-
counted_nodes_ref["value"] += 1
|
|
658
|
-
current_index = counted_nodes_ref["value"]
|
|
659
386
|
if texts:
|
|
660
|
-
self._log_tool_event(
|
|
661
|
-
f"File {file_key} : processing text node {node_id} ({current_index}/{total_nodes})."
|
|
662
|
-
)
|
|
663
387
|
yield Document(
|
|
664
388
|
page_content="\n".join(texts),
|
|
665
389
|
metadata={
|
|
@@ -667,49 +391,41 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
667
391
|
'updated_on': document.metadata.get('updated_on', ''),
|
|
668
392
|
'file_key': file_key,
|
|
669
393
|
'node_id': node_id,
|
|
670
|
-
'type': 'text'
|
|
671
|
-
}
|
|
394
|
+
'type': 'text'
|
|
395
|
+
}
|
|
672
396
|
)
|
|
673
397
|
|
|
398
|
+
def _remove_metadata_keys(self):
|
|
399
|
+
return super()._remove_metadata_keys() + ['figma_pages_include', 'figma_pages_exclude', 'figma_nodes_include', 'figma_nodes_exclude']
|
|
400
|
+
|
|
674
401
|
def _index_tool_params(self):
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
default=None)),
|
|
703
|
-
'node_ids_exclude': (Optional[List[str]], Field(
|
|
704
|
-
description="List of top-level nodes (pages) in file to exclude from index. It is applied only if node_ids_include is not provided. It is node-id from figma url: i.e. ['Fp24FuzPwH0L74ODSrCnQo', 'jmhAr6q78dJoMRqt48zisY']",
|
|
705
|
-
default=None)),
|
|
706
|
-
'node_types_include': (Optional[List[str]], Field(
|
|
707
|
-
description="List type of nodes to include in index: i.e. ['FRAME', 'COMPONENT', 'RECTANGLE', 'COMPONENT_SET', 'INSTANCE', 'VECTOR', ...].",
|
|
708
|
-
default=None)),
|
|
709
|
-
'node_types_exclude': (Optional[List[str]], Field(
|
|
710
|
-
description="List type of nodes to exclude from index. It is applied only if node_types_include is not provided: i.e. ['FRAME', 'COMPONENT', 'RECTANGLE', 'COMPONENT_SET', 'INSTANCE', 'VECTOR', ...]",
|
|
711
|
-
default=None)),
|
|
712
|
-
}
|
|
402
|
+
"""Return the parameters for indexing data."""
|
|
403
|
+
return {
|
|
404
|
+
"file_or_page_url": (Optional[str], Field(
|
|
405
|
+
description="Url to file or page to index: i.e. https://www.figma.com/design/[YOUR_FILE_KEY]/Login-page-designs?node-id=[YOUR_PAGE_ID]",
|
|
406
|
+
default=None)),
|
|
407
|
+
"project_id": (Optional[str], Field(
|
|
408
|
+
description="ID of the project to list files from: i.e. 55391681",
|
|
409
|
+
default=None)),
|
|
410
|
+
'file_keys_include': (Optional[List[str]], Field(
|
|
411
|
+
description="List of file keys to include in index if project_id is not provided: i.e. ['Fp24FuzPwH0L74ODSrCnQo', 'jmhAr6q78dJoMRqt48zisY']",
|
|
412
|
+
default=None)),
|
|
413
|
+
'file_keys_exclude': (Optional[List[str]], Field(
|
|
414
|
+
description="List of file keys to exclude from index. It is applied only if project_id is provided and file_keys_include is not provided: i.e. ['Fp24FuzPwH0L74ODSrCnQo', 'jmhAr6q78dJoMRqt48zisY']",
|
|
415
|
+
default=None)),
|
|
416
|
+
'node_ids_include': (Optional[List[str]], Field(
|
|
417
|
+
description="List of top-level nodes (pages) in file to include in index. It is node-id from figma url: i.e. ['123-56', '7651-9230'].",
|
|
418
|
+
default=None)),
|
|
419
|
+
'node_ids_exclude': (Optional[List[str]], Field(
|
|
420
|
+
description="List of top-level nodes (pages) in file to exclude from index. It is applied only if node_ids_include is not provided. It is node-id from figma url: i.e. ['Fp24FuzPwH0L74ODSrCnQo', 'jmhAr6q78dJoMRqt48zisY']",
|
|
421
|
+
default=None)),
|
|
422
|
+
'node_types_include': (Optional[List[str]], Field(
|
|
423
|
+
description="List type of nodes to include in index: i.e. ['FRAME', 'COMPONENT', 'RECTANGLE', 'COMPONENT_SET', 'INSTANCE', 'VECTOR', ...].",
|
|
424
|
+
default=None)),
|
|
425
|
+
'node_types_exclude': (Optional[List[str]], Field(
|
|
426
|
+
description="List type of nodes to exclude from index. It is applied only if node_types_include is not provided: i.e. ['FRAME', 'COMPONENT', 'RECTANGLE', 'COMPONENT_SET', 'INSTANCE', 'VECTOR', ...]",
|
|
427
|
+
default=None))
|
|
428
|
+
}
|
|
713
429
|
|
|
714
430
|
def _send_request(
|
|
715
431
|
self,
|
|
@@ -760,22 +476,22 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
760
476
|
except re.error as e:
|
|
761
477
|
msg = f"Failed to compile regex pattern: {str(e)}"
|
|
762
478
|
logging.error(msg)
|
|
763
|
-
|
|
479
|
+
return ToolException(msg)
|
|
764
480
|
|
|
765
481
|
try:
|
|
766
482
|
if token:
|
|
767
|
-
cls._client =
|
|
483
|
+
cls._client = FigmaPy(token=token, oauth2=False)
|
|
768
484
|
logging.info("Authenticated with Figma token")
|
|
769
485
|
elif oauth2:
|
|
770
|
-
cls._client =
|
|
486
|
+
cls._client = FigmaPy(token=oauth2, oauth2=True)
|
|
771
487
|
logging.info("Authenticated with OAuth2 token")
|
|
772
488
|
else:
|
|
773
|
-
|
|
489
|
+
return ToolException("You have to define Figma token.")
|
|
774
490
|
logging.info("Successfully authenticated to Figma.")
|
|
775
491
|
except Exception as e:
|
|
776
492
|
msg = f"Failed to authenticate with Figma: {str(e)}"
|
|
777
493
|
logging.error(msg)
|
|
778
|
-
|
|
494
|
+
return ToolException(msg)
|
|
779
495
|
|
|
780
496
|
return values
|
|
781
497
|
|
|
@@ -938,132 +654,6 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
938
654
|
"""Reads a specified file by field key from Figma."""
|
|
939
655
|
return self._client.get_file(file_key, geometry, version)
|
|
940
656
|
|
|
941
|
-
@process_output
|
|
942
|
-
def get_file_summary(
|
|
943
|
-
self,
|
|
944
|
-
url: Optional[str] = None,
|
|
945
|
-
file_key: Optional[str] = None,
|
|
946
|
-
include_node_ids: Optional[str] = None,
|
|
947
|
-
exclude_node_ids: Optional[str] = None,
|
|
948
|
-
**kwargs,
|
|
949
|
-
):
|
|
950
|
-
"""Summarizes a Figma file by loading pages and nodes via URL or file key.
|
|
951
|
-
|
|
952
|
-
Configuration for image processing and summarization is taken from the toolkit
|
|
953
|
-
configuration (see FigmaToolkit.toolkit_config_schema):
|
|
954
|
-
|
|
955
|
-
- self.apply_images_prompt: if True, pass self.images_prompt to the image-processing step.
|
|
956
|
-
- self.images_prompt: instruction string for how to treat image-based nodes.
|
|
957
|
-
- self.apply_summary_prompt: if True and self.summary_prompt is set and an LLM is configured,
|
|
958
|
-
return a single summarized string; otherwise return the raw list of node documents.
|
|
959
|
-
- self.summary_prompt: instruction string for LLM summarization.
|
|
960
|
-
|
|
961
|
-
Tool arguments mirror ArgsSchema.FileSummary and control only which file/pages are loaded.
|
|
962
|
-
"""
|
|
963
|
-
# Prepare params for _base_loader without evaluating any logic here
|
|
964
|
-
node_ids_include_list = None
|
|
965
|
-
node_ids_exclude_list = None
|
|
966
|
-
|
|
967
|
-
if include_node_ids:
|
|
968
|
-
node_ids_include_list = [nid.strip() for nid in include_node_ids.split(',') if nid.strip()]
|
|
969
|
-
|
|
970
|
-
if exclude_node_ids:
|
|
971
|
-
node_ids_exclude_list = [nid.strip() for nid in exclude_node_ids.split(',') if nid.strip()]
|
|
972
|
-
|
|
973
|
-
# Delegate URL and file_key handling to _base_loader
|
|
974
|
-
base_docs = self._base_loader(
|
|
975
|
-
url=url,
|
|
976
|
-
file_keys_include=[file_key] if file_key else None,
|
|
977
|
-
node_ids_include=node_ids_include_list,
|
|
978
|
-
node_ids_exclude=node_ids_exclude_list,
|
|
979
|
-
)
|
|
980
|
-
|
|
981
|
-
# Read prompt-related configuration from toolkit instance (set via wrapper_payload)
|
|
982
|
-
apply_images_prompt = getattr(self, "apply_images_prompt", False)
|
|
983
|
-
images_prompt = getattr(self, "images_prompt", None)
|
|
984
|
-
apply_summary_prompt = getattr(self, "apply_summary_prompt", True)
|
|
985
|
-
summary_prompt = getattr(self, "summary_prompt", None)
|
|
986
|
-
|
|
987
|
-
# Decide whether to apply images_prompt. Expect dict with 'prompt'.
|
|
988
|
-
if (
|
|
989
|
-
apply_images_prompt
|
|
990
|
-
and isinstance(images_prompt, dict)
|
|
991
|
-
and isinstance(images_prompt.get("prompt"), str)
|
|
992
|
-
and images_prompt["prompt"].strip()
|
|
993
|
-
):
|
|
994
|
-
images_prompt_str = images_prompt["prompt"].strip()
|
|
995
|
-
else:
|
|
996
|
-
images_prompt_str = ""
|
|
997
|
-
|
|
998
|
-
results: List[Dict] = []
|
|
999
|
-
for base_doc in base_docs:
|
|
1000
|
-
for dep in self._process_document(
|
|
1001
|
-
base_doc,
|
|
1002
|
-
images_prompt_str,
|
|
1003
|
-
):
|
|
1004
|
-
results.append({
|
|
1005
|
-
"page_content": dep.page_content,
|
|
1006
|
-
"metadata": dep.metadata,
|
|
1007
|
-
})
|
|
1008
|
-
|
|
1009
|
-
# Decide whether to apply summary_prompt
|
|
1010
|
-
has_summary_prompt = bool(
|
|
1011
|
-
isinstance(summary_prompt, dict)
|
|
1012
|
-
and isinstance(summary_prompt.get("prompt"), str)
|
|
1013
|
-
and summary_prompt["prompt"].strip()
|
|
1014
|
-
)
|
|
1015
|
-
if not apply_summary_prompt or not has_summary_prompt:
|
|
1016
|
-
# Return raw docs when summary is disabled or no prompt provided
|
|
1017
|
-
self._log_tool_event("Summary prompt not provided: returning raw documents.")
|
|
1018
|
-
return results
|
|
1019
|
-
|
|
1020
|
-
# If summary_prompt is enabled, generate an LLM-based summary over the loaded docs
|
|
1021
|
-
try:
|
|
1022
|
-
# Build a structured, ordered view of images and texts to help the LLM infer flows.
|
|
1023
|
-
blocks = []
|
|
1024
|
-
for item in results:
|
|
1025
|
-
metadata = item.get("metadata", {}) or {}
|
|
1026
|
-
node_type = str(metadata.get("type", "")).lower()
|
|
1027
|
-
node_id = metadata.get("node_id") or metadata.get("id", "")
|
|
1028
|
-
page_content = str(item.get("page_content", "")).strip()
|
|
1029
|
-
|
|
1030
|
-
if not page_content:
|
|
1031
|
-
continue
|
|
1032
|
-
|
|
1033
|
-
if node_type == "image":
|
|
1034
|
-
image_url = metadata.get("image_url", "")
|
|
1035
|
-
header = f"Image ({node_id}), {image_url}".strip().rstrip(',')
|
|
1036
|
-
body = page_content
|
|
1037
|
-
else:
|
|
1038
|
-
header = f"Text ({node_id})".strip()
|
|
1039
|
-
body = page_content
|
|
1040
|
-
|
|
1041
|
-
block = f"{header}\n{body}\n--------------------"
|
|
1042
|
-
blocks.append(block)
|
|
1043
|
-
|
|
1044
|
-
full_content = "\n".join(blocks) if blocks else "(no content)"
|
|
1045
|
-
self._log_tool_event("Invoking LLM for Figma file summary.")
|
|
1046
|
-
|
|
1047
|
-
if not getattr(self, "llm", None):
|
|
1048
|
-
raise RuntimeError("LLM is not configured for this toolkit; cannot apply summary_prompt.")
|
|
1049
|
-
|
|
1050
|
-
# Use the 'prompt' field from the summary_prompt dict as the instruction block
|
|
1051
|
-
summary_prompt_text = summary_prompt["prompt"].strip()
|
|
1052
|
-
prompt_text = f"{summary_prompt_text}\n\nCONTENT BEGIN\n{full_content}\nCONTENT END"
|
|
1053
|
-
llm_response = self.llm.invoke(prompt_text) if hasattr(self.llm, "invoke") else self.llm(prompt_text)
|
|
1054
|
-
|
|
1055
|
-
if hasattr(llm_response, "content"):
|
|
1056
|
-
summary_text = str(llm_response.content)
|
|
1057
|
-
else:
|
|
1058
|
-
summary_text = str(llm_response)
|
|
1059
|
-
|
|
1060
|
-
self._log_tool_event("Successfully generated LLM-based file summary.")
|
|
1061
|
-
return summary_text
|
|
1062
|
-
except Exception as e:
|
|
1063
|
-
logging.warning(f"Failed to apply summary_prompt in get_file_summary: {e}")
|
|
1064
|
-
self._log_tool_event("Falling back to raw documents due to summary_prompt failure.")
|
|
1065
|
-
return results
|
|
1066
|
-
|
|
1067
657
|
@process_output
|
|
1068
658
|
def get_file_versions(self, file_key: str, **kwargs):
|
|
1069
659
|
"""Retrieves the version history of a specified file from Figma."""
|
|
@@ -1119,608 +709,6 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
1119
709
|
"""Retrieves all files for a specified project ID from Figma."""
|
|
1120
710
|
return self._client.get_project_files(project_id)
|
|
1121
711
|
|
|
1122
|
-
# -------------------------------------------------------------------------
|
|
1123
|
-
# TOON Format Tools (Token-Optimized Output)
|
|
1124
|
-
# -------------------------------------------------------------------------
|
|
1125
|
-
|
|
1126
|
-
def get_file_structure_toon(
|
|
1127
|
-
self,
|
|
1128
|
-
url: Optional[str] = None,
|
|
1129
|
-
file_key: Optional[str] = None,
|
|
1130
|
-
include_pages: Optional[str] = None,
|
|
1131
|
-
exclude_pages: Optional[str] = None,
|
|
1132
|
-
max_frames: int = 50,
|
|
1133
|
-
**kwargs,
|
|
1134
|
-
) -> str:
|
|
1135
|
-
"""
|
|
1136
|
-
Get file structure in TOON format - optimized for LLM token consumption.
|
|
1137
|
-
|
|
1138
|
-
Returns a compact, human-readable format with:
|
|
1139
|
-
- Page and frame hierarchy
|
|
1140
|
-
- Text content categorized (headings, labels, buttons, body, errors)
|
|
1141
|
-
- Component usage
|
|
1142
|
-
- Inferred screen types and states
|
|
1143
|
-
- Flow analysis (sequences, variants, CTA destinations)
|
|
1144
|
-
|
|
1145
|
-
TOON format uses ~70% fewer tokens than JSON for the same data.
|
|
1146
|
-
|
|
1147
|
-
Use this tool when you need to:
|
|
1148
|
-
- Understand overall file structure quickly
|
|
1149
|
-
- Generate user journey documentation
|
|
1150
|
-
- Analyze screen flows and navigation
|
|
1151
|
-
- Identify UI patterns and components
|
|
1152
|
-
"""
|
|
1153
|
-
self._log_tool_event("Getting file structure in TOON format")
|
|
1154
|
-
|
|
1155
|
-
# Parse URL or use file_key
|
|
1156
|
-
if url:
|
|
1157
|
-
file_key, node_ids_from_url = self._parse_figma_url(url)
|
|
1158
|
-
if node_ids_from_url and not include_pages:
|
|
1159
|
-
include_pages = ','.join(node_ids_from_url)
|
|
1160
|
-
|
|
1161
|
-
if not file_key:
|
|
1162
|
-
raise ToolException("Either url or file_key must be provided")
|
|
1163
|
-
|
|
1164
|
-
# Parse include/exclude pages
|
|
1165
|
-
include_ids = [p.strip() for p in include_pages.split(',')] if include_pages else None
|
|
1166
|
-
exclude_ids = [p.strip() for p in exclude_pages.split(',')] if exclude_pages else None
|
|
1167
|
-
|
|
1168
|
-
# Get file structure (shallow fetch - only top-level pages, not full content)
|
|
1169
|
-
# This avoids "Request too large" errors for big files
|
|
1170
|
-
self._log_tool_event(f"Fetching file structure for {file_key}")
|
|
1171
|
-
file_data = self._client.get_file(file_key, geometry='depth=1')
|
|
1172
|
-
|
|
1173
|
-
if not file_data:
|
|
1174
|
-
raise ToolException(f"Failed to retrieve file {file_key}")
|
|
1175
|
-
|
|
1176
|
-
# Process pages
|
|
1177
|
-
pages_data = []
|
|
1178
|
-
all_pages = file_data.document.get('children', [])
|
|
1179
|
-
|
|
1180
|
-
for page_node in all_pages:
|
|
1181
|
-
page_id = page_node.get('id', '')
|
|
1182
|
-
|
|
1183
|
-
# Apply page filters
|
|
1184
|
-
if include_ids and page_id not in include_ids and page_id.replace(':', '-') not in include_ids:
|
|
1185
|
-
continue
|
|
1186
|
-
if exclude_ids and not include_ids:
|
|
1187
|
-
if page_id in exclude_ids or page_id.replace(':', '-') in exclude_ids:
|
|
1188
|
-
continue
|
|
1189
|
-
|
|
1190
|
-
self._log_tool_event(f"Processing page: {page_node.get('name', 'Untitled')}")
|
|
1191
|
-
|
|
1192
|
-
# Fetch full page content individually (avoids large single request)
|
|
1193
|
-
try:
|
|
1194
|
-
page_full = self._get_file_nodes(file_key, page_id)
|
|
1195
|
-
if page_full:
|
|
1196
|
-
page_content = page_full.get('nodes', {}).get(page_id, {}).get('document', page_node)
|
|
1197
|
-
else:
|
|
1198
|
-
page_content = page_node
|
|
1199
|
-
except Exception as e:
|
|
1200
|
-
self._log_tool_event(f"Warning: Could not fetch full page content for {page_id}: {e}")
|
|
1201
|
-
page_content = page_node
|
|
1202
|
-
|
|
1203
|
-
page_data = process_page_to_toon_data(page_content)
|
|
1204
|
-
|
|
1205
|
-
# Limit frames per page
|
|
1206
|
-
if len(page_data['frames']) > max_frames:
|
|
1207
|
-
page_data['frames'] = page_data['frames'][:max_frames]
|
|
1208
|
-
page_data['truncated'] = True
|
|
1209
|
-
|
|
1210
|
-
pages_data.append(page_data)
|
|
1211
|
-
|
|
1212
|
-
# Build file data structure
|
|
1213
|
-
toon_data = {
|
|
1214
|
-
'name': file_data.name,
|
|
1215
|
-
'key': file_key,
|
|
1216
|
-
'pages': pages_data,
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
# Serialize to TOON format
|
|
1220
|
-
serializer = TOONSerializer()
|
|
1221
|
-
result = serializer.serialize_file(toon_data)
|
|
1222
|
-
|
|
1223
|
-
self._log_tool_event("File structure extracted in TOON format")
|
|
1224
|
-
return result
|
|
1225
|
-
|
|
1226
|
-
def get_page_flows_toon(
|
|
1227
|
-
self,
|
|
1228
|
-
url: Optional[str] = None,
|
|
1229
|
-
file_key: Optional[str] = None,
|
|
1230
|
-
page_id: Optional[str] = None,
|
|
1231
|
-
**kwargs,
|
|
1232
|
-
) -> str:
|
|
1233
|
-
"""
|
|
1234
|
-
Analyze a single page for user flows in TOON format.
|
|
1235
|
-
|
|
1236
|
-
Returns detailed flow analysis:
|
|
1237
|
-
- Frame sequence detection (from naming: 01_, Step 1, etc.)
|
|
1238
|
-
- Screen variant grouping (Login, Login_Error, Login_Loading)
|
|
1239
|
-
- CTA/button destination mapping
|
|
1240
|
-
- Spatial ordering hints
|
|
1241
|
-
|
|
1242
|
-
Use this for in-depth flow analysis of a specific page.
|
|
1243
|
-
Requires a PAGE ID (not a frame ID). Use get_file_structure_toon to find page IDs.
|
|
1244
|
-
"""
|
|
1245
|
-
self._log_tool_event("Analyzing page flows in TOON format")
|
|
1246
|
-
|
|
1247
|
-
# Parse URL
|
|
1248
|
-
if url:
|
|
1249
|
-
file_key, node_ids_from_url = self._parse_figma_url(url)
|
|
1250
|
-
if node_ids_from_url:
|
|
1251
|
-
page_id = node_ids_from_url[0]
|
|
1252
|
-
|
|
1253
|
-
if not file_key:
|
|
1254
|
-
raise ToolException("Either url or file_key must be provided")
|
|
1255
|
-
if not page_id:
|
|
1256
|
-
raise ToolException("page_id must be provided (or include node-id in URL)")
|
|
1257
|
-
|
|
1258
|
-
# Fetch node content
|
|
1259
|
-
self._log_tool_event(f"Fetching node {page_id} from file {file_key}")
|
|
1260
|
-
node_full = self._get_file_nodes(file_key, page_id)
|
|
1261
|
-
|
|
1262
|
-
if not node_full:
|
|
1263
|
-
raise ToolException(f"Failed to retrieve node {page_id}")
|
|
1264
|
-
|
|
1265
|
-
node_content = node_full.get('nodes', {}).get(page_id, {}).get('document', {})
|
|
1266
|
-
if not node_content:
|
|
1267
|
-
raise ToolException(f"Node {page_id} has no content")
|
|
1268
|
-
|
|
1269
|
-
# Check if this is a page (CANVAS) or a frame
|
|
1270
|
-
node_type = node_content.get('type', '').upper()
|
|
1271
|
-
if node_type != 'CANVAS':
|
|
1272
|
-
# This is a frame, not a page - provide helpful error
|
|
1273
|
-
raise ToolException(
|
|
1274
|
-
f"Node {page_id} is a {node_type}, not a PAGE. "
|
|
1275
|
-
f"This tool requires a page ID. Use get_file_structure_toon first to find page IDs "
|
|
1276
|
-
f"(look for PAGE: ... #<page_id>)"
|
|
1277
|
-
)
|
|
1278
|
-
|
|
1279
|
-
page_content = node_content
|
|
1280
|
-
|
|
1281
|
-
# Process page
|
|
1282
|
-
page_data = process_page_to_toon_data(page_content)
|
|
1283
|
-
frames = page_data.get('frames', [])
|
|
1284
|
-
|
|
1285
|
-
# Build detailed flow analysis
|
|
1286
|
-
lines = []
|
|
1287
|
-
lines.append(f"PAGE: {page_data.get('name', 'Untitled')} [id:{page_id}]")
|
|
1288
|
-
lines.append(f" frames: {len(frames)}")
|
|
1289
|
-
lines.append("")
|
|
1290
|
-
|
|
1291
|
-
# Sequence analysis
|
|
1292
|
-
sequences = detect_sequences(frames)
|
|
1293
|
-
if sequences:
|
|
1294
|
-
lines.append("SEQUENCES (by naming):")
|
|
1295
|
-
for seq in sequences:
|
|
1296
|
-
lines.append(f" {' > '.join(seq)}")
|
|
1297
|
-
lines.append("")
|
|
1298
|
-
|
|
1299
|
-
# Variant analysis
|
|
1300
|
-
variants = group_variants(frames)
|
|
1301
|
-
if variants:
|
|
1302
|
-
lines.append("VARIANTS (grouped screens):")
|
|
1303
|
-
for base, variant_list in variants.items():
|
|
1304
|
-
lines.append(f" {base}:")
|
|
1305
|
-
for v in variant_list:
|
|
1306
|
-
v_name = v.get('name', '')
|
|
1307
|
-
v_id = v.get('id', '')
|
|
1308
|
-
state = next((f.get('state', 'default') for f in frames if f.get('name') == v_name), 'default')
|
|
1309
|
-
lines.append(f" - {v_name} [{state}] #{v_id}")
|
|
1310
|
-
lines.append("")
|
|
1311
|
-
|
|
1312
|
-
# CTA mapping
|
|
1313
|
-
lines.append("CTA DESTINATIONS:")
|
|
1314
|
-
cta_map = {}
|
|
1315
|
-
for frame in frames:
|
|
1316
|
-
frame_name = frame.get('name', '')
|
|
1317
|
-
for btn in frame.get('buttons', []):
|
|
1318
|
-
dest = infer_cta_destination(btn)
|
|
1319
|
-
if dest not in cta_map:
|
|
1320
|
-
cta_map[dest] = []
|
|
1321
|
-
cta_map[dest].append(f'"{btn}" in {frame_name}')
|
|
1322
|
-
|
|
1323
|
-
for dest, ctas in cta_map.items():
|
|
1324
|
-
lines.append(f" > {dest}:")
|
|
1325
|
-
for cta in ctas[:5]: # Limit per destination
|
|
1326
|
-
lines.append(f" {cta}")
|
|
1327
|
-
lines.append("")
|
|
1328
|
-
|
|
1329
|
-
# Spatial ordering
|
|
1330
|
-
lines.append("SPATIAL ORDER (canvas position):")
|
|
1331
|
-
sorted_frames = sorted(frames, key=lambda f: (f['position']['y'], f['position']['x']))
|
|
1332
|
-
for i, frame in enumerate(sorted_frames[:20], 1):
|
|
1333
|
-
pos = frame.get('position', {})
|
|
1334
|
-
lines.append(f" {i}. {frame.get('name', '')} [{int(pos.get('x', 0))},{int(pos.get('y', 0))}]")
|
|
1335
|
-
|
|
1336
|
-
# Frame details
|
|
1337
|
-
lines.append("")
|
|
1338
|
-
lines.append("FRAME DETAILS:")
|
|
1339
|
-
|
|
1340
|
-
serializer = TOONSerializer()
|
|
1341
|
-
for frame in frames[:30]: # Limit frames
|
|
1342
|
-
frame_lines = serializer.serialize_frame(frame, level=1)
|
|
1343
|
-
lines.extend(frame_lines)
|
|
1344
|
-
|
|
1345
|
-
self._log_tool_event("Page flow analysis complete")
|
|
1346
|
-
return '\n'.join(lines)
|
|
1347
|
-
|
|
1348
|
-
def get_frame_detail_toon(
|
|
1349
|
-
self,
|
|
1350
|
-
file_key: str,
|
|
1351
|
-
frame_ids: str,
|
|
1352
|
-
**kwargs,
|
|
1353
|
-
) -> str:
|
|
1354
|
-
"""
|
|
1355
|
-
Get detailed information for specific frames in TOON format.
|
|
1356
|
-
|
|
1357
|
-
Returns per-frame:
|
|
1358
|
-
- All text content (headings, labels, buttons, body, errors)
|
|
1359
|
-
- Component hierarchy
|
|
1360
|
-
- Inferred screen type and state
|
|
1361
|
-
- Position and size
|
|
1362
|
-
|
|
1363
|
-
Use this to drill down into specific screens identified from file structure.
|
|
1364
|
-
"""
|
|
1365
|
-
try:
|
|
1366
|
-
return self._get_frame_detail_toon_internal(file_key=file_key, frame_ids=frame_ids, **kwargs)
|
|
1367
|
-
except ToolException as e:
|
|
1368
|
-
raise ToolException(_handle_figma_error(e))
|
|
1369
|
-
|
|
1370
|
-
def _get_frame_detail_toon_internal(
|
|
1371
|
-
self,
|
|
1372
|
-
file_key: str,
|
|
1373
|
-
frame_ids: str,
|
|
1374
|
-
**kwargs,
|
|
1375
|
-
) -> str:
|
|
1376
|
-
"""Internal implementation of get_frame_detail_toon without error handling wrapper."""
|
|
1377
|
-
self._log_tool_event("Getting frame details in TOON format")
|
|
1378
|
-
|
|
1379
|
-
ids_list = [fid.strip() for fid in frame_ids.split(',') if fid.strip()]
|
|
1380
|
-
if not ids_list:
|
|
1381
|
-
raise ToolException("frame_ids must contain at least one frame ID")
|
|
1382
|
-
|
|
1383
|
-
# Fetch frames
|
|
1384
|
-
self._log_tool_event(f"Fetching {len(ids_list)} frames from file {file_key}")
|
|
1385
|
-
nodes_data = self._get_file_nodes(file_key, ','.join(ids_list))
|
|
1386
|
-
|
|
1387
|
-
if not nodes_data:
|
|
1388
|
-
raise ToolException(f"Failed to retrieve frames from file {file_key}")
|
|
1389
|
-
|
|
1390
|
-
# Process each frame
|
|
1391
|
-
lines = [f"FRAMES [{len(ids_list)} requested]", ""]
|
|
1392
|
-
|
|
1393
|
-
serializer = TOONSerializer()
|
|
1394
|
-
|
|
1395
|
-
for frame_id in ids_list:
|
|
1396
|
-
node_data = nodes_data.get('nodes', {}).get(frame_id, {})
|
|
1397
|
-
frame_node = node_data.get('document', {})
|
|
1398
|
-
|
|
1399
|
-
if not frame_node:
|
|
1400
|
-
lines.append(f"FRAME: {frame_id} [NOT FOUND]")
|
|
1401
|
-
lines.append("")
|
|
1402
|
-
continue
|
|
1403
|
-
|
|
1404
|
-
frame_data = process_frame_to_toon_data(frame_node)
|
|
1405
|
-
frame_lines = serializer.serialize_frame(frame_data, level=0)
|
|
1406
|
-
lines.extend(frame_lines)
|
|
1407
|
-
|
|
1408
|
-
# Add extra details for individual frames
|
|
1409
|
-
lines.append(f" ID: {frame_id}")
|
|
1410
|
-
|
|
1411
|
-
# Component breakdown
|
|
1412
|
-
components = frame_data.get('components', [])
|
|
1413
|
-
if components:
|
|
1414
|
-
# Count component usage
|
|
1415
|
-
from collections import Counter
|
|
1416
|
-
comp_counts = Counter(components)
|
|
1417
|
-
lines.append(f" COMPONENT_COUNTS:")
|
|
1418
|
-
for comp, count in comp_counts.most_common(10):
|
|
1419
|
-
lines.append(f" {comp}: {count}")
|
|
1420
|
-
|
|
1421
|
-
lines.append("")
|
|
1422
|
-
|
|
1423
|
-
self._log_tool_event("Frame details extracted")
|
|
1424
|
-
return '\n'.join(lines)
|
|
1425
|
-
|
|
1426
|
-
def analyze_file(
|
|
1427
|
-
self,
|
|
1428
|
-
url: Optional[str] = None,
|
|
1429
|
-
file_key: Optional[str] = None,
|
|
1430
|
-
node_id: Optional[str] = None,
|
|
1431
|
-
include_pages: Optional[str] = None,
|
|
1432
|
-
exclude_pages: Optional[str] = None,
|
|
1433
|
-
max_frames: int = 50,
|
|
1434
|
-
**kwargs,
|
|
1435
|
-
) -> str:
|
|
1436
|
-
"""
|
|
1437
|
-
Comprehensive Figma file analyzer with LLM-powered insights.
|
|
1438
|
-
|
|
1439
|
-
Returns detailed analysis including:
|
|
1440
|
-
- File/page/frame structure with all content (text, buttons, components)
|
|
1441
|
-
- LLM-powered screen explanations with visual insights (using frame images)
|
|
1442
|
-
- LLM-powered user flow analysis identifying key user journeys
|
|
1443
|
-
- Design insights (patterns, gaps, recommendations)
|
|
1444
|
-
|
|
1445
|
-
Drill-Down:
|
|
1446
|
-
- No node_id: Analyzes entire file (respecting include/exclude pages)
|
|
1447
|
-
- node_id=page_id: Focuses on specific page
|
|
1448
|
-
- node_id=frame_id: Returns detailed frame analysis
|
|
1449
|
-
|
|
1450
|
-
For targeted analysis of specific frames (2-3 frames), use get_frame_detail_toon instead.
|
|
1451
|
-
"""
|
|
1452
|
-
try:
|
|
1453
|
-
return self._analyze_file_internal(
|
|
1454
|
-
url=url,
|
|
1455
|
-
file_key=file_key,
|
|
1456
|
-
node_id=node_id,
|
|
1457
|
-
include_pages=include_pages,
|
|
1458
|
-
exclude_pages=exclude_pages,
|
|
1459
|
-
max_frames=max_frames,
|
|
1460
|
-
**kwargs,
|
|
1461
|
-
)
|
|
1462
|
-
except ToolException as e:
|
|
1463
|
-
raise ToolException(_handle_figma_error(e))
|
|
1464
|
-
|
|
1465
|
-
def _analyze_file_internal(
|
|
1466
|
-
self,
|
|
1467
|
-
url: Optional[str] = None,
|
|
1468
|
-
file_key: Optional[str] = None,
|
|
1469
|
-
node_id: Optional[str] = None,
|
|
1470
|
-
include_pages: Optional[str] = None,
|
|
1471
|
-
exclude_pages: Optional[str] = None,
|
|
1472
|
-
max_frames: int = 50,
|
|
1473
|
-
**kwargs,
|
|
1474
|
-
) -> str:
|
|
1475
|
-
"""Internal implementation of analyze_file without error handling wrapper."""
|
|
1476
|
-
# Always use maximum detail level and LLM analysis
|
|
1477
|
-
detail_level = 3
|
|
1478
|
-
llm_analysis = 'detailed' if self.llm else 'none'
|
|
1479
|
-
self._log_tool_event(f"Getting file in TOON format (detail_level={detail_level}, llm_analysis={llm_analysis})")
|
|
1480
|
-
|
|
1481
|
-
# Parse URL if provided
|
|
1482
|
-
if url:
|
|
1483
|
-
file_key, node_ids_from_url = self._parse_figma_url(url)
|
|
1484
|
-
if node_ids_from_url and not node_id:
|
|
1485
|
-
node_id = node_ids_from_url[0]
|
|
1486
|
-
|
|
1487
|
-
if not file_key:
|
|
1488
|
-
raise ToolException("Either url or file_key must be provided")
|
|
1489
|
-
|
|
1490
|
-
# Convert node_id from URL format (hyphen) to API format (colon)
|
|
1491
|
-
if node_id:
|
|
1492
|
-
node_id = node_id.replace('-', ':')
|
|
1493
|
-
|
|
1494
|
-
# Check if node_id is a frame or page (for drill-down)
|
|
1495
|
-
node_id_is_page = False
|
|
1496
|
-
if node_id:
|
|
1497
|
-
try:
|
|
1498
|
-
nodes_data = self._get_file_nodes(file_key, node_id)
|
|
1499
|
-
if nodes_data:
|
|
1500
|
-
node_info = nodes_data.get('nodes', {}).get(node_id, {})
|
|
1501
|
-
node_doc = node_info.get('document', {})
|
|
1502
|
-
node_type = node_doc.get('type', '').upper()
|
|
1503
|
-
|
|
1504
|
-
if node_type == 'FRAME':
|
|
1505
|
-
# It's a frame - use frame detail tool (internal to avoid double-wrapping)
|
|
1506
|
-
return self._get_frame_detail_toon_internal(file_key=file_key, frame_ids=node_id)
|
|
1507
|
-
elif node_type == 'CANVAS':
|
|
1508
|
-
# It's a page - we'll filter to this page
|
|
1509
|
-
node_id_is_page = True
|
|
1510
|
-
except Exception:
|
|
1511
|
-
pass # Fall through to page/file analysis
|
|
1512
|
-
|
|
1513
|
-
# Get file structure
|
|
1514
|
-
file_data = self._client.get_file(file_key, geometry='depth=1')
|
|
1515
|
-
if not file_data:
|
|
1516
|
-
raise ToolException(f"Failed to retrieve file {file_key}")
|
|
1517
|
-
|
|
1518
|
-
# Determine which pages to process
|
|
1519
|
-
# Check if document exists and has the expected structure
|
|
1520
|
-
if not hasattr(file_data, 'document') or file_data.document is None:
|
|
1521
|
-
self._log_tool_event(f"Warning: file_data has no document attribute. Type: {type(file_data)}")
|
|
1522
|
-
all_pages = []
|
|
1523
|
-
else:
|
|
1524
|
-
all_pages = file_data.document.get('children', [])
|
|
1525
|
-
self._log_tool_event(f"File has {len(all_pages)} pages, node_id={node_id}, node_id_is_page={node_id_is_page}")
|
|
1526
|
-
|
|
1527
|
-
# Only filter by node_id if it's confirmed to be a page ID
|
|
1528
|
-
if node_id and node_id_is_page:
|
|
1529
|
-
include_pages = node_id
|
|
1530
|
-
|
|
1531
|
-
include_ids = [p.strip() for p in include_pages.split(',')] if include_pages else None
|
|
1532
|
-
exclude_ids = [p.strip() for p in exclude_pages.split(',')] if exclude_pages else None
|
|
1533
|
-
|
|
1534
|
-
pages_to_process = []
|
|
1535
|
-
for page_node in all_pages:
|
|
1536
|
-
page_id = page_node.get('id', '')
|
|
1537
|
-
if include_ids and page_id not in include_ids:
|
|
1538
|
-
continue
|
|
1539
|
-
if exclude_ids and page_id in exclude_ids:
|
|
1540
|
-
continue
|
|
1541
|
-
pages_to_process.append(page_node)
|
|
1542
|
-
|
|
1543
|
-
# Build output based on detail level
|
|
1544
|
-
lines = [f"FILE: {file_data.name} [key:{file_key}]"]
|
|
1545
|
-
serializer = TOONSerializer()
|
|
1546
|
-
|
|
1547
|
-
all_frames_for_flows = [] # Collect frames for flow analysis at Level 2+
|
|
1548
|
-
|
|
1549
|
-
if not pages_to_process:
|
|
1550
|
-
if not all_pages:
|
|
1551
|
-
lines.append(" [No pages found in file - file may be empty or access restricted]")
|
|
1552
|
-
else:
|
|
1553
|
-
lines.append(f" [All {len(all_pages)} pages filtered out by include/exclude settings]")
|
|
1554
|
-
self._log_tool_event(f"No pages to process. all_pages={len(all_pages)}, include_ids={include_ids}, exclude_ids={exclude_ids}")
|
|
1555
|
-
|
|
1556
|
-
self._log_tool_event(f"Processing {len(pages_to_process)} pages at detail_level={detail_level}")
|
|
1557
|
-
|
|
1558
|
-
for page_node in pages_to_process:
|
|
1559
|
-
page_id = page_node.get('id', '')
|
|
1560
|
-
page_name = page_node.get('name', 'Untitled')
|
|
1561
|
-
|
|
1562
|
-
if detail_level == 1:
|
|
1563
|
-
# Level 1: Structure only - just hierarchy with IDs
|
|
1564
|
-
lines.append(f" PAGE: {page_name} #{page_id}")
|
|
1565
|
-
frames = page_node.get('children', [])[:max_frames]
|
|
1566
|
-
for frame in frames:
|
|
1567
|
-
if frame.get('type', '').upper() == 'FRAME':
|
|
1568
|
-
frame_id = frame.get('id', '')
|
|
1569
|
-
frame_name = frame.get('name', 'Untitled')
|
|
1570
|
-
lines.append(f" FRAME: {frame_name} #{frame_id}")
|
|
1571
|
-
else:
|
|
1572
|
-
# Level 2+: Need full page content - fetch via nodes API
|
|
1573
|
-
page_fetch_error = None
|
|
1574
|
-
try:
|
|
1575
|
-
nodes_data = self._get_file_nodes(file_key, page_id)
|
|
1576
|
-
if nodes_data:
|
|
1577
|
-
full_page_node = nodes_data.get('nodes', {}).get(page_id, {}).get('document', {})
|
|
1578
|
-
if full_page_node:
|
|
1579
|
-
page_node = full_page_node
|
|
1580
|
-
except ToolException as e:
|
|
1581
|
-
page_fetch_error = _handle_figma_error(e)
|
|
1582
|
-
self._log_tool_event(f"Error fetching page {page_id}: {page_fetch_error}")
|
|
1583
|
-
except Exception as e:
|
|
1584
|
-
page_fetch_error = str(e)
|
|
1585
|
-
self._log_tool_event(f"Error fetching page {page_id}: {e}")
|
|
1586
|
-
|
|
1587
|
-
# Process whatever data we have (full or shallow)
|
|
1588
|
-
page_data = process_page_to_toon_data(page_node, max_frames=max_frames)
|
|
1589
|
-
frames = page_data.get('frames', [])
|
|
1590
|
-
|
|
1591
|
-
# If we had an error and got no frames, show the error
|
|
1592
|
-
if page_fetch_error and not frames:
|
|
1593
|
-
lines.append(f" PAGE: {page_name} #{page_id}")
|
|
1594
|
-
lines.append(f" [Error: {page_fetch_error}]")
|
|
1595
|
-
continue
|
|
1596
|
-
|
|
1597
|
-
if detail_level == 2:
|
|
1598
|
-
# Level 2: Standard - content via serialize_page
|
|
1599
|
-
page_lines = serializer.serialize_page(page_data, level=0)
|
|
1600
|
-
lines.extend(page_lines)
|
|
1601
|
-
else:
|
|
1602
|
-
# Level 3: Detailed - content + per-frame component counts
|
|
1603
|
-
lines.append(f"PAGE: {page_data.get('name', 'Untitled')} #{page_data.get('id', '')}")
|
|
1604
|
-
for frame_data in frames:
|
|
1605
|
-
frame_lines = serializer.serialize_frame(frame_data, level=1)
|
|
1606
|
-
lines.extend(frame_lines)
|
|
1607
|
-
|
|
1608
|
-
# Add detailed component counts
|
|
1609
|
-
components = frame_data.get('components', [])
|
|
1610
|
-
if components:
|
|
1611
|
-
from collections import Counter
|
|
1612
|
-
comp_counts = Counter(components)
|
|
1613
|
-
lines.append(f" COMPONENT_COUNTS:")
|
|
1614
|
-
for comp, count in comp_counts.most_common(10):
|
|
1615
|
-
lines.append(f" {comp}: {count}")
|
|
1616
|
-
|
|
1617
|
-
# Collect frames for flow analysis
|
|
1618
|
-
all_frames_for_flows.extend(frames)
|
|
1619
|
-
|
|
1620
|
-
lines.append("")
|
|
1621
|
-
|
|
1622
|
-
# Level 2+: Add global flow analysis at the end
|
|
1623
|
-
if detail_level >= 2 and all_frames_for_flows:
|
|
1624
|
-
flow_lines = serializer.serialize_flows(all_frames_for_flows, level=0)
|
|
1625
|
-
if flow_lines:
|
|
1626
|
-
lines.append("FLOWS:")
|
|
1627
|
-
lines.extend(flow_lines)
|
|
1628
|
-
|
|
1629
|
-
toon_output = '\n'.join(lines)
|
|
1630
|
-
|
|
1631
|
-
# Add LLM analysis if requested
|
|
1632
|
-
if llm_analysis and llm_analysis != 'none' and self.llm:
|
|
1633
|
-
self._log_tool_event(f"Running LLM analysis (level={llm_analysis})")
|
|
1634
|
-
try:
|
|
1635
|
-
# Build file_data structure for LLM analysis
|
|
1636
|
-
file_data_for_llm = {
|
|
1637
|
-
'name': file_data.name,
|
|
1638
|
-
'key': file_key,
|
|
1639
|
-
'pages': [],
|
|
1640
|
-
}
|
|
1641
|
-
# Collect frame IDs for image fetching (for detailed analysis)
|
|
1642
|
-
all_frame_ids = []
|
|
1643
|
-
|
|
1644
|
-
# Re-use processed page data
|
|
1645
|
-
for page_node in pages_to_process:
|
|
1646
|
-
page_id = page_node.get('id', '')
|
|
1647
|
-
try:
|
|
1648
|
-
# Fetch full page if needed
|
|
1649
|
-
nodes_data = self._get_file_nodes(file_key, page_id)
|
|
1650
|
-
if nodes_data:
|
|
1651
|
-
full_page_node = nodes_data.get('nodes', {}).get(page_id, {}).get('document', {})
|
|
1652
|
-
if full_page_node:
|
|
1653
|
-
page_node = full_page_node
|
|
1654
|
-
except Exception:
|
|
1655
|
-
pass # Use shallow data
|
|
1656
|
-
page_data = process_page_to_toon_data(page_node, max_frames=max_frames)
|
|
1657
|
-
file_data_for_llm['pages'].append(page_data)
|
|
1658
|
-
|
|
1659
|
-
# Collect frame IDs for vision analysis
|
|
1660
|
-
for frame in page_data.get('frames', []):
|
|
1661
|
-
frame_id = frame.get('id')
|
|
1662
|
-
if frame_id:
|
|
1663
|
-
all_frame_ids.append(frame_id)
|
|
1664
|
-
|
|
1665
|
-
# Fetch frame images for vision-based analysis (detailed mode only)
|
|
1666
|
-
frame_images = {}
|
|
1667
|
-
# Use max_frames parameter to limit LLM analysis (respects user setting)
|
|
1668
|
-
frames_to_analyze = min(max_frames, len(all_frame_ids))
|
|
1669
|
-
if llm_analysis == 'detailed' and all_frame_ids:
|
|
1670
|
-
self._log_tool_event(f"Fetching images for {frames_to_analyze} frames (vision analysis)")
|
|
1671
|
-
try:
|
|
1672
|
-
frame_ids_to_fetch = all_frame_ids[:frames_to_analyze]
|
|
1673
|
-
images_response = self._client.get_file_images(
|
|
1674
|
-
file_key=file_key,
|
|
1675
|
-
ids=frame_ids_to_fetch,
|
|
1676
|
-
scale=1, # Scale 1 is sufficient for analysis
|
|
1677
|
-
format='png'
|
|
1678
|
-
)
|
|
1679
|
-
if images_response and hasattr(images_response, 'images'):
|
|
1680
|
-
frame_images = images_response.images or {}
|
|
1681
|
-
self._log_tool_event(f"Fetched {len(frame_images)} frame images")
|
|
1682
|
-
self._log_tool_event("Processing images and preparing for LLM analysis...")
|
|
1683
|
-
except Exception as img_err:
|
|
1684
|
-
self._log_tool_event(f"Frame image fetch failed (continuing without vision): {img_err}")
|
|
1685
|
-
# Continue without images - will fall back to text analysis
|
|
1686
|
-
|
|
1687
|
-
# Create status callback for progress updates
|
|
1688
|
-
def _status_callback(msg: str):
|
|
1689
|
-
self._log_tool_event(msg)
|
|
1690
|
-
|
|
1691
|
-
# Import here to avoid circular imports
|
|
1692
|
-
from .toon_tools import enrich_toon_with_llm_analysis
|
|
1693
|
-
|
|
1694
|
-
# Check if design insights should be included (default True)
|
|
1695
|
-
include_design_insights = kwargs.get('include_design_insights', True)
|
|
1696
|
-
|
|
1697
|
-
# Get parallel workers from toolkit config (or default)
|
|
1698
|
-
parallel_workers = getattr(self, "number_of_threads", DEFAULT_NUMBER_OF_THREADS)
|
|
1699
|
-
if parallel_workers is None or not isinstance(parallel_workers, int):
|
|
1700
|
-
parallel_workers = DEFAULT_NUMBER_OF_THREADS
|
|
1701
|
-
parallel_workers = max(1, min(parallel_workers, 5))
|
|
1702
|
-
|
|
1703
|
-
self._log_tool_event(f"Starting LLM analysis of {frames_to_analyze} frames with {parallel_workers} parallel workers...")
|
|
1704
|
-
toon_output = enrich_toon_with_llm_analysis(
|
|
1705
|
-
toon_output=toon_output,
|
|
1706
|
-
file_data=file_data_for_llm,
|
|
1707
|
-
llm=self.llm,
|
|
1708
|
-
analysis_level=llm_analysis,
|
|
1709
|
-
frame_images=frame_images,
|
|
1710
|
-
status_callback=_status_callback,
|
|
1711
|
-
include_design_insights=include_design_insights,
|
|
1712
|
-
parallel_workers=parallel_workers,
|
|
1713
|
-
max_frames_to_analyze=frames_to_analyze,
|
|
1714
|
-
)
|
|
1715
|
-
self._log_tool_event("LLM analysis complete")
|
|
1716
|
-
except Exception as e:
|
|
1717
|
-
self._log_tool_event(f"LLM analysis failed: {e}")
|
|
1718
|
-
# Return TOON output without LLM analysis on error
|
|
1719
|
-
toon_output += f"\n\n[LLM analysis failed: {e}]"
|
|
1720
|
-
|
|
1721
|
-
self._log_tool_event(f"File analysis complete (detail_level={detail_level})")
|
|
1722
|
-
return toon_output
|
|
1723
|
-
|
|
1724
712
|
@extend_with_parent_available_tools
|
|
1725
713
|
def get_available_tools(self):
|
|
1726
714
|
return [
|
|
@@ -1736,13 +724,6 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
1736
724
|
"args_schema": ArgsSchema.File.value,
|
|
1737
725
|
"ref": self.get_file,
|
|
1738
726
|
},
|
|
1739
|
-
# TODO disabled until new requirements
|
|
1740
|
-
# {
|
|
1741
|
-
# "name": "get_file_summary",
|
|
1742
|
-
# "description": self.get_file_summary.__doc__,
|
|
1743
|
-
# "args_schema": ArgsSchema.FileSummary.value,
|
|
1744
|
-
# "ref": self.get_file_summary,
|
|
1745
|
-
# },
|
|
1746
727
|
{
|
|
1747
728
|
"name": "get_file_versions",
|
|
1748
729
|
"description": self.get_file_versions.__doc__,
|
|
@@ -1779,19 +760,4 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
|
|
|
1779
760
|
"args_schema": ArgsSchema.ProjectFiles.value,
|
|
1780
761
|
"ref": self.get_project_files,
|
|
1781
762
|
},
|
|
1782
|
-
# TOON Format Tools (Token-Optimized)
|
|
1783
|
-
# Primary unified tool with configurable detail levels
|
|
1784
|
-
{
|
|
1785
|
-
"name": "analyze_file",
|
|
1786
|
-
"description": self.analyze_file.__doc__,
|
|
1787
|
-
"args_schema": AnalyzeFileSchema,
|
|
1788
|
-
"ref": self.analyze_file,
|
|
1789
|
-
},
|
|
1790
|
-
# Targeted drill-down for specific frames (more efficient than level 3 for 2-3 frames)
|
|
1791
|
-
{
|
|
1792
|
-
"name": "get_frame_detail_toon",
|
|
1793
|
-
"description": self.get_frame_detail_toon.__doc__,
|
|
1794
|
-
"args_schema": FrameDetailTOONSchema,
|
|
1795
|
-
"ref": self.get_frame_detail_toon,
|
|
1796
|
-
},
|
|
1797
763
|
]
|