|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.springchat; |
| 18 | + |
| 19 | +import com.google.adk.agents.RunConfig; |
| 20 | +import com.google.adk.events.Event; |
| 21 | +import com.google.adk.runner.Runner; |
| 22 | +import com.google.genai.types.Content; |
| 23 | +import com.google.genai.types.Part; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | +import org.springframework.http.HttpStatus; |
| 27 | +import org.springframework.web.bind.annotation.PostMapping; |
| 28 | +import org.springframework.web.bind.annotation.RequestBody; |
| 29 | +import org.springframework.web.bind.annotation.RestController; |
| 30 | +import org.springframework.web.server.ResponseStatusException; |
| 31 | + |
| 32 | +/** REST endpoint that forwards chat messages from the browser to the ADK runner. */ |
| 33 | +@RestController |
| 34 | +class ChatController { |
| 35 | + |
| 36 | + // The sample has no login, so every browser tab shares one user and keeps its own session. |
| 37 | + private static final String USER_ID = "spring-chat-user"; |
| 38 | + |
| 39 | + private static final RunConfig RUN_CONFIG = RunConfig.builder().autoCreateSession(true).build(); |
| 40 | + |
| 41 | + private final Runner runner; |
| 42 | + |
| 43 | + ChatController(Runner runner) { |
| 44 | + this.runner = runner; |
| 45 | + } |
| 46 | + |
| 47 | + /** A message from the browser; {@code sessionId} is generated client-side once per page load. */ |
| 48 | + private record ChatRequest(@Nullable String sessionId, @Nullable String message) {} |
| 49 | + |
| 50 | + private record ChatResponse(String reply) {} |
| 51 | + |
| 52 | + @PostMapping("/api/chat") |
| 53 | + ChatResponse chat(@RequestBody ChatRequest request) { |
| 54 | + if (isBlank(request.sessionId()) || isBlank(request.message())) { |
| 55 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST); |
| 56 | + } |
| 57 | + // The session service trims ids it creates, so look them up trimmed too. |
| 58 | + String sessionId = request.sessionId().trim(); |
| 59 | + Content userMessage = Content.fromParts(Part.fromText(request.message())); |
| 60 | + String reply = |
| 61 | + runner |
| 62 | + .runAsync(USER_ID, sessionId, userMessage, RUN_CONFIG) |
| 63 | + .filter(Event::finalResponse) |
| 64 | + .map(ChatController::replyText) |
| 65 | + .collect(Collectors.joining("\n")) |
| 66 | + .blockingGet(); |
| 67 | + return new ChatResponse(reply.strip()); |
| 68 | + } |
| 69 | + |
| 70 | + /** Returns the event's text, followed by why the model stopped, if it ended early. */ |
| 71 | + private static String replyText(Event event) { |
| 72 | + String text = event.stringifyContent(); |
| 73 | + return event.errorMessage().map(error -> text + "\n[" + error + "]").orElse(text); |
| 74 | + } |
| 75 | + |
| 76 | + private static boolean isBlank(@Nullable String s) { |
| 77 | + return s == null || s.trim().isEmpty(); |
| 78 | + } |
| 79 | +} |
0 commit comments