# 📋 OpenCode API 基本使用示例 ## 🔐 认证信息 ``` 用户名: admin 密码: admin123 ``` ## 🚀 基本 API 调用示例 ### 1. 健康检查 ```bash # OpenCode 服务器健康检查 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/global/health" # Nginx 健康检查 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/health" ``` ### 2. API 文档 ```bash # 获取完整的 API 文档 (OpenAPI 规范) curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/doc" # 在浏览器中查看 # https://airsltd-ocngx.hf.space/opencode/doc ``` ### 3. 项目管理 ```bash # 列出所有项目 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/project" # 获取当前项目信息 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/project/current" # 列出当前目录下的项目 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/project?directory=/path/to/project" ``` ### 4. 会话管理 ```bash # 创建新的 AI 编程会话 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{"title": "AI 编程会话"}' \ "https://airsltd-ocngx.hf.space/opencode/session" # 列出所有会话 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session" # 获取会话状态 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session/status" # 删除特定会话 curl -u admin:admin123 -s -X DELETE \ "https://airsltd-ocngx.hf.space/opencode/session/ses_123456" ``` ### 5. AI 编程请求 ```bash # 创建会话并立即发送消息 SESSION=$(curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{"title": "Python 开发"}' \ "https://airsltd-ocngx.hf.space/opencode/session" | jq -r '.id') # 发送文本消息 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [ {"type": "text", "text": "请创建一个简单的 Python Flask 应用"} ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" # 发送文件分析请求 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [ {"type": "text", "text": "请分析这个 Python 文件并优化性能"}, {"type": "file", "path": "/path/to/app.py"} ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" ``` ### 6. 文件操作 ```bash # 搜索文件 curl -u admin:admin123 -s \ "https://airsltd-ocngx.hf.space/opencode/find/file?query=main.py" # 读取文件内容 curl -u admin:admin123 -s \ "https://airsltd-ocngx.hf.space/opencode/file/content?path=/path/to/file.py" # 列出目录 curl -u admin:admin123 -s \ "https://airsltd-ocngx.hf.space/opencode/file?path=/path/to/directory" ``` ### 7. 提供商管理 ```bash # 列出所有可用的 AI 提供商 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/provider" # 获取认证方法 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/provider/auth" ``` ### 8. 配置管理 ```bash # 获取当前配置 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/config" # 列出提供商标认方法 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/config/providers" ``` ## 🧪 高级用法示例 ### 创建 Web 应用 ```bash SESSION=$(curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{"title": "Flask Web 开发"}' \ "https://airsltd-ocngx.hf.space/opencode/session" | jq -r '.id') curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [ { "type": "text", "text": "创建一个完整的 Flask Web 应用,包含:\n1. 主页路由 (/)\n2. 用户管理 API (/api/users)\n3. 数据库配置\n4. 基础的前端页面\n5. 错误处理\n请生成完整可运行的代码,包含所有必要的文件" } ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" ``` ### 代码重构 ```bash # 分析现有代码并重构 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [ { "type": "text", "text": "请重构这个函数,提高性能和可读性:\n\n```\ndef calculate_sum(numbers):\n total = 0\n for num in numbers:\n total += num\n return total\n```\n\n请使用 Python 最佳实践重写这个函数。" } ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" ``` ### 数据库操作 ```bash # 生成数据库操作代码 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [ { "type": "text", "text": "创建一个用户管理系统的数据库模型和 API,使用 SQLAlchemy 和 Flask,包含:\n1. User 模型\n2. CRUD 操作\n3. API 路由\n4. 数据验证\n5. 错误处理" } ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" ``` ## 🔍 响应处理 ### 解析 JSON 响应 ```bash # 使用 jq 美化输出 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session/ses_123" | jq '.' curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session/ses_123" | jq '.title' curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session/ses_123" | jq '.time' ``` ### 流式响应处理 ```bash # 对于长时间运行的请求,可以监控进度 curl -u admin:admin123 -N \ -H "Content-Type: application/json" \ -d '{ "parts": [ {"type": "text", "text": "生成一个大型的 Web 应用项目,包括前端和后端"} ] }' \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" ``` ## 🎯 最佳实践 ### 1. 错误处理 ```bash # 检查响应状态 RESPONSE=$(curl -u admin:admin123 -w "%{http_code}" -s -o /tmp/response.json \ "https://airsltd-ocngx.hf.space/opencode/session") if [ "$RESPONSE" = "200" ]; then echo "✅ 请求成功" cat /tmp/response.json | jq '.' else echo "❌ 请求失败,状态码: $RESPONSE" fi ``` ### 2. 会话管理 ```bash # 保存会话 ID 到环境变量 export OPENCODE_SESSION=$(curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d '{"title": "编程助手"}' \ "https://airsltd-ocngx.hf.space/opencode/session" | jq -r '.id') echo "会话 ID 已保存: $OPENCODE_SESSION" # 在后续请求中使用 curl -u admin:admin123 -s "https://airsltd-ocngx.hf.space/opencode/session/$OPENCODE_SESSION/message" \ -H "Content-Type: application/json" \ -d '{"parts": [{"type": "text", "text": "继续之前的开发"}]}' ``` ### 3. 批量操作 ```bash # 创建多个项目会话 for project in "用户管理" "订单系统" "数据分析"; do curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d "{\"title\": \"$project 开发\"}" \ "https://airsltd-ocngx.hf.space/opencode/session" echo "创建了 $project 项目会话" done ``` ## 📱 实际应用场景 ### 自动化代码生成 ```bash # 脚本:自动生成 REST API generate_api() { local model=$1 local description=$2 SESSION=$(curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d "{\"title\": \"API 开发\"}" \ "https://airsltd-ocngx.hf.space/opencode/session" | jq -r '.id') curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d "{\"parts\": [{\"type\": \"text\", \"text\": \"创建 $description 的 REST API,使用 $model,包含完整的 CRUD 操作和错误处理\"}]}" \ "https://airsltd-ocngx.hf.space/opencode/session/$SESSION/message" | jq -r '.info.parts[1].text' } # 使用示例 generate_api "GPT-4" "用户管理系统" ``` ### 代码审查助手 ```bash # 代码审查会话 REVIEW_SESSION=$(curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d "{\"title\": \"代码审查\"}" \ "https://airsltd-ocngx.hf.space/opencode/session" | jq -r '.id') # 提交代码审查 curl -u admin:admin123 -s -X POST \ -H "Content-Type: application/json" \ -d "{\"parts\": [{\"type\": \"text\", \"text\": \"请审查以下代码的安全性、性能和可维护性:[代码内容]\"}]}" \ "https://airsltd-ocngx.hf.space/opencode/session/$REVIEW_SESSION/message" ``` 这个 API 集成现在完全可用!您可以通过这些命令实现各种 AI 辅助的编程任务。