Skip to main content

Agents

An agent gives a model one reusable role: a name, a system prompt, and an optional model route. Declaring an agent registers the role. It does not run it.

Declare and run an agent​

import { function agent(input: AgentInput): Agent
This describes an agent, it does not run one. There is no handler, and nothing executes when the mod activates. Put the result under `ai.agents` and the host registers it so the user or another agent can call it by id, `<extensionId>/<name>`. That is the line between this and the rest of the AI surface. To actually run something, use `ctx.ai.ask` for a single answer or `ctx.ai.sessions` for a conversation. To give the model a capability it can invoke, write a `tool`, which does have a handler.
@categoryAuthoring
agent
, function command<const TId extends string, TResult>(commandValue: CommandInput<TId, TResult>): Command<TId, TResult>
Declare an action the user runs from the command palette or a keybinding. Reach for this when a person triggers the behavior. The `title` is what they read in the palette, `category` groups the entry, and `when` decides whether it shows up at all. The handler takes no argument, only a context. Whatever it returns is the command's result: `ctx.commands.execute(thisCommand)` resolves with that type, and the build compiles the same type into the result contract the host enforces, so return JSON data or nothing. Two neighbours are easy to confuse. `slashCommand` is also user-triggered, but it is typed inside a chat session and receives free text plus the live AI session. `tool` is not user-triggered at all, the model decides to call it mid-turn. The result goes in the mod's top-level `commands` array, alongside any slash commands. The `id` is the address a keybinding or a `command:` hook targets, so keep it stable.
@categoryAuthoring
command
, function defineMod(definition: ModDefinition): Mod
The entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.
@example```ts export default defineMod({ metadata: { displayName: 'Review', description: 'Review helpers for the current change.', }, commands: [ command({ id: 'review.open', title: 'Review: Open', run: openPanel }), slashCommand({ id: 'review', description: 'Review this change', run: review }), ], ai: { tools: [reviewTool], }, }); ```@categoryAuthoring
defineMod
, function prompt(input: PromptInput): Prompt
Declare reusable prompt text the user can drop into the composer. Put the result in the mod's top-level `prompts` array. It is text and nothing more: no model, no handler, no execution. An agent is the next step up, it binds prompt text to a model under a callable name. The same `Prompt` value can serve both, handed to `agent({ prompt })` and listed in `prompts`, so the agent and the user work from one wording.
@example```ts const commitStyle = prompt({ name: 'commit-style', description: 'Insert the commit message rules for this repo.', content: ` # Commit message Start with a bracketed area, for example `[terminal]`. `, }); ```@categoryAuthoring
prompt
} from '@modular/sdk';
const const reviewInstructions: PromptreviewInstructions = function prompt(input: PromptInput): Prompt
Declare reusable prompt text the user can drop into the composer. Put the result in the mod's top-level `prompts` array. It is text and nothing more: no model, no handler, no execution. An agent is the next step up, it binds prompt text to a model under a callable name. The same `Prompt` value can serve both, handed to `agent({ prompt })` and listed in `prompts`, so the agent and the user work from one wording.
@example```ts const commitStyle = prompt({ name: 'commit-style', description: 'Insert the commit message rules for this repo.', content: ` # Commit message Start with a bracketed area, for example `[terminal]`. `, }); ```@categoryAuthoring
prompt
({
PromptInput.name: stringname: 'review-instructions', PromptInput.description: stringdescription: 'Insert the standard code-review instructions.', PromptInput.content: stringcontent: 'Review correctness, cancellation, and cleanup. Cite exact files.', }); const const reviewer: Agentreviewer = function agent(input: AgentInput): Agent
This describes an agent, it does not run one. There is no handler, and nothing executes when the mod activates. Put the result under `ai.agents` and the host registers it so the user or another agent can call it by id, `<extensionId>/<name>`. That is the line between this and the rest of the AI surface. To actually run something, use `ctx.ai.ask` for a single answer or `ctx.ai.sessions` for a conversation. To give the model a capability it can invoke, write a `tool`, which does have a handler.
@categoryAuthoring
agent
({
AgentInput.name: stringname: 'reviewer', AgentInput.description: stringdescription: 'Reviews a change and reports concrete defects.', AgentInput.prompt: string | Promptprompt: const reviewInstructions: PromptreviewInstructions, AgentInput.model: AgentModelmodel: { kind: "inherit"kind: 'inherit' }, }); const const reviewCurrentChange: Command<"research.review-current-change", void>reviewCurrentChange = function command<"research.review-current-change", void>(commandValue: CommandInput<"research.review-current-change", void>): Command<"research.review-current-change", void>
Declare an action the user runs from the command palette or a keybinding. Reach for this when a person triggers the behavior. The `title` is what they read in the palette, `category` groups the entry, and `when` decides whether it shows up at all. The handler takes no argument, only a context. Whatever it returns is the command's result: `ctx.commands.execute(thisCommand)` resolves with that type, and the build compiles the same type into the result contract the host enforces, so return JSON data or nothing. Two neighbours are easy to confuse. `slashCommand` is also user-triggered, but it is typed inside a chat session and receives free text plus the live AI session. `tool` is not user-triggered at all, the model decides to call it mid-turn. The result goes in the mod's top-level `commands` array, alongside any slash commands. The `id` is the address a keybinding or a `command:` hook targets, so keep it stable.
@categoryAuthoring
command
({
id: "research.review-current-change"id: 'research.review-current-change', title: stringtitle: 'Review Current Change', run: (call: CommandCall) => Awaitable<void>run: async ({ ctx: CommandContextctx }) => { const const handle: AiAgentHandlehandle = await ctx: CommandContextctx.ContextBase.ai: AiRuntimeApi
One off answers, agents, models, and sessions.
ai
.AiRuntimeApi.agents: AiAgentsApiagents.AiAgentsApi.get(agent: AgentLookup): Promise<AiAgentHandle>
Look up one of this mod's own `agent(...)` values, or another mod's agent as `agent:<modId>/<name>`; rejects if it does not exist.
get
(const reviewer: Agentreviewer);
const const result: AiRunResultresult = await const handle: AiAgentHandlehandle.AiAgentHandle.run(prompt: string, options?: AiSendOptions): Promise<AiRunResult>run('Review the current workspace change.'); ctx: CommandContextctx.ui: UiCapability
Notifications, dialogs, quick input, and progress.
ui
.UiCapability.notification: UiNotificationCapabilitynotification.UiNotificationCapability.info(notice: UiNotice): voidinfo({ UiNotice.message: stringmessage: const result: AiRunResultresult.AiRunResult.text: stringtext });
}, }); export default function defineMod(definition: ModDefinition): Mod
The entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.
@example```ts export default defineMod({ metadata: { displayName: 'Review', description: 'Review helpers for the current change.', }, commands: [ command({ id: 'review.open', title: 'Review: Open', run: openPanel }), slashCommand({ id: 'review', description: 'Review this change', run: review }), ], ai: { tools: [reviewTool], }, }); ```@categoryAuthoring
defineMod
({
metadata: ModMetadatametadata: { ModMetadata.displayName: stringdisplayName: 'Research', ModMetadata.description: stringdescription: 'Reusable research agents', }, prompts?: readonly Prompt[] | undefinedprompts: [const reviewInstructions: PromptreviewInstructions], ai?: AiModDefinition | undefinedai: { AiModDefinition.agents?: readonly Agent[] | undefinedagents: [const reviewer: Agentreviewer] }, commands?: readonly ModCommand<string>[] | undefinedcommands: [const reviewCurrentChange: Command<"research.review-current-change", void>reviewCurrentChange], });

The same Prompt value is available in the composer and used as the agent's system prompt. One value owns the wording.

Agents go under ai.agents, not a top-level agents slot. Their callable identity is <extensionId>/<name>. Passing the declared Agent value to ctx.ai.agents.get(...) avoids reconstructing that identity by hand.

Run or spawn​

AiAgentHandle exposes two execution shapes:

MethodResultUse it when
run(prompt)A completed answerThe caller needs one result now
spawn(config?)A durable root sessionThe work needs follow-up turns or its own visible conversation
import { function agent(input: AgentInput): Agent
This describes an agent, it does not run one. There is no handler, and nothing executes when the mod activates. Put the result under `ai.agents` and the host registers it so the user or another agent can call it by id, `<extensionId>/<name>`. That is the line between this and the rest of the AI surface. To actually run something, use `ctx.ai.ask` for a single answer or `ctx.ai.sessions` for a conversation. To give the model a capability it can invoke, write a `tool`, which does have a handler.
@categoryAuthoring
agent
, function command<const TId extends string, TResult>(commandValue: CommandInput<TId, TResult>): Command<TId, TResult>
Declare an action the user runs from the command palette or a keybinding. Reach for this when a person triggers the behavior. The `title` is what they read in the palette, `category` groups the entry, and `when` decides whether it shows up at all. The handler takes no argument, only a context. Whatever it returns is the command's result: `ctx.commands.execute(thisCommand)` resolves with that type, and the build compiles the same type into the result contract the host enforces, so return JSON data or nothing. Two neighbours are easy to confuse. `slashCommand` is also user-triggered, but it is typed inside a chat session and receives free text plus the live AI session. `tool` is not user-triggered at all, the model decides to call it mid-turn. The result goes in the mod's top-level `commands` array, alongside any slash commands. The `id` is the address a keybinding or a `command:` hook targets, so keep it stable.
@categoryAuthoring
command
, function defineMod(definition: ModDefinition): Mod
The entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.
@example```ts export default defineMod({ metadata: { displayName: 'Review', description: 'Review helpers for the current change.', }, commands: [ command({ id: 'review.open', title: 'Review: Open', run: openPanel }), slashCommand({ id: 'review', description: 'Review this change', run: review }), ], ai: { tools: [reviewTool], }, }); ```@categoryAuthoring
defineMod
} from '@modular/sdk';
const const researcher: Agentresearcher = function agent(input: AgentInput): Agent
This describes an agent, it does not run one. There is no handler, and nothing executes when the mod activates. Put the result under `ai.agents` and the host registers it so the user or another agent can call it by id, `<extensionId>/<name>`. That is the line between this and the rest of the AI surface. To actually run something, use `ctx.ai.ask` for a single answer or `ctx.ai.sessions` for a conversation. To give the model a capability it can invoke, write a `tool`, which does have a handler.
@categoryAuthoring
agent
({
AgentInput.name: stringname: 'researcher', AgentInput.description: stringdescription: 'Investigates a topic and records source-backed findings.', AgentInput.prompt: string | Promptprompt: 'Separate verified facts from unresolved questions.', AgentInput.model: AgentModelmodel: { kind: "inherit"kind: 'inherit' }, }); export default function defineMod(definition: ModDefinition): Mod
The entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.
@example```ts export default defineMod({ metadata: { displayName: 'Review', description: 'Review helpers for the current change.', }, commands: [ command({ id: 'review.open', title: 'Review: Open', run: openPanel }), slashCommand({ id: 'review', description: 'Review this change', run: review }), ], ai: { tools: [reviewTool], }, }); ```@categoryAuthoring
defineMod
({
metadata: ModMetadatametadata: { ModMetadata.displayName: stringdisplayName: 'Research', ModMetadata.description: stringdescription: 'Reusable research agents', }, ai?: AiModDefinition | undefinedai: { AiModDefinition.agents?: readonly Agent[] | undefinedagents: [const researcher: Agentresearcher] }, commands?: readonly ModCommand<string>[] | undefinedcommands: [ function command<"research.start", void>(commandValue: CommandInput<"research.start", void>): Command<"research.start", void>
Declare an action the user runs from the command palette or a keybinding. Reach for this when a person triggers the behavior. The `title` is what they read in the palette, `category` groups the entry, and `when` decides whether it shows up at all. The handler takes no argument, only a context. Whatever it returns is the command's result: `ctx.commands.execute(thisCommand)` resolves with that type, and the build compiles the same type into the result contract the host enforces, so return JSON data or nothing. Two neighbours are easy to confuse. `slashCommand` is also user-triggered, but it is typed inside a chat session and receives free text plus the live AI session. `tool` is not user-triggered at all, the model decides to call it mid-turn. The result goes in the mod's top-level `commands` array, alongside any slash commands. The `id` is the address a keybinding or a `command:` hook targets, so keep it stable.
@categoryAuthoring
command
({
id: "research.start"id: 'research.start', title: stringtitle: 'Start Research Session', run: (call: CommandCall) => Awaitable<void>run: async ({ ctx: CommandContextctx }) => { const const handle: AiAgentHandlehandle = await ctx: CommandContextctx.ContextBase.ai: AiRuntimeApi
One off answers, agents, models, and sessions.
ai
.AiRuntimeApi.agents: AiAgentsApiagents.AiAgentsApi.get(agent: AgentLookup): Promise<AiAgentHandle>
Look up one of this mod's own `agent(...)` values, or another mod's agent as `agent:<modId>/<name>`; rejects if it does not exist.
get
(const researcher: Agentresearcher);
const const session: AiSessionHandlesession = await const handle: AiAgentHandlehandle.AiAgentHandle.spawn(config?: AiSessionConfig): Promise<AiSessionHandle>spawn({ AiSessionConfig.title?: string | undefinedtitle: 'Dependency research', AiSessionConfig.foreground?: boolean | undefinedforeground: true, }); await const session: AiSessionHandlesession.AiSessionHandle.run(prompt: string, options?: AiSendOptions): Promise<AiRunResult>run('Map the dependency boundary for this package.'); }, }), ], });

Members​

Agent contains name, description, prompt, model, and kind.

  • description explains the role to people and model routing.
  • prompt accepts inline text or a declared prompt.
  • model is { kind: 'inherit' } to use the calling session's model, or { kind: 'route', id } to pin a model route id.

Use ctx.ai.agents.list(), find(), get(), and observe() when the agent is selected dynamically rather than imported as a declared value.

Declaration, metadata, configuration, execution​

The public model keeps four states separate:

Agent                 authored declaration
  │
  ▼
AiAgentMetadata       catalog projection
  │
  ▼
AiAgentHandle         executable identity
  │
  ├── config()        resolved session configuration
  ├── run()           one completed answer in a temporary root session
  └── spawn()         durable root session configured by the agent

handle.config(overrides) resolves the declared prompt and model into an AiSessionConfig without creating a session. handle.metadata() reads the current registered definition. run() and spawn() re-read the definition, so a retained handle follows a promoted declaration update rather than freezing an old snapshot.

An agent's spawn() is not ctx.session.spawn(). The first creates a root session configured by the agent. The second creates a child of the current conversation. See Lineage before choosing between them.