1- import { defineCommand , detectOutputFormat , fetchModelList , type Client } from "bailian-cli-core" ;
1+ import { defineCommand , detectOutputFormat , unwrapResponse } from "bailian-cli-core" ;
22import { emitResult } from "bailian-cli-runtime" ;
3-
4- const ACTIVATE_API = "zeldaEasy.broadscope-bailian.freeTrial.batchActivateFreeTierOnly" ;
5- const DEACTIVATE_API = "zeldaEasy.broadscope-bailian.freeTrial.batchDeactivateFreeTierOnly" ;
6- const FREE_TIER_API = "zeldaEasy.broadscope-bailian.freeTrial.queryFreeTierQuota" ;
7- const FREE_TIER_ONLY_STATUS_API = "zeldaEasy.broadscope-bailian.freeTrial.queryFreeTierOnlyStatus" ;
8-
9- interface FreeTierQuota {
10- model : string ;
11- quotaTotal : number ;
12- quotaInitTotal : number ;
13- }
14-
15- interface FreeTierOnlyStatus {
16- model : string ;
17- freeTierOnly : boolean ;
18- }
3+ import {
4+ FREE_TIER_API ,
5+ FREE_TIER_ONLY_STATUS_API ,
6+ extractFreeTierOnlyStatuses ,
7+ extractQuotas ,
8+ fetchAllModels ,
9+ pollFreeTierBatch ,
10+ } from "./shared.ts" ;
11+
12+ const ACTIVATE_API = "zeldaEasy.bailian-commerce.freeTrial.batchActivateFreeTierOnly" ;
13+ const DEACTIVATE_API = "zeldaEasy.bailian-commerce.freeTrial.batchDeactivateFreeTierOnly" ;
1914
2015interface BatchResultFailure {
2116 failureModelId : string ;
2217 errorCode : string ;
2318}
2419
25- function getNestedRecord (
26- obj : Record < string , unknown > ,
27- key : string ,
28- ) : Record < string , unknown > | undefined {
29- const val = obj [ key ] ;
30- if ( val && typeof val === "object" && ! Array . isArray ( val ) ) return val as Record < string , unknown > ;
31- return undefined ;
32- }
33-
34- function extractResponseData ( result : Record < string , unknown > ) : Record < string , unknown > {
35- const data = getNestedRecord ( result , "data" ) ;
36- if ( ! data ) return result ;
37-
38- const dataV2 = getNestedRecord ( data , "DataV2" ) ;
39- if ( dataV2 ) {
40- const inner = getNestedRecord ( dataV2 , "data" ) ;
41- const innerData = inner ? getNestedRecord ( inner , "data" ) : undefined ;
42- return innerData ?? inner ?? dataV2 ;
43- }
44-
45- const direct = getNestedRecord ( data , "data" ) ;
46- return direct ?? data ;
47- }
48-
49- const POLL_INTERVAL_MS = 500 ;
50- const MAX_POLLS = 20 ;
51-
52- async function pollUntilDone (
53- client : Client ,
54- api : string ,
55- requestKey : string ,
56- models : string [ ] ,
57- ) : Promise < unknown > {
58- let nextTaskId : string | undefined ;
59-
60- for ( let attempt = 0 ; attempt < MAX_POLLS ; attempt ++ ) {
61- const requestData = {
62- [ requestKey ] : nextTaskId ? { taskId : nextTaskId } : { models } ,
63- } ;
64-
65- const raw = await client . console ( api , requestData ) ;
66-
67- const resp = extractResponseData ( raw as Record < string , unknown > ) ;
68- if ( resp . taskId && Object . keys ( resp ) . length === 1 ) {
69- nextTaskId = resp . taskId as string ;
70- await new Promise ( ( resolve ) => setTimeout ( resolve , POLL_INTERVAL_MS ) ) ;
71- continue ;
72- }
73- return raw ;
74- }
75- return null ;
76- }
77-
78- async function fetchAllModelNames ( client : Client ) : Promise < string [ ] > {
79- const allModels : Record < string , unknown > [ ] = [ ] ;
80- let page = 1 ;
81- while ( true ) {
82- const result = await fetchModelList ( ( api , data ) => client . console ( api , data ) , {
83- pageNo : page ,
84- pageSize : 50 ,
85- } ) ;
86- allModels . push ( ...result . models ) ;
87- if ( allModels . length >= result . total ) break ;
88- page ++ ;
89- }
90- return allModels . map ( ( item ) => item . model as string ) . filter ( Boolean ) ;
91- }
92-
9320export default defineCommand ( {
9421 description :
9522 "Enable or disable auto-stop for free-tier models. Enables by default; use --off to disable" ,
@@ -161,7 +88,7 @@ export default defineCommand({
16188 }
16289
16390 if ( ! modelFlag ) {
164- models = await fetchAllModelNames ( ctx . client ) ;
91+ models = ( await fetchAllModels ( ctx . client ) ) . map ( ( model ) => model . name ) ;
16592 }
16693
16794 if ( off ) {
@@ -172,12 +99,10 @@ export default defineCommand({
17299 } ) ,
173100 ] ) ;
174101
175- const quotaData = extractResponseData ( quotaResult as Record < string , unknown > ) ;
176- const quotas = ( quotaData . freeTierQuotas ?? [ ] ) as FreeTierQuota [ ] ;
102+ const quotas = extractQuotas ( quotaResult ) ;
177103 const quotaMap = new Map ( quotas . map ( ( quota ) => [ quota . model , quota ] ) ) ;
178104
179- const stopData = extractResponseData ( stopResult as Record < string , unknown > ) ;
180- const stopStatuses = ( stopData . freeTierOnlyStatuses ?? [ ] ) as FreeTierOnlyStatus [ ] ;
105+ const stopStatuses = extractFreeTierOnlyStatuses ( stopResult ) ;
181106 const stopMap = new Map ( stopStatuses . map ( ( status ) => [ status . model , status . freeTierOnly ] ) ) ;
182107
183108 for ( const name of models ) {
@@ -192,21 +117,21 @@ export default defineCommand({
192117 ) ;
193118 continue ;
194119 }
195- await pollUntilDone ( ctx . client , api , requestKey , [ name ] ) ;
120+ await pollFreeTierBatch ( ctx . client , api , requestKey , [ name ] ) ;
196121 process . stdout . write ( `Disabled auto-stop for "${ name } ".\n` ) ;
197122 }
198123 return ;
199124 }
200125
201126 const jsonResults : unknown [ ] = [ ] ;
202127 for ( const name of models ) {
203- const result = await pollUntilDone ( ctx . client , api , requestKey , [ name ] ) ;
128+ const result = await pollFreeTierBatch ( ctx . client , api , requestKey , [ name ] ) ;
204129 if ( format === "json" ) {
205130 jsonResults . push ( result ) ;
206131 continue ;
207132 }
208133 if ( result ) {
209- const resultData = extractResponseData ( result as Record < string , unknown > ) ;
134+ const resultData = unwrapResponse ( result as Record < string , unknown > ) ;
210135 const failureModels = ( resultData . failureModels as BatchResultFailure [ ] ) ?? [ ] ;
211136 if ( failureModels . length > 0 ) {
212137 process . stderr . write (
0 commit comments