-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathExecutionController.java
More file actions
243 lines (225 loc) · 9.66 KB
/
ExecutionController.java
File metadata and controls
243 lines (225 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.adk.web.controller;
import com.google.adk.agents.RunConfig;
import com.google.adk.agents.RunConfig.StreamingMode;
import com.google.adk.events.Event;
import com.google.adk.runner.Runner;
import com.google.adk.web.dto.AgentRunRequest;
import com.google.adk.web.service.RunnerService;
import com.google.common.collect.Lists;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
/** Controller handling agent execution endpoints. */
@RestController
public class ExecutionController {
private static final Logger log = LoggerFactory.getLogger(ExecutionController.class);
private final RunnerService runnerService;
private final ExecutorService sseExecutor = Executors.newCachedThreadPool();
@Autowired
public ExecutionController(RunnerService runnerService) {
this.runnerService = runnerService;
}
/**
* Executes a non-streaming agent run for a given session and message.
*
* @param request The AgentRunRequest containing run details.
* @return A list of events generated during the run.
* @throws ResponseStatusException if the session is not found or the run fails.
*/
@PostMapping("/run")
public List<Event> agentRun(@RequestBody AgentRunRequest request) {
if (request.appName == null || request.appName.trim().isEmpty()) {
log.warn("appName cannot be null or empty in POST /run request.");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "appName cannot be null or empty");
}
if (request.sessionId == null || request.sessionId.trim().isEmpty()) {
log.warn("sessionId cannot be null or empty in POST /run request.");
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "sessionId cannot be null or empty");
}
log.info("Request received for POST /run for session: {}", request.sessionId);
Runner runner = this.runnerService.getRunner(request.appName);
try {
RunConfig runConfig = RunConfig.builder().setStreamingMode(StreamingMode.NONE).build();
Flowable<Event> eventStream =
runner.runAsync(
request.userId,
request.sessionId,
request.getNewMessage(),
runConfig,
request.stateDelta);
List<Event> events = Lists.newArrayList(eventStream.blockingIterable());
log.info("Agent run for session {} generated {} events.", request.sessionId, events.size());
return events;
} catch (Exception e) {
log.error("Error during agent run for session {}", request.sessionId, e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Agent run failed", e);
}
}
/**
* Executes an agent run and streams the resulting events using Server-Sent Events (SSE).
*
* @param request The AgentRunRequest containing run details.
* @return A Flux that will stream events to the client.
*/
@PostMapping(value = "/run_sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter agentRunSse(@RequestBody AgentRunRequest request) {
SseEmitter emitter = new SseEmitter(60 * 60 * 1000L); // 1 hour timeout
if (request.appName == null || request.appName.trim().isEmpty()) {
log.warn(
"appName cannot be null or empty in SseEmitter request for appName: {}, session: {}",
request.appName,
request.sessionId);
emitter.completeWithError(
new ResponseStatusException(HttpStatus.BAD_REQUEST, "appName cannot be null or empty"));
return emitter;
}
if (request.sessionId == null || request.sessionId.trim().isEmpty()) {
log.warn(
"sessionId cannot be null or empty in SseEmitter request for appName: {}, session: {}",
request.appName,
request.sessionId);
emitter.completeWithError(
new ResponseStatusException(HttpStatus.BAD_REQUEST, "sessionId cannot be null or empty"));
return emitter;
}
log.info(
"SseEmitter Request received for POST /run_sse_emitter for session: {}", request.sessionId);
final String sessionId = request.sessionId;
sseExecutor.execute(
() -> {
Runner runner;
try {
runner = this.runnerService.getRunner(request.appName);
} catch (ResponseStatusException e) {
log.warn(
"Setup failed for SseEmitter request for session {}: {}",
sessionId,
e.getMessage());
try {
emitter.completeWithError(e);
} catch (Exception ex) {
log.warn(
"Error completing emitter after setup failure for session {}: {}",
sessionId,
ex.getMessage());
}
return;
}
final RunConfig runConfig =
RunConfig.builder()
.setStreamingMode(request.getStreaming() ? StreamingMode.SSE : StreamingMode.NONE)
.build();
Flowable<Event> eventFlowable =
runner.runAsync(
request.userId,
request.sessionId,
request.getNewMessage(),
runConfig,
request.stateDelta);
Disposable disposable =
eventFlowable
.observeOn(Schedulers.io())
.subscribe(
event -> {
try {
log.debug(
"SseEmitter: Sending event {} for session {}", event.id(), sessionId);
emitter.send(SseEmitter.event().data(event));
} catch (IOException e) {
log.error(
"SseEmitter: IOException sending event for session {}: {}",
sessionId,
e.getMessage());
throw new RuntimeException("Failed to send event", e);
} catch (Exception e) {
log.error(
"SseEmitter: Unexpected error sending event for session {}: {}",
sessionId,
e.getMessage(),
e);
throw new RuntimeException("Unexpected error sending event", e);
}
},
error -> {
log.error(
"SseEmitter: Stream error for session {}: {}",
sessionId,
error.getMessage(),
error);
try {
emitter.completeWithError(error);
} catch (Exception ex) {
log.warn(
"Error completing emitter after stream error for session {}: {}",
sessionId,
ex.getMessage());
}
},
() -> {
log.debug(
"SseEmitter: Stream completed normally for session: {}", sessionId);
try {
emitter.complete();
} catch (Exception ex) {
log.warn(
"Error completing emitter after normal completion for session {}:"
+ " {}",
sessionId,
ex.getMessage());
}
});
emitter.onCompletion(
() -> {
log.debug(
"SseEmitter: onCompletion callback for session: {}. Disposing subscription.",
sessionId);
if (!disposable.isDisposed()) {
disposable.dispose();
}
});
emitter.onTimeout(
() -> {
log.debug(
"SseEmitter: onTimeout callback for session: {}. Disposing subscription and"
+ " completing.",
sessionId);
if (!disposable.isDisposed()) {
disposable.dispose();
}
emitter.complete();
});
});
log.debug("SseEmitter: Returning emitter for session: {}", sessionId);
return emitter;
}
}