Code Mode

AI-powered code generation and editing tool

Diagram

Input

Agent

Tool

Output

Code Mode

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

Installation

bash
npm install praisonai-ts

Quick Start

  1. Simple Usage
    typescript
    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');
  2. With Configuration

    Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.

Configuration

typescript
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

typescript
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

typescript
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

typescript
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

Related