-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudLLMService.js
More file actions
1460 lines (1285 loc) · 63.8 KB
/
cloudLLMService.js
File metadata and controls
1460 lines (1285 loc) · 63.8 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
/**
* guIDE Cloud LLM Service — Multi-provider cloud API integration
* Copyright (c) 2025-2026 Brendan Gray (GitHub: FileShot)
* All Rights Reserved. See LICENSE for terms.
*
* 26 providers: OpenAI, Anthropic, Google, xAI, OpenRouter, Cerebras, SambaNova,
* Groq, Together, Fireworks, NVIDIA, Cohere, Mistral, HuggingFace, Cloudflare,
* Perplexity, DeepSeek, AI21, DeepInfra, Hyperbolic, Novita, Moonshot, Upstage,
* Lepton, APIFreeLLM, GraySoft. Plus local Ollama.
*/
'use strict';
const https = require('https');
const http = require('http');
const { EventEmitter } = require('events');
const keepAliveAgent = new https.Agent({ keepAlive: true, maxSockets: 6, timeout: 60000 });
// ─── Provider endpoint map ────────────────────────────────────────────────────
const ENDPOINTS = {
graysoft: { host: 'pocket.graysoft.dev', path: '/api/v1/chat/completions' },
openai: { host: 'api.openai.com', path: '/v1/chat/completions' },
anthropic: { host: 'api.anthropic.com', path: '/v1/messages' },
google: { host: 'generativelanguage.googleapis.com', path: '/v1beta/openai/chat/completions' },
xai: { host: 'api.x.ai', path: '/v1/chat/completions' },
groq: { host: 'api.groq.com', path: '/openai/v1/chat/completions' },
openrouter: { host: 'openrouter.ai', path: '/api/v1/chat/completions' },
apifreellm: { host: 'apifreellm.com', path: '/api/v1/chat' },
cerebras: { host: 'api.cerebras.ai', path: '/v1/chat/completions' },
sambanova: { host: 'api.sambanova.ai', path: '/v1/chat/completions' },
together: { host: 'api.together.xyz', path: '/v1/chat/completions' },
fireworks: { host: 'api.fireworks.ai', path: '/inference/v1/chat/completions' },
nvidia: { host: 'integrate.api.nvidia.com', path: '/v1/chat/completions' },
cohere: { host: 'api.cohere.ai', path: '/compatibility/v1/chat/completions' },
mistral: { host: 'api.mistral.ai', path: '/v1/chat/completions' },
huggingface: { host: 'router.huggingface.co', path: '/v1/chat/completions' },
cloudflare: { host: 'api.cloudflare.com', path: null },
perplexity: { host: 'api.perplexity.ai', path: '/chat/completions' },
deepseek: { host: 'api.deepseek.com', path: '/v1/chat/completions' },
ai21: { host: 'api.ai21.com', path: '/studio/v1/chat/completions' },
deepinfra: { host: 'api.deepinfra.com', path: '/v1/openai/chat/completions' },
hyperbolic: { host: 'api.hyperbolic.xyz', path: '/v1/chat/completions' },
novita: { host: 'api.novita.ai', path: '/v3/openai/chat/completions' },
moonshot: { host: 'api.moonshot.cn', path: '/v1/chat/completions' },
upstage: { host: 'api.upstage.ai', path: '/v1/chat/completions' },
lepton: { host: 'emc.lepton.run', path: '/api/v1/chat/completions' },
};
// ─── Provider labels ──────────────────────────────────────────────────────────
const PROVIDER_LABELS = {
graysoft: 'GraySoft Cloud', openai: 'OpenAI', anthropic: 'Anthropic',
google: 'Google Gemini', xai: 'xAI Grok', openrouter: 'OpenRouter',
groq: 'Groq', apifreellm: 'APIFreeLLM', cerebras: 'Cerebras',
sambanova: 'SambaNova', together: 'Together AI', fireworks: 'Fireworks AI',
nvidia: 'NVIDIA NIM', cohere: 'Cohere', mistral: 'Mistral AI',
huggingface: 'Hugging Face', cloudflare: 'Cloudflare Workers AI',
perplexity: 'Perplexity', deepseek: 'DeepSeek', ai21: 'AI21 Labs',
deepinfra: 'DeepInfra', hyperbolic: 'Hyperbolic', novita: 'Novita AI',
moonshot: 'Moonshot AI', upstage: 'Upstage', lepton: 'Lepton AI',
ollama: 'Ollama (Local)',
};
// ─── Provider model catalogs ─────────────────────────────────────────────────
const PROVIDER_MODELS = {
graysoft: [
{ id: 'graysoft-cloud', name: 'GraySoft Cloud AI' },
],
openai: [
{ id: 'gpt-4.1', name: 'GPT-4.1 (Flagship)' },
{ id: 'gpt-4.1-mini', name: 'GPT-4.1 Mini' },
{ id: 'gpt-4o', name: 'GPT-4o' },
{ id: 'gpt-4o-mini', name: 'GPT-4o Mini' },
{ id: 'gpt-4-turbo', name: 'GPT-4 Turbo' },
],
anthropic: [
{ id: 'claude-sonnet-4-20250514', name: 'Claude Sonnet 4' },
{ id: 'claude-3-5-sonnet-20241022', name: 'Claude 3.5 Sonnet' },
{ id: 'claude-3-haiku-20240307', name: 'Claude 3 Haiku (Fast)' },
],
google: [
{ id: 'gemini-2.5-pro', name: 'Gemini 2.5 Pro (Free)' },
{ id: 'gemini-2.5-flash', name: 'Gemini 2.5 Flash (Free)' },
{ id: 'gemini-2.0-flash', name: 'Gemini 2.0 Flash (Free)' },
{ id: 'gemini-2.5-flash-lite', name: 'Gemini 2.5 Flash-Lite (Free)' },
{ id: 'gemini-2.0-flash-lite', name: 'Gemini 2.0 Flash-Lite (Free)' },
{ id: 'gemini-3-flash-preview', name: 'Gemini 3 Flash Preview (Free)' },
],
xai: [
{ id: 'grok-3', name: 'Grok 3' },
{ id: 'grok-3-mini', name: 'Grok 3 Mini' },
],
openrouter: [
{ id: 'meta-llama/llama-3.3-70b-instruct:free', name: 'Llama 3.3 70B (Free)' },
{ id: 'mistralai/mistral-small-3.1-24b-instruct:free', name: 'Mistral Small 3.1 (Free)' },
],
apifreellm: [
{ id: 'apifreellm', name: 'APIFreeLLM 200B+ (Free)' },
],
groq: [
{ id: 'llama-3.3-70b-versatile', name: 'Llama 3.3 70B (Free, 12K TPM, Default)' },
{ id: 'meta-llama/llama-4-maverick-17b-128e-instruct', name: 'Llama 4 Maverick 17B (Free)' },
{ id: 'meta-llama/llama-4-scout-17b-16e-instruct', name: 'Llama 4 Scout 17B (Free, 30K TPM)' },
{ id: 'moonshotai/kimi-k2-instruct-0905', name: 'Kimi K2 131K (Free, Moonshot AI)' },
{ id: 'openai/gpt-oss-120b', name: 'GPT-OSS 120B (Free)' },
{ id: 'openai/gpt-oss-20b', name: 'GPT-OSS 20B (Free, 1000 tps)' },
{ id: 'qwen/qwen3-32b', name: 'Qwen 3 32B (Free)' },
{ id: 'groq/compound', name: 'Groq Compound (Agentic, Tools)' },
{ id: 'llama-3.1-8b-instant', name: 'Llama 3.1 8B Instant (Free, 14K RPM)' },
],
cerebras: [
{ id: 'gpt-oss-120b', name: 'GPT-OSS 120B (Free, Default, 30 RPM/key)' },
{ id: 'llama3.1-8b', name: 'Llama 3.1 8B (Free, 30 RPM/key)' },
],
sambanova: [
{ id: 'Meta-Llama-3.3-70B-Instruct', name: 'Llama 3.3 70B (Free, Default)' },
{ id: 'DeepSeek-V3.1', name: 'DeepSeek V3.1 (Free)' },
{ id: 'DeepSeek-R1-0528', name: 'DeepSeek R1 Reasoning (Free)' },
{ id: 'Llama-4-Maverick-17B-128E-Instruct', name: 'Llama 4 Maverick 17B (Free)' },
{ id: 'Qwen3-32B', name: 'Qwen 3 32B (Free)' },
{ id: 'gpt-oss-120b', name: 'GPT-OSS 120B (Free)' },
],
together: [
{ id: 'meta-llama/Llama-3.3-70B-Instruct-Turbo', name: 'Llama 3.3 70B Turbo' },
{ id: 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo', name: 'Llama 3.1 405B Turbo' },
{ id: 'deepseek-ai/DeepSeek-R1', name: 'DeepSeek R1' },
{ id: 'Qwen/Qwen2.5-72B-Instruct-Turbo', name: 'Qwen 2.5 72B Turbo' },
{ id: 'mistralai/Mixtral-8x22B-Instruct-v0.1', name: 'Mixtral 8x22B' },
{ id: 'google/gemma-2-27b-it', name: 'Gemma 2 27B' },
],
fireworks: [
{ id: 'accounts/fireworks/models/llama-v3p3-70b-instruct', name: 'Llama 3.3 70B' },
{ id: 'accounts/fireworks/models/deepseek-r1', name: 'DeepSeek R1' },
{ id: 'accounts/fireworks/models/qwen2p5-72b-instruct', name: 'Qwen 2.5 72B' },
],
nvidia: [
{ id: 'meta/llama-3.3-70b-instruct', name: 'Llama 3.3 70B (Free)' },
{ id: 'deepseek/deepseek-r1', name: 'DeepSeek R1 (Free)' },
{ id: 'nvidia/llama-3.1-nemotron-70b-instruct', name: 'Nemotron 70B (Free)' },
{ id: 'qwen/qwq-32b', name: 'QWQ 32B Reasoning (Free)' },
],
cohere: [
{ id: 'command-a-03-2025', name: 'Command A (Free, Flagship)' },
{ id: 'command-r-plus', name: 'Command R+ (Free)' },
{ id: 'command-r', name: 'Command R (Free)' },
{ id: 'command-r7b-12-2024', name: 'Command R 7B (Free, Fast)' },
],
mistral: [
{ id: 'mistral-small-latest', name: 'Mistral Small 3.2 (Free)' },
{ id: 'mistral-large-latest', name: 'Mistral Large 3 (Free)' },
{ id: 'magistral-medium-2509', name: 'Magistral Medium 1.2 (Reasoning)' },
{ id: 'magistral-small-2509', name: 'Magistral Small 1.2 (Reasoning)' },
{ id: 'ministral-8b-latest', name: 'Ministral 8B (Free, Fast)' },
{ id: 'devstral-small-latest', name: 'Devstral Small (Code Agents)' },
{ id: 'mistral-nemo', name: 'Mistral Nemo 12B (Free)' },
{ id: 'pixtral-12b-2409', name: 'Pixtral 12B Vision (Free)' },
],
huggingface: [
{ id: 'deepseek-ai/DeepSeek-V3-0324', name: 'DeepSeek V3 (Free)' },
{ id: 'Qwen/Qwen3-235B-A22B', name: 'Qwen3 235B (Free)' },
{ id: 'meta-llama/Llama-3.3-70B-Instruct', name: 'Llama 3.3 70B (Free)' },
{ id: 'mistralai/Mistral-Small-3.1-24B-Instruct-2503', name: 'Mistral Small 3.1 (Free)' },
],
cloudflare: [
{ id: '@cf/meta/llama-3.3-70b-instruct-fp8-fast', name: 'Llama 3.3 70B (Free)' },
{ id: '@cf/qwen/qwq-32b', name: 'QWQ 32B Reasoning (Free)' },
{ id: '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b', name: 'DeepSeek R1 Distill 32B (Free)' },
{ id: '@cf/mistralai/mistral-small-3.1-24b-instruct', name: 'Mistral Small 3.1 (Free)' },
{ id: '@cf/google/gemma-3-12b-it', name: 'Gemma 3 12B (Free)' },
],
perplexity: [
{ id: 'sonar-pro', name: 'Sonar Pro (Web Search)' },
{ id: 'sonar', name: 'Sonar (Web Search, Fast)' },
{ id: 'sonar-reasoning-pro', name: 'Sonar Reasoning Pro' },
{ id: 'sonar-reasoning', name: 'Sonar Reasoning' },
{ id: 'r1-1776', name: 'R1-1776 (Offline, No Search)' },
],
deepseek: [
{ id: 'deepseek-chat', name: 'DeepSeek V3 (Flagship)' },
{ id: 'deepseek-reasoner', name: 'DeepSeek R1 (Reasoning)' },
],
ai21: [
{ id: 'jamba-2.5', name: 'Jamba 2.5 (256K context)' },
{ id: 'jamba-2.5-mini', name: 'Jamba 2.5 Mini (256K, Fast)' },
],
deepinfra: [
{ id: 'meta-llama/Llama-3.3-70B-Instruct', name: 'Llama 3.3 70B' },
{ id: 'deepseek-ai/DeepSeek-V3', name: 'DeepSeek V3' },
{ id: 'deepseek-ai/DeepSeek-R1', name: 'DeepSeek R1 (Reasoning)' },
{ id: 'Qwen/Qwen3-235B-A22B', name: 'Qwen3 235B MoE' },
{ id: 'Qwen/QwQ-32B', name: 'QwQ 32B Reasoning' },
],
hyperbolic: [
{ id: 'deepseek-ai/DeepSeek-V3', name: 'DeepSeek V3' },
{ id: 'deepseek-ai/DeepSeek-R1', name: 'DeepSeek R1 (Reasoning)' },
{ id: 'Qwen/Qwen3-235B-A22B', name: 'Qwen3 235B MoE' },
{ id: 'meta-llama/Llama-3.3-70B-Instruct', name: 'Llama 3.3 70B' },
],
novita: [
{ id: 'deepseek/deepseek-v3-0324', name: 'DeepSeek V3' },
{ id: 'deepseek/deepseek-r1', name: 'DeepSeek R1 (Reasoning)' },
{ id: 'meta-llama/llama-3.3-70b-instruct', name: 'Llama 3.3 70B' },
{ id: 'qwen/qwen3-235b-a22b', name: 'Qwen3 235B MoE' },
],
moonshot: [
{ id: 'moonshot-v1-8k', name: 'Moonshot v1 8K' },
{ id: 'moonshot-v1-32k', name: 'Moonshot v1 32K' },
{ id: 'moonshot-v1-128k', name: 'Moonshot v1 128K' },
{ id: 'kimi-k2', name: 'Kimi K2 (Agentic)' },
],
upstage: [
{ id: 'solar-pro2', name: 'Solar Pro 2 (Flagship)' },
{ id: 'solar-mini-ja', name: 'Solar Mini' },
],
lepton: [
{ id: 'llama3-3-70b', name: 'Llama 3.3 70B' },
{ id: 'deepseek-r1', name: 'DeepSeek R1 (Reasoning)' },
{ id: 'qwen3-235b', name: 'Qwen3 235B MoE' },
],
};
// ─── Context window limits (tokens) ──────────────────────────────────────────
const CONTEXT_LIMITS = {
'gpt-4.1': 1047576, 'gpt-4.1-mini': 1047576,
'gpt-4o': 128000, 'gpt-4o-mini': 128000, 'gpt-4-turbo': 128000,
'claude-sonnet-4-20250514': 200000, 'claude-3-5-sonnet-20241022': 200000, 'claude-3-haiku-20240307': 200000,
'gemini-2.5-pro': 1048576, 'gemini-2.5-flash': 1048576, 'gemini-2.0-flash': 1048576,
'gemini-2.5-flash-lite': 1048576, 'gemini-2.0-flash-lite': 1048576, 'gemini-3-flash-preview': 1048576,
'grok-3': 131072, 'grok-3-mini': 131072,
'llama-3.3-70b-versatile': 32768, 'llama-3.1-8b-instant': 8192,
'meta-llama/llama-4-maverick-17b-128e-instruct': 131072,
'meta-llama/llama-4-scout-17b-16e-instruct': 131072,
'moonshotai/kimi-k2-instruct': 131072,
'openai/gpt-oss-120b': 32768, 'qwen/qwen3-32b': 32768,
'gpt-oss-120b': 32768, 'llama3.1-8b': 8192,
'DeepSeek-V3.2': 65536, 'DeepSeek-V3.1': 65536, 'Meta-Llama-3.3-70B-Instruct': 8192,
'DeepSeek-R1-0528': 65536, 'Qwen3-235B': 32768, 'Qwen3-32B': 32768,
'Llama-4-Maverick-17B-128E-Instruct': 131072, 'MiniMax-M2.5': 65536,
'command-a-03-2025': 256000, 'command-r-plus': 128000, 'command-r': 128000, 'command-r7b-12-2024': 128000,
'mistral-small-latest': 32768, 'mistral-large-latest': 131072,
'ministral-8b-latest': 131072, 'mistral-nemo': 131072, 'pixtral-12b-2409': 131072,
'sonar-pro': 127072, 'sonar': 127072, 'sonar-reasoning-pro': 127072,
'sonar-reasoning': 127072, 'r1-1776': 128000,
'deepseek-chat': 65536, 'deepseek-reasoner': 65536,
'jamba-2.5': 262144, 'jamba-2.5-mini': 262144,
'moonshot-v1-8k': 8192, 'moonshot-v1-32k': 32768, 'moonshot-v1-128k': 131072, 'kimi-k2': 131072,
'solar-pro2': 32768, 'solar-mini-ja': 32768,
};
// ─── Vision-capable models per provider ──────────────────────────────────────
const VISION_MODELS = {
openai: ['gpt-4.1', 'gpt-4.1-mini', 'gpt-4o', 'gpt-4o-mini', 'gpt-4-turbo'],
anthropic: ['claude-sonnet-4-20250514', 'claude-3-5-sonnet-20241022', 'claude-3-haiku-20240307'],
google: ['gemini-2.5-pro', 'gemini-2.5-flash', 'gemini-2.0-flash', 'gemini-2.5-flash-lite', 'gemini-2.0-flash-lite', 'gemini-3-flash-preview'],
xai: ['grok-3', 'grok-3-mini'],
openrouter: ['google/gemini-2.0-flash-exp:free'],
mistral: ['pixtral-12b-2409'],
};
// ─── Fallback order + preferred fallback models ──────────────────────────────
const FALLBACK_ORDER = ['cerebras', 'sambanova', 'google', 'nvidia', 'cohere', 'mistral',
'huggingface', 'cloudflare', 'together', 'fireworks', 'openrouter', 'groq'];
const PREFERRED_FALLBACK_MODEL = {
cerebras: 'gpt-oss-120b',
sambanova: 'Meta-Llama-3.3-70B-Instruct',
openrouter: 'meta-llama/llama-3.3-70b-instruct:free',
groq: 'llama-3.3-70b-versatile',
google: 'gemini-2.5-flash',
};
// ─── Default RPM per-key estimates for free tiers ────────────────────────────
const DEFAULT_RPM = {
groq: 30, cerebras: 30, sambanova: 10, google: 15,
openrouter: 20, openai: 3, anthropic: 5, xai: 60,
};
// ─── Bundled key constants ───────────────────────────────────────────────────
const BUNDLED_PROVIDERS = new Set(['groq', 'cerebras', 'sambanova', 'google', 'openrouter']);
const BUNDLED_KEYS = {
groq: 'PSkxBSo0Fg4QaREdEgstG21qajwdFi9jDR0+IzhpHAMAbTkoChQUHx4vEiMjaRIYMAoKAzIVDWI=',
cerebras: 'OSkxdzRvNDwuNyNpNz8/OSMqIm8jMDwtLSMxaCwtPi1jLSxiOWI/OWMqLTIxbzQyKGM5NA==',
sambanova: 'Ozs/Pj9tOG53bmxoP3duaWhqdztrbmx3bTg8OG0+az9jbmlr',
google: 'GxMgOwkjGDAKAgwSYygiKCBoETssDz4wGxcVKw4WFR0MIxQoFhwD',
openrouter: 'KTF3NSh3LGt3bm1rP2xjYzs8az4/O2JrOD8+ajs8bm5tOz5ibTg/bWJvY2pvPGlpb2lrPmlpbGJja21pOT5qbjloaTs4Pz48Pg==',
};
const CEREBRAS_POOL_KEYS = [
'OSkxdzRvNDwuNyNpNz8/OSMqIm8jMDwtLSMxaCwtPi1jLSxiOWI/OWMqLTIxbzQyKGM5NA==',
'OSkxd2gqNDQtPzdpLixiPDEsaShpYiMoaG8/aTI8b2NjaSNvLDRiI2w/NDRvLC40LCJibw==',
'OSkxdy45PDwxMm4qLSJjKixoPDE0PjFuPi4saDciKi4qbC5sPzFvPD8uYzkyPyM0OWMyaA==',
'OSkxdzdsaDwtYi0uPjJsPCJuPD8wMSoiOWIyLjQibCJoPDcuNz40YmI+I2JjaGkjLSMwPw==',
'OSkxdzkyMTIyMCIqMDI0LippY2JpLjIoaGJvKDEwNG8jLWhvN25iLW8uIjFiPzQ+aCo3Yg==',
'OSkxd2lsYyg/KGkyaT4wLigjbD5uP2g/LTc5aC4xOSpvMjk+LiI5LCoqKDAwYio3KmwyNA==',
'OSkxdzwxLG4jaDcxMGJoMiwqPy0iPjkoOTkoKDRjYmJvIyowMGkib24wMi5ibDksaTxuLA==',
'OSkxd2lpIm5oKDcxKGluaS1uPzdsOTRjPz4iLioxPDxuPzwsLmluPD8tY2MobGgyPzI5Nw==',
'OSkxd2I0LjA+KGk+Py1pImIqLG8sOSI+Py1jNz9jMTciPD4iKDc3LSM5Ii4/Km8wMDwsaA==',
'OSkxdzIyPzI0bjRvbzkuLT5sMWljPC40b2g8P25sLS0sbzdsN2IxIjEyPzk5MD83LC1sLg==',
'OSkxd2w0MjkwYmhibigobDE+NzkwbCNuaCJjMiNsbGJuY2M/LmxjN28jPjw8Kj5jPGM5bg==',
'OSkxdz4qPygyPiJiLG9uaS05MG5oIz5oby43LGk+Mmk3I2kjPmMuLGw0bGMxOTA5LG4tPw==',
'OSkxdzlobzQ3PiwuOTQqN283Mi00KjIuKixjMj9pIippIzE+IjQtbGI3IzkjbD8uN28xIg==',
'OSkxdzdubzxoLSosMWw8OSIxaCMtLjQ+MjEjPj5jbm4yLi5ubzJpYyosaCxoNDE+OS5jMg==',
'OSkxd2gwKigiaD85LjwxPCguLCxpbC4wLi1jMihoaSIqaTQsPzI0aWg0PjkjP2IxOSJvIg==',
'OSkxd2ljLDk3NCpsNGg8Yi0+YjIoPDxsPjE+LCIibCo0aCgtMSo3Yj8qPz8/PGNjIj43LQ==',
'OSkxd29iPC40b2MsKCMxPy4yN24uYipub2wqLGkxYj8yLD8yPz4+bDxpaCM3Kj4sYywjOQ==',
'OSkxdzxvMSJuMGw0MGw8MD4ybmhsaCM3MWxvLDktKm5uaDxpIio/OTliLG4wLGkwKmkxMg==',
'OSkxd28qKiIuYiwtYiwtbmhpIjEiMGw3Yz4yPChvYjc+LipuaW40KCI+MTdpbyxjbyhiPw==',
'OSkxd29oPj83bChubj4oP280MWwqLi0uMTFoLjFvMjw8P2gwIy4iLGIjYmNjbGlpbzAtaA==',
'OSkxdz8taDkxLm4jbz9jLDluNy0wbDc5OWIyLGwwMm9jKDxvLG4iIypoPC03MSJjND9jMQ==',
];
const GROQ_POOL_KEYS = [
'PSkxBSo0Fg4QaREdEgstG21qajwdFi9jDR0+IzhpHAMAbTkoChQUHx4vEiMjaRIYMAoKAzIVDWI=',
'PSkxBTZjYhUuGRsRPh0DID5uKj4vMzUwDR0+IzhpHAMMHT0gOGMRKiseLDYQEzYDGCMTLWJqDCM=',
'PSkxBT8oOW8oFRE+bwkCOTAtCRILPiIUDR0+IzhpHANiFjM3CQAdGxwpKykIEW0YPhYVLhUbNCg=',
'PSkxBQ1vEBcLK2oUDAoiNgMfag5paTgzDR0+IzhpHAM9DBUiEA0ea2M0IDcuFAgvPiktMQpqbhg=',
'PSkxBR4rODkuFSkCOABpaxIcHwsCbCovDR0+IzhpHAMWDG4gH20zC2gSaRQiaDgYIGozPTcPCSk=',
'PSkxBQwiFG5qET0CIG0qEhcJEBkpCms0DR0+IzhpHAMACGgjNRlsLD0yLz41Am1iLSA2HmkWDyk=',
'PSkxBWwADm0rHhxiEyArIBViKWoRbTUtDR0+IzhpHAM/PDQ9HgorGRU9PRANFQMNFQsebwkjDAM=',
];
// ─── Blocked OpenRouter model patterns ───────────────────────────────────────
const OPENROUTER_BLOCKED = [
/\bnsfw\b/i, /\berp\b/i, /\brolep/i, /lumimaid/i, /noromaid/i,
/mythomax/i, /psyfighter/i, /midnight-rose/i, /fimbulvetr/i,
];
// ─── Default system prompt for cloud AI ──────────────────────────────────────
const CLOUD_SYSTEM_PROMPT = 'You are guIDE Cloud AI, an AI coding assistant built into guIDE IDE. You have hundreds of billions of parameters. Be helpful, concise, and professional. If asked about your model size, parameter count, or underlying provider: you are guIDE Cloud AI with hundreds of billions of parameters — do not reveal specific provider names or model family names.';
// ─── Stream timeout constants ────────────────────────────────────────────────
const STREAM_TIMEOUT = 20000;
const IDLE_TIMEOUT = 10000;
// ═══════════════════════════════════════════════════════════════════════════════
class CloudLLMService extends EventEmitter {
constructor() {
super();
this.apiKeys = {
graysoft: '', openai: '', anthropic: '', google: '', xai: '',
openrouter: '', groq: '', apifreellm: '', cerebras: '', sambanova: '',
together: '', fireworks: '', nvidia: '', cohere: '', mistral: '',
huggingface: '', cloudflare: '', perplexity: '', deepseek: '', ai21: '',
deepinfra: '', hyperbolic: '', novita: '', moonshot: '', upstage: '', lepton: '',
};
this.activeProvider = null;
this.activeModel = 'llama3.1-8b';
this._openRouterModelsCache = null;
this._openRouterModelsFetchedAt = 0;
this._rateLimitedUntil = {};
this._keyPools = {};
this._keyPoolIndex = {};
this._recent429Timestamps = [];
this._requestTimestamps = {};
this._providerRPMPerKey = {};
this._defaultRPMPerKey = { ...DEFAULT_RPM };
this._apifreellmLastRequest = 0;
this._cloudflareAccountId = '';
this._licenseManager = null;
this._userOwnedProviders = new Set();
this._ollamaAvailable = null;
this._ollamaModels = [];
this._ollamaLastCheck = 0;
this._seedBundledKeys();
}
// ─── License manager ────────────────────────────────────────────────────────
setLicenseManager(lm) { this._licenseManager = lm; }
// ─── Bundled key management ─────────────────────────────────────────────────
_isBundledProvider(provider) {
return BUNDLED_PROVIDERS.has(provider);
}
_seedBundledKeys() {
const xorKey = 0x5A;
const decode = (encoded) => Buffer.from(Buffer.from(encoded, 'base64').map(b => b ^ xorKey)).toString();
try {
for (const [provider, encoded] of Object.entries(BUNDLED_KEYS)) {
if (!this.apiKeys[provider] || !this.apiKeys[provider].trim()) {
this.apiKeys[provider] = decode(encoded);
}
}
for (const encoded of CEREBRAS_POOL_KEYS) {
this.addKeyToPool('cerebras', decode(encoded));
}
for (const encoded of GROQ_POOL_KEYS) {
this.addKeyToPool('groq', decode(encoded));
}
} catch (e) {
console.warn('[CloudLLM] Key seed error:', e.message);
}
}
// ─── Request tracking + RPM pacing ──────────────────────────────────────────
_recordRequest(provider) {
if (!this._requestTimestamps[provider]) this._requestTimestamps[provider] = [];
this._requestTimestamps[provider].push(Date.now());
}
_learnRPMFromHeaders(provider, headers) {
if (!headers) return;
const limitStr = headers['x-ratelimit-limit-requests']
|| headers['ratelimit-limit']
|| headers['x-ratelimit-limit-requests-minute'];
if (!limitStr) return;
const limit = parseInt(limitStr, 10);
if (limit > 0 && limit < 10000) {
const prev = this._providerRPMPerKey[provider];
this._providerRPMPerKey[provider] = limit;
if (prev !== limit) {
console.log(`[CloudLLM] Learned ${provider} RPM/key = ${limit} (was ${prev || 'default'})`);
}
}
}
_getPerKeyRPM(provider) {
return this._providerRPMPerKey[provider]
|| this._defaultRPMPerKey[provider]
|| 30;
}
getProactivePaceMs(provider) {
const perKeyRPM = this._getPerKeyRPM(provider);
const poolSize = this._keyPools[provider]?.length || 1;
const totalRPM = perKeyRPM * poolSize;
const targetRPM = totalRPM * 0.85;
const minIntervalMs = 60000 / targetRPM;
const stamps = this._requestTimestamps[provider] || [];
const now = Date.now();
const oneMinAgo = now - 60000;
const recentStamps = stamps.filter(t => t > oneMinAgo);
this._requestTimestamps[provider] = recentStamps;
const recentCount = recentStamps.length;
if (recentCount >= targetRPM) {
const oldestRecent = recentStamps[0];
const msUntilSlotFrees = (oldestRecent + 60000) - now;
return Math.max(msUntilSlotFrees, minIntervalMs);
}
if (recentCount > 0) {
const lastRequestMs = recentStamps[recentStamps.length - 1];
const elapsed = now - lastRequestMs;
if (elapsed < minIntervalMs) return minIntervalMs - elapsed;
}
return 0;
}
getRecommendedPaceMs(provider) {
const now = Date.now();
this._recent429Timestamps = this._recent429Timestamps.filter(t => now - t < 120000);
const recent429Count = this._recent429Timestamps.length;
if (recent429Count === 0) return 0;
if (recent429Count <= 2) return 2000;
if (recent429Count <= 5) return 5000;
return 10000;
}
// ─── API key management ─────────────────────────────────────────────────────
setApiKey(provider, key) {
if (provider === 'cloudflare' && key && key.includes(':')) {
const [accountId, token] = key.split(':');
this._cloudflareAccountId = accountId;
this.apiKeys.cloudflare = token;
} else {
this.apiKeys[provider] = key || '';
}
if (key && key.trim()) {
this._userOwnedProviders.add(provider);
}
}
isUsingOwnKey(provider) {
return this._userOwnedProviders.has(provider);
}
// ─── Key pool management ────────────────────────────────────────────────────
addKeyToPool(provider, key) {
if (!this._keyPools[provider]) this._keyPools[provider] = [];
if (this._keyPools[provider].some(e => e.key === key)) return;
this._keyPools[provider].push({ key, cooldownUntil: 0 });
if (!this._keyPoolIndex[provider]) this._keyPoolIndex[provider] = 0;
if (!this.apiKeys[provider] || !this.apiKeys[provider].trim()) {
this.apiKeys[provider] = key;
}
}
_getPoolKey(provider) {
const pool = this._keyPools[provider];
if (!pool || pool.length === 0) return null;
const now = Date.now();
const startIdx = this._keyPoolIndex[provider] || 0;
for (let i = 0; i < pool.length; i++) {
const idx = (startIdx + i) % pool.length;
if (pool[idx].cooldownUntil <= now) {
this._keyPoolIndex[provider] = (idx + 1) % pool.length;
return pool[idx].key;
}
}
let earliest = pool[0];
for (let i = 1; i < pool.length; i++) {
if (pool[i].cooldownUntil < earliest.cooldownUntil) earliest = pool[i];
}
return earliest.key;
}
_cooldownPoolKey(provider, key, durationMs) {
const pool = this._keyPools[provider];
if (!pool) return;
const entry = pool.find(e => e.key === key);
if (entry) entry.cooldownUntil = Date.now() + durationMs;
}
getPoolStatus(provider) {
const pool = this._keyPools[provider];
if (!pool) return { total: 0, available: 0, onCooldown: 0 };
const now = Date.now();
const available = pool.filter(e => e.cooldownUntil <= now).length;
return { total: pool.length, available, onCooldown: pool.length - available };
}
// ─── Provider catalog ───────────────────────────────────────────────────────
getConfiguredProviders() {
const providers = [];
for (const [provider, key] of Object.entries(this.apiKeys)) {
if (key && key.trim()) {
providers.push({ provider, label: PROVIDER_LABELS[provider] || provider });
}
}
// v2.2.10: GraySoft cloud is available when the user has a session token
const sessionToken = this._licenseManager?.getSessionToken();
if (sessionToken && !providers.find(p => p.provider === 'graysoft')) {
providers.push({ provider: 'graysoft', label: PROVIDER_LABELS['graysoft'] || 'GraySoft Cloud' });
}
if (this._ollamaAvailable) {
providers.push({ provider: 'ollama', label: 'Ollama (Local)' });
}
return providers;
}
getAllProviders() {
return Object.entries(PROVIDER_LABELS)
.filter(([p]) => p !== 'ollama')
.map(([provider, label]) => ({
provider,
label,
hasKey: !!(this.apiKeys[provider] && this.apiKeys[provider].trim()),
isFree: BUNDLED_PROVIDERS.has(provider),
}));
}
_getProviderLabel(provider) {
return PROVIDER_LABELS[provider] || provider;
}
_getProviderModels(provider) {
return PROVIDER_MODELS[provider] || [];
}
_getEndpoint(provider) {
const ep = ENDPOINTS[provider];
if (!ep) return null;
if (provider === 'cloudflare') {
return {
host: ep.host,
path: `/client/v4/accounts/${this._cloudflareAccountId || 'ACCOUNT_ID'}/ai/v1/chat/completions`,
};
}
return ep;
}
_supportsVision(provider, model) {
if (provider === 'ollama') {
return /llava|bakllava|qwen.*vl|minicpm.*v|moondream|internvl|cogvlm|-vl\b|\.vision\b|vision-/i.test(model);
}
const list = VISION_MODELS[provider] || [];
return list.some(m => model.includes(m) || m.includes(model));
}
_getModelContextLimit(provider, model) {
return CONTEXT_LIMITS[model] || 32768;
}
// ─── Ollama local LLM detection ─────────────────────────────────────────────
async detectOllama() {
const now = Date.now();
if (this._ollamaLastCheck && now - this._ollamaLastCheck < 30000) {
return this._ollamaAvailable;
}
return new Promise((resolve) => {
const req = http.request({
hostname: 'localhost', port: 11434, path: '/api/tags',
method: 'GET', timeout: 3000,
}, (res) => {
let data = '';
res.on('data', c => data += c);
res.on('end', () => {
try {
const parsed = JSON.parse(data);
this._ollamaModels = (parsed.models || []).map(m => ({
id: m.name, name: m.name,
size: m.size, modified: m.modified_at,
}));
this._ollamaAvailable = true;
} catch {
this._ollamaAvailable = false;
this._ollamaModels = [];
}
this._ollamaLastCheck = Date.now();
resolve(this._ollamaAvailable);
});
});
req.on('error', () => {
this._ollamaAvailable = false;
this._ollamaModels = [];
this._ollamaLastCheck = Date.now();
resolve(false);
});
req.on('timeout', () => { req.destroy(); resolve(false); });
req.end();
});
}
getOllamaModels() { return this._ollamaModels; }
getOllamaVisionModels() {
return this._ollamaModels.filter(m =>
/llava|bakllava|qwen.*vl|minicpm.*v|moondream|internvl|cogvlm/i.test(m.id)
);
}
// ─── OpenRouter live model catalog ──────────────────────────────────────────
async fetchOpenRouterModels() {
const now = Date.now();
if (this._openRouterModelsCache && now - this._openRouterModelsFetchedAt < 600000) {
return this._openRouterModelsCache;
}
return new Promise((resolve) => {
const req = https.request({
host: 'openrouter.ai', path: '/api/v1/models',
method: 'GET', timeout: 10000,
}, (res) => {
let data = '';
res.on('data', c => data += c);
res.on('end', () => {
try {
const parsed = JSON.parse(data);
const models = (parsed.data || [])
.filter(m => {
if (m.id.includes('/image') || m.id.includes('/audio')) return false;
if (OPENROUTER_BLOCKED.some(rx => rx.test(m.id) || rx.test(m.name || ''))) return false;
return true;
})
.map(m => ({
id: m.id,
name: m.name || m.id,
contextLength: m.context_length,
pricing: m.pricing,
isFree: m.pricing && parseFloat(m.pricing.prompt) === 0 && parseFloat(m.pricing.completion) === 0,
}))
.sort((a, b) => {
if (a.isFree && !b.isFree) return -1;
if (!a.isFree && b.isFree) return 1;
return (a.name || '').localeCompare(b.name || '');
});
this._openRouterModelsCache = models;
this._openRouterModelsFetchedAt = Date.now();
resolve(models);
} catch {
resolve(this._openRouterModelsCache || PROVIDER_MODELS.openrouter);
}
});
});
req.on('error', () => resolve(this._openRouterModelsCache || PROVIDER_MODELS.openrouter));
req.on('timeout', () => { req.destroy(); resolve(this._openRouterModelsCache || PROVIDER_MODELS.openrouter); });
req.end();
});
}
// ─── Context trimming ───────────────────────────────────────────────────────
_trimToContextLimit(messages, provider, model, maxTokens) {
const contextLimit = this._getModelContextLimit(provider, model);
const reserveForOutput = maxTokens || 2048;
const budgetChars = (contextLimit - reserveForOutput) * 3.5;
if (budgetChars <= 0) return messages;
let totalChars = 0;
for (const m of messages) {
const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content);
totalChars += content.length;
}
if (totalChars <= budgetChars) return messages;
const system = messages[0];
const user = messages[messages.length - 1];
const middle = messages.slice(1, -1);
const systemLen = (typeof system.content === 'string' ? system.content : JSON.stringify(system.content)).length;
const userLen = (typeof user.content === 'string' ? user.content : JSON.stringify(user.content)).length;
let remaining = budgetChars - systemLen - userLen;
const kept = [];
for (let i = middle.length - 1; i >= 0; i--) {
const content = typeof middle[i].content === 'string' ? middle[i].content : JSON.stringify(middle[i].content);
if (remaining - content.length > 0) {
kept.unshift(middle[i]);
remaining -= content.length;
} else {
break;
}
}
const trimmed = middle.length - kept.length;
if (trimmed > 0) {
console.log(`[CloudLLM] Auto-trimmed ${trimmed} oldest messages to fit ${model} context (${contextLimit} tokens)`);
}
return [system, ...kept, user];
}
// ─── Proxy routing ──────────────────────────────────────────────────────────
async _generateViaProxy(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, sessionToken) {
const messages = [
...conversationHistory.map(m => ({ role: m.role, content: m.content })),
{ role: 'user', content: prompt },
];
const proxyBody = JSON.stringify({
provider, model, messages, systemPrompt,
maxTokens: options.maxTokens || 2048,
temperature: options.temperature || 0.7,
stream: !!onToken,
});
try {
const result = await this._streamRequest(
'graysoft.dev', '/api/ai/proxy', sessionToken, proxyBody,
'openai', onToken, {}, onThinkingToken, provider
);
return { ...result, model, provider, viaProxy: true };
} catch (err) {
if (err.message && (err.message.includes('quota_exceeded') || err.message.includes('429'))) {
const e = new Error(err.message);
e.isQuotaError = true;
throw e;
}
console.warn(`[CloudLLM] Proxy request failed for ${provider}, falling through to direct:`, err.message?.substring(0, 120));
throw err;
}
}
// ─── Main generate entry point ──────────────────────────────────────────────
async generate(prompt, options = {}) {
const provider = options.provider || this.activeProvider;
const model = options.model || this.activeModel;
console.log(`[CloudLLM] generate called: provider=${provider}, model=${model}, hasKey=${!!this.apiKeys[provider]}, promptLen=${prompt?.length || 0}, stream=${options.stream}`);
const systemPrompt = options.systemPrompt || CLOUD_SYSTEM_PROMPT;
const onToken = options.onToken;
const onThinkingToken = options.onThinkingToken || null;
const conversationHistory = options.conversationHistory || [];
const images = options.images || [];
const noFallback = options.noFallback || false;
if (!provider || (!this.apiKeys[provider] && provider !== 'ollama' && provider !== 'graysoft')) {
throw new Error(`No API key configured for ${provider}`);
}
if (provider === 'ollama') {
return this._generateOllama(model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images);
}
// v2.2.10: GraySoft uses session token as auth — set it as the API key for this request
const sessionToken = this._licenseManager?.getSessionToken();
if (provider === 'graysoft') {
if (!sessionToken) {
throw new Error('GraySoft Cloud requires a GraySoft account. Create one in Settings > Account.');
}
this.apiKeys['graysoft'] = sessionToken;
}
if (sessionToken && (this._isBundledProvider(provider) || provider === 'graysoft') && !(images && images.length > 0) && !options.skipProxy) {
try {
return await this._generateViaProxy(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, sessionToken);
} catch (err) {
if (err.isQuotaError) throw err;
console.warn('[CloudLLM] Proxy unreachable, using direct bundled key as fallback');
}
}
if (images && images.length > 0 && !this._supportsVision(provider, model)) {
if (onToken) {
onToken(`\n\n*Note: ${model} does not support image input. Images will be ignored. Use a vision-capable model like GPT-4o, Claude Sonnet 4, or Gemini 2.5 for image analysis.*\n\n`);
}
options.images = [];
}
const now = Date.now();
let providerOnCooldown = false;
if (this._rateLimitedUntil[provider] && this._rateLimitedUntil[provider] > now) {
const pool = this._keyPools[provider];
const hasAvailableKey = pool && pool.some(e => e.cooldownUntil <= now);
if (hasAvailableKey) {
console.log(`[CloudLLM] ${provider} provider cooldown but pool has available keys, retrying...`);
delete this._rateLimitedUntil[provider];
} else {
providerOnCooldown = true;
const waitSec = Math.ceil((this._rateLimitedUntil[provider] - now) / 1000);
console.log(`[CloudLLM] ${provider} on cooldown for ${waitSec}s, switching seamlessly to next provider`);
}
}
if (!providerOnCooldown) {
const result = await this._attemptWithPoolRotation(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images, noFallback);
if (result) return result;
}
// R47-Fix-E: Only respect noFallback for actual rate limits (429).
// For transient server errors (5xx), always try fallback chain — the user
// benefits from transparent failover rather than a misleading "Rate limited" error.
if (noFallback && !this._lastAttemptWasServerError) {
throw new Error(`Rate limited on all available providers. Please wait a minute and try again.`);
}
return this._attemptFallbackChain(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images);
}
async _attemptWithPoolRotation(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images, noFallback) {
this._lastAttemptWasServerError = false; // R47-Fix-E: Track whether failure was 5xx
const proactivePace = this.getProactivePaceMs(provider);
if (proactivePace > 0) {
console.log(`[CloudLLM] Proactive pacing: waiting ${proactivePace}ms before ${provider} request (RPM budget management)`);
await new Promise(r => setTimeout(r, proactivePace));
}
const pool = this._keyPools[provider];
const maxRetries = pool ? pool.length : 1;
for (let attempt = 0; attempt < maxRetries; attempt++) {
const attemptKey = this._getPoolKey(provider) || this.apiKeys[provider];
if (!attemptKey) break;
this._recordRequest(provider);
try {
return await this._executeGeneration(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images, attemptKey);
} catch (err) {
// Classify errors using actual HTTP status codes when available,
// falling back to string matching only when no status code is present
const msg = err.message || '';
const msgLower = msg.toLowerCase();
const statusCode = err.status || err.statusCode || err.response?.status || 0;
let is429, is403, is5xx;
if (statusCode > 0) {
// Use the actual HTTP status code — no string matching needed
is429 = statusCode === 429 || statusCode === 401 || statusCode === 413;
is403 = !is429 && statusCode === 403;
is5xx = !is429 && !is403 && statusCode >= 500 && statusCode < 600;
} else {
// Fallback: string matching only when no status code is available
is429 = msg.includes('429') || msg.includes('401') || msg.includes('413') || msgLower.includes('rate limit') || msgLower.includes('unauthorized') || msgLower.includes('too large') || msgLower.includes('tokens per minute');
is403 = !is429 && (msg.includes('403') || msgLower.includes('forbidden'));
is5xx = !is429 && !is403 && (msg.includes('500') || msg.includes('502') || msg.includes('503') || msg.includes('ECONNRESET') || msgLower.includes('timeout'));
}
if (is429) {
this._recent429Timestamps.push(Date.now());
this._cooldownPoolKey(provider, attemptKey, 60000);
if (pool && pool.length > 1 && attempt < maxRetries - 1) {
console.log(`[CloudLLM] 429 on ${provider} key ...${attemptKey.slice(-6)}, rotating (attempt ${attempt + 2}/${maxRetries})`);
continue;
}
this._rateLimitedUntil[provider] = Date.now() + 60000;
console.log(`[CloudLLM] 429 on ${provider} (all pool keys exhausted), cooldown for 60s`);
if (noFallback) {
const label = (this._isBundledProvider(provider) && !this.isUsingOwnKey(provider))
? 'guIDE Cloud AI'
: this._getProviderLabel(provider);
throw new Error(`${label} rate limited. Please wait a minute or try a different model.`);
}
return null;
}
if (is403 || is5xx) {
const cooldownMs = is403 ? 300000 : 60000;
this._rateLimitedUntil[provider] = Date.now() + cooldownMs;
this._lastAttemptWasServerError = true; // R47-Fix-E: 5xx/403 should still try fallback
console.log(`[CloudLLM] ${msg.substring(0, 80)} on ${provider}, cooldown ${cooldownMs / 1000}s, falling to fallback chain`);
return null;
}
throw err;
}
}
return null;
}
async _attemptFallbackChain(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images) {
const fallbackChain = [];
const now = Date.now();
if (provider === 'google') {
const altModels = (PROVIDER_MODELS.google || []).map(m => m.id).filter(m => m !== model);
for (const altModel of altModels) {
fallbackChain.push({ provider: 'google', model: altModel });
}
}
for (const p of FALLBACK_ORDER) {
if (p === provider) continue;
if (!this.apiKeys[p]) continue;
if (this._rateLimitedUntil[p] && this._rateLimitedUntil[p] > now) continue;
const pModel = PREFERRED_FALLBACK_MODEL[p] || (PROVIDER_MODELS[p] || [])[0]?.id;
if (pModel) fallbackChain.push({ provider: p, model: pModel });
}
for (const fb of fallbackChain) {
if (fb.provider !== 'google' && this._rateLimitedUntil[fb.provider] && this._rateLimitedUntil[fb.provider] > Date.now()) continue;
console.log(`[CloudLLM] Falling back to ${fb.provider}/${fb.model}`);
await new Promise(r => setTimeout(r, 0));
try {
const result = await this._executeGeneration(fb.provider, fb.model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images);
result.fallbackUsed = { from: provider, to: fb.provider, model: fb.model };
return result;
} catch (fbErr) {
const msg = fbErr.message || '';
const msgLower = msg.toLowerCase();
if (msg.includes('429') || msg.includes('413') || msgLower.includes('rate limit') || msgLower.includes('too large') || msgLower.includes('tokens per minute')) {
this._rateLimitedUntil[fb.provider] = Date.now() + 60000;
console.log(`[CloudLLM] Fallback ${fb.provider} also rate limited (cooldown 60s), trying next...`);
continue;
}
if (msg.includes('403') || msgLower.includes('forbidden')) {
this._rateLimitedUntil[fb.provider] = Date.now() + 300000;
console.log(`[CloudLLM] Fallback ${fb.provider} 403 forbidden, cooldown 5min, trying next...`);
continue;
}
if (msg.includes('500') || msg.includes('502') || msg.includes('503') || msgLower.includes('timeout') || msg.includes('ECONNRESET')) {
console.log(`[CloudLLM] Fallback ${fb.provider} transient error: ${msg.substring(0, 100)}, trying next...`);
continue;
}
throw fbErr;
}
}
throw new Error('Rate limited on all available providers. Please wait a minute and try again.');
}
// ─── Provider routing ───────────────────────────────────────────────────────
_executeGeneration(provider, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images = [], overrideKey = null) {
console.log(`[CloudLLM] _executeGeneration: provider=${provider}, model=${model}, isOllama=${provider === 'ollama'}, isAnthropic=${provider === 'anthropic'}, stream=${!!onToken}, historyLen=${conversationHistory?.length || 0}`);
if (provider === 'ollama') {
return this._generateOllama(model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images);
}
const apiKey = overrideKey || this._getPoolKey(provider) || this.apiKeys[provider];
if (!apiKey) throw new Error(`No API key configured for ${provider}`);
if (provider === 'apifreellm') {
return this._generateAPIFreeLLM(apiKey, systemPrompt, prompt, options, onToken, conversationHistory);
}
if (provider === 'anthropic') {
return this._generateAnthropic(apiKey, model, systemPrompt, prompt, options, onToken, conversationHistory, onThinkingToken, images);