Spaces:
Sleeping
Sleeping
File size: 2,760 Bytes
4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import { query } from "./client";
import { ensureUserTableExists } from "./users";
import { ModelPrice, updateModelPrice } from "./client";
async function ensureModelPricesTableExists() {
const defaultInputPrice = parseFloat(
process.env.DEFAULT_MODEL_INPUT_PRICE || "60"
);
const defaultOutputPrice = parseFloat(
process.env.DEFAULT_MODEL_OUTPUT_PRICE || "60"
);
const defaultPerMsgPrice = parseFloat(
process.env.DEFAULT_MODEL_PER_MSG_PRICE || "-1"
);
await query(
`CREATE TABLE IF NOT EXISTS model_prices (
id TEXT PRIMARY KEY,
model_name TEXT NOT NULL,
input_price DECIMAL(10, 6) DEFAULT CAST($1 AS DECIMAL(10, 6)),
output_price DECIMAL(10, 6) DEFAULT CAST($2 AS DECIMAL(10, 6)),
per_msg_price DECIMAL(10, 6) DEFAULT CAST($3 AS DECIMAL(10, 6)),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);`,
[defaultInputPrice, defaultOutputPrice, defaultPerMsgPrice]
);
await query(
`DO $$
BEGIN
BEGIN
ALTER TABLE model_prices ADD COLUMN per_msg_price DECIMAL(10, 6) DEFAULT CAST($1 AS DECIMAL(10, 6));
ALTER TABLE model_prices ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
EXCEPTION
WHEN duplicate_column THEN NULL;
END;
END $$;`,
[defaultPerMsgPrice]
);
}
export async function ensureTablesExist() {
await ensureModelPricesTableExists();
await ensureUserTableExists();
}
export async function getOrCreateModelPrice(
id: string,
name: string
): Promise<ModelPrice> {
try {
const defaultPerMsgPrice = parseFloat(
process.env.DEFAULT_MODEL_PER_MSG_PRICE || "-1"
);
const result = await query(
`INSERT INTO model_prices (id, model_name, per_msg_price, updated_at)
VALUES ($1, $2, CAST($3 AS DECIMAL(10, 6)), CURRENT_TIMESTAMP)
ON CONFLICT (id) DO UPDATE
SET model_name = $2, updated_at = CURRENT_TIMESTAMP
RETURNING *`,
[id, name, defaultPerMsgPrice]
);
return {
id: result.rows[0].id,
name: result.rows[0].model_name,
input_price: Number(result.rows[0].input_price),
output_price: Number(result.rows[0].output_price),
per_msg_price: Number(result.rows[0].per_msg_price),
updated_at: result.rows[0].updated_at,
};
} catch (error: any) {
console.error("Error in getOrCreateModelPrice:", error);
if (error.message.includes("Connection terminated unexpectedly")) {
console.log("Retrying database connection...");
await new Promise((resolve) => setTimeout(resolve, 1000));
return getOrCreateModelPrice(id, name);
}
throw error;
}
}
export {
getUsers,
getOrCreateUser,
updateUserBalance,
deleteUser,
} from "./users";
|