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89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# InterConnect-Server AI API Documentation

本文档描述了 InterConnect-Server 的 AI 聊天功能相关 API 接口。

## 概述

AI 功能允许 Minecraft 服务器通过 InterConnect-Client 插件/模组调用 OpenAI 格式的 API,实现游戏内 AI 聊天功能。

**基础路径**: `/api/ai`

---

## 认证

所有 API 请求需要在 Header 中携带 API Key:

```
Authorization: Bearer <your_api_key>
```

### 权限要求

| 接口 | 权限要求 |
|------|----------|
| `GET /api/ai/config` | Admin Key |
| `POST /api/ai/config` | Admin Key |
| `PATCH /api/ai/config` | Admin Key |
| `DELETE /api/ai/config` | Admin Key |
| `POST /api/ai/config/test` | Admin Key |
| `POST /api/ai/chat` | Server Key 或 Admin Key |

---

## AI 配置接口

### 获取 AI 配置

获取当前的 AI 配置信息(API Key 会被部分隐藏)。

**请求**

```http
GET /api/ai/config
Authorization: Bearer <admin_key>
```

**成功响应** `200 OK`

```json
{
  "apiUrl": "https://api.openai.com/v1/chat/completions",
  "modelId": "gpt-3.5-turbo",
  "apiKey": "sk-x****xxxx",
  "enabled": true,
  "systemPrompt": "You are a helpful assistant for Minecraft players.",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": "2024-01-15T12:00:00.000Z"
}
```

**错误响应** `404 Not Found`

```json
{
  "detail": "AI configuration not found"
}
```

---

### 创建 AI 配置

创建新的 AI 配置。

**请求**

```http
POST /api/ai/config
Authorization: Bearer <admin_key>
Content-Type: application/json
```

**请求体**

```json
{
  "api_url": "https://api.openai.com/v1/chat/completions",
  "model_id": "gpt-3.5-turbo",
  "api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxx",
  "enabled": true,
  "system_prompt": "You are a helpful assistant for Minecraft players."
}
```

| 字段 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `api_url` | string | ✅ | OpenAI 格式的 API URL |
| `model_id` | string | ✅ | 模型 ID (如 gpt-3.5-turbo, gpt-4) |
| `api_key` | string | ✅ | API Key |
| `enabled` | boolean | ❌ | 是否启用,默认 `true` |
| `system_prompt` | string | ❌ | 系统提示词,可选 |

**成功响应** `201 Created`

```json
{
  "apiUrl": "https://api.openai.com/v1/chat/completions",
  "modelId": "gpt-3.5-turbo",
  "apiKey": "sk-x****xxxx",
  "enabled": true,
  "systemPrompt": "You are a helpful assistant for Minecraft players.",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": "2024-01-15T10:30:00.000Z"
}
```

**错误响应** `400 Bad Request`

```json
{
  "detail": "api_url, model_id, and api_key are required"
}
```

---

### 更新 AI 配置

更新现有的 AI 配置,只需提供要更新的字段。

**请求**

```http
PATCH /api/ai/config
Authorization: Bearer <admin_key>
Content-Type: application/json
```

**请求体**

```json
{
  "model_id": "gpt-4",
  "enabled": false
}
```

| 字段 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `api_url` | string | ❌ | OpenAI 格式的 API URL |
| `model_id` | string | ❌ | 模型 ID |
| `api_key` | string | ❌ | API Key(不提供则保持原值) |
| `enabled` | boolean | ❌ | 是否启用 |
| `system_prompt` | string | ❌ | 系统提示词 |

**成功响应** `200 OK`

```json
{
  "apiUrl": "https://api.openai.com/v1/chat/completions",
  "modelId": "gpt-4",
  "apiKey": "sk-x****xxxx",
  "enabled": false,
  "systemPrompt": "You are a helpful assistant for Minecraft players.",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": "2024-01-15T14:00:00.000Z"
}
```

---

### 删除 AI 配置

删除 AI 配置。

**请求**

```http
DELETE /api/ai/config
Authorization: Bearer <admin_key>
```

**成功响应** `204 No Content`

无响应体

**错误响应** `404 Not Found`

```json
{
  "detail": "AI configuration not found"
}
```

---

### 测试 AI 连接

测试当前 AI 配置是否能正常连接。

**请求**

```http
POST /api/ai/config/test
Authorization: Bearer <admin_key>
```

**成功响应** `200 OK`

```json
{
  "success": true,
  "message": "Connection successful",
  "model": "gpt-3.5-turbo",
  "response": "OK"
}
```

**错误响应** `400 Bad Request`

```json
{
  "success": false,
  "message": "Connection failed",
  "error": "Invalid API key"
}
```

---

## AI 聊天接口

### 发送聊天消息

发送消息到 AI 并获取回复。此接口供 Minecraft 服务器的 InterConnect-Client 插件/模组调用。

**请求**

```http
POST /api/ai/chat
Authorization: Bearer <server_key>
Content-Type: application/json
```

**请求体**

```json
{
  "message": "你好,请介绍一下 Minecraft 的红石系统",
  "player_name": "Steve",
  "server_id": "my-server-1"
}
```

| 字段 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `message` | string | ✅ | 用户发送的消息内容 |
| `player_name` | string | ❌ | 发送消息的玩家名称 |
| `server_id` | string | ❌ | 服务器 ID(Admin Key 可指定,Server Key 使用绑定的 server_id) |

**成功响应** `200 OK`

```json
{
  "success": true,
  "reply": "红石是 Minecraft 中的一种特殊材料,可以用来创建各种机械装置...",
  "model": "gpt-3.5-turbo",
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 150,
    "total_tokens": 175
  }
}
```

**错误响应**

`404 Not Found` - AI 配置不存在

```json
{
  "detail": "AI configuration not found"
}
```

`403 Forbidden` - AI 功能已禁用

```json
{
  "detail": "AI feature is disabled"
}
```

`400 Bad Request` - 缺少必填字段

```json
{
  "detail": "message is required"
}
```

`500 Internal Server Error` - AI 请求失败

```json
{
  "success": false,
  "detail": "AI request failed",
  "error": "Rate limit exceeded"
}
```

---

## 事件类型

当 AI 聊天发生时,会产生 `ai_chat` 类型的事件,通过 WebSocket 广播给所有连接的客户端。

**事件格式**

```json
{
  "type": "minecraft_event",
  "event": {
    "event_type": "ai_chat",
    "server_name": "my-server-1",
    "timestamp": "2024-01-15T10:30:00.000Z",
    "data": {
      "player_name": "Steve",
      "message": "你好",
      "reply": "你好!有什么我可以帮助你的吗?",
      "model": "gpt-3.5-turbo"
    }
  },
  "source_key_id_prefix": "abc12345"
}
```

---

## Minecraft 插件/模组集成示例

### 命令格式

在 Minecraft 服务器中,玩家可以使用以下命令调用 AI:

```
/ic chat <消息内容>
```

### 插件实现流程

1. 玩家执行 `/ic chat 你好` 命令
2. 插件捕获命令,提取消息内容
3. 插件向 InterConnect-Server 发送 POST 请求:

```http
POST /api/ai/chat
Authorization: Bearer <server_key>
Content-Type: application/json

{
  "message": "你好",
  "player_name": "Steve",
  "server_id": "my-server-1"
}
```

4. 收到响应后,将 AI 回复发送给玩家

### 响应处理示例(伪代码)

```java
// 发送请求
Response response = httpClient.post("/api/ai/chat", {
    "message": playerMessage,
    "player_name": player.getName(),
    "server_id": serverId
});

// 处理响应
if (response.success) {
    player.sendMessage("[AI] " + response.reply);
} else {
    player.sendMessage("[AI] 请求失败: " + response.error);
}
```

---

## 支持的 API 提供商

本系统支持所有兼容 OpenAI Chat Completions API 格式的服务:

| 提供商 | API URL 示例 |
|--------|-------------|
| OpenAI | `https://api.openai.com/v1/chat/completions` |
| Azure OpenAI | `https://{resource}.openai.azure.com/openai/deployments/{deployment}/chat/completions?api-version=2024-02-01` |
| Anthropic (via proxy) | 需要兼容层 |
| 本地模型 (Ollama) | `http://localhost:11434/v1/chat/completions` |
| 其他兼容服务 | 根据服务商文档配置 |

---

## 错误码参考

| HTTP 状态码 | 说明 |
|-------------|------|
| 200 | 请求成功 |
| 201 | 创建成功 |
| 204 | 删除成功(无内容) |
| 400 | 请求参数错误 |
| 401 | 未认证或 API Key 无效 |
| 403 | 权限不足或功能已禁用 |
| 404 | 资源不存在 |
| 500 | 服务器内部错误 |