@dangao/bun-server 1.12.1 → 2.0.1

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.
Files changed (214) hide show
  1. package/README.md +32 -0
  2. package/dist/ai/ai-module.d.ts +24 -0
  3. package/dist/ai/ai-module.d.ts.map +1 -0
  4. package/dist/ai/decorators.d.ts +25 -0
  5. package/dist/ai/decorators.d.ts.map +1 -0
  6. package/dist/ai/errors.d.ts +39 -0
  7. package/dist/ai/errors.d.ts.map +1 -0
  8. package/dist/ai/index.d.ts +12 -0
  9. package/dist/ai/index.d.ts.map +1 -0
  10. package/dist/ai/providers/anthropic-provider.d.ts +23 -0
  11. package/dist/ai/providers/anthropic-provider.d.ts.map +1 -0
  12. package/dist/ai/providers/google-provider.d.ts +20 -0
  13. package/dist/ai/providers/google-provider.d.ts.map +1 -0
  14. package/dist/ai/providers/ollama-provider.d.ts +17 -0
  15. package/dist/ai/providers/ollama-provider.d.ts.map +1 -0
  16. package/dist/ai/providers/openai-provider.d.ts +28 -0
  17. package/dist/ai/providers/openai-provider.d.ts.map +1 -0
  18. package/dist/ai/service.d.ts +40 -0
  19. package/dist/ai/service.d.ts.map +1 -0
  20. package/dist/ai/tools/tool-executor.d.ts +15 -0
  21. package/dist/ai/tools/tool-executor.d.ts.map +1 -0
  22. package/dist/ai/tools/tool-registry.d.ts +39 -0
  23. package/dist/ai/tools/tool-registry.d.ts.map +1 -0
  24. package/dist/ai/types.d.ts +134 -0
  25. package/dist/ai/types.d.ts.map +1 -0
  26. package/dist/ai-guard/ai-guard-module.d.ts +18 -0
  27. package/dist/ai-guard/ai-guard-module.d.ts.map +1 -0
  28. package/dist/ai-guard/decorators.d.ts +16 -0
  29. package/dist/ai-guard/decorators.d.ts.map +1 -0
  30. package/dist/ai-guard/detectors/content-moderator.d.ts +26 -0
  31. package/dist/ai-guard/detectors/content-moderator.d.ts.map +1 -0
  32. package/dist/ai-guard/detectors/injection-detector.d.ts +13 -0
  33. package/dist/ai-guard/detectors/injection-detector.d.ts.map +1 -0
  34. package/dist/ai-guard/detectors/pii-detector.d.ts +11 -0
  35. package/dist/ai-guard/detectors/pii-detector.d.ts.map +1 -0
  36. package/dist/ai-guard/index.d.ts +8 -0
  37. package/dist/ai-guard/index.d.ts.map +1 -0
  38. package/dist/ai-guard/service.d.ts +21 -0
  39. package/dist/ai-guard/service.d.ts.map +1 -0
  40. package/dist/ai-guard/types.d.ts +59 -0
  41. package/dist/ai-guard/types.d.ts.map +1 -0
  42. package/dist/conversation/conversation-module.d.ts +25 -0
  43. package/dist/conversation/conversation-module.d.ts.map +1 -0
  44. package/dist/conversation/decorators.d.ts +28 -0
  45. package/dist/conversation/decorators.d.ts.map +1 -0
  46. package/dist/conversation/index.d.ts +8 -0
  47. package/dist/conversation/index.d.ts.map +1 -0
  48. package/dist/conversation/service.d.ts +43 -0
  49. package/dist/conversation/service.d.ts.map +1 -0
  50. package/dist/conversation/stores/database-store.d.ts +46 -0
  51. package/dist/conversation/stores/database-store.d.ts.map +1 -0
  52. package/dist/conversation/stores/memory-store.d.ts +17 -0
  53. package/dist/conversation/stores/memory-store.d.ts.map +1 -0
  54. package/dist/conversation/stores/redis-store.d.ts +39 -0
  55. package/dist/conversation/stores/redis-store.d.ts.map +1 -0
  56. package/dist/conversation/types.d.ts +64 -0
  57. package/dist/conversation/types.d.ts.map +1 -0
  58. package/dist/embedding/embedding-module.d.ts +20 -0
  59. package/dist/embedding/embedding-module.d.ts.map +1 -0
  60. package/dist/embedding/index.d.ts +6 -0
  61. package/dist/embedding/index.d.ts.map +1 -0
  62. package/dist/embedding/providers/ollama-embedding-provider.d.ts +18 -0
  63. package/dist/embedding/providers/ollama-embedding-provider.d.ts.map +1 -0
  64. package/dist/embedding/providers/openai-embedding-provider.d.ts +18 -0
  65. package/dist/embedding/providers/openai-embedding-provider.d.ts.map +1 -0
  66. package/dist/embedding/service.d.ts +27 -0
  67. package/dist/embedding/service.d.ts.map +1 -0
  68. package/dist/embedding/types.d.ts +25 -0
  69. package/dist/embedding/types.d.ts.map +1 -0
  70. package/dist/index.d.ts +8 -0
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +2638 -1
  73. package/dist/mcp/decorators.d.ts +42 -0
  74. package/dist/mcp/decorators.d.ts.map +1 -0
  75. package/dist/mcp/index.d.ts +6 -0
  76. package/dist/mcp/index.d.ts.map +1 -0
  77. package/dist/mcp/mcp-module.d.ts +22 -0
  78. package/dist/mcp/mcp-module.d.ts.map +1 -0
  79. package/dist/mcp/registry.d.ts +23 -0
  80. package/dist/mcp/registry.d.ts.map +1 -0
  81. package/dist/mcp/server.d.ts +29 -0
  82. package/dist/mcp/server.d.ts.map +1 -0
  83. package/dist/mcp/types.d.ts +60 -0
  84. package/dist/mcp/types.d.ts.map +1 -0
  85. package/dist/prompt/index.d.ts +6 -0
  86. package/dist/prompt/index.d.ts.map +1 -0
  87. package/dist/prompt/prompt-module.d.ts +23 -0
  88. package/dist/prompt/prompt-module.d.ts.map +1 -0
  89. package/dist/prompt/service.d.ts +47 -0
  90. package/dist/prompt/service.d.ts.map +1 -0
  91. package/dist/prompt/stores/file-store.d.ts +36 -0
  92. package/dist/prompt/stores/file-store.d.ts.map +1 -0
  93. package/dist/prompt/stores/memory-store.d.ts +17 -0
  94. package/dist/prompt/stores/memory-store.d.ts.map +1 -0
  95. package/dist/prompt/types.d.ts +68 -0
  96. package/dist/prompt/types.d.ts.map +1 -0
  97. package/dist/rag/chunkers/markdown-chunker.d.ts +11 -0
  98. package/dist/rag/chunkers/markdown-chunker.d.ts.map +1 -0
  99. package/dist/rag/chunkers/text-chunker.d.ts +11 -0
  100. package/dist/rag/chunkers/text-chunker.d.ts.map +1 -0
  101. package/dist/rag/decorators.d.ts +24 -0
  102. package/dist/rag/decorators.d.ts.map +1 -0
  103. package/dist/rag/index.d.ts +7 -0
  104. package/dist/rag/index.d.ts.map +1 -0
  105. package/dist/rag/rag-module.d.ts +23 -0
  106. package/dist/rag/rag-module.d.ts.map +1 -0
  107. package/dist/rag/service.d.ts +36 -0
  108. package/dist/rag/service.d.ts.map +1 -0
  109. package/dist/rag/types.d.ts +56 -0
  110. package/dist/rag/types.d.ts.map +1 -0
  111. package/dist/vector-store/index.d.ts +6 -0
  112. package/dist/vector-store/index.d.ts.map +1 -0
  113. package/dist/vector-store/stores/memory-store.d.ts +17 -0
  114. package/dist/vector-store/stores/memory-store.d.ts.map +1 -0
  115. package/dist/vector-store/stores/pinecone-store.d.ts +27 -0
  116. package/dist/vector-store/stores/pinecone-store.d.ts.map +1 -0
  117. package/dist/vector-store/stores/qdrant-store.d.ts +29 -0
  118. package/dist/vector-store/stores/qdrant-store.d.ts.map +1 -0
  119. package/dist/vector-store/types.d.ts +60 -0
  120. package/dist/vector-store/types.d.ts.map +1 -0
  121. package/dist/vector-store/vector-store-module.d.ts +20 -0
  122. package/dist/vector-store/vector-store-module.d.ts.map +1 -0
  123. package/docs/ai.md +500 -0
  124. package/docs/best-practices.md +83 -8
  125. package/docs/database.md +23 -0
  126. package/docs/guide.md +90 -27
  127. package/docs/migration.md +81 -7
  128. package/docs/security.md +23 -0
  129. package/docs/zh/ai.md +441 -0
  130. package/docs/zh/best-practices.md +43 -0
  131. package/docs/zh/database.md +23 -0
  132. package/docs/zh/guide.md +40 -1
  133. package/docs/zh/migration.md +39 -0
  134. package/docs/zh/security.md +23 -0
  135. package/package.json +3 -3
  136. package/src/ai/ai-module.ts +62 -0
  137. package/src/ai/decorators.ts +30 -0
  138. package/src/ai/errors.ts +71 -0
  139. package/src/ai/index.ts +11 -0
  140. package/src/ai/providers/anthropic-provider.ts +190 -0
  141. package/src/ai/providers/google-provider.ts +179 -0
  142. package/src/ai/providers/ollama-provider.ts +126 -0
  143. package/src/ai/providers/openai-provider.ts +242 -0
  144. package/src/ai/service.ts +155 -0
  145. package/src/ai/tools/tool-executor.ts +38 -0
  146. package/src/ai/tools/tool-registry.ts +91 -0
  147. package/src/ai/types.ts +145 -0
  148. package/src/ai-guard/ai-guard-module.ts +50 -0
  149. package/src/ai-guard/decorators.ts +21 -0
  150. package/src/ai-guard/detectors/content-moderator.ts +80 -0
  151. package/src/ai-guard/detectors/injection-detector.ts +48 -0
  152. package/src/ai-guard/detectors/pii-detector.ts +64 -0
  153. package/src/ai-guard/index.ts +7 -0
  154. package/src/ai-guard/service.ts +100 -0
  155. package/src/ai-guard/types.ts +61 -0
  156. package/src/conversation/conversation-module.ts +63 -0
  157. package/src/conversation/decorators.ts +47 -0
  158. package/src/conversation/index.ts +7 -0
  159. package/src/conversation/service.ts +133 -0
  160. package/src/conversation/stores/database-store.ts +125 -0
  161. package/src/conversation/stores/memory-store.ts +57 -0
  162. package/src/conversation/stores/redis-store.ts +101 -0
  163. package/src/conversation/types.ts +68 -0
  164. package/src/embedding/embedding-module.ts +52 -0
  165. package/src/embedding/index.ts +5 -0
  166. package/src/embedding/providers/ollama-embedding-provider.ts +39 -0
  167. package/src/embedding/providers/openai-embedding-provider.ts +47 -0
  168. package/src/embedding/service.ts +55 -0
  169. package/src/embedding/types.ts +27 -0
  170. package/src/index.ts +10 -0
  171. package/src/mcp/decorators.ts +60 -0
  172. package/src/mcp/index.ts +5 -0
  173. package/src/mcp/mcp-module.ts +58 -0
  174. package/src/mcp/registry.ts +72 -0
  175. package/src/mcp/server.ts +164 -0
  176. package/src/mcp/types.ts +63 -0
  177. package/src/prompt/index.ts +5 -0
  178. package/src/prompt/prompt-module.ts +61 -0
  179. package/src/prompt/service.ts +93 -0
  180. package/src/prompt/stores/file-store.ts +135 -0
  181. package/src/prompt/stores/memory-store.ts +82 -0
  182. package/src/prompt/types.ts +84 -0
  183. package/src/rag/chunkers/markdown-chunker.ts +40 -0
  184. package/src/rag/chunkers/text-chunker.ts +30 -0
  185. package/src/rag/decorators.ts +26 -0
  186. package/src/rag/index.ts +6 -0
  187. package/src/rag/rag-module.ts +78 -0
  188. package/src/rag/service.ts +134 -0
  189. package/src/rag/types.ts +47 -0
  190. package/src/vector-store/index.ts +5 -0
  191. package/src/vector-store/stores/memory-store.ts +69 -0
  192. package/src/vector-store/stores/pinecone-store.ts +123 -0
  193. package/src/vector-store/stores/qdrant-store.ts +147 -0
  194. package/src/vector-store/types.ts +77 -0
  195. package/src/vector-store/vector-store-module.ts +50 -0
  196. package/tests/ai/ai-module.test.ts +46 -0
  197. package/tests/ai/ai-service.test.ts +91 -0
  198. package/tests/ai/tool-registry.test.ts +57 -0
  199. package/tests/ai-guard/ai-guard-module.test.ts +23 -0
  200. package/tests/ai-guard/content-moderator.test.ts +65 -0
  201. package/tests/ai-guard/pii-detector.test.ts +41 -0
  202. package/tests/conversation/conversation-module.test.ts +26 -0
  203. package/tests/conversation/conversation-service.test.ts +64 -0
  204. package/tests/conversation/memory-store.test.ts +68 -0
  205. package/tests/embedding/embedding-service.test.ts +55 -0
  206. package/tests/mcp/mcp-server.test.ts +85 -0
  207. package/tests/prompt/prompt-module.test.ts +30 -0
  208. package/tests/prompt/prompt-service.test.ts +74 -0
  209. package/tests/rag/chunkers.test.ts +58 -0
  210. package/tests/rag/rag-service.test.ts +66 -0
  211. package/tests/vector-store/memory-vector-store.test.ts +84 -0
  212. package/tests/interceptor/perf/interceptor-performance.test.ts +0 -340
  213. package/tests/perf/optimization.test.ts +0 -182
  214. package/tests/perf/regression.test.ts +0 -120
@@ -1,12 +1,87 @@
1
- # Best Practices (English Draft)
1
+ # Best Practices
2
2
 
3
- The Chinese document (`docs/best-practices.md`) is the source of truth. This file lists the
4
- main topics that still need translation:
3
+ **English** | [中文](./zh/best-practices.md)
5
4
 
6
- - Coding conventions & comment style
7
- - Logger usage (English-only requirement)
8
- - Recommended middleware layering
5
+ ---
6
+
7
+ ## AI Best Practices (v2.0.0)
8
+
9
+ ### Use Ollama for Local Development
10
+
11
+ Avoid API costs during development by defaulting to Ollama:
12
+
13
+ ```typescript
14
+ AiModule.forRoot({
15
+ providers: [
16
+ { name: 'ollama', provider: OllamaProvider, config: {}, default: true }, // Free, local
17
+ { name: 'openai', provider: OpenAIProvider, config: { apiKey: env.OPENAI_API_KEY! } },
18
+ ],
19
+ fallback: true, // Fallback to OpenAI if Ollama unavailable
20
+ });
21
+ ```
22
+
23
+ ### Cache AI Responses
24
+
25
+ Combine AiModule with CacheModule to reduce redundant LLM calls:
26
+
27
+ ```typescript
28
+ const cacheKey = `ai:${createHash('md5').update(JSON.stringify(messages)).digest('hex')}`;
29
+ const cached = await cacheService.getOrSet(cacheKey, () => aiService.complete({ messages }), 300_000);
30
+ ```
31
+
32
+ ### Track Token Usage
33
+
34
+ Monitor costs by logging `response.usage`:
35
+
36
+ ```typescript
37
+ const response = await aiService.complete({ messages });
38
+ metricsService.increment('ai.tokens.total', response.usage.totalTokens);
39
+ metricsService.gauge('ai.cost.usd', response.usage.estimatedCostUsd ?? 0);
40
+ ```
41
+
42
+ ### Always Guard User Input in Production
43
+
44
+ ```typescript
45
+ // Check before processing
46
+ const safeInput = await guardService.checkOrThrow(userMessage);
47
+
48
+ // Or at the controller level
49
+ @POST('/chat')
50
+ @AiGuard({ piiDetection: true, promptInjection: true })
51
+ async chat(@Body() body: { message: string }) { ... }
52
+ ```
53
+
54
+ ### Streaming Pattern
55
+
56
+ Keep streaming responses consistent:
57
+
58
+ ```typescript
59
+ @POST('/stream')
60
+ stream(@Body() body: { message: string }): Response {
61
+ return new Response(
62
+ this.aiService.stream({ messages: [{ role: 'user', content: body.message }] }),
63
+ { headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' } },
64
+ );
65
+ }
66
+ ```
67
+
68
+ ### RAG Chunking Strategy
69
+
70
+ Choose chunker based on content type:
71
+
72
+ - **`TextChunker`** — plain text, prose documents
73
+ - **`MarkdownChunker`** — `.md`/`.mdx` files (splits by headings)
74
+
75
+ Tune `chunkSize` (512 chars recommended) and `topK` (3-5 usually sufficient).
76
+
77
+ ---
78
+
79
+ ## General Best Practices
80
+
81
+ See [Chinese best-practices.md](./zh/best-practices.md) for the full guide covering:
82
+
83
+ - Coding conventions and comment style
84
+ - Logger usage recommendations
85
+ - Middleware layering patterns
9
86
  - Testing strategy
10
87
  - Benchmark workflow
11
-
12
- Help us by submitting translations or summaries for each section.
@@ -0,0 +1,23 @@
1
+ # Database
2
+
3
+ This page summarizes database-related documentation in Bun Server Framework.
4
+
5
+ ## Core Database Capabilities
6
+
7
+ - `DatabaseModule`: connection management, health checks, and SQL access.
8
+ - ORM support: entity metadata, repositories, and query helpers.
9
+ - Transactions: declarative transaction boundaries and rollback handling.
10
+
11
+ ## Recommended Reading
12
+
13
+ - [API Reference](./api.md)
14
+ - [Best Practices](./best-practices.md)
15
+ - [Testing](./testing.md)
16
+ - [Migration Guide](./migration.md)
17
+
18
+ ## Typical Setup
19
+
20
+ 1. Configure `DatabaseModule.forRoot(...)` in the root module.
21
+ 2. Define entities/repositories per feature module.
22
+ 3. Use transactions for multi-step write operations.
23
+ 4. Add health checks and monitor connection pool metrics.
package/docs/guide.md CHANGED
@@ -4,13 +4,15 @@ Covers key steps for building Bun Server applications from scratch.
4
4
 
5
5
  ## Request Lifecycle Overview
6
6
 
7
- Before diving into implementation details, it's helpful to understand how Bun Server processes requests:
7
+ Before diving into implementation details, it's helpful to understand how Bun
8
+ Server processes requests:
8
9
 
9
10
  ```
10
11
  HTTP Request → Middleware → Security → Router → Interceptors(Pre) → Validation → Handler → Interceptors(Post) → Exception Filter → HTTP Response
11
12
  ```
12
13
 
13
- For detailed lifecycle documentation, see [Request Lifecycle](./request-lifecycle.md).
14
+ For detailed lifecycle documentation, see
15
+ [Request Lifecycle](./request-lifecycle.md).
14
16
 
15
17
  ## 1. Initialize Application
16
18
 
@@ -48,8 +50,8 @@ Use `ContextService` to access the current request context in services:
48
50
 
49
51
  ```ts
50
52
  import {
51
- ContextService,
52
53
  CONTEXT_SERVICE_TOKEN,
54
+ ContextService,
53
55
  Inject,
54
56
  Injectable,
55
57
  } from "@dangao/bun-server";
@@ -57,7 +59,8 @@ import {
57
59
  @Injectable()
58
60
  class UserService {
59
61
  public constructor(
60
- @Inject(CONTEXT_SERVICE_TOKEN) private readonly contextService: ContextService,
62
+ @Inject(CONTEXT_SERVICE_TOKEN) private readonly contextService:
63
+ ContextService,
61
64
  ) {}
62
65
 
63
66
  public getCurrentUser() {
@@ -779,7 +782,8 @@ class UserController {
779
782
 
780
783
  ## 16. Guards
781
784
 
782
- Guards provide fine-grained access control for your routes. They execute after middleware and before interceptors, deciding whether a request should proceed.
785
+ Guards provide fine-grained access control for your routes. They execute after
786
+ middleware and before interceptors, deciding whether a request should proceed.
783
787
 
784
788
  ### Built-in Guards
785
789
 
@@ -848,17 +852,18 @@ For detailed documentation, see [Guards](./guards.md).
848
852
 
849
853
  ## 17. Event System
850
854
 
851
- The Event Module provides a powerful event-driven architecture for building loosely coupled applications.
855
+ The Event Module provides a powerful event-driven architecture for building
856
+ loosely coupled applications.
852
857
 
853
858
  ### Basic Usage
854
859
 
855
860
  ```ts
856
861
  import {
862
+ EVENT_EMITTER_TOKEN,
857
863
  EventModule,
858
- Injectable,
859
864
  Inject,
865
+ Injectable,
860
866
  OnEvent,
861
- EVENT_EMITTER_TOKEN,
862
867
  } from "@dangao/bun-server";
863
868
  import type { EventEmitter } from "@dangao/bun-server";
864
869
 
@@ -958,7 +963,9 @@ For detailed documentation, see [Event System](./events.md).
958
963
 
959
964
  ## 18. Global Modules
960
965
 
961
- Global modules allow you to share providers across all modules without explicit imports. This is useful for commonly used services like configuration, logging, or caching.
966
+ Global modules allow you to share providers across all modules without explicit
967
+ imports. This is useful for commonly used services like configuration, logging,
968
+ or caching.
962
969
 
963
970
  ### Creating a Global Module
964
971
 
@@ -991,7 +998,8 @@ class GlobalConfigModule {}
991
998
 
992
999
  ### Using Global Module Exports
993
1000
 
994
- Other modules can use the exported providers without importing the global module:
1001
+ Other modules can use the exported providers without importing the global
1002
+ module:
995
1003
 
996
1004
  ```ts
997
1005
  @Injectable()
@@ -1014,7 +1022,8 @@ class UserModule {}
1014
1022
 
1015
1023
  ### Registering Global Modules
1016
1024
 
1017
- Global modules must be registered with the application, typically in your root module:
1025
+ Global modules must be registered with the application, typically in your root
1026
+ module:
1018
1027
 
1019
1028
  ```ts
1020
1029
  @Module({
@@ -1033,10 +1042,14 @@ app.registerModule(AppModule);
1033
1042
 
1034
1043
  ### Key Points
1035
1044
 
1036
- - **Single Registration**: Global modules only need to be registered once (usually in the root module)
1037
- - **Automatic Availability**: Exports from global modules are available to all other modules
1038
- - **Singleton Sharing**: Global module providers maintain singleton behavior across the application
1039
- - **No Import Required**: Other modules don't need to add global modules to their `imports` array
1045
+ - **Single Registration**: Global modules only need to be registered once
1046
+ (usually in the root module)
1047
+ - **Automatic Availability**: Exports from global modules are available to all
1048
+ other modules
1049
+ - **Singleton Sharing**: Global module providers maintain singleton behavior
1050
+ across the application
1051
+ - **No Import Required**: Other modules don't need to add global modules to
1052
+ their `imports` array
1040
1053
 
1041
1054
  ### Use Cases
1042
1055
 
@@ -1077,14 +1090,16 @@ class AppService {
1077
1090
 
1078
1091
  ## 19. Microservice Support
1079
1092
 
1080
- Bun Server provides comprehensive microservice architecture support, including configuration center, service registry, service client, governance, and observability.
1093
+ Bun Server provides comprehensive microservice architecture support, including
1094
+ configuration center, service registry, service client, governance, and
1095
+ observability.
1081
1096
 
1082
1097
  ### Configuration Center
1083
1098
 
1084
1099
  ```ts
1085
1100
  import {
1086
- ConfigCenterModule,
1087
1101
  CONFIG_CENTER_TOKEN,
1102
+ ConfigCenterModule,
1088
1103
  Inject,
1089
1104
  Injectable,
1090
1105
  } from "@dangao/bun-server";
@@ -1119,10 +1134,7 @@ class ConfigService {
1119
1134
  ### Service Registry
1120
1135
 
1121
1136
  ```ts
1122
- import {
1123
- RegisterService,
1124
- ServiceRegistryModule,
1125
- } from "@dangao/bun-server";
1137
+ import { RegisterService, ServiceRegistryModule } from "@dangao/bun-server";
1126
1138
 
1127
1139
  ServiceRegistryModule.forRoot({
1128
1140
  provider: "nacos",
@@ -1149,11 +1161,7 @@ class MyService {
1149
1161
  ### Service Client
1150
1162
 
1151
1163
  ```ts
1152
- import {
1153
- ServiceClient,
1154
- ServiceCall,
1155
- Injectable,
1156
- } from "@dangao/bun-server";
1164
+ import { Injectable, ServiceCall, ServiceClient } from "@dangao/bun-server";
1157
1165
 
1158
1166
  @Injectable()
1159
1167
  class OrderService {
@@ -1169,7 +1177,62 @@ class OrderService {
1169
1177
 
1170
1178
  For detailed documentation, see [Microservice Architecture](./microservice.md).
1171
1179
 
1172
- ## 20. Testing Recommendations
1180
+ ## 20. AI Modules (v2.0.0)
1181
+
1182
+ Starting from v2.0.0, the framework includes 9 official AI modules for building
1183
+ LLM-powered applications.
1184
+
1185
+ ### Quick Setup
1186
+
1187
+ ```typescript
1188
+ import {
1189
+ AiGuardModule, // Content safety
1190
+ AiModule,
1191
+ ConversationModule,
1192
+ MemoryConversationStore, // Conversation memory
1193
+ OllamaProvider, // LLM access
1194
+ RagModule, // RAG pipeline
1195
+ } from "@dangao/bun-server";
1196
+
1197
+ AiModule.forRoot({
1198
+ providers: [{
1199
+ name: "ollama",
1200
+ provider: OllamaProvider,
1201
+ config: {},
1202
+ default: true,
1203
+ }],
1204
+ fallback: true,
1205
+ });
1206
+
1207
+ ConversationModule.forRoot({
1208
+ store: new MemoryConversationStore(),
1209
+ maxMessages: 50,
1210
+ });
1211
+ AiGuardModule.forRoot({
1212
+ piiDetection: true,
1213
+ promptInjection: { sensitivity: "medium" },
1214
+ });
1215
+ ```
1216
+
1217
+ See [AI Modules Documentation](./ai.md) for the complete guide covering all 9
1218
+ modules.
1219
+
1220
+ ### AI Modules at a Glance
1221
+
1222
+ | Module | Purpose |
1223
+ | -------------------- | -------------------------------------------- |
1224
+ | `AiModule` | LLM providers, streaming, Tool Calling |
1225
+ | `ConversationModule` | Multi-turn conversation memory |
1226
+ | `PromptModule` | Prompt templates with variable interpolation |
1227
+ | `EmbeddingModule` | Text embedding generation |
1228
+ | `VectorStoreModule` | Vector similarity search |
1229
+ | `RagModule` | Full RAG pipeline |
1230
+ | `McpModule` | MCP protocol server |
1231
+ | `AiGuardModule` | PII detection, content moderation |
1232
+
1233
+ ---
1234
+
1235
+ ## 21. Testing Recommendations
1173
1236
 
1174
1237
  - Use `tests/utils/test-port.ts` to get auto-incrementing ports, avoiding local
1175
1238
  conflicts.
package/docs/migration.md CHANGED
@@ -1,10 +1,84 @@
1
- # Migration Guide (English Draft)
1
+ # Migration Guide
2
2
 
3
- A full translation of `docs/migration.md` is in progress. Planned sections:
3
+ **English** | [中文](./zh/migration.md)
4
4
 
5
- - Migrating from Express / Koa / NestJS
6
- - Moving from Node.js to Bun
7
- - Updating legacy middleware
8
- - Breaking changes per release
5
+ ---
9
6
 
10
- Please consult the Chinese version for now. If you would like to help, open an issue or PR.
7
+ ## v1.x v2.0
8
+
9
+ v2.0.0 is **fully backward compatible** with v1.x. All existing modules, APIs, and patterns continue to work without changes.
10
+
11
+ ### What's New
12
+
13
+ v2.0.0 adds 9 official AI modules as purely additive features:
14
+
15
+ - `AiModule` — LLM unified access + Tool Calling + streaming
16
+ - `ConversationModule` — Multi-turn conversation history
17
+ - `PromptModule` — Prompt templates + versioning
18
+ - `EmbeddingModule` — Text embedding generation
19
+ - `VectorStoreModule` — Vector similarity search
20
+ - `RagModule` — Full RAG pipeline
21
+ - `McpModule` — MCP protocol server
22
+ - `AiGuardModule` — Content safety
23
+
24
+ See [AI Modules Guide](./ai.md) for full documentation.
25
+
26
+ ### No Breaking Changes
27
+
28
+ No existing code needs modification. Simply update the package:
29
+
30
+ ```bash
31
+ bun add @dangao/bun-server@2.0.0
32
+ ```
33
+
34
+ ---
35
+
36
+ ## Migrating from Express / Koa / NestJS
37
+
38
+ Please see the Chinese migration guide (`docs/zh/migration.md`) for detailed migration instructions from:
39
+
40
+ - Express.js
41
+ - Koa.js
42
+ - NestJS
43
+ - Node.js (to Bun Runtime)
44
+
45
+ ---
46
+
47
+ ## Migrating from Node.js to Bun Runtime
48
+
49
+ Key differences when switching from Node.js:
50
+
51
+ 1. **No `require()` / CommonJS** — use `import` / ESM exclusively
52
+ 2. **Use Bun APIs** — prefer `Bun.file()`, `Bun.serve()`, `Bun.SQL` over Node equivalents
53
+ 3. **TypeScript natively supported** — no compilation step needed for development
54
+ 4. **Engine**: JavaScriptCore instead of V8 — performance characteristics differ
55
+ 5. **Package manager**: use `bun install` instead of `npm install`
56
+
57
+ ```bash
58
+ # Install Bun
59
+ curl -fsSL https://bun.sh/install | bash
60
+
61
+ # Convert project
62
+ bun install # reads existing package.json
63
+ bun run src/index.ts # runs TypeScript directly
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Breaking Changes by Release
69
+
70
+ ### v2.0.0
71
+
72
+ No breaking changes. Purely additive AI module additions.
73
+
74
+ ### v1.9.0
75
+
76
+ - `EventModule`: `@OnEvent()` listener classes are now auto-scanned at `app.listen()`. Remove manual calls to `EventModule.initializeListeners()` if present.
77
+
78
+ ### v1.8.0
79
+
80
+ - `ClusterManager` API: `ClusterManager.start()` now accepts options object instead of positional arguments.
81
+
82
+ ### v1.2.0
83
+
84
+ - `ContextService.getContext()` now returns `Context | undefined` instead of throwing. Update callers to handle `undefined`.
@@ -0,0 +1,23 @@
1
+ # Security
2
+
3
+ This page summarizes security-related documentation in Bun Server Framework.
4
+
5
+ ## Core Security Modules
6
+
7
+ - `SecurityModule`: authentication flow, authorization checks, guard integration.
8
+ - `auth` module: JWT/OAuth2 providers and token processing.
9
+ - Guards: route-level access control and role checks.
10
+
11
+ ## Recommended Reading
12
+
13
+ - [API Reference](./api.md)
14
+ - [Guards](./guards.md)
15
+ - [Best Practices](./best-practices.md)
16
+ - [Error Handling](./error-handling.md)
17
+
18
+ ## Typical Setup
19
+
20
+ 1. Configure `SecurityModule.forRoot(...)` in the root module.
21
+ 2. Apply `@Auth()` / guards on protected routes.
22
+ 3. Keep secrets in environment variables, never in source code.
23
+ 4. Add request logging and rate limiting for sensitive endpoints.