alita-sdk 0.3.562__py3-none-any.whl → 0.3.585__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.

Potentially problematic release.


This version of alita-sdk might be problematic. Click here for more details.

Files changed (74) hide show
  1. alita_sdk/cli/agents.py +358 -165
  2. alita_sdk/configurations/openapi.py +227 -15
  3. alita_sdk/runtime/langchain/langraph_agent.py +93 -20
  4. alita_sdk/runtime/langchain/utils.py +30 -14
  5. alita_sdk/runtime/toolkits/artifact.py +2 -1
  6. alita_sdk/runtime/toolkits/mcp.py +4 -2
  7. alita_sdk/runtime/toolkits/skill_router.py +1 -1
  8. alita_sdk/runtime/toolkits/vectorstore.py +1 -1
  9. alita_sdk/runtime/tools/data_analysis.py +1 -1
  10. alita_sdk/runtime/tools/llm.py +30 -11
  11. alita_sdk/runtime/utils/constants.py +5 -1
  12. alita_sdk/tools/ado/repos/__init__.py +2 -1
  13. alita_sdk/tools/ado/test_plan/__init__.py +2 -1
  14. alita_sdk/tools/ado/wiki/__init__.py +2 -1
  15. alita_sdk/tools/ado/work_item/__init__.py +2 -1
  16. alita_sdk/tools/advanced_jira_mining/__init__.py +2 -1
  17. alita_sdk/tools/aws/delta_lake/__init__.py +2 -1
  18. alita_sdk/tools/azure_ai/search/__init__.py +2 -1
  19. alita_sdk/tools/bitbucket/__init__.py +2 -1
  20. alita_sdk/tools/browser/__init__.py +1 -1
  21. alita_sdk/tools/carrier/__init__.py +1 -1
  22. alita_sdk/tools/cloud/aws/__init__.py +2 -1
  23. alita_sdk/tools/cloud/azure/__init__.py +2 -1
  24. alita_sdk/tools/cloud/gcp/__init__.py +2 -1
  25. alita_sdk/tools/cloud/k8s/__init__.py +2 -1
  26. alita_sdk/tools/code/linter/__init__.py +2 -1
  27. alita_sdk/tools/code/sonar/__init__.py +2 -1
  28. alita_sdk/tools/confluence/__init__.py +2 -1
  29. alita_sdk/tools/custom_open_api/__init__.py +2 -1
  30. alita_sdk/tools/elastic/__init__.py +2 -1
  31. alita_sdk/tools/figma/__init__.py +51 -5
  32. alita_sdk/tools/figma/api_wrapper.py +1157 -123
  33. alita_sdk/tools/figma/figma_client.py +73 -0
  34. alita_sdk/tools/figma/toon_tools.py +2748 -0
  35. alita_sdk/tools/github/__init__.py +2 -1
  36. alita_sdk/tools/gitlab/__init__.py +2 -1
  37. alita_sdk/tools/gitlab/api_wrapper.py +32 -0
  38. alita_sdk/tools/gitlab_org/__init__.py +2 -1
  39. alita_sdk/tools/google/bigquery/__init__.py +2 -1
  40. alita_sdk/tools/google_places/__init__.py +2 -1
  41. alita_sdk/tools/jira/__init__.py +2 -1
  42. alita_sdk/tools/keycloak/__init__.py +2 -1
  43. alita_sdk/tools/localgit/__init__.py +2 -1
  44. alita_sdk/tools/memory/__init__.py +1 -1
  45. alita_sdk/tools/ocr/__init__.py +2 -1
  46. alita_sdk/tools/openapi/__init__.py +227 -15
  47. alita_sdk/tools/openapi/api_wrapper.py +1287 -802
  48. alita_sdk/tools/pandas/__init__.py +3 -2
  49. alita_sdk/tools/postman/__init__.py +2 -1
  50. alita_sdk/tools/pptx/__init__.py +2 -1
  51. alita_sdk/tools/qtest/__init__.py +2 -1
  52. alita_sdk/tools/rally/__init__.py +2 -1
  53. alita_sdk/tools/report_portal/__init__.py +2 -1
  54. alita_sdk/tools/salesforce/__init__.py +2 -1
  55. alita_sdk/tools/servicenow/__init__.py +2 -1
  56. alita_sdk/tools/sharepoint/__init__.py +2 -1
  57. alita_sdk/tools/slack/__init__.py +3 -2
  58. alita_sdk/tools/sql/__init__.py +2 -1
  59. alita_sdk/tools/testio/__init__.py +2 -1
  60. alita_sdk/tools/testrail/__init__.py +2 -1
  61. alita_sdk/tools/utils/content_parser.py +68 -2
  62. alita_sdk/tools/xray/__init__.py +2 -1
  63. alita_sdk/tools/yagmail/__init__.py +2 -1
  64. alita_sdk/tools/zephyr/__init__.py +2 -1
  65. alita_sdk/tools/zephyr_enterprise/__init__.py +2 -1
  66. alita_sdk/tools/zephyr_essential/__init__.py +2 -1
  67. alita_sdk/tools/zephyr_scale/__init__.py +2 -1
  68. alita_sdk/tools/zephyr_squad/__init__.py +2 -1
  69. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/METADATA +1 -1
  70. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/RECORD +74 -72
  71. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/WHEEL +0 -0
  72. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/entry_points.txt +0 -0
  73. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/licenses/LICENSE +0 -0
  74. {alita_sdk-0.3.562.dist-info → alita_sdk-0.3.585.dist-info}/top_level.txt +0 -0
@@ -2,37 +2,107 @@ import functools
2
2
  import json
3
3
  import logging
4
4
  import re
5
+ from concurrent.futures import ThreadPoolExecutor, as_completed
5
6
  from enum import Enum
6
7
  from typing import Dict, List, Generator, Optional, Union
7
8
  from urllib.parse import urlparse, parse_qs
8
9
 
9
10
  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
+
15
52
  from ..non_code_indexer_toolkit import NonCodeIndexerToolkit
16
53
  from ..utils.available_tools_decorator import extend_with_parent_available_tools
17
- from ..utils.content_parser import load_content_from_bytes
54
+ from ..utils.content_parser import _load_content_from_bytes_with_prompt
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
+ )
18
68
 
19
- GLOBAL_LIMIT = 10000
69
+ GLOBAL_LIMIT = 1000000
20
70
  GLOBAL_RETAIN = ['id', 'name', 'type', 'document', 'children']
21
71
  GLOBAL_REMOVE = []
22
- GLOBAL_DEPTH_START = 4
72
+ GLOBAL_DEPTH_START = 1
23
73
  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
+ }
24
98
  EXTRA_PARAMS = (
25
99
  Optional[Dict[str, Union[str, int, List, None]]],
26
100
  Field(
27
101
  description=(
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."
102
+ "Optional output controls: `limit` (max characters, always applied), `regexp` (regex cleanup on text), "
103
+ "`fields_retain`/`fields_remove` (which keys to keep or drop), and `depth_start`/`depth_end` (depth range "
104
+ "where that key filtering is applied). Field/depth filters are only used when the serialized JSON result "
105
+ "exceeds `limit` to reduce its size."
36
106
  ),
37
107
  default={
38
108
  "limit": GLOBAL_LIMIT, "regexp": None,
@@ -179,6 +249,52 @@ class ArgsSchema(Enum):
179
249
  ),
180
250
  extra_params=EXTRA_PARAMS,
181
251
  )
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
+ )
182
298
 
183
299
 
184
300
  class FigmaApiWrapper(NonCodeIndexerToolkit):
@@ -188,39 +304,88 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
188
304
  global_regexp: Optional[str] = Field(default=None)
189
305
  global_fields_retain: Optional[List[str]] = GLOBAL_RETAIN
190
306
  global_fields_remove: Optional[List[str]] = GLOBAL_REMOVE
191
- global_depth_start: Optional[int] = GLOBAL_DEPTH_START
192
- global_depth_end: Optional[int] = GLOBAL_DEPTH_END
193
- _client: Optional[FigmaPy] = PrivateAttr()
307
+ global_depth_start: Optional[int] = Field(default=GLOBAL_DEPTH_START)
308
+ global_depth_end: Optional[int] = Field(default=GLOBAL_DEPTH_END)
309
+ # prompt-related configuration, populated from FigmaToolkit.toolkit_config_schema
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
+ )
194
370
 
195
371
  def _base_loader(
196
372
  self,
197
- file_or_page_url: Optional[str] = None,
198
- project_id: Optional[str] = None,
373
+ url: Optional[str] = None,
199
374
  file_keys_include: Optional[List[str]] = None,
200
375
  file_keys_exclude: Optional[List[str]] = None,
201
376
  node_ids_include: Optional[List[str]] = None,
202
377
  node_ids_exclude: Optional[List[str]] = None,
203
378
  node_types_include: Optional[List[str]] = None,
204
379
  node_types_exclude: Optional[List[str]] = None,
380
+ number_of_threads: Optional[int] = None,
205
381
  **kwargs
206
382
  ) -> Generator[Document, None, None]:
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}")
383
+ if url:
384
+ file_key, node_ids_from_url = self._parse_figma_url(url)
385
+ # Override include params based on URL
386
+ file_keys_include = [file_key]
387
+ if node_ids_from_url and not node_ids_include:
388
+ node_ids_include = node_ids_from_url
224
389
 
225
390
  # If both include and exclude are provided, use only include
226
391
  if file_keys_include:
@@ -230,6 +395,7 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
230
395
  file = self._client.get_file(file_key, geometry='depth=1') # fetch only top-level structure (only pages without inner components)
231
396
  if not file:
232
397
  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
233
399
  metadata = {
234
400
  'id': file_key,
235
401
  'file_key': file_key,
@@ -238,29 +404,15 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
238
404
  'figma_pages_include': node_ids_include or [],
239
405
  'figma_pages_exclude': node_ids_exclude or [],
240
406
  'figma_nodes_include': node_types_include or [],
241
- 'figma_nodes_exclude': node_types_exclude or []
407
+ 'figma_nodes_exclude': node_types_exclude or [],
242
408
  }
409
+ if isinstance(number_of_threads, int) and 1 <= number_of_threads <= 5:
410
+ metadata['number_of_threads_override'] = number_of_threads
243
411
  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
- })
260
412
  elif file_keys_exclude or node_ids_exclude:
261
- raise ValueError("Excludes without parent (project_id or file_keys_include) do not make sense.")
413
+ raise ValueError("Excludes without parent (file_keys_include) do not make sense.")
262
414
  else:
263
- raise ValueError("You must provide at least project_id or file_keys_include.")
415
+ raise ValueError("You must provide file_keys_include or a URL.")
264
416
 
265
417
  def has_image_representation(self, node):
266
418
  node_type = node.get('type', '').lower()
@@ -294,7 +446,11 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
294
446
  # try to fetch only specified pages/nodes in one request
295
447
  file = self._get_file_nodes(file_key,','.join(node_ids_include)) # attempt to fetch only specified pages/nodes in one request
296
448
  if file:
297
- return [node['document'] for node in file.get('nodes', {}).values() if 'document' in node]
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
+ ]
298
454
  else:
299
455
  #
300
456
  file = self._client.get_file(file_key)
@@ -319,7 +475,69 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
319
475
  result.append(page_res)
320
476
  return result
321
477
 
322
- def _process_document(self, document: Document) -> Generator[Document, None, None]:
478
+ def _process_single_image(
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]:
323
541
  file_key = document.metadata.get('id', '')
324
542
  self._log_tool_event(f"Loading details (images) for `{file_key}`")
325
543
  figma_pages = self._load_pages(document)
@@ -343,47 +561,105 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
343
561
  image_nodes.append(node['id'])
344
562
  else:
345
563
  text_nodes[node['id']] = self.get_texts_recursive(node)
346
- # process image nodes
564
+ total_nodes = len(image_nodes) + len(text_nodes)
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 ---
347
580
  if image_nodes:
348
581
  file_images = self._client.get_file_images(file_key, image_nodes)
349
582
  images = self._client.get_file_images(file_key, image_nodes).images or {} if file_images else {}
350
583
  total_images = len(images)
351
584
  if total_images == 0:
352
585
  logging.info(f"No images found for file {file_key}.")
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
- }
586
+ else:
587
+ self._log_tool_event(
588
+ f"File {file_key}: starting download/processing for total {total_nodes} nodes"
589
+ )
590
+
591
+ # Decide how many workers to use (bounded by total_images and configuration).
592
+ max_workers = number_of_threads
593
+ max_workers = max(1, min(max_workers, total_images))
594
+
595
+ if max_workers == 1:
596
+ # Keep original sequential behavior
597
+ for node_id, image_url in images.items():
598
+ doc = self._process_single_image(
599
+ file_key=file_key,
600
+ document=document,
601
+ node_id=node_id,
602
+ image_url=image_url,
603
+ prompt=prompt,
377
604
  )
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
605
+ counted_nodes_ref["value"] += 1
606
+ if doc is not None:
607
+ self._log_tool_event(
608
+ f"File {file_key}: processing image node {node_id} "
609
+ f"({counted_nodes_ref['value']}/{total_nodes} in {max_workers} threads)."
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) ---
384
655
  if text_nodes:
385
656
  for node_id, texts in text_nodes.items():
657
+ counted_nodes_ref["value"] += 1
658
+ current_index = counted_nodes_ref["value"]
386
659
  if texts:
660
+ self._log_tool_event(
661
+ f"File {file_key} : processing text node {node_id} ({current_index}/{total_nodes})."
662
+ )
387
663
  yield Document(
388
664
  page_content="\n".join(texts),
389
665
  metadata={
@@ -391,41 +667,49 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
391
667
  'updated_on': document.metadata.get('updated_on', ''),
392
668
  'file_key': file_key,
393
669
  'node_id': node_id,
394
- 'type': 'text'
395
- }
670
+ 'type': 'text',
671
+ },
396
672
  )
397
673
 
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
-
401
674
  def _index_tool_params(self):
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
- }
675
+ """Return the parameters for indexing data."""
676
+ return {
677
+ "url": (Optional[str], Field(
678
+ description=(
679
+ "Full Figma file or page URL to index. Must be in one of the following formats: "
680
+ "'https://www.figma.com/file/<FILE_KEY>/...' or 'https://www.figma.com/design/<FILE_KEY>/...'. "
681
+ "If present, the 'node-id' query parameter (e.g. '?node-id=<PAGE_ID>') will be used to limit "
682
+ "indexing to that page or node. When this URL is provided, it overrides 'file_keys_include' ('node_ids_include')."
683
+ ),
684
+ default=None)),
685
+ 'number_of_threads': (Optional[int], Field(
686
+ description=(
687
+ "Optional override for the number of worker threads used when indexing Figma images. "
688
+ f"Valid values are from 1 to 5. Default is {DEFAULT_NUMBER_OF_THREADS}."
689
+ ),
690
+ default=DEFAULT_NUMBER_OF_THREADS,
691
+ ge=1,
692
+ le=5,
693
+ )),
694
+ 'file_keys_include': (Optional[List[str]], Field(
695
+ description="List of file keys to include in index if project_id is not provided: i.e. ['Fp24FuzPwH0L74ODSrCnQo', 'jmhAr6q78dJoMRqt48zisY']",
696
+ default=None)),
697
+ 'file_keys_exclude': (Optional[List[str]], Field(
698
+ 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']",
699
+ default=None)),
700
+ 'node_ids_include': (Optional[List[str]], Field(
701
+ 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'].",
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
+ }
429
713
 
430
714
  def _send_request(
431
715
  self,
@@ -476,22 +760,22 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
476
760
  except re.error as e:
477
761
  msg = f"Failed to compile regex pattern: {str(e)}"
478
762
  logging.error(msg)
479
- return ToolException(msg)
763
+ raise ToolException(msg)
480
764
 
481
765
  try:
482
766
  if token:
483
- cls._client = FigmaPy(token=token, oauth2=False)
767
+ cls._client = AlitaFigmaPy(token=token, oauth2=False)
484
768
  logging.info("Authenticated with Figma token")
485
769
  elif oauth2:
486
- cls._client = FigmaPy(token=oauth2, oauth2=True)
770
+ cls._client = AlitaFigmaPy(token=oauth2, oauth2=True)
487
771
  logging.info("Authenticated with OAuth2 token")
488
772
  else:
489
- return ToolException("You have to define Figma token.")
773
+ raise ToolException("You have to define Figma token.")
490
774
  logging.info("Successfully authenticated to Figma.")
491
775
  except Exception as e:
492
776
  msg = f"Failed to authenticate with Figma: {str(e)}"
493
777
  logging.error(msg)
494
- return ToolException(msg)
778
+ raise ToolException(msg)
495
779
 
496
780
  return values
497
781
 
@@ -654,6 +938,132 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
654
938
  """Reads a specified file by field key from Figma."""
655
939
  return self._client.get_file(file_key, geometry, version)
656
940
 
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
+
657
1067
  @process_output
658
1068
  def get_file_versions(self, file_key: str, **kwargs):
659
1069
  """Retrieves the version history of a specified file from Figma."""
@@ -709,6 +1119,608 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
709
1119
  """Retrieves all files for a specified project ID from Figma."""
710
1120
  return self._client.get_project_files(project_id)
711
1121
 
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
+
712
1724
  @extend_with_parent_available_tools
713
1725
  def get_available_tools(self):
714
1726
  return [
@@ -724,6 +1736,13 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
724
1736
  "args_schema": ArgsSchema.File.value,
725
1737
  "ref": self.get_file,
726
1738
  },
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
+ # },
727
1746
  {
728
1747
  "name": "get_file_versions",
729
1748
  "description": self.get_file_versions.__doc__,
@@ -760,4 +1779,19 @@ class FigmaApiWrapper(NonCodeIndexerToolkit):
760
1779
  "args_schema": ArgsSchema.ProjectFiles.value,
761
1780
  "ref": self.get_project_files,
762
1781
  },
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
+ },
763
1797
  ]