Skip to main content

Code Mode

Code Mode provides AI-powered code generation and editing capabilities for agents.

Installation

npm install praisonai-ts

Quick Start

import { Agent, codeMode } from 'praisonai-ts';

const codeTool = codeMode({
  language: 'typescript',
});

const agent = new Agent({
  name: 'CodeAgent',
  instructions: 'You help write and edit code.',
  tools: [codeTool],
});

const response = await agent.chat('Write a function to calculate fibonacci numbers');

Configuration

interface CodeModeConfig {
  /** Programming language */
  language?: 'typescript' | 'javascript' | 'python' | 'go' | 'rust';
  /** Enable code formatting */
  format?: boolean;
  /** Enable linting */
  lint?: boolean;
  /** Working directory */
  workDir?: string;
}

Usage Examples

Generate Code

const codeTool = codeMode({ language: 'python' });

const agent = new Agent({
  name: 'PythonCoder',
  instructions: 'You write Python code.',
  tools: [codeTool],
});

await agent.chat('Create a class for managing a todo list');

Edit Existing Code

const codeTool = codeMode({
  language: 'typescript',
  workDir: './src',
});

const agent = new Agent({
  name: 'CodeEditor',
  instructions: 'You help refactor and improve code.',
  tools: [codeTool],
});

await agent.chat('Refactor the utils.ts file to use async/await');

Input/Output Types

interface CodeModeInput {
  /** Action to perform */
  action: 'generate' | 'edit' | 'explain' | 'review';
  /** Code or description */
  content: string;
  /** Target file path (for edit) */
  filePath?: string;
}

interface CodeModeResult {
  /** Generated or edited code */
  code: string;
  /** Explanation */
  explanation?: string;
  /** File path if saved */
  filePath?: string;
  /** Language */
  language: string;
}

Environment Variables

VariableRequiredDescription
OPENAI_API_KEYYesFor code generation