@eko-ai/eko 1.3.2 → 1.3.4

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.
@@ -14,6 +14,7 @@ export declare class Eko {
14
14
  workflow?: Workflow;
15
15
  constructor(llmConfig: LLMConfig, ekoConfig?: EkoConfig);
16
16
  static getLogger(): Logger<ILogObj>;
17
+ getLoggerInstaceUUID(): string;
17
18
  private buildEkoConfig;
18
19
  private registerTools;
19
20
  generate(prompt: string, tabs?: chrome.tabs.Tab[], param?: EkoInvokeParam): Promise<Workflow>;
@@ -8,12 +8,6 @@ export declare class TabManagement implements Tool<TabManagementParam, TabManage
8
8
  description: string;
9
9
  input_schema: InputSchema;
10
10
  constructor();
11
- /**
12
- * Tab management
13
- *
14
- * @param {*} params { command: `new_tab [url]` | 'tab_all' | 'current_tab' | 'go_back' | 'close_tab' | 'switch_tab [tabId]' }
15
- * @returns > { result, success: true }
16
- */
17
11
  execute(context: ExecutionContext, params: TabManagementParam): Promise<TabManagementResult>;
18
12
  destroy(context: ExecutionContext): void;
19
13
  }
@@ -600,9 +600,31 @@ class Logger extends BaseLogger {
600
600
  }
601
601
 
602
602
  function transportFormatted(logMetaMarkup, logArgs, logErrors, settings) {
603
- const logErrorsStr = (logErrors.length > 0 && logArgs.length > 0 ? "\n" : "") + logErrors.join("\n");
604
603
  settings.prettyInspectOptions.colors = settings.stylePrettyLogs;
605
- console.log(logMetaMarkup, ...logArgs, logErrorsStr);
604
+ const logLevel = logMetaMarkup.trim().split(" ")[2];
605
+ let logFunc;
606
+ switch (logLevel) {
607
+ case "WARN":
608
+ logFunc = console.warn;
609
+ break;
610
+ case "ERROR":
611
+ case "FATAL":
612
+ logFunc = console.error;
613
+ break;
614
+ case "INFO":
615
+ logFunc = console.info;
616
+ break;
617
+ case "DEBUG":
618
+ case "TRACE":
619
+ case "SILLY":
620
+ default:
621
+ logFunc = console.debug;
622
+ break;
623
+ }
624
+ logFunc(logMetaMarkup, ...logArgs);
625
+ logErrors.forEach(err => {
626
+ console.error(logMetaMarkup + err);
627
+ });
606
628
  }
607
629
  function formatMeta(logObjMeta) {
608
630
  if (!logObjMeta) {
@@ -663,51 +685,34 @@ async function getWindowId(context) {
663
685
  return windowId;
664
686
  }
665
687
  async function getTabId(context) {
666
- logger.debug("debug the getTabId()...");
667
- let tabId = context.variables.get('tabId');
668
- if (tabId) {
669
- try {
670
- await context.ekoConfig.chromeProxy.tabs.get(tabId);
671
- }
672
- catch (e) {
673
- tabId = null;
674
- context.variables.delete('tabId');
675
- }
676
- }
677
- if (!tabId) {
678
- logger.debug("tabId is empty");
679
- let windowId = await getWindowId(context);
680
- logger.debug(`windowId=${windowId}`);
681
- if (windowId) {
682
- try {
683
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy, windowId);
684
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy, windowId) returns " + tabId);
685
- }
686
- catch (e) {
687
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy);
688
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy, windowId) throws an error");
689
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy) returns " + tabId);
690
- context.variables.delete('windowId');
691
- }
688
+ logger.debug("getTabId()...");
689
+ let tabs = await context.ekoConfig.chromeProxy.tabs.query({});
690
+ logger.debug("all tabs:", tabs);
691
+ const filtered = tabs.filter((tab) => tab.title && tab.url);
692
+ logger.debug("filtered:", filtered);
693
+ if (filtered.length > 0) {
694
+ if (typeof filtered[0].activeTime != "undefined") {
695
+ const sorted = filtered.sort((a, b) => parseInt(b.activeTime) - parseInt(a.activeTime));
696
+ logger.debug("sorted tabs:", sorted);
697
+ const tabId = sorted[0].id;
698
+ logger.debug("tabId:", tabId);
699
+ return tabId;
692
700
  }
693
701
  else {
694
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy);
695
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy) #2 returns " + tabId);
696
- }
697
- logger.debug("tabId:", tabId);
698
- if (!tabId) {
699
- const fellouTabId = window.__FELLOU_TAB_ID__;
700
- if (fellouTabId) {
701
- tabId = fellouTabId;
702
+ tabs = await context.ekoConfig.chromeProxy.tabs.query({ active: true, currentWindow: true });
703
+ if (tabs.length > 0) {
704
+ const tabId = tabs[0].id;
705
+ logger.debug("tabId:", tabId);
706
+ return tabId;
702
707
  }
703
708
  else {
704
- throw new Error('Could not find a valid tab');
709
+ throw Error("no active tab found");
705
710
  }
706
711
  }
707
- context.variables.set('tabId', tabId);
708
712
  }
709
- logger.debug(`debug the getTabId()...returns ${tabId}`);
710
- return tabId;
713
+ else {
714
+ throw Error("no tab found");
715
+ }
711
716
  }
712
717
  function getCurrentTabId(chromeProxy, windowId) {
713
718
  return new Promise((resolve, reject) => {
@@ -1955,65 +1960,31 @@ class TabManagement {
1955
1960
  type: 'string',
1956
1961
  description: `The command to perform. The available commands are:
1957
1962
  * \`tab_all\`: View all tabs and return the tabId and title.
1958
- * \`current_tab\`: Get current tab information (tabId, url, title).
1959
1963
  * \`go_back\`: Go back to the previous page in the current tab.
1960
- * \`close_tab\`: Close the current tab.`,
1961
- enum: ['tab_all', 'current_tab', 'go_back', 'close_tab'],
1964
+ * \`switch_tab\`: Switch to the specified tab by tabId.`,
1965
+ enum: ['tab_all', 'go_back', 'switch_tab'],
1966
+ },
1967
+ tabId: {
1968
+ type: 'integer',
1969
+ description: "Tab id. Only needed when using 'switch_tab'",
1962
1970
  },
1963
1971
  },
1964
1972
  required: ['command'],
1965
1973
  };
1966
1974
  }
1967
- /**
1968
- * Tab management
1969
- *
1970
- * @param {*} params { command: `new_tab [url]` | 'tab_all' | 'current_tab' | 'go_back' | 'close_tab' | 'switch_tab [tabId]' }
1971
- * @returns > { result, success: true }
1972
- */
1973
1975
  async execute(context, params) {
1974
1976
  if (params === null || !params.command) {
1975
1977
  throw new Error('Invalid parameters. Expected an object with a "command" property.');
1976
1978
  }
1977
- let windowId = await getWindowId(context);
1978
- let command = params.command.trim();
1979
- if (command.startsWith('`')) {
1980
- command = command.substring(1);
1981
- }
1982
- if (command.endsWith('`')) {
1983
- command = command.substring(0, command.length - 1);
1984
- }
1985
- let result;
1986
- if (command == 'tab_all') {
1987
- result = [];
1988
- let tabs = await context.ekoConfig.chromeProxy.tabs.query({ windowId: windowId });
1989
- for (let i = 0; i < tabs.length; i++) {
1990
- let tab = tabs[i];
1991
- let tabInfo = {
1992
- tabId: tab.id,
1993
- windowId: tab.windowId,
1994
- title: tab.title,
1995
- url: tab.url,
1996
- };
1997
- if (tab.active) {
1998
- tabInfo.active = true;
1999
- }
2000
- result.push(tabInfo);
2001
- }
2002
- }
2003
- else if (command == 'current_tab') {
2004
- let tabId = await getTabId(context);
2005
- let tab = await context.ekoConfig.chromeProxy.tabs.get(tabId);
2006
- let tabInfo = { tabId, windowId: tab.windowId, title: tab.title, url: tab.url };
2007
- result = tabInfo;
2008
- }
2009
- else if (command == 'go_back') {
1979
+ if (params.command == 'tab_all') ;
1980
+ else if (params.command == 'go_back') {
2010
1981
  let tabId = await getTabId(context);
2011
1982
  await context.ekoConfig.chromeProxy.tabs.goBack(tabId);
2012
- let tab = await context.ekoConfig.chromeProxy.tabs.get(tabId);
2013
- let tabInfo = { tabId, windowId: tab.windowId, title: tab.title, url: tab.url };
2014
- result = tabInfo;
2015
1983
  }
2016
- else if (command == 'close_tab') {
1984
+ else if (params.command == "switch_tab") {
1985
+ await context.ekoConfig.chromeProxy.tabs.select(params.tabId);
1986
+ }
1987
+ else if (params.command == 'close_tab') {
2017
1988
  let closedTabId = await getTabId(context);
2018
1989
  await context.ekoConfig.chromeProxy.tabs.remove(closedTabId);
2019
1990
  await sleep(100);
@@ -2025,16 +1996,28 @@ class TabManagement {
2025
1996
  if (!tab.active) {
2026
1997
  await context.ekoConfig.chromeProxy.tabs.update(tab.id, { active: true });
2027
1998
  }
2028
- let newTabId = tab.id;
2029
1999
  context.variables.set('tabId', tab.id);
2030
2000
  context.variables.set('windowId', tab.windowId);
2031
- let closeTabInfo = { closedTabId, newTabId, newTabTitle: tab.title };
2032
- result = closeTabInfo;
2033
2001
  }
2034
2002
  else {
2035
- throw Error('Unknown command: ' + command);
2003
+ throw Error('Unknown command: ' + params.command);
2004
+ }
2005
+ // build return value
2006
+ let tabs = await context.ekoConfig.chromeProxy.tabs.query({});
2007
+ tabs = tabs.filter((tab) => tab.title && tab.url);
2008
+ if (tabs.length > 0) {
2009
+ let result = "After operation, the existing tabs are as follows:\n";
2010
+ for (const tab of tabs) {
2011
+ result += `<tab><id>${tab.id}</id><title>${tab.title}</title><url>${tab.url}</url></tab>\n`;
2012
+ }
2013
+ let currentTabId = await getTabId(context);
2014
+ let currentTab = await context.ekoConfig.chromeProxy.tabs.get(currentTabId);
2015
+ result += `The current active tab: <tab><id>${currentTab.id}</id><title>${currentTab.title}</title><url>${currentTab.url}</url></tab>`;
2016
+ return result;
2017
+ }
2018
+ else {
2019
+ return "No existing tab. Use 'open_url' to open a new tab";
2036
2020
  }
2037
- return result;
2038
2021
  }
2039
2022
  destroy(context) {
2040
2023
  let windowIds = context.variables.get('windowIds');
@@ -598,9 +598,31 @@ class Logger extends BaseLogger {
598
598
  }
599
599
 
600
600
  function transportFormatted(logMetaMarkup, logArgs, logErrors, settings) {
601
- const logErrorsStr = (logErrors.length > 0 && logArgs.length > 0 ? "\n" : "") + logErrors.join("\n");
602
601
  settings.prettyInspectOptions.colors = settings.stylePrettyLogs;
603
- console.log(logMetaMarkup, ...logArgs, logErrorsStr);
602
+ const logLevel = logMetaMarkup.trim().split(" ")[2];
603
+ let logFunc;
604
+ switch (logLevel) {
605
+ case "WARN":
606
+ logFunc = console.warn;
607
+ break;
608
+ case "ERROR":
609
+ case "FATAL":
610
+ logFunc = console.error;
611
+ break;
612
+ case "INFO":
613
+ logFunc = console.info;
614
+ break;
615
+ case "DEBUG":
616
+ case "TRACE":
617
+ case "SILLY":
618
+ default:
619
+ logFunc = console.debug;
620
+ break;
621
+ }
622
+ logFunc(logMetaMarkup, ...logArgs);
623
+ logErrors.forEach(err => {
624
+ console.error(logMetaMarkup + err);
625
+ });
604
626
  }
605
627
  function formatMeta(logObjMeta) {
606
628
  if (!logObjMeta) {
@@ -661,51 +683,34 @@ async function getWindowId(context) {
661
683
  return windowId;
662
684
  }
663
685
  async function getTabId(context) {
664
- logger.debug("debug the getTabId()...");
665
- let tabId = context.variables.get('tabId');
666
- if (tabId) {
667
- try {
668
- await context.ekoConfig.chromeProxy.tabs.get(tabId);
669
- }
670
- catch (e) {
671
- tabId = null;
672
- context.variables.delete('tabId');
673
- }
674
- }
675
- if (!tabId) {
676
- logger.debug("tabId is empty");
677
- let windowId = await getWindowId(context);
678
- logger.debug(`windowId=${windowId}`);
679
- if (windowId) {
680
- try {
681
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy, windowId);
682
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy, windowId) returns " + tabId);
683
- }
684
- catch (e) {
685
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy);
686
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy, windowId) throws an error");
687
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy) returns " + tabId);
688
- context.variables.delete('windowId');
689
- }
686
+ logger.debug("getTabId()...");
687
+ let tabs = await context.ekoConfig.chromeProxy.tabs.query({});
688
+ logger.debug("all tabs:", tabs);
689
+ const filtered = tabs.filter((tab) => tab.title && tab.url);
690
+ logger.debug("filtered:", filtered);
691
+ if (filtered.length > 0) {
692
+ if (typeof filtered[0].activeTime != "undefined") {
693
+ const sorted = filtered.sort((a, b) => parseInt(b.activeTime) - parseInt(a.activeTime));
694
+ logger.debug("sorted tabs:", sorted);
695
+ const tabId = sorted[0].id;
696
+ logger.debug("tabId:", tabId);
697
+ return tabId;
690
698
  }
691
699
  else {
692
- tabId = await getCurrentTabId(context.ekoConfig.chromeProxy);
693
- logger.debug("getCurrentTabId(context.ekoConfig.chromeProxy) #2 returns " + tabId);
694
- }
695
- logger.debug("tabId:", tabId);
696
- if (!tabId) {
697
- const fellouTabId = window.__FELLOU_TAB_ID__;
698
- if (fellouTabId) {
699
- tabId = fellouTabId;
700
+ tabs = await context.ekoConfig.chromeProxy.tabs.query({ active: true, currentWindow: true });
701
+ if (tabs.length > 0) {
702
+ const tabId = tabs[0].id;
703
+ logger.debug("tabId:", tabId);
704
+ return tabId;
700
705
  }
701
706
  else {
702
- throw new Error('Could not find a valid tab');
707
+ throw Error("no active tab found");
703
708
  }
704
709
  }
705
- context.variables.set('tabId', tabId);
706
710
  }
707
- logger.debug(`debug the getTabId()...returns ${tabId}`);
708
- return tabId;
711
+ else {
712
+ throw Error("no tab found");
713
+ }
709
714
  }
710
715
  function getCurrentTabId(chromeProxy, windowId) {
711
716
  return new Promise((resolve, reject) => {
@@ -1953,65 +1958,31 @@ class TabManagement {
1953
1958
  type: 'string',
1954
1959
  description: `The command to perform. The available commands are:
1955
1960
  * \`tab_all\`: View all tabs and return the tabId and title.
1956
- * \`current_tab\`: Get current tab information (tabId, url, title).
1957
1961
  * \`go_back\`: Go back to the previous page in the current tab.
1958
- * \`close_tab\`: Close the current tab.`,
1959
- enum: ['tab_all', 'current_tab', 'go_back', 'close_tab'],
1962
+ * \`switch_tab\`: Switch to the specified tab by tabId.`,
1963
+ enum: ['tab_all', 'go_back', 'switch_tab'],
1964
+ },
1965
+ tabId: {
1966
+ type: 'integer',
1967
+ description: "Tab id. Only needed when using 'switch_tab'",
1960
1968
  },
1961
1969
  },
1962
1970
  required: ['command'],
1963
1971
  };
1964
1972
  }
1965
- /**
1966
- * Tab management
1967
- *
1968
- * @param {*} params { command: `new_tab [url]` | 'tab_all' | 'current_tab' | 'go_back' | 'close_tab' | 'switch_tab [tabId]' }
1969
- * @returns > { result, success: true }
1970
- */
1971
1973
  async execute(context, params) {
1972
1974
  if (params === null || !params.command) {
1973
1975
  throw new Error('Invalid parameters. Expected an object with a "command" property.');
1974
1976
  }
1975
- let windowId = await getWindowId(context);
1976
- let command = params.command.trim();
1977
- if (command.startsWith('`')) {
1978
- command = command.substring(1);
1979
- }
1980
- if (command.endsWith('`')) {
1981
- command = command.substring(0, command.length - 1);
1982
- }
1983
- let result;
1984
- if (command == 'tab_all') {
1985
- result = [];
1986
- let tabs = await context.ekoConfig.chromeProxy.tabs.query({ windowId: windowId });
1987
- for (let i = 0; i < tabs.length; i++) {
1988
- let tab = tabs[i];
1989
- let tabInfo = {
1990
- tabId: tab.id,
1991
- windowId: tab.windowId,
1992
- title: tab.title,
1993
- url: tab.url,
1994
- };
1995
- if (tab.active) {
1996
- tabInfo.active = true;
1997
- }
1998
- result.push(tabInfo);
1999
- }
2000
- }
2001
- else if (command == 'current_tab') {
2002
- let tabId = await getTabId(context);
2003
- let tab = await context.ekoConfig.chromeProxy.tabs.get(tabId);
2004
- let tabInfo = { tabId, windowId: tab.windowId, title: tab.title, url: tab.url };
2005
- result = tabInfo;
2006
- }
2007
- else if (command == 'go_back') {
1977
+ if (params.command == 'tab_all') ;
1978
+ else if (params.command == 'go_back') {
2008
1979
  let tabId = await getTabId(context);
2009
1980
  await context.ekoConfig.chromeProxy.tabs.goBack(tabId);
2010
- let tab = await context.ekoConfig.chromeProxy.tabs.get(tabId);
2011
- let tabInfo = { tabId, windowId: tab.windowId, title: tab.title, url: tab.url };
2012
- result = tabInfo;
2013
1981
  }
2014
- else if (command == 'close_tab') {
1982
+ else if (params.command == "switch_tab") {
1983
+ await context.ekoConfig.chromeProxy.tabs.select(params.tabId);
1984
+ }
1985
+ else if (params.command == 'close_tab') {
2015
1986
  let closedTabId = await getTabId(context);
2016
1987
  await context.ekoConfig.chromeProxy.tabs.remove(closedTabId);
2017
1988
  await sleep(100);
@@ -2023,16 +1994,28 @@ class TabManagement {
2023
1994
  if (!tab.active) {
2024
1995
  await context.ekoConfig.chromeProxy.tabs.update(tab.id, { active: true });
2025
1996
  }
2026
- let newTabId = tab.id;
2027
1997
  context.variables.set('tabId', tab.id);
2028
1998
  context.variables.set('windowId', tab.windowId);
2029
- let closeTabInfo = { closedTabId, newTabId, newTabTitle: tab.title };
2030
- result = closeTabInfo;
2031
1999
  }
2032
2000
  else {
2033
- throw Error('Unknown command: ' + command);
2001
+ throw Error('Unknown command: ' + params.command);
2002
+ }
2003
+ // build return value
2004
+ let tabs = await context.ekoConfig.chromeProxy.tabs.query({});
2005
+ tabs = tabs.filter((tab) => tab.title && tab.url);
2006
+ if (tabs.length > 0) {
2007
+ let result = "After operation, the existing tabs are as follows:\n";
2008
+ for (const tab of tabs) {
2009
+ result += `<tab><id>${tab.id}</id><title>${tab.title}</title><url>${tab.url}</url></tab>\n`;
2010
+ }
2011
+ let currentTabId = await getTabId(context);
2012
+ let currentTab = await context.ekoConfig.chromeProxy.tabs.get(currentTabId);
2013
+ result += `The current active tab: <tab><id>${currentTab.id}</id><title>${currentTab.title}</title><url>${currentTab.url}</url></tab>`;
2014
+ return result;
2015
+ }
2016
+ else {
2017
+ return "No existing tab. Use 'open_url' to open a new tab";
2034
2018
  }
2035
- return result;
2036
2019
  }
2037
2020
  destroy(context) {
2038
2021
  let windowIds = context.variables.get('windowIds');
@@ -598,9 +598,31 @@ class Logger extends BaseLogger {
598
598
  }
599
599
 
600
600
  function transportFormatted(logMetaMarkup, logArgs, logErrors, settings) {
601
- const logErrorsStr = (logErrors.length > 0 && logArgs.length > 0 ? "\n" : "") + logErrors.join("\n");
602
601
  settings.prettyInspectOptions.colors = settings.stylePrettyLogs;
603
- console.log(logMetaMarkup, ...logArgs, logErrorsStr);
602
+ const logLevel = logMetaMarkup.trim().split(" ")[2];
603
+ let logFunc;
604
+ switch (logLevel) {
605
+ case "WARN":
606
+ logFunc = console.warn;
607
+ break;
608
+ case "ERROR":
609
+ case "FATAL":
610
+ logFunc = console.error;
611
+ break;
612
+ case "INFO":
613
+ logFunc = console.info;
614
+ break;
615
+ case "DEBUG":
616
+ case "TRACE":
617
+ case "SILLY":
618
+ default:
619
+ logFunc = console.debug;
620
+ break;
621
+ }
622
+ logFunc(logMetaMarkup, ...logArgs);
623
+ logErrors.forEach(err => {
624
+ console.error(logMetaMarkup + err);
625
+ });
604
626
  }
605
627
  function formatMeta(logObjMeta) {
606
628
  if (!logObjMeta) {