@dangao/bun-server 1.12.0 → 2.0.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/README.md +32 -0
- package/dist/ai/ai-module.d.ts +24 -0
- package/dist/ai/ai-module.d.ts.map +1 -0
- package/dist/ai/decorators.d.ts +25 -0
- package/dist/ai/decorators.d.ts.map +1 -0
- package/dist/ai/errors.d.ts +39 -0
- package/dist/ai/errors.d.ts.map +1 -0
- package/dist/ai/index.d.ts +12 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/providers/anthropic-provider.d.ts +23 -0
- package/dist/ai/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/ai/providers/google-provider.d.ts +20 -0
- package/dist/ai/providers/google-provider.d.ts.map +1 -0
- package/dist/ai/providers/ollama-provider.d.ts +17 -0
- package/dist/ai/providers/ollama-provider.d.ts.map +1 -0
- package/dist/ai/providers/openai-provider.d.ts +28 -0
- package/dist/ai/providers/openai-provider.d.ts.map +1 -0
- package/dist/ai/service.d.ts +40 -0
- package/dist/ai/service.d.ts.map +1 -0
- package/dist/ai/tools/tool-executor.d.ts +15 -0
- package/dist/ai/tools/tool-executor.d.ts.map +1 -0
- package/dist/ai/tools/tool-registry.d.ts +39 -0
- package/dist/ai/tools/tool-registry.d.ts.map +1 -0
- package/dist/ai/types.d.ts +134 -0
- package/dist/ai/types.d.ts.map +1 -0
- package/dist/ai-guard/ai-guard-module.d.ts +18 -0
- package/dist/ai-guard/ai-guard-module.d.ts.map +1 -0
- package/dist/ai-guard/decorators.d.ts +16 -0
- package/dist/ai-guard/decorators.d.ts.map +1 -0
- package/dist/ai-guard/detectors/content-moderator.d.ts +26 -0
- package/dist/ai-guard/detectors/content-moderator.d.ts.map +1 -0
- package/dist/ai-guard/detectors/injection-detector.d.ts +13 -0
- package/dist/ai-guard/detectors/injection-detector.d.ts.map +1 -0
- package/dist/ai-guard/detectors/pii-detector.d.ts +11 -0
- package/dist/ai-guard/detectors/pii-detector.d.ts.map +1 -0
- package/dist/ai-guard/index.d.ts +8 -0
- package/dist/ai-guard/index.d.ts.map +1 -0
- package/dist/ai-guard/service.d.ts +21 -0
- package/dist/ai-guard/service.d.ts.map +1 -0
- package/dist/ai-guard/types.d.ts +59 -0
- package/dist/ai-guard/types.d.ts.map +1 -0
- package/dist/conversation/conversation-module.d.ts +25 -0
- package/dist/conversation/conversation-module.d.ts.map +1 -0
- package/dist/conversation/decorators.d.ts +28 -0
- package/dist/conversation/decorators.d.ts.map +1 -0
- package/dist/conversation/index.d.ts +8 -0
- package/dist/conversation/index.d.ts.map +1 -0
- package/dist/conversation/service.d.ts +43 -0
- package/dist/conversation/service.d.ts.map +1 -0
- package/dist/conversation/stores/database-store.d.ts +46 -0
- package/dist/conversation/stores/database-store.d.ts.map +1 -0
- package/dist/conversation/stores/memory-store.d.ts +17 -0
- package/dist/conversation/stores/memory-store.d.ts.map +1 -0
- package/dist/conversation/stores/redis-store.d.ts +39 -0
- package/dist/conversation/stores/redis-store.d.ts.map +1 -0
- package/dist/conversation/types.d.ts +64 -0
- package/dist/conversation/types.d.ts.map +1 -0
- package/dist/core/cluster.d.ts +42 -3
- package/dist/core/cluster.d.ts.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/server.d.ts.map +1 -1
- package/dist/embedding/embedding-module.d.ts +20 -0
- package/dist/embedding/embedding-module.d.ts.map +1 -0
- package/dist/embedding/index.d.ts +6 -0
- package/dist/embedding/index.d.ts.map +1 -0
- package/dist/embedding/providers/ollama-embedding-provider.d.ts +18 -0
- package/dist/embedding/providers/ollama-embedding-provider.d.ts.map +1 -0
- package/dist/embedding/providers/openai-embedding-provider.d.ts +18 -0
- package/dist/embedding/providers/openai-embedding-provider.d.ts.map +1 -0
- package/dist/embedding/service.d.ts +27 -0
- package/dist/embedding/service.d.ts.map +1 -0
- package/dist/embedding/types.d.ts +25 -0
- package/dist/embedding/types.d.ts.map +1 -0
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2870 -88
- package/dist/mcp/decorators.d.ts +42 -0
- package/dist/mcp/decorators.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/mcp-module.d.ts +22 -0
- package/dist/mcp/mcp-module.d.ts.map +1 -0
- package/dist/mcp/registry.d.ts +23 -0
- package/dist/mcp/registry.d.ts.map +1 -0
- package/dist/mcp/server.d.ts +29 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/types.d.ts +60 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/prompt/index.d.ts +6 -0
- package/dist/prompt/index.d.ts.map +1 -0
- package/dist/prompt/prompt-module.d.ts +23 -0
- package/dist/prompt/prompt-module.d.ts.map +1 -0
- package/dist/prompt/service.d.ts +47 -0
- package/dist/prompt/service.d.ts.map +1 -0
- package/dist/prompt/stores/file-store.d.ts +36 -0
- package/dist/prompt/stores/file-store.d.ts.map +1 -0
- package/dist/prompt/stores/memory-store.d.ts +17 -0
- package/dist/prompt/stores/memory-store.d.ts.map +1 -0
- package/dist/prompt/types.d.ts +68 -0
- package/dist/prompt/types.d.ts.map +1 -0
- package/dist/rag/chunkers/markdown-chunker.d.ts +11 -0
- package/dist/rag/chunkers/markdown-chunker.d.ts.map +1 -0
- package/dist/rag/chunkers/text-chunker.d.ts +11 -0
- package/dist/rag/chunkers/text-chunker.d.ts.map +1 -0
- package/dist/rag/decorators.d.ts +24 -0
- package/dist/rag/decorators.d.ts.map +1 -0
- package/dist/rag/index.d.ts +7 -0
- package/dist/rag/index.d.ts.map +1 -0
- package/dist/rag/rag-module.d.ts +23 -0
- package/dist/rag/rag-module.d.ts.map +1 -0
- package/dist/rag/service.d.ts +36 -0
- package/dist/rag/service.d.ts.map +1 -0
- package/dist/rag/types.d.ts +56 -0
- package/dist/rag/types.d.ts.map +1 -0
- package/dist/vector-store/index.d.ts +6 -0
- package/dist/vector-store/index.d.ts.map +1 -0
- package/dist/vector-store/stores/memory-store.d.ts +17 -0
- package/dist/vector-store/stores/memory-store.d.ts.map +1 -0
- package/dist/vector-store/stores/pinecone-store.d.ts +27 -0
- package/dist/vector-store/stores/pinecone-store.d.ts.map +1 -0
- package/dist/vector-store/stores/qdrant-store.d.ts +29 -0
- package/dist/vector-store/stores/qdrant-store.d.ts.map +1 -0
- package/dist/vector-store/types.d.ts +60 -0
- package/dist/vector-store/types.d.ts.map +1 -0
- package/dist/vector-store/vector-store-module.d.ts +20 -0
- package/dist/vector-store/vector-store-module.d.ts.map +1 -0
- package/docs/ai.md +500 -0
- package/docs/best-practices.md +83 -8
- package/docs/database.md +23 -0
- package/docs/guide.md +90 -27
- package/docs/migration.md +81 -7
- package/docs/security.md +23 -0
- package/docs/zh/ai.md +441 -0
- package/docs/zh/best-practices.md +43 -0
- package/docs/zh/database.md +23 -0
- package/docs/zh/guide.md +40 -1
- package/docs/zh/migration.md +39 -0
- package/docs/zh/security.md +23 -0
- package/package.json +2 -2
- package/src/ai/ai-module.ts +62 -0
- package/src/ai/decorators.ts +30 -0
- package/src/ai/errors.ts +71 -0
- package/src/ai/index.ts +11 -0
- package/src/ai/providers/anthropic-provider.ts +190 -0
- package/src/ai/providers/google-provider.ts +179 -0
- package/src/ai/providers/ollama-provider.ts +126 -0
- package/src/ai/providers/openai-provider.ts +242 -0
- package/src/ai/service.ts +155 -0
- package/src/ai/tools/tool-executor.ts +38 -0
- package/src/ai/tools/tool-registry.ts +91 -0
- package/src/ai/types.ts +145 -0
- package/src/ai-guard/ai-guard-module.ts +50 -0
- package/src/ai-guard/decorators.ts +21 -0
- package/src/ai-guard/detectors/content-moderator.ts +80 -0
- package/src/ai-guard/detectors/injection-detector.ts +48 -0
- package/src/ai-guard/detectors/pii-detector.ts +64 -0
- package/src/ai-guard/index.ts +7 -0
- package/src/ai-guard/service.ts +100 -0
- package/src/ai-guard/types.ts +61 -0
- package/src/conversation/conversation-module.ts +63 -0
- package/src/conversation/decorators.ts +47 -0
- package/src/conversation/index.ts +7 -0
- package/src/conversation/service.ts +133 -0
- package/src/conversation/stores/database-store.ts +125 -0
- package/src/conversation/stores/memory-store.ts +57 -0
- package/src/conversation/stores/redis-store.ts +101 -0
- package/src/conversation/types.ts +68 -0
- package/src/core/cluster.ts +239 -46
- package/src/core/index.ts +1 -1
- package/src/core/server.ts +91 -78
- package/src/embedding/embedding-module.ts +52 -0
- package/src/embedding/index.ts +5 -0
- package/src/embedding/providers/ollama-embedding-provider.ts +39 -0
- package/src/embedding/providers/openai-embedding-provider.ts +47 -0
- package/src/embedding/service.ts +55 -0
- package/src/embedding/types.ts +27 -0
- package/src/index.ts +11 -1
- package/src/mcp/decorators.ts +60 -0
- package/src/mcp/index.ts +5 -0
- package/src/mcp/mcp-module.ts +58 -0
- package/src/mcp/registry.ts +72 -0
- package/src/mcp/server.ts +164 -0
- package/src/mcp/types.ts +63 -0
- package/src/prompt/index.ts +5 -0
- package/src/prompt/prompt-module.ts +61 -0
- package/src/prompt/service.ts +93 -0
- package/src/prompt/stores/file-store.ts +135 -0
- package/src/prompt/stores/memory-store.ts +82 -0
- package/src/prompt/types.ts +84 -0
- package/src/rag/chunkers/markdown-chunker.ts +40 -0
- package/src/rag/chunkers/text-chunker.ts +30 -0
- package/src/rag/decorators.ts +26 -0
- package/src/rag/index.ts +6 -0
- package/src/rag/rag-module.ts +78 -0
- package/src/rag/service.ts +134 -0
- package/src/rag/types.ts +47 -0
- package/src/vector-store/index.ts +5 -0
- package/src/vector-store/stores/memory-store.ts +69 -0
- package/src/vector-store/stores/pinecone-store.ts +123 -0
- package/src/vector-store/stores/qdrant-store.ts +147 -0
- package/src/vector-store/types.ts +77 -0
- package/src/vector-store/vector-store-module.ts +50 -0
- package/tests/ai/ai-module.test.ts +46 -0
- package/tests/ai/ai-service.test.ts +91 -0
- package/tests/ai/tool-registry.test.ts +57 -0
- package/tests/ai-guard/ai-guard-module.test.ts +23 -0
- package/tests/ai-guard/content-moderator.test.ts +65 -0
- package/tests/ai-guard/pii-detector.test.ts +41 -0
- package/tests/conversation/conversation-module.test.ts +26 -0
- package/tests/conversation/conversation-service.test.ts +64 -0
- package/tests/conversation/memory-store.test.ts +68 -0
- package/tests/core/cluster.test.ts +45 -1
- package/tests/embedding/embedding-service.test.ts +55 -0
- package/tests/mcp/mcp-server.test.ts +85 -0
- package/tests/prompt/prompt-module.test.ts +30 -0
- package/tests/prompt/prompt-service.test.ts +74 -0
- package/tests/rag/chunkers.test.ts +58 -0
- package/tests/rag/rag-service.test.ts +66 -0
- package/tests/vector-store/memory-vector-store.test.ts +84 -0
- package/tests/interceptor/perf/interceptor-performance.test.ts +0 -340
- package/tests/perf/optimization.test.ts +0 -182
- package/tests/perf/regression.test.ts +0 -120
package/README.md
CHANGED
|
@@ -63,6 +63,38 @@
|
|
|
63
63
|
- 🖥️ **Zero-config cluster** — `ClusterManager` auto-spawns reusePort
|
|
64
64
|
workers matching CPU core count.
|
|
65
65
|
|
|
66
|
+
## AI Modules (v2.0.0)
|
|
67
|
+
|
|
68
|
+
9 official AI modules for building production-grade LLM applications. All providers use Bun's native `fetch()` — zero external SDK dependencies.
|
|
69
|
+
|
|
70
|
+
| Module | Purpose |
|
|
71
|
+
|--------|---------|
|
|
72
|
+
| `AiModule` | LLM unified access (OpenAI, Claude, Gemini, Ollama) + Tool Calling + streaming |
|
|
73
|
+
| `ConversationModule` | Multi-turn conversation memory (Memory/Redis/Database stores) |
|
|
74
|
+
| `PromptModule` | Reusable prompt templates with `{{variable}}` interpolation and versioning |
|
|
75
|
+
| `EmbeddingModule` | Text embedding generation (OpenAI, Ollama) |
|
|
76
|
+
| `VectorStoreModule` | Vector similarity search (Memory, Pinecone, Qdrant) |
|
|
77
|
+
| `RagModule` | Full RAG pipeline: ingest → chunk → embed → retrieve |
|
|
78
|
+
| `McpModule` | MCP protocol server (JSON-RPC 2.0, SSE transport) |
|
|
79
|
+
| `AiGuardModule` | PII detection, prompt injection detection, content moderation |
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { AiModule, OllamaProvider, AI_SERVICE_TOKEN, AiService } from '@dangao/bun-server';
|
|
83
|
+
|
|
84
|
+
AiModule.forRoot({
|
|
85
|
+
providers: [{ name: 'ollama', provider: OllamaProvider, config: {}, default: true }],
|
|
86
|
+
fallback: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
@Injectable()
|
|
90
|
+
class ChatService {
|
|
91
|
+
constructor(@Inject(AI_SERVICE_TOKEN) private ai: AiService) {}
|
|
92
|
+
chat(message: string) { return this.ai.complete({ messages: [{ role: 'user', content: message }] }); }
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
See [docs/ai.md](docs/ai.md) for the complete AI modules guide and [examples/05-ai/](examples/05-ai/) for working examples including the [AI Platform MVP Demo](examples/05-ai/ai-platform-mvp/).
|
|
97
|
+
|
|
66
98
|
## Architecture
|
|
67
99
|
|
|
68
100
|
### Request Lifecycle
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type AiModuleOptions } from './types';
|
|
2
|
+
export declare class AiModule {
|
|
3
|
+
/**
|
|
4
|
+
* Configure the AI module with one or more LLM providers.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* AiModule.forRoot({
|
|
9
|
+
* providers: [
|
|
10
|
+
* { name: 'openai', provider: OpenAIProvider, config: { apiKey: '...' }, default: true },
|
|
11
|
+
* { name: 'ollama', provider: OllamaProvider, config: {} },
|
|
12
|
+
* ],
|
|
13
|
+
* fallback: true,
|
|
14
|
+
* timeout: 30000,
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
static forRoot(options: AiModuleOptions): typeof AiModule;
|
|
19
|
+
/**
|
|
20
|
+
* Reset module state (for testing)
|
|
21
|
+
*/
|
|
22
|
+
static reset(): void;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=ai-module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-module.d.ts","sourceRoot":"","sources":["../../src/ai/ai-module.ts"],"names":[],"mappings":"AAGA,OAAO,EAIL,KAAK,eAAe,EACrB,MAAM,SAAS,CAAC;AAGjB,qBACa,QAAQ;IACnB;;;;;;;;;;;;;;OAcG;WACW,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,QAAQ;IA2BhE;;OAEG;WACW,KAAK,IAAI,IAAI;CAG5B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AiToolDefinition } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Mark a service method as an AI tool available for Tool Calling.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* \@Injectable()
|
|
8
|
+
* class CalculatorService {
|
|
9
|
+
* \@AiTool({
|
|
10
|
+
* name: 'calculate',
|
|
11
|
+
* description: 'Evaluate a mathematical expression',
|
|
12
|
+
* parameters: {
|
|
13
|
+
* type: 'object',
|
|
14
|
+
* properties: { expression: { type: 'string' } },
|
|
15
|
+
* required: ['expression'],
|
|
16
|
+
* },
|
|
17
|
+
* })
|
|
18
|
+
* public calculate({ expression }: { expression: string }): string {
|
|
19
|
+
* return String(eval(expression));
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function AiTool(definition: AiToolDefinition): MethodDecorator;
|
|
25
|
+
//# sourceMappingURL=decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../../src/ai/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGhD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CAAC,UAAU,EAAE,gBAAgB,GAAG,eAAe,CAIpE"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { HttpException } from '../error/http-exception';
|
|
2
|
+
/**
|
|
3
|
+
* Base AI provider error
|
|
4
|
+
*/
|
|
5
|
+
export declare class AiProviderError extends HttpException {
|
|
6
|
+
readonly provider: string;
|
|
7
|
+
constructor(message: string, provider: string, statusCode?: number);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* LLM rate limit exceeded
|
|
11
|
+
*/
|
|
12
|
+
export declare class AiRateLimitError extends AiProviderError {
|
|
13
|
+
constructor(provider: string, retryAfterMs?: number);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Input exceeds model context window
|
|
17
|
+
*/
|
|
18
|
+
export declare class AiContextLengthError extends AiProviderError {
|
|
19
|
+
constructor(provider: string, maxTokens?: number);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* LLM request timed out
|
|
23
|
+
*/
|
|
24
|
+
export declare class AiTimeoutError extends AiProviderError {
|
|
25
|
+
constructor(provider: string, timeoutMs: number);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* No AI providers configured
|
|
29
|
+
*/
|
|
30
|
+
export declare class AiNoProviderError extends HttpException {
|
|
31
|
+
constructor();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* All providers in fallback chain failed
|
|
35
|
+
*/
|
|
36
|
+
export declare class AiAllProvidersFailed extends HttpException {
|
|
37
|
+
constructor(errors: string[]);
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/ai/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAExD;;GAEG;AACH,qBAAa,eAAgB,SAAQ,aAAa;aAG9B,QAAQ,EAAE,MAAM;gBADhC,OAAO,EAAE,MAAM,EACC,QAAQ,EAAE,MAAM,EAChC,UAAU,SAAM;CAInB;AAED;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,eAAe;gBAChC,QAAQ,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM;CAS3D;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,eAAe;gBACpC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CASxD;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,eAAe;gBAC9B,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAGvD;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;;CAInD;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;gBAClC,MAAM,EAAE,MAAM,EAAE;CAGpC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './errors';
|
|
3
|
+
export * from './decorators';
|
|
4
|
+
export * from './service';
|
|
5
|
+
export * from './ai-module';
|
|
6
|
+
export * from './tools/tool-registry';
|
|
7
|
+
export * from './tools/tool-executor';
|
|
8
|
+
export * from './providers/openai-provider';
|
|
9
|
+
export * from './providers/anthropic-provider';
|
|
10
|
+
export * from './providers/ollama-provider';
|
|
11
|
+
export * from './providers/google-provider';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ai/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { LlmProvider, AiRequest, AiResponse, AiMessage } from '../types';
|
|
2
|
+
export interface AnthropicProviderConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Default: https://api.anthropic.com */
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
/** Default: claude-3-7-sonnet-20250219 */
|
|
7
|
+
defaultModel?: string;
|
|
8
|
+
/** API version header */
|
|
9
|
+
anthropicVersion?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare class AnthropicProvider implements LlmProvider {
|
|
12
|
+
readonly name = "anthropic";
|
|
13
|
+
private readonly apiKey;
|
|
14
|
+
private readonly baseUrl;
|
|
15
|
+
private readonly defaultModel;
|
|
16
|
+
private readonly anthropicVersion;
|
|
17
|
+
constructor(config: AnthropicProviderConfig);
|
|
18
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
19
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
20
|
+
countTokens(messages: AiMessage[]): number;
|
|
21
|
+
private post;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=anthropic-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/anthropic-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG9E,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yBAAyB;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,qBAAa,iBAAkB,YAAW,WAAW;IACnD,SAAgB,IAAI,eAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;gBAEvB,MAAM,EAAE,uBAAuB;IAOrC,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IA+DvD,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC;IA8EtD,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;YAInC,IAAI;CAiBnB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { LlmProvider, AiRequest, AiResponse, AiMessage } from '../types';
|
|
2
|
+
export interface GoogleProviderConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Default: gemini-2.0-flash */
|
|
5
|
+
defaultModel?: string;
|
|
6
|
+
/** Default: https://generativelanguage.googleapis.com/v1beta */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class GoogleProvider implements LlmProvider {
|
|
10
|
+
readonly name = "google";
|
|
11
|
+
private readonly apiKey;
|
|
12
|
+
private readonly defaultModel;
|
|
13
|
+
private readonly baseUrl;
|
|
14
|
+
constructor(config: GoogleProviderConfig);
|
|
15
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
16
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
17
|
+
countTokens(messages: AiMessage[]): number;
|
|
18
|
+
private toGeminiMessages;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=google-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/google-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG9E,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,gCAAgC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,cAAe,YAAW,WAAW;IAChD,SAAgB,IAAI,YAAY;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAEd,MAAM,EAAE,oBAAoB;IAMlC,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IAoEvD,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC;IAkEtD,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;IAIjD,OAAO,CAAC,gBAAgB;CAiBzB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LlmProvider, AiRequest, AiResponse, AiMessage } from '../types';
|
|
2
|
+
export interface OllamaProviderConfig {
|
|
3
|
+
/** Default: http://localhost:11434 */
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
/** Default model to use */
|
|
6
|
+
defaultModel?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class OllamaProvider implements LlmProvider {
|
|
9
|
+
readonly name = "ollama";
|
|
10
|
+
private readonly baseUrl;
|
|
11
|
+
private readonly defaultModel;
|
|
12
|
+
constructor(config?: OllamaProviderConfig);
|
|
13
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
14
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
15
|
+
countTokens(messages: AiMessage[]): number;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=ollama-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ollama-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/ollama-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG9E,MAAM,WAAW,oBAAoB;IACnC,sCAAsC;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2BAA2B;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,cAAe,YAAW,WAAW;IAChD,SAAgB,IAAI,YAAY;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;gBAEnB,MAAM,GAAE,oBAAyB;IAKvC,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IAuCvD,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC;IA+DtD,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;CAGlD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { LlmProvider, AiRequest, AiResponse, AiMessage } from '../types';
|
|
2
|
+
export interface OpenAIProviderConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Default: https://api.openai.com/v1 */
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
/** Default: gpt-4o */
|
|
7
|
+
defaultModel?: string;
|
|
8
|
+
/** Pricing per 1M tokens (input/output) for cost estimation */
|
|
9
|
+
pricing?: Record<string, {
|
|
10
|
+
input: number;
|
|
11
|
+
output: number;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
export declare class OpenAIProvider implements LlmProvider {
|
|
15
|
+
readonly name = "openai";
|
|
16
|
+
private readonly apiKey;
|
|
17
|
+
private readonly baseUrl;
|
|
18
|
+
private readonly defaultModel;
|
|
19
|
+
private readonly pricing;
|
|
20
|
+
constructor(config: OpenAIProviderConfig);
|
|
21
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
22
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
23
|
+
countTokens(messages: AiMessage[]): number;
|
|
24
|
+
private post;
|
|
25
|
+
private safeParseToolArguments;
|
|
26
|
+
private estimateCost;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=openai-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-provider.d.ts","sourceRoot":"","sources":["../../../src/ai/providers/openai-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAG9E,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sBAAsB;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC7D;AAwCD,qBAAa,cAAe,YAAW,WAAW;IAChD,SAAgB,IAAI,YAAY;IAChC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoD;gBAEzD,MAAM,EAAE,oBAAoB;IAOlC,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IA4CvD,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC;IA+EtD,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;YAKnC,IAAI;IA4BlB,OAAO,CAAC,sBAAsB;IAe9B,OAAO,CAAC,YAAY;CAKrB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { LlmProvider, AiRequest, AiResponse, AiModuleOptions, AiMessage } from './types';
|
|
2
|
+
import type { ToolRegistry } from './tools/tool-registry';
|
|
3
|
+
/**
|
|
4
|
+
* Core AI service — manages multiple LLM providers, fallback, streaming,
|
|
5
|
+
* Tool Calling loop, and cost tracking.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AiService {
|
|
8
|
+
private readonly providers;
|
|
9
|
+
private defaultProviderName;
|
|
10
|
+
private readonly options;
|
|
11
|
+
private toolExecutor;
|
|
12
|
+
constructor(options: AiModuleOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Attach a ToolRegistry so the service can run Tool Calling loops
|
|
15
|
+
*/
|
|
16
|
+
setToolRegistry(registry: ToolRegistry): void;
|
|
17
|
+
/**
|
|
18
|
+
* Non-streaming completion with optional Tool Calling loop
|
|
19
|
+
*/
|
|
20
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
21
|
+
/**
|
|
22
|
+
* Streaming completion — returns SSE ReadableStream
|
|
23
|
+
*/
|
|
24
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
25
|
+
/**
|
|
26
|
+
* Estimate token count for messages using the default provider
|
|
27
|
+
*/
|
|
28
|
+
countTokens(messages: AiMessage[]): number;
|
|
29
|
+
/**
|
|
30
|
+
* Get a provider by name (or default)
|
|
31
|
+
*/
|
|
32
|
+
getProvider(name?: string): LlmProvider;
|
|
33
|
+
/**
|
|
34
|
+
* List all registered provider names
|
|
35
|
+
*/
|
|
36
|
+
getProviderNames(): string[];
|
|
37
|
+
private completeSingle;
|
|
38
|
+
private withTimeout;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/ai/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,WAAW,EACX,SAAS,EACT,UAAU,EACV,eAAe,EACf,SAAS,EACV,MAAM,SAAS,CAAC;AAIjB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG1D;;;GAGG;AACH,qBACa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAkC;IAC5D,OAAO,CAAC,mBAAmB,CAAqB;IAChD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAC1C,OAAO,CAAC,YAAY,CAA6B;gBAGd,OAAO,EAAE,eAAe;IAY3D;;OAEG;IACI,eAAe,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;IAIpD;;OAEG;IACU,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IA8B9D;;OAEG;IACI,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC;IAK7D;;OAEG;IACI,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;IAKjD;;OAEG;IACI,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW;IAU9C;;OAEG;IACI,gBAAgB,IAAI,MAAM,EAAE;YAIrB,cAAc;IA+B5B,OAAO,CAAC,WAAW;CASpB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AiMessage, AiToolCall } from '../types';
|
|
2
|
+
import type { ToolRegistry } from './tool-registry';
|
|
3
|
+
/**
|
|
4
|
+
* Executes tool calls from LLM responses and formats results as AiMessages
|
|
5
|
+
*/
|
|
6
|
+
export declare class ToolExecutor {
|
|
7
|
+
private readonly registry;
|
|
8
|
+
constructor(registry: ToolRegistry);
|
|
9
|
+
/**
|
|
10
|
+
* Execute all tool calls in parallel and return result messages
|
|
11
|
+
*/
|
|
12
|
+
executeAll(toolCalls: AiToolCall[]): Promise<AiMessage[]>;
|
|
13
|
+
private executeOne;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=tool-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-executor.d.ts","sourceRoot":"","sources":["../../../src/ai/tools/tool-executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD;;GAEG;AACH,qBAAa,YAAY;IACJ,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,YAAY;IAE1D;;OAEG;IACU,UAAU,CAAC,SAAS,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;YAOxD,UAAU;CAkBzB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AiToolDefinition } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Registered tool entry
|
|
4
|
+
*/
|
|
5
|
+
export interface RegisteredTool extends AiToolDefinition {
|
|
6
|
+
/** Bound execute function */
|
|
7
|
+
execute(args: Record<string, unknown>): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Registry for all @AiTool()-decorated methods
|
|
11
|
+
*/
|
|
12
|
+
export declare class ToolRegistry {
|
|
13
|
+
private readonly tools;
|
|
14
|
+
/**
|
|
15
|
+
* Register a tool manually
|
|
16
|
+
*/
|
|
17
|
+
register(tool: RegisteredTool): void;
|
|
18
|
+
/**
|
|
19
|
+
* Scan an object instance for @AiTool() decorated methods and register them
|
|
20
|
+
*/
|
|
21
|
+
scanAndRegister(instance: object): void;
|
|
22
|
+
/**
|
|
23
|
+
* Get all registered tools as definitions (for LLM request)
|
|
24
|
+
*/
|
|
25
|
+
getDefinitions(): AiToolDefinition[];
|
|
26
|
+
/**
|
|
27
|
+
* Execute a tool by name
|
|
28
|
+
*/
|
|
29
|
+
execute(name: string, args: Record<string, unknown>): Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Check whether a tool exists
|
|
32
|
+
*/
|
|
33
|
+
has(name: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Number of registered tools
|
|
36
|
+
*/
|
|
37
|
+
get size(): number;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=tool-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-registry.d.ts","sourceRoot":"","sources":["../../../src/ai/tools/tool-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAGjD;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,gBAAgB;IACtD,6BAA6B;IAC7B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1D;AAED;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAqC;IAE3D;;OAEG;IACI,QAAQ,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI;IAI3C;;OAEG;IACI,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IA4B9C;;OAEG;IACI,cAAc,IAAI,gBAAgB,EAAE;IAQ3C;;OAEG;IACU,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IAQnF;;OAEG;IACI,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIjC;;OAEG;IACH,IAAW,IAAI,IAAI,MAAM,CAExB;CACF"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI message role
|
|
3
|
+
*/
|
|
4
|
+
export type AiMessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
5
|
+
/**
|
|
6
|
+
* Single AI message
|
|
7
|
+
*/
|
|
8
|
+
export interface AiMessage {
|
|
9
|
+
role: AiMessageRole;
|
|
10
|
+
content: string;
|
|
11
|
+
/** Tool call ID (only for role='tool') */
|
|
12
|
+
toolCallId?: string;
|
|
13
|
+
/** Tool calls returned by assistant */
|
|
14
|
+
toolCalls?: AiToolCall[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* AI tool call from LLM response
|
|
18
|
+
*/
|
|
19
|
+
export interface AiToolCall {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
arguments: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* AI tool definition (for function calling)
|
|
26
|
+
*/
|
|
27
|
+
export interface AiToolDefinition {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
/** JSON Schema for parameters */
|
|
31
|
+
parameters: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* AI request to LLM
|
|
35
|
+
*/
|
|
36
|
+
export interface AiRequest {
|
|
37
|
+
messages: AiMessage[];
|
|
38
|
+
model?: string;
|
|
39
|
+
temperature?: number;
|
|
40
|
+
maxTokens?: number;
|
|
41
|
+
tools?: AiToolDefinition[];
|
|
42
|
+
/** Provider name override */
|
|
43
|
+
provider?: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Non-streaming AI response
|
|
47
|
+
*/
|
|
48
|
+
export interface AiResponse {
|
|
49
|
+
content: string;
|
|
50
|
+
toolCalls?: AiToolCall[];
|
|
51
|
+
model: string;
|
|
52
|
+
provider: string;
|
|
53
|
+
usage: AiUsage;
|
|
54
|
+
finishReason: 'stop' | 'tool_calls' | 'length' | 'error';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Streaming AI chunk
|
|
58
|
+
*/
|
|
59
|
+
export interface AiChunk {
|
|
60
|
+
content?: string;
|
|
61
|
+
toolCallDelta?: {
|
|
62
|
+
index: number;
|
|
63
|
+
id?: string;
|
|
64
|
+
name?: string;
|
|
65
|
+
argumentsDelta?: string;
|
|
66
|
+
};
|
|
67
|
+
done: boolean;
|
|
68
|
+
model?: string;
|
|
69
|
+
usage?: AiUsage;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Token usage statistics
|
|
73
|
+
*/
|
|
74
|
+
export interface AiUsage {
|
|
75
|
+
promptTokens: number;
|
|
76
|
+
completionTokens: number;
|
|
77
|
+
totalTokens: number;
|
|
78
|
+
/** Estimated cost in USD */
|
|
79
|
+
estimatedCostUsd?: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* LLM provider abstraction interface
|
|
83
|
+
*/
|
|
84
|
+
export interface LlmProvider {
|
|
85
|
+
/**
|
|
86
|
+
* Non-streaming completion
|
|
87
|
+
*/
|
|
88
|
+
complete(request: AiRequest): Promise<AiResponse>;
|
|
89
|
+
/**
|
|
90
|
+
* Streaming completion — returns SSE-encoded ReadableStream
|
|
91
|
+
*/
|
|
92
|
+
stream(request: AiRequest): ReadableStream<Uint8Array>;
|
|
93
|
+
/**
|
|
94
|
+
* Approximate token count for messages
|
|
95
|
+
*/
|
|
96
|
+
countTokens(messages: AiMessage[]): number;
|
|
97
|
+
/** Provider name */
|
|
98
|
+
readonly name: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Provider configuration entry
|
|
102
|
+
*/
|
|
103
|
+
export interface AiProviderConfig<T = unknown> {
|
|
104
|
+
name: string;
|
|
105
|
+
provider: new (config: T) => LlmProvider;
|
|
106
|
+
config: T;
|
|
107
|
+
/** Use this provider by default */
|
|
108
|
+
default?: boolean;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* AiModule configuration
|
|
112
|
+
*/
|
|
113
|
+
export interface AiModuleOptions {
|
|
114
|
+
providers: AiProviderConfig[];
|
|
115
|
+
/** Enable provider fallback chain on error */
|
|
116
|
+
fallback?: boolean;
|
|
117
|
+
/** Request timeout in ms */
|
|
118
|
+
timeout?: number;
|
|
119
|
+
/** Tool calling configuration */
|
|
120
|
+
tools?: {
|
|
121
|
+
/** Auto-discover @AiTool() decorated methods */
|
|
122
|
+
autoDiscover?: boolean;
|
|
123
|
+
/** Max tool call iterations per request */
|
|
124
|
+
maxIterations?: number;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
export declare const AI_SERVICE_TOKEN: unique symbol;
|
|
128
|
+
export declare const AI_MODULE_OPTIONS_TOKEN: unique symbol;
|
|
129
|
+
export declare const AI_TOOL_REGISTRY_TOKEN: unique symbol;
|
|
130
|
+
/**
|
|
131
|
+
* Metadata key for @AiTool decorator
|
|
132
|
+
*/
|
|
133
|
+
export declare const AI_TOOL_METADATA_KEY = "@dangao/bun-server:ai:tool";
|
|
134
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/ai/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,MAAM,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,aAAa,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,SAAS,CAAC,EAAE,UAAU,EAAE,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC3B,6BAA6B;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,UAAU,EAAE,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,EAAE,MAAM,GAAG,YAAY,GAAG,QAAQ,GAAG,OAAO,CAAC;CAC1D;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE;QACd,KAAK,EAAE,MAAM,CAAC;QACd,EAAE,CAAC,EAAE,MAAM,CAAC;QACZ,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;IACF,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,4BAA4B;IAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAClD;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;IACvD;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;IAC3C,oBAAoB;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,OAAO;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,KAAK,MAAM,EAAE,CAAC,KAAK,WAAW,CAAC;IACzC,MAAM,EAAE,CAAC,CAAC;IACV,mCAAmC;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,4BAA4B;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iCAAiC;IACjC,KAAK,CAAC,EAAE;QACN,gDAAgD;QAChD,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,2CAA2C;QAC3C,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;CACH;AAED,eAAO,MAAM,gBAAgB,eAA0C,CAAC;AACxE,eAAO,MAAM,uBAAuB,eAA0C,CAAC;AAC/E,eAAO,MAAM,sBAAsB,eAAgD,CAAC;AAEpF;;GAEG;AACH,eAAO,MAAM,oBAAoB,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type AiGuardModuleOptions } from './types';
|
|
2
|
+
export declare class AiGuardModule {
|
|
3
|
+
/**
|
|
4
|
+
* Configure AI safety guards.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* AiGuardModule.forRoot({
|
|
9
|
+
* piiDetection: true,
|
|
10
|
+
* moderation: { openaiApiKey: process.env.OPENAI_API_KEY },
|
|
11
|
+
* promptInjection: { sensitivity: 'medium' },
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
static forRoot(options?: AiGuardModuleOptions): typeof AiGuardModule;
|
|
16
|
+
static reset(): void;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=ai-guard-module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-guard-module.d.ts","sourceRoot":"","sources":["../../src/ai-guard/ai-guard-module.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,SAAS,CAAC;AAEjB,qBACa,aAAa;IACxB;;;;;;;;;;;OAWG;WACW,OAAO,CAAC,OAAO,GAAE,oBAAyB,GAAG,OAAO,aAAa;WAuBjE,KAAK,IAAI,IAAI;CAG5B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AiGuardModuleOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Mark a controller method to run AI guard checks on the request body.
|
|
4
|
+
* Requires `AiGuardModule` to be configured.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* \@POST('/chat')
|
|
9
|
+
* \@AiGuard({ piiDetection: true, moderation: true, promptInjection: true })
|
|
10
|
+
* public async chat(\@Body() body: { message: string }) {
|
|
11
|
+
* // body.message has already been checked and sanitized
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function AiGuard(options?: Partial<AiGuardModuleOptions>): MethodDecorator;
|
|
16
|
+
//# sourceMappingURL=decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../../src/ai-guard/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAGpD;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,OAAO,CAAC,oBAAoB,CAAM,GAAG,eAAe,CAIpF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ModerationResult } from '../types';
|
|
2
|
+
export interface ContentModeratorConfig {
|
|
3
|
+
/** OpenAI API key for the Moderation API */
|
|
4
|
+
openaiApiKey?: string;
|
|
5
|
+
/** Custom moderator function (overrides OpenAI if provided) */
|
|
6
|
+
moderator?: (text: string) => Promise<ModerationResult>;
|
|
7
|
+
/** Categories that will cause content to be flagged */
|
|
8
|
+
blockCategories?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Content moderation using OpenAI Moderation API or a custom function.
|
|
12
|
+
*/
|
|
13
|
+
export declare class ContentModerator {
|
|
14
|
+
private readonly config;
|
|
15
|
+
constructor(config?: ContentModeratorConfig);
|
|
16
|
+
/**
|
|
17
|
+
* Check text for harmful content
|
|
18
|
+
*/
|
|
19
|
+
moderate(text: string): Promise<ModerationResult>;
|
|
20
|
+
/**
|
|
21
|
+
* Check whether the result should block the request
|
|
22
|
+
*/
|
|
23
|
+
isBlocked(result: ModerationResult): boolean;
|
|
24
|
+
private moderateWithOpenAI;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=content-moderator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-moderator.d.ts","sourceRoot":"","sources":["../../../src/ai-guard/detectors/content-moderator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAEjD,MAAM,WAAW,sBAAsB;IACrC,4CAA4C;IAC5C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACxD,uDAAuD;IACvD,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyB;gBAE7B,MAAM,GAAE,sBAA2B;IAItD;;OAEG;IACU,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAa9D;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO;YAOrC,kBAAkB;CAgCjC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { InjectionDetectionResult } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Detects prompt injection attacks using heuristic pattern matching.
|
|
4
|
+
*/
|
|
5
|
+
export declare class PromptInjectionDetector {
|
|
6
|
+
private readonly threshold;
|
|
7
|
+
constructor(sensitivity?: 'low' | 'medium' | 'high');
|
|
8
|
+
/**
|
|
9
|
+
* Analyze text for prompt injection patterns
|
|
10
|
+
*/
|
|
11
|
+
detect(text: string): InjectionDetectionResult;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=injection-detector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injection-detector.d.ts","sourceRoot":"","sources":["../../../src/ai-guard/detectors/injection-detector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC;AAezD;;GAEG;AACH,qBAAa,uBAAuB;IAClC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEhB,WAAW,GAAE,KAAK,GAAG,QAAQ,GAAG,MAAiB;IAIpE;;OAEG;IACI,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,wBAAwB;CAmBtD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PiiDetectionResult } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Detects and optionally redacts PII from text using regex patterns.
|
|
4
|
+
*/
|
|
5
|
+
export declare class PiiDetector {
|
|
6
|
+
/**
|
|
7
|
+
* Analyze text for PII
|
|
8
|
+
*/
|
|
9
|
+
detect(text: string, redact?: boolean): PiiDetectionResult;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=pii-detector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pii-detector.d.ts","sourceRoot":"","sources":["../../../src/ai-guard/detectors/pii-detector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAmCnD;;GAEG;AACH,qBAAa,WAAW;IACtB;;OAEG;IACI,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,UAAO,GAAG,kBAAkB;CAqB/D"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './decorators';
|
|
3
|
+
export * from './service';
|
|
4
|
+
export * from './ai-guard-module';
|
|
5
|
+
export * from './detectors/pii-detector';
|
|
6
|
+
export * from './detectors/content-moderator';
|
|
7
|
+
export * from './detectors/injection-detector';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ai-guard/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC"}
|