@@ -3,6 +3,8 @@ import { Tensor } from './tensor';
33import { Model } from './model' ;
44import * as util from 'util' ;
55import { DTypeMap } from './dtype' ;
6+ import { Script } from './script' ;
7+ import { BackendMap } from './backend' ;
68
79export class Client {
810 private _sendCommand : any ;
@@ -67,6 +69,10 @@ export class Client {
6769
6870 public modelset ( keName : string , m : Model ) : Promise < any > {
6971 const args = [ keName , m . backend , m . device ] ;
72+ if ( m . tag !== undefined ) {
73+ args . push ( 'TAG' ) ;
74+ args . push ( m . tag . toString ( ) ) ;
75+ }
7076 if ( m . inputs . length > 0 ) {
7177 args . push ( 'INPUTS' ) ;
7278 m . inputs . forEach ( ( value ) => args . push ( value ) ) ;
@@ -87,4 +93,112 @@ export class Client {
8793 outputs . forEach ( ( value ) => args . push ( value ) ) ;
8894 return this . _sendCommand ( 'ai.modelrun' , args ) ;
8995 }
96+
97+ public modeldel ( modelName : string ) : Promise < any > {
98+ const args = [ modelName ] ;
99+ return this . _sendCommand ( 'ai.modeldel' , args ) ;
100+ }
101+
102+ public modelget ( modelName : string ) : Promise < any > {
103+ const args = [ modelName , 'META' , 'BLOB' ] ;
104+ return this . _sendCommand ( 'ai.modelget' , args )
105+ . then ( ( reply : any [ ] ) => {
106+ let backend = null ;
107+ let device = null ;
108+ let tag = null ;
109+ let blob = null ;
110+ for ( let i = 0 ; i < reply . length ; i += 2 ) {
111+ const key = reply [ i ] ;
112+ const obj = reply [ i + 1 ] ;
113+ switch ( key . toString ( ) ) {
114+ case 'backend' :
115+ backend = BackendMap [ obj . toString ( ) ] ;
116+ break ;
117+ case 'device' :
118+ // @ts -ignore
119+ device = obj . toString ( ) ;
120+ break ;
121+ case 'tag' :
122+ tag = obj . toString ( ) ;
123+ break ;
124+ case 'blob' :
125+ // blob = obj;
126+ blob = Buffer . from ( obj ) ;
127+ break ;
128+ }
129+ }
130+ if ( backend == null || device == null || blob == null ) {
131+ throw Error ( 'modelget reply did not had the full elements to build the tensor' ) ;
132+ }
133+ const model = new Model ( backend , device , [ ] , [ ] , blob ) ;
134+ if ( tag !== null ) {
135+ model . tag = tag ;
136+ }
137+ return model ;
138+ } )
139+ . catch ( ( error : any ) => {
140+ throw error ;
141+ } ) ;
142+ }
143+
144+ public scriptset ( keName : string , s : Script ) : Promise < any > {
145+ const args = [ keName , s . device ] ;
146+ if ( s . tag !== undefined ) {
147+ args . push ( 'TAG' ) ;
148+ args . push ( s . tag ) ;
149+ }
150+ args . push ( 'SOURCE' ) ;
151+ args . push ( s . script ) ;
152+ return this . _sendCommand ( 'ai.scriptset' , args ) ;
153+ }
154+
155+ public scriptrun ( scriptName : string , functionName : string , inputs : string [ ] , outputs : string [ ] ) : Promise < any > {
156+ const args = [ scriptName , functionName , 'INPUTS' ] ;
157+ inputs . forEach ( ( value ) => args . push ( value ) ) ;
158+ args . push ( 'OUTPUTS' ) ;
159+ outputs . forEach ( ( value ) => args . push ( value ) ) ;
160+ return this . _sendCommand ( 'ai.scriptrun' , args ) ;
161+ }
162+
163+ public scriptdel ( scriptName : string ) : Promise < any > {
164+ const args = [ scriptName ] ;
165+ return this . _sendCommand ( 'ai.scriptdel' , args ) ;
166+ }
167+
168+ public scriptget ( scriptName : string ) : Promise < any > {
169+ const args = [ scriptName , 'META' , 'SOURCE' ] ;
170+ return this . _sendCommand ( 'ai.scriptget' , args )
171+ . then ( ( reply : any [ ] ) => {
172+ let device = null ;
173+ let tag = null ;
174+ let source = null ;
175+ for ( let i = 0 ; i < reply . length ; i += 2 ) {
176+ const key = reply [ i ] ;
177+ const obj = reply [ i + 1 ] ;
178+ switch ( key . toString ( ) ) {
179+ case 'device' :
180+ // @ts -ignore
181+ device = obj . toString ( ) ;
182+ break ;
183+ case 'tag' :
184+ tag = obj . toString ( ) ;
185+ break ;
186+ case 'source' :
187+ source = obj . toString ( ) ;
188+ break ;
189+ }
190+ }
191+ if ( device == null || source == null ) {
192+ throw Error ( 'scriptget reply did not had the full elements to build the tensor' ) ;
193+ }
194+ const script = new Script ( device , source ) ;
195+ if ( tag !== null ) {
196+ script . tag = tag ;
197+ }
198+ return script ;
199+ } )
200+ . catch ( ( error : any ) => {
201+ throw error ;
202+ } ) ;
203+ }
90204}
0 commit comments