代理OS 的 CLI 执行器扩展
执行 shell 命令,运行脚本,并为 AgentOS 代理管理文件. 这是两种基本原始(与Web浏览器相通)之一,能够使自建递归性代理能力.
特征
- 执行壳牌: 运行输出抓取的任意 shell 命令
- 文件管理: 读取、撰写和列出文件/目录
- 文件一/O: 读取并写入 xlsx, csv, docx, 和 pdf 文件
- 安保管制: 危险命令检测和屏蔽
- 横线: 在 Windows、 macOS 和 Linux 上工作
安装
npm install @framers/agentos-ext-cli-executor
快速启动
import { ExtensionManager } from '@framers/agentos';
import { createExtensionPack } from '@framers/agentos-ext-cli-executor';
const extensionManager = new ExtensionManager();
// Load the pack into the runtime
await extensionManager.loadPackFromFactory(
createExtensionPack({
options: {
defaultShell: 'bash',
timeout: 60000,
blockedCommands: ['rm -rf /', 'format'],
// Restrict file_* tools to a per-agent workspace
filesystem: { allowRead: true, allowWrite: true },
agentWorkspace: { agentId: 'my-agent' },
},
logger: console,
}),
'@framers/agentos-ext-cli-executor',
);
工具
shell 执行
执行 shell 命令 .
const result = await gmi.executeTool('shell_execute', {
command: 'npm install lodash',
cwd: '/path/to/project',
timeout: 30000
});
// Returns: { command, exitCode, stdout, stderr, duration, success }
文件读取
读取文件内容 .
const result = await gmi.executeTool('file_read', {
path: './package.json',
encoding: 'utf-8'
});
// Returns: { path, content, size, truncated, encoding }
// Read last 50 lines
const logs = await gmi.executeTool('file_read', {
path: './app.log',
lines: 50,
fromEnd: true
});
文件写入
将内容写入文件 .
const result = await gmi.executeTool('file_write', {
path: './config.json',
content: JSON.stringify({ key: 'value' }),
createDirs: true
});
// Returns: { path, bytesWritten, created, appended }
列表( d)
列表目录内容 .
const result = await gmi.executeTool('list_directory', {
path: './src',
recursive: true,
pattern: '*.ts',
includeStats: true
});
// Returns: { path, entries: [{ name, path, type, size, ... }], count }
已读文档
从二进制文档格式(xlsx,xls,csv,tsv,docx,pdf)中读取文本.
// Read an Excel spreadsheet
const result = await gmi.executeTool('read_document', {
path: './data/report.xlsx',
sheet: 'Q1 Revenue', // optional: specific sheet
maxRows: 200 // optional: limit rows (default: 500)
});
// Returns: { path, format, size, content (markdown tables), structured: { sheets }, metadata }
// Read a Word document
const doc = await gmi.executeTool('read_document', {
path: './docs/proposal.docx'
});
// Returns: { path, format, size, content (extracted text), metadata }
// Read a PDF
const pdf = await gmi.executeTool('read_document', {
path: './reports/annual.pdf'
});
// Returns: { path, format, size, content (extracted text), metadata: { pages, info } }
说明: file_read 自动将二进制格式(.xlsx,.docx,.pdf)重定向到 read_document 带有帮助的错误信息.
创建 pdf
从文本内容创建真实的 PDF 文档 .
const result = await gmi.executeTool('create_pdf', {
path: './output/report.pdf',
content: 'Annual Report\n\nRevenue increased 25% year over year...',
title: 'Annual Report 2026',
author: 'Research Agent',
fontSize: 11
});
// Returns: { path, pages, bytes }
创建页面( b)
从结构化数据创建 Excel (.xlsx) 或 CSV 文件 .
// From headers + rows (2D array)
const result = await gmi.executeTool('create_spreadsheet', {
path: './output/data.xlsx',
headers: ['Name', 'Age', 'City'],
rows: [['Alice', 30, 'NYC'], ['Bob', 25, 'SF']],
sheetName: 'Contacts'
});
// Returns: { path, format, rows, bytes }
// From array of objects
const result = await gmi.executeTool('create_spreadsheet', {
path: './output/users.xlsx',
data: [
{ name: 'Alice', age: 30, city: 'NYC' },
{ name: 'Bob', age: 25, city: 'SF' }
]
});
// From a markdown table
const result = await gmi.executeTool('create_spreadsheet', {
path: './output/report.csv',
markdown: '| Product | Sales |\n| --- | --- |\n| Widget | 1500 |\n| Gadget | 2300 |'
});
// CSV output (auto-detected from extension)
const csv = await gmi.executeTool('create_spreadsheet', {
path: './output/export.csv',
headers: ['id', 'value'],
rows: [['1', '100'], ['2', '200']]
});
创建( d)
从文本或下标内容创建 Word 文档(.docx) .
const result = await gmi.executeTool('create_document', {
path: './output/report.docx',
content: '# Quarterly Report\n\n## Summary\n\nRevenue was **$1.5M**, up *25%* from last quarter.\n\n- Widget sales: $800K\n- Gadget sales: $700K',
title: 'Q1 Report',
author: 'Research Agent'
});
// Returns: { path, paragraphs, bytes }
支持的下标 : # headings (H1-H6), (中文(简体) ), **bold**, (中文(简体) ), *italic*, (中文(简体) ), - bullet lists。 。 。 .
说明: file_write 自动将.pdf,.xlsx,和.docx重定向到专用的创建工具,并带有有帮助的错误信息.
警卫
该扩展包括内置安全控制:
危险图案检测
与这些模式相匹配的命令默认被屏蔽 :
rm -rf /(重复删除根)format C:(格式驱动器)- 叉子炸弹
- 直接磁盘写入
- 系统关闭/重启命令
自定义块列表
createExtensionPack({
options: {
blockedCommands: ['sudo', 'su', 'chmod 777'],
allowedCommands: ['npm', 'node', 'python', 'git'] // Whitelist mode
}
});
故障安全检查(危险)
如果您需要完全控制(例如,在被锁定的容器或本地dev中),可以禁用所有命令安全检查:
createExtensionPack({
options: {
dangerouslySkipSecurityChecks: true
}
});
风险评估
每项命令都按风险等级评估:
| 风险水平 | 实例 |
|---|---|
safe | ls, (中文(简体) ), cat, (中文(简体) ), npm list |
low | echo "text" > file.txt |
medium | rm file.txt, (中文(简体) ), eval |
high | sudo,“曲线” |
critical | rm -rf /,阻断模式 |
配置
| 选项 | 类型 | 默认 | 说明 |
|---|---|---|---|
defaultShell | 字符串 | auto | 要使用的外壳( bash, powershell, cmd, zsh) |
timeout | 编号 | 60000 | 默认超时( 毫秒) |
workingDirectory | 字符串 | process.cwd() | 默认工作目录 |
filesystem | 对象 | undefined | 文件QQ工具的可选政策( 读取/ 写出 + 根) |
agentWorkspace | 对象 | undefined | 可选的代理工作空间目录帮助器 |
allowedCommands | 字符串 [] | [] | 命令白名单( 空 = 全部) |
blockedCommands | 字符串 [] | [] | 命令黑名单( 内置危险模式外加) |
dangerouslySkipSecurityChecks | 布尔 | false | 禁用所有命令安全检查( 只在信任的环境中使用) |
env | 对象 | {} | 环境变量 |
文件系统政策( 建议)
默认情况下 file_* 工具可以访问任何路径(遗产行为). 要执行安全的文件系统沙盒, 请配置 filesystem + 根数 :
createExtensionPack({
options: {
filesystem: {
allowRead: true,
allowWrite: true,
readRoots: ['/Users/me/Documents/AgentOS/agents/my-agent'],
writeRoots: ['/Users/me/Documents/AgentOS/agents/my-agent'],
},
},
});
代理工作空间助手
为了简化安全默认, 您可以配置 agentWorkspace当与 filesystem.allowRead/allowWrite,扩展默认为工作空间目录的根.
createExtensionPack({
options: {
filesystem: { allowRead: true, allowWrite: true },
agentWorkspace: {
agentId: 'my-agent',
// baseDir defaults to ~/Documents/AgentOS
subdirs: ['assets', 'exports', 'tmp'],
},
},
});
使用大小写
代码生成和执行
// Write generated code
await gmi.executeTool('file_write', {
path: './generated/app.py',
content: generatedPythonCode,
createDirs: true
});
// Execute it
await gmi.executeTool('shell_execute', {
command: 'python ./generated/app.py',
timeout: 30000
});
项目设置
// Create project structure
await gmi.executeTool('shell_execute', {
command: 'npx create-react-app my-app --template typescript'
});
// Install dependencies
await gmi.executeTool('shell_execute', {
command: 'npm install axios lodash',
cwd: './my-app'
});
逻辑分析
// Read recent logs
const logs = await gmi.executeTool('file_read', {
path: '/var/log/app.log',
lines: 100,
fromEnd: true
});
// Parse and analyze
const errorCount = logs.output.content.match(/ERROR/g)?.length || 0;
文档处理管道
// Read a spreadsheet, analyze it, write a report
const data = await gmi.executeTool('read_document', {
path: './data/sales.xlsx',
sheet: 'Q1'
});
// Create a summary report as Word doc
await gmi.executeTool('create_document', {
path: './output/summary.docx',
content: `# Sales Summary\n\n${data.output.content}`,
title: 'Q1 Sales Summary'
});
// Export processed data as CSV
await gmi.executeTool('create_spreadsheet', {
path: './output/processed.csv',
headers: ['Region', 'Total'],
rows: processedData
});
// Generate PDF invoice
await gmi.executeTool('create_pdf', {
path: './output/invoice.pdf',
content: invoiceText,
title: 'Invoice #1234'
});
二一理论
这一扩展与Web浏览器扩展相结合,提供了递归自建剂所需的两个基本能力:
-
CLI 执行者 (本扩展名)
- 执行任意代码
- 管理文件
- 安装依赖关系
- 运行测试并构建
-
网页浏览器 (见网页浏览器扩展名)
- 搜索信息
- 读取文档
- 学习新技术
- 核查执行情况
一个聪明的特工可以:
- 确定它需要学习的东西 网络搜索
- 查找文档/讲座 网络刮取
- 写入代码 – 文件写入
- 执行和测试 — shell 执行
- 调试和重排 ~ 重复
许可证
Apache-2.0 框架.dev