AS AgentScopeAPI 手册 / API Reference

BackgroundTask

public class BackgroundTask
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:35–158

Background 任务,支持智能体之间的发现、委派与协作。位于 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.

参数Parameters
名称Name类型Type说明Description
taskIdString任务标识(类型 `String`)。The task id.
agentIdString智能体标识(类型 `String`)。The agent id.
futureCompletableFuture<String>异步(类型 `CompletableFuture`)。The future.
源码Source 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.

参数Parameters
名称Name类型Type说明Description
actionjava.util.function.BiConsumer<String, Throwable>动作(类型 `java.util.function.BiConsumer`)。The action.
返回Returns BackgroundTask
返回BackgroundTask。Returns background task.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:53–56

getTaskId

public String getTaskId()

获取任务标识。

Returns the task id.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:58–60

getAgentId

public String getAgentId()

获取智能体标识。

Returns the agent id.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:62–64

getCreatedAt

public Instant getCreatedAt()

获取已创建 At。

Returns the created at.

返回Returns Instant
返回Instant。Returns instant.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:66–68

getLastCheckedAt

public Instant getLastCheckedAt()

获取 getLastCheckedAt。

Returns the last checked at.

返回Returns Instant
返回Instant。Returns instant.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:70–72

updateLastCheckedAt

public void updateLastCheckedAt()

更新 updateLastCheckedAt。

Updates the last checked at.

源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:74–76

isCompleted

public boolean isCompleted()

判断是否已完成。

Returns whether the completed.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:78–80

getTaskStatus

public TaskStatus getTaskStatus()

获取任务状态。

Returns the structured TaskStatus for this task, reflecting the underlying future state and any explicit cancellation.

返回Returns TaskStatus
返回TaskStatus。Returns task status.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:86–97

getStatus

public String getStatus()

获取状态。

Returns a human-readable status string.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:100–107

getResult

public String getResult()

获取结果。

Non-blocking result peek; returns null if not yet completed or if an error occurred.

返回Returns String
返回字符串结果。Returns a string.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:110–116

getError

public Exception getError()

获取错误。

Returns the error.

返回Returns Exception
返回异常。Returns exception.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:118–127

waitForCompletion

public boolean waitForCompletion(long timeoutMs) throws InterruptedException

等待用于补全。

Blocks until the task completes or the timeout elapses.

参数Parameters
名称Name类型Type说明Description
timeoutMslong超时 Ms(类型 `long`)。The timeout ms.
返回Returns boolean
返回布尔值,表示判断结果。true if completed within timeout, false if timed out
抛出Throws
  • InterruptedException 可能抛出 `InterruptedException`。Thrown when the operation cannot be completed.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:134–148

cancel

public boolean cancel(boolean mayInterruptIfRunning)

执行取消相关操作。

Cancels the task.

参数Parameters
名称Name类型Type说明Description
mayInterruptIfRunningboolean可能中断 If 运行中(类型 `boolean`)。The may interrupt if running.
返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/harness/agent/subagent/task/BackgroundTask.java:154–157
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.