|
1 | 1 | const mongoose = require('mongoose');
|
2 |
| -const { isEnabled } = require('../server/utils/handleText'); |
| 2 | +const { isEnabled } = require('~/server/utils/handleText'); |
3 | 3 | const transactionSchema = require('./schema/transaction');
|
4 | 4 | const { getMultiplier, getCacheMultiplier } = require('./tx');
|
5 | 5 | const { logger } = require('~/config');
|
@@ -76,7 +76,7 @@ transactionSchema.statics.createStructured = async function (txData) {
|
76 | 76 | await transaction.save();
|
77 | 77 |
|
78 | 78 | if (!isEnabled(process.env.CHECK_BALANCE)) {
|
79 |
| - return transaction; |
| 79 | + return; |
80 | 80 | }
|
81 | 81 |
|
82 | 82 | let balance = await Balance.findOne({ user: transaction.user }).lean();
|
@@ -122,28 +122,33 @@ transactionSchema.methods.calculateStructuredTokenValue = function () {
|
122 | 122 | read: readMultiplier,
|
123 | 123 | };
|
124 | 124 |
|
125 |
| - const totalTokens = (this.inputTokens || 0) + (this.writeTokens || 0) + (this.readTokens || 0); |
| 125 | + const totalPromptTokens = |
| 126 | + Math.abs(this.inputTokens || 0) + |
| 127 | + Math.abs(this.writeTokens || 0) + |
| 128 | + Math.abs(this.readTokens || 0); |
126 | 129 |
|
127 |
| - if (totalTokens > 0) { |
| 130 | + if (totalPromptTokens > 0) { |
128 | 131 | this.rate =
|
129 |
| - (inputMultiplier * (this.inputTokens || 0) + |
130 |
| - writeMultiplier * (this.writeTokens || 0) + |
131 |
| - readMultiplier * (this.readTokens || 0)) / |
132 |
| - totalTokens; |
| 132 | + (Math.abs(inputMultiplier * (this.inputTokens || 0)) + |
| 133 | + Math.abs(writeMultiplier * (this.writeTokens || 0)) + |
| 134 | + Math.abs(readMultiplier * (this.readTokens || 0))) / |
| 135 | + totalPromptTokens; |
133 | 136 | } else {
|
134 |
| - this.rate = inputMultiplier; // Default to input rate if no tokens |
| 137 | + this.rate = Math.abs(inputMultiplier); // Default to input rate if no tokens |
135 | 138 | }
|
136 | 139 |
|
137 |
| - this.tokenValue = |
138 |
| - this.inputTokens * inputMultiplier + |
139 |
| - (this.writeTokens || 0) * writeMultiplier + |
140 |
| - (this.readTokens || 0) * readMultiplier; |
141 |
| - } else { |
142 |
| - const multiplier = Math.abs( |
143 |
| - getMultiplier({ tokenType: this.tokenType, model, endpointTokenConfig }), |
| 140 | + this.tokenValue = -( |
| 141 | + Math.abs(this.inputTokens || 0) * inputMultiplier + |
| 142 | + Math.abs(this.writeTokens || 0) * writeMultiplier + |
| 143 | + Math.abs(this.readTokens || 0) * readMultiplier |
144 | 144 | );
|
145 |
| - this.rate = multiplier; |
146 |
| - this.tokenValue = this.rawAmount * multiplier; |
| 145 | + |
| 146 | + this.rawAmount = -totalPromptTokens; |
| 147 | + } else if (this.tokenType === 'completion') { |
| 148 | + const multiplier = getMultiplier({ tokenType: this.tokenType, model, endpointTokenConfig }); |
| 149 | + this.rate = Math.abs(multiplier); |
| 150 | + this.tokenValue = -Math.abs(this.rawAmount) * multiplier; |
| 151 | + this.rawAmount = -Math.abs(this.rawAmount); |
147 | 152 | }
|
148 | 153 |
|
149 | 154 | if (this.context && this.tokenType === 'completion' && this.context === 'incomplete') {
|
|
0 commit comments