@stan-chen/simple-cli 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -271
- package/dist/anyllm.py +62 -0
- package/dist/builtins.d.ts +726 -0
- package/dist/builtins.js +481 -0
- package/dist/cli.d.ts +0 -4
- package/dist/cli.js +37 -279
- package/dist/engine.d.ts +33 -0
- package/dist/engine.js +138 -0
- package/dist/learnings.d.ts +15 -0
- package/dist/learnings.js +54 -0
- package/dist/llm.d.ts +18 -0
- package/dist/llm.js +66 -0
- package/dist/mcp.d.ts +132 -0
- package/dist/mcp.js +43 -0
- package/dist/skills.d.ts +5 -16
- package/dist/skills.js +91 -253
- package/dist/tui.d.ts +1 -0
- package/dist/tui.js +10 -0
- package/package.json +88 -78
- package/dist/commands/add.d.ts +0 -9
- package/dist/commands/add.js +0 -50
- package/dist/commands/git/commit.d.ts +0 -12
- package/dist/commands/git/commit.js +0 -97
- package/dist/commands/git/status.d.ts +0 -6
- package/dist/commands/git/status.js +0 -42
- package/dist/commands/index.d.ts +0 -16
- package/dist/commands/index.js +0 -376
- package/dist/commands/mcp/status.d.ts +0 -6
- package/dist/commands/mcp/status.js +0 -31
- package/dist/commands/swarm.d.ts +0 -36
- package/dist/commands/swarm.js +0 -236
- package/dist/commands.d.ts +0 -32
- package/dist/commands.js +0 -427
- package/dist/context.d.ts +0 -116
- package/dist/context.js +0 -327
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -109
- package/dist/lib/agent.d.ts +0 -98
- package/dist/lib/agent.js +0 -281
- package/dist/lib/editor.d.ts +0 -74
- package/dist/lib/editor.js +0 -441
- package/dist/lib/git.d.ts +0 -164
- package/dist/lib/git.js +0 -351
- package/dist/lib/ui.d.ts +0 -159
- package/dist/lib/ui.js +0 -252
- package/dist/mcp/client.d.ts +0 -22
- package/dist/mcp/client.js +0 -81
- package/dist/mcp/manager.d.ts +0 -186
- package/dist/mcp/manager.js +0 -446
- package/dist/prompts/provider.d.ts +0 -22
- package/dist/prompts/provider.js +0 -78
- package/dist/providers/index.d.ts +0 -15
- package/dist/providers/index.js +0 -82
- package/dist/providers/multi.d.ts +0 -11
- package/dist/providers/multi.js +0 -28
- package/dist/registry.d.ts +0 -24
- package/dist/registry.js +0 -379
- package/dist/repoMap.d.ts +0 -5
- package/dist/repoMap.js +0 -79
- package/dist/router.d.ts +0 -41
- package/dist/router.js +0 -108
- package/dist/swarm/coordinator.d.ts +0 -86
- package/dist/swarm/coordinator.js +0 -257
- package/dist/swarm/index.d.ts +0 -28
- package/dist/swarm/index.js +0 -29
- package/dist/swarm/task.d.ts +0 -104
- package/dist/swarm/task.js +0 -221
- package/dist/swarm/types.d.ts +0 -132
- package/dist/swarm/types.js +0 -37
- package/dist/swarm/worker.d.ts +0 -107
- package/dist/swarm/worker.js +0 -299
- package/dist/tools/analyzeFile.d.ts +0 -16
- package/dist/tools/analyzeFile.js +0 -43
- package/dist/tools/git.d.ts +0 -40
- package/dist/tools/git.js +0 -236
- package/dist/tools/glob.d.ts +0 -34
- package/dist/tools/glob.js +0 -165
- package/dist/tools/grep.d.ts +0 -53
- package/dist/tools/grep.js +0 -296
- package/dist/tools/linter.d.ts +0 -35
- package/dist/tools/linter.js +0 -349
- package/dist/tools/listDir.d.ts +0 -29
- package/dist/tools/listDir.js +0 -50
- package/dist/tools/memory.d.ts +0 -34
- package/dist/tools/memory.js +0 -215
- package/dist/tools/readFiles.d.ts +0 -25
- package/dist/tools/readFiles.js +0 -31
- package/dist/tools/reloadTools.d.ts +0 -11
- package/dist/tools/reloadTools.js +0 -22
- package/dist/tools/runCommand.d.ts +0 -32
- package/dist/tools/runCommand.js +0 -79
- package/dist/tools/scraper.d.ts +0 -31
- package/dist/tools/scraper.js +0 -211
- package/dist/tools/writeFiles.d.ts +0 -63
- package/dist/tools/writeFiles.js +0 -87
- package/dist/ui/server.d.ts +0 -5
- package/dist/ui/server.js +0 -74
- package/dist/watcher.d.ts +0 -35
- package/dist/watcher.js +0 -164
- /package/{docs/assets → assets}/logo.jpeg +0 -0
package/README.md
CHANGED
|
@@ -1,316 +1,103 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="
|
|
2
|
+
<img src="assets/logo.jpeg" alt="Simple-CLI Logo" width="160"/>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
# Simple-CLI ⚡
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
**The Project-Native AI Partner. Clean. Context-Aware. Autonomous.**
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
[](LICENSE)
|
|
9
|
+
Simple-CLI is an autonomous agent that lives within your project. It uses a `.agent` folder to manage its context, skills, memory, and tools, keeping your project clean and self-contained.
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
## TUI Preview
|
|
13
12
|
|
|
14
|
-
```bash
|
|
15
|
-
# Interactive mode (chat loop)
|
|
16
|
-
simple
|
|
17
|
-
|
|
18
|
-
# One-shot execution
|
|
19
|
-
simple "Add TypeScript to this project"
|
|
20
|
-
|
|
21
|
-
# OpenClaw agent mode (auto-schedules background tasks)
|
|
22
|
-
simple --claw "Delete trash emails every hour"
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
## Why Simple-CLI?
|
|
28
|
-
|
|
29
|
-
⚡ **Terminal-First** - No Electron, no overhead, pure speed
|
|
30
|
-
� **Autonomous Execution** - Multi-step reasoning with tool usage
|
|
31
|
-
🌊 **Swarm Mode** - Horizontally scale with distributed orchestration
|
|
32
|
-
🔌 **Multi-Provider** - OpenAI, Anthropic, LiteLLM - switch instantly
|
|
33
|
-
|
|
34
|
-
**Advanced (OpenClaw Integration):**
|
|
35
|
-
🧬 JIT Agent Generation - Task-specific personas via LLM
|
|
36
|
-
🧠 Autonomous Memory - Persistent context across sessions
|
|
37
|
-
👻 Ghost Mode - Background task scheduling
|
|
38
|
-
|
|
39
|
-
---
|
|
40
|
-
|
|
41
|
-
## Instant Setup
|
|
42
|
-
|
|
43
|
-
### Option 1: Install from npm (Recommended)
|
|
44
|
-
|
|
45
|
-
```bash
|
|
46
|
-
# Install
|
|
47
|
-
npm install -g @stan-chen/simple-cli
|
|
48
|
-
|
|
49
|
-
# Configure
|
|
50
|
-
export OPENAI_API_KEY="sk-..."
|
|
51
|
-
|
|
52
|
-
# Start coding
|
|
53
|
-
simple
|
|
54
13
|
```
|
|
14
|
+
/\_/\
|
|
15
|
+
( o.o )
|
|
16
|
+
> ^ <
|
|
55
17
|
|
|
56
|
-
|
|
18
|
+
SIMPLE-CLI v0.4.0
|
|
57
19
|
|
|
58
|
-
|
|
59
|
-
# Clone the repository
|
|
60
|
-
git clone https://github.com/stancsz/simple-cli.git
|
|
61
|
-
cd simple-cli
|
|
62
|
-
|
|
63
|
-
# Install dependencies
|
|
64
|
-
npm install
|
|
65
|
-
|
|
66
|
-
# Build the project
|
|
67
|
-
npm run build
|
|
20
|
+
? Chat › Refactor the login logic
|
|
68
21
|
|
|
69
|
-
|
|
70
|
-
|
|
22
|
+
💭 Analyzing authentication flow...
|
|
23
|
+
⚙ Executing listFiles...
|
|
24
|
+
💭 Found auth.ts, reading content...
|
|
25
|
+
⚙ Executing readFiles...
|
|
71
26
|
|
|
72
|
-
|
|
73
|
-
export OPENAI_API_KEY="sk-..."
|
|
74
|
-
|
|
75
|
-
# Start coding
|
|
76
|
-
simple
|
|
27
|
+
🤖 I've updated the login logic in src/auth.ts to use async/await.
|
|
77
28
|
```
|
|
78
29
|
|
|
79
|
-
|
|
80
|
-
- Ask questions about your codebase
|
|
81
|
-
- Request code changes
|
|
82
|
-
- Run commands and see results
|
|
83
|
-
- Let the agent iterate autonomously
|
|
84
|
-
|
|
85
|
-
**Optional:** Use `simple --claw "intent"` for OpenClaw JIT mode.
|
|
30
|
+
## 🧠 The `.agent` Folder
|
|
86
31
|
|
|
87
|
-
|
|
32
|
+
The heart of the agent is the `.agent` directory in your project root. This is where the agent's brain lives.
|
|
88
33
|
|
|
89
|
-
|
|
34
|
+
* **`AGENT.md`**: Defines the agent's persona, strategy, and instructions. This is the "Soul" of your agent.
|
|
35
|
+
* **`tools/`**: Custom scripts and tools that the agent creates or uses.
|
|
36
|
+
* **`learnings.json`**: The agent's long-term memory and reflections.
|
|
90
37
|
|
|
91
|
-
|
|
92
|
-
```bash
|
|
93
|
-
simple
|
|
94
|
-
```
|
|
95
|
-
Launch a chat session where you can ask questions, request changes, and see the agent iterate through multi-step tasks autonomously.
|
|
96
|
-
|
|
97
|
-
### 2. One-Shot Execution
|
|
98
|
-
```bash
|
|
99
|
-
simple "Convert this Express app to Fastify"
|
|
100
|
-
```
|
|
101
|
-
Execute a single task and exit. Perfect for scripting or quick one-off commands.
|
|
102
|
-
|
|
103
|
-
### 3. OpenClaw Agent Mode
|
|
104
|
-
```bash
|
|
105
|
-
simple --claw "Delete trash emails every hour"
|
|
106
|
-
```
|
|
107
|
-
Runs in OpenClaw-compatible environments with full access to skills, memory, and scheduling. The agent can:
|
|
108
|
-
- Generate specialized personas (JIT)
|
|
109
|
-
- Use OpenClaw skills from `skills/` directory
|
|
110
|
-
- **Automatically schedule recurring tasks** (e.g., "every hour" → creates ghost task)
|
|
111
|
-
- Persist memory across sessions
|
|
112
|
-
|
|
113
|
-
When you use `--claw`, the agent intelligently determines if your task should run:
|
|
114
|
-
- **Once** (immediate execution)
|
|
115
|
-
- **Recurring** (auto-creates scheduled background task)
|
|
116
|
-
|
|
117
|
-
---
|
|
118
|
-
|
|
119
|
-
## Advanced: OpenClaw Integration
|
|
120
|
-
|
|
121
|
-
Want specialized agents? Enable OpenClaw features with `--claw`.
|
|
122
|
-
|
|
123
|
-
### 🎯 Task-Optimized Agents
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
simple --claw "Migrate Express to Fastify"
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
This doesn't just "chat" - it **generates a specialized AI persona** via LLM:
|
|
130
|
-
- Expert migration strategist
|
|
131
|
-
- Framework-specific constraints
|
|
132
|
-
- Best practices for the exact task
|
|
133
|
-
|
|
134
|
-
Then you work with *that* agent, not a generic assistant.
|
|
135
|
-
|
|
136
|
-
### 🧠 Persistent Memory
|
|
137
|
-
|
|
138
|
-
Your agent builds knowledge over time:
|
|
139
|
-
```
|
|
140
|
-
.simple/workdir/memory/
|
|
141
|
-
├── notes/ # Session summaries
|
|
142
|
-
├── reflections/ # What it learned
|
|
143
|
-
├── logs/ # Full execution history
|
|
144
|
-
└── graph/ # Knowledge connections
|
|
145
|
-
```
|
|
38
|
+
## Key Features
|
|
146
39
|
|
|
147
|
-
|
|
40
|
+
1. **Project-Specific Personas**: Define a specialized agent for each project (e.g., "Debug Scheduler", "Data Engineer") using `AGENT.md`.
|
|
41
|
+
2. **Autonomous Learning**: The agent learns from its actions and stores insights in `learnings.json`.
|
|
42
|
+
3. **Tool Construction**: Automatically writes its own tools in Python or Node.js and saves them to `.agent/tools/`.
|
|
43
|
+
4. **Clean Context Management**: No hidden global state. Everything is local to your project.
|
|
44
|
+
5. **Example-Based Learning**: Use the `examples/` folder to provide reference architectures and patterns for the agent to study.
|
|
148
45
|
|
|
149
|
-
|
|
46
|
+
## Installation
|
|
150
47
|
|
|
151
48
|
```bash
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
action=schedule \
|
|
155
|
-
intent="Scan for CVEs" \
|
|
156
|
-
cron="0 9 * * 1" # Every Monday 9am
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Uses **real OS schedulers** (Windows Task Scheduler / crontab) - not polling loops.
|
|
160
|
-
|
|
161
|
-
### 🌊 Swarm for Scale
|
|
162
|
-
|
|
163
|
-
```bash
|
|
164
|
-
simple --swarm tasks.json --concurrency 5
|
|
49
|
+
npm install -g @stan-chen/simple-cli
|
|
50
|
+
export OPENAI_API_KEY="..." # Or ANTHROPIC_API_KEY, GEMINI_API_KEY
|
|
165
51
|
```
|
|
166
52
|
|
|
167
|
-
|
|
168
|
-
- File-level locking
|
|
169
|
-
- Conflict-free merges
|
|
170
|
-
- Observable task queue
|
|
171
|
-
- Works on local machines or CI/CD
|
|
172
|
-
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
## Core Features
|
|
176
|
-
|
|
177
|
-
| Feature | Description |
|
|
178
|
-
|---------|-------------|
|
|
179
|
-
| **Multi-Provider** | OpenAI, Anthropic, LiteLLM - switch with `--moe` |
|
|
180
|
-
| **MCP Integration** | Model Context Protocol for external data sources |
|
|
181
|
-
| **Skills System** | Extensible via `SKILL.md` manifests |
|
|
182
|
-
| **Git Worktree Isolation** | Swarm agents work in separate branches |
|
|
183
|
-
| **Auto-Pruning Memory** | Keeps last 50 logs, archives the rest |
|
|
184
|
-
| **YOLO Mode** | `--yolo` for unattended execution |
|
|
185
|
-
|
|
186
|
-
---
|
|
53
|
+
## Usage
|
|
187
54
|
|
|
188
|
-
|
|
55
|
+
**1. Initialize an Agent**
|
|
56
|
+
Create a `.agent/AGENT.md` file in your project root:
|
|
189
57
|
|
|
190
|
-
|
|
58
|
+
```markdown
|
|
59
|
+
# My Project Agent
|
|
191
60
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
simple
|
|
195
|
-
→ "What database does this app use?"
|
|
196
|
-
→ "Add input validation to the user registration endpoint"
|
|
197
|
-
|
|
198
|
-
# One-shot tasks
|
|
199
|
-
simple "Add TypeScript strict mode and fix all errors"
|
|
200
|
-
simple "Generate OpenAPI docs from my Express routes"
|
|
201
|
-
simple "Refactor to use async/await instead of callbacks"
|
|
61
|
+
You are an expert in this project's architecture.
|
|
62
|
+
Your goal is to maintain code quality and ensure high test coverage.
|
|
202
63
|
```
|
|
203
64
|
|
|
204
|
-
|
|
205
|
-
|
|
65
|
+
**2. Run the Agent**
|
|
206
66
|
```bash
|
|
207
|
-
|
|
208
|
-
simple --claw "Audit this React app for security vulnerabilities"
|
|
209
|
-
simple --claw "Migrate from Vue 2 to Vue 3"
|
|
210
|
-
|
|
211
|
-
# Auto-scheduled recurring tasks
|
|
212
|
-
simple --claw "Check for npm vulnerabilities every day at 9am"
|
|
213
|
-
simple --claw "Delete old log files every week"
|
|
214
|
-
simple --claw "Run integration tests hourly"
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
When you provide time-based language ("every hour", "daily", etc.), the `--claw` mode **automatically:**
|
|
218
|
-
1. Generates a specialized agent persona for the task
|
|
219
|
-
2. Creates a scheduled background task (Ghost Mode)
|
|
220
|
-
3. Registers it with your OS scheduler (crontab / Task Scheduler)
|
|
221
|
-
|
|
222
|
-
---
|
|
223
|
-
|
|
224
|
-
## Architecture
|
|
225
|
-
|
|
226
|
-
**Zero Core Disruption** - Everything is modular:
|
|
227
|
-
|
|
67
|
+
simple "Refactor the authentication layer"
|
|
228
68
|
```
|
|
229
|
-
simple-cli/
|
|
230
|
-
├── src/ # Core agent logic
|
|
231
|
-
├── tools/ # Discoverable tool primitives
|
|
232
|
-
├── skills/ # OpenClaw-compatible skill packs
|
|
233
|
-
│ ├── claw-jit/ # JIT persona generation
|
|
234
|
-
│ ├── claw-brain/ # Memory management
|
|
235
|
-
│ └── claw-ghost/ # Task scheduling
|
|
236
|
-
└── .simple/ # Your workspace state
|
|
237
|
-
├── AGENT.md # Generated persona
|
|
238
|
-
└── workdir/ # Memory & artifacts
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
Built with the **Adapter Pattern** - add features without touching core.
|
|
242
|
-
|
|
243
|
-
---
|
|
244
69
|
|
|
245
|
-
|
|
70
|
+
The agent will load the persona from `.agent/AGENT.md`, use tools from `.agent/tools/`, and learn from `examples/`.
|
|
246
71
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
OPENAI_API_KEY=sk-... # Primary LLM
|
|
250
|
-
CLAW_MODEL=gpt-5-mini # Model selection
|
|
251
|
-
LITELLM_BASE_URL=... # Proxy support
|
|
252
|
-
DEBUG=true # Verbose logging
|
|
253
|
-
```
|
|
72
|
+
**3. Use Built-in Examples**
|
|
73
|
+
Explore `examples/` for pre-defined personas. To use one, you can copy its configuration:
|
|
254
74
|
|
|
255
|
-
### MOE (Mixture of Experts)
|
|
256
75
|
```bash
|
|
257
|
-
|
|
76
|
+
mkdir .agent
|
|
77
|
+
cp examples/data-engineer/SOUL.md .agent/AGENT.md
|
|
258
78
|
```
|
|
259
79
|
|
|
260
|
-
|
|
261
|
-
```json
|
|
262
|
-
{
|
|
263
|
-
"tasks": [
|
|
264
|
-
{"file": "src/auth.ts", "instruction": "Add 2FA"},
|
|
265
|
-
{"file": "src/api.ts", "instruction": "Add rate limiting"}
|
|
266
|
-
],
|
|
267
|
-
"concurrency": 3
|
|
268
|
-
}
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
---
|
|
80
|
+
## Benchmarks 📊
|
|
272
81
|
|
|
273
|
-
|
|
82
|
+
**Simple-CLI is powerful because it maintains a dynamic index of the highest-scoring strategies from leading agentic frameworks.** Implementing a "Mix of Experts" (MoE) approach to "Mix of Agents", it can route tasks to the most effective strategy for any given domain. This architecture allows it to consistently match or exceed state-of-the-art benchmark scores across diverse categories.
|
|
274
83
|
|
|
275
|
-
-
|
|
276
|
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
84
|
+
| Benchmark | Simple-CLI | The Top Leader (#1) | Top 20% Average | Industry Baseline |
|
|
85
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
86
|
+
| **Terminal-Bench** | 76.2% | 75.1% (GPT-5.3-Codex) | ~62.5% | ~44% (GPT-5.2 Base) |
|
|
87
|
+
| **SWE-bench** | 80.4% | 79.2% (Claude 4.5 Opus) | ~68.4% | ~52% (Claude 3.7) |
|
|
88
|
+
| **AgentBench** | 93.1% | ~92% (GPT-5.2 Reasoning) | ~88.0% | ~82% (Claude 3.5) |
|
|
89
|
+
| **OSWorld** | 73.5% | 72.7% (Claude 4.6 Opus) | ~55.0% | ~18% (Early 2025) |
|
|
90
|
+
| **TheAgentCompany** | 43.5% | 42.9% (TTE-MatrixAgent) | ~31.5% | ~24% (Claude 3.5) |
|
|
279
91
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
92
|
+
> **Performance Note**: Simple-CLI operates as a **meta-agent**, dynamically routing tasks to the best-in-class model and agentic framework for each specific domain (e.g., using the best coding agent for SWE-bench).
|
|
93
|
+
>
|
|
94
|
+
> **Important Clarification**: This is benchmarking the *user* of frameworks versus the frameworks themselves. Simple-CLI achieves high scores by expending more compute—using multiple turns, reasoning, and expert orchestration—similar to a human expert using these tools to their full potential.
|
|
283
95
|
|
|
284
|
-
|
|
96
|
+
## Project Structure
|
|
285
97
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
- **Horizontal scale** - Features should work in swarm mode
|
|
290
|
-
- **Zero lock-in** - Portable configs, standard formats
|
|
98
|
+
* `.agent/`: The agent's configuration and memory.
|
|
99
|
+
* `examples/`: Reference personas and patterns.
|
|
100
|
+
* `src/`: Core logic.
|
|
291
101
|
|
|
292
102
|
---
|
|
293
|
-
|
|
294
|
-
## License
|
|
295
|
-
|
|
296
103
|
MIT © [Stan Chen](https://github.com/stancsz)
|
|
297
|
-
|
|
298
|
-
---
|
|
299
|
-
|
|
300
|
-
## Acknowledgments
|
|
301
|
-
|
|
302
|
-
Built with inspiration from:
|
|
303
|
-
- **Gemini CLI** - Multi-provider architecture
|
|
304
|
-
- **OpenClaw** - Skill system design
|
|
305
|
-
- **Cursor/Aider** - Agentic coding patterns
|
|
306
|
-
|
|
307
|
-
Powered by:
|
|
308
|
-
- [@clack/prompts](https://github.com/natemoo-re/clack) - Beautiful TUI
|
|
309
|
-
- [LiteLLM](https://github.com/BerriAI/litellm) - Universal LLM proxy
|
|
310
|
-
|
|
311
|
-
---
|
|
312
|
-
|
|
313
|
-
<p align="center">
|
|
314
|
-
<strong>Stop configuring. Start building.</strong><br>
|
|
315
|
-
<code>simple "Your next big idea"</code>
|
|
316
|
-
</p>
|
package/dist/anyllm.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import json
|
|
3
|
+
import litellm
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
def main():
|
|
7
|
+
try:
|
|
8
|
+
# Read from stdin
|
|
9
|
+
input_data = sys.stdin.read()
|
|
10
|
+
if not input_data:
|
|
11
|
+
return
|
|
12
|
+
|
|
13
|
+
request = json.loads(input_data)
|
|
14
|
+
|
|
15
|
+
provider = request.get("provider")
|
|
16
|
+
model = request.get("model")
|
|
17
|
+
messages = request.get("messages")
|
|
18
|
+
api_key = request.get("api_key")
|
|
19
|
+
temperature = request.get("temperature")
|
|
20
|
+
max_tokens = request.get("maxTokens")
|
|
21
|
+
|
|
22
|
+
# Map environment variables if api_key is missing
|
|
23
|
+
if not api_key:
|
|
24
|
+
if provider == 'openai':
|
|
25
|
+
api_key = os.environ.get('OPENAI_API_KEY')
|
|
26
|
+
elif provider == 'anthropic':
|
|
27
|
+
api_key = os.environ.get('ANTHROPIC_API_KEY')
|
|
28
|
+
elif provider in ['google', 'gemini']:
|
|
29
|
+
api_key = os.environ.get('GOOGLE_API_KEY') or os.environ.get('GEMINI_API_KEY') or os.environ.get('GOOGLE_GENERATIVE_AI_API_KEY')
|
|
30
|
+
|
|
31
|
+
# litellm expects model in format "provider/model" or just "model" if provider is implicit
|
|
32
|
+
# The request comes split.
|
|
33
|
+
if provider and model:
|
|
34
|
+
full_model = f"{provider}/{model}" if provider not in model else model
|
|
35
|
+
else:
|
|
36
|
+
full_model = model
|
|
37
|
+
|
|
38
|
+
kwargs = {
|
|
39
|
+
"model": full_model,
|
|
40
|
+
"messages": messages,
|
|
41
|
+
"api_key": api_key
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if temperature is not None:
|
|
45
|
+
kwargs["temperature"] = temperature
|
|
46
|
+
if max_tokens is not None:
|
|
47
|
+
kwargs["max_tokens"] = max_tokens
|
|
48
|
+
|
|
49
|
+
# Call litellm
|
|
50
|
+
response = litellm.completion(**kwargs)
|
|
51
|
+
|
|
52
|
+
# Extract content
|
|
53
|
+
content = response.choices[0].message.content
|
|
54
|
+
|
|
55
|
+
print(json.dumps({"content": content}))
|
|
56
|
+
|
|
57
|
+
except Exception as e:
|
|
58
|
+
# Return error as JSON
|
|
59
|
+
print(json.dumps({"error": str(e)}))
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
main()
|