Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

2.9 KB
Raw Blame
import { Command, Options } from '@effect/cli';
import { Effect, Match, Option } from 'effect';
import { ProjectEnvironmentDetector } from 'src/services/project-environment-detector';
import { NodeProcess } from 'src/services/node-process';
import { TerminalUI } from 'src/services/terminal-ui';
import { generateTypescriptTypeStubs } from '../ts/commands/ts.generate.cmd';
import { generatePythonTypeStubs } from '../py/commands/py.generate.cmd';
import { generateCmd$Py } from './generate.py.cmd';
import { generateCmd$Ts } from './generate.ts.cmd';

const outputOpt = Options.optional(
  Options.directory('output-dir', {
    exists: 'either',
  })
).pipe(Options.withAlias('o'), Options.withDescription('Output directory for type stubs'));

const typeTools = Options.boolean('type-tools').pipe(
  Options.withDefault(false),
  Options.withDescription(
    'Generate typed input/output schemas for each tool (TypeScript only, slower)'
  )
);

const toolkitsOpt = Options.text('toolkits').pipe(
  Options.repeated,
  Options.withDescription(
    'Only generate types for specific toolkits (e.g., --toolkits gmail --toolkits slack)'
  )
);

/**
 * CLI entry point for code generation commands.
 *
 * When invoked without a subcommand, auto-detects the project language.
 * Subcommands `py` and `ts` allow explicit language selection.
 *
 * @example
 * ```bash
 * composio generate
 * composio generate py
 * composio generate ts
 * composio generate ts --type-tools
 * ```
 */
export const generateCmd = Command.make('generate', { outputOpt, typeTools, toolkitsOpt }).pipe(
  Command.withDescription(
    'Generate type stubs for toolkits, tools, and triggers, auto-detecting project language (TypeScript | Python)'
  ),
  Command.withHandler(({ outputOpt, typeTools, toolkitsOpt }) =>
    Effect.gen(function* () {
      const ui = yield* TerminalUI;
      const process = yield* NodeProcess;
      const cwd = process.cwd;

      yield* Effect.logDebug('Identifying project type...');
      const envDetector = yield* ProjectEnvironmentDetector;
      const env = yield* envDetector.detectProjectEnvironment(cwd);
      const displayLang = env.kind === 'js' ? 'TypeScript' : 'Python';
      yield* ui.log.step(`Project type detected: ${displayLang}`);

      // Apply the same transpile default as `composio generate ts`:
      // transpile when writing to node_modules (no --output-dir)
      const shouldCompile = !Option.isSome(outputOpt);

      yield* Match.value(env.kind).pipe(
        Match.when('js', () =>
          generateTypescriptTypeStubs({
            outputOpt,
            compact: false,
            transpiled: shouldCompile,
            typeTools,
            toolkitsOpt,
          })
        ),
        Match.when('python', () => generatePythonTypeStubs({ outputOpt, toolkitsOpt })),
        Match.exhaustive
      );
    })
  ),
  Command.withSubcommands([generateCmd$Py, generateCmd$Ts])
);