BackgroundTask
public class BackgroundTask
io/agentscope/harness/agent/subagent/task/BackgroundTask.java:35–158Background 任务,支持智能体之间的发现、委派与协作。位于 io.agentscope.harness.agent.subagent.task 包。核心成员包括 whenComplete、getTaskId、getAgentId、getCreatedAt、getLastCheckedAt、updateLastCheckedAt。
Wraps a CompletableFuture to track background subagent task execution with status,
result, error, and lifecycle metadata. Thread-safe by delegation to the underlying future.
Lifecycle metadata:
-
agentId— which subagent type is executing -
createdAt— when the task was created -
lastCheckedAt— when the task status was last polled
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public BackgroundTask(String taskId, String agentId, CompletableFuture<String> future) | 初始化 `BackgroundTask` 实例。 Creates a new instance. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public BackgroundTask whenComplete(java.util.function.BiConsumer<String, Throwable> action) | 设置该参数。 Observe the backing future; workspace local tasks persist their result before resolving it. |
public String getTaskId() | 获取任务标识。 Returns the task id. |
public String getAgentId() | 获取智能体标识。 Returns the agent id. |
public Instant getCreatedAt() | 获取已创建 At。 Returns the created at. |
public Instant getLastCheckedAt() | 获取 `getLastCheckedAt`。 Returns the last checked at. |
public void updateLastCheckedAt() | 更新 `updateLastCheckedAt`。 Updates the last checked at. |
public boolean isCompleted() | 判断是否已完成。 Returns whether the completed. |
public TaskStatus getTaskStatus() | 获取任务状态。 Returns the structured `TaskStatus` for this task, reflecting the underlying future
state and any explicit cancellation. |
public String getStatus() | 获取状态。 Returns a human-readable status string. |
public String getResult() | 获取结果。 Non-blocking result peek; returns null if not yet completed or if an error occurred. |
public Exception getError() | 获取错误。 Returns the error. |
public boolean waitForCompletion(long timeoutMs) throws InterruptedException | 等待用于补全。 Blocks until the task completes or the timeout elapses. |
public boolean cancel(boolean mayInterruptIfRunning) | 执行取消相关操作。 Cancels the task. |
构造器详情Constructor Detail
BackgroundTask
public BackgroundTask(String taskId, String agentId, CompletableFuture<String> future)
初始化 BackgroundTask 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
taskId | String | 任务标识(类型 `String`)。The task id. |
agentId | String | 智能体标识(类型 `String`)。The agent id. |
future | CompletableFuture<String> | 异步(类型 `CompletableFuture`)。The future. |
io/agentscope/harness/agent/subagent/task/BackgroundTask.java:44–50方法详情Method Detail
whenComplete
public BackgroundTask whenComplete(java.util.function.BiConsumer<String, Throwable> action)
设置该参数。
Observe the backing future; workspace local tasks persist their result before resolving it.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
action | java.util.function.BiConsumer<String, Throwable> | 动作(类型 `java.util.function.BiConsumer`)。The action. |
BackgroundTaskio/agentscope/harness/agent/subagent/task/BackgroundTask.java:53–56getTaskId
public String getTaskId()
获取任务标识。
Returns the task id.
Stringio/agentscope/harness/agent/subagent/task/BackgroundTask.java:58–60getAgentId
public String getAgentId()
获取智能体标识。
Returns the agent id.
Stringio/agentscope/harness/agent/subagent/task/BackgroundTask.java:62–64getCreatedAt
public Instant getCreatedAt()
获取已创建 At。
Returns the created at.
Instantio/agentscope/harness/agent/subagent/task/BackgroundTask.java:66–68getLastCheckedAt
public Instant getLastCheckedAt()
获取 getLastCheckedAt。
Returns the last checked at.
Instantio/agentscope/harness/agent/subagent/task/BackgroundTask.java:70–72updateLastCheckedAt
public void updateLastCheckedAt()
更新 updateLastCheckedAt。
Updates the last checked at.
io/agentscope/harness/agent/subagent/task/BackgroundTask.java:74–76isCompleted
public boolean isCompleted()
判断是否已完成。
Returns whether the completed.
booleanio/agentscope/harness/agent/subagent/task/BackgroundTask.java:78–80getTaskStatus
public TaskStatus getTaskStatus()
获取任务状态。
Returns the structured TaskStatus for this task, reflecting the underlying future state and any explicit cancellation.
TaskStatusio/agentscope/harness/agent/subagent/task/BackgroundTask.java:86–97getStatus
public String getStatus()
获取状态。
Returns a human-readable status string.
Stringio/agentscope/harness/agent/subagent/task/BackgroundTask.java:100–107getResult
public String getResult()
获取结果。
Non-blocking result peek; returns null if not yet completed or if an error occurred.
Stringio/agentscope/harness/agent/subagent/task/BackgroundTask.java:110–116getError
public Exception getError()
获取错误。
Returns the error.
Exceptionio/agentscope/harness/agent/subagent/task/BackgroundTask.java:118–127waitForCompletion
public boolean waitForCompletion(long timeoutMs) throws InterruptedException
等待用于补全。
Blocks until the task completes or the timeout elapses.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
timeoutMs | long | 超时 Ms(类型 `long`)。The timeout ms. |
booleanInterruptedException可能抛出 `InterruptedException`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/subagent/task/BackgroundTask.java:134–148cancel
public boolean cancel(boolean mayInterruptIfRunning)
执行取消相关操作。
Cancels the task.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mayInterruptIfRunning | boolean | 可能中断 If 运行中(类型 `boolean`)。The may interrupt if running. |
booleanio/agentscope/harness/agent/subagent/task/BackgroundTask.java:154–157