@@ -43,6 +43,7 @@ export interface RunOpts {
43
43
confirm ?: boolean
44
44
prompt ?: boolean
45
45
credentialOverrides ?: string [ ]
46
+ credentialContexts ?: string [ ]
46
47
location ?: string
47
48
env ?: string [ ]
48
49
forceSequential ?: boolean
@@ -320,6 +321,47 @@ export class GPTScript {
320
321
return this . _load ( { toolDefs, disableCache, subTool} )
321
322
}
322
323
324
+ async listCredentials ( context : Array < string > , allContexts : boolean ) : Promise < Array < Credential > > {
325
+ if ( ! this . ready ) {
326
+ this . ready = await this . testGPTScriptURL ( 20 )
327
+ }
328
+
329
+ const r : Run = new RunSubcommand ( "credentials" , "" , { } , GPTScript . serverURL )
330
+ r . request ( { context, allContexts} )
331
+ const out = await r . json ( )
332
+ return out . map ( ( c : any ) => jsonToCredential ( JSON . stringify ( c ) ) )
333
+ }
334
+
335
+ async createCredential ( credential : Credential ) : Promise < void > {
336
+ if ( ! this . ready ) {
337
+ this . ready = await this . testGPTScriptURL ( 20 )
338
+ }
339
+
340
+ const r : Run = new RunSubcommand ( "credentials/create" , "" , { } , GPTScript . serverURL )
341
+ r . request ( { content : credentialToJSON ( credential ) } )
342
+ await r . text ( )
343
+ }
344
+
345
+ async revealCredential ( context : Array < string > , name : string ) : Promise < Credential > {
346
+ if ( ! this . ready ) {
347
+ this . ready = await this . testGPTScriptURL ( 20 )
348
+ }
349
+
350
+ const r : Run = new RunSubcommand ( "credentials/reveal" , "" , { } , GPTScript . serverURL )
351
+ r . request ( { context, name} )
352
+ return jsonToCredential ( await r . text ( ) )
353
+ }
354
+
355
+ async deleteCredential ( context : string , name : string ) : Promise < void > {
356
+ if ( ! this . ready ) {
357
+ this . ready = await this . testGPTScriptURL ( 20 )
358
+ }
359
+
360
+ const r : Run = new RunSubcommand ( "credentials/delete" , "" , { } , GPTScript . serverURL )
361
+ r . request ( { context : [ context ] , name} )
362
+ await r . text ( )
363
+ }
364
+
323
365
/**
324
366
* Helper method to handle the common logic for loading.
325
367
*
@@ -967,3 +1009,48 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
967
1009
function randomId ( prefix : string ) : string {
968
1010
return prefix + Math . random ( ) . toString ( 36 ) . substring ( 2 , 12 )
969
1011
}
1012
+
1013
+ export enum CredentialType {
1014
+ Tool = "tool" ,
1015
+ ModelProvider = "modelProvider" ,
1016
+ }
1017
+
1018
+ export type Credential = {
1019
+ context : string
1020
+ name : string
1021
+ type : CredentialType
1022
+ env : Record < string , string >
1023
+ ephemeral : boolean
1024
+ expiresAt ?: Date | undefined
1025
+ refreshToken ?: string | undefined
1026
+ }
1027
+
1028
+ // for internal use only
1029
+ type cred = {
1030
+ context : string
1031
+ toolName : string
1032
+ type : string
1033
+ env : Record < string , string >
1034
+ ephemeral : boolean
1035
+ expiresAt : string | undefined
1036
+ refreshToken : string | undefined
1037
+ }
1038
+
1039
+ export function credentialToJSON ( c : Credential ) : string {
1040
+ const expiresAt = c . expiresAt ? c . expiresAt . toISOString ( ) : undefined
1041
+ const type = c . type === CredentialType . Tool ? "tool" : "modelProvider"
1042
+ return JSON . stringify ( { context : c . context , toolName : c . name , type : type , env : c . env , ephemeral : c . ephemeral , expiresAt : expiresAt , refreshToken : c . refreshToken } as cred )
1043
+ }
1044
+
1045
+ function jsonToCredential ( cred : string ) : Credential {
1046
+ const c = JSON . parse ( cred ) as cred
1047
+ return {
1048
+ context : c . context ,
1049
+ name : c . toolName ,
1050
+ type : c . type === "tool" ? CredentialType . Tool : CredentialType . ModelProvider ,
1051
+ env : c . env ,
1052
+ ephemeral : c . ephemeral ,
1053
+ expiresAt : c . expiresAt ? new Date ( c . expiresAt ) : undefined ,
1054
+ refreshToken : c . refreshToken
1055
+ }
1056
+ }
0 commit comments