AS AgentScopeAPI 手册 / API Reference
类class agentscope.state

ToolContext

class ToolContext(BaseModel)
源码Source agentscope/state/_state.py:32–172

工具上下文,在多次调用之间保存和恢复会话与记忆数据。位于 agentscope.state 包。核心成员包括 get_cache、cache_file、clean_file_cache。

The tool context, e.g. Declared in agentscope.state.

属性摘要Attribute Summary

属性Attribute类型Type说明Description值Value
max_cache_filesint
最大缓存文件字段。
Field(default=100, gt=1)
max_cache_bytesfloat
最大缓存字节字段。
Field(default=25000, gt=10000)
read_file_cachelist[ReadCacheEntry]
读取文件缓存字段。
Field(default_factory=list)
activated_groupslist[str]
`activated_groups` 字段。
Field(default_factory=list)

方法摘要Method Summary

方法Method说明Description
async def get_cache(file_path: str, mtime: float | None = None) -> ReadCacheEntry | Noneasync
获取缓存。
Get cached file content if still valid.
async def cache_file(file_path: str, lines: list[str], mtime: float | None = None) -> Noneasync
执行缓存文件相关操作。
Cache file content with LRU eviction.
async def clean_file_cache(reserved_file_paths: set[str] | None = None) -> Noneasync
设置干净文件缓存。
Drop read caches whose paths are not in ``reserved_file_paths``.

属性详情Attribute Detail

max_cache_files

int = Field(default=100, gt=1)

最大缓存文件字段。声明为 int。

The max cache files field.

源码Source agentscope/state/_state.py:35

max_cache_bytes

float = Field(default=25000, gt=10000)

最大缓存字节字段。声明为 float。

The max cache bytes field.

源码Source agentscope/state/_state.py:37

read_file_cache

list[ReadCacheEntry] = Field(default_factory=list)

读取文件缓存字段。声明为 list[ReadCacheEntry]。

The read file cache field.

源码Source agentscope/state/_state.py:39

activated_groups

list[str] = Field(default_factory=list)

activated_groups 字段。声明为 list[str]。

The activated groups field.

源码Source agentscope/state/_state.py:42

方法详情Method Detail

get_cache

async def get_cache(file_path: str, mtime: float | None = None) -> ReadCacheEntry | None
async

获取缓存。

Get cached file content if still valid.

参数Parameters
名称Name类型Type说明Description
file_pathstr文件路径(类型 `str`)。The absolute path of the file.
mtime默认default Nonefloat | Nonemtime(类型 `float | None`)。The file's modification time, obtained from the same filesystem that read the file, e.g. a workspace backend. Falls back to the host filesystem when not provided.
返回Returns ReadCacheEntry | None
返回ReadCacheEntry|None。`ReadCacheEntry | None`:
源码Source agentscope/state/_state.py:46–88

cache_file

async def cache_file(file_path: str, lines: list[str], mtime: float | None = None) -> None
async

执行缓存文件相关操作。

Cache file content with LRU eviction.

参数Parameters
名称Name类型Type说明Description
file_pathstr文件路径(类型 `str`)。The absolute path of the file.
lineslist[str]lines(类型 `list[str]`)。The lines of the file content.
mtime默认default Nonefloat | Nonemtime(类型 `float | None`)。The file's modification time, obtained from the same filesystem that read the file, e.g. a workspace backend. Falls back to the host filesystem when not provided.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/state/_state.py:90–153

clean_file_cache

async def clean_file_cache(reserved_file_paths: set[str] | None = None) -> None
async

设置干净文件缓存。

Drop read caches whose paths are not in reserved_file_paths.

参数Parameters
名称Name类型Type说明Description
reserved_file_paths默认default Noneset[str] | Nonereserved 文件 paths(类型 `set[str] | None`)。File paths from Read calls that remain in the context. Caches for these files are kept; all others are evicted.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/state/_state.py:155–172
基于官方固定版本源码静态生成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.