Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

9.3 KB
Raw Blame
import { controllerConnectionId, sharedConfigSchema } from '@switch-console/agent-providers';
import { sessionLaunchConfig } from '@switch-console/plugins/agents';
import { SWITCH_SKILL_CONTEXT, SWITCH_SKILL_FILE } from '@switch-console/plugins/switch-skill';
import { describe, expect, it } from 'vitest';
import { PROVIDERS } from './schemas';
import {
  advancedConfigDefinitionProblem,
  buildWatcherTemplate,
  watcherSessionId,
} from './template';

const definition = {
  name: 'scout',
  model: null,
  advanced_config: {},
  instructions: 'Review pull requests.',
  auto_approve: false,
  auto_mode: false,
};

describe('buildWatcherTemplate', () => {
  it.each(PROVIDERS)('builds a configuration the shared host accepts for %s', (provider) => {
    const template = buildWatcherTemplate({
      agentId: 'agent-1',
      provider,
      definition,
      cwd: '/work/scout',
      credentialsPath: '/data/agents/agent-1/credentials.json',
      binaryPath: `/usr/bin/${provider}`,
    });
    expect(sharedConfigSchema.safeParse(template).success).toBe(true);
    expect(template.session).toMatchObject({
      sessionId: watcherSessionId('agent-1'),
      agentId: 'agent-1',
      provider,
      status: 'starting',
    });
    expect(template.start.provider).toBe(provider);
    expect(template.start.input).toMatchObject({
      sessionId: watcherSessionId('agent-1'),
      cwd: '/work/scout',
      runtimeMode: 'approval-required',
      env: {},
      mcpServers: {},
    });
    expect(template.start.input.model).toBeUndefined();
    expect(template.roomConnection).toEqual({ connectionId: controllerConnectionId('agent-1') });
    expect(template.execution).toMatchObject({
      credentialsPath: '/data/agents/agent-1/credentials.json',
      binaryPath: `/usr/bin/${provider}`,
      codexConfig:
        provider === 'codex'
          ? 'sandbox_mode = "workspace-write"\ndeveloper_instructions = "Review pull requests."\n'
          : '',
    });
    expect(template.execution!.inheritEnv).toEqual(
      expect.arrayContaining(['PATH', 'HOME', 'ANTHROPIC_API_KEY', 'OPENAI_API_KEY'])
    );
    if (provider === 'opencode') {
      expect(template.execution!.skill).toBe(SWITCH_SKILL_FILE);
      expect(template.execution!.context).toBe('Review pull requests.');
    } else {
      expect(template.execution!.skill).toBe('');
      expect(template.execution!.context).toBe(`${SWITCH_SKILL_CONTEXT}\n\nReview pull requests.`);
    }
  });

  it('runs an auto-approving agent with full access, on its model', () => {
    const template = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'claude',
      definition: {
        name: 'scout',
        model: 'opus',
        advanced_config: {},
        instructions: '',
        auto_approve: true,
        auto_mode: false,
      },
      cwd: '/work/scout',
      credentialsPath: '/data/c.json',
      binaryPath: null,
    });
    expect(template.start.input.runtimeMode).toBe('full-access');
    expect(template.start.input.model).toEqual({ id: 'opus' });
    expect(template.execution).not.toHaveProperty('binaryPath');
    expect(template.execution!.context).toBe(SWITCH_SKILL_CONTEXT);
  });

  it.each(PROVIDERS)(
    'runs %s in auto mode, which bypass wins over, keeping Codex in its workspace sandbox',
    (provider) => {
      const build = (auto_approve: boolean) =>
        buildWatcherTemplate({
          agentId: 'agent-1',
          provider,
          definition: { ...definition, auto_approve, auto_mode: true },
          cwd: '/work/scout',
          credentialsPath: '/data/c.json',
          binaryPath: null,
        });
      const auto = build(false);
      expect(sharedConfigSchema.safeParse(auto).success).toBe(true);
      expect(auto.start.input.runtimeMode).toBe('auto');
      expect(build(true).start.input.runtimeMode).toBe('full-access');
      if (provider === 'codex') {
        expect(auto.execution!.codexConfig).toMatch(/^sandbox_mode = "workspace-write"\n/);
        expect(build(true).execution!.codexConfig).toMatch(
          /^sandbox_mode = "danger-full-access"\n/
        );
      }
    }
  );

  it('applies a Codex agent’s advanced configuration as Console does: model options and profile', () => {
    const advanced = {
      model: 'gpt-5.5',
      advancedConfig: { effort: 'high', verbosity: 'low', webSearch: 'true' },
    };
    const template = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'codex',
      definition: {
        name: 'scout',
        model: advanced.model,
        advanced_config: advanced.advancedConfig,
        instructions: 'Review pull requests.',
        auto_approve: false,
        auto_mode: false,
      },
      cwd: '/work/scout',
      credentialsPath: '/data/c.json',
      binaryPath: null,
    });
    const console = sessionLaunchConfig({
      provider: 'codex',
      slug: 'scout',
      description: '',
      cwd: '/work/scout',
      instructions: 'Review pull requests.',
      ...advanced,
    });
    expect(template.start.input.model).toEqual({ id: 'gpt-5.5', options: { effort: 'high' } });
    expect(template.execution!.codexConfig).toBe(
      `sandbox_mode = "workspace-write"\n${console.codexConfig}`
    );
    expect(template.execution!.codexConfig).toContain('model_verbosity = "low"');
    expect(template.execution!.codexConfig).toContain('web_search = true');
    expect(template.start.input).not.toHaveProperty('agentDefinition');
  });

  it('runs a Claude Code agent as the definition its advanced configuration amounts to', () => {
    const template = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'claude',
      definition: {
        name: 'scout',
        model: 'opus',
        advanced_config: {
          tools: ['Read'],
          permissionMode: 'plan',
          maxTurns: 5,
          isolation: 'worktree',
          effort: 'max',
        },
        instructions: 'Review pull requests.',
        auto_approve: false,
        auto_mode: false,
      },
      cwd: '/work/scout',
      credentialsPath: '/data/c.json',
      binaryPath: null,
    });
    expect(template.start.input.model).toEqual({ id: 'opus', options: { effort: 'max' } });
    expect(template.start.input.agentName).toBe('scout');
    expect(template.start.input.agentDefinition).toEqual({
      description: 'scout',
      prompt: 'Review pull requests.',
      model: 'opus',
      tools: ['Read', 'mcp__switch'],
      permissionMode: 'plan',
      maxTurns: 5,
      effort: 'max',
    });
    expect(template.execution).not.toHaveProperty('agentDefinition');
  });

  it('gives an OpenCode agent its variant with the model', () => {
    const template = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'opencode',
      definition: {
        name: 'scout',
        model: 'anthropic/claude-sonnet-4-5',
        advanced_config: { variant: 'high', temperature: 0.2 },
        instructions: '',
        auto_approve: false,
        auto_mode: false,
      },
      cwd: '/work/scout',
      credentialsPath: '/data/c.json',
      binaryPath: null,
    });
    expect(template.start.input.model).toEqual({
      id: 'anthropic/claude-sonnet-4-5',
      options: { variant: 'high' },
    });
  });

  it('advertises the adapter’s own approval and question capabilities', () => {
    const codex = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'codex',
      definition,
      cwd: '/w',
      credentialsPath: '/c',
      binaryPath: null,
    });
    const claude = buildWatcherTemplate({
      agentId: 'agent-1',
      provider: 'claude',
      definition,
      cwd: '/w',
      credentialsPath: '/c',
      binaryPath: null,
    });
    expect(codex.session.capabilities).toMatchObject({ approvals: true, questions: false });
    expect(claude.session.capabilities).toMatchObject({ approvals: true, questions: true });
  });
});

describe('advancedConfigDefinitionProblem', () => {
  it('accepts the advanced configuration a provider applies', () => {
    expect(
      advancedConfigDefinitionProblem('opencode', {
        ...definition,
        advanced_config: { variant: 'high', maxSteps: 40, webSearch: 'false' },
      })
    ).toBeNull();
  });

  it('names a field this controller does not know for the provider', () => {
    expect(
      advancedConfigDefinitionProblem('codex', {
        ...definition,
        advanced_config: { sandbox: 'workspace-write' },
      })
    ).toMatch(/'sandbox'/);
    expect(
      advancedConfigDefinitionProblem('cursor', {
        ...definition,
        advanced_config: { effort: 'high' },
      })
    ).toMatch(/'effort'/);
  });

  it('accepts an empty configuration for a provider that applies no settings', () => {
    expect(
      advancedConfigDefinitionProblem('antigravity', { ...definition, advanced_config: {} })
    ).toBeNull();
    expect(
      advancedConfigDefinitionProblem('cursor', { ...definition, advanced_config: {} })
    ).toBeNull();
  });

  it('names a value of a shape this controller cannot apply', () => {
    expect(
      advancedConfigDefinitionProblem('opencode', {
        ...definition,
        advanced_config: { temperature: 'warm' },
      })
    ).toMatch(/'temperature'.*number/);
  });

  it('names a Claude Code setting its session cannot start with', () => {
    expect(
      advancedConfigDefinitionProblem('claude', {
        ...definition,
        advanced_config: { maxTurns: 1.5 },
      })
    ).toMatch(/maxTurns/);
  });
});