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
39 changes: 29 additions & 10 deletions src/main/java/run/halo/live2d/agent/AgentSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ public List<AgentToolConfig> normalizedAiTools() {

@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentBuiltInCapabilities(
boolean pageContext,
boolean haloNavigation,
boolean haloContentSearch,
boolean networkAccess,
Boolean pageContext,
Boolean haloNavigation,
Boolean haloContentSearch,
Boolean networkAccess,
AgentCommentCapability commentCapability
) {
public AgentBuiltInCapabilities {
pageContext = pageContext == null ? true : pageContext;
haloNavigation = haloNavigation == null ? true : haloNavigation;
haloContentSearch = haloContentSearch == null ? true : haloContentSearch;
networkAccess = networkAccess == null ? false : networkAccess;
commentCapability = commentCapability == null
? AgentCommentCapability.ASSIST
: commentCapability;
Expand All @@ -74,8 +78,12 @@ public static AgentBuiltInCapabilities defaults() {
@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentToolSecurity(
Object allowedExternalOrigins,
boolean allowNewTab
Boolean allowNewTab
) {
public AgentToolSecurity {
allowNewTab = allowNewTab == null ? false : allowNewTab;
}

public static AgentToolSecurity defaults() {
return new AgentToolSecurity(List.of(), false);
}
Expand All @@ -88,8 +96,12 @@ public List<String> normalizedAllowedExternalOrigins() {
@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentHaloSearchSettings(
List<String> allowedTypes,
int defaultLimit
Integer defaultLimit
) {
public AgentHaloSearchSettings {
defaultLimit = defaultLimit == null ? 5 : defaultLimit;
}

public static AgentHaloSearchSettings defaults() {
return new AgentHaloSearchSettings(List.of(), 5);
}
Expand All @@ -108,9 +120,10 @@ public int normalizedDefaultLimit() {

@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentHaloResourceDetailSettings(
int maxContentChars
Integer maxContentChars
) {
public AgentHaloResourceDetailSettings {
maxContentChars = maxContentChars == null ? 3000 : maxContentChars;
if (maxContentChars < 500) {
maxContentChars = 3000;
}
Expand All @@ -127,9 +140,14 @@ public static AgentHaloResourceDetailSettings defaults() {
@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentNetworkAccessSettings(
Object allowedOrigins,
int maxResponseChars,
int timeoutSeconds
Integer maxResponseChars,
Integer timeoutSeconds
) {
public AgentNetworkAccessSettings {
maxResponseChars = maxResponseChars == null ? 4000 : maxResponseChars;
timeoutSeconds = timeoutSeconds == null ? 5 : timeoutSeconds;
}

public static AgentNetworkAccessSettings defaults() {
return new AgentNetworkAccessSettings(List.of(), 4000, 5);
}
Expand All @@ -152,7 +170,7 @@ public int normalizedTimeoutSeconds() {
@JsonIgnoreProperties(ignoreUnknown = true)
public record AgentToolConfig(
String name,
boolean enabled,
Boolean enabled,
String description,
JsonNode inputSchema,
AgentToolApproval approval,
Expand All @@ -161,6 +179,7 @@ public record AgentToolConfig(
JsonNode testInput
) {
public AgentToolConfig {
enabled = enabled == null ? false : enabled;
approval = approval == null ? AgentToolApproval.DEFAULT : approval;
requiredAuth = requiredAuth == null ? AgentToolAuth.NONE : requiredAuth;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/run/halo/live2d/chat/AiChatEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ record AiChatConfig(boolean isAiChat, AiChatBaseSetting aiChatBaseSetting) {
}
}

record AiChatBaseSetting(boolean isAnonymous, String accessMode, String systemMessage,
record AiChatBaseSetting(Boolean isAnonymous, String accessMode, String systemMessage,
String modelName) {
AiChatBaseSetting {
if (StringUtils.isBlank(systemMessage)) {
Expand All @@ -179,7 +179,7 @@ record AiChatBaseSetting(boolean isAnonymous, String accessMode, String systemMe
}

AgentAccessMode resolvedAccessMode() {
return AgentAccessMode.from(accessMode, isAnonymous);
return AgentAccessMode.from(accessMode, Boolean.TRUE.equals(isAnonymous));
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/test/java/run/halo/live2d/agent/AgentToolNormalizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,56 @@ void mapsFormValuesToAgentEnums() throws Exception {
.contains("\"commentCapability\":\"assist\"");
}

@Test
void mapsNullPrimitiveAgentSettingsToDefaults() throws Exception {
var settings = objectMapper.readValue(
"""
{
"builtIn": {
"pageContext": null,
"haloNavigation": null,
"haloContentSearch": null,
"networkAccess": null,
"commentCapability": null
},
"toolSecurity": {
"allowNewTab": null
},
"haloSearch": {
"defaultLimit": null
},
"haloResourceDetail": {
"maxContentChars": null
},
"networkAccess": {
"maxResponseChars": null,
"timeoutSeconds": null
},
"aiTools": [{
"name": "open_contact_form",
"enabled": null,
"description": "打开留言面板",
"action": {
"type": "registered"
}
}]
}
""",
AgentSettings.class);

assertThat(settings.builtIn().pageContext()).isTrue();
assertThat(settings.builtIn().haloNavigation()).isTrue();
assertThat(settings.builtIn().haloContentSearch()).isTrue();
assertThat(settings.builtIn().networkAccess()).isFalse();
assertThat(settings.builtIn().commentCapability()).isEqualTo(AgentCommentCapability.ASSIST);
assertThat(settings.toolSecurity().allowNewTab()).isFalse();
assertThat(settings.haloSearch().normalizedDefaultLimit()).isEqualTo(5);
assertThat(settings.haloResourceDetail().maxContentChars()).isEqualTo(3000);
assertThat(settings.networkAccess().normalizedMaxResponseChars()).isEqualTo(4000);
assertThat(settings.networkAccess().normalizedTimeoutSeconds()).isEqualTo(5);
assertThat(settings.normalizedAiTools().getFirst().enabled()).isFalse();
}

@Test
void normalizesValidCustomTool() throws Exception {
var settings = new AgentSettings(
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/run/halo/live2d/chat/AiChatEndpointTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package run.halo.live2d.chat;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import run.halo.live2d.agent.AgentAccessMode;

class AiChatEndpointTest {

private final ObjectMapper objectMapper = new ObjectMapper();

@Test
void mapsNullLegacyAnonymousSettingToAuthenticatedChat() throws Exception {
var config = objectMapper.readValue("""
{
"isAiChat": true,
"aiChatBaseSetting": {
"isAnonymous": null,
"systemMessage": "system",
"modelName": "model"
}
}
""", AiChatEndpoint.AiChatConfig.class);

assertThat(config.aiChatBaseSetting().resolvedAccessMode())
.isEqualTo(AgentAccessMode.AUTHENTICATED_CHAT);
}

@Test
void accessModeTakesPrecedenceOverNullLegacyAnonymousSetting() throws Exception {
var config = objectMapper.readValue("""
{
"isAiChat": true,
"aiChatBaseSetting": {
"isAnonymous": null,
"accessMode": "anonymous_chat",
"systemMessage": "system",
"modelName": "model"
}
}
""", AiChatEndpoint.AiChatConfig.class);

assertThat(config.aiChatBaseSetting().resolvedAccessMode())
.isEqualTo(AgentAccessMode.ANONYMOUS_CHAT);
}
}