genai-lite 0.4.3 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +377 -16
- package/dist/adapters/image/ElectronDiffusionAdapter.d.ts +10 -0
- package/dist/adapters/image/ElectronDiffusionAdapter.js +11 -0
- package/dist/adapters/image/GenaiElectronImageAdapter.d.ts +69 -0
- package/dist/adapters/image/GenaiElectronImageAdapter.js +273 -0
- package/dist/adapters/image/MockImageAdapter.d.ts +23 -0
- package/dist/adapters/image/MockImageAdapter.js +55 -0
- package/dist/adapters/image/OpenAIImageAdapter.d.ts +62 -0
- package/dist/adapters/image/OpenAIImageAdapter.js +303 -0
- package/dist/config/image-presets.json +212 -0
- package/dist/config/llm-presets.json +326 -0
- package/dist/image/ImageService.d.ts +53 -0
- package/dist/image/ImageService.js +199 -0
- package/dist/image/config.d.ts +48 -0
- package/dist/image/config.js +221 -0
- package/dist/image/services/ImageAdapterRegistry.d.ts +61 -0
- package/dist/image/services/ImageAdapterRegistry.js +95 -0
- package/dist/image/services/ImageModelResolver.d.ts +26 -0
- package/dist/image/services/ImageModelResolver.js +98 -0
- package/dist/image/services/ImagePresetManager.d.ts +27 -0
- package/dist/image/services/ImagePresetManager.js +52 -0
- package/dist/image/services/ImageRequestValidator.d.ts +37 -0
- package/dist/image/services/ImageRequestValidator.js +133 -0
- package/dist/image/services/ImageSettingsResolver.d.ts +25 -0
- package/dist/image/services/ImageSettingsResolver.js +76 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -1
- package/dist/llm/LLMService.d.ts +3 -4
- package/dist/llm/LLMService.js +14 -4
- package/dist/llm/clients/AnthropicClientAdapter.js +2 -2
- package/dist/llm/clients/GeminiClientAdapter.js +2 -2
- package/dist/llm/clients/LlamaCppClientAdapter.d.ts +1 -1
- package/dist/llm/clients/LlamaCppClientAdapter.js +3 -3
- package/dist/llm/clients/OpenAIClientAdapter.js +2 -2
- package/dist/llm/config.js +4 -28
- package/dist/llm/services/ModelResolver.d.ts +6 -4
- package/dist/providers/fromEnvironment.d.ts +5 -1
- package/dist/providers/fromEnvironment.js +29 -4
- package/dist/shared/adapters/errorUtils.d.ts +26 -0
- package/dist/shared/adapters/errorUtils.js +107 -0
- package/dist/shared/services/AdapterRegistry.d.ts +89 -0
- package/dist/shared/services/AdapterRegistry.js +144 -0
- package/dist/shared/services/PresetManager.d.ts +33 -0
- package/dist/shared/services/PresetManager.js +56 -0
- package/dist/types/image.d.ts +399 -0
- package/dist/types/image.js +8 -0
- package/dist/types.d.ts +6 -0
- package/package.json +3 -2
- package/src/config/image-presets.json +212 -0
- /package/src/config/{presets.json → llm-presets.json} +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageRequestValidator = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Validates image generation requests
|
|
6
|
+
*/
|
|
7
|
+
class ImageRequestValidator {
|
|
8
|
+
/**
|
|
9
|
+
* Validates the basic structure of an image generation request
|
|
10
|
+
*
|
|
11
|
+
* @param request - The request to validate
|
|
12
|
+
* @returns null if valid, ImageFailureResponse if invalid
|
|
13
|
+
*/
|
|
14
|
+
validateRequestStructure(request) {
|
|
15
|
+
// Validate provider ID
|
|
16
|
+
if (!request.providerId || typeof request.providerId !== 'string') {
|
|
17
|
+
return this.createValidationError(request, 'Missing or invalid providerId', 'MISSING_PROVIDER_ID', 'providerId');
|
|
18
|
+
}
|
|
19
|
+
// Validate model ID
|
|
20
|
+
if (!request.modelId || typeof request.modelId !== 'string') {
|
|
21
|
+
return this.createValidationError(request, 'Missing or invalid modelId', 'MISSING_MODEL_ID', 'modelId');
|
|
22
|
+
}
|
|
23
|
+
// Validate prompt exists and is a string
|
|
24
|
+
if (request.prompt === undefined || request.prompt === null || typeof request.prompt !== 'string') {
|
|
25
|
+
return this.createValidationError(request, 'Missing or invalid prompt', 'MISSING_PROMPT', 'prompt');
|
|
26
|
+
}
|
|
27
|
+
// Validate prompt is not empty/whitespace
|
|
28
|
+
if (request.prompt.trim().length === 0) {
|
|
29
|
+
return this.createValidationError(request, 'Prompt cannot be empty or whitespace-only', 'EMPTY_PROMPT', 'prompt');
|
|
30
|
+
}
|
|
31
|
+
// Validate count if provided
|
|
32
|
+
if (request.count !== undefined) {
|
|
33
|
+
const countValidation = this.validateCount(request);
|
|
34
|
+
if (countValidation) {
|
|
35
|
+
return countValidation;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Validate settings if provided
|
|
39
|
+
if (request.settings) {
|
|
40
|
+
const settingsValidation = this.validateSettings(request);
|
|
41
|
+
if (settingsValidation) {
|
|
42
|
+
return settingsValidation;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null; // Valid
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validates the count parameter
|
|
49
|
+
*
|
|
50
|
+
* @param request - The request containing the count
|
|
51
|
+
* @returns null if valid, ImageFailureResponse if invalid
|
|
52
|
+
*/
|
|
53
|
+
validateCount(request) {
|
|
54
|
+
const count = request.count;
|
|
55
|
+
if (count === undefined) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
if (!Number.isInteger(count)) {
|
|
59
|
+
return this.createValidationError(request, 'count must be an integer', 'INVALID_COUNT', 'count');
|
|
60
|
+
}
|
|
61
|
+
if (count <= 0) {
|
|
62
|
+
return this.createValidationError(request, 'count must be a positive integer (> 0)', 'INVALID_COUNT', 'count');
|
|
63
|
+
}
|
|
64
|
+
if (count > 10) {
|
|
65
|
+
return this.createValidationError(request, 'count cannot exceed 10 images per request', 'INVALID_COUNT', 'count');
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Validates settings
|
|
71
|
+
*
|
|
72
|
+
* @param request - The request containing the settings
|
|
73
|
+
* @returns null if valid, ImageFailureResponse if invalid
|
|
74
|
+
*/
|
|
75
|
+
validateSettings(request) {
|
|
76
|
+
const settings = request.settings;
|
|
77
|
+
if (!settings) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
// Validate width
|
|
81
|
+
if (settings.width !== undefined) {
|
|
82
|
+
if (!Number.isInteger(settings.width) || settings.width < 64 || settings.width > 2048) {
|
|
83
|
+
return this.createValidationError(request, 'Image width must be an integer between 64 and 2048 pixels', 'INVALID_WIDTH', 'settings.width');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Validate height
|
|
87
|
+
if (settings.height !== undefined) {
|
|
88
|
+
if (!Number.isInteger(settings.height) || settings.height < 64 || settings.height > 2048) {
|
|
89
|
+
return this.createValidationError(request, 'Image height must be an integer between 64 and 2048 pixels', 'INVALID_HEIGHT', 'settings.height');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Validate diffusion settings if present
|
|
93
|
+
if (settings.diffusion) {
|
|
94
|
+
const diffusion = settings.diffusion;
|
|
95
|
+
// Validate steps
|
|
96
|
+
if (diffusion.steps !== undefined) {
|
|
97
|
+
if (diffusion.steps < 1 || diffusion.steps > 150) {
|
|
98
|
+
return this.createValidationError(request, 'Diffusion steps must be between 1 and 150', 'INVALID_DIFFUSION_STEPS', 'settings.diffusion.steps');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Validate cfgScale
|
|
102
|
+
if (diffusion.cfgScale !== undefined) {
|
|
103
|
+
if (diffusion.cfgScale < 0.1 || diffusion.cfgScale > 30) {
|
|
104
|
+
return this.createValidationError(request, 'Diffusion cfgScale must be between 0.1 and 30', 'INVALID_DIFFUSION_CFG_SCALE', 'settings.diffusion.cfgScale');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates a validation error response
|
|
112
|
+
*
|
|
113
|
+
* @param request - The original request
|
|
114
|
+
* @param message - Error message
|
|
115
|
+
* @param code - Error code
|
|
116
|
+
* @param param - Parameter that caused the error
|
|
117
|
+
* @returns ImageFailureResponse
|
|
118
|
+
*/
|
|
119
|
+
createValidationError(request, message, code, param) {
|
|
120
|
+
return {
|
|
121
|
+
object: 'error',
|
|
122
|
+
providerId: request.providerId || 'unknown',
|
|
123
|
+
modelId: request.modelId,
|
|
124
|
+
error: {
|
|
125
|
+
message,
|
|
126
|
+
code,
|
|
127
|
+
type: 'validation_error',
|
|
128
|
+
param,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.ImageRequestValidator = ImageRequestValidator;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ImageGenerationSettings, ImageModelInfo, ResolvedImageGenerationSettings } from '../../types/image';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves and merges image generation settings from multiple sources
|
|
4
|
+
*/
|
|
5
|
+
export declare class ImageSettingsResolver {
|
|
6
|
+
/**
|
|
7
|
+
* Resolves settings by merging defaults, preset settings, and request settings
|
|
8
|
+
* Priority: Model defaults < Preset settings < Request settings
|
|
9
|
+
*
|
|
10
|
+
* @param modelInfo - Model information containing defaults
|
|
11
|
+
* @param presetSettings - Settings from preset (if any)
|
|
12
|
+
* @param requestSettings - Settings from request (if any)
|
|
13
|
+
* @returns Resolved settings with all required fields
|
|
14
|
+
*/
|
|
15
|
+
resolveSettings(modelInfo: ImageModelInfo, presetSettings?: ImageGenerationSettings, requestSettings?: ImageGenerationSettings): ResolvedImageGenerationSettings;
|
|
16
|
+
/**
|
|
17
|
+
* Merges diffusion settings from multiple sources
|
|
18
|
+
*
|
|
19
|
+
* @param modelDefaults - Model default diffusion settings
|
|
20
|
+
* @param presetSettings - Preset diffusion settings
|
|
21
|
+
* @param requestSettings - Request diffusion settings
|
|
22
|
+
* @returns Merged diffusion settings
|
|
23
|
+
*/
|
|
24
|
+
private mergeDiffusionSettings;
|
|
25
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageSettingsResolver = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves and merges image generation settings from multiple sources
|
|
6
|
+
*/
|
|
7
|
+
class ImageSettingsResolver {
|
|
8
|
+
/**
|
|
9
|
+
* Resolves settings by merging defaults, preset settings, and request settings
|
|
10
|
+
* Priority: Model defaults < Preset settings < Request settings
|
|
11
|
+
*
|
|
12
|
+
* @param modelInfo - Model information containing defaults
|
|
13
|
+
* @param presetSettings - Settings from preset (if any)
|
|
14
|
+
* @param requestSettings - Settings from request (if any)
|
|
15
|
+
* @returns Resolved settings with all required fields
|
|
16
|
+
*/
|
|
17
|
+
resolveSettings(modelInfo, presetSettings, requestSettings) {
|
|
18
|
+
// Start with library defaults
|
|
19
|
+
const defaults = {
|
|
20
|
+
width: 1024,
|
|
21
|
+
height: 1024,
|
|
22
|
+
responseFormat: 'buffer',
|
|
23
|
+
quality: 'standard',
|
|
24
|
+
style: 'natural',
|
|
25
|
+
};
|
|
26
|
+
// Merge in model defaults
|
|
27
|
+
const modelDefaults = modelInfo.defaultSettings || {};
|
|
28
|
+
// Merge all settings (defaults < model < preset < request)
|
|
29
|
+
const merged = {
|
|
30
|
+
...defaults,
|
|
31
|
+
...modelDefaults,
|
|
32
|
+
...(presetSettings || {}),
|
|
33
|
+
...(requestSettings || {}),
|
|
34
|
+
};
|
|
35
|
+
// Handle diffusion settings separately (deep merge)
|
|
36
|
+
const diffusionSettings = this.mergeDiffusionSettings(modelDefaults.diffusion, presetSettings?.diffusion, requestSettings?.diffusion);
|
|
37
|
+
// Build final resolved settings
|
|
38
|
+
const resolved = {
|
|
39
|
+
width: merged.width || 1024,
|
|
40
|
+
height: merged.height || 1024,
|
|
41
|
+
responseFormat: merged.responseFormat || 'buffer',
|
|
42
|
+
quality: merged.quality || 'standard',
|
|
43
|
+
style: merged.style || 'natural',
|
|
44
|
+
user: merged.user,
|
|
45
|
+
n: merged.n,
|
|
46
|
+
};
|
|
47
|
+
if (diffusionSettings) {
|
|
48
|
+
// Ensure required diffusion fields have defaults
|
|
49
|
+
resolved.diffusion = {
|
|
50
|
+
...diffusionSettings,
|
|
51
|
+
steps: diffusionSettings.steps || 20,
|
|
52
|
+
cfgScale: diffusionSettings.cfgScale || 7.5,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return resolved;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Merges diffusion settings from multiple sources
|
|
59
|
+
*
|
|
60
|
+
* @param modelDefaults - Model default diffusion settings
|
|
61
|
+
* @param presetSettings - Preset diffusion settings
|
|
62
|
+
* @param requestSettings - Request diffusion settings
|
|
63
|
+
* @returns Merged diffusion settings
|
|
64
|
+
*/
|
|
65
|
+
mergeDiffusionSettings(modelDefaults, presetSettings, requestSettings) {
|
|
66
|
+
if (!modelDefaults && !presetSettings && !requestSettings) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
...(modelDefaults || {}),
|
|
71
|
+
...(presetSettings || {}),
|
|
72
|
+
...(requestSettings || {}),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.ImageSettingsResolver = ImageSettingsResolver;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type { ApiKeyProvider } from "./types";
|
|
1
|
+
export type { ApiKeyProvider, PresetMode } from "./types";
|
|
2
2
|
export { LLMService } from "./llm/LLMService";
|
|
3
|
-
export type { LLMServiceOptions,
|
|
3
|
+
export type { LLMServiceOptions, CreateMessagesResult } from "./llm/LLMService";
|
|
4
4
|
export type { ModelPreset } from "./types/presets";
|
|
5
5
|
export * from "./llm/types";
|
|
6
6
|
export * from "./llm/clients/types";
|
|
@@ -9,6 +9,8 @@ export { LlamaCppClientAdapter } from "./llm/clients/LlamaCppClientAdapter";
|
|
|
9
9
|
export { LlamaCppServerClient } from "./llm/clients/LlamaCppServerClient";
|
|
10
10
|
export type { LlamaCppClientConfig, } from "./llm/clients/LlamaCppClientAdapter";
|
|
11
11
|
export type { LlamaCppHealthResponse, LlamaCppTokenizeResponse, LlamaCppDetokenizeResponse, LlamaCppEmbeddingResponse, LlamaCppInfillResponse, LlamaCppPropsResponse, LlamaCppMetricsResponse, LlamaCppSlot, LlamaCppSlotsResponse, LlamaCppModel, LlamaCppModelsResponse, } from "./llm/clients/LlamaCppServerClient";
|
|
12
|
+
export { ImageService } from "./image/ImageService";
|
|
13
|
+
export type { ImageProviderId, ImageMimeType, ImageResponseFormat, ImageQuality, ImageStyle, DiffusionSampler, ImageProgressStage, ImageProgressCallback, DiffusionSettings, OpenAISpecificSettings, ImageGenerationSettings, ResolvedImageGenerationSettings, ImageUsage, GeneratedImage, ImageGenerationRequestBase, ImageGenerationRequest, ImageGenerationRequestWithPreset, ImageGenerationResponse, ImageFailureResponse, ImageProviderCapabilities, ImageModelInfo, ImageProviderInfo, ImagePreset, ImageProviderAdapterConfig, ImageProviderAdapter, ImageServiceOptions, CreatePromptResult, } from "./types/image";
|
|
12
14
|
export { renderTemplate } from "./prompting/template";
|
|
13
15
|
export { countTokens, getSmartPreview, extractRandomVariables } from "./prompting/content";
|
|
14
16
|
export { parseStructuredContent, parseRoleTags, extractInitialTaggedContent, parseTemplateWithMetadata } from "./prompting/parser";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.KNOWN_GGUF_MODELS = exports.detectGgufCapabilities = exports.createFallbackModelInfo = exports.parseTemplateWithMetadata = exports.extractInitialTaggedContent = exports.parseRoleTags = exports.parseStructuredContent = exports.extractRandomVariables = exports.getSmartPreview = exports.countTokens = exports.renderTemplate = exports.LlamaCppServerClient = exports.LlamaCppClientAdapter = exports.fromEnvironment = exports.LLMService = void 0;
|
|
17
|
+
exports.KNOWN_GGUF_MODELS = exports.detectGgufCapabilities = exports.createFallbackModelInfo = exports.parseTemplateWithMetadata = exports.extractInitialTaggedContent = exports.parseRoleTags = exports.parseStructuredContent = exports.extractRandomVariables = exports.getSmartPreview = exports.countTokens = exports.renderTemplate = exports.ImageService = exports.LlamaCppServerClient = exports.LlamaCppClientAdapter = exports.fromEnvironment = exports.LLMService = void 0;
|
|
18
18
|
// --- LLM Service ---
|
|
19
19
|
var LLMService_1 = require("./llm/LLMService");
|
|
20
20
|
Object.defineProperty(exports, "LLMService", { enumerable: true, get: function () { return LLMService_1.LLMService; } });
|
|
@@ -30,6 +30,10 @@ var LlamaCppClientAdapter_1 = require("./llm/clients/LlamaCppClientAdapter");
|
|
|
30
30
|
Object.defineProperty(exports, "LlamaCppClientAdapter", { enumerable: true, get: function () { return LlamaCppClientAdapter_1.LlamaCppClientAdapter; } });
|
|
31
31
|
var LlamaCppServerClient_1 = require("./llm/clients/LlamaCppServerClient");
|
|
32
32
|
Object.defineProperty(exports, "LlamaCppServerClient", { enumerable: true, get: function () { return LlamaCppServerClient_1.LlamaCppServerClient; } });
|
|
33
|
+
// --- Image Generation ---
|
|
34
|
+
// Export Image Service
|
|
35
|
+
var ImageService_1 = require("./image/ImageService");
|
|
36
|
+
Object.defineProperty(exports, "ImageService", { enumerable: true, get: function () { return ImageService_1.ImageService; } });
|
|
33
37
|
// --- Utilities ---
|
|
34
38
|
var template_1 = require("./prompting/template");
|
|
35
39
|
Object.defineProperty(exports, "renderTemplate", { enumerable: true, get: function () { return template_1.renderTemplate; } });
|
package/dist/llm/LLMService.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { ApiKeyProvider } from '../types';
|
|
1
|
+
import type { ApiKeyProvider, PresetMode } from '../types';
|
|
2
2
|
import type { LLMChatRequest, LLMChatRequestWithPreset, LLMResponse, LLMFailureResponse, ProviderInfo, ModelInfo, ApiProviderId, LLMSettings, ModelContext, LLMMessage } from "./types";
|
|
3
3
|
import type { ModelPreset } from "../types/presets";
|
|
4
|
-
import { type PresetMode } from "./services/PresetManager";
|
|
5
4
|
export type { PresetMode };
|
|
6
5
|
/**
|
|
7
6
|
* Options for configuring the LLMService
|
|
@@ -129,11 +128,11 @@ export declare class LLMService {
|
|
|
129
128
|
*
|
|
130
129
|
* @returns Map of provider IDs to adapter info
|
|
131
130
|
*/
|
|
132
|
-
getRegisteredAdapters(): Map<string, import("
|
|
131
|
+
getRegisteredAdapters(): Map<string, import("../shared/services/AdapterRegistry").AdapterInfo<string>>;
|
|
133
132
|
/**
|
|
134
133
|
* Gets a summary of available providers and their adapter status
|
|
135
134
|
*
|
|
136
135
|
* @returns Summary of provider availability
|
|
137
136
|
*/
|
|
138
|
-
getProviderSummary(): import("
|
|
137
|
+
getProviderSummary(): import("../shared/services/AdapterRegistry").ProviderSummary;
|
|
139
138
|
}
|
package/dist/llm/LLMService.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// AI Summary: Main process service for LLM operations, integrating with ApiKeyProvider for secure key access.
|
|
3
3
|
// Orchestrates LLM requests through provider-specific client adapters with proper error handling.
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
4
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
8
|
exports.LLMService = void 0;
|
|
6
9
|
const config_1 = require("./config");
|
|
7
10
|
const template_1 = require("../prompting/template");
|
|
8
11
|
const parser_1 = require("../prompting/parser");
|
|
12
|
+
const llm_presets_json_1 = __importDefault(require("../config/llm-presets.json"));
|
|
13
|
+
const MockClientAdapter_1 = require("./clients/MockClientAdapter");
|
|
9
14
|
// Import the extracted services
|
|
10
|
-
const PresetManager_1 = require("
|
|
11
|
-
const AdapterRegistry_1 = require("
|
|
15
|
+
const PresetManager_1 = require("../shared/services/PresetManager");
|
|
16
|
+
const AdapterRegistry_1 = require("../shared/services/AdapterRegistry");
|
|
12
17
|
const RequestValidator_1 = require("./services/RequestValidator");
|
|
13
18
|
const SettingsManager_1 = require("./services/SettingsManager");
|
|
14
19
|
const ModelResolver_1 = require("./services/ModelResolver");
|
|
@@ -27,8 +32,13 @@ class LLMService {
|
|
|
27
32
|
constructor(getApiKey, options = {}) {
|
|
28
33
|
this.getApiKey = getApiKey;
|
|
29
34
|
// Initialize services
|
|
30
|
-
this.presetManager = new PresetManager_1.PresetManager(options.presets, options.presetMode);
|
|
31
|
-
this.adapterRegistry = new AdapterRegistry_1.AdapterRegistry(
|
|
35
|
+
this.presetManager = new PresetManager_1.PresetManager(llm_presets_json_1.default, options.presets, options.presetMode);
|
|
36
|
+
this.adapterRegistry = new AdapterRegistry_1.AdapterRegistry({
|
|
37
|
+
supportedProviders: config_1.SUPPORTED_PROVIDERS,
|
|
38
|
+
fallbackAdapter: new MockClientAdapter_1.MockClientAdapter(),
|
|
39
|
+
adapterConstructors: config_1.ADAPTER_CONSTRUCTORS,
|
|
40
|
+
adapterConfigs: config_1.ADAPTER_CONFIGS,
|
|
41
|
+
});
|
|
32
42
|
this.requestValidator = new RequestValidator_1.RequestValidator();
|
|
33
43
|
this.settingsManager = new SettingsManager_1.SettingsManager();
|
|
34
44
|
this.modelResolver = new ModelResolver_1.ModelResolver(this.presetManager, this.adapterRegistry);
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.AnthropicClientAdapter = void 0;
|
|
9
9
|
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
10
10
|
const types_1 = require("./types");
|
|
11
|
-
const
|
|
11
|
+
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
12
12
|
/**
|
|
13
13
|
* Client adapter for Anthropic API integration
|
|
14
14
|
*
|
|
@@ -304,7 +304,7 @@ class AnthropicClientAdapter {
|
|
|
304
304
|
createErrorResponse(error, request) {
|
|
305
305
|
// Use shared error mapping utility for common error patterns
|
|
306
306
|
const initialProviderMessage = error instanceof sdk_1.default.APIError ? error.message : undefined;
|
|
307
|
-
let { errorCode, errorMessage, errorType, status } = (0,
|
|
307
|
+
let { errorCode, errorMessage, errorType, status } = (0, errorUtils_1.getCommonMappedErrorDetails)(error, initialProviderMessage);
|
|
308
308
|
// Apply Anthropic-specific refinements for 400 errors based on message content
|
|
309
309
|
if (error instanceof sdk_1.default.APIError && status === 400) {
|
|
310
310
|
if (error.message.toLowerCase().includes("context length") ||
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
exports.GeminiClientAdapter = void 0;
|
|
6
6
|
const genai_1 = require("@google/genai");
|
|
7
7
|
const types_1 = require("./types");
|
|
8
|
-
const
|
|
8
|
+
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
9
9
|
/**
|
|
10
10
|
* Client adapter for Google Gemini API integration
|
|
11
11
|
*
|
|
@@ -276,7 +276,7 @@ class GeminiClientAdapter {
|
|
|
276
276
|
createErrorResponse(error, request) {
|
|
277
277
|
// Use shared error mapping utility for common error patterns
|
|
278
278
|
const initialProviderMessage = error?.message;
|
|
279
|
-
let { errorCode, errorMessage, errorType, status } = (0,
|
|
279
|
+
let { errorCode, errorMessage, errorType, status } = (0, errorUtils_1.getCommonMappedErrorDetails)(error, initialProviderMessage);
|
|
280
280
|
// Apply Gemini-specific refinements for certain error types
|
|
281
281
|
if (error && error.message) {
|
|
282
282
|
const message = error.message.toLowerCase();
|
|
@@ -40,7 +40,7 @@ export interface LlamaCppClientConfig {
|
|
|
40
40
|
* // Use via LLMService
|
|
41
41
|
* const response = await service.sendMessage({
|
|
42
42
|
* providerId: 'llamacpp',
|
|
43
|
-
* modelId: '
|
|
43
|
+
* modelId: 'llamacpp',
|
|
44
44
|
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
45
45
|
* });
|
|
46
46
|
* ```
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.LlamaCppClientAdapter = void 0;
|
|
9
9
|
const openai_1 = __importDefault(require("openai"));
|
|
10
10
|
const types_1 = require("./types");
|
|
11
|
-
const
|
|
11
|
+
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
12
12
|
const LlamaCppServerClient_1 = require("./LlamaCppServerClient");
|
|
13
13
|
const config_1 = require("../config");
|
|
14
14
|
/**
|
|
@@ -41,7 +41,7 @@ const config_1 = require("../config");
|
|
|
41
41
|
* // Use via LLMService
|
|
42
42
|
* const response = await service.sendMessage({
|
|
43
43
|
* providerId: 'llamacpp',
|
|
44
|
-
* modelId: '
|
|
44
|
+
* modelId: 'llamacpp',
|
|
45
45
|
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
46
46
|
* });
|
|
47
47
|
* ```
|
|
@@ -351,7 +351,7 @@ class LlamaCppClientAdapter {
|
|
|
351
351
|
};
|
|
352
352
|
}
|
|
353
353
|
// Use common error mapping for other errors
|
|
354
|
-
const mappedError = (0,
|
|
354
|
+
const mappedError = (0, errorUtils_1.getCommonMappedErrorDetails)(error);
|
|
355
355
|
return {
|
|
356
356
|
provider: request.providerId,
|
|
357
357
|
model: request.modelId,
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.OpenAIClientAdapter = void 0;
|
|
9
9
|
const openai_1 = __importDefault(require("openai"));
|
|
10
10
|
const types_1 = require("./types");
|
|
11
|
-
const
|
|
11
|
+
const errorUtils_1 = require("../../shared/adapters/errorUtils");
|
|
12
12
|
/**
|
|
13
13
|
* Client adapter for OpenAI API integration
|
|
14
14
|
*
|
|
@@ -214,7 +214,7 @@ class OpenAIClientAdapter {
|
|
|
214
214
|
createErrorResponse(error, request) {
|
|
215
215
|
// Use shared error mapping utility for common error patterns
|
|
216
216
|
const initialProviderMessage = error instanceof openai_1.default.APIError ? error.message : undefined;
|
|
217
|
-
let { errorCode, errorMessage, errorType, status } = (0,
|
|
217
|
+
let { errorCode, errorMessage, errorType, status } = (0, errorUtils_1.getCommonMappedErrorDetails)(error, initialProviderMessage);
|
|
218
218
|
// Apply OpenAI-specific refinements for 400 errors based on message content
|
|
219
219
|
if (error instanceof openai_1.default.APIError && status === 400) {
|
|
220
220
|
if (error.message.toLowerCase().includes("context length")) {
|
package/dist/llm/config.js
CHANGED
|
@@ -573,39 +573,15 @@ exports.SUPPORTED_MODELS = [
|
|
|
573
573
|
supportsImages: false,
|
|
574
574
|
supportsPromptCache: false,
|
|
575
575
|
},
|
|
576
|
-
// llama.cpp
|
|
576
|
+
// llama.cpp Model (generic - actual model determined by llama.cpp server)
|
|
577
577
|
{
|
|
578
|
-
id: "
|
|
579
|
-
name: "
|
|
580
|
-
providerId: "llamacpp",
|
|
581
|
-
contextWindow: 8192,
|
|
582
|
-
inputPrice: 0.0,
|
|
583
|
-
outputPrice: 0.0,
|
|
584
|
-
description: "Local Llama 3 8B model via llama.cpp server",
|
|
585
|
-
maxTokens: 4096,
|
|
586
|
-
supportsImages: false,
|
|
587
|
-
supportsPromptCache: false,
|
|
588
|
-
},
|
|
589
|
-
{
|
|
590
|
-
id: "llama-3-70b-instruct",
|
|
591
|
-
name: "Llama 3 70B Instruct",
|
|
578
|
+
id: "llamacpp",
|
|
579
|
+
name: "llama.cpp Local Model",
|
|
592
580
|
providerId: "llamacpp",
|
|
593
581
|
contextWindow: 8192,
|
|
594
582
|
inputPrice: 0.0,
|
|
595
583
|
outputPrice: 0.0,
|
|
596
|
-
description: "Local
|
|
597
|
-
maxTokens: 4096,
|
|
598
|
-
supportsImages: false,
|
|
599
|
-
supportsPromptCache: false,
|
|
600
|
-
},
|
|
601
|
-
{
|
|
602
|
-
id: "mistral-7b-instruct",
|
|
603
|
-
name: "Mistral 7B Instruct",
|
|
604
|
-
providerId: "llamacpp",
|
|
605
|
-
contextWindow: 32768,
|
|
606
|
-
inputPrice: 0.0,
|
|
607
|
-
outputPrice: 0.0,
|
|
608
|
-
description: "Local Mistral 7B model via llama.cpp server",
|
|
584
|
+
description: "Local model running via llama.cpp server (model determined by server)",
|
|
609
585
|
maxTokens: 4096,
|
|
610
586
|
supportsImages: false,
|
|
611
587
|
supportsPromptCache: false,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { LLMFailureResponse, LLMSettings, ModelInfo } from "../types";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { LLMFailureResponse, LLMSettings, ModelInfo, ApiProviderId } from "../types";
|
|
2
|
+
import type { ILLMClientAdapter } from "../clients/types";
|
|
3
|
+
import type { ModelPreset } from "../../types/presets";
|
|
4
|
+
import { PresetManager } from "../../shared/services/PresetManager";
|
|
5
|
+
import { AdapterRegistry } from "../../shared/services/AdapterRegistry";
|
|
4
6
|
/**
|
|
5
7
|
* Options for model selection
|
|
6
8
|
*/
|
|
@@ -26,7 +28,7 @@ export interface ModelResolution {
|
|
|
26
28
|
export declare class ModelResolver {
|
|
27
29
|
private presetManager;
|
|
28
30
|
private adapterRegistry;
|
|
29
|
-
constructor(presetManager: PresetManager
|
|
31
|
+
constructor(presetManager: PresetManager<ModelPreset>, adapterRegistry: AdapterRegistry<ILLMClientAdapter, ApiProviderId>);
|
|
30
32
|
/**
|
|
31
33
|
* Resolves model information from either a preset ID or provider/model IDs
|
|
32
34
|
*
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import type { ApiKeyProvider } from "../types";
|
|
2
2
|
/**
|
|
3
3
|
* Creates an ApiKeyProvider that sources keys from system environment variables.
|
|
4
|
-
* It looks for variables in the format: PROVIDERID_API_KEY (e.g.,
|
|
4
|
+
* It looks for variables in the format: PROVIDERID_API_KEY (e.g., OPENAI_IMAGES_API_KEY).
|
|
5
5
|
* This is a secure and standard practice for server-side applications.
|
|
6
6
|
* Note: Provider IDs are converted to uppercase.
|
|
7
7
|
*
|
|
8
8
|
* Special handling:
|
|
9
9
|
* - llamacpp: Returns 'not-needed' (local server, no authentication)
|
|
10
10
|
* - mock: Returns 'not-needed' (test provider, no authentication)
|
|
11
|
+
* - genai-electron-images: Returns 'not-needed' (local server, no authentication)
|
|
12
|
+
*
|
|
13
|
+
* Provider Aliases:
|
|
14
|
+
* - openai-images: Also accepts OPENAI_API_KEY (standard OpenAI env var)
|
|
11
15
|
*/
|
|
12
16
|
export declare const fromEnvironment: ApiKeyProvider;
|
|
@@ -1,22 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fromEnvironment = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Provider-specific environment variable aliases.
|
|
6
|
+
* Allows providers to accept alternative env var names for convenience.
|
|
7
|
+
*
|
|
8
|
+
* Example: 'openai-images' accepts both OPENAI_IMAGES_API_KEY and OPENAI_API_KEY
|
|
9
|
+
*/
|
|
10
|
+
const PROVIDER_ENV_VAR_ALIASES = {
|
|
11
|
+
'openai-images': ['OPENAI'], // Accepts standard OPENAI_API_KEY
|
|
12
|
+
};
|
|
4
13
|
/**
|
|
5
14
|
* Creates an ApiKeyProvider that sources keys from system environment variables.
|
|
6
|
-
* It looks for variables in the format: PROVIDERID_API_KEY (e.g.,
|
|
15
|
+
* It looks for variables in the format: PROVIDERID_API_KEY (e.g., OPENAI_IMAGES_API_KEY).
|
|
7
16
|
* This is a secure and standard practice for server-side applications.
|
|
8
17
|
* Note: Provider IDs are converted to uppercase.
|
|
9
18
|
*
|
|
10
19
|
* Special handling:
|
|
11
20
|
* - llamacpp: Returns 'not-needed' (local server, no authentication)
|
|
12
21
|
* - mock: Returns 'not-needed' (test provider, no authentication)
|
|
22
|
+
* - genai-electron-images: Returns 'not-needed' (local server, no authentication)
|
|
23
|
+
*
|
|
24
|
+
* Provider Aliases:
|
|
25
|
+
* - openai-images: Also accepts OPENAI_API_KEY (standard OpenAI env var)
|
|
13
26
|
*/
|
|
14
27
|
const fromEnvironment = async (providerId) => {
|
|
15
28
|
// Providers that don't require API keys (local/test providers)
|
|
16
|
-
if (providerId === 'llamacpp' || providerId === 'mock') {
|
|
29
|
+
if (providerId === 'llamacpp' || providerId === 'mock' || providerId === 'genai-electron-images') {
|
|
17
30
|
return 'not-needed';
|
|
18
31
|
}
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
// Try standard env var first: PROVIDERID_API_KEY
|
|
33
|
+
const standardEnvVar = `${providerId.toUpperCase().replace(/-/g, '_')}_API_KEY`;
|
|
34
|
+
if (process.env[standardEnvVar]) {
|
|
35
|
+
return process.env[standardEnvVar];
|
|
36
|
+
}
|
|
37
|
+
// Try aliases
|
|
38
|
+
const aliases = PROVIDER_ENV_VAR_ALIASES[providerId] || [];
|
|
39
|
+
for (const alias of aliases) {
|
|
40
|
+
const aliasEnvVar = `${alias.toUpperCase()}_API_KEY`;
|
|
41
|
+
if (process.env[aliasEnvVar]) {
|
|
42
|
+
return process.env[aliasEnvVar];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
21
46
|
};
|
|
22
47
|
exports.fromEnvironment = fromEnvironment;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type AdapterErrorCode } from '../../llm/clients/types';
|
|
2
|
+
/**
|
|
3
|
+
* Mapped error details returned by the utility function
|
|
4
|
+
*/
|
|
5
|
+
export interface MappedErrorDetails {
|
|
6
|
+
errorCode: AdapterErrorCode;
|
|
7
|
+
errorMessage: string;
|
|
8
|
+
errorType: string;
|
|
9
|
+
status?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Maps common error patterns to standardized error codes and types
|
|
13
|
+
*
|
|
14
|
+
* This utility handles:
|
|
15
|
+
* - Common HTTP status codes (401, 402, 404, 429, 4xx, 5xx)
|
|
16
|
+
* - Network connection errors (ENOTFOUND, ECONNREFUSED, timeouts)
|
|
17
|
+
* - Generic JavaScript errors
|
|
18
|
+
*
|
|
19
|
+
* Individual adapters can further refine the mappings for provider-specific cases,
|
|
20
|
+
* particularly for 400 errors where message content determines the specific error type.
|
|
21
|
+
*
|
|
22
|
+
* @param error - The error object from the provider SDK or network layer
|
|
23
|
+
* @param providerMessageOverride - Optional override for the error message (e.g., from provider SDK)
|
|
24
|
+
* @returns Mapped error details with standardized codes and types
|
|
25
|
+
*/
|
|
26
|
+
export declare function getCommonMappedErrorDetails(error: any, providerMessageOverride?: string): MappedErrorDetails;
|