crabmate 0.5.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
Documentation
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! OpenAPI `paths` 对象(由 `openapi::build_openapi_spec` 组装);多段 `json!` 合并,降低单函数 `nloc`。

use serde_json::{Map, Value, json};
use super::{
    openapi_paths_chat_stream, openapi_paths_tool_jobs, openapi_paths_user_data,
    openapi_paths_user_data_mcp, openapi_paths_workspace,
};

fn merge_path_fragments(fragments: &[Value]) -> Value {
    let mut map = Map::new();
    for fragment in fragments {
        let Value::Object(o) = fragment else {
            panic!("openapi path fragment must be a JSON object");
        };
        for (k, v) in o {
            if map.insert(k.clone(), v.clone()).is_some() {
                panic!("duplicate OpenAPI path key: {k}");
            }
        }
    }
    Value::Object(map)
}

fn openapi_paths_fragment_system() -> Value {
    json!({
        "/openapi.json": {
            "get": {
                "tags": ["system"],
                "summary": "OpenAPI 本文档",
                "responses": {
                    "200": {
                        "description": "OpenAPI 3.0 JSON",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    }
                }
            }
        },
        "/health": {
            "get": {
                "tags": ["system"],
                "summary": "健康检查(依赖、可选 LLM models 探活等)",
                "responses": {
                    "200": {
                        "description": "健康报告 JSON",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    }
                }
            }
        },
        "/status": {
            "get": {
                "tags": ["system"],
                "summary": "运行状态(模型、工具数、会话模式默认、规划配置等)",
                "parameters": [
                    {
                        "name": "view",
                        "in": "query",
                        "required": false,
                        "schema": { "type": "string", "enum": ["shell"] },
                        "description": "为 shell 时返回 Web 壳层稳定子集 StatusShellView;省略则返回完整运行状态(StatusResponseBody)"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "状态 JSON",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "oneOf": [
                                        { "$ref": "#/components/schemas/StatusShellView" },
                                        { "$ref": "#/components/schemas/StatusResponseBody" }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        },
        "/web-ui": {
            "get": {
                "tags": ["system"],
                "summary": "CSR 展示开关(Markdown、助手展示过滤;受 CM_WEB_* 环境变量影响)",
                "responses": {
                    "200": {
                        "description": "Web UI 配置 JSON",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WebUiConfigResponse" }
                            }
                        }
                    }
                }
            }
        },
    })
}

fn openapi_paths_fragment_chat_core() -> Value {
    json!({
        "/chat": {
            "post": {
                "tags": ["chat"],
                "summary": "非流式对话",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/ChatRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "助手回复",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ChatResponseBody" }
                            }
                        }
                    },
                    "4XX": { "description": "业务或参数错误", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } } },
                    "5XX": { "description": "服务器错误" }
                }
            }
        },
        "/chat/stream": {
            "post": {
                "tags": ["chat"],
                "summary": "SSE 流式对话",
                "description": "响应 `Content-Type: text/event-stream`;成功时响应头可含 `x-conversation-id`。事件载荷见 `docs/SSE协议.md`。",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/ChatRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "SSE 字节流",
                        "content": { "text/event-stream": { "schema": { "type": "string", "format": "binary" } } }
                    },
                    "4XX": { "description": "错误(部分场景仍为 SSE 控制面 `error` 事件)" },
                    "5XX": { "description": "服务器错误" }
                }
            }
        }
    })
}

fn openapi_paths_fragment_chat_async() -> Value {
    json!({
        "/chat/async": {
            "post": {
                "tags": ["chat"],
                "summary": "异步非流式对话(轮询 / Webhook)",
                "description": "立即返回 job_id;执行与 POST /chat 相同 JSON 队列路径。状态仅存本进程内存,serve 重启丢失。需工具审批请用 POST /chat/stream。",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/ChatAsyncRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "已接受",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ChatAsyncSubmitResponseBody" }
                            }
                        }
                    },
                    "4XX": { "description": "参数或业务错误", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } } },
                    "503": { "description": "队列满(QUEUE_FULL)" }
                }
            }
        },
        "/chat/jobs/{job_id}": {
            "get": {
                "tags": ["chat"],
                "summary": "查询异步对话任务状态",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "job_id",
                        "in": "path",
                        "required": true,
                        "schema": { "type": "integer", "format": "int64" }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "任务状态",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ChatJobStatusResponseBody" }
                            }
                        }
                    },
                    "404": { "description": "未知 job_id(UNKNOWN_JOB)" }
                }
            }
        }
    })
}

fn openapi_paths_fragment_chat_extras() -> Value {
    json!({
        "/chat/approval": {
            "post": {
                "tags": ["chat"],
                "summary": "工具/HTTP 审批决策",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/ChatApprovalRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "审批结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ChatApprovalResponseBody" }
                            }
                        }
                    }
                }
            }
        },
        "/chat/branch": {
            "post": {
                "tags": ["chat"],
                "summary": "会话分叉截断(持久化会话)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/ChatBranchRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "截断结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ChatBranchResponseBody" }
                            }
                        }
                    }
                }
            }
        },
        "/conversation/messages": {
            "get": {
                "tags": ["chat"],
                "summary": "只读拉取已持久化会话消息与 revision",
                "description": "供 Web 刷新后与 `conversation_id` 对齐;不含长期记忆/变更集/首轮工作区画像注入;404 表示不存在或已过期。",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "conversation_id",
                        "in": "query",
                        "required": true,
                        "schema": { "type": "string" }
                    },
                    {
                        "name": "limit",
                        "in": "query",
                        "required": false,
                        "description": "分页每页条数;省略或 0 表示返回过滤后的全量(兼容旧客户端)",
                        "schema": { "type": "integer", "format": "int32", "minimum": 0 }
                    },
                    {
                        "name": "before_index",
                        "in": "query",
                        "required": false,
                        "description": "取该下标之前的更早消息;省略表示尾部一页",
                        "schema": { "type": "integer", "format": "int32", "minimum": 0 }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "会话快照",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ConversationMessagesResponseBody" }
                            }
                        }
                    },
                    "400": { "description": "参数错误" },
                    "404": { "description": "会话不存在或已过期" }
                }
            }
        },
        "/upload": {
            "post": {
                "tags": ["uploads"],
                "summary": "multipart 文件上传",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "multipart/form-data": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "file": { "type": "string", "format": "binary" }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "上传结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/UploadResponseBody" }
                            }
                        }
                    }
                }
            }
        },
        "/uploads/{filename}": {
            "get": {
                "tags": ["uploads"],
                "summary": "读取聊天附图字节(与其它受保护 API 同鉴权)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "filename",
                        "in": "path",
                        "required": true,
                        "schema": { "type": "string" },
                        "description": "单段文件名(与 POST /upload 返回的 /uploads/<filename> 一致)"
                    }
                ],
                "responses": {
                    "200": { "description": "文件字节" },
                    "400": { "description": "非法文件名" },
                    "404": { "description": "不存在或已过期" }
                }
            }
        },
        "/uploads/delete": {
            "post": {
                "tags": ["uploads"],
                "summary": "按 URL 删除已上传文件",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/DeleteUploadsBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "删除结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/DeleteUploadsResponseBody" }
                            }
                        }
                    }
                }
            }
        },
    })
}

fn openapi_paths_fragment_workspace_list() -> Value {
    json!({
        "/workspace": {
            "get": {
                "tags": ["workspace"],
                "summary": "列出工作区目录项",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "path",
                        "in": "query",
                        "required": false,
                        "schema": { "type": "string" },
                        "description": "相对子路径,可选"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "目录列表",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceResponse" }
                            }
                        }
                    }
                }
            },
            "post": {
                "tags": ["workspace"],
                "summary": "设置当前 Web 工作区根",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/WorkspaceSetBody" }
                        }
                    }
                },
                "responses": {
                    "200": { "description": "设置结果(JSON,形状与实现一致)" }
                }
            }
        },
        "/workspace/pick": {
            "get": {
                "tags": ["workspace"],
                "summary": "工作区选目录占位(始终 path=null;请在侧栏手动填写路径)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "所选路径或 null",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspacePickResponse" }
                            }
                        }
                    }
                }
            }
        },
        "/workspace/projects": {
            "get": {
                "tags": ["workspace"],
                "summary": "列出 Web 项目池状态与项目名",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "enabled / pool_path / projects"
                    }
                }
            },
            "post": {
                "tags": ["workspace"],
                "summary": "按名称打开或新建项目池工作区",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": { "description": "切换结果(含 path)" }
                }
            }
        },
        "/workspace/clone/stream": {
            "post": {
                "tags": ["workspace"],
                "summary": "在项目池内 git clone(SSE 进度;成功后切换当前工作区)",
                "description": "仅当已配置 web_workspace_pool。请求 JSON:url、name,可选 depth、branch。成功路径:phase → log/progress → phase=activate → done(name,path)。墙钟超时 20 分钟。",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": ["url", "name"],
                                "properties": {
                                    "url": { "type": "string" },
                                    "name": { "type": "string" },
                                    "depth": { "type": "integer", "minimum": 1 },
                                    "branch": { "type": "string" }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "text/event-stream(phase/log/progress/done/error)"
                    },
                    "400": { "description": "CLONE_BAD_URL / CLONE_BAD_NAME / CLONE_BAD_DEPTH 等" },
                    "404": { "description": "CLONE_NO_POOL" },
                    "409": { "description": "CLONE_DIR_EXISTS" },
                    "429": { "description": "CLONE_BUSY(并发上限)" },
                    "503": { "description": "CLONE_NO_GIT" }
                }
            }
        },
        "/workspace/search": {
            "post": {
                "tags": ["workspace"],
                "summary": "工作区内搜索",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/WorkspaceSearchBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "搜索结果文本",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceSearchResponse" }
                            }
                        }
                    }
                }
            }
        },
    })
}

fn openapi_paths_fragment_workspace_file_meta() -> Value {
    json!({
        "/workspace/file": {
            "get": {
                "tags": ["workspace"],
                "summary": "读取工作区内文本文件(有大小上限)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "path",
                        "in": "query",
                        "required": true,
                        "schema": { "type": "string" }
                    },
                    {
                        "name": "encoding",
                        "in": "query",
                        "required": false,
                        "schema": { "type": "string" },
                        "description": "如 utf-8、gb18030、auto 等,与 `read_file` 工具一致"
                    }
                ],
                "responses": {
                    "200": { "description": "文件正文或 JSON 包装(与实现一致)" },
                    "4XX": { "description": "路径或编码错误" }
                }
            },
            "post": {
                "tags": ["workspace"],
                "summary": "写入工作区文件",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/WorkspaceFileWriteBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "写入结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceFileWriteResponse" }
                            }
                        }
                    }
                }
            },
            "delete": {
                "tags": ["workspace"],
                "summary": "删除工作区文件",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "path",
                        "in": "query",
                        "required": true,
                        "schema": { "type": "string" }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "删除结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceFileDeleteResponse" }
                            }
                        }
                    }
                }
            }
        },
        "/workspace/dir": {
            "post": {
                "tags": ["workspace"],
                "summary": "在工作区内创建目录,或通过 JSON delete=true 删除目录(与 DELETE 等价)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/WorkspaceDirCreateBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "创建结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceDirCreateResponse" }
                            }
                        }
                    }
                }
            },
            "delete": {
                "tags": ["workspace"],
                "summary": "删除工作区目录(须 confirm=true;非空目录须 recursive=true)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "path",
                        "in": "query",
                        "required": true,
                        "schema": { "type": "string" }
                    },
                    {
                        "name": "confirm",
                        "in": "query",
                        "required": true,
                        "schema": { "type": "boolean" }
                    },
                    {
                        "name": "recursive",
                        "in": "query",
                        "required": false,
                        "schema": { "type": "boolean" }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "删除结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceDirDeleteResponse" }
                            }
                        }
                    }
                }
            }
        },
        "/workspace/profile": {
            "get": {
                "tags": ["workspace"],
                "summary": "项目画像 Markdown",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "画像",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceProfileResponse" }
                            }
                        }
                    }
                }
            }
        },
        "/workspace/changelog": {
            "get": {
                "tags": ["workspace"],
                "summary": "本会话工作区变更集 Markdown",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "conversation_id",
                        "in": "query",
                        "required": false,
                        "schema": { "type": "string" }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "变更集",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/WorkspaceChangelogResponse" }
                            }
                        }
                    }
                }
            }
        },
    })
}

fn openapi_paths_fragment_workspace_tasks_and_config() -> Value {
    json!({
        "/tasks": {
            "get": {
                "tags": ["tasks"],
                "summary": "读取当前工作区任务清单(进程内存)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "任务数据",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/TasksData" }
                            }
                        }
                    }
                }
            },
            "post": {
                "tags": ["tasks"],
                "summary": "保存当前工作区任务清单",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/TasksData" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "回显保存后的数据",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/TasksData" }
                            }
                        }
                    }
                }
            }
        },
        "/config/reload": {
            "post": {
                "tags": ["config"],
                "summary": "热重载 AgentConfig(不含部分字段,见 CONFIGURATION.md)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": { "type": "object" },
                            "example": {}
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "重载结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ConfigReloadResponseBody" }
                            }
                        }
                    }
                }
            }
        },
        "/config/session/conversation-store": {
            "post": {
                "tags": ["config"],
                "summary": "切换 Web 会话存储后端(内存 / SQLite,仅当前进程)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": { "$ref": "#/components/schemas/SessionConversationStoreRequestBody" }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "切换结果",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/ConfigReloadResponseBody" }
                            }
                        }
                    }
                }
            }
        },
    })
}

fn openapi_paths_fragment_github() -> Value {
    json!({
        "/github/repo-context": {
            "get": {
                "tags": ["github"],
                "summary": "GitHub 仓库上下文(只读;优先 gh,失败时从 git remote 解析 URL)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "仓库与分支摘要",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    }
                }
            }
        },
        "/github/pr/current/checks": {
            "get": {
                "tags": ["github"],
                "summary": "当前分支关联 PR 的 CI checks(只读)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": {
                    "200": {
                        "description": "checks 摘要",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    }
                }
            }
        },
        "/github/oauth/device/start": {
            "post": {
                "tags": ["github"],
                "summary": "启动 GitHub Device Flow(body.client_id 必填;不返回 device_code)",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": ["client_id"],
                                "properties": {
                                    "client_id": { "type": "string" }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "user_code 与 verification_uri_complete 等",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    },
                    "400": { "description": "GITHUB_OAUTH_CLIENT_ID_REQUIRED" }
                }
            }
        },
        "/github/oauth/device/status": {
            "get": {
                "tags": ["github"],
                "summary": "Device Flow 状态;成功时 Set-Cookie crabmate_github_token;头 X-CrabMate-GitHub-Token-Delivery: body 时 JSON 含一次性 access_token",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "parameters": [
                    {
                        "name": "X-CrabMate-GitHub-Token-Delivery",
                        "in": "header",
                        "required": false,
                        "schema": { "type": "string", "enum": ["body"] }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "state / login / error;壳可选 access_token",
                        "content": { "application/json": { "schema": { "type": "object" } } }
                    }
                }
            }
        },
        "/github/oauth/device/cancel": {
            "post": {
                "tags": ["github"],
                "summary": "取消当前 Device 会话",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": { "204": { "description": "已取消" } }
            }
        },
        "/github/oauth/device/logout": {
            "post": {
                "tags": ["github"],
                "summary": "清除浏览器 HttpOnly Cookie crabmate_github_token",
                "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
                "responses": { "204": { "description": "已清除 Cookie" } }
            }
        },
    })
}

pub(super) fn openapi_paths_value() -> Value {
    merge_path_fragments(&[
        openapi_paths_fragment_system(),
        openapi_paths_fragment_chat_core(),
        openapi_paths_chat_stream::openapi_paths_fragment_chat_stream_cancel(),
        openapi_paths_fragment_chat_async(),
        openapi_paths_fragment_chat_extras(),
        openapi_paths_fragment_workspace_list(),
        openapi_paths_workspace::openapi_paths_fragment_skills(),
        openapi_paths_fragment_github(),
        openapi_paths_workspace::openapi_paths_fragment_workspace_file_raw(),
        openapi_paths_fragment_workspace_file_meta(),
        openapi_paths_fragment_workspace_tasks_and_config(),
        openapi_paths_tool_jobs::openapi_paths_fragment_tool_jobs(),
        openapi_paths_user_data::openapi_paths_fragment_user_data(),
        openapi_paths_user_data_mcp::openapi_paths_fragment_user_data_mcp(),
    ])
}