briyah 1.0.1 → 1.0.3
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/dist/server/src/ai/LLM/deepseek.service.js +1 -1
- package/package.json +2 -2
- package/dist/ai/LLM/anthropic.module.d.ts +0 -2
- package/dist/ai/LLM/anthropic.module.js +0 -21
- package/dist/ai/LLM/anthropic.service.d.ts +0 -27
- package/dist/ai/LLM/anthropic.service.js +0 -329
- package/dist/ai/LLM/base-ai.service.d.ts +0 -45
- package/dist/ai/LLM/base-ai.service.js +0 -409
- package/dist/ai/LLM/deepseek.module.d.ts +0 -2
- package/dist/ai/LLM/deepseek.module.js +0 -21
- package/dist/ai/LLM/deepseek.service.d.ts +0 -15
- package/dist/ai/LLM/deepseek.service.js +0 -279
- package/dist/ai/LLM/fal.module.d.ts +0 -2
- package/dist/ai/LLM/fal.module.js +0 -21
- package/dist/ai/LLM/fal.service.d.ts +0 -17
- package/dist/ai/LLM/fal.service.js +0 -314
- package/dist/ai/LLM/googleai.module.d.ts +0 -2
- package/dist/ai/LLM/googleai.module.js +0 -21
- package/dist/ai/LLM/googleai.service.d.ts +0 -20
- package/dist/ai/LLM/googleai.service.js +0 -271
- package/dist/ai/LLM/grok.module.d.ts +0 -2
- package/dist/ai/LLM/grok.module.js +0 -21
- package/dist/ai/LLM/grok.service.d.ts +0 -14
- package/dist/ai/LLM/grok.service.js +0 -263
- package/dist/ai/LLM/mock.module.d.ts +0 -2
- package/dist/ai/LLM/mock.module.js +0 -21
- package/dist/ai/LLM/mock.service.d.ts +0 -13
- package/dist/ai/LLM/mock.service.js +0 -194
- package/dist/ai/LLM/openai.module.d.ts +0 -2
- package/dist/ai/LLM/openai.module.js +0 -21
- package/dist/ai/LLM/openai.service.d.ts +0 -21
- package/dist/ai/LLM/openai.service.js +0 -421
- package/dist/ai/LLM/together.module.d.ts +0 -2
- package/dist/ai/LLM/together.module.js +0 -21
- package/dist/ai/LLM/together.service.d.ts +0 -18
- package/dist/ai/LLM/together.service.js +0 -285
- package/dist/ai/LLM/vertexai.module.d.ts +0 -2
- package/dist/ai/LLM/vertexai.module.js +0 -21
- package/dist/ai/LLM/vertexai.service.d.ts +0 -17
- package/dist/ai/LLM/vertexai.service.js +0 -323
- package/dist/ai/agent-config.d.ts +0 -32
- package/dist/ai/agent-config.js +0 -2
- package/dist/ai/agent-factory.d.ts +0 -15
- package/dist/ai/agent-factory.js +0 -24
- package/dist/ai/agent-message.service.d.ts +0 -9
- package/dist/ai/agent-message.service.js +0 -56
- package/dist/ai/agent-store.module.d.ts +0 -2
- package/dist/ai/agent-store.module.js +0 -21
- package/dist/ai/agent-store.service.d.ts +0 -31
- package/dist/ai/agent-store.service.js +0 -352
- package/dist/ai/agent.d.ts +0 -75
- package/dist/ai/agent.js +0 -174
- package/dist/ai/ai-factory.module.d.ts +0 -2
- package/dist/ai/ai-factory.module.js +0 -40
- package/dist/ai/ai-factory.service.d.ts +0 -24
- package/dist/ai/ai-factory.service.js +0 -94
- package/dist/ai/artifact.module.d.ts +0 -2
- package/dist/ai/artifact.module.js +0 -22
- package/dist/ai/artifact.service.d.ts +0 -21
- package/dist/ai/artifact.service.js +0 -239
- package/dist/ai/attached-file.module.d.ts +0 -2
- package/dist/ai/attached-file.module.js +0 -22
- package/dist/ai/attached-file.service.d.ts +0 -35
- package/dist/ai/attached-file.service.js +0 -253
- package/dist/ai/model_prices.d.ts +0 -2
- package/dist/ai/model_prices.js +0 -54
- package/dist/ai/published-agents.service.d.ts +0 -13
- package/dist/ai/published-agents.service.js +0 -118
- package/dist/app/balance-message.service.d.ts +0 -8
- package/dist/app/balance-message.service.js +0 -45
- package/dist/app/balance.module.d.ts +0 -2
- package/dist/app/balance.module.js +0 -20
- package/dist/app/balance.service.d.ts +0 -18
- package/dist/app/balance.service.js +0 -158
- package/dist/app/stripe.controller.d.ts +0 -31
- package/dist/app/stripe.controller.js +0 -150
- package/dist/app/stripe.module.d.ts +0 -2
- package/dist/app/stripe.module.js +0 -22
- package/dist/app/stripe.service.d.ts +0 -28
- package/dist/app/stripe.service.js +0 -198
- package/dist/app/transaction.service.d.ts +0 -15
- package/dist/app/transaction.service.js +0 -139
- package/dist/app/user-service-factory.d.ts +0 -21
- package/dist/app/user-service-factory.js +0 -81
- package/dist/app/user-service-manager.d.ts +0 -28
- package/dist/app/user-service-manager.js +0 -102
- package/dist/app.controller.d.ts +0 -232
- package/dist/app.controller.js +0 -2536
- package/dist/app.module.d.ts +0 -2
- package/dist/app.module.js +0 -45
- package/dist/app.service.d.ts +0 -186
- package/dist/app.service.js +0 -1334
- package/dist/auth/agent-access.decorator.d.ts +0 -2
- package/dist/auth/agent-access.decorator.js +0 -10
- package/dist/auth/auth.controller.d.ts +0 -38
- package/dist/auth/auth.controller.js +0 -356
- package/dist/auth/auth.module.d.ts +0 -2
- package/dist/auth/auth.module.js +0 -55
- package/dist/auth/auth.service.d.ts +0 -14
- package/dist/auth/auth.service.js +0 -84
- package/dist/auth/dto/bot-login.dto.d.ts +0 -4
- package/dist/auth/dto/bot-login.dto.js +0 -2
- package/dist/auth/dto/password-reset.dto.d.ts +0 -11
- package/dist/auth/dto/password-reset.dto.js +0 -2
- package/dist/auth/dto/phone-login.dto.d.ts +0 -8
- package/dist/auth/dto/phone-login.dto.js +0 -2
- package/dist/auth/dto/phone-verification.dto.d.ts +0 -9
- package/dist/auth/dto/phone-verification.dto.js +0 -2
- package/dist/auth/jwt-auth.guard.d.ts +0 -13
- package/dist/auth/jwt-auth.guard.js +0 -143
- package/dist/auth/jwt.strategy.d.ts +0 -17
- package/dist/auth/jwt.strategy.js +0 -45
- package/dist/auth/phone-validation.service.d.ts +0 -7
- package/dist/auth/phone-validation.service.js +0 -50
- package/dist/auth/public.decorator.d.ts +0 -2
- package/dist/auth/public.decorator.js +0 -7
- package/dist/auth/rate-limit.service.d.ts +0 -16
- package/dist/auth/rate-limit.service.js +0 -84
- package/dist/auth/room-access.decorator.d.ts +0 -2
- package/dist/auth/room-access.decorator.js +0 -10
- package/dist/auth/scopes.decorator.d.ts +0 -2
- package/dist/auth/scopes.decorator.js +0 -7
- package/dist/auth/scopes.guard.d.ts +0 -9
- package/dist/auth/scopes.guard.js +0 -82
- package/dist/auth/session.guard.d.ts +0 -7
- package/dist/auth/session.guard.js +0 -55
- package/dist/auth/twilio.service.d.ts +0 -15
- package/dist/auth/twilio.service.js +0 -139
- package/dist/auth/users.service.d.ts +0 -24
- package/dist/auth/users.service.js +0 -149
- package/dist/common/errors.d.ts +0 -12
- package/dist/common/errors.js +0 -43
- package/dist/config/configuration.module.d.ts +0 -2
- package/dist/config/configuration.module.js +0 -20
- package/dist/config/configuration.service.d.ts +0 -26
- package/dist/config/configuration.service.js +0 -131
- package/dist/main.js +0 -15749
- package/dist/room/artifact-store.service.d.ts +0 -15
- package/dist/room/artifact-store.service.js +0 -226
- package/dist/room/artifact.d.ts +0 -6
- package/dist/room/artifact.js +0 -2
- package/dist/room/message.d.ts +0 -19
- package/dist/room/message.js +0 -70
- package/dist/room/published-rooms.service.d.ts +0 -12
- package/dist/room/published-rooms.service.js +0 -108
- package/dist/room/room-config.d.ts +0 -12
- package/dist/room/room-config.js +0 -2
- package/dist/room/room-factory.d.ts +0 -7
- package/dist/room/room-factory.js +0 -16
- package/dist/room/room-message.module.d.ts +0 -2
- package/dist/room/room-message.module.js +0 -21
- package/dist/room/room-message.service.d.ts +0 -8
- package/dist/room/room-message.service.js +0 -44
- package/dist/room/room-store.module.d.ts +0 -2
- package/dist/room/room-store.module.js +0 -25
- package/dist/room/room-store.service.d.ts +0 -38
- package/dist/room/room-store.service.js +0 -353
- package/dist/room/room.d.ts +0 -107
- package/dist/room/room.js +0 -833
- package/dist/sdk/briyah-config.d.ts +0 -13
- package/dist/sdk/briyah-config.js +0 -32
- package/dist/sdk/briyah.d.ts +0 -36
- package/dist/sdk/briyah.js +0 -122
- package/dist/sdk/index.d.ts +0 -6
- package/dist/sdk/index.js +0 -11
- package/dist/shared/shared.module.d.ts +0 -2
- package/dist/shared/shared.module.js +0 -46
- package/dist/story/story-message.service.d.ts +0 -10
- package/dist/story/story-message.service.js +0 -57
- package/dist/story/story-progress.service.d.ts +0 -8
- package/dist/story/story-progress.service.js +0 -44
- package/dist/story/story-store.module.d.ts +0 -2
- package/dist/story/story-store.module.js +0 -22
- package/dist/story/story-store.service.d.ts +0 -23
- package/dist/story/story-store.service.js +0 -392
- package/dist/story/story.service.d.ts +0 -94
- package/dist/story/story.service.js +0 -1797
|
@@ -1,21 +0,0 @@
|
|
|
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.TogetherAiModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const together_service_1 = require("./together.service");
|
|
12
|
-
let TogetherAiModule = class TogetherAiModule {
|
|
13
|
-
};
|
|
14
|
-
exports.TogetherAiModule = TogetherAiModule;
|
|
15
|
-
exports.TogetherAiModule = TogetherAiModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
imports: [],
|
|
18
|
-
providers: [together_service_1.TogetherAiService],
|
|
19
|
-
exports: [together_service_1.TogetherAiService],
|
|
20
|
-
})
|
|
21
|
-
], TogetherAiModule);
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { BaseAiService } from './base-ai.service';
|
|
2
|
-
import { Agent } from '../agent';
|
|
3
|
-
import { ModelInfo } from '../../shared/types/app.types';
|
|
4
|
-
export declare class TogetherAiService extends BaseAiService {
|
|
5
|
-
private togetherClient;
|
|
6
|
-
constructor();
|
|
7
|
-
getServiceName(): string;
|
|
8
|
-
supportsImageGeneration(): boolean;
|
|
9
|
-
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
10
|
-
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean, developer?: boolean): void;
|
|
11
|
-
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
12
|
-
private computeMessageCost;
|
|
13
|
-
private computeImageCost;
|
|
14
|
-
generateImage(agent: Agent, prompt: string, width: number, height: number, quality: string, referenceImageArtifactIds?: string[]): Promise<{
|
|
15
|
-
artifactId?: string;
|
|
16
|
-
error?: any;
|
|
17
|
-
}>;
|
|
18
|
-
}
|
|
@@ -1,285 +0,0 @@
|
|
|
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.TogetherAiService = void 0;
|
|
16
|
-
const common_1 = require("@nestjs/common");
|
|
17
|
-
const together_ai_1 = __importDefault(require("together-ai"));
|
|
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 TogetherAiService = class TogetherAiService extends base_ai_service_1.BaseAiService {
|
|
22
|
-
togetherClient;
|
|
23
|
-
constructor() {
|
|
24
|
-
super();
|
|
25
|
-
try {
|
|
26
|
-
const apiKey = process.env['TOGETHER_API_KEY'];
|
|
27
|
-
if (!apiKey) {
|
|
28
|
-
console.warn('TOGETHER_API_KEY environment variable not set. Together AI service will not be available.');
|
|
29
|
-
this._isAvailable = false;
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
this.togetherClient = new together_ai_1.default({
|
|
33
|
-
apiKey: apiKey,
|
|
34
|
-
timeout: 600000,
|
|
35
|
-
maxRetries: 3,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
console.error('Error initializing Together AI service:', error);
|
|
40
|
-
this._isAvailable = false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
getServiceName() {
|
|
44
|
-
return 'TogetherAI';
|
|
45
|
-
}
|
|
46
|
-
supportsImageGeneration() {
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
async fetchModelsFromApi() {
|
|
50
|
-
let models = await this.togetherClient.models.list();
|
|
51
|
-
return models
|
|
52
|
-
.filter((model) => model.type.includes('chat'))
|
|
53
|
-
.map((model) => {
|
|
54
|
-
return {
|
|
55
|
-
name: model.id,
|
|
56
|
-
description: model.id,
|
|
57
|
-
model: model.id,
|
|
58
|
-
service: 'TogetherAI',
|
|
59
|
-
};
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
addToConversationHistory(agent, message, fromSelf = false, developer = false) {
|
|
63
|
-
if (!agent.history) {
|
|
64
|
-
agent.history = [];
|
|
65
|
-
}
|
|
66
|
-
if (!message)
|
|
67
|
-
return;
|
|
68
|
-
let role = fromSelf ? 'assistant' : 'user';
|
|
69
|
-
if (developer)
|
|
70
|
-
role = 'system';
|
|
71
|
-
agent.history.push({
|
|
72
|
-
role: role,
|
|
73
|
-
content: message,
|
|
74
|
-
});
|
|
75
|
-
agent.trimHistoryIfNeeded();
|
|
76
|
-
}
|
|
77
|
-
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null, _cacheConfig = null, maxOutputChars = 0) {
|
|
78
|
-
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
79
|
-
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
80
|
-
if (!hasSufficientBalance) {
|
|
81
|
-
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
85
|
-
const instructions = promptInstructions || agent.systemInstruction;
|
|
86
|
-
const messages = [];
|
|
87
|
-
if (instructions)
|
|
88
|
-
messages.push({ role: 'system', content: instructions });
|
|
89
|
-
messages.push({ role: 'system', content: identity });
|
|
90
|
-
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
91
|
-
for (const file of agent.attachedFiles) {
|
|
92
|
-
if (file.serviceMetadata?.fileData) {
|
|
93
|
-
const fileData = new Uint8Array(file.serviceMetadata.fileData);
|
|
94
|
-
const textContent = Buffer.from(fileData).toString('utf-8');
|
|
95
|
-
messages.push({
|
|
96
|
-
role: 'user',
|
|
97
|
-
content: `Attached file "${file.fileName}":\n\n${textContent}`,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
messages.push(...agent.history);
|
|
103
|
-
if (prompt) {
|
|
104
|
-
messages.push({
|
|
105
|
-
role: 'user',
|
|
106
|
-
content: prompt,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
const responseFormat = jsonSchema
|
|
110
|
-
? {
|
|
111
|
-
type: 'json_schema',
|
|
112
|
-
json_schema: {
|
|
113
|
-
name: 'response',
|
|
114
|
-
schema: jsonSchema,
|
|
115
|
-
strict: true,
|
|
116
|
-
},
|
|
117
|
-
}
|
|
118
|
-
: undefined;
|
|
119
|
-
let responseText;
|
|
120
|
-
try {
|
|
121
|
-
const response = await this.togetherClient.chat.completions.create({
|
|
122
|
-
model: agent.modelName,
|
|
123
|
-
messages: messages,
|
|
124
|
-
temperature: 0,
|
|
125
|
-
response_format: responseFormat,
|
|
126
|
-
});
|
|
127
|
-
if (response.choices && response.choices[0]?.message?.content) {
|
|
128
|
-
responseText = response.choices[0].message.content;
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
responseText = '';
|
|
132
|
-
}
|
|
133
|
-
if (jsonSchema) {
|
|
134
|
-
responseText = this.trimToJson(responseText);
|
|
135
|
-
responseText = this.sanitizeJsonString(responseText);
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
responseText = this.trimResponseText(responseText);
|
|
139
|
-
}
|
|
140
|
-
if (response.usage) {
|
|
141
|
-
this.computeMessageCost(agent, response.usage);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
catch (error) {
|
|
145
|
-
console.error('Error calling Together AI:', error);
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
if (saveResponse) {
|
|
149
|
-
this.addToConversationHistory(agent, prompt, false);
|
|
150
|
-
this.addToConversationHistory(agent, responseText, true);
|
|
151
|
-
}
|
|
152
|
-
if (jsonSchema) {
|
|
153
|
-
return JSON.parse(responseText);
|
|
154
|
-
}
|
|
155
|
-
return responseText;
|
|
156
|
-
}
|
|
157
|
-
computeMessageCost(agent, usage) {
|
|
158
|
-
const promptTokens = usage.prompt_tokens || 0;
|
|
159
|
-
const completionTokens = usage.completion_tokens || 0;
|
|
160
|
-
let modelInfo = model_prices_1.default[agent.modelName];
|
|
161
|
-
if (!modelInfo && !agent.modelName.startsWith('together_ai/')) {
|
|
162
|
-
modelInfo = model_prices_1.default[`together_ai/${agent.modelName}`];
|
|
163
|
-
}
|
|
164
|
-
if (!modelInfo) {
|
|
165
|
-
console.error(`No price info found for model ${agent.modelName} or together_ai/${agent.modelName}`);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
const inputCost = promptTokens * modelInfo.input_cost_per_token;
|
|
169
|
-
const outputCost = completionTokens * modelInfo.output_cost_per_token;
|
|
170
|
-
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
171
|
-
const cost = inputCost + outputCost + markup;
|
|
172
|
-
agent.totalInputTokens += promptTokens;
|
|
173
|
-
agent.totalOutputTokens += completionTokens;
|
|
174
|
-
agent.totalCost += cost;
|
|
175
|
-
agent.totalMarkup += markup;
|
|
176
|
-
console.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
177
|
-
if (agent.balanceService && cost > 0) {
|
|
178
|
-
agent.balanceService.decrementBalance(cost, markup);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
computeImageCost(agent, usage) {
|
|
182
|
-
const modelInfo = model_prices_1.default[agent.modelName];
|
|
183
|
-
if (!modelInfo) {
|
|
184
|
-
console.error(`No price info found for model ${agent.modelName}`);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
const inputImageTokens = usage.input_tokens_details?.image_tokens || 0;
|
|
188
|
-
const inputTextTokens = usage.input_tokens_details?.text_tokens || 0;
|
|
189
|
-
const outputImageTokens = usage.output_tokens_details?.image_tokens || 0;
|
|
190
|
-
const outputTextTokens = usage.output_tokens_details?.text_tokens || 0;
|
|
191
|
-
const totalInputTokens = usage.input_tokens || 0;
|
|
192
|
-
const totalOutputTokens = usage.output_tokens || 0;
|
|
193
|
-
agent.totalInputTokens += totalInputTokens;
|
|
194
|
-
agent.totalOutputTokens += totalOutputTokens;
|
|
195
|
-
let inputCost = 0;
|
|
196
|
-
if (inputImageTokens > 0) {
|
|
197
|
-
const cachedImageTokens = usage.input_tokens_details?.cached_image_tokens || 0;
|
|
198
|
-
const uncachedImageTokens = inputImageTokens - cachedImageTokens;
|
|
199
|
-
inputCost += uncachedImageTokens * (modelInfo.input_cost_per_image_token || 0);
|
|
200
|
-
if (cachedImageTokens > 0) {
|
|
201
|
-
inputCost +=
|
|
202
|
-
cachedImageTokens *
|
|
203
|
-
(modelInfo.cache_read_input_image_token_cost ||
|
|
204
|
-
modelInfo.input_cost_per_image_token * 0.5);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (inputTextTokens > 0) {
|
|
208
|
-
const cachedTextTokens = usage.input_tokens_details?.cached_text_tokens || 0;
|
|
209
|
-
const uncachedTextTokens = inputTextTokens - cachedTextTokens;
|
|
210
|
-
inputCost += uncachedTextTokens * (modelInfo.input_cost_per_token || 0);
|
|
211
|
-
if (cachedTextTokens > 0) {
|
|
212
|
-
inputCost +=
|
|
213
|
-
cachedTextTokens *
|
|
214
|
-
(modelInfo.cache_read_input_text_token_cost ||
|
|
215
|
-
modelInfo.input_cost_per_token * 0.5);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
let outputCost = 0;
|
|
219
|
-
if (outputImageTokens > 0) {
|
|
220
|
-
outputCost += outputImageTokens * (modelInfo.output_cost_per_image_token || 0);
|
|
221
|
-
}
|
|
222
|
-
if (outputTextTokens > 0) {
|
|
223
|
-
outputCost += outputTextTokens * (modelInfo.output_cost_per_token || 0);
|
|
224
|
-
}
|
|
225
|
-
const markup = outputCost * 0;
|
|
226
|
-
const cost = inputCost + outputCost + markup;
|
|
227
|
-
agent.totalCost += cost;
|
|
228
|
-
agent.totalMarkup += markup;
|
|
229
|
-
console.log(`Cost for image generation: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
230
|
-
if (agent.balanceService && cost > 0) {
|
|
231
|
-
agent.balanceService.decrementBalance(cost, markup);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
async generateImage(agent, prompt, width, height, quality, referenceImageArtifactIds) {
|
|
235
|
-
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
236
|
-
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
237
|
-
if (!hasSufficientBalance) {
|
|
238
|
-
throw new errors_1.InsufficientBalanceError('Insufficient balance for image generation. Please add funds to continue.');
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
try {
|
|
242
|
-
let imageResponse;
|
|
243
|
-
if (referenceImageArtifactIds && referenceImageArtifactIds.length > 0) {
|
|
244
|
-
const referenceImages = await this.loadReferenceImages(agent, referenceImageArtifactIds);
|
|
245
|
-
imageResponse = await this.togetherClient.images.generate({
|
|
246
|
-
reference_image: referenceImages.length === 1 ? referenceImages[0] : referenceImages,
|
|
247
|
-
model: agent.modelName,
|
|
248
|
-
prompt: prompt,
|
|
249
|
-
height: height,
|
|
250
|
-
width: width,
|
|
251
|
-
n: 1,
|
|
252
|
-
disable_safety_checker: true,
|
|
253
|
-
response_format: 'base64',
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
imageResponse = await this.togetherClient.images.generate({
|
|
258
|
-
model: agent.modelName,
|
|
259
|
-
prompt: prompt,
|
|
260
|
-
height: height,
|
|
261
|
-
width: width,
|
|
262
|
-
n: 1,
|
|
263
|
-
disable_safety_checker: true,
|
|
264
|
-
response_format: 'base64',
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
if (imageResponse.usage) {
|
|
268
|
-
this.computeImageCost(agent, imageResponse.usage);
|
|
269
|
-
}
|
|
270
|
-
const base64Data = imageResponse.data[0].b64_json;
|
|
271
|
-
const artifactId = await this.saveImageAsArtifact(agent, base64Data, prompt);
|
|
272
|
-
return { artifactId };
|
|
273
|
-
}
|
|
274
|
-
catch (error) {
|
|
275
|
-
console.error('Error generating image:', error.error?.message || error.message);
|
|
276
|
-
console.log(prompt);
|
|
277
|
-
return { error };
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
exports.TogetherAiService = TogetherAiService;
|
|
282
|
-
exports.TogetherAiService = TogetherAiService = __decorate([
|
|
283
|
-
(0, common_1.Injectable)(),
|
|
284
|
-
__metadata("design:paramtypes", [])
|
|
285
|
-
], TogetherAiService);
|
|
@@ -1,21 +0,0 @@
|
|
|
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.VertexAiModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const vertexai_service_1 = require("./vertexai.service");
|
|
12
|
-
let VertexAiModule = class VertexAiModule {
|
|
13
|
-
};
|
|
14
|
-
exports.VertexAiModule = VertexAiModule;
|
|
15
|
-
exports.VertexAiModule = VertexAiModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
imports: [],
|
|
18
|
-
providers: [vertexai_service_1.VertexAiService],
|
|
19
|
-
exports: [vertexai_service_1.VertexAiService],
|
|
20
|
-
})
|
|
21
|
-
], VertexAiModule);
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { VertexAI } from '@google-cloud/vertexai';
|
|
2
|
-
import { Agent } from '../agent';
|
|
3
|
-
import { BaseAiService } from './base-ai.service';
|
|
4
|
-
import { ModelInfo } from '../../shared/types/app.types';
|
|
5
|
-
export declare class VertexAiService extends BaseAiService {
|
|
6
|
-
vertexAI: VertexAI;
|
|
7
|
-
constructor();
|
|
8
|
-
getServiceName(): string;
|
|
9
|
-
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
10
|
-
uploadFile(fileContents: Uint8Array): Promise<string>;
|
|
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
|
-
private computeMessageCost;
|
|
14
|
-
attachDocument(agent: Agent, fileName: string, fileData: Uint8Array): Promise<string>;
|
|
15
|
-
formatMessage(message: any): string;
|
|
16
|
-
finishAgent(agent: Agent): Promise<void>;
|
|
17
|
-
}
|