@iflow-ai/iflow-cli-sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +401 -0
- package/README_CN.md +401 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +966 -0
- package/dist/index.esm.js +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present iFlow SDK Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# iFlow CLI TypeScript SDK
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](docs/protocol.md)
|
|
5
|
+
|
|
6
|
+
[English](README.md) | [中文](README_CN.md)
|
|
7
|
+
|
|
8
|
+
A powerful TypeScript SDK for interacting with iFlow CLI using the Agent Communication Protocol (ACP). Build AI-powered applications with full control over conversations, tool execution, and SubAgent orchestration.
|
|
9
|
+
|
|
10
|
+
**✨ Key Feature: The SDK automatically manages the iFlow process - no manual setup required!**
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- 🚀 **Automatic Process Management** - Zero-config setup! SDK auto-starts and manages iFlow CLI
|
|
15
|
+
- 🔌 **Smart Port Detection** - Automatically finds available ports, no conflicts
|
|
16
|
+
- 🔄 **Bidirectional Communication** - Real-time streaming of messages and responses
|
|
17
|
+
- 🛠️ **Tool Call Management** - Handle and control tool executions with fine-grained permissions
|
|
18
|
+
- 🤖 **SubAgent Support** - Track and manage multiple AI agents with `agentId` propagation
|
|
19
|
+
- 📋 **Task Planning** - Receive and process structured task plans
|
|
20
|
+
- 🔍 **Raw Data Access** - Debug and inspect protocol-level messages
|
|
21
|
+
- ⚡ **Async/Await Support** - Modern asynchronous Python with full type hints
|
|
22
|
+
- 🎯 **Simple & Advanced APIs** - From one-line queries to complex conversation management
|
|
23
|
+
- 📦 **Full ACP v1 Protocol** - Complete implementation of the Agent Communication Protocol
|
|
24
|
+
- 🚦 **Advanced Approval Modes** - Including `default`, `autoEdit`, `yolo`, and `plan` modes
|
|
25
|
+
- 🔗 **MCP Server Integration** - Support for Model Context Protocol servers
|
|
26
|
+
- 🪝 **Lifecycle Hooks** - Execute commands at different stages of conversation
|
|
27
|
+
- 🎮 **Session Settings** - Fine-grained control over model behavior and tools
|
|
28
|
+
- 🤖 **Custom Agents** - Define specialized agents with custom prompts and tools
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
### 1. Install iFlow CLI
|
|
33
|
+
|
|
34
|
+
If you haven't installed iFlow CLI yet:
|
|
35
|
+
|
|
36
|
+
**Mac/Linux/Ubuntu:**
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bash -c "$(curl -fsSL https://cloud.iflow.cn/iflow-cli/install.sh)"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Windows:**
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install -g @iflow-ai/iflow-cli@latest
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. Install the SDK
|
|
49
|
+
|
|
50
|
+
**Install from NPM:**
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install --save @iflow-ai/iflow-cli-sdk
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Quick Start
|
|
57
|
+
|
|
58
|
+
The SDK **automatically manages the iFlow process** - no manual setup required!
|
|
59
|
+
|
|
60
|
+
### Default Usage (Automatic Process Management)
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { IFlowClient } from "@iflow-ai/iflow-cli-sdk";
|
|
64
|
+
|
|
65
|
+
async function main() {
|
|
66
|
+
// SDK automatically:
|
|
67
|
+
// 1. Detects if iFlow is installed
|
|
68
|
+
// 2. Starts iFlow process if not running
|
|
69
|
+
// 3. Finds an available port
|
|
70
|
+
// 4. Cleans up on exit
|
|
71
|
+
const client = new IFlowClient();
|
|
72
|
+
|
|
73
|
+
await client.connect();
|
|
74
|
+
await client.sendMessage("Hello, iFlow!");
|
|
75
|
+
|
|
76
|
+
for await (const message in client.receiveMessages()) {
|
|
77
|
+
console.log(message);
|
|
78
|
+
// Process messages...
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**No need to manually start iFlow!** The SDK handles everything for you.
|
|
84
|
+
|
|
85
|
+
### Advanced: Manual Process Control
|
|
86
|
+
|
|
87
|
+
If you need to manage iFlow yourself (rare cases):
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { IFlowClient } from "@iflow-ai/iflow-cli-sdk";
|
|
91
|
+
|
|
92
|
+
async function main() {
|
|
93
|
+
// Disable automatic process management
|
|
94
|
+
const client = new IFlowClient({
|
|
95
|
+
url: "ws://localhost:8090/acp", // Connect to existing iFlow
|
|
96
|
+
autoStartProcess: false,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
await client.connect();
|
|
100
|
+
await client.sendMessage("Hello, iFlow!");
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Note:** Manual mode requires you to start iFlow separately:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
iflow --experimental-acp --port 8090
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Simple Examples
|
|
111
|
+
|
|
112
|
+
#### Simple Query
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { query } from "@iflow-ai/iflow-cli-sdk";
|
|
116
|
+
|
|
117
|
+
async function main() {
|
|
118
|
+
const response = await query("What is the capital of France?");
|
|
119
|
+
console.log(response); // "The capital of France is Paris."
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Interactive Conversation
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { IFlowClient, MessageType } from "@iflow-ai/iflow-cli-sdk";
|
|
127
|
+
|
|
128
|
+
async function main() {
|
|
129
|
+
const client = new IFlowClient();
|
|
130
|
+
|
|
131
|
+
await client.connect();
|
|
132
|
+
await client.sendMessage("Explain quantum computing");
|
|
133
|
+
|
|
134
|
+
for await (const message in client.receiveMessages()) {
|
|
135
|
+
if (message.type === MessageType.ASSISTANT && message.chunk.text) {
|
|
136
|
+
console.log(message.chunk.text);
|
|
137
|
+
} else if (message.type === MessageType.TASK_FINISH) {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### Tool Call Control with Agent Information
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { IFlowClient, PermissionMode, MessageType } from "@iflow-ai/iflow-cli-sdk";
|
|
148
|
+
|
|
149
|
+
async function main() {
|
|
150
|
+
const client = new IFlowClient({
|
|
151
|
+
permissionMode: PermissionMode.AUTO,
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
await client.connect();
|
|
155
|
+
await client.sendMessage("Create a file called test.txt");
|
|
156
|
+
|
|
157
|
+
for await (const message in client.receiveMessages()) {
|
|
158
|
+
if (message.type === MessageType.TOOL_CALL) {
|
|
159
|
+
console.log(`Tool name: ${message.toolName}`);
|
|
160
|
+
console.log(`Tool status: ${message.status}`);
|
|
161
|
+
|
|
162
|
+
if (message.agentInfo) {
|
|
163
|
+
console.log(`Agent ID: ${message.agentInfo.agentId}`);
|
|
164
|
+
console.log(`Task ID: ${message.agentInfo.taskId}`);
|
|
165
|
+
console.log(`Agent index: ${message.agentInfo.agentIndex}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (message.args) {
|
|
169
|
+
console.log(message.args);
|
|
170
|
+
}
|
|
171
|
+
if (message.output) {
|
|
172
|
+
console.log(message.output);
|
|
173
|
+
}
|
|
174
|
+
} else if (message.type === MessageType.TASK_FINISH) {
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Working with AgentInfo
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { IFlowClient, MessageType } from "@iflow-ai/iflow-cli-sdk";
|
|
185
|
+
|
|
186
|
+
const options: IFlowOptions = {
|
|
187
|
+
agents: [
|
|
188
|
+
{
|
|
189
|
+
agentType: "code-reviewer",
|
|
190
|
+
name: "reviewer",
|
|
191
|
+
description: "Code review specialist",
|
|
192
|
+
whenToUse: "For code review and quality checks",
|
|
193
|
+
allowedTools: ["fs", "grep"],
|
|
194
|
+
allowedMcps: ["eslint", "prettier"],
|
|
195
|
+
systemPrompt: "You are a code review expert.",
|
|
196
|
+
proactive: False,
|
|
197
|
+
location: "project",
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
agentType: "test-writer",
|
|
201
|
+
name: "tester",
|
|
202
|
+
description: "Test writing specialist",
|
|
203
|
+
whenToUse: "For writing unit and integration tests",
|
|
204
|
+
allowedTools: ["fs", "bash"],
|
|
205
|
+
systemPrompt: "You are a test writing expert.",
|
|
206
|
+
location: "project",
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
async function main() {
|
|
212
|
+
const client = new IFlowClient(options)
|
|
213
|
+
|
|
214
|
+
await client.connect();
|
|
215
|
+
await client.sendMessage("$test-writer Write a unit test");
|
|
216
|
+
|
|
217
|
+
for await (const message in client.receiveMessages()) {
|
|
218
|
+
if (message.type === MessageType.TOOL_CALL) {
|
|
219
|
+
console.log(`Tool name: ${message.toolName}`);
|
|
220
|
+
|
|
221
|
+
if (message.args) {
|
|
222
|
+
console.log(message.args);
|
|
223
|
+
}
|
|
224
|
+
if (message.output) {
|
|
225
|
+
console.log(message.output);
|
|
226
|
+
}
|
|
227
|
+
} if (message.type === MessageType.ASSISTANT && message.chunk.text) {
|
|
228
|
+
console.log(message.chunk.text);
|
|
229
|
+
} else if (message.type === MessageType.TASK_FINISH) {
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### Advanced Protocol Features
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import { IFlowClient, IFlowOptions, ApprovalMode, HookEventType } from "@iflow-ai/iflow-cli-sdk";
|
|
240
|
+
|
|
241
|
+
const options: IFlowOptions = {
|
|
242
|
+
mcpServers: [
|
|
243
|
+
{
|
|
244
|
+
name: "filesystem",
|
|
245
|
+
command: "mcp-server-filesystem",
|
|
246
|
+
args: ["--allowed-dirs", "/workspace"],
|
|
247
|
+
env: [
|
|
248
|
+
{
|
|
249
|
+
name: "DEBUG",
|
|
250
|
+
value: "1",
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
|
|
256
|
+
sessionSettings: {
|
|
257
|
+
allowed_tools: ["read_file", "write_file", "execute_code"],
|
|
258
|
+
system_prompt: "You are an expert Python developer",
|
|
259
|
+
permission_mode: ApprovalMode.AUTO_EDIT,
|
|
260
|
+
max_turns: 100,
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
hooks: {
|
|
264
|
+
[HookEventType.PRE_TOOL_USE]: [
|
|
265
|
+
{
|
|
266
|
+
hooks: {
|
|
267
|
+
command: "echo 'Processing request...'",
|
|
268
|
+
timeout: 5,
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
commands: [
|
|
275
|
+
{
|
|
276
|
+
name: "test",
|
|
277
|
+
content: "pytest --verbose",
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
|
|
281
|
+
agents: [
|
|
282
|
+
{
|
|
283
|
+
agentType: "python-expert",
|
|
284
|
+
whenToUse: "For Python development tasks",
|
|
285
|
+
allowedTools: ["edit_file", "run_python", "debug"],
|
|
286
|
+
systemPrompt: "You are a Python expert focused on clean, efficient code",
|
|
287
|
+
name: "Python Expert",
|
|
288
|
+
description: "Specialized in Python development",
|
|
289
|
+
},
|
|
290
|
+
],
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
async function main() {
|
|
294
|
+
const client = new IFlowClient(options);
|
|
295
|
+
|
|
296
|
+
await client.connect();
|
|
297
|
+
await client.sendMessage("$test-writer Write a unit test");
|
|
298
|
+
|
|
299
|
+
// Process responses...
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## API Reference
|
|
304
|
+
|
|
305
|
+
### Core Classes
|
|
306
|
+
|
|
307
|
+
- **`IFlowClient`**: Main client for bidirectional communication
|
|
308
|
+
- **`IFlowOptions`**: Configuration options
|
|
309
|
+
- **`RawDataClient`**: Access to raw protocol data
|
|
310
|
+
|
|
311
|
+
### Message Types
|
|
312
|
+
|
|
313
|
+
- **`AssistantMessage`**: AI assistant responses with optional agent information
|
|
314
|
+
- **`ToolCallMessage`**: Tool execution requests with execution details (toolName, args, output) and agent information
|
|
315
|
+
- **`PlanMessage`**: Structured task plans with priority and status
|
|
316
|
+
- **`TaskFinishMessage`**: Task completion signal with stop reason (end_urn, max_tokens, refusal, cancelled)
|
|
317
|
+
|
|
318
|
+
### Agent Information
|
|
319
|
+
|
|
320
|
+
- **`AgentInfo`**: Agent metadata extracted from iFlow's agentId format (agentId, taskId, agentIndex, timestamp)
|
|
321
|
+
|
|
322
|
+
### Convenience Functions
|
|
323
|
+
|
|
324
|
+
- `query(prompt)`: Simple synchronous query
|
|
325
|
+
- `querySync(prompt)`: Synchronous with timeout
|
|
326
|
+
- `queryStream(prompt)`: Streaming responses
|
|
327
|
+
|
|
328
|
+
## Project Structure
|
|
329
|
+
|
|
330
|
+
```
|
|
331
|
+
src/
|
|
332
|
+
├── internals/
|
|
333
|
+
│ ├── FileHandler.ts # File access
|
|
334
|
+
│ ├── ProcessManager.ts # iFlow process management
|
|
335
|
+
│ ├── Protocol.ts # ACP protocol handler
|
|
336
|
+
│ └── Transport.ts # WebSocket transport layer
|
|
337
|
+
├── types/
|
|
338
|
+
│ ├── acp.ts # ACP data types
|
|
339
|
+
│ ├── messages.ts # Message types
|
|
340
|
+
│ └── options.ts # iFlow options
|
|
341
|
+
├── utils/
|
|
342
|
+
│ ├── logger.ts # Logs util
|
|
343
|
+
│ └── parseAgentId.ts # Parse agent id
|
|
344
|
+
├── IFlowClient.ts # Main IFlowClient implementation
|
|
345
|
+
├── RawDataClient.ts # Raw protocol access
|
|
346
|
+
├── query.ts # Simple query functions
|
|
347
|
+
└── index.ts # Main entry
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Development
|
|
351
|
+
|
|
352
|
+
### Running Tests
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
npm test
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### Code Quality
|
|
359
|
+
|
|
360
|
+
```bash
|
|
361
|
+
# Lint code
|
|
362
|
+
npm run lint
|
|
363
|
+
|
|
364
|
+
# Format code
|
|
365
|
+
npm run format
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
## Protocol Support
|
|
369
|
+
|
|
370
|
+
The SDK implements the Agent Communication Protocol (ACP) v1 with full extension support, including:
|
|
371
|
+
|
|
372
|
+
- **Session Management**: Create, load, and manage conversation sessions with advanced settings
|
|
373
|
+
- **Message Types**:
|
|
374
|
+
- `agent_message_chunk` - Assistant responses
|
|
375
|
+
- `agent_thought_chunk` - Internal reasoning
|
|
376
|
+
- `tool_call` / `tool_call_update` - Tool execution lifecycle
|
|
377
|
+
- `plan` - Structured task planning with priorities
|
|
378
|
+
- `user_message_chunk` - User message echoing
|
|
379
|
+
- `stop_reason` - Task completion with reason (end_turn, max_tokens, refusal, cancelled)
|
|
380
|
+
- **Authentication**: Built-in iFlow authentication with token support
|
|
381
|
+
- **File System Access**: Read/write file permissions with configurable limits
|
|
382
|
+
- **SubAgent Support**: Full `agentId` tracking and management
|
|
383
|
+
- **Advanced Features**:
|
|
384
|
+
- **MCP Servers**: Integrate Model Context Protocol servers for extended capabilities
|
|
385
|
+
- **Approval Modes**: DEFAULT, AUTO_EDIT, YOLO (auto-approve all), PLAN modes
|
|
386
|
+
- **Session Settings**: Control allowed tools, system prompts, model selection
|
|
387
|
+
- **Lifecycle Hooks**: Execute commands at different conversation stages
|
|
388
|
+
- **Custom Commands**: Define and execute custom commands
|
|
389
|
+
- **Specialized Agents**: Create agents with specific expertise and tool access
|
|
390
|
+
|
|
391
|
+
## Contributing
|
|
392
|
+
|
|
393
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
394
|
+
|
|
395
|
+
## License
|
|
396
|
+
|
|
397
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
398
|
+
|
|
399
|
+
---
|
|
400
|
+
|
|
401
|
+
Built with ❤️ for the AI development community
|