agentscope.app.storageStorageBase
class StorageBase(ABC)
agentscope/app/storage/_base.py:32–1447存储抽象基类类。位于 agentscope.app.storage 包,继承自 ABC。核心成员包括 aclose、upsert_credential、list_credentials、get_credential、delete_credential、upsert_mcp。
The storage abstract base class. Declared in agentscope.app.storage. Extends ABC.
方法摘要Method Summary
方法详情Method Detail
__aenter__
async def __aenter__() -> Self
进入异步上下文并返回自身。
Start the storage backend (open connection pool, etc.).
agentscope/app/storage/_base.py:35–37__aexit__
async def __aexit__(exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: Any) -> None
退出异步上下文并清理资源。
Shut down the storage backend.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
exc_type | type[BaseException] | None | exc 类型(类型 `type[BaseException] | None`)。The exc type. |
exc_value | BaseException | None | exc 值(类型 `BaseException | None`)。The exc value. |
traceback | Any | traceback(类型 `Any`)。The traceback. |
agentscope/app/storage/_base.py:39–46aclose
async def aclose() -> None
返回 aclose 结果。
Release underlying connection resources.
agentscope/app/storage/_base.py:48–49upsert_credential
async def upsert_credential(user_id: str, credential_data: CredentialBase) -> str
创建或更新凭证 in 存储。
Create or update a credential in the storage.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
credential_data | CredentialBase | 凭证数据。The credential data. |
agentscope/app/storage/_base.py:52–68list_credentials
async def list_credentials(user_id: str) -> list[CredentialRecord]
列出凭证。
List all credentials for a given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agentscope/app/storage/_base.py:71–81get_credential
async def get_credential(user_id: str, credential_id: str) -> CredentialRecord | None
获取凭证。
Fetch a single credential record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
credential_id | str | 凭证标识。The credential id. |
agentscope/app/storage/_base.py:84–97delete_credential
async def delete_credential(user_id: str, credential_id: str) -> bool
删除凭证。
Delete a credential.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
credential_id | str | 凭证标识。The credential id. |
agentscope/app/storage/_base.py:100–116upsert_mcp
async def upsert_mcp(user_id: str, mcp_record: MCPRecord) -> str
创建或更新 installed-MCP。
The record's client.name is unique per user — the workspace relation is derived by joining on it — so writing a name another record already holds is an error rather than an overwrite.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
mcp_record | MCPRecord | MCP 记录(类型 `MCPRecord`)。The record to write. Its `id` decides create vs update. |
- 可能抛出 `Exception`。`ValueError`:
- 可能抛出 `Exception`。When another record of this user already uses the name.
agentscope/app/storage/_base.py:119–139list_mcps
async def list_mcps(user_id: str) -> list[MCPRecord]
列出 list_mcps。
List every MCP a user has installed, enabled or not.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agentscope/app/storage/_base.py:142–152get_mcp
async def get_mcp(user_id: str, mcp_id: str) -> MCPRecord | None
获取MCP。
Fetch a single installed-MCP record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
mcp_id | str | MCP 标识(类型 `str`)。The record id. |
agentscope/app/storage/_base.py:155–167get_mcp_by_name
async def get_mcp_by_name(user_id: str, name: str) -> MCPRecord | None
获取MCP 通过名称。
This is the lookup the workspace relation is derived through — a workspace holds names, not record ids.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
name | str | 名称(类型 `str`)。The MCP name, i.e. `record.client.name`. |
agentscope/app/storage/_base.py:170–189delete_mcp
async def delete_mcp(user_id: str, mcp_id: str) -> bool
删除 installed-MCP。
Delete an installed-MCP record.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
mcp_id | str | MCP 标识(类型 `str`)。The record id. |
agentscope/app/storage/_base.py:192–204upsert_skill
async def upsert_skill(user_id: str, skill_record: SkillRecord) -> str
执行 upsert_skill 方法。
The record's name is unique per user, for the same reason MCP names are: a workspace refers to a skill by name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
skill_record | SkillRecord | 技能记录(类型 `SkillRecord`)。The record to write. Its `id` decides create vs update. |
- 可能抛出 `Exception`。`ValueError`:
- 可能抛出 `Exception`。When another record of this user already uses the name.
agentscope/app/storage/_base.py:207–230list_skills
async def list_skills(user_id: str) -> list[SkillRecord]
列出技能。
Named apart from the workspace's list_skills because these are library records, not skills present in any workspace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agentscope/app/storage/_base.py:233–246get_skill
async def get_skill(user_id: str, skill_id: str) -> SkillRecord | None
获取技能。
Fetch a single installed-skill record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
skill_id | str | 技能标识(类型 `str`)。The record id. |
agentscope/app/storage/_base.py:249–265get_skill_by_name
async def get_skill_by_name(user_id: str, name: str) -> SkillRecord | None
获取技能通过名称。
Fetch an installed-skill record by its skill name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
name | str | 技能名称。The skill name. |
agentscope/app/storage/_base.py:268–284delete_skill
async def delete_skill(user_id: str, skill_id: str) -> bool
删除技能。
Delete an installed-skill record.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
skill_id | str | 技能标识(类型 `str`)。The record id. |
agentscope/app/storage/_base.py:287–303upsert_agent
async def upsert_agent(user_id: str, agent_record: AgentRecord) -> str
执行 upsert_agent 方法。
Create an agent record in the storage.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agent_record | AgentRecord | 智能体类。The agent record. |
agentscope/app/storage/_base.py:306–322list_agents
async def list_agents(user_id: str) -> list[AgentRecord]
列出智能体。
List all agents for a given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agentscope/app/storage/_base.py:325–335get_agent
async def get_agent(user_id: str, agent_id: str) -> AgentRecord | None
获取智能体。
Fetch a single agent record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agent_id | str | 智能体标识。The agent id. |
agentscope/app/storage/_base.py:338–351delete_agent
async def delete_agent(user_id: str, agent_id: str) -> bool
删除智能体。
Delete an agent record.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agent_id | str | 智能体标识。The agent id. |
agentscope/app/storage/_base.py:354–366upsert_session
async def upsert_session(user_id: str, agent_id: str, config: SessionConfig, state: AgentState | None = None, session_id: str | None = None, origin: SessionOrigin | None = None, source: str | None = None, source_schedule_id: str | None = None, source_chat_id: str | None = None, source_chat_name: str | None = None, source_channel_id: str | None = None) -> SessionRecord
执行 upsert_session 方法。
Create or update a session for a (user, agent) pair.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agent_id | str | 智能体标识。The agent id. |
config | SessionConfig | 配置对象(类型 `SessionConfig`)。Immutable session configuration (model, workspace). Required on create; passed unchanged on state-only updates. |
state默认default None | AgentState | None | 状态对象(类型 `AgentState | None`)。Runtime state to persist. Defaults to a fresh `AgentState()` when `None`. |
session_id默认default None | str | None | 会话标识(类型 `str | None`)。If provided, update the existing session with this id. If `None`, create a new session. |
origin默认default None | SessionOrigin | None | 来源(类型 `SessionOrigin | None`)。How the session came to exist — a :class:`ScheduleOrigin` also indexes it under its schedule. Defaults to :class:`UserOrigin`. source / source_schedule_id / source_chat_id / source_chat_name / source_channel_id: **Deprecated** — the flat shape `origin` replaced. Passing any of them still builds the matching origin, so callers written against the old signature keep working. |
source默认default None | str | None | 来源(类型 `str | None`)。The source. |
source_schedule_id默认default None | str | None | 来源调度标识(类型 `str | None`)。The source schedule id. |
source_chat_id默认default None | str | None | 来源聊天标识(类型 `str | None`)。The source chat id. |
source_chat_name默认default None | str | None | 来源聊天名称(类型 `str | None`)。The source chat name. |
source_channel_id默认default None | str | None | 来源渠道标识(类型 `str | None`)。The source channel id. |
agentscope/app/storage/_base.py:369–407set_session_team_id
async def set_session_team_id(user_id: str, session_id: str, team_id: str | None) -> None
设置会话团队标识。
Bypasses :meth:upsert_session because that method does not write team_id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
session_id | str | 会话标识(类型 `str`)。The session whose `team_id` should be updated. |
team_id | str | None | 团队标识(类型 `str | None`)。The new value. `None` detaches the session from any team. |
agentscope/app/storage/_base.py:410–432update_session_state
async def update_session_state(user_id: str, agent_id: str, session_id: str, state: AgentState) -> None
更新会话状态。
Convenience method for the hot path (post-chat-turn persistence).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agent_id | str | 智能体标识。The agent id. |
session_id | str | 会话标识。The session id. |
state | AgentState | 状态对象(类型 `AgentState`)。The new agent state to persist. |
agentscope/app/storage/_base.py:435–452list_sessions
async def list_sessions(user_id: str, agent_id: str) -> list[SessionRecord]
列出会话。
List all sessions for a given user and agent entity.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agent_id | str | 智能体标识。The agent id. |
agentscope/app/storage/_base.py:455–468delete_session
async def delete_session(user_id: str, agent_id: str, session_id: str) -> bool
删除会话。
Delete a session.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agent_id | str | 智能体标识。The agent id. |
session_id | str | 会话标识。The session id. |
agentscope/app/storage/_base.py:471–486get_session
async def get_session(user_id: str, agent_id: str, session_id: str) -> SessionRecord | None
获取会话。
Fetch a single session record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agent_id | str | 智能体标识。The agent id. |
session_id | str | 会话标识。The session id. |
agentscope/app/storage/_base.py:489–504list_sessions_by_schedule
async def list_sessions_by_schedule(user_id: str, schedule_id: str) -> list[SessionRecord]
列出会话通过调度。
Return all sessions created by a given schedule.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
schedule_id | str | 调度标识。The schedule id. |
agentscope/app/storage/_base.py:507–521list_sessions_by_channel
async def list_sessions_by_channel(user_id: str, channel_id: str) -> list[SessionRecord]
列出会话通过渠道。
Return all sessions derived from a given channel.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
channel_id | str | 渠道标识。The channel id. |
agentscope/app/storage/_base.py:524–538upsert_schedule
async def upsert_schedule(user_id: str, record: ScheduleRecord) -> str
执行 upsert_schedule 方法。
Persist a cron task record and register it in the user's index.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
record | ScheduleRecord | 记录(类型 `ScheduleRecord`)。The fully-populated record to store. |
agentscope/app/storage/_base.py:541–554get_schedule
async def get_schedule(user_id: str, schedule_id: str) -> ScheduleRecord | None
获取调度。
Fetch a single cron task record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
schedule_id | str | 任务标识。The task id. |
agentscope/app/storage/_base.py:557–570list_schedules
async def list_schedules(user_id: str) -> list[ScheduleRecord]
列出调度。
Return all cron task records belonging to the given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agentscope/app/storage/_base.py:573–584delete_schedule
async def delete_schedule(user_id: str, schedule_id: str) -> bool
删除调度。
Delete a cron task record and remove it from the user's index.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
schedule_id | str | 任务到删除的标识。The id of the task to delete. |
agentscope/app/storage/_base.py:587–600list_all_schedules
async def list_all_schedules() -> list[ScheduleRecord]
列出所有调度。
Used on startup to restore the in-memory scheduler from persisted state.
agentscope/app/storage/_base.py:603–611upsert_channel
async def upsert_channel(record: ChannelRecord, platform_bot_id: str) -> str
执行 upsert_channel 方法。
record.id is a globally unique UUID, so the record lives at a single global key; record.user_id drives the per-user index and platform_bot_id (extracted from credentials by the caller) drives the uniqueness index.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
record | ChannelRecord | 记录(类型 `ChannelRecord`)。The channel record to store. |
platform_bot_id | str | 平台 bot 标识(类型 `str`)。The platform-side bot identifier, used to maintain the dedup index. |
agentscope/app/storage/_base.py:623–645get_channel
async def get_channel(channel_id: str) -> ChannelRecord | None
获取渠道。
This is the primary lookup, used both by the management API and by the channel runtime (which only has a channel_id in hand).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
channel_id | str | 渠道标识。The channel id. |
agentscope/app/storage/_base.py:647–664list_channels
async def list_channels(user_id: str) -> list[ChannelRecord]
列出渠道。
Return all channel records owned by the given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agentscope/app/storage/_base.py:666–680list_all_channels
async def list_all_channels() -> list[ChannelRecord]
列出所有渠道。
Used on startup / reconcile to restore channel instances.
agentscope/app/storage/_base.py:682–692delete_channel
async def delete_channel(channel_id: str, platform_bot_id: str) -> bool
删除渠道。
Delete a channel record and remove it from all indexes.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
channel_id | str | 渠道到删除的标识。The id of the channel to delete. |
platform_bot_id | str | 平台 bot 标识(类型 `str`)。The bot identifier (re-extracted from credentials by the caller) so the dedup index entry can be removed. |
agentscope/app/storage/_base.py:694–712get_channel_id_by_platform_bot_id
async def get_channel_id_by_platform_bot_id(platform_bot_id: str) -> str | None
获取 get_channel_id_by_platform_bot_id。
Used for uniqueness validation — no two channels may share the same platform_bot_id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
platform_bot_id | str | 平台 bot 标识(类型 `str`)。The platform-side bot identifier. |
agentscope/app/storage/_base.py:714–731upsert_message
async def upsert_message(user_id: str, session_id: str, msg: Msg) -> None
执行 upsert_message 方法。
If a message in the list has the same id as msg, it is replaced in place.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
session_id | str | 会话标识。The session id. |
msg | Msg | 消息(类型 `Msg`)。The message to persist. |
agentscope/app/storage/_base.py:738–753delete_message
async def delete_message(user_id: str, session_id: str, message_id: str) -> bool
删除消息。
Delete one message from a session.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
session_id | str | 会话标识。The session id. |
message_id | str | 消息标识到删除。The message id to delete. |
agentscope/app/storage/_base.py:756–771get_message
async def get_message(user_id: str, session_id: str, message_id: str) -> Msg | None
获取消息。
Fetch a single message by id from the session's message list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
session_id | str | 会话标识。The session id. |
message_id | str | 消息标识(类型 `str`)。The message id to look up. |
agentscope/app/storage/_base.py:774–789list_messages
async def list_messages(user_id: str, session_id: str, limit: int = 50, before: str | None = None, **kwargs: Any) -> tuple[list[Msg], bool]
列出消息。
Return the most recent messages for a session with cursor-based pagination.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
session_id | str | 会话标识。The session id. |
limit默认default 50 | int | 数量上限(类型 `int`)。Maximum number of messages to return. Defaults to 50. |
before默认default None | str | None | 之前(类型 `str | None`)。A message ID used as the cursor. When provided, returns messages created before this message. Omit to get the latest page. |
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
agentscope/app/storage/_base.py:792–819upsert_sop
async def upsert_sop(user_id: str, record: SOPRecord) -> SOPRecord
执行 upsert_sop 方法。
Create or overwrite a procedure.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
record | SOPRecord | 记录(类型 `SOPRecord`)。The procedure to store. |
agentscope/app/storage/_base.py:825–838get_sop
async def get_sop(user_id: str, sop_id: str) -> SOPRecord | None
获取SOP。
Fetch one procedure; owner-scoped.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_id | str | SOP 标识(类型 `str`)。The procedure id. |
agentscope/app/storage/_base.py:840–853list_sops
async def list_sops(user_id: str) -> list[SOPRecord]
列出 list_sops。
List the user's procedures.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agentscope/app/storage/_base.py:855–866delete_sop
async def delete_sop(user_id: str, sop_id: str) -> bool
删除SOP。
Delete a procedure, every run of it, and their conversations.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_id | str | SOP 标识(类型 `str`)。The procedure id. |
agentscope/app/storage/_base.py:868–881upsert_sop_run
async def upsert_sop_run(user_id: str, record: SOPRunRecord) -> SOPRunRecord
执行 upsert_sop_run 方法。
Create or overwrite a run.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
record | SOPRunRecord | 运行到存储。The run to store. |
agentscope/app/storage/_base.py:883–900get_sop_run
async def get_sop_run(user_id: str, sop_run_id: str) -> SOPRunRecord | None
获取SOP 运行。
Fetch one run; owner-scoped.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_run_id | str | 运行标识。The run id. |
agentscope/app/storage/_base.py:902–919list_sop_runs
async def list_sop_runs(user_id: str, sop_id: str | None = None, phase: SOPPhase | None = None) -> list[SOPRunRecord]
列出 list_sop_runs。
List the user's runs, newest first.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_id默认default None | str | None | SOP 标识(类型 `str | None`)。Only runs of this procedure. `None` means all of them. |
phase默认default None | SOPPhase | None | 阶段(类型 `SOPPhase | None`)。Only runs in this phase. `None` means all of them. |
agentscope/app/storage/_base.py:921–941update_sop_run
async def update_sop_run(user_id: str, sop_run_id: str, state: SOPRunState, sessions: dict[str, str] | None = None) -> None
更新SOP 运行。
Update a run's state (and sessions), leaving its definition.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_run_id | str | 运行标识。The run id. |
state | SOPRunState | 状态对象(类型 `SOPRunState`)。How the run is going now. |
sessions默认default None | dict[str, str] | None | 会话(类型 `dict[str, str] | None`)。The run's conversations. `None` leaves them as is. |
- 可能抛出 `Exception`。`KeyError`:
- 可能抛出 `Exception`。If the user has no such run.
agentscope/app/storage/_base.py:943–966delete_sop_run
async def delete_sop_run(user_id: str, sop_run_id: str) -> bool
删除SOP 运行。
Delete one run and the conversations it opened.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
sop_run_id | str | 运行标识。The run id. |
agentscope/app/storage/_base.py:968–981upsert_team
async def upsert_team(user_id: str, record: TeamRecord) -> TeamRecord
创建或更新团队。
Create or update a team record.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
record | TeamRecord | 记录(类型 `TeamRecord`)。The team record to persist. The record's `id` is used as the primary key; if a record with the same id already exists it is overwritten. |
agentscope/app/storage/_base.py:988–1003get_team
async def get_team(user_id: str, team_id: str) -> TeamRecord | None
获取团队。
Fetch a single team record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
team_id | str | 团队标识。The team id. |
agentscope/app/storage/_base.py:1006–1019list_teams
async def list_teams(user_id: str) -> list[TeamRecord]
列出 list_teams。
List all teams owned by a given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识。The user id. |
agentscope/app/storage/_base.py:1022–1030delete_team
async def delete_team(user_id: str, team_id: str) -> bool
删除团队。
The cascade is role-aware — the two team-membership modes (created vs invited, see :class:TeamMember) must be handled differently: 1.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
team_id | str | 团队到删除的标识。The id of the team to delete. |
agentscope/app/storage/_base.py:1033–1070upsert_knowledge_base
async def upsert_knowledge_base(user_id: str, record: KnowledgeBaseRecord) -> KnowledgeBaseRecord
创建或更新知识库基类。
The caller is responsible for constructing the full :class:KnowledgeBaseRecord (including id and collection_name).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识(类型 `str`)。The owner user id. Must match `record.user_id`. |
record | KnowledgeBaseRecord | 记录(类型 `KnowledgeBaseRecord`)。The fully-populated record to persist. |
agentscope/app/storage/_base.py:1077–1098get_knowledge_base
async def get_knowledge_base(user_id: str, knowledge_base_id: str) -> KnowledgeBaseRecord | None
获取知识库。
Fetch a single knowledge base record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 知识库基类标识。The knowledge base id. |
agentscope/app/storage/_base.py:1101–1118list_knowledge_bases
async def list_knowledge_bases(user_id: str) -> list[KnowledgeBaseRecord]
列出 list_knowledge_bases。
List all knowledge base records owned by the given user.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
agentscope/app/storage/_base.py:1121–1134delete_knowledge_base
async def delete_knowledge_base(user_id: str, knowledge_base_id: str) -> bool
删除知识库。
Note: this only removes the metadata record; deletion of the underlying vector store collection is the caller's responsibility (typically the knowledge base manager).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 知识库标识(类型 `str`)。The id of the record to delete. |
agentscope/app/storage/_base.py:1137–1158upsert_knowledge_document
async def upsert_knowledge_document(user_id: str, record: KnowledgeDocumentRecord) -> KnowledgeDocumentRecord
创建或更新知识库文档。
Used by the upload endpoint to register a freshly arrived document (status='pending') and by other code paths that need to overwrite the full record.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 用户标识(类型 `str`)。The owner user id. Must match `record.user_id`. |
record | KnowledgeDocumentRecord | 记录(类型 `KnowledgeDocumentRecord`)。The fully-populated record to persist. |
agentscope/app/storage/_base.py:1165–1187get_knowledge_document
async def get_knowledge_document(user_id: str, knowledge_base_id: str, document_id: str) -> KnowledgeDocumentRecord | None
获取知识库文档。
Fetch a single knowledge document record by id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识。The document id. |
agentscope/app/storage/_base.py:1190–1209list_knowledge_documents
async def list_knowledge_documents(user_id: str, knowledge_base_id: str) -> list[KnowledgeDocumentRecord]
列出知识库文档。
List all documents in a knowledge base.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
agentscope/app/storage/_base.py:1212–1229delete_knowledge_document
async def delete_knowledge_document(user_id: str, knowledge_base_id: str, document_id: str) -> bool
删除知识库文档。
Only removes the metadata record; cleanup of the underlying blob and vector store records is the caller's responsibility.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识(类型 `str`)。The id of the record to delete. |
agentscope/app/storage/_base.py:1232–1255update_knowledge_document_status
async def update_knowledge_document_status(user_id: str, knowledge_base_id: str, document_id: str, status: KnowledgeDocumentStatus, error: str | None = None, chunk_count: int | None = None) -> None
更新知识库文档状态。
Used by the indexing worker as it walks the lifecycle transitions (parsing → chunking → indexing → ready / error).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识(类型 `str`)。The document being updated. |
status | KnowledgeDocumentStatus | 新的生命周期状态。The new lifecycle state. |
error默认default None | str | None | 错误(类型 `str | None`)。Failure reason, set when `status == 'error'`. Ignored otherwise. `None` leaves the existing value unchanged. |
chunk_count默认default None | int | None | 分块数量(类型 `int | None`)。The final chunk count, set when `status == 'ready'`. `None` leaves the existing value unchanged. |
agentscope/app/storage/_base.py:1258–1291acquire_knowledge_document_lease
async def acquire_knowledge_document_lease(user_id: str, knowledge_base_id: str, document_id: str, processing_node: str, lease_ttl: timedelta, now: datetime | None = None) -> bool
执行 acquire_knowledge_document_lease 方法。
Succeeds only if no other worker currently holds a live lease — i.e.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识(类型 `str`)。The document whose lease is being acquired. |
processing_node | str | processing 节点(类型 `str`)。Stable identifier of the calling worker (e.g. `hostname:pid:uuid`). Persisted as `processing_node` on success. |
lease_ttl | timedelta | lease 存活时间(类型 `timedelta`)。How long the lease should live from `now`. |
now默认default None | datetime | None | 当前时间(类型 `datetime | None`)。Reference time for the comparison. Defaults to `datetime.now()`. Injectable for testing. |
agentscope/app/storage/_base.py:1294–1334renew_knowledge_document_lease
async def renew_knowledge_document_lease(user_id: str, knowledge_base_id: str, document_id: str, processing_node: str, lease_ttl: timedelta, now: datetime | None = None) -> bool
执行 renew_knowledge_document_lease 方法。
Required for long-running parses that exceed lease_ttl — the worker calls this periodically so the sweeper does not mistake it for a crash.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识(类型 `str`)。The document being renewed. |
processing_node | str | processing 节点(类型 `str`)。The caller's processing node id; must match the record's current `processing_node` for renewal to succeed. |
lease_ttl | timedelta | lease 存活时间(类型 `timedelta`)。The new lease duration relative to `now`. |
now默认default None | datetime | None | 当前时间(类型 `datetime | None`)。Reference time. Defaults to `datetime.now()`. |
agentscope/app/storage/_base.py:1337–1375release_knowledge_document_lease
async def release_knowledge_document_lease(user_id: str, knowledge_base_id: str, document_id: str, processing_node: str) -> None
执行 release_knowledge_document_lease 方法。
Clears processing_node and data.lease_expires_at only if the current holder matches processing_node — a stolen lease (e.g.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
user_id | str | 所有者用户标识。The owner user id. |
knowledge_base_id | str | 父级知识库基类标识。The parent knowledge base id. |
document_id | str | 文档标识(类型 `str`)。The document whose lease is being released. |
processing_node | str | processing 节点(类型 `str`)。The caller's processing node id; must match the record's current holder. |
agentscope/app/storage/_base.py:1378–1402list_knowledge_documents_with_expired_lease
async def list_knowledge_documents_with_expired_lease(now: datetime | None = None) -> list[KnowledgeDocumentRecord]
列出 list_knowledge_documents_with_expired_lease。
Scans every user / knowledge base — used by the sweeper, not by user-facing endpoints.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
now默认default None | datetime | None | 当前时间(类型 `datetime | None`)。Reference time. Defaults to `datetime.now()`. |
agentscope/app/storage/_base.py:1405–1425list_knowledge_documents_pending_since
async def list_knowledge_documents_pending_since(threshold: datetime) -> list[KnowledgeDocumentRecord]
列出 list_knowledge_documents_pending_since。
Catches the corner case where the upload endpoint persisted a record but the dispatcher (or the process holding it) died before any worker picked the document up — a crashed lease sweep would miss it because no lease was ever written.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
threshold | datetime | 阈值(类型 `datetime`)。Cut-off creation time; only `pending` records `created_at < threshold` are returned. |
agentscope/app/storage/_base.py:1428–1447