-
Notifications
You must be signed in to change notification settings - Fork 796
fix(middleware): expose runtime context in onAgent #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guslegend0510
wants to merge
7
commits into
agentscope-ai:main
Choose a base branch
from
guslegend0510:fix/issue-1672-runtime-context-in-middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e1d029
fix(middleware): expose runtime context in onAgent
guslegend 537e253
test(middleware): cover runtime context branches
guslegend 3f3a565
Merge upstream/main into fix/issue-1672-runtime-context-in-middleware
guslegend d767a3b
test(agent): cover default runtime context contract
guslegend 71108d1
chore: retrigger ci
guslegend fb2afdb
style(harness): remove redundant runtime context null check
guslegend 889d6d7
Merge upstream/main into fix/issue-1672-runtime-context-in-middleware
guslegend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
agentscope-core/src/test/java/io/agentscope/core/ReActAgentAgentImplRuntimeContextTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.agentscope.core; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.agentscope.core.agent.Agent; | ||
| import io.agentscope.core.agent.RuntimeContext; | ||
| import io.agentscope.core.event.AgentEndEvent; | ||
| import io.agentscope.core.event.AgentEvent; | ||
| import io.agentscope.core.message.ContentBlock; | ||
| import io.agentscope.core.message.Msg; | ||
| import io.agentscope.core.message.TextBlock; | ||
| import io.agentscope.core.middleware.ActingInput; | ||
| import io.agentscope.core.middleware.AgentInput; | ||
| import io.agentscope.core.middleware.MiddlewareBase; | ||
| import io.agentscope.core.middleware.ModelCallInput; | ||
| import io.agentscope.core.middleware.ReasoningInput; | ||
| import io.agentscope.core.model.ChatModelBase; | ||
| import io.agentscope.core.model.ChatResponse; | ||
| import io.agentscope.core.model.GenerateOptions; | ||
| import io.agentscope.core.model.ToolSchema; | ||
| import io.agentscope.core.tool.Toolkit; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Function; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| class ReActAgentAgentImplRuntimeContextTest { | ||
|
|
||
| private static final class FixedTextModel extends ChatModelBase { | ||
| @Override | ||
| public String getModelName() { | ||
| return "fixed"; | ||
| } | ||
|
|
||
| @Override | ||
| protected Flux<ChatResponse> doStream( | ||
| List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) { | ||
| return Flux.just( | ||
| ChatResponse.builder() | ||
| .content(List.<ContentBlock>of(TextBlock.builder().text("ok").build())) | ||
| .build()); | ||
| } | ||
| } | ||
|
|
||
| private static final class CapturingMiddleware implements MiddlewareBase { | ||
| private final boolean shortCircuit; | ||
| private final AtomicReference<RuntimeContext> seen = new AtomicReference<>(); | ||
|
|
||
| private CapturingMiddleware(boolean shortCircuit) { | ||
| this.shortCircuit = shortCircuit; | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<AgentEvent> onAgent( | ||
| Agent agent, | ||
| RuntimeContext ctx, | ||
| AgentInput input, | ||
| Function<AgentInput, Flux<AgentEvent>> next) { | ||
| seen.set(ctx); | ||
| return shortCircuit ? Flux.empty() : next.apply(input); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<AgentEvent> onReasoning( | ||
| Agent agent, | ||
| RuntimeContext ctx, | ||
| ReasoningInput input, | ||
| Function<ReasoningInput, Flux<AgentEvent>> next) { | ||
| return next.apply(input); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<AgentEvent> onModelCall( | ||
| Agent agent, | ||
| RuntimeContext ctx, | ||
| ModelCallInput input, | ||
| Function<ModelCallInput, Flux<AgentEvent>> next) { | ||
| return next.apply(input); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<AgentEvent> onActing( | ||
| Agent agent, | ||
| RuntimeContext ctx, | ||
| ActingInput input, | ||
| Function<ActingInput, Flux<AgentEvent>> next) { | ||
| return next.apply(input); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<String> onSystemPrompt(Agent agent, RuntimeContext ctx, String currentPrompt) { | ||
| return Mono.just(currentPrompt); | ||
| } | ||
| } | ||
|
|
||
| private static ReActAgent buildAgent(MiddlewareBase middleware) { | ||
| return ReActAgent.builder() | ||
| .name("asst") | ||
| .sysPrompt("hello-system") | ||
| .model(new FixedTextModel()) | ||
| .toolkit(new Toolkit()) | ||
| .middlewares(List.of(middleware)) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| void streamEventsRunsCoreLifecycleWithRuntimeContext() { | ||
| CapturingMiddleware middleware = new CapturingMiddleware(false); | ||
| ReActAgent agent = buildAgent(middleware); | ||
| RuntimeContext runtimeContext = | ||
| RuntimeContext.builder().sessionId("runtime-context-session").build(); | ||
|
|
||
| List<AgentEvent> events = | ||
| agent.streamEvents(List.of(), runtimeContext).collectList().block(); | ||
|
|
||
| assertNotNull(events); | ||
| assertTrue(events.get(events.size() - 1) instanceof AgentEndEvent); | ||
| assertSame(runtimeContext, middleware.seen.get()); | ||
| assertNull(agent.getRuntimeContext()); | ||
| } | ||
|
|
||
| @Test | ||
| void streamEventsClearsRuntimeContextWhenShortCircuited() { | ||
| CapturingMiddleware middleware = new CapturingMiddleware(true); | ||
| ReActAgent agent = buildAgent(middleware); | ||
| RuntimeContext runtimeContext = | ||
| RuntimeContext.builder().sessionId("runtime-context-session").build(); | ||
|
|
||
| List<AgentEvent> events = | ||
| agent.streamEvents(List.of(), runtimeContext).collectList().block(); | ||
|
|
||
| assertNotNull(events); | ||
| assertTrue(events.isEmpty(), events.toString()); | ||
| assertSame(runtimeContext, middleware.seen.get()); | ||
| assertNull(agent.getRuntimeContext()); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
agentscope-core/src/test/java/io/agentscope/core/agent/AgentTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.agentscope.core.agent; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import io.agentscope.core.message.Msg; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| class AgentTest { | ||
|
|
||
| @Test | ||
| void getRuntimeContext_defaultsToNull() { | ||
| Agent agent = new BareAgent(); | ||
|
|
||
| assertNull(agent.getRuntimeContext()); | ||
| } | ||
|
|
||
| private static final class BareAgent implements Agent { | ||
|
|
||
| @Override | ||
| public String getAgentId() { | ||
| return "bare-agent"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "bare-agent"; | ||
| } | ||
|
|
||
| @Override | ||
| public void interrupt() {} | ||
|
|
||
| @Override | ||
| public void interrupt(Msg msg) {} | ||
|
|
||
| @Override | ||
| public Mono<Msg> call(List<Msg> msgs) { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Msg> call(List<Msg> msgs, Class<?> structuredModel) { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Msg> call(List<Msg> msgs, JsonNode schema) { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<Event> stream(List<Msg> msgs, StreamOptions options) { | ||
| return Flux.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<Event> stream(List<Msg> msgs, StreamOptions options, Class<?> structuredModel) { | ||
| return Flux.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<Event> stream(List<Msg> msgs, StreamOptions options, JsonNode schema) { | ||
| return Flux.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Void> observe(Msg msg) { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Void> observe(List<Msg> msgs) { | ||
| return Mono.empty(); | ||
| } | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
...core/src/test/java/io/agentscope/core/skill/DynamicSkillMiddlewareRuntimeContextTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.agentscope.core.skill; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.agentscope.core.agent.Agent; | ||
| import io.agentscope.core.agent.RuntimeContext; | ||
| import io.agentscope.core.skill.repository.AgentSkillRepository; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class DynamicSkillMiddlewareRuntimeContextTest { | ||
|
|
||
| private static final class CapturingDynamicSkillMiddleware extends DynamicSkillMiddleware { | ||
| private final AtomicReference<RuntimeContext> seen = new AtomicReference<>(); | ||
|
|
||
| private CapturingDynamicSkillMiddleware(List<AgentSkillRepository> repositories) { | ||
| super(repositories, null); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<AgentSkill> filterVisible(List<AgentSkill> raw, RuntimeContext ctx) { | ||
| seen.set(ctx); | ||
| return raw; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void onSystemPromptFallsBackToEmptyContextWhenAgentIsNull() { | ||
| CapturingDynamicSkillMiddleware middleware = | ||
| new CapturingDynamicSkillMiddleware(List.of(skillRepo())); | ||
|
|
||
| String prompt = middleware.onSystemPrompt(null, RuntimeContext.empty(), "BASE").block(); | ||
|
|
||
| assertNotNull(prompt); | ||
| assertTrue(prompt.startsWith("BASE")); | ||
| assertNotNull(middleware.seen.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void onSystemPromptUsesSuppliedRuntimeContext() { | ||
| RuntimeContext runtimeContext = | ||
| RuntimeContext.builder().sessionId("dynamic-skill-session").build(); | ||
| Agent agent = mock(Agent.class); | ||
| CapturingDynamicSkillMiddleware middleware = | ||
| new CapturingDynamicSkillMiddleware(List.of(skillRepo())); | ||
|
|
||
| String prompt = middleware.onSystemPrompt(agent, runtimeContext, "BASE").block(); | ||
|
|
||
| assertNotNull(prompt); | ||
| assertSame(runtimeContext, middleware.seen.get()); | ||
| } | ||
|
|
||
| private static AgentSkillRepository skillRepo() { | ||
| AgentSkillRepository repo = mock(AgentSkillRepository.class); | ||
| when(repo.getAllSkills()) | ||
| .thenReturn( | ||
| List.of( | ||
| new AgentSkill( | ||
| "sample", | ||
| "Sample skill", | ||
| "You can use the sample skill.", | ||
| Map.of()))); | ||
| return repo; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.