-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathn8n.py
More file actions
1461 lines (1279 loc) · 66.1 KB
/
n8n.py
File metadata and controls
1461 lines (1279 loc) · 66.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
title: n8n Pipeline with StreamingResponse Support
author: owndev
author_url: https://github.com/owndev/
project_url: https://github.com/owndev/Open-WebUI-Functions
funding_url: https://github.com/sponsors/owndev
n8n_template: https://github.com/owndev/Open-WebUI-Functions/blob/main/pipelines/n8n/Open_WebUI_Test_Agent_Streaming.json
version: 2.3.0
required_open_webui_version: 0.8.0
license: Apache License 2.0
description: An optimized streaming-enabled pipeline for interacting with N8N workflows, consistent response handling for both streaming and non-streaming modes, robust error handling, and simplified status management. Supports Server-Sent Events (SSE) streaming and various N8N workflow formats. Now includes configurable AI Agent tool usage display with three verbosity levels (minimal, compact, detailed) and customizable length limits for tool inputs/outputs (non-streaming mode only).
features:
- Integrates with N8N for seamless streaming communication.
- Uses FastAPI StreamingResponse for real-time streaming.
- Enables real-time interaction with N8N workflows.
- Provides configurable status emissions and chunk streaming.
- Cloudflare Access support for secure communication.
- Encrypted storage of sensitive API keys.
- Fallback support for non-streaming responses.
- Compatible with Open WebUI streaming architecture.
- Displays N8N AI Agent tool usage with configurable verbosity (non-streaming mode only).
- Three display modes: minimal (tool names only), compact (names + preview), detailed (full collapsible sections).
- Customizable length limits for tool inputs and outputs.
- Shows tool calls, inputs, and results from intermediateSteps in non-streaming mode (N8N limitation - streaming responses do not include intermediateSteps).
"""
from typing import (
Optional,
Callable,
Awaitable,
Any,
Dict,
AsyncIterator,
Union,
Generator,
Iterator,
)
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from starlette.background import BackgroundTask
from cryptography.fernet import Fernet, InvalidToken
import aiohttp
import os
import base64
import hashlib
import logging
import json
import asyncio
from open_webui.env import AIOHTTP_CLIENT_TIMEOUT, SRC_LOG_LEVELS
from pydantic_core import core_schema
import time
import re
# Simplified encryption implementation with automatic handling
class EncryptedStr(str):
"""A string type that automatically handles encryption/decryption"""
@classmethod
def _get_encryption_key(cls) -> Optional[bytes]:
"""
Generate encryption key from WEBUI_SECRET_KEY if available
Returns None if no key is configured
"""
secret = os.getenv("WEBUI_SECRET_KEY")
if not secret:
return None
hashed_key = hashlib.sha256(secret.encode()).digest()
return base64.urlsafe_b64encode(hashed_key)
@classmethod
def encrypt(cls, value: str) -> str:
"""
Encrypt a string value if a key is available
Returns the original value if no key is available
"""
if not value or value.startswith("encrypted:"):
return value
key = cls._get_encryption_key()
if not key: # No encryption if no key
return value
f = Fernet(key)
encrypted = f.encrypt(value.encode())
return f"encrypted:{encrypted.decode()}"
@classmethod
def decrypt(cls, value: str) -> str:
"""
Decrypt an encrypted string value if a key is available
Returns the original value if no key is available or decryption fails
"""
if not value or not value.startswith("encrypted:"):
return value
key = cls._get_encryption_key()
if not key: # No decryption if no key
return value[len("encrypted:") :] # Return without prefix
try:
encrypted_part = value[len("encrypted:") :]
f = Fernet(key)
decrypted = f.decrypt(encrypted_part.encode())
return decrypted.decode()
except (InvalidToken, Exception):
return value
# Pydantic integration
@classmethod
def __get_pydantic_core_schema__(
cls, _source_type: Any, _handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.union_schema(
[
core_schema.is_instance_schema(cls),
core_schema.chain_schema(
[
core_schema.str_schema(),
core_schema.no_info_plain_validator_function(
lambda value: cls(cls.encrypt(value) if value else value)
),
]
),
],
serialization=core_schema.plain_serializer_function_ser_schema(
lambda instance: str(instance)
),
)
# Helper functions for resource cleanup
async def cleanup_response(
response: Optional[aiohttp.ClientResponse],
session: Optional[aiohttp.ClientSession],
) -> None:
"""
Clean up the response and session objects.
Args:
response: The ClientResponse object to close
session: The ClientSession object to close
"""
if response:
response.close()
if session:
await session.close()
async def stream_processor(
content: aiohttp.StreamReader,
__event_emitter__=None,
response: Optional[aiohttp.ClientResponse] = None,
session: Optional[aiohttp.ClientSession] = None,
logger: Optional[logging.Logger] = None,
) -> AsyncIterator[str]:
"""
Process streaming content from n8n and yield chunks for StreamingResponse.
Args:
content: The streaming content from the response
__event_emitter__: Optional event emitter for status updates
response: The response object for cleanup
session: The session object for cleanup
logger: Logger for debugging
Yields:
String content from the streaming response
"""
try:
if logger:
logger.info("Starting stream processing...")
buffer = ""
# Attempt to read preserve flag later via closure if needed
async for chunk_bytes in content:
chunk_str = chunk_bytes.decode("utf-8", errors="ignore")
if not chunk_str:
continue
buffer += chunk_str
# Process complete lines (retain trailing newline info)
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
had_newline = True
original_line = line # without \n
if line.endswith("\r"):
line = line[:-1]
if logger:
logger.debug(f"Raw line received: {repr(line)}")
# Preserve blank lines
if line == "":
yield "\n"
continue
content_text = ""
if line.startswith("data: "):
data_part = line[6:]
if logger:
logger.debug(f"SSE data part: {repr(data_part)}")
if data_part == "[DONE]":
if logger:
logger.debug("Received [DONE] signal")
buffer = ""
break
try:
event_data = json.loads(data_part)
if logger:
logger.debug(f"Parsed SSE JSON: {event_data}")
for key in ("content", "text", "output", "data"):
val = event_data.get(key)
if isinstance(val, str) and val:
content_text = val
break
except json.JSONDecodeError:
content_text = data_part
if logger:
logger.debug(
f"Using raw data as content: {repr(content_text)}"
)
elif not line.startswith(":"):
# Plain text (non-SSE)
content_text = original_line
if logger:
logger.debug(f"Plain text content: {repr(content_text)}")
if content_text:
if not content_text.endswith("\n"):
content_text += "\n"
if logger:
logger.debug(f"Yielding content: {repr(content_text)}")
yield content_text
# Send completion status update when streaming is done
if __event_emitter__:
await __event_emitter__(
{
"type": "status",
"data": {
"status": "complete",
"description": "N8N streaming completed successfully",
"done": True,
},
}
)
if logger:
logger.info("Stream processing completed successfully")
except Exception as e:
if logger:
logger.error(f"Error processing stream: {e}")
# Send error status update
if __event_emitter__:
await __event_emitter__(
{
"type": "status",
"data": {
"status": "error",
"description": f"N8N streaming error: {str(e)}",
"done": True,
},
}
)
raise
finally:
# Always attempt to close response and session to avoid resource leaks
await cleanup_response(response, session)
class Pipe:
class Valves(BaseModel):
N8N_URL: str = Field(
default="https://<your-endpoint>/webhook/<your-webhook>",
description="URL for the N8N webhook",
)
N8N_BEARER_TOKEN: EncryptedStr = Field(
default="",
description="Bearer token for authenticating with the N8N webhook",
json_schema_extra={"input": {"type": "password"}},
)
INPUT_FIELD: str = Field(
default="chatInput",
description="Field name for the input message in the N8N payload",
)
RESPONSE_FIELD: str = Field(
default="output",
description="Field name for the response message in the N8N payload",
)
SEND_CONVERSATION_HISTORY: bool = Field(
default=False,
description="Whether to include conversation history when sending requests to N8N",
)
TOOL_DISPLAY_VERBOSITY: str = Field(
default="detailed",
description="Verbosity level for tool usage display: 'minimal' (only tool names), 'compact' (names + short preview), 'detailed' (full info with collapsible sections)",
)
TOOL_INPUT_MAX_LENGTH: int = Field(
default=500,
description="Maximum length for tool input display (0 = unlimited). Longer inputs will be truncated.",
)
TOOL_OUTPUT_MAX_LENGTH: int = Field(
default=500,
description="Maximum length for tool output/observation display (0 = unlimited). Longer outputs will be truncated.",
)
CF_ACCESS_CLIENT_ID: EncryptedStr = Field(
default="",
description="Only if behind Cloudflare: https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/",
json_schema_extra={"input": {"type": "password"}},
)
CF_ACCESS_CLIENT_SECRET: EncryptedStr = Field(
default="",
description="Only if behind Cloudflare: https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/",
json_schema_extra={"input": {"type": "password"}},
)
def __init__(self):
self.name = "N8N Agent"
self.valves = self.Valves()
self.log = logging.getLogger("n8n_streaming_pipeline")
self.log.setLevel(SRC_LOG_LEVELS.get("OPENAI", logging.INFO))
def _format_tool_calls_section(
self, intermediate_steps: list, for_streaming: bool = False
) -> str:
"""
Creates a formatted tool calls section using collapsible details elements.
Args:
intermediate_steps: List of intermediate step objects from N8N response
for_streaming: If True, format for streaming (with escaping), else for regular response
Returns:
Formatted tool calls section with HTML details elements
"""
if not intermediate_steps:
return ""
verbosity = self.valves.TOOL_DISPLAY_VERBOSITY.lower()
input_max_len = self.valves.TOOL_INPUT_MAX_LENGTH
output_max_len = self.valves.TOOL_OUTPUT_MAX_LENGTH
# Helper function to truncate text
def truncate_text(text: str, max_length: int) -> str:
if max_length <= 0 or len(text) <= max_length:
return text
return text[:max_length] + "..."
# Minimal mode: just list tool names
if verbosity == "minimal":
tool_names = []
for i, step in enumerate(intermediate_steps, 1):
if isinstance(step, dict):
tool_name = step.get("action", {}).get("tool", "Unknown Tool")
tool_names.append(f"{i}. {tool_name}")
tool_list = "\\n" if for_streaming else "\n"
tool_list = tool_list.join(tool_names)
if for_streaming:
return f"\\n\\n<details>\\n<summary>🛠️ Tool Calls ({len(intermediate_steps)} steps)</summary>\\n\\n{tool_list}\\n\\n</details>\\n"
else:
return f"\n\n<details>\n<summary>🛠️ Tool Calls ({len(intermediate_steps)} steps)</summary>\n\n{tool_list}\n\n</details>\n"
# Compact mode: tool names with short preview
if verbosity == "compact":
tool_summaries = []
for i, step in enumerate(intermediate_steps, 1):
if not isinstance(step, dict):
continue
action = step.get("action", {})
observation = step.get("observation", "")
tool_name = action.get("tool", "Unknown Tool")
# Get short preview of output
preview = ""
if observation:
obs_str = str(observation)
# If output_max_len is 0 (unlimited), use a reasonable default preview length for compact mode
# Otherwise, use the configured limit
if output_max_len > 0:
preview_len = min(100, output_max_len)
else:
preview_len = 100 # Default preview length for compact mode when unlimited
preview = truncate_text(obs_str, preview_len)
summary = f"**{i}. {tool_name}**"
if preview:
summary += f" → {preview}"
tool_summaries.append(summary)
summary_text = "\\n" if for_streaming else "\n"
summary_text = summary_text.join(tool_summaries)
if for_streaming:
return f"\\n\\n<details>\\n<summary>🛠️ Tool Calls ({len(intermediate_steps)} steps)</summary>\\n\\n{summary_text}\\n\\n</details>\\n"
else:
return f"\n\n<details>\n<summary>🛠️ Tool Calls ({len(intermediate_steps)} steps)</summary>\n\n{summary_text}\n\n</details>\n"
# Detailed mode: full collapsible sections (default)
tool_entries = []
for i, step in enumerate(intermediate_steps, 1):
if not isinstance(step, dict):
continue
action = step.get("action", {})
observation = step.get("observation", "")
tool_name = action.get("tool", "Unknown Tool")
tool_input = action.get("toolInput", {})
tool_call_id = action.get("toolCallId", "")
log_message = action.get("log", "")
# Build individual tool call details
tool_info = []
tool_info.append(f"🔧 **Tool:** {tool_name}")
if tool_call_id:
tool_info.append(f"🆔 **Call ID:** `{tool_call_id}`")
# Format tool input
if tool_input:
try:
if isinstance(tool_input, dict):
input_json = json.dumps(tool_input, indent=2)
# Apply max length limit
if input_max_len > 0:
input_json = truncate_text(input_json, input_max_len)
if for_streaming:
# Escape for streaming
input_json = (
input_json.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("\n", "\\n")
)
tool_info.append(
f"📥 **Input:**\\n```json\\n{input_json}\\n```"
)
else:
tool_info.append(
f"📥 **Input:**\n```json\n{input_json}\n```"
)
else:
input_str = str(tool_input)
if input_max_len > 0:
input_str = truncate_text(input_str, input_max_len)
tool_info.append(f"📥 **Input:** `{input_str}`")
except Exception:
input_str = str(tool_input)
if input_max_len > 0:
input_str = truncate_text(input_str, input_max_len)
tool_info.append(f"📥 **Input:** `{input_str}`")
# Format observation/result
if observation:
try:
# Try to parse as JSON for better formatting
if isinstance(observation, str) and (
observation.startswith("[") or observation.startswith("{")
):
obs_json = json.loads(observation)
obs_formatted = json.dumps(obs_json, indent=2)
# Apply max length limit
if output_max_len > 0:
obs_formatted = truncate_text(obs_formatted, output_max_len)
if for_streaming:
obs_formatted = (
obs_formatted.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("\n", "\\n")
)
tool_info.append(
f"📤 **Result:**\\n```json\\n{obs_formatted}\\n```"
)
else:
tool_info.append(
f"📤 **Result:**\n```json\n{obs_formatted}\n```"
)
else:
# Plain text observation
obs_str = str(observation)
# Apply configured limit (0 = unlimited, don't truncate)
obs_preview = (
truncate_text(obs_str, output_max_len)
if output_max_len > 0
else obs_str
)
if for_streaming:
obs_preview = (
obs_preview.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("\n", "\\n")
)
tool_info.append(f"📤 **Result:** {obs_preview}")
except Exception:
obs_str = str(observation)
# Apply configured limit (0 = unlimited, don't truncate)
obs_preview = (
truncate_text(obs_str, output_max_len)
if output_max_len > 0
else obs_str
)
tool_info.append(f"📤 **Result:** {obs_preview}")
# Add log if available
if log_message:
log_preview = truncate_text(log_message, 200)
tool_info.append(f"📝 **Log:** {log_preview}")
# Create collapsible details for individual tool call
tool_info_text = "\\n" if for_streaming else "\n"
tool_info_text = tool_info_text.join(tool_info)
if for_streaming:
tool_entry = f"<details>\\n<summary>Step {i}: {tool_name}</summary>\\n\\n{tool_info_text}\\n\\n</details>"
else:
tool_entry = f"<details>\n<summary>Step {i}: {tool_name}</summary>\n\n{tool_info_text}\n\n</details>"
tool_entries.append(tool_entry)
# Combine all tool calls into main collapsible section
if for_streaming:
all_tools = "\\n\\n".join(tool_entries)
result = f"\\n\\n<details>\\n<summary>🛠️ Tool Calls ({len(tool_entries)} steps)</summary>\\n\\n{all_tools}\\n\\n</details>\\n"
else:
all_tools = "\n\n".join(tool_entries)
result = f"\n\n<details>\n<summary>🛠️ Tool Calls ({len(tool_entries)} steps)</summary>\n\n{all_tools}\n\n</details>\n"
return result
async def emit_simple_status(
self,
__event_emitter__: Callable[[dict], Awaitable[None]],
status: str,
message: str,
done: bool = False,
):
"""Simplified status emission without intervals"""
if __event_emitter__:
await __event_emitter__(
{
"type": "status",
"data": {
"status": status,
"description": message,
"done": done,
},
}
)
def extract_event_info(self, event_emitter):
if not event_emitter or not event_emitter.__closure__:
return None, None
for cell in event_emitter.__closure__:
if isinstance(request_info := cell.cell_contents, dict):
chat_id = request_info.get("chat_id")
message_id = request_info.get("message_id")
return chat_id, message_id
return None, None
def get_headers(self) -> Dict[str, str]:
"""
Constructs the headers for the API request.
Returns:
Dictionary containing the required headers for the API request.
"""
headers = {"Content-Type": "application/json"}
# Add bearer token if available
bearer_token = EncryptedStr.decrypt(self.valves.N8N_BEARER_TOKEN)
if bearer_token:
headers["Authorization"] = f"Bearer {bearer_token}"
# Add Cloudflare Access headers if available
cf_client_id = EncryptedStr.decrypt(self.valves.CF_ACCESS_CLIENT_ID)
if cf_client_id:
headers["CF-Access-Client-Id"] = cf_client_id
cf_client_secret = EncryptedStr.decrypt(self.valves.CF_ACCESS_CLIENT_SECRET)
if cf_client_secret:
headers["CF-Access-Client-Secret"] = cf_client_secret
return headers
def parse_n8n_streaming_chunk(self, chunk_text: str) -> Optional[str]:
"""Parse N8N streaming chunk and extract content, filtering out metadata"""
if not chunk_text.strip():
return None
try:
data = json.loads(chunk_text.strip())
if isinstance(data, dict):
# Check if this chunk contains intermediateSteps (will be handled separately)
# Note: Don't skip chunks just because they have a type field
chunk_type = data.get("type", "")
# Skip only true metadata chunks that have no content or intermediateSteps
if (
chunk_type in ["begin", "end", "error", "metadata"]
and "intermediateSteps" not in data
):
self.log.debug(f"Skipping N8N metadata chunk: {chunk_type}")
return None
# Skip metadata-only chunks (but allow intermediateSteps)
if (
"metadata" in data
and len(data) <= 2
and "intermediateSteps" not in data
):
return None
# Extract content from various possible field names
content = (
data.get("text")
or data.get("content")
or data.get("output")
or data.get("message")
or data.get("delta")
or data.get("data")
or data.get("response")
or data.get("result")
)
# Handle OpenAI-style streaming format
if not content and "choices" in data:
choices = data.get("choices", [])
if choices and isinstance(choices[0], dict):
delta = choices[0].get("delta", {})
content = delta.get("content", "")
if content:
self.log.debug(
f"Extracted content from JSON: {repr(content[:100])}"
)
return str(content)
# Return non-metadata objects as strings (be more permissive)
if not any(
key in data
for key in [
"type",
"metadata",
"nodeId",
"nodeName",
"timestamp",
"id",
]
):
# For smaller models, return the entire object if it's simple
self.log.debug(
f"Returning entire object as content: {repr(str(data)[:100])}"
)
return str(data)
except json.JSONDecodeError:
# Handle plain text content - be more permissive
stripped = chunk_text.strip()
if stripped and not stripped.startswith("{"):
self.log.debug(f"Returning plain text content: {repr(stripped[:100])}")
return stripped
return None
def extract_content_from_mixed_stream(self, raw_text: str) -> str:
"""Extract content from mixed stream containing both metadata and content"""
content_parts = []
# First try to handle concatenated JSON objects
if "{" in raw_text and "}" in raw_text:
parts = raw_text.split("}{")
for i, part in enumerate(parts):
# Reconstruct valid JSON
if i > 0:
part = "{" + part
if i < len(parts) - 1:
part = part + "}"
extracted = self.parse_n8n_streaming_chunk(part)
if extracted:
content_parts.append(extracted)
# If no JSON content found, treat as plain text
if not content_parts:
# Remove common streaming artifacts but preserve actual content
cleaned = raw_text.strip()
if (
cleaned
and not cleaned.startswith("data:")
and not cleaned.startswith(":")
):
self.log.debug(f"Using raw text as content: {repr(cleaned[:100])}")
return cleaned
return "".join(content_parts)
def dedupe_system_prompt(self, text: str) -> str:
"""Remove duplicated content from the system prompt.
Strategies:
1. Detect full duplication where the prompt text is repeated twice consecutively.
2. Remove duplicate lines (keeping first occurrence, preserving order & spacing where possible).
3. Preserve blank lines but collapse consecutive duplicate non-blank lines.
"""
if not text:
return text
original = text
stripped = text.strip()
# 1. Full duplication detection (exact repeat of first half == second half)
half = len(stripped) // 2
if len(stripped) % 2 == 0:
first_half = stripped[:half].strip()
second_half = stripped[half:].strip()
if first_half and first_half == second_half:
text = first_half
# 2. Line-level dedupe
lines = text.splitlines()
seen = set()
deduped = []
for line in lines:
key = line.strip()
# Allow empty lines to pass through (formatting), but avoid repeating identical non-empty lines
if key and key in seen:
continue
if key:
seen.add(key)
deduped.append(line)
deduped_text = "\n".join(deduped).strip()
if deduped_text != original.strip():
self.log.debug("System prompt deduplicated")
return deduped_text
async def pipe(
self,
body: dict,
__user__: Optional[dict] = None,
__event_emitter__: Callable[[dict], Awaitable[None]] = None,
__event_call__: Callable[[dict], Awaitable[dict]] = None,
) -> Union[str, Generator, Iterator, Dict[str, Any], StreamingResponse]:
"""
Main method for sending requests to the N8N endpoint.
Args:
body: The request body containing messages and other parameters
__event_emitter__: Optional event emitter function for status updates
Returns:
Response from N8N API, which could be a string, dictionary or streaming response
"""
self.log.setLevel(SRC_LOG_LEVELS.get("OPENAI", logging.INFO))
await self.emit_simple_status(
__event_emitter__, "in_progress", f"Calling {self.name} ...", False
)
session = None
n8n_response = ""
messages = body.get("messages", [])
# Verify a message is available
if messages:
question = messages[-1]["content"]
if "Prompt: " in question:
question = question.split("Prompt: ")[-1]
try:
# Extract chat_id and message_id
chat_id, message_id = self.extract_event_info(__event_emitter__)
self.log.info(f"Starting N8N workflow request for chat ID: {chat_id}")
# Extract system prompt correctly
system_prompt = ""
if messages and messages[0].get("role") == "system":
system_prompt = self.dedupe_system_prompt(messages[0]["content"])
# Optionally include full conversation history (controlled by valve)
conversation_history = []
if self.valves.SEND_CONVERSATION_HISTORY:
for msg in messages:
if msg.get("role") in ["user", "assistant"]:
conversation_history.append(
{"role": msg["role"], "content": msg["content"]}
)
# Prepare payload for N8N workflow (improved version)
payload = {
"systemPrompt": system_prompt,
# Include messages only when enabled in valves for privacy/control
"messages": (
conversation_history
if self.valves.SEND_CONVERSATION_HISTORY
else []
),
"currentMessage": question, # Current user message
"user_id": __user__.get("id") if __user__ else None,
"user_email": __user__.get("email") if __user__ else None,
"user_name": __user__.get("name") if __user__ else None,
"user_role": __user__.get("role") if __user__ else None,
"chat_id": chat_id,
"message_id": message_id,
}
# Keep backward compatibility
payload[self.valves.INPUT_FIELD] = question
# Get headers for the request
headers = self.get_headers()
# Create session with no timeout like in stream-example.py
session = aiohttp.ClientSession(
trust_env=True,
timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT),
)
self.log.debug(f"Sending request to N8N: {self.valves.N8N_URL}")
# Send status update via event emitter if available
if __event_emitter__:
await __event_emitter__(
{
"type": "status",
"data": {
"status": "in_progress",
"description": "Sending request to N8N...",
"done": False,
},
}
)
# Make the request
request = session.post(
self.valves.N8N_URL, json=payload, headers=headers
)
response = await request.__aenter__()
self.log.debug(f"Response status: {response.status}")
self.log.debug(f"Response headers: {dict(response.headers)}")
if response.status == 200:
# Enhanced streaming detection (n8n controls streaming)
content_type = response.headers.get("Content-Type", "").lower()
# Check for explicit streaming indicators
# Note: Don't rely solely on Transfer-Encoding: chunked as regular JSON can also be chunked
is_streaming = (
"text/event-stream" in content_type
or "application/x-ndjson" in content_type
or (
"application/json" in content_type
and response.headers.get("Transfer-Encoding") == "chunked"
and "Cache-Control" in response.headers
and "no-cache"
in response.headers.get("Cache-Control", "").lower()
)
)
# Additional check: if content-type is text/html or application/json without streaming headers, it's likely not streaming
if "text/html" in content_type:
is_streaming = False
elif (
"application/json" in content_type
and "Cache-Control" not in response.headers
):
is_streaming = False
if is_streaming:
# Enhanced streaming like in stream-example.py
self.log.info("Processing streaming response from N8N")
n8n_response = ""
buffer = ""
completed_thoughts: list[str] = []
intermediate_steps = [] # Collect tool calls
try:
async for chunk in response.content.iter_any():
if not chunk:
continue
text = chunk.decode(errors="ignore")
buffer += text
# Handle different streaming formats
if "{" in buffer and "}" in buffer:
# Process complete JSON objects like in stream-example.py
while True:
start_idx = buffer.find("{")
if start_idx == -1:
break
# Find matching closing brace
brace_count = 0
end_idx = -1
for i in range(start_idx, len(buffer)):
if buffer[i] == "{":
brace_count += 1
elif buffer[i] == "}":
brace_count -= 1
if brace_count == 0:
end_idx = i
break
if end_idx == -1:
# Incomplete JSON, wait for more data
break
# Extract and process the JSON chunk
json_chunk = buffer[start_idx : end_idx + 1]
buffer = buffer[end_idx + 1 :]
# Try to parse the chunk as JSON to extract intermediateSteps
# This must happen BEFORE parse_n8n_streaming_chunk filters out metadata
# Future-proof: If N8N adds intermediateSteps support in streaming, this will work automatically
try:
parsed_chunk = json.loads(json_chunk)
if isinstance(parsed_chunk, dict):
# Extract intermediateSteps if present (future-proof for when N8N supports this)
chunk_steps = parsed_chunk.get(
"intermediateSteps", []
)
if chunk_steps:
intermediate_steps.extend(
chunk_steps
)
self.log.info(
f"✓ Found {len(chunk_steps)} intermediate steps in streaming chunk"
)
except json.JSONDecodeError:
pass # Continue with content parsing
# Parse N8N streaming chunk for content
content = self.parse_n8n_streaming_chunk(
json_chunk
)
if content:
# Normalize escaped newlines to actual newlines (like non-streaming)
content = content.replace("\\n", "\n")
# Just accumulate content without processing think blocks yet
n8n_response += content
# Emit delta without think block processing
if __event_emitter__:
await __event_emitter__(
{
"type": "chat:message:delta",
"data": {
"role": "assistant",
"content": content,
},
}
)
else:
# Handle plain text streaming (for smaller models)
# Process line by line for plain text
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
if line.strip(): # Only process non-empty lines
self.log.debug(
f"Processing plain text line: {repr(line[:100])}"
)
# Normalize content
content = line.replace("\\n", "\n")
n8n_response += content + "\n"
# Emit delta for plain text
if __event_emitter__:
await __event_emitter__(
{
"type": "chat:message:delta",
"data": {
"role": "assistant",
"content": content + "\n",
},
}
)
# Process any remaining content in buffer (CRITICAL FIX)
if buffer.strip():
self.log.debug(