codebridge-cli 0.6.0-alpha

Local coding agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# CodeBridge CLI

A powerful local server that enables LLM-driven code automation with intelligent build detection, git operations, and script execution capabilities.

## πŸš€ New Features

### ✨ Smart Build Detection
Automatically detects your project type and uses the appropriate build command:
- **Rust** (Cargo.toml) β†’ `cargo check`
- **Node.js** (package.json) β†’ `npm run build`
- **Python** (requirements.txt) β†’ `python -m py_compile`
- **Documentation** (mostly .md files) β†’ No build needed
- **Unknown** β†’ No build attempted

No more "could not find Cargo.toml" errors on documentation projects!

### πŸ”§ Git Repository Initialization
New `GitInit` action allows the LLM to initialize git repositories on-demand. No more manual setup required.

### 🎯 Script Execution (Experimental)
Execute custom scripts in multiple languages for complex automation:
- **Deno**: TypeScript/JavaScript with secure defaults
- **Python**: General-purpose scripting
- **Bash**: Shell automation
- **Rust**: Compiled scripts (coming soon)

**Security Features**:
- Sandboxed execution (filesystem restricted to project)
- Timeout enforcement (30s default)
- No network access by default
- Resource limits

---

## πŸ“¦ Installation

### Prerequisites
- Rust 1.70+ (for building)
- Git 2.0+ (for git operations)
- Deno 1.30+ (optional, for script execution)
- Python 3.8+ (optional, for script execution)

### Build from Source
```bash
cargo build --release
```

### Run
```bash
./target/release/codebridge-cli \
  --port 3000 \
  --workspace ./workspace \
  --build-command auto \
  --enable-script-execution \
  --script-languages deno,python
```

---

## πŸ”§ Configuration

### CLI Options

| Flag | Default | Description |
|------|---------|-------------|
| `--port` | `3000` | HTTP server port |
| `--workspace` | `../codebridge-workspace` | Root directory for projects |
| `--build-command` | `auto` | Build command (use `auto` for detection, `none` to disable) |
| `--allowed-commands` | `npm,cargo,python` | Comma-separated list of allowed shell commands |
| `--enable-script-execution` | `false` | Enable LLM script execution |
| `--script-languages` | `deno,python` | Allowed script languages |
| `--script-timeout` | `30` | Script execution timeout (seconds) |

### Examples

**Rust-only environment**:
```bash
codebridge-cli --build-command "cargo check" --allowed-commands cargo
```

**Documentation project**:
```bash
codebridge-cli --build-command none
```

**Full automation mode** (with script execution):
```bash
codebridge-cli \
  --enable-script-execution \
  --script-languages deno,python,bash \
  --script-timeout 60
```

---

## πŸ“‘ API Reference

### Endpoints

#### `POST /execute`
Execute a series of actions and optionally run a build.

**Request**:
```json
{
  "projectId": "my-project",
  "actions": [
    { "type": "writeFile", "params": { "path": "src/main.rs", "content": "fn main() {}" } },
    { "type": "gitInit" },
    { "type": "gitAdd", "params": { "files": ["src/main.rs"] } },
    { "type": "gitCommit", "params": { "message": "Initial commit" } }
  ]
}
```

**Response**:
```json
{
  "actionResults": [
    { "success": true, "content": null, "error": null },
    { "success": true, "content": "Git repository initialized", "error": null },
    { "success": true, "content": null, "error": null },
    { "success": true, "content": "Committed: abc123...", "error": null }
  ],
  "buildOutput": {
    "success": true,
    "errors": [],
    "warnings": [],
    "rawOutput": "..."
  },
  "continueLoop": false
}
```

---

## 🎬 Action Types

### File Operations

#### `readFile`
```json
{ "type": "readFile", "params": { "path": "README.md" } }
```

#### `writeFile`
```json
{ 
  "type": "writeFile", 
  "params": { 
    "path": "src/lib.rs", 
    "content": "pub fn hello() { println!(\"Hello\"); }" 
  } 
}
```

#### `deleteFile`
```json
{ "type": "deleteFile", "params": { "path": "old-file.txt" } }
```

#### `createDirectory`
```json
{ "type": "createDirectory", "params": { "path": "src/components" } }
```

#### `listDirectory`
```json
{ "type": "listDirectory", "params": { "path": "src" } }
```

---

### Git Operations

#### `gitInit` πŸ†•
Initialize a git repository.
```json
{ "type": "gitInit" }
```

#### `gitStatus`
```json
{ "type": "gitStatus" }
```

#### `gitAdd`
```json
{ "type": "gitAdd", "params": { "files": ["src/main.rs", "Cargo.toml"] } }
```

#### `gitCommit`
```json
{ "type": "gitCommit", "params": { "message": "feat: Add new feature" } }
```

#### `gitPush`
```json
{ 
  "type": "gitPush", 
  "params": { 
    "remote": "origin",  // optional
    "branch": "main"      // optional
  } 
}
```

#### `gitDiff`
```json
{ 
  "type": "gitDiff", 
  "params": { 
    "target": "staged"  // "staged" or omit for unstaged
  } 
}
```

---

### Script Execution πŸ†•

#### `runScript`
Execute custom scripts in supported languages.

**Deno Example** (TypeScript):
```json
{
  "type": "runScript",
  "params": {
    "language": "deno",
    "code": "import { walk } from 'https://deno.land/std/fs/mod.ts';\n\nfor await (const entry of walk('.', { exts: ['.rs'] })) {\n  logger.log(entry.path);\n}",
    "args": [],
    "timeout": 30
  }
}
```

**Python Example**:
```json
{
  "type": "runScript",
  "params": {
    "language": "python",
    "code": "import os\nfor root, dirs, files in os.walk('.'):\n    for f in files:\n        if f.endswith('.py'):\n            print(os.path.join(root, f))",
    "args": [],
    "timeout": 30
  }
}
```

**Bash Example**:
```json
{
  "type": "runScript",
  "params": {
    "language": "bash",
    "code": "#!/bin/bash\nfind . -name '*.log' -type f -delete\necho 'Cleaned up log files'",
    "args": []
  }
}
```

**Response**:
```json
{
  "success": true,
  "content": "STDOUT:\n./src/main.rs\n./src/lib.rs\nSTDERR:\n",
  "error": null
}
```

---

## πŸ”’ Security

### Filesystem Isolation
All file operations are restricted to the project directory. Path traversal attempts (`../../../etc/passwd`) are blocked.

### Script Sandboxing
Scripts executed via `runScript`:
- **Cannot access files outside the project directory**
- **Cannot make network requests** (unless `--enable-network` flag is set)
- **Have a timeout** (default 30 seconds)
- **Run with limited privileges** (no sudo/root)

### Git Operations
Git push requires SSH key authentication configured on the host system.

---

## πŸ§ͺ Testing

### Run Tests
```bash
cargo test
```

### Manual Testing

**Test 1: Smart Build Detection**
```bash
# Create a docs-only project
mkdir -p workspace/docs-test
echo "# Documentation" > workspace/docs-test/README.md

# Send execute request (should skip build)
curl -X POST http://localhost:3000/execute \
  -H "Content-Type: application/json" \
  -d '{"projectId":"docs-test","actions":[]}'

# Expected: buildOutput should be null
```

**Test 2: Git Initialization**
```bash
curl -X POST http://localhost:3000/execute \
  -H "Content-Type: application/json" \
  -d '{"projectId":"new-project","actions":[{"type":"gitInit"}]}'

# Expected: success with "Git repository initialized"
```

**Test 3: Script Execution**
```bash
curl -X POST http://localhost:3000/execute \
  -H "Content-Type: application/json" \
  -d '{
    "projectId":"test-project",
    "actions":[{
      "type":"runScript",
      "params":{
        "language":"python",
        "code":"print(\"Hello from Python!\")",
        "args":[]
      }
    }]
  }'

# Expected: success with stdout containing "Hello from Python!"
```

---

## πŸ› Troubleshooting

### Issue: "Not a git repository" error

**Solution**: Use the `GitInit` action before other git operations.

```json
{ "type": "gitInit" }
```

---

### Issue: "could not find Cargo.toml" on docs project

**Solution**: Set `--build-command none` or use `auto` (default). The system should automatically detect documentation projects and skip builds.

---

### Issue: Script execution timeout

**Solutions**:
1. Optimize your script to run faster
2. Increase timeout: `--script-timeout 60`
3. Break script into smaller chunks

---

### Issue: Script cannot access files

**Check**:
- File paths are relative to project root
- File exists in the project directory
- No path traversal (`../`) attempts

---

## πŸ“Š Logging

Set log level via environment variable:
```bash
RUST_LOG=codebridge_cli=debug ./codebridge-cli
```

Log levels: `error`, `warn`, `info`, `debug`, `trace`

Log output includes:
- Request IDs for tracking
- Action execution details
- Build command detection
- Script execution logs
- Error details with context

---

## πŸ—ΊοΈ Roadmap

### Phase 1 (Current)
- [x] Smart build detection
- [x] Git initialization action
- [x] Script execution (Deno, Python, Bash)

### Phase 2 (Next)
- [ ] Rust script support
- [ ] Docker sandboxing
- [ ] Script library with pre-approved scripts
- [ ] User approval UI for script execution

### Phase 3 (Future)
- [ ] Heartbeat mechanism for multi-task coordination
- [ ] Dependency installation actions
- [ ] Network access controls
- [ ] Resource monitoring and limits

---

## 🀝 Contributing

Contributions are welcome! Please:
1. Fork the repository
2. Create a feature branch
3. Add tests for new features
4. Submit a pull request

### Development Setup
```bash
git clone <repo>
cd codebridge-cli
cargo build
cargo test
```

---

## πŸ“„ License

MIT License - see LICENSE file for details

---

## πŸ™ Acknowledgments

Built with:
- [axum]https://github.com/tokio-rs/axum - Web framework
- [git2]https://github.com/rust-lang/git2-rs - Git operations
- [tokio]https://tokio.rs/ - Async runtime
- [tracing]https://github.com/tokio-rs/tracing - Logging

---

## πŸ“ž Support

For issues and questions:
- GitHub Issues: [link]
- Documentation: [link]
- Discord: [link]