Skip to content
Open
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
224 changes: 0 additions & 224 deletions core/src/main/java/com/google/adk/models/ChatCompletionsResponse.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2026 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
*
* https://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.models.chat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.genai.types.FunctionCall;
import com.google.genai.types.Part;
import java.util.Base64;
import java.util.Map;
import org.jspecify.annotations.Nullable;

/** Shared models for Chat Completions Request and Response. */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
final class ChatCompletionsCommon {

private ChatCompletionsCommon() {}

private static final ObjectMapper objectMapper = new ObjectMapper();

public static final String ROLE_ASSISTANT = "assistant";
public static final String ROLE_MODEL = "model";

public static final String METADATA_KEY_ID = "id";
public static final String METADATA_KEY_CREATED = "created";
public static final String METADATA_KEY_OBJECT = "object";
public static final String METADATA_KEY_SYSTEM_FINGERPRINT = "system_fingerprint";
public static final String METADATA_KEY_SERVICE_TIER = "service_tier";

/** Details for a tool call made by the model. */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
static class ToolCall {
/** Index of the tool call, used in response streaming. */
public Integer index;

/** Unique ID assigned to the tool call. */
public String id;

/** The type of tool. Supported values are "function" or "custom". */
public String type;

/** Function call details, if applicable. */
public Function function;

/** Custom tool call details, if applicable. */
public Custom custom;

/** Optional telemetry or signature information. */
@JsonProperty("extra_content")
public Map<String, Object> extraContent;

/**
* Converts the tool call to a {@link Part}.
*
* @return a {@link Part} containing the function call, or {@code null} if this tool call does
* not contain a function call.
*/
public @Nullable Part toPart() {

if (function != null) {
FunctionCall fc = function.toFunctionCall(id);
Part part = Part.builder().functionCall(fc).build();
return applyThoughtSignature(part);
}
return null;
}

/**
* Applies the thought signature from {@code extraContent} to the given {@link Part} if present.
* This is used to support the Google Gemini/Vertex AI implementation of the chat/completions
* API.
*
* @param part the {@link Part} to modify.
* @return a new {@link Part} with the thought signature applied, or the original {@link Part}
* if no thought signature is found.
*/
public Part applyThoughtSignature(Part part) {

if (extraContent != null && extraContent.containsKey("google")) {
Object googleObj = extraContent.get("google");
if (googleObj instanceof Map<?, ?> googleMap) {
Object sigObj = googleMap.get("thought_signature");
if (sigObj instanceof String sig) {
return part.toBuilder().thoughtSignature(Base64.getDecoder().decode(sig)).build();
}
}
}
return part;
}
}

/** Details for a function call. */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
static class Function {
/** Name of the function to be executed. */
public String name;

/** Arguments for the function call, provided as a JSON-formatted string. */
public String arguments; // JSON string

/**
* Converts this function to a {@link FunctionCall}.
*
* @param toolCallId the ID of the tool call, or {@code null} if not applicable.
* @return the {@link FunctionCall} object.
*/
public FunctionCall toFunctionCall(@Nullable String toolCallId) {

FunctionCall.Builder fcBuilder = FunctionCall.builder();
if (name != null) {
fcBuilder.name(name);
}
if (arguments != null) {
try {
Map<String, Object> args =
objectMapper.readValue(arguments, new TypeReference<Map<String, Object>>() {});
fcBuilder.args(args);
} catch (Exception e) {
throw new IllegalArgumentException(
"Failed to parse function arguments JSON: " + arguments, e);
}
}
if (toolCallId != null) {
fcBuilder.id(toolCallId);
}
return fcBuilder.build();
}
}

/** Details for a custom tool call. */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
static class Custom {
/** Model-generated input for the custom tool. */
public String input;

/** Name of the custom tool to be executed. */
public String name;
}
}
Loading