Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.agentscope.core.state.StateModule;
import io.agentscope.core.tracing.TracerRegistry;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -98,6 +99,8 @@ public abstract class AgentBase implements StateModule, Agent {
// Interrupt state management (available to all agents)
private final AtomicBoolean interruptFlag = new AtomicBoolean(false);
private final AtomicReference<Msg> userInterruptMessage = new AtomicReference<>(null);
// Hook non-null
private static final Comparator<Hook> HOOK_COMPARATOR = Comparator.comparingInt(Hook::priority);

/**
* Constructor for AgentBase.
Expand Down Expand Up @@ -133,6 +136,7 @@ public AgentBase(String name, String description, boolean checkRunning, List<Hoo
this.checkRunning = checkRunning;
this.hooks = new CopyOnWriteArrayList<>(hooks != null ? hooks : List.of());
this.hooks.addAll(systemHooks);
sortHooks();
}

@Override
Expand Down Expand Up @@ -474,9 +478,14 @@ public List<Hook> getHooks() {
protected void addHook(Hook hook) {
if (hook != null) {
hooks.add(hook);
sortHooks();
}
}

private void sortHooks() {
this.hooks.sort(HOOK_COMPARATOR);
}

/**
* Remove a hook from this agent dynamically.
*
Expand All @@ -498,7 +507,7 @@ protected void removeHook(Hook hook) {
* @return Sorted list of hooks
*/
protected List<Hook> getSortedHooks() {
return hooks.stream().sorted(java.util.Comparator.comparingInt(Hook::priority)).toList();
return hooks;
}

/**
Expand Down Expand Up @@ -692,7 +701,7 @@ private Flux<Event> createEventStream(StreamOptions options, Supplier<Mono<Msg>>
new StreamingHook(sink, options);

// Add temporary hook
hooks.add(streamingHook);
addHook(streamingHook);

// Use Mono.defer to ensure trace context propagation
// while maintaining streaming hook functionality
Expand Down
Loading