@xuda.io/account_module 1.2.2122 → 1.2.2124
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/index.mjs +95 -96
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1722,7 +1722,6 @@ export const get_user_name = async function (uid) {
|
|
|
1722
1722
|
};
|
|
1723
1723
|
|
|
1724
1724
|
export const get_user_card = async function (req) {
|
|
1725
|
-
debugger;
|
|
1726
1725
|
const { uid, uid_query } = req;
|
|
1727
1726
|
if (uid === uid_query) return await get_user_contact(uid, uid_query);
|
|
1728
1727
|
|
|
@@ -1787,101 +1786,6 @@ export const ts_contact = async function (uid, contact_id) {
|
|
|
1787
1786
|
}
|
|
1788
1787
|
};
|
|
1789
1788
|
|
|
1790
|
-
export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
1791
|
-
const { uid, year_from, month_from, day_from, year_to, month_to, day_to } = req;
|
|
1792
|
-
try {
|
|
1793
|
-
const app_id = await get_account_default_project_id(uid);
|
|
1794
|
-
|
|
1795
|
-
const d = new Date();
|
|
1796
|
-
const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
|
|
1797
|
-
const curr_year = d.getFullYear();
|
|
1798
|
-
|
|
1799
|
-
const ai_usage = await db_module.get_couch_view('xuda_usage', 'ai_usage', {
|
|
1800
|
-
startkey: [uid, year_from || curr_year, month_from || curr_month, day_from || 0],
|
|
1801
|
-
endkey: [uid, year_to || curr_year, month_to || curr_month, day_to || 99],
|
|
1802
|
-
reduce: true,
|
|
1803
|
-
group_level: 999,
|
|
1804
|
-
});
|
|
1805
|
-
// console.log('ai_usage', ai_usage.rows);
|
|
1806
|
-
|
|
1807
|
-
let total_usage = 0;
|
|
1808
|
-
let profile = {};
|
|
1809
|
-
|
|
1810
|
-
for (const row of ai_usage.data.rows) {
|
|
1811
|
-
const user = row.key[0];
|
|
1812
|
-
const year = row.key[1];
|
|
1813
|
-
const month = row.key[2];
|
|
1814
|
-
const day = row.key[3];
|
|
1815
|
-
const account_profile_id = row.key[4];
|
|
1816
|
-
const uid = row.key[5];
|
|
1817
|
-
const model = row.key[6];
|
|
1818
|
-
const input_tokens = row.value[0];
|
|
1819
|
-
const output_tokens = row.value[1];
|
|
1820
|
-
const input_credits = row.value[2];
|
|
1821
|
-
const output_credits = row.value[3];
|
|
1822
|
-
if (!profile[account_profile_id]) {
|
|
1823
|
-
profile[account_profile_id] = {};
|
|
1824
|
-
}
|
|
1825
|
-
if (!profile[account_profile_id][uid]) {
|
|
1826
|
-
profile[account_profile_id][uid] = 0;
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
|
-
profile[account_profile_id][uid] += input_credits + output_credits;
|
|
1830
|
-
total_usage += input_credits + output_credits;
|
|
1831
|
-
}
|
|
1832
|
-
|
|
1833
|
-
// let membership_plan = _conf.PLAN_OBJ?.[acc_obj?.ai_workspace_plan]?.features?.ai_credits || 0;
|
|
1834
|
-
// let ai_workspace_plan = _conf.PLAN_OBJ?.[acc_obj?.membership_plan]?.features?.ai_credits || 0;
|
|
1835
|
-
// let contact_connection = 0;
|
|
1836
|
-
// let total_credits = membership_plan + ai_workspace_plan + contact_connection;
|
|
1837
|
-
|
|
1838
|
-
const ai_credits = await db_module.get_couch_view('xuda_billing', 'ai_credits', {
|
|
1839
|
-
startkey: [uid, ''],
|
|
1840
|
-
endkey: [uid, 'zzz'],
|
|
1841
|
-
reduce: true,
|
|
1842
|
-
group_level: 999,
|
|
1843
|
-
});
|
|
1844
|
-
|
|
1845
|
-
let credits_obj = {},
|
|
1846
|
-
total_credits = 0;
|
|
1847
|
-
for (const row of ai_credits.data.rows) {
|
|
1848
|
-
const user = row.key[0];
|
|
1849
|
-
const source = row.key[1];
|
|
1850
|
-
|
|
1851
|
-
const credits = row.value;
|
|
1852
|
-
|
|
1853
|
-
total_credits += credits;
|
|
1854
|
-
|
|
1855
|
-
if (!credits_obj[source]) {
|
|
1856
|
-
credits_obj[source] = 0;
|
|
1857
|
-
}
|
|
1858
|
-
credits_obj[source] += credits;
|
|
1859
|
-
}
|
|
1860
|
-
|
|
1861
|
-
let data = { credits: { ...credits_obj, total: total_credits }, usage: { profile, projects: [], total: total_usage } };
|
|
1862
|
-
|
|
1863
|
-
if (job_id) {
|
|
1864
|
-
data.profile_info = {};
|
|
1865
|
-
data.user_info = {};
|
|
1866
|
-
for (const [key, val] of Object.entries(data?.usage?.profile || {})) {
|
|
1867
|
-
try {
|
|
1868
|
-
data.profile_info[key] = (await db_module.get_app_couch_doc_native(app_id, key))?.profile_name;
|
|
1869
|
-
|
|
1870
|
-
for (const [user_id, val2] of Object.entries(val || {})) {
|
|
1871
|
-
try {
|
|
1872
|
-
data.user_info[user_id] = (await db_module.get_couch_doc_native('xuda_accounts', user_id))?.account_info?.full_name;
|
|
1873
|
-
} catch (error) {}
|
|
1874
|
-
}
|
|
1875
|
-
} catch (error) {}
|
|
1876
|
-
}
|
|
1877
|
-
}
|
|
1878
|
-
|
|
1879
|
-
return { code: 55, data };
|
|
1880
|
-
} catch (err) {
|
|
1881
|
-
return { code: -55, data: err.message };
|
|
1882
|
-
}
|
|
1883
|
-
};
|
|
1884
|
-
|
|
1885
1789
|
const set_account_profile_picture = async function (uid, account_uid, metadata, job_id, headers, account_profile_info) {
|
|
1886
1790
|
await update_account_profile_picture_status(account_uid, 1);
|
|
1887
1791
|
try {
|
|
@@ -3406,6 +3310,101 @@ export const save_cache_hit = async function (_id) {
|
|
|
3406
3310
|
|
|
3407
3311
|
///////////////////////////////
|
|
3408
3312
|
|
|
3313
|
+
export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
3314
|
+
const { uid, year_from, month_from, day_from, year_to, month_to, day_to } = req;
|
|
3315
|
+
try {
|
|
3316
|
+
const app_id = await get_account_default_project_id(uid);
|
|
3317
|
+
|
|
3318
|
+
const d = new Date();
|
|
3319
|
+
const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
|
|
3320
|
+
const curr_year = d.getFullYear();
|
|
3321
|
+
|
|
3322
|
+
const ai_usage = await db_module.get_couch_view('xuda_usage', 'ai_usage', {
|
|
3323
|
+
startkey: [uid, year_from || curr_year, month_from || curr_month, day_from || 0],
|
|
3324
|
+
endkey: [uid, year_to || curr_year, month_to || curr_month, day_to || 99],
|
|
3325
|
+
reduce: true,
|
|
3326
|
+
group_level: 999,
|
|
3327
|
+
});
|
|
3328
|
+
// console.log('ai_usage', ai_usage.rows);
|
|
3329
|
+
|
|
3330
|
+
let total_usage = 0;
|
|
3331
|
+
let profile = {};
|
|
3332
|
+
|
|
3333
|
+
for (const row of ai_usage.data.rows) {
|
|
3334
|
+
const user = row.key[0];
|
|
3335
|
+
const year = row.key[1];
|
|
3336
|
+
const month = row.key[2];
|
|
3337
|
+
const day = row.key[3];
|
|
3338
|
+
const account_profile_id = row.key[4];
|
|
3339
|
+
const uid = row.key[5];
|
|
3340
|
+
const model = row.key[6];
|
|
3341
|
+
const input_tokens = row.value[0];
|
|
3342
|
+
const output_tokens = row.value[1];
|
|
3343
|
+
const input_credits = row.value[2];
|
|
3344
|
+
const output_credits = row.value[3];
|
|
3345
|
+
if (!profile[account_profile_id]) {
|
|
3346
|
+
profile[account_profile_id] = {};
|
|
3347
|
+
}
|
|
3348
|
+
if (!profile[account_profile_id][uid]) {
|
|
3349
|
+
profile[account_profile_id][uid] = 0;
|
|
3350
|
+
}
|
|
3351
|
+
|
|
3352
|
+
profile[account_profile_id][uid] += input_credits + output_credits;
|
|
3353
|
+
total_usage += input_credits + output_credits;
|
|
3354
|
+
}
|
|
3355
|
+
|
|
3356
|
+
// let membership_plan = _conf.PLAN_OBJ?.[acc_obj?.ai_workspace_plan]?.features?.ai_credits || 0;
|
|
3357
|
+
// let ai_workspace_plan = _conf.PLAN_OBJ?.[acc_obj?.membership_plan]?.features?.ai_credits || 0;
|
|
3358
|
+
// let contact_connection = 0;
|
|
3359
|
+
// let total_credits = membership_plan + ai_workspace_plan + contact_connection;
|
|
3360
|
+
|
|
3361
|
+
const ai_credits = await db_module.get_couch_view('xuda_billing', 'ai_credits', {
|
|
3362
|
+
startkey: [uid, ''],
|
|
3363
|
+
endkey: [uid, 'zzz'],
|
|
3364
|
+
reduce: true,
|
|
3365
|
+
group_level: 999,
|
|
3366
|
+
});
|
|
3367
|
+
|
|
3368
|
+
let credits_obj = {},
|
|
3369
|
+
total_credits = 0;
|
|
3370
|
+
for (const row of ai_credits.data.rows) {
|
|
3371
|
+
const user = row.key[0];
|
|
3372
|
+
const source = row.key[1];
|
|
3373
|
+
|
|
3374
|
+
const credits = row.value;
|
|
3375
|
+
|
|
3376
|
+
total_credits += credits;
|
|
3377
|
+
|
|
3378
|
+
if (!credits_obj[source]) {
|
|
3379
|
+
credits_obj[source] = 0;
|
|
3380
|
+
}
|
|
3381
|
+
credits_obj[source] += credits;
|
|
3382
|
+
}
|
|
3383
|
+
|
|
3384
|
+
let data = { credits: { ...credits_obj, total: total_credits }, usage: { profile, projects: [], total: total_usage } };
|
|
3385
|
+
|
|
3386
|
+
if (job_id) {
|
|
3387
|
+
data.profile_info = {};
|
|
3388
|
+
data.user_info = {};
|
|
3389
|
+
for (const [key, val] of Object.entries(data?.usage?.profile || {})) {
|
|
3390
|
+
try {
|
|
3391
|
+
data.profile_info[key] = (await db_module.get_app_couch_doc_native(app_id, key))?.profile_name;
|
|
3392
|
+
|
|
3393
|
+
for (const [user_id, val2] of Object.entries(val || {})) {
|
|
3394
|
+
try {
|
|
3395
|
+
data.user_info[user_id] = (await db_module.get_couch_doc_native('xuda_accounts', user_id))?.account_info?.full_name;
|
|
3396
|
+
} catch (error) {}
|
|
3397
|
+
}
|
|
3398
|
+
} catch (error) {}
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
|
|
3402
|
+
return { code: 55, data };
|
|
3403
|
+
} catch (err) {
|
|
3404
|
+
return { code: -55, data: err.message };
|
|
3405
|
+
}
|
|
3406
|
+
};
|
|
3407
|
+
|
|
3409
3408
|
export const record_ai_usage = async function (uid, input_tokens, output_tokens, source, prompt, model, metadata = {}, account_profile_info, tools) {
|
|
3410
3409
|
try {
|
|
3411
3410
|
if (!account_profile_info) throw new Error('missing account_profile_info');
|