signalwire-agents 1.0.12__py3-none-any.whl → 1.0.14__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.
@@ -0,0 +1,2423 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ SignalWire Agent Dokku Deployment Tool
4
+
5
+ CLI tool for deploying SignalWire agents to Dokku with support for:
6
+ - Simple git push deployment
7
+ - Full CI/CD with GitHub Actions
8
+ - Service provisioning (PostgreSQL, Redis)
9
+ - Preview environments for PRs
10
+
11
+ Usage:
12
+ sw-agent-dokku init myagent # Simple mode
13
+ sw-agent-dokku init myagent --cicd # With GitHub Actions CI/CD
14
+ sw-agent-dokku deploy # Deploy current directory
15
+ sw-agent-dokku logs # Tail logs
16
+ sw-agent-dokku config set KEY=value # Set environment variables
17
+ sw-agent-dokku scale web=2 # Scale processes
18
+ """
19
+
20
+ import os
21
+ import sys
22
+ import subprocess
23
+ import secrets
24
+ import argparse
25
+ import shutil
26
+ from pathlib import Path
27
+ from typing import Optional, Dict, List, Any
28
+
29
+
30
+ # =============================================================================
31
+ # ANSI Colors
32
+ # =============================================================================
33
+
34
+ class Colors:
35
+ RED = '\033[0;31m'
36
+ GREEN = '\033[0;32m'
37
+ YELLOW = '\033[1;33m'
38
+ BLUE = '\033[0;34m'
39
+ CYAN = '\033[0;36m'
40
+ MAGENTA = '\033[0;35m'
41
+ BOLD = '\033[1m'
42
+ DIM = '\033[2m'
43
+ NC = '\033[0m'
44
+
45
+
46
+ def print_step(msg: str):
47
+ print(f"{Colors.BLUE}==>{Colors.NC} {msg}")
48
+
49
+
50
+ def print_success(msg: str):
51
+ print(f"{Colors.GREEN}✓{Colors.NC} {msg}")
52
+
53
+
54
+ def print_warning(msg: str):
55
+ print(f"{Colors.YELLOW}!{Colors.NC} {msg}")
56
+
57
+
58
+ def print_error(msg: str):
59
+ print(f"{Colors.RED}✗{Colors.NC} {msg}")
60
+
61
+
62
+ def print_header(msg: str):
63
+ print(f"\n{Colors.BOLD}{Colors.CYAN}{msg}{Colors.NC}")
64
+
65
+
66
+ def prompt(question: str, default: str = "") -> str:
67
+ if default:
68
+ result = input(f"{question} [{default}]: ").strip()
69
+ return result if result else default
70
+ return input(f"{question}: ").strip()
71
+
72
+
73
+ def prompt_yes_no(question: str, default: bool = True) -> bool:
74
+ hint = "Y/n" if default else "y/N"
75
+ result = input(f"{question} [{hint}]: ").strip().lower()
76
+ if not result:
77
+ return default
78
+ return result in ('y', 'yes')
79
+
80
+
81
+ def generate_password(length: int = 32) -> str:
82
+ return secrets.token_urlsafe(length)[:length]
83
+
84
+
85
+ # =============================================================================
86
+ # Templates - Core Files
87
+ # =============================================================================
88
+
89
+ PROCFILE_TEMPLATE = """web: gunicorn app:app --bind 0.0.0.0:$PORT --workers 2 --worker-class uvicorn.workers.UvicornWorker
90
+ """
91
+
92
+ RUNTIME_TEMPLATE = """python-3.11
93
+ """
94
+
95
+ REQUIREMENTS_TEMPLATE = """signalwire-agents>=1.0.13
96
+ gunicorn>=21.0.0
97
+ uvicorn>=0.24.0
98
+ python-dotenv>=1.0.0
99
+ requests>=2.28.0
100
+ """
101
+
102
+ CHECKS_TEMPLATE = """WAIT=5
103
+ TIMEOUT=30
104
+ ATTEMPTS=5
105
+
106
+ /health
107
+ """
108
+
109
+ GITIGNORE_TEMPLATE = """# Environment
110
+ .env
111
+ .venv/
112
+ venv/
113
+ __pycache__/
114
+ *.pyc
115
+ *.pyo
116
+
117
+ # IDE
118
+ .vscode/
119
+ .idea/
120
+ *.swp
121
+ *.swo
122
+
123
+ # Testing
124
+ .pytest_cache/
125
+ .coverage
126
+ htmlcov/
127
+
128
+ # Build
129
+ dist/
130
+ build/
131
+ *.egg-info/
132
+
133
+ # Logs
134
+ *.log
135
+
136
+ # OS
137
+ .DS_Store
138
+ Thumbs.db
139
+ """
140
+
141
+ ENV_EXAMPLE_TEMPLATE = """# SignalWire Agent Configuration
142
+ # =============================================================================
143
+
144
+ # SignalWire Credentials (required for WebRTC calling)
145
+ SIGNALWIRE_SPACE_NAME=your-space
146
+ SIGNALWIRE_PROJECT_ID=your-project-id
147
+ SIGNALWIRE_TOKEN=your-api-token
148
+
149
+ # Public URL for SWML callbacks (required for WebRTC calling)
150
+ # This should be your publicly accessible URL (e.g., ngrok, dokku domain)
151
+ SWML_PROXY_URL_BASE=https://your-app.example.com
152
+
153
+ # Basic Auth for SWML endpoints (recommended)
154
+ SWML_BASIC_AUTH_USER=admin
155
+ SWML_BASIC_AUTH_PASSWORD=your-secure-password
156
+
157
+ # Agent Configuration
158
+ AGENT_NAME={app_name}
159
+
160
+ # App Configuration
161
+ APP_ENV=production
162
+ APP_NAME={app_name}
163
+
164
+ # Optional: External Services
165
+ # DATABASE_URL=postgres://user:pass@host:5432/db
166
+ # REDIS_URL=redis://host:6379
167
+ """
168
+
169
+ APP_TEMPLATE = '''#!/usr/bin/env python3
170
+ """
171
+ {agent_name} - SignalWire AI Agent
172
+
173
+ Deployed to Dokku with automatic health checks and SWAIG support.
174
+ """
175
+
176
+ import os
177
+ from dotenv import load_dotenv
178
+ from signalwire_agents import AgentBase, SwaigFunctionResult
179
+
180
+ # Load environment variables from .env file
181
+ load_dotenv()
182
+
183
+
184
+ class {agent_class}(AgentBase):
185
+ """{agent_name} agent for Dokku deployment."""
186
+
187
+ def __init__(self):
188
+ super().__init__(name="{agent_slug}")
189
+
190
+ self._configure_prompts()
191
+ self.add_language("English", "en-US", "rime.spore")
192
+ self._setup_functions()
193
+
194
+ def _configure_prompts(self):
195
+ self.prompt_add_section(
196
+ "Role",
197
+ "You are a helpful AI assistant deployed on Dokku."
198
+ )
199
+
200
+ self.prompt_add_section(
201
+ "Guidelines",
202
+ bullets=[
203
+ "Be professional and courteous",
204
+ "Ask clarifying questions when needed",
205
+ "Keep responses concise and helpful"
206
+ ]
207
+ )
208
+
209
+ def _setup_functions(self):
210
+ @self.tool(
211
+ description="Get information about a topic",
212
+ parameters={{
213
+ "type": "object",
214
+ "properties": {{
215
+ "topic": {{
216
+ "type": "string",
217
+ "description": "The topic to get information about"
218
+ }}
219
+ }},
220
+ "required": ["topic"]
221
+ }}
222
+ )
223
+ def get_info(args, raw_data):
224
+ topic = args.get("topic", "")
225
+ return SwaigFunctionResult(
226
+ f"Information about {{topic}}: This is a placeholder response."
227
+ )
228
+
229
+ @self.tool(description="Get deployment information")
230
+ def get_deployment_info(args, raw_data):
231
+ app_name = os.getenv("APP_NAME", "unknown")
232
+ app_env = os.getenv("APP_ENV", "unknown")
233
+
234
+ return SwaigFunctionResult(
235
+ f"Running on Dokku. App: {{app_name}}, Environment: {{app_env}}."
236
+ )
237
+
238
+
239
+ # Create agent instance
240
+ agent = {agent_class}()
241
+
242
+ # Expose the FastAPI app for gunicorn/uvicorn
243
+ app = agent.get_app()
244
+
245
+ if __name__ == "__main__":
246
+ agent.run()
247
+ '''
248
+
249
+ APP_TEMPLATE_WITH_WEB = '''#!/usr/bin/env python3
250
+ """
251
+ {agent_name} - SignalWire AI Agent
252
+
253
+ Deployed to Dokku with automatic health checks, SWAIG support, and web interface.
254
+ Includes WebRTC calling support with dynamic token generation.
255
+ """
256
+
257
+ import os
258
+ import time
259
+ from pathlib import Path
260
+ from dotenv import load_dotenv
261
+ import requests
262
+ from starlette.responses import JSONResponse
263
+ from signalwire_agents import AgentBase, AgentServer, SwaigFunctionResult
264
+
265
+ # Load environment variables from .env file
266
+ load_dotenv()
267
+
268
+
269
+ class {agent_class}(AgentBase):
270
+ """{agent_name} agent for Dokku deployment."""
271
+
272
+ def __init__(self):
273
+ super().__init__(name="{agent_slug}", route="/swml")
274
+
275
+ self._configure_prompts()
276
+ self.add_language("English", "en-US", "rime.spore")
277
+ self._setup_functions()
278
+
279
+ def _configure_prompts(self):
280
+ self.prompt_add_section(
281
+ "Role",
282
+ "You are a helpful AI assistant deployed on Dokku."
283
+ )
284
+
285
+ self.prompt_add_section(
286
+ "Guidelines",
287
+ bullets=[
288
+ "Be professional and courteous",
289
+ "Ask clarifying questions when needed",
290
+ "Keep responses concise and helpful"
291
+ ]
292
+ )
293
+
294
+ def _setup_functions(self):
295
+ @self.tool(
296
+ description="Get information about a topic",
297
+ parameters={{
298
+ "type": "object",
299
+ "properties": {{
300
+ "topic": {{
301
+ "type": "string",
302
+ "description": "The topic to get information about"
303
+ }}
304
+ }},
305
+ "required": ["topic"]
306
+ }}
307
+ )
308
+ def get_info(args, raw_data):
309
+ topic = args.get("topic", "")
310
+ return SwaigFunctionResult(
311
+ f"Information about {{topic}}: This is a placeholder response."
312
+ )
313
+
314
+ @self.tool(description="Get deployment information")
315
+ def get_deployment_info(args, raw_data):
316
+ app_name = os.getenv("APP_NAME", "unknown")
317
+ app_env = os.getenv("APP_ENV", "unknown")
318
+
319
+ return SwaigFunctionResult(
320
+ f"Running on Dokku. App: {{app_name}}, Environment: {{app_env}}."
321
+ )
322
+
323
+
324
+ # =============================================================================
325
+ # SignalWire SWML Handler Management
326
+ # =============================================================================
327
+
328
+ def get_signalwire_host():
329
+ """Get the full SignalWire host from space name."""
330
+ space = os.getenv("SIGNALWIRE_SPACE_NAME", "")
331
+ if not space:
332
+ return None
333
+ if "." in space:
334
+ return space
335
+ return f"{{space}}.signalwire.com"
336
+
337
+
338
+ def find_existing_handler(sw_host, auth, agent_name):
339
+ """Find an existing SWML handler by name."""
340
+ try:
341
+ resp = requests.get(
342
+ f"https://{{sw_host}}/api/fabric/resources/external_swml_handlers",
343
+ auth=auth,
344
+ headers={{"Accept": "application/json"}}
345
+ )
346
+ if resp.status_code != 200:
347
+ return None
348
+
349
+ handlers = resp.json().get("data", [])
350
+ for handler in handlers:
351
+ swml_webhook = handler.get("swml_webhook", {{}})
352
+ handler_name = swml_webhook.get("name") or handler.get("display_name")
353
+ if handler_name == agent_name:
354
+ handler_id = handler.get("id")
355
+ handler_url = swml_webhook.get("primary_request_url", "")
356
+ addr_resp = requests.get(
357
+ f"https://{{sw_host}}/api/fabric/resources/external_swml_handlers/{{handler_id}}/addresses",
358
+ auth=auth,
359
+ headers={{"Accept": "application/json"}}
360
+ )
361
+ if addr_resp.status_code == 200:
362
+ addresses = addr_resp.json().get("data", [])
363
+ if addresses:
364
+ return {{
365
+ "id": handler_id,
366
+ "name": handler_name,
367
+ "url": handler_url,
368
+ "address_id": addresses[0]["id"],
369
+ "address": addresses[0]["channels"]["audio"]
370
+ }}
371
+ except Exception as e:
372
+ print(f"Error checking existing handlers: {{e}}")
373
+ return None
374
+
375
+
376
+ # Store SWML handler info
377
+ swml_handler_info = {{"id": None, "address_id": None, "address": None}}
378
+
379
+
380
+ def setup_swml_handler():
381
+ """Set up SWML handler on startup."""
382
+ sw_host = get_signalwire_host()
383
+ project = os.getenv("SIGNALWIRE_PROJECT_ID", "")
384
+ token = os.getenv("SIGNALWIRE_TOKEN", "")
385
+ agent_name = os.getenv("AGENT_NAME", "{agent_slug}")
386
+ proxy_url = os.getenv("SWML_PROXY_URL_BASE", "")
387
+ auth_user = os.getenv("SWML_BASIC_AUTH_USER", "")
388
+ auth_pass = os.getenv("SWML_BASIC_AUTH_PASSWORD", "")
389
+
390
+ if not all([sw_host, project, token]):
391
+ print("SignalWire credentials not configured - skipping SWML handler setup")
392
+ return
393
+
394
+ if not proxy_url:
395
+ print("SWML_PROXY_URL_BASE not set - skipping SWML handler setup")
396
+ return
397
+
398
+ # Build SWML URL with basic auth
399
+ if auth_user and auth_pass and "://" in proxy_url:
400
+ scheme, rest = proxy_url.split("://", 1)
401
+ swml_url = f"{{scheme}}://{{auth_user}}:{{auth_pass}}@{{rest}}/swml"
402
+ else:
403
+ swml_url = proxy_url + "/swml"
404
+
405
+ auth = (project, token)
406
+ headers = {{"Content-Type": "application/json", "Accept": "application/json"}}
407
+
408
+ existing = find_existing_handler(sw_host, auth, agent_name)
409
+ if existing:
410
+ swml_handler_info["id"] = existing["id"]
411
+ swml_handler_info["address_id"] = existing["address_id"]
412
+ swml_handler_info["address"] = existing["address"]
413
+
414
+ if existing.get("url") != swml_url:
415
+ try:
416
+ requests.put(
417
+ f"https://{{sw_host}}/api/fabric/resources/external_swml_handlers/{{existing['id']}}",
418
+ json={{"primary_request_url": swml_url, "primary_request_method": "POST"}},
419
+ auth=auth,
420
+ headers=headers
421
+ )
422
+ print(f"Updated SWML handler: {{existing['name']}}")
423
+ except Exception as e:
424
+ print(f"Failed to update handler URL: {{e}}")
425
+ else:
426
+ print(f"Using existing SWML handler: {{existing['name']}}")
427
+ print(f"Call address: {{existing['address']}}")
428
+ else:
429
+ try:
430
+ handler_resp = requests.post(
431
+ f"https://{{sw_host}}/api/fabric/resources/external_swml_handlers",
432
+ json={{
433
+ "name": agent_name,
434
+ "used_for": "calling",
435
+ "primary_request_url": swml_url,
436
+ "primary_request_method": "POST"
437
+ }},
438
+ auth=auth,
439
+ headers=headers
440
+ )
441
+ handler_resp.raise_for_status()
442
+ handler_id = handler_resp.json().get("id")
443
+ swml_handler_info["id"] = handler_id
444
+
445
+ addr_resp = requests.get(
446
+ f"https://{{sw_host}}/api/fabric/resources/external_swml_handlers/{{handler_id}}/addresses",
447
+ auth=auth,
448
+ headers={{"Accept": "application/json"}}
449
+ )
450
+ addr_resp.raise_for_status()
451
+ addresses = addr_resp.json().get("data", [])
452
+ if addresses:
453
+ swml_handler_info["address_id"] = addresses[0]["id"]
454
+ swml_handler_info["address"] = addresses[0]["channels"]["audio"]
455
+ print(f"Created SWML handler: {{agent_name}}")
456
+ print(f"Call address: {{swml_handler_info['address']}}")
457
+ except Exception as e:
458
+ print(f"Failed to create SWML handler: {{e}}")
459
+
460
+
461
+ # =============================================================================
462
+ # Server Setup
463
+ # =============================================================================
464
+
465
+ server = AgentServer(host="0.0.0.0", port=int(os.getenv("PORT", 3000)))
466
+ server.register({agent_class}())
467
+
468
+ # Serve static files from web/ directory
469
+ web_dir = Path(__file__).parent / "web"
470
+ if web_dir.exists():
471
+ server.serve_static_files(str(web_dir))
472
+
473
+
474
+ # =============================================================================
475
+ # API Endpoints
476
+ # =============================================================================
477
+
478
+ @server.app.get("/get_token")
479
+ def get_token():
480
+ """Get a guest token for WebRTC calls."""
481
+ sw_host = get_signalwire_host()
482
+ project = os.getenv("SIGNALWIRE_PROJECT_ID", "")
483
+ token = os.getenv("SIGNALWIRE_TOKEN", "")
484
+
485
+ if not all([sw_host, project, token]):
486
+ return JSONResponse({{"error": "SignalWire credentials not configured"}}, status_code=500)
487
+
488
+ if not swml_handler_info["address_id"]:
489
+ return JSONResponse({{"error": "SWML handler not configured - check startup logs"}}, status_code=500)
490
+
491
+ auth = (project, token)
492
+ headers = {{"Content-Type": "application/json", "Accept": "application/json"}}
493
+
494
+ try:
495
+ expire_at = int(time.time()) + 3600 * 24 # 24 hours
496
+
497
+ guest_resp = requests.post(
498
+ f"https://{{sw_host}}/api/fabric/guests/tokens",
499
+ json={{
500
+ "allowed_addresses": [swml_handler_info["address_id"]],
501
+ "expire_at": expire_at
502
+ }},
503
+ auth=auth,
504
+ headers=headers
505
+ )
506
+ guest_resp.raise_for_status()
507
+ guest_token = guest_resp.json().get("token", "")
508
+
509
+ return {{
510
+ "token": guest_token,
511
+ "address": swml_handler_info["address"]
512
+ }}
513
+
514
+ except requests.exceptions.RequestException as e:
515
+ print(f"Token request failed: {{e}}")
516
+ return JSONResponse({{"error": str(e)}}, status_code=500)
517
+
518
+
519
+ @server.app.get("/get_credentials")
520
+ def get_credentials():
521
+ """Get basic auth credentials for curl examples."""
522
+ return {{
523
+ "user": os.getenv("SWML_BASIC_AUTH_USER", ""),
524
+ "password": os.getenv("SWML_BASIC_AUTH_PASSWORD", "")
525
+ }}
526
+
527
+
528
+ @server.app.get("/get_resource_info")
529
+ def get_resource_info():
530
+ """Get SWML handler resource info for dashboard link."""
531
+ sw_host = get_signalwire_host()
532
+ space_name = os.getenv("SIGNALWIRE_SPACE_NAME", "")
533
+ return {{
534
+ "space_name": space_name,
535
+ "resource_id": swml_handler_info["id"],
536
+ "dashboard_url": f"https://{{sw_host}}/neon/resources/{{swml_handler_info['id']}}/edit?t=addresses" if sw_host and swml_handler_info["id"] else None
537
+ }}
538
+
539
+
540
+ # =============================================================================
541
+ # Static File Handling
542
+ # =============================================================================
543
+
544
+ from fastapi import Request, HTTPException
545
+ from fastapi.responses import FileResponse, RedirectResponse
546
+ import mimetypes
547
+
548
+ @server.app.api_route("/swml", methods=["GET", "POST"])
549
+ async def swml_redirect():
550
+ return RedirectResponse(url="/swml/", status_code=307)
551
+
552
+ @server.app.get("/{{full_path:path}}")
553
+ async def serve_static(request: Request, full_path: str):
554
+ """Serve static files from web/ directory"""
555
+ if not web_dir.exists():
556
+ raise HTTPException(status_code=404, detail="Not Found")
557
+
558
+ if not full_path or full_path == "/":
559
+ full_path = "index.html"
560
+
561
+ file_path = web_dir / full_path
562
+
563
+ try:
564
+ file_path = file_path.resolve()
565
+ if not str(file_path).startswith(str(web_dir.resolve())):
566
+ raise HTTPException(status_code=404, detail="Not Found")
567
+ except Exception:
568
+ raise HTTPException(status_code=404, detail="Not Found")
569
+
570
+ if file_path.exists() and file_path.is_file():
571
+ media_type, _ = mimetypes.guess_type(str(file_path))
572
+ return FileResponse(file_path, media_type=media_type)
573
+
574
+ if (web_dir / full_path / "index.html").exists():
575
+ return FileResponse(web_dir / full_path / "index.html", media_type="text/html")
576
+
577
+ raise HTTPException(status_code=404, detail="Not Found")
578
+
579
+
580
+ # Set up SWML handler on startup
581
+ setup_swml_handler()
582
+
583
+ # Expose ASGI app for gunicorn
584
+ app = server.app
585
+
586
+ if __name__ == "__main__":
587
+ server.run()
588
+ '''
589
+
590
+ WEB_INDEX_TEMPLATE = '''<!DOCTYPE html>
591
+ <html lang="en">
592
+ <head>
593
+ <meta charset="UTF-8">
594
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
595
+ <title>{agent_name}</title>
596
+ <style>
597
+ * {{ box-sizing: border-box; }}
598
+ body {{
599
+ font-family: system-ui, -apple-system, sans-serif;
600
+ max-width: 900px;
601
+ margin: 0 auto;
602
+ padding: 40px 20px;
603
+ background: #f8f9fa;
604
+ color: #333;
605
+ }}
606
+ h1 {{
607
+ color: #044cf6;
608
+ border-bottom: 3px solid #044cf6;
609
+ padding-bottom: 10px;
610
+ }}
611
+ h2 {{
612
+ color: #333;
613
+ margin-top: 40px;
614
+ border-bottom: 1px solid #ddd;
615
+ padding-bottom: 8px;
616
+ }}
617
+ .status {{
618
+ background: #d4edda;
619
+ border: 1px solid #c3e6cb;
620
+ color: #155724;
621
+ padding: 15px;
622
+ border-radius: 8px;
623
+ margin: 20px 0;
624
+ }}
625
+ .call-section {{
626
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
627
+ border-radius: 12px;
628
+ padding: 30px;
629
+ margin: 20px 0;
630
+ color: white;
631
+ text-align: center;
632
+ }}
633
+ .call-section h2 {{
634
+ color: white;
635
+ border: none;
636
+ margin-top: 0;
637
+ }}
638
+ .call-controls {{
639
+ display: flex;
640
+ gap: 15px;
641
+ justify-content: center;
642
+ align-items: center;
643
+ flex-wrap: wrap;
644
+ }}
645
+ .call-btn {{
646
+ padding: 15px 40px;
647
+ font-size: 18px;
648
+ font-weight: bold;
649
+ border: none;
650
+ border-radius: 8px;
651
+ cursor: pointer;
652
+ transition: all 0.3s ease;
653
+ }}
654
+ .call-btn:disabled {{
655
+ opacity: 0.5;
656
+ cursor: not-allowed;
657
+ }}
658
+ .call-btn.connect {{
659
+ background: #10b981;
660
+ color: white;
661
+ }}
662
+ .call-btn.connect:hover:not(:disabled) {{
663
+ background: #059669;
664
+ transform: translateY(-2px);
665
+ box-shadow: 0 5px 15px rgba(16, 185, 129, 0.4);
666
+ }}
667
+ .call-btn.disconnect {{
668
+ background: #ef4444;
669
+ color: white;
670
+ }}
671
+ .call-btn.disconnect:hover:not(:disabled) {{
672
+ background: #dc2626;
673
+ }}
674
+ .call-status {{
675
+ margin-top: 15px;
676
+ font-size: 14px;
677
+ opacity: 0.9;
678
+ }}
679
+ .destination-input {{
680
+ padding: 12px 15px;
681
+ font-size: 14px;
682
+ border: 2px solid rgba(255,255,255,0.3);
683
+ border-radius: 8px;
684
+ background: rgba(255,255,255,0.1);
685
+ color: white;
686
+ width: 250px;
687
+ }}
688
+ .destination-input::placeholder {{
689
+ color: rgba(255,255,255,0.6);
690
+ }}
691
+ .destination-input:focus {{
692
+ outline: none;
693
+ border-color: rgba(255,255,255,0.6);
694
+ background: rgba(255,255,255,0.2);
695
+ }}
696
+ .endpoint {{
697
+ background: white;
698
+ border: 1px solid #ddd;
699
+ border-radius: 8px;
700
+ padding: 20px;
701
+ margin: 15px 0;
702
+ }}
703
+ .endpoint h3 {{
704
+ margin-top: 0;
705
+ color: #044cf6;
706
+ }}
707
+ .method {{
708
+ display: inline-block;
709
+ padding: 4px 10px;
710
+ border-radius: 4px;
711
+ font-weight: bold;
712
+ font-size: 12px;
713
+ margin-right: 10px;
714
+ }}
715
+ .method.get {{ background: #61affe; color: white; }}
716
+ .method.post {{ background: #49cc90; color: white; }}
717
+ .path {{
718
+ font-family: monospace;
719
+ font-size: 16px;
720
+ color: #333;
721
+ }}
722
+ code, pre {{
723
+ font-family: 'SF Mono', Monaco, 'Courier New', monospace;
724
+ }}
725
+ code {{
726
+ background: #e9ecef;
727
+ padding: 2px 6px;
728
+ border-radius: 4px;
729
+ font-size: 14px;
730
+ }}
731
+ pre {{
732
+ background: #1e1e1e;
733
+ color: #d4d4d4;
734
+ padding: 15px;
735
+ border-radius: 8px;
736
+ overflow-x: auto;
737
+ font-size: 13px;
738
+ line-height: 1.5;
739
+ margin: 0;
740
+ }}
741
+ pre .comment {{ color: #6a9955; }}
742
+ .tabs {{
743
+ display: flex;
744
+ gap: 0;
745
+ margin-top: 15px;
746
+ }}
747
+ .tab {{
748
+ padding: 8px 16px;
749
+ background: #e9ecef;
750
+ border: 1px solid #ddd;
751
+ border-bottom: none;
752
+ border-radius: 8px 8px 0 0;
753
+ cursor: pointer;
754
+ font-size: 13px;
755
+ font-weight: 500;
756
+ color: #666;
757
+ transition: all 0.2s;
758
+ }}
759
+ .tab:hover {{ background: #dee2e6; }}
760
+ .tab.active {{
761
+ background: #1e1e1e;
762
+ color: #d4d4d4;
763
+ border-color: #1e1e1e;
764
+ }}
765
+ .tab-content {{
766
+ display: none;
767
+ border-radius: 0 8px 8px 8px;
768
+ }}
769
+ .tab-content.active {{ display: block; }}
770
+ .browser-panel {{
771
+ background: #f8f9fa;
772
+ border: 1px solid #ddd;
773
+ border-radius: 0 8px 8px 8px;
774
+ padding: 15px;
775
+ }}
776
+ .try-btn {{
777
+ padding: 10px 20px;
778
+ background: #044cf6;
779
+ color: white;
780
+ border: none;
781
+ border-radius: 6px;
782
+ cursor: pointer;
783
+ font-weight: 500;
784
+ font-size: 14px;
785
+ transition: all 0.2s;
786
+ }}
787
+ .try-btn:hover {{ background: #0339c2; }}
788
+ .try-btn:disabled {{
789
+ background: #ccc;
790
+ cursor: not-allowed;
791
+ }}
792
+ .response-area {{
793
+ margin-top: 15px;
794
+ display: none;
795
+ }}
796
+ .response-area.visible {{ display: block; }}
797
+ .response-header {{
798
+ display: flex;
799
+ justify-content: space-between;
800
+ align-items: center;
801
+ margin-bottom: 8px;
802
+ }}
803
+ .response-status {{
804
+ font-size: 13px;
805
+ font-weight: 500;
806
+ }}
807
+ .response-status.success {{ color: #10b981; }}
808
+ .response-status.error {{ color: #ef4444; }}
809
+ .response-time {{
810
+ font-size: 12px;
811
+ color: #666;
812
+ }}
813
+ .response-body {{
814
+ background: #1e1e1e;
815
+ color: #d4d4d4;
816
+ padding: 15px;
817
+ border-radius: 8px;
818
+ font-family: 'SF Mono', Monaco, monospace;
819
+ font-size: 12px;
820
+ max-height: 300px;
821
+ overflow: auto;
822
+ white-space: pre-wrap;
823
+ word-break: break-word;
824
+ }}
825
+ .curl-panel {{
826
+ position: relative;
827
+ }}
828
+ .copy-btn {{
829
+ position: absolute;
830
+ top: 10px;
831
+ right: 10px;
832
+ padding: 5px 10px;
833
+ background: rgba(255,255,255,0.1);
834
+ color: #999;
835
+ border: 1px solid rgba(255,255,255,0.2);
836
+ border-radius: 4px;
837
+ cursor: pointer;
838
+ font-size: 11px;
839
+ transition: all 0.2s;
840
+ }}
841
+ .copy-btn:hover {{
842
+ background: rgba(255,255,255,0.2);
843
+ color: #fff;
844
+ }}
845
+ .audio-settings {{
846
+ display: flex;
847
+ gap: 20px;
848
+ justify-content: center;
849
+ margin-top: 15px;
850
+ flex-wrap: wrap;
851
+ }}
852
+ .audio-setting {{
853
+ display: flex;
854
+ align-items: center;
855
+ gap: 8px;
856
+ font-size: 13px;
857
+ color: rgba(255,255,255,0.9);
858
+ }}
859
+ .audio-setting input[type="checkbox"] {{
860
+ width: 18px;
861
+ height: 18px;
862
+ cursor: pointer;
863
+ }}
864
+ .audio-setting label {{
865
+ cursor: pointer;
866
+ }}
867
+ .phone-info {{
868
+ background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
869
+ border: 1px solid #044cf6;
870
+ border-radius: 12px;
871
+ padding: 20px 25px;
872
+ margin: 30px 0;
873
+ max-width: 800px;
874
+ margin-left: auto;
875
+ margin-right: auto;
876
+ }}
877
+ .phone-info h3 {{
878
+ margin: 0 0 10px 0;
879
+ color: #fff;
880
+ font-size: 16px;
881
+ }}
882
+ .phone-info p {{
883
+ margin: 0 0 15px 0;
884
+ color: rgba(255,255,255,0.8);
885
+ font-size: 14px;
886
+ line-height: 1.5;
887
+ }}
888
+ .phone-info a {{
889
+ display: inline-block;
890
+ padding: 10px 20px;
891
+ background: #044cf6;
892
+ color: white;
893
+ text-decoration: none;
894
+ border-radius: 6px;
895
+ font-weight: 500;
896
+ font-size: 14px;
897
+ transition: all 0.2s;
898
+ }}
899
+ .phone-info a:hover {{
900
+ background: #0339c2;
901
+ }}
902
+ .phone-info.hidden {{
903
+ display: none;
904
+ }}
905
+ .footer {{
906
+ margin-top: 50px;
907
+ padding-top: 20px;
908
+ border-top: 1px solid #ddd;
909
+ color: #666;
910
+ font-size: 14px;
911
+ }}
912
+ </style>
913
+ </head>
914
+ <body>
915
+ <h1>{agent_name}</h1>
916
+
917
+ <div class="status">
918
+ Your agent is running and ready to receive calls!
919
+ </div>
920
+
921
+ <div class="call-section">
922
+ <h2>Call Your Agent</h2>
923
+ <p>Test your agent directly from the browser using WebRTC.</p>
924
+ <div class="call-controls">
925
+ <input type="text" id="destination" class="destination-input" placeholder="Address will be auto-filled" />
926
+ <button id="connectBtn" class="call-btn connect">Call Agent</button>
927
+ <button id="disconnectBtn" class="call-btn disconnect" disabled>Hang Up</button>
928
+ </div>
929
+ <div class="audio-settings">
930
+ <div class="audio-setting">
931
+ <input type="checkbox" id="echoCancellation" checked>
932
+ <label for="echoCancellation">Echo Cancellation</label>
933
+ </div>
934
+ <div class="audio-setting">
935
+ <input type="checkbox" id="noiseSuppression">
936
+ <label for="noiseSuppression">Noise Suppression</label>
937
+ </div>
938
+ <div class="audio-setting">
939
+ <input type="checkbox" id="autoGainControl">
940
+ <label for="autoGainControl">Auto Gain Control</label>
941
+ </div>
942
+ </div>
943
+ <div id="callStatus" class="call-status"></div>
944
+ </div>
945
+
946
+ <div id="phoneInfo" class="phone-info hidden">
947
+ <h3>Want to call from a phone number?</h3>
948
+ <p>You can assign a SignalWire phone number to this agent. Click below to add a number in the dashboard.</p>
949
+ <a id="dashboardLink" href="#" target="_blank">Add Phone Number in Dashboard</a>
950
+ </div>
951
+
952
+ <h2>Endpoints</h2>
953
+
954
+ <div class="endpoint">
955
+ <h3><span class="method post">POST</span> <span class="path">/swml</span></h3>
956
+ <p>Main SWML endpoint for SignalWire to fetch agent configuration.</p>
957
+ <div class="tabs">
958
+ <div class="tab active" onclick="switchTab(this, 'swml-browser')">Browser</div>
959
+ <div class="tab" onclick="switchTab(this, 'swml-curl')">curl</div>
960
+ </div>
961
+ <div id="swml-browser" class="tab-content active">
962
+ <div class="browser-panel">
963
+ <button class="try-btn" onclick="tryEndpoint('POST', '/swml', {{}}, 'swml-response', true)">Try it</button>
964
+ <div id="swml-response" class="response-area"></div>
965
+ </div>
966
+ </div>
967
+ <div id="swml-curl" class="tab-content">
968
+ <div class="curl-panel">
969
+ <button class="copy-btn" onclick="copyCode(this)">Copy</button>
970
+ <pre><span class="comment"># Get the SWML configuration</span>
971
+ curl -X POST <span class="base-url"></span>/swml \\
972
+ -u <span class="auth-creds"></span> \\
973
+ -H "Content-Type: application/json" \\
974
+ -d '{{}}'</pre>
975
+ </div>
976
+ </div>
977
+ </div>
978
+
979
+ <div class="endpoint">
980
+ <h3><span class="method get">GET</span> <span class="path">/get_token</span></h3>
981
+ <p>Get a guest token for WebRTC calls. Returns a token and call address.</p>
982
+ <div class="tabs">
983
+ <div class="tab active" onclick="switchTab(this, 'token-browser')">Browser</div>
984
+ <div class="tab" onclick="switchTab(this, 'token-curl')">curl</div>
985
+ </div>
986
+ <div id="token-browser" class="tab-content active">
987
+ <div class="browser-panel">
988
+ <button class="try-btn" onclick="tryEndpoint('GET', '/get_token', null, 'token-response')">Try it</button>
989
+ <div id="token-response" class="response-area"></div>
990
+ </div>
991
+ </div>
992
+ <div id="token-curl" class="tab-content">
993
+ <div class="curl-panel">
994
+ <button class="copy-btn" onclick="copyCode(this)">Copy</button>
995
+ <pre><span class="comment"># Get a guest token</span>
996
+ curl <span class="base-url"></span>/get_token</pre>
997
+ </div>
998
+ </div>
999
+ </div>
1000
+
1001
+ <div class="endpoint">
1002
+ <h3><span class="method post">POST</span> <span class="path">/swml/swaig/</span></h3>
1003
+ <p>SWAIG function endpoint. Test your agent's functions.</p>
1004
+ <div class="tabs">
1005
+ <div class="tab active" onclick="switchTab(this, 'swaig-browser')">Browser</div>
1006
+ <div class="tab" onclick="switchTab(this, 'swaig-curl')">curl</div>
1007
+ </div>
1008
+ <div id="swaig-browser" class="tab-content active">
1009
+ <div class="browser-panel">
1010
+ <button class="try-btn" onclick="tryEndpoint('POST', '/swml/swaig/', {{function: 'get_info', argument: {{parsed: [{{topic: 'SignalWire'}}]}}}}, 'swaig-response', true)">Try it</button>
1011
+ <div id="swaig-response" class="response-area"></div>
1012
+ </div>
1013
+ </div>
1014
+ <div id="swaig-curl" class="tab-content">
1015
+ <div class="curl-panel">
1016
+ <button class="copy-btn" onclick="copyCode(this)">Copy</button>
1017
+ <pre><span class="comment"># Call a SWAIG function</span>
1018
+ curl -X POST <span class="base-url"></span>/swml/swaig/ \\
1019
+ -u <span class="auth-creds"></span> \\
1020
+ -H "Content-Type: application/json" \\
1021
+ -d '{{"function": "get_info", "argument": {{"parsed": [{{"topic": "SignalWire"}}]}}}}'</pre>
1022
+ </div>
1023
+ </div>
1024
+ </div>
1025
+
1026
+ <div class="endpoint">
1027
+ <h3><span class="method get">GET</span> <span class="path">/health</span></h3>
1028
+ <p>Health check endpoint for load balancers and monitoring.</p>
1029
+ </div>
1030
+
1031
+ <div class="footer">
1032
+ Powered by <a href="https://signalwire.com">SignalWire</a> and the
1033
+ <a href="https://github.com/signalwire/signalwire-agents">SignalWire Agents SDK</a>
1034
+ </div>
1035
+
1036
+ <script src="https://cdn.signalwire.com/@signalwire/client"></script>
1037
+ <script>
1038
+ let authCreds = null;
1039
+
1040
+ function switchTab(tabEl, contentId) {{
1041
+ const endpoint = tabEl.closest('.endpoint');
1042
+ endpoint.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
1043
+ endpoint.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
1044
+ tabEl.classList.add('active');
1045
+ document.getElementById(contentId).classList.add('active');
1046
+ }}
1047
+
1048
+ function copyCode(btn) {{
1049
+ const pre = btn.parentElement.querySelector('pre');
1050
+ const text = pre.textContent;
1051
+ navigator.clipboard.writeText(text).then(() => {{
1052
+ const orig = btn.textContent;
1053
+ btn.textContent = 'Copied!';
1054
+ setTimeout(() => btn.textContent = orig, 1500);
1055
+ }});
1056
+ }}
1057
+
1058
+ async function tryEndpoint(method, path, body, responseId, requiresAuth) {{
1059
+ const responseArea = document.getElementById(responseId);
1060
+ responseArea.classList.add('visible');
1061
+ responseArea.innerHTML = '<div class="response-status">Loading...</div>';
1062
+
1063
+ const startTime = performance.now();
1064
+ try {{
1065
+ const options = {{
1066
+ method: method,
1067
+ headers: {{}}
1068
+ }};
1069
+ if (body) {{
1070
+ options.headers['Content-Type'] = 'application/json';
1071
+ options.body = JSON.stringify(body);
1072
+ }}
1073
+ if (requiresAuth && authCreds) {{
1074
+ options.headers['Authorization'] = 'Basic ' + btoa(authCreds.user + ':' + authCreds.password);
1075
+ }}
1076
+
1077
+ const resp = await fetch(path, options);
1078
+ const endTime = performance.now();
1079
+ const duration = Math.round(endTime - startTime);
1080
+
1081
+ let data;
1082
+ const contentType = resp.headers.get('content-type') || '';
1083
+ if (contentType.includes('json')) {{
1084
+ data = await resp.json();
1085
+ data = JSON.stringify(data, null, 2);
1086
+ }} else {{
1087
+ data = await resp.text();
1088
+ }}
1089
+
1090
+ const statusClass = resp.ok ? 'success' : 'error';
1091
+ responseArea.innerHTML = `
1092
+ <div class="response-header">
1093
+ <span class="response-status ${{statusClass}}">${{resp.status}} ${{resp.statusText}}</span>
1094
+ <span class="response-time">${{duration}}ms</span>
1095
+ </div>
1096
+ <div class="response-body">${{escapeHtml(data)}}</div>
1097
+ `;
1098
+ }} catch (err) {{
1099
+ responseArea.innerHTML = `
1100
+ <div class="response-header">
1101
+ <span class="response-status error">Error</span>
1102
+ </div>
1103
+ <div class="response-body">${{escapeHtml(err.message)}}</div>
1104
+ `;
1105
+ }}
1106
+ }}
1107
+
1108
+ function escapeHtml(text) {{
1109
+ const div = document.createElement('div');
1110
+ div.textContent = text;
1111
+ return div.innerHTML;
1112
+ }}
1113
+
1114
+ // WebRTC calling
1115
+ let client = null;
1116
+ let roomSession = null;
1117
+
1118
+ const connectBtn = document.getElementById('connectBtn');
1119
+ const disconnectBtn = document.getElementById('disconnectBtn');
1120
+ const destinationInput = document.getElementById('destination');
1121
+ const callStatus = document.getElementById('callStatus');
1122
+
1123
+ function updateCallStatus(message) {{
1124
+ callStatus.textContent = message;
1125
+ }}
1126
+
1127
+ async function connect() {{
1128
+ try {{
1129
+ connectBtn.disabled = true;
1130
+ updateCallStatus('Getting token...');
1131
+
1132
+ const tokenResp = await fetch('/get_token');
1133
+ const tokenData = await tokenResp.json();
1134
+
1135
+ if (tokenData.error) {{
1136
+ throw new Error(tokenData.error);
1137
+ }}
1138
+
1139
+ if (tokenData.address) {{
1140
+ destinationInput.value = tokenData.address;
1141
+ }}
1142
+
1143
+ updateCallStatus('Connecting...');
1144
+
1145
+ client = await window.SignalWire.SignalWire({{
1146
+ token: tokenData.token
1147
+ }});
1148
+
1149
+ const destination = tokenData.address || destinationInput.value;
1150
+ roomSession = await client.dial({{
1151
+ to: destination,
1152
+ audio: {{
1153
+ echoCancellation: document.getElementById('echoCancellation').checked,
1154
+ noiseSuppression: document.getElementById('noiseSuppression').checked,
1155
+ autoGainControl: document.getElementById('autoGainControl').checked
1156
+ }},
1157
+ video: false
1158
+ }});
1159
+
1160
+ roomSession.on('call.joined', () => {{
1161
+ updateCallStatus('Connected');
1162
+ disconnectBtn.disabled = false;
1163
+ }});
1164
+
1165
+ roomSession.on('call.left', () => {{
1166
+ updateCallStatus('Call ended');
1167
+ cleanup();
1168
+ }});
1169
+
1170
+ roomSession.on('destroy', () => {{
1171
+ updateCallStatus('Call ended');
1172
+ cleanup();
1173
+ }});
1174
+
1175
+ await roomSession.start();
1176
+
1177
+ }} catch (err) {{
1178
+ console.error('Connection error:', err);
1179
+ updateCallStatus('Error: ' + err.message);
1180
+ cleanup();
1181
+ }}
1182
+ }}
1183
+
1184
+ async function disconnect() {{
1185
+ try {{
1186
+ if (roomSession) {{
1187
+ await roomSession.hangup();
1188
+ }}
1189
+ }} catch (err) {{
1190
+ console.error('Disconnect error:', err);
1191
+ }}
1192
+ cleanup();
1193
+ }}
1194
+
1195
+ function cleanup() {{
1196
+ connectBtn.disabled = false;
1197
+ disconnectBtn.disabled = true;
1198
+ roomSession = null;
1199
+ client = null;
1200
+ }}
1201
+
1202
+ connectBtn.addEventListener('click', connect);
1203
+ disconnectBtn.addEventListener('click', disconnect);
1204
+
1205
+ // Initialize on load
1206
+ document.addEventListener('DOMContentLoaded', async function() {{
1207
+ const baseUrl = window.location.origin;
1208
+ document.querySelectorAll('.base-url').forEach(function(el) {{
1209
+ el.textContent = baseUrl;
1210
+ }});
1211
+
1212
+ // Fetch auth credentials
1213
+ try {{
1214
+ const credsResp = await fetch('/get_credentials');
1215
+ if (credsResp.ok) {{
1216
+ authCreds = await credsResp.json();
1217
+ document.querySelectorAll('.auth-creds').forEach(function(el) {{
1218
+ el.textContent = authCreds.user + ':' + authCreds.password;
1219
+ }});
1220
+ }}
1221
+ }} catch (e) {{
1222
+ document.querySelectorAll('.auth-creds').forEach(function(el) {{
1223
+ el.textContent = 'user:password';
1224
+ }});
1225
+ }}
1226
+
1227
+ // Fetch resource info for dashboard link
1228
+ try {{
1229
+ const resourceResp = await fetch('/get_resource_info');
1230
+ if (resourceResp.ok) {{
1231
+ const resourceInfo = await resourceResp.json();
1232
+ if (resourceInfo.dashboard_url) {{
1233
+ document.getElementById('dashboardLink').href = resourceInfo.dashboard_url;
1234
+ document.getElementById('phoneInfo').classList.remove('hidden');
1235
+ }}
1236
+ }}
1237
+ }} catch (e) {{
1238
+ console.log('Could not fetch resource info:', e);
1239
+ }}
1240
+ }});
1241
+ </script>
1242
+ </body>
1243
+ </html>
1244
+ '''
1245
+
1246
+ APP_JSON_TEMPLATE = '''{{
1247
+ "name": "{app_name}",
1248
+ "description": "SignalWire AI Agent with WebRTC calling support",
1249
+ "keywords": ["signalwire", "ai", "agent", "python", "webrtc"],
1250
+ "env": {{
1251
+ "APP_ENV": {{
1252
+ "description": "Application environment",
1253
+ "value": "production"
1254
+ }},
1255
+ "AGENT_NAME": {{
1256
+ "description": "Name for the SWML handler resource",
1257
+ "value": "{app_name}"
1258
+ }},
1259
+ "SIGNALWIRE_SPACE_NAME": {{
1260
+ "description": "SignalWire space name (e.g., 'myspace' or 'myspace.signalwire.com')",
1261
+ "required": true
1262
+ }},
1263
+ "SIGNALWIRE_PROJECT_ID": {{
1264
+ "description": "SignalWire project ID",
1265
+ "required": true
1266
+ }},
1267
+ "SIGNALWIRE_TOKEN": {{
1268
+ "description": "SignalWire API token",
1269
+ "required": true
1270
+ }},
1271
+ "SWML_PROXY_URL_BASE": {{
1272
+ "description": "Public URL base for SWML callbacks (e.g., 'https://myapp.example.com')",
1273
+ "required": true
1274
+ }},
1275
+ "SWML_BASIC_AUTH_USER": {{
1276
+ "description": "Basic auth username for SWML endpoints",
1277
+ "required": true
1278
+ }},
1279
+ "SWML_BASIC_AUTH_PASSWORD": {{
1280
+ "description": "Basic auth password for SWML endpoints",
1281
+ "required": true
1282
+ }}
1283
+ }},
1284
+ "formation": {{
1285
+ "web": {{
1286
+ "quantity": 1
1287
+ }}
1288
+ }},
1289
+ "buildpacks": [
1290
+ {{
1291
+ "url": "heroku/python"
1292
+ }}
1293
+ ],
1294
+ "healthchecks": {{
1295
+ "web": [
1296
+ {{
1297
+ "type": "startup",
1298
+ "name": "port listening",
1299
+ "listening": true,
1300
+ "attempts": 10,
1301
+ "wait": 5,
1302
+ "timeout": 60
1303
+ }}
1304
+ ]
1305
+ }}
1306
+ }}
1307
+ '''
1308
+
1309
+ # =============================================================================
1310
+ # Templates - Simple Mode
1311
+ # =============================================================================
1312
+
1313
+ DEPLOY_SCRIPT_TEMPLATE = '''#!/bin/bash
1314
+ # Dokku deployment helper for {app_name}
1315
+ set -e
1316
+
1317
+ APP_NAME="${{1:-{app_name}}}"
1318
+ DOKKU_HOST="${{2:-{dokku_host}}}"
1319
+
1320
+ echo "═══════════════════════════════════════════════════════════"
1321
+ echo " Deploying $APP_NAME to $DOKKU_HOST"
1322
+ echo "═══════════════════════════════════════════════════════════"
1323
+
1324
+ # Initialize git if needed
1325
+ if [ ! -d .git ]; then
1326
+ echo "→ Initializing git repository..."
1327
+ git init
1328
+ git add .
1329
+ git commit -m "Initial commit"
1330
+ fi
1331
+
1332
+ # Create app if it doesn't exist
1333
+ echo "→ Creating app (if not exists)..."
1334
+ ssh dokku@$DOKKU_HOST apps:create $APP_NAME 2>/dev/null || true
1335
+
1336
+ # Set environment variables
1337
+ echo "→ Setting environment variables..."
1338
+ AUTH_PASS=$(openssl rand -base64 24 | tr -d '/+=' | head -c 24)
1339
+ ssh dokku@$DOKKU_HOST config:set --no-restart $APP_NAME \\
1340
+ APP_ENV=production \\
1341
+ APP_NAME=$APP_NAME \\
1342
+ SWML_BASIC_AUTH_USER=admin \\
1343
+ SWML_BASIC_AUTH_PASSWORD=$AUTH_PASS
1344
+
1345
+ # Add dokku remote
1346
+ echo "→ Configuring git remote..."
1347
+ git remote add dokku dokku@$DOKKU_HOST:$APP_NAME 2>/dev/null || \\
1348
+ git remote set-url dokku dokku@$DOKKU_HOST:$APP_NAME
1349
+
1350
+ # Deploy
1351
+ echo "→ Pushing to Dokku..."
1352
+ git push dokku main --force
1353
+
1354
+ # Enable SSL
1355
+ echo "→ Enabling Let's Encrypt SSL..."
1356
+ ssh dokku@$DOKKU_HOST letsencrypt:enable $APP_NAME 2>/dev/null || \\
1357
+ echo " (SSL setup may require manual configuration)"
1358
+
1359
+ echo ""
1360
+ echo "═══════════════════════════════════════════════════════════"
1361
+ echo " ✅ Deployment complete!"
1362
+ echo ""
1363
+ echo " 🌐 URL: https://$APP_NAME.$DOKKU_HOST"
1364
+ echo " 🔑 Auth: admin / $AUTH_PASS"
1365
+ echo ""
1366
+ echo " Configure SignalWire phone number SWML URL to:"
1367
+ echo " https://admin:$AUTH_PASS@$APP_NAME.$DOKKU_HOST/{route}"
1368
+ echo "═══════════════════════════════════════════════════════════"
1369
+ '''
1370
+
1371
+ README_SIMPLE_TEMPLATE = '''# {app_name}
1372
+
1373
+ A SignalWire AI Agent deployed to Dokku.
1374
+
1375
+ ## Quick Deploy
1376
+
1377
+ ```bash
1378
+ ./deploy.sh {app_name} {dokku_host}
1379
+ ```
1380
+
1381
+ ## Manual Deployment
1382
+
1383
+ 1. **Create the app:**
1384
+ ```bash
1385
+ ssh dokku@{dokku_host} apps:create {app_name}
1386
+ ```
1387
+
1388
+ 2. **Set environment variables:**
1389
+ ```bash
1390
+ ssh dokku@{dokku_host} config:set {app_name} \\
1391
+ SWML_BASIC_AUTH_USER=admin \\
1392
+ SWML_BASIC_AUTH_PASSWORD=secure-password \\
1393
+ APP_ENV=production
1394
+ ```
1395
+
1396
+ 3. **Add git remote and deploy:**
1397
+ ```bash
1398
+ git remote add dokku dokku@{dokku_host}:{app_name}
1399
+ git push dokku main
1400
+ ```
1401
+
1402
+ 4. **Enable SSL:**
1403
+ ```bash
1404
+ ssh dokku@{dokku_host} letsencrypt:enable {app_name}
1405
+ ```
1406
+
1407
+ ## Usage
1408
+
1409
+ Your agent is available at: `https://{app_name}.{dokku_host_domain}`
1410
+
1411
+ Configure your SignalWire phone number:
1412
+ - **SWML URL:** `https://{app_name}.{dokku_host_domain}/{route}`
1413
+ - **Auth:** Basic auth with your configured credentials
1414
+
1415
+ ## Useful Commands
1416
+
1417
+ ```bash
1418
+ # View logs
1419
+ ssh dokku@{dokku_host} logs {app_name} -t
1420
+
1421
+ # Restart app
1422
+ ssh dokku@{dokku_host} ps:restart {app_name}
1423
+
1424
+ # View environment variables
1425
+ ssh dokku@{dokku_host} config:show {app_name}
1426
+
1427
+ # Scale workers
1428
+ ssh dokku@{dokku_host} ps:scale {app_name} web=2
1429
+
1430
+ # Rollback to previous release
1431
+ ssh dokku@{dokku_host} releases:rollback {app_name}
1432
+ ```
1433
+
1434
+ ## Local Development
1435
+
1436
+ ```bash
1437
+ pip install -r requirements.txt
1438
+ uvicorn app:app --reload --port 8080
1439
+ ```
1440
+
1441
+ Test with swaig-test:
1442
+ ```bash
1443
+ swaig-test app.py --list-tools
1444
+ ```
1445
+ '''
1446
+
1447
+ # =============================================================================
1448
+ # Templates - CI/CD Mode
1449
+ # =============================================================================
1450
+
1451
+ DEPLOY_WORKFLOW_TEMPLATE = '''# Auto-deploy to Dokku on push
1452
+ name: Deploy
1453
+
1454
+ on:
1455
+ workflow_dispatch:
1456
+ push:
1457
+ branches: [main, staging, develop]
1458
+
1459
+ concurrency:
1460
+ group: deploy-${{{{ github.ref }}}}
1461
+ cancel-in-progress: true
1462
+
1463
+ jobs:
1464
+ deploy:
1465
+ runs-on: ubuntu-latest
1466
+ environment: ${{{{ github.ref_name == 'main' && 'production' || github.ref_name == 'staging' && 'staging' || 'development' }}}}
1467
+ env:
1468
+ BASE_APP_NAME: ${{{{ github.event.repository.name }}}}
1469
+ steps:
1470
+ - uses: actions/checkout@v4
1471
+ with:
1472
+ fetch-depth: 0
1473
+
1474
+ - name: Set variables
1475
+ id: vars
1476
+ run: |
1477
+ BRANCH="${{GITHUB_REF#refs/heads/}}"
1478
+ case "$BRANCH" in
1479
+ main) APP="${{BASE_APP_NAME}}"; ENV="production" ;;
1480
+ staging) APP="${{BASE_APP_NAME}}-staging"; ENV="staging" ;;
1481
+ develop) APP="${{BASE_APP_NAME}}-dev"; ENV="development" ;;
1482
+ *) APP="${{BASE_APP_NAME}}"; ENV="production" ;;
1483
+ esac
1484
+ echo "app_name=$APP" >> $GITHUB_OUTPUT
1485
+ echo "environment=$ENV" >> $GITHUB_OUTPUT
1486
+
1487
+ - name: Setup SSH
1488
+ run: |
1489
+ mkdir -p ~/.ssh && chmod 700 ~/.ssh
1490
+ echo "${{{{ secrets.DOKKU_SSH_PRIVATE_KEY }}}}" > ~/.ssh/key && chmod 600 ~/.ssh/key
1491
+ ssh-keyscan -H ${{{{ secrets.DOKKU_HOST }}}} >> ~/.ssh/known_hosts
1492
+ echo -e "Host dokku\\n HostName ${{{{ secrets.DOKKU_HOST }}}}\\n User dokku\\n IdentityFile ~/.ssh/key" > ~/.ssh/config
1493
+
1494
+ - name: Create app
1495
+ run: |
1496
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1497
+ ssh dokku apps:exists $APP_NAME 2>/dev/null || ssh dokku apps:create $APP_NAME
1498
+
1499
+ - name: Unlock app
1500
+ run: |
1501
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1502
+ ssh dokku apps:unlock $APP_NAME 2>/dev/null || true
1503
+
1504
+ - name: Configure env
1505
+ run: |
1506
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1507
+ DOMAIN="${{APP_NAME}}.${{{{ secrets.BASE_DOMAIN }}}}"
1508
+ ssh dokku config:set --no-restart $APP_NAME \\
1509
+ APP_ENV="${{{{ steps.vars.outputs.environment }}}}" \\
1510
+ APP_URL="https://${{DOMAIN}}" \\
1511
+ SWML_BASIC_AUTH_USER="${{{{ secrets.SWML_BASIC_AUTH_USER }}}}" \\
1512
+ SWML_BASIC_AUTH_PASSWORD="${{{{ secrets.SWML_BASIC_AUTH_PASSWORD }}}}"
1513
+
1514
+ - name: Deploy
1515
+ run: |
1516
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1517
+ git remote add dokku dokku@${{{{ secrets.DOKKU_HOST }}}}:$APP_NAME 2>/dev/null || true
1518
+ GIT_SSH_COMMAND="ssh -i ~/.ssh/key" git push dokku HEAD:main -f
1519
+
1520
+ - name: Configure domain
1521
+ run: |
1522
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1523
+ DOMAIN="${{APP_NAME}}.${{{{ secrets.BASE_DOMAIN }}}}"
1524
+ ssh dokku domains:clear $APP_NAME 2>/dev/null || true
1525
+ ssh dokku domains:add $APP_NAME $DOMAIN
1526
+
1527
+ - name: SSL
1528
+ run: |
1529
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1530
+ echo "Checking SSL status..."
1531
+ SSL_STATUS=$(ssh dokku letsencrypt:active $APP_NAME 2>/dev/null || echo "error")
1532
+ echo "SSL status: $SSL_STATUS"
1533
+ if [ "$SSL_STATUS" = "true" ]; then
1534
+ echo "SSL already active"
1535
+ else
1536
+ echo "Enabling SSL..."
1537
+ ssh dokku letsencrypt:enable $APP_NAME 2>&1 || echo "SSL enable failed"
1538
+ fi
1539
+
1540
+ - name: Verify
1541
+ id: verify
1542
+ run: |
1543
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1544
+ DOMAIN="${{APP_NAME}}.${{{{ secrets.BASE_DOMAIN }}}}"
1545
+ sleep 10
1546
+ if curl -sf "https://${{DOMAIN}}/health"; then
1547
+ echo "HTTPS OK: https://${{DOMAIN}}"
1548
+ echo "status=success" >> $GITHUB_OUTPUT
1549
+ echo "url=https://${{DOMAIN}}" >> $GITHUB_OUTPUT
1550
+ elif curl -sf "http://${{DOMAIN}}/health"; then
1551
+ echo "HTTP only: http://${{DOMAIN}}"
1552
+ echo "status=success" >> $GITHUB_OUTPUT
1553
+ echo "url=http://${{DOMAIN}}" >> $GITHUB_OUTPUT
1554
+ else
1555
+ echo "Check logs"
1556
+ echo "status=failed" >> $GITHUB_OUTPUT
1557
+ fi
1558
+
1559
+ - name: Notify Slack
1560
+ if: always()
1561
+ env:
1562
+ SLACK_WEBHOOK_URL: ${{{{ secrets.SLACK_WEBHOOK_URL }}}}
1563
+ run: |
1564
+ [ -z "$SLACK_WEBHOOK_URL" ] && exit 0
1565
+ APP_NAME="${{{{ steps.vars.outputs.app_name }}}}"
1566
+ DOMAIN="${{APP_NAME}}.${{{{ secrets.BASE_DOMAIN }}}}"
1567
+ if [ "${{{{ steps.verify.outputs.status }}}}" == "success" ]; then
1568
+ COLOR="good"
1569
+ STATUS="✅ Deployed"
1570
+ else
1571
+ COLOR="danger"
1572
+ STATUS="❌ Deploy failed"
1573
+ fi
1574
+ curl -X POST -H 'Content-type: application/json' \\
1575
+ --data "{{
1576
+ \\"attachments\\": [{{
1577
+ \\"color\\": \\"$COLOR\\",
1578
+ \\"title\\": \\"$STATUS: $APP_NAME\\",
1579
+ \\"fields\\": [
1580
+ {{\\"title\\": \\"Environment\\", \\"value\\": \\"${{{{ steps.vars.outputs.environment }}}}\\", \\"short\\": true}},
1581
+ {{\\"title\\": \\"Branch\\", \\"value\\": \\"${{{{ github.ref_name }}}}\\", \\"short\\": true}},
1582
+ {{\\"title\\": \\"URL\\", \\"value\\": \\"https://$DOMAIN\\", \\"short\\": false}}
1583
+ ],
1584
+ \\"footer\\": \\"<${{{{ github.server_url }}}}/${{{{ github.repository }}}}/actions/runs/${{{{ github.run_id }}}}|View Workflow>\\"
1585
+ }}]
1586
+ }}" \\
1587
+ "$SLACK_WEBHOOK_URL" || true
1588
+ '''
1589
+
1590
+ PREVIEW_WORKFLOW_TEMPLATE = '''# Preview environments for pull requests
1591
+ name: Preview
1592
+
1593
+ on:
1594
+ pull_request:
1595
+ types: [opened, synchronize, reopened, closed]
1596
+
1597
+ concurrency:
1598
+ group: preview-${{{{ github.event.pull_request.number }}}}
1599
+
1600
+ env:
1601
+ APP_NAME: ${{{{ github.event.repository.name }}}}-pr-${{{{ github.event.pull_request.number }}}}
1602
+
1603
+ jobs:
1604
+ deploy:
1605
+ if: github.event.action != 'closed'
1606
+ runs-on: ubuntu-latest
1607
+ environment: preview
1608
+ steps:
1609
+ - uses: actions/checkout@v4
1610
+ with:
1611
+ fetch-depth: 0
1612
+
1613
+ - name: Setup SSH
1614
+ run: |
1615
+ mkdir -p ~/.ssh && chmod 700 ~/.ssh
1616
+ echo "${{{{ secrets.DOKKU_SSH_PRIVATE_KEY }}}}" > ~/.ssh/key && chmod 600 ~/.ssh/key
1617
+ ssh-keyscan -H ${{{{ secrets.DOKKU_HOST }}}} >> ~/.ssh/known_hosts
1618
+
1619
+ - name: Create app
1620
+ run: |
1621
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} apps:exists $APP_NAME 2>/dev/null || \\
1622
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} apps:create $APP_NAME
1623
+
1624
+ - name: Configure env
1625
+ run: |
1626
+ DOMAIN="${{{{ env.APP_NAME }}}}.${{{{ secrets.BASE_DOMAIN }}}}"
1627
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} config:set --no-restart $APP_NAME \\
1628
+ APP_ENV=preview \\
1629
+ APP_URL="https://$DOMAIN"
1630
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} resource:limit $APP_NAME --memory 256m || true
1631
+
1632
+ - name: Deploy
1633
+ run: |
1634
+ git remote add dokku dokku@${{{{ secrets.DOKKU_HOST }}}}:$APP_NAME 2>/dev/null || true
1635
+ GIT_SSH_COMMAND="ssh -i ~/.ssh/key" git push dokku HEAD:main -f
1636
+
1637
+ - name: Configure domain
1638
+ run: |
1639
+ DOMAIN="${{{{ env.APP_NAME }}}}.${{{{ secrets.BASE_DOMAIN }}}}"
1640
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} domains:clear $APP_NAME 2>/dev/null || true
1641
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} domains:add $APP_NAME $DOMAIN
1642
+
1643
+ - name: SSL
1644
+ run: |
1645
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} letsencrypt:enable $APP_NAME || true
1646
+
1647
+ - name: Comment URL
1648
+ uses: actions/github-script@v7
1649
+ with:
1650
+ script: |
1651
+ const url = `https://${{{{ env.APP_NAME }}}}.${{{{ secrets.BASE_DOMAIN }}}}`;
1652
+ const body = `## 🚀 Preview\\n\\n✅ Deployed: [${{url}}](${{url}})\\n\\n<sub>Auto-destroyed on PR close</sub>`;
1653
+ const comments = await github.rest.issues.listComments({{owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number}});
1654
+ const bot = comments.data.find(c => c.user.type === 'Bot' && c.body.includes('Preview'));
1655
+ if (bot) await github.rest.issues.updateComment({{owner: context.repo.owner, repo: context.repo.repo, comment_id: bot.id, body}});
1656
+ else await github.rest.issues.createComment({{owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, body}});
1657
+
1658
+ cleanup:
1659
+ if: github.event.action == 'closed'
1660
+ runs-on: ubuntu-latest
1661
+ steps:
1662
+ - name: Destroy preview
1663
+ run: |
1664
+ mkdir -p ~/.ssh
1665
+ echo "${{{{ secrets.DOKKU_SSH_PRIVATE_KEY }}}}" > ~/.ssh/key && chmod 600 ~/.ssh/key
1666
+ ssh-keyscan -H ${{{{ secrets.DOKKU_HOST }}}} >> ~/.ssh/known_hosts
1667
+ ssh -i ~/.ssh/key dokku@${{{{ secrets.DOKKU_HOST }}}} apps:destroy ${{{{ env.APP_NAME }}}} --force || true
1668
+ '''
1669
+
1670
+ DOKKU_CONFIG_TEMPLATE = '''# ═══════════════════════════════════════════════════════════════════════════════
1671
+ # Dokku App Configuration
1672
+ # ═══════════════════════════════════════════════════════════════════════════════
1673
+ #
1674
+ # Configuration for your Dokku app deployment.
1675
+ # These settings are applied during the deployment workflow.
1676
+ #
1677
+ # ═══════════════════════════════════════════════════════════════════════════════
1678
+
1679
+ # ─────────────────────────────────────────────────────────────────────────────
1680
+ # Resource Limits
1681
+ # ─────────────────────────────────────────────────────────────────────────────
1682
+ # Memory: 256m, 512m, 1g, 2g, etc.
1683
+ # CPU: Number of cores (can be fractional, e.g., 0.5)
1684
+ resources:
1685
+ memory: 512m
1686
+ cpu: 1
1687
+
1688
+ # ─────────────────────────────────────────────────────────────────────────────
1689
+ # Health Check
1690
+ # ─────────────────────────────────────────────────────────────────────────────
1691
+ # Path that returns 200 OK when app is healthy
1692
+ healthcheck:
1693
+ path: /health
1694
+ timeout: 30
1695
+ attempts: 5
1696
+
1697
+ # ─────────────────────────────────────────────────────────────────────────────
1698
+ # Scaling
1699
+ # ─────────────────────────────────────────────────────────────────────────────
1700
+ # Number of web workers (dynos)
1701
+ scale:
1702
+ web: 1
1703
+ # worker: 1 # Uncomment for background workers
1704
+
1705
+ # ─────────────────────────────────────────────────────────────────────────────
1706
+ # Custom Domains
1707
+ # ─────────────────────────────────────────────────────────────────────────────
1708
+ # Additional domains for this app (requires DNS configuration)
1709
+ # custom_domains:
1710
+ # - www.example.com
1711
+ # - api.example.com
1712
+
1713
+ # ─────────────────────────────────────────────────────────────────────────────
1714
+ # Environment-Specific Overrides
1715
+ # ─────────────────────────────────────────────────────────────────────────────
1716
+ environments:
1717
+ production:
1718
+ resources:
1719
+ memory: 1g
1720
+ cpu: 2
1721
+ scale:
1722
+ web: 2
1723
+
1724
+ staging:
1725
+ resources:
1726
+ memory: 512m
1727
+ cpu: 1
1728
+
1729
+ preview:
1730
+ resources:
1731
+ memory: 256m
1732
+ cpu: 0.5
1733
+ '''
1734
+
1735
+ SERVICES_TEMPLATE = '''# ═══════════════════════════════════════════════════════════════════════════════
1736
+ # Dokku Services Configuration
1737
+ # ═══════════════════════════════════════════════════════════════════════════════
1738
+ #
1739
+ # Define which backing services your app needs.
1740
+ # Services are automatically provisioned and linked during deployment.
1741
+ #
1742
+ # When a service is linked, its connection URL is automatically
1743
+ # injected as an environment variable (e.g., DATABASE_URL, REDIS_URL).
1744
+ #
1745
+ # ═══════════════════════════════════════════════════════════════════════════════
1746
+
1747
+ services:
1748
+ # ─────────────────────────────────────────────────────────────────────────────
1749
+ # PostgreSQL Database
1750
+ # ─────────────────────────────────────────────────────────────────────────────
1751
+ # Environment variable: DATABASE_URL
1752
+ # Format: postgres://user:pass@host:5432/database
1753
+ postgres:
1754
+ enabled: false # Set to true to enable
1755
+ environments:
1756
+ production:
1757
+ # Production gets its own dedicated database
1758
+ dedicated: true
1759
+ staging:
1760
+ # Staging gets its own database
1761
+ dedicated: true
1762
+ preview:
1763
+ # All preview apps share a single database to save resources
1764
+ shared: true
1765
+
1766
+ # ─────────────────────────────────────────────────────────────────────────────
1767
+ # Redis Cache/Queue
1768
+ # ─────────────────────────────────────────────────────────────────────────────
1769
+ # Environment variable: REDIS_URL
1770
+ # Format: redis://host:6379
1771
+ redis:
1772
+ enabled: false # Set to true to enable
1773
+ environments:
1774
+ production:
1775
+ dedicated: true
1776
+ staging:
1777
+ dedicated: true
1778
+ preview:
1779
+ shared: true
1780
+
1781
+ # ─────────────────────────────────────────────────────────────────────────────
1782
+ # MySQL Database
1783
+ # ─────────────────────────────────────────────────────────────────────────────
1784
+ # Environment variable: DATABASE_URL
1785
+ # Format: mysql://user:pass@host:3306/database
1786
+ mysql:
1787
+ enabled: false
1788
+ environments:
1789
+ preview:
1790
+ shared: true
1791
+
1792
+ # ─────────────────────────────────────────────────────────────────────────────
1793
+ # MongoDB
1794
+ # ─────────────────────────────────────────────────────────────────────────────
1795
+ # Environment variable: MONGO_URL
1796
+ # Format: mongodb://user:pass@host:27017/database
1797
+ mongo:
1798
+ enabled: false
1799
+ environments:
1800
+ preview:
1801
+ shared: true
1802
+
1803
+ # ─────────────────────────────────────────────────────────────────────────────
1804
+ # RabbitMQ Message Queue
1805
+ # ─────────────────────────────────────────────────────────────────────────────
1806
+ # Environment variable: RABBITMQ_URL
1807
+ # Format: amqp://user:pass@host:5672
1808
+ rabbitmq:
1809
+ enabled: false
1810
+ environments:
1811
+ preview:
1812
+ # Don't provision RabbitMQ for previews (too expensive)
1813
+ enabled: false
1814
+
1815
+ # ─────────────────────────────────────────────────────────────────────────────
1816
+ # Elasticsearch
1817
+ # ─────────────────────────────────────────────────────────────────────────────
1818
+ # Environment variable: ELASTICSEARCH_URL
1819
+ # Format: http://host:9200
1820
+ elasticsearch:
1821
+ enabled: false
1822
+ environments:
1823
+ preview:
1824
+ enabled: false
1825
+
1826
+ # ═══════════════════════════════════════════════════════════════════════════════
1827
+ # External/Managed Services
1828
+ # ═══════════════════════════════════════════════════════════════════════════════
1829
+ #
1830
+ # For production, you may want to use managed services like AWS RDS,
1831
+ # ElastiCache, etc. Define the connection URLs here (reference GitHub secrets).
1832
+ #
1833
+ # These override the Dokku-managed services for the specified environment.
1834
+ #
1835
+ # ═══════════════════════════════════════════════════════════════════════════════
1836
+
1837
+ # external_services:
1838
+ # production:
1839
+ # DATABASE_URL: "${secrets.PROD_DATABASE_URL}"
1840
+ # REDIS_URL: "${secrets.PROD_REDIS_URL}"
1841
+ # staging:
1842
+ # DATABASE_URL: "${secrets.STAGING_DATABASE_URL}"
1843
+ '''
1844
+
1845
+ README_CICD_TEMPLATE = '''# {app_name}
1846
+
1847
+ A SignalWire AI Agent with automated GitHub → Dokku deployments.
1848
+
1849
+ ## Features
1850
+
1851
+ - ✅ Auto-deploy on push to main/staging/develop
1852
+ - ✅ Preview environments for pull requests
1853
+ - ✅ Automatic SSL via Let's Encrypt
1854
+ - ✅ Zero-downtime deployments
1855
+ - ✅ Multi-environment support
1856
+
1857
+ ## Setup
1858
+
1859
+ ### 1. GitHub Secrets
1860
+
1861
+ Add these secrets to your repository (Settings → Secrets → Actions):
1862
+
1863
+ | Secret | Description |
1864
+ |--------|-------------|
1865
+ | `DOKKU_HOST` | Your Dokku server hostname |
1866
+ | `DOKKU_SSH_PRIVATE_KEY` | SSH private key for deployments |
1867
+ | `BASE_DOMAIN` | Base domain (e.g., `yourdomain.com`) |
1868
+ | `SWML_BASIC_AUTH_USER` | Basic auth username |
1869
+ | `SWML_BASIC_AUTH_PASSWORD` | Basic auth password |
1870
+
1871
+ ### 2. GitHub Environments
1872
+
1873
+ Create these environments (Settings → Environments):
1874
+ - `production` - Deploy from `main` branch
1875
+ - `staging` - Deploy from `staging` branch
1876
+ - `development` - Deploy from `develop` branch
1877
+ - `preview` - Deploy from pull requests
1878
+
1879
+ ### 3. Deploy
1880
+
1881
+ Just push to a branch:
1882
+
1883
+ ```bash
1884
+ git push origin main # → {app_name}.yourdomain.com
1885
+ git push origin staging # → {app_name}-staging.yourdomain.com
1886
+ git push origin develop # → {app_name}-dev.yourdomain.com
1887
+ ```
1888
+
1889
+ Or open a PR for a preview environment.
1890
+
1891
+ ## Branch → Environment Mapping
1892
+
1893
+ | Branch | App Name | URL |
1894
+ |--------|----------|-----|
1895
+ | `main` | `{app_name}` | `{app_name}.yourdomain.com` |
1896
+ | `staging` | `{app_name}-staging` | `{app_name}-staging.yourdomain.com` |
1897
+ | `develop` | `{app_name}-dev` | `{app_name}-dev.yourdomain.com` |
1898
+ | PR #42 | `{app_name}-pr-42` | `{app_name}-pr-42.yourdomain.com` |
1899
+
1900
+ ## Manual Operations
1901
+
1902
+ ```bash
1903
+ # View logs
1904
+ ssh dokku@server logs {app_name} -t
1905
+
1906
+ # SSH into container
1907
+ ssh dokku@server enter {app_name}
1908
+
1909
+ # Restart
1910
+ ssh dokku@server ps:restart {app_name}
1911
+
1912
+ # Rollback
1913
+ ssh dokku@server releases:rollback {app_name}
1914
+
1915
+ # Scale
1916
+ ssh dokku@server ps:scale {app_name} web=2
1917
+ ```
1918
+
1919
+ ## Local Development
1920
+
1921
+ ```bash
1922
+ pip install -r requirements.txt
1923
+ uvicorn app:app --reload --port 8080
1924
+ ```
1925
+
1926
+ Test with swaig-test:
1927
+ ```bash
1928
+ swaig-test app.py --list-tools
1929
+ ```
1930
+ '''
1931
+
1932
+
1933
+ # =============================================================================
1934
+ # Project Generator
1935
+ # =============================================================================
1936
+
1937
+ class DokkuProjectGenerator:
1938
+ """Generates Dokku deployment files for SignalWire agents."""
1939
+
1940
+ def __init__(self, app_name: str, options: Dict[str, Any]):
1941
+ self.app_name = app_name
1942
+ self.options = options
1943
+ self.project_dir = Path(options.get('project_dir', f'./{app_name}'))
1944
+
1945
+ # Derived names
1946
+ self.agent_slug = app_name.lower().replace(' ', '-').replace('_', '-')
1947
+ self.agent_class = ''.join(
1948
+ word.capitalize()
1949
+ for word in app_name.replace('-', ' ').replace('_', ' ').split()
1950
+ ) + 'Agent'
1951
+
1952
+ def generate(self) -> bool:
1953
+ """Generate the project files."""
1954
+ try:
1955
+ self.project_dir.mkdir(parents=True, exist_ok=True)
1956
+ print_success(f"Created {self.project_dir}/")
1957
+
1958
+ # Core files (both modes)
1959
+ self._write_core_files()
1960
+
1961
+ # Mode-specific files
1962
+ if self.options.get('cicd'):
1963
+ self._write_cicd_files()
1964
+ else:
1965
+ self._write_simple_files()
1966
+
1967
+ return True
1968
+ except Exception as e:
1969
+ print_error(f"Failed to generate project: {e}")
1970
+ return False
1971
+
1972
+ def _write_core_files(self):
1973
+ """Write files common to both modes."""
1974
+ # Procfile
1975
+ self._write_file('Procfile', PROCFILE_TEMPLATE)
1976
+
1977
+ # runtime.txt
1978
+ self._write_file('runtime.txt', RUNTIME_TEMPLATE)
1979
+
1980
+ # requirements.txt
1981
+ self._write_file('requirements.txt', REQUIREMENTS_TEMPLATE)
1982
+
1983
+ # CHECKS
1984
+ self._write_file('CHECKS', CHECKS_TEMPLATE)
1985
+
1986
+ # .gitignore
1987
+ self._write_file('.gitignore', GITIGNORE_TEMPLATE)
1988
+
1989
+ # .env.example
1990
+ self._write_file('.env.example', ENV_EXAMPLE_TEMPLATE.format(
1991
+ app_name=self.app_name
1992
+ ))
1993
+
1994
+ # app.json
1995
+ self._write_file('app.json', APP_JSON_TEMPLATE.format(
1996
+ app_name=self.app_name
1997
+ ))
1998
+
1999
+ # app.py - use web template if web option is enabled
2000
+ if self.options.get('web'):
2001
+ self._write_file('app.py', APP_TEMPLATE_WITH_WEB.format(
2002
+ agent_name=self.app_name,
2003
+ agent_class=self.agent_class,
2004
+ agent_slug=self.agent_slug
2005
+ ))
2006
+ self._write_web_files()
2007
+ else:
2008
+ self._write_file('app.py', APP_TEMPLATE.format(
2009
+ agent_name=self.app_name,
2010
+ agent_class=self.agent_class,
2011
+ agent_slug=self.agent_slug
2012
+ ))
2013
+
2014
+ def _write_web_files(self):
2015
+ """Write web interface files."""
2016
+ # Create web directory
2017
+ web_dir = self.project_dir / 'web'
2018
+ web_dir.mkdir(parents=True, exist_ok=True)
2019
+
2020
+ route = self.options.get('route', 'swaig')
2021
+
2022
+ # index.html
2023
+ self._write_file('web/index.html', WEB_INDEX_TEMPLATE.format(
2024
+ agent_name=self.app_name,
2025
+ route=route
2026
+ ))
2027
+
2028
+ def _write_simple_files(self):
2029
+ """Write files for simple deployment mode."""
2030
+ dokku_host = self.options.get('dokku_host', 'dokku.yourdomain.com')
2031
+ route = self.options.get('route', 'swaig')
2032
+
2033
+ # deploy.sh
2034
+ deploy_script = DEPLOY_SCRIPT_TEMPLATE.format(
2035
+ app_name=self.app_name,
2036
+ dokku_host=dokku_host,
2037
+ route=route
2038
+ )
2039
+ self._write_file('deploy.sh', deploy_script, executable=True)
2040
+
2041
+ # README.md
2042
+ readme = README_SIMPLE_TEMPLATE.format(
2043
+ app_name=self.app_name,
2044
+ dokku_host=dokku_host,
2045
+ dokku_host_domain=dokku_host.replace('dokku.', ''),
2046
+ route=route
2047
+ )
2048
+ self._write_file('README.md', readme)
2049
+
2050
+ def _write_cicd_files(self):
2051
+ """Write files for CI/CD deployment mode."""
2052
+ # Create .github/workflows directory
2053
+ workflows_dir = self.project_dir / '.github' / 'workflows'
2054
+ workflows_dir.mkdir(parents=True, exist_ok=True)
2055
+
2056
+ # deploy.yml
2057
+ deploy_workflow = DEPLOY_WORKFLOW_TEMPLATE.format(app_name=self.app_name)
2058
+ self._write_file('.github/workflows/deploy.yml', deploy_workflow)
2059
+
2060
+ # preview.yml
2061
+ preview_workflow = PREVIEW_WORKFLOW_TEMPLATE.format(app_name=self.app_name)
2062
+ self._write_file('.github/workflows/preview.yml', preview_workflow)
2063
+
2064
+ # Create .dokku directory
2065
+ dokku_dir = self.project_dir / '.dokku'
2066
+ dokku_dir.mkdir(parents=True, exist_ok=True)
2067
+
2068
+ # config.yml
2069
+ self._write_file('.dokku/config.yml', DOKKU_CONFIG_TEMPLATE)
2070
+
2071
+ # services.yml
2072
+ self._write_file('.dokku/services.yml', SERVICES_TEMPLATE)
2073
+
2074
+ # README.md
2075
+ readme = README_CICD_TEMPLATE.format(app_name=self.app_name)
2076
+ self._write_file('README.md', readme)
2077
+
2078
+ def _write_file(self, path: str, content: str, executable: bool = False):
2079
+ """Write a file to the project directory."""
2080
+ file_path = self.project_dir / path
2081
+ file_path.parent.mkdir(parents=True, exist_ok=True)
2082
+ file_path.write_text(content)
2083
+
2084
+ if executable:
2085
+ file_path.chmod(0o755)
2086
+
2087
+ print_success(f"Created {path}")
2088
+
2089
+
2090
+ # =============================================================================
2091
+ # CLI Commands
2092
+ # =============================================================================
2093
+
2094
+ def cmd_init(args):
2095
+ """Initialize a new Dokku project."""
2096
+ app_name = args.name
2097
+
2098
+ print_header(f"Creating Dokku project: {app_name}")
2099
+
2100
+ # Gather options
2101
+ options = {
2102
+ 'project_dir': args.dir or f'./{app_name}',
2103
+ 'cicd': args.cicd,
2104
+ 'dokku_host': args.host or 'dokku.yourdomain.com',
2105
+ 'route': 'swaig',
2106
+ 'web': args.web,
2107
+ }
2108
+
2109
+ # Interactive mode if not all options provided
2110
+ if not args.host and not args.cicd:
2111
+ print("\n")
2112
+ if prompt_yes_no("Enable GitHub Actions CI/CD?", default=False):
2113
+ options['cicd'] = True
2114
+ else:
2115
+ options['dokku_host'] = prompt("Dokku server hostname", "dokku.yourdomain.com")
2116
+
2117
+ # Ask about web interface if not specified
2118
+ if not args.web:
2119
+ options['web'] = prompt_yes_no("Include web interface (static files at /)?", default=True)
2120
+
2121
+ # Check if directory exists
2122
+ project_dir = Path(options['project_dir'])
2123
+ if project_dir.exists():
2124
+ if not args.force:
2125
+ if not prompt_yes_no(f"Directory {project_dir} exists. Overwrite?", default=False):
2126
+ print("Aborted.")
2127
+ return 1
2128
+ shutil.rmtree(project_dir)
2129
+
2130
+ # Generate project
2131
+ generator = DokkuProjectGenerator(app_name, options)
2132
+ if generator.generate():
2133
+ print(f"\n{Colors.GREEN}{Colors.BOLD}Project created successfully!{Colors.NC}\n")
2134
+
2135
+ if options['cicd']:
2136
+ _print_cicd_instructions(app_name)
2137
+ else:
2138
+ _print_simple_instructions(app_name, options['dokku_host'], project_dir)
2139
+
2140
+ return 0
2141
+ return 1
2142
+
2143
+
2144
+ def _print_simple_instructions(app_name: str, dokku_host: str, project_dir: Path):
2145
+ """Print instructions for simple mode."""
2146
+ print(f"""To deploy your agent:
2147
+
2148
+ {Colors.CYAN}cd {project_dir}{Colors.NC}
2149
+ {Colors.CYAN}./deploy.sh{Colors.NC}
2150
+
2151
+ Or manually:
2152
+
2153
+ {Colors.DIM}git init && git add . && git commit -m "Initial commit"
2154
+ git remote add dokku dokku@{dokku_host}:{app_name}
2155
+ git push dokku main{Colors.NC}
2156
+ """)
2157
+
2158
+
2159
+ def _print_cicd_instructions(app_name: str):
2160
+ """Print instructions for CI/CD mode."""
2161
+ print(f"""
2162
+ {Colors.BOLD}═══════════════════════════════════════════════════════════{Colors.NC}
2163
+ {Colors.BOLD} CI/CD Setup Instructions{Colors.NC}
2164
+ {Colors.BOLD}═══════════════════════════════════════════════════════════{Colors.NC}
2165
+
2166
+ 1. Push this repository to GitHub
2167
+
2168
+ 2. Add these secrets to your GitHub repository:
2169
+ (Settings → Secrets → Actions)
2170
+
2171
+ • {Colors.CYAN}DOKKU_HOST{Colors.NC} - Your Dokku server hostname
2172
+ • {Colors.CYAN}DOKKU_SSH_PRIVATE_KEY{Colors.NC} - SSH key for deployments
2173
+ • {Colors.CYAN}BASE_DOMAIN{Colors.NC} - Base domain (e.g., yourdomain.com)
2174
+ • {Colors.CYAN}SWML_BASIC_AUTH_USER{Colors.NC} - Basic auth username
2175
+ • {Colors.CYAN}SWML_BASIC_AUTH_PASSWORD{Colors.NC} - Basic auth password
2176
+
2177
+ 3. Create GitHub environments:
2178
+ (Settings → Environments)
2179
+
2180
+ • production
2181
+ • staging
2182
+ • development
2183
+ • preview
2184
+
2185
+ 4. Push to deploy:
2186
+
2187
+ {Colors.CYAN}git push origin main{Colors.NC} # Deploys to production
2188
+
2189
+ 5. Open a PR for preview environments!
2190
+
2191
+ {Colors.BOLD}═══════════════════════════════════════════════════════════{Colors.NC}
2192
+ """)
2193
+
2194
+
2195
+ def cmd_deploy(args):
2196
+ """Deploy to Dokku."""
2197
+ # Check if we're in a Dokku project
2198
+ if not Path('Procfile').exists():
2199
+ print_error("No Procfile found. Are you in a Dokku project directory?")
2200
+ print("Run 'sw-agent-dokku init <name>' to create a new project.")
2201
+ return 1
2202
+
2203
+ dokku_host = args.host
2204
+ app_name = args.app
2205
+
2206
+ # Try to get app name from app.json
2207
+ if not app_name and Path('app.json').exists():
2208
+ import json
2209
+ try:
2210
+ with open('app.json') as f:
2211
+ app_json = json.load(f)
2212
+ app_name = app_json.get('name')
2213
+ except:
2214
+ pass
2215
+
2216
+ if not app_name:
2217
+ app_name = prompt("App name", Path.cwd().name)
2218
+
2219
+ if not dokku_host:
2220
+ dokku_host = prompt("Dokku host", "dokku.yourdomain.com")
2221
+
2222
+ print_header(f"Deploying {app_name} to {dokku_host}")
2223
+
2224
+ # Check git status
2225
+ if not Path('.git').exists():
2226
+ print_step("Initializing git repository...")
2227
+ subprocess.run(['git', 'init'], check=True)
2228
+ subprocess.run(['git', 'add', '.'], check=True)
2229
+ subprocess.run(['git', 'commit', '-m', 'Initial commit'], check=True)
2230
+
2231
+ # Create app
2232
+ print_step("Creating app (if not exists)...")
2233
+ subprocess.run(
2234
+ ['ssh', f'dokku@{dokku_host}', 'apps:create', app_name],
2235
+ capture_output=True
2236
+ )
2237
+
2238
+ # Set up git remote
2239
+ print_step("Configuring git remote...")
2240
+ remote_url = f'dokku@{dokku_host}:{app_name}'
2241
+ subprocess.run(['git', 'remote', 'remove', 'dokku'], capture_output=True)
2242
+ subprocess.run(['git', 'remote', 'add', 'dokku', remote_url], check=True)
2243
+
2244
+ # Deploy
2245
+ print_step("Pushing to Dokku...")
2246
+ result = subprocess.run(
2247
+ ['git', 'push', 'dokku', 'HEAD:main', '--force'],
2248
+ capture_output=False
2249
+ )
2250
+
2251
+ if result.returncode == 0:
2252
+ print_success(f"Deployed to https://{app_name}.{dokku_host.replace('dokku.', '')}")
2253
+ else:
2254
+ print_error("Deployment failed")
2255
+ return 1
2256
+
2257
+ return 0
2258
+
2259
+
2260
+ def cmd_logs(args):
2261
+ """Tail Dokku logs."""
2262
+ app_name = args.app
2263
+ dokku_host = args.host
2264
+
2265
+ if not app_name:
2266
+ app_name = _get_app_name()
2267
+ if not dokku_host:
2268
+ dokku_host = prompt("Dokku host", "dokku.yourdomain.com")
2269
+
2270
+ print_header(f"Tailing logs for {app_name}")
2271
+
2272
+ cmd = ['ssh', f'dokku@{dokku_host}', 'logs', app_name]
2273
+ if args.tail:
2274
+ cmd.append('-t')
2275
+ if args.num:
2276
+ cmd.extend(['--num', str(args.num)])
2277
+
2278
+ subprocess.run(cmd)
2279
+ return 0
2280
+
2281
+
2282
+ def cmd_config(args):
2283
+ """Manage Dokku config."""
2284
+ app_name = args.app
2285
+ dokku_host = args.host
2286
+
2287
+ if not app_name:
2288
+ app_name = _get_app_name()
2289
+ if not dokku_host:
2290
+ dokku_host = prompt("Dokku host", "dokku.yourdomain.com")
2291
+
2292
+ if args.config_action == 'show':
2293
+ subprocess.run(['ssh', f'dokku@{dokku_host}', 'config:show', app_name])
2294
+ elif args.config_action == 'set':
2295
+ if not args.vars:
2296
+ print_error("No variables provided. Use: sw-agent-dokku config set KEY=value")
2297
+ return 1
2298
+ cmd = ['ssh', f'dokku@{dokku_host}', 'config:set', app_name] + args.vars
2299
+ subprocess.run(cmd)
2300
+ elif args.config_action == 'unset':
2301
+ if not args.vars:
2302
+ print_error("No variables provided. Use: sw-agent-dokku config unset KEY")
2303
+ return 1
2304
+ cmd = ['ssh', f'dokku@{dokku_host}', 'config:unset', app_name] + args.vars
2305
+ subprocess.run(cmd)
2306
+
2307
+ return 0
2308
+
2309
+
2310
+ def cmd_scale(args):
2311
+ """Scale Dokku processes."""
2312
+ app_name = args.app
2313
+ dokku_host = args.host
2314
+
2315
+ if not app_name:
2316
+ app_name = _get_app_name()
2317
+ if not dokku_host:
2318
+ dokku_host = prompt("Dokku host", "dokku.yourdomain.com")
2319
+
2320
+ if not args.scale_args:
2321
+ # Show current scale
2322
+ subprocess.run(['ssh', f'dokku@{dokku_host}', 'ps:scale', app_name])
2323
+ else:
2324
+ # Set scale
2325
+ cmd = ['ssh', f'dokku@{dokku_host}', 'ps:scale', app_name] + args.scale_args
2326
+ subprocess.run(cmd)
2327
+
2328
+ return 0
2329
+
2330
+
2331
+ def _get_app_name() -> str:
2332
+ """Try to get app name from app.json or prompt."""
2333
+ if Path('app.json').exists():
2334
+ import json
2335
+ try:
2336
+ with open('app.json') as f:
2337
+ return json.load(f).get('name', '')
2338
+ except:
2339
+ pass
2340
+ return prompt("App name", Path.cwd().name)
2341
+
2342
+
2343
+ # =============================================================================
2344
+ # Main Entry Point
2345
+ # =============================================================================
2346
+
2347
+ def main():
2348
+ parser = argparse.ArgumentParser(
2349
+ description='SignalWire Agent Dokku Deployment Tool',
2350
+ formatter_class=argparse.RawDescriptionHelpFormatter,
2351
+ epilog='''
2352
+ Examples:
2353
+ sw-agent-dokku init myagent # Create simple project (with prompts)
2354
+ sw-agent-dokku init myagent --web # Create with web interface at /
2355
+ sw-agent-dokku init myagent --cicd # Create with CI/CD workflows
2356
+ sw-agent-dokku init myagent --host dokku.example.com
2357
+ sw-agent-dokku deploy # Deploy current directory
2358
+ sw-agent-dokku logs -t # Tail logs
2359
+ sw-agent-dokku config show # Show config
2360
+ sw-agent-dokku config set KEY=value # Set config
2361
+ sw-agent-dokku scale web=2 # Scale processes
2362
+ '''
2363
+ )
2364
+
2365
+ subparsers = parser.add_subparsers(dest='command', help='Commands')
2366
+
2367
+ # init command
2368
+ init_parser = subparsers.add_parser('init', help='Initialize a new Dokku project')
2369
+ init_parser.add_argument('name', help='Project/app name')
2370
+ init_parser.add_argument('--cicd', action='store_true',
2371
+ help='Include GitHub Actions CI/CD workflows')
2372
+ init_parser.add_argument('--web', action='store_true',
2373
+ help='Include web interface (static files at /)')
2374
+ init_parser.add_argument('--host', help='Dokku server hostname')
2375
+ init_parser.add_argument('--dir', help='Project directory')
2376
+ init_parser.add_argument('--force', '-f', action='store_true',
2377
+ help='Overwrite existing directory')
2378
+
2379
+ # deploy command
2380
+ deploy_parser = subparsers.add_parser('deploy', help='Deploy to Dokku')
2381
+ deploy_parser.add_argument('--app', '-a', help='App name')
2382
+ deploy_parser.add_argument('--host', '-H', help='Dokku server hostname')
2383
+
2384
+ # logs command
2385
+ logs_parser = subparsers.add_parser('logs', help='View Dokku logs')
2386
+ logs_parser.add_argument('--app', '-a', help='App name')
2387
+ logs_parser.add_argument('--host', '-H', help='Dokku server hostname')
2388
+ logs_parser.add_argument('--tail', '-t', action='store_true', help='Tail logs')
2389
+ logs_parser.add_argument('--num', '-n', type=int, help='Number of lines')
2390
+
2391
+ # config command
2392
+ config_parser = subparsers.add_parser('config', help='Manage config variables')
2393
+ config_parser.add_argument('config_action', choices=['show', 'set', 'unset'],
2394
+ help='Config action')
2395
+ config_parser.add_argument('vars', nargs='*', help='Variables (KEY=value)')
2396
+ config_parser.add_argument('--app', '-a', help='App name')
2397
+ config_parser.add_argument('--host', '-H', help='Dokku server hostname')
2398
+
2399
+ # scale command
2400
+ scale_parser = subparsers.add_parser('scale', help='Scale processes')
2401
+ scale_parser.add_argument('scale_args', nargs='*', help='Scale args (web=2)')
2402
+ scale_parser.add_argument('--app', '-a', help='App name')
2403
+ scale_parser.add_argument('--host', '-H', help='Dokku server hostname')
2404
+
2405
+ args = parser.parse_args()
2406
+
2407
+ if not args.command:
2408
+ parser.print_help()
2409
+ return 1
2410
+
2411
+ commands = {
2412
+ 'init': cmd_init,
2413
+ 'deploy': cmd_deploy,
2414
+ 'logs': cmd_logs,
2415
+ 'config': cmd_config,
2416
+ 'scale': cmd_scale,
2417
+ }
2418
+
2419
+ return commands[args.command](args)
2420
+
2421
+
2422
+ if __name__ == '__main__':
2423
+ sys.exit(main())