@casual-simulation/aux-records
Advanced tools
Comparing version 3.2.2-alpha.5694783443 to 3.2.2
import { InvalidSubscriptionTierError, NotLoggedInError, NotSubscribedError, NotSupportedError, ServerError } from './Errors'; | ||
import { AIChatInterface, AIChatMessage } from './AIChatInterface'; | ||
import { AIGenerateSkyboxInterface, AIGenerateSkyboxInterfaceBlockadeLabsOptions } from './AIGenerateSkyboxInterface'; | ||
import { AIGeneratedImage, AIImageInterface } from './AIImageInterface'; | ||
export interface AIConfiguration { | ||
chat: AIChatConfiguration | null; | ||
generateSkybox: AIGenerateSkyboxConfiguration | null; | ||
images: AIGenerateImageConfiguration | null; | ||
} | ||
@@ -42,2 +44,54 @@ export interface AIChatConfiguration { | ||
} | ||
export interface AIGenerateImageConfiguration { | ||
interfaces: { | ||
[provider: string]: AIImageInterface; | ||
}; | ||
options: AIGenerateImageOptions; | ||
} | ||
export interface AIGenerateImageOptions { | ||
/** | ||
* The model that should be used when none is specified in a request. | ||
*/ | ||
defaultModel: string; | ||
/** | ||
* The width that should be used for images that don't specify a width. | ||
*/ | ||
defaultWidth: number; | ||
/** | ||
* The height that should be used for images that don't specify a height. | ||
*/ | ||
defaultHeight: number; | ||
/** | ||
* The maximum width that can be requested. | ||
*/ | ||
maxWidth: number; | ||
/** | ||
* The maximum height that can be requested. | ||
*/ | ||
maxHeight: number; | ||
/** | ||
* The maximum number of diffusion steps that can be requested. | ||
*/ | ||
maxSteps: number; | ||
/** | ||
* The maximum number of images that can be requested. | ||
*/ | ||
maxImages: number; | ||
/** | ||
* The list of models grouped by their respective providers. | ||
*/ | ||
allowedModels: { | ||
[provider: string]: string[]; | ||
}; | ||
/** | ||
* The list of subscription tiers that are allowed to be used for generate image. | ||
* | ||
* - `true` indicates that all users are allowed, regardless of their subscription tier or if they are even subscribed. | ||
* - An array of strings indicates that only users with the given subscription tiers are allowed. | ||
*/ | ||
allowedSubscriptionTiers: true | string[]; | ||
} | ||
export interface AIImageProviders { | ||
[provider: string]: AIImageInterface; | ||
} | ||
/** | ||
@@ -53,2 +107,6 @@ * Defines a class that is able to handle AI requests. | ||
private _allowedGenerateSkyboxSubscriptionTiers; | ||
private _imageProviders; | ||
private _allowedImageModels; | ||
private _allowedImageSubscriptionTiers; | ||
private _imageOptions; | ||
constructor(configuration: AIConfiguration); | ||
@@ -58,2 +116,3 @@ chat(request: AIChatRequest): Promise<AIChatResponse>; | ||
getSkybox(request: AIGetSkyboxRequest): Promise<AIGetSkyboxResponse>; | ||
generateImage(request: AIGenerateImageRequest): Promise<AIGenerateImageResponse>; | ||
private _matchesSubscriptionTiers; | ||
@@ -185,2 +244,77 @@ } | ||
} | ||
export interface AIGenerateImageRequest { | ||
/** | ||
* The ID of the user that is making the request. | ||
*/ | ||
userId: string; | ||
/** | ||
* The subscription tier of the user. | ||
* Should be null if the user is not logged in or if they do not have a subscription. | ||
*/ | ||
userSubscriptionTier: string; | ||
/** | ||
* The description of what the generated image(s) should look like. | ||
*/ | ||
prompt: string; | ||
/** | ||
* The description of what the generated image(s) should not look like. | ||
*/ | ||
negativePrompt?: string; | ||
/** | ||
* The model that should be used to generate the image(s). | ||
*/ | ||
model?: string; | ||
/** | ||
* The desired width of the image(s) in pixels. | ||
*/ | ||
width?: number; | ||
/** | ||
* The desired height of the image(s) in pixels. | ||
*/ | ||
height?: number; | ||
/** | ||
* The number of images that should be generated. | ||
*/ | ||
numberOfImages?: number; | ||
/** | ||
* The random noise seed that should be used. | ||
*/ | ||
seed?: number; | ||
/** | ||
* The number of diffusion steps to run. | ||
*/ | ||
steps?: number; | ||
/** | ||
* How strictly the diffusion process adheres to the prompt text. | ||
* Higher values keep the image closer to the prompt. | ||
*/ | ||
cfgScale?: number; | ||
/** | ||
* The sampler to use for the diffusion process. | ||
*/ | ||
sampler?: string; | ||
/** | ||
* The clip guidance preset. | ||
*/ | ||
clipGuidancePreset?: string; | ||
/** | ||
* The style preset that should be used to guide the image model torwards a specific style. | ||
*/ | ||
stylePreset?: string; | ||
} | ||
export type AIGenerateImageResponse = AIGenerateImageSuccess | AIGenerateImageFailure; | ||
export interface AIGenerateImageSuccess { | ||
success: true; | ||
/** | ||
* The list of images that were generated. | ||
*/ | ||
images: AIGeneratedImage[]; | ||
} | ||
export interface AIGenerateImageFailure { | ||
success: false; | ||
errorCode: ServerError | NotLoggedInError | NotSubscribedError | InvalidSubscriptionTierError | NotSupportedError | 'invalid_model'; | ||
errorMessage: string; | ||
allowedSubscriptionTiers?: string[]; | ||
currentSubscriptionTier?: string; | ||
} | ||
//# sourceMappingURL=AIController.d.ts.map |
@@ -34,2 +34,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
if (configuration.images) { | ||
this._imageProviders = configuration.images.interfaces; | ||
const options = configuration.images.options; | ||
this._imageOptions = options; | ||
this._allowedImageSubscriptionTiers = | ||
typeof options.allowedSubscriptionTiers === 'boolean' | ||
? options.allowedSubscriptionTiers | ||
: new Set(options.allowedSubscriptionTiers); | ||
this._allowedImageModels = new Map(); | ||
for (let provider in options.allowedModels) { | ||
for (let model of options.allowedModels[provider]) { | ||
this._allowedImageModels.set(model, provider); | ||
} | ||
} | ||
} | ||
} | ||
@@ -244,2 +259,92 @@ chat(request) { | ||
} | ||
generateImage(request) { | ||
var _a, _b, _c, _d, _e; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!this._imageProviders) { | ||
return { | ||
success: false, | ||
errorCode: 'not_supported', | ||
errorMessage: 'This operation is not supported.', | ||
}; | ||
} | ||
if (!request.userId) { | ||
return { | ||
success: false, | ||
errorCode: 'not_logged_in', | ||
errorMessage: 'The user must be logged in. Please provide a sessionKey or a recordKey.', | ||
}; | ||
} | ||
if (!this._matchesSubscriptionTiers(request.userSubscriptionTier, this._allowedImageSubscriptionTiers)) { | ||
if (!request.userSubscriptionTier) { | ||
return { | ||
success: false, | ||
errorCode: 'not_subscribed', | ||
errorMessage: 'The user must be subscribed in order to use this operation.', | ||
allowedSubscriptionTiers: [ | ||
...this | ||
._allowedImageSubscriptionTiers, | ||
], | ||
}; | ||
} | ||
else { | ||
return { | ||
success: false, | ||
errorCode: 'invalid_subscription_tier', | ||
errorMessage: 'This operation is not available to the user at their current subscription tier.', | ||
allowedSubscriptionTiers: [ | ||
...this | ||
._allowedImageSubscriptionTiers, | ||
], | ||
currentSubscriptionTier: request.userSubscriptionTier, | ||
}; | ||
} | ||
} | ||
const model = (_a = request.model) !== null && _a !== void 0 ? _a : this._imageOptions.defaultModel; | ||
if (!this._allowedImageModels.has(model)) { | ||
return { | ||
success: false, | ||
errorCode: 'invalid_model', | ||
errorMessage: `The given model is not allowed for images.`, | ||
}; | ||
} | ||
const providerId = this._allowedImageModels.get(model); | ||
const provider = this._imageProviders[providerId]; | ||
if (!provider) { | ||
return { | ||
success: false, | ||
errorCode: 'invalid_model', | ||
errorMessage: `The given model is not allowed for images.`, | ||
}; | ||
} | ||
const result = yield provider.generateImage({ | ||
model, | ||
prompt: request.prompt, | ||
negativePrompt: request.negativePrompt, | ||
width: Math.min((_b = request.width) !== null && _b !== void 0 ? _b : this._imageOptions.defaultWidth, this._imageOptions.maxWidth), | ||
height: Math.min((_c = request.height) !== null && _c !== void 0 ? _c : this._imageOptions.defaultHeight, this._imageOptions.maxHeight), | ||
numberOfImages: Math.min((_d = request.numberOfImages) !== null && _d !== void 0 ? _d : 1, this._imageOptions.maxImages), | ||
seed: request.seed, | ||
steps: Math.min((_e = request.steps) !== null && _e !== void 0 ? _e : 30, this._imageOptions.maxSteps), | ||
cfgScale: request.cfgScale, | ||
sampler: request.sampler, | ||
clipGuidancePreset: request.clipGuidancePreset, | ||
stylePreset: request.stylePreset, | ||
userId: request.userId, | ||
}); | ||
return { | ||
success: true, | ||
images: result.images, | ||
}; | ||
} | ||
catch (err) { | ||
console.error('[AIController] Error handling generate image request:', err); | ||
return { | ||
success: false, | ||
errorCode: 'server_error', | ||
errorMessage: 'A server error occurred.', | ||
}; | ||
} | ||
}); | ||
} | ||
_matchesSubscriptionTiers(tier, allowedTiers) { | ||
@@ -246,0 +351,0 @@ return allowedTiers === true || allowedTiers.has(tier); |
@@ -29,2 +29,5 @@ export * from './AuthController'; | ||
export * from './BlockadeLabsGenerateSkyboxInterface'; | ||
export * from './AIImageInterface'; | ||
export * from './OpenAIImageInterface'; | ||
export * from './StabilityAIImageInterface'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -29,2 +29,5 @@ export * from './AuthController'; | ||
export * from './BlockadeLabsGenerateSkyboxInterface'; | ||
export * from './AIImageInterface'; | ||
export * from './OpenAIImageInterface'; | ||
export * from './StabilityAIImageInterface'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@casual-simulation/aux-records", | ||
"version": "3.2.2-alpha.5694783443", | ||
"version": "3.2.2", | ||
"description": "Helpers and managers used by the CasualOS records system.", | ||
@@ -47,3 +47,3 @@ "keywords": [], | ||
}, | ||
"gitHead": "38f9d2559a32ed9a9ffb228d36a0af558df8b40a" | ||
"gitHead": "2ad53363d56543293b84a535d04e888604ecf9ab" | ||
} |
@@ -122,2 +122,3 @@ import { AuthController } from './AuthController'; | ||
private _aiGetSkybox; | ||
private _aiGenerateImage; | ||
private _listData; | ||
@@ -124,0 +125,0 @@ private _handleRecordFileOptions; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1015300
132
17765
0