Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
Feature/extend AI service by OpenRouter access (#5025)
Jack Ulf559cac31bd64
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0edf0d5902..48ffd00181 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Changed- Extended the selector handling of the scraper configuration for more use cases+- Extended the _AI_ service by an access to _OpenRouter_ (experimental)- Changed `node main` to `exec node main` in the `entrypoint.sh` file to improve the container signal handling- Renamed `Account` to `account` in the `AccountBalance` database schema- Improved the language localization for Catalan (`ca`)diff --git a/apps/api/src/app/endpoints/ai/ai.module.ts b/apps/api/src/app/endpoints/ai/ai.module.tsindex 42480efd78..8a441fde7f 100644--- a/apps/api/src/app/endpoints/ai/ai.module.ts+++ b/apps/api/src/app/endpoints/ai/ai.module.ts@@ -17,6 +17,7 @@ import { ImpersonationModule } from '@ghostfolio/api/services/impersonation/impeimport { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module';import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';+import { PropertyModule } from '@ghostfolio/api/services/property/property.module';import { PortfolioSnapshotQueueModule } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.module';import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module';@@ -39,6 +40,7 @@ import { AiService } from './ai.service';OrderModule,PortfolioSnapshotQueueModule,PrismaModule,+ PropertyModule,RedisCacheModule,SymbolProfileModule,UserModulediff --git a/apps/api/src/app/endpoints/ai/ai.service.ts b/apps/api/src/app/endpoints/ai/ai.service.tsindex df6b7749a8..e56ee062d6 100644--- a/apps/api/src/app/endpoints/ai/ai.service.ts+++ b/apps/api/src/app/endpoints/ai/ai.service.ts@@ -1,12 +1,41 @@import { PortfolioService } from '@ghostfolio/api/app/portfolio/portfolio.service';+import { PropertyService } from '@ghostfolio/api/services/property/property.service';+import {+ PROPERTY_API_KEY_OPENROUTER,+ PROPERTY_OPENROUTER_MODEL+} from '@ghostfolio/common/config';import { Filter } from '@ghostfolio/common/interfaces';import type { AiPromptMode } from '@ghostfolio/common/types';import { Injectable } from '@nestjs/common';+import { createOpenRouter } from '@openrouter/ai-sdk-provider';+import { generateText } from 'ai';@Injectable()export class AiService {- public constructor(private readonly portfolioService: PortfolioService) {}+ public constructor(+ private readonly portfolioService: PortfolioService,+ private readonly propertyService: PropertyService+ ) {}++ public async generateText({ prompt }: { prompt: string }) {+ const openRouterApiKey = (await this.propertyService.getByKey(+ PROPERTY_API_KEY_OPENROUTER+ )) as string;++ const openRouterModel = (await this.propertyService.getByKey(+ PROPERTY_OPENROUTER_MODEL+ )) as string;++ const openRouterService = createOpenRouter({+ apiKey: openRouterApiKey+ });++ return generateText({+ prompt,+ model: openRouterService.chat(openRouterModel)+ });+ }public async getPrompt({filters,diff --git a/libs/common/src/lib/config.ts b/libs/common/src/lib/config.tsindex e124360a13..336235e583 100644--- a/libs/common/src/lib/config.ts+++ b/libs/common/src/lib/config.ts@@ -112,6 +112,7 @@ export const MAX_TOP_HOLDINGS = 50;export const NUMERICAL_PRECISION_THRESHOLD = 100000;export const PROPERTY_API_KEY_GHOSTFOLIO = 'API_KEY_GHOSTFOLIO';+export const PROPERTY_API_KEY_OPENROUTER = 'API_KEY_OPENROUTER';export const PROPERTY_BENCHMARKS = 'BENCHMARKS';export const PROPERTY_BETTER_UPTIME_MONITOR_ID = 'BETTER_UPTIME_MONITOR_ID';export const PROPERTY_COUNTRIES_OF_SUBSCRIBERS = 'COUNTRIES_OF_SUBSCRIBERS';@@ -125,6 +126,7 @@ export const PROPERTY_DEMO_USER_ID = 'DEMO_USER_ID';export const PROPERTY_IS_DATA_GATHERING_ENABLED = 'IS_DATA_GATHERING_ENABLED';export const PROPERTY_IS_READ_ONLY_MODE = 'IS_READ_ONLY_MODE';export const PROPERTY_IS_USER_SIGNUP_ENABLED = 'IS_USER_SIGNUP_ENABLED';+export const PROPERTY_OPENROUTER_MODEL = 'OPENROUTER_MODEL';export const PROPERTY_SLACK_COMMUNITY_USERS = 'SLACK_COMMUNITY_USERS';export const PROPERTY_STRIPE_CONFIG = 'STRIPE_CONFIG';export const PROPERTY_SYSTEM_MESSAGE = 'SYSTEM_MESSAGE';diff --git a/package-lock.json b/package-lock.jsonindex 2e9fa968c2..701bb1686d 100644--- a/package-lock.json+++ b/package-lock.json@@ -42,10 +42,12 @@"@nestjs/platform-express": "11.1.3","@nestjs/schedule": "6.0.0","@nestjs/serve-static": "5.0.3",+ "@openrouter/ai-sdk-provider": "0.7.2","@prisma/client": "6.10.1","@simplewebauthn/browser": "13.1.0","@simplewebauthn/server": "13.1.1","@stripe/stripe-js": "7.3.1",+ "ai": "4.3.16","alphavantage": "2.2.0","big.js": "7.0.1","bootstrap": "4.6.2",@@ -171,6 +173,88 @@"dev": true,"license": "MIT"},+ "node_modules/@ai-sdk/provider": {+ "version": "1.1.3",+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-1.1.3.tgz",+ "integrity": "sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg==",+ "license": "Apache-2.0",+ "dependencies": {+ "json-schema": "^0.4.0"+ },+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@ai-sdk/provider-utils": {+ "version": "2.2.8",+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-2.2.8.tgz",+ "integrity": "sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "1.1.3",+ "nanoid": "^3.3.8",+ "secure-json-parse": "^2.7.0"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "zod": "^3.23.8"+ }+ },+ "node_modules/@ai-sdk/react": {+ "version": "1.2.12",+ "resolved": "https://registry.npmjs.org/@ai-sdk/react/-/react-1.2.12.tgz",+ "integrity": "sha512-jK1IZZ22evPZoQW3vlkZ7wvjYGYF+tRBKXtrcolduIkQ/m/sOAVcVeVDUDvh1T91xCnWCdUGCPZg2avZ90mv3g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider-utils": "2.2.8",+ "@ai-sdk/ui-utils": "1.2.11",+ "swr": "^2.2.5",+ "throttleit": "2.1.0"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "react": "^18 || ^19 || ^19.0.0-rc",+ "zod": "^3.23.8"+ },+ "peerDependenciesMeta": {+ "zod": {+ "optional": true+ }+ }+ },+ "node_modules/@ai-sdk/react/node_modules/throttleit": {+ "version": "2.1.0",+ "resolved": "https://registry.npmjs.org/throttleit/-/throttleit-2.1.0.tgz",+ "integrity": "sha512-nt6AMGKW1p/70DF/hGBdJB57B8Tspmbp5gfJ8ilhLnt7kkr2ye7hzD6NVG8GGErk2HWF34igrL2CXmNIkzKqKw==",+ "license": "MIT",+ "engines": {+ "node": ">=18"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/@ai-sdk/ui-utils": {+ "version": "1.2.11",+ "resolved": "https://registry.npmjs.org/@ai-sdk/ui-utils/-/ui-utils-1.2.11.tgz",+ "integrity": "sha512-3zcwCc8ezzFlwp3ZD15wAPjf2Au4s3vAbKsXQVyhxODHcmu0iyPO2Eua6D/vicq/AUm/BAo60r97O6HU+EI0+w==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "1.1.3",+ "@ai-sdk/provider-utils": "2.2.8",+ "zod-to-json-schema": "^3.24.1"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "zod": "^3.23.8"+ }+ },"node_modules/@ampproject/remapping": {"version": "2.3.0","resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz",@@ -9348,6 +9432,32 @@"yargs-parser": "21.1.1"}},+ "node_modules/@openrouter/ai-sdk-provider": {+ "version": "0.7.2",+ "resolved": "https://registry.npmjs.org/@openrouter/ai-sdk-provider/-/ai-sdk-provider-0.7.2.tgz",+ "integrity": "sha512-Fry2mV7uGGJRmP9JntTZRc8ElESIk7AJNTacLbF6Syoeb5k8d7HPGkcK9rTXDlqBb8HgU1hOKtz23HojesTmnw==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "1.1.3",+ "@ai-sdk/provider-utils": "2.2.8"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "ai": "^4.3.16",+ "zod": "^3.25.34"+ }+ },+ "node_modules/@opentelemetry/api": {+ "version": "1.9.0",+ "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",+ "integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",+ "license": "Apache-2.0",+ "engines": {+ "node": ">=8.0.0"+ }+ },"node_modules/@parcel/watcher": {"version": "2.5.1","resolved": "https://registry.npmjs.org/@parcel/watcher/-/watcher-2.5.1.tgz",@@ -12196,6 +12306,12 @@"@types/d3-selection": "*"}},+ "node_modules/@types/diff-match-patch": {+ "version": "1.0.36",+ "resolved": "https://registry.npmjs.org/@types/diff-match-patch/-/diff-match-patch-1.0.36.tgz",+ "integrity": "sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==",+ "license": "MIT"+ },"node_modules/@types/eslint": {"version": "9.6.1","resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-9.6.1.tgz",@@ -13686,6 +13802,32 @@"node": ">= 14"}},+ "node_modules/ai": {+ "version": "4.3.16",+ "resolved": "https://registry.npmjs.org/ai/-/ai-4.3.16.tgz",+ "integrity": "sha512-KUDwlThJ5tr2Vw0A1ZkbDKNME3wzWhuVfAOwIvFUzl1TPVDFAXDFTXio3p+jaKneB+dKNCvFFlolYmmgHttG1g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "1.1.3",+ "@ai-sdk/provider-utils": "2.2.8",+ "@ai-sdk/react": "1.2.12",+ "@ai-sdk/ui-utils": "1.2.11",+ "@opentelemetry/api": "1.9.0",+ "jsondiffpatch": "0.6.0"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "react": "^18 || ^19 || ^19.0.0-rc",+ "zod": "^3.23.8"+ },+ "peerDependenciesMeta": {+ "react": {+ "optional": true+ }+ }+ },"node_modules/ajv": {"version": "8.17.1","resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz",@@ -17874,7 +18016,6 @@"version": "2.0.3","resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz","integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==",- "dev": true,"license": "MIT","engines": {"node": ">=6"@@ -17943,6 +18084,12 @@"node": ">=0.3.1"}},+ "node_modules/diff-match-patch": {+ "version": "1.0.5",+ "resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.5.tgz",+ "integrity": "sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==",+ "license": "Apache-2.0"+ },"node_modules/diff-sequences": {"version": "29.6.3","resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.6.3.tgz",@@ -24216,7 +24363,6 @@"version": "0.4.0","resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz","integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==",- "dev": true,"license": "(AFL-2.1 OR BSD-3-Clause)"},"node_modules/json-schema-traverse": {@@ -24303,6 +24449,35 @@"integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==","license": "MIT"},+ "node_modules/jsondiffpatch": {+ "version": "0.6.0",+ "resolved": "https://registry.npmjs.org/jsondiffpatch/-/jsondiffpatch-0.6.0.tgz",+ "integrity": "sha512-3QItJOXp2AP1uv7waBkao5nCvhEv+QmJAd38Ybq7wNI74Q+BBmnLn4EDKz6yI9xGAIQoUF87qHt+kc1IVxB4zQ==",+ "license": "MIT",+ "dependencies": {+ "@types/diff-match-patch": "^1.0.36",+ "chalk": "^5.3.0",+ "diff-match-patch": "^1.0.5"+ },+ "bin": {+ "jsondiffpatch": "bin/jsondiffpatch.js"+ },+ "engines": {+ "node": "^18.0.0 || >=20.0.0"+ }+ },+ "node_modules/jsondiffpatch/node_modules/chalk": {+ "version": "5.4.1",+ "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.4.1.tgz",+ "integrity": "sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==",+ "license": "MIT",+ "engines": {+ "node": "^12.17.0 || ^14.13 || >=16.0.0"+ },+ "funding": {+ "url": "https://github.com/chalk/chalk?sponsor=1"+ }+ },"node_modules/jsonfile": {"version": "6.1.0","resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",@@ -25837,7 +26012,6 @@"version": "1.4.0","resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz","integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",- "dev": true,"license": "MIT","dependencies": {"js-tokens": "^3.0.0 || ^4.0.0"@@ -29592,7 +29766,6 @@"version": "18.2.0","resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz","integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==",- "dev": true,"license": "MIT","dependencies": {"loose-envify": "^1.1.0"@@ -31113,6 +31286,12 @@"dev": true,"license": "MIT"},+ "node_modules/secure-json-parse": {+ "version": "2.7.0",+ "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.7.0.tgz",+ "integrity": "sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==",+ "license": "BSD-3-Clause"+ },"node_modules/select": {"version": "1.1.2","resolved": "https://registry.npmjs.org/select/-/select-1.1.2.tgz",@@ -32571,6 +32750,19 @@"node": ">= 10"}},+ "node_modules/swr": {+ "version": "2.3.3",+ "resolved": "https://registry.npmjs.org/swr/-/swr-2.3.3.tgz",+ "integrity": "sha512-dshNvs3ExOqtZ6kJBaAsabhPdHyeY4P2cKwRCniDVifBMoG/SVI7tfLWqPXriVspf2Rg4tPzXJTnwaihIeFw2A==",+ "license": "MIT",+ "dependencies": {+ "dequal": "^2.0.3",+ "use-sync-external-store": "^1.4.0"+ },+ "peerDependencies": {+ "react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"+ }+ },"node_modules/symbol-observable": {"version": "4.0.0","resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-4.0.0.tgz",@@ -34142,6 +34334,15 @@"dev": true,"license": "MIT"},+ "node_modules/use-sync-external-store": {+ "version": "1.5.0",+ "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.5.0.tgz",+ "integrity": "sha512-Rb46I4cGGVBmjamjphe8L/UnvJD+uPPtTkNvX5mZgqdbavhI4EbgIWJiIHXJ8bc/i9EQGPRh4DwEURJ552Do0A==",+ "license": "MIT",+ "peerDependencies": {+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"+ }+ },"node_modules/util": {"version": "0.12.5","resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz",@@ -35791,6 +35992,25 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/zod": {+ "version": "3.25.67",+ "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.67.tgz",+ "integrity": "sha512-idA2YXwpCdqUSKRCACDE6ItZD9TZzy3OZMtpfLoh6oPR47lipysRrJfjzMqFxQ3uJuUPyUeWe1r9vLH33xO/Qw==",+ "license": "MIT",+ "peer": true,+ "funding": {+ "url": "https://github.com/sponsors/colinhacks"+ }+ },+ "node_modules/zod-to-json-schema": {+ "version": "3.24.6",+ "resolved": "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.24.6.tgz",+ "integrity": "sha512-h/z3PKvcTcTetyjl1fkj79MHNEjm+HpD6NXheWjzOekY7kV+lwDYnHw+ivHkijnCSMz1yJaWBD9vu/Fcmk+vEg==",+ "license": "ISC",+ "peerDependencies": {+ "zod": "^3.24.1"+ }+ },"node_modules/zone.js": {"version": "0.15.1","resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.15.1.tgz",diff --git a/package.json b/package.jsonindex f0b23efb5b..28f6569043 100644--- a/package.json+++ b/package.json@@ -88,10 +88,12 @@"@nestjs/platform-express": "11.1.3","@nestjs/schedule": "6.0.0","@nestjs/serve-static": "5.0.3",+ "@openrouter/ai-sdk-provider": "0.7.2","@prisma/client": "6.10.1","@simplewebauthn/browser": "13.1.0","@simplewebauthn/server": "13.1.1","@stripe/stripe-js": "7.3.1",+ "ai": "4.3.16","alphavantage": "2.2.0","big.js": "7.0.1","bootstrap": "4.6.2",