-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn8n_webhook.py
More file actions
371 lines (312 loc) · 11.5 KB
/
n8n_webhook.py
File metadata and controls
371 lines (312 loc) · 11.5 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
#!/usr/bin/env python3
"""
Integration: n8n Webhook
Demonstrates integrating AGIRAILS with n8n workflow automation.
Exposes AGIRAILS services as webhook endpoints.
Requirements:
pip install flask requests
Run: python integrations/n8n_webhook.py
"""
import asyncio
import sys
import threading
import time
from pathlib import Path
from dataclasses import dataclass
from typing import Any, Dict, Optional
from queue import Queue
import json
sys.path.insert(0, str(Path(__file__).parent.parent))
from src.utils.helpers import clear_mock_state, log, log_section
# Check if Flask is available
try:
from flask import Flask, request as flask_request, jsonify
HAS_FLASK = True
except ImportError:
HAS_FLASK = False
print("Flask not installed. Run: pip install flask")
print("This example will show the conceptual implementation.\n")
@dataclass
class WebhookConfig:
"""Configuration for webhook server."""
host: str = "0.0.0.0"
port: int = 5000
debug: bool = False
@dataclass
class AsyncJob:
"""Represents an async job."""
id: str
service: str
input: Dict[str, Any]
callback_url: Optional[str] = None
status: str = "pending"
result: Optional[Dict[str, Any]] = None
error: Optional[str] = None
class AGIRAILSWebhookServer:
"""Webhook server for AGIRAILS services."""
def __init__(self, client, provider_address: str, config: WebhookConfig = None):
self.client = client
self.provider_address = provider_address
self.config = config or WebhookConfig()
self.jobs: Dict[str, AsyncJob] = {}
self.job_queue: Queue = Queue()
self._loop = asyncio.new_event_loop()
if HAS_FLASK:
self.app = Flask(__name__)
self._setup_routes()
def _setup_routes(self):
"""Setup Flask routes."""
@self.app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok", "service": "agirails-webhook"})
@self.app.route("/webhook/<service>", methods=["POST"])
def handle_sync_webhook(service: str):
"""Synchronous webhook - waits for result."""
data = flask_request.json or {}
budget = data.pop("budget", 1.0)
try:
result = self._execute_request(service, data, budget)
return jsonify({
"success": True,
"result": result,
})
except Exception as e:
return jsonify({
"success": False,
"error": str(e),
}), 500
@self.app.route("/webhook/<service>/async", methods=["POST"])
def handle_async_webhook(service: str):
"""Async webhook - returns job ID immediately."""
data = flask_request.json or {}
callback_url = data.pop("callback_url", None)
budget = data.pop("budget", 1.0)
import uuid
job_id = str(uuid.uuid4())
job = AsyncJob(
id=job_id,
service=service,
input=data,
callback_url=callback_url,
)
self.jobs[job_id] = job
self.job_queue.put((job, budget))
return jsonify({
"success": True,
"job_id": job_id,
"status_url": f"/jobs/{job_id}",
}), 202
@self.app.route("/jobs/<job_id>", methods=["GET"])
def get_job_status(job_id: str):
"""Get job status."""
job = self.jobs.get(job_id)
if not job:
return jsonify({"error": "Job not found"}), 404
return jsonify({
"job_id": job.id,
"service": job.service,
"status": job.status,
"result": job.result,
"error": job.error,
})
def _execute_request(self, service: str, input_data: dict, budget: float) -> dict:
"""Execute AGIRAILS request synchronously."""
from agirails.level0 import request
async def _async_request():
result = await request(
service,
input=input_data,
budget=budget,
client=self.client,
provider=self.provider_address,
)
return result.result
return self._loop.run_until_complete(_async_request())
def run(self):
"""Run the webhook server."""
if HAS_FLASK:
self.app.run(
host=self.config.host,
port=self.config.port,
debug=self.config.debug,
)
async def main() -> None:
log_section("AGIRAILS Integration - n8n Webhook")
clear_mock_state()
from agirails import ACTPClient
from agirails.level0 import provide, set_provider_client, start_provider, stop_provider
REQUESTER = "0x1111111111111111111111111111111111111111"
PROVIDER = "0x2222222222222222222222222222222222222222"
# =====================================================
# Part 1: Setup AGIRAILS Services
# =====================================================
log_section("Part 1: Setup AGIRAILS Services")
# Translation service
async def translate_handler(data: dict) -> dict:
text = data.get("text", "")
target = data.get("targetLang", "de")
return {
"translated": f"[{target.upper()}] {text}",
"sourceLang": "en",
"targetLang": target,
}
# Summarization service
async def summarize_handler(data: dict) -> dict:
text = data.get("text", "")
max_length = data.get("maxLength", 100)
summary = text[:max_length] + "..." if len(text) > max_length else text
return {
"summary": summary,
"originalLength": len(text),
"summaryLength": len(summary),
}
provide("translate", translate_handler)
provide("summarize", summarize_handler)
client = await ACTPClient.create(mode="mock", requester_address=REQUESTER)
await client.mint_tokens(REQUESTER, 100_000_000)
await client.mint_tokens(PROVIDER, 10_000_000)
set_provider_client(client, address=PROVIDER)
await start_provider()
print("Services active: translate, summarize")
print()
# =====================================================
# Part 2: Webhook Server Architecture
# =====================================================
log_section("Part 2: Webhook Server Architecture")
print("Webhook endpoints:")
print()
print(" GET /health")
print(" Health check endpoint")
print()
print(" POST /webhook/<service>")
print(" Synchronous - waits for result")
print(" Body: { input data, budget: 1.0 }")
print()
print(" POST /webhook/<service>/async")
print(" Asynchronous - returns job ID")
print(" Body: { input data, budget: 1.0, callback_url: '...' }")
print()
print(" GET /jobs/<job_id>")
print(" Check async job status")
print()
# =====================================================
# Part 3: n8n Workflow Configuration
# =====================================================
log_section("Part 3: n8n Workflow Configuration")
print("n8n HTTP Request node configuration:")
print()
print("```json")
print(json.dumps({
"method": "POST",
"url": "http://localhost:5000/webhook/translate",
"headers": {
"Content-Type": "application/json"
},
"body": {
"text": "{{ $json.textToTranslate }}",
"targetLang": "de",
"budget": 1.0
}
}, indent=2))
print("```")
print()
# =====================================================
# Part 4: Example Workflows
# =====================================================
log_section("Part 4: Example n8n Workflows")
print("Workflow 1: Email Translation")
print(" 1. Trigger: New email received")
print(" 2. HTTP Request: POST /webhook/translate")
print(" 3. Email: Send translated version")
print()
print("Workflow 2: Document Processing")
print(" 1. Trigger: File uploaded to Google Drive")
print(" 2. HTTP Request: POST /webhook/summarize")
print(" 3. Slack: Post summary to channel")
print()
print("Workflow 3: Async Processing")
print(" 1. Trigger: Webhook receives document")
print(" 2. HTTP Request: POST /webhook/analyze/async")
print(" 3. Wait: Poll /jobs/{id} until complete")
print(" 4. HTTP Request: POST callback with results")
print()
# =====================================================
# Part 5: Flask Implementation
# =====================================================
log_section("Part 5: Flask Implementation")
print("Complete Flask server code:")
print()
print("```python")
print("from flask import Flask, request, jsonify")
print("from agirails.level0 import request as agirails_request")
print()
print("app = Flask(__name__)")
print()
print("@app.route('/webhook/<service>', methods=['POST'])")
print("async def webhook(service: str):")
print(" data = request.json")
print(" budget = data.pop('budget', 1.0)")
print(" ")
print(" result = await agirails_request(")
print(" service,")
print(" input=data,")
print(" budget=budget,")
print(" )")
print(" ")
print(" return jsonify({")
print(" 'success': True,")
print(" 'result': result.result,")
print(" })")
print()
print("if __name__ == '__main__':")
print(" app.run(port=5000)")
print("```")
print()
# =====================================================
# Part 6: Testing with curl
# =====================================================
log_section("Part 6: Testing")
print("Test with curl:")
print()
print("# Synchronous request")
print("curl -X POST http://localhost:5000/webhook/translate \\")
print(" -H 'Content-Type: application/json' \\")
print(" -d '{\"text\": \"Hello world\", \"targetLang\": \"de\", \"budget\": 1.0}'")
print()
print("# Async request")
print("curl -X POST http://localhost:5000/webhook/translate/async \\")
print(" -H 'Content-Type: application/json' \\")
print(" -d '{\"text\": \"Long document...\", \"callback_url\": \"https://example.com/callback\"}'")
print()
print("# Check job status")
print("curl http://localhost:5000/jobs/<job_id>")
print()
# =====================================================
# Part 7: Production Considerations
# =====================================================
log_section("Part 7: Production")
print("Production deployment considerations:")
print()
print("1. Authentication")
print(" - API keys for webhook access")
print(" - HMAC signature verification")
print(" - Rate limiting per client")
print()
print("2. Reliability")
print(" - Queue async jobs (Redis, RabbitMQ)")
print(" - Retry failed callbacks")
print(" - Dead letter queue for failures")
print()
print("3. Monitoring")
print(" - Request/response logging")
print(" - Job processing metrics")
print(" - Error alerting")
print()
print("4. Scaling")
print(" - Multiple worker processes")
print(" - Load balancer (nginx)")
print(" - Kubernetes deployment")
await stop_provider()
log("🎉", "n8n webhook integration demo complete!")
if __name__ == "__main__":
asyncio.run(main())