Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* 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 io.agentscope.spring.boot.agui.common;

import io.agentscope.core.agui.model.RunAgentInput;
import io.agentscope.core.util.JsonUtils;
import java.util.Objects;

/**
* Parses AG-UI request bodies with AgentScope's JSON codec.
*
* <p>The default AgentScope codec is Jackson 2. Keeping this conversion at the AG-UI boundary
* avoids selecting Spring Boot 4's Jackson 3 codec for AG-UI's Jackson 2-annotated models, while
* leaving the application's other HTTP converters unchanged.
*/
public final class AguiRequestBodyParser {

private AguiRequestBodyParser() {}

/**
* Parses a JSON request body into {@link RunAgentInput}.
*
* @param body the raw JSON request body
* @return the parsed AG-UI input
*/
public static RunAgentInput parse(String body) {
Objects.requireNonNull(body, "body cannot be null");
return JsonUtils.getJsonCodec().fromJson(body, RunAgentInput.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
*/
package io.agentscope.spring.boot.agui.mvc;

import io.agentscope.core.agui.encoder.AguiEventEncoder;
import io.agentscope.core.agui.event.AguiEvent;
import io.agentscope.core.agui.model.RunAgentInput;
import io.agentscope.core.util.JsonException;
import io.agentscope.spring.boot.agui.common.AguiRequestBodyParser;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -35,6 +42,7 @@
public class AguiRestController {

private final AguiMvcController aguiMvcController;
private final AguiEventEncoder encoder = new AguiEventEncoder();
private final String pathPrefix;
private final boolean enablePathRouting;

Expand Down Expand Up @@ -63,7 +71,7 @@ public AguiRestController(
* <li>"default"</li>
* </ol>
*
* @param input The run agent input
* @param body The raw run agent input JSON
* @param agentIdHeader The agent ID from HTTP header (optional)
* @return An SseEmitter for streaming AG-UI events
*/
Expand All @@ -72,12 +80,13 @@ public AguiRestController(
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter run(
@RequestBody RunAgentInput input,
@RequestBody String body,
@RequestHeader(
value = "${agentscope.agui.agent-id-header:X-Agent-Id}",
required = false)
String agentIdHeader,
HttpServletRequest request) {
RunAgentInput input = AguiRequestBodyParser.parse(body);
return aguiMvcController.handle(input, agentIdHeader, request);
Comment thread
jujn marked this conversation as resolved.
}

Expand All @@ -87,7 +96,7 @@ public SseEmitter run(
* <p>The path variable takes highest priority for agent resolution.
*
* @param agentId The agent ID from path variable
* @param input The run agent input
* @param body The raw run agent input JSON
* @param agentIdHeader The agent ID from HTTP header (optional)
* @return An SseEmitter for streaming AG-UI events
*/
Expand All @@ -97,12 +106,37 @@ public SseEmitter run(
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter runWithAgentId(
@PathVariable String agentId,
@RequestBody RunAgentInput input,
@RequestBody String body,
@RequestHeader(
value = "${agentscope.agui.agent-id-header:X-Agent-Id}",
required = false)
String agentIdHeader,
HttpServletRequest request) {
RunAgentInput input = AguiRequestBodyParser.parse(body);
return aguiMvcController.handleWithAgentId(input, agentIdHeader, agentId, request);
}

/**
* Return HTTP 400 for AG-UI request body parsing failures.
*
* @param error the JSON parse failure
* @return an SSE-compatible bad request response
*/
@ExceptionHandler(JsonException.class)
public ResponseEntity<String> handleParseError(JsonException error) {
String errorEvent =
encoder.encodeToJson(
new AguiEvent.Raw(
"unknown",
"unknown",
Map.of(
"error",
"Failed to parse request: " + error.getMessage())))
.trim();
String finishEvent =
encoder.encodeToJson(new AguiEvent.RunFinished("unknown", "unknown")).trim();
return ResponseEntity.badRequest()
.contentType(MediaType.TEXT_EVENT_STREAM)
.body("data: " + errorEvent + "\n\n" + "data: " + finishEvent + "\n\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.agentscope.core.agui.model.RunAgentInput;
import io.agentscope.core.agui.processor.AguiRequestProcessor;
import io.agentscope.core.agui.registry.AguiAgentRegistry;
import io.agentscope.spring.boot.agui.common.AguiRequestBodyParser;
import io.agentscope.spring.boot.agui.common.AguiRuntimeContextRequest;
import io.agentscope.spring.boot.agui.common.AguiRuntimeContextResolver;
import io.agentscope.spring.boot.agui.common.DefaultAgentResolver;
Expand Down Expand Up @@ -112,7 +113,8 @@ private AguiWebFluxHandler(Builder builder) {
* @return A Mono containing the server response with SSE stream
*/
public Mono<ServerResponse> handle(ServerRequest request) {
return request.bodyToMono(RunAgentInput.class)
return request.bodyToMono(String.class)
.map(AguiRequestBodyParser::parse)
.flatMap(input -> processInput(input, request, null))
.onErrorResume(this::handleParseError);
}
Expand All @@ -128,7 +130,8 @@ public Mono<ServerResponse> handle(ServerRequest request) {
*/
public Mono<ServerResponse> handleWithAgentId(ServerRequest request) {
String pathAgentId = request.pathVariable(AGENT_ID_PATH_VARIABLE);
return request.bodyToMono(RunAgentInput.class)
return request.bodyToMono(String.class)
.map(AguiRequestBodyParser::parse)
.flatMap(input -> processInput(input, request, pathAgentId))
.onErrorResume(this::handleParseError);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* 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 io.agentscope.spring.boot.agui.common;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.agentscope.core.agui.model.MessageContent;
import io.agentscope.core.agui.model.RunAgentInput;
import org.junit.jupiter.api.Test;

class AguiRequestBodyParserTest {

@Test
void shouldParseTextContentWithAgentScopeCodec() {
String body =
"""
{
"threadId": "thread-1",
"runId": "run-1",
"messages": [
{"id": "message-1", "role": "user", "content": "Hello!"}
]
}
""";

RunAgentInput input = AguiRequestBodyParser.parse(body);

assertEquals("Hello!", input.getMessages().get(0).getTextContent());
}

@Test
void shouldParseMultimodalContentWithAgentScopeCodec() {
String body =
"""
{
"threadId": "thread-1",
"runId": "run-1",
"messages": [
{
"id": "message-1",
"role": "user",
"content": [
{"type": "text", "text": "Describe this"},
{
"type": "image",
"source": {
"type": "url",
"value": "https://example.com/image.png"
}
}
]
}
]
}
""";

RunAgentInput input = AguiRequestBodyParser.parse(body);
MessageContent.Blocks content =
assertInstanceOf(
MessageContent.Blocks.class, input.getMessages().get(0).getContent());

assertEquals(2, content.parts().size());
assertTrue(input.getMessages().get(0).hasBlocks());
}

@Test
void shouldRejectNullBody() {
assertThrows(NullPointerException.class, () -> AguiRequestBodyParser.parse(null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* 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 io.agentscope.spring.boot.agui.mvc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.agentscope.core.util.JsonException;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

class AguiRestControllerTest {

@Test
void shouldReturnBadRequestSseForParseErrors() {
AguiRestController controller = new AguiRestController(null, "/agui", true);

ResponseEntity<String> response =
controller.handleParseError(new JsonException("bad json"));

assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
assertEquals(MediaType.TEXT_EVENT_STREAM, response.getHeaders().getContentType());
assertNotNull(response.getBody());
assertTrue(response.getBody().contains("Failed to parse request: bad json"));
assertTrue(response.getBody().contains("data: "));
}
}
Loading