briyah 1.0.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/.prettierrc +7 -0
- package/dist/ai/LLM/anthropic.module.d.ts +2 -0
- package/dist/ai/LLM/anthropic.module.js +21 -0
- package/dist/ai/LLM/anthropic.service.d.ts +27 -0
- package/dist/ai/LLM/anthropic.service.js +329 -0
- package/dist/ai/LLM/base-ai.service.d.ts +45 -0
- package/dist/ai/LLM/base-ai.service.js +409 -0
- package/dist/ai/LLM/deepseek.module.d.ts +2 -0
- package/dist/ai/LLM/deepseek.module.js +21 -0
- package/dist/ai/LLM/deepseek.service.d.ts +15 -0
- package/dist/ai/LLM/deepseek.service.js +279 -0
- package/dist/ai/LLM/fal.module.d.ts +2 -0
- package/dist/ai/LLM/fal.module.js +21 -0
- package/dist/ai/LLM/fal.service.d.ts +17 -0
- package/dist/ai/LLM/fal.service.js +314 -0
- package/dist/ai/LLM/googleai.module.d.ts +2 -0
- package/dist/ai/LLM/googleai.module.js +21 -0
- package/dist/ai/LLM/googleai.service.d.ts +20 -0
- package/dist/ai/LLM/googleai.service.js +271 -0
- package/dist/ai/LLM/grok.module.d.ts +2 -0
- package/dist/ai/LLM/grok.module.js +21 -0
- package/dist/ai/LLM/grok.service.d.ts +14 -0
- package/dist/ai/LLM/grok.service.js +263 -0
- package/dist/ai/LLM/mock.module.d.ts +2 -0
- package/dist/ai/LLM/mock.module.js +21 -0
- package/dist/ai/LLM/mock.service.d.ts +13 -0
- package/dist/ai/LLM/mock.service.js +194 -0
- package/dist/ai/LLM/openai.module.d.ts +2 -0
- package/dist/ai/LLM/openai.module.js +21 -0
- package/dist/ai/LLM/openai.service.d.ts +21 -0
- package/dist/ai/LLM/openai.service.js +421 -0
- package/dist/ai/LLM/together.module.d.ts +2 -0
- package/dist/ai/LLM/together.module.js +21 -0
- package/dist/ai/LLM/together.service.d.ts +18 -0
- package/dist/ai/LLM/together.service.js +285 -0
- package/dist/ai/LLM/vertexai.module.d.ts +2 -0
- package/dist/ai/LLM/vertexai.module.js +21 -0
- package/dist/ai/LLM/vertexai.service.d.ts +17 -0
- package/dist/ai/LLM/vertexai.service.js +323 -0
- package/dist/ai/agent-config.d.ts +32 -0
- package/dist/ai/agent-config.js +2 -0
- package/dist/ai/agent-factory.d.ts +15 -0
- package/dist/ai/agent-factory.js +24 -0
- package/dist/ai/agent-message.service.d.ts +9 -0
- package/dist/ai/agent-message.service.js +56 -0
- package/dist/ai/agent-store.module.d.ts +2 -0
- package/dist/ai/agent-store.module.js +21 -0
- package/dist/ai/agent-store.service.d.ts +31 -0
- package/dist/ai/agent-store.service.js +352 -0
- package/dist/ai/agent.d.ts +75 -0
- package/dist/ai/agent.js +174 -0
- package/dist/ai/ai-factory.module.d.ts +2 -0
- package/dist/ai/ai-factory.module.js +40 -0
- package/dist/ai/ai-factory.service.d.ts +24 -0
- package/dist/ai/ai-factory.service.js +94 -0
- package/dist/ai/artifact.module.d.ts +2 -0
- package/dist/ai/artifact.module.js +22 -0
- package/dist/ai/artifact.service.d.ts +21 -0
- package/dist/ai/artifact.service.js +239 -0
- package/dist/ai/attached-file.module.d.ts +2 -0
- package/dist/ai/attached-file.module.js +22 -0
- package/dist/ai/attached-file.service.d.ts +35 -0
- package/dist/ai/attached-file.service.js +253 -0
- package/dist/ai/model_prices.d.ts +2 -0
- package/dist/ai/model_prices.js +54 -0
- package/dist/ai/published-agents.service.d.ts +13 -0
- package/dist/ai/published-agents.service.js +118 -0
- package/dist/app/balance-message.service.d.ts +8 -0
- package/dist/app/balance-message.service.js +45 -0
- package/dist/app/balance.module.d.ts +2 -0
- package/dist/app/balance.module.js +20 -0
- package/dist/app/balance.service.d.ts +18 -0
- package/dist/app/balance.service.js +158 -0
- package/dist/app/stripe.controller.d.ts +31 -0
- package/dist/app/stripe.controller.js +150 -0
- package/dist/app/stripe.module.d.ts +2 -0
- package/dist/app/stripe.module.js +22 -0
- package/dist/app/stripe.service.d.ts +28 -0
- package/dist/app/stripe.service.js +198 -0
- package/dist/app/transaction.service.d.ts +15 -0
- package/dist/app/transaction.service.js +139 -0
- package/dist/app/user-service-factory.d.ts +21 -0
- package/dist/app/user-service-factory.js +81 -0
- package/dist/app/user-service-manager.d.ts +28 -0
- package/dist/app/user-service-manager.js +102 -0
- package/dist/app.controller.d.ts +232 -0
- package/dist/app.controller.js +2536 -0
- package/dist/app.module.d.ts +2 -0
- package/dist/app.module.js +45 -0
- package/dist/app.service.d.ts +186 -0
- package/dist/app.service.js +1334 -0
- package/dist/auth/agent-access.decorator.d.ts +2 -0
- package/dist/auth/agent-access.decorator.js +10 -0
- package/dist/auth/auth.controller.d.ts +38 -0
- package/dist/auth/auth.controller.js +356 -0
- package/dist/auth/auth.module.d.ts +2 -0
- package/dist/auth/auth.module.js +55 -0
- package/dist/auth/auth.service.d.ts +14 -0
- package/dist/auth/auth.service.js +84 -0
- package/dist/auth/dto/bot-login.dto.d.ts +4 -0
- package/dist/auth/dto/bot-login.dto.js +2 -0
- package/dist/auth/dto/password-reset.dto.d.ts +11 -0
- package/dist/auth/dto/password-reset.dto.js +2 -0
- package/dist/auth/dto/phone-login.dto.d.ts +8 -0
- package/dist/auth/dto/phone-login.dto.js +2 -0
- package/dist/auth/dto/phone-verification.dto.d.ts +9 -0
- package/dist/auth/dto/phone-verification.dto.js +2 -0
- package/dist/auth/jwt-auth.guard.d.ts +13 -0
- package/dist/auth/jwt-auth.guard.js +143 -0
- package/dist/auth/jwt.strategy.d.ts +17 -0
- package/dist/auth/jwt.strategy.js +45 -0
- package/dist/auth/phone-validation.service.d.ts +7 -0
- package/dist/auth/phone-validation.service.js +50 -0
- package/dist/auth/public.decorator.d.ts +2 -0
- package/dist/auth/public.decorator.js +7 -0
- package/dist/auth/rate-limit.service.d.ts +16 -0
- package/dist/auth/rate-limit.service.js +84 -0
- package/dist/auth/room-access.decorator.d.ts +2 -0
- package/dist/auth/room-access.decorator.js +10 -0
- package/dist/auth/scopes.decorator.d.ts +2 -0
- package/dist/auth/scopes.decorator.js +7 -0
- package/dist/auth/scopes.guard.d.ts +9 -0
- package/dist/auth/scopes.guard.js +82 -0
- package/dist/auth/session.guard.d.ts +7 -0
- package/dist/auth/session.guard.js +55 -0
- package/dist/auth/twilio.service.d.ts +15 -0
- package/dist/auth/twilio.service.js +139 -0
- package/dist/auth/users.service.d.ts +24 -0
- package/dist/auth/users.service.js +149 -0
- package/dist/common/errors.d.ts +12 -0
- package/dist/common/errors.js +43 -0
- package/dist/config/configuration.module.d.ts +2 -0
- package/dist/config/configuration.module.js +20 -0
- package/dist/config/configuration.service.d.ts +26 -0
- package/dist/config/configuration.service.js +131 -0
- package/dist/main.js +15749 -0
- package/dist/room/artifact-store.service.d.ts +15 -0
- package/dist/room/artifact-store.service.js +226 -0
- package/dist/room/artifact.d.ts +6 -0
- package/dist/room/artifact.js +2 -0
- package/dist/room/message.d.ts +19 -0
- package/dist/room/message.js +70 -0
- package/dist/room/published-rooms.service.d.ts +12 -0
- package/dist/room/published-rooms.service.js +108 -0
- package/dist/room/room-config.d.ts +12 -0
- package/dist/room/room-config.js +2 -0
- package/dist/room/room-factory.d.ts +7 -0
- package/dist/room/room-factory.js +16 -0
- package/dist/room/room-message.module.d.ts +2 -0
- package/dist/room/room-message.module.js +21 -0
- package/dist/room/room-message.service.d.ts +8 -0
- package/dist/room/room-message.service.js +44 -0
- package/dist/room/room-store.module.d.ts +2 -0
- package/dist/room/room-store.module.js +25 -0
- package/dist/room/room-store.service.d.ts +38 -0
- package/dist/room/room-store.service.js +353 -0
- package/dist/room/room.d.ts +107 -0
- package/dist/room/room.js +833 -0
- package/dist/sdk/briyah-config.d.ts +13 -0
- package/dist/sdk/briyah-config.js +32 -0
- package/dist/sdk/briyah.d.ts +36 -0
- package/dist/sdk/briyah.js +122 -0
- package/dist/sdk/index.d.ts +6 -0
- package/dist/sdk/index.js +11 -0
- package/dist/server/src/ai/LLM/anthropic.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/anthropic.module.js +21 -0
- package/dist/server/src/ai/LLM/anthropic.service.d.ts +27 -0
- package/dist/server/src/ai/LLM/anthropic.service.js +329 -0
- package/dist/server/src/ai/LLM/base-ai.service.d.ts +45 -0
- package/dist/server/src/ai/LLM/base-ai.service.js +409 -0
- package/dist/server/src/ai/LLM/deepseek.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/deepseek.module.js +21 -0
- package/dist/server/src/ai/LLM/deepseek.service.d.ts +15 -0
- package/dist/server/src/ai/LLM/deepseek.service.js +279 -0
- package/dist/server/src/ai/LLM/fal.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/fal.module.js +21 -0
- package/dist/server/src/ai/LLM/fal.service.d.ts +17 -0
- package/dist/server/src/ai/LLM/fal.service.js +314 -0
- package/dist/server/src/ai/LLM/googleai.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/googleai.module.js +21 -0
- package/dist/server/src/ai/LLM/googleai.service.d.ts +20 -0
- package/dist/server/src/ai/LLM/googleai.service.js +271 -0
- package/dist/server/src/ai/LLM/grok.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/grok.module.js +21 -0
- package/dist/server/src/ai/LLM/grok.service.d.ts +14 -0
- package/dist/server/src/ai/LLM/grok.service.js +263 -0
- package/dist/server/src/ai/LLM/mock.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/mock.module.js +21 -0
- package/dist/server/src/ai/LLM/mock.service.d.ts +13 -0
- package/dist/server/src/ai/LLM/mock.service.js +194 -0
- package/dist/server/src/ai/LLM/openai.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/openai.module.js +21 -0
- package/dist/server/src/ai/LLM/openai.service.d.ts +21 -0
- package/dist/server/src/ai/LLM/openai.service.js +421 -0
- package/dist/server/src/ai/LLM/together.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/together.module.js +21 -0
- package/dist/server/src/ai/LLM/together.service.d.ts +18 -0
- package/dist/server/src/ai/LLM/together.service.js +285 -0
- package/dist/server/src/ai/LLM/vertexai.module.d.ts +2 -0
- package/dist/server/src/ai/LLM/vertexai.module.js +21 -0
- package/dist/server/src/ai/LLM/vertexai.service.d.ts +17 -0
- package/dist/server/src/ai/LLM/vertexai.service.js +323 -0
- package/dist/server/src/ai/agent-config.d.ts +32 -0
- package/dist/server/src/ai/agent-config.js +2 -0
- package/dist/server/src/ai/agent-factory.d.ts +15 -0
- package/dist/server/src/ai/agent-factory.js +24 -0
- package/dist/server/src/ai/agent-message.service.d.ts +9 -0
- package/dist/server/src/ai/agent-message.service.js +56 -0
- package/dist/server/src/ai/agent-store.module.d.ts +2 -0
- package/dist/server/src/ai/agent-store.module.js +21 -0
- package/dist/server/src/ai/agent-store.service.d.ts +31 -0
- package/dist/server/src/ai/agent-store.service.js +352 -0
- package/dist/server/src/ai/agent.d.ts +75 -0
- package/dist/server/src/ai/agent.js +174 -0
- package/dist/server/src/ai/ai-factory.module.d.ts +2 -0
- package/dist/server/src/ai/ai-factory.module.js +40 -0
- package/dist/server/src/ai/ai-factory.service.d.ts +24 -0
- package/dist/server/src/ai/ai-factory.service.js +94 -0
- package/dist/server/src/ai/artifact.module.d.ts +2 -0
- package/dist/server/src/ai/artifact.module.js +22 -0
- package/dist/server/src/ai/artifact.service.d.ts +21 -0
- package/dist/server/src/ai/artifact.service.js +239 -0
- package/dist/server/src/ai/attached-file.module.d.ts +2 -0
- package/dist/server/src/ai/attached-file.module.js +22 -0
- package/dist/server/src/ai/attached-file.service.d.ts +35 -0
- package/dist/server/src/ai/attached-file.service.js +253 -0
- package/dist/server/src/ai/model_prices.d.ts +2 -0
- package/dist/server/src/ai/model_prices.js +54 -0
- package/dist/server/src/ai/published-agents.service.d.ts +13 -0
- package/dist/server/src/ai/published-agents.service.js +118 -0
- package/dist/server/src/app/balance-message.service.d.ts +8 -0
- package/dist/server/src/app/balance-message.service.js +45 -0
- package/dist/server/src/app/balance.module.d.ts +2 -0
- package/dist/server/src/app/balance.module.js +20 -0
- package/dist/server/src/app/balance.service.d.ts +18 -0
- package/dist/server/src/app/balance.service.js +158 -0
- package/dist/server/src/app/stripe.controller.d.ts +31 -0
- package/dist/server/src/app/stripe.controller.js +148 -0
- package/dist/server/src/app/stripe.module.d.ts +2 -0
- package/dist/server/src/app/stripe.module.js +22 -0
- package/dist/server/src/app/stripe.service.d.ts +28 -0
- package/dist/server/src/app/stripe.service.js +198 -0
- package/dist/server/src/app/transaction.service.d.ts +15 -0
- package/dist/server/src/app/transaction.service.js +139 -0
- package/dist/server/src/app/user-service-factory.d.ts +21 -0
- package/dist/server/src/app/user-service-factory.js +81 -0
- package/dist/server/src/app/user-service-manager.d.ts +28 -0
- package/dist/server/src/app/user-service-manager.js +102 -0
- package/dist/server/src/app.controller.d.ts +232 -0
- package/dist/server/src/app.controller.js +2534 -0
- package/dist/server/src/app.module.d.ts +2 -0
- package/dist/server/src/app.module.js +45 -0
- package/dist/server/src/app.service.d.ts +186 -0
- package/dist/server/src/app.service.js +1334 -0
- package/dist/server/src/auth/agent-access.decorator.d.ts +2 -0
- package/dist/server/src/auth/agent-access.decorator.js +10 -0
- package/dist/server/src/auth/auth.controller.d.ts +38 -0
- package/dist/server/src/auth/auth.controller.js +356 -0
- package/dist/server/src/auth/auth.module.d.ts +2 -0
- package/dist/server/src/auth/auth.module.js +55 -0
- package/dist/server/src/auth/auth.service.d.ts +14 -0
- package/dist/server/src/auth/auth.service.js +84 -0
- package/dist/server/src/auth/dto/bot-login.dto.d.ts +4 -0
- package/dist/server/src/auth/dto/bot-login.dto.js +2 -0
- package/dist/server/src/auth/dto/password-reset.dto.d.ts +11 -0
- package/dist/server/src/auth/dto/password-reset.dto.js +2 -0
- package/dist/server/src/auth/dto/phone-login.dto.d.ts +8 -0
- package/dist/server/src/auth/dto/phone-login.dto.js +2 -0
- package/dist/server/src/auth/dto/phone-verification.dto.d.ts +9 -0
- package/dist/server/src/auth/dto/phone-verification.dto.js +2 -0
- package/dist/server/src/auth/jwt-auth.guard.d.ts +13 -0
- package/dist/server/src/auth/jwt-auth.guard.js +143 -0
- package/dist/server/src/auth/jwt.strategy.d.ts +17 -0
- package/dist/server/src/auth/jwt.strategy.js +45 -0
- package/dist/server/src/auth/phone-validation.service.d.ts +7 -0
- package/dist/server/src/auth/phone-validation.service.js +50 -0
- package/dist/server/src/auth/public.decorator.d.ts +2 -0
- package/dist/server/src/auth/public.decorator.js +7 -0
- package/dist/server/src/auth/rate-limit.service.d.ts +16 -0
- package/dist/server/src/auth/rate-limit.service.js +84 -0
- package/dist/server/src/auth/room-access.decorator.d.ts +2 -0
- package/dist/server/src/auth/room-access.decorator.js +10 -0
- package/dist/server/src/auth/scopes.decorator.d.ts +2 -0
- package/dist/server/src/auth/scopes.decorator.js +7 -0
- package/dist/server/src/auth/scopes.guard.d.ts +9 -0
- package/dist/server/src/auth/scopes.guard.js +82 -0
- package/dist/server/src/auth/session.guard.d.ts +7 -0
- package/dist/server/src/auth/session.guard.js +55 -0
- package/dist/server/src/auth/twilio.service.d.ts +15 -0
- package/dist/server/src/auth/twilio.service.js +139 -0
- package/dist/server/src/auth/users.service.d.ts +24 -0
- package/dist/server/src/auth/users.service.js +149 -0
- package/dist/server/src/common/errors.d.ts +12 -0
- package/dist/server/src/common/errors.js +43 -0
- package/dist/server/src/config/configuration.module.d.ts +2 -0
- package/dist/server/src/config/configuration.module.js +20 -0
- package/dist/server/src/config/configuration.service.d.ts +26 -0
- package/dist/server/src/config/configuration.service.js +131 -0
- package/dist/server/src/room/artifact-store.service.d.ts +15 -0
- package/dist/server/src/room/artifact-store.service.js +226 -0
- package/dist/server/src/room/artifact.d.ts +6 -0
- package/dist/server/src/room/artifact.js +2 -0
- package/dist/server/src/room/message.d.ts +19 -0
- package/dist/server/src/room/message.js +70 -0
- package/dist/server/src/room/published-rooms.service.d.ts +12 -0
- package/dist/server/src/room/published-rooms.service.js +108 -0
- package/dist/server/src/room/room-config.d.ts +12 -0
- package/dist/server/src/room/room-config.js +2 -0
- package/dist/server/src/room/room-factory.d.ts +7 -0
- package/dist/server/src/room/room-factory.js +16 -0
- package/dist/server/src/room/room-message.module.d.ts +2 -0
- package/dist/server/src/room/room-message.module.js +21 -0
- package/dist/server/src/room/room-message.service.d.ts +8 -0
- package/dist/server/src/room/room-message.service.js +44 -0
- package/dist/server/src/room/room-store.module.d.ts +2 -0
- package/dist/server/src/room/room-store.module.js +25 -0
- package/dist/server/src/room/room-store.service.d.ts +38 -0
- package/dist/server/src/room/room-store.service.js +353 -0
- package/dist/server/src/room/room.d.ts +107 -0
- package/dist/server/src/room/room.js +833 -0
- package/dist/server/src/sdk/briyah-config.d.ts +13 -0
- package/dist/server/src/sdk/briyah-config.js +32 -0
- package/dist/server/src/sdk/briyah.d.ts +36 -0
- package/dist/server/src/sdk/briyah.js +122 -0
- package/dist/server/src/sdk/index.d.ts +6 -0
- package/dist/server/src/sdk/index.js +11 -0
- package/dist/server/src/shared/shared.module.d.ts +2 -0
- package/dist/server/src/shared/shared.module.js +46 -0
- package/dist/server/src/story/story-message.service.d.ts +10 -0
- package/dist/server/src/story/story-message.service.js +57 -0
- package/dist/server/src/story/story-progress.service.d.ts +8 -0
- package/dist/server/src/story/story-progress.service.js +44 -0
- package/dist/server/src/story/story-store.module.d.ts +2 -0
- package/dist/server/src/story/story-store.module.js +22 -0
- package/dist/server/src/story/story-store.service.d.ts +23 -0
- package/dist/server/src/story/story-store.service.js +392 -0
- package/dist/server/src/story/story.service.d.ts +94 -0
- package/dist/server/src/story/story.service.js +1797 -0
- package/dist/shared/shared.module.d.ts +2 -0
- package/dist/shared/shared.module.js +46 -0
- package/dist/shared/types/app.types.d.ts +296 -0
- package/dist/shared/types/app.types.js +17 -0
- package/dist/story/story-message.service.d.ts +10 -0
- package/dist/story/story-message.service.js +57 -0
- package/dist/story/story-progress.service.d.ts +8 -0
- package/dist/story/story-progress.service.js +44 -0
- package/dist/story/story-store.module.d.ts +2 -0
- package/dist/story/story-store.module.js +22 -0
- package/dist/story/story-store.service.d.ts +23 -0
- package/dist/story/story-store.service.js +392 -0
- package/dist/story/story.service.d.ts +94 -0
- package/dist/story/story.service.js +1797 -0
- package/eslint.config.js +29 -0
- package/package.json +125 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.DeepSeekAiService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const deepseek_1 = require("@ai-sdk/deepseek");
|
|
18
|
+
const ai_1 = require("ai");
|
|
19
|
+
const base_ai_service_1 = require("./base-ai.service");
|
|
20
|
+
const zod_1 = require("zod");
|
|
21
|
+
const model_prices_1 = __importDefault(require("../model_prices"));
|
|
22
|
+
const errors_1 = require("../../common/errors");
|
|
23
|
+
let DeepSeekAiService = class DeepSeekAiService extends base_ai_service_1.BaseAiService {
|
|
24
|
+
deepseekClient;
|
|
25
|
+
constructor() {
|
|
26
|
+
super();
|
|
27
|
+
try {
|
|
28
|
+
const apiKey = process.env['DEEPSEEK_API_KEY'];
|
|
29
|
+
if (!apiKey) {
|
|
30
|
+
console.warn('DEEPSEEK_API_KEY environment variable not set. DeepSeek service will not be available.');
|
|
31
|
+
this._isAvailable = false;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.deepseekClient = (0, deepseek_1.createDeepSeek)({
|
|
35
|
+
apiKey: apiKey,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error('Error initializing DeepSeek service:', error);
|
|
40
|
+
this._isAvailable = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
getServiceName() {
|
|
44
|
+
return 'DeepSeek';
|
|
45
|
+
}
|
|
46
|
+
async fetchModelsFromApi() {
|
|
47
|
+
try {
|
|
48
|
+
const apiKey = process.env['DEEPSEEK_API_KEY'];
|
|
49
|
+
if (!apiKey) {
|
|
50
|
+
throw new Error('DEEPSEEK_API_KEY not configured');
|
|
51
|
+
}
|
|
52
|
+
const response = await fetch('https://api.deepseek.com/models', {
|
|
53
|
+
method: 'GET',
|
|
54
|
+
headers: {
|
|
55
|
+
Authorization: `Bearer ${apiKey}`,
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
throw new Error(`DeepSeek API returned ${response.status}: ${response.statusText}`);
|
|
61
|
+
}
|
|
62
|
+
const list = await response.json();
|
|
63
|
+
return list?.data?.map((model) => {
|
|
64
|
+
return {
|
|
65
|
+
name: model.id,
|
|
66
|
+
description: model.id,
|
|
67
|
+
model: model.id,
|
|
68
|
+
service: 'DeepSeek',
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('Error fetching DeepSeek models from API:', error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
addToConversationHistory(agent, message, fromSelf = false) {
|
|
78
|
+
if (!agent.history) {
|
|
79
|
+
agent.history = [];
|
|
80
|
+
}
|
|
81
|
+
if (!message)
|
|
82
|
+
return;
|
|
83
|
+
agent.history.push({
|
|
84
|
+
role: fromSelf ? 'assistant' : 'user',
|
|
85
|
+
content: message,
|
|
86
|
+
});
|
|
87
|
+
agent.trimHistoryIfNeeded();
|
|
88
|
+
}
|
|
89
|
+
jsonSchemaToZod(jsonSchema) {
|
|
90
|
+
if (!jsonSchema || !jsonSchema.type) {
|
|
91
|
+
return zod_1.z.any();
|
|
92
|
+
}
|
|
93
|
+
switch (jsonSchema.type) {
|
|
94
|
+
case 'object':
|
|
95
|
+
const shape = {};
|
|
96
|
+
if (jsonSchema.properties) {
|
|
97
|
+
for (const [key, propSchema] of Object.entries(jsonSchema.properties)) {
|
|
98
|
+
shape[key] = this.jsonSchemaToZod(propSchema);
|
|
99
|
+
if (jsonSchema.required && !jsonSchema.required.includes(key)) {
|
|
100
|
+
shape[key] = shape[key].optional();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return zod_1.z.object(shape);
|
|
105
|
+
case 'array':
|
|
106
|
+
if (jsonSchema.items) {
|
|
107
|
+
return zod_1.z.array(this.jsonSchemaToZod(jsonSchema.items));
|
|
108
|
+
}
|
|
109
|
+
return zod_1.z.array(zod_1.z.any());
|
|
110
|
+
case 'string':
|
|
111
|
+
let stringSchema = zod_1.z.string();
|
|
112
|
+
if (jsonSchema.minLength !== undefined) {
|
|
113
|
+
stringSchema = stringSchema.min(jsonSchema.minLength);
|
|
114
|
+
}
|
|
115
|
+
if (jsonSchema.maxLength !== undefined) {
|
|
116
|
+
stringSchema = stringSchema.max(jsonSchema.maxLength);
|
|
117
|
+
}
|
|
118
|
+
if (jsonSchema.pattern) {
|
|
119
|
+
stringSchema = stringSchema.regex(new RegExp(jsonSchema.pattern));
|
|
120
|
+
}
|
|
121
|
+
if (jsonSchema.enum) {
|
|
122
|
+
return zod_1.z.enum(jsonSchema.enum);
|
|
123
|
+
}
|
|
124
|
+
return stringSchema;
|
|
125
|
+
case 'number':
|
|
126
|
+
case 'integer':
|
|
127
|
+
let numberSchema = jsonSchema.type === 'integer' ? zod_1.z.number().int() : zod_1.z.number();
|
|
128
|
+
if (jsonSchema.minimum !== undefined) {
|
|
129
|
+
numberSchema = numberSchema.min(jsonSchema.minimum);
|
|
130
|
+
}
|
|
131
|
+
if (jsonSchema.maximum !== undefined) {
|
|
132
|
+
numberSchema = numberSchema.max(jsonSchema.maximum);
|
|
133
|
+
}
|
|
134
|
+
return numberSchema;
|
|
135
|
+
case 'boolean':
|
|
136
|
+
return zod_1.z.boolean();
|
|
137
|
+
case 'null':
|
|
138
|
+
return zod_1.z.null();
|
|
139
|
+
default:
|
|
140
|
+
return zod_1.z.any();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null, _cacheConfig = null, maxOutputChars = 0) {
|
|
144
|
+
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
145
|
+
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
146
|
+
if (!hasSufficientBalance) {
|
|
147
|
+
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
151
|
+
const instructions = promptInstructions || agent.systemInstruction;
|
|
152
|
+
const messages = [];
|
|
153
|
+
if (instructions)
|
|
154
|
+
messages.push({ role: 'system', content: instructions });
|
|
155
|
+
messages.push({ role: 'user', content: identity });
|
|
156
|
+
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
157
|
+
for (const file of agent.attachedFiles) {
|
|
158
|
+
if (file.serviceMetadata?.fileData) {
|
|
159
|
+
const fileData = new Uint8Array(file.serviceMetadata.fileData);
|
|
160
|
+
const textContent = Buffer.from(fileData).toString('utf-8');
|
|
161
|
+
messages.push({
|
|
162
|
+
role: 'user',
|
|
163
|
+
content: `Attached file "${file.fileName}":\n\n${textContent}`,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (agent.attachedArtifacts && agent.attachedArtifacts.length > 0) {
|
|
169
|
+
for (const artifactId of agent.attachedArtifacts) {
|
|
170
|
+
const content = agent.artifactService.getArtifact(artifactId);
|
|
171
|
+
if (content && typeof content === 'string') {
|
|
172
|
+
const metadata = agent.artifactService.getArtifactMetadata(artifactId);
|
|
173
|
+
const fileName = metadata ? `${metadata.name}.md` : 'artifact.md';
|
|
174
|
+
messages.push({
|
|
175
|
+
role: 'user',
|
|
176
|
+
content: `Attached document: ${fileName}\n\n${content}`,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
messages.push(...agent.history);
|
|
182
|
+
if (prompt) {
|
|
183
|
+
messages.push({
|
|
184
|
+
role: 'user',
|
|
185
|
+
content: prompt,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
let reasoningEffort = agent.reasoningEffort ? agent.reasoningEffort : undefined;
|
|
189
|
+
try {
|
|
190
|
+
if (jsonSchema) {
|
|
191
|
+
const result = await (0, ai_1.generateObject)({
|
|
192
|
+
model: this.deepseekClient(agent.modelName),
|
|
193
|
+
messages: messages,
|
|
194
|
+
temperature: 0,
|
|
195
|
+
schema: jsonSchema,
|
|
196
|
+
providerOptions: {
|
|
197
|
+
deepseek: {
|
|
198
|
+
reasoningEffort: reasoningEffort,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
if (result.usage) {
|
|
203
|
+
this.computeMessageCost(agent, result.usage);
|
|
204
|
+
}
|
|
205
|
+
if (saveResponse) {
|
|
206
|
+
this.addToConversationHistory(agent, prompt, false);
|
|
207
|
+
this.addToConversationHistory(agent, JSON.stringify(result.object), true);
|
|
208
|
+
}
|
|
209
|
+
return result.object;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const result = await (0, ai_1.generateText)({
|
|
213
|
+
model: this.deepseekClient(agent.modelName),
|
|
214
|
+
messages: messages,
|
|
215
|
+
temperature: 0,
|
|
216
|
+
providerOptions: {
|
|
217
|
+
deepseek: {
|
|
218
|
+
reasoningEffort: reasoningEffort,
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
if (result.usage) {
|
|
223
|
+
this.computeMessageCost(agent, result.usage);
|
|
224
|
+
}
|
|
225
|
+
if (saveResponse) {
|
|
226
|
+
this.addToConversationHistory(agent, prompt, false);
|
|
227
|
+
this.addToConversationHistory(agent, result.text, true);
|
|
228
|
+
}
|
|
229
|
+
return result.text;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
console.error('Error generating response from DeepSeek:', error);
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async attachDocument(agent, fileName, fileData) {
|
|
238
|
+
if (agent === null) {
|
|
239
|
+
throw new Error('Shared document attachment not supported by the DeepSeek service');
|
|
240
|
+
}
|
|
241
|
+
return super.attachDocument(agent, fileName, fileData);
|
|
242
|
+
}
|
|
243
|
+
computeMessageCost(agent, usage) {
|
|
244
|
+
const totalInputTokens = usage.inputTokens || 0;
|
|
245
|
+
const cachedTokens = usage.cachedInputTokens || 0;
|
|
246
|
+
const uncachedInputTokens = totalInputTokens - cachedTokens;
|
|
247
|
+
const outputTokens = usage.outputTokens || 0;
|
|
248
|
+
let modelInfo = model_prices_1.default[agent.modelName];
|
|
249
|
+
if (!modelInfo && !agent.modelName.startsWith('deepseek/')) {
|
|
250
|
+
modelInfo = model_prices_1.default[`deepseek/${agent.modelName}`];
|
|
251
|
+
}
|
|
252
|
+
if (!modelInfo) {
|
|
253
|
+
console.error(`No price info found for model ${agent.modelName} or deepseek/${agent.modelName}`);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const uncachedCost = uncachedInputTokens * modelInfo.input_cost_per_token;
|
|
257
|
+
const cachedCost = cachedTokens *
|
|
258
|
+
(modelInfo.cache_read_input_token_cost || modelInfo.input_cost_per_token * 0.25);
|
|
259
|
+
const outputCost = outputTokens * modelInfo.output_cost_per_token;
|
|
260
|
+
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
261
|
+
const cost = uncachedCost + cachedCost + outputCost + markup;
|
|
262
|
+
agent.totalInputTokens += totalInputTokens;
|
|
263
|
+
agent.totalOutputTokens += outputTokens;
|
|
264
|
+
agent.totalCost += cost;
|
|
265
|
+
agent.totalMarkup += markup;
|
|
266
|
+
console.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
267
|
+
if (cachedTokens > 0) {
|
|
268
|
+
console.log(`Cache metrics for ${agent.agentName}: cached=${cachedTokens}, uncached=${uncachedInputTokens}`);
|
|
269
|
+
}
|
|
270
|
+
if (agent.balanceService && cost > 0) {
|
|
271
|
+
agent.balanceService.decrementBalance(cost, markup);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
exports.DeepSeekAiService = DeepSeekAiService;
|
|
276
|
+
exports.DeepSeekAiService = DeepSeekAiService = __decorate([
|
|
277
|
+
(0, common_1.Injectable)(),
|
|
278
|
+
__metadata("design:paramtypes", [])
|
|
279
|
+
], DeepSeekAiService);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.FalAiModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const fal_service_1 = require("./fal.service");
|
|
12
|
+
let FalAiModule = class FalAiModule {
|
|
13
|
+
};
|
|
14
|
+
exports.FalAiModule = FalAiModule;
|
|
15
|
+
exports.FalAiModule = FalAiModule = __decorate([
|
|
16
|
+
(0, common_1.Module)({
|
|
17
|
+
imports: [],
|
|
18
|
+
providers: [fal_service_1.FalAiService],
|
|
19
|
+
exports: [fal_service_1.FalAiService],
|
|
20
|
+
})
|
|
21
|
+
], FalAiModule);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseAiService } from './base-ai.service';
|
|
2
|
+
import { Agent } from '../agent';
|
|
3
|
+
import { ModelInfo } from '@shared/types/app.types';
|
|
4
|
+
export declare class FalAiService extends BaseAiService {
|
|
5
|
+
constructor();
|
|
6
|
+
getServiceName(): string;
|
|
7
|
+
supportsImageGeneration(): boolean;
|
|
8
|
+
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
9
|
+
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean, developer?: boolean): void;
|
|
10
|
+
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
11
|
+
private computeMessageCost;
|
|
12
|
+
private computeImageCost;
|
|
13
|
+
generateImage(agent: Agent, prompt: string, width: number, height: number, quality: string, referenceImageArtifactIds?: string[]): Promise<{
|
|
14
|
+
artifactId?: string;
|
|
15
|
+
error?: any;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FalAiService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const client_1 = require("@fal-ai/client");
|
|
18
|
+
const base_ai_service_1 = require("./base-ai.service");
|
|
19
|
+
const model_prices_1 = __importDefault(require("../model_prices"));
|
|
20
|
+
const errors_1 = require("../../common/errors");
|
|
21
|
+
let FalAiService = class FalAiService extends base_ai_service_1.BaseAiService {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
try {
|
|
25
|
+
const apiKey = process.env['FAL_API_KEY'];
|
|
26
|
+
if (!apiKey) {
|
|
27
|
+
console.warn('FAL_API_KEY environment variable not set. Fal AI service will not be available.');
|
|
28
|
+
this._isAvailable = false;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
client_1.fal.config({ credentials: apiKey });
|
|
32
|
+
console.log('Fal AI service initialized successfully');
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error('Error initializing Fal AI service:', error);
|
|
36
|
+
this._isAvailable = false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getServiceName() {
|
|
40
|
+
return 'FalAI';
|
|
41
|
+
}
|
|
42
|
+
supportsImageGeneration() {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
async fetchModelsFromApi() {
|
|
46
|
+
try {
|
|
47
|
+
const apiKey = process.env['FAL_API_KEY'];
|
|
48
|
+
if (!apiKey) {
|
|
49
|
+
console.warn('FAL_API_KEY not set, cannot fetch models');
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
const response = await fetch('https://api.fal.ai/v1/models', {
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Key ${apiKey}`,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
console.error(`Fal API error: ${response.status} ${response.statusText}`);
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
const models = [];
|
|
63
|
+
if (data.models && Array.isArray(data.models)) {
|
|
64
|
+
for (const model of data.models) {
|
|
65
|
+
models.push({
|
|
66
|
+
name: model.metadata.display_name,
|
|
67
|
+
description: model.metadata.description,
|
|
68
|
+
model: model.endpoint_id,
|
|
69
|
+
service: 'FalAI',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
console.log(`Fetched ${models.length} active models from Fal AI`);
|
|
74
|
+
return models;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error('Error fetching Fal AI models:', error);
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
addToConversationHistory(agent, message, fromSelf = false, developer = false) {
|
|
82
|
+
if (!agent.history) {
|
|
83
|
+
agent.history = [];
|
|
84
|
+
}
|
|
85
|
+
if (!message)
|
|
86
|
+
return;
|
|
87
|
+
let role = fromSelf ? 'assistant' : 'user';
|
|
88
|
+
if (developer)
|
|
89
|
+
role = 'system';
|
|
90
|
+
agent.history.push({
|
|
91
|
+
role: role,
|
|
92
|
+
content: message,
|
|
93
|
+
});
|
|
94
|
+
agent.trimHistoryIfNeeded();
|
|
95
|
+
}
|
|
96
|
+
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null, _cacheConfig = null, maxOutputChars = 0) {
|
|
97
|
+
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
98
|
+
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
99
|
+
if (!hasSufficientBalance) {
|
|
100
|
+
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
104
|
+
const instructions = promptInstructions || agent.systemInstruction;
|
|
105
|
+
let fullPrompt = '';
|
|
106
|
+
if (instructions) {
|
|
107
|
+
fullPrompt += instructions + '\n\n';
|
|
108
|
+
}
|
|
109
|
+
fullPrompt += identity + '\n\n';
|
|
110
|
+
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
111
|
+
for (const file of agent.attachedFiles) {
|
|
112
|
+
if (file.serviceMetadata?.fileData) {
|
|
113
|
+
const fileData = new Uint8Array(file.serviceMetadata.fileData);
|
|
114
|
+
const textContent = Buffer.from(fileData).toString('utf-8');
|
|
115
|
+
fullPrompt += `Attached file "${file.fileName}":\n\n${textContent}\n\n`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const msg of agent.history) {
|
|
120
|
+
fullPrompt += `${msg.role}: ${msg.content}\n\n`;
|
|
121
|
+
}
|
|
122
|
+
if (jsonSchema) {
|
|
123
|
+
fullPrompt += `You must respond with valid JSON matching this schema: ${JSON.stringify(jsonSchema)}\n\n`;
|
|
124
|
+
}
|
|
125
|
+
if (prompt) {
|
|
126
|
+
fullPrompt += `user: ${prompt}\n\n`;
|
|
127
|
+
}
|
|
128
|
+
fullPrompt += 'assistant: ';
|
|
129
|
+
let responseText;
|
|
130
|
+
let estimatedInputTokens = 0;
|
|
131
|
+
let estimatedOutputTokens = 0;
|
|
132
|
+
try {
|
|
133
|
+
estimatedInputTokens = Math.ceil(fullPrompt.length / 4);
|
|
134
|
+
const result = (await client_1.fal.subscribe('fal-ai/any-llm', {
|
|
135
|
+
input: {
|
|
136
|
+
model: agent.modelName,
|
|
137
|
+
prompt: fullPrompt,
|
|
138
|
+
},
|
|
139
|
+
}));
|
|
140
|
+
if (result.output) {
|
|
141
|
+
responseText = result.output;
|
|
142
|
+
}
|
|
143
|
+
else if (result.text) {
|
|
144
|
+
responseText = result.text;
|
|
145
|
+
}
|
|
146
|
+
else if (typeof result === 'string') {
|
|
147
|
+
responseText = result;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
responseText = '';
|
|
151
|
+
}
|
|
152
|
+
estimatedOutputTokens = Math.ceil(responseText.length / 4);
|
|
153
|
+
if (jsonSchema) {
|
|
154
|
+
responseText = this.trimToJson(responseText);
|
|
155
|
+
responseText = this.sanitizeJsonString(responseText);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
responseText = this.trimResponseText(responseText);
|
|
159
|
+
}
|
|
160
|
+
const usage = result.usage || {
|
|
161
|
+
prompt_tokens: estimatedInputTokens,
|
|
162
|
+
completion_tokens: estimatedOutputTokens,
|
|
163
|
+
};
|
|
164
|
+
this.computeMessageCost(agent, usage, estimatedInputTokens, estimatedOutputTokens);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
console.error('Error calling Fal AI:', error);
|
|
168
|
+
if (error.message?.includes('authentication') ||
|
|
169
|
+
error.message?.includes('unauthorized')) {
|
|
170
|
+
throw new Error('Fal AI authentication failed. Check FAL_API_KEY.');
|
|
171
|
+
}
|
|
172
|
+
else if (error.message?.includes('rate limit')) {
|
|
173
|
+
throw new Error('Fal AI rate limit exceeded.');
|
|
174
|
+
}
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
if (saveResponse) {
|
|
178
|
+
this.addToConversationHistory(agent, prompt, false);
|
|
179
|
+
this.addToConversationHistory(agent, responseText, true);
|
|
180
|
+
}
|
|
181
|
+
if (jsonSchema) {
|
|
182
|
+
try {
|
|
183
|
+
return JSON.parse(responseText);
|
|
184
|
+
}
|
|
185
|
+
catch (parseError) {
|
|
186
|
+
console.error('Failed to parse JSON response:', parseError);
|
|
187
|
+
console.error('Response text:', responseText);
|
|
188
|
+
throw new Error('Failed to parse JSON response from Fal AI');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return responseText;
|
|
192
|
+
}
|
|
193
|
+
computeMessageCost(agent, usage, estimatedInputTokens, estimatedOutputTokens) {
|
|
194
|
+
const inputTokens = usage?.prompt_tokens || estimatedInputTokens || 0;
|
|
195
|
+
const outputTokens = usage?.completion_tokens || estimatedOutputTokens || 0;
|
|
196
|
+
let modelInfo = model_prices_1.default[agent.modelName];
|
|
197
|
+
if (!modelInfo && !agent.modelName.startsWith('fal_ai/')) {
|
|
198
|
+
modelInfo = model_prices_1.default[`fal_ai/${agent.modelName}`];
|
|
199
|
+
}
|
|
200
|
+
if (!modelInfo) {
|
|
201
|
+
console.warn(`No price info found for model ${agent.modelName}. Skipping cost tracking.`);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const inputCost = inputTokens * (modelInfo.input_cost_per_token || 0);
|
|
205
|
+
const outputCost = outputTokens * (modelInfo.output_cost_per_token || 0);
|
|
206
|
+
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
207
|
+
const cost = inputCost + outputCost + markup;
|
|
208
|
+
agent.totalInputTokens += inputTokens;
|
|
209
|
+
agent.totalOutputTokens += outputTokens;
|
|
210
|
+
agent.totalCost += cost;
|
|
211
|
+
agent.totalMarkup += markup;
|
|
212
|
+
if (!usage?.prompt_tokens) {
|
|
213
|
+
console.log(`Cost for ${agent.agentName} message (estimated): ${cost.toFixed(4)} (markup: ${markup.toFixed(4)})`);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
console.log(`Cost for ${agent.agentName} message: ${cost.toFixed(4)} (markup: ${markup.toFixed(4)})`);
|
|
217
|
+
}
|
|
218
|
+
if (agent.balanceService && cost > 0) {
|
|
219
|
+
agent.balanceService.decrementBalance(cost, markup);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
computeImageCost(agent) {
|
|
223
|
+
let modelInfo = model_prices_1.default[agent.modelName];
|
|
224
|
+
if (!modelInfo && !agent.modelName.startsWith('fal_ai/')) {
|
|
225
|
+
modelInfo = model_prices_1.default[`fal_ai/${agent.modelName}`];
|
|
226
|
+
}
|
|
227
|
+
if (!modelInfo || !modelInfo.output_cost_per_image) {
|
|
228
|
+
console.warn(`No image price info found for ${agent.modelName}. Skipping cost tracking.`);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const cost = modelInfo.output_cost_per_image;
|
|
232
|
+
const markup = 0;
|
|
233
|
+
agent.totalCost += cost;
|
|
234
|
+
agent.totalMarkup += markup;
|
|
235
|
+
console.log(`Cost for image generation: ${cost.toFixed(4)} (markup: ${markup.toFixed(4)})`);
|
|
236
|
+
if (agent.balanceService && cost > 0) {
|
|
237
|
+
agent.balanceService.decrementBalance(cost, markup);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async generateImage(agent, prompt, width, height, quality, referenceImageArtifactIds) {
|
|
241
|
+
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
242
|
+
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
243
|
+
if (!hasSufficientBalance) {
|
|
244
|
+
throw new errors_1.InsufficientBalanceError('Insufficient balance for image generation. Please add funds to continue.');
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
let endpoint = agent.modelName;
|
|
249
|
+
if (!endpoint.startsWith('fal-ai/')) {
|
|
250
|
+
endpoint = `fal-ai/${endpoint}`;
|
|
251
|
+
}
|
|
252
|
+
const input = {
|
|
253
|
+
prompt: prompt,
|
|
254
|
+
image_size: {
|
|
255
|
+
width: width,
|
|
256
|
+
height: height,
|
|
257
|
+
},
|
|
258
|
+
num_images: 1,
|
|
259
|
+
output_format: 'png',
|
|
260
|
+
};
|
|
261
|
+
if (referenceImageArtifactIds && referenceImageArtifactIds.length > 0) {
|
|
262
|
+
input.image_urls = await this.loadReferenceImages(agent, referenceImageArtifactIds);
|
|
263
|
+
if (!endpoint.endsWith('/edit'))
|
|
264
|
+
endpoint += '/edit';
|
|
265
|
+
}
|
|
266
|
+
const result = (await client_1.fal.subscribe(endpoint, {
|
|
267
|
+
input: input,
|
|
268
|
+
}));
|
|
269
|
+
this.computeImageCost(agent);
|
|
270
|
+
let base64Data;
|
|
271
|
+
if (result.data.images && result.data.images[0]) {
|
|
272
|
+
const image = result.data.images[0];
|
|
273
|
+
if (image.url) {
|
|
274
|
+
const imageResponse = await fetch(image.url);
|
|
275
|
+
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
|
276
|
+
base64Data = imageBuffer.toString('base64');
|
|
277
|
+
}
|
|
278
|
+
else if (image.content) {
|
|
279
|
+
base64Data = image.content;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
throw new Error('No image data found in response');
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else if (result.data.image) {
|
|
286
|
+
if (result.data.image.url) {
|
|
287
|
+
const imageResponse = await fetch(result.data.image.url);
|
|
288
|
+
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
|
289
|
+
base64Data = imageBuffer.toString('base64');
|
|
290
|
+
}
|
|
291
|
+
else if (result.data.image.content) {
|
|
292
|
+
base64Data = result.data.image.content;
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
throw new Error('No image data found in response');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
throw new Error('Unexpected response format from Fal AI');
|
|
300
|
+
}
|
|
301
|
+
const artifactId = await this.saveImageAsArtifact(agent, base64Data, prompt);
|
|
302
|
+
return { artifactId };
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
console.error('Error generating image with Fal AI:', error.message || error);
|
|
306
|
+
return { error };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
exports.FalAiService = FalAiService;
|
|
311
|
+
exports.FalAiService = FalAiService = __decorate([
|
|
312
|
+
(0, common_1.Injectable)(),
|
|
313
|
+
__metadata("design:paramtypes", [])
|
|
314
|
+
], FalAiService);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.GoogleAiModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const googleai_service_1 = require("./googleai.service");
|
|
12
|
+
let GoogleAiModule = class GoogleAiModule {
|
|
13
|
+
};
|
|
14
|
+
exports.GoogleAiModule = GoogleAiModule;
|
|
15
|
+
exports.GoogleAiModule = GoogleAiModule = __decorate([
|
|
16
|
+
(0, common_1.Module)({
|
|
17
|
+
imports: [],
|
|
18
|
+
providers: [googleai_service_1.GoogleAiService],
|
|
19
|
+
exports: [googleai_service_1.GoogleAiService],
|
|
20
|
+
})
|
|
21
|
+
], GoogleAiModule);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BaseAiService } from './base-ai.service';
|
|
2
|
+
import { Agent } from '../agent';
|
|
3
|
+
import { ModelInfo } from '@shared/types/app.types';
|
|
4
|
+
export declare class GoogleAiService extends BaseAiService {
|
|
5
|
+
private googleAI;
|
|
6
|
+
constructor();
|
|
7
|
+
getServiceName(): string;
|
|
8
|
+
supportsImageGeneration(): boolean;
|
|
9
|
+
supportsPromptCaching(): boolean;
|
|
10
|
+
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
11
|
+
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean): void;
|
|
12
|
+
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
13
|
+
generateImage(agent: Agent, prompt: string, width: number, height: number, quality: string, _referenceImageArtifactIds?: string[]): Promise<{
|
|
14
|
+
artifactId?: string;
|
|
15
|
+
error?: any;
|
|
16
|
+
}>;
|
|
17
|
+
private computeMessageCost;
|
|
18
|
+
private computeImageCost;
|
|
19
|
+
formatMessage(message: any): string;
|
|
20
|
+
}
|