w-dispatch-ai 1.0.0 → 1.0.1

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.
package/docs/global.html CHANGED
@@ -29,7 +29,7 @@
29
29
  <nav >
30
30
 
31
31
 
32
- <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#WDispatchAi">WDispatchAi</a></li><li><a href="global.html#adapters">adapters</a></li><li><a href="global.html#dispatchAi">dispatchAi</a></li><li><a href="global.html#dispatchClaude">dispatchClaude</a></li><li><a href="global.html#dispatchCodex">dispatchCodex</a></li><li><a href="global.html#dispatchOpencode">dispatchOpencode</a></li><li><a href="global.html#getCliArgs">getCliArgs</a></li><li><a href="global.html#getErrorResult">getErrorResult</a></li></ul>
32
+ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#WDispatchAi">WDispatchAi</a></li><li><a href="global.html#adapters">adapters</a></li><li><a href="global.html#dispatchAi">dispatchAi</a></li><li><a href="global.html#dispatchAiFallback">dispatchAiFallback</a></li><li><a href="global.html#dispatchClaude">dispatchClaude</a></li><li><a href="global.html#dispatchCodex">dispatchCodex</a></li><li><a href="global.html#dispatchOpencode">dispatchOpencode</a></li><li><a href="global.html#getCliArgs">getCliArgs</a></li><li><a href="global.html#getErrorResult">getErrorResult</a></li><li><a href="global.html#isKeyIndependentFail">isKeyIndependentFail</a></li></ul>
33
33
 
34
34
  </nav>
35
35
 
@@ -140,7 +140,7 @@
140
140
 
141
141
  <dt class="tag-source">Source:</dt>
142
142
  <dd class="tag-source"><ul class="dummy"><li>
143
- <a href="WDispatchAi.mjs.html">WDispatchAi.mjs</a>, <a href="WDispatchAi.mjs.html#line29">line 29</a>
143
+ <a href="WDispatchAi.mjs.html">WDispatchAi.mjs</a>, <a href="WDispatchAi.mjs.html#line30">line 30</a>
144
144
  </li></ul></dd>
145
145
 
146
146
 
@@ -191,7 +191,7 @@
191
191
 
192
192
  <h5 class="h5-examples">Example</h5>
193
193
 
194
- <pre class="prettyprint"><code>詳見dispatchAi、dispatchOpencode、dispatchClaude、dispatchCodex範例</code></pre>
194
+ <pre class="prettyprint"><code>詳見dispatchAi、dispatchAiFallback、dispatchOpencode、dispatchClaude、dispatchCodex範例</code></pre>
195
195
 
196
196
 
197
197
 
@@ -563,6 +563,719 @@ await test()
563
563
 
564
564
 
565
565
 
566
+ <h4 class="name" id="dispatchAiFallback"><span class="type-signature type-signature-async">(async) </span>dispatchAiFallback<span class="signature">(prompt, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
567
+
568
+
569
+
570
+
571
+
572
+
573
+ <dl class="details">
574
+
575
+ <dt class="tag-description">Description:</dt>
576
+ <dd class="tag-description"><ul class="dummy"><li><p>依供應商清單順序自動遞補調用AI,組內多金鑰以游標輪替</p>
577
+ <p>特點:
578
+ providers陣列順序即優先序,排前面的先用;
579
+ 條目本身即該次調用之opt(除id與keys外原樣透傳對應轉接器),與dispatchAi「條目直接當opt」同一約定;
580
+ 條目給予keys(多把金鑰)時以游標輪替,某把失敗自動換下一把,全數失敗才遞補下一組;
581
+ 與金鑰無關之失敗(逾時/執行檔不存在/參數錯誤/輸出未過驗證/未知kind)直接整組跳過,不逐把空耗;
582
+ 跨次執行僅記憶游標(經store注入持久化),不設金鑰停用清單——額度視窗形態多樣(5小時滾動/逐時/逐日),
583
+ 停用會把已恢復的金鑰閒置,而重探的代價僅一次快速失敗;
584
+ 本函數不會reject,一律以結果物件之ok與error欄位回報成敗</p></li></ul></dd>
585
+
586
+
587
+
588
+ <dt class="tag-source">Source:</dt>
589
+ <dd class="tag-source"><ul class="dummy"><li>
590
+ <a href="dispatchAiFallback.mjs.html">dispatchAiFallback.mjs</a>, <a href="dispatchAiFallback.mjs.html#line152">line 152</a>
591
+ </li></ul></dd>
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
+ </dl>
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+ <h5 class="h5-examples">Example</h5>
636
+
637
+ <pre class="prettyprint"><code>//need opencode, claude, codex cli in system PATH
638
+
639
+ import dispatchAiFallback from './src/dispatchAiFallback.mjs'
640
+
641
+ let test = async () => {
642
+
643
+ let r = await dispatchAiFallback('請只回覆兩個字:完成', {
644
+ providers: [
645
+ {
646
+ id: 'deepseek',
647
+ kind: 'opencode',
648
+ model: 'opencode/deepseek-v4-flash-free',
649
+ provider: 'opencode',
650
+ keys: ['sk-aaa', 'sk-bbb'], //多把金鑰, 某把失敗自動換下一把
651
+ timeoutMs: 180000,
652
+ },
653
+ { id: 'claude', kind: 'claude', model: 'sonnet' }, //deepseek全敗時遞補
654
+ { id: 'codex', kind: 'codex', model: 'gpt-5.6-luna', sandbox: 'read-only' },
655
+ ],
656
+ budgetMs: 600000,
657
+ onEvent: (ev) => console.log(ev.type, ev.providerId, ev.keyIndex),
658
+ })
659
+ console.log(r.ok, r.providerId, r.keyIndex, r.tried.length)
660
+ // => true 'deepseek' 0 1
661
+
662
+ }
663
+ await test()
664
+ .catch((err) => {
665
+ console.log(err)
666
+ })</code></pre>
667
+
668
+
669
+
670
+
671
+ <h5 class="h5-parameters">Parameters:</h5>
672
+
673
+
674
+ <table class="params">
675
+ <thead>
676
+ <tr>
677
+
678
+ <th>Name</th>
679
+
680
+
681
+ <th>Type</th>
682
+
683
+
684
+ <th>Attributes</th>
685
+
686
+
687
+
688
+ <th>Default</th>
689
+
690
+
691
+ <th class="last">Description</th>
692
+ </tr>
693
+ </thead>
694
+
695
+ <tbody>
696
+
697
+
698
+ <tr>
699
+
700
+ <td class="name"><code>prompt</code></td>
701
+
702
+
703
+ <td class="type">
704
+
705
+
706
+ <span class="param-type">String</span>
707
+
708
+
709
+
710
+
711
+ </td>
712
+
713
+
714
+ <td class="attributes">
715
+
716
+
717
+
718
+
719
+
720
+ </td>
721
+
722
+
723
+
724
+ <td class="default">
725
+
726
+ </td>
727
+
728
+
729
+ <td class="description last"><p>輸入提示詞字串,一律以stdin傳入子進程</p></td>
730
+ </tr>
731
+
732
+
733
+
734
+ <tr>
735
+
736
+ <td class="name"><code>opt</code></td>
737
+
738
+
739
+ <td class="type">
740
+
741
+
742
+ <span class="param-type">Object</span>
743
+
744
+
745
+
746
+
747
+ </td>
748
+
749
+
750
+ <td class="attributes">
751
+
752
+ &lt;optional><br>
753
+
754
+
755
+
756
+
757
+
758
+ </td>
759
+
760
+
761
+
762
+ <td class="default">
763
+
764
+ <code>{}</code>
765
+
766
+ </td>
767
+
768
+
769
+ <td class="description last"><p>輸入設定物件,預設{}</p>
770
+ <h6>Properties</h6>
771
+
772
+
773
+ <table class="params">
774
+ <thead>
775
+ <tr>
776
+
777
+ <th>Name</th>
778
+
779
+
780
+ <th>Type</th>
781
+
782
+
783
+ <th>Attributes</th>
784
+
785
+
786
+
787
+ <th>Default</th>
788
+
789
+
790
+ <th class="last">Description</th>
791
+ </tr>
792
+ </thead>
793
+
794
+ <tbody>
795
+
796
+
797
+ <tr>
798
+
799
+ <td class="name"><code>providers</code></td>
800
+
801
+
802
+ <td class="type">
803
+
804
+
805
+ <span class="param-type">Array</span>
806
+
807
+
808
+
809
+
810
+ </td>
811
+
812
+
813
+ <td class="attributes">
814
+
815
+
816
+
817
+
818
+
819
+ </td>
820
+
821
+
822
+
823
+ <td class="default">
824
+
825
+ </td>
826
+
827
+
828
+ <td class="description last"><p>輸入供應商條目物件陣列,順序即優先序。各條目除下列鍵外,其餘鍵(kind、model、exe、provider、config、sandbox、timeoutMs等)即該條目之opt原樣透傳對應轉接器</p>
829
+ <h6>Properties</h6>
830
+
831
+
832
+ <table class="params">
833
+ <thead>
834
+ <tr>
835
+
836
+ <th>Name</th>
837
+
838
+
839
+ <th>Type</th>
840
+
841
+
842
+ <th>Attributes</th>
843
+
844
+
845
+
846
+ <th>Default</th>
847
+
848
+
849
+ <th class="last">Description</th>
850
+ </tr>
851
+ </thead>
852
+
853
+ <tbody>
854
+
855
+
856
+ <tr>
857
+
858
+ <td class="name"><code>id</code></td>
859
+
860
+
861
+ <td class="type">
862
+
863
+
864
+ <span class="param-type">String</span>
865
+
866
+
867
+
868
+
869
+ </td>
870
+
871
+
872
+ <td class="attributes">
873
+
874
+ &lt;optional><br>
875
+
876
+
877
+
878
+
879
+
880
+ </td>
881
+
882
+
883
+
884
+ <td class="default">
885
+
886
+ <code>條目索引字串</code>
887
+
888
+ </td>
889
+
890
+
891
+ <td class="description last"><p>輸入群組識別字串,游標以此為鍵,多金鑰條目應給予穩定id,預設為條目索引字串</p></td>
892
+ </tr>
893
+
894
+
895
+
896
+ <tr>
897
+
898
+ <td class="name"><code>keys</code></td>
899
+
900
+
901
+ <td class="type">
902
+
903
+
904
+ <span class="param-type">Array</span>
905
+
906
+
907
+
908
+
909
+ </td>
910
+
911
+
912
+ <td class="attributes">
913
+
914
+ &lt;optional><br>
915
+
916
+
917
+
918
+
919
+
920
+ </td>
921
+
922
+
923
+
924
+ <td class="default">
925
+
926
+ <code>[]</code>
927
+
928
+ </td>
929
+
930
+
931
+ <td class="description last"><p>輸入同一服務之多把API key字串陣列,逐次注入輪替(kind為opencode時須同時於條目給予provider),省略代表沿用CLI既有登入狀態之單一虛擬金鑰</p></td>
932
+ </tr>
933
+
934
+
935
+ </tbody>
936
+ </table>
937
+
938
+ </td>
939
+ </tr>
940
+
941
+
942
+
943
+ <tr>
944
+
945
+ <td class="name"><code>budgetMs</code></td>
946
+
947
+
948
+ <td class="type">
949
+
950
+
951
+ <span class="param-type">Number</span>
952
+
953
+
954
+
955
+
956
+ </td>
957
+
958
+
959
+ <td class="attributes">
960
+
961
+ &lt;optional><br>
962
+
963
+
964
+
965
+
966
+
967
+ </td>
968
+
969
+
970
+
971
+ <td class="default">
972
+
973
+ <code>null</code>
974
+
975
+ </td>
976
+
977
+
978
+ <td class="description last"><p>輸入整輪遞補之時間上限毫秒正整數,剩餘預算會壓進每次呼叫之timeoutMs,預設null代表不限</p></td>
979
+ </tr>
980
+
981
+
982
+
983
+ <tr>
984
+
985
+ <td class="name"><code>minAttemptMs</code></td>
986
+
987
+
988
+ <td class="type">
989
+
990
+
991
+ <span class="param-type">Number</span>
992
+
993
+
994
+
995
+
996
+ </td>
997
+
998
+
999
+ <td class="attributes">
1000
+
1001
+ &lt;optional><br>
1002
+
1003
+
1004
+
1005
+
1006
+
1007
+ </td>
1008
+
1009
+
1010
+
1011
+ <td class="default">
1012
+
1013
+ <code>20000</code>
1014
+
1015
+ </td>
1016
+
1017
+
1018
+ <td class="description last"><p>輸入單次嘗試之最低剩餘預算毫秒正整數,剩餘低於此值即停止嘗試回報budget exhausted,預設20000</p></td>
1019
+ </tr>
1020
+
1021
+
1022
+
1023
+ <tr>
1024
+
1025
+ <td class="name"><code>store</code></td>
1026
+
1027
+
1028
+ <td class="type">
1029
+
1030
+
1031
+ <span class="param-type">Object</span>
1032
+
1033
+
1034
+
1035
+
1036
+ </td>
1037
+
1038
+
1039
+ <td class="attributes">
1040
+
1041
+ &lt;optional><br>
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+ </td>
1048
+
1049
+
1050
+
1051
+ <td class="default">
1052
+
1053
+ <code>null</code>
1054
+
1055
+ </td>
1056
+
1057
+
1058
+ <td class="description last"><p>輸入狀態持久化物件{get:()=&gt;state,set:(state)=&gt;{}},state內含cursors(逐群組游標),省略代表用行程內記憶體(跨呼叫有效,重啟歸零)。假定單行程序列調用,並行請自行加鎖</p></td>
1059
+ </tr>
1060
+
1061
+
1062
+
1063
+ <tr>
1064
+
1065
+ <td class="name"><code>onEvent</code></td>
1066
+
1067
+
1068
+ <td class="type">
1069
+
1070
+
1071
+ <span class="param-type">function</span>
1072
+
1073
+
1074
+
1075
+
1076
+ </td>
1077
+
1078
+
1079
+ <td class="attributes">
1080
+
1081
+ &lt;optional><br>
1082
+
1083
+
1084
+
1085
+
1086
+
1087
+ </td>
1088
+
1089
+
1090
+
1091
+ <td class="default">
1092
+
1093
+ <code>null</code>
1094
+
1095
+ </td>
1096
+
1097
+
1098
+ <td class="description last"><p>輸入事件回調函數(ev)=&gt;{},ev.type可為'try'、'ok'、'next-key'、'skip-group'、'budget-out',回調拋出例外不影響主流程,預設null</p></td>
1099
+ </tr>
1100
+
1101
+
1102
+
1103
+ <tr>
1104
+
1105
+ <td class="name"><code>timeoutMs</code></td>
1106
+
1107
+
1108
+ <td class="type">
1109
+
1110
+
1111
+ <span class="param-type">Number</span>
1112
+
1113
+
1114
+
1115
+
1116
+ </td>
1117
+
1118
+
1119
+ <td class="attributes">
1120
+
1121
+ &lt;optional><br>
1122
+
1123
+
1124
+
1125
+
1126
+
1127
+ </td>
1128
+
1129
+
1130
+
1131
+ <td class="default">
1132
+
1133
+ <code>120000</code>
1134
+
1135
+ </td>
1136
+
1137
+
1138
+ <td class="description last"><p>輸入各attempt共用之逾時毫秒正整數,條目可覆寫,預設120000</p></td>
1139
+ </tr>
1140
+
1141
+
1142
+
1143
+ <tr>
1144
+
1145
+ <td class="name"><code>validate</code></td>
1146
+
1147
+
1148
+ <td class="type">
1149
+
1150
+
1151
+ <span class="param-type">String</span>
1152
+ |
1153
+
1154
+ <span class="param-type">function</span>
1155
+
1156
+
1157
+
1158
+
1159
+ </td>
1160
+
1161
+
1162
+ <td class="attributes">
1163
+
1164
+ &lt;optional><br>
1165
+
1166
+
1167
+
1168
+
1169
+
1170
+ </td>
1171
+
1172
+
1173
+
1174
+ <td class="default">
1175
+
1176
+ </td>
1177
+
1178
+
1179
+ <td class="description last"><p>輸入各attempt共用之stdout驗證規則,條目可覆寫,預設undefined</p></td>
1180
+ </tr>
1181
+
1182
+
1183
+
1184
+ <tr>
1185
+
1186
+ <td class="name"><code>maxRetries</code></td>
1187
+
1188
+
1189
+ <td class="type">
1190
+
1191
+
1192
+ <span class="param-type">Number</span>
1193
+
1194
+
1195
+
1196
+
1197
+ </td>
1198
+
1199
+
1200
+ <td class="attributes">
1201
+
1202
+ &lt;optional><br>
1203
+
1204
+
1205
+
1206
+
1207
+
1208
+ </td>
1209
+
1210
+
1211
+
1212
+ <td class="default">
1213
+
1214
+ <code>0</code>
1215
+
1216
+ </td>
1217
+
1218
+
1219
+ <td class="description last"><p>輸入各attempt共用之同家重試次數非負整數,韌性建議交給換家而非重試同一家,預設0</p></td>
1220
+ </tr>
1221
+
1222
+
1223
+ </tbody>
1224
+ </table>
1225
+
1226
+ </td>
1227
+ </tr>
1228
+
1229
+
1230
+ </tbody>
1231
+ </table>
1232
+
1233
+
1234
+
1235
+
1236
+
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+ <h5 class="h5-returns">Returns:</h5>
1249
+
1250
+
1251
+ <div class="param-desc">
1252
+ <p>回傳Promise,resolve回傳結果物件,除execCli既有欄位(ok、stdout、stderr、code、error、durationMs、attempts、pid)外,追加providerId(實際使用之群組)、keyIndex(實際使用之金鑰索引,無keys時為null)、kind、model、tried(全部嘗試歷程陣列,成功時亦回傳),本函數不會reject</p>
1253
+ </div>
1254
+
1255
+
1256
+
1257
+ <dl class="param-type">
1258
+ <dt>
1259
+ Type
1260
+ </dt>
1261
+ <dd>
1262
+
1263
+ <span class="param-type">Promise</span>
1264
+
1265
+
1266
+
1267
+ </dd>
1268
+ </dl>
1269
+
1270
+
1271
+
1272
+
1273
+
1274
+
1275
+
1276
+
1277
+
1278
+
566
1279
  <h4 class="name" id="dispatchClaude"><span class="type-signature type-signature-async">(async) </span>dispatchClaude<span class="signature">(prompt, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
567
1280
 
568
1281
 
@@ -2892,6 +3605,164 @@ console.log(getErrorResult(null).error)
2892
3605
 
2893
3606
 
2894
3607
 
3608
+
3609
+
3610
+
3611
+
3612
+ <h4 class="name" id="isKeyIndependentFail"><span class="type-signature"></span>isKeyIndependentFail<span class="signature">(r)</span><span class="type-signature"> &rarr; {Boolean}</span></h4>
3613
+
3614
+
3615
+
3616
+
3617
+
3618
+
3619
+ <dl class="details">
3620
+
3621
+ <dt class="tag-description">Description:</dt>
3622
+ <dd class="tag-description"><ul class="dummy"><li><p>判斷失敗結果是否與「哪一把金鑰」無關(換組內金鑰必然再敗, 應整組跳過)</p></li></ul></dd>
3623
+
3624
+
3625
+
3626
+ <dt class="tag-source">Source:</dt>
3627
+ <dd class="tag-source"><ul class="dummy"><li>
3628
+ <a href="dispatchAiFallback.mjs.html">dispatchAiFallback.mjs</a>, <a href="dispatchAiFallback.mjs.html#line56">line 56</a>
3629
+ </li></ul></dd>
3630
+
3631
+
3632
+
3633
+
3634
+
3635
+
3636
+
3637
+
3638
+
3639
+
3640
+
3641
+
3642
+
3643
+
3644
+
3645
+
3646
+
3647
+
3648
+
3649
+
3650
+
3651
+
3652
+
3653
+
3654
+
3655
+
3656
+
3657
+
3658
+
3659
+
3660
+
3661
+ </dl>
3662
+
3663
+
3664
+
3665
+
3666
+
3667
+
3668
+
3669
+
3670
+
3671
+
3672
+
3673
+
3674
+
3675
+ <h5 class="h5-parameters">Parameters:</h5>
3676
+
3677
+
3678
+ <table class="params">
3679
+ <thead>
3680
+ <tr>
3681
+
3682
+ <th>Name</th>
3683
+
3684
+
3685
+ <th>Type</th>
3686
+
3687
+
3688
+
3689
+
3690
+
3691
+ <th class="last">Description</th>
3692
+ </tr>
3693
+ </thead>
3694
+
3695
+ <tbody>
3696
+
3697
+
3698
+ <tr>
3699
+
3700
+ <td class="name"><code>r</code></td>
3701
+
3702
+
3703
+ <td class="type">
3704
+
3705
+
3706
+ <span class="param-type">Object</span>
3707
+
3708
+
3709
+
3710
+
3711
+ </td>
3712
+
3713
+
3714
+
3715
+
3716
+
3717
+ <td class="description last"><p>輸入dispatchAi失敗結果物件</p></td>
3718
+ </tr>
3719
+
3720
+
3721
+ </tbody>
3722
+ </table>
3723
+
3724
+
3725
+
3726
+
3727
+
3728
+
3729
+
3730
+
3731
+
3732
+
3733
+
3734
+
3735
+
3736
+
3737
+
3738
+
3739
+ <h5 class="h5-returns">Returns:</h5>
3740
+
3741
+
3742
+ <div class="param-desc">
3743
+ <p>回傳是否應整組跳過之布林值</p>
3744
+ </div>
3745
+
3746
+
3747
+
3748
+ <dl class="param-type">
3749
+ <dt>
3750
+ Type
3751
+ </dt>
3752
+ <dd>
3753
+
3754
+ <span class="param-type">Boolean</span>
3755
+
3756
+
3757
+
3758
+ </dd>
3759
+ </dl>
3760
+
3761
+
3762
+
3763
+
3764
+
3765
+
2895
3766
 
2896
3767
 
2897
3768
 
@@ -2911,7 +3782,7 @@ console.log(getErrorResult(null).error)
2911
3782
  <br class="clear">
2912
3783
 
2913
3784
  <footer>
2914
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Sun Aug 09 2026 22:39:08 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
3785
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Sun Aug 09 2026 23:14:54 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
2915
3786
  </footer>
2916
3787
 
2917
3788
  <script>prettyPrint();</script>