feat: sync latest workspace changes

This commit is contained in:
coso
2026-04-19 18:50:13 +08:00
parent 3715b6e304
commit 9f31ba4cd7
301 changed files with 14618 additions and 14009 deletions
+41 -23
View File
@@ -1,6 +1,6 @@
---
title: API 概览
description: 面向进阶用户的本地接口能力说明
description: 当前只保留本地兼容接口入口说明
navigation:
icon: i-heroicons-code-bracket
---
@@ -8,48 +8,66 @@ navigation:
# API 概览
::alert{type="info"}
本章节面向进阶用户与开发者。普通创作者可直接在应用内使用,无需 API 接入。
本章节面向进阶用户与开发者。普通创作者直接在应用内使用即可,无需单独接 API。
::
当你需要把 Lime 接入脚本、自动化流程或第三方工具时,可使用本地 API。
当前 `content/api-reference` 只保留仍与实现锚点对齐的本地兼容接口说明,不再继续扩写旧管理接口或未挂载的兼容路由叙事。
## 常见端点类型
## 当前保留的兼容面
### 通用对话端点
### OpenAI 兼容
用于文本生成、对话续写等任务。
用于本地对话与模型发现:
### 模型与管理端点
- `POST /v1/chat/completions`
- `GET /v1/models`
用于读取模型列表、状态信息和部分管理能力。
[查看 OpenAI API 说明 →](/api-reference/openai-api)
### 扩展端点
### Claude 兼容
用于特定平台或集成场景。
用于 Claude Messages 协议兼容:
## 认证方式
- `POST /v1/messages`
- `POST /v1/messages/count_tokens`
- `Authorization: Bearer <api-key>`
- 或使用兼容格式的密钥头
请确保 API Key 仅在可信环境使用。
[查看 Claude API 说明 →](/api-reference/claude-api)
## 默认地址
默认本地地址:`http://127.0.0.1:8999`
默认本地地址:
通常建议保持本地监听,不对公网暴露。
```text
http://127.0.0.1:8999
```
通常建议只在本地监听,不对公网直接暴露。
## 认证方式
认证头会跟随兼容协议变化:
- OpenAI 兼容:`Authorization: Bearer <api-key>`
- Claude 兼容:`x-api-key: <api-key>`,并携带 `anthropic-version`
具体请求示例以各协议子页面为准。
## 错误排查建议
- `401`:密钥错误或请求头格式错误
- `404`:端点路径错误
- `429`:请求频率过高
- `5xx`:服务端异常或上游波动
- `401`
- 密钥错误
- 认证头格式与协议不匹配
- `404`
- 路径错误
- 协议路径写错
- `429`
- 请求频率过高
- 上游限流
- `5xx`
- 本地服务异常
- 上游模型服务波动
## 下一步
- [OpenAI API](/api-reference/openai-api)
- [Claude API](/api-reference/claude-api)
- [管理 API](/api-reference/management-api)
- [Amp CLI API](/api-reference/amp-cli-api)
+17 -11
View File
@@ -11,7 +11,13 @@ navigation:
本页是开发者进阶文档。普通创作者可直接在应用内使用,无需 API 调用。
::
Lime 提供完整的 OpenAI Chat Completions API 兼容。
Lime 当前提供 OpenAI Chat Completions 协议兼容入口,以及 `/v1/models` 模型发现端点。
## 当前边界
- 当前只覆盖 `/v1/chat/completions` 与 `/v1/models`
- 本页不把其他 OpenAI 风格端点写成已落地能力
- 具体可用模型仍以 Lime 当前加载到本地运行时的结果为准
## /v1/chat/completions
@@ -27,7 +33,7 @@ Authorization: Bearer your-api-key
```json
{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
@@ -72,7 +78,7 @@ Authorization: Bearer your-api-key
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"choices": [
{
"index": 0,
@@ -100,7 +106,7 @@ curl http://127.0.0.1:8999/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": true
}'
@@ -132,16 +138,16 @@ Authorization: Bearer your-api-key
"object": "list",
"data": [
{
"id": "claude-sonnet-4-20250514",
"id": "example-chat-model",
"object": "model",
"created": 1234567890,
"owned_by": "anthropic"
"owned_by": "provider-a"
},
{
"id": "gemini-2.0-flash",
"id": "example-fast-model",
"object": "model",
"created": 1234567890,
"owned_by": "google"
"owned_by": "provider-b"
}
]
}
@@ -153,7 +159,7 @@ Authorization: Bearer your-api-key
```json
{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"messages": [{"role": "user", "content": "What's the weather in Tokyo?"}],
"tools": [
{
@@ -211,7 +217,7 @@ client = openai.OpenAI(
)
response = client.chat.completions.create(
model="claude-sonnet-4-20250514",
model="your-model-id",
messages=[{"role": "user", "content": "Hello!"}]
)
@@ -229,7 +235,7 @@ const client = new OpenAI({
});
const response = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
model: 'your-model-id',
messages: [{ role: 'user', content: 'Hello!' }]
});
+17 -9
View File
@@ -11,7 +11,13 @@ navigation:
本页是开发者进阶文档。普通创作者可直接在应用内使用,无需 API 调用。
::
Lime 提供 Anthropic Claude Messages API 兼容。
Lime 当前提供 Claude Messages 协议兼容入口。
## 当前边界
- 当前只覆盖 `/v1/messages` 与 `/v1/messages/count_tokens`
- `/v1/messages/count_tokens` 目前用于兼容需要估算值的客户端,不承诺返回上游精确计费结果
- 具体可用模型仍以 Lime 当前加载到本地运行时的结果为准
## /v1/messages
@@ -28,7 +34,7 @@ anthropic-version: 2023-06-01
```json
{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello!"}
@@ -83,7 +89,7 @@ anthropic-version: 2023-06-01
"content": [
{"type": "text", "text": "Hello! How can I help you today?"}
],
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 10,
@@ -102,7 +108,7 @@ curl http://127.0.0.1:8999/v1/messages \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello!"}],
"stream": true
@@ -127,6 +133,8 @@ data: {"type":"message_stop"}
## /v1/messages/count_tokens
当前该端点返回估算值,用于兼容需要预估 token 数的客户端。
### 请求
```bash
@@ -140,7 +148,7 @@ anthropic-version: 2023-06-01
```json
{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"messages": [
{"role": "user", "content": "Hello!"}
]
@@ -151,7 +159,7 @@ anthropic-version: 2023-06-01
```json
{
"input_tokens": 10
"input_tokens": 100
}
```
@@ -161,7 +169,7 @@ anthropic-version: 2023-06-01
```json
{
"model": "claude-sonnet-4-20250514",
"model": "your-model-id",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "What's the weather in Tokyo?"}],
"tools": [
@@ -209,7 +217,7 @@ client = anthropic.Anthropic(
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
model="your-model-id",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)
@@ -228,7 +236,7 @@ const client = new Anthropic({
});
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
model: 'your-model-id',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }]
});
@@ -1,324 +0,0 @@
---
title: 管理 API
description: Lime 远程管理 API 端点
navigation:
icon: i-heroicons-cog-6-tooth
---
# 管理 API
::alert{type="info"}
本页是开发者进阶文档,主要用于自动化管理与运维集成。
::
Lime 提供远程管理 API,用于配置和监控服务。
## 认证
所有管理 API 请求需要在 `Authorization` 头中提供密钥:
```bash
Authorization: Bearer your-secret-key
```
## 访问控制
管理 API 的访问受以下配置控制:
| 配置项 | 说明 |
|--------|------|
| `secret_key` | 管理密钥,为空时禁用所有管理端点(返回 404) |
| `allow_remote` | 是否允许远程访问,为 false 时仅允许 localhost |
::alert{type="warning"}
当前版本未启用 TLS,仅支持本地访问,`allow_remote` 必须保持为 `false`。
::
## /v0/management/status
获取服务器状态信息。
### 请求
```bash
GET /v0/management/status
Authorization: Bearer your-secret-key
```
### 响应
```json
{
"status": "running",
"version": "1.0.0",
"uptime_seconds": 3600,
"tls_enabled": false,
"active_credentials": 5,
"total_requests": 1234,
"providers": {
"kiro": {
"enabled": true,
"credentials_count": 2
},
"gemini": {
"enabled": true,
"credentials_count": 1
}
}
}
```
## /v0/management/credentials
### 获取凭证列表
```bash
GET /v0/management/credentials
Authorization: Bearer your-secret-key
```
### 响应
```json
{
"credentials": [
{
"id": "kiro-main",
"provider": "kiro",
"type": "oauth",
"status": "valid",
"expires_at": "2025-01-01T00:00:00Z",
"disabled": false
},
{
"id": "gemini-key-1",
"provider": "gemini_api_key",
"type": "api_key",
"status": "valid",
"disabled": false,
"excluded_models": ["gemini-2.5-pro"]
}
]
}
```
### 添加凭证
```bash
POST /v0/management/credentials
Authorization: Bearer your-secret-key
Content-Type: application/json
```
#### 添加 OAuth 凭证
```json
{
"provider": "kiro",
"id": "kiro-new",
"token_file": "kiro/new-token.json"
}
```
#### 添加 API Key 凭证
```json
{
"provider": "openai",
"id": "openai-new",
"api_key": "sk-xxx...",
"base_url": "https://api.openai.com/v1"
}
```
#### 添加 Gemini API Key
```json
{
"provider": "gemini_api_key",
"id": "gemini-key-new",
"api_key": "AIzaSy...",
"base_url": "https://generativelanguage.googleapis.com",
"excluded_models": ["gemini-2.5-pro", "*-preview"]
}
```
### 响应
```json
{
"success": true,
"credential_id": "kiro-new"
}
```
### 删除凭证
```bash
DELETE /v0/management/credentials/{credential_id}
Authorization: Bearer your-secret-key
```
### 响应
```json
{
"success": true
}
```
## /v0/management/config
### 获取配置
```bash
GET /v0/management/config
Authorization: Bearer your-secret-key
```
### 响应
```json
{
"server": {
"host": "127.0.0.1",
"port": 8999,
"tls": {
"enable": false
}
},
"routing": {
"default_provider": "kiro"
},
"quota_exceeded": {
"switch_project": true,
"switch_preview_model": true,
"cooldown_seconds": 300
}
}
```
### 更新配置
```bash
PUT /v0/management/config
Authorization: Bearer your-secret-key
Content-Type: application/json
```
```json
{
"routing": {
"default_provider": "gemini"
},
"quota_exceeded": {
"cooldown_seconds": 600
}
}
```
### 响应
```json
{
"success": true,
"restart_required": false
}
```
> **注意**: 某些配置更改(如 TLS、端口)需要重启服务器才能生效。
## 错误响应
### 401 Unauthorized
密钥无效或缺失:
```json
{
"error": {
"message": "Invalid or missing secret key",
"type": "authentication_error",
"code": "invalid_api_key"
}
}
```
### 403 Forbidden
远程访问被禁止:
```json
{
"error": {
"message": "Remote access not allowed",
"type": "permission_error",
"code": "remote_access_denied"
}
}
```
### 404 Not Found
管理 API 已禁用(secret_key 为空):
```json
{
"error": {
"message": "Management API is disabled",
"type": "not_found_error",
"code": "endpoint_not_found"
}
}
```
## 示例代码
### cURL
```bash
# 获取状态
curl http://127.0.0.1:8999/v0/management/status \
-H "Authorization: Bearer your-secret-key"
# 获取凭证列表
curl http://127.0.0.1:8999/v0/management/credentials \
-H "Authorization: Bearer your-secret-key"
# 添加凭证
curl http://127.0.0.1:8999/v0/management/credentials \
-H "Authorization: Bearer your-secret-key" \
-H "Content-Type: application/json" \
-d '{"provider": "openai", "id": "openai-new", "api_key": "sk-xxx"}'
```
### Python
```python
import requests
BASE_URL = "http://127.0.0.1:8999"
SECRET_KEY = "your-secret-key"
headers = {
"Authorization": f"Bearer {SECRET_KEY}",
"Content-Type": "application/json"
}
# 获取状态
response = requests.get(f"{BASE_URL}/v0/management/status", headers=headers)
print(response.json())
# 添加凭证
credential = {
"provider": "openai",
"id": "openai-new",
"api_key": "sk-xxx"
}
response = requests.post(
f"{BASE_URL}/v0/management/credentials",
headers=headers,
json=credential
)
print(response.json())
```
@@ -1,248 +0,0 @@
---
title: Amp CLI API
description: Amp CLI 集成路由端点
navigation:
icon: i-heroicons-command-line
---
# Amp CLI API
::alert{type="info"}
本页是开发者进阶文档。若你不涉及 Amp CLI 集成,可跳过。
::
Lime 提供 Amp CLI 兼容的路由端点,支持将 Amp CLI 请求路由到本地 OAuth 凭证。
## 概述
Amp CLI 集成允许你:
- 使用本地 OAuth 凭证处理 Amp CLI 请求
- 将不可用的模型映射到可用的替代模型
- 代理 Amp 的认证和账户管理功能
## Provider 路由
### /api/provider/{provider}/v1/chat/completions
处理 Amp CLI 的 OpenAI 格式聊天请求。
```bash
POST /api/provider/{provider}/v1/chat/completions
Content-Type: application/json
Authorization: Bearer your-api-key
```
#### 支持的 Provider
| Provider | 说明 |
|----------|------|
| `anthropic` | Claude 模型 |
| `openai` | GPT 模型 |
| `google` | Gemini 模型 |
#### 请求示例
```bash
curl http://127.0.0.1:8999/api/provider/anthropic/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": true
}'
```
### /api/provider/{provider}/v1/messages
处理 Amp CLI 的 Anthropic Messages 格式请求。
```bash
POST /api/provider/{provider}/v1/messages
Content-Type: application/json
x-api-key: your-api-key
anthropic-version: 2023-06-01
```
#### 请求示例
```bash
curl http://127.0.0.1:8999/api/provider/anthropic/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello!"}]
}'
```
## 模型映射
当 Amp CLI 请求的模型不可用时,Lime 可以自动映射到可用的替代模型。
### 配置
```yaml
ampcode:
model_mappings:
- from: "claude-opus-4.5"
to: "claude-sonnet-4"
- from: "gpt-5"
to: "gemini-2.5-pro"
- from: "claude-3-opus-20240229"
to: "claude-3-5-sonnet-20241022"
```
### 映射行为
1. 收到请求时检查模型名称
2. 如果模型在映射列表中,替换为目标模型
3. 使用替换后的模型名称路由请求
4. 响应中保留原始请求的模型名称
## 管理端点代理
Lime 可以代理 Amp 的认证和账户管理端点到上游服务器。
### /api/auth/*
代理认证相关请求。
```bash
# 登录
POST /api/auth/login
# 刷新 Token
POST /api/auth/refresh
# 登出
POST /api/auth/logout
```
### /api/user/*
代理用户账户相关请求。
```bash
# 获取用户信息
GET /api/user/profile
# 获取使用统计
GET /api/user/usage
```
### 配置
```yaml
ampcode:
upstream_url: "https://ampcode.com"
restrict_management_to_localhost: false
```
| 配置项 | 说明 |
|--------|------|
| `upstream_url` | Amp 上游服务器 URL |
| `restrict_management_to_localhost` | 是否限制管理端点只能从 localhost 访问 |
## 使用场景
### 场景 1:使用本地 OAuth 凭证
你有 Kiro 的 Claude 订阅,想用 Amp CLI 但不想额外付费:
1. 配置 Lime 加载 Kiro OAuth 凭证
2. 在 Amp CLI 中配置 Lime 作为 API 端点
3. Amp CLI 请求通过 Lime 路由到 Kiro 凭证
### 场景 2:模型替换
Amp CLI 请求 Claude Opus 4.5,但你只有 Sonnet 4 的访问权限:
```yaml
ampcode:
model_mappings:
- from: "claude-opus-4.5"
to: "claude-sonnet-4"
```
### 场景 3:多 Provider 负载均衡
配置多个凭证,Lime 自动在它们之间负载均衡:
```yaml
credential_pool:
kiro:
- id: "kiro-1"
token_file: "kiro/token-1.json"
- id: "kiro-2"
token_file: "kiro/token-2.json"
```
## Amp CLI 配置
在 Amp CLI 中配置 Lime:
```bash
# 设置 API 端点
amp config set api.base_url http://127.0.0.1:8999/api/provider
# 设置 API Key
amp config set api.key your-lime-api-key
```
或在配置文件中:
```yaml
# ~/.amp/config.yaml
api:
base_url: http://127.0.0.1:8999/api/provider
key: your-lime-api-key
```
## 错误处理
### 模型不可用
当请求的模型不可用且没有配置映射时:
```json
{
"error": {
"message": "Model 'claude-opus-4.5' is not available",
"type": "invalid_request_error",
"code": "model_not_found"
}
}
```
### 上游连接失败
当无法连接到 Amp 上游服务器时:
```json
{
"error": {
"message": "Failed to connect to upstream server",
"type": "upstream_error",
"code": "connection_failed"
}
}
```
### 凭证耗尽
当所有凭证都不可用时:
```json
{
"error": {
"message": "All credentials exhausted",
"type": "service_unavailable",
"code": "no_credentials_available"
}
}
```
响应头会包含 `Retry-After` 指示何时可以重试。