feat(bedrock-runtime): stub Converse and InvokeModel for #87 (#486)

Adds a bedrock-runtime service stub so AWS SDK and CLI clients can target
Floci for local development of Bedrock Runtime callers. Dummy responses
only: no real model inference.

- Register bedrock-runtime in ResolvedServiceCatalog under both "bedrock"
  and "bedrock-runtime" credential scopes (boto3 signs with signingName
  "bedrock" while some clients use the endpoint id)
- POST /model/{modelId}/converse returns a fixed assistant message plus
  synthetic usage and latency metrics matching the AWS Converse contract
- POST /model/{modelId}/invoke returns an Anthropic-shaped body for
  anthropic.* and *.anthropic.* model ids, generic {"outputs": [...]}
  otherwise. Request body is passed through as opaque bytes
- {modelId:.+} path regex accepts full ARNs (inference profiles,
  provisioned throughput) in addition to plain ids
- converse-stream and invoke-with-response-stream return 501 so SDKs
  get a clear error instead of 404
- Response building uses JsonNode rather than typed POJOs, avoiding
  native-image reflection registration

13 new integration tests (Converse + InvokeModel happy paths, validation,
inference-profile ids and ARNs, 501 streaming responses). Full suite
remains green: 2263 tests, 0 failures.
This commit is contained in:
TiM authored and GitHub committed 2026-04-16 17:13:17 -05:00
1 parent c0e4af32f7
commit c565398beb
13 files changed
+536 -4

No files matched your search

@@ -234,6 +234,7 @@ public interface EmulatorConfig {
AppConfigDataServiceConfig appconfigdata();
EcrServiceConfig ecr();
ResourceGroupsTaggingServiceConfig tagging();
BedrockRuntimeServiceConfig bedrockRuntime();
}
interface SsmServiceConfig {
@@ -468,6 +469,11 @@ public interface EmulatorConfig {
boolean enabled();
}
interface BedrockRuntimeServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface EcrServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.appconfig.AppConfigController;
import io.github.hectorvent.floci.services.appconfig.AppConfigDataController;
import io.github.hectorvent.floci.services.bedrockruntime.BedrockRuntimeController;
import io.github.hectorvent.floci.services.cognito.CognitoOAuthController;
import io.github.hectorvent.floci.services.cognito.CognitoWellKnownController;
import io.github.hectorvent.floci.services.lambda.LambdaController;
@@ -181,7 +182,18 @@ public class ResolvedServiceCatalog {
descriptor("tagging", "tagging", config.services().tagging().enabled(), true,
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("ResourceGroupsTaggingAPI_20170126."), Set.of("tagging"), Set.of(), Set.of())
Set.of("ResourceGroupsTaggingAPI_20170126."), Set.of("tagging"), Set.of(), Set.of()),
descriptor("bedrock-runtime", "bedrock-runtime",
config.services().bedrockRuntime().enabled(), true,
null, null, 5000L, null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
Set.of(),
// Register both signing names. boto3's service model declares
// signingName=bedrock for bedrock-runtime; register the endpoint
// id too as a safety net (catalog lookup is exact-match).
Set.of("bedrock", "bedrock-runtime"),
Set.of(),
Set.of(BedrockRuntimeController.class))
));
}
@@ -0,0 +1,101 @@
package io.github.hectorvent.floci.services.bedrockruntime;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
/**
* AWS Bedrock Runtime REST JSON endpoints.
*
* Real Bedrock Runtime uses {@code POST /model/{modelId}/converse} and
* {@code POST /model/{modelId}/invoke} against the
* {@code bedrock-runtime.<region>.amazonaws.com} host. Floci routes all
* hostnames to port 4566, so path-based dispatch is sufficient.
*
* Returns dummy responses only: no real model inference.
*/
@Path("/model")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BedrockRuntimeController {
private static final Logger LOG = Logger.getLogger(BedrockRuntimeController.class);
private final BedrockRuntimeService service;
private final ObjectMapper objectMapper;
@Inject
public BedrockRuntimeController(BedrockRuntimeService service, ObjectMapper objectMapper) {
this.service = service;
this.objectMapper = objectMapper;
}
@POST
@Path("/{modelId:.+}/converse")
public Response converse(@PathParam("modelId") String modelId, String body) {
if (modelId == null || modelId.isBlank()) {
throw new AwsException("ValidationException", "modelId is required.", 400);
}
JsonNode request;
try {
request = body == null || body.isBlank()
? objectMapper.createObjectNode()
: objectMapper.readTree(body);
} catch (Exception e) {
throw new AwsException("ValidationException",
"Malformed request body: " + e.getMessage(), 400);
}
JsonNode messages = request.path("messages");
if (!messages.isArray() || messages.isEmpty()) {
throw new AwsException("ValidationException",
"messages is required and must be a non-empty array.", 400);
}
ObjectNode response = service.buildConverseResponse(modelId);
LOG.debugv("Bedrock Converse: modelId={0}, messages={1}", modelId, messages.size());
return Response.ok(response).build();
}
@POST
@Path("/{modelId:.+}/invoke")
@Consumes(MediaType.WILDCARD)
public Response invokeModel(@PathParam("modelId") String modelId, byte[] body) {
if (modelId == null || modelId.isBlank()) {
throw new AwsException("ValidationException", "modelId is required.", 400);
}
// Bedrock InvokeModel bodies are model-specific opaque blobs; do not parse.
byte[] response = service.buildInvokeModelResponse(modelId);
LOG.debugv("Bedrock InvokeModel: modelId={0}, bodyBytes={1}",
modelId, body == null ? 0 : body.length);
return Response.ok(response, MediaType.APPLICATION_JSON).build();
}
@POST
@Path("/{modelId:.+}/invoke-with-response-stream")
@Consumes(MediaType.WILDCARD)
public Response invokeModelWithResponseStream(@PathParam("modelId") String modelId) {
throw new AwsException("UnsupportedOperationException",
"InvokeModelWithResponseStream is not supported by the Floci stub. "
+ "Use InvokeModel or Converse instead.", 501);
}
@POST
@Path("/{modelId:.+}/converse-stream")
@Consumes(MediaType.WILDCARD)
public Response converseStream(@PathParam("modelId") String modelId) {
throw new AwsException("UnsupportedOperationException",
"ConverseStream is not supported by the Floci stub. "
+ "Use Converse instead.", 501);
}
}
@@ -0,0 +1,75 @@
package io.github.hectorvent.floci.services.bedrockruntime;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
/**
* Dummy response builder for Bedrock Runtime. Stateless.
* No real model inference: returns a fixed assistant turn plus token usage metadata.
*/
@ApplicationScoped
public class BedrockRuntimeService {
private final ObjectMapper objectMapper;
@Inject
public BedrockRuntimeService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public ObjectNode buildConverseResponse(String modelId) {
ObjectNode root = objectMapper.createObjectNode();
ObjectNode output = root.putObject("output");
ObjectNode message = output.putObject("message");
message.put("role", "assistant");
ArrayNode content = message.putArray("content");
ObjectNode textBlock = content.addObject();
textBlock.put("text", "Floci stub response for model=" + modelId);
root.put("stopReason", "end_turn");
ObjectNode usage = root.putObject("usage");
usage.put("inputTokens", 10);
usage.put("outputTokens", 12);
usage.put("totalTokens", 22);
ObjectNode metrics = root.putObject("metrics");
metrics.put("latencyMs", 1);
return root;
}
public byte[] buildInvokeModelResponse(String modelId) {
ObjectNode root = objectMapper.createObjectNode();
String lower = modelId == null ? "" : modelId.toLowerCase();
if (lower.startsWith("anthropic.") || lower.contains(".anthropic.")) {
root.put("id", "msg_stub");
root.put("type", "message");
root.put("role", "assistant");
ArrayNode content = root.putArray("content");
ObjectNode block = content.addObject();
block.put("type", "text");
block.put("text", "Floci stub response");
root.put("model", modelId);
root.put("stop_reason", "end_turn");
ObjectNode usage = root.putObject("usage");
usage.put("input_tokens", 10);
usage.put("output_tokens", 12);
} else {
// Generic minimal shape for Meta, Mistral, Titan and others.
// Bedrock returns provider-specific bodies; callers parse by model family.
ArrayNode outputs = root.putArray("outputs");
ObjectNode item = outputs.addObject();
item.put("text", "Floci stub response");
}
try {
return objectMapper.writeValueAsBytes(root);
} catch (Exception e) {
throw new RuntimeException("Failed to serialize InvokeModel response", e);
}
}
}