@mastra/core
Version:
196 lines • 9.45 kB
TypeScript
import { MAX_FS_SUBAGENT_DEPTH } from '../../_types/@internal_core@0.1.4/dist/fs-routing/index.js';
import type { MastraScorer, MastraScorerEntry } from '../../evals/index.js';
import type { MastraMemory } from '../../memory/memory.js';
import type { InputProcessorOrWorkflow, OutputProcessorOrWorkflow } from '../../processors/index.js';
import type { AgentScheduleDefinition } from '../../schedules/define.js';
import type { InlineSkill } from '../../skills/types.js';
import type { DynamicArgument } from '../../types/index.js';
import type { AnyWorkspace } from '../../workspace/index.js';
import { Agent } from '../agent.js';
import type { AgentConfig, AgentInstructions, ToolsInput } from '../types.js';
/**
* Identity helper for a file-system routed agent config. Returns the provided
* partial config unchanged — its only purpose is to give authors editor types
* for `agents/<name>/config.ts` while letting `instructions`/`model`/`tools` be
* supplied by sibling files (`instructions.md`/`instructions.ts`, `tools/*.ts`).
*
* @example
* ```ts
* // src/mastra/agents/weather/config.ts
* import { agentConfig } from '@mastra/core/agent';
*
* export default agentConfig({
* model: 'openai/gpt-4o',
* // instructions omitted -> taken from instructions.md
* // tools omitted -> taken from tools/*.ts
* });
* ```
*/
export type FsAgentConfig = Partial<Omit<AgentConfig, 'id' | 'name'>> & {
id?: string;
name?: string;
};
export declare function agentConfig(config: FsAgentConfig): FsAgentConfig;
/**
* Identity helper for a file-system routed agent's `instructions.ts`. Returns
* the provided value unchanged — its only purpose is to give authors editor
* types for `agents/<name>/instructions.ts`, which is the code counterpart to
* `instructions.md`: use it when the prompt is computed, composed from shared
* constants, or resolved per request.
*
* @example
* ```ts
* // src/mastra/agents/support/instructions.ts
* import { agentInstructions } from '@mastra/core/agent';
*
* export default agentInstructions(({ requestContext }) => {
* const tier = requestContext.get('tier') ?? 'standard';
* return `You are a support agent. Treat this as a ${tier}-tier customer.`;
* });
* ```
*/
export declare function agentInstructions<TRequestContext extends Record<string, any> | unknown = unknown>(instructions: DynamicArgument<AgentInstructions, TRequestContext>): DynamicArgument<AgentInstructions, TRequestContext>;
export { defineSchedule } from '../../schedules/define.js';
export type { AgentScheduleDefinition, AgentScheduleHandler, DeclaredAgentSchedule } from '../../schedules/define.js';
/**
* A single tool discovered under `agents/<name>/tools/`. `key` defaults to the
* filename slug; `tool` is the default export of that module.
*/
export interface FsAgentToolEntry {
key: string;
tool: ToolsInput[string];
}
/**
* A single scorer discovered under `agents/<name>/scorers/`. `key` defaults to
* the filename slug. `scorer` is the module's default export, which may be a
* bare `MastraScorer` (it is wrapped into `{ scorer }`) or an explicit
* `{ scorer, sampling }` entry.
*/
export interface FsAgentScorerEntry {
key: string;
scorer: MastraScorer<any, any, any, any> | MastraScorerEntry;
}
/**
* A single schedule discovered under `agents/<name>/schedules/`. `key` is the
* file's path relative to `schedules/` with the extension stripped, so nested
* files keep a stable identity (`billing/sweep.ts` → `billing/sweep`).
* `schedule` is the default export of a `.ts` module or the parsed form of a
* `.md` file (cron frontmatter + body as the prompt).
*/
export interface FsAgentScheduleEntry {
key: string;
schedule: AgentScheduleDefinition;
}
export interface FsAgentEntry {
/** Agent directory name. Used as the default `id`/`name`. */
name: string;
/**
* Default export of `config.ts`, if present. Either an `agentConfig(...)`
* partial or a fully code-defined `Agent` instance (`new Agent({...})`).
*/
config?: FsAgentConfig | Agent;
/**
* Default export of `agents/<name>/instructions.ts`, if present. Either an
* `AgentInstructions` value or a function resolved per request. Unlike
* `instructionsMd` this is a live module value, so it can be computed. Set the
* key only when the file exists: assembly reads presence from the key, so an
* `undefined` value is a broken module rather than an absent one.
*/
instructions?: DynamicArgument<AgentInstructions>;
/** Raw contents of `instructions.md`, if present. */
instructionsMd?: string;
/** Tools discovered under `tools/`, already loaded. */
tools?: FsAgentToolEntry[];
/**
* Skills discovered under `skills/`, already loaded as inline skills
* (the codegen layer inlines each `SKILL.md` + references via `createSkill`).
*/
skills?: InlineSkill[];
/**
* Default export of `agents/<name>/workspace.ts`, if present. A `Workspace`
* instance that overrides the convention default.
*/
workspace?: AnyWorkspace;
/**
* Default export of `agents/<name>/memory.ts`, if present. A `MastraMemory`
* instance wired into the assembled agent as its `memory`. `config.memory`
* (from `config.ts`) takes precedence on conflict.
*/
memory?: MastraMemory;
/**
* Base path for the convention default workspace. When provided and neither
* `config.workspace` nor `workspace.ts` supplies one, an FS agent gets a
* default `Workspace` (a contained `LocalFilesystem` rooted here plus a
* `LocalSandbox`), giving file-based agents file/shell tools automatically.
* Callers (the deployer codegen layer) pass a per-agent directory here.
*/
defaultWorkspaceBasePath?: string;
/**
* Input processors discovered under `processors/input/`, already loaded.
* Merged with `config.inputProcessors`; config takes precedence on collision.
*/
inputProcessors?: InputProcessorOrWorkflow[];
/**
* Output processors discovered under `processors/output/`, already loaded.
* Merged with `config.outputProcessors`; config takes precedence on collision.
*/
outputProcessors?: OutputProcessorOrWorkflow[];
/**
* Scorers discovered under `agents/<name>/scorers/`, already loaded. Each
* entry's `scorer` is either a bare `MastraScorer` (the default export of the
* scorer module) or a `{ scorer, sampling }` entry. Merged into
* `config.scorers`; config takes precedence on key collision.
*/
scorers?: FsAgentScorerEntry[];
/**
* Schedules discovered under `agents/<name>/schedules/`, in stable
* (path-sorted) order. Attached to the assembled agent and synced into
* schedule storage by Mastra at boot. Only root agents may declare
* schedules — a schedule on a subagent is a build error, because subagents
* are wired into their parent's `agents` map rather than registered on the
* Mastra instance, so the scheduler could never resolve their `agentId`.
*/
schedules?: FsAgentScheduleEntry[];
/**
* Declared subagents discovered under `agents/<name>/subagents/<childId>/`.
* Each entry is assembled into its own `Agent` and wired into the parent's
* `agents` map under its directory name, becoming a model-visible delegation
* tool. Subagents may declare their own `subagents`, up to
* `MAX_FS_SUBAGENT_DEPTH` levels below the top-level agent; deeper entries
* are ignored with a warning.
*/
subagents?: FsAgentEntry[];
}
export { MAX_FS_SUBAGENT_DEPTH };
/**
* Assemble a single `Agent` from already-loaded file-system entries for one
* `agents/<name>/` directory. Performs no filesystem access — callers load the
* modules and pass them in, keeping this unit-testable and runtime-portable.
*
* Precedence rules:
* - `id`/`name` default to the directory name when omitted in config.
* - `instructions`: a dynamic (function) `config.instructions` wins over both
* instructions files; otherwise `instructions.ts` wins over `instructions.md`,
* which wins over a static `config.instructions`. Missing all of them is an
* error.
* - `model` is required (from config); missing is an error.
* - `tools`: discovered `tools/*.ts` are merged with `config.tools`; on key
* collision `config.tools` wins (a warning is surfaced via `onWarn`).
* - `skills`: discovered `skills/*` are merged with `config.skills`; on name
* collision `config.skills` wins (a warning is surfaced via `onWarn`). A
* dynamic (function) `config.skills` wins wholesale and discovered skills are
* ignored with a warning.
* - `memory`: `memory.ts`'s default export is used unless `config.memory` is
* set, in which case `config.memory` wins (a warning is surfaced via
* `onWarn`). Missing both leaves the agent without memory.
*
* If `config` is already an `Agent` instance (the author wrote
* `export default new Agent({...})` in `config.ts`), it is used as-is — no
* partial-config assembly is performed. This lets a folder under `agents/`
* hold either an `agentConfig(...)` partial or a fully code-defined
* `new Agent(...)` without the loader trying to re-wrap the latter.
*/
export declare function assembleAgentFromFsEntry(entry: FsAgentEntry, options?: {
onWarn?: (message: string) => void;
}): Agent;
//# sourceMappingURL=index.d.ts.map