Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Autogen Studio Type Guards

From Leeroopedia
Knowledge Sources python/packages/autogen-studio/frontend/src/components/types/guards.ts
Domains Frontend, Type Safety, Runtime Validation, TypeScript Guards
Last Updated 2026-02-11

Overview

The Type Guards module provides runtime type checking functions and provider constants for AutoGen Studio components, enabling type-safe narrowing and validation of component configurations at runtime.

Description

This TypeScript module implements a comprehensive type guard system for AutoGen Studio's component architecture. It includes:

Provider Constants (PROVIDERS):

  • Centralized mapping of all AutoGen Python class paths to TypeScript constants
  • 40+ provider identifiers organized by category (teams, agents, models, tools, workbenches, terminations, contexts)
  • Type-safe provider enumeration exported as const assertion for literal type inference

Type Guard Functions:

  • Base Component Guards: isComponent, isTeamComponent, isAgentComponent, isModelComponent, isToolComponent, isTerminationComponent, isWorkbenchComponent
  • Team Guards: isRoundRobinTeam, isSelectorTeam, isSwarmTeam
  • Agent Guards: isAssistantAgent, isUserProxyAgent, isWebSurferAgent
  • Model Guards: isOpenAIModel, isAzureOpenAIModel, isAnthropicModel
  • Tool Guards: isFunctionTool
  • Workbench Guards: isStaticWorkbench, isStaticStreamWorkbench, isAnyStaticWorkbench, isMcpWorkbench
  • Termination Guards: 11 individual guards plus isCombinationTermination for OR/AND terminators

Type System Features:

  • Generic ProviderToConfig type mapping providers to their configuration types
  • ConfigForProvider helper type for extracting config type from provider
  • Proper TypeScript type predicates (x is Type) for compile-time narrowing
  • Runtime assertions with assertComponentType for strict validation

The module uses TypeScript's discriminated unions and type predicates to provide both compile-time type safety and runtime validation. Each guard function returns a boolean with a type predicate, allowing TypeScript to narrow the component type in conditional blocks.

Usage

This module is imported wherever component types need to be checked or narrowed at runtime, including component editors, configuration validators, and UI rendering logic.

import {
  PROVIDERS,
  isAssistantAgent,
  isOpenAIModel,
  isAnyStaticWorkbench
} from "./types/guards";

Code Reference

Source Location: /tmp/kapso_repo_2mr4n2g4/python/packages/autogen-studio/frontend/src/components/types/guards.ts

Key Exports:

// Provider constants
export const PROVIDERS = {
  // Teams
  ROUND_ROBIN_TEAM: "autogen_agentchat.teams.RoundRobinGroupChat",
  SELECTOR_TEAM: "autogen_agentchat.teams.SelectorGroupChat",
  SWARM_TEAM: "autogen_agentchat.teams.Swarm",

  // Agents
  ASSISTANT_AGENT: "autogen_agentchat.agents.AssistantAgent",
  USER_PROXY: "autogen_agentchat.agents.UserProxyAgent",
  WEB_SURFER: "autogen_ext.agents.web_surfer.MultimodalWebSurfer",

  // Models
  OPENAI: "autogen_ext.models.openai.OpenAIChatCompletionClient",
  AZURE_OPENAI: "autogen_ext.models.openai.AzureOpenAIChatCompletionClient",
  ANTHROPIC: "autogen_ext.models.anthropic.AnthropicChatCompletionClient",

  // ... (40+ total providers)
} as const;

export type Provider = (typeof PROVIDERS)[keyof typeof PROVIDERS];

// Type guard function signature
export function isAssistantAgent(
  component: Component<ComponentConfig>
): component is Component<AssistantAgentConfig> {
  return component.provider === PROVIDERS.ASSISTANT_AGENT;
}

// Runtime assertion
export function assertComponentType<P extends Provider>(
  component: Component<ComponentConfig>,
  provider: P
): asserts component is Component<ConfigForProvider<P>> {
  if (component.provider !== provider) {
    throw new Error(
      `Expected component with provider ${provider}, got ${component.provider}`
    );
  }
}

Import Statement:

import {
  // Constants
  PROVIDERS,
  type Provider,

  // Base guards
  isComponent,
  isTeamComponent,
  isAgentComponent,
  isModelComponent,
  isToolComponent,
  isTerminationComponent,
  isWorkbenchComponent,

  // Specific guards
  isAssistantAgent,
  isRoundRobinTeam,
  isOpenAIModel,
  isStaticWorkbench,
  isMcpWorkbench,
  isTextMentionTermination,
  isCombinationTermination,

  // Utilities
  assertComponentType
} from "./types/guards";

I/O Contract

Provider Constants

Category Providers Count
Teams ROUND_ROBIN_TEAM, SELECTOR_TEAM, SWARM_TEAM 3
Agents ASSISTANT_AGENT, USER_PROXY, WEB_SURFER 3
Models OPENAI, AZURE_OPENAI, ANTHROPIC 3
Tools FUNCTION_TOOL, PYTHON_CODE_EXECUTION_TOOL 2
Code Executors LOCAL_COMMAND_LINE_CODE_EXECUTOR 1
Workbenches STATIC_WORKBENCH, STATIC_STREAM_WORKBENCH, MCP_WORKBENCH 3
Terminations OR_TERMINATION, AND_TERMINATION, MAX_MESSAGE, TEXT_MENTION, STOP_MESSAGE, TOKEN_USAGE, HANDOFF, TIMEOUT, EXTERNAL, SOURCE_MATCH, TEXT_MESSAGE 11
Contexts UNBOUNDED_CONTEXT 1

Type Guard Functions

Function Parameter Returns Narrows To
isComponent value: any value is Component<ComponentConfig> Component type
isTeamComponent component: Component<ComponentConfig> component is Component<TeamConfig> Team component
isAgentComponent component: Component<ComponentConfig> component is Component<AgentConfig> Agent component
isModelComponent component: Component<ComponentConfig> component is Component<ModelConfig> Model component
isToolComponent component: Component<ComponentConfig> component is Component<ToolConfig> Tool component (deprecated)
isTerminationComponent component: Component<ComponentConfig> component is Component<TerminationConfig> Termination component
isWorkbenchComponent component: Component<ComponentConfig> component is Component<WorkbenchConfig> Workbench component
isRoundRobinTeam component: Component<ComponentConfig> component is Component<RoundRobinGroupChatConfig> Round robin team
isSelectorTeam component: Component<ComponentConfig> component is Component<SelectorGroupChatConfig> Selector team
isSwarmTeam component: Component<ComponentConfig> component is Component<SwarmConfig> Swarm team
isAssistantAgent component: Component<ComponentConfig> component is Component<AssistantAgentConfig> Assistant agent
isUserProxyAgent component: Component<ComponentConfig> component is Component<UserProxyAgentConfig> User proxy agent
isWebSurferAgent component: Component<ComponentConfig> component is Component<MultimodalWebSurferConfig> Web surfer agent
isOpenAIModel component: Component<ComponentConfig> component is Component<OpenAIClientConfig> OpenAI model
isAzureOpenAIModel component: Component<ComponentConfig> component is Component<AzureOpenAIClientConfig> Azure OpenAI model
isAnthropicModel component: Component<ComponentConfig> component is Component<AnthropicClientConfig> Anthropic model
isFunctionTool component: Component<ComponentConfig> component is Component<FunctionToolConfig> Function tool
isStaticWorkbench null | undefined component is Component<StaticWorkbenchConfig> Static workbench
isStaticStreamWorkbench null | undefined component is Component<StaticWorkbenchConfig> Static stream workbench
isAnyStaticWorkbench null | undefined component is Component<StaticWorkbenchConfig> Any static workbench
isMcpWorkbench null | undefined component is Component<McpWorkbenchConfig> MCP workbench
isOrTermination component: Component<ComponentConfig> component is Component<OrTerminationConfig> OR termination
isAndTermination component: Component<ComponentConfig> component is Component<AndTerminationConfig> AND termination
isCombinationTermination component: Component<ComponentConfig> AndTerminationConfig> OR or AND termination
isMaxMessageTermination component: Component<ComponentConfig> component is Component<MaxMessageTerminationConfig> Max message termination
isTextMentionTermination component: Component<ComponentConfig> component is Component<TextMentionTerminationConfig> Text mention termination
assertComponentType component: Component<ComponentConfig>, provider: Provider asserts component is Component<ConfigForProvider<P>> Throws error if mismatch

Usage Examples

Type Narrowing in Conditionals

import { isAssistantAgent, isOpenAIModel } from "./types/guards";
import type { Component, ComponentConfig } from "./types/datamodel";

function processComponent(component: Component<ComponentConfig>) {
  if (isAssistantAgent(component)) {
    // TypeScript knows component.config is AssistantAgentConfig
    console.log(`Agent name: ${component.config.name}`);
    console.log(`System message: ${component.config.system_message}`);

    // Access nested model client
    if (isOpenAIModel(component.config.model_client)) {
      console.log(`Using model: ${component.config.model_client.config.model}`);
    }
  }
}

Workbench Type Checking

import { isAnyStaticWorkbench, isMcpWorkbench } from "./types/guards";
import type { Component, WorkbenchConfig } from "./types/datamodel";

function renderWorkbench(workbench: Component<WorkbenchConfig>) {
  if (isAnyStaticWorkbench(workbench)) {
    // TypeScript knows workbench.config is StaticWorkbenchConfig
    console.log(`Static workbench with ${workbench.config.tools.length} tools`);
    return <StaticWorkbenchEditor workbench={workbench} />;
  } else if (isMcpWorkbench(workbench)) {
    // TypeScript knows workbench.config is McpWorkbenchConfig
    const serverType = workbench.config.server_params.type;
    console.log(`MCP workbench using ${serverType}`);
    return <McpWorkbenchEditor workbench={workbench} />;
  }
}

Using Provider Constants

import { PROVIDERS } from "./types/guards";
import type { Component, AgentConfig } from "./types/datamodel";

// Creating components with correct provider strings
const assistant: Component<AgentConfig> = {
  provider: PROVIDERS.ASSISTANT_AGENT, // Type-safe string literal
  component_type: "agent",
  config: {
    name: "my_assistant",
    // ... rest of config
  }
};

// Checking provider equality
if (component.provider === PROVIDERS.OPENAI) {
  console.log("This is an OpenAI model");
}

Runtime Assertions

import { assertComponentType, PROVIDERS } from "./types/guards";
import type { Component, ComponentConfig, OpenAIClientConfig } from "./types/datamodel";

function configureOpenAIModel(component: Component<ComponentConfig>) {
  // Assert the component is an OpenAI model - throws if not
  assertComponentType(component, PROVIDERS.OPENAI);

  // TypeScript now knows component.config is OpenAIClientConfig
  component.config.temperature = 0.7;
  component.config.max_tokens = 2000;

  return component;
}

Handling Termination Combinations

import {
  isCombinationTermination,
  isTextMentionTermination,
  isMaxMessageTermination
} from "./types/guards";
import type { Component, TerminationConfig } from "./types/datamodel";

function analyzeTermination(termination: Component<TerminationConfig>) {
  if (isCombinationTermination(termination)) {
    // TypeScript knows termination.config has 'conditions' array
    console.log(`Combination with ${termination.config.conditions.length} conditions`);

    termination.config.conditions.forEach(condition => {
      if (isTextMentionTermination(condition)) {
        console.log(`- Text mention: "${condition.config.text}"`);
      } else if (isMaxMessageTermination(condition)) {
        console.log(`- Max messages: ${condition.config.max_messages}`);
      }
    });
  }
}

Component Validation

import { isComponent, isAgentComponent, isAssistantAgent } from "./types/guards";

function validateComponent(data: unknown): boolean {
  // First check if it's a valid component structure
  if (!isComponent(data)) {
    console.error("Invalid component structure");
    return false;
  }

  // Then check specific type requirements
  if (isAgentComponent(data) && isAssistantAgent(data)) {
    // Validate AssistantAgent specific properties
    if (!data.config.model_client) {
      console.error("AssistantAgent requires model_client");
      return false;
    }
    return true;
  }

  return true;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment