Skip to main content

Slash commands

A person types it inside a chat session. Same trigger as a command — a human — but a different place and a different handler shape.

import { 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 slashCommand<const TId extends string, TResult>(slashCommandValue: SlashCommandInput<TId, TResult>): SlashCommand<TId, TResult>
Declare a command the user types inside a chat session. The `id` is the name typed after the slash, `description` is the line shown in the typeahead, and `argumentHint` is the placeholder for whatever the user types after the name. `argument` declares whether that text is `'required'`, `'optional'`, or `'none'`. The host trims it, answers a missing required argument with the usage line, and hands the handler `{ ctx, argument }`, where `ctx` carries the AI session the command was typed into. Use this instead of `command` when the behavior belongs to a conversation and needs the session. Use `command` when it belongs to the workbench and runs from the palette. Use `tool` when the model, not the user, should decide to run it. Slash commands go in the same top-level `commands` array as palette commands.
@categoryAuthoring
slashCommand
} from '@modular/sdk';
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: 'Hello', ModMetadata.description: stringdescription: 'A first mod' }, commands?: readonly ModCommand<string>[] | undefinedcommands: [ function slashCommand<string, void>(slashCommandValue: SlashCommandInput<string, void>): SlashCommand<string, void>
Declare a command the user types inside a chat session. The `id` is the name typed after the slash, `description` is the line shown in the typeahead, and `argumentHint` is the placeholder for whatever the user types after the name. `argument` declares whether that text is `'required'`, `'optional'`, or `'none'`. The host trims it, answers a missing required argument with the usage line, and hands the handler `{ ctx, argument }`, where `ctx` carries the AI session the command was typed into. Use this instead of `command` when the behavior belongs to a conversation and needs the session. Use `command` when it belongs to the workbench and runs from the palette. Use `tool` when the model, not the user, should decide to run it. Slash commands go in the same top-level `commands` array as palette commands.
@categoryAuthoring
slashCommand
({
id: stringid: 'ask', description: stringdescription: 'Ask something', argumentHint?: string | undefinedargumentHint: '<question>', argument: "required"argument: 'required', run: (call: SlashCommandCall<string>) => Awaitable<void>run: async ({ argument: stringargument, ctx: SlashCommandContextctx }) => { await ctx: SlashCommandContextctx.session: AiSessionHandle
The chat session the command was typed into.
session
.AiSessionHandle.run(prompt: string, options?: AiSendOptions): Promise<AiRunResult>run(argument: stringargument);
}, }), ], });

The handler takes one call object: argument, the text the person typed after the command name, and ctx, the context. argument declares what the command accepts, and that declaration types the handler's argument:

argumentThe handler's argument
'required'string; without text the host answers with the usage line and does not run the handler
'optional'string | undefined, undefined when nothing was typed
'none'undefined; the command takes no text

Slash commands go in the same commands array as commands. ModCommand is Command | SlashCommand.

Members​

SlashCommand — id, description, run, argumentHint, when, kind.

The differences from Command are the interesting part:

  • No title. In a chat session the id is what a person types, so a separate display title would be a second name for the same thing.
  • description instead — it is what a person reads in the autocomplete list, not what they type.
  • argumentHint tells them what to type after the id.

Because a slash command runs inside a live session, its handler receives free text and the session alongside the usual context.