genai-lite 0.1.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 +241 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +27 -0
- package/dist/llm/LLMService.d.ts +86 -0
- package/dist/llm/LLMService.js +410 -0
- package/dist/llm/clients/AnthropicClientAdapter.d.ts +84 -0
- package/dist/llm/clients/AnthropicClientAdapter.js +281 -0
- package/dist/llm/clients/GeminiClientAdapter.d.ts +83 -0
- package/dist/llm/clients/GeminiClientAdapter.js +266 -0
- package/dist/llm/clients/MockClientAdapter.d.ts +69 -0
- package/dist/llm/clients/MockClientAdapter.js +284 -0
- package/dist/llm/clients/OpenAIClientAdapter.d.ts +69 -0
- package/dist/llm/clients/OpenAIClientAdapter.js +227 -0
- package/dist/llm/clients/adapterErrorUtils.d.ts +26 -0
- package/dist/llm/clients/adapterErrorUtils.js +107 -0
- package/dist/llm/clients/types.d.ts +65 -0
- package/dist/llm/clients/types.js +19 -0
- package/dist/llm/config.d.ts +90 -0
- package/dist/llm/config.js +508 -0
- package/dist/llm/types.d.ts +155 -0
- package/dist/llm/types.js +14 -0
- package/dist/providers/fromEnvironment.d.ts +8 -0
- package/dist/providers/fromEnvironment.js +14 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +2 -0
- package/package.json +38 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// AI Summary: Centralized error mapping utility for LLM client adapters.
|
|
3
|
+
// Maps common HTTP status codes and network errors to standardized AdapterErrorCode and errorType.
|
|
4
|
+
// Reduces duplication across OpenAI, Anthropic and other provider adapters.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCommonMappedErrorDetails = getCommonMappedErrorDetails;
|
|
7
|
+
const types_1 = require("./types");
|
|
8
|
+
/**
|
|
9
|
+
* Maps common error patterns to standardized error codes and types
|
|
10
|
+
*
|
|
11
|
+
* This utility handles:
|
|
12
|
+
* - Common HTTP status codes (401, 402, 404, 429, 4xx, 5xx)
|
|
13
|
+
* - Network connection errors (ENOTFOUND, ECONNREFUSED, timeouts)
|
|
14
|
+
* - Generic JavaScript errors
|
|
15
|
+
*
|
|
16
|
+
* Individual adapters can further refine the mappings for provider-specific cases,
|
|
17
|
+
* particularly for 400 errors where message content determines the specific error type.
|
|
18
|
+
*
|
|
19
|
+
* @param error - The error object from the provider SDK or network layer
|
|
20
|
+
* @param providerMessageOverride - Optional override for the error message (e.g., from provider SDK)
|
|
21
|
+
* @returns Mapped error details with standardized codes and types
|
|
22
|
+
*/
|
|
23
|
+
function getCommonMappedErrorDetails(error, providerMessageOverride) {
|
|
24
|
+
let errorCode = types_1.ADAPTER_ERROR_CODES.UNKNOWN_ERROR;
|
|
25
|
+
let errorMessage = providerMessageOverride || error?.message || 'Unknown error occurred';
|
|
26
|
+
let errorType = 'server_error';
|
|
27
|
+
let status;
|
|
28
|
+
// Handle API errors with HTTP status codes
|
|
29
|
+
if (error && typeof error.status === 'number') {
|
|
30
|
+
const httpStatus = error.status;
|
|
31
|
+
status = httpStatus;
|
|
32
|
+
errorMessage = providerMessageOverride || error.message || `HTTP ${httpStatus} error`;
|
|
33
|
+
// Map common HTTP status codes
|
|
34
|
+
// TypeScript knows httpStatus is defined here due to the typeof check above
|
|
35
|
+
switch (httpStatus) {
|
|
36
|
+
case 400:
|
|
37
|
+
// Default mapping for 400 errors - adapters should refine based on message content
|
|
38
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.PROVIDER_ERROR;
|
|
39
|
+
errorType = 'invalid_request_error';
|
|
40
|
+
break;
|
|
41
|
+
case 401:
|
|
42
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.INVALID_API_KEY;
|
|
43
|
+
errorType = 'authentication_error';
|
|
44
|
+
break;
|
|
45
|
+
case 402:
|
|
46
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.INSUFFICIENT_CREDITS;
|
|
47
|
+
errorType = 'rate_limit_error';
|
|
48
|
+
break;
|
|
49
|
+
case 404:
|
|
50
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.MODEL_NOT_FOUND;
|
|
51
|
+
errorType = 'invalid_request_error';
|
|
52
|
+
break;
|
|
53
|
+
case 429:
|
|
54
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.RATE_LIMIT_EXCEEDED;
|
|
55
|
+
errorType = 'rate_limit_error';
|
|
56
|
+
break;
|
|
57
|
+
case 500:
|
|
58
|
+
case 502:
|
|
59
|
+
case 503:
|
|
60
|
+
case 504:
|
|
61
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.PROVIDER_ERROR;
|
|
62
|
+
errorType = 'server_error';
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
if (httpStatus >= 400 && httpStatus < 500) {
|
|
66
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.PROVIDER_ERROR;
|
|
67
|
+
errorType = 'invalid_request_error';
|
|
68
|
+
}
|
|
69
|
+
else if (httpStatus >= 500) {
|
|
70
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.PROVIDER_ERROR;
|
|
71
|
+
errorType = 'server_error';
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.PROVIDER_ERROR;
|
|
75
|
+
errorType = 'server_error';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Handle network connection errors
|
|
80
|
+
else if (error && (error.code === 'ENOTFOUND' ||
|
|
81
|
+
error.code === 'ECONNREFUSED' ||
|
|
82
|
+
error.code === 'ETIMEDOUT' ||
|
|
83
|
+
error.name === 'ConnectTimeoutError' ||
|
|
84
|
+
(error.type && error.type.includes('timeout')))) {
|
|
85
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.NETWORK_ERROR;
|
|
86
|
+
errorType = 'connection_error';
|
|
87
|
+
errorMessage = providerMessageOverride || error.message || 'Network connection failed';
|
|
88
|
+
}
|
|
89
|
+
// Handle generic JavaScript errors
|
|
90
|
+
else if (error instanceof Error) {
|
|
91
|
+
errorMessage = providerMessageOverride || error.message || 'Client error occurred';
|
|
92
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.UNKNOWN_ERROR;
|
|
93
|
+
errorType = 'client_error';
|
|
94
|
+
}
|
|
95
|
+
// Handle unknown error types
|
|
96
|
+
else {
|
|
97
|
+
errorMessage = providerMessageOverride || 'Unknown error occurred';
|
|
98
|
+
errorCode = types_1.ADAPTER_ERROR_CODES.UNKNOWN_ERROR;
|
|
99
|
+
errorType = 'server_error';
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
errorCode,
|
|
103
|
+
errorMessage,
|
|
104
|
+
errorType,
|
|
105
|
+
status
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { LLMChatRequest, LLMResponse, LLMFailureResponse, LLMSettings } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Internal request structure used by client adapters with applied defaults
|
|
4
|
+
* This ensures all settings have values and adapters don't need to handle undefined values
|
|
5
|
+
*/
|
|
6
|
+
export interface InternalLLMChatRequest extends Omit<LLMChatRequest, "settings"> {
|
|
7
|
+
settings: Required<LLMSettings>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Interface that all LLM client adapters must implement
|
|
11
|
+
*
|
|
12
|
+
* Client adapters handle the provider-specific logic for:
|
|
13
|
+
* - Formatting requests according to provider API requirements
|
|
14
|
+
* - Making HTTP calls to provider endpoints
|
|
15
|
+
* - Parsing responses into standardized format
|
|
16
|
+
* - Handling provider-specific errors
|
|
17
|
+
* - Managing provider-specific authentication
|
|
18
|
+
*/
|
|
19
|
+
export interface ILLMClientAdapter {
|
|
20
|
+
/**
|
|
21
|
+
* Sends a chat message to the LLM provider
|
|
22
|
+
*
|
|
23
|
+
* @param request - The LLM request with applied default settings
|
|
24
|
+
* @param apiKey - The decrypted API key for the provider
|
|
25
|
+
* @returns Promise resolving to either a successful response or failure response
|
|
26
|
+
*
|
|
27
|
+
* @throws Should not throw - all errors should be caught and returned as LLMFailureResponse
|
|
28
|
+
*/
|
|
29
|
+
sendMessage(request: InternalLLMChatRequest, apiKey: string): Promise<LLMResponse | LLMFailureResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Optional method to validate API key format before making requests
|
|
32
|
+
*
|
|
33
|
+
* @param apiKey - The API key to validate
|
|
34
|
+
* @returns True if the key format appears valid for this provider
|
|
35
|
+
*/
|
|
36
|
+
validateApiKey?(apiKey: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Optional method to get provider-specific information
|
|
39
|
+
*
|
|
40
|
+
* @returns Information about this adapter's capabilities or configuration
|
|
41
|
+
*/
|
|
42
|
+
getAdapterInfo?(): {
|
|
43
|
+
providerId: string;
|
|
44
|
+
name: string;
|
|
45
|
+
version?: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Base error codes that adapters should use for consistency
|
|
50
|
+
*/
|
|
51
|
+
export declare const ADAPTER_ERROR_CODES: {
|
|
52
|
+
readonly INVALID_API_KEY: "INVALID_API_KEY";
|
|
53
|
+
readonly RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED";
|
|
54
|
+
readonly INSUFFICIENT_CREDITS: "INSUFFICIENT_CREDITS";
|
|
55
|
+
readonly MODEL_NOT_FOUND: "MODEL_NOT_FOUND";
|
|
56
|
+
readonly CONTEXT_LENGTH_EXCEEDED: "CONTEXT_LENGTH_EXCEEDED";
|
|
57
|
+
readonly CONTENT_FILTER: "CONTENT_FILTER";
|
|
58
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
59
|
+
readonly PROVIDER_ERROR: "PROVIDER_ERROR";
|
|
60
|
+
readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Helper type for adapter error codes
|
|
64
|
+
*/
|
|
65
|
+
export type AdapterErrorCode = (typeof ADAPTER_ERROR_CODES)[keyof typeof ADAPTER_ERROR_CODES];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// AI Summary: Interface definition for LLM client adapters that handle provider-specific API calls.
|
|
3
|
+
// Defines the contract that all LLM provider clients must implement with enhanced type safety.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.ADAPTER_ERROR_CODES = void 0;
|
|
6
|
+
/**
|
|
7
|
+
* Base error codes that adapters should use for consistency
|
|
8
|
+
*/
|
|
9
|
+
exports.ADAPTER_ERROR_CODES = {
|
|
10
|
+
INVALID_API_KEY: "INVALID_API_KEY",
|
|
11
|
+
RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED",
|
|
12
|
+
INSUFFICIENT_CREDITS: "INSUFFICIENT_CREDITS",
|
|
13
|
+
MODEL_NOT_FOUND: "MODEL_NOT_FOUND",
|
|
14
|
+
CONTEXT_LENGTH_EXCEEDED: "CONTEXT_LENGTH_EXCEEDED",
|
|
15
|
+
CONTENT_FILTER: "CONTENT_FILTER",
|
|
16
|
+
NETWORK_ERROR: "NETWORK_ERROR",
|
|
17
|
+
PROVIDER_ERROR: "PROVIDER_ERROR",
|
|
18
|
+
UNKNOWN_ERROR: "UNKNOWN_ERROR",
|
|
19
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { LLMSettings, ProviderInfo, ModelInfo, ApiProviderId } from "./types";
|
|
2
|
+
import type { ILLMClientAdapter } from "./clients/types";
|
|
3
|
+
/**
|
|
4
|
+
* Mapping from provider IDs to their corresponding adapter constructor classes
|
|
5
|
+
* This enables dynamic registration of client adapters in LLMServiceMain
|
|
6
|
+
*/
|
|
7
|
+
export declare const ADAPTER_CONSTRUCTORS: Partial<Record<ApiProviderId, new (config?: {
|
|
8
|
+
baseURL?: string;
|
|
9
|
+
}) => ILLMClientAdapter>>;
|
|
10
|
+
/**
|
|
11
|
+
* Optional configuration objects for each adapter
|
|
12
|
+
* Allows passing parameters like baseURL during instantiation
|
|
13
|
+
*/
|
|
14
|
+
export declare const ADAPTER_CONFIGS: Partial<Record<ApiProviderId, {
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
}>>;
|
|
17
|
+
/**
|
|
18
|
+
* Default settings applied to all LLM requests unless overridden
|
|
19
|
+
*/
|
|
20
|
+
export declare const DEFAULT_LLM_SETTINGS: Required<LLMSettings>;
|
|
21
|
+
/**
|
|
22
|
+
* Per-provider default setting overrides
|
|
23
|
+
*/
|
|
24
|
+
export declare const PROVIDER_DEFAULT_SETTINGS: Partial<Record<ApiProviderId, Partial<LLMSettings>>>;
|
|
25
|
+
/**
|
|
26
|
+
* Per-model default setting overrides (takes precedence over provider defaults)
|
|
27
|
+
*/
|
|
28
|
+
export declare const MODEL_DEFAULT_SETTINGS: Record<string, Partial<LLMSettings>>;
|
|
29
|
+
/**
|
|
30
|
+
* Supported LLM providers
|
|
31
|
+
*/
|
|
32
|
+
export declare const SUPPORTED_PROVIDERS: ProviderInfo[];
|
|
33
|
+
/**
|
|
34
|
+
* Supported LLM models with their configurations
|
|
35
|
+
* ModelInfo is similar to Cline model info
|
|
36
|
+
* See: https://github.com/cline/cline/blob/main/src/shared/api.ts
|
|
37
|
+
*/
|
|
38
|
+
export declare const SUPPORTED_MODELS: ModelInfo[];
|
|
39
|
+
/**
|
|
40
|
+
* Gets provider information by ID
|
|
41
|
+
*
|
|
42
|
+
* @param providerId - The provider ID to look up
|
|
43
|
+
* @returns The provider info or undefined if not found
|
|
44
|
+
*/
|
|
45
|
+
export declare function getProviderById(providerId: string): ProviderInfo | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Gets model information by ID and provider
|
|
48
|
+
*
|
|
49
|
+
* @param modelId - The model ID to look up
|
|
50
|
+
* @param providerId - The provider ID to filter by
|
|
51
|
+
* @returns The model info or undefined if not found
|
|
52
|
+
*/
|
|
53
|
+
export declare function getModelById(modelId: string, providerId?: string): ModelInfo | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Gets all models for a specific provider
|
|
56
|
+
*
|
|
57
|
+
* @param providerId - The provider ID to filter by
|
|
58
|
+
* @returns Array of model info for the provider
|
|
59
|
+
*/
|
|
60
|
+
export declare function getModelsByProvider(providerId: string): ModelInfo[];
|
|
61
|
+
/**
|
|
62
|
+
* Validates if a provider is supported
|
|
63
|
+
*
|
|
64
|
+
* @param providerId - The provider ID to validate
|
|
65
|
+
* @returns True if the provider is supported
|
|
66
|
+
*/
|
|
67
|
+
export declare function isProviderSupported(providerId: string): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Validates if a model is supported for a given provider
|
|
70
|
+
*
|
|
71
|
+
* @param modelId - The model ID to validate
|
|
72
|
+
* @param providerId - The provider ID to validate against
|
|
73
|
+
* @returns True if the model is supported for the provider
|
|
74
|
+
*/
|
|
75
|
+
export declare function isModelSupported(modelId: string, providerId: string): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Gets merged default settings for a specific model and provider
|
|
78
|
+
*
|
|
79
|
+
* @param modelId - The model ID
|
|
80
|
+
* @param providerId - The provider ID
|
|
81
|
+
* @returns Merged default settings with model-specific overrides applied
|
|
82
|
+
*/
|
|
83
|
+
export declare function getDefaultSettingsForModel(modelId: string, providerId: ApiProviderId): Required<LLMSettings>;
|
|
84
|
+
/**
|
|
85
|
+
* Validates LLM settings values
|
|
86
|
+
*
|
|
87
|
+
* @param settings - The settings to validate
|
|
88
|
+
* @returns Array of validation error messages, empty if valid
|
|
89
|
+
*/
|
|
90
|
+
export declare function validateLLMSettings(settings: Partial<LLMSettings>): string[];
|