mcp-agent-foundry 1.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/LICENSE +21 -0
- package/README.md +477 -0
- package/dist/cli/install-skills.d.ts +11 -0
- package/dist/cli/install-skills.d.ts.map +1 -0
- package/dist/cli/install-skills.js +143 -0
- package/dist/cli/install-skills.js.map +1 -0
- package/dist/cli/recovery-commands.d.ts +41 -0
- package/dist/cli/recovery-commands.d.ts.map +1 -0
- package/dist/cli/recovery-commands.js +241 -0
- package/dist/cli/recovery-commands.js.map +1 -0
- package/dist/cli/setup-wizard.d.ts +25 -0
- package/dist/cli/setup-wizard.d.ts.map +1 -0
- package/dist/cli/setup-wizard.js +1417 -0
- package/dist/cli/setup-wizard.js.map +1 -0
- package/dist/cli/test-connection.d.ts +45 -0
- package/dist/cli/test-connection.d.ts.map +1 -0
- package/dist/cli/test-connection.js +317 -0
- package/dist/cli/test-connection.js.map +1 -0
- package/dist/cli.d.ts +75 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +704 -0
- package/dist/cli.js.map +1 -0
- package/dist/config/defaults.d.ts +57 -0
- package/dist/config/defaults.d.ts.map +1 -0
- package/dist/config/defaults.js +99 -0
- package/dist/config/defaults.js.map +1 -0
- package/dist/config/index.d.ts +14 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +22 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/manager.d.ts +184 -0
- package/dist/config/manager.d.ts.map +1 -0
- package/dist/config/manager.js +347 -0
- package/dist/config/manager.js.map +1 -0
- package/dist/config/merger.d.ts +76 -0
- package/dist/config/merger.d.ts.map +1 -0
- package/dist/config/merger.js +189 -0
- package/dist/config/merger.js.map +1 -0
- package/dist/config/schema.d.ts +20 -0
- package/dist/config/schema.d.ts.map +1 -0
- package/dist/config/schema.js +20 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/config/validator.d.ts +254 -0
- package/dist/config/validator.d.ts.map +1 -0
- package/dist/config/validator.js +363 -0
- package/dist/config/validator.js.map +1 -0
- package/dist/config/worktree-defaults.d.ts +23 -0
- package/dist/config/worktree-defaults.d.ts.map +1 -0
- package/dist/config/worktree-defaults.js +78 -0
- package/dist/config/worktree-defaults.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/tools/compare-agents.d.ts +25 -0
- package/dist/mcp/tools/compare-agents.d.ts.map +1 -0
- package/dist/mcp/tools/compare-agents.js +177 -0
- package/dist/mcp/tools/compare-agents.js.map +1 -0
- package/dist/mcp/tools/critique-plan.d.ts +26 -0
- package/dist/mcp/tools/critique-plan.d.ts.map +1 -0
- package/dist/mcp/tools/critique-plan.js +162 -0
- package/dist/mcp/tools/critique-plan.js.map +1 -0
- package/dist/mcp/tools/design-feedback.d.ts +26 -0
- package/dist/mcp/tools/design-feedback.d.ts.map +1 -0
- package/dist/mcp/tools/design-feedback.js +216 -0
- package/dist/mcp/tools/design-feedback.js.map +1 -0
- package/dist/mcp/tools/index.d.ts +50 -0
- package/dist/mcp/tools/index.d.ts.map +1 -0
- package/dist/mcp/tools/index.js +191 -0
- package/dist/mcp/tools/index.js.map +1 -0
- package/dist/mcp/tools/invoke-agent.d.ts +25 -0
- package/dist/mcp/tools/invoke-agent.d.ts.map +1 -0
- package/dist/mcp/tools/invoke-agent.js +141 -0
- package/dist/mcp/tools/invoke-agent.js.map +1 -0
- package/dist/mcp/tools/review-code.d.ts +25 -0
- package/dist/mcp/tools/review-code.d.ts.map +1 -0
- package/dist/mcp/tools/review-code.js +170 -0
- package/dist/mcp/tools/review-code.js.map +1 -0
- package/dist/mcp/tools/tasks/claim-next-task.d.ts +22 -0
- package/dist/mcp/tools/tasks/claim-next-task.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/claim-next-task.js +203 -0
- package/dist/mcp/tools/tasks/claim-next-task.js.map +1 -0
- package/dist/mcp/tools/tasks/create-routed-task.d.ts +17 -0
- package/dist/mcp/tools/tasks/create-routed-task.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/create-routed-task.js +178 -0
- package/dist/mcp/tools/tasks/create-routed-task.js.map +1 -0
- package/dist/mcp/tools/tasks/execute-pipeline.d.ts +22 -0
- package/dist/mcp/tools/tasks/execute-pipeline.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/execute-pipeline.js +401 -0
- package/dist/mcp/tools/tasks/execute-pipeline.js.map +1 -0
- package/dist/mcp/tools/tasks/execute-task.d.ts +32 -0
- package/dist/mcp/tools/tasks/execute-task.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/execute-task.js +284 -0
- package/dist/mcp/tools/tasks/execute-task.js.map +1 -0
- package/dist/mcp/tools/tasks/get-pipeline-status.d.ts +26 -0
- package/dist/mcp/tools/tasks/get-pipeline-status.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/get-pipeline-status.js +460 -0
- package/dist/mcp/tools/tasks/get-pipeline-status.js.map +1 -0
- package/dist/mcp/tools/tasks/index.d.ts +36 -0
- package/dist/mcp/tools/tasks/index.d.ts.map +1 -0
- package/dist/mcp/tools/tasks/index.js +66 -0
- package/dist/mcp/tools/tasks/index.js.map +1 -0
- package/dist/mcp/tools/worktree/cleanup-worktrees.d.ts +17 -0
- package/dist/mcp/tools/worktree/cleanup-worktrees.d.ts.map +1 -0
- package/dist/mcp/tools/worktree/cleanup-worktrees.js +147 -0
- package/dist/mcp/tools/worktree/cleanup-worktrees.js.map +1 -0
- package/dist/mcp/tools/worktree/get-worktree-status.d.ts +17 -0
- package/dist/mcp/tools/worktree/get-worktree-status.d.ts.map +1 -0
- package/dist/mcp/tools/worktree/get-worktree-status.js +123 -0
- package/dist/mcp/tools/worktree/get-worktree-status.js.map +1 -0
- package/dist/mcp/tools/worktree/index.d.ts +41 -0
- package/dist/mcp/tools/worktree/index.d.ts.map +1 -0
- package/dist/mcp/tools/worktree/index.js +69 -0
- package/dist/mcp/tools/worktree/index.js.map +1 -0
- package/dist/mcp/tools/worktree/list-worktrees.d.ts +17 -0
- package/dist/mcp/tools/worktree/list-worktrees.d.ts.map +1 -0
- package/dist/mcp/tools/worktree/list-worktrees.js +136 -0
- package/dist/mcp/tools/worktree/list-worktrees.js.map +1 -0
- package/dist/mcp/tools/worktree/resolve-conflicts.d.ts +19 -0
- package/dist/mcp/tools/worktree/resolve-conflicts.d.ts.map +1 -0
- package/dist/mcp/tools/worktree/resolve-conflicts.js +228 -0
- package/dist/mcp/tools/worktree/resolve-conflicts.js.map +1 -0
- package/dist/mcp/transport/stdio.d.ts +13 -0
- package/dist/mcp/transport/stdio.d.ts.map +1 -0
- package/dist/mcp/transport/stdio.js +15 -0
- package/dist/mcp/transport/stdio.js.map +1 -0
- package/dist/observability/logger.d.ts +137 -0
- package/dist/observability/logger.d.ts.map +1 -0
- package/dist/observability/logger.js +235 -0
- package/dist/observability/logger.js.map +1 -0
- package/dist/observability/metrics.d.ts +250 -0
- package/dist/observability/metrics.d.ts.map +1 -0
- package/dist/observability/metrics.js +364 -0
- package/dist/observability/metrics.js.map +1 -0
- package/dist/persistence/index.d.ts +9 -0
- package/dist/persistence/index.d.ts.map +1 -0
- package/dist/persistence/index.js +9 -0
- package/dist/persistence/index.js.map +1 -0
- package/dist/persistence/state-schema.d.ts +116 -0
- package/dist/persistence/state-schema.d.ts.map +1 -0
- package/dist/persistence/state-schema.js +28 -0
- package/dist/persistence/state-schema.js.map +1 -0
- package/dist/persistence/state-store.d.ts +111 -0
- package/dist/persistence/state-store.d.ts.map +1 -0
- package/dist/persistence/state-store.js +291 -0
- package/dist/persistence/state-store.js.map +1 -0
- package/dist/providers/anthropic.d.ts +164 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +500 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/base.d.ts +151 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +227 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/gemini.d.ts +85 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +414 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/kimi.d.ts +19 -0
- package/dist/providers/kimi.d.ts.map +1 -0
- package/dist/providers/kimi.js +20 -0
- package/dist/providers/kimi.js.map +1 -0
- package/dist/providers/manager.d.ts +160 -0
- package/dist/providers/manager.d.ts.map +1 -0
- package/dist/providers/manager.js +264 -0
- package/dist/providers/manager.js.map +1 -0
- package/dist/providers/ollama.d.ts +83 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +453 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +96 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +457 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/zai.d.ts +19 -0
- package/dist/providers/zai.d.ts.map +1 -0
- package/dist/providers/zai.js +20 -0
- package/dist/providers/zai.js.map +1 -0
- package/dist/router/context-manager.d.ts +2 -0
- package/dist/router/context-manager.d.ts.map +1 -0
- package/dist/router/context-manager.js +3 -0
- package/dist/router/context-manager.js.map +1 -0
- package/dist/router/engine.d.ts +169 -0
- package/dist/router/engine.d.ts.map +1 -0
- package/dist/router/engine.js +435 -0
- package/dist/router/engine.js.map +1 -0
- package/dist/router/pattern-executor.d.ts +317 -0
- package/dist/router/pattern-executor.d.ts.map +1 -0
- package/dist/router/pattern-executor.js +571 -0
- package/dist/router/pattern-executor.js.map +1 -0
- package/dist/router/role-resolver.d.ts +59 -0
- package/dist/router/role-resolver.d.ts.map +1 -0
- package/dist/router/role-resolver.js +95 -0
- package/dist/router/role-resolver.js.map +1 -0
- package/dist/server.d.ts +32 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +223 -0
- package/dist/server.js.map +1 -0
- package/dist/startup.d.ts +78 -0
- package/dist/startup.d.ts.map +1 -0
- package/dist/startup.js +107 -0
- package/dist/startup.js.map +1 -0
- package/dist/tasks/coordinator.d.ts +141 -0
- package/dist/tasks/coordinator.d.ts.map +1 -0
- package/dist/tasks/coordinator.js +331 -0
- package/dist/tasks/coordinator.js.map +1 -0
- package/dist/tasks/index.d.ts +13 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +13 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/persistent-state-coordinator.d.ts +89 -0
- package/dist/tasks/persistent-state-coordinator.d.ts.map +1 -0
- package/dist/tasks/persistent-state-coordinator.js +371 -0
- package/dist/tasks/persistent-state-coordinator.js.map +1 -0
- package/dist/tasks/pipeline-manager.d.ts +103 -0
- package/dist/tasks/pipeline-manager.d.ts.map +1 -0
- package/dist/tasks/pipeline-manager.js +358 -0
- package/dist/tasks/pipeline-manager.js.map +1 -0
- package/dist/tasks/state-coordinator.d.ts +79 -0
- package/dist/tasks/state-coordinator.d.ts.map +1 -0
- package/dist/tasks/state-coordinator.js +200 -0
- package/dist/tasks/state-coordinator.js.map +1 -0
- package/dist/tasks/worker-mode.d.ts +65 -0
- package/dist/tasks/worker-mode.d.ts.map +1 -0
- package/dist/tasks/worker-mode.js +208 -0
- package/dist/tasks/worker-mode.js.map +1 -0
- package/dist/translation/errors.d.ts +203 -0
- package/dist/translation/errors.d.ts.map +1 -0
- package/dist/translation/errors.js +477 -0
- package/dist/translation/errors.js.map +1 -0
- package/dist/translation/index.d.ts +12 -0
- package/dist/translation/index.d.ts.map +1 -0
- package/dist/translation/index.js +32 -0
- package/dist/translation/index.js.map +1 -0
- package/dist/translation/messages.d.ts +295 -0
- package/dist/translation/messages.d.ts.map +1 -0
- package/dist/translation/messages.js +557 -0
- package/dist/translation/messages.js.map +1 -0
- package/dist/translation/streaming.d.ts +226 -0
- package/dist/translation/streaming.d.ts.map +1 -0
- package/dist/translation/streaming.js +520 -0
- package/dist/translation/streaming.js.map +1 -0
- package/dist/translation/tools.d.ts +209 -0
- package/dist/translation/tools.d.ts.map +1 -0
- package/dist/translation/tools.js +331 -0
- package/dist/translation/tools.js.map +1 -0
- package/dist/types.d.ts +747 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +86 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/circuit-breaker.d.ts +175 -0
- package/dist/utils/circuit-breaker.d.ts.map +1 -0
- package/dist/utils/circuit-breaker.js +315 -0
- package/dist/utils/circuit-breaker.js.map +1 -0
- package/dist/utils/env.d.ts +2 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/env.js +3 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/git.d.ts +58 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +197 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +9 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/merge-ordering.d.ts +45 -0
- package/dist/utils/merge-ordering.d.ts.map +1 -0
- package/dist/utils/merge-ordering.js +128 -0
- package/dist/utils/merge-ordering.js.map +1 -0
- package/dist/utils/retry.d.ts +106 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +188 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/worktrees/branch-manager.d.ts +55 -0
- package/dist/worktrees/branch-manager.d.ts.map +1 -0
- package/dist/worktrees/branch-manager.js +129 -0
- package/dist/worktrees/branch-manager.js.map +1 -0
- package/dist/worktrees/conflict-handler.d.ts +72 -0
- package/dist/worktrees/conflict-handler.d.ts.map +1 -0
- package/dist/worktrees/conflict-handler.js +287 -0
- package/dist/worktrees/conflict-handler.js.map +1 -0
- package/dist/worktrees/conflict-parser.d.ts +28 -0
- package/dist/worktrees/conflict-parser.d.ts.map +1 -0
- package/dist/worktrees/conflict-parser.js +140 -0
- package/dist/worktrees/conflict-parser.js.map +1 -0
- package/dist/worktrees/index.d.ts +20 -0
- package/dist/worktrees/index.d.ts.map +1 -0
- package/dist/worktrees/index.js +20 -0
- package/dist/worktrees/index.js.map +1 -0
- package/dist/worktrees/instructions.d.ts +20 -0
- package/dist/worktrees/instructions.d.ts.map +1 -0
- package/dist/worktrees/instructions.js +84 -0
- package/dist/worktrees/instructions.js.map +1 -0
- package/dist/worktrees/manager.d.ts +76 -0
- package/dist/worktrees/manager.d.ts.map +1 -0
- package/dist/worktrees/manager.js +277 -0
- package/dist/worktrees/manager.js.map +1 -0
- package/dist/worktrees/pipeline-merge-orchestrator.d.ts +55 -0
- package/dist/worktrees/pipeline-merge-orchestrator.d.ts.map +1 -0
- package/dist/worktrees/pipeline-merge-orchestrator.js +221 -0
- package/dist/worktrees/pipeline-merge-orchestrator.js.map +1 -0
- package/dist/worktrees/pool.d.ts +95 -0
- package/dist/worktrees/pool.d.ts.map +1 -0
- package/dist/worktrees/pool.js +271 -0
- package/dist/worktrees/pool.js.map +1 -0
- package/dist/worktrees/recovery.d.ts +94 -0
- package/dist/worktrees/recovery.d.ts.map +1 -0
- package/dist/worktrees/recovery.js +371 -0
- package/dist/worktrees/recovery.js.map +1 -0
- package/dist/worktrees/resource-manager.d.ts +74 -0
- package/dist/worktrees/resource-manager.d.ts.map +1 -0
- package/dist/worktrees/resource-manager.js +228 -0
- package/dist/worktrees/resource-manager.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Provider Implementation
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements common functionality for all LLM providers.
|
|
5
|
+
* Handles authentication headers, error handling, timeout management, and
|
|
6
|
+
* provides the interface that concrete providers must implement.
|
|
7
|
+
*/
|
|
8
|
+
import { type Provider, type ProviderConfig, type CompletionRequest, type CompletionResponse, type StreamChunk } from '../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Options for making HTTP requests to provider APIs.
|
|
11
|
+
*/
|
|
12
|
+
export interface RequestOptions {
|
|
13
|
+
/** HTTP method */
|
|
14
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
15
|
+
/** Request URL */
|
|
16
|
+
url: string;
|
|
17
|
+
/** Request headers */
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
/** Request body (will be JSON stringified) */
|
|
20
|
+
body?: unknown;
|
|
21
|
+
/** Timeout in milliseconds */
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
/** Whether this is a streaming request */
|
|
24
|
+
stream?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error response structure from provider APIs.
|
|
28
|
+
*/
|
|
29
|
+
export interface ProviderErrorResponse {
|
|
30
|
+
error?: {
|
|
31
|
+
message?: string;
|
|
32
|
+
type?: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
};
|
|
35
|
+
message?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Abstract base class for LLM providers.
|
|
39
|
+
*
|
|
40
|
+
* Provides common functionality including:
|
|
41
|
+
* - Authentication header management
|
|
42
|
+
* - HTTP request helpers with timeout support
|
|
43
|
+
* - Standardized error handling
|
|
44
|
+
* - Response parsing utilities
|
|
45
|
+
*
|
|
46
|
+
* Concrete providers must implement:
|
|
47
|
+
* - complete(): Execute a completion request
|
|
48
|
+
* - completeStream(): Execute a streaming completion request
|
|
49
|
+
* - healthCheck(): Verify provider connectivity
|
|
50
|
+
*/
|
|
51
|
+
export declare abstract class BaseProvider implements Provider {
|
|
52
|
+
/** Provider name identifier */
|
|
53
|
+
abstract readonly name: string;
|
|
54
|
+
/** Provider configuration */
|
|
55
|
+
protected readonly config: ProviderConfig;
|
|
56
|
+
/** Default timeout in milliseconds (3 minutes for complex LLM operations) */
|
|
57
|
+
protected readonly defaultTimeoutMs: number;
|
|
58
|
+
/**
|
|
59
|
+
* Create a new provider instance.
|
|
60
|
+
*
|
|
61
|
+
* @param config - Provider-specific configuration (API keys, URLs, etc.)
|
|
62
|
+
*/
|
|
63
|
+
constructor(config: ProviderConfig);
|
|
64
|
+
/**
|
|
65
|
+
* Execute a completion request and return the full response.
|
|
66
|
+
* Must be implemented by concrete providers.
|
|
67
|
+
*
|
|
68
|
+
* @param request - The completion request
|
|
69
|
+
* @returns Promise resolving to the completion response
|
|
70
|
+
*/
|
|
71
|
+
abstract complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Execute a streaming completion request.
|
|
74
|
+
* Must be implemented by concrete providers.
|
|
75
|
+
*
|
|
76
|
+
* @param request - The completion request
|
|
77
|
+
* @returns Async iterable of stream chunks
|
|
78
|
+
*/
|
|
79
|
+
abstract completeStream(request: CompletionRequest): AsyncIterable<StreamChunk>;
|
|
80
|
+
/**
|
|
81
|
+
* Check if the provider is available and configured correctly.
|
|
82
|
+
* Must be implemented by concrete providers.
|
|
83
|
+
*
|
|
84
|
+
* @throws ProviderError if the health check fails
|
|
85
|
+
*/
|
|
86
|
+
abstract healthCheck(): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Build common HTTP headers for provider requests.
|
|
89
|
+
* Includes authentication and content-type headers.
|
|
90
|
+
*
|
|
91
|
+
* @returns Headers object for HTTP requests
|
|
92
|
+
*/
|
|
93
|
+
protected buildHeaders(): Record<string, string>;
|
|
94
|
+
/**
|
|
95
|
+
* Make an HTTP request to the provider API with timeout support.
|
|
96
|
+
*
|
|
97
|
+
* @param options - Request options
|
|
98
|
+
* @returns Promise resolving to the parsed JSON response
|
|
99
|
+
* @throws ProviderError for API errors
|
|
100
|
+
* @throws TimeoutError if the request times out
|
|
101
|
+
*/
|
|
102
|
+
protected makeRequest<T>(options: RequestOptions): Promise<T>;
|
|
103
|
+
/**
|
|
104
|
+
* Make a streaming HTTP request to the provider API.
|
|
105
|
+
*
|
|
106
|
+
* @param options - Request options
|
|
107
|
+
* @returns Response object for streaming
|
|
108
|
+
* @throws ProviderError for API errors
|
|
109
|
+
* @throws TimeoutError if the connection times out
|
|
110
|
+
*/
|
|
111
|
+
protected makeStreamingRequest(options: RequestOptions): Promise<Response>;
|
|
112
|
+
/**
|
|
113
|
+
* Handle HTTP error responses from provider APIs.
|
|
114
|
+
* Throws appropriate error types based on status code.
|
|
115
|
+
*
|
|
116
|
+
* @param response - The HTTP response object
|
|
117
|
+
* @throws RateLimitError for 429 responses
|
|
118
|
+
* @throws AuthenticationError for 401 responses
|
|
119
|
+
* @throws ProviderError for other error responses
|
|
120
|
+
*/
|
|
121
|
+
protected handleErrorResponse(response: Response): Promise<never>;
|
|
122
|
+
/**
|
|
123
|
+
* Handle request-level errors (network, timeout, etc.).
|
|
124
|
+
*
|
|
125
|
+
* @param error - The caught error
|
|
126
|
+
* @param timeoutMs - The timeout that was configured
|
|
127
|
+
* @throws TimeoutError for abort errors
|
|
128
|
+
* @throws ProviderError for other errors
|
|
129
|
+
*/
|
|
130
|
+
protected handleRequestError(error: unknown, timeoutMs: number): never;
|
|
131
|
+
/**
|
|
132
|
+
* Get the base URL for API requests.
|
|
133
|
+
* Uses configured base_url or returns the default for the provider.
|
|
134
|
+
*
|
|
135
|
+
* @param defaultUrl - Default URL if not configured
|
|
136
|
+
* @returns The base URL to use for requests
|
|
137
|
+
*/
|
|
138
|
+
protected getBaseUrl(defaultUrl: string): string;
|
|
139
|
+
/**
|
|
140
|
+
* Create an AbortController with a timeout.
|
|
141
|
+
* Useful for streaming requests where we need to track the controller.
|
|
142
|
+
*
|
|
143
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
144
|
+
* @returns Object containing the controller and a cleanup function
|
|
145
|
+
*/
|
|
146
|
+
protected createTimeoutController(timeoutMs: number): {
|
|
147
|
+
controller: AbortController;
|
|
148
|
+
cleanup: () => void;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/providers/base.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAKP,K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,IAAI,OAAO,CAAC,IAAI,CAAC;IAE5C;;;;;OAKG;IACH,SAAS,CAAC,YAAY,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAkBhD;;;;;;;OAOG;cACa,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAoCnE;;;;;;;OAOG;cACa,oBAAoB,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IAmChF;;;;;;;;OAQG;cACa,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC;IAoCvE;;;;;;;OAOG;IACH,SAAS,CAAC,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK;IAyBtE;;;;;;OAMG;IACH,SAAS,CAAC,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;IAIhD;;;;;;OAMG;IACH,SAAS,CAAC,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG;QACpD,UAAU,EAAE,eAAe,CAAC;QAC5B,OAAO,EAAE,MAAM,IAAI,CAAC;KACrB;CASF"}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Provider Implementation
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements common functionality for all LLM providers.
|
|
5
|
+
* Handles authentication headers, error handling, timeout management, and
|
|
6
|
+
* provides the interface that concrete providers must implement.
|
|
7
|
+
*/
|
|
8
|
+
import { ProviderError, RateLimitError, AuthenticationError, TimeoutError } from '../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base class for LLM providers.
|
|
11
|
+
*
|
|
12
|
+
* Provides common functionality including:
|
|
13
|
+
* - Authentication header management
|
|
14
|
+
* - HTTP request helpers with timeout support
|
|
15
|
+
* - Standardized error handling
|
|
16
|
+
* - Response parsing utilities
|
|
17
|
+
*
|
|
18
|
+
* Concrete providers must implement:
|
|
19
|
+
* - complete(): Execute a completion request
|
|
20
|
+
* - completeStream(): Execute a streaming completion request
|
|
21
|
+
* - healthCheck(): Verify provider connectivity
|
|
22
|
+
*/
|
|
23
|
+
export class BaseProvider {
|
|
24
|
+
/** Provider configuration */
|
|
25
|
+
config;
|
|
26
|
+
/** Default timeout in milliseconds (3 minutes for complex LLM operations) */
|
|
27
|
+
defaultTimeoutMs = 180000;
|
|
28
|
+
/**
|
|
29
|
+
* Create a new provider instance.
|
|
30
|
+
*
|
|
31
|
+
* @param config - Provider-specific configuration (API keys, URLs, etc.)
|
|
32
|
+
*/
|
|
33
|
+
constructor(config) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Build common HTTP headers for provider requests.
|
|
38
|
+
* Includes authentication and content-type headers.
|
|
39
|
+
*
|
|
40
|
+
* @returns Headers object for HTTP requests
|
|
41
|
+
*/
|
|
42
|
+
buildHeaders() {
|
|
43
|
+
const headers = {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
};
|
|
46
|
+
// Add API key authentication if configured
|
|
47
|
+
if (this.config.api_key) {
|
|
48
|
+
headers['Authorization'] = `Bearer ${this.config.api_key}`;
|
|
49
|
+
}
|
|
50
|
+
// Add any custom headers from config
|
|
51
|
+
if (this.config.headers) {
|
|
52
|
+
Object.assign(headers, this.config.headers);
|
|
53
|
+
}
|
|
54
|
+
return headers;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Make an HTTP request to the provider API with timeout support.
|
|
58
|
+
*
|
|
59
|
+
* @param options - Request options
|
|
60
|
+
* @returns Promise resolving to the parsed JSON response
|
|
61
|
+
* @throws ProviderError for API errors
|
|
62
|
+
* @throws TimeoutError if the request times out
|
|
63
|
+
*/
|
|
64
|
+
async makeRequest(options) {
|
|
65
|
+
const { method, url, headers, body, timeoutMs = this.defaultTimeoutMs } = options;
|
|
66
|
+
// Create abort controller for timeout
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
const timeoutId = setTimeout(() => { controller.abort(); }, timeoutMs);
|
|
69
|
+
try {
|
|
70
|
+
const fetchOptions = {
|
|
71
|
+
method,
|
|
72
|
+
headers,
|
|
73
|
+
signal: controller.signal,
|
|
74
|
+
};
|
|
75
|
+
if (body) {
|
|
76
|
+
fetchOptions.body = JSON.stringify(body);
|
|
77
|
+
}
|
|
78
|
+
const response = await fetch(url, fetchOptions);
|
|
79
|
+
// Clear timeout on successful response
|
|
80
|
+
clearTimeout(timeoutId);
|
|
81
|
+
// Handle non-OK responses
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
await this.handleErrorResponse(response);
|
|
84
|
+
}
|
|
85
|
+
// Parse and return JSON response
|
|
86
|
+
return (await response.json());
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
clearTimeout(timeoutId);
|
|
90
|
+
this.handleRequestError(error, timeoutMs);
|
|
91
|
+
// handleRequestError always throws, but TypeScript needs this
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Make a streaming HTTP request to the provider API.
|
|
97
|
+
*
|
|
98
|
+
* @param options - Request options
|
|
99
|
+
* @returns Response object for streaming
|
|
100
|
+
* @throws ProviderError for API errors
|
|
101
|
+
* @throws TimeoutError if the connection times out
|
|
102
|
+
*/
|
|
103
|
+
async makeStreamingRequest(options) {
|
|
104
|
+
const { method, url, headers, body, timeoutMs = this.defaultTimeoutMs } = options;
|
|
105
|
+
// Create abort controller for timeout
|
|
106
|
+
const controller = new AbortController();
|
|
107
|
+
const timeoutId = setTimeout(() => { controller.abort(); }, timeoutMs);
|
|
108
|
+
try {
|
|
109
|
+
const fetchOptions = {
|
|
110
|
+
method,
|
|
111
|
+
headers,
|
|
112
|
+
signal: controller.signal,
|
|
113
|
+
};
|
|
114
|
+
if (body) {
|
|
115
|
+
fetchOptions.body = JSON.stringify(body);
|
|
116
|
+
}
|
|
117
|
+
const response = await fetch(url, fetchOptions);
|
|
118
|
+
// Clear timeout once we have a response (streaming will handle its own timing)
|
|
119
|
+
clearTimeout(timeoutId);
|
|
120
|
+
// Handle non-OK responses
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
await this.handleErrorResponse(response);
|
|
123
|
+
}
|
|
124
|
+
return response;
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
clearTimeout(timeoutId);
|
|
128
|
+
this.handleRequestError(error, timeoutMs);
|
|
129
|
+
// handleRequestError always throws, but TypeScript needs this
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Handle HTTP error responses from provider APIs.
|
|
135
|
+
* Throws appropriate error types based on status code.
|
|
136
|
+
*
|
|
137
|
+
* @param response - The HTTP response object
|
|
138
|
+
* @throws RateLimitError for 429 responses
|
|
139
|
+
* @throws AuthenticationError for 401 responses
|
|
140
|
+
* @throws ProviderError for other error responses
|
|
141
|
+
*/
|
|
142
|
+
async handleErrorResponse(response) {
|
|
143
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
144
|
+
let retryAfterMs;
|
|
145
|
+
// Try to parse error details from response body
|
|
146
|
+
try {
|
|
147
|
+
const errorBody = (await response.json());
|
|
148
|
+
if (errorBody.error?.message) {
|
|
149
|
+
errorMessage = errorBody.error.message;
|
|
150
|
+
}
|
|
151
|
+
else if (errorBody.message) {
|
|
152
|
+
errorMessage = errorBody.message;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// Ignore JSON parse errors, use default message
|
|
157
|
+
}
|
|
158
|
+
// Check for retry-after header on rate limits
|
|
159
|
+
const retryAfter = response.headers.get('retry-after');
|
|
160
|
+
if (retryAfter) {
|
|
161
|
+
retryAfterMs = parseInt(retryAfter, 10) * 1000;
|
|
162
|
+
}
|
|
163
|
+
// Throw appropriate error type based on status
|
|
164
|
+
switch (response.status) {
|
|
165
|
+
case 401:
|
|
166
|
+
case 403:
|
|
167
|
+
throw new AuthenticationError(this.name);
|
|
168
|
+
case 429:
|
|
169
|
+
throw new RateLimitError(this.name, retryAfterMs);
|
|
170
|
+
default:
|
|
171
|
+
throw new ProviderError(errorMessage, this.name, response.status);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Handle request-level errors (network, timeout, etc.).
|
|
176
|
+
*
|
|
177
|
+
* @param error - The caught error
|
|
178
|
+
* @param timeoutMs - The timeout that was configured
|
|
179
|
+
* @throws TimeoutError for abort errors
|
|
180
|
+
* @throws ProviderError for other errors
|
|
181
|
+
*/
|
|
182
|
+
handleRequestError(error, timeoutMs) {
|
|
183
|
+
// Check if this is an abort error (timeout)
|
|
184
|
+
if (error instanceof Error) {
|
|
185
|
+
if (error.name === 'AbortError') {
|
|
186
|
+
throw new TimeoutError(this.name, timeoutMs);
|
|
187
|
+
}
|
|
188
|
+
// Re-throw if it's already one of our error types
|
|
189
|
+
if (error instanceof ProviderError ||
|
|
190
|
+
error instanceof RateLimitError ||
|
|
191
|
+
error instanceof AuthenticationError ||
|
|
192
|
+
error instanceof TimeoutError) {
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
// Wrap other errors
|
|
196
|
+
throw new ProviderError(error.message, this.name, undefined, error);
|
|
197
|
+
}
|
|
198
|
+
// Handle non-Error objects
|
|
199
|
+
throw new ProviderError(String(error), this.name);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Get the base URL for API requests.
|
|
203
|
+
* Uses configured base_url or returns the default for the provider.
|
|
204
|
+
*
|
|
205
|
+
* @param defaultUrl - Default URL if not configured
|
|
206
|
+
* @returns The base URL to use for requests
|
|
207
|
+
*/
|
|
208
|
+
getBaseUrl(defaultUrl) {
|
|
209
|
+
return this.config.base_url || defaultUrl;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Create an AbortController with a timeout.
|
|
213
|
+
* Useful for streaming requests where we need to track the controller.
|
|
214
|
+
*
|
|
215
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
216
|
+
* @returns Object containing the controller and a cleanup function
|
|
217
|
+
*/
|
|
218
|
+
createTimeoutController(timeoutMs) {
|
|
219
|
+
const controller = new AbortController();
|
|
220
|
+
const timeoutId = setTimeout(() => { controller.abort(); }, timeoutMs);
|
|
221
|
+
return {
|
|
222
|
+
controller,
|
|
223
|
+
cleanup: () => { clearTimeout(timeoutId); },
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/providers/base.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,YAAY,EAMA,MAAM,aAAa,CAAC;AAiClC;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAgB,YAAY;IAIhC,6BAA6B;IACV,MAAM,CAAiB;IAE1C,6EAA6E;IAC1D,gBAAgB,GAAW,MAAM,CAAC;IAErD;;;;OAIG;IACH,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IA4BD;;;;;OAKG;IACO,YAAY;QACpB,MAAM,OAAO,GAA2B;YACtC,cAAc,EAAE,kBAAkB;SACnC,CAAC;QAEF,2CAA2C;QAC3C,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACxB,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC7D,CAAC;QAED,qCAAqC;QACrC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACxB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;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,YAAY,mBAAmB;gBACpC,KAAK,YAAY,YAAY,EAC7B,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;YAED,oBAAoB;YACpB,MAAM,IAAI,aAAa,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;QACtE,CAAC;QAED,2BAA2B;QAC3B,MAAM,IAAI,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;OAMG;IACO,UAAU,CAAC,UAAkB;QACrC,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,IAAI,UAAU,CAAC;IAC5C,CAAC;IAED;;;;;;OAMG;IACO,uBAAuB,CAAC,SAAiB;QAIjD,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;QACzC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;QAEvE,OAAO;YACL,UAAU;YACV,OAAO,EAAE,GAAG,EAAE,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;SAC5C,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google Gemini Provider Implementation
|
|
3
|
+
*
|
|
4
|
+
* Provider adapter for the Google Gemini API (Generative Language API).
|
|
5
|
+
* Handles message formatting, streaming, and tool use for Gemini models.
|
|
6
|
+
*
|
|
7
|
+
* Key differences from Anthropic format:
|
|
8
|
+
* - Messages use "contents" array with "parts" instead of "content"
|
|
9
|
+
* - System prompt is a separate "systemInstruction" field
|
|
10
|
+
* - Tool schemas use "parameters" (OpenAI style) in function declarations
|
|
11
|
+
* - Tool calls return "functionCall" parts, results use "functionResponse" parts
|
|
12
|
+
* - Auth via API key query param or Authorization header
|
|
13
|
+
*/
|
|
14
|
+
import { BaseProvider } from './base.js';
|
|
15
|
+
import type { CompletionRequest, CompletionResponse, StreamChunk, ProviderConfig } from '../types.js';
|
|
16
|
+
/**
|
|
17
|
+
* Google Gemini API provider adapter.
|
|
18
|
+
*
|
|
19
|
+
* Implements the Provider interface for Google's Gemini models.
|
|
20
|
+
* Handles:
|
|
21
|
+
* - Message format translation (our format -> Gemini format)
|
|
22
|
+
* - Streaming SSE event parsing
|
|
23
|
+
* - Tool use / function calling
|
|
24
|
+
* - Authentication via API key
|
|
25
|
+
*/
|
|
26
|
+
export declare class GeminiProvider extends BaseProvider {
|
|
27
|
+
readonly name = "google";
|
|
28
|
+
private static readonly DEFAULT_BASE_URL;
|
|
29
|
+
/** Default max tokens if not specified in request */
|
|
30
|
+
private static readonly DEFAULT_MAX_TOKENS;
|
|
31
|
+
constructor(config: ProviderConfig);
|
|
32
|
+
/**
|
|
33
|
+
* Build the full API URL with model and action.
|
|
34
|
+
* Gemini uses URL path for model selection and action.
|
|
35
|
+
*
|
|
36
|
+
* @param model - Model identifier
|
|
37
|
+
* @param action - API action (generateContent, streamGenerateContent)
|
|
38
|
+
* @returns Full API URL
|
|
39
|
+
*/
|
|
40
|
+
private buildUrl;
|
|
41
|
+
/**
|
|
42
|
+
* Build headers for Gemini API requests.
|
|
43
|
+
* Uses Bearer token if API key is provided (alternative to query param).
|
|
44
|
+
*/
|
|
45
|
+
protected buildHeaders(): Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Execute a completion request and return the full response.
|
|
48
|
+
*/
|
|
49
|
+
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
50
|
+
/**
|
|
51
|
+
* Execute a streaming completion request.
|
|
52
|
+
*/
|
|
53
|
+
completeStream(request: CompletionRequest): AsyncIterable<StreamChunk>;
|
|
54
|
+
/**
|
|
55
|
+
* Check if the Gemini API is available and credentials are valid.
|
|
56
|
+
*/
|
|
57
|
+
healthCheck(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Translate AgentRouter request to Gemini format.
|
|
60
|
+
*/
|
|
61
|
+
private translateRequest;
|
|
62
|
+
/**
|
|
63
|
+
* Translate messages from AgentRouter format to Gemini format.
|
|
64
|
+
*
|
|
65
|
+
* Key translations:
|
|
66
|
+
* - role: "user" -> "user", role: "assistant" -> "model"
|
|
67
|
+
* - Content blocks become parts
|
|
68
|
+
* - tool_use blocks become functionCall parts
|
|
69
|
+
* - tool_result blocks become functionResponse parts
|
|
70
|
+
*/
|
|
71
|
+
private translateMessages;
|
|
72
|
+
/**
|
|
73
|
+
* Translate tools from AgentRouter format to Gemini function declarations.
|
|
74
|
+
*/
|
|
75
|
+
private translateTools;
|
|
76
|
+
/**
|
|
77
|
+
* Translate Gemini response to AgentRouter format.
|
|
78
|
+
*/
|
|
79
|
+
private translateResponse;
|
|
80
|
+
/**
|
|
81
|
+
* Translate Gemini streaming chunk to AgentRouter StreamChunk format.
|
|
82
|
+
*/
|
|
83
|
+
private translateStreamChunk;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=gemini.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../src/providers/gemini.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,KAAK,EACV,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,EAIX,cAAc,EACf,MAAM,aAAa,CAAC;AAqHrB;;;;;;;;;GASG;AACH,qBAAa,cAAe,SAAQ,YAAY;IAC9C,SAAgB,IAAI,YAAY;IAEhC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAsD;IAE9F,qDAAqD;IACrD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;gBAEtC,MAAM,EAAE,cAAc;IAIlC;;;;;;;OAOG;IACH,OAAO,CAAC,QAAQ;IAYhB;;;OAGG;cACgB,YAAY,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAmBzD;;OAEG;IACU,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAe9E;;OAEG;IACW,cAAc,CAAC,OAAO,EAAE,iBAAiB,GAAG,aAAa,CAAC,WAAW,CAAC;IAwFpF;;OAEG;IACU,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAqBzC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAgCxB;;;;;;;;OAQG;IACH,OAAO,CAAC,iBAAiB;IA0DzB;;OAEG;IACH,OAAO,CAAC,cAAc;IAQtB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAuEzB;;OAEG;IACH,OAAO,CAAC,oBAAoB;CA4E7B"}
|