All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.ai.spring.chat.prompt.MessageContent Maven / Gradle / Ivy

package fi.evolver.ai.spring.chat.prompt;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import fi.evolver.ai.spring.chat.FunctionCall;
import fi.evolver.ai.spring.file.AiFile;

public interface MessageContent {
	public String asText();

	public static class TextContent implements MessageContent {

		private final String text;

		public TextContent(String text) {
			this.text = text;
		}

		public String getText() {
			return text;
		}

		@Override
		@JsonIgnore
		public String asText() {
			return getText();
		}

		@Override
		public int hashCode() {
			return Objects.hash(text);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			TextContent other = (TextContent) obj;
			return Objects.equals(text, other.text);
		}

		@Override
		public String toString() {
			return "TextContent [text=" + text + "]";
		}
	}

	public static class ImageContent implements MessageContent {

		private final AiFile data;

		public ImageContent(AiFile data) {
			this.data = data;
		}

		public AiFile getData() {
			return data;
		}

		@Override
		@JsonIgnore
		public String asText() {
			return "[" + data.fileName() + "]";
		}

		@Override
		public int hashCode() {
			return Objects.hash(data);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ImageContent other = (ImageContent) obj;
			return Objects.equals(data, other.data);
		}

		@Override
		public String toString() {
			return "ImageContent [data=" + data + "]";
		}
	}


	public static class ToolCallsContent implements MessageContent {
		private final List functionCalls;


		public ToolCallsContent(List functionCalls) {
			this.functionCalls = functionCalls;
		}


		public List getFunctionCalls() {
			return functionCalls;
		}

		@Override
		public String asText() {
			return functionCalls.stream().map(FunctionCall::getFunctionName).collect(Collectors.joining(", "));
		}
	}


	public static class ToolResultContent implements MessageContent {

		private final String toolUseId;
		private final List content;

		public ToolResultContent(String toolUseId, List content) {
			this.toolUseId = toolUseId;
			this.content = content;
		}

		public String getToolUseId() {
			return toolUseId;
		}

		@JsonProperty("content")
		public List getContents() {
			return content;
		}

		@JsonIgnore
		public String getContent() {
			return String.join("\n", content.stream().map(MessageContent::asText).toList());
		}

		@Override
		@JsonIgnore
		public String asText() {
			return "Result for tool usage " + toolUseId + ":\n" + String.join("\n", content.stream().map(MessageContent::asText).toList());
		}

		@Override
		public int hashCode() {
			return Objects.hash(content, toolUseId);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ToolResultContent other = (ToolResultContent) obj;
			return Objects.equals(content, other.content) && Objects.equals(toolUseId, other.toolUseId);
		}

		@Override
		public String toString() {
			return "ToolResultContent [toolUseId=" + toolUseId + ", content=" + content + "]";
		}
	}

	public static TextContent text(String text) {
		return new TextContent(text);
	}

	public static ImageContent image(AiFile data) {
		return new ImageContent(data);
	}

	public static ToolCallsContent toolCalls(List calls) {
		return new ToolCallsContent(calls);
	}

	public static ToolResultContent toolResult(String toolUseId, List content) {
		return new ToolResultContent(toolUseId, content);
	}

	public static ToolResultContent toolResult(String toolUseId, MessageContent content) {
		return toolResult(toolUseId, List.of(content));
	}

	public static ToolResultContent toolResult(String toolUseId, String content) {
		return toolResult(toolUseId, text(content));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy