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 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
openapi: 3.0.0
info:
  title: InterConnect-Server API
  version: 1.0.0
  description: |
    InterConnect-Server 是一个 Minecraft WebSocket API 服务器,提供 API Key 管理、事件广播、服务器命令等功能。
    
    权限说明:
    - **Admin Key**: `mc_admin_` 前缀,拥有完整管理权限。
    - **Regular Key**: `mc_key_` 前缀,可查看/管理关联的 Server Key,发送服务器命令。
    - **Server Key**: `mc_server_` 前缀,仅用于插件/Mod 配置,用于认证和事件上报。

servers:
  - url: http://localhost:8000
    description: 本地服务器

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: 使用 API Key 进行认证 (Bearer Token)

  schemas:
    ApiKey:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        description:
          type: string
        keyPrefix:
          type: string
          enum: [mc_admin_, mc_key_, mc_server_]
        keyType:
          type: string
          enum: [admin, regular, server]
        serverId:
          type: string
          nullable: true
        regularKeyId:
          type: string
          nullable: true
        createdAt:
          type: string
          format: date-time
        lastUsed:
          type: string
          format: date-time
          nullable: true
        isActive:
          type: boolean
    
    ApiKeyWithSecret:
      allOf:
        - $ref: '#/components/schemas/ApiKey'
        - type: object
          properties:
            key:
              type: string
              description: 原始 API Key,仅在创建时返回一次

    CreateKeyRequest:
      type: object
      required: [name]
      properties:
        name:
          type: string
        description:
          type: string
        key_type:
          type: string
          enum: [regular, admin]
          default: regular
        server_id:
          type: string

    CreateServerKeyRequest:
      type: object
      required: [name, regular_key_id]
      properties:
        name:
          type: string
        description:
          type: string
        server_id:
          type: string
        regular_key_id:
          type: string

    Event:
      type: object
      required: [event_type, server_name, timestamp, data]
      properties:
        event_type:
          type: string
          description: 事件类型 (e.g., player_join, player_leave, message, ai_chat)
        server_name:
          type: string
        timestamp:
          type: string
          format: date-time
        data:
          type: object
          description: 事件具体数据

    CommandRequest:
      type: object
      required: [command]
      properties:
        command:
          type: string
        server_id:
          type: string
          description:  Admin Key 需要/可以指定

    AiConfig:
      type: object
      properties:
        apiUrl:
          type: string
        modelId:
          type: string
        apiKey:
          type: string
          description: 部分隐藏的 API Key
        enabled:
          type: boolean
        systemPrompt:
          type: string
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time

    AiConfigUpdateRequest:
      type: object
      properties:
        api_url:
          type: string
        model_id:
          type: string
        api_key:
          type: string
        enabled:
          type: boolean
        system_prompt:
          type: string

    AiChatRequest:
      type: object
      required: [message]
      properties:
        message:
          type: string
        player_name:
          type: string
        server_id:
          type: string

paths:
  /health:
    get:
      summary: 健康检查
      description: 获取服务器状态和统计信息
      security: []
      responses:
        '200':
          description: 服务器正常
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                  timestamp:
                    type: string
                  active_ws:
                    type: integer
                  keys_total:
                    type: integer
                  admin_active:
                    type: integer
                  server_active:
                    type: integer
                  regular_active:
                    type: integer

  /manage/keys:
    get:
      summary: 获取所有 API Key
      description: 获取所有 API Key 列表 (仅 Admin)
      security:
        - bearerAuth: []
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ApiKey'
        '403':
          description: 权限不足
    
    post:
      summary: 创建 API Key
      description: 创建新的 Admin  Regular Key (创建 Regular Key 会自动附带一个 Server Key)
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateKeyRequest'
      responses:
        '201':
          description: 创建成功
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ApiKeyWithSecret'
                  - type: object
                    properties:
                      regularKey:
                        $ref: '#/components/schemas/ApiKeyWithSecret'
                      serverKey:
                        $ref: '#/components/schemas/ApiKeyWithSecret'

  /manage/keys/{key_id}:
    get:
      summary: 获取 API Key 详情
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: key_id
          schema:
            type: string
          required: true
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKey'
        '404':
          description: 未找到
    
    delete:
      summary: 删除 API Key
      description:  Admin 可操作,不能删除自己
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: key_id
          schema:
            type: string
          required: true
      responses:
        '204':
          description: 删除成功
        '400':
          description: 无法删除(如试图删除自己)

  /manage/keys/{key_id}/activate:
    patch:
      summary: 激活 API Key
      description: Admin 可激活任意 Key,Regular 仅可激活关联的 Server Key
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: key_id
          schema:
            type: string
          required: true
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string

  /manage/keys/{key_id}/deactivate:
    patch:
      summary: 停用 API Key
      description: Admin 可停用任意 Key,Regular 仅可停用关联的 Server Key
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: key_id
          schema:
            type: string
          required: true
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string

  /manage/keys/server-keys:
    get:
      summary: 获取 Server Key 列表
      description: Admin 获取所有 Server Key,Regular 获取关联的 Server Key
      security:
        - bearerAuth: []
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ApiKey'
    
    post:
      summary: 创建 Server Key
      description: 为指定的 Regular Key 创建新的 Server Key (仅 Admin)
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateServerKeyRequest'
      responses:
        '201':
          description: 创建成功
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyWithSecret'
        '400':
          description: 参数错误
        '404':
          description: Regular Key 不存在

  /api/events:
    post:
      summary: 发送事件
      description: 发送 Minecraft 事件并广播给所有 WebSocket 连接
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Event'
      responses:
        '200':
          description: 成功广播

  /api/server/info:
    get:
      summary: 获取服务器信息
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: server_id
          schema:
            type: string
          description:  Admin 需要
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  server_id:
                    type: string
                  status:
                    type: string
                  online_players:
                    type: integer

  /api/server/command:
    post:
      summary: 发送服务器命令
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CommandRequest'
      responses:
        '200':
          description: 命令发送成功
        '403':
          description: 禁止的命令

  /api/ai/config:
    get:
      summary: 获取 AI 配置 (Admin)
      security:
        - bearerAuth: []
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AiConfig'
    
    post:
      summary: 创建 AI 配置 (Admin)
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AiConfigUpdateRequest'
      responses:
        '201':
          description: 创建成功
    
    patch:
      summary: 更新 AI 配置 (Admin)
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AiConfigUpdateRequest'
      responses:
        '200':
          description: 更新成功
    
    delete:
      summary: 删除 AI 配置 (Admin)
      security:
        - bearerAuth: []
      responses:
        '204':
          description: 删除成功

  /api/ai/config/test:
    post:
      summary: 测试 AI 连接 (Admin)
      security:
        - bearerAuth: []
      responses:
        '200':
          description: 连接成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  message:
                    type: string

  /api/ai/chat:
    post:
      summary: AI 聊天
      description: 发送消息给 AI 并获取回复
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AiChatRequest'
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  reply:
                    type: string
                  model:
                    type: string