@lyrolab/nest-shared 1.8.0 → 1.9.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/CHANGELOG.md +2 -2
- package/README.md +4 -2
- package/dist/ai/ai.constants.d.ts +1 -0
- package/dist/ai/ai.constants.js +5 -0
- package/dist/ai/ai.constants.js.map +1 -0
- package/dist/ai/index.d.ts +2 -0
- package/dist/ai/index.js +2 -0
- package/dist/ai/index.js.map +1 -1
- package/dist/ai/interfaces/ai-module-options.interface.d.ts +15 -0
- package/dist/ai/interfaces/ai-module-options.interface.js +3 -0
- package/dist/ai/interfaces/ai-module-options.interface.js.map +1 -0
- package/dist/ai/services/ai.service.d.ts +3 -1
- package/dist/ai/services/ai.service.js +12 -4
- package/dist/ai/services/ai.service.js.map +1 -1
- package/dist/ai/shared-ai.module.d.ts +6 -0
- package/dist/ai/shared-ai.module.js +58 -6
- package/dist/ai/shared-ai.module.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +5 -9
- package/tsconfig.build.json +4 -0
- package/dist/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# [1.
|
|
1
|
+
# [1.9.0](https://github.com/lyrolab/nest-shared/compare/v1.8.0...v1.9.0) (2026-03-06)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Features
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* implement SharedAiModule with configuration options ([8d400a7](https://github.com/lyrolab/nest-shared/commit/8d400a7a145cc395fe807c7c89fa329ed91c392d))
|
|
7
7
|
|
|
8
8
|
## [1.0.1](https://github.com/lyrolab/nest-shared/compare/v1.0.0...v1.0.1) (2025-04-10)
|
|
9
9
|
|
package/README.md
CHANGED
|
@@ -27,14 +27,16 @@ npm install @lyrolab/nest-shared
|
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
29
|
import { SharedDatabaseModule } from "@lyrolab/nest-shared/database"
|
|
30
|
-
import {
|
|
30
|
+
import { SharedAiModule } from "@lyrolab/nest-shared/ai"
|
|
31
31
|
import { SharedQueueModule } from "@lyrolab/nest-shared/queue"
|
|
32
32
|
import { SharedCacheModule } from "@lyrolab/nest-shared/cache"
|
|
33
33
|
|
|
34
34
|
@Module({
|
|
35
35
|
imports: [
|
|
36
36
|
SharedDatabaseModule.forRoot(),
|
|
37
|
-
|
|
37
|
+
SharedAiModule.forRoot({
|
|
38
|
+
apiKey: "your-openrouter-api-key",
|
|
39
|
+
}),
|
|
38
40
|
SharedQueueModule,
|
|
39
41
|
SharedCacheModule.forRoot(),
|
|
40
42
|
],
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const AI_MODULE_OPTIONS = "AI_MODULE_OPTIONS";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai.constants.js","sourceRoot":"","sources":["../../src/ai/ai.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,iBAAiB,GAAG,mBAAmB,CAAA"}
|
package/dist/ai/index.d.ts
CHANGED
package/dist/ai/index.js
CHANGED
|
@@ -15,5 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./shared-ai.module"), exports);
|
|
18
|
+
__exportStar(require("./ai.constants"), exports);
|
|
19
|
+
__exportStar(require("./interfaces/ai-module-options.interface"), exports);
|
|
18
20
|
__exportStar(require("./services/ai.service"), exports);
|
|
19
21
|
//# sourceMappingURL=index.js.map
|
package/dist/ai/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ai/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,wDAAqC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ai/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,iDAA8B;AAC9B,2EAAwD;AACxD,wDAAqC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ModuleMetadata, Type } from "@nestjs/common";
|
|
2
|
+
export interface AiModuleOptions {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
defaultModel?: string;
|
|
5
|
+
cacheTtlMs?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface AiModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
|
|
8
|
+
useFactory?: (...args: any[]) => Promise<AiModuleOptions> | AiModuleOptions;
|
|
9
|
+
inject?: any[];
|
|
10
|
+
useClass?: Type<AiOptionsFactory>;
|
|
11
|
+
useExisting?: Type<AiOptionsFactory>;
|
|
12
|
+
}
|
|
13
|
+
export interface AiOptionsFactory {
|
|
14
|
+
createAiOptions(): Promise<AiModuleOptions> | AiModuleOptions;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-module-options.interface.js","sourceRoot":"","sources":["../../../src/ai/interfaces/ai-module-options.interface.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { LanguageModelV3 } from "@ai-sdk/provider";
|
|
2
2
|
import { Cache } from "cache-manager";
|
|
3
|
+
import { AiModuleOptions } from "../interfaces/ai-module-options.interface";
|
|
3
4
|
export type BuildModelOptions = {
|
|
4
5
|
model?: string;
|
|
5
6
|
};
|
|
6
7
|
export declare class AiService {
|
|
7
8
|
private cache;
|
|
9
|
+
private options;
|
|
8
10
|
private readonly openrouter;
|
|
9
11
|
private readonly openrouterChat;
|
|
10
|
-
constructor(cache: Cache);
|
|
12
|
+
constructor(cache: Cache, options: AiModuleOptions);
|
|
11
13
|
get model(): LanguageModelV3;
|
|
12
14
|
buildModel({ model }?: BuildModelOptions): LanguageModelV3;
|
|
13
15
|
wrapModel(model: LanguageModelV3): LanguageModelV3;
|
|
@@ -17,15 +17,18 @@ const cache_manager_1 = require("@nestjs/cache-manager");
|
|
|
17
17
|
const common_1 = require("@nestjs/common");
|
|
18
18
|
const ai_sdk_provider_1 = require("@openrouter/ai-sdk-provider");
|
|
19
19
|
const ai_1 = require("ai");
|
|
20
|
+
const ai_constants_1 = require("../ai.constants");
|
|
20
21
|
const DEFAULT_MODEL = "google/gemini-2.0-flash-001";
|
|
21
22
|
let AiService = class AiService {
|
|
22
23
|
cache;
|
|
24
|
+
options;
|
|
23
25
|
openrouter;
|
|
24
26
|
openrouterChat;
|
|
25
|
-
constructor(cache) {
|
|
27
|
+
constructor(cache, options) {
|
|
26
28
|
this.cache = cache;
|
|
29
|
+
this.options = options;
|
|
27
30
|
this.openrouter = (0, ai_sdk_provider_1.createOpenRouter)({
|
|
28
|
-
apiKey:
|
|
31
|
+
apiKey: this.options.apiKey,
|
|
29
32
|
});
|
|
30
33
|
this.openrouterChat = this.buildModel();
|
|
31
34
|
}
|
|
@@ -33,7 +36,7 @@ let AiService = class AiService {
|
|
|
33
36
|
return this.openrouterChat;
|
|
34
37
|
}
|
|
35
38
|
buildModel({ model } = {}) {
|
|
36
|
-
return this.wrapModel(this.openrouter.chat(model ?? DEFAULT_MODEL));
|
|
39
|
+
return this.wrapModel(this.openrouter.chat(model ?? this.options.defaultModel ?? DEFAULT_MODEL));
|
|
37
40
|
}
|
|
38
41
|
wrapModel(model) {
|
|
39
42
|
return (0, ai_1.wrapLanguageModel)({
|
|
@@ -53,6 +56,10 @@ let AiService = class AiService {
|
|
|
53
56
|
return cachedResult;
|
|
54
57
|
}
|
|
55
58
|
const result = await doGenerate();
|
|
59
|
+
if (this.options.cacheTtlMs !== undefined) {
|
|
60
|
+
await this.cache.set(cacheKey, result, this.options.cacheTtlMs);
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
56
63
|
await this.cache.set(cacheKey, result);
|
|
57
64
|
return result;
|
|
58
65
|
}
|
|
@@ -61,6 +68,7 @@ exports.AiService = AiService;
|
|
|
61
68
|
exports.AiService = AiService = __decorate([
|
|
62
69
|
(0, common_1.Injectable)(),
|
|
63
70
|
__param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
|
64
|
-
|
|
71
|
+
__param(1, (0, common_1.Inject)(ai_constants_1.AI_MODULE_OPTIONS)),
|
|
72
|
+
__metadata("design:paramtypes", [Object, Object])
|
|
65
73
|
], AiService);
|
|
66
74
|
//# sourceMappingURL=ai.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.service.js","sourceRoot":"","sources":["../../../src/ai/services/ai.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yDAAqD;AACrD,2CAAmD;AACnD,iEAGoC;AACpC,2BAAsC;
|
|
1
|
+
{"version":3,"file":"ai.service.js","sourceRoot":"","sources":["../../../src/ai/services/ai.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yDAAqD;AACrD,2CAAmD;AACnD,iEAGoC;AACpC,2BAAsC;AAEtC,kDAAmD;AAGnD,MAAM,aAAa,GAAG,6BAA6B,CAAA;AAO5C,IAAM,SAAS,GAAf,MAAM,SAAS;IAKa;IACI;IALpB,UAAU,CAAoB;IAC9B,cAAc,CAAiB;IAEhD,YACiC,KAAY,EACR,OAAwB;QAD5B,UAAK,GAAL,KAAK,CAAO;QACR,YAAO,GAAP,OAAO,CAAiB;QAE3D,IAAI,CAAC,UAAU,GAAG,IAAA,kCAAgB,EAAC;YACjC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;SAC5B,CAAC,CAAA;QACF,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;IACzC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,cAAc,CAAA;IAC5B,CAAC;IAED,UAAU,CAAC,EAAE,KAAK,KAAwB,EAAE;QAC1C,OAAO,IAAI,CAAC,SAAS,CACnB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC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import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { AiModuleAsyncOptions, AiModuleOptions } from "./interfaces/ai-module-options.interface";
|
|
1
3
|
export declare class SharedAiModule {
|
|
4
|
+
static forRoot(options: AiModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: AiModuleAsyncOptions): DynamicModule;
|
|
6
|
+
private static createAsyncProviders;
|
|
7
|
+
private static createAsyncOptionsProvider;
|
|
2
8
|
}
|
|
@@ -5,17 +5,69 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
+
var SharedAiModule_1;
|
|
8
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
10
|
exports.SharedAiModule = void 0;
|
|
10
11
|
const common_1 = require("@nestjs/common");
|
|
12
|
+
const cache_1 = require("../cache");
|
|
13
|
+
const ai_constants_1 = require("./ai.constants");
|
|
11
14
|
const ai_service_1 = require("./services/ai.service");
|
|
12
|
-
let SharedAiModule = class SharedAiModule {
|
|
15
|
+
let SharedAiModule = SharedAiModule_1 = class SharedAiModule {
|
|
16
|
+
static forRoot(options) {
|
|
17
|
+
return {
|
|
18
|
+
module: SharedAiModule_1,
|
|
19
|
+
global: false,
|
|
20
|
+
imports: [cache_1.SharedCacheModule.forRoot()],
|
|
21
|
+
providers: [
|
|
22
|
+
{
|
|
23
|
+
provide: ai_constants_1.AI_MODULE_OPTIONS,
|
|
24
|
+
useValue: options,
|
|
25
|
+
},
|
|
26
|
+
ai_service_1.AiService,
|
|
27
|
+
],
|
|
28
|
+
exports: [ai_service_1.AiService],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
static forRootAsync(options) {
|
|
32
|
+
return {
|
|
33
|
+
module: SharedAiModule_1,
|
|
34
|
+
global: false,
|
|
35
|
+
imports: [cache_1.SharedCacheModule.forRoot(), ...(options.imports || [])],
|
|
36
|
+
providers: [...this.createAsyncProviders(options), ai_service_1.AiService],
|
|
37
|
+
exports: [ai_service_1.AiService],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
static createAsyncProviders(options) {
|
|
41
|
+
if (options.useExisting || options.useFactory) {
|
|
42
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
43
|
+
}
|
|
44
|
+
return [
|
|
45
|
+
this.createAsyncOptionsProvider(options),
|
|
46
|
+
{
|
|
47
|
+
provide: options.useClass,
|
|
48
|
+
useClass: options.useClass,
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
static createAsyncOptionsProvider(options) {
|
|
53
|
+
if (options.useFactory) {
|
|
54
|
+
return {
|
|
55
|
+
provide: ai_constants_1.AI_MODULE_OPTIONS,
|
|
56
|
+
useFactory: options.useFactory,
|
|
57
|
+
inject: options.inject || [],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
provide: ai_constants_1.AI_MODULE_OPTIONS,
|
|
62
|
+
useFactory: async (optionsFactory) => {
|
|
63
|
+
return optionsFactory.createAiOptions();
|
|
64
|
+
},
|
|
65
|
+
inject: [options.useExisting || options.useClass],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
13
68
|
};
|
|
14
69
|
exports.SharedAiModule = SharedAiModule;
|
|
15
|
-
exports.SharedAiModule = SharedAiModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
providers: [ai_service_1.AiService],
|
|
18
|
-
exports: [ai_service_1.AiService],
|
|
19
|
-
})
|
|
70
|
+
exports.SharedAiModule = SharedAiModule = SharedAiModule_1 = __decorate([
|
|
71
|
+
(0, common_1.Module)({})
|
|
20
72
|
], SharedAiModule);
|
|
21
73
|
//# sourceMappingURL=shared-ai.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-ai.module.js","sourceRoot":"","sources":["../../src/ai/shared-ai.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shared-ai.module.js","sourceRoot":"","sources":["../../src/ai/shared-ai.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAgE;AAChE,oCAA4C;AAC5C,iDAAkD;AAMlD,sDAAiD;AAG1C,IAAM,cAAc,sBAApB,MAAM,cAAc;IACzB,MAAM,CAAC,OAAO,CAAC,OAAwB;QACrC,OAAO;YACL,MAAM,EAAE,gBAAc;YACtB,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,CAAC,yBAAiB,CAAC,OAAO,EAAE,CAAC;YACtC,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,gCAAiB;oBAC1B,QAAQ,EAAE,OAAO;iBAClB;gBACD,sBAAS;aACV;YACD,OAAO,EAAE,CAAC,sBAAS,CAAC;SACrB,CAAA;IACH,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAA6B;QAC/C,OAAO;YACL,MAAM,EAAE,gBAAc;YACtB,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,CAAC,yBAAiB,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;YAClE,SAAS,EAAE,CAAC,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,EAAE,sBAAS,CAAC;YAC7D,OAAO,EAAE,CAAC,sBAAS,CAAC;SACrB,CAAA;IACH,CAAC;IAEO,MAAM,CAAC,oBAAoB,CACjC,OAA6B;QAE7B,IAAI,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YAC9C,OAAO,CAAC,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC,CAAC,CAAA;QACnD,CAAC;QAED,OAAO;YACL,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC;YACxC;gBACE,OAAO,EAAE,OAAO,CAAC,QAAS;gBAC1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