Back to Copilotkit

defineChannelCommand

showcase/shell-docs/src/content/reference/channels/functions/defineChannelCommand.mdx

1.64.22.0 KB
Original Source

defineChannelCommand preserves a command definition and infers structured options when the provider supplies them.

Signature

ts
function defineChannelCommand<Schema extends ObjectSchema>(
  command: ChannelCommand<Schema>,
): ChannelCommand<Schema>;
ts
interface ChannelCommand<Schema extends ObjectSchema> {
  name: string;
  description?: string;
  options?: Schema;
  handler(
    context: CommandContext<InferSchemaOutput<Schema>>,
  ): void | Promise<void>;
}

Example

ts
import { defineChannelCommand } from "@copilotkit/channels";

export const triage = defineChannelCommand({
  name: "triage",
  description: "Triage the current conversation.",
  async handler({ thread, text, user, platform }) {
    await thread.runAgent({
      prompt: text || "Triage this conversation.",
      context: [
        { description: "Provider", value: platform },
        {
          description: "Invoking user",
          value: user?.name ?? user?.id ?? "unknown",
        },
      ],
    });
  },
});

Register it with createChannel({ commands: [triage] }) or channel.onCommand(triage) before startup.

CommandContext

FieldTypeNotes
threadThreadConversation where the command was invoked.
commandstringNormalized name: no leading slash, lowercase, hyphens and underscores route equivalently.
textstringRaw argument text. Managed Slack and Teams use this field.
optionsTOptionsStructured options when a provider supplies them; empty on managed Slack and Teams.
userPlatformUser | undefinedInvoking provider user.
platformstringNative provider ("slack" or "teams") on managed command ingress.
openModalfunction or undefinedOmitted by the managed Slack and Teams adapter.

Slack must also register the native slash command in the Slack app. Teams routes messages matching /name or /name arguments. See Commands and reactions.