@xuda.io/account_module 1.2.2123 → 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.
Files changed (2) hide show
  1. package/index.mjs +95 -95
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1786,101 +1786,6 @@ export const ts_contact = async function (uid, contact_id) {
1786
1786
  }
1787
1787
  };
1788
1788
 
1789
- export const get_account_ai_usage = async function (req, job_id, headers) {
1790
- const { uid, year_from, month_from, day_from, year_to, month_to, day_to } = req;
1791
- try {
1792
- const app_id = await get_account_default_project_id(uid);
1793
-
1794
- const d = new Date();
1795
- const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
1796
- const curr_year = d.getFullYear();
1797
-
1798
- const ai_usage = await db_module.get_couch_view('xuda_usage', 'ai_usage', {
1799
- startkey: [uid, year_from || curr_year, month_from || curr_month, day_from || 0],
1800
- endkey: [uid, year_to || curr_year, month_to || curr_month, day_to || 99],
1801
- reduce: true,
1802
- group_level: 999,
1803
- });
1804
- // console.log('ai_usage', ai_usage.rows);
1805
-
1806
- let total_usage = 0;
1807
- let profile = {};
1808
-
1809
- for (const row of ai_usage.data.rows) {
1810
- const user = row.key[0];
1811
- const year = row.key[1];
1812
- const month = row.key[2];
1813
- const day = row.key[3];
1814
- const account_profile_id = row.key[4];
1815
- const uid = row.key[5];
1816
- const model = row.key[6];
1817
- const input_tokens = row.value[0];
1818
- const output_tokens = row.value[1];
1819
- const input_credits = row.value[2];
1820
- const output_credits = row.value[3];
1821
- if (!profile[account_profile_id]) {
1822
- profile[account_profile_id] = {};
1823
- }
1824
- if (!profile[account_profile_id][uid]) {
1825
- profile[account_profile_id][uid] = 0;
1826
- }
1827
-
1828
- profile[account_profile_id][uid] += input_credits + output_credits;
1829
- total_usage += input_credits + output_credits;
1830
- }
1831
-
1832
- // let membership_plan = _conf.PLAN_OBJ?.[acc_obj?.ai_workspace_plan]?.features?.ai_credits || 0;
1833
- // let ai_workspace_plan = _conf.PLAN_OBJ?.[acc_obj?.membership_plan]?.features?.ai_credits || 0;
1834
- // let contact_connection = 0;
1835
- // let total_credits = membership_plan + ai_workspace_plan + contact_connection;
1836
-
1837
- const ai_credits = await db_module.get_couch_view('xuda_billing', 'ai_credits', {
1838
- startkey: [uid, ''],
1839
- endkey: [uid, 'zzz'],
1840
- reduce: true,
1841
- group_level: 999,
1842
- });
1843
-
1844
- let credits_obj = {},
1845
- total_credits = 0;
1846
- for (const row of ai_credits.data.rows) {
1847
- const user = row.key[0];
1848
- const source = row.key[1];
1849
-
1850
- const credits = row.value;
1851
-
1852
- total_credits += credits;
1853
-
1854
- if (!credits_obj[source]) {
1855
- credits_obj[source] = 0;
1856
- }
1857
- credits_obj[source] += credits;
1858
- }
1859
-
1860
- let data = { credits: { ...credits_obj, total: total_credits }, usage: { profile, projects: [], total: total_usage } };
1861
-
1862
- if (job_id) {
1863
- data.profile_info = {};
1864
- data.user_info = {};
1865
- for (const [key, val] of Object.entries(data?.usage?.profile || {})) {
1866
- try {
1867
- data.profile_info[key] = (await db_module.get_app_couch_doc_native(app_id, key))?.profile_name;
1868
-
1869
- for (const [user_id, val2] of Object.entries(val || {})) {
1870
- try {
1871
- data.user_info[user_id] = (await db_module.get_couch_doc_native('xuda_accounts', user_id))?.account_info?.full_name;
1872
- } catch (error) {}
1873
- }
1874
- } catch (error) {}
1875
- }
1876
- }
1877
-
1878
- return { code: 55, data };
1879
- } catch (err) {
1880
- return { code: -55, data: err.message };
1881
- }
1882
- };
1883
-
1884
1789
  const set_account_profile_picture = async function (uid, account_uid, metadata, job_id, headers, account_profile_info) {
1885
1790
  await update_account_profile_picture_status(account_uid, 1);
1886
1791
  try {
@@ -3405,6 +3310,101 @@ export const save_cache_hit = async function (_id) {
3405
3310
 
3406
3311
  ///////////////////////////////
3407
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
+
3408
3408
  export const record_ai_usage = async function (uid, input_tokens, output_tokens, source, prompt, model, metadata = {}, account_profile_info, tools) {
3409
3409
  try {
3410
3410
  if (!account_profile_info) throw new Error('missing account_profile_info');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.2123",
3
+ "version": "1.2.2124",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {