|
|
@@ -0,0 +1,155 @@
|
|
|
+package com.zsjz.ai.module.ai.core.agent;
|
|
|
+
|
|
|
+import com.zsjz.ai.module.ai.config.AiProperties;
|
|
|
+import com.zsjz.ai.module.ai.core.AiRetrievalSourceRegistry;
|
|
|
+import com.zsjz.ai.module.ai.core.safety.AiSafetyGuard;
|
|
|
+import com.zsjz.ai.module.ai.domain.AiToolCallLog;
|
|
|
+import com.zsjz.ai.module.ai.service.IAiSafetyEventService;
|
|
|
+import com.zsjz.ai.module.ai.service.IAiTraceService;
|
|
|
+import io.agentscope.core.event.ToolResultEndEvent;
|
|
|
+import io.agentscope.core.event.ToolResultStartEvent;
|
|
|
+import io.agentscope.core.event.ToolResultTextDeltaEvent;
|
|
|
+import io.agentscope.core.message.ToolResultState;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * HITL 恢复段工具明细落库测试(DEF-03)。
|
|
|
+ *
|
|
|
+ * <p>恢复段不会重新收到 {@code ToolCallStartEvent} / {@code ToolCallDeltaEvent},只收到结果事件。
|
|
|
+ * 因此必须在恢复前把 Redis 暂存的原始入参回填进流式状态,并用 {@code ToolResultStartEvent}
|
|
|
+ * 登记执行起点,否则确认/拒绝类工具的 {@code toolInput} 为空、{@code durationMs} 为 null。</p>
|
|
|
+ *
|
|
|
+ * @author BizBuddy
|
|
|
+ */
|
|
|
+class AiStreamStateResumeToolLogTest {
|
|
|
+
|
|
|
+ private static final String REPLY_ID = "reply-1";
|
|
|
+
|
|
|
+ private static final String TOOL_CALL_ID = "call-1";
|
|
|
+
|
|
|
+ private static final String TOOL_NAME = "execute";
|
|
|
+
|
|
|
+ private static final String RAW_INPUT = "{\"command\":\"echo hi\"}";
|
|
|
+
|
|
|
+ private AiStreamEventBridge bridge;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ AiRetrievalSourceRegistry sourceRegistry = mock(AiRetrievalSourceRegistry.class);
|
|
|
+ when(sourceRegistry.drain(anyString())).thenReturn(List.of());
|
|
|
+ bridge = new AiStreamEventBridge(
|
|
|
+ mock(IAiTraceService.class),
|
|
|
+ mock(AiProperties.class),
|
|
|
+ mock(AiSafetyGuard.class),
|
|
|
+ mock(IAiSafetyEventService.class),
|
|
|
+ sourceRegistry,
|
|
|
+ mock(ToolLabelResolver.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void confirmedResumeKeepsInputAndDuration() {
|
|
|
+ AiStreamState state = new AiStreamState();
|
|
|
+ seed(state, TOOL_CALL_ID, TOOL_NAME, RAW_INPUT);
|
|
|
+
|
|
|
+ bridge.handle(null, state, new ToolResultStartEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultTextDeltaEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME, "ok"), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultEndEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME, ToolResultState.SUCCESS), false);
|
|
|
+
|
|
|
+ AiToolCallLog log = onlyLog(state);
|
|
|
+ assertEquals(RAW_INPUT, log.getToolInput());
|
|
|
+ assertEquals("SUCCESS", log.getState());
|
|
|
+ assertEquals("ok", log.getToolOutput());
|
|
|
+ assertNotNull(log.getDurationMs());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void deniedResumeKeepsInputReasonAndDuration() {
|
|
|
+ AiStreamState state = new AiStreamState();
|
|
|
+ seed(state, TOOL_CALL_ID, TOOL_NAME, RAW_INPUT);
|
|
|
+ String reason = "请不要执行命令";
|
|
|
+
|
|
|
+ bridge.handle(null, state, new ToolResultStartEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultTextDeltaEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME, reason), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultEndEvent(REPLY_ID, TOOL_CALL_ID, TOOL_NAME, ToolResultState.DENIED), false);
|
|
|
+
|
|
|
+ AiToolCallLog log = onlyLog(state);
|
|
|
+ assertEquals(RAW_INPUT, log.getToolInput());
|
|
|
+ assertEquals("DENIED", log.getState());
|
|
|
+ assertEquals(reason, log.getToolOutput());
|
|
|
+ assertNotNull(log.getDurationMs());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void seedingMultipleCallsKeepsBothInputs() {
|
|
|
+ AiStreamState state = new AiStreamState();
|
|
|
+ state.seedPendingToolCalls(List.of(
|
|
|
+ new AiConfirmPendingStore.PendingToolCall("call-a", "execute",
|
|
|
+ Map.of("command", "echo a"), "{\"command\":\"echo a\"}"),
|
|
|
+ new AiConfirmPendingStore.PendingToolCall("call-b", "write_file",
|
|
|
+ Map.of("path", "b.txt"), "{\"path\":\"b.txt\"}")));
|
|
|
+
|
|
|
+ bridge.handle(null, state, new ToolResultStartEvent(REPLY_ID, "call-a", "execute"), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultEndEvent(REPLY_ID, "call-a", "execute", ToolResultState.SUCCESS), false);
|
|
|
+
|
|
|
+ // 第二次暂停/恢复只回填新的待确认工具,不得覆盖上一次已执行的入参
|
|
|
+ state.seedPendingToolCalls(List.of(
|
|
|
+ new AiConfirmPendingStore.PendingToolCall("call-b", "write_file",
|
|
|
+ Map.of("path", "b.txt"), "{\"path\":\"b.txt\"}")));
|
|
|
+ bridge.handle(null, state, new ToolResultStartEvent(REPLY_ID, "call-b", "write_file"), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultEndEvent(REPLY_ID, "call-b", "write_file", ToolResultState.SUCCESS), false);
|
|
|
+
|
|
|
+ List<AiToolCallLog> logs = state.toToolLogs(200, 10L);
|
|
|
+ assertEquals(2, logs.size());
|
|
|
+ assertEquals("{\"command\":\"echo a\"}", findLog(logs, "call-a").getToolInput());
|
|
|
+ assertEquals("{\"path\":\"b.txt\"}", findLog(logs, "call-b").getToolInput());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void emptyInputForExecutedToolBecomesEmptyJson() {
|
|
|
+ AiStreamState state = new AiStreamState();
|
|
|
+ state.seedPendingToolCalls(List.of(
|
|
|
+ new AiConfirmPendingStore.PendingToolCall("call-plan", "plan_exit", Map.of(), "")));
|
|
|
+
|
|
|
+ bridge.handle(null, state, new ToolResultStartEvent(REPLY_ID, "call-plan", "plan_exit"), false);
|
|
|
+ bridge.handle(null, state,
|
|
|
+ new ToolResultEndEvent(REPLY_ID, "call-plan", "plan_exit", ToolResultState.SUCCESS), false);
|
|
|
+
|
|
|
+ AiToolCallLog log = onlyLog(state);
|
|
|
+ assertEquals("{}", log.getToolInput());
|
|
|
+ assertNotNull(log.getDurationMs());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void seed(AiStreamState state, String toolCallId, String toolName, String rawInput) {
|
|
|
+ state.seedPendingToolCalls(List.of(new AiConfirmPendingStore.PendingToolCall(
|
|
|
+ toolCallId, toolName, Map.of("command", "echo hi"), rawInput)));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static AiToolCallLog onlyLog(AiStreamState state) {
|
|
|
+ List<AiToolCallLog> logs = state.toToolLogs(200, 10L);
|
|
|
+ assertEquals(1, logs.size());
|
|
|
+ return logs.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static AiToolCallLog findLog(List<AiToolCallLog> logs, String toolCallId) {
|
|
|
+ return logs.stream()
|
|
|
+ .filter(log -> toolCallId.equals(log.getToolCallId()))
|
|
|
+ .findFirst()
|
|
|
+ .orElseThrow();
|
|
|
+ }
|
|
|
+}
|