@@ -31,6 +31,7 @@ import {
31
31
ChatEvent ,
32
32
ChatSession ,
33
33
GenerateResponse ,
34
+ SyncResponse ,
34
35
} from '@aws/genai-plugin-for-backstage-common' ;
35
36
import { SessionStore } from '../database' ;
36
37
@@ -111,6 +112,10 @@ export class DefaultAgentService implements AgentService {
111
112
return service ;
112
113
}
113
114
115
+ getAgents ( ) : Agent [ ] {
116
+ return Array . from ( this . agents . values ( ) ) ;
117
+ }
118
+
114
119
async stream (
115
120
userMessage : string ,
116
121
options : {
@@ -133,7 +138,7 @@ export class DefaultAgentService implements AgentService {
133
138
const agent = this . getActualAgent ( options . agentName ) ;
134
139
135
140
if ( ! sessionId ) {
136
- session = await this . createSession ( agentName , principal , false ) ;
141
+ session = await this . makeSession ( agentName , principal , false ) ;
137
142
138
143
newSession = true ;
139
144
} else {
@@ -172,6 +177,19 @@ export class DefaultAgentService implements AgentService {
172
177
return this . sessionStore . getSession ( agentName , sessionId , principal ) ;
173
178
}
174
179
180
+ async createSession ( options : {
181
+ agentName : string ;
182
+ credentials : BackstageCredentials <
183
+ BackstageUserPrincipal | BackstageServicePrincipal
184
+ > ;
185
+ } ) : Promise < ChatSession > {
186
+ const { agentName, credentials } = options ;
187
+
188
+ const { principal } = await this . getUserEntityRef ( credentials ) ;
189
+
190
+ return this . makeSession ( agentName , principal , false ) ;
191
+ }
192
+
175
193
async endSession ( options : {
176
194
agentName : string ;
177
195
sessionId : string ;
@@ -186,6 +204,35 @@ export class DefaultAgentService implements AgentService {
186
204
await this . sessionStore . endSession ( agentName , sessionId , principal ) ;
187
205
}
188
206
207
+ async sync (
208
+ userMessage : string ,
209
+ options : {
210
+ agentName : string ;
211
+ sessionId ?: string ;
212
+ credentials : BackstageCredentials <
213
+ BackstageUserPrincipal | BackstageServicePrincipal
214
+ > ;
215
+ } ,
216
+ ) : Promise < SyncResponse > {
217
+ const { principal, userEntityRef } = await this . getUserEntityRef (
218
+ options . credentials ,
219
+ ) ;
220
+
221
+ const agent = this . getActualAgent ( options . agentName ) ;
222
+
223
+ const session = await this . makeSession ( options . agentName , principal , true ) ;
224
+
225
+ const output = agent . generate ( userMessage , session . sessionId , {
226
+ userEntityRef,
227
+ credentials : options . credentials ,
228
+ } ) ;
229
+
230
+ return {
231
+ output,
232
+ sessionId : session . sessionId ,
233
+ } ;
234
+ }
235
+
189
236
async generate (
190
237
prompt : string ,
191
238
options : {
@@ -201,11 +248,7 @@ export class DefaultAgentService implements AgentService {
201
248
202
249
const agent = this . getActualAgent ( options . agentName ) ;
203
250
204
- const session = await this . createSession (
205
- options . agentName ,
206
- principal ,
207
- true ,
208
- ) ;
251
+ const session = await this . makeSession ( options . agentName , principal , true ) ;
209
252
210
253
return agent . generate ( prompt , session . sessionId , {
211
254
userEntityRef,
@@ -227,7 +270,7 @@ export class DefaultAgentService implements AgentService {
227
270
return this . agents . get ( agent ) ;
228
271
}
229
272
230
- private createSession ( agent : string , principal : string , ended : boolean ) {
273
+ private makeSession ( agent : string , principal : string , ended : boolean ) {
231
274
const sessionId = uuidv4 ( ) ;
232
275
233
276
this . logger . info ( `Generated session ${ sessionId } ` ) ;
0 commit comments