-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_keys.py
More file actions
1025 lines (948 loc) · 40.5 KB
/
api_keys.py
File metadata and controls
1025 lines (948 loc) · 40.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
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
"""
api_keys.py - API Integration Configuration Manager
IC-7610 Voice Agent
Manages API keys and provider configurations for third-party TTS and other
integrations. Keys are stored in a JSON file on disk with optional obfuscation.
New providers can be added by appending to PROVIDER_REGISTRY.
Storage: /home/riggpt/api_keys.json
Format: { "provider_id": { "field_id": "value", ... }, ... }
Security note: Keys are stored in plaintext in the JSON file.
Protect the file with filesystem permissions (chmod 600).
This is appropriate for a self-hosted amateur radio automation tool.
If you need stronger protection, set RIGGPT_KEY_FILE to a path on
an encrypted volume, or provide keys via environment variables which
take precedence over stored values.
"""
import os
import json
import logging
import hashlib
import requests
from pathlib import Path
from typing import Any
logger = logging.getLogger('ic7610.apikeys')
def _ascii(s) -> str:
"""Strip non-ASCII from external API responses so Flask latin-1
encoder never chokes on Unicode in error messages."""
return str(s).encode('ascii', errors='replace').decode('ascii')
KEY_FILE = os.environ.get(
'RIGGPT_KEY_FILE',
'/home/riggpt/api_keys.json'
)
# ─────────────────────────────────────────────────────────────
# Provider Registry
#
# Each entry defines a TTS/API provider. Fields are rendered as
# form inputs in the Settings -> API Integrations panel.
#
# Field types:
# password - masked input, stored encrypted-at-rest (obfuscated)
# text - plain text input
# select - dropdown; include 'options': [{'value':..,'label':..}]
# url - URL input with validation
#
# env_override: if set, this env var takes precedence over stored value
# test_fn: callable(config_dict) -> (bool, str) - tests connectivity
# ─────────────────────────────────────────────────────────────
def _test_elevenlabs(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.elevenlabs.io/v1/user',
headers={'xi-api-key': key},
timeout=8
)
if r.status_code == 200:
data = r.json()
tier = data.get('subscription', {}).get('tier', 'unknown')
chars = data.get('subscription', {}).get('character_count', '?')
limit = data.get('subscription', {}).get('character_limit', '?')
return True, _ascii(f'Connected - tier: {tier}, chars used: {chars:,}/{limit:,}')
elif r.status_code == 401:
return False, 'Invalid API key (401 Unauthorized)'
else:
return False, f'Unexpected response: HTTP {r.status_code}'
except requests.Timeout:
return False, 'Connection timed out'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_speechify(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
# Speechify API v1 - list available voices as connectivity check
r = requests.get(
'https://api.sws.speechify.com/v1/voices',
headers={
'Authorization': f'Bearer {key}',
'Accept': 'application/json',
},
timeout=8
)
if r.status_code == 200:
data = r.json()
voices = data if isinstance(data, list) else data.get('voices', [])
return True, _ascii(f'Connected - {len(voices)} voice(s) available')
elif r.status_code == 401:
return False, 'Invalid API key (401 Unauthorized)'
elif r.status_code == 403:
return False, 'Access denied - check API key permissions'
else:
return False, f'Unexpected response: HTTP {r.status_code}'
except requests.Timeout:
return False, 'Connection timed out'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_openai(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.openai.com/v1/models',
headers={'Authorization': f'Bearer {key}'},
timeout=8
)
if r.status_code == 200:
models = [m['id'] for m in r.json().get('data', []) if 'tts' in m['id']]
return True, _ascii(f'Connected - TTS models: {", ".join(models) or "none found"}')
elif r.status_code == 401:
return False, 'Invalid API key'
else:
return False, f'HTTP {r.status_code}'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_azure(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
region = cfg.get('region', '').strip()
if not key or not region:
return False, 'API key and region are both required'
try:
r = requests.post(
f'https://{region}.api.cognitive.microsoft.com/sts/v1.0/issueToken',
headers={'Ocp-Apim-Subscription-Key': key},
timeout=8
)
if r.status_code == 200:
return True, _ascii(f'Connected - region: {region}')
elif r.status_code == 401:
return False, 'Invalid key or wrong region'
else:
return False, f'HTTP {r.status_code}'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_gemini(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://generativelanguage.googleapis.com/v1beta/models',
params={'key': key},
timeout=8
)
if r.status_code == 200:
models = [m.get('name','').split('/')[-1] for m in r.json().get('models',[])
if 'generateContent' in m.get('supportedGenerationMethods',[])]
return True, _ascii(f'Connected - {len(models)} generative model(s): {", ".join(models[:4])}')
elif r.status_code == 400:
return False, 'Invalid API key format'
elif r.status_code == 403:
return False, 'Invalid API key or API not enabled'
else:
return False, f'HTTP {r.status_code}: {r.text[:80]}'
except requests.Timeout:
return False, 'Connection timed out'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_openai_chat(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.openai.com/v1/models',
headers={'Authorization': f'Bearer {key}'},
timeout=8
)
if r.status_code == 200:
models = [m['id'] for m in r.json().get('data', [])
if 'gpt' in m['id'] and 'instruct' not in m['id']]
chat_models = [m for m in models if any(x in m for x in ['gpt-4o','gpt-4','gpt-3.5'])]
return True, _ascii(f'Connected - chat models: {", ".join(sorted(set(chat_models))[:5])}')
elif r.status_code == 401:
return False, 'Invalid API key'
else:
return False, f'HTTP {r.status_code}'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_anthropic(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.anthropic.com/v1/models',
headers={
'x-api-key': key,
'anthropic-version': '2023-06-01',
},
timeout=8
)
if r.status_code == 200:
models = [m.get('id','') for m in r.json().get('data',[])]
return True, _ascii(f'Connected - {len(models)} model(s): {", ".join(models[:3])}')
elif r.status_code == 401:
return False, 'Invalid API key'
else:
return False, f'HTTP {r.status_code}: {r.text[:80]}'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_mistral(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.mistral.ai/v1/models',
headers={'Authorization': f'Bearer {key}'},
timeout=8
)
if r.status_code == 200:
models = [m.get('id','') for m in r.json().get('data',[])]
return True, _ascii(f'Connected - {len(models)} model(s): {", ".join(models[:4])}')
elif r.status_code == 401:
return False, 'Invalid API key'
else:
return False, f'HTTP {r.status_code}'
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_discord_ctrl(cfg: dict) -> tuple[bool, str]:
token = cfg.get('bot_token', '').strip()
guild_id = cfg.get('guild_id', '').strip()
channel_id = cfg.get('channel_id', '').strip()
if not token:
return False, 'No bot token configured'
try:
r = requests.get(
'https://discord.com/api/v10/users/@me',
headers={'Authorization': f'Bot {token}'},
timeout=8,
)
if r.status_code == 401:
return False, 'Invalid bot token'
if r.status_code != 200:
return False, _ascii(f'HTTP {r.status_code}: {r.text[:120]}')
bot_name = _ascii(r.json().get('username', 'unknown'))
if channel_id:
rc = requests.get(
f'https://discord.com/api/v10/channels/{channel_id}',
headers={'Authorization': f'Bot {token}'},
timeout=8,
)
if rc.status_code == 404:
return False, _ascii(f'Bot "{bot_name}" OK but channel {channel_id} not found')
if rc.status_code == 403:
return False, _ascii(f'Bot "{bot_name}" cannot read channel {channel_id} -- check permissions')
if rc.status_code == 200:
ch_name = _ascii('#' + rc.json().get('name', channel_id))
return True, _ascii(f'Bot "{bot_name}" connected -- control channel: {ch_name}. Post !chaos to test.')
return True, _ascii(f'Bot "{bot_name}" token valid (no channel ID to verify)')
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_fastkoko(cfg: dict) -> tuple[bool, str]:
url = cfg.get('url', '').strip().rstrip('/')
if not url:
return False, 'No server URL configured'
try:
r = requests.get(f'{url}/v1/audio/voices', timeout=6)
if r.status_code != 200:
return False, _ascii(f'HTTP {r.status_code}: {r.text[:120]}')
voices = r.json().get('voices', [])
if not voices:
return False, 'Server responded but returned no voices'
return True, _ascii(f'OK -- {len(voices)} voices available (e.g. {voices[0]})')
except requests.exceptions.ConnectionError:
return False, _ascii(f'Cannot connect to {url} -- is FastKoko running?')
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_discord(cfg: dict) -> tuple[bool, str]:
token = cfg.get('bot_token', '').strip()
guild_id = cfg.get('guild_id', '').strip()
channel_id = cfg.get('channel_id', '').strip()
if not token:
return False, 'No bot token configured'
try:
# Verify token by fetching bot identity
r = requests.get(
'https://discord.com/api/v10/users/@me',
headers={'Authorization': f'Bot {token}'},
timeout=8,
)
if r.status_code == 401:
return False, 'Invalid bot token'
if r.status_code != 200:
return False, _ascii(f'HTTP {r.status_code}: {r.text[:120]}')
bot = r.json()
bot_name = _ascii(bot.get('username', 'unknown'))
# If guild_id provided, verify bot is a member of that server
if guild_id:
rg = requests.get(
f'https://discord.com/api/v10/guilds/{guild_id}',
headers={'Authorization': f'Bot {token}'},
timeout=8,
)
if rg.status_code in (403, 404):
return False, _ascii(
f'Bot "{bot_name}" authenticated OK but is not a member of server {guild_id}. '
f'Invite the bot first: discord.com/developers/applications -> '
f'Your App -> OAuth2 -> URL Generator -> scope: bot, permission: Read Messages -> '
f'open the generated URL and add the bot to your server'
)
if rg.status_code == 200:
guild_name = _ascii(rg.json().get('name', guild_id))
else:
guild_name = guild_id
# If channel_id provided, verify bot can read that channel
if channel_id:
rc = requests.get(
f'https://discord.com/api/v10/channels/{channel_id}',
headers={'Authorization': f'Bot {token}'},
timeout=8,
)
if rc.status_code == 403:
return False, _ascii(f'Bot lacks permission to read channel {channel_id}')
if rc.status_code == 404:
return False, _ascii(f'Channel {channel_id} not found')
if rc.status_code == 200:
ch_name = _ascii(rc.json().get('name', channel_id))
return True, _ascii(f'Connected - bot: {bot_name}, server: {guild_name}, channel: #{ch_name}')
return True, _ascii(f'Connected - bot: {bot_name}, server: {guild_name} (no channel set)')
return True, _ascii(f'Connected - bot: {bot_name} (no server/channel configured yet)')
except Exception as e:
return False, _ascii(f'Error: {e}')
def _test_groq(cfg: dict) -> tuple[bool, str]:
key = cfg.get('api_key', '').strip()
if not key:
return False, 'No API key configured'
try:
r = requests.get(
'https://api.groq.com/openai/v1/models',
headers={'Authorization': f'Bearer {key}'},
timeout=8
)
if r.status_code == 200:
models = [m.get('id','') for m in r.json().get('data',[])]
return True, _ascii(f'Connected - {len(models)} model(s): {", ".join(models[:4])}')
elif r.status_code == 401:
return False, 'Invalid API key'
else:
return False, f'HTTP {r.status_code}'
except Exception as e:
return False, _ascii(f'Error: {e}')
# ── The registry ─────────────────────────────────────────────
PROVIDER_REGISTRY: list[dict] = [
{
'id': 'elevenlabs',
'name': 'ElevenLabs',
'description': 'High-quality neural TTS with custom voice cloning. Requires an ElevenLabs account.',
'docs_url': 'https://elevenlabs.io/docs/api-reference/text-to-speech',
'category': 'tts',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'sk-...',
'required': True,
'env_override': 'ELEVENLABS_API_KEY',
'help': 'Found at elevenlabs.io -> Profile -> API Keys',
},
],
'test_fn': _test_elevenlabs,
},
{
'id': 'speechify',
'name': 'Speechify',
'description': 'Natural-sounding TTS with a large voice library. Requires a Speechify API subscription.',
'docs_url': 'https://docs.sws.speechify.com',
'category': 'tts',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'Bearer token from Speechify dashboard',
'required': True,
'env_override': 'SPEECHIFY_API_KEY',
'help': 'Found at app.speechify.com -> API -> Access Token',
},
{
'id': 'default_voice',
'label': 'Default Voice ID',
'type': 'text',
'placeholder': 'henry',
'required': False,
'help': 'Voice ID from Speechify voice library. Leave blank to use the API default.',
},
],
'test_fn': _test_speechify,
},
{
'id': 'openai',
'name': 'OpenAI TTS',
'description': 'OpenAI TTS-1 and TTS-1-HD models (Alloy, Echo, Fable, Onyx, Nova, Shimmer voices).',
'docs_url': 'https://platform.openai.com/docs/guides/text-to-speech',
'category': 'tts',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'sk-...',
'required': True,
'env_override': 'OPENAI_API_KEY',
'help': 'Found at platform.openai.com -> API Keys',
},
{
'id': 'model',
'label': 'Model',
'type': 'select',
'default': 'tts-1',
'required': False,
'options': [
{'value': 'tts-1', 'label': 'TTS-1 (faster, lower latency)'},
{'value': 'tts-1-hd', 'label': 'TTS-1-HD (higher quality)'},
],
'help': 'TTS-1-HD produces better audio but costs more per character.',
},
{
'id': 'default_voice',
'label': 'Default Voice',
'type': 'select',
'default': 'onyx',
'required': False,
'options': [
{'value': 'alloy', 'label': 'Alloy - balanced, neutral'},
{'value': 'echo', 'label': 'Echo - clear, direct'},
{'value': 'fable', 'label': 'Fable - warm, narrative'},
{'value': 'onyx', 'label': 'Onyx - deep, authoritative'},
{'value': 'nova', 'label': 'Nova - bright, energetic'},
{'value': 'shimmer', 'label': 'Shimmer - soft, gentle'},
],
'help': 'Default voice when none is specified in a transmission.',
},
],
'test_fn': _test_openai,
},
{
'id': 'azure_tts',
'name': 'Azure Cognitive Services TTS',
'description': 'Microsoft Azure neural TTS with 400+ voices in 140+ languages.',
'docs_url': 'https://learn.microsoft.com/en-us/azure/ai-services/speech-service/text-to-speech',
'category': 'tts',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'Subscription Key',
'type': 'password',
'placeholder': '32-character hex key',
'required': True,
'env_override': 'AZURE_TTS_KEY',
'help': 'Azure portal -> Speech resource -> Keys and Endpoint -> KEY 1',
},
{
'id': 'region',
'label': 'Region',
'type': 'text',
'placeholder': 'eastus',
'required': True,
'env_override': 'AZURE_TTS_REGION',
'help': 'Azure region slug, e.g. eastus, westeurope, australiaeast',
},
{
'id': 'default_voice',
'label': 'Default Voice Name',
'type': 'text',
'placeholder': 'en-US-GuyNeural',
'required': False,
'help': 'Full voice name from Azure voice gallery, e.g. en-US-JennyNeural',
},
],
'test_fn': _test_azure,
},
{
'id': 'gemini',
'name': 'Google Gemini',
'description': 'Google Gemini generative AI models (Gemini 1.5 Flash, Pro, Ultra). Use for AI chat, generation, and analysis features.',
'docs_url': 'https://ai.google.dev/docs',
'category': 'llm',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'AIza...',
'required': True,
'env_override': 'GEMINI_API_KEY',
'help': 'Google AI Studio -> Get API Key (aistudio.google.com)',
},
{
'id': 'default_model',
'label': 'Default Model',
'type': 'select',
'default': 'gemini-1.5-flash',
'required': False,
'options': [
{'value': 'gemini-1.5-flash', 'label': 'Gemini 1.5 Flash (fast, efficient)'},
{'value': 'gemini-1.5-pro', 'label': 'Gemini 1.5 Pro (powerful)'},
{'value': 'gemini-2.0-flash', 'label': 'Gemini 2.0 Flash (latest fast)'},
],
'help': 'Model used when Gemini is selected for AI generation features.',
},
],
'test_fn': _test_gemini,
},
{
'id': 'openai_chat',
'name': 'OpenAI ChatGPT',
'description': 'OpenAI GPT-4o and GPT-4 chat models for AI generation, debate, and analysis features.',
'docs_url': 'https://platform.openai.com/docs/guides/chat-completions',
'category': 'llm',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'sk-...',
'required': True,
'env_override': 'OPENAI_API_KEY',
'help': 'platform.openai.com -> API Keys. Same key as OpenAI TTS if already set.',
},
{
'id': 'default_model',
'label': 'Default Model',
'type': 'select',
'default': 'gpt-4o-mini',
'required': False,
'options': [
{'value': 'gpt-4o-mini', 'label': 'GPT-4o Mini (fast, cheap)'},
{'value': 'gpt-4o', 'label': 'GPT-4o (powerful)'},
{'value': 'gpt-4-turbo', 'label': 'GPT-4 Turbo'},
{'value': 'gpt-3.5-turbo', 'label': 'GPT-3.5 Turbo (cheapest)'},
],
'help': 'Model used when OpenAI Chat is selected for AI generation features.',
},
],
'test_fn': _test_openai_chat,
},
{
'id': 'anthropic',
'name': 'Anthropic Claude',
'description': 'Anthropic Claude models for high-quality AI generation, analysis, and creative content.',
'docs_url': 'https://docs.anthropic.com/en/api',
'category': 'llm',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'sk-ant-...',
'required': True,
'env_override': 'ANTHROPIC_API_KEY',
'help': 'console.anthropic.com -> API Keys',
},
{
'id': 'default_model',
'label': 'Default Model',
'type': 'select',
'default': 'claude-3-5-haiku-latest',
'required': False,
'options': [
{'value': 'claude-3-5-haiku-latest', 'label': 'Claude 3.5 Haiku (fast, efficient)'},
{'value': 'claude-3-5-sonnet-latest', 'label': 'Claude 3.5 Sonnet (powerful)'},
{'value': 'claude-3-opus-latest', 'label': 'Claude 3 Opus (most capable)'},
],
'help': 'Model used when Claude is selected for AI generation features.',
},
],
'test_fn': _test_anthropic,
},
{
'id': 'mistral',
'name': 'Mistral AI',
'description': 'Mistral open and commercial LLMs. Good balance of speed and capability.',
'docs_url': 'https://docs.mistral.ai',
'category': 'llm',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'your-mistral-key',
'required': True,
'env_override': 'MISTRAL_API_KEY',
'help': 'console.mistral.ai -> API Keys',
},
{
'id': 'default_model',
'label': 'Default Model',
'type': 'select',
'default': 'mistral-small-latest',
'required': False,
'options': [
{'value': 'mistral-small-latest', 'label': 'Mistral Small (fast, cheap)'},
{'value': 'mistral-medium-latest', 'label': 'Mistral Medium'},
{'value': 'mistral-large-latest', 'label': 'Mistral Large (most capable)'},
],
'help': 'Model used when Mistral is selected for AI generation.',
},
],
'test_fn': _test_mistral,
},
{
'id': 'groq',
'name': 'Groq (Fast Inference)',
'description': 'Groq LPU inference - extremely fast responses for Llama 3, Mixtral, and Gemma. Free tier available.',
'docs_url': 'https://console.groq.com/docs/openai',
'category': 'llm',
'enabled': True,
'fields': [
{
'id': 'api_key',
'label': 'API Key',
'type': 'password',
'placeholder': 'gsk_...',
'required': True,
'env_override': 'GROQ_API_KEY',
'help': 'console.groq.com -> API Keys (free tier available)',
},
{
'id': 'default_model',
'label': 'Default Model',
'type': 'select',
'default': 'llama-3.3-70b-versatile',
'required': False,
'options': [
{'value': 'llama-3.3-70b-versatile', 'label': 'Llama 3.3 70B Versatile'},
{'value': 'llama-3.1-8b-instant', 'label': 'Llama 3.1 8B Instant (fastest)'},
{'value': 'mixtral-8x7b-32768', 'label': 'Mixtral 8x7B'},
{'value': 'gemma2-9b-it', 'label': 'Gemma 2 9B'},
],
'help': 'Groq provides very fast inference - great for real-time debate and generation.',
},
],
'test_fn': _test_groq,
},
{
'id': 'discord',
'name': 'Discord',
'description': 'Monitor a Discord channel and feed messages into Acid Trip agent context. Requires a bot token with MESSAGE_CONTENT intent enabled.',
'docs_url': 'https://discord.com/developers/docs/intro',
'category': 'integration',
'enabled': True,
'fields': [
{
'id': 'bot_token',
'label': 'Bot Token',
'type': 'password',
'placeholder': 'MTxxxxxxxxxxxxxxxxxxxxxxxx.Gxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'required': True,
'env_override': 'DISCORD_BOT_TOKEN',
'help': 'discord.com/developers/applications -> Your App -> Bot -> Token',
},
{
'id': 'guild_id',
'label': 'Server (Guild) ID',
'type': 'text',
'placeholder': '123456789012345678',
'required': True,
'env_override': 'DISCORD_GUILD_ID',
'help': 'Enable Developer Mode in Discord settings, then right-click server name -> Copy Server ID',
},
{
'id': 'channel_id',
'label': 'Channel ID',
'type': 'text',
'placeholder': '123456789012345678',
'required': True,
'env_override': 'DISCORD_CHANNEL_ID',
'help': 'Right-click channel name -> Copy Channel ID (requires Developer Mode)',
},
{
'id': 'poll_interval',
'label': 'Poll Interval (sec)',
'type': 'text',
'placeholder': '10',
'required': False,
'default': '10',
'help': 'How often to fetch new messages from the channel (5-300 seconds)',
},
{
'id': 'max_messages',
'label': 'Messages to Fetch',
'type': 'text',
'placeholder': '5',
'required': False,
'default': '5',
'help': 'Number of recent messages to retrieve per poll (1-20)',
},
],
'test_fn': _test_discord,
},
{
'id': 'fastkoko',
'name': 'FastKoko (Local)',
'description': 'Local Kokoro TTS server (FastAPI/OpenAI-compatible). Runs on your LAN — no API key required, just the server URL.',
'docs_url': 'https://github.com/remsky/Kokoro-FastAPI',
'category': 'integration',
'enabled': True,
'fields': [
{
'id': 'url',
'label': 'Server URL',
'type': 'text',
'placeholder': 'http://192.168.40.15:8880',
'required': True,
'env_override': 'FASTKOKO_URL',
'help': 'Base URL of your FastKoko server — no trailing slash. Voices fetched from /v1/audio/voices, synthesis via /v1/audio/speech.',
},
{
'id': 'default_voice',
'label': 'Default Voice',
'type': 'text',
'placeholder': 'af_sarah',
'required': False,
'env_override': 'FASTKOKO_VOICE',
'help': 'Default voice ID used when no voice is specified. Leave blank to use af_sarah.',
},
],
'test_fn': _test_fastkoko,
},
{
'id': 'discord_ctrl',
'name': 'Discord Control Channel',
'description': 'A separate Discord server/channel for operator commands. Uses the same bot token as the chaos monitor, but watches a different channel for !commands that control the running trip.',
'docs_url': '/trip/commands',
'category': 'integration',
'enabled': True,
'fields': [
{
'id': 'bot_token',
'label': 'Bot Token',
'type': 'password',
'placeholder': 'MTxxxxxxxxxxxxxxxxxxxxxxxx.Gxxxxx.xxxxxxxxxxxxxxxxxx',
'required': True,
'env_override': 'DISCORD_CTRL_BOT_TOKEN',
'help': 'Can be the same token as the chaos monitor bot — the bot just needs to be in the control server.',
},
{
'id': 'guild_id',
'label': 'Control Server (Guild) ID',
'type': 'text',
'placeholder': '123456789012345678',
'required': True,
'env_override': 'DISCORD_CTRL_GUILD_ID',
'help': 'Right-click server name in Discord (Developer Mode on) -> Copy Server ID',
},
{
'id': 'channel_id',
'label': 'Control Channel ID',
'type': 'text',
'placeholder': '123456789012345678',
'required': True,
'env_override': 'DISCORD_CTRL_CHANNEL_ID',
'help': 'Right-click the control channel -> Copy Channel ID',
},
{
'id': 'poll_interval',
'label': 'Poll Interval (s)',
'type': 'number',
'placeholder': '10',
'required': False,
'env_override': 'DISCORD_CTRL_POLL_INTERVAL',
'help': '5–300 seconds. Commands are processed within one poll interval.',
},
],
'test_fn': _test_discord_ctrl,
},
]
# Index by id for O(1) lookup
_REGISTRY_BY_ID: dict[str, dict] = {p['id']: p for p in PROVIDER_REGISTRY}
# ─────────────────────────────────────────────────────────────
# Key Storage
# ─────────────────────────────────────────────────────────────
def _obfuscate(value: str) -> str:
"""Light XOR obfuscation - not encryption, just avoids plaintext in logs."""
key = b'ic7610radioagent'
enc = bytearray()
for i, c in enumerate(value.encode()):
enc.append(c ^ key[i % len(key)])
return enc.hex()
def _deobfuscate(value: str) -> str:
try:
enc = bytes.fromhex(value)
key = b'ic7610radioagent'
return bytes(c ^ key[i % len(key)] for i, c in enumerate(enc)).decode()
except Exception:
return value # already plaintext (legacy)
def load_config() -> dict:
"""Load all provider configs from disk."""
try:
if Path(KEY_FILE).exists():
with open(KEY_FILE) as f:
return json.load(f)
except Exception as e:
logger.warning(f"Could not load API key config: {e}")
return {}
def save_config(config: dict) -> bool:
"""Save all provider configs to disk."""
try:
Path(KEY_FILE).parent.mkdir(parents=True, exist_ok=True)
with open(KEY_FILE, 'w') as f:
json.dump(config, f, indent=2)
os.chmod(KEY_FILE, 0o600) # owner read/write only
return True
except Exception as e:
logger.error(f"Could not save API key config: {e}")
return False
def get_provider_config(provider_id: str) -> dict:
"""
Get effective config for a provider, merging:
1. Stored values from disk
2. Environment variable overrides (highest priority)
Password fields are deobfuscated before returning.
"""
stored = load_config().get(provider_id, {})
provider = _REGISTRY_BY_ID.get(provider_id, {})
result = {}
for field in provider.get('fields', []):
fid = field['id']
env_var = field.get('env_override')
stored_v = stored.get(fid, '')
# Deobfuscate password fields
if field['type'] == 'password' and stored_v:
stored_v = _deobfuscate(stored_v)
# Env override wins
if env_var and os.environ.get(env_var, '').strip():
result[fid] = os.environ[env_var].strip()
result[f'_{fid}_source'] = 'env'
elif stored_v:
result[fid] = stored_v
result[f'_{fid}_source'] = 'stored'
else:
result[fid] = field.get('default', '')
result[f'_{fid}_source'] = 'default'
return result
def get_key(provider_id: str, field_id: str = 'api_key') -> str:
"""Convenience: get a single field value for a provider."""
return get_provider_config(provider_id).get(field_id, '')
def set_provider_config(provider_id: str, field_values: dict) -> bool:
"""
Save field values for a provider.
Password fields are obfuscated before storage.
"""
provider = _REGISTRY_BY_ID.get(provider_id)
if not provider:
logger.warning(f"Unknown provider: {provider_id}")
return False
config = load_config()
if provider_id not in config:
config[provider_id] = {}
for field in provider['fields']:
fid = field['id']
if fid in field_values:
val = str(field_values[fid]).strip()
if field['type'] == 'password' and val:
config[provider_id][fid] = _obfuscate(val)
else:
config[provider_id][fid] = val
return save_config(config)
def delete_provider_config(provider_id: str) -> bool:
"""Remove all stored config for a provider."""
config = load_config()
if provider_id in config:
del config[provider_id]
return save_config(config)
return True
def mask_value(value: str) -> str:
"""Return a masked version of a secret for display."""
if not value:
return ''
if len(value) <= 8:
return '*' * len(value)
return value[:4] + '*' * (len(value) - 8) + value[-4:]
def get_registry_for_api() -> list[dict]:
"""
Return provider registry in a form safe for the API -
includes field definitions but never actual key values.
Includes configured/unconfigured status and source info.
"""
result = []
config = load_config()
for provider in PROVIDER_REGISTRY:
pid = provider['id']
stored = config.get(pid, {})
cfg = get_provider_config(pid)
fields_out = []
for field in provider['fields']:
fid = field['id']
raw_val = cfg.get(fid, '')
source = cfg.get(f'_{fid}_source', 'default')
has_value = bool(raw_val and raw_val != field.get('default', ''))
f_out = {
'id': fid,
'label': field['label'],
'type': field['type'],
'placeholder': field.get('placeholder', ''),
'required': field.get('required', False),
'help': field.get('help', ''),
'has_value': has_value,
'source': source, # 'env', 'stored', or 'default'
'masked': mask_value(raw_val) if field['type'] == 'password' else (raw_val if not field.get('sensitive') else mask_value(raw_val)),
}
if field['type'] == 'select':
f_out['options'] = field.get('options', [])
f_out['current'] = raw_val or field.get('default', '')
fields_out.append(f_out)
# Overall configured status: all required fields have values
required_fields = [f for f in provider['fields'] if f.get('required')]
all_required_set = all(
bool(cfg.get(f['id'], '').strip())
for f in required_fields
)
result.append({
'id': pid,