bakit's profile picture
bakit

terminal

Public

411 Downloads

1 star

1 fork

README

๐Ÿš€ LM Studio Terminal Plugin

A powerful terminal plugin for LM Studio with enhanced security, comprehensive system tools, and a unified toolset.


๐Ÿ“ฆ Available Tools

ToolDescription
shell_execExecute a shell command with sanitisation, retry, and configurable timeout
run_processSpawn a process with arguments (no shell interpreter)
file_writeWrite content to a file (creates directories automatically)
read_fileRead file contents
file_diffCompare two files (unified diff format, pure JS LCS/Myers algorithm)
file_checksumCalculate SHA256 or MD5 checksum
file_statsGet file metadata (size, modified, permissions, type)
list_directoryList directory contents with metadata
create_directoryCreate directories (recursive by default)
delete_fileDelete a file or directory (auto-detects type)
delete_directoryDelete a directory and contents
system_infoComprehensive system diagnostics (CPU, memory, uptime, load avg)
list_processesList running processes with PID, CPU%, memory, status
kill_processTerminate a process by PID (validated, no shell injection)
check_networkDNS + TCP connectivity check with latency reporting
get_network_infoGet network interface information
get_disk_infoGet disk/volume information
zip_createCreate a .zip archive (pure JS, no external deps)
zip_extractExtract a .zip archive (pure JS)
zip_listList contents of a .zip archive (pure JS)
replace_textFind and replace text within a file (regex support)
count_textCount occurrences of text or regex patterns in a file
insert_textInsert text at a specific line in a file
delete_linesDelete a range of lines from a file

Total: 22 tools


๐Ÿš€ Quick Start

Installation

cd "C:\code for plugin LM Studio\terminal"
npm install
npm run build

Load in LM Studio

  • Open LM Studio โ†’ Plugins section
  • Add the terminal plugin directory
  • Configure settings as needed (shell path, windows shell, etc.)
  • Start using terminal commands!

๐Ÿ› Development

# 1. Install once
npm install

# 2. Build (tsc โ†’ dist/)
npm run build

# 3. lms dev (watch/restart dev server)
lms dev

Notes:

  • lms ships at ~/.lmstudio/bin/lms.exe. It is not on PATH by default โ€” add it to your shell's PATH (git-bash: persist in ~/.bashrc; PowerShell: add to the User PATH, then reopen the window) for lms dev / npm run dev to resolve.
  • npm run build compiles src/ โ†’ dist/ (21 files). Run npm install first for a reproducible build.

๐Ÿ”’ Security

  • Command sanitisation: Blocks dangerous patterns (rm -rf, del, format, shutdown, reboot, eval, source) โ€” checks both original and lowercased input
  • Path sanitisation: Prevents writing to system directories, blocks .. traversal and null bytes
  • Input validation: All parameters are validated via Zod schemas before execution
  • zip slip prevention: zip_create validates entry names (no .., no absolute paths, no drive letters, max 65535 chars); zip_extract blocks traversal archive members
  • PID injection prevention: kill_process validates PID is a positive integer and uses runProcess with array args (no shell)
  • Host injection prevention: check_network escapes single quotes in hostnames
  • Error logging: All errors are logged via Pino

โš™๏ธ Configuration

SettingDescriptionDefault
defaultShellDefault shellpowershell
defaultTimeoutMsDefault command timeout30,000 ms
maxOutputBytesMaximum output size10 MB
maxConcurrentExecutionsMax concurrent executions4
logLevelLogging levelinfo
maxRetriesRetry attempts on transient errors2
retryDelayMsDelay between retries500 ms

Config is stored at ~/.lmstudio-terminal/config.json.


๐Ÿงช Testing

The project includes 90 tests across 3 test files:

  • security.test.ts โ€” Security validation and sanitisation tests
  • fileUtilities.test.ts โ€” File I/O, zip, diff, and checksum tests
  • executor.test.ts โ€” Command execution and process spawning tests

Run tests with:

npm test

๐Ÿ— Project Structure

terminal/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.ts              # Plugin entry point
โ”‚   โ”œโ”€โ”€ toolsProvider.ts      # All tool definitions (22 tools)
โ”‚   โ”œโ”€โ”€ platform.ts           # detectPlatform, resolvePwshPath
โ”‚   โ”œโ”€โ”€ shell.ts              # resolveShell, buildExecInvocation, isCmdStyleCommand, isAbsolutePathCommand
โ”‚   โ”œโ”€โ”€ executor.ts           # Barrel re-exports from executor_impl
โ”‚   โ”œโ”€โ”€ executor_impl.ts      # execCommand, runProcess, execCommandStream, spawnProcess helper
โ”‚   โ”œโ”€โ”€ fileUtilities.ts      # Barrel re-exports from fileIO, zip, diff, checksum
โ”‚   โ”œโ”€โ”€ fileIO.ts             # readFile, writeFileSync, etc.
โ”‚   โ”œโ”€โ”€ zip.ts                # zipCreate, zipExtract, zipList, crc32
โ”‚   โ”œโ”€โ”€ diff.ts               # diffFiles, diffLcs, diffMyers
โ”‚   โ”œโ”€โ”€ checksum.ts           # fileChecksum
โ”‚   โ”œโ”€โ”€ systemUtilities.ts    # System info, processes, network, disks
โ”‚   โ”œโ”€โ”€ securityEnhanced.ts   # sanitizeFilePath, validateWritePath, sanitizeCommand
โ”‚   โ”œโ”€โ”€ config.ts             # TerminalConfig, loadConfig, saveConfig
โ”‚   โ”œโ”€โ”€ constants.ts          # EXEC_DEFAULT_TIMEOUT_MS, EXEC_MAX_TIMEOUT_MS
โ”‚   โ”œโ”€โ”€ errors.ts             # TerminalError, SecurityError, ValidationError, etc.
โ”‚   โ”œโ”€โ”€ logger.ts             # Pino-based logging
โ”‚   โ”œโ”€โ”€ types.ts              # Shared TypeScript types
โ”‚   โ”œโ”€โ”€ runtimeResolver.ts    # Runtime detection & resolution
โ”‚   โ””โ”€โ”€ tools/
โ”‚       โ”œโ”€โ”€ shell_tools.ts    # shell_exec, run_process
โ”‚       โ”œโ”€โ”€ file_tools.ts     # file_write, read_file, diff, checksum, stats, list, create/delete dir
โ”‚       โ”œโ”€โ”€ system_tools.ts   # system_info, list_processes, kill_process, check_network, get_network_info, get_disk_info
โ”‚       โ”œโ”€โ”€ archive_tools.ts  # zip_create, zip_extract, zip_list
โ”‚       โ””โ”€โ”€ text_tools.ts     # replace_text, count_text, insert_text, delete_lines
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ manifest.json
โ””โ”€โ”€ .gitignore

๐Ÿ“„ License

Apache 2.0