|
| 1 | +package com.launchdarkly.sdk.server.ai.datamodel; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * A single prompt message in an AI Config, consisting of a {@link Role} and string content. |
| 7 | + * <p> |
| 8 | + * Instances are immutable. |
| 9 | + */ |
| 10 | +public final class LDMessage { |
| 11 | + /** |
| 12 | + * The role of a {@link LDMessage}. |
| 13 | + */ |
| 14 | + public enum Role { |
| 15 | + /** |
| 16 | + * A system message, typically used to set behavior or context. |
| 17 | + */ |
| 18 | + SYSTEM("system"), |
| 19 | + |
| 20 | + /** |
| 21 | + * A message authored by the end user. |
| 22 | + */ |
| 23 | + USER("user"), |
| 24 | + |
| 25 | + /** |
| 26 | + * A message authored by the assistant (model). |
| 27 | + */ |
| 28 | + ASSISTANT("assistant"); |
| 29 | + |
| 30 | + private final String wireValue; |
| 31 | + |
| 32 | + Role(String wireValue) { |
| 33 | + this.wireValue = wireValue; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the string used to represent this role in the JSON protocol. |
| 38 | + * |
| 39 | + * @return the wire representation (for example {@code "system"}) |
| 40 | + */ |
| 41 | + public String getWireValue() { |
| 42 | + return wireValue; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Resolves a wire string to a role. |
| 47 | + * |
| 48 | + * @param value the wire value, such as {@code "user"}; may be {@code null} |
| 49 | + * @return the matching role, or {@code null} if the value is {@code null} or unrecognized |
| 50 | + */ |
| 51 | + public static Role fromWireValue(String value) { |
| 52 | + if (value == null) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + for (Role role : values()) { |
| 56 | + if (role.wireValue.equals(value)) { |
| 57 | + return role; |
| 58 | + } |
| 59 | + } |
| 60 | + return null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private final Role role; |
| 65 | + private final String content; |
| 66 | + |
| 67 | + /** |
| 68 | + * Constructs a message. |
| 69 | + * |
| 70 | + * @param role the role of the message; must not be {@code null} |
| 71 | + * @param content the message content; must not be {@code null} |
| 72 | + * @throws NullPointerException if {@code role} or {@code content} is {@code null} |
| 73 | + */ |
| 74 | + public LDMessage(Role role, String content) { |
| 75 | + this.role = Objects.requireNonNull(role, "role"); |
| 76 | + this.content = Objects.requireNonNull(content, "content"); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the role of this message. |
| 81 | + * |
| 82 | + * @return the role, never {@code null} |
| 83 | + */ |
| 84 | + public Role getRole() { |
| 85 | + return role; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the content of this message. |
| 90 | + * |
| 91 | + * @return the content, never {@code null} |
| 92 | + */ |
| 93 | + public String getContent() { |
| 94 | + return content; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns a copy of this message with the given content, preserving the role. |
| 99 | + * <p> |
| 100 | + * Used by the interpolation layer to produce a rendered message without mutating the original. |
| 101 | + * |
| 102 | + * @param newContent the replacement content; must not be {@code null} |
| 103 | + * @return a new {@link LDMessage} |
| 104 | + */ |
| 105 | + public LDMessage withContent(String newContent) { |
| 106 | + return new LDMessage(role, newContent); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean equals(Object o) { |
| 111 | + if (this == o) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + if (!(o instanceof LDMessage)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + LDMessage other = (LDMessage) o; |
| 118 | + return role == other.role && content.equals(other.content); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int hashCode() { |
| 123 | + return Objects.hash(role, content); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String toString() { |
| 128 | + return "LDMessage{role=" + role + ", content=" + content + '}'; |
| 129 | + } |
| 130 | +} |
0 commit comments