npm package: @statiolake/coc-lm-chat
Description: Language Model Chat agent extension for coc.nvim
Author: statiolake
Latest version: 2.4.0

# @statiolake/coc-lm-chat

Interactive AI chat interface for coc.nvim with autonomous agent capabilities and tool integration.

## Features

- 🗨️ **Interactive Chat Interface**: Real-time chat with language models in Neovim
- 🤖 **Autonomous Agent**: Self-operating agent with tool execution capabilities
- 🛠️ **Built-in Tools**: Time, calculation, and workspace information tools
- 📝 **Markdown Rendering**: Syntax-highlighted chat display with virtual text
- 🔄 **Streaming Responses**: Real-time token streaming for immediate feedback
- 🎯 **Context-Aware**: Maintains conversation history and context
- 📊 **Sidebar Support**: Optional sidebar chat interface

## Installation

### Prerequisites

- [coc.nvim](https://github.com/neoclide/coc.nvim) 0.0.82+
- Node.js 16+
- **[@statiolake/coc-lm-api](https://www.npmjs.com/package/@statiolake/coc-lm-api)**: Required dependency
- **[@statiolake/coc-github-copilot](https://www.npmjs.com/package/@statiolake/coc-github-copilot)**: Recommended for GitHub Copilot models

### Install via coc.nvim

```vim
:CocInstall @statiolake/coc-lm-api @statiolake/coc-github-copilot @statiolake/coc-lm-chat
```

### Install via npm

```bash
npm install -g @statiolake/coc-lm-api @statiolake/coc-github-copilot @statiolake/coc-lm-chat
```

## Setup

1. Install all required extensions
2. Authenticate with your language model provider (e.g., `:CocCommand copilot.signIn` for GitHub Copilot)
3. Start chatting with `:CocCommand copilot.chat`

## Commands

| Command | Description |
|---------|-------------|
| `:CocCommand copilot.chat` | Open a new chat buffer |
| `:CocCommand copilot.sendMessage` | Send current buffer content as message |
| `:CocCommand copilot.chatSideBarToggle` | Toggle sidebar chat |
| `:CocCommand copilot.chatSideBarOpen` | Open sidebar chat |
| `:CocCommand copilot.chatSideBarClose` | Close sidebar chat |
| `:CocCommand copilot.chatSideBarReset` | Reset sidebar chat |

## Usage

### Basic Chat

1. Open a chat session: `:CocCommand copilot.chat`
2. Type your message in the input area (after the \"You:\" line)
3. Press `<Enter>` in normal mode to send the message
4. Watch the AI response stream in real-time

### Chat Interface

The chat buffer displays:

```
# Copilot Chat

You:
[Type your message here]

Agent:
[AI response appears here]

Tool:
🔧 **toolName** {\"param\": \"value\"}
```tool output```

You:
[Next message...]
```

### Keybindings

In chat buffers:

- `<Enter>` (normal mode): Send the current message
- `<C-c>` (normal mode): Clear conversation history

### Sidebar Chat

For a persistent chat interface:

```vim
:CocCommand copilot.chatSideBarOpen
```

The sidebar provides a dedicated chat area that doesn't interfere with your main editing workflow.

## Built-in Tools

The extension includes several built-in tools that the AI can use:

### getCurrentTime
Get the current date and time.

```json
{
  \"name\": \"getCurrentTime\",
  \"description\": \"Get the current date and time\",
  \"inputSchema\": { \"type\": \"object\", \"properties\": {} }
}
```

### calculate
Perform mathematical calculations.

```json
{
  \"name\": \"calculate\",
  \"description\": \"Perform mathematical calculations\",
  \"inputSchema\": {
    \"type\": \"object\",
    \"properties\": {
      \"expression\": { \"type\": \"string\", \"description\": \"Mathematical expression to evaluate\" }
    },
    \"required\": [\"expression\"]
  }
}
```

### getWorkspaceInfo
Get information about the current workspace.

```json
{
  \"name\": \"getWorkspaceInfo\",
  \"description\": \"Get information about the current workspace\",
  \"inputSchema\": { \"type\": \"object\", \"properties\": {} }
}
```

## Configuration

Add to your coc-settings.json:

```json
{
  \"copilot.enable\": true
}
```

### Agent Configuration

The autonomous agent supports various configuration options that can be customized by extension developers:

- `maxIterations`: Maximum number of tool execution iterations (default: 10)
- `maxDepth`: Maximum recursion depth for tool calls (default: 3)
- `autoExecute`: Whether to automatically execute tools (default: true)
- `timeout`: Timeout for tool execution in milliseconds (default: 30000)
- `enableLogging`: Enable detailed logging (default: true)

## Architecture

### Agent System

The extension implements a self-operating agent that can:

1. **Understand Requests**: Parse user messages and determine appropriate actions
2. **Execute Tools**: Automatically invoke registered tools based on context
3. **Chain Operations**: Use tool results to inform subsequent actions
4. **Provide Feedback**: Stream responses and tool execution results in real-time

### Tool Integration

Tools are registered with the LM API and can be invoked by the language model:

```typescript
// Tools are automatically registered on startup
lmApi.registerTool('getCurrentTime', getCurrentTimeTool);
lmApi.registerTool('calculate', calculateTool);
lmApi.registerTool('getWorkspaceInfo', getWorkspaceInfoTool);
```

### Message Flow

1. User types message in chat buffer
2. Message sent to language model via LM API
3. Model processes request and may invoke tools
4. Tool results fed back to model
5. Final response streamed to chat buffer

## Examples

### Basic Conversation

```
You:
What time is it?

Agent:
Let me check the current time for you.

Tool:
🔧 **getCurrentTime** {}
2025-01-13T22:45:06.406Z

Agent:
The current time is January 13th, 2025 at 10:45 PM UTC.
```

### Mathematical Calculation

```
You:
Calculate 15% tip on a $45 bill

Agent:
I'll calculate that for you.

Tool:
🔧 **calculate** {\"expression\": \"45 * 0.15\"}
6.75

Agent:
A 15% tip on a $45 bill would be $6.75, making the total $51.75.
```

### Workspace Information

```
You:
What files are in my current workspace?

Agent:
Let me check your workspace information.

Tool:
🔧 **getWorkspaceInfo** {}
Current workspace: /home/user/project
Files: src/main.ts, package.json, README.md

Agent:
Your current workspace is `/home/user/project` and contains 3 files: src/main.ts, package.json, and README.md.
```

## Troubleshooting

### No Language Models Available

1. Ensure LM API extension is installed: `:CocInstall @statiolake/coc-lm-api`
2. Install a language model provider: `:CocInstall @statiolake/coc-github-copilot`
3. Authenticate with the provider: `:CocCommand copilot.signIn`
4. Restart coc.nvim: `:CocRestart`

### Agent Not Responding

1. Check if the agent is initialized properly
2. Verify language models are available
3. Check coc.nvim logs for errors: `:CocOpenLog`

### Tool Execution Errors

1. Ensure tools are properly registered
2. Check tool input parameters
3. Verify workspace permissions for file operations

## Development

```bash
# Clone the repository
git clone https://github.com/statiolake/coc-github-copilot.git
cd coc-github-copilot/coc-lm-chat

# Install dependencies
npm install

# Build
npm run build

# Watch mode for development
npm run watch

# Run linting
npm run lint
```

### Adding Custom Tools

Developers can extend the tool system by registering additional tools:

```typescript
import type { LmApi } from '@statiolake/coc-lm-api';

// Register a custom tool
lmApi.registerTool('customTool', {
  name: 'customTool',
  description: 'Description of what the tool does',
  inputSchema: {
    type: 'object',
    properties: {
      param: { type: 'string', description: 'Parameter description' }
    },
    required: ['param']
  },
  invoke: async (options) => {
    const { param } = options.input;
    // Tool implementation
    return {
      content: [new LanguageModelTextPart(`Result: ${param}`)]
    };
  }
});
```

## Related Extensions

- **[@statiolake/coc-lm-api](https://www.npmjs.com/package/@statiolake/coc-lm-api)**: Required Language Model API interface
- **[@statiolake/coc-github-copilot](https://www.npmjs.com/package/@statiolake/coc-github-copilot)**: GitHub Copilot integration providing language models

## License

MIT

## Repository

[GitHub](https://github.com/statiolake/coc-github-copilot)

## Acknowledgments

- [coc.nvim](https://github.com/neoclide/coc.nvim) for the extension framework
- Language model providers for AI capabilities
- The Vim/Neovim community for inspiration and feedback