agentscope-ai / agentscope-ai/agentscope-java

[Bug]: interrupt don't work

Aperta
#1,036 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area/core/agent bug
Lingua principale
Java
Stelle
5.6k
Fork
1.3k
Merge medio
4g 12h
PR unite (30g)
77

Descrizione

**AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.**

**Describe the bug**
InterruptionExample do not work

**To Reproduce**
run main method

/*
* 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.examples.quickstart;

import io.agentscope.core.ReActAgent;
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
import io.agentscope.core.hook.ActingChunkEvent;
import io.agentscope.core.hook.ErrorEvent;
import io.agentscope.core.hook.Hook;
import io.agentscope.core.hook.HookEvent;
import io.agentscope.core.hook.PostActingEvent;
import io.agentscope.core.hook.PostCallEvent;
import io.agentscope.core.hook.PreActingEvent;
import io.agentscope.core.hook.PreCallEvent;
import io.agentscope.core.memory.InMemoryMemory;
import io.agentscope.core.message.ContentBlock;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
import io.agentscope.core.message.ToolResultBlock;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolEmitter;
import io.agentscope.core.tool.ToolParam;
import io.agentscope.core.tool.Toolkit;
import io.agentscope.examples.quickstart.util.MsgUtils;

import java.util.List;

import reactor.core.publisher.Mono;

import static io.agentscope.examples.quickstart.ModelConstants.CHAT_MODEL;

/**
* InterruptionExample - Demonstrates agent interruption mechanism.
*/
public class InterruptionExample {

public static void main(String[] args) throws Exception {
// Print welcome message
ExampleUtils.printWelcome("Interruption Example", "This example demonstrates user-initiated interruption of agent execution.\n" + "The agent will start a long-running task and be interrupted after 2" + " seconds.");
// Create toolkit with long-running tool
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new LongRunningTools());

// Create agent with monitoring hook
ReActAgent agent = ReActAgent.builder().name("DataAgent").sysPrompt("You are a data processing assistant. " + "Use the process_large_dataset tool to process datasets.").model(CHAT_MODEL).toolkit(toolkit).memory(new InMemoryMemory()).maxIters(10).build();

// Create user message
Msg userMsg = Msg.builder().name("User").role(MsgRole.USER).content(TextBlock.builder().text("Please process the 'customer_data' dataset with" + " 'analyze' operation.").build()).build();
Thread agentThread = new Thread(() -> {
try {
agent.call(userMsg).block();
} catch (Exception e) {
System.err.println("[--------------------------------------------------------------] " + e.getMessage());
}
});

agentThread.start();
try {
System.out.println("cccccccccccccccccccc1");
Thread.sleep(5000);
agent.interrupt();
System.out.println("cccccccccccccccccccc2");
agentThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Main thread interrupted: " + e.getMessage());
}
}

public static class LongRunningTools {

@Tool(name = "process_large_dataset", description = "Process a large dataset (simulated long operation)")
public String processLargeDataset(@ToolParam(name = "dataset_name", description = "Name of the dataset") String datasetName, @ToolParam(name = "operation", description = "Operation to perform") String operation, ToolEmitter toolEmitter) {

System.out.println("[Tool] Starting to process dataset: " + datasetName + " with operation: " + operation);

// Simulate long-running operation with progress updates
for (int i = 1; i <= 10; i++) {
try {
System.out.println("---------------------------------------------do work" +"Processed " + (i * 10) + "%");
Thread.sleep(500); // 500ms per step = 5 seconds total
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("---------------------------------------------error");
return "Processing interrupted";
}

// Emit progress
toolEmitter.emit(ToolResultBlock.text("Processed " + (i * 10) + "%"));
}

return String.format("Successfully processed dataset '%s' with operation '%s'. " + "Results: 1000 records analyzed.", datasetName, operation);
}
}
}


io.agentscope
agentscope-dependencies-bom
${revision}
pom
import


io.agentscope
agentscope-bom
${revision}
pom
import

1.0.10

**Expected behavior**
=== Interruption Example ===

This example demonstrates user-initiated interruption of agent execution.
The agent will start a long-running task and be interrupted after 2 seconds.

[main] INFO io.agentscope.core.tool.Toolkit - Registered tool 'process_large_dataset' in group 'ungrouped'
cccccccccccccccccccc1
[Tool] Starting to process dataset: customer_data with operation: analyze
---------------------------------------------do workProcessed 10%
---------------------------------------------do workProcessed 20%
---------------------------------------------do workProcessed 30%
---------------------------------------------do workProcessed 40%
---------------------------------------------do workProcessed 50%
---------------------------------------------do workProcessed 60%
cccccccccccccccccccc2
---------------------------------------------do workProcessed 70%
---------------------------------------------do workProcessed 80%
---------------------------------------------do workProcessed 90%
---------------------------------------------do workProcessed 100%
[HttpTransportFactory-ShutdownHook] INFO io.agentscope.core.model.transport.HttpTransportFactory - Shutting down 1 managed HttpTransport(s)
Disconnected from the target VM, address: '127.0.0.1:10631', transport: 'socket'

**Error messages**
Detailed error messages.

**Environment (please complete the following information):**

version: 1.0.10
java version:17
windows
here.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.