@xuda.io/ai_module 1.1.3972 → 1.1.3973

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 +341 -339
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -4602,393 +4602,395 @@ const ai_chat_conversation = async function (req, job_id, headers) {
4602
4602
  }
4603
4603
 
4604
4604
  for (const agent_id of ai_agents) {
4605
- let ai_agent_doc;
4606
4605
  try {
4607
- ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
4608
- if (ai_agent_doc.studio_meta.shared_from_uid) {
4609
- const reference_doc = _.cloneDeep(ai_agent_doc);
4610
- const app_id = await account_module.get_account_default_project_id(ai_agent_doc.studio_meta.shared_from_uid);
4611
- ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, ai_agent_doc.studio_meta.share_item_id);
4612
- ai_agent_doc.reference_doc = reference_doc;
4613
- } else if (ai_agent_doc.studio_meta.installed_from_uid) {
4614
- const reference_doc = _.cloneDeep(ai_agent_doc);
4615
- const app_id = await account_module.get_account_default_project_id(ai_agent_doc.studio_meta.installed_from_uid);
4616
- ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, ai_agent_doc.studio_meta.share_item_id);
4617
- ai_agent_doc.reference_doc = reference_doc;
4606
+ let ai_agent_doc;
4607
+ try {
4608
+ ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, agent_id);
4609
+ if (ai_agent_doc.studio_meta.shared_from_uid) {
4610
+ const reference_doc = _.cloneDeep(ai_agent_doc);
4611
+ const app_id = await account_module.get_account_default_project_id(ai_agent_doc.studio_meta.shared_from_uid);
4612
+ ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, ai_agent_doc.studio_meta.share_item_id);
4613
+ ai_agent_doc.reference_doc = reference_doc;
4614
+ } else if (ai_agent_doc.studio_meta.installed_from_uid) {
4615
+ const reference_doc = _.cloneDeep(ai_agent_doc);
4616
+ const app_id = await account_module.get_account_default_project_id(ai_agent_doc.studio_meta.installed_from_uid);
4617
+ ai_agent_doc = await db_module.get_app_couch_doc_native(app_id, ai_agent_doc.studio_meta.share_item_id);
4618
+ ai_agent_doc.reference_doc = reference_doc;
4619
+ }
4620
+ } catch (err) {
4621
+ throw new Error(err.message);
4618
4622
  }
4619
- } catch (err) {
4620
- throw new Error(err.message);
4621
- }
4622
4623
 
4623
- if (ai_agent_doc.use_tools_only) {
4624
- modelSettings.toolChoice = 'required';
4625
- }
4624
+ if (ai_agent_doc.use_tools_only) {
4625
+ modelSettings.toolChoice = 'required';
4626
+ }
4626
4627
 
4627
- let tools = [];
4628
- let tool_resources = {};
4628
+ let tools = [];
4629
+ let tool_resources = {};
4629
4630
 
4630
- for (const [key, val] of Object.entries(ai_agent_doc?.agentConfig?.agent_tools || [])) {
4631
- const { name, description } = val;
4632
- switch (val.type) {
4633
- case 'web_search': {
4634
- //hosted_web_search
4635
- tools.push(webSearchTool());
4636
- break;
4637
- }
4631
+ for (const [key, val] of Object.entries(ai_agent_doc?.agentConfig?.agent_tools || [])) {
4632
+ const { name, description } = val;
4633
+ switch (val.type) {
4634
+ case 'web_search': {
4635
+ //hosted_web_search
4636
+ tools.push(webSearchTool());
4637
+ break;
4638
+ }
4638
4639
 
4639
- case 'mcp': {
4640
- tools.push(
4641
- hostedMcpTool({
4642
- serverLabel: 'mcp_' + (val?.name?.replaceAll(' ', '_') || key), //ai_agent_doc.properties.menuName,
4643
- serverUrl: val.url,
4644
- headers: {
4645
- Authorization: `Bearer ${val.authentication_key || gtp_token}`,
4646
- 'X-gtp-token': 'true',
4647
- 'Content-Type': 'application/json',
4648
- },
4649
- require_approval: 'never',
4650
- }),
4651
- );
4652
- break;
4653
- }
4640
+ case 'mcp': {
4641
+ tools.push(
4642
+ hostedMcpTool({
4643
+ serverLabel: 'mcp_' + (val?.name?.replaceAll(' ', '_') || key), //ai_agent_doc.properties.menuName,
4644
+ serverUrl: val.url,
4645
+ headers: {
4646
+ Authorization: `Bearer ${val.authentication_key || gtp_token}`,
4647
+ 'X-gtp-token': 'true',
4648
+ 'Content-Type': 'application/json',
4649
+ },
4650
+ require_approval: 'never',
4651
+ }),
4652
+ );
4653
+ break;
4654
+ }
4654
4655
 
4655
- case 'cpi': {
4656
- try {
4657
- debugger;
4658
- const api_allow_methods = Object.keys(_conf.cpi_methods);
4659
- for (const method_name of api_allow_methods || []) {
4660
- const method_prop = _conf.cpi_methods[method_name];
4661
-
4662
- if (!method_prop) continue;
4663
- if (method_prop.private) continue;
4664
- if (!method_prop.description) continue;
4665
-
4666
- let inputSchema = {};
4667
-
4668
- const get_z_item = function (key, val) {
4669
- let prop = {};
4670
- if (val.type === 'object' && val.items) {
4671
- for (const [item_key, item_val] of Object.entries(val.items.properties)) {
4672
- prop[item_key] = get_z_item(item_key, item_val);
4673
- if (!val?.items?.required?.includes(item_key)) {
4674
- prop[item_key] = prop[item_key].optional();
4656
+ case 'cpi': {
4657
+ try {
4658
+ debugger;
4659
+ const api_allow_methods = Object.keys(_conf.cpi_methods);
4660
+ for (const method_name of api_allow_methods || []) {
4661
+ const method_prop = _conf.cpi_methods[method_name];
4662
+
4663
+ if (!method_prop) continue;
4664
+ if (method_prop.private) continue;
4665
+ if (!method_prop.description) continue;
4666
+
4667
+ let inputSchema = {};
4668
+
4669
+ const get_z_item = function (key, val) {
4670
+ let prop = {};
4671
+ if (val.type === 'object' && val.items) {
4672
+ for (const [item_key, item_val] of Object.entries(val.items.properties)) {
4673
+ prop[item_key] = get_z_item(item_key, item_val);
4674
+ if (!val?.items?.required?.includes(item_key)) {
4675
+ prop[item_key] = prop[item_key].optional();
4676
+ }
4675
4677
  }
4676
4678
  }
4677
- }
4678
4679
 
4679
- if (val.type === 'array') {
4680
- prop = get_z_item('', val.items);
4681
- }
4680
+ if (val.type === 'array') {
4681
+ prop = get_z_item('', val.items);
4682
+ }
4682
4683
 
4683
- let _z = z[val.type](prop);
4684
+ let _z = z[val.type](prop);
4684
4685
 
4685
- if (!val.mandatory) {
4686
- _z = _z.optional();
4687
- }
4686
+ if (!val.mandatory) {
4687
+ _z = _z.optional();
4688
+ }
4688
4689
 
4689
- if (val.enum) {
4690
- _z = z.enum(val.enum);
4691
- }
4690
+ if (val.enum) {
4691
+ _z = z.enum(val.enum);
4692
+ }
4692
4693
 
4693
- let valid_values = '';
4694
- if (val.options) {
4695
- valid_values = val.options.join(', ');
4696
- }
4697
- if (val.description) {
4698
- const desc = val.name || key.replace(/_/g, ' ').replace(/\b(\w)/g, (_, char) => char.toUpperCase());
4699
- _z = _z.describe((desc ? desc + ' - ' : '') + val.description + (valid_values ? ' Valid values: ' + valid_values : ''));
4700
- }
4694
+ let valid_values = '';
4695
+ if (val.options) {
4696
+ valid_values = val.options.join(', ');
4697
+ }
4698
+ if (val.description) {
4699
+ const desc = val.name || key.replace(/_/g, ' ').replace(/\b(\w)/g, (_, char) => char.toUpperCase());
4700
+ _z = _z.describe((desc ? desc + ' - ' : '') + val.description + (valid_values ? ' Valid values: ' + valid_values : ''));
4701
+ }
4701
4702
 
4702
- return _z;
4703
- };
4703
+ return _z;
4704
+ };
4704
4705
 
4705
- if (method_prop.fields) {
4706
- try {
4707
- for (const [key, val] of Object.entries(method_prop.fields)) {
4708
- inputSchema[key] = get_z_item(key, val);
4706
+ if (method_prop.fields) {
4707
+ try {
4708
+ for (const [key, val] of Object.entries(method_prop.fields)) {
4709
+ inputSchema[key] = get_z_item(key, val);
4710
+ }
4711
+ } catch (error) {
4712
+ debugger;
4709
4713
  }
4714
+ }
4715
+ try {
4716
+ tools.push(
4717
+ tool({
4718
+ name: method_name.replace(/_/g, ' ').replace(/\b(\w)/g, (_, char) => char.toUpperCase()),
4719
+ description: method_prop.description,
4720
+ parameters: z.object(inputSchema),
4721
+ async execute(e, RunContext) {
4722
+ const params = e;
4723
+ try {
4724
+ const response = await fetch(`http://localhost:3007/cpi/${method_name}`, {
4725
+ method: 'POST',
4726
+ headers: {
4727
+ Accept: 'application/json',
4728
+ 'Content-Type': 'application/json',
4729
+ },
4730
+ body: JSON.stringify({ ...params, ...{ gtp_token } }),
4731
+ });
4732
+ const ret = await response.json();
4733
+ return {
4734
+ content: [{ type: 'text', text: JSON.stringify(ret || '', null, 2) }],
4735
+ };
4736
+ } catch (err) {
4737
+ return 'unknown';
4738
+ }
4739
+ },
4740
+ }),
4741
+ );
4710
4742
  } catch (error) {
4711
4743
  debugger;
4712
4744
  }
4713
4745
  }
4714
- try {
4715
- tools.push(
4716
- tool({
4717
- name: method_name.replace(/_/g, ' ').replace(/\b(\w)/g, (_, char) => char.toUpperCase()),
4718
- description: method_prop.description,
4719
- parameters: z.object(inputSchema),
4720
- async execute(e, RunContext) {
4721
- const params = e;
4722
- try {
4723
- const response = await fetch(`http://localhost:3007/cpi/${method_name}`, {
4724
- method: 'POST',
4725
- headers: {
4726
- Accept: 'application/json',
4727
- 'Content-Type': 'application/json',
4728
- },
4729
- body: JSON.stringify({ ...params, ...{ gtp_token } }),
4730
- });
4731
- const ret = await response.json();
4732
- return {
4733
- content: [{ type: 'text', text: JSON.stringify(ret || '', null, 2) }],
4734
- };
4735
- } catch (err) {
4736
- return 'unknown';
4737
- }
4738
- },
4739
- }),
4740
- );
4741
- } catch (error) {
4742
- debugger;
4743
- }
4746
+ } catch (error) {
4747
+ debugger;
4744
4748
  }
4745
- } catch (error) {
4746
- debugger;
4749
+ break;
4747
4750
  }
4748
- break;
4749
- }
4750
4751
 
4751
- // case 'web_search': {
4752
- // tools.push(
4753
- // tool({
4754
- // name: name || val.type,
4755
- // description: description || val.type,
4756
- // parameters: z.object({
4757
- // query: z.string().describe('The search query text.'),
4758
- // numResults: z.number().int().describe('Number of top search results to return.').default(3),
4759
- // }),
4760
- // async execute(e, RunContext) {
4761
- // const { query, numResults } = e;
4762
- // try {
4763
- // const searchUrl = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_redirect=1`;
4764
- // const response = await fetch(searchUrl);
4765
- // const data = await response.json();
4766
-
4767
- // // Simplify results
4768
- // const results = (data.RelatedTopics || []).slice(0, numResults).map((item) => ({
4769
- // title: item.Text,
4770
- // url: item.FirstURL,
4771
- // }));
4772
-
4773
- // return JSON.stringify(results, null, 2);
4774
- // } catch (err) {
4775
- // return 'unknown';
4776
- // }
4777
- // },
4778
- // }),
4779
- // );
4780
- // break;
4781
- // }
4782
-
4783
- // case 'image_generate': {
4784
- // tools.push(
4785
- // tool({
4786
- // name,
4787
- // description,
4788
- // parameters: z.object({
4789
- // prompt: z.string().describe('The prompt for the image.'),
4790
- // numGenerations: z.number().int().describe('Number generations to image create.').default(3),
4791
- // }),
4792
- // async execute(e, RunContext) {
4793
- // const { prompt, numGenerations = val?.image_generations } = e;
4794
- // try {
4795
- // const base64 = await create_image(prompt, undefined, undefined, numGenerations);
4796
- // // console.log(base64);
4797
-
4798
- // // return `<img src="data:image/jpg;base64,${base64}">`;
4799
- // return `![Generated Image](data:image/jpeg;base64,${base64})`;
4800
- // } catch (err) {
4801
- // return 'unknown';
4802
- // }
4803
- // },
4804
- // }),
4805
- // );
4806
- // break;
4807
- // }
4808
-
4809
- case 'image_generate': {
4810
- tools.push(
4811
- tool({
4812
- name,
4813
- description,
4814
- parameters: z.object({
4815
- prompt: z.string().describe('The prompt for the image.'),
4816
- numGenerations: z.number().int().describe('Number generations to image create.').default(3),
4817
- }),
4818
- async execute(e, RunContext) {
4819
- const { prompt, numGenerations } = e;
4820
- try {
4821
- let imgTags = '';
4822
- const images_arr = await create_and_upload_image_to_drive('workspace', 'Chat Images', prompt, numGenerations, uid, app_id, job_id, headers);
4823
- for (const img_obj of images_arr) {
4824
- imgTags += `<img src="${img_obj.file_url}" alt="${prompt}" style="max-width: 100%; border-radius: 8px;" />`;
4825
- }
4752
+ // case 'web_search': {
4753
+ // tools.push(
4754
+ // tool({
4755
+ // name: name || val.type,
4756
+ // description: description || val.type,
4757
+ // parameters: z.object({
4758
+ // query: z.string().describe('The search query text.'),
4759
+ // numResults: z.number().int().describe('Number of top search results to return.').default(3),
4760
+ // }),
4761
+ // async execute(e, RunContext) {
4762
+ // const { query, numResults } = e;
4763
+ // try {
4764
+ // const searchUrl = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_redirect=1`;
4765
+ // const response = await fetch(searchUrl);
4766
+ // const data = await response.json();
4767
+
4768
+ // // Simplify results
4769
+ // const results = (data.RelatedTopics || []).slice(0, numResults).map((item) => ({
4770
+ // title: item.Text,
4771
+ // url: item.FirstURL,
4772
+ // }));
4773
+
4774
+ // return JSON.stringify(results, null, 2);
4775
+ // } catch (err) {
4776
+ // return 'unknown';
4777
+ // }
4778
+ // },
4779
+ // }),
4780
+ // );
4781
+ // break;
4782
+ // }
4826
4783
 
4827
- return imgTags;
4828
- } catch (err) {
4829
- console.error('Image Tool Error:', err);
4830
- return `Error generating image: ${err.message}`;
4831
- }
4832
- },
4833
- }),
4834
- );
4835
- break;
4836
- }
4784
+ // case 'image_generate': {
4785
+ // tools.push(
4786
+ // tool({
4787
+ // name,
4788
+ // description,
4789
+ // parameters: z.object({
4790
+ // prompt: z.string().describe('The prompt for the image.'),
4791
+ // numGenerations: z.number().int().describe('Number generations to image create.').default(3),
4792
+ // }),
4793
+ // async execute(e, RunContext) {
4794
+ // const { prompt, numGenerations = val?.image_generations } = e;
4795
+ // try {
4796
+ // const base64 = await create_image(prompt, undefined, undefined, numGenerations);
4797
+ // // console.log(base64);
4798
+
4799
+ // // return `<img src="data:image/jpg;base64,${base64}">`;
4800
+ // return `![Generated Image](data:image/jpeg;base64,${base64})`;
4801
+ // } catch (err) {
4802
+ // return 'unknown';
4803
+ // }
4804
+ // },
4805
+ // }),
4806
+ // );
4807
+ // break;
4808
+ // }
4837
4809
 
4838
- case 'table': {
4839
- const table_id = val.prog_id;
4840
- // prompt += ` for table_id: ${table_id}`;
4841
- context.table_id = table_id;
4842
- tools.push(get_studio_table_info_by_table_id_tool);
4843
- //======== mock data ==========
4844
- // const mock_data_generator_agent = get_mock_data_generator_agent(app_id, table_id);
4845
- // tools.push(
4846
- // mock_data_generator_agent.asTool({
4847
- // toolName: 'Mock Data Generator',
4848
- // toolDescription: `generate Mock Data to a table`,
4849
- // }),
4850
- // );
4851
- // tools.push(get_studio_table_info_by_table_id_tool);
4852
-
4853
- //======== add data ==========
4854
- tools.push(
4855
- add_data_to_table_agent.asTool({
4856
- toolName: 'add data to table',
4857
- toolDescription: `add data to table`,
4858
- }),
4859
- );
4810
+ case 'image_generate': {
4811
+ tools.push(
4812
+ tool({
4813
+ name,
4814
+ description,
4815
+ parameters: z.object({
4816
+ prompt: z.string().describe('The prompt for the image.'),
4817
+ numGenerations: z.number().int().describe('Number generations to image create.').default(3),
4818
+ }),
4819
+ async execute(e, RunContext) {
4820
+ const { prompt, numGenerations } = e;
4821
+ try {
4822
+ let imgTags = '';
4823
+ const images_arr = await create_and_upload_image_to_drive('workspace', 'Chat Images', prompt, numGenerations, uid, app_id, job_id, headers);
4824
+ for (const img_obj of images_arr) {
4825
+ imgTags += `<img src="${img_obj.file_url}" alt="${prompt}" style="max-width: 100%; border-radius: 8px;" />`;
4826
+ }
4860
4827
 
4861
- //======== fetch data ==========
4862
- tools.push(
4863
- fetch_table_data_agent.asTool({
4864
- toolName: 'Fetch Table Data Agent',
4865
- toolDescription: `fetch data from a table`,
4866
- }),
4867
- );
4828
+ return imgTags;
4829
+ } catch (err) {
4830
+ console.error('Image Tool Error:', err);
4831
+ return `Error generating image: ${err.message}`;
4832
+ }
4833
+ },
4834
+ }),
4835
+ );
4836
+ break;
4837
+ }
4868
4838
 
4869
- //======== delete data ==========
4870
- tools.push(
4871
- delete_table_doc.asTool({
4872
- toolName: 'Delete Table Doc',
4873
- toolDescription: `The tool delete document from the database, need to ask for table`,
4874
- }),
4875
- );
4839
+ case 'table': {
4840
+ const table_id = val.prog_id;
4841
+ // prompt += ` for table_id: ${table_id}`;
4842
+ context.table_id = table_id;
4843
+ tools.push(get_studio_table_info_by_table_id_tool);
4844
+ //======== mock data ==========
4845
+ // const mock_data_generator_agent = get_mock_data_generator_agent(app_id, table_id);
4846
+ // tools.push(
4847
+ // mock_data_generator_agent.asTool({
4848
+ // toolName: 'Mock Data Generator',
4849
+ // toolDescription: `generate Mock Data to a table`,
4850
+ // }),
4851
+ // );
4852
+ // tools.push(get_studio_table_info_by_table_id_tool);
4853
+
4854
+ //======== add data ==========
4855
+ tools.push(
4856
+ add_data_to_table_agent.asTool({
4857
+ toolName: 'add data to table',
4858
+ toolDescription: `add data to table`,
4859
+ }),
4860
+ );
4876
4861
 
4877
- // tools.push(create_table_management_tool);
4862
+ //======== fetch data ==========
4863
+ tools.push(
4864
+ fetch_table_data_agent.asTool({
4865
+ toolName: 'Fetch Table Data Agent',
4866
+ toolDescription: `fetch data from a table`,
4867
+ }),
4868
+ );
4878
4869
 
4879
- // tools.push(create_landing_page_tool);
4870
+ //======== delete data ==========
4871
+ tools.push(
4872
+ delete_table_doc.asTool({
4873
+ toolName: 'Delete Table Doc',
4874
+ toolDescription: `The tool delete document from the database, need to ask for table`,
4875
+ }),
4876
+ );
4880
4877
 
4881
- // const add_data_to_table_agent = get_add_data_to_table_agent(app_id, table_id);
4882
- // tools.push(
4883
- // add_data_to_table_agent.asTool({
4884
- // toolName: 'add data to table',
4885
- // toolDescription: `add data to table`,
4886
- // }),
4887
- // );
4878
+ // tools.push(create_table_management_tool);
4888
4879
 
4889
- break;
4890
- }
4880
+ // tools.push(create_landing_page_tool);
4891
4881
 
4892
- case 'file_search': {
4893
- if (val?.file?.vector_store_id) {
4894
- tools.push(fileSearchTool(val.file.vector_store_id));
4895
- }
4882
+ // const add_data_to_table_agent = get_add_data_to_table_agent(app_id, table_id);
4883
+ // tools.push(
4884
+ // add_data_to_table_agent.asTool({
4885
+ // toolName: 'add data to table',
4886
+ // toolDescription: `add data to table`,
4887
+ // }),
4888
+ // );
4896
4889
 
4897
- if (val?.youtube?.vector_store_id) {
4898
- tools.push(fileSearchTool(val.youtube.vector_store_id));
4890
+ break;
4899
4891
  }
4900
4892
 
4901
- break;
4902
- }
4893
+ case 'file_search': {
4894
+ if (val?.file?.vector_store_id) {
4895
+ tools.push(fileSearchTool(val.file.vector_store_id));
4896
+ }
4903
4897
 
4904
- case 'ai_agent': {
4905
- let opt = {
4906
- selector: {
4907
- docType: 'studio',
4908
- stat: 3,
4909
- 'properties.menuType': 'ai_agent',
4910
- },
4911
- limit: limit || 9999,
4912
- };
4898
+ if (val?.youtube?.vector_store_id) {
4899
+ tools.push(fileSearchTool(val.youtube.vector_store_id));
4900
+ }
4913
4901
 
4914
- // let opt = {};
4915
- // add all agents if no agent id
4916
- if (val.ai_agent_id) {
4917
- opt.selector._id = val.ai_agent_id;
4902
+ break;
4918
4903
  }
4919
4904
 
4920
- const user_agents = await db_module.find_app_couch_query(ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id, opt);
4921
- for (const agent_doc of user_agents.docs) {
4922
- const agent = new Agent({
4923
- name: agent_doc.agent_name,
4924
- instructions: agent_doc.agent_instructions,
4925
- });
4926
-
4927
- tools.push(
4928
- agent.asTool({
4929
- toolName: agent_doc.agent_name,
4930
- toolDescription: agent_doc.agent_instructions,
4931
- }),
4932
- );
4933
- }
4934
- // const agent_ret = await get_ai_agents(opt);
4935
- // if (agent_ret.code > -1) {
4936
- // const agent_doc = agent_ret.data;
4905
+ case 'ai_agent': {
4906
+ let opt = {
4907
+ selector: {
4908
+ docType: 'studio',
4909
+ stat: 3,
4910
+ 'properties.menuType': 'ai_agent',
4911
+ },
4912
+ limit: limit || 9999,
4913
+ };
4937
4914
 
4938
- // const agent = new Agent({
4939
- // name: agent_doc.agent_name,
4940
- // instructions: agent_doc.agent_instructions,
4941
- // });
4915
+ // let opt = {};
4916
+ // add all agents if no agent id
4917
+ if (val.ai_agent_id) {
4918
+ opt.selector._id = val.ai_agent_id;
4919
+ }
4942
4920
 
4943
- // tools.push(
4944
- // agent.asTool({
4945
- // toolName: agent_doc.agent_name,
4946
- // toolDescription: agent_doc.agent_instructions,
4947
- // }),
4948
- // );
4949
- // }
4950
- // }
4951
- break;
4952
- }
4953
- case 'plugin': {
4954
- if (!val.plugin_id) {
4955
- throw new Error('undefined plugin_id');
4921
+ const user_agents = await db_module.find_app_couch_query(ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id, opt);
4922
+ for (const agent_doc of user_agents.docs) {
4923
+ const agent = new Agent({
4924
+ name: agent_doc.agent_name,
4925
+ instructions: agent_doc.agent_instructions,
4926
+ });
4927
+
4928
+ tools.push(
4929
+ agent.asTool({
4930
+ toolName: agent_doc.agent_name,
4931
+ toolDescription: agent_doc.agent_instructions,
4932
+ }),
4933
+ );
4934
+ }
4935
+ // const agent_ret = await get_ai_agents(opt);
4936
+ // if (agent_ret.code > -1) {
4937
+ // const agent_doc = agent_ret.data;
4938
+
4939
+ // const agent = new Agent({
4940
+ // name: agent_doc.agent_name,
4941
+ // instructions: agent_doc.agent_instructions,
4942
+ // });
4943
+
4944
+ // tools.push(
4945
+ // agent.asTool({
4946
+ // toolName: agent_doc.agent_name,
4947
+ // toolDescription: agent_doc.agent_instructions,
4948
+ // }),
4949
+ // );
4950
+ // }
4951
+ // }
4952
+ break;
4956
4953
  }
4957
- if (!val.method) {
4958
- throw new Error('undefined method');
4954
+ case 'plugin': {
4955
+ if (!val.plugin_id) {
4956
+ throw new Error('undefined plugin_id');
4957
+ }
4958
+ if (!val.method) {
4959
+ throw new Error('undefined method');
4960
+ }
4961
+ // const tool = await get_plugin_tool(app_id, uid, val.plugin_id, val.method, {});
4962
+ const tool = await get_plugin_tool(
4963
+ ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id,
4964
+ ai_agent_doc?.reference_doc?.studio_meta?.shared_from_uid || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_uid || uid,
4965
+ val.plugin_id,
4966
+ val.method,
4967
+ {},
4968
+ );
4969
+ tools.push(tool);
4970
+ break;
4959
4971
  }
4960
- // const tool = await get_plugin_tool(app_id, uid, val.plugin_id, val.method, {});
4961
- const tool = await get_plugin_tool(
4962
- ai_agent_doc?.reference_doc?.studio_meta?.shared_from_app_id || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_app_id || app_id,
4963
- ai_agent_doc?.reference_doc?.studio_meta?.shared_from_uid || ai_agent_doc?.reference_doc?.studio_meta?.installed_from_uid || uid,
4964
- val.plugin_id,
4965
- val.method,
4966
- {},
4967
- );
4968
- tools.push(tool);
4969
- break;
4972
+ default:
4973
+ break;
4970
4974
  }
4971
- default:
4972
- break;
4973
4975
  }
4974
- }
4975
- // if (!tools.length) continue;
4976
- const agent = new Agent({
4977
- name: ai_agent_doc._id,
4978
- instructions: ai_agent_doc.agentConfig.agent_instructions + get_agent_instructions(),
4979
- model,
4980
- tools,
4981
- metadata: {
4982
- agent_id: ai_agent_doc._id,
4983
- ts: Date.now(),
4984
- },
4985
- tool_resources,
4986
- modelSettings,
4987
- });
4976
+ // if (!tools.length) continue;
4977
+ const agent = new Agent({
4978
+ name: ai_agent_doc._id,
4979
+ instructions: ai_agent_doc.agentConfig.agent_instructions + get_agent_instructions(),
4980
+ model,
4981
+ tools,
4982
+ metadata: {
4983
+ agent_id: ai_agent_doc._id,
4984
+ ts: Date.now(),
4985
+ },
4986
+ tool_resources,
4987
+ modelSettings,
4988
+ });
4988
4989
 
4989
- init_agent_hooks(agent);
4990
+ init_agent_hooks(agent);
4990
4991
 
4991
- list.push(agent);
4992
+ list.push(agent);
4993
+ } catch (err) {}
4992
4994
  }
4993
4995
  //tell me more on xuda
4994
4996
  // debugger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.3972",
3
+ "version": "1.1.3973",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",