|
| 1 | +package io.temporal.springai.replay; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import io.temporal.activity.ActivityInterface; |
| 6 | +import io.temporal.activity.ActivityMethod; |
| 7 | +import io.temporal.activity.ActivityOptions; |
| 8 | +import io.temporal.api.history.v1.HistoryEvent; |
| 9 | +import io.temporal.client.WorkflowClient; |
| 10 | +import io.temporal.client.WorkflowOptions; |
| 11 | +import io.temporal.client.WorkflowStub; |
| 12 | +import io.temporal.springai.activity.ChatModelActivityImpl; |
| 13 | +import io.temporal.springai.chat.TemporalChatClient; |
| 14 | +import io.temporal.springai.model.ActivityChatModel; |
| 15 | +import io.temporal.testing.TestEnvironmentOptions; |
| 16 | +import io.temporal.testing.TestWorkflowEnvironment; |
| 17 | +import io.temporal.worker.Worker; |
| 18 | +import io.temporal.worker.WorkerFactoryOptions; |
| 19 | +import io.temporal.workflow.Workflow; |
| 20 | +import io.temporal.workflow.WorkflowInterface; |
| 21 | +import io.temporal.workflow.WorkflowMethod; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.ai.chat.client.ChatClient; |
| 29 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 30 | +import org.springframework.ai.chat.model.ChatModel; |
| 31 | +import org.springframework.ai.chat.model.ChatResponse; |
| 32 | +import org.springframework.ai.chat.model.Generation; |
| 33 | +import org.springframework.ai.chat.prompt.Prompt; |
| 34 | +import org.springframework.ai.tool.annotation.Tool; |
| 35 | + |
| 36 | +/** |
| 37 | + * Asserts that {@link TemporalChatClient#defaultTools} routes activity-stub tools through the |
| 38 | + * activity boundary the user already set up (via {@code Workflow.newActivityStub}) rather than |
| 39 | + * invoking the underlying impl directly from workflow code. This is a plugin property: the plugin |
| 40 | + * must recognize the stub as an activity-backed tool and invoke it as such, not unwrap it into a |
| 41 | + * plain in-workflow Java method call. Temporal's replay semantics for activities are assumed |
| 42 | + * correct. |
| 43 | + * |
| 44 | + * <p>We verify by scanning history for {@code ActivityTaskScheduled} events. Counting invocations |
| 45 | + * of the backing activity impl would conflate two different signals: the plugin inlining the tool |
| 46 | + * call (what we want to catch) vs. Temporal re-delivering the activity task to the worker (which |
| 47 | + * can legitimately happen with {@code maxAttempts > 1}). |
| 48 | + */ |
| 49 | +class ActivityToolSideEffectTest { |
| 50 | + |
| 51 | + private static final String TASK_QUEUE = "test-spring-ai-activity-tool-side-effect"; |
| 52 | + |
| 53 | + private TestWorkflowEnvironment testEnv; |
| 54 | + private WorkflowClient client; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void setUp() { |
| 58 | + testEnv = |
| 59 | + TestWorkflowEnvironment.newInstance( |
| 60 | + TestEnvironmentOptions.newBuilder() |
| 61 | + .setWorkerFactoryOptions( |
| 62 | + WorkerFactoryOptions.newBuilder().setWorkflowCacheSize(0).build()) |
| 63 | + .build()); |
| 64 | + client = testEnv.getWorkflowClient(); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterEach |
| 68 | + void tearDown() { |
| 69 | + testEnv.close(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void activityTool_schedulesExactlyOneActivity() { |
| 74 | + Worker worker = testEnv.newWorker(TASK_QUEUE); |
| 75 | + worker.registerWorkflowImplementationTypes(ChatWithToolsWorkflowImpl.class); |
| 76 | + worker.registerActivitiesImplementations( |
| 77 | + new ChatModelActivityImpl(new ToolCallingStubChatModel()), new AddActivityImpl()); |
| 78 | + testEnv.start(); |
| 79 | + |
| 80 | + ChatWithToolsWorkflow workflow = |
| 81 | + client.newWorkflowStub( |
| 82 | + ChatWithToolsWorkflow.class, |
| 83 | + WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build()); |
| 84 | + assertEquals("The answer is 5", workflow.chat("What is 2+3?")); |
| 85 | + |
| 86 | + List<HistoryEvent> events = |
| 87 | + client |
| 88 | + .fetchHistory(WorkflowStub.fromTyped(workflow).getExecution().getWorkflowId()) |
| 89 | + .getHistory() |
| 90 | + .getEventsList(); |
| 91 | + long scheduled = |
| 92 | + events.stream() |
| 93 | + .filter(HistoryEvent::hasActivityTaskScheduledEventAttributes) |
| 94 | + .filter( |
| 95 | + e -> |
| 96 | + "Add" |
| 97 | + .equals( |
| 98 | + e.getActivityTaskScheduledEventAttributes() |
| 99 | + .getActivityType() |
| 100 | + .getName())) |
| 101 | + .count(); |
| 102 | + assertEquals( |
| 103 | + 1, |
| 104 | + scheduled, |
| 105 | + "TemporalChatClient must invoke activity-stub tools as activities; expected exactly one" |
| 106 | + + " Add ActivityTaskScheduled event"); |
| 107 | + } |
| 108 | + |
| 109 | + @WorkflowInterface |
| 110 | + public interface ChatWithToolsWorkflow { |
| 111 | + @WorkflowMethod |
| 112 | + String chat(String message); |
| 113 | + } |
| 114 | + |
| 115 | + @ActivityInterface |
| 116 | + public interface AddActivity { |
| 117 | + @Tool(description = "Add two numbers") |
| 118 | + @ActivityMethod |
| 119 | + int add(int a, int b); |
| 120 | + } |
| 121 | + |
| 122 | + public static class AddActivityImpl implements AddActivity { |
| 123 | + @Override |
| 124 | + public int add(int a, int b) { |
| 125 | + return a + b; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public static class ChatWithToolsWorkflowImpl implements ChatWithToolsWorkflow { |
| 130 | + @Override |
| 131 | + public String chat(String message) { |
| 132 | + ActivityChatModel chatModel = ActivityChatModel.forDefault(); |
| 133 | + AddActivity addTool = |
| 134 | + Workflow.newActivityStub( |
| 135 | + AddActivity.class, |
| 136 | + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(30)).build()); |
| 137 | + ChatClient chatClient = TemporalChatClient.builder(chatModel).defaultTools(addTool).build(); |
| 138 | + return chatClient.prompt().user(message).call().content(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** First call: request the "add" tool. Second call: return final text. */ |
| 143 | + private static class ToolCallingStubChatModel implements ChatModel { |
| 144 | + private final AtomicInteger callCount = new AtomicInteger(0); |
| 145 | + |
| 146 | + @Override |
| 147 | + public ChatResponse call(Prompt prompt) { |
| 148 | + if (callCount.getAndIncrement() == 0) { |
| 149 | + AssistantMessage toolRequest = |
| 150 | + AssistantMessage.builder() |
| 151 | + .content("") |
| 152 | + .toolCalls( |
| 153 | + List.of( |
| 154 | + new AssistantMessage.ToolCall( |
| 155 | + "call_1", "function", "add", "{\"a\":2,\"b\":3}"))) |
| 156 | + .build(); |
| 157 | + return ChatResponse.builder().generations(List.of(new Generation(toolRequest))).build(); |
| 158 | + } |
| 159 | + return ChatResponse.builder() |
| 160 | + .generations(List.of(new Generation(new AssistantMessage("The answer is 5")))) |
| 161 | + .build(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public reactor.core.publisher.Flux<ChatResponse> stream(Prompt prompt) { |
| 166 | + throw new UnsupportedOperationException(); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments