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