@rotifer/playground 0.5.0-alpha.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +159 -15
- package/LICENSE +1 -1
- package/README.md +51 -23
- package/README.zh.md +54 -25
- package/dist/cloud/auth.d.ts +7 -1
- package/dist/cloud/auth.d.ts.map +1 -1
- package/dist/cloud/auth.js +65 -1
- package/dist/cloud/auth.js.map +1 -1
- package/dist/cloud/client.d.ts +4 -1
- package/dist/cloud/client.d.ts.map +1 -1
- package/dist/cloud/client.js +9 -2
- package/dist/cloud/client.js.map +1 -1
- package/dist/cloud/types.d.ts +3 -1
- package/dist/cloud/types.d.ts.map +1 -1
- package/dist/cloud/types.js.map +1 -1
- package/dist/commands/agent-create.d.ts.map +1 -1
- package/dist/commands/agent-create.js +66 -3
- package/dist/commands/agent-create.js.map +1 -1
- package/dist/commands/agent-run.d.ts.map +1 -1
- package/dist/commands/agent-run.js +296 -32
- package/dist/commands/agent-run.js.map +1 -1
- package/dist/commands/arena-submit.d.ts.map +1 -1
- package/dist/commands/arena-submit.js +45 -17
- package/dist/commands/arena-submit.js.map +1 -1
- package/dist/commands/compile.d.ts.map +1 -1
- package/dist/commands/compile.js +9 -3
- package/dist/commands/compile.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +13 -3
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +23 -16
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/network.js +4 -4
- package/dist/commands/network.js.map +1 -1
- package/dist/commands/publish.d.ts.map +1 -1
- package/dist/commands/publish.js +162 -44
- package/dist/commands/publish.js.map +1 -1
- package/dist/commands/test.d.ts.map +1 -1
- package/dist/commands/test.js +209 -23
- package/dist/commands/test.js.map +1 -1
- package/dist/commands/wrap.d.ts.map +1 -1
- package/dist/commands/wrap.js +17 -1
- package/dist/commands/wrap.js.map +1 -1
- package/dist/index.js +0 -0
- package/dist/runtime/network-gateway.d.ts +53 -0
- package/dist/runtime/network-gateway.d.ts.map +1 -0
- package/dist/runtime/network-gateway.js +147 -0
- package/dist/runtime/network-gateway.js.map +1 -0
- package/dist/utils/binding.d.ts +25 -0
- package/dist/utils/binding.d.ts.map +1 -1
- package/dist/utils/binding.js.map +1 -1
- package/dist/utils/open-browser.d.ts +10 -0
- package/dist/utils/open-browser.d.ts.map +1 -0
- package/dist/utils/open-browser.js +23 -0
- package/dist/utils/open-browser.js.map +1 -0
- package/genes/academic-writer/.cloud-manifest.json +6 -0
- package/genes/academic-writer/.gene-manifest.json +8 -0
- package/genes/academic-writer/SKILL.md +274 -0
- package/genes/academic-writer/phenotype.json +28 -0
- package/genes/ai-components/.cloud-manifest.json +6 -0
- package/genes/ai-components/.gene-manifest.json +8 -0
- package/genes/ai-components/SKILL.md +381 -0
- package/genes/ai-components/phenotype.json +28 -0
- package/genes/algorithmic-art/.cloud-manifest.json +6 -0
- package/genes/algorithmic-art/.gene-manifest.json +8 -0
- package/genes/algorithmic-art/SKILL.md +405 -0
- package/genes/algorithmic-art/phenotype.json +28 -0
- package/genes/answer-synthesizer/.cloud-manifest.json +6 -0
- package/genes/answer-synthesizer/index.ts +194 -0
- package/genes/answer-synthesizer/phenotype.json +61 -0
- package/genes/api-designer/.cloud-manifest.json +6 -0
- package/genes/api-designer/.gene-manifest.json +8 -0
- package/genes/api-designer/SKILL.md +456 -0
- package/genes/api-designer/phenotype.json +28 -0
- package/genes/auto-coder/.cloud-manifest.json +6 -0
- package/genes/auto-coder/.gene-manifest.json +8 -0
- package/genes/auto-coder/SKILL.md +400 -0
- package/genes/auto-coder/phenotype.json +28 -0
- package/genes/auto-writer/.cloud-manifest.json +6 -0
- package/genes/auto-writer/.gene-manifest.json +8 -0
- package/genes/auto-writer/SKILL.md +361 -0
- package/genes/auto-writer/phenotype.json +28 -0
- package/genes/brand-personality/.cloud-manifest.json +6 -0
- package/genes/brand-personality/.gene-manifest.json +8 -0
- package/genes/brand-personality/SKILL.md +549 -0
- package/genes/brand-personality/phenotype.json +28 -0
- package/genes/business-writer/.cloud-manifest.json +6 -0
- package/genes/business-writer/.gene-manifest.json +8 -0
- package/genes/business-writer/SKILL.md +448 -0
- package/genes/business-writer/phenotype.json +28 -0
- package/genes/citation-manager/.cloud-manifest.json +6 -0
- package/genes/citation-manager/.gene-manifest.json +8 -0
- package/genes/citation-manager/SKILL.md +279 -0
- package/genes/citation-manager/index.ts +162 -0
- package/genes/citation-manager/package.json +1 -0
- package/genes/citation-manager/phenotype.json +50 -0
- package/genes/code-complexity/.cloud-manifest.json +6 -0
- package/genes/code-complexity/README.md +35 -0
- package/genes/code-complexity/index.ts +101 -0
- package/genes/code-complexity/phenotype.json +34 -0
- package/genes/copywriter/.cloud-manifest.json +6 -0
- package/genes/copywriter/.gene-manifest.json +8 -0
- package/genes/copywriter/SKILL.md +329 -0
- package/genes/copywriter/phenotype.json +28 -0
- package/genes/creative-writer/.cloud-manifest.json +6 -0
- package/genes/creative-writer/.gene-manifest.json +8 -0
- package/genes/creative-writer/SKILL.md +356 -0
- package/genes/creative-writer/phenotype.json +28 -0
- package/genes/data-modeler/.cloud-manifest.json +6 -0
- package/genes/data-modeler/.gene-manifest.json +8 -0
- package/genes/data-modeler/SKILL.md +486 -0
- package/genes/data-modeler/phenotype.json +28 -0
- package/genes/debugger/.cloud-manifest.json +6 -0
- package/genes/debugger/.gene-manifest.json +8 -0
- package/genes/debugger/SKILL.md +416 -0
- package/genes/debugger/phenotype.json +28 -0
- package/genes/design-tokens/.cloud-manifest.json +6 -0
- package/genes/design-tokens/.gene-manifest.json +8 -0
- package/genes/design-tokens/SKILL.md +222 -0
- package/genes/design-tokens/index.ts +128 -0
- package/genes/design-tokens/package.json +1 -0
- package/genes/design-tokens/phenotype.json +1 -0
- package/genes/devops-automator/.cloud-manifest.json +6 -0
- package/genes/devops-automator/.gene-manifest.json +8 -0
- package/genes/devops-automator/SKILL.md +490 -0
- package/genes/devops-automator/phenotype.json +28 -0
- package/genes/doc-coauthoring/.cloud-manifest.json +6 -0
- package/genes/doc-coauthoring/.gene-manifest.json +8 -0
- package/genes/doc-coauthoring/SKILL.md +375 -0
- package/genes/doc-coauthoring/phenotype.json +28 -0
- package/genes/doc-retrieval/.cloud-manifest.json +6 -0
- package/genes/doc-retrieval/index.ts +134 -0
- package/genes/doc-retrieval/phenotype.json +54 -0
- package/genes/docs-writer/.cloud-manifest.json +6 -0
- package/genes/docs-writer/.gene-manifest.json +8 -0
- package/genes/docs-writer/SKILL.md +492 -0
- package/genes/docs-writer/phenotype.json +28 -0
- package/genes/evolve-life/.cloud-manifest.json +6 -0
- package/genes/evolve-life/.compile-result.json +12 -0
- package/genes/evolve-life/README.md +52 -0
- package/genes/evolve-life/gene.ir.wasm +0 -0
- package/genes/evolve-life/gene.wasm +0 -0
- package/genes/evolve-life/index.ts +255 -0
- package/genes/evolve-life/phenotype.json +129 -0
- package/genes/evolve-life-bitwise/.cloud-manifest.json +6 -0
- package/genes/evolve-life-bitwise/.compile-result.json +12 -0
- package/genes/evolve-life-bitwise/gene.ir.wasm +0 -0
- package/genes/evolve-life-bitwise/gene.wasm +0 -0
- package/genes/evolve-life-bitwise/index.ts +273 -0
- package/genes/evolve-life-bitwise/phenotype.json +129 -0
- package/genes/evolve-life-sparse/.cloud-manifest.json +6 -0
- package/genes/evolve-life-sparse/.compile-result.json +12 -0
- package/genes/evolve-life-sparse/gene.ir.wasm +0 -0
- package/genes/evolve-life-sparse/gene.wasm +0 -0
- package/genes/evolve-life-sparse/index.ts +236 -0
- package/genes/evolve-life-sparse/phenotype.json +129 -0
- package/genes/fact-checker/.cloud-manifest.json +6 -0
- package/genes/fact-checker/.gene-manifest.json +8 -0
- package/genes/fact-checker/SKILL.md +373 -0
- package/genes/fact-checker/phenotype.json +28 -0
- package/genes/genesis-code-format/.cloud-manifest.json +6 -0
- package/genes/genesis-code-format/package.json +1 -0
- package/genes/genesis-code-format/phenotype.json +1 -0
- package/genes/genesis-file-read/.cloud-manifest.json +6 -0
- package/genes/genesis-file-read/index.ts +11 -1
- package/genes/genesis-file-read/package.json +1 -0
- package/genes/genesis-file-read/phenotype.json +1 -0
- package/genes/genesis-l0-constraint/.cloud-manifest.json +6 -0
- package/genes/genesis-l0-constraint/package.json +1 -0
- package/genes/genesis-l0-constraint/phenotype.json +1 -0
- package/genes/genesis-web-search/.cloud-manifest.json +2 -2
- package/genes/genesis-web-search/package.json +1 -0
- package/genes/genesis-web-search/phenotype.json +1 -0
- package/genes/genesis-web-search-lite/.cloud-manifest.json +6 -0
- package/genes/genesis-web-search-lite/package.json +1 -0
- package/genes/genesis-web-search-lite/phenotype.json +1 -0
- package/genes/git-workflow/.cloud-manifest.json +6 -0
- package/genes/git-workflow/.gene-manifest.json +8 -0
- package/genes/git-workflow/SKILL.md +407 -0
- package/genes/git-workflow/phenotype.json +28 -0
- package/genes/grammar-checker/.cloud-manifest.json +6 -0
- package/genes/grammar-checker/.gene-manifest.json +8 -0
- package/genes/grammar-checker/SKILL.md +194 -0
- package/genes/grammar-checker/index.ts +168 -0
- package/genes/grammar-checker/package.json +1 -0
- package/genes/grammar-checker/phenotype.json +52 -0
- package/genes/json-validator/.cloud-manifest.json +6 -0
- package/genes/json-validator/README.md +42 -0
- package/genes/json-validator/index.ts +112 -0
- package/genes/json-validator/phenotype.json +42 -0
- package/genes/license-advisor/.cloud-manifest.json +6 -0
- package/genes/license-advisor/.gene-manifest.json +8 -0
- package/genes/license-advisor/SKILL.md +117 -0
- package/genes/license-advisor/phenotype.json +28 -0
- package/genes/logic-architect/.cloud-manifest.json +6 -0
- package/genes/logic-architect/.gene-manifest.json +8 -0
- package/genes/logic-architect/SKILL.md +451 -0
- package/genes/logic-architect/phenotype.json +28 -0
- package/genes/markdown-formatter/.cloud-manifest.json +6 -0
- package/genes/markdown-formatter/README.md +34 -0
- package/genes/markdown-formatter/index.ts +86 -0
- package/genes/markdown-formatter/phenotype.json +32 -0
- package/genes/orch/.cloud-manifest.json +6 -0
- package/genes/orch/.gene-manifest.json +8 -0
- package/genes/orch/SKILL.md +504 -0
- package/genes/orch/phenotype.json +28 -0
- package/genes/particle-barneshut/.cloud-manifest.json +6 -0
- package/genes/particle-barneshut/.compile-result.json +12 -0
- package/genes/particle-barneshut/README.md +55 -0
- package/genes/particle-barneshut/gene.ir.wasm +0 -0
- package/genes/particle-barneshut/gene.wasm +0 -0
- package/genes/particle-barneshut/index.ts +486 -0
- package/genes/particle-barneshut/phenotype.json +137 -0
- package/genes/particle-brute/.cloud-manifest.json +6 -0
- package/genes/particle-brute/.compile-result.json +12 -0
- package/genes/particle-brute/README.md +55 -0
- package/genes/particle-brute/gene.ir.wasm +0 -0
- package/genes/particle-brute/gene.wasm +0 -0
- package/genes/particle-brute/index.ts +277 -0
- package/genes/particle-brute/phenotype.json +137 -0
- package/genes/particle-spatial/.cloud-manifest.json +6 -0
- package/genes/particle-spatial/.compile-result.json +12 -0
- package/genes/particle-spatial/README.md +53 -0
- package/genes/particle-spatial/gene.ir.wasm +0 -0
- package/genes/particle-spatial/gene.wasm +0 -0
- package/genes/particle-spatial/index.ts +352 -0
- package/genes/particle-spatial/phenotype.json +137 -0
- package/genes/performance-optimizer/.cloud-manifest.json +6 -0
- package/genes/performance-optimizer/.gene-manifest.json +8 -0
- package/genes/performance-optimizer/SKILL.md +480 -0
- package/genes/performance-optimizer/phenotype.json +28 -0
- package/genes/plagiarism-checker/.cloud-manifest.json +6 -0
- package/genes/plagiarism-checker/.gene-manifest.json +8 -0
- package/genes/plagiarism-checker/SKILL.md +342 -0
- package/genes/plagiarism-checker/phenotype.json +28 -0
- package/genes/product-manager/.cloud-manifest.json +6 -0
- package/genes/product-manager/.gene-manifest.json +8 -0
- package/genes/product-manager/SKILL.md +249 -0
- package/genes/product-manager/phenotype.json +28 -0
- package/genes/project-reviewer/.cloud-manifest.json +6 -0
- package/genes/project-reviewer/.gene-manifest.json +8 -0
- package/genes/project-reviewer/SKILL.md +312 -0
- package/genes/project-reviewer/phenotype.json +28 -0
- package/genes/prompt-engineer/.cloud-manifest.json +6 -0
- package/genes/prompt-engineer/.gene-manifest.json +8 -0
- package/genes/prompt-engineer/SKILL.md +411 -0
- package/genes/prompt-engineer/phenotype.json +28 -0
- package/genes/readability-analyzer/.cloud-manifest.json +6 -0
- package/genes/readability-analyzer/.gene-manifest.json +8 -0
- package/genes/readability-analyzer/SKILL.md +357 -0
- package/genes/readability-analyzer/index.ts +123 -0
- package/genes/readability-analyzer/package.json +1 -0
- package/genes/readability-analyzer/phenotype.json +35 -0
- package/genes/rotifer-protocol/SKILL.md +121 -0
- package/genes/security-auditor/.cloud-manifest.json +6 -0
- package/genes/security-auditor/.gene-manifest.json +8 -0
- package/genes/security-auditor/SKILL.md +494 -0
- package/genes/security-auditor/phenotype.json +28 -0
- package/genes/seo-optimizer/.cloud-manifest.json +6 -0
- package/genes/seo-optimizer/.gene-manifest.json +8 -0
- package/genes/seo-optimizer/SKILL.md +327 -0
- package/genes/seo-optimizer/index.ts +206 -0
- package/genes/seo-optimizer/package.json +1 -0
- package/genes/seo-optimizer/phenotype.json +1 -0
- package/genes/source-linker/.cloud-manifest.json +6 -0
- package/genes/source-linker/index.ts +88 -0
- package/genes/source-linker/phenotype.json +45 -0
- package/genes/style-optimizer/.cloud-manifest.json +6 -0
- package/genes/style-optimizer/.gene-manifest.json +8 -0
- package/genes/style-optimizer/SKILL.md +285 -0
- package/genes/style-optimizer/phenotype.json +28 -0
- package/genes/tech-lead/.cloud-manifest.json +6 -0
- package/genes/tech-lead/.gene-manifest.json +8 -0
- package/genes/tech-lead/SKILL.md +451 -0
- package/genes/tech-lead/phenotype.json +28 -0
- package/genes/test-wrap/.cloud-manifest.json +6 -0
- package/genes/test-wrap/.gene-manifest.json +8 -0
- package/genes/test-wrap/phenotype.json +28 -0
- package/genes/testing-strategist/.cloud-manifest.json +6 -0
- package/genes/testing-strategist/.gene-manifest.json +8 -0
- package/genes/testing-strategist/SKILL.md +500 -0
- package/genes/testing-strategist/phenotype.json +28 -0
- package/genes/text-summarizer/.cloud-manifest.json +6 -0
- package/genes/text-summarizer/README.md +34 -0
- package/genes/text-summarizer/index.ts +122 -0
- package/genes/text-summarizer/phenotype.json +32 -0
- package/genes/tone-analyzer/.cloud-manifest.json +6 -0
- package/genes/tone-analyzer/.gene-manifest.json +8 -0
- package/genes/tone-analyzer/SKILL.md +410 -0
- package/genes/tone-analyzer/phenotype.json +28 -0
- package/genes/translator/.cloud-manifest.json +6 -0
- package/genes/translator/.gene-manifest.json +8 -0
- package/genes/translator/SKILL.md +355 -0
- package/genes/translator/phenotype.json +28 -0
- package/genes/ui-components/.cloud-manifest.json +6 -0
- package/genes/ui-components/.gene-manifest.json +8 -0
- package/genes/ui-components/SKILL.md +467 -0
- package/genes/ui-components/phenotype.json +28 -0
- package/genes/uiux-designer/.cloud-manifest.json +6 -0
- package/genes/uiux-designer/.gene-manifest.json +8 -0
- package/genes/uiux-designer/SKILL.md +353 -0
- package/genes/uiux-designer/phenotype.json +28 -0
- package/genes/url-extractor/.cloud-manifest.json +6 -0
- package/genes/url-extractor/README.md +37 -0
- package/genes/url-extractor/index.ts +86 -0
- package/genes/url-extractor/phenotype.json +48 -0
- package/genes/ux-patterns/.cloud-manifest.json +6 -0
- package/genes/ux-patterns/.gene-manifest.json +8 -0
- package/genes/ux-patterns/SKILL.md +872 -0
- package/genes/ux-patterns/phenotype.json +28 -0
- package/genes/web3-components/.cloud-manifest.json +6 -0
- package/genes/web3-components/.gene-manifest.json +8 -0
- package/genes/web3-components/SKILL.md +390 -0
- package/genes/web3-components/phenotype.json +28 -0
- package/package.json +6 -5
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"domain": "ai.synthesis",
|
|
3
|
+
"description": "Synthesizes a structured answer from retrieved document chunks using an LLM. Provider-agnostic: supports Claude and OpenAI via environment configuration.",
|
|
4
|
+
"inputSchema": {
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"question": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"description": "Original user question"
|
|
10
|
+
},
|
|
11
|
+
"chunks": {
|
|
12
|
+
"type": "array",
|
|
13
|
+
"items": {
|
|
14
|
+
"type": "object",
|
|
15
|
+
"properties": {
|
|
16
|
+
"content": { "type": "string" },
|
|
17
|
+
"source": { "type": "string" },
|
|
18
|
+
"score": { "type": "number" }
|
|
19
|
+
},
|
|
20
|
+
"required": ["content", "source"]
|
|
21
|
+
},
|
|
22
|
+
"description": "Retrieved context chunks from doc-retrieval"
|
|
23
|
+
},
|
|
24
|
+
"provider": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"description": "LLM provider override: 'claude' or 'openai' (default from env ROTIFER_LLM_PROVIDER)"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"required": ["question", "chunks"]
|
|
30
|
+
},
|
|
31
|
+
"outputSchema": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"properties": {
|
|
34
|
+
"answer": { "type": "string" },
|
|
35
|
+
"sources": {
|
|
36
|
+
"type": "array",
|
|
37
|
+
"items": { "type": "string" }
|
|
38
|
+
},
|
|
39
|
+
"confidence": {
|
|
40
|
+
"type": "number",
|
|
41
|
+
"description": "0-1 confidence score based on chunk relevance"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"required": ["answer", "sources", "confidence"]
|
|
45
|
+
},
|
|
46
|
+
"dependencies": [],
|
|
47
|
+
"version": "0.1.0",
|
|
48
|
+
"author": "rotifer-team",
|
|
49
|
+
"createdAt": 1772236800000,
|
|
50
|
+
"fidelity": "Hybrid",
|
|
51
|
+
"transparency": "Open",
|
|
52
|
+
"network": {
|
|
53
|
+
"allowedDomains": [
|
|
54
|
+
"api.anthropic.com",
|
|
55
|
+
"api.openai.com"
|
|
56
|
+
],
|
|
57
|
+
"maxTimeoutMs": 30000,
|
|
58
|
+
"maxResponseBytes": 1048576,
|
|
59
|
+
"maxRequestsPerMin": 10
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: api-designer
|
|
3
|
+
description: Design RESTful and tRPC APIs with consistent patterns, error handling, and documentation. Define endpoint structure, response formats, and error codes. Use when designing APIs, creating endpoints, or when the user mentions API, endpoint, REST, tRPC, or response format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# API Designer (API 设计师)
|
|
7
|
+
|
|
8
|
+
**Goal**: 设计一致、可预测、易于使用的 API 接口。
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 1. RESTful API Design
|
|
13
|
+
|
|
14
|
+
### URL 结构
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
# 资源命名 (名词复数)
|
|
18
|
+
GET /api/users # 列表
|
|
19
|
+
GET /api/users/:id # 详情
|
|
20
|
+
POST /api/users # 创建
|
|
21
|
+
PUT /api/users/:id # 全量更新
|
|
22
|
+
PATCH /api/users/:id # 部分更新
|
|
23
|
+
DELETE /api/users/:id # 删除
|
|
24
|
+
|
|
25
|
+
# 嵌套资源
|
|
26
|
+
GET /api/users/:id/posts
|
|
27
|
+
POST /api/users/:id/posts
|
|
28
|
+
|
|
29
|
+
# 操作 (动词,仅在必要时)
|
|
30
|
+
POST /api/users/:id/activate
|
|
31
|
+
POST /api/chat/completions
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### HTTP 方法语义
|
|
35
|
+
|
|
36
|
+
| Method | 幂等 | 安全 | 用途 |
|
|
37
|
+
|--------|------|------|------|
|
|
38
|
+
| GET | ✅ | ✅ | 读取资源 |
|
|
39
|
+
| POST | ❌ | ❌ | 创建资源/执行操作 |
|
|
40
|
+
| PUT | ✅ | ❌ | 全量替换 |
|
|
41
|
+
| PATCH | ❌ | ❌ | 部分更新 |
|
|
42
|
+
| DELETE | ✅ | ❌ | 删除资源 |
|
|
43
|
+
|
|
44
|
+
### HTTP 状态码
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// 成功
|
|
48
|
+
200 OK // 通用成功
|
|
49
|
+
201 Created // 创建成功
|
|
50
|
+
204 No Content // 删除成功
|
|
51
|
+
|
|
52
|
+
// 客户端错误
|
|
53
|
+
400 Bad Request // 请求格式错误
|
|
54
|
+
401 Unauthorized // 未认证
|
|
55
|
+
403 Forbidden // 无权限
|
|
56
|
+
404 Not Found // 资源不存在
|
|
57
|
+
409 Conflict // 冲突 (如重复创建)
|
|
58
|
+
422 Unprocessable // 验证失败
|
|
59
|
+
429 Too Many Reqs // 限流
|
|
60
|
+
|
|
61
|
+
// 服务端错误
|
|
62
|
+
500 Internal Error // 服务器错误
|
|
63
|
+
502 Bad Gateway // 上游错误
|
|
64
|
+
503 Unavailable // 服务不可用
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 2. Response Format
|
|
70
|
+
|
|
71
|
+
### 成功响应
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// 单个资源
|
|
75
|
+
{
|
|
76
|
+
"data": {
|
|
77
|
+
"id": "123",
|
|
78
|
+
"type": "user",
|
|
79
|
+
"attributes": {
|
|
80
|
+
"name": "张三",
|
|
81
|
+
"email": "zhang@example.com"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 简化版 (推荐 OPC)
|
|
87
|
+
{
|
|
88
|
+
"id": "123",
|
|
89
|
+
"name": "张三",
|
|
90
|
+
"email": "zhang@example.com"
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 列表响应
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
{
|
|
98
|
+
"data": [...],
|
|
99
|
+
"pagination": {
|
|
100
|
+
"page": 1,
|
|
101
|
+
"pageSize": 20,
|
|
102
|
+
"total": 100,
|
|
103
|
+
"totalPages": 5
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 错误响应
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
{
|
|
112
|
+
"error": {
|
|
113
|
+
"code": "VALIDATION_ERROR",
|
|
114
|
+
"message": "请求参数验证失败",
|
|
115
|
+
"details": [
|
|
116
|
+
{ "field": "email", "message": "邮箱格式不正确" },
|
|
117
|
+
{ "field": "password", "message": "密码至少 8 位" }
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## 3. Error Code System
|
|
126
|
+
|
|
127
|
+
### 错误码设计
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// types/errors.ts
|
|
131
|
+
export const ERROR_CODES = {
|
|
132
|
+
// 认证 (1xxx)
|
|
133
|
+
AUTH_REQUIRED: { code: 1001, status: 401, message: '请先登录' },
|
|
134
|
+
AUTH_EXPIRED: { code: 1002, status: 401, message: '登录已过期' },
|
|
135
|
+
AUTH_INVALID: { code: 1003, status: 401, message: '认证信息无效' },
|
|
136
|
+
|
|
137
|
+
// 权限 (2xxx)
|
|
138
|
+
FORBIDDEN: { code: 2001, status: 403, message: '无权限访问' },
|
|
139
|
+
|
|
140
|
+
// 资源 (3xxx)
|
|
141
|
+
NOT_FOUND: { code: 3001, status: 404, message: '资源不存在' },
|
|
142
|
+
ALREADY_EXISTS: { code: 3002, status: 409, message: '资源已存在' },
|
|
143
|
+
|
|
144
|
+
// 验证 (4xxx)
|
|
145
|
+
VALIDATION_ERROR: { code: 4001, status: 422, message: '参数验证失败' },
|
|
146
|
+
INVALID_FORMAT: { code: 4002, status: 400, message: '请求格式错误' },
|
|
147
|
+
|
|
148
|
+
// 限流 (5xxx)
|
|
149
|
+
RATE_LIMITED: { code: 5001, status: 429, message: '请求过于频繁' },
|
|
150
|
+
|
|
151
|
+
// AI 相关 (6xxx)
|
|
152
|
+
AI_TOKEN_LIMIT: { code: 6001, status: 400, message: 'Token 超出限制' },
|
|
153
|
+
AI_TIMEOUT: { code: 6002, status: 504, message: 'AI 响应超时' },
|
|
154
|
+
AI_UNAVAILABLE: { code: 6003, status: 503, message: 'AI 服务不可用' },
|
|
155
|
+
|
|
156
|
+
// Web3 相关 (7xxx)
|
|
157
|
+
WALLET_NOT_CONNECTED: { code: 7001, status: 400, message: '钱包未连接' },
|
|
158
|
+
INVALID_SIGNATURE: { code: 7002, status: 401, message: '签名验证失败' },
|
|
159
|
+
INSUFFICIENT_BALANCE: { code: 7003, status: 400, message: '余额不足' },
|
|
160
|
+
|
|
161
|
+
// 服务器 (9xxx)
|
|
162
|
+
INTERNAL_ERROR: { code: 9001, status: 500, message: '服务器错误' },
|
|
163
|
+
} as const
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 错误处理工具
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
// lib/api/error.ts
|
|
170
|
+
import { ERROR_CODES } from '@/types/errors'
|
|
171
|
+
|
|
172
|
+
export class APIError extends Error {
|
|
173
|
+
constructor(
|
|
174
|
+
public code: keyof typeof ERROR_CODES,
|
|
175
|
+
public details?: unknown
|
|
176
|
+
) {
|
|
177
|
+
super(ERROR_CODES[code].message)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
toResponse() {
|
|
181
|
+
const { code: errorCode, status, message } = ERROR_CODES[this.code]
|
|
182
|
+
return Response.json(
|
|
183
|
+
{ error: { code: this.code, message, details: this.details } },
|
|
184
|
+
{ status }
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 使用
|
|
190
|
+
throw new APIError('VALIDATION_ERROR', [
|
|
191
|
+
{ field: 'email', message: '邮箱格式不正确' }
|
|
192
|
+
])
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 4. Query Parameters
|
|
198
|
+
|
|
199
|
+
### 分页
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
GET /api/users?page=1&pageSize=20
|
|
203
|
+
GET /api/users?cursor=abc123&limit=20 # 游标分页
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### 过滤
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
GET /api/users?status=active
|
|
210
|
+
GET /api/users?role=admin,user
|
|
211
|
+
GET /api/users?createdAt[gte]=2024-01-01
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### 排序
|
|
215
|
+
|
|
216
|
+
```
|
|
217
|
+
GET /api/users?sort=createdAt # 升序
|
|
218
|
+
GET /api/users?sort=-createdAt # 降序
|
|
219
|
+
GET /api/users?sort=-createdAt,name # 多字段
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 字段选择
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
GET /api/users?fields=id,name,email
|
|
226
|
+
GET /api/users?include=posts,comments
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 5. API Versioning
|
|
232
|
+
|
|
233
|
+
### URL 版本 (推荐)
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
/api/v1/users
|
|
237
|
+
/api/v2/users
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 版本迁移策略
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
// 同时支持多版本
|
|
244
|
+
// app/api/v1/users/route.ts
|
|
245
|
+
export async function GET() {
|
|
246
|
+
return formatV1Response(users)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// app/api/v2/users/route.ts
|
|
250
|
+
export async function GET() {
|
|
251
|
+
return formatV2Response(users)
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## 6. Next.js API Routes
|
|
258
|
+
|
|
259
|
+
### 基础结构
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
// app/api/users/route.ts
|
|
263
|
+
import { NextRequest } from 'next/server'
|
|
264
|
+
import { z } from 'zod'
|
|
265
|
+
import { APIError } from '@/lib/api/error'
|
|
266
|
+
|
|
267
|
+
const CreateUserSchema = z.object({
|
|
268
|
+
name: z.string().min(1),
|
|
269
|
+
email: z.string().email(),
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
export async function POST(request: NextRequest) {
|
|
273
|
+
try {
|
|
274
|
+
const body = await request.json()
|
|
275
|
+
const data = CreateUserSchema.parse(body)
|
|
276
|
+
|
|
277
|
+
const user = await createUser(data)
|
|
278
|
+
|
|
279
|
+
return Response.json(user, { status: 201 })
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (error instanceof z.ZodError) {
|
|
282
|
+
return new APIError('VALIDATION_ERROR', error.errors).toResponse()
|
|
283
|
+
}
|
|
284
|
+
if (error instanceof APIError) {
|
|
285
|
+
return error.toResponse()
|
|
286
|
+
}
|
|
287
|
+
return new APIError('INTERNAL_ERROR').toResponse()
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### 动态路由
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// app/api/users/[id]/route.ts
|
|
296
|
+
export async function GET(
|
|
297
|
+
request: NextRequest,
|
|
298
|
+
{ params }: { params: { id: string } }
|
|
299
|
+
) {
|
|
300
|
+
const user = await findUser(params.id)
|
|
301
|
+
|
|
302
|
+
if (!user) {
|
|
303
|
+
return new APIError('NOT_FOUND').toResponse()
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return Response.json(user)
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### 中间件模式
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
// lib/api/middleware.ts
|
|
314
|
+
type Handler = (req: NextRequest, ctx: any) => Promise<Response>
|
|
315
|
+
|
|
316
|
+
export function withAuth(handler: Handler): Handler {
|
|
317
|
+
return async (req, ctx) => {
|
|
318
|
+
const token = req.headers.get('authorization')?.replace('Bearer ', '')
|
|
319
|
+
|
|
320
|
+
if (!token) {
|
|
321
|
+
return new APIError('AUTH_REQUIRED').toResponse()
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const user = await verifyToken(token)
|
|
325
|
+
if (!user) {
|
|
326
|
+
return new APIError('AUTH_INVALID').toResponse()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// 注入用户信息
|
|
330
|
+
;(req as any).user = user
|
|
331
|
+
|
|
332
|
+
return handler(req, ctx)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 使用
|
|
337
|
+
export const GET = withAuth(async (req) => {
|
|
338
|
+
const user = (req as any).user
|
|
339
|
+
return Response.json({ user })
|
|
340
|
+
})
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## 7. tRPC Design (Alternative)
|
|
346
|
+
|
|
347
|
+
### 定义 Router
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
// server/routers/user.ts
|
|
351
|
+
import { z } from 'zod'
|
|
352
|
+
import { router, publicProcedure, protectedProcedure } from '../trpc'
|
|
353
|
+
|
|
354
|
+
export const userRouter = router({
|
|
355
|
+
list: publicProcedure
|
|
356
|
+
.input(z.object({
|
|
357
|
+
page: z.number().default(1),
|
|
358
|
+
pageSize: z.number().default(20),
|
|
359
|
+
}))
|
|
360
|
+
.query(async ({ input }) => {
|
|
361
|
+
return getUsers(input)
|
|
362
|
+
}),
|
|
363
|
+
|
|
364
|
+
create: protectedProcedure
|
|
365
|
+
.input(z.object({
|
|
366
|
+
name: z.string(),
|
|
367
|
+
email: z.string().email(),
|
|
368
|
+
}))
|
|
369
|
+
.mutation(async ({ input, ctx }) => {
|
|
370
|
+
return createUser(input, ctx.user)
|
|
371
|
+
}),
|
|
372
|
+
})
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### 合并 Router
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
// server/routers/_app.ts
|
|
379
|
+
export const appRouter = router({
|
|
380
|
+
user: userRouter,
|
|
381
|
+
chat: chatRouter,
|
|
382
|
+
wallet: walletRouter,
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
export type AppRouter = typeof appRouter
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
---
|
|
389
|
+
|
|
390
|
+
## 8. Streaming API (AI)
|
|
391
|
+
|
|
392
|
+
### Server-Sent Events
|
|
393
|
+
|
|
394
|
+
```typescript
|
|
395
|
+
// app/api/chat/route.ts
|
|
396
|
+
export async function POST(request: NextRequest) {
|
|
397
|
+
const { messages } = await request.json()
|
|
398
|
+
|
|
399
|
+
const stream = new ReadableStream({
|
|
400
|
+
async start(controller) {
|
|
401
|
+
const encoder = new TextEncoder()
|
|
402
|
+
|
|
403
|
+
for await (const chunk of streamChat(messages)) {
|
|
404
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`))
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
controller.enqueue(encoder.encode('data: [DONE]\n\n'))
|
|
408
|
+
controller.close()
|
|
409
|
+
},
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
return new Response(stream, {
|
|
413
|
+
headers: {
|
|
414
|
+
'Content-Type': 'text/event-stream',
|
|
415
|
+
'Cache-Control': 'no-cache',
|
|
416
|
+
'Connection': 'keep-alive',
|
|
417
|
+
},
|
|
418
|
+
})
|
|
419
|
+
}
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
---
|
|
423
|
+
|
|
424
|
+
## Quick Reference
|
|
425
|
+
|
|
426
|
+
### 端点命名
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
✅ /api/users (复数名词)
|
|
430
|
+
✅ /api/user-profiles (kebab-case)
|
|
431
|
+
❌ /api/getUsers (不用动词)
|
|
432
|
+
❌ /api/user (不用单数)
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### 状态码速查
|
|
436
|
+
|
|
437
|
+
```
|
|
438
|
+
200 - 成功 (读取/更新)
|
|
439
|
+
201 - 创建成功
|
|
440
|
+
204 - 删除成功
|
|
441
|
+
400 - 请求错误
|
|
442
|
+
401 - 未认证
|
|
443
|
+
403 - 无权限
|
|
444
|
+
404 - 不存在
|
|
445
|
+
422 - 验证失败
|
|
446
|
+
429 - 限流
|
|
447
|
+
500 - 服务器错误
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### 推荐库
|
|
451
|
+
|
|
452
|
+
```
|
|
453
|
+
zod - 验证
|
|
454
|
+
@trpc/server - tRPC
|
|
455
|
+
@upstash/ratelimit - 限流
|
|
456
|
+
```
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"domain": "code.api",
|
|
3
|
+
"description": "Design RESTful and tRPC APIs with consistent patterns, error handling, and documentation. Define endpoint structure, response formats, and error codes. Use when designing APIs, creating endpoints, or when the user mentions API, endpoint, REST, tRPC, or response format.",
|
|
4
|
+
"inputSchema": {
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"prompt": {
|
|
8
|
+
"type": "string"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"required": []
|
|
12
|
+
},
|
|
13
|
+
"outputSchema": {
|
|
14
|
+
"type": "object",
|
|
15
|
+
"properties": {
|
|
16
|
+
"result": {
|
|
17
|
+
"type": "string"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"dependencies": [],
|
|
22
|
+
"version": "0.1.0",
|
|
23
|
+
"author": "rotifer-team",
|
|
24
|
+
"createdAt": 1771939408115,
|
|
25
|
+
"fidelity": "Wrapped",
|
|
26
|
+
"transparency": "Open",
|
|
27
|
+
"source": "skill"
|
|
28
|
+
}
|