griptape-nodes 0.52.1__py3-none-any.whl → 0.54.0__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.
Files changed (71) hide show
  1. griptape_nodes/__init__.py +8 -942
  2. griptape_nodes/__main__.py +6 -0
  3. griptape_nodes/app/app.py +48 -86
  4. griptape_nodes/bootstrap/workflow_executors/local_workflow_executor.py +35 -5
  5. griptape_nodes/bootstrap/workflow_executors/workflow_executor.py +15 -1
  6. griptape_nodes/cli/__init__.py +1 -0
  7. griptape_nodes/cli/commands/__init__.py +1 -0
  8. griptape_nodes/cli/commands/config.py +74 -0
  9. griptape_nodes/cli/commands/engine.py +80 -0
  10. griptape_nodes/cli/commands/init.py +550 -0
  11. griptape_nodes/cli/commands/libraries.py +96 -0
  12. griptape_nodes/cli/commands/models.py +504 -0
  13. griptape_nodes/cli/commands/self.py +120 -0
  14. griptape_nodes/cli/main.py +56 -0
  15. griptape_nodes/cli/shared.py +75 -0
  16. griptape_nodes/common/__init__.py +1 -0
  17. griptape_nodes/common/directed_graph.py +71 -0
  18. griptape_nodes/drivers/storage/base_storage_driver.py +40 -20
  19. griptape_nodes/drivers/storage/griptape_cloud_storage_driver.py +24 -29
  20. griptape_nodes/drivers/storage/local_storage_driver.py +23 -14
  21. griptape_nodes/exe_types/core_types.py +60 -2
  22. griptape_nodes/exe_types/node_types.py +257 -38
  23. griptape_nodes/exe_types/param_components/__init__.py +1 -0
  24. griptape_nodes/exe_types/param_components/execution_status_component.py +138 -0
  25. griptape_nodes/machines/control_flow.py +195 -94
  26. griptape_nodes/machines/dag_builder.py +207 -0
  27. griptape_nodes/machines/fsm.py +10 -1
  28. griptape_nodes/machines/parallel_resolution.py +558 -0
  29. griptape_nodes/machines/{node_resolution.py → sequential_resolution.py} +30 -57
  30. griptape_nodes/node_library/library_registry.py +34 -1
  31. griptape_nodes/retained_mode/events/app_events.py +5 -1
  32. griptape_nodes/retained_mode/events/base_events.py +9 -9
  33. griptape_nodes/retained_mode/events/config_events.py +30 -0
  34. griptape_nodes/retained_mode/events/execution_events.py +2 -2
  35. griptape_nodes/retained_mode/events/model_events.py +296 -0
  36. griptape_nodes/retained_mode/events/node_events.py +4 -3
  37. griptape_nodes/retained_mode/griptape_nodes.py +34 -12
  38. griptape_nodes/retained_mode/managers/agent_manager.py +23 -5
  39. griptape_nodes/retained_mode/managers/arbitrary_code_exec_manager.py +3 -1
  40. griptape_nodes/retained_mode/managers/config_manager.py +44 -3
  41. griptape_nodes/retained_mode/managers/context_manager.py +6 -5
  42. griptape_nodes/retained_mode/managers/event_manager.py +8 -2
  43. griptape_nodes/retained_mode/managers/flow_manager.py +150 -206
  44. griptape_nodes/retained_mode/managers/library_lifecycle/library_directory.py +1 -1
  45. griptape_nodes/retained_mode/managers/library_manager.py +35 -25
  46. griptape_nodes/retained_mode/managers/model_manager.py +1107 -0
  47. griptape_nodes/retained_mode/managers/node_manager.py +102 -220
  48. griptape_nodes/retained_mode/managers/object_manager.py +11 -5
  49. griptape_nodes/retained_mode/managers/os_manager.py +28 -13
  50. griptape_nodes/retained_mode/managers/secrets_manager.py +8 -4
  51. griptape_nodes/retained_mode/managers/settings.py +116 -7
  52. griptape_nodes/retained_mode/managers/static_files_manager.py +85 -12
  53. griptape_nodes/retained_mode/managers/sync_manager.py +17 -9
  54. griptape_nodes/retained_mode/managers/workflow_manager.py +186 -192
  55. griptape_nodes/retained_mode/retained_mode.py +19 -0
  56. griptape_nodes/servers/__init__.py +1 -0
  57. griptape_nodes/{mcp_server/server.py → servers/mcp.py} +1 -1
  58. griptape_nodes/{app/api.py → servers/static.py} +43 -40
  59. griptape_nodes/traits/add_param_button.py +1 -1
  60. griptape_nodes/traits/button.py +334 -6
  61. griptape_nodes/traits/color_picker.py +66 -0
  62. griptape_nodes/traits/multi_options.py +188 -0
  63. griptape_nodes/traits/numbers_selector.py +77 -0
  64. griptape_nodes/traits/options.py +93 -2
  65. griptape_nodes/traits/traits.json +4 -0
  66. griptape_nodes/utils/async_utils.py +31 -0
  67. {griptape_nodes-0.52.1.dist-info → griptape_nodes-0.54.0.dist-info}/METADATA +4 -1
  68. {griptape_nodes-0.52.1.dist-info → griptape_nodes-0.54.0.dist-info}/RECORD +71 -48
  69. {griptape_nodes-0.52.1.dist-info → griptape_nodes-0.54.0.dist-info}/WHEEL +1 -1
  70. /griptape_nodes/{mcp_server → servers}/ws_request_manager.py +0 -0
  71. {griptape_nodes-0.52.1.dist-info → griptape_nodes-0.54.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,550 @@
1
+ """Init command for Griptape Nodes CLI."""
2
+
3
+ import asyncio
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Annotated, Any
7
+
8
+ import typer
9
+ from rich.box import HEAVY_EDGE
10
+ from rich.panel import Panel
11
+ from rich.prompt import Confirm, Prompt
12
+ from rich.table import Table
13
+
14
+ from griptape_nodes.cli.commands.libraries import _sync_libraries
15
+ from griptape_nodes.cli.shared import (
16
+ CONFIG_DIR,
17
+ CONFIG_FILE,
18
+ ENV_FILE,
19
+ ENV_LIBRARIES_BASE_DIR,
20
+ GT_CLOUD_BASE_URL,
21
+ NODES_APP_URL,
22
+ InitConfig,
23
+ console,
24
+ )
25
+ from griptape_nodes.drivers.storage import StorageBackend
26
+ from griptape_nodes.drivers.storage.griptape_cloud_storage_driver import GriptapeCloudStorageDriver
27
+ from griptape_nodes.retained_mode.griptape_nodes import GriptapeNodes
28
+
29
+ config_manager = GriptapeNodes.ConfigManager()
30
+ secrets_manager = GriptapeNodes.SecretsManager()
31
+
32
+
33
+ def init_command( # noqa: PLR0913
34
+ api_key: Annotated[str | None, typer.Option(help="Set the Griptape Nodes API key.")] = None,
35
+ workspace_directory: Annotated[str | None, typer.Option(help="Set the Griptape Nodes workspace directory.")] = None,
36
+ storage_backend: Annotated[
37
+ StorageBackend | None,
38
+ typer.Option(help="Set the storage backend ('local' or 'gtc').", case_sensitive=False),
39
+ ] = None,
40
+ bucket_name: Annotated[
41
+ str | None, typer.Option(help="Name for the bucket (existing or new) when using 'gtc' storage backend.")
42
+ ] = None,
43
+ register_advanced_library: Annotated[
44
+ bool | None,
45
+ typer.Option(
46
+ "--register-advanced-library/--no-register-advanced-library",
47
+ help="Install the Griptape Nodes Advanced Image Library.",
48
+ ),
49
+ ] = None,
50
+ libraries_sync: Annotated[
51
+ bool | None,
52
+ typer.Option("--libraries-sync/--no-libraries-sync", help="Sync the Griptape Nodes libraries."),
53
+ ] = None,
54
+ no_interactive: Annotated[ # noqa: FBT002
55
+ bool,
56
+ typer.Option(help="Run init in non-interactive mode (no prompts)."),
57
+ ] = False,
58
+ config: Annotated[
59
+ list[str] | None,
60
+ typer.Option(
61
+ help="Set arbitrary config values as key=value pairs (can be used multiple times). Example: --config log_level=DEBUG --config workspace_directory=/tmp"
62
+ ),
63
+ ] = None,
64
+ secret: Annotated[
65
+ list[str] | None,
66
+ typer.Option(
67
+ help="Set arbitrary secret values as key=value pairs (can be used multiple times). Example: --secret MY_API_KEY=abc123 --secret OTHER_KEY=xyz789"
68
+ ),
69
+ ] = None,
70
+ ) -> None:
71
+ """Initialize engine configuration."""
72
+ config_values = _parse_key_value_pairs(config)
73
+ secret_values = _parse_key_value_pairs(secret)
74
+
75
+ _run_init(
76
+ InitConfig(
77
+ interactive=not no_interactive,
78
+ workspace_directory=workspace_directory,
79
+ api_key=api_key,
80
+ storage_backend=storage_backend,
81
+ register_advanced_library=register_advanced_library,
82
+ config_values=config_values,
83
+ secret_values=secret_values,
84
+ libraries_sync=libraries_sync,
85
+ bucket_name=bucket_name,
86
+ )
87
+ )
88
+
89
+
90
+ def _run_init(config: InitConfig) -> None:
91
+ """Runs through the engine init steps.
92
+
93
+ Args:
94
+ config: Initialization configuration.
95
+ """
96
+ _init_system_config()
97
+
98
+ # Run configuration flow
99
+ _run_init_configuration(config)
100
+
101
+ # Sync libraries
102
+ if config.libraries_sync is not False:
103
+ asyncio.run(_sync_libraries(load_libraries_from_config=False))
104
+
105
+ console.print("[bold green]Initialization complete![/bold green]")
106
+
107
+
108
+ def _init_system_config() -> None:
109
+ """Initializes the system config directory if it doesn't exist."""
110
+ if not CONFIG_DIR.exists():
111
+ CONFIG_DIR.mkdir(parents=True, exist_ok=True)
112
+
113
+ files_to_create = [
114
+ (ENV_FILE, ""),
115
+ (CONFIG_FILE, "{}"),
116
+ ]
117
+
118
+ for file_name in files_to_create:
119
+ file_path = CONFIG_DIR / file_name[0]
120
+ if not file_path.exists():
121
+ with Path.open(file_path, "w", encoding="utf-8") as file:
122
+ file.write(file_name[1])
123
+
124
+
125
+ def _run_init_configuration(config: InitConfig) -> None:
126
+ """Handle initialization with proper dependency ordering."""
127
+ _handle_api_key_config(config)
128
+ _handle_workspace_config(config)
129
+ _handle_storage_backend_config(config)
130
+ _handle_bucket_config(config)
131
+ _handle_advanced_library_config(config)
132
+ _handle_arbitrary_configs(config)
133
+
134
+
135
+ def _handle_api_key_config(config: InitConfig) -> str | None:
136
+ """Handle API key configuration step."""
137
+ api_key = config.api_key
138
+
139
+ if config.interactive:
140
+ api_key = _prompt_for_api_key(default_api_key=api_key)
141
+
142
+ if api_key is not None:
143
+ secrets_manager.set_secret("GT_CLOUD_API_KEY", api_key)
144
+ console.print("[bold green]Griptape API Key set")
145
+
146
+ return api_key
147
+
148
+
149
+ def _handle_workspace_config(config: InitConfig) -> str | None:
150
+ """Handle workspace directory configuration step."""
151
+ workspace_directory = config.workspace_directory
152
+
153
+ if config.interactive:
154
+ workspace_directory = _prompt_for_workspace(default_workspace_directory=workspace_directory)
155
+
156
+ if workspace_directory is not None:
157
+ config_manager.set_config_value("workspace_directory", workspace_directory)
158
+ console.print(f"[bold green]Workspace directory set to: {workspace_directory}[/bold green]")
159
+
160
+ return workspace_directory
161
+
162
+
163
+ def _handle_storage_backend_config(config: InitConfig) -> str | None:
164
+ """Handle storage backend configuration step."""
165
+ storage_backend = config.storage_backend
166
+
167
+ if config.interactive:
168
+ storage_backend = _prompt_for_storage_backend(default_storage_backend=storage_backend)
169
+
170
+ if storage_backend is not None:
171
+ config_manager.set_config_value("storage_backend", storage_backend)
172
+ console.print(f"[bold green]Storage backend set to: {storage_backend}")
173
+
174
+ return storage_backend
175
+
176
+
177
+ def _handle_bucket_config(config: InitConfig) -> str | None:
178
+ """Handle bucket configuration step (depends on API key)."""
179
+ bucket_id = None
180
+
181
+ if config.interactive:
182
+ # First ask if they want to configure a bucket
183
+ configure_bucket = _prompt_for_bucket_configuration()
184
+ if configure_bucket:
185
+ bucket_id = _prompt_for_gtc_bucket_name(default_bucket_name=config.bucket_name)
186
+ elif config.bucket_name is not None:
187
+ bucket_id = _get_or_create_bucket_id(config.bucket_name)
188
+
189
+ if bucket_id is not None:
190
+ secrets_manager.set_secret("GT_CLOUD_BUCKET_ID", bucket_id)
191
+ console.print(f"[bold green]Bucket ID set to: {bucket_id}[/bold green]")
192
+
193
+ return bucket_id
194
+
195
+
196
+ def _handle_advanced_library_config(config: InitConfig) -> bool | None:
197
+ """Handle advanced library configuration step."""
198
+ register_advanced_library = config.register_advanced_library
199
+
200
+ if config.interactive:
201
+ register_advanced_library = _prompt_for_advanced_media_library(
202
+ default_prompt_for_advanced_media_library=register_advanced_library
203
+ )
204
+
205
+ if register_advanced_library is not None:
206
+ libraries_to_register = _build_libraries_list(register_advanced_library=register_advanced_library)
207
+ config_manager.set_config_value(
208
+ "app_events.on_app_initialization_complete.libraries_to_register", libraries_to_register
209
+ )
210
+ console.print(f"[bold green]Libraries to register set to: {', '.join(libraries_to_register)}[/bold green]")
211
+
212
+ return register_advanced_library
213
+
214
+
215
+ def _handle_arbitrary_configs(config: InitConfig) -> None:
216
+ """Handle arbitrary config and secret values."""
217
+ # Set arbitrary config values
218
+ if config.config_values:
219
+ for key, value in config.config_values.items():
220
+ config_manager.set_config_value(key, value)
221
+ console.print(f"[bold green]Config '{key}' set to: {value}[/bold green]")
222
+
223
+ # Set arbitrary secret values
224
+ if config.secret_values:
225
+ for key, value in config.secret_values.items():
226
+ secrets_manager.set_secret(key, value)
227
+ console.print(f"[bold green]Secret '{key}' set[/bold green]")
228
+
229
+
230
+ def _prompt_for_api_key(default_api_key: str | None = None) -> str:
231
+ """Prompts the user for their GT_CLOUD_API_KEY unless it's provided."""
232
+ if default_api_key is None:
233
+ default_api_key = secrets_manager.get_secret("GT_CLOUD_API_KEY", should_error_on_not_found=False)
234
+ explainer = f"""[bold cyan]Griptape API Key[/bold cyan]
235
+ A Griptape API Key is needed to proceed.
236
+ This key allows the Griptape Nodes Engine to communicate with the Griptape Nodes Editor.
237
+ In order to get your key, return to the [link={NODES_APP_URL}]{NODES_APP_URL}[/link] tab in your browser and click the button
238
+ "Generate API Key".
239
+ Once the key is generated, copy and paste its value here to proceed."""
240
+ console.print(Panel(explainer, expand=False))
241
+
242
+ while True:
243
+ api_key = Prompt.ask(
244
+ "Griptape API Key",
245
+ default=default_api_key,
246
+ show_default=True,
247
+ )
248
+ if api_key:
249
+ break
250
+
251
+ return api_key
252
+
253
+
254
+ def _prompt_for_workspace(*, default_workspace_directory: str | None = None) -> str:
255
+ """Prompts the user for their workspace directory."""
256
+ if default_workspace_directory is None:
257
+ default_workspace_directory = config_manager.get_config_value("workspace_directory")
258
+ explainer = """[bold cyan]Workspace Directory[/bold cyan]
259
+ Select the workspace directory. This is the location where Griptape Nodes will store your saved workflows.
260
+ You may enter a custom directory or press Return to accept the default workspace directory"""
261
+ console.print(Panel(explainer, expand=False))
262
+
263
+ while True:
264
+ try:
265
+ workspace_to_test = Prompt.ask(
266
+ "Workspace Directory",
267
+ default=default_workspace_directory,
268
+ show_default=True,
269
+ )
270
+ if workspace_to_test:
271
+ workspace_directory = str(Path(workspace_to_test).expanduser().resolve())
272
+ break
273
+ except OSError as e:
274
+ console.print(f"[bold red]Invalid workspace directory: {e}[/bold red]")
275
+ except json.JSONDecodeError as e:
276
+ console.print(f"[bold red]Error reading config file: {e}[/bold red]")
277
+
278
+ return workspace_directory
279
+
280
+
281
+ def _prompt_for_storage_backend(*, default_storage_backend: str | None = None) -> str:
282
+ """Prompts the user for their storage backend."""
283
+ if default_storage_backend is None:
284
+ default_storage_backend = config_manager.get_config_value("storage_backend")
285
+ explainer = """[bold cyan]Storage Backend[/bold cyan]
286
+ Select the storage backend. This is where Griptape Nodes will store your static files.
287
+ Enter 'gtc' to use Griptape Cloud Bucket Storage, or press Return to accept the default of the local static file server."""
288
+ console.print(Panel(explainer, expand=False))
289
+
290
+ while True:
291
+ try:
292
+ storage_backend = Prompt.ask(
293
+ "Storage Backend",
294
+ choices=list(StorageBackend),
295
+ default=default_storage_backend,
296
+ show_default=True,
297
+ )
298
+ if storage_backend:
299
+ break
300
+ except json.JSONDecodeError as e:
301
+ console.print(f"[bold red]Error reading config file: {e}[/bold red]")
302
+
303
+ return storage_backend
304
+
305
+
306
+ def _get_griptape_cloud_buckets_and_display_table() -> tuple[list[str], dict[str, str], Table]:
307
+ """Fetches the list of Griptape Cloud Buckets from the API.
308
+
309
+ Returns:
310
+ tuple: (bucket_names, name_to_id_mapping, display_table)
311
+ """
312
+ api_key = secrets_manager.get_secret("GT_CLOUD_API_KEY")
313
+ bucket_names: list[str] = []
314
+ name_to_id: dict[str, str] = {}
315
+
316
+ if api_key is None:
317
+ msg = "Griptape Cloud API Key not found."
318
+ raise RuntimeError(msg)
319
+
320
+ table = Table(show_header=True, box=HEAVY_EDGE, show_lines=True, expand=True)
321
+ table.add_column("Bucket Name", style="green")
322
+ table.add_column("Bucket ID", style="green")
323
+
324
+ try:
325
+ buckets = GriptapeCloudStorageDriver.list_buckets(base_url=GT_CLOUD_BASE_URL, api_key=api_key)
326
+ for bucket in buckets:
327
+ bucket_name = bucket["name"]
328
+ bucket_id = bucket["bucket_id"]
329
+ bucket_names.append(bucket_name)
330
+ name_to_id[bucket_name] = bucket_id
331
+ table.add_row(bucket_name, bucket_id)
332
+ except RuntimeError as e:
333
+ console.print(f"[red]Error fetching buckets: {e}[/red]")
334
+
335
+ return bucket_names, name_to_id, table
336
+
337
+
338
+ def _prompt_for_bucket_configuration() -> bool:
339
+ """Prompts the user whether to configure a bucket for multi-machine workflow and asset syncing."""
340
+ # Check if there's already a bucket configured
341
+ current_bucket_id = secrets_manager.get_secret("GT_CLOUD_BUCKET_ID", should_error_on_not_found=False)
342
+
343
+ if current_bucket_id:
344
+ explainer = f"""[bold cyan]Griptape Cloud Bucket Configuration[/bold cyan]
345
+ You currently have a bucket configured (ID: {current_bucket_id}).
346
+
347
+ Buckets are used for multi-machine workflow and asset syncing, allowing you to:
348
+ - Share workflows and assets across multiple devices
349
+ - Sync generated content between different Griptape Nodes instances
350
+ - Access your work from anywhere
351
+
352
+ Would you like to change your selected bucket or keep the current one?"""
353
+ prompt_text = "Change selected Griptape Cloud bucket?"
354
+ default_value = False
355
+ else:
356
+ explainer = """[bold cyan]Griptape Cloud Bucket Configuration[/bold cyan]
357
+ Would you like to configure a Griptape Cloud bucket?
358
+ Buckets are used for multi-machine workflow and asset syncing, allowing you to:
359
+ - Share workflows and assets across multiple devices
360
+ - Sync generated content between different Griptape Nodes instances
361
+ - Access your work from anywhere
362
+
363
+ If you do not intend to use Griptape Nodes to collaborate or revision control your workflows, you can skip this step.
364
+
365
+ You can always configure a bucket later by running the initialization process again."""
366
+ prompt_text = "Configure Griptape Cloud bucket?"
367
+ default_value = False
368
+
369
+ console.print(Panel(explainer, expand=False))
370
+ return Confirm.ask(prompt_text, default=default_value)
371
+
372
+
373
+ def _prompt_for_gtc_bucket_name(default_bucket_name: str | None = None) -> str:
374
+ """Prompts the user for a GTC bucket and returns the bucket ID."""
375
+ explainer = """[bold cyan]Storage Backend Bucket Selection[/bold cyan]
376
+ Select a Griptape Cloud Bucket to use for storage. This is the location where Griptape Nodes will store your static files."""
377
+ console.print(Panel(explainer, expand=False))
378
+
379
+ # Fetch existing buckets
380
+ bucket_names, name_to_id, table = _get_griptape_cloud_buckets_and_display_table()
381
+ if default_bucket_name is None:
382
+ # Default to "default" bucket if it exists
383
+ default_bucket_name = "default" if "default" in name_to_id else None
384
+
385
+ # Display existing buckets if any
386
+ if len(bucket_names) > 0:
387
+ console.print(table)
388
+ console.print("\n[dim]You can enter an existing bucket by name, or enter a new name to create one.[/dim]")
389
+
390
+ while True:
391
+ # Prompt user for bucket name
392
+ selected_bucket_name = Prompt.ask(
393
+ "Enter bucket name",
394
+ default=default_bucket_name,
395
+ show_default=bool(default_bucket_name),
396
+ )
397
+
398
+ if selected_bucket_name:
399
+ # Check if it's an existing bucket
400
+ if selected_bucket_name in name_to_id:
401
+ return name_to_id[selected_bucket_name]
402
+ # It's a new bucket name, confirm creation
403
+ create_bucket = Confirm.ask(
404
+ f"Bucket '{selected_bucket_name}' doesn't exist. Create it?",
405
+ default=True,
406
+ )
407
+ if create_bucket:
408
+ return _create_new_bucket(selected_bucket_name)
409
+ # If they don't want to create, continue the loop to ask again
410
+
411
+
412
+ def _get_or_create_bucket_id(bucket_name: str) -> str:
413
+ """Gets the bucket ID for an existing bucket or creates a new one.
414
+
415
+ Args:
416
+ bucket_name: Name of the bucket to lookup or create
417
+
418
+ Returns:
419
+ The bucket ID
420
+ """
421
+ # Fetch existing buckets to check if bucket_name exists
422
+ _, name_to_id, _ = _get_griptape_cloud_buckets_and_display_table()
423
+
424
+ # Check if bucket already exists
425
+ if bucket_name in name_to_id:
426
+ return name_to_id[bucket_name]
427
+
428
+ # Create the bucket
429
+ return _create_new_bucket(bucket_name)
430
+
431
+
432
+ def _prompt_for_advanced_media_library(*, default_prompt_for_advanced_media_library: bool | None = None) -> bool:
433
+ """Prompts the user whether to register the advanced media library."""
434
+ if default_prompt_for_advanced_media_library is None:
435
+ default_prompt_for_advanced_media_library = False
436
+ explainer = """[bold cyan]Advanced Media Library[/bold cyan]
437
+ Would you like to install the Griptape Nodes Advanced Media Library?
438
+ This node library makes advanced media generation and manipulation nodes available.
439
+ For example, nodes are available for Flux AI image upscaling, or to leverage CUDA for GPU-accelerated image generation.
440
+ CAVEAT: Installing this library requires additional dependencies to download and install, which can take several minutes.
441
+ The Griptape Nodes Advanced Media Library can be added later by following instructions here: [bold blue][link=https://docs.griptapenodes.com]https://docs.griptapenodes.com[/link][/bold blue].
442
+ """
443
+ console.print(Panel(explainer, expand=False))
444
+
445
+ return Confirm.ask("Register Advanced Media Library?", default=default_prompt_for_advanced_media_library)
446
+
447
+
448
+ def _build_libraries_list(*, register_advanced_library: bool) -> list[str]:
449
+ """Builds the list of libraries to register based on the advanced library setting."""
450
+ # TODO: https://github.com/griptape-ai/griptape-nodes/issues/929
451
+ libraries_key = "app_events.on_app_initialization_complete.libraries_to_register"
452
+ library_base_dir = Path(ENV_LIBRARIES_BASE_DIR)
453
+
454
+ current_libraries = config_manager.get_config_value(
455
+ libraries_key,
456
+ config_source="user_config",
457
+ default=config_manager.get_config_value(libraries_key, config_source="default_config", default=[]),
458
+ )
459
+ new_libraries = current_libraries.copy()
460
+
461
+ def _get_library_identifier(library_path: str) -> str:
462
+ """Get the unique identifier for a library based on parent/filename."""
463
+ path = Path(library_path)
464
+ return f"{path.parent.name}/{path.name}"
465
+
466
+ # Create a set of current library identifiers for fast lookup
467
+ current_identifiers = {_get_library_identifier(lib) for lib in current_libraries}
468
+
469
+ default_library = str(library_base_dir / "griptape_nodes_library/griptape_nodes_library.json")
470
+ default_identifier = _get_library_identifier(default_library)
471
+ # If somehow the user removed the default library, add it back
472
+ if default_identifier not in current_identifiers:
473
+ new_libraries.append(default_library)
474
+
475
+ advanced_media_library = str(library_base_dir / "griptape_nodes_advanced_media_library/griptape_nodes_library.json")
476
+ advanced_identifier = _get_library_identifier(advanced_media_library)
477
+ if register_advanced_library:
478
+ # If the advanced media library is not registered, add it
479
+ if advanced_identifier not in current_identifiers:
480
+ new_libraries.append(advanced_media_library)
481
+ else:
482
+ # If the advanced media library is registered, remove it
483
+ libraries_to_remove = [lib for lib in new_libraries if _get_library_identifier(lib) == advanced_identifier]
484
+ for lib in libraries_to_remove:
485
+ new_libraries.remove(lib)
486
+
487
+ return new_libraries
488
+
489
+
490
+ def _create_new_bucket(bucket_name: str) -> str:
491
+ """Create a new Griptape Cloud bucket.
492
+
493
+ Args:
494
+ bucket_name: Name for the bucket
495
+
496
+ Returns:
497
+ The bucket ID of the created bucket.
498
+ """
499
+ api_key = secrets_manager.get_secret("GT_CLOUD_API_KEY")
500
+ if api_key is None:
501
+ msg = "GT_CLOUD_API_KEY secret is required to create a bucket."
502
+ raise ValueError(msg)
503
+
504
+ try:
505
+ bucket_id = GriptapeCloudStorageDriver.create_bucket(
506
+ bucket_name=bucket_name, base_url=GT_CLOUD_BASE_URL, api_key=api_key
507
+ )
508
+ except Exception as e:
509
+ console.print(f"[bold red]Failed to create bucket: {e}[/bold red]")
510
+ raise
511
+ else:
512
+ console.print(f"[bold green]Successfully created bucket '{bucket_name}' with ID: {bucket_id}[/bold green]")
513
+ return bucket_id
514
+
515
+
516
+ def _parse_key_value_pairs(pairs: list[str] | None) -> dict[str, Any] | None:
517
+ """Parse key=value pairs from a list of strings.
518
+
519
+ Args:
520
+ pairs: List of strings in the format "key=value"
521
+
522
+ Returns:
523
+ Dictionary of key-value pairs, or None if no pairs provided
524
+ """
525
+ if not pairs:
526
+ return None
527
+
528
+ result = {}
529
+ for pair in pairs:
530
+ if "=" not in pair:
531
+ console.print(f"[bold red]Invalid key=value pair: {pair}. Expected format: key=value[/bold red]")
532
+ continue
533
+ # Split only on the first = to handle values that contain =
534
+ key, value = pair.split("=", 1)
535
+ key = key.strip()
536
+ value = value.strip()
537
+
538
+ if not key:
539
+ console.print(f"[bold red]Empty key in pair: {pair}[/bold red]")
540
+ continue
541
+
542
+ # Try to parse value as JSON, fall back to string if it fails
543
+ try:
544
+ parsed_value = json.loads(value)
545
+ result[key] = parsed_value
546
+ except (json.JSONDecodeError, ValueError):
547
+ # If JSON parsing fails, use the original string value
548
+ result[key] = value
549
+
550
+ return result if result else None
@@ -0,0 +1,96 @@
1
+ """Libraries command for Griptape Nodes CLI."""
2
+
3
+ import asyncio
4
+ import shutil
5
+ import tarfile
6
+ import tempfile
7
+ from pathlib import Path
8
+
9
+ import httpx
10
+ import typer
11
+ from rich.progress import Progress
12
+
13
+ from griptape_nodes.cli.shared import (
14
+ ENV_LIBRARIES_BASE_DIR,
15
+ LATEST_TAG,
16
+ NODES_TARBALL_URL,
17
+ console,
18
+ )
19
+ from griptape_nodes.retained_mode.griptape_nodes import GriptapeNodes
20
+ from griptape_nodes.utils.version_utils import get_current_version, get_install_source
21
+
22
+ app = typer.Typer(help="Manage local libraries.")
23
+
24
+
25
+ @app.command()
26
+ def sync() -> None:
27
+ """Sync libraries with your current engine version."""
28
+ asyncio.run(_sync_libraries())
29
+
30
+
31
+ async def _sync_libraries(*, load_libraries_from_config: bool = True) -> None:
32
+ """Download and sync Griptape Nodes libraries, copying only directories from synced libraries.
33
+
34
+ Args:
35
+ load_libraries_from_config (bool): If True, re-initialize all libraries from config
36
+
37
+ """
38
+ install_source, _ = get_install_source()
39
+ # Unless we're installed from PyPi, grab libraries from the 'latest' tag
40
+ if install_source == "pypi":
41
+ version = get_current_version()
42
+ else:
43
+ version = LATEST_TAG
44
+
45
+ console.print(f"[bold cyan]Fetching Griptape Nodes libraries ({version})...[/bold cyan]")
46
+
47
+ tar_url = NODES_TARBALL_URL.format(tag=version)
48
+ console.print(f"[green]Downloading from {tar_url}[/green]")
49
+ dest_nodes = Path(ENV_LIBRARIES_BASE_DIR)
50
+
51
+ with tempfile.TemporaryDirectory() as tmp:
52
+ tar_path = Path(tmp) / "nodes.tar.gz"
53
+
54
+ # Streaming download with a tiny progress bar
55
+ with httpx.stream("GET", tar_url, follow_redirects=True) as r, Progress() as progress:
56
+ task = progress.add_task("[green]Downloading...", total=int(r.headers.get("Content-Length", 0)))
57
+ progress.start()
58
+ try:
59
+ r.raise_for_status()
60
+ except httpx.HTTPStatusError as e:
61
+ console.print(f"[red]Error fetching libraries: {e}[/red]")
62
+ return
63
+ with tar_path.open("wb") as f:
64
+ for chunk in r.iter_bytes():
65
+ f.write(chunk)
66
+ progress.update(task, advance=len(chunk))
67
+
68
+ console.print("[green]Extracting...[/green]")
69
+ # Extract and locate extracted directory
70
+ with tarfile.open(tar_path) as tar:
71
+ tar.extractall(tmp, filter="data")
72
+
73
+ extracted_root = next(Path(tmp).glob("griptape-nodes-*"))
74
+ extracted_libs = extracted_root / "libraries"
75
+
76
+ # Copy directories from synced libraries without removing existing content
77
+ console.print(f"[green]Syncing libraries to {dest_nodes.resolve()}...[/green]")
78
+ dest_nodes.mkdir(parents=True, exist_ok=True)
79
+ for library_dir in extracted_libs.iterdir():
80
+ if library_dir.is_dir():
81
+ dest_library_dir = dest_nodes / library_dir.name
82
+ if dest_library_dir.exists():
83
+ shutil.rmtree(dest_library_dir)
84
+ shutil.copytree(library_dir, dest_library_dir)
85
+ console.print(f"[green]Synced library: {library_dir.name}[/green]")
86
+
87
+ # Re-initialize all libraries from config
88
+ if load_libraries_from_config:
89
+ console.print("[bold cyan]Initializing libraries...[/bold cyan]")
90
+ try:
91
+ await GriptapeNodes.LibraryManager().load_all_libraries_from_config()
92
+ console.print("[bold green]Libraries Initialized successfully.[/bold green]")
93
+ except Exception as e:
94
+ console.print(f"[red]Error initializing libraries: {e}[/red]")
95
+
96
+ console.print("[bold green]Libraries synced.[/bold green]")