@xuda.io/ai_module 1.1.4914 → 1.1.4916
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 +108 -153
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -61,12 +61,14 @@ const { init, check, check_structure, get_zod_schema, get_fields_z_schema } = aw
|
|
|
61
61
|
|
|
62
62
|
const { get_xuda_couch } = _common;
|
|
63
63
|
|
|
64
|
-
const account_module = await import(`${module_path}/account_module/index.mjs`);
|
|
64
|
+
// const account_module = await import(`${module_path}/account_module/index.mjs`);
|
|
65
65
|
const db_module = await import(`${module_path}/db_module/index.mjs`);
|
|
66
|
-
const drive_module = await import(`${module_path}/drive_module/index.mjs`);
|
|
67
|
-
const jobs_module = await import(`${module_path}/jobs_module/index.mjs`);
|
|
66
|
+
// const drive_module = await import(`${module_path}/drive_module/index.mjs`);
|
|
67
|
+
// const jobs_module = await import(`${module_path}/jobs_module/index.mjs`);
|
|
68
68
|
|
|
69
69
|
const account_ms = await import(`${module_path}/account_module/index_ms.mjs`);
|
|
70
|
+
const drive_ms = await import(`${module_path}/drive_module/index_ms.mjs`);
|
|
71
|
+
const jobs_ms = await import(`${module_path}/jobs_module/index_ms.mjs`);
|
|
70
72
|
|
|
71
73
|
var open_ai_status = {};
|
|
72
74
|
const report_ai_status = function (model, err) {
|
|
@@ -1080,7 +1082,7 @@ const check_studio_doc_tool = tool({
|
|
|
1080
1082
|
export const get_chat_conversation = async function (req) {
|
|
1081
1083
|
let { conversation_id, order = 'asc', limit = 9999, skip = 0, _id, uid } = req;
|
|
1082
1084
|
|
|
1083
|
-
const account_profile_info = await
|
|
1085
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1084
1086
|
|
|
1085
1087
|
if (_id) {
|
|
1086
1088
|
let data = await db_module.get_app_couch_doc(account_profile_info.app_id, _id);
|
|
@@ -1123,7 +1125,7 @@ export const get_chat_conversation = async function (req) {
|
|
|
1123
1125
|
export const get_ai_chats = async function (req, job_id, headers) {
|
|
1124
1126
|
let { uid, reference_id, _id, search, filter_type = 'all', conversation_type, limit, skip, conversation_id, profile_id } = req;
|
|
1125
1127
|
|
|
1126
|
-
const account_profile_info = await
|
|
1128
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
1127
1129
|
|
|
1128
1130
|
const get_conversation_background = function (doc) {
|
|
1129
1131
|
let deg = 145;
|
|
@@ -1160,7 +1162,6 @@ export const get_ai_chats = async function (req, job_id, headers) {
|
|
|
1160
1162
|
|
|
1161
1163
|
try {
|
|
1162
1164
|
if (_id) {
|
|
1163
|
-
// const app_id = await account_module.get_account_default_project_id(uid);
|
|
1164
1165
|
let data = await db_module.get_app_couch_doc(account_profile_info.app_id, _id);
|
|
1165
1166
|
return data;
|
|
1166
1167
|
}
|
|
@@ -1276,11 +1277,11 @@ export const get_ai_chats = async function (req, job_id, headers) {
|
|
|
1276
1277
|
let doc = { date_created: e.team_req_date, pending: true, uid: e.team_req_from_uid, team_req_id: e._id };
|
|
1277
1278
|
|
|
1278
1279
|
if (e.team_req_from_uid) {
|
|
1279
|
-
doc.user_contact = await
|
|
1280
|
+
doc.user_contact = await account_ms.get_user_card(uid, e.team_req_from_uid);
|
|
1280
1281
|
}
|
|
1281
1282
|
|
|
1282
1283
|
//share_item_id
|
|
1283
|
-
const app_id = await
|
|
1284
|
+
const app_id = await account_ms.get_account_default_project_id(e.team_req_from_uid);
|
|
1284
1285
|
const chat_ret = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
|
|
1285
1286
|
doc.title = chat_ret.title;
|
|
1286
1287
|
doc.chat_image = chat_ret.chat_image;
|
|
@@ -1309,7 +1310,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
|
|
|
1309
1310
|
}
|
|
1310
1311
|
|
|
1311
1312
|
let docs = [];
|
|
1312
|
-
|
|
1313
|
+
|
|
1313
1314
|
for (let doc of ai_chats.docs || []) {
|
|
1314
1315
|
let reference_doc;
|
|
1315
1316
|
|
|
@@ -1338,7 +1339,7 @@ export const get_ai_chats = async function (req, job_id, headers) {
|
|
|
1338
1339
|
// }
|
|
1339
1340
|
// }
|
|
1340
1341
|
|
|
1341
|
-
doc.user_contact = await
|
|
1342
|
+
doc.user_contact = await account_ms.get_user_card(uid, doc.uid);
|
|
1342
1343
|
|
|
1343
1344
|
doc.privilege = doc.uid === uid || doc?.account_profile_info?.uid === uid;
|
|
1344
1345
|
|
|
@@ -1358,9 +1359,9 @@ export const get_ai_chats = async function (req, job_id, headers) {
|
|
|
1358
1359
|
export const delete_ai_chat = async function (req) {
|
|
1359
1360
|
let { conversation_id, uid } = req;
|
|
1360
1361
|
|
|
1361
|
-
const account_profile_info = await
|
|
1362
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1362
1363
|
|
|
1363
|
-
const app_id = await
|
|
1364
|
+
const app_id = await account_ms.get_account_default_project_id(uid);
|
|
1364
1365
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
1365
1366
|
|
|
1366
1367
|
let save_ret;
|
|
@@ -1389,7 +1390,7 @@ export const delete_ai_chat = async function (req) {
|
|
|
1389
1390
|
}
|
|
1390
1391
|
|
|
1391
1392
|
if (conversation_doc?.chat_image?.[0]) {
|
|
1392
|
-
|
|
1393
|
+
drive_ms.delete_drive_files({ uid, drive_type: 'user', files_arr: [{ type: 'file', file_path: path.join(conversation_doc.chat_image[0].file_path, conversation_doc.chat_image[0].filename) }] });
|
|
1393
1394
|
}
|
|
1394
1395
|
}
|
|
1395
1396
|
|
|
@@ -1401,9 +1402,8 @@ export const delete_ai_chat = async function (req) {
|
|
|
1401
1402
|
export const archive_ai_chat = async function (req) {
|
|
1402
1403
|
let { conversation_id, uid } = req;
|
|
1403
1404
|
|
|
1404
|
-
const account_profile_info = await
|
|
1405
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1405
1406
|
|
|
1406
|
-
const app_id = await account_module.get_account_default_project_id(uid);
|
|
1407
1407
|
try {
|
|
1408
1408
|
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
1409
1409
|
await team_module.validate_share_exist(uid, 'chat', conversation_id);
|
|
@@ -1426,8 +1426,8 @@ export const archive_ai_chat = async function (req) {
|
|
|
1426
1426
|
|
|
1427
1427
|
export const unarchive_ai_chat = async function (req) {
|
|
1428
1428
|
let { conversation_id, uid } = req;
|
|
1429
|
-
const account_profile_info = await
|
|
1430
|
-
|
|
1429
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1430
|
+
|
|
1431
1431
|
try {
|
|
1432
1432
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
1433
1433
|
|
|
@@ -1512,7 +1512,7 @@ const create_image = async function (uid, prompt, model = 'gpt-image-1-mini', si
|
|
|
1512
1512
|
|
|
1513
1513
|
const thumbnailBase64 = resized.toString('base64');
|
|
1514
1514
|
|
|
1515
|
-
|
|
1515
|
+
account_ms.record_ai_usage(uid, response.usage.input_tokens, response.usage.output_tokens, 'create image', prompt, model, metadata, account_profile_info);
|
|
1516
1516
|
|
|
1517
1517
|
return thumbnailBase64;
|
|
1518
1518
|
} catch (err) {
|
|
@@ -1523,9 +1523,9 @@ const create_image = async function (uid, prompt, model = 'gpt-image-1-mini', si
|
|
|
1523
1523
|
|
|
1524
1524
|
const get_studio_doc = async function (req) {
|
|
1525
1525
|
let { uid, _id } = req;
|
|
1526
|
-
const account_profile_info = await
|
|
1526
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1527
1527
|
|
|
1528
|
-
const app_id = await
|
|
1528
|
+
const app_id = await account_ms.get_account_default_project_id(uid);
|
|
1529
1529
|
try {
|
|
1530
1530
|
let data = await db_module.get_app_couch_doc_native(account_profile_info.app_id, _id);
|
|
1531
1531
|
|
|
@@ -1802,7 +1802,7 @@ export const get_ai_agent_info = async function (uid, job_id, headers, doc, from
|
|
|
1802
1802
|
}
|
|
1803
1803
|
|
|
1804
1804
|
if (doc?.studio_meta?.createdByUid) {
|
|
1805
|
-
doc.user_contact = await
|
|
1805
|
+
doc.user_contact = await account_ms.get_user_card(uid, doc.studio_meta.createdByUid);
|
|
1806
1806
|
}
|
|
1807
1807
|
if (!from_marketplace) {
|
|
1808
1808
|
doc.interactions = 0;
|
|
@@ -1820,7 +1820,7 @@ export const get_ai_agent_info = async function (uid, job_id, headers, doc, from
|
|
|
1820
1820
|
|
|
1821
1821
|
if (!from_marketplace) {
|
|
1822
1822
|
if (doc?.reference_doc?.studio_meta?.shared_from_uid) {
|
|
1823
|
-
const shared_from_uid_ret = await
|
|
1823
|
+
const shared_from_uid_ret = await account_ms.get_account_name({ uid_query: doc.reference_doc.studio_meta.shared_from_uid });
|
|
1824
1824
|
doc.icon_pattern = shared_from_uid_ret.data.profile_avatar;
|
|
1825
1825
|
}
|
|
1826
1826
|
}
|
|
@@ -1828,9 +1828,8 @@ export const get_ai_agent_info = async function (uid, job_id, headers, doc, from
|
|
|
1828
1828
|
};
|
|
1829
1829
|
|
|
1830
1830
|
const get_user_ai_agents = async function (uid) {
|
|
1831
|
-
const account_profile_info = await
|
|
1831
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
1832
1832
|
|
|
1833
|
-
const app_id = await account_module.get_account_default_project_id(uid);
|
|
1834
1833
|
let opt = {
|
|
1835
1834
|
selector: {
|
|
1836
1835
|
docType: 'studio',
|
|
@@ -1843,7 +1842,7 @@ const get_user_ai_agents = async function (uid) {
|
|
|
1843
1842
|
|
|
1844
1843
|
export const get_ai_agents = async function (req, job_id, headers) {
|
|
1845
1844
|
let { uid, _id, search, filter_type = 'all', limit, skip, agent_id, profile_id } = req;
|
|
1846
|
-
const account_profile_info = await
|
|
1845
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
1847
1846
|
try {
|
|
1848
1847
|
if (_id) {
|
|
1849
1848
|
let doc_ret = await get_studio_doc({ _id, uid });
|
|
@@ -1927,8 +1926,6 @@ export const get_ai_agents = async function (req, job_id, headers) {
|
|
|
1927
1926
|
}
|
|
1928
1927
|
//////////////
|
|
1929
1928
|
|
|
1930
|
-
// const app_id = await account_module.get_account_default_project_id(uid);
|
|
1931
|
-
|
|
1932
1929
|
let user_agents = { docs: [], total_docs: 0 };
|
|
1933
1930
|
if (filter_type !== 'pending') {
|
|
1934
1931
|
user_agents = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
|
|
@@ -1955,12 +1952,12 @@ export const get_ai_agents = async function (req, job_id, headers) {
|
|
|
1955
1952
|
let doc = { ts: e.team_req_date, pending: true, properties: {}, studio_meta: { createdByUid: e.team_req_from_uid }, team_req_id: e._id };
|
|
1956
1953
|
|
|
1957
1954
|
if (e.team_req_from_uid) {
|
|
1958
|
-
doc.user_contact = await
|
|
1955
|
+
doc.user_contact = await account_ms.get_user_card(uid, e.team_req_from_uid);
|
|
1959
1956
|
doc.icon_pattern = doc.user_contact.profile_avatar;
|
|
1960
1957
|
}
|
|
1961
1958
|
|
|
1962
1959
|
//share_item_id
|
|
1963
|
-
const app_id = await
|
|
1960
|
+
const app_id = await account_ms.get_account_default_project_id(e.team_req_from_uid);
|
|
1964
1961
|
const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
|
|
1965
1962
|
doc.properties.menuName = ai_agent_doc.properties.menuName;
|
|
1966
1963
|
doc.studio_meta.agent_image = ai_agent_doc.studio_meta.agent_image;
|
|
@@ -2010,7 +2007,7 @@ export const get_ai_agents = async function (req, job_id, headers) {
|
|
|
2010
2007
|
export const delete_ai_agent = async function (req) {
|
|
2011
2008
|
let { agent_id, uid, app_id } = req;
|
|
2012
2009
|
|
|
2013
|
-
const account_profile_info = await
|
|
2010
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2014
2011
|
|
|
2015
2012
|
// const project_db = await get_account_project_db(uid);
|
|
2016
2013
|
|
|
@@ -2052,9 +2049,7 @@ export const delete_ai_agent = async function (req) {
|
|
|
2052
2049
|
export const uninstall_ai_agent = async function (req) {
|
|
2053
2050
|
let { agent_id, uid } = req;
|
|
2054
2051
|
|
|
2055
|
-
const account_profile_info = await
|
|
2056
|
-
|
|
2057
|
-
const app_id = await account_module.get_account_default_project_id(uid);
|
|
2052
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2058
2053
|
|
|
2059
2054
|
try {
|
|
2060
2055
|
let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
|
|
@@ -2076,9 +2071,7 @@ export const uninstall_ai_agent = async function (req) {
|
|
|
2076
2071
|
export const unarchive_ai_agent = async function (req) {
|
|
2077
2072
|
let { agent_id, uid } = req;
|
|
2078
2073
|
|
|
2079
|
-
const account_profile_info = await
|
|
2080
|
-
|
|
2081
|
-
const app_id = await account_module.get_account_default_project_id(uid);
|
|
2074
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2082
2075
|
|
|
2083
2076
|
try {
|
|
2084
2077
|
let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
|
|
@@ -2099,8 +2092,8 @@ export const unarchive_ai_agent = async function (req) {
|
|
|
2099
2092
|
};
|
|
2100
2093
|
export const archive_ai_agent = async function (req) {
|
|
2101
2094
|
let { agent_id, uid } = req;
|
|
2102
|
-
const account_profile_info = await
|
|
2103
|
-
|
|
2095
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2096
|
+
|
|
2104
2097
|
try {
|
|
2105
2098
|
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
2106
2099
|
await team_module.validate_share_exist(uid, 'ai_agent', agent_id);
|
|
@@ -2124,8 +2117,8 @@ export const archive_ai_agent = async function (req) {
|
|
|
2124
2117
|
export const archive_mini_app = async function (req) {
|
|
2125
2118
|
let { prog_id, uid } = req;
|
|
2126
2119
|
|
|
2127
|
-
const account_profile_info = await
|
|
2128
|
-
|
|
2120
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2121
|
+
|
|
2129
2122
|
try {
|
|
2130
2123
|
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
2131
2124
|
await team_module.validate_share_exist(uid, 'miniapp', prog_id);
|
|
@@ -2149,8 +2142,8 @@ export const archive_mini_app = async function (req) {
|
|
|
2149
2142
|
export const unarchive_mini_app = async function (req) {
|
|
2150
2143
|
let { prog_id, uid } = req;
|
|
2151
2144
|
|
|
2152
|
-
const account_profile_info = await
|
|
2153
|
-
|
|
2145
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2146
|
+
|
|
2154
2147
|
try {
|
|
2155
2148
|
let prog_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
|
|
2156
2149
|
|
|
@@ -2171,8 +2164,8 @@ export const unarchive_mini_app = async function (req) {
|
|
|
2171
2164
|
export const delete_mini_app = async function (req) {
|
|
2172
2165
|
let { prog_id, uid } = req;
|
|
2173
2166
|
|
|
2174
|
-
const account_profile_info = await
|
|
2175
|
-
|
|
2167
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2168
|
+
|
|
2176
2169
|
try {
|
|
2177
2170
|
let prog_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, prog_id);
|
|
2178
2171
|
|
|
@@ -2200,9 +2193,7 @@ export const delete_mini_app = async function (req) {
|
|
|
2200
2193
|
export const update_ai_agent = async function (req, job_id, headers) {
|
|
2201
2194
|
let { agent_id, agentConfig, uid } = req;
|
|
2202
2195
|
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
const account_profile_info = await account_module.get_active_account_profile_info(uid);
|
|
2196
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2206
2197
|
|
|
2207
2198
|
try {
|
|
2208
2199
|
let agent_doc = await await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
|
|
@@ -2241,33 +2232,9 @@ export const update_ai_agent = async function (req, job_id, headers) {
|
|
|
2241
2232
|
}
|
|
2242
2233
|
};
|
|
2243
2234
|
|
|
2244
|
-
// export const install_ai_agent = async function (req) {
|
|
2245
|
-
// let { agent_id, agent_uid, uid } = req;
|
|
2246
|
-
|
|
2247
|
-
// const source_project_db = await get_account_project_db(agent_uid);
|
|
2248
|
-
// const target_project_db = await get_account_project_db(uid);
|
|
2249
|
-
|
|
2250
|
-
// try {
|
|
2251
|
-
// let agent_doc = await source_project_db.get(agent_id);
|
|
2252
|
-
// if (agent_doc.agentConfig.agent_visibility === 'private') {
|
|
2253
|
-
// throw new Error('agent is private');
|
|
2254
|
-
// }
|
|
2255
|
-
// const t = Date.now();
|
|
2256
|
-
// agent_doc.ts = t;
|
|
2257
|
-
// agent_doc.studio_meta.installed_ts = t;
|
|
2258
|
-
// agent_doc.studio_meta.installed_from_app_id = await account_module.get_account_default_project_id(agent_uid);
|
|
2259
|
-
|
|
2260
|
-
// const data = await target_project_db.insert(agent_doc);
|
|
2261
|
-
|
|
2262
|
-
// return { code: 10, data };
|
|
2263
|
-
// } catch (err) {
|
|
2264
|
-
// return { code: -10, data: err.message };
|
|
2265
|
-
// }
|
|
2266
|
-
// };
|
|
2267
|
-
|
|
2268
2235
|
export const get_apps = async function (req, job_id, headers) {
|
|
2269
2236
|
let { uid, _id, search, filter_type = 'all', limit, skip, mini_app_id, profile_id } = req;
|
|
2270
|
-
const account_profile_info = await
|
|
2237
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
2271
2238
|
try {
|
|
2272
2239
|
if (_id) return await get_studio_doc({ _id, uid });
|
|
2273
2240
|
///////////////
|
|
@@ -2335,8 +2302,6 @@ export const get_apps = async function (req, job_id, headers) {
|
|
|
2335
2302
|
}
|
|
2336
2303
|
//////////////
|
|
2337
2304
|
|
|
2338
|
-
// const app_id = await account_module.get_account_default_project_id(uid);
|
|
2339
|
-
|
|
2340
2305
|
let user_apps = { docs: [], total_docs: 0 };
|
|
2341
2306
|
if (filter_type !== 'pending') {
|
|
2342
2307
|
user_apps = await db_module.find_app_couch_query(account_profile_info.app_id, opt);
|
|
@@ -2367,11 +2332,11 @@ export const get_apps = async function (req, job_id, headers) {
|
|
|
2367
2332
|
let doc = { ts: e.team_req_date, pending: true, properties: {}, studio_meta: { createdByUid: e.team_req_from_uid }, team_req_id: e._id };
|
|
2368
2333
|
|
|
2369
2334
|
if (e.team_req_from_uid) {
|
|
2370
|
-
doc.user_contact = await
|
|
2335
|
+
doc.user_contact = await account_ms.get_user_card(uid, e.team_req_from_uid);
|
|
2371
2336
|
}
|
|
2372
2337
|
|
|
2373
2338
|
//share_item_id
|
|
2374
|
-
const app_id = await
|
|
2339
|
+
const app_id = await account_ms.get_account_default_project_id(e.team_req_from_uid);
|
|
2375
2340
|
const miniapp_ret = await db_module.get_app_couch_doc_native(app_id, e.share_item_id);
|
|
2376
2341
|
doc.properties.menuName = miniapp_ret.properties.menuName;
|
|
2377
2342
|
doc.studio_meta.agent_image = miniapp_ret.studio_meta.agent_image;
|
|
@@ -2392,7 +2357,7 @@ export const get_apps = async function (req, job_id, headers) {
|
|
|
2392
2357
|
doc.reference_doc = reference_doc;
|
|
2393
2358
|
}
|
|
2394
2359
|
|
|
2395
|
-
doc.user_contact = await
|
|
2360
|
+
doc.user_contact = await account_ms.get_user_card(uid, doc.studio_meta.createdByUid);
|
|
2396
2361
|
|
|
2397
2362
|
doc.privilege = doc.studio_meta.createdByUid === uid || doc?.studio_meta?.account_profile_info?.uid === uid;
|
|
2398
2363
|
|
|
@@ -2411,14 +2376,14 @@ export const get_apps = async function (req, job_id, headers) {
|
|
|
2411
2376
|
// };
|
|
2412
2377
|
|
|
2413
2378
|
const upload_agent_files = async function (uid, agent_doc) {
|
|
2414
|
-
const account_profile_info = await
|
|
2379
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2415
2380
|
|
|
2416
2381
|
let { agentConfig } = agent_doc;
|
|
2417
2382
|
for await (let tool of agentConfig.agent_tools || []) {
|
|
2418
2383
|
if (tool.type === 'file_search') {
|
|
2419
2384
|
if (!_.isEmpty(tool.file)) {
|
|
2420
2385
|
if (!tool.file.file_id) {
|
|
2421
|
-
const validation_ret =
|
|
2386
|
+
const validation_ret = drive_ms.file_upload_validator(tool.file.name);
|
|
2422
2387
|
if (!validation_ret.valid) {
|
|
2423
2388
|
throw new Error(validation_ret.error);
|
|
2424
2389
|
}
|
|
@@ -2595,7 +2560,7 @@ const upload_agent_files = async function (uid, agent_doc) {
|
|
|
2595
2560
|
|
|
2596
2561
|
const save_agent_status = async function (uid, agent_id, stat, agentConfig) {
|
|
2597
2562
|
// const project_db = await get_account_project_db(uid);
|
|
2598
|
-
const account_profile_info = await
|
|
2563
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2599
2564
|
|
|
2600
2565
|
try {
|
|
2601
2566
|
let agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
|
|
@@ -2613,9 +2578,9 @@ const save_agent_status = async function (uid, agent_id, stat, agentConfig) {
|
|
|
2613
2578
|
export const create_ai_agent = async function (req, job_id, headers) {
|
|
2614
2579
|
const { agentConfig, uid } = req;
|
|
2615
2580
|
|
|
2616
|
-
const { account_project_id: app_id, account_info } = await
|
|
2581
|
+
const { account_project_id: app_id, account_info } = await account_ms.get_default_project_account_doc(uid);
|
|
2617
2582
|
|
|
2618
|
-
const account_profile_info = await
|
|
2583
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2619
2584
|
const { account_profile_obj } = account_profile_info;
|
|
2620
2585
|
try {
|
|
2621
2586
|
const param = { uid, properties: { menuName: agentConfig.agent_name, menuTitle: agentConfig.agent_name, menuType: 'ai_agent' }, checkedInUserName: account_info.first_name + ' ' + account_info.last_name, app_id: account_profile_info.app_id, parentId: 'ai_agents' };
|
|
@@ -2649,7 +2614,7 @@ export const update_ai_agent_properties = async function (doc, app_id, uid, fiel
|
|
|
2649
2614
|
debugger;
|
|
2650
2615
|
const db_doc = await db_module.get_app_couch_doc_native(app_id, doc._id);
|
|
2651
2616
|
// const { account_profile_info } = doc.studio_meta;
|
|
2652
|
-
const account_profile_info = await
|
|
2617
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
2653
2618
|
|
|
2654
2619
|
const get_agent_assistant_name = async function () {
|
|
2655
2620
|
const ret = await submit_chat_gpt_prompt({
|
|
@@ -2896,7 +2861,7 @@ export const update_thumbnail = async function (type, doc, app_id, uid, job_id,
|
|
|
2896
2861
|
images_arr = [cache];
|
|
2897
2862
|
if (!cache) {
|
|
2898
2863
|
images_arr = await create_and_upload_image_to_drive('user', 'Chat Thumbnails', doc._id, prompt, 1, uid, app_id, job_id, headers, true, tags, account_profile_info);
|
|
2899
|
-
|
|
2864
|
+
account_ms.save_xuda_cache(uid, 'chat_thumbnail', doc.title, images_arr[0], { tags });
|
|
2900
2865
|
}
|
|
2901
2866
|
db_doc = await get_db_doc();
|
|
2902
2867
|
db_doc.thumbnail_request_ts = Date.now();
|
|
@@ -2915,7 +2880,7 @@ export const update_thumbnail = async function (type, doc, app_id, uid, job_id,
|
|
|
2915
2880
|
images_arr = [cache];
|
|
2916
2881
|
if (!cache) {
|
|
2917
2882
|
images_arr = await create_and_upload_image_to_drive('user', 'Chat Thumbnails', doc._id, prompt, 1, uid, app_id, job_id, headers, true, tags, account_profile_info);
|
|
2918
|
-
|
|
2883
|
+
account_ms.save_xuda_cache(uid, 'chat_thumbnail', doc.category_info.category, images_arr[0], { tags });
|
|
2919
2884
|
}
|
|
2920
2885
|
db_doc = await get_db_doc();
|
|
2921
2886
|
db_doc.thumbnail_request_ts = Date.now();
|
|
@@ -2987,7 +2952,7 @@ export const update_thumbnail = async function (type, doc, app_id, uid, job_id,
|
|
|
2987
2952
|
};
|
|
2988
2953
|
|
|
2989
2954
|
const get_drive_file_response = async function (drive_type, app_id, uid, file_path) {
|
|
2990
|
-
const check_existing_file_ret = await
|
|
2955
|
+
const check_existing_file_ret = await drive_ms.check_drive_file({ drive_type, app_id, uid, file_path });
|
|
2991
2956
|
|
|
2992
2957
|
if (!check_existing_file_ret?.duplicates) {
|
|
2993
2958
|
throw new Error(`${file_path} document not found`);
|
|
@@ -3053,7 +3018,7 @@ const transcribe = async function (uid, stream, metadata, account_profile_info)
|
|
|
3053
3018
|
// temperature: 0 // lower = more literal
|
|
3054
3019
|
});
|
|
3055
3020
|
report_ai_status(model);
|
|
3056
|
-
|
|
3021
|
+
account_ms.record_ai_usage(uid, 0, resp.length || 0, 'transcribe', '', model, metadata, account_profile_info);
|
|
3057
3022
|
// console.log(resp); // or resp for JSON formats
|
|
3058
3023
|
return resp;
|
|
3059
3024
|
} catch (err) {
|
|
@@ -3062,13 +3027,6 @@ const transcribe = async function (uid, stream, metadata, account_profile_info)
|
|
|
3062
3027
|
}
|
|
3063
3028
|
};
|
|
3064
3029
|
|
|
3065
|
-
// const get_account_project_db = async function (uid) {
|
|
3066
|
-
// const user_project_id = await account_module.get_account_default_project_id(uid);
|
|
3067
|
-
|
|
3068
|
-
// const project_db = await get_project_db(user_project_id);
|
|
3069
|
-
// return project_db;
|
|
3070
|
-
// };
|
|
3071
|
-
|
|
3072
3030
|
const get_app_obj = async function (app_id) {
|
|
3073
3031
|
const db = get_xuda_couch('xuda_master');
|
|
3074
3032
|
const app_obj = await db.get(app_id);
|
|
@@ -3328,13 +3286,13 @@ export const upload_prompt_attachment = async function (req, job_id, headers, fi
|
|
|
3328
3286
|
|
|
3329
3287
|
const file_name = file_obj?.['originalname'];
|
|
3330
3288
|
|
|
3331
|
-
const validation_ret =
|
|
3289
|
+
const validation_ret = drive_ms.file_upload_validator(file_name);
|
|
3332
3290
|
|
|
3333
3291
|
if (!validation_ret.valid) {
|
|
3334
3292
|
throw new Error(validation_ret.error);
|
|
3335
3293
|
}
|
|
3336
3294
|
|
|
3337
|
-
const drive_ret = await
|
|
3295
|
+
const drive_ret = await drive_ms.upload_drive_file_user({ uid, path: '/Chat Attachments' }, job_id, headers, file_obj);
|
|
3338
3296
|
|
|
3339
3297
|
return drive_ret;
|
|
3340
3298
|
} catch (err) {
|
|
@@ -3380,7 +3338,7 @@ export const submit_chat_gpt_prompt = async function (req) {
|
|
|
3380
3338
|
report_ai_status(model, err);
|
|
3381
3339
|
throw err;
|
|
3382
3340
|
}
|
|
3383
|
-
|
|
3341
|
+
account_ms.record_ai_usage(uid, response.usage.input_tokens, response.usage.output_tokens, 'submit chat', prompt, model, metadata, account_profile_info, tools);
|
|
3384
3342
|
return { code: 5, data: response.output_text };
|
|
3385
3343
|
} catch (err) {
|
|
3386
3344
|
return { code: -5, data: err.message };
|
|
@@ -3397,7 +3355,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3397
3355
|
const { profile_id, uid, prompt, perform_ai_execution = true, email_id, email_direction = 'out', from_mailbox, date_created } = req;
|
|
3398
3356
|
let { reference_type, reference_id = '', conversation_type, email_recipient_type } = req;
|
|
3399
3357
|
|
|
3400
|
-
const account_profile_info = await
|
|
3358
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3401
3359
|
const { account_profile_obj } = account_profile_info;
|
|
3402
3360
|
try {
|
|
3403
3361
|
if (conversation_type === 'email') {
|
|
@@ -3407,7 +3365,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3407
3365
|
}
|
|
3408
3366
|
}
|
|
3409
3367
|
|
|
3410
|
-
let { data: ai_credits } = await
|
|
3368
|
+
let { data: ai_credits } = await account_ms.get_account_ai_usage(account_profile_info.uid);
|
|
3411
3369
|
|
|
3412
3370
|
if (ai_credits.credits.total - ai_credits.usage.total < -0.5) {
|
|
3413
3371
|
throw new Error('ai credits reach to hard limit');
|
|
@@ -3454,7 +3412,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3454
3412
|
if (conversation_doc.reference_type === 'contacts' && conversation_type === 'chat') {
|
|
3455
3413
|
// reference_id is the contact id
|
|
3456
3414
|
contact_id = reference_id;
|
|
3457
|
-
contact_doc = await
|
|
3415
|
+
contact_doc = await account_ms.get_contact_info(uid, null, contact_id);
|
|
3458
3416
|
|
|
3459
3417
|
if (!contact_doc?.contact_uid) {
|
|
3460
3418
|
throw new Error('chat conversation to non user recipient is not allowed');
|
|
@@ -3467,7 +3425,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3467
3425
|
recipient_contact_id = contact_doc.connection_contact_id;
|
|
3468
3426
|
recipient_uid = contact_doc.connection_uid;
|
|
3469
3427
|
|
|
3470
|
-
const app_id = await
|
|
3428
|
+
const app_id = await account_ms.get_account_default_project_id(recipient_uid);
|
|
3471
3429
|
const save_ret = await db_module.save_app_couch_doc(app_id, { ...conversation_doc, ...{ uid: recipient_uid, reference_id: recipient_contact_id } });
|
|
3472
3430
|
}
|
|
3473
3431
|
|
|
@@ -3484,7 +3442,7 @@ export const create_conversation = async function (req, job_id, headers) {
|
|
|
3484
3442
|
|
|
3485
3443
|
const conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_doc._id);
|
|
3486
3444
|
|
|
3487
|
-
const recipient_account_profile_info = await
|
|
3445
|
+
const recipient_account_profile_info = await account_ms.get_active_account_profile_info(recipient_uid);
|
|
3488
3446
|
let recipient_conversation_doc = await db_module.get_app_couch_doc_native(recipient_account_profile_info.app_id, conversation_doc._id);
|
|
3489
3447
|
recipient_conversation_doc.title = conversation_doc.title;
|
|
3490
3448
|
|
|
@@ -3609,7 +3567,7 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
|
|
|
3609
3567
|
let { conversation_id } = req;
|
|
3610
3568
|
try {
|
|
3611
3569
|
if (!conversation_id) throw new Error('missing conversation_id');
|
|
3612
|
-
const account_profile_info = await
|
|
3570
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3613
3571
|
|
|
3614
3572
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
3615
3573
|
req.conversation_doc = conversation_doc;
|
|
@@ -3617,7 +3575,7 @@ export const submit_chat_conversation = async function (req, job_id, headers) {
|
|
|
3617
3575
|
if (conversation_doc.reference_type === 'contacts') {
|
|
3618
3576
|
req._thread_reentry = Date.now() - conversation_doc.date_created_ts > 60000;
|
|
3619
3577
|
|
|
3620
|
-
|
|
3578
|
+
account_ms.ts_contact(uid, conversation_doc.reference_id);
|
|
3621
3579
|
|
|
3622
3580
|
switch (conversation_doc.conversation_type) {
|
|
3623
3581
|
case 'note': {
|
|
@@ -3675,17 +3633,17 @@ const update_conversation_mood_level = async function (uid, target_contacts = []
|
|
|
3675
3633
|
const mood_level_obj = JSON5.parse(mood_level.data);
|
|
3676
3634
|
|
|
3677
3635
|
for await (const target of target_contacts) {
|
|
3678
|
-
const account_profile_info = await
|
|
3636
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(target.uid);
|
|
3679
3637
|
let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_id);
|
|
3680
3638
|
conversation_doc.mood_level = mood_level_obj.mood_level;
|
|
3681
3639
|
await db_module.save_app_couch_doc_native(account_profile_info.app_id, conversation_doc);
|
|
3682
3640
|
|
|
3683
3641
|
if (update_contact) {
|
|
3684
|
-
const account_profile_info = await
|
|
3685
|
-
let contact_doc = await
|
|
3642
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(target.uid);
|
|
3643
|
+
let contact_doc = await account_ms.get_contact(account_profile_info.uid, target.contact_id);
|
|
3686
3644
|
contact_doc.contact_mood_level = mood_level_obj.mood_level;
|
|
3687
3645
|
contact_doc.ts = Date.now();
|
|
3688
|
-
await
|
|
3646
|
+
await account_ms.save_contact(target.uid, contact_doc);
|
|
3689
3647
|
}
|
|
3690
3648
|
}
|
|
3691
3649
|
} catch (err) {
|
|
@@ -3695,16 +3653,16 @@ const update_conversation_mood_level = async function (uid, target_contacts = []
|
|
|
3695
3653
|
|
|
3696
3654
|
const contact_chat_conversation = async function (req, job_id, headers) {
|
|
3697
3655
|
const { uid, profile_id } = req;
|
|
3698
|
-
const account_profile_info = await
|
|
3656
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3699
3657
|
let { prompt, conversation_doc, attachments = [], ai_agents } = req;
|
|
3700
3658
|
|
|
3701
3659
|
const conversation_id = conversation_doc._id;
|
|
3702
3660
|
|
|
3703
|
-
const receiver_contact_doc = await
|
|
3704
|
-
const receiver_app_id = await
|
|
3661
|
+
const receiver_contact_doc = await account_ms.get_contact_info(uid, null, conversation_doc.reference_id);
|
|
3662
|
+
const receiver_app_id = await account_ms.get_account_default_project_id(receiver_contact_doc.contact_uid);
|
|
3705
3663
|
let receiver_conversation_doc = await db_module.get_app_couch_doc_native(receiver_app_id, conversation_id);
|
|
3706
3664
|
|
|
3707
|
-
const sender_contact_doc = await
|
|
3665
|
+
const sender_contact_doc = await account_ms.get_contact_info(uid, null, conversation_doc.reference_id);
|
|
3708
3666
|
const sender_app_id = account_profile_info.app_id;
|
|
3709
3667
|
let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
|
|
3710
3668
|
|
|
@@ -3759,12 +3717,12 @@ const contact_chat_conversation = async function (req, job_id, headers) {
|
|
|
3759
3717
|
|
|
3760
3718
|
const chat_note = async function (req, job_id, headers) {
|
|
3761
3719
|
const { uid, profile_id } = req;
|
|
3762
|
-
const account_profile_info = await
|
|
3720
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3763
3721
|
let { prompt: body, conversation_doc, attachments = [], ai_agents } = req;
|
|
3764
3722
|
try {
|
|
3765
3723
|
const conversation_id = conversation_doc._id;
|
|
3766
3724
|
|
|
3767
|
-
const sender_app_id = account_profile_info.app_id; //await
|
|
3725
|
+
const sender_app_id = account_profile_info.app_id; //await account_ms.get_account_default_project_id(uid);
|
|
3768
3726
|
let sender_conversation_doc = await db_module.get_app_couch_doc_native(sender_app_id, conversation_id);
|
|
3769
3727
|
|
|
3770
3728
|
await attachment_handler(uid, sender_app_id, conversation_id, attachments, account_profile_info);
|
|
@@ -3821,7 +3779,7 @@ const chat_note = async function (req, job_id, headers) {
|
|
|
3821
3779
|
|
|
3822
3780
|
const chat_email = async function (req, job_id, headers) {
|
|
3823
3781
|
const { profile_id, uid, email_id, perform_ai_execution = true, from_mailbox, _thread_reentry, email_direction } = req;
|
|
3824
|
-
const account_profile_info = await
|
|
3782
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3825
3783
|
let { prompt: body, conversation_doc, attachments = [], ai_agents } = req;
|
|
3826
3784
|
try {
|
|
3827
3785
|
if (!account_profile_info.account_profile_obj?.email_account_id) {
|
|
@@ -3839,7 +3797,7 @@ const chat_email = async function (req, job_id, headers) {
|
|
|
3839
3797
|
throw new Error('not an contact conversation');
|
|
3840
3798
|
}
|
|
3841
3799
|
const contact_id = sender_conversation_doc.reference_id;
|
|
3842
|
-
const contact_info = await
|
|
3800
|
+
const contact_info = await account_ms.get_contact_info(uid, null, contact_id);
|
|
3843
3801
|
if (!contact_info.email) {
|
|
3844
3802
|
throw new Error('empty contact email');
|
|
3845
3803
|
}
|
|
@@ -3964,7 +3922,7 @@ const chat_email = async function (req, job_id, headers) {
|
|
|
3964
3922
|
|
|
3965
3923
|
const ai_chat_conversation = async function (req, job_id, headers) {
|
|
3966
3924
|
const { uid, gtp_token, profile_id } = req;
|
|
3967
|
-
const account_profile_info = await
|
|
3925
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid, profile_id);
|
|
3968
3926
|
let { prompt, conversation_doc, attachments = [], ai_agents, output_format = 'md', stream = true, with_context = true } = req;
|
|
3969
3927
|
await _utils.delay(1000);
|
|
3970
3928
|
const conversation_id = conversation_doc._id;
|
|
@@ -4032,10 +3990,10 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4032
3990
|
};
|
|
4033
3991
|
|
|
4034
3992
|
try {
|
|
4035
|
-
const app_id = await
|
|
3993
|
+
const app_id = await account_ms.get_account_default_project_id(uid);
|
|
4036
3994
|
const app_obj = await get_app_obj(account_profile_info.app_id);
|
|
4037
|
-
const userName = await
|
|
4038
|
-
const account_info = (await
|
|
3995
|
+
const userName = await account_ms.get_user_name(uid);
|
|
3996
|
+
const account_info = (await account_ms.get_account_name({ uid })).data;
|
|
4039
3997
|
let context = { ...{ app_id: account_profile_info.app_id, uid, userName, account_info, app_obj, account_id: uid }, ...extractClientInfo(headers) };
|
|
4040
3998
|
const d = new Intl.DateTimeFormat('en-US').format(new Date());
|
|
4041
3999
|
if (!visitors[d]) {
|
|
@@ -4054,7 +4012,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4054
4012
|
// emitToDashboard_interval = setInterval(() => {
|
|
4055
4013
|
// emitToDashboard('stream_delta', '.');
|
|
4056
4014
|
// }, 500);
|
|
4057
|
-
await
|
|
4015
|
+
await jobs_ms.update_job(
|
|
4058
4016
|
{
|
|
4059
4017
|
job_id,
|
|
4060
4018
|
current_step_name: current_step_name,
|
|
@@ -4073,9 +4031,6 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4073
4031
|
// let conversation_doc = await db_module.get_app_couch_doc_native(app_id, conversation_id);
|
|
4074
4032
|
const reference_type = conversation_doc.reference_type;
|
|
4075
4033
|
const reference_id = conversation_doc.reference_id;
|
|
4076
|
-
// if (reference_type === 'contact') {
|
|
4077
|
-
// account_module.ts_contact(reference_id);
|
|
4078
|
-
// }
|
|
4079
4034
|
|
|
4080
4035
|
conversation_doc.stat = 2;
|
|
4081
4036
|
conversation_doc.ts = Date.now();
|
|
@@ -4195,12 +4150,12 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4195
4150
|
ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, agent_id);
|
|
4196
4151
|
if (ai_agent_doc.studio_meta.shared_from_uid) {
|
|
4197
4152
|
const reference_doc = _.cloneDeep(ai_agent_doc);
|
|
4198
|
-
const app_id = await
|
|
4153
|
+
const app_id = await account_ms.get_account_default_project_id(ai_agent_doc.studio_meta.shared_from_uid);
|
|
4199
4154
|
ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
|
|
4200
4155
|
ai_agent_doc.reference_doc = reference_doc;
|
|
4201
4156
|
} else if (ai_agent_doc.studio_meta.installed_from_uid) {
|
|
4202
4157
|
const reference_doc = _.cloneDeep(ai_agent_doc);
|
|
4203
|
-
const app_id = await
|
|
4158
|
+
const app_id = await account_ms.get_account_default_project_id(ai_agent_doc.studio_meta.installed_from_uid);
|
|
4204
4159
|
ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_doc.studio_meta.share_item_id);
|
|
4205
4160
|
ai_agent_doc.reference_doc = reference_doc;
|
|
4206
4161
|
}
|
|
@@ -4650,7 +4605,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4650
4605
|
out_conversation_item_doc.conversation_item_reference_id = conversation_items?.data?.[0]?.id;
|
|
4651
4606
|
await db_module.save_app_couch_doc_native(account_profile_info.app_id, out_conversation_item_doc);
|
|
4652
4607
|
|
|
4653
|
-
|
|
4608
|
+
account_ms.record_ai_usage(uid, inputTokens, outputTokens, 'conversation chat', prompt, model, { conversation_id, ai_agent_id: in_conversation_item_obj.ai_agent_id, item_id: in_conversation_item_obj._id }, account_profile_info);
|
|
4654
4609
|
// if (!conversation_doc.chat_image && (!conversation_doc.thumbnail_request_ts || Date.now() - conversation_doc.thumbnail_request_ts > 120000)) {
|
|
4655
4610
|
// update_thumbnail('conversation', conversation_doc, app_id, uid, job_id, headers);
|
|
4656
4611
|
// }
|
|
@@ -4716,7 +4671,7 @@ const ai_chat_conversation = async function (req, job_id, headers) {
|
|
|
4716
4671
|
setTimeout(async () => {
|
|
4717
4672
|
const app_id = 'prj712ffdf5aa8adce6cedef988f9c12392'; //'prj3937cb6f9a31c8c7dea25055bba845b1'; //
|
|
4718
4673
|
const uid = 'd39126e0e2c51ffbd1aad10709fc8335';
|
|
4719
|
-
const account_profile_info = await
|
|
4674
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
4720
4675
|
|
|
4721
4676
|
// await init_studio_units();
|
|
4722
4677
|
let ret;
|
|
@@ -4747,8 +4702,8 @@ setTimeout(async () => {
|
|
|
4747
4702
|
|
|
4748
4703
|
// ret = await conversation_actions({ action: 'save_to_drive', conversation_id: 'conv_6935865eb6388193bc266aa33e5ad1e10932c1744e314a2f', item_id: 'msg_0932c1744e314a2f00693586a7cae48193a52be621d41a8755' });
|
|
4749
4704
|
|
|
4750
|
-
// ret = await
|
|
4751
|
-
// ret = await
|
|
4705
|
+
// ret = await drive_ms.delete_drive_files({ uid, drive_type: 'user', files_arr: [{ type: 'file', file_path: path.join('/Chat Thumbnails', 'conv_69368cb025208194bce284cf2185d7b80321fd7a19a74e74.png') }] });
|
|
4706
|
+
// ret = await drive_ms.delete_file_from_bucket({
|
|
4752
4707
|
// 'dev.xuda.io': {
|
|
4753
4708
|
// ETag: '"0e89207990ce19d8b3e61dfea6e61919"',
|
|
4754
4709
|
// $metadata: {
|
|
@@ -5106,7 +5061,7 @@ const create_and_upload_image_to_drive = async function (drive_type, file_path,
|
|
|
5106
5061
|
// C. Upload to Drive Module
|
|
5107
5062
|
// FIX 2: Sending file_obj directly (not wrapped in object)
|
|
5108
5063
|
|
|
5109
|
-
const drive_ret = await
|
|
5064
|
+
const drive_ret = await drive_ms[`upload_drive_file_${drive_type}`]({ uid, tags, app_id, path: path.join('/', file_path), is_system }, job_id, headers, file_obj);
|
|
5110
5065
|
|
|
5111
5066
|
// FIX 1: Check return code
|
|
5112
5067
|
if (drive_ret.code < 0) {
|
|
@@ -5150,7 +5105,7 @@ export const create_avatar = async function (req, job_id, headers) {
|
|
|
5150
5105
|
throw new Error('invalid url');
|
|
5151
5106
|
}
|
|
5152
5107
|
|
|
5153
|
-
const account_profile_info = await
|
|
5108
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
5154
5109
|
|
|
5155
5110
|
const account_obj = await db_module.get_couch_doc_native('xuda_accounts', uid);
|
|
5156
5111
|
const account_info = account_obj.account_info;
|
|
@@ -5223,7 +5178,7 @@ export const get_profile_avatar = async function (profile_picture, uid, prompt,
|
|
|
5223
5178
|
throw err;
|
|
5224
5179
|
}
|
|
5225
5180
|
imageBase64 = ai_avatar_response.data[0].b64_json;
|
|
5226
|
-
|
|
5181
|
+
account_ms.record_ai_usage(uid, ai_avatar_response.usage.input_tokens, ai_avatar_response.usage.output_tokens, 'profile avatar', prompt, model, { profile_picture }, account_profile_info);
|
|
5227
5182
|
} else {
|
|
5228
5183
|
imageBase64 = Buffer.from(await image_blob_ret.image_blob.arrayBuffer()).toString('base64');
|
|
5229
5184
|
if (account_type === 'business') {
|
|
@@ -5315,7 +5270,7 @@ export const get_profile_avatar = async function (profile_picture, uid, prompt,
|
|
|
5315
5270
|
// The background should be fully removed, remove trees,clouds,sky or any landscape objects`,
|
|
5316
5271
|
// background: 'transparent',
|
|
5317
5272
|
// });
|
|
5318
|
-
//
|
|
5273
|
+
// account_ms.record_ai_usage(uid, ai_avatar_response.usage.input_tokens, ai_avatar_response.usage.output_tokens, 'profile avatar', prompt, model, { profile_picture }, account_profile_info);
|
|
5319
5274
|
// imageBase64 = ai_avatar_response.data[0].b64_json;
|
|
5320
5275
|
if (is_user) {
|
|
5321
5276
|
if (business_has_person) {
|
|
@@ -5342,7 +5297,7 @@ export const get_profile_avatar = async function (profile_picture, uid, prompt,
|
|
|
5342
5297
|
report_ai_status(model, err);
|
|
5343
5298
|
throw err;
|
|
5344
5299
|
}
|
|
5345
|
-
|
|
5300
|
+
account_ms.record_ai_usage(uid, ai_avatar_response.usage.input_tokens, ai_avatar_response.usage.output_tokens, 'profile avatar', prompt, model, { profile_picture }, account_profile_info);
|
|
5346
5301
|
imageBase64 = ai_avatar_response.data[0].b64_json;
|
|
5347
5302
|
}
|
|
5348
5303
|
} else {
|
|
@@ -5414,7 +5369,7 @@ export const get_profile_avatar = async function (profile_picture, uid, prompt,
|
|
|
5414
5369
|
report_ai_status(model, err);
|
|
5415
5370
|
throw err;
|
|
5416
5371
|
}
|
|
5417
|
-
|
|
5372
|
+
account_ms.record_ai_usage(uid, ai_avatar_response.usage.input_tokens, ai_avatar_response.usage.output_tokens, 'profile avatar', prompt, model, { profile_picture }, account_profile_info);
|
|
5418
5373
|
imageBase64 = ai_avatar_response.data[0].b64_json;
|
|
5419
5374
|
avatar_source = 'ai profile';
|
|
5420
5375
|
};
|
|
@@ -5463,7 +5418,7 @@ export const get_profile_avatar = async function (profile_picture, uid, prompt,
|
|
|
5463
5418
|
mimetype: `image/${ext}`,
|
|
5464
5419
|
};
|
|
5465
5420
|
|
|
5466
|
-
let drive_ret = await
|
|
5421
|
+
let drive_ret = await drive_ms.upload_drive_file_user({ uid, path: '/Profile Images' }, job_id, headers, file_obj);
|
|
5467
5422
|
drive_ret.data.avatar_source = avatar_source;
|
|
5468
5423
|
|
|
5469
5424
|
return drive_ret;
|
|
@@ -5637,7 +5592,7 @@ export const get_profile_picture = async function (uid, account_type = 'business
|
|
|
5637
5592
|
}
|
|
5638
5593
|
|
|
5639
5594
|
imageBase64 = await normalizeBase64To1024(image_response.data[0].b64_json, resize);
|
|
5640
|
-
|
|
5595
|
+
account_ms.record_ai_usage(uid, image_response.usage.input_tokens, image_response.usage.output_tokens, 'profile image', prompt, model, { url }, account_profile_info);
|
|
5641
5596
|
}
|
|
5642
5597
|
|
|
5643
5598
|
const outputBuffer = Buffer.from(imageBase64, 'base64');
|
|
@@ -5654,7 +5609,7 @@ export const get_profile_picture = async function (uid, account_type = 'business
|
|
|
5654
5609
|
mimetype: `image/png`,
|
|
5655
5610
|
};
|
|
5656
5611
|
|
|
5657
|
-
drive_ret = await
|
|
5612
|
+
drive_ret = await drive_ms.upload_drive_file_user({ uid, path: '/Profile Images' }, job_id, headers, file_obj);
|
|
5658
5613
|
drive_ret.profile_picture_source = profile_picture_source;
|
|
5659
5614
|
}
|
|
5660
5615
|
return drive_ret;
|
|
@@ -5716,7 +5671,7 @@ export const conversation_actions = async function (req, job_id, headers) {
|
|
|
5716
5671
|
mimetype: `application/pdf`,
|
|
5717
5672
|
};
|
|
5718
5673
|
|
|
5719
|
-
const drive_ret = await
|
|
5674
|
+
const drive_ret = await drive_ms.upload_drive_file_user({ uid, path: '/' }, job_id, headers, file_obj);
|
|
5720
5675
|
|
|
5721
5676
|
return {
|
|
5722
5677
|
code: 20,
|
|
@@ -5747,7 +5702,7 @@ export const modify_profile_avatar = async function (req, job_id, headers) {
|
|
|
5747
5702
|
// the function modify human avatar
|
|
5748
5703
|
const { methods = [], docType, uid, url, _id, name, account_type } = req;
|
|
5749
5704
|
let { metadata } = req;
|
|
5750
|
-
const account_profile_info = await
|
|
5705
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
5751
5706
|
|
|
5752
5707
|
try {
|
|
5753
5708
|
let prompt = '';
|
|
@@ -5862,7 +5817,7 @@ const create_ai_agent_image = async function (req, job_id, headers) {
|
|
|
5862
5817
|
const tempOutputPath = path.join(tempDir, `output_${uniqueId}.png`);
|
|
5863
5818
|
|
|
5864
5819
|
const { app_id, uid, ai_agent_id, tags } = req;
|
|
5865
|
-
const account_profile_info = await
|
|
5820
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
5866
5821
|
const ai_agent_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, ai_agent_id);
|
|
5867
5822
|
const account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
|
|
5868
5823
|
|
|
@@ -5953,7 +5908,7 @@ const create_ai_agent_image = async function (req, job_id, headers) {
|
|
|
5953
5908
|
throw err;
|
|
5954
5909
|
}
|
|
5955
5910
|
imageBase64 = ai_avatar_response.data[0].b64_json;
|
|
5956
|
-
|
|
5911
|
+
account_ms.record_ai_usage(uid, ai_avatar_response.usage.input_tokens, ai_avatar_response.usage.output_tokens, 'agent avatar', prompt, model, { ai_agent_id }, account_profile_info);
|
|
5957
5912
|
console.log('Normalizing final avatar to 1024 with transparent padding...');
|
|
5958
5913
|
imageBase64 = await normalizeBase64To1024(imageBase64);
|
|
5959
5914
|
|
|
@@ -5977,7 +5932,7 @@ const create_ai_agent_image = async function (req, job_id, headers) {
|
|
|
5977
5932
|
mimetype: `image/${ext}`,
|
|
5978
5933
|
};
|
|
5979
5934
|
|
|
5980
|
-
const drive_ret = await
|
|
5935
|
+
const drive_ret = await drive_ms.upload_drive_file_studio({ uid, app_id, path: '/Progs Thumbnails' }, job_id, headers, file_obj);
|
|
5981
5936
|
|
|
5982
5937
|
if (drive_ret.code > 0) {
|
|
5983
5938
|
return drive_ret;
|
|
@@ -5995,8 +5950,8 @@ const create_ai_agent_image = async function (req, job_id, headers) {
|
|
|
5995
5950
|
export const create_mini_app = async function (req, job_id, headers) {
|
|
5996
5951
|
const { uid, prompt } = req;
|
|
5997
5952
|
try {
|
|
5998
|
-
const { account_project_id: app_id, account_info } = await
|
|
5999
|
-
const account_profile_info = await
|
|
5953
|
+
const { account_project_id: app_id, account_info } = await account_ms.get_default_project_account_doc(uid);
|
|
5954
|
+
const account_profile_info = await account_ms.get_active_account_profile_info(uid);
|
|
6000
5955
|
const { account_profile_obj } = account_profile_info;
|
|
6001
5956
|
|
|
6002
5957
|
const param = { uid, properties: { menuName: prompt, menuTitle: prompt, menuType: 'component' }, checkedInUserName: account_info.first_name + ' ' + account_info.last_name, app_id, parentId: 'programs' };
|
|
@@ -6021,7 +5976,7 @@ export const create_mini_app = async function (req, job_id, headers) {
|
|
|
6021
5976
|
export const pin_ai_agent = async function (req, job_id, headers) {
|
|
6022
5977
|
const { uid, agent_id } = req;
|
|
6023
5978
|
try {
|
|
6024
|
-
const app_id = await
|
|
5979
|
+
const app_id = await account_ms.get_account_default_project_id(uid);
|
|
6025
5980
|
const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
|
|
6026
5981
|
|
|
6027
5982
|
ai_agent_doc.studio_meta.pinned = true;
|
|
@@ -6053,7 +6008,7 @@ export const pin_ai_agent = async function (req, job_id, headers) {
|
|
|
6053
6008
|
export const unpin_ai_agent = async function (req, job_id, headers) {
|
|
6054
6009
|
const { uid, agent_id } = req;
|
|
6055
6010
|
try {
|
|
6056
|
-
const app_id = await
|
|
6011
|
+
const app_id = await account_ms.get_account_default_project_id(uid);
|
|
6057
6012
|
const ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
|
|
6058
6013
|
|
|
6059
6014
|
ai_agent_doc.studio_meta.pinned = false;
|
|
@@ -6092,7 +6047,7 @@ export const get_transcript = async function (uid, app_id, conversation_id, acco
|
|
|
6092
6047
|
return Buffer.concat(chunks);
|
|
6093
6048
|
}
|
|
6094
6049
|
|
|
6095
|
-
const validation_ret =
|
|
6050
|
+
const validation_ret = drive_ms.file_upload_validator(filename);
|
|
6096
6051
|
if (!validation_ret.valid) {
|
|
6097
6052
|
throw new Error(validation_ret.error);
|
|
6098
6053
|
}
|
|
@@ -6220,7 +6175,7 @@ const attachment_handler = async function (uid, app_id, conversation_id, attachm
|
|
|
6220
6175
|
// return Buffer.concat(chunks);
|
|
6221
6176
|
// }
|
|
6222
6177
|
|
|
6223
|
-
// const validation_ret =
|
|
6178
|
+
// const validation_ret = drive_ms.file_upload_validator(filename);
|
|
6224
6179
|
// if (!validation_ret.valid) {
|
|
6225
6180
|
// throw new Error(validation_ret.error);
|
|
6226
6181
|
// }
|
|
@@ -6324,7 +6279,7 @@ const attachment_handler = async function (uid, app_id, conversation_id, attachm
|
|
|
6324
6279
|
let transcript;
|
|
6325
6280
|
try {
|
|
6326
6281
|
if (conversation_doc.reference_type === 'contacts' && conversation_doc.conversation_type === 'email') {
|
|
6327
|
-
let contact_doc = await
|
|
6282
|
+
let contact_doc = await account_ms.get_contact(uid, conversation_doc.reference_id);
|
|
6328
6283
|
if (!contact_doc.deep_research) throw new Error('deep research not set');
|
|
6329
6284
|
}
|
|
6330
6285
|
|
|
@@ -6641,7 +6596,7 @@ const get_chat_picture_cache = async function (uid, docType, key) {
|
|
|
6641
6596
|
const ret = await db_module.find_couch_query('xuda_cache', query);
|
|
6642
6597
|
|
|
6643
6598
|
if (ret.docs.length > 0) {
|
|
6644
|
-
|
|
6599
|
+
account_ms.save_cache_hit(ret.docs[0]._id);
|
|
6645
6600
|
return ret.docs[0].file;
|
|
6646
6601
|
}
|
|
6647
6602
|
}
|
|
@@ -6666,7 +6621,7 @@ const save_drive_file_to_disk = async function (drive_file) {
|
|
|
6666
6621
|
const tempDir = os.tmpdir();
|
|
6667
6622
|
|
|
6668
6623
|
const tempOutputPath = path.join(tempDir, drive_file.filename + '_' + Date.now());
|
|
6669
|
-
const validation_ret =
|
|
6624
|
+
const validation_ret = drive_ms.file_upload_validator(drive_file.filename);
|
|
6670
6625
|
|
|
6671
6626
|
// 5. Write the downloaded image to disk
|
|
6672
6627
|
fs.writeFileSync(tempOutputPath, imageBuffer);
|