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,271 +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.GoogleAiService = void 0;
|
|
16
|
-
const common_1 = require("@nestjs/common");
|
|
17
|
-
const genai_1 = require("@google/genai");
|
|
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 GoogleAiService = class GoogleAiService extends base_ai_service_1.BaseAiService {
|
|
22
|
-
googleAI = null;
|
|
23
|
-
constructor() {
|
|
24
|
-
super();
|
|
25
|
-
try {
|
|
26
|
-
const apiKey = process.env['GOOGLE_GENAI_API_KEY'];
|
|
27
|
-
if (!apiKey) {
|
|
28
|
-
console.warn('GOOGLE_GENAI_API_KEY environment variable not set. Google AI service will not be available.');
|
|
29
|
-
this._isAvailable = false;
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
this.googleAI = new genai_1.GoogleGenAI({ apiKey });
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
console.error('Error initializing Google AI service:', error);
|
|
36
|
-
this._isAvailable = false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
getServiceName() {
|
|
40
|
-
return 'GoogleAI';
|
|
41
|
-
}
|
|
42
|
-
supportsImageGeneration() {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
supportsPromptCaching() {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
async fetchModelsFromApi() {
|
|
49
|
-
try {
|
|
50
|
-
const response = await this.googleAI.models.list();
|
|
51
|
-
return response.page.map((model) => {
|
|
52
|
-
const name = model.name.replace('models/', '');
|
|
53
|
-
return {
|
|
54
|
-
name: name,
|
|
55
|
-
description: name,
|
|
56
|
-
model: name,
|
|
57
|
-
service: 'GoogleAI',
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
console.warn('Could not fetch models from Google AI API, using fallback list:', error.message);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
addToConversationHistory(agent, message, fromSelf = false) {
|
|
66
|
-
if (!agent.history) {
|
|
67
|
-
agent.history = [];
|
|
68
|
-
}
|
|
69
|
-
if (!message)
|
|
70
|
-
return;
|
|
71
|
-
agent.history.push({
|
|
72
|
-
role: fromSelf ? 'model' : 'user',
|
|
73
|
-
parts: [{ text: 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 messages = [{ role: 'user', parts: [{ text: identity }] }];
|
|
86
|
-
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
87
|
-
for (const file of agent.attachedFiles) {
|
|
88
|
-
if (file.serviceMetadata?.fileData) {
|
|
89
|
-
const fileData = new Uint8Array(file.serviceMetadata.fileData);
|
|
90
|
-
const textContent = Buffer.from(fileData).toString('utf-8');
|
|
91
|
-
messages.push({
|
|
92
|
-
role: 'user',
|
|
93
|
-
parts: [{ text: `Attached file "${file.fileName}":\n\n${textContent}` }],
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
messages.push(...agent.history);
|
|
99
|
-
if (prompt) {
|
|
100
|
-
messages.push({
|
|
101
|
-
role: 'user',
|
|
102
|
-
parts: [{ text: prompt }],
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
const generationConfig = {
|
|
106
|
-
maxOutputTokens: 8192,
|
|
107
|
-
temperature: 0,
|
|
108
|
-
};
|
|
109
|
-
if (jsonSchema) {
|
|
110
|
-
generationConfig.responseMimeType = 'application/json';
|
|
111
|
-
const { name: _name, description: _description, ...schemaOnly } = jsonSchema;
|
|
112
|
-
generationConfig.responseSchema = schemaOnly;
|
|
113
|
-
}
|
|
114
|
-
const instructions = promptInstructions || agent.systemInstruction;
|
|
115
|
-
let responseText;
|
|
116
|
-
try {
|
|
117
|
-
const response = await this.googleAI.models.generateContent({
|
|
118
|
-
model: agent.modelName,
|
|
119
|
-
contents: messages,
|
|
120
|
-
config: {
|
|
121
|
-
systemInstruction: instructions,
|
|
122
|
-
...generationConfig,
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
if (response.text) {
|
|
126
|
-
responseText = response.text;
|
|
127
|
-
}
|
|
128
|
-
else if (response.candidates?.[0]?.content?.parts?.[0]?.text) {
|
|
129
|
-
responseText = response.candidates[0].content.parts[0].text;
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
responseText = '';
|
|
133
|
-
}
|
|
134
|
-
if (jsonSchema) {
|
|
135
|
-
responseText = this.trimToJson(responseText);
|
|
136
|
-
responseText = this.sanitizeJsonString(responseText);
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
responseText = this.trimResponseText(responseText);
|
|
140
|
-
}
|
|
141
|
-
if (response.usageMetadata) {
|
|
142
|
-
this.computeMessageCost(agent, response.usageMetadata);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
console.error('Error calling Google AI:', error);
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
if (saveResponse) {
|
|
150
|
-
this.addToConversationHistory(agent, prompt, false);
|
|
151
|
-
this.addToConversationHistory(agent, responseText, true);
|
|
152
|
-
}
|
|
153
|
-
if (jsonSchema) {
|
|
154
|
-
return JSON.parse(responseText);
|
|
155
|
-
}
|
|
156
|
-
return responseText;
|
|
157
|
-
}
|
|
158
|
-
async generateImage(agent, prompt, width, height, quality, _referenceImageArtifactIds) {
|
|
159
|
-
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
160
|
-
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
161
|
-
if (!hasSufficientBalance) {
|
|
162
|
-
throw new errors_1.InsufficientBalanceError('Insufficient balance for image generation. Please add funds to continue.');
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
const modelName = agent.modelName;
|
|
167
|
-
let aspectRatio = '1:1';
|
|
168
|
-
if (width && height) {
|
|
169
|
-
const ratio = width / height;
|
|
170
|
-
if (ratio > 1.4)
|
|
171
|
-
aspectRatio = '16:9';
|
|
172
|
-
else if (ratio > 1.2)
|
|
173
|
-
aspectRatio = '3:2';
|
|
174
|
-
else if (ratio < 0.7)
|
|
175
|
-
aspectRatio = '9:16';
|
|
176
|
-
else if (ratio < 0.8)
|
|
177
|
-
aspectRatio = '2:3';
|
|
178
|
-
}
|
|
179
|
-
const config = {
|
|
180
|
-
numberOfImages: 1,
|
|
181
|
-
aspectRatio: aspectRatio,
|
|
182
|
-
};
|
|
183
|
-
if (quality && quality !== 'auto') {
|
|
184
|
-
config.outputOptions = {
|
|
185
|
-
compressionQuality: quality === 'high' ? 90 : quality === 'medium' ? 70 : 50,
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
const response = await this.googleAI.models.generateImages({
|
|
189
|
-
model: modelName,
|
|
190
|
-
prompt: prompt,
|
|
191
|
-
config: config,
|
|
192
|
-
});
|
|
193
|
-
let base64Data;
|
|
194
|
-
if (response.generatedImages?.[0]?.image?.imageBytes) {
|
|
195
|
-
const imageBytes = response.generatedImages[0].image.imageBytes;
|
|
196
|
-
base64Data = Buffer.isBuffer(imageBytes)
|
|
197
|
-
? imageBytes.toString('base64')
|
|
198
|
-
: imageBytes;
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
throw new Error('No image data in response');
|
|
202
|
-
}
|
|
203
|
-
this.computeImageCost(agent, modelName);
|
|
204
|
-
const artifactId = await this.saveImageAsArtifact(agent, base64Data, prompt);
|
|
205
|
-
return { artifactId };
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
console.error('Error generating image with Google AI:', error);
|
|
209
|
-
return { error };
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
computeMessageCost(agent, usage) {
|
|
213
|
-
const inputTokens = usage.promptTokenCount || 0;
|
|
214
|
-
const outputTokens = usage.candidatesTokenCount || 0;
|
|
215
|
-
const modelInfo = model_prices_1.default[agent.modelName];
|
|
216
|
-
if (!modelInfo) {
|
|
217
|
-
console.error(`No price info found for model ${agent.modelName}`);
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const inputCost = inputTokens * modelInfo.input_cost_per_token;
|
|
221
|
-
const outputCost = outputTokens * modelInfo.output_cost_per_token;
|
|
222
|
-
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
223
|
-
const cost = inputCost + outputCost + markup;
|
|
224
|
-
agent.totalInputTokens += inputTokens;
|
|
225
|
-
agent.totalOutputTokens += outputTokens;
|
|
226
|
-
agent.totalCost += cost;
|
|
227
|
-
agent.totalMarkup += markup;
|
|
228
|
-
console.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
229
|
-
if (agent.balanceService && cost > 0) {
|
|
230
|
-
agent.balanceService.decrementBalance(cost, markup);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
computeImageCost(agent, modelName) {
|
|
234
|
-
const modelInfo = model_prices_1.default[modelName];
|
|
235
|
-
if (!modelInfo || !modelInfo.output_cost_per_image) {
|
|
236
|
-
console.error(`No image price info found for model ${modelName}`);
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
const outputCost = modelInfo.output_cost_per_image;
|
|
240
|
-
const markup = outputCost * 0;
|
|
241
|
-
const cost = outputCost + markup;
|
|
242
|
-
agent.totalCost += cost;
|
|
243
|
-
agent.totalMarkup += markup;
|
|
244
|
-
console.log(`Cost for image generation: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
245
|
-
if (agent.balanceService && cost > 0) {
|
|
246
|
-
agent.balanceService.decrementBalance(cost, markup);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
formatMessage(message) {
|
|
250
|
-
if (!message) {
|
|
251
|
-
return 'No message';
|
|
252
|
-
}
|
|
253
|
-
if (message.role && message.parts) {
|
|
254
|
-
const parts = message.parts
|
|
255
|
-
.map((part) => {
|
|
256
|
-
if (part.text) {
|
|
257
|
-
return part.text;
|
|
258
|
-
}
|
|
259
|
-
return JSON.stringify(part);
|
|
260
|
-
})
|
|
261
|
-
.join('\n');
|
|
262
|
-
return `${message.role}: ${parts}`;
|
|
263
|
-
}
|
|
264
|
-
return super.formatMessage(message);
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
exports.GoogleAiService = GoogleAiService;
|
|
268
|
-
exports.GoogleAiService = GoogleAiService = __decorate([
|
|
269
|
-
(0, common_1.Injectable)(),
|
|
270
|
-
__metadata("design:paramtypes", [])
|
|
271
|
-
], GoogleAiService);
|
|
@@ -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.GrokAiModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const grok_service_1 = require("./grok.service");
|
|
12
|
-
let GrokAiModule = class GrokAiModule {
|
|
13
|
-
};
|
|
14
|
-
exports.GrokAiModule = GrokAiModule;
|
|
15
|
-
exports.GrokAiModule = GrokAiModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
imports: [],
|
|
18
|
-
providers: [grok_service_1.GrokAiService],
|
|
19
|
-
exports: [grok_service_1.GrokAiService],
|
|
20
|
-
})
|
|
21
|
-
], GrokAiModule);
|
|
@@ -1,14 +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 GrokAiService extends BaseAiService {
|
|
5
|
-
private openai;
|
|
6
|
-
constructor();
|
|
7
|
-
getServiceName(): string;
|
|
8
|
-
fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
9
|
-
attachDocument(agent: Agent, fileName: string, fileData: Uint8Array): Promise<string>;
|
|
10
|
-
finishAgent(agent: Agent): Promise<void>;
|
|
11
|
-
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
12
|
-
addToConversationHistory(agent: Agent, message: string, fromSelf?: boolean, developer?: boolean): void;
|
|
13
|
-
private computeMessageCost;
|
|
14
|
-
}
|
|
@@ -1,263 +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.GrokAiService = void 0;
|
|
16
|
-
const common_1 = require("@nestjs/common");
|
|
17
|
-
const openai_1 = require("openai");
|
|
18
|
-
const openai_2 = require("openai");
|
|
19
|
-
const base_ai_service_1 = require("./base-ai.service");
|
|
20
|
-
const model_prices_1 = __importDefault(require("../model_prices"));
|
|
21
|
-
const errors_1 = require("../../common/errors");
|
|
22
|
-
let GrokAiService = class GrokAiService extends base_ai_service_1.BaseAiService {
|
|
23
|
-
openai;
|
|
24
|
-
constructor() {
|
|
25
|
-
super();
|
|
26
|
-
try {
|
|
27
|
-
const apiKey = process.env['XAI_API_KEY'];
|
|
28
|
-
if (!apiKey) {
|
|
29
|
-
console.warn('XAI_API_KEY environment variable not set. Grok service will not be available.');
|
|
30
|
-
this._isAvailable = false;
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const options = {
|
|
34
|
-
apiKey: apiKey,
|
|
35
|
-
baseURL: 'https://api.x.ai/v1',
|
|
36
|
-
timeout: 600000,
|
|
37
|
-
maxRetries: 3,
|
|
38
|
-
};
|
|
39
|
-
this.openai = new openai_1.OpenAI(options);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
console.error('Error initializing Grok service:', error);
|
|
43
|
-
this._isAvailable = false;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
getServiceName() {
|
|
47
|
-
return 'Grok';
|
|
48
|
-
}
|
|
49
|
-
async fetchModelsFromApi() {
|
|
50
|
-
const models = await this.openai.models.list();
|
|
51
|
-
return models.data.map((model) => {
|
|
52
|
-
let name = model.id;
|
|
53
|
-
return {
|
|
54
|
-
name: name,
|
|
55
|
-
description: name,
|
|
56
|
-
model: name,
|
|
57
|
-
service: 'Grok',
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
async attachDocument(agent, fileName, fileData) {
|
|
62
|
-
if (agent.fullContextFiles) {
|
|
63
|
-
return super.attachDocument(agent, fileName, fileData);
|
|
64
|
-
}
|
|
65
|
-
const existingMetadata = agent.attachedFiles.find((f) => f.fileName === fileName && f.serviceMetadata?.serviceName === this.getServiceName());
|
|
66
|
-
if (existingMetadata?.serviceMetadata?.resourceId) {
|
|
67
|
-
const fileId = existingMetadata.serviceMetadata.resourceId;
|
|
68
|
-
return `File found with Grok file id: ${fileId}`;
|
|
69
|
-
}
|
|
70
|
-
let fileInput = fileData;
|
|
71
|
-
let fileToUpload = await (0, openai_2.toFile)(fileInput, fileName);
|
|
72
|
-
const fileObject = await this.openai.files.create({
|
|
73
|
-
file: fileToUpload,
|
|
74
|
-
purpose: 'assistants',
|
|
75
|
-
});
|
|
76
|
-
const additionalMetadata = {
|
|
77
|
-
serviceMetadata: {
|
|
78
|
-
serviceName: this.getServiceName(),
|
|
79
|
-
resourceId: fileObject.id,
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
const fileMetadataIndex = agent.attachedFiles.findIndex((f) => f.fileName === fileName);
|
|
83
|
-
if (fileMetadataIndex >= 0) {
|
|
84
|
-
agent.attachedFiles[fileMetadataIndex] = {
|
|
85
|
-
...agent.attachedFiles[fileMetadataIndex],
|
|
86
|
-
...additionalMetadata,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return `Uploaded file to Grok: ${fileName}`;
|
|
90
|
-
}
|
|
91
|
-
async finishAgent(agent) {
|
|
92
|
-
if (!agent)
|
|
93
|
-
return;
|
|
94
|
-
for (const metadata of agent.attachedFiles) {
|
|
95
|
-
if (metadata.serviceMetadata?.serviceName === this.getServiceName() &&
|
|
96
|
-
metadata.serviceMetadata?.resourceId) {
|
|
97
|
-
try {
|
|
98
|
-
await this.openai.files.delete(metadata.serviceMetadata.resourceId);
|
|
99
|
-
console.log(`Deleted Grok file ${metadata.serviceMetadata.resourceId} for agent ${agent.id}`);
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
console.error(`Error deleting Grok file ${metadata.serviceMetadata.resourceId}:`, error);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async textPrompt(agent, prompt, jsonSchema = null, saveResponse = true, promptInstructions = null, _cacheConfig = null, maxOutputChars = 0) {
|
|
108
|
-
if (agent.balanceService && !agent.disableBalanceCheck) {
|
|
109
|
-
const hasSufficientBalance = agent.balanceService.hasSufficientBalance();
|
|
110
|
-
if (!hasSufficientBalance) {
|
|
111
|
-
throw new errors_1.InsufficientBalanceError('Insufficient balance. Please add funds to continue using AI services.');
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const identity = `Your agent identity is ${agent.agentName}: ${agent.description}`;
|
|
115
|
-
const instructions = promptInstructions || agent.systemInstruction;
|
|
116
|
-
const messages = [];
|
|
117
|
-
if (instructions)
|
|
118
|
-
messages.push({
|
|
119
|
-
role: 'system',
|
|
120
|
-
content: [{ type: 'text', text: instructions }],
|
|
121
|
-
});
|
|
122
|
-
messages.push({
|
|
123
|
-
role: 'system',
|
|
124
|
-
content: [{ type: 'text', text: identity }],
|
|
125
|
-
});
|
|
126
|
-
if (agent.attachedFiles && agent.attachedFiles.length > 0) {
|
|
127
|
-
for (const file of agent.attachedFiles) {
|
|
128
|
-
if (file.serviceMetadata?.serviceName === this.getServiceName() &&
|
|
129
|
-
file.serviceMetadata?.resourceId) {
|
|
130
|
-
messages.push({
|
|
131
|
-
role: 'user',
|
|
132
|
-
content: [
|
|
133
|
-
{
|
|
134
|
-
type: 'text',
|
|
135
|
-
text: `File available for document_search: "${file.fileName}" (file_id: ${file.serviceMetadata.resourceId})`,
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
type: 'file',
|
|
139
|
-
file: { file_id: file.serviceMetadata.resourceId },
|
|
140
|
-
},
|
|
141
|
-
],
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
const fileData = new Uint8Array(file.serviceMetadata.fileData);
|
|
146
|
-
const textContent = Buffer.from(fileData).toString('utf-8');
|
|
147
|
-
messages.push({
|
|
148
|
-
role: 'user',
|
|
149
|
-
content: [
|
|
150
|
-
{ type: 'text', text: textContent },
|
|
151
|
-
],
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (agent.attachedArtifacts && agent.attachedArtifacts.length > 0) {
|
|
157
|
-
for (const artifactId of agent.attachedArtifacts) {
|
|
158
|
-
const content = agent.artifactService.getArtifact(artifactId);
|
|
159
|
-
if (content && typeof content === 'string') {
|
|
160
|
-
const metadata = agent.artifactService.getArtifactMetadata(artifactId);
|
|
161
|
-
const fileName = metadata ? `${metadata.name}.md` : 'artifact.md';
|
|
162
|
-
messages.push({
|
|
163
|
-
role: 'user',
|
|
164
|
-
content: [
|
|
165
|
-
{
|
|
166
|
-
type: 'text',
|
|
167
|
-
text: `Attached document: ${fileName}\n\n${content}`,
|
|
168
|
-
},
|
|
169
|
-
],
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
messages.push(...agent.history);
|
|
175
|
-
if (prompt) {
|
|
176
|
-
messages.push({
|
|
177
|
-
role: 'user',
|
|
178
|
-
content: [{ type: 'text', text: prompt }],
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
let responseText;
|
|
182
|
-
try {
|
|
183
|
-
let response = await this.openai.chat.completions.create({
|
|
184
|
-
model: agent.modelName,
|
|
185
|
-
messages: messages,
|
|
186
|
-
store: false,
|
|
187
|
-
});
|
|
188
|
-
for (const choice of response.choices) {
|
|
189
|
-
if (choice.message.content)
|
|
190
|
-
responseText = choice.message.content;
|
|
191
|
-
}
|
|
192
|
-
if (jsonSchema) {
|
|
193
|
-
responseText = this.trimToJson(responseText);
|
|
194
|
-
responseText = this.sanitizeJsonString(responseText);
|
|
195
|
-
}
|
|
196
|
-
if (response.usage) {
|
|
197
|
-
this.computeMessageCost(agent, response.usage);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
catch (error) {
|
|
201
|
-
console.error(error);
|
|
202
|
-
throw error;
|
|
203
|
-
}
|
|
204
|
-
if (saveResponse) {
|
|
205
|
-
this.addToConversationHistory(agent, prompt, false);
|
|
206
|
-
this.addToConversationHistory(agent, responseText, true);
|
|
207
|
-
}
|
|
208
|
-
if (jsonSchema)
|
|
209
|
-
return JSON.parse(responseText);
|
|
210
|
-
return responseText;
|
|
211
|
-
}
|
|
212
|
-
addToConversationHistory(agent, message, fromSelf = false, developer = false) {
|
|
213
|
-
if (!agent.history) {
|
|
214
|
-
agent.history = [];
|
|
215
|
-
}
|
|
216
|
-
if (!message)
|
|
217
|
-
return;
|
|
218
|
-
let role = fromSelf ? 'assistant' : 'user';
|
|
219
|
-
if (developer)
|
|
220
|
-
role = 'system';
|
|
221
|
-
agent.history.push({
|
|
222
|
-
role: role,
|
|
223
|
-
content: message,
|
|
224
|
-
});
|
|
225
|
-
agent.trimHistoryIfNeeded();
|
|
226
|
-
}
|
|
227
|
-
computeMessageCost(agent, usage) {
|
|
228
|
-
const totalPromptTokens = usage.prompt_tokens || 0;
|
|
229
|
-
const cachedTokens = usage.prompt_tokens_details?.cached_tokens || 0;
|
|
230
|
-
const uncachedPromptTokens = totalPromptTokens - cachedTokens;
|
|
231
|
-
const completionTokens = usage.completion_tokens || 0;
|
|
232
|
-
let modelInfo = model_prices_1.default[agent.modelName];
|
|
233
|
-
if (!modelInfo && !agent.modelName.startsWith('xai/')) {
|
|
234
|
-
modelInfo = model_prices_1.default[`xai/${agent.modelName}`];
|
|
235
|
-
}
|
|
236
|
-
if (!modelInfo) {
|
|
237
|
-
console.error(`No price info found for model ${agent.modelName} or xai/${agent.modelName}`);
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
const uncachedCost = uncachedPromptTokens * modelInfo.input_cost_per_token;
|
|
241
|
-
const cachedCost = cachedTokens *
|
|
242
|
-
(modelInfo.cache_read_input_token_cost || modelInfo.input_cost_per_token * 0.25);
|
|
243
|
-
const outputCost = completionTokens * modelInfo.output_cost_per_token;
|
|
244
|
-
const markup = agent.disableMarkup ? 0 : outputCost * agent.markupRate;
|
|
245
|
-
const cost = uncachedCost + cachedCost + outputCost + markup;
|
|
246
|
-
agent.totalInputTokens += totalPromptTokens;
|
|
247
|
-
agent.totalOutputTokens += completionTokens;
|
|
248
|
-
agent.totalCost += cost;
|
|
249
|
-
agent.totalMarkup += markup;
|
|
250
|
-
console.log(`Cost for ${agent.agentName} message: ${cost} (markup: ${markup.toFixed(4)})`);
|
|
251
|
-
if (cachedTokens > 0) {
|
|
252
|
-
console.log(`Cache metrics for ${agent.agentName}: cached=${cachedTokens}, uncached=${uncachedPromptTokens}`);
|
|
253
|
-
}
|
|
254
|
-
if (agent.balanceService && cost > 0) {
|
|
255
|
-
agent.balanceService.decrementBalance(cost, markup);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
};
|
|
259
|
-
exports.GrokAiService = GrokAiService;
|
|
260
|
-
exports.GrokAiService = GrokAiService = __decorate([
|
|
261
|
-
(0, common_1.Injectable)(),
|
|
262
|
-
__metadata("design:paramtypes", [])
|
|
263
|
-
], GrokAiService);
|
|
@@ -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.MockAiModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const mock_service_1 = require("./mock.service");
|
|
12
|
-
let MockAiModule = class MockAiModule {
|
|
13
|
-
};
|
|
14
|
-
exports.MockAiModule = MockAiModule;
|
|
15
|
-
exports.MockAiModule = MockAiModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
imports: [],
|
|
18
|
-
providers: [mock_service_1.MockAiService],
|
|
19
|
-
exports: [mock_service_1.MockAiService],
|
|
20
|
-
})
|
|
21
|
-
], MockAiModule);
|
|
@@ -1,13 +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 MockAiService extends BaseAiService {
|
|
5
|
-
constructor();
|
|
6
|
-
getServiceName(): string;
|
|
7
|
-
protected fetchModelsFromApi(): Promise<ModelInfo[]>;
|
|
8
|
-
preparedPrompt(agent: Agent, promptFileName: string, variables: any, saveResponse?: boolean, _cacheConfig?: any): Promise<any>;
|
|
9
|
-
getMockFile(promptFileName: string, prompt: string, variables: any): string;
|
|
10
|
-
instructedPrompt(agent: Agent, prompt: string, promptFileName: string, variables?: any, saveResponse?: boolean, cacheConfig?: any): Promise<any>;
|
|
11
|
-
textPrompt(agent: Agent, prompt: string, jsonSchema?: any, saveResponse?: boolean, _promptInstructions?: string, _cacheConfig?: any, maxOutputChars?: number): Promise<any>;
|
|
12
|
-
private trackMockCosts;
|
|
13
|
-
}
|