Mastra.getStoredAgentById()
.getStoredAgentById() 方法通过其 ID 从存储中检索代理配置,并创建一个可执行的 Agent 实例。存储的代理允许你将代理配置保存在数据库中,并在运行时动态加载它们。
🌐 The .getStoredAgentById() method retrieves an agent configuration from storage by its ID and creates an executable Agent instance. Stored agents allow you to persist agent configurations in a database and dynamically load them at runtime.
使用示例Direct link to 使用示例
🌐 Usage example
// Get an Agent instance from storage
const agent = await mastra.getStoredAgentById("my-stored-agent");
if (agent) {
const response = await agent.generate("Hello");
console.log(response.text);
}
// Get the raw storage data instead of an Agent instance
const storedConfig = await mastra.getStoredAgentById("my-stored-agent", { raw: true });
if (storedConfig) {
console.log(storedConfig.instructions);
console.log(storedConfig.createdAt);
}
参数Direct link to 参数
🌐 Parameters
id:
options?:
选项Direct link to 选项
🌐 Options
raw?:
返回Direct link to 返回
🌐 Returns
result:
原始分辨率Direct link to 原始分辨率
🌐 Primitive Resolution
从存储的配置创建 Agent 实例时,该方法会解析对已注册原语的引用:
🌐 When creating an Agent instance from stored configuration, the method resolves references to registered primitives:
- 工具:已从 Mastra 配置中注册的
tools解析 - 工作流程:已从 Mastra 配置中注册的
workflows解析 - 子代理:已从 Mastra 配置中注册的
agents解析 - 内存:已从 Mastra 配置中注册的
memory解析 - 评分器:从 Mastra 配置中注册的
scorers解析,包括采样配置
如果在注册表中找不到引用的原语,会记录一个警告,但代理仍然会被创建。
🌐 If a referenced primitive is not found in the registry, a warning is logged but the agent is still created.
StorageAgentTypeDirect link to StorageAgentType
使用 raw: true 时,返回的对象具有以下结构:
🌐 When using raw: true, the returned object has the following structure:
id:
name:
description?:
instructions:
model:
tools?:
workflows?:
agents?:
memory?:
scorers?:
defaultOptions?:
metadata?:
createdAt:
updatedAt:
相关Direct link to 相关
🌐 Related