langflow-base-nightly 1.7.0.dev55__py3-none-any.whl → 1.7.0.dev58__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.
- langflow/api/v2/files.py +6 -6
- langflow/initial_setup/starter_projects/Basic Prompt Chaining.json +31 -1088
- langflow/initial_setup/starter_projects/Basic Prompting.json +196 -135
- langflow/initial_setup/starter_projects/Blog Writer.json +141 -84
- langflow/initial_setup/starter_projects/Custom Component Generator.json +133 -73
- langflow/initial_setup/starter_projects/Document Q&A.json +136 -81
- langflow/initial_setup/starter_projects/Financial Report Parser.json +12 -365
- langflow/initial_setup/starter_projects/Hybrid Search RAG.json +19 -729
- langflow/initial_setup/starter_projects/Image Sentiment Analysis.json +688 -733
- langflow/initial_setup/starter_projects/Instagram Copywriter.json +322 -203
- langflow/initial_setup/starter_projects/Invoice Summarizer.json +47 -21
- langflow/initial_setup/starter_projects/Market Research.json +63 -394
- langflow/initial_setup/starter_projects/Meeting Summary.json +266 -168
- langflow/initial_setup/starter_projects/Memory Chatbot.json +136 -81
- langflow/initial_setup/starter_projects/News Aggregator.json +49 -24
- langflow/initial_setup/starter_projects/Nvidia Remix.json +48 -23
- langflow/initial_setup/starter_projects/Pok/303/251dex Agent.json" +49 -23
- langflow/initial_setup/starter_projects/Portfolio Website Code Generator.json +113 -418
- langflow/initial_setup/starter_projects/Price Deal Finder.json +48 -22
- langflow/initial_setup/starter_projects/Research Agent.json +319 -181
- langflow/initial_setup/starter_projects/Research Translation Loop.json +636 -615
- langflow/initial_setup/starter_projects/SEO Keyword Generator.json +145 -89
- langflow/initial_setup/starter_projects/SaaS Pricing.json +48 -22
- langflow/initial_setup/starter_projects/Search agent.json +47 -21
- langflow/initial_setup/starter_projects/Sequential Tasks Agents.json +147 -54
- langflow/initial_setup/starter_projects/Simple Agent.json +47 -16
- langflow/initial_setup/starter_projects/Social Media Agent.json +47 -16
- langflow/initial_setup/starter_projects/Text Sentiment Analysis.json +398 -251
- langflow/initial_setup/starter_projects/Travel Planning Agents.json +146 -53
- langflow/initial_setup/starter_projects/Twitter Thread Generator.json +137 -81
- langflow/initial_setup/starter_projects/Vector Store RAG.json +133 -82
- langflow/initial_setup/starter_projects/Youtube Analysis.json +182 -106
- langflow/services/storage/local.py +13 -8
- langflow/services/storage/s3.py +0 -6
- {langflow_base_nightly-1.7.0.dev55.dist-info → langflow_base_nightly-1.7.0.dev58.dist-info}/METADATA +2 -2
- {langflow_base_nightly-1.7.0.dev55.dist-info → langflow_base_nightly-1.7.0.dev58.dist-info}/RECORD +38 -38
- {langflow_base_nightly-1.7.0.dev55.dist-info → langflow_base_nightly-1.7.0.dev58.dist-info}/WHEEL +0 -0
- {langflow_base_nightly-1.7.0.dev55.dist-info → langflow_base_nightly-1.7.0.dev58.dist-info}/entry_points.txt +0 -0
|
@@ -1140,27 +1140,20 @@
|
|
|
1140
1140
|
"Message"
|
|
1141
1141
|
],
|
|
1142
1142
|
"beta": false,
|
|
1143
|
-
"category": "agents",
|
|
1144
1143
|
"conditional_paths": [],
|
|
1145
1144
|
"custom_fields": {},
|
|
1146
1145
|
"description": "Define the agent's instructions, then enter a task to complete using tools.",
|
|
1147
1146
|
"display_name": "Agent",
|
|
1148
|
-
"documentation": "",
|
|
1147
|
+
"documentation": "https://docs.langflow.org/agents",
|
|
1149
1148
|
"edited": false,
|
|
1150
1149
|
"field_order": [
|
|
1151
|
-
"
|
|
1152
|
-
"max_tokens",
|
|
1153
|
-
"model_kwargs",
|
|
1154
|
-
"json_mode",
|
|
1155
|
-
"model_name",
|
|
1156
|
-
"openai_api_base",
|
|
1150
|
+
"model",
|
|
1157
1151
|
"api_key",
|
|
1158
|
-
"temperature",
|
|
1159
|
-
"seed",
|
|
1160
|
-
"max_retries",
|
|
1161
|
-
"timeout",
|
|
1162
1152
|
"system_prompt",
|
|
1153
|
+
"context_id",
|
|
1163
1154
|
"n_messages",
|
|
1155
|
+
"format_instructions",
|
|
1156
|
+
"output_schema",
|
|
1164
1157
|
"tools",
|
|
1165
1158
|
"input_value",
|
|
1166
1159
|
"handle_parsing_errors",
|
|
@@ -1171,8 +1164,7 @@
|
|
|
1171
1164
|
],
|
|
1172
1165
|
"frozen": false,
|
|
1173
1166
|
"icon": "bot",
|
|
1174
|
-
"
|
|
1175
|
-
"last_updated": "2025-09-30T16:16:12.101Z",
|
|
1167
|
+
"last_updated": "2025-12-11T21:41:48.407Z",
|
|
1176
1168
|
"legacy": false,
|
|
1177
1169
|
"metadata": {
|
|
1178
1170
|
"code_hash": "1834a4d901fa",
|
|
@@ -1205,8 +1197,6 @@
|
|
|
1205
1197
|
"group_outputs": false,
|
|
1206
1198
|
"method": "message_response",
|
|
1207
1199
|
"name": "response",
|
|
1208
|
-
"options": null,
|
|
1209
|
-
"required_inputs": null,
|
|
1210
1200
|
"selected": "Message",
|
|
1211
1201
|
"tool_mode": true,
|
|
1212
1202
|
"types": [
|
|
@@ -1216,7 +1206,6 @@
|
|
|
1216
1206
|
}
|
|
1217
1207
|
],
|
|
1218
1208
|
"pinned": false,
|
|
1219
|
-
"score": 1.1732828199964098e-19,
|
|
1220
1209
|
"template": {
|
|
1221
1210
|
"_type": "Component",
|
|
1222
1211
|
"add_current_date_tool": {
|
|
@@ -1225,21 +1214,25 @@
|
|
|
1225
1214
|
"display_name": "Current Date",
|
|
1226
1215
|
"dynamic": false,
|
|
1227
1216
|
"info": "If true, will add a tool to the agent that returns the current date.",
|
|
1217
|
+
"input_types": [],
|
|
1228
1218
|
"list": false,
|
|
1229
1219
|
"list_add_label": "Add More",
|
|
1230
1220
|
"name": "add_current_date_tool",
|
|
1221
|
+
"override_skip": false,
|
|
1231
1222
|
"placeholder": "",
|
|
1232
1223
|
"required": false,
|
|
1233
1224
|
"show": true,
|
|
1234
1225
|
"title_case": false,
|
|
1235
1226
|
"tool_mode": false,
|
|
1236
1227
|
"trace_as_metadata": true,
|
|
1228
|
+
"track_in_telemetry": true,
|
|
1237
1229
|
"type": "bool",
|
|
1238
1230
|
"value": true
|
|
1239
1231
|
},
|
|
1240
1232
|
"agent_description": {
|
|
1241
1233
|
"_input_type": "MultilineInput",
|
|
1242
1234
|
"advanced": true,
|
|
1235
|
+
"ai_enabled": false,
|
|
1243
1236
|
"copy_field": false,
|
|
1244
1237
|
"display_name": "Agent Description [Deprecated]",
|
|
1245
1238
|
"dynamic": false,
|
|
@@ -1252,6 +1245,7 @@
|
|
|
1252
1245
|
"load_from_db": false,
|
|
1253
1246
|
"multiline": true,
|
|
1254
1247
|
"name": "agent_description",
|
|
1248
|
+
"override_skip": false,
|
|
1255
1249
|
"placeholder": "",
|
|
1256
1250
|
"required": false,
|
|
1257
1251
|
"show": true,
|
|
@@ -1259,26 +1253,29 @@
|
|
|
1259
1253
|
"tool_mode": false,
|
|
1260
1254
|
"trace_as_input": true,
|
|
1261
1255
|
"trace_as_metadata": true,
|
|
1256
|
+
"track_in_telemetry": false,
|
|
1262
1257
|
"type": "str",
|
|
1263
1258
|
"value": "A helpful assistant with access to the following tools:"
|
|
1264
1259
|
},
|
|
1265
1260
|
"api_key": {
|
|
1266
1261
|
"_input_type": "SecretStrInput",
|
|
1267
|
-
"advanced":
|
|
1262
|
+
"advanced": true,
|
|
1268
1263
|
"display_name": "API Key",
|
|
1269
1264
|
"dynamic": false,
|
|
1270
1265
|
"info": "Model Provider API key",
|
|
1271
1266
|
"input_types": [],
|
|
1272
1267
|
"load_from_db": true,
|
|
1273
1268
|
"name": "api_key",
|
|
1269
|
+
"override_skip": false,
|
|
1274
1270
|
"password": true,
|
|
1275
1271
|
"placeholder": "",
|
|
1276
1272
|
"real_time_refresh": true,
|
|
1277
1273
|
"required": false,
|
|
1278
1274
|
"show": true,
|
|
1279
1275
|
"title_case": false,
|
|
1276
|
+
"track_in_telemetry": false,
|
|
1280
1277
|
"type": "str",
|
|
1281
|
-
"value": "
|
|
1278
|
+
"value": ""
|
|
1282
1279
|
},
|
|
1283
1280
|
"code": {
|
|
1284
1281
|
"advanced": true,
|
|
@@ -1311,6 +1308,7 @@
|
|
|
1311
1308
|
"list_add_label": "Add More",
|
|
1312
1309
|
"load_from_db": false,
|
|
1313
1310
|
"name": "context_id",
|
|
1311
|
+
"override_skip": false,
|
|
1314
1312
|
"placeholder": "",
|
|
1315
1313
|
"required": false,
|
|
1316
1314
|
"show": true,
|
|
@@ -1318,12 +1316,14 @@
|
|
|
1318
1316
|
"tool_mode": false,
|
|
1319
1317
|
"trace_as_input": true,
|
|
1320
1318
|
"trace_as_metadata": true,
|
|
1319
|
+
"track_in_telemetry": false,
|
|
1321
1320
|
"type": "str",
|
|
1322
1321
|
"value": ""
|
|
1323
1322
|
},
|
|
1324
1323
|
"format_instructions": {
|
|
1325
1324
|
"_input_type": "MultilineInput",
|
|
1326
1325
|
"advanced": true,
|
|
1326
|
+
"ai_enabled": false,
|
|
1327
1327
|
"copy_field": false,
|
|
1328
1328
|
"display_name": "Output Format Instructions",
|
|
1329
1329
|
"dynamic": false,
|
|
@@ -1336,6 +1336,7 @@
|
|
|
1336
1336
|
"load_from_db": false,
|
|
1337
1337
|
"multiline": true,
|
|
1338
1338
|
"name": "format_instructions",
|
|
1339
|
+
"override_skip": false,
|
|
1339
1340
|
"placeholder": "",
|
|
1340
1341
|
"required": false,
|
|
1341
1342
|
"show": true,
|
|
@@ -1343,6 +1344,7 @@
|
|
|
1343
1344
|
"tool_mode": false,
|
|
1344
1345
|
"trace_as_input": true,
|
|
1345
1346
|
"trace_as_metadata": true,
|
|
1347
|
+
"track_in_telemetry": false,
|
|
1346
1348
|
"type": "str",
|
|
1347
1349
|
"value": "You are an AI that extracts structured JSON objects from unstructured text. Use a predefined schema with expected types (str, int, float, bool, dict). Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. Fill missing or ambiguous values with defaults: null for missing values. Remove exact duplicates but keep variations that have different field values. Always return valid JSON in the expected format, never throw errors. If multiple objects can be extracted, return them all in the structured format."
|
|
1348
1350
|
},
|
|
@@ -1352,20 +1354,23 @@
|
|
|
1352
1354
|
"display_name": "Handle Parse Errors",
|
|
1353
1355
|
"dynamic": false,
|
|
1354
1356
|
"info": "Should the Agent fix errors when reading user input for better processing?",
|
|
1357
|
+
"input_types": [],
|
|
1355
1358
|
"list": false,
|
|
1356
1359
|
"list_add_label": "Add More",
|
|
1357
1360
|
"name": "handle_parsing_errors",
|
|
1361
|
+
"override_skip": false,
|
|
1358
1362
|
"placeholder": "",
|
|
1359
1363
|
"required": false,
|
|
1360
1364
|
"show": true,
|
|
1361
1365
|
"title_case": false,
|
|
1362
1366
|
"tool_mode": false,
|
|
1363
1367
|
"trace_as_metadata": true,
|
|
1368
|
+
"track_in_telemetry": true,
|
|
1364
1369
|
"type": "bool",
|
|
1365
1370
|
"value": true
|
|
1366
1371
|
},
|
|
1367
1372
|
"input_value": {
|
|
1368
|
-
"_input_type": "
|
|
1373
|
+
"_input_type": "MessageInput",
|
|
1369
1374
|
"advanced": false,
|
|
1370
1375
|
"display_name": "Input",
|
|
1371
1376
|
"dynamic": false,
|
|
@@ -1377,6 +1382,7 @@
|
|
|
1377
1382
|
"list_add_label": "Add More",
|
|
1378
1383
|
"load_from_db": false,
|
|
1379
1384
|
"name": "input_value",
|
|
1385
|
+
"override_skip": false,
|
|
1380
1386
|
"placeholder": "",
|
|
1381
1387
|
"required": false,
|
|
1382
1388
|
"show": true,
|
|
@@ -1384,6 +1390,7 @@
|
|
|
1384
1390
|
"tool_mode": true,
|
|
1385
1391
|
"trace_as_input": true,
|
|
1386
1392
|
"trace_as_metadata": true,
|
|
1393
|
+
"track_in_telemetry": false,
|
|
1387
1394
|
"type": "str",
|
|
1388
1395
|
"value": ""
|
|
1389
1396
|
},
|
|
@@ -1393,15 +1400,18 @@
|
|
|
1393
1400
|
"display_name": "Max Iterations",
|
|
1394
1401
|
"dynamic": false,
|
|
1395
1402
|
"info": "The maximum number of attempts the agent can make to complete its task before it stops.",
|
|
1403
|
+
"input_types": [],
|
|
1396
1404
|
"list": false,
|
|
1397
1405
|
"list_add_label": "Add More",
|
|
1398
1406
|
"name": "max_iterations",
|
|
1407
|
+
"override_skip": false,
|
|
1399
1408
|
"placeholder": "",
|
|
1400
1409
|
"required": false,
|
|
1401
1410
|
"show": true,
|
|
1402
1411
|
"title_case": false,
|
|
1403
1412
|
"tool_mode": false,
|
|
1404
1413
|
"trace_as_metadata": true,
|
|
1414
|
+
"track_in_telemetry": true,
|
|
1405
1415
|
"type": "int",
|
|
1406
1416
|
"value": 15
|
|
1407
1417
|
},
|
|
@@ -1429,6 +1439,7 @@
|
|
|
1429
1439
|
"list_add_label": "Add More",
|
|
1430
1440
|
"model_type": "language",
|
|
1431
1441
|
"name": "model",
|
|
1442
|
+
"options": [],
|
|
1432
1443
|
"override_skip": false,
|
|
1433
1444
|
"placeholder": "Setup Provider",
|
|
1434
1445
|
"real_time_refresh": true,
|
|
@@ -1440,7 +1451,7 @@
|
|
|
1440
1451
|
"trace_as_input": true,
|
|
1441
1452
|
"track_in_telemetry": false,
|
|
1442
1453
|
"type": "model",
|
|
1443
|
-
"value":
|
|
1454
|
+
"value": []
|
|
1444
1455
|
},
|
|
1445
1456
|
"n_messages": {
|
|
1446
1457
|
"_input_type": "IntInput",
|
|
@@ -1448,15 +1459,18 @@
|
|
|
1448
1459
|
"display_name": "Number of Chat History Messages",
|
|
1449
1460
|
"dynamic": false,
|
|
1450
1461
|
"info": "Number of chat history messages to retrieve.",
|
|
1462
|
+
"input_types": [],
|
|
1451
1463
|
"list": false,
|
|
1452
1464
|
"list_add_label": "Add More",
|
|
1453
1465
|
"name": "n_messages",
|
|
1466
|
+
"override_skip": false,
|
|
1454
1467
|
"placeholder": "",
|
|
1455
1468
|
"required": false,
|
|
1456
1469
|
"show": true,
|
|
1457
1470
|
"title_case": false,
|
|
1458
1471
|
"tool_mode": false,
|
|
1459
1472
|
"trace_as_metadata": true,
|
|
1473
|
+
"track_in_telemetry": true,
|
|
1460
1474
|
"type": "int",
|
|
1461
1475
|
"value": 100
|
|
1462
1476
|
},
|
|
@@ -1466,9 +1480,11 @@
|
|
|
1466
1480
|
"display_name": "Output Schema",
|
|
1467
1481
|
"dynamic": false,
|
|
1468
1482
|
"info": "Schema Validation: Define the structure and data types for structured output. No validation if no output schema.",
|
|
1483
|
+
"input_types": [],
|
|
1469
1484
|
"is_list": true,
|
|
1470
1485
|
"list_add_label": "Add More",
|
|
1471
1486
|
"name": "output_schema",
|
|
1487
|
+
"override_skip": false,
|
|
1472
1488
|
"placeholder": "",
|
|
1473
1489
|
"required": false,
|
|
1474
1490
|
"show": true,
|
|
@@ -1517,6 +1533,7 @@
|
|
|
1517
1533
|
"title_case": false,
|
|
1518
1534
|
"tool_mode": false,
|
|
1519
1535
|
"trace_as_metadata": true,
|
|
1536
|
+
"track_in_telemetry": false,
|
|
1520
1537
|
"trigger_icon": "Table",
|
|
1521
1538
|
"trigger_text": "Open table",
|
|
1522
1539
|
"type": "table",
|
|
@@ -1525,6 +1542,7 @@
|
|
|
1525
1542
|
"system_prompt": {
|
|
1526
1543
|
"_input_type": "MultilineInput",
|
|
1527
1544
|
"advanced": false,
|
|
1545
|
+
"ai_enabled": false,
|
|
1528
1546
|
"copy_field": false,
|
|
1529
1547
|
"display_name": "Agent Instructions",
|
|
1530
1548
|
"dynamic": false,
|
|
@@ -1537,6 +1555,7 @@
|
|
|
1537
1555
|
"load_from_db": false,
|
|
1538
1556
|
"multiline": true,
|
|
1539
1557
|
"name": "system_prompt",
|
|
1558
|
+
"override_skip": false,
|
|
1540
1559
|
"placeholder": "",
|
|
1541
1560
|
"required": false,
|
|
1542
1561
|
"show": true,
|
|
@@ -1544,8 +1563,9 @@
|
|
|
1544
1563
|
"tool_mode": false,
|
|
1545
1564
|
"trace_as_input": true,
|
|
1546
1565
|
"trace_as_metadata": true,
|
|
1566
|
+
"track_in_telemetry": false,
|
|
1547
1567
|
"type": "str",
|
|
1548
|
-
"value": "You are a helpful
|
|
1568
|
+
"value": "You are a helpful assistant that can use tools to answer questions and perform tasks."
|
|
1549
1569
|
},
|
|
1550
1570
|
"tools": {
|
|
1551
1571
|
"_input_type": "HandleInput",
|
|
@@ -1559,11 +1579,13 @@
|
|
|
1559
1579
|
"list": true,
|
|
1560
1580
|
"list_add_label": "Add More",
|
|
1561
1581
|
"name": "tools",
|
|
1582
|
+
"override_skip": false,
|
|
1562
1583
|
"placeholder": "",
|
|
1563
1584
|
"required": false,
|
|
1564
1585
|
"show": true,
|
|
1565
1586
|
"title_case": false,
|
|
1566
1587
|
"trace_as_metadata": true,
|
|
1588
|
+
"track_in_telemetry": false,
|
|
1567
1589
|
"type": "other",
|
|
1568
1590
|
"value": ""
|
|
1569
1591
|
},
|
|
@@ -1573,15 +1595,18 @@
|
|
|
1573
1595
|
"display_name": "Verbose",
|
|
1574
1596
|
"dynamic": false,
|
|
1575
1597
|
"info": "",
|
|
1598
|
+
"input_types": [],
|
|
1576
1599
|
"list": false,
|
|
1577
1600
|
"list_add_label": "Add More",
|
|
1578
1601
|
"name": "verbose",
|
|
1602
|
+
"override_skip": false,
|
|
1579
1603
|
"placeholder": "",
|
|
1580
1604
|
"required": false,
|
|
1581
1605
|
"show": true,
|
|
1582
1606
|
"title_case": false,
|
|
1583
1607
|
"tool_mode": false,
|
|
1584
1608
|
"trace_as_metadata": true,
|
|
1609
|
+
"track_in_telemetry": true,
|
|
1585
1610
|
"type": "bool",
|
|
1586
1611
|
"value": true
|
|
1587
1612
|
}
|
|
@@ -1638,7 +1663,7 @@
|
|
|
1638
1663
|
"last_updated": "2025-09-30T16:16:26.172Z",
|
|
1639
1664
|
"legacy": false,
|
|
1640
1665
|
"metadata": {
|
|
1641
|
-
"code_hash": "
|
|
1666
|
+
"code_hash": "3a9077d34836",
|
|
1642
1667
|
"dependencies": {
|
|
1643
1668
|
"dependencies": [
|
|
1644
1669
|
{
|
|
@@ -1843,7 +1868,7 @@
|
|
|
1843
1868
|
"show": true,
|
|
1844
1869
|
"title_case": false,
|
|
1845
1870
|
"type": "code",
|
|
1846
|
-
"value": "import json\nfrom collections.abc import AsyncIterator, Iterator\nfrom pathlib import Path\n\nimport orjson\nimport pandas as pd\nfrom fastapi import UploadFile\nfrom fastapi.encoders import jsonable_encoder\n\nfrom lfx.custom import Component\nfrom lfx.inputs import SortableListInput\nfrom lfx.io import BoolInput, DropdownInput, HandleInput, SecretStrInput, StrInput\nfrom lfx.schema import Data, DataFrame, Message\nfrom lfx.services.deps import get_settings_service, get_storage_service, session_scope\nfrom lfx.template.field.base import Output\nfrom lfx.utils.validate_cloud import is_astra_cloud_environment\n\n\ndef _get_storage_location_options():\n \"\"\"Get storage location options, filtering out Local if in Astra cloud environment.\"\"\"\n all_options = [{\"name\": \"AWS\", \"icon\": \"Amazon\"}, {\"name\": \"Google Drive\", \"icon\": \"google\"}]\n if is_astra_cloud_environment():\n return all_options\n return [{\"name\": \"Local\", \"icon\": \"hard-drive\"}, *all_options]\n\n\nclass SaveToFileComponent(Component):\n display_name = \"Write File\"\n description = \"Save data to local file, AWS S3, or Google Drive in the selected format.\"\n documentation: str = \"https://docs.langflow.org/write-file\"\n icon = \"file-text\"\n name = \"SaveToFile\"\n\n # File format options for different storage types\n LOCAL_DATA_FORMAT_CHOICES = [\"csv\", \"excel\", \"json\", \"markdown\"]\n LOCAL_MESSAGE_FORMAT_CHOICES = [\"txt\", \"json\", \"markdown\"]\n AWS_FORMAT_CHOICES = [\n \"txt\",\n \"json\",\n \"csv\",\n \"xml\",\n \"html\",\n \"md\",\n \"yaml\",\n \"log\",\n \"tsv\",\n \"jsonl\",\n \"parquet\",\n \"xlsx\",\n \"zip\",\n ]\n GDRIVE_FORMAT_CHOICES = [\"txt\", \"json\", \"csv\", \"xlsx\", \"slides\", \"docs\", \"jpg\", \"mp3\"]\n\n inputs = [\n # Storage location selection\n SortableListInput(\n name=\"storage_location\",\n display_name=\"Storage Location\",\n placeholder=\"Select Location\",\n info=\"Choose where to save the file.\",\n options=_get_storage_location_options(),\n real_time_refresh=True,\n limit=1,\n ),\n # Common inputs\n HandleInput(\n name=\"input\",\n display_name=\"File Content\",\n info=\"The input to save.\",\n dynamic=True,\n input_types=[\"Data\", \"DataFrame\", \"Message\"],\n required=True,\n ),\n StrInput(\n name=\"file_name\",\n display_name=\"File Name\",\n info=\"Name file will be saved as (without extension).\",\n required=True,\n show=False,\n tool_mode=True,\n ),\n BoolInput(\n name=\"append_mode\",\n display_name=\"Append\",\n info=(\n \"Append to file if it exists (only for Local storage with plain text formats). \"\n \"Not supported for cloud storage (AWS/Google Drive).\"\n ),\n value=False,\n show=False,\n ),\n # Format inputs (dynamic based on storage location)\n DropdownInput(\n name=\"local_format\",\n display_name=\"File Format\",\n options=list(dict.fromkeys(LOCAL_DATA_FORMAT_CHOICES + LOCAL_MESSAGE_FORMAT_CHOICES)),\n info=\"Select the file format for local storage.\",\n value=\"json\",\n show=False,\n ),\n DropdownInput(\n name=\"aws_format\",\n display_name=\"File Format\",\n options=AWS_FORMAT_CHOICES,\n info=\"Select the file format for AWS S3 storage.\",\n value=\"txt\",\n show=False,\n ),\n DropdownInput(\n name=\"gdrive_format\",\n display_name=\"File Format\",\n options=GDRIVE_FORMAT_CHOICES,\n info=\"Select the file format for Google Drive storage.\",\n value=\"txt\",\n show=False,\n ),\n # AWS S3 specific inputs\n SecretStrInput(\n name=\"aws_access_key_id\",\n display_name=\"AWS Access Key ID\",\n info=\"AWS Access key ID.\",\n show=False,\n advanced=True,\n ),\n SecretStrInput(\n name=\"aws_secret_access_key\",\n display_name=\"AWS Secret Key\",\n info=\"AWS Secret Key.\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"bucket_name\",\n display_name=\"S3 Bucket Name\",\n info=\"Enter the name of the S3 bucket.\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"aws_region\",\n display_name=\"AWS Region\",\n info=\"AWS region (e.g., us-east-1, eu-west-1).\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"s3_prefix\",\n display_name=\"S3 Prefix\",\n info=\"Prefix for all files in S3.\",\n show=False,\n advanced=True,\n ),\n # Google Drive specific inputs\n SecretStrInput(\n name=\"service_account_key\",\n display_name=\"GCP Credentials Secret Key\",\n info=\"Your Google Cloud Platform service account JSON key as a secret string (complete JSON content).\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"folder_id\",\n display_name=\"Google Drive Folder ID\",\n info=(\n \"The Google Drive folder ID where the file will be uploaded. \"\n \"The folder must be shared with the service account email.\"\n ),\n required=True,\n show=False,\n advanced=True,\n ),\n ]\n\n outputs = [Output(display_name=\"File Path\", name=\"message\", method=\"save_to_file\")]\n\n def update_build_config(self, build_config, field_value, field_name=None):\n \"\"\"Update build configuration to show/hide fields based on storage location selection.\"\"\"\n # Update options dynamically based on cloud environment\n # This ensures options are refreshed when build_config is updated\n if \"storage_location\" in build_config:\n updated_options = _get_storage_location_options()\n build_config[\"storage_location\"][\"options\"] = updated_options\n\n if field_name != \"storage_location\":\n return build_config\n\n # Extract selected storage location\n selected = [location[\"name\"] for location in field_value] if isinstance(field_value, list) else []\n\n # Hide all dynamic fields first\n dynamic_fields = [\n \"file_name\", # Common fields (input is always visible)\n \"append_mode\",\n \"local_format\",\n \"aws_format\",\n \"gdrive_format\",\n \"aws_access_key_id\",\n \"aws_secret_access_key\",\n \"bucket_name\",\n \"aws_region\",\n \"s3_prefix\",\n \"service_account_key\",\n \"folder_id\",\n ]\n\n for f_name in dynamic_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = False\n\n # Show fields based on selected storage location\n if len(selected) == 1:\n location = selected[0]\n\n # Show file_name when any storage location is selected\n if \"file_name\" in build_config:\n build_config[\"file_name\"][\"show\"] = True\n\n # Show append_mode only for Local storage (not supported for cloud storage)\n if \"append_mode\" in build_config:\n build_config[\"append_mode\"][\"show\"] = location == \"Local\"\n\n if location == \"Local\":\n if \"local_format\" in build_config:\n build_config[\"local_format\"][\"show\"] = True\n\n elif location == \"AWS\":\n aws_fields = [\n \"aws_format\",\n \"aws_access_key_id\",\n \"aws_secret_access_key\",\n \"bucket_name\",\n \"aws_region\",\n \"s3_prefix\",\n ]\n for f_name in aws_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = True\n\n elif location == \"Google Drive\":\n gdrive_fields = [\"gdrive_format\", \"service_account_key\", \"folder_id\"]\n for f_name in gdrive_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = True\n\n return build_config\n\n async def save_to_file(self) -> Message:\n \"\"\"Save the input to a file and upload it, returning a confirmation message.\"\"\"\n # Validate inputs\n if not self.file_name:\n msg = \"File name must be provided.\"\n raise ValueError(msg)\n if not self._get_input_type():\n msg = \"Input type is not set.\"\n raise ValueError(msg)\n\n # Get selected storage location\n storage_location = self._get_selected_storage_location()\n if not storage_location:\n msg = \"Storage location must be selected.\"\n raise ValueError(msg)\n\n # Check if Local storage is disabled in cloud environment\n if storage_location == \"Local\" and is_astra_cloud_environment():\n msg = \"Local storage is not available in cloud environment. Please use AWS or Google Drive.\"\n raise ValueError(msg)\n\n # Route to appropriate save method based on storage location\n if storage_location == \"Local\":\n return await self._save_to_local()\n if storage_location == \"AWS\":\n return await self._save_to_aws()\n if storage_location == \"Google Drive\":\n return await self._save_to_google_drive()\n msg = f\"Unsupported storage location: {storage_location}\"\n raise ValueError(msg)\n\n def _get_input_type(self) -> str:\n \"\"\"Determine the input type based on the provided input.\"\"\"\n # Use exact type checking (type() is) instead of isinstance() to avoid inheritance issues.\n # Since Message inherits from Data, isinstance(message, Data) would return True for Message objects,\n # causing Message inputs to be incorrectly identified as Data type.\n if type(self.input) is DataFrame:\n return \"DataFrame\"\n if type(self.input) is Message:\n return \"Message\"\n if type(self.input) is Data:\n return \"Data\"\n msg = f\"Unsupported input type: {type(self.input)}\"\n raise ValueError(msg)\n\n def _get_default_format(self) -> str:\n \"\"\"Return the default file format based on input type.\"\"\"\n if self._get_input_type() == \"DataFrame\":\n return \"csv\"\n if self._get_input_type() == \"Data\":\n return \"json\"\n if self._get_input_type() == \"Message\":\n return \"json\"\n return \"json\" # Fallback\n\n def _adjust_file_path_with_format(self, path: Path, fmt: str) -> Path:\n \"\"\"Adjust the file path to include the correct extension.\"\"\"\n file_extension = path.suffix.lower().lstrip(\".\")\n if fmt == \"excel\":\n return Path(f\"{path}.xlsx\").expanduser() if file_extension not in [\"xlsx\", \"xls\"] else path\n return Path(f\"{path}.{fmt}\").expanduser() if file_extension != fmt else path\n\n def _is_plain_text_format(self, fmt: str) -> bool:\n \"\"\"Check if a file format is plain text (supports appending).\"\"\"\n plain_text_formats = [\"txt\", \"json\", \"markdown\", \"md\", \"csv\", \"xml\", \"html\", \"yaml\", \"log\", \"tsv\", \"jsonl\"]\n return fmt.lower() in plain_text_formats\n\n async def _upload_file(self, file_path: Path) -> None:\n \"\"\"Upload the saved file using the upload_user_file service.\"\"\"\n from langflow.api.v2.files import upload_user_file\n from langflow.services.database.models.user.crud import get_user_by_id\n\n # Ensure the file exists\n if not file_path.exists():\n msg = f\"File not found: {file_path}\"\n raise FileNotFoundError(msg)\n\n # Upload the file - always use append=False because the local file already contains\n # the correct content (either new or appended locally)\n with file_path.open(\"rb\") as f:\n async with session_scope() as db:\n if not self.user_id:\n msg = \"User ID is required for file saving.\"\n raise ValueError(msg)\n current_user = await get_user_by_id(db, self.user_id)\n\n await upload_user_file(\n file=UploadFile(filename=file_path.name, file=f, size=file_path.stat().st_size),\n session=db,\n current_user=current_user,\n storage_service=get_storage_service(),\n settings_service=get_settings_service(),\n append=False,\n )\n\n def _save_dataframe(self, dataframe: DataFrame, path: Path, fmt: str) -> str:\n \"\"\"Save a DataFrame to the specified file format.\"\"\"\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"csv\":\n dataframe.to_csv(path, index=False, mode=\"a\" if should_append else \"w\", header=not should_append)\n elif fmt == \"excel\":\n dataframe.to_excel(path, index=False, engine=\"openpyxl\")\n elif fmt == \"json\":\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new data\n new_records = json.loads(dataframe.to_json(orient=\"records\"))\n existing_data.extend(new_records)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n dataframe.to_json(path, orient=\"records\", indent=2)\n elif fmt == \"markdown\":\n content = dataframe.to_markdown(index=False)\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported DataFrame format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"DataFrame {action} '{path}'\"\n\n def _save_data(self, data: Data, path: Path, fmt: str) -> str:\n \"\"\"Save a Data object to the specified file format.\"\"\"\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"csv\":\n pd.DataFrame(data.data).to_csv(\n path,\n index=False,\n mode=\"a\" if should_append else \"w\",\n header=not should_append,\n )\n elif fmt == \"excel\":\n pd.DataFrame(data.data).to_excel(path, index=False, engine=\"openpyxl\")\n elif fmt == \"json\":\n new_data = jsonable_encoder(data.data)\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new data\n if isinstance(new_data, list):\n existing_data.extend(new_data)\n else:\n existing_data.append(new_data)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n content = orjson.dumps(new_data, option=orjson.OPT_INDENT_2).decode(\"utf-8\")\n path.write_text(content, encoding=\"utf-8\")\n elif fmt == \"markdown\":\n content = pd.DataFrame(data.data).to_markdown(index=False)\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported Data format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"Data {action} '{path}'\"\n\n async def _save_message(self, message: Message, path: Path, fmt: str) -> str:\n \"\"\"Save a Message to the specified file format, handling async iterators.\"\"\"\n content = \"\"\n if message.text is None:\n content = \"\"\n elif isinstance(message.text, AsyncIterator):\n async for item in message.text:\n content += str(item) + \" \"\n content = content.strip()\n elif isinstance(message.text, Iterator):\n content = \" \".join(str(item) for item in message.text)\n else:\n content = str(message.text)\n\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"txt\":\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n elif fmt == \"json\":\n new_message = {\"message\": content}\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new message\n existing_data.append(new_message)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n path.write_text(json.dumps(new_message, indent=2), encoding=\"utf-8\")\n elif fmt == \"markdown\":\n md_content = f\"**Message:**\\n\\n{content}\"\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + md_content, encoding=\"utf-8\")\n else:\n path.write_text(md_content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported Message format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"Message {action} '{path}'\"\n\n def _get_selected_storage_location(self) -> str:\n \"\"\"Get the selected storage location from the SortableListInput.\"\"\"\n if hasattr(self, \"storage_location\") and self.storage_location:\n if isinstance(self.storage_location, list) and len(self.storage_location) > 0:\n return self.storage_location[0].get(\"name\", \"\")\n if isinstance(self.storage_location, dict):\n return self.storage_location.get(\"name\", \"\")\n return \"\"\n\n def _get_file_format_for_location(self, location: str) -> str:\n \"\"\"Get the appropriate file format based on storage location.\"\"\"\n if location == \"Local\":\n return getattr(self, \"local_format\", None) or self._get_default_format()\n if location == \"AWS\":\n return getattr(self, \"aws_format\", \"txt\")\n if location == \"Google Drive\":\n return getattr(self, \"gdrive_format\", \"txt\")\n return self._get_default_format()\n\n async def _save_to_local(self) -> Message:\n \"\"\"Save file to local storage (original functionality).\"\"\"\n file_format = self._get_file_format_for_location(\"Local\")\n\n # Validate file format based on input type\n allowed_formats = (\n self.LOCAL_MESSAGE_FORMAT_CHOICES if self._get_input_type() == \"Message\" else self.LOCAL_DATA_FORMAT_CHOICES\n )\n if file_format not in allowed_formats:\n msg = f\"Invalid file format '{file_format}' for {self._get_input_type()}. Allowed: {allowed_formats}\"\n raise ValueError(msg)\n\n # Prepare file path\n file_path = Path(self.file_name).expanduser()\n if not file_path.parent.exists():\n file_path.parent.mkdir(parents=True, exist_ok=True)\n file_path = self._adjust_file_path_with_format(file_path, file_format)\n\n # Save the input to file based on type\n if self._get_input_type() == \"DataFrame\":\n confirmation = self._save_dataframe(self.input, file_path, file_format)\n elif self._get_input_type() == \"Data\":\n confirmation = self._save_data(self.input, file_path, file_format)\n elif self._get_input_type() == \"Message\":\n confirmation = await self._save_message(self.input, file_path, file_format)\n else:\n msg = f\"Unsupported input type: {self._get_input_type()}\"\n raise ValueError(msg)\n\n # Upload the saved file\n await self._upload_file(file_path)\n\n # Return the final file path and confirmation message\n final_path = Path.cwd() / file_path if not file_path.is_absolute() else file_path\n return Message(text=f\"{confirmation} at {final_path}\")\n\n async def _save_to_aws(self) -> Message:\n \"\"\"Save file to AWS S3 using S3 functionality.\"\"\"\n # Validate AWS credentials\n if not getattr(self, \"aws_access_key_id\", None):\n msg = \"AWS Access Key ID is required for S3 storage\"\n raise ValueError(msg)\n if not getattr(self, \"aws_secret_access_key\", None):\n msg = \"AWS Secret Key is required for S3 storage\"\n raise ValueError(msg)\n if not getattr(self, \"bucket_name\", None):\n msg = \"S3 Bucket Name is required for S3 storage\"\n raise ValueError(msg)\n\n # Use S3 upload functionality\n try:\n import boto3\n except ImportError as e:\n msg = \"boto3 is not installed. Please install it using `uv pip install boto3`.\"\n raise ImportError(msg) from e\n\n # Create S3 client\n client_config = {\n \"aws_access_key_id\": self.aws_access_key_id,\n \"aws_secret_access_key\": self.aws_secret_access_key,\n }\n\n if hasattr(self, \"aws_region\") and self.aws_region:\n client_config[\"region_name\"] = self.aws_region\n\n s3_client = boto3.client(\"s3\", **client_config)\n\n # Extract content\n content = self._extract_content_for_upload()\n file_format = self._get_file_format_for_location(\"AWS\")\n\n # Generate file path\n file_path = f\"{self.file_name}.{file_format}\"\n if hasattr(self, \"s3_prefix\") and self.s3_prefix:\n file_path = f\"{self.s3_prefix.rstrip('/')}/{file_path}\"\n\n # Create temporary file\n import tempfile\n\n with tempfile.NamedTemporaryFile(\n mode=\"w\", encoding=\"utf-8\", suffix=f\".{file_format}\", delete=False\n ) as temp_file:\n temp_file.write(content)\n temp_file_path = temp_file.name\n\n try:\n # Upload to S3\n s3_client.upload_file(temp_file_path, self.bucket_name, file_path)\n s3_url = f\"s3://{self.bucket_name}/{file_path}\"\n return Message(text=f\"File successfully uploaded to {s3_url}\")\n finally:\n # Clean up temp file\n if Path(temp_file_path).exists():\n Path(temp_file_path).unlink()\n\n async def _save_to_google_drive(self) -> Message:\n \"\"\"Save file to Google Drive using Google Drive functionality.\"\"\"\n # Validate Google Drive credentials\n if not getattr(self, \"service_account_key\", None):\n msg = \"GCP Credentials Secret Key is required for Google Drive storage\"\n raise ValueError(msg)\n if not getattr(self, \"folder_id\", None):\n msg = \"Google Drive Folder ID is required for Google Drive storage\"\n raise ValueError(msg)\n\n # Use Google Drive upload functionality\n try:\n import json\n import tempfile\n\n from google.oauth2 import service_account\n from googleapiclient.discovery import build\n from googleapiclient.http import MediaFileUpload\n except ImportError as e:\n msg = \"Google API client libraries are not installed. Please install them.\"\n raise ImportError(msg) from e\n\n # Parse credentials with multiple fallback strategies\n credentials_dict = None\n parse_errors = []\n\n # Strategy 1: Parse as-is with strict=False to allow control characters\n try:\n credentials_dict = json.loads(self.service_account_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Standard parse: {e!s}\")\n\n # Strategy 2: Strip whitespace and try again\n if credentials_dict is None:\n try:\n cleaned_key = self.service_account_key.strip()\n credentials_dict = json.loads(cleaned_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Stripped parse: {e!s}\")\n\n # Strategy 3: Check if it's double-encoded (JSON string of a JSON string)\n if credentials_dict is None:\n try:\n decoded_once = json.loads(self.service_account_key, strict=False)\n if isinstance(decoded_once, str):\n credentials_dict = json.loads(decoded_once, strict=False)\n else:\n credentials_dict = decoded_once\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Double-encoded parse: {e!s}\")\n\n # Strategy 4: Try to fix common issues with newlines in the private_key field\n if credentials_dict is None:\n try:\n # Replace literal \\n with actual newlines which is common in pasted JSON\n fixed_key = self.service_account_key.replace(\"\\\\n\", \"\\n\")\n credentials_dict = json.loads(fixed_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Newline-fixed parse: {e!s}\")\n\n if credentials_dict is None:\n error_details = \"; \".join(parse_errors)\n msg = (\n f\"Unable to parse service account key JSON. Tried multiple strategies: {error_details}. \"\n \"Please ensure you've copied the entire JSON content from your service account key file. \"\n \"The JSON should start with '{' and contain fields like 'type', 'project_id', 'private_key', etc.\"\n )\n raise ValueError(msg)\n\n # Create Google Drive service with appropriate scopes\n # Use drive scope for folder access, file scope is too restrictive for folder verification\n credentials = service_account.Credentials.from_service_account_info(\n credentials_dict, scopes=[\"https://www.googleapis.com/auth/drive\"]\n )\n drive_service = build(\"drive\", \"v3\", credentials=credentials)\n\n # Extract content and format\n content = self._extract_content_for_upload()\n file_format = self._get_file_format_for_location(\"Google Drive\")\n\n # Handle special Google Drive formats\n if file_format in [\"slides\", \"docs\"]:\n return await self._save_to_google_apps(drive_service, credentials, content, file_format)\n\n # Create temporary file\n file_path = f\"{self.file_name}.{file_format}\"\n with tempfile.NamedTemporaryFile(\n mode=\"w\",\n encoding=\"utf-8\",\n suffix=f\".{file_format}\",\n delete=False,\n ) as temp_file:\n temp_file.write(content)\n temp_file_path = temp_file.name\n\n try:\n # Upload to Google Drive\n # Note: We skip explicit folder verification since it requires broader permissions.\n # If the folder doesn't exist or isn't accessible, the create() call will fail with a clear error.\n file_metadata = {\"name\": file_path, \"parents\": [self.folder_id]}\n media = MediaFileUpload(temp_file_path, resumable=True)\n\n try:\n uploaded_file = (\n drive_service.files().create(body=file_metadata, media_body=media, fields=\"id\").execute()\n )\n except Exception as e:\n msg = (\n f\"Unable to upload file to Google Drive folder '{self.folder_id}'. \"\n f\"Error: {e!s}. \"\n \"Please ensure: 1) The folder ID is correct, 2) The folder exists, \"\n \"3) The service account has been granted access to this folder.\"\n )\n raise ValueError(msg) from e\n\n file_id = uploaded_file.get(\"id\")\n file_url = f\"https://drive.google.com/file/d/{file_id}/view\"\n return Message(text=f\"File successfully uploaded to Google Drive: {file_url}\")\n finally:\n # Clean up temp file\n if Path(temp_file_path).exists():\n Path(temp_file_path).unlink()\n\n async def _save_to_google_apps(self, drive_service, credentials, content: str, app_type: str) -> Message:\n \"\"\"Save content to Google Apps (Slides or Docs).\"\"\"\n import time\n\n if app_type == \"slides\":\n from googleapiclient.discovery import build\n\n slides_service = build(\"slides\", \"v1\", credentials=credentials)\n\n file_metadata = {\n \"name\": self.file_name,\n \"mimeType\": \"application/vnd.google-apps.presentation\",\n \"parents\": [self.folder_id],\n }\n\n created_file = drive_service.files().create(body=file_metadata, fields=\"id\").execute()\n presentation_id = created_file[\"id\"]\n\n time.sleep(2) # Wait for file to be available # noqa: ASYNC251\n\n presentation = slides_service.presentations().get(presentationId=presentation_id).execute()\n slide_id = presentation[\"slides\"][0][\"objectId\"]\n\n # Add content to slide\n requests = [\n {\n \"createShape\": {\n \"objectId\": \"TextBox_01\",\n \"shapeType\": \"TEXT_BOX\",\n \"elementProperties\": {\n \"pageObjectId\": slide_id,\n \"size\": {\n \"height\": {\"magnitude\": 3000000, \"unit\": \"EMU\"},\n \"width\": {\"magnitude\": 6000000, \"unit\": \"EMU\"},\n },\n \"transform\": {\n \"scaleX\": 1,\n \"scaleY\": 1,\n \"translateX\": 1000000,\n \"translateY\": 1000000,\n \"unit\": \"EMU\",\n },\n },\n }\n },\n {\"insertText\": {\"objectId\": \"TextBox_01\", \"insertionIndex\": 0, \"text\": content}},\n ]\n\n slides_service.presentations().batchUpdate(\n presentationId=presentation_id, body={\"requests\": requests}\n ).execute()\n file_url = f\"https://docs.google.com/presentation/d/{presentation_id}/edit\"\n\n elif app_type == \"docs\":\n from googleapiclient.discovery import build\n\n docs_service = build(\"docs\", \"v1\", credentials=credentials)\n\n file_metadata = {\n \"name\": self.file_name,\n \"mimeType\": \"application/vnd.google-apps.document\",\n \"parents\": [self.folder_id],\n }\n\n created_file = drive_service.files().create(body=file_metadata, fields=\"id\").execute()\n document_id = created_file[\"id\"]\n\n time.sleep(2) # Wait for file to be available # noqa: ASYNC251\n\n # Add content to document\n requests = [{\"insertText\": {\"location\": {\"index\": 1}, \"text\": content}}]\n docs_service.documents().batchUpdate(documentId=document_id, body={\"requests\": requests}).execute()\n file_url = f\"https://docs.google.com/document/d/{document_id}/edit\"\n\n return Message(text=f\"File successfully created in Google {app_type.title()}: {file_url}\")\n\n def _extract_content_for_upload(self) -> str:\n \"\"\"Extract content from input for upload to cloud services.\"\"\"\n if self._get_input_type() == \"DataFrame\":\n return self.input.to_csv(index=False)\n if self._get_input_type() == \"Data\":\n if hasattr(self.input, \"data\") and self.input.data:\n if isinstance(self.input.data, dict):\n import json\n\n return json.dumps(self.input.data, indent=2, ensure_ascii=False)\n return str(self.input.data)\n return str(self.input)\n if self._get_input_type() == \"Message\":\n return str(self.input.text) if self.input.text else str(self.input)\n return str(self.input)\n"
|
|
1871
|
+
"value": "import json\nfrom collections.abc import AsyncIterator, Iterator\nfrom pathlib import Path\nfrom typing import Any\n\nimport orjson\nimport pandas as pd\nfrom fastapi import UploadFile\nfrom fastapi.encoders import jsonable_encoder\n\nfrom lfx.custom import Component\nfrom lfx.inputs import SortableListInput\nfrom lfx.io import BoolInput, DropdownInput, HandleInput, SecretStrInput, StrInput\nfrom lfx.schema import Data, DataFrame, Message\nfrom lfx.services.deps import get_settings_service, get_storage_service, session_scope\nfrom lfx.template.field.base import Output\nfrom lfx.utils.validate_cloud import is_astra_cloud_environment\n\n\ndef _get_storage_location_options():\n \"\"\"Get storage location options, filtering out Local if in Astra cloud environment.\"\"\"\n all_options = [{\"name\": \"AWS\", \"icon\": \"Amazon\"}, {\"name\": \"Google Drive\", \"icon\": \"google\"}]\n if is_astra_cloud_environment():\n return all_options\n return [{\"name\": \"Local\", \"icon\": \"hard-drive\"}, *all_options]\n\n\nclass SaveToFileComponent(Component):\n display_name = \"Write File\"\n description = \"Save data to local file, AWS S3, or Google Drive in the selected format.\"\n documentation: str = \"https://docs.langflow.org/write-file\"\n icon = \"file-text\"\n name = \"SaveToFile\"\n\n # File format options for different storage types\n LOCAL_DATA_FORMAT_CHOICES = [\"csv\", \"excel\", \"json\", \"markdown\"]\n LOCAL_MESSAGE_FORMAT_CHOICES = [\"txt\", \"json\", \"markdown\"]\n AWS_FORMAT_CHOICES = [\n \"txt\",\n \"json\",\n \"csv\",\n \"xml\",\n \"html\",\n \"md\",\n \"yaml\",\n \"log\",\n \"tsv\",\n \"jsonl\",\n \"parquet\",\n \"xlsx\",\n \"zip\",\n ]\n GDRIVE_FORMAT_CHOICES = [\"txt\", \"json\", \"csv\", \"xlsx\", \"slides\", \"docs\", \"jpg\", \"mp3\"]\n\n inputs = [\n # Storage location selection\n SortableListInput(\n name=\"storage_location\",\n display_name=\"Storage Location\",\n placeholder=\"Select Location\",\n info=\"Choose where to save the file.\",\n options=_get_storage_location_options(),\n real_time_refresh=True,\n limit=1,\n ),\n # Common inputs\n HandleInput(\n name=\"input\",\n display_name=\"File Content\",\n info=\"The input to save.\",\n dynamic=True,\n input_types=[\"Data\", \"DataFrame\", \"Message\"],\n required=True,\n ),\n StrInput(\n name=\"file_name\",\n display_name=\"File Name\",\n info=\"Name file will be saved as (without extension).\",\n required=True,\n show=False,\n tool_mode=True,\n ),\n BoolInput(\n name=\"append_mode\",\n display_name=\"Append\",\n info=(\n \"Append to file if it exists (only for Local storage with plain text formats). \"\n \"Not supported for cloud storage (AWS/Google Drive).\"\n ),\n value=False,\n show=False,\n ),\n # Format inputs (dynamic based on storage location)\n DropdownInput(\n name=\"local_format\",\n display_name=\"File Format\",\n options=list(dict.fromkeys(LOCAL_DATA_FORMAT_CHOICES + LOCAL_MESSAGE_FORMAT_CHOICES)),\n info=\"Select the file format for local storage.\",\n value=\"json\",\n show=False,\n ),\n DropdownInput(\n name=\"aws_format\",\n display_name=\"File Format\",\n options=AWS_FORMAT_CHOICES,\n info=\"Select the file format for AWS S3 storage.\",\n value=\"txt\",\n show=False,\n ),\n DropdownInput(\n name=\"gdrive_format\",\n display_name=\"File Format\",\n options=GDRIVE_FORMAT_CHOICES,\n info=\"Select the file format for Google Drive storage.\",\n value=\"txt\",\n show=False,\n ),\n # AWS S3 specific inputs\n SecretStrInput(\n name=\"aws_access_key_id\",\n display_name=\"AWS Access Key ID\",\n info=\"AWS Access key ID.\",\n show=False,\n advanced=True,\n ),\n SecretStrInput(\n name=\"aws_secret_access_key\",\n display_name=\"AWS Secret Key\",\n info=\"AWS Secret Key.\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"bucket_name\",\n display_name=\"S3 Bucket Name\",\n info=\"Enter the name of the S3 bucket.\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"aws_region\",\n display_name=\"AWS Region\",\n info=\"AWS region (e.g., us-east-1, eu-west-1).\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"s3_prefix\",\n display_name=\"S3 Prefix\",\n info=\"Prefix for all files in S3.\",\n show=False,\n advanced=True,\n ),\n # Google Drive specific inputs\n SecretStrInput(\n name=\"service_account_key\",\n display_name=\"GCP Credentials Secret Key\",\n info=\"Your Google Cloud Platform service account JSON key as a secret string (complete JSON content).\",\n show=False,\n advanced=True,\n ),\n StrInput(\n name=\"folder_id\",\n display_name=\"Google Drive Folder ID\",\n info=(\n \"The Google Drive folder ID where the file will be uploaded. \"\n \"The folder must be shared with the service account email.\"\n ),\n required=True,\n show=False,\n advanced=True,\n ),\n ]\n\n outputs = [Output(display_name=\"File Path\", name=\"message\", method=\"save_to_file\")]\n\n def update_build_config(self, build_config, field_value, field_name=None):\n \"\"\"Update build configuration to show/hide fields based on storage location selection.\"\"\"\n # Update options dynamically based on cloud environment\n # This ensures options are refreshed when build_config is updated\n if \"storage_location\" in build_config:\n updated_options = _get_storage_location_options()\n build_config[\"storage_location\"][\"options\"] = updated_options\n\n if field_name != \"storage_location\":\n return build_config\n\n # Extract selected storage location\n selected = [location[\"name\"] for location in field_value] if isinstance(field_value, list) else []\n\n # Hide all dynamic fields first\n dynamic_fields = [\n \"file_name\", # Common fields (input is always visible)\n \"append_mode\",\n \"local_format\",\n \"aws_format\",\n \"gdrive_format\",\n \"aws_access_key_id\",\n \"aws_secret_access_key\",\n \"bucket_name\",\n \"aws_region\",\n \"s3_prefix\",\n \"service_account_key\",\n \"folder_id\",\n ]\n\n for f_name in dynamic_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = False\n\n # Show fields based on selected storage location\n if len(selected) == 1:\n location = selected[0]\n\n # Show file_name when any storage location is selected\n if \"file_name\" in build_config:\n build_config[\"file_name\"][\"show\"] = True\n\n # Show append_mode only for Local storage (not supported for cloud storage)\n if \"append_mode\" in build_config:\n build_config[\"append_mode\"][\"show\"] = location == \"Local\"\n\n if location == \"Local\":\n if \"local_format\" in build_config:\n build_config[\"local_format\"][\"show\"] = True\n\n elif location == \"AWS\":\n aws_fields = [\n \"aws_format\",\n \"aws_access_key_id\",\n \"aws_secret_access_key\",\n \"bucket_name\",\n \"aws_region\",\n \"s3_prefix\",\n ]\n for f_name in aws_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = True\n\n elif location == \"Google Drive\":\n gdrive_fields = [\"gdrive_format\", \"service_account_key\", \"folder_id\"]\n for f_name in gdrive_fields:\n if f_name in build_config:\n build_config[f_name][\"show\"] = True\n\n return build_config\n\n async def save_to_file(self) -> Message:\n \"\"\"Save the input to a file and upload it, returning a confirmation message.\"\"\"\n # Validate inputs\n if not self.file_name:\n msg = \"File name must be provided.\"\n raise ValueError(msg)\n if not self._get_input_type():\n msg = \"Input type is not set.\"\n raise ValueError(msg)\n\n # Get selected storage location\n storage_location = self._get_selected_storage_location()\n if not storage_location:\n msg = \"Storage location must be selected.\"\n raise ValueError(msg)\n\n # Check if Local storage is disabled in cloud environment\n if storage_location == \"Local\" and is_astra_cloud_environment():\n msg = \"Local storage is not available in cloud environment. Please use AWS or Google Drive.\"\n raise ValueError(msg)\n\n # Route to appropriate save method based on storage location\n if storage_location == \"Local\":\n return await self._save_to_local()\n if storage_location == \"AWS\":\n return await self._save_to_aws()\n if storage_location == \"Google Drive\":\n return await self._save_to_google_drive()\n msg = f\"Unsupported storage location: {storage_location}\"\n raise ValueError(msg)\n\n def _get_input_type(self) -> str:\n \"\"\"Determine the input type based on the provided input.\"\"\"\n # Use exact type checking (type() is) instead of isinstance() to avoid inheritance issues.\n # Since Message inherits from Data, isinstance(message, Data) would return True for Message objects,\n # causing Message inputs to be incorrectly identified as Data type.\n if type(self.input) is DataFrame:\n return \"DataFrame\"\n if type(self.input) is Message:\n return \"Message\"\n if type(self.input) is Data:\n return \"Data\"\n msg = f\"Unsupported input type: {type(self.input)}\"\n raise ValueError(msg)\n\n def _get_default_format(self) -> str:\n \"\"\"Return the default file format based on input type.\"\"\"\n if self._get_input_type() == \"DataFrame\":\n return \"csv\"\n if self._get_input_type() == \"Data\":\n return \"json\"\n if self._get_input_type() == \"Message\":\n return \"json\"\n return \"json\" # Fallback\n\n def _adjust_file_path_with_format(self, path: Path, fmt: str) -> Path:\n \"\"\"Adjust the file path to include the correct extension.\"\"\"\n file_extension = path.suffix.lower().lstrip(\".\")\n if fmt == \"excel\":\n return Path(f\"{path}.xlsx\").expanduser() if file_extension not in [\"xlsx\", \"xls\"] else path\n return Path(f\"{path}.{fmt}\").expanduser() if file_extension != fmt else path\n\n def _is_plain_text_format(self, fmt: str) -> bool:\n \"\"\"Check if a file format is plain text (supports appending).\"\"\"\n plain_text_formats = [\"txt\", \"json\", \"markdown\", \"md\", \"csv\", \"xml\", \"html\", \"yaml\", \"log\", \"tsv\", \"jsonl\"]\n return fmt.lower() in plain_text_formats\n\n async def _upload_file(self, file_path: Path) -> None:\n \"\"\"Upload the saved file using the upload_user_file service.\"\"\"\n from langflow.api.v2.files import upload_user_file\n from langflow.services.database.models.user.crud import get_user_by_id\n\n # Ensure the file exists\n if not file_path.exists():\n msg = f\"File not found: {file_path}\"\n raise FileNotFoundError(msg)\n\n # Upload the file - always use append=False because the local file already contains\n # the correct content (either new or appended locally)\n with file_path.open(\"rb\") as f:\n async with session_scope() as db:\n if not self.user_id:\n msg = \"User ID is required for file saving.\"\n raise ValueError(msg)\n current_user = await get_user_by_id(db, self.user_id)\n\n await upload_user_file(\n file=UploadFile(filename=file_path.name, file=f, size=file_path.stat().st_size),\n session=db,\n current_user=current_user,\n storage_service=get_storage_service(),\n settings_service=get_settings_service(),\n append=False,\n )\n\n def _save_dataframe(self, dataframe: DataFrame, path: Path, fmt: str) -> str:\n \"\"\"Save a DataFrame to the specified file format.\"\"\"\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"csv\":\n dataframe.to_csv(path, index=False, mode=\"a\" if should_append else \"w\", header=not should_append)\n elif fmt == \"excel\":\n dataframe.to_excel(path, index=False, engine=\"openpyxl\")\n elif fmt == \"json\":\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new data\n new_records = json.loads(dataframe.to_json(orient=\"records\"))\n existing_data.extend(new_records)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n dataframe.to_json(path, orient=\"records\", indent=2)\n elif fmt == \"markdown\":\n content = dataframe.to_markdown(index=False)\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported DataFrame format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"DataFrame {action} '{path}'\"\n\n def _save_data(self, data: Data, path: Path, fmt: str) -> str:\n \"\"\"Save a Data object to the specified file format.\"\"\"\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"csv\":\n pd.DataFrame(data.data).to_csv(\n path,\n index=False,\n mode=\"a\" if should_append else \"w\",\n header=not should_append,\n )\n elif fmt == \"excel\":\n pd.DataFrame(data.data).to_excel(path, index=False, engine=\"openpyxl\")\n elif fmt == \"json\":\n new_data = jsonable_encoder(data.data)\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new data\n if isinstance(new_data, list):\n existing_data.extend(new_data)\n else:\n existing_data.append(new_data)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n content = orjson.dumps(new_data, option=orjson.OPT_INDENT_2).decode(\"utf-8\")\n path.write_text(content, encoding=\"utf-8\")\n elif fmt == \"markdown\":\n content = pd.DataFrame(data.data).to_markdown(index=False)\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported Data format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"Data {action} '{path}'\"\n\n async def _save_message(self, message: Message, path: Path, fmt: str) -> str:\n \"\"\"Save a Message to the specified file format, handling async iterators.\"\"\"\n content = \"\"\n if message.text is None:\n content = \"\"\n elif isinstance(message.text, AsyncIterator):\n async for item in message.text:\n content += str(item) + \" \"\n content = content.strip()\n elif isinstance(message.text, Iterator):\n content = \" \".join(str(item) for item in message.text)\n else:\n content = str(message.text)\n\n append_mode = getattr(self, \"append_mode\", False)\n should_append = append_mode and path.exists() and self._is_plain_text_format(fmt)\n\n if fmt == \"txt\":\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\" + content, encoding=\"utf-8\")\n else:\n path.write_text(content, encoding=\"utf-8\")\n elif fmt == \"json\":\n new_message = {\"message\": content}\n if should_append:\n # Read and parse existing JSON\n existing_data = []\n try:\n existing_content = path.read_text(encoding=\"utf-8\").strip()\n if existing_content:\n parsed = json.loads(existing_content)\n # Handle case where existing content is a single object\n if isinstance(parsed, dict):\n existing_data = [parsed]\n elif isinstance(parsed, list):\n existing_data = parsed\n except (json.JSONDecodeError, FileNotFoundError):\n # Treat parse errors or missing file as empty array\n existing_data = []\n\n # Append new message\n existing_data.append(new_message)\n\n # Write back as a single JSON array\n path.write_text(json.dumps(existing_data, indent=2), encoding=\"utf-8\")\n else:\n path.write_text(json.dumps(new_message, indent=2), encoding=\"utf-8\")\n elif fmt == \"markdown\":\n md_content = f\"**Message:**\\n\\n{content}\"\n if should_append:\n path.write_text(path.read_text(encoding=\"utf-8\") + \"\\n\\n\" + md_content, encoding=\"utf-8\")\n else:\n path.write_text(md_content, encoding=\"utf-8\")\n else:\n msg = f\"Unsupported Message format: {fmt}\"\n raise ValueError(msg)\n action = \"appended to\" if should_append else \"saved successfully as\"\n return f\"Message {action} '{path}'\"\n\n def _get_selected_storage_location(self) -> str:\n \"\"\"Get the selected storage location from the SortableListInput.\"\"\"\n if hasattr(self, \"storage_location\") and self.storage_location:\n if isinstance(self.storage_location, list) and len(self.storage_location) > 0:\n return self.storage_location[0].get(\"name\", \"\")\n if isinstance(self.storage_location, dict):\n return self.storage_location.get(\"name\", \"\")\n return \"\"\n\n def _get_file_format_for_location(self, location: str) -> str:\n \"\"\"Get the appropriate file format based on storage location.\"\"\"\n if location == \"Local\":\n return getattr(self, \"local_format\", None) or self._get_default_format()\n if location == \"AWS\":\n return getattr(self, \"aws_format\", \"txt\")\n if location == \"Google Drive\":\n return getattr(self, \"gdrive_format\", \"txt\")\n return self._get_default_format()\n\n async def _save_to_local(self) -> Message:\n \"\"\"Save file to local storage (original functionality).\"\"\"\n file_format = self._get_file_format_for_location(\"Local\")\n\n # Validate file format based on input type\n allowed_formats = (\n self.LOCAL_MESSAGE_FORMAT_CHOICES if self._get_input_type() == \"Message\" else self.LOCAL_DATA_FORMAT_CHOICES\n )\n if file_format not in allowed_formats:\n msg = f\"Invalid file format '{file_format}' for {self._get_input_type()}. Allowed: {allowed_formats}\"\n raise ValueError(msg)\n\n # Prepare file path\n file_path = Path(self.file_name).expanduser()\n if not file_path.parent.exists():\n file_path.parent.mkdir(parents=True, exist_ok=True)\n file_path = self._adjust_file_path_with_format(file_path, file_format)\n\n # Save the input to file based on type\n if self._get_input_type() == \"DataFrame\":\n confirmation = self._save_dataframe(self.input, file_path, file_format)\n elif self._get_input_type() == \"Data\":\n confirmation = self._save_data(self.input, file_path, file_format)\n elif self._get_input_type() == \"Message\":\n confirmation = await self._save_message(self.input, file_path, file_format)\n else:\n msg = f\"Unsupported input type: {self._get_input_type()}\"\n raise ValueError(msg)\n\n # Upload the saved file\n await self._upload_file(file_path)\n\n # Return the final file path and confirmation message\n final_path = Path.cwd() / file_path if not file_path.is_absolute() else file_path\n return Message(text=f\"{confirmation} at {final_path}\")\n\n async def _save_to_aws(self) -> Message:\n \"\"\"Save file to AWS S3 using S3 functionality.\"\"\"\n import os\n\n # Get AWS credentials from component inputs or fall back to environment variables\n aws_access_key_id = getattr(self, \"aws_access_key_id\", None)\n if aws_access_key_id and hasattr(aws_access_key_id, \"get_secret_value\"):\n aws_access_key_id = aws_access_key_id.get_secret_value()\n if not aws_access_key_id:\n aws_access_key_id = os.getenv(\"AWS_ACCESS_KEY_ID\")\n\n aws_secret_access_key = getattr(self, \"aws_secret_access_key\", None)\n if aws_secret_access_key and hasattr(aws_secret_access_key, \"get_secret_value\"):\n aws_secret_access_key = aws_secret_access_key.get_secret_value()\n if not aws_secret_access_key:\n aws_secret_access_key = os.getenv(\"AWS_SECRET_ACCESS_KEY\")\n\n bucket_name = getattr(self, \"bucket_name\", None)\n if not bucket_name:\n # Try to get from storage service settings\n settings = get_settings_service().settings\n bucket_name = settings.object_storage_bucket_name\n\n # Validate AWS credentials\n if not aws_access_key_id:\n msg = (\n \"AWS Access Key ID is required for S3 storage. Provide it as a component input \"\n \"or set AWS_ACCESS_KEY_ID environment variable.\"\n )\n raise ValueError(msg)\n if not aws_secret_access_key:\n msg = (\n \"AWS Secret Key is required for S3 storage. Provide it as a component input \"\n \"or set AWS_SECRET_ACCESS_KEY environment variable.\"\n )\n raise ValueError(msg)\n if not bucket_name:\n msg = (\n \"S3 Bucket Name is required for S3 storage. Provide it as a component input \"\n \"or set LANGFLOW_OBJECT_STORAGE_BUCKET_NAME environment variable.\"\n )\n raise ValueError(msg)\n\n # Use S3 upload functionality\n try:\n import boto3\n except ImportError as e:\n msg = \"boto3 is not installed. Please install it using `uv pip install boto3`.\"\n raise ImportError(msg) from e\n\n # Create S3 client\n client_config: dict[str, Any] = {\n \"aws_access_key_id\": str(aws_access_key_id),\n \"aws_secret_access_key\": str(aws_secret_access_key),\n }\n\n # Get region from component input, environment variable, or settings\n aws_region = getattr(self, \"aws_region\", None)\n if not aws_region:\n aws_region = os.getenv(\"AWS_DEFAULT_REGION\") or os.getenv(\"AWS_REGION\")\n if aws_region:\n client_config[\"region_name\"] = str(aws_region)\n\n s3_client = boto3.client(\"s3\", **client_config)\n\n # Extract content\n content = self._extract_content_for_upload()\n file_format = self._get_file_format_for_location(\"AWS\")\n\n # Generate file path\n file_path = f\"{self.file_name}.{file_format}\"\n if hasattr(self, \"s3_prefix\") and self.s3_prefix:\n file_path = f\"{self.s3_prefix.rstrip('/')}/{file_path}\"\n\n # Create temporary file\n import tempfile\n\n with tempfile.NamedTemporaryFile(\n mode=\"w\", encoding=\"utf-8\", suffix=f\".{file_format}\", delete=False\n ) as temp_file:\n temp_file.write(content)\n temp_file_path = temp_file.name\n\n try:\n # Upload to S3\n s3_client.upload_file(temp_file_path, bucket_name, file_path)\n s3_url = f\"s3://{bucket_name}/{file_path}\"\n return Message(text=f\"File successfully uploaded to {s3_url}\")\n finally:\n # Clean up temp file\n if Path(temp_file_path).exists():\n Path(temp_file_path).unlink()\n\n async def _save_to_google_drive(self) -> Message:\n \"\"\"Save file to Google Drive using Google Drive functionality.\"\"\"\n # Validate Google Drive credentials\n if not getattr(self, \"service_account_key\", None):\n msg = \"GCP Credentials Secret Key is required for Google Drive storage\"\n raise ValueError(msg)\n if not getattr(self, \"folder_id\", None):\n msg = \"Google Drive Folder ID is required for Google Drive storage\"\n raise ValueError(msg)\n\n # Use Google Drive upload functionality\n try:\n import json\n import tempfile\n\n from google.oauth2 import service_account\n from googleapiclient.discovery import build\n from googleapiclient.http import MediaFileUpload\n except ImportError as e:\n msg = \"Google API client libraries are not installed. Please install them.\"\n raise ImportError(msg) from e\n\n # Parse credentials with multiple fallback strategies\n credentials_dict = None\n parse_errors = []\n\n # Strategy 1: Parse as-is with strict=False to allow control characters\n try:\n credentials_dict = json.loads(self.service_account_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Standard parse: {e!s}\")\n\n # Strategy 2: Strip whitespace and try again\n if credentials_dict is None:\n try:\n cleaned_key = self.service_account_key.strip()\n credentials_dict = json.loads(cleaned_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Stripped parse: {e!s}\")\n\n # Strategy 3: Check if it's double-encoded (JSON string of a JSON string)\n if credentials_dict is None:\n try:\n decoded_once = json.loads(self.service_account_key, strict=False)\n if isinstance(decoded_once, str):\n credentials_dict = json.loads(decoded_once, strict=False)\n else:\n credentials_dict = decoded_once\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Double-encoded parse: {e!s}\")\n\n # Strategy 4: Try to fix common issues with newlines in the private_key field\n if credentials_dict is None:\n try:\n # Replace literal \\n with actual newlines which is common in pasted JSON\n fixed_key = self.service_account_key.replace(\"\\\\n\", \"\\n\")\n credentials_dict = json.loads(fixed_key, strict=False)\n except json.JSONDecodeError as e:\n parse_errors.append(f\"Newline-fixed parse: {e!s}\")\n\n if credentials_dict is None:\n error_details = \"; \".join(parse_errors)\n msg = (\n f\"Unable to parse service account key JSON. Tried multiple strategies: {error_details}. \"\n \"Please ensure you've copied the entire JSON content from your service account key file. \"\n \"The JSON should start with '{' and contain fields like 'type', 'project_id', 'private_key', etc.\"\n )\n raise ValueError(msg)\n\n # Create Google Drive service with appropriate scopes\n # Use drive scope for folder access, file scope is too restrictive for folder verification\n credentials = service_account.Credentials.from_service_account_info(\n credentials_dict, scopes=[\"https://www.googleapis.com/auth/drive\"]\n )\n drive_service = build(\"drive\", \"v3\", credentials=credentials)\n\n # Extract content and format\n content = self._extract_content_for_upload()\n file_format = self._get_file_format_for_location(\"Google Drive\")\n\n # Handle special Google Drive formats\n if file_format in [\"slides\", \"docs\"]:\n return await self._save_to_google_apps(drive_service, credentials, content, file_format)\n\n # Create temporary file\n file_path = f\"{self.file_name}.{file_format}\"\n with tempfile.NamedTemporaryFile(\n mode=\"w\",\n encoding=\"utf-8\",\n suffix=f\".{file_format}\",\n delete=False,\n ) as temp_file:\n temp_file.write(content)\n temp_file_path = temp_file.name\n\n try:\n # Upload to Google Drive\n # Note: We skip explicit folder verification since it requires broader permissions.\n # If the folder doesn't exist or isn't accessible, the create() call will fail with a clear error.\n file_metadata = {\"name\": file_path, \"parents\": [self.folder_id]}\n media = MediaFileUpload(temp_file_path, resumable=True)\n\n try:\n uploaded_file = (\n drive_service.files().create(body=file_metadata, media_body=media, fields=\"id\").execute()\n )\n except Exception as e:\n msg = (\n f\"Unable to upload file to Google Drive folder '{self.folder_id}'. \"\n f\"Error: {e!s}. \"\n \"Please ensure: 1) The folder ID is correct, 2) The folder exists, \"\n \"3) The service account has been granted access to this folder.\"\n )\n raise ValueError(msg) from e\n\n file_id = uploaded_file.get(\"id\")\n file_url = f\"https://drive.google.com/file/d/{file_id}/view\"\n return Message(text=f\"File successfully uploaded to Google Drive: {file_url}\")\n finally:\n # Clean up temp file\n if Path(temp_file_path).exists():\n Path(temp_file_path).unlink()\n\n async def _save_to_google_apps(self, drive_service, credentials, content: str, app_type: str) -> Message:\n \"\"\"Save content to Google Apps (Slides or Docs).\"\"\"\n import time\n\n if app_type == \"slides\":\n from googleapiclient.discovery import build\n\n slides_service = build(\"slides\", \"v1\", credentials=credentials)\n\n file_metadata = {\n \"name\": self.file_name,\n \"mimeType\": \"application/vnd.google-apps.presentation\",\n \"parents\": [self.folder_id],\n }\n\n created_file = drive_service.files().create(body=file_metadata, fields=\"id\").execute()\n presentation_id = created_file[\"id\"]\n\n time.sleep(2) # Wait for file to be available # noqa: ASYNC251\n\n presentation = slides_service.presentations().get(presentationId=presentation_id).execute()\n slide_id = presentation[\"slides\"][0][\"objectId\"]\n\n # Add content to slide\n requests = [\n {\n \"createShape\": {\n \"objectId\": \"TextBox_01\",\n \"shapeType\": \"TEXT_BOX\",\n \"elementProperties\": {\n \"pageObjectId\": slide_id,\n \"size\": {\n \"height\": {\"magnitude\": 3000000, \"unit\": \"EMU\"},\n \"width\": {\"magnitude\": 6000000, \"unit\": \"EMU\"},\n },\n \"transform\": {\n \"scaleX\": 1,\n \"scaleY\": 1,\n \"translateX\": 1000000,\n \"translateY\": 1000000,\n \"unit\": \"EMU\",\n },\n },\n }\n },\n {\"insertText\": {\"objectId\": \"TextBox_01\", \"insertionIndex\": 0, \"text\": content}},\n ]\n\n slides_service.presentations().batchUpdate(\n presentationId=presentation_id, body={\"requests\": requests}\n ).execute()\n file_url = f\"https://docs.google.com/presentation/d/{presentation_id}/edit\"\n\n elif app_type == \"docs\":\n from googleapiclient.discovery import build\n\n docs_service = build(\"docs\", \"v1\", credentials=credentials)\n\n file_metadata = {\n \"name\": self.file_name,\n \"mimeType\": \"application/vnd.google-apps.document\",\n \"parents\": [self.folder_id],\n }\n\n created_file = drive_service.files().create(body=file_metadata, fields=\"id\").execute()\n document_id = created_file[\"id\"]\n\n time.sleep(2) # Wait for file to be available # noqa: ASYNC251\n\n # Add content to document\n requests = [{\"insertText\": {\"location\": {\"index\": 1}, \"text\": content}}]\n docs_service.documents().batchUpdate(documentId=document_id, body={\"requests\": requests}).execute()\n file_url = f\"https://docs.google.com/document/d/{document_id}/edit\"\n\n return Message(text=f\"File successfully created in Google {app_type.title()}: {file_url}\")\n\n def _extract_content_for_upload(self) -> str:\n \"\"\"Extract content from input for upload to cloud services.\"\"\"\n if self._get_input_type() == \"DataFrame\":\n return self.input.to_csv(index=False)\n if self._get_input_type() == \"Data\":\n if hasattr(self.input, \"data\") and self.input.data:\n if isinstance(self.input.data, dict):\n import json\n\n return json.dumps(self.input.data, indent=2, ensure_ascii=False)\n return str(self.input.data)\n return str(self.input)\n if self._get_input_type() == \"Message\":\n return str(self.input.text) if self.input.text else str(self.input)\n return str(self.input)\n"
|
|
1847
1872
|
},
|
|
1848
1873
|
"file_name": {
|
|
1849
1874
|
"_input_type": "StrInput",
|