Skip to main content

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:

string
The unique identifier of the stored agent to retrieve.

options?:

{ raw?: boolean }
Optional configuration object.

选项
Direct link to 选项

🌐 Options

raw?:

boolean
= false
When `true`, returns the raw `StorageAgentType` object from storage instead of creating an `Agent` instance. Useful for inspecting stored configuration or metadata.

返回
Direct link to 返回

🌐 Returns

result:

Agent | StorageAgentType | null
Returns an `Agent` instance by default, or `StorageAgentType` when `raw: true`. Returns `null` if no agent with the given ID exists.

原始分辨率
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.

StorageAgentType
Direct link to StorageAgentType

使用 raw: true 时,返回的对象具有以下结构:

🌐 When using raw: true, the returned object has the following structure:

id:

string
Unique identifier for the agent.

name:

string
Display name of the agent.

description?:

string
Optional description of the agent.

instructions:

string
System instructions for the agent.

model:

Record<string, unknown>
Model configuration with provider and name.

tools?:

Record<string, unknown>
Tool references to resolve from registry.

workflows?:

Record<string, unknown>
Workflow references to resolve from registry.

agents?:

Record<string, unknown>
Sub-agent references to resolve from registry.

memory?:

Record<string, unknown>
Memory reference to resolve from registry.

scorers?:

Record<string, unknown>
Scorer references with optional sampling config.

defaultOptions?:

Record<string, unknown>
Default options passed to agent execution.

metadata?:

Record<string, unknown>
Custom metadata stored with the agent.

createdAt:

Date
Timestamp when the agent was created.

updatedAt:

Date
Timestamp when the agent was last updated.

🌐 Related