Code Mode
AI-powered code generation and editing tool
Code Mode
Code Mode provides AI-powered code generation and editing capabilities for agents.
Installation
npm install praisonai-tsQuick Start
- 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'); - With Configuration
Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.
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
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY | Yes | For code generation |