Skip to content

kevinwangl/kiro-server

Repository files navigation

kiro-server

基于 kiro-cli 的 AI 分析服务。将 kiro-cli 的 AI 能力封装为 HTTP API,业务方通过 YAML 文件定义提示词模板,零代码接入。

架构

HTTP 请求 (文件 + prompt 名称)
        ↓
  kiro-server (Rust/axum)
        ↓
  保存文件到临时目录
        ↓
  匹配提示词模板 (YAML)
        ↓
  ┌──────────────┬──────────────┐
  │ kiro-cli 模式 │ 外部命令模式  │
  │ 渲染提示词     │ 拼装命令      │
  │ spawn kiro-cli│ spawn 外部程序│
  └──────────────┴──────────────┘
        ↓
  返回 JSON (含耗时/tokens)

快速开始

1. 编译

cd kiro-server
cargo build --release

2. 确保 kiro-cli 已登录

kiro-cli login
kiro-cli whoami  # 确认登录态

3. 编写业务提示词 YAML

# my-prompts.yaml
prompts:
  - name: error_screenshot
    description: "分析系统错误截图"
    file_types: [".png", ".jpg"]
    prompt: |
      请分析文件 {file_path}:
      1. 识别错误类型
      2. 分析可能原因
      3. 给出解决方案
    output:
      type: stream

4. 校验并启动

# 校验 YAML 格式
kiro-server check -f my-prompts.yaml

# 启动服务
kiro-server serve -f my-prompts.yaml

# 指定端口
kiro-server serve -f my-prompts.yaml -p 9000

# 加载多个 YAML
kiro-server serve -f prompts-a.yaml -f prompts-b.yaml

5. 调用 API

# 上传文件执行分析
curl -X POST http://localhost:8000/analyze/error_screenshot \
  -F "file=@screenshot.png"

# 查看已加载的模板
curl http://localhost:8000/prompts

# 健康检查
curl http://localhost:8000/health

API 接口

POST /analyze/:prompt_name

上传文件并执行分析。

请求: multipart/form-data,字段名 file

响应:

{
  "output": "kiro-cli 或外部命令的输出内容",
  "duration_ms": 24466,
  "input_tokens": 1500,
  "output_tokens": 320
}

output.type 为 file 时额外返回:

{
  "output": "...",
  "duration_ms": 8000,
  "files": ["/tmp/kiro-output/fix.sh"]
}

GET /prompts

返回已加载的提示词模板列表。

GET /health

健康检查,验证 kiro-cli 登录态。

提示词模板 YAML 格式

kiro-cli 模式(默认)

通过提示词模板调用 kiro-cli:

prompts:
  - name: error_screenshot          # 唯一标识,用于 API 路径
    description: "分析系统错误截图"    # 描述
    file_types: [".png", ".jpg"]     # 支持的文件类型(文档用途)
    prompt: |                        # 提示词模板
      请分析文件 {file_path}...
    output:
      type: stream                   # stream: 返回文本 / file: 返回文件列表

外部命令模式

调用外部程序(如 Python 脚本):

prompts:
  - name: receipt_ocr_dual
    description: "双引擎收据识别"
    file_types: [".jpg", ".png"]
    executor: "python"                          # 执行器
    command: "receipt_ocr.py {file_path}"        # 命令模板
    output:
      type: stream

内置变量

变量 说明
{file_path} 上传文件的临时存储路径
{output_dir} 输出目录(仅 output.type = "file" 时可用)

输出类型

类型 说明 额外字段
stream 返回文本输出
file 返回生成的文件列表 需指定 output_dir

服务端配置

可选的 kiro-server.toml 配置文件,不存在时使用默认值:

# 生成默认配置
kiro-server init
# kiro-server.toml
[server]
host = "0.0.0.0"              # 监听地址
port = 8000                    # 监听端口
temp_dir = "/tmp/kiro-server"  # 上传文件临时目录

[kiro]                         # 不配置则用 kiro-cli 默认值
agent = "my-agent"             # kiro-cli agent 名称
model = "claude-sonnet-4.5"    # 模型
timeout = 120                  # 执行超时(秒)
max_concurrent = 4             # 最大并发数
trust_all_tools = true         # 自动审批(服务端必须为 true)

CLI 命令

kiro-server serve   -f <yaml> [-p port] [--host addr] [-c config]  启动服务
kiro-server check   -f <yaml>                                      校验 YAML
kiro-server list    -f <yaml>                                      列出模板
kiro-server init                                                   生成默认配置
kiro-server config  [--config path]                                查看配置

服务端日志

每次请求输出完整的处理链路日志:

────────────────────────────────────────────────────────────
[2026-03-18 10:14:26.102] [REQ ] POST /analyze/receipt_ocr
[2026-03-18 10:14:26.103] [FILE] 接收文件: image.jpg (125.3KB)
[2026-03-18 10:14:26.105] [FILE] 保存完成: /tmp/kiro-server/uuid.jpg (2ms)
[2026-03-18 10:14:26.106] [EXEC] 开始执行: prompt=receipt_ocr, 模式=kiro-cli
[2026-03-18 10:14:26.106] [EXEC] 命令: kiro-cli chat --no-interactive -a "..."
[2026-03-18 10:14:55.230] [EXEC] 执行完成: 29124ms | input_tokens: 1500 | output_tokens: 320
[2026-03-18 10:14:55.231] [RES ] POST /analyze/receipt_ocr ← image.jpg — 总耗时: 29129ms

测试

提供客户端测试脚本 test_plan_a_client.py

# 单次测试
python3 test_plan_a_client.py single /path/to/image.jpg

# 全量测试
python3 test_plan_a_client.py batch

# 指定服务器
python3 test_plan_a_client.py --server http://192.168.1.100:8000 single image.jpg

项目结构

kiro-server/
├── Cargo.toml                   # Rust 项目配置
├── README.md                    # 本文档
├── src/
│   ├── main.rs                  # CLI 入口 (clap)
│   ├── config.rs                # 服务端配置 (kiro-server.toml)
│   ├── prompt.rs                # 业务提示词模板 (YAML)
│   ├── engine.rs                # 统一执行引擎
│   └── server.rs                # HTTP 服务 (axum)
├── example-prompts.yaml         # 通用示例模板
├── receipt-ocr-prompts.yaml     # 收据 OCR 模板 (kiro-cli 模式)
├── receipt-ocr-external.yaml    # 收据 OCR 模板 (外部命令模式)
└── test_plan_a_client.py        # 客户端测试脚本

已知限制

  • 冷启动开销: 每次请求 spawn 新的 kiro-cli 进程,约 3-5s 固定开销
  • 并发能力: 受 kiro-cli 进程数限制,默认最大 4 并发
  • 登录态: 需要预先执行 kiro-cli login,token 会自动刷新
  • 输出解析: kiro-cli 输出包含 ANSI 颜色码,客户端需自行清理

License

MIT

About

kiro-cli的服务封装

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors