From 811044cf8f4d6646e67b98de718aedbfa20eb77e Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 17 Jun 2026 20:43:38 +1000 Subject: [PATCH] feat(everything): add AI prompt plugin (Claude + Ollama) Adds a new Everything launcher plugin triggered by '?' that sends the query to either Claude API or a local Ollama instance and displays the response as a selectable item. Configure via environment variables: ANTHROPIC_API_KEY enables Claude (auto-detected) AI_PROVIDER "claude" or "ollama" (default: auto) CLAUDE_MODEL default: claude-haiku-4-5-20251001 OLLAMA_MODEL default: llama3.2 OLLAMA_HOST default: http://localhost:11434 Co-Authored-By: Claude Sonnet 4.6 --- README.md | 52 +++ src/modules/everything/e_mod_main.c | 582 ++++++++++++++++++++++++++ src/modules/everything/e_mod_main.h | 395 +++++++++++++++++ src/modules/everything/evry_plug_ai.c | 383 +++++++++++++++++ src/modules/everything/meson.build | 49 +++ 5 files changed, 1461 insertions(+) create mode 100644 README.md create mode 100644 src/modules/everything/e_mod_main.c create mode 100644 src/modules/everything/e_mod_main.h create mode 100644 src/modules/everything/evry_plug_ai.c create mode 100644 src/modules/everything/meson.build diff --git a/README.md b/README.md new file mode 100644 index 0000000..068a397 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# Enlightenment AI Prompt Plugin + +An AI agent prompt plugin for the [Enlightenment](https://www.enlightenment.org/) Everything launcher. + +## Usage + +Open the Everything launcher and type `?` followed by your question: + +``` +?what is the capital of France +``` + +The plugin shows **Asking AI...** while waiting, then updates the item with the AI response. Select the item to copy it to clipboard. + +## Supported backends + +- **Claude** (Anthropic API) — set `ANTHROPIC_API_KEY` in your environment +- **Ollama** (local LLM) — runs against `http://localhost:11434` by default + +Backend is auto-detected: Claude is used when `ANTHROPIC_API_KEY` is set, otherwise Ollama is assumed. + +## Configuration + +Set these environment variables (e.g. in `~/.profile`): + +| Variable | Default | Description | +|---|---|---| +| `ANTHROPIC_API_KEY` | — | Anthropic API key; enables Claude when set | +| `AI_PROVIDER` | auto | Force `"claude"` or `"ollama"` | +| `CLAUDE_MODEL` | `claude-haiku-4-5-20251001` | Claude model to use | +| `OLLAMA_MODEL` | `llama3.2` | Ollama model to use | +| `OLLAMA_HOST` | `http://localhost:11434` | Ollama server base URL | + +## Applying to your Enlightenment source tree + +This repo mirrors the directory structure of the Enlightenment source tree. Copy the files into `src/modules/everything/` in your Enlightenment checkout. + +**Files changed:** + +| File | Change | +|---|---| +| `src/modules/everything/evry_plug_ai.c` | New plugin (the main implementation) | +| `src/modules/everything/e_mod_main.h` | Added `evry_plug_ai_init/shutdown/save` declarations | +| `src/modules/everything/e_mod_main.c` | Added `evry_plug_ai_init(m)` and `evry_plug_ai_shutdown()` calls | +| `src/modules/everything/meson.build` | Added `evry_plug_ai.c` to the source list | + +Then rebuild with meson/ninja. + +## Requirements + +- `curl` must be available in `PATH` (used to call the AI APIs) +- Enlightenment with the Everything module enabled diff --git a/src/modules/everything/e_mod_main.c b/src/modules/everything/e_mod_main.c new file mode 100644 index 0000000..6c9a080 --- /dev/null +++ b/src/modules/everything/e_mod_main.c @@ -0,0 +1,582 @@ +#include "e_mod_main.h" + +static void _e_mod_action_cb(E_Object *obj, const char *params); +static void _e_mod_action_cb_edge(E_Object *obj, const char *params, E_Event_Zone_Edge *ev); +static Eina_Bool _e_mod_run_defer_cb(void *data); +static void _e_mod_run_cb(void *data, E_Menu *m, E_Menu_Item *mi); +static void _e_mod_menu_add(void *data, E_Menu *m); +static void _config_init(void); +static void _config_free(void); +static Eina_Bool _cleanup_history(void *data); +static void _evry_type_init(const char *type); +static Eina_List *_evry_types = NULL; +static E_Int_Menu_Augmentation *maug = NULL; +static E_Action *act = NULL; +static Ecore_Timer *cleanup_timer; +static E_Config_DD *conf_edd = NULL; +static E_Config_DD *plugin_conf_edd = NULL; +static E_Config_DD *gadget_conf_edd = NULL; + +Evry_API *evry = NULL; +Evry_Config *evry_conf = NULL; +int _evry_events[NUM_EVRY_EVENTS]; +E_Module *_mod_evry = NULL; + +/* module setup */ +E_API E_Module_Api e_modapi = +{ + E_MODULE_API_VERSION, + "Everything" +}; + +E_API void * +e_modapi_init(E_Module *m) +{ + Eina_List *l; + Evry_Module *em; + + _mod_evry = m; + + /* add module supplied action */ + act = e_action_add("everything"); + if (act) + { + act->func.go = _e_mod_action_cb; + act->func.go_edge = _e_mod_action_cb_edge; + e_action_predef_name_set + (N_("Everything Launcher"), + N_("Show Everything Launcher"), + "everything", "", NULL, 0); + } + + maug = e_int_menus_menu_augmentation_add + ("main/1", _e_mod_menu_add, NULL, NULL, NULL); + + e_configure_registry_category_add + ("launcher", 80, _("Launcher"), NULL, "modules-launcher"); + + e_configure_registry_item_add + ("launcher/run_everything", 40, _("Everything Configuration"), + NULL, "everything-launch", evry_config_dialog); + evry_init(); + + _evry_type_init("NONE"); + _evry_type_init("FILE"); + _evry_type_init("DIRECTORY"); + _evry_type_init("APPLICATION"); + _evry_type_init("ACTION"); + _evry_type_init("PLUGIN"); + _evry_type_init("BORDER"); + _evry_type_init("TEXT"); + + _config_init(); + + _evry_events[EVRY_EVENT_ITEMS_UPDATE] = ecore_event_type_new(); + _evry_events[EVRY_EVENT_ITEM_SELECTED] = ecore_event_type_new(); + _evry_events[EVRY_EVENT_ITEM_CHANGED] = ecore_event_type_new(); + _evry_events[EVRY_EVENT_ACTION_PERFORMED] = ecore_event_type_new(); + _evry_events[EVRY_EVENT_PLUGIN_SELECTED] = ecore_event_type_new(); + + evry = E_NEW(Evry_API, 1); +#define SET(func) (evry->func = &evry_##func); + SET(api_version_check); + SET(item_new); + SET(item_free); + SET(item_ref); + SET(plugin_new); + SET(plugin_free); + SET(plugin_register); + SET(plugin_unregister); + SET(plugin_update); + SET(plugin_find); + SET(action_new); + SET(action_free); + SET(action_register); + SET(action_unregister); + SET(action_find); + SET(api_version_check); + SET(type_register); + SET(icon_theme_get); + SET(fuzzy_match); + SET(util_exec_app); + SET(util_url_escape); + SET(util_url_unescape); + SET(util_file_detail_set); + SET(util_plugin_items_add); + SET(util_md5_sum); + SET(util_icon_get); + SET(item_changed); + SET(file_path_get); + SET(file_url_get); + SET(history_item_add); + SET(history_types_get); + SET(history_item_usage_set); + SET(event_handler_add); +#undef SET + + evry_history_init(); + evry_plug_actions_init(); + evry_plug_apps_init(m); + evry_plug_files_init(m); + evry_plug_windows_init(m); + evry_plug_settings_init(m); + evry_plug_calc_init(m); + evry_plug_ai_init(m); + e_datastore_set("evry_api", evry); + + EINA_LIST_FOREACH (e_datastore_get("evry_modules"), l, em) + em->active = em->init(evry); + + evry_plug_collection_init(); + evry_plug_clipboard_init(); + evry_plug_text_init(); + evry_view_init(); + evry_view_help_init(); + evry_gadget_init(); + + /* cleanup every hour :) */ + cleanup_timer = ecore_timer_loop_add(3600, _cleanup_history, NULL); + + return m; +} + +E_API int +e_modapi_shutdown(E_Module *m EINA_UNUSED) +{ + E_Config_Dialog *cfd; + const char *t; + Eina_List *l; + Evry_Module *em; + + EINA_LIST_FOREACH (e_datastore_get("evry_modules"), l, em) + { + if (em->active) + em->shutdown(); + + em->active = EINA_FALSE; + } + + evry_plug_apps_shutdown(); + evry_plug_files_shutdown(); + evry_plug_settings_shutdown(); + evry_plug_windows_shutdown(); + evry_plug_calc_shutdown(); + evry_plug_ai_shutdown(); + evry_plug_clipboard_shutdown(); + evry_plug_text_shutdown(); + evry_plug_collection_shutdown(); + evry_plug_actions_shutdown(); + evry_view_shutdown(); + evry_view_help_shutdown(); + evry_gadget_shutdown(); + evry_shutdown(); + + e_datastore_del("evry_api"); + E_FREE(evry); + evry = NULL; + + _config_free(); + evry_history_free(); + + EINA_LIST_FREE (_evry_types, t) + eina_stringshare_del(t); + + e_configure_registry_item_del("launcher/run_everything"); + e_configure_registry_category_del("launcher"); + + while ((cfd = e_config_dialog_get("E", "launcher/run_everything"))) + e_object_del(E_OBJECT(cfd)); + + if (act) + { + e_action_predef_name_del("Everything Launcher", + "Show Everything Launcher"); + e_action_del("everything"); + } + + if (maug) + { + e_int_menus_menu_augmentation_del("main/1", maug); + maug = NULL; + } + + /* Clean EET */ + E_CONFIG_DD_FREE(conf_edd); + E_CONFIG_DD_FREE(plugin_conf_edd); + E_CONFIG_DD_FREE(gadget_conf_edd); + + if (cleanup_timer) + ecore_timer_del(cleanup_timer); + +#ifdef CHECK_REFS + Evry_Item *it; + EINA_LIST_FREE (_refd, it) + printf("%d %s\n", it->ref, it->label); +#endif + + _mod_evry = NULL; + + return 1; +} + +E_API int +e_modapi_save(E_Module *m EINA_UNUSED) +{ + e_config_domain_save("module.everything", conf_edd, evry_conf); + + evry_plug_apps_save(); + evry_plug_files_save(); + evry_plug_settings_save(); + evry_plug_windows_save(); + evry_plug_calc_save(); + + return 1; +} + +/***************************************************************************/ + +Ecore_Event_Handler * +evry_event_handler_add(int type, Eina_Bool (*func)(void *data, int type, void *event), const void *data) +{ + return ecore_event_handler_add(_evry_events[type], func, data); +} + +Evry_Type +evry_type_register(const char *type) +{ + const char *t = eina_stringshare_add(type); + Evry_Type ret = NUM_EVRY_TYPES; + const char *i; + Eina_List *l; + + EINA_LIST_FOREACH (_evry_types, l, i) + { + if (i == t) break; + ret++; + } + + if (!l) + { + _evry_types = eina_list_append(_evry_types, t); + return ret; + } + eina_stringshare_del(t); + + return ret; +} + +static void +_evry_type_init(const char *type) +{ + const char *t = eina_stringshare_add(type); + _evry_types = eina_list_append(_evry_types, t); +} + +const char * +evry_type_get(Evry_Type type) +{ + const char *ret = eina_list_nth(_evry_types, type); + if (!ret) + return eina_stringshare_add(""); + + return ret; +} + +int +evry_api_version_check(int version) +{ + if (EVRY_API_VERSION == version) + return 1; + + ERR("module API is %d, required is %d", version, EVRY_API_VERSION); + + return 0; +} + +static int +_evry_cb_view_sort(const void *data1, const void *data2) +{ + const Evry_View *v1 = data1; + const Evry_View *v2 = data2; + return v1->priority - v2->priority; +} + +void +evry_view_register(Evry_View *view, int priority) +{ + view->priority = priority; + + evry_conf->views = eina_list_append(evry_conf->views, view); + + evry_conf->views = eina_list_sort(evry_conf->views, + eina_list_count(evry_conf->views), + _evry_cb_view_sort); +} + +void +evry_view_unregister(Evry_View *view) +{ + evry_conf->views = eina_list_remove(evry_conf->views, view); +} + +/***************************************************************************/ + +static Eina_Bool +_cleanup_history(void *data EINA_UNUSED) +{ + /* evrything is active */ + if (evry_hist) + return ECORE_CALLBACK_RENEW; + + /* cleanup old entries */ + evry_history_free(); + evry_history_init(); + + return ECORE_CALLBACK_RENEW; +} + +static void +_config_init() +{ + Plugin_Config *pc, *pcc; + +#undef T +#undef D +#define T Plugin_Config +#define D plugin_conf_edd + plugin_conf_edd = E_CONFIG_DD_NEW("Plugin_Config", Plugin_Config); + E_CONFIG_VAL(D, T, name, STR); + E_CONFIG_VAL(D, T, enabled, INT); + E_CONFIG_VAL(D, T, priority, INT); + E_CONFIG_VAL(D, T, trigger, STR); + E_CONFIG_VAL(D, T, trigger_only, INT); + E_CONFIG_VAL(D, T, view_mode, INT); + E_CONFIG_VAL(D, T, aggregate, INT); + E_CONFIG_VAL(D, T, top_level, INT); + E_CONFIG_VAL(D, T, min_query, INT); + E_CONFIG_LIST(D, T, plugins, plugin_conf_edd); +#undef T +#undef D +#define T Gadget_Config +#define D gadget_conf_edd + gadget_conf_edd = E_CONFIG_DD_NEW("Gadget_Config", Gadget_Config); + E_CONFIG_VAL(D, T, id, STR); + E_CONFIG_VAL(D, T, plugin, STR); + E_CONFIG_VAL(D, T, hide_after_action, INT); + E_CONFIG_VAL(D, T, popup, INT); +#undef T +#undef D +#define T Evry_Config +#define D conf_edd + conf_edd = E_CONFIG_DD_NEW("Config", Evry_Config); + E_CONFIG_VAL(D, T, version, INT); + E_CONFIG_VAL(D, T, width, INT); + E_CONFIG_VAL(D, T, height, INT); + E_CONFIG_VAL(D, T, edge_width, INT); + E_CONFIG_VAL(D, T, edge_height, INT); + E_CONFIG_VAL(D, T, rel_x, DOUBLE); + E_CONFIG_VAL(D, T, rel_y, DOUBLE); + E_CONFIG_VAL(D, T, scroll_animate, INT); + E_CONFIG_VAL(D, T, scroll_speed, DOUBLE); + E_CONFIG_VAL(D, T, hide_input, INT); + E_CONFIG_VAL(D, T, hide_list, INT); + E_CONFIG_VAL(D, T, quick_nav, INT); + E_CONFIG_VAL(D, T, view_mode, INT); + E_CONFIG_VAL(D, T, view_zoom, INT); + E_CONFIG_VAL(D, T, cycle_mode, INT); + E_CONFIG_VAL(D, T, history_sort_mode, INT); + E_CONFIG_LIST(D, T, conf_subjects, plugin_conf_edd); + E_CONFIG_LIST(D, T, conf_actions, plugin_conf_edd); + E_CONFIG_LIST(D, T, conf_objects, plugin_conf_edd); + E_CONFIG_LIST(D, T, conf_views, plugin_conf_edd); + E_CONFIG_LIST(D, T, collections, plugin_conf_edd); + E_CONFIG_LIST(D, T, gadgets, gadget_conf_edd); + E_CONFIG_VAL(D, T, first_run, UCHAR); +#undef T +#undef D + evry_conf = e_config_domain_load("module.everything", conf_edd); + + if (evry_conf && !e_util_module_config_check(_("Everything Module"), + evry_conf->version, + MOD_CONFIG_FILE_VERSION)) + _config_free(); + + if (!evry_conf) + { + evry_conf = E_NEW(Evry_Config, 1); + + /* setup defaults */ + evry_conf->rel_x = 0.5; + evry_conf->rel_y = 0.43; + evry_conf->width = 455; + evry_conf->height = 430; + evry_conf->scroll_animate = 1; + evry_conf->scroll_speed = 10.0; + evry_conf->hide_input = 0; + evry_conf->hide_list = 0; + evry_conf->quick_nav = 1; + evry_conf->view_mode = VIEW_MODE_DETAIL; + evry_conf->view_zoom = 0; + evry_conf->cycle_mode = 0; + evry_conf->history_sort_mode = 0; + evry_conf->edge_width = 340; + evry_conf->edge_height = 385; + evry_conf->first_run = EINA_TRUE; + + pcc = E_NEW(Plugin_Config, 1); + pcc->name = eina_stringshare_add("Start"); + pcc->enabled = EINA_FALSE; + pcc->aggregate = EINA_FALSE; + pcc->top_level = EINA_TRUE; + pcc->view_mode = VIEW_MODE_THUMB; + evry_conf->collections = eina_list_append(evry_conf->collections, pcc); + + pc = E_NEW(Plugin_Config, 1); + pc->name = eina_stringshare_add("Windows"); + pc->enabled = EINA_TRUE; + pc->view_mode = VIEW_MODE_NONE; + pcc->plugins = eina_list_append(pcc->plugins, pc); + + pc = E_NEW(Plugin_Config, 1); + pc->name = eina_stringshare_add("Settings"); + pc->enabled = EINA_TRUE; + pc->view_mode = VIEW_MODE_NONE; + pcc->plugins = eina_list_append(pcc->plugins, pc); + + pc = E_NEW(Plugin_Config, 1); + pc->name = eina_stringshare_add("Files"); + pc->enabled = EINA_TRUE; + pc->view_mode = VIEW_MODE_NONE; + pcc->plugins = eina_list_append(pcc->plugins, pc); + + pc = E_NEW(Plugin_Config, 1); + pc->name = eina_stringshare_add("Applications"); + pc->enabled = EINA_TRUE; + pc->view_mode = VIEW_MODE_NONE; + pcc->plugins = eina_list_append(pcc->plugins, pc); + evry_conf->width = 464; + evry_conf->height = 366; + } + + evry_conf->version = MOD_CONFIG_FILE_VERSION; +} + +static void +_config_free(void) +{ + Plugin_Config *pc, *pc2; + Gadget_Config *gc; + + EINA_LIST_FREE (evry_conf->collections, pc) + { + EINA_LIST_FREE (pc->plugins, pc2) + { + IF_RELEASE(pc2->name); + IF_RELEASE(pc2->trigger); + E_FREE(pc2); + } + // collections become registered as SUBJECT + // plugin, therefore dont free pc here + } + + EINA_LIST_FREE (evry_conf->conf_subjects, pc) + { + IF_RELEASE(pc->name); + IF_RELEASE(pc->trigger); + E_FREE(pc); + } + EINA_LIST_FREE (evry_conf->conf_actions, pc) + { + IF_RELEASE(pc->name); + IF_RELEASE(pc->trigger); + E_FREE(pc); + } + EINA_LIST_FREE (evry_conf->conf_objects, pc) + { + IF_RELEASE(pc->name); + IF_RELEASE(pc->trigger); + E_FREE(pc); + } + EINA_LIST_FREE (evry_conf->conf_views, pc) + { + IF_RELEASE(pc->name); + IF_RELEASE(pc->trigger); + E_FREE(pc); + } + EINA_LIST_FREE (evry_conf->gadgets, gc) + { + IF_RELEASE(gc->id); + IF_RELEASE(gc->plugin); + E_FREE(gc); + } + + E_FREE(evry_conf); +} + +/***************************************************************************/ +/* action callback */ + +static Ecore_Idle_Enterer *_idler = NULL; +static const char *_params = NULL; + +static Eina_Bool +_e_mod_run_defer_cb(void *data) +{ + E_Zone *zone; + + zone = data; + if (zone) evry_show(zone, E_ZONE_EDGE_NONE, _params, EINA_TRUE); + + _idler = NULL; + return ECORE_CALLBACK_CANCEL; +} + +static void +_e_mod_action_cb(E_Object *obj EINA_UNUSED, const char *params) +{ + E_Zone *zone = NULL; + + zone = e_zone_current_get(); + if (!zone) return; + + IF_RELEASE(_params); + if (params && params[0]) + _params = eina_stringshare_add(params); + /* if (zone) evry_show(zone, _params); */ + + if (_idler) ecore_idle_enterer_del(_idler); + _idler = ecore_idle_enterer_add(_e_mod_run_defer_cb, zone); +} + +static void +_e_mod_action_cb_edge(E_Object *obj EINA_UNUSED, const char *params, E_Event_Zone_Edge *ev) +{ + IF_RELEASE(_params); + if (params && params[0]) + _params = eina_stringshare_add(params); + + if (_idler) ecore_idle_enterer_del(_idler); + + evry_show(ev->zone, ev->edge, _params, EINA_TRUE); +} + +/* menu item callback(s) */ +static void +_e_mod_run_cb(void *data EINA_UNUSED, E_Menu *m, E_Menu_Item *mi EINA_UNUSED) +{ + IF_RELEASE(_params); + ecore_idle_enterer_add(_e_mod_run_defer_cb, m->zone); +} + +/* menu item add hook */ +static void +_e_mod_menu_add(void *data EINA_UNUSED, E_Menu *m) +{ + E_Menu_Item *mi; + + mi = e_menu_item_new(m); + e_menu_item_label_set(mi, _("Run Everything")); + e_util_menu_item_theme_icon_set(mi, "system-run"); + e_menu_item_callback_set(mi, _e_mod_run_cb, NULL); +} + diff --git a/src/modules/everything/e_mod_main.h b/src/modules/everything/e_mod_main.h new file mode 100644 index 0000000..2bb4ceb --- /dev/null +++ b/src/modules/everything/e_mod_main.h @@ -0,0 +1,395 @@ +#ifndef EVRY_H +#define EVRY_H + +#include "e.h" +#include "evry_api.h" + +/* Increment for Major Changes */ +#define MOD_CONFIG_FILE_EPOCH 1 +/* Increment for Minor Changes (ie: user doesn't need a new config) */ +#define MOD_CONFIG_FILE_GENERATION 0 +#define MOD_CONFIG_FILE_VERSION ((MOD_CONFIG_FILE_EPOCH * 1000000) + MOD_CONFIG_FILE_GENERATION) + +#define SLIDE_LEFT 1 +#define SLIDE_RIGHT -1 + +typedef struct _History Evry_History; +typedef struct _Config Evry_Config; +typedef struct _Evry_Selector Evry_Selector; +typedef struct _Tab_View Tab_View; +typedef struct _Evry_Window Evry_Window; +typedef struct _Gadget_Config Gadget_Config; + +struct _Evry_Window +{ + Evas_Object *ewin; + Evas *evas; + E_Zone *zone; + Evas_Object *o_main; + + Eina_Bool request_selection; + Eina_Bool plugin_dedicated; + Eina_Bool visible; + + Eina_List *handlers; + + Evry_Selector *selector; + Evry_Selector **selectors; + Evry_Selector **sel_list; + + unsigned int level; + + unsigned int mouse_button; + Eina_Bool mouse_out; + + Eina_Bool grab; + + Evry_State *state_clearing; + + struct + { + void (*hide) (Evry_Window *win, int finished); + } func; + + /* only to be used by creator of win */ + void *data; + + Ecore_Timer *delay_hide_action; +}; + +struct _Evry_Selector +{ + Evry_Window *win; + + /* current state */ + Evry_State *state; + + /* stack of states (for browseable plugins) */ + Eina_List *states; + + /* provides collection of items from other plugins */ + Evry_Plugin *aggregator; + + /* action selector plugin */ + Evry_Plugin *actions; + + /* all plugins that belong to this selector*/ + Eina_List *plugins; + + /* list view instance */ + Evry_View *view; + + Evas_Object *o_icon; + Evas_Object *o_thumb; + const Evas_Object *event_object; + Eina_Bool do_thumb; + + Ecore_Timer *update_timer; + Ecore_Timer *action_timer; + + const char *edje_part; +}; + +struct _Evry_State +{ + Evry_Selector *selector; + + char *inp; /* alloced input */ + + char *input; /* pointer to input + trigger */ + /* all available plugins for current state */ + Eina_List *plugins; + + /* currently active plugins, i.e. those that provide items */ + Eina_List *cur_plugins; + + /* active plugin */ + Evry_Plugin *plugin; + + /* aggregator instance */ + Evry_Plugin *aggregator; + + /* selected item */ + Evry_Item *cur_item; + + /* marked items */ + Eina_List *sel_items; + + Eina_Bool plugin_auto_selected; + Eina_Bool item_auto_selected; + + /* current view instance */ + Evry_View *view; + + Eina_Bool changed; + Eina_Bool trigger_active; + + unsigned int request; + + Ecore_Timer *clear_timer; + + Eina_Bool delete_me; +}; + +struct _Tab_View +{ + const Evry_State *state; + + Evry_View *view; + Evas *evas; + + Evas_Object *o_tabs; + Eina_List *tabs; + + void (*update) (Tab_View *tv); + void (*clear) (Tab_View *tv); + int (*key_down) (Tab_View *tv, const Ecore_Event_Key *ev); + + double align; + double align_to; + Ecore_Animator *animator; + Ecore_Timer *timer; +}; + +struct _Config +{ + int version; + /* position */ + double rel_x, rel_y; + /* size */ + int width, height; + int edge_width, edge_height; + + Eina_List *modules; + + /* generic plugin config */ + Eina_List *conf_subjects; + Eina_List *conf_actions; + Eina_List *conf_objects; + Eina_List *conf_views; + Eina_List *collections; + + int scroll_animate; + double scroll_speed; + + int hide_input; + int hide_list; + + /* quick navigation mode */ + int quick_nav; + + /* default view mode */ + int view_mode; + int view_zoom; + + int history_sort_mode; + + /* use up/down keys for prev/next in thumb view */ + int cycle_mode; + + Eina_List *gadgets; + + unsigned char first_run; + /* not saved data */ + Eina_List *actions; + Eina_List *views; + + int min_w, min_h; +}; + +struct _Gadget_Config +{ + const char *id; + const char *plugin; + int hide_after_action; + int popup; +}; + +struct _History +{ + int version; + Eina_Hash *subjects; + double begin; +}; + +/*** Evry_Api functions ***/ +void evry_item_select(const Evry_State *s, Evry_Item *it); +void evry_item_mark(const Evry_State *state, Evry_Item *it, Eina_Bool mark); +void evry_plugin_select(Evry_Plugin *p); +Evry_Item *evry_item_new(Evry_Item *base, Evry_Plugin *p, const char *label, + Evas_Object *(*icon_get) (Evry_Item *it, Evas *e), + void (*cb_free) (Evry_Item *item)); +void evry_item_free(Evry_Item *it); +void evry_item_ref(Evry_Item *it); + +void evry_plugin_update(Evry_Plugin *plugin, int state); +void evry_clear_input(Evry_Plugin *p); + +/* evry_util.c */ +/* Evas_Object *evry_icon_mime_get(const char *mime, Evas *e); */ +Evas_Object *evry_icon_theme_get(const char *icon, Evas *e); +int evry_fuzzy_match(const char *str, const char *match); +Eina_List *evry_fuzzy_match_sort(Eina_List *items); +int evry_util_exec_app(const Evry_Item *it_app, const Evry_Item *it_file); +char *evry_util_url_escape(const char *string, int inlength); +char *evry_util_url_unescape(const char *string, int length); +void evry_util_file_detail_set(Evry_Item_File *file); +int evry_util_module_config_check(const char *module_name, int conf, int epoch, int version); +Evas_Object *evry_util_icon_get(Evry_Item *it, Evas *e); +int evry_util_plugin_items_add(Evry_Plugin *p, Eina_List *items, const char *input, int match_detail, int set_usage); +void evry_item_changed(Evry_Item *it, int change_icon, int change_selected); +char *evry_util_md5_sum(const char *str); +void evry_util_items_sort(Eina_List **items, int flags); + +const char *evry_file_path_get(Evry_Item_File *file); +const char *evry_file_url_get(Evry_Item_File *file); + +int evry_plugin_register(Evry_Plugin *p, int type, int priority); +void evry_plugin_unregister(Evry_Plugin *p); +Evry_Plugin *evry_plugin_find(const char *name); +void evry_action_register(Evry_Action *act, int priority); +void evry_action_unregister(Evry_Action *act); +void evry_view_register(Evry_View *view, int priority); +void evry_view_unregister(Evry_View *view); +Evry_Action *evry_action_find(const char *name); + +void evry_history_load(void); +void evry_history_unload(void); +History_Item *evry_history_item_add(Evry_Item *it, const char *ctxt, const char *input); +int evry_history_item_usage_set(Evry_Item *it, const char *input, const char *ctxt); +History_Types *evry_history_types_get(Evry_Type type); + +Evry_Plugin *evry_plugin_new(Evry_Plugin *base, const char *name, const char *label, const char *icon, + Evry_Type item_type, + Evry_Plugin *(*begin) (Evry_Plugin *p, const Evry_Item *item), + void (*cleanup) (Evry_Plugin *p), + int (*fetch) (Evry_Plugin *p, const char *input)); + +void evry_plugin_free(Evry_Plugin *p); + +Evry_Action *evry_action_new(const char *name, const char *label, + Evry_Type type1, Evry_Type type2, + const char *icon, + int (*action) (Evry_Action *act), + int (*check_item) (Evry_Action *act, const Evry_Item *it)); + +void evry_action_free(Evry_Action *act); + +int evry_api_version_check(int version); + +Evry_Type evry_type_register(const char *type); +const char *evry_type_get(Evry_Type type); + +/*** internal ***/ +Tab_View *evry_tab_view_new(Evry_View *view, const Evry_State *s, Evas_Object *parent); +void evry_tab_view_free(Tab_View *v); + +Eina_Bool evry_view_init(void); +void evry_view_shutdown(void); + +Eina_Bool evry_view_help_init(void); +void evry_view_help_shutdown(void); + +Eina_Bool evry_plug_clipboard_init(void); +void evry_plug_clipboard_shutdown(void); + +Eina_Bool evry_plug_text_init(void); +void evry_plug_text_shutdown(void); + +Eina_Bool evry_plug_collection_init(void); +void evry_plug_collection_shutdown(void); + +int evry_init(void); +int evry_shutdown(void); +Evry_Window *evry_show(E_Zone *zone, E_Zone_Edge edge, const char *params, Eina_Bool popup); +void evry_hide(Evry_Window *win, int clear); + +int evry_plug_actions_init(void); +void evry_plug_actions_shutdown(void); + +Evry_Plugin *evry_aggregator_new(int type); + +void evry_history_init(void); +void evry_history_free(void); + +int evry_browse_item(Evry_Item *it); +int evry_browse_back(Evry_Selector *sel); + +void evry_plugin_action(Evry_Window *win, int finished); + +int evry_state_push(Evry_Selector *sel, Eina_List *plugins); +int evry_selectors_switch(Evry_Window *win,int dir, int slide); +int evry_view_toggle(Evry_State *s, const char *trigger); + +int evry_gadget_init(void); +void evry_gadget_shutdown(void); + +Eina_Bool evry_plug_apps_init(E_Module *m); +void evry_plug_apps_shutdown(void); +void evry_plug_apps_save(void); + +Eina_Bool evry_plug_files_init(E_Module *m); +void evry_plug_files_shutdown(void); +void evry_plug_files_save(void); + +Eina_Bool evry_plug_windows_init(E_Module *m); +void evry_plug_windows_shutdown(void); +void evry_plug_windows_save(void); + +Eina_Bool evry_plug_settings_init(E_Module *m); +void evry_plug_settings_shutdown(void); +void evry_plug_settings_save(void); + +Eina_Bool evry_plug_calc_init(E_Module *m); +void evry_plug_calc_shutdown(void); +void evry_plug_calc_save(void); + +Eina_Bool evry_plug_ai_init(E_Module *m); +void evry_plug_ai_shutdown(void); +void evry_plug_ai_save(void); + +Ecore_Event_Handler *evry_event_handler_add(int type, Eina_Bool (*func) (void *data, int type, void *event), const void *data); + +extern Evry_API *evry; +extern Evry_History *evry_hist; +extern Evry_Config *evry_conf; +extern int _evry_events[NUM_EVRY_EVENTS]; +extern E_Module *_mod_evry; + +/*** E Module ***/ +E_API void *e_modapi_init (E_Module *m); +E_API int e_modapi_shutdown (E_Module *m); +E_API int e_modapi_save (E_Module *m); +E_API E_Config_Dialog *evry_config_dialog(Evas_Object *parent, const char *params); +E_API E_Config_Dialog *evry_collection_conf_dialog(Evas_Object *parent, const char *params); +E_API extern E_Module_Api e_modapi; + +/* #define CHECK_REFS 1 + * #define PRINT_REFS 1 + * #define CHECK_TIME 1 + * #undef DBG + * #define DBG(...) ERR(__VA_ARGS__) */ + +#ifdef CHECK_REFS +extern Eina_List *_refd; +#endif + +#ifdef CHECK_TIME +extern double _evry_time; +#endif + + +/** + * @addtogroup Optional_Launcher + * @{ + * + * @defgroup Module_Everything Everything Launcher + * + * Flexible launcher with plugins. Can do search as you type + * filtering, browse directories, view pictures, simple math, spell + * checking and of course: launching programs and executing commands. + * + * @} + */ + +#endif diff --git a/src/modules/everything/evry_plug_ai.c b/src/modules/everything/evry_plug_ai.c new file mode 100644 index 0000000..6c751ec --- /dev/null +++ b/src/modules/everything/evry_plug_ai.c @@ -0,0 +1,383 @@ +/* AI prompt plugin for the Everything launcher + * Trigger: ? (e.g., ?what is the capital of France) + * + * Environment variables: + * ANTHROPIC_API_KEY - enables Claude when set + * AI_PROVIDER - "claude" or "ollama" (auto-detected from ANTHROPIC_API_KEY) + * CLAUDE_MODEL - Claude model name (default: claude-haiku-4-5-20251001) + * OLLAMA_MODEL - Ollama model name (default: llama3.2) + * OLLAMA_HOST - Ollama base URL (default: http://localhost:11434) + */ +#include "e.h" +#include "evry_api.h" + +typedef struct _Plugin Plugin; + +struct _Plugin +{ + Evry_Plugin base; +}; + +static const Evry_API *evry = NULL; +static Evry_Module *evry_module = NULL; +static Evry_Plugin *_plug = NULL; +static Ecore_Exe *_exe = NULL; +static Eina_List *_handlers = NULL; +static Eina_Bool _active = EINA_FALSE; +static Plugin *_cur_plugin = NULL; +static Evry_Item *_cur_item = NULL; + +/* accumulated response from curl stdout */ +static char _response_buf[65536]; +static int _response_len = 0; + +static Eina_Bool _cb_data(void *data, int type, void *event); +static Eina_Bool _cb_del(void *data, int type, void *event); + +static void +_exe_stop(void) +{ + if (_exe) + { + Ecore_Exe *old = _exe; + _exe = NULL; + ecore_exe_terminate(old); + ecore_exe_free(old); + } +} + +/* Extract text value from Claude API response JSON */ +static char * +_json_extract_claude(char *json) +{ + char *p = strstr(json, "\"type\":\"text\""); + if (!p) return NULL; + p = strstr(p, "\"text\":"); + if (!p) return NULL; + p += 7; + while (*p == ' ') p++; + if (*p != '"') return NULL; + return p + 1; +} + +/* Extract response value from Ollama API response JSON */ +static char * +_json_extract_ollama(char *json) +{ + char *p = strstr(json, "\"response\":"); + if (!p) return NULL; + p += 11; + while (*p == ' ') p++; + if (*p != '"') return NULL; + return p + 1; +} + +/* Decode JSON string escapes in-place up to the closing quote */ +static void +_json_unescape(char *s) +{ + char *r = s, *w = s; + + while (*r && *r != '"') + { + if (*r == '\\' && *(r + 1)) + { + r++; + switch (*r) + { + case 'n': *w++ = '\n'; break; + case 't': *w++ = '\t'; break; + case 'r': *w++ = '\r'; break; + case '"': *w++ = '"'; break; + case '\\': *w++ = '\\'; break; + default: *w++ = *r; break; + } + } + else + *w++ = *r; + r++; + } + *w = '\0'; +} + +static const char * +_provider_get(void) +{ + const char *p = getenv("AI_PROVIDER"); + if (p) return p; + return getenv("ANTHROPIC_API_KEY") ? "claude" : "ollama"; +} + +/* Escape a string for embedding in a JSON value */ +static void +_json_escape(char *dst, int dstsz, const char *src) +{ + int i = 0, j = 0; + + for (; src[i] && j < dstsz - 2; i++) + { + switch (src[i]) + { + case '"': dst[j++] = '\\'; dst[j++] = '"'; break; + case '\\': dst[j++] = '\\'; dst[j++] = '\\'; break; + case '\n': dst[j++] = '\\'; dst[j++] = 'n'; break; + case '\r': dst[j++] = '\\'; dst[j++] = 'r'; break; + case '\t': dst[j++] = '\\'; dst[j++] = 't'; break; + default: dst[j++] = src[i]; break; + } + } + dst[j] = '\0'; +} + +static void +_query_send(const char *input) +{ + char cmd[8192]; + char prompt[4096]; + + _response_len = 0; + _response_buf[0] = '\0'; + + _json_escape(prompt, sizeof(prompt), input); + + if (!strcmp(_provider_get(), "claude")) + { + const char *key = getenv("ANTHROPIC_API_KEY"); + const char *model = getenv("CLAUDE_MODEL"); + if (!model) model = "claude-haiku-4-5-20251001"; + if (!key) return; + + snprintf(cmd, sizeof(cmd), + "curl -s -m 30 -X POST https://api.anthropic.com/v1/messages" + " -H 'x-api-key: %s'" + " -H 'anthropic-version: 2023-06-01'" + " -H 'content-type: application/json'" + " -d '{\"model\":\"%s\",\"max_tokens\":1024," + "\"messages\":[{\"role\":\"user\",\"content\":\"%s\"}]}'", + key, model, prompt); + } + else + { + const char *model = getenv("OLLAMA_MODEL"); + const char *host = getenv("OLLAMA_HOST"); + if (!model) model = "llama3.2"; + if (!host) host = "http://localhost:11434"; + + snprintf(cmd, sizeof(cmd), + "curl -s -m 120 -X POST %s/api/generate" + " -H 'content-type: application/json'" + " -d '{\"model\":\"%s\",\"prompt\":\"%s\",\"stream\":false}'", + host, model, prompt); + } + + _exe = ecore_exe_pipe_run(cmd, + ECORE_EXE_PIPE_READ | + ECORE_EXE_PIPE_READ_LINE_BUFFERED | + ECORE_EXE_PIPE_ERROR | + ECORE_EXE_PIPE_ERROR_LINE_BUFFERED, + NULL); +} + +static Evry_Plugin * +_begin(Evry_Plugin *plugin, const Evry_Item *item EINA_UNUSED) +{ + Plugin *p; + + if (_active) return NULL; + + EVRY_PLUGIN_INSTANCE(p, plugin); + _active = EINA_TRUE; + _cur_plugin = p; + + _handlers = eina_list_append(_handlers, + ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _cb_data, p)); + _handlers = eina_list_append(_handlers, + ecore_event_handler_add(ECORE_EXE_EVENT_DEL, _cb_del, p)); + + return EVRY_PLUGIN(p); +} + +static void +_finish(Evry_Plugin *plugin) +{ + GET_PLUGIN(p, plugin); + Ecore_Event_Handler *h; + + _exe_stop(); + + EINA_LIST_FREE(_handlers, h) + ecore_event_handler_del(h); + + if (_cur_item) + { + EVRY_ITEM_FREE(_cur_item); + _cur_item = NULL; + } + + EVRY_PLUGIN_ITEMS_FREE(p); + _cur_plugin = NULL; + _active = EINA_FALSE; + + E_FREE(p); +} + +static int +_fetch(Evry_Plugin *plugin, const char *input) +{ + GET_PLUGIN(p, plugin); + + if (!input || !*input) return 0; + + _exe_stop(); + + if (!_cur_item) + { + _cur_item = EVRY_ITEM_NEW(Evry_Item, p, "Asking AI...", NULL, NULL); + _cur_item->fuzzy_match = 1; + EVRY_PLUGIN_ITEM_APPEND(p, _cur_item); + EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD); + } + else + { + EVRY_ITEM_LABEL_SET(_cur_item, "Asking AI..."); + EVRY_ITEM_DETAIL_SET(_cur_item, NULL); + evry->item_changed(_cur_item, 0, 0); + } + + _query_send(input); + + return EVRY_PLUGIN_HAS_ITEMS(p); +} + +static Eina_Bool +_cb_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Exe_Event_Data *ev = event; + + if (ev->exe != _exe) return ECORE_CALLBACK_PASS_ON; + + if (ev->lines) + { + int i; + for (i = 0; ev->lines[i].line; i++) + { + int avail = (int)sizeof(_response_buf) - _response_len - 1; + if (avail <= 0) break; + int len = ev->lines[i].size < avail ? ev->lines[i].size : avail; + memcpy(_response_buf + _response_len, ev->lines[i].line, len); + _response_len += len; + _response_buf[_response_len] = '\0'; + } + } + + return ECORE_CALLBACK_PASS_ON; +} + +static Eina_Bool +_cb_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Exe_Event_Del *ev = event; + Evry_Plugin *p; + char *text_start; + static char result[16384]; + char label[256]; + + if (ev->exe != _exe) return ECORE_CALLBACK_PASS_ON; + + _exe = NULL; + + if (!_cur_plugin || !_cur_item) return ECORE_CALLBACK_PASS_ON; + p = EVRY_PLUGIN(_cur_plugin); + + if (!strcmp(_provider_get(), "claude")) + text_start = _json_extract_claude(_response_buf); + else + text_start = _json_extract_ollama(_response_buf); + + if (text_start) + { + strncpy(result, text_start, sizeof(result) - 1); + result[sizeof(result) - 1] = '\0'; + _json_unescape(result); + + /* single-line label: replace newlines with spaces */ + char *nl; + strncpy(label, result, sizeof(label) - 1); + label[sizeof(label) - 1] = '\0'; + for (nl = label; *nl; nl++) + if (*nl == '\n' || *nl == '\r') *nl = ' '; + + EVRY_ITEM_LABEL_SET(_cur_item, label); + EVRY_ITEM_DETAIL_SET(_cur_item, result); + } + else + { + EVRY_ITEM_LABEL_SET(_cur_item, "AI error — check ANTHROPIC_API_KEY / OLLAMA_HOST"); + EVRY_ITEM_DETAIL_SET(_cur_item, _response_buf[0] ? _response_buf : NULL); + } + + evry->item_changed(_cur_item, 0, 0); + EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_REFRESH); + + return ECORE_CALLBACK_PASS_ON; +} + +static int +_plugins_init(const Evry_API *_api) +{ + evry = _api; + + if (!evry->api_version_check(EVRY_API_VERSION)) + return EINA_FALSE; + + _plug = EVRY_PLUGIN_BASE(N_("AI Prompt"), "system-search", + EVRY_TYPE_TEXT, _begin, _finish, _fetch); + _plug->history = EINA_FALSE; + _plug->async_fetch = EINA_TRUE; + + if (evry->plugin_register(_plug, EVRY_PLUGIN_SUBJECT, 0)) + { + Plugin_Config *pc = _plug->config; + pc->view_mode = VIEW_MODE_LIST; + pc->trigger = eina_stringshare_add("?"); + pc->trigger_only = EINA_TRUE; + pc->aggregate = EINA_FALSE; + } + + return EINA_TRUE; +} + +static void +_plugins_shutdown(void) +{ + Ecore_Event_Handler *h; + + _exe_stop(); + + EINA_LIST_FREE(_handlers, h) + ecore_event_handler_del(h); + + if (_cur_item) + { + EVRY_ITEM_FREE(_cur_item); + _cur_item = NULL; + } + + EVRY_PLUGIN_FREE(_plug); +} + +Eina_Bool +evry_plug_ai_init(E_Module *m EINA_UNUSED) +{ + EVRY_MODULE_NEW(evry_module, evry, _plugins_init, _plugins_shutdown); + return EINA_TRUE; +} + +void +evry_plug_ai_shutdown(void) +{ + EVRY_MODULE_FREE(evry_module); +} + +void +evry_plug_ai_save(void) {} diff --git a/src/modules/everything/meson.build b/src/modules/everything/meson.build new file mode 100644 index 0000000..496a3b8 --- /dev/null +++ b/src/modules/everything/meson.build @@ -0,0 +1,49 @@ +src = files( + 'e_mod_main.c', + 'evry.c', + 'evry_config.c', + 'evry_gadget.c', + 'evry_history.c', + 'evry_plug_actions.c', + 'evry_plug_aggregator.c', + 'evry_plug_apps.c', + 'evry_plug_calc.c', + 'evry_plug_ai.c', + 'evry_plug_clipboard.c', + 'evry_plug_collection.c', + 'evry_plug_files.c', + 'evry_plugin.c', + 'evry_plug_settings.c', + 'evry_plug_text.c', + 'evry_plug_windows.c', + 'evry_util.c', + 'evry_view.c', + 'evry_view_help.c', + 'evry_view_tabs.c', + 'md5.c', + 'e_mod_main.h', + 'evry_api.h', + 'evry_types.h', + 'md5.h' + ) + +data = [ 'e-module-everything-start.edj' ] + +if get_option(m) == true + install_headers([ 'evry_api.h', 'evry_types.h' ], + subdir: 'enlightenment' + ) + pkgconfig.generate(name: 'e17-everything', + description: 'Everything module for Enlightenment', + filebase : 'everything', + subdirs : 'enlightenment', + requires : 'enlightenment', + version : e_version_rev, + install_dir: dir_pkgconfig, + variables : [ + 'exec_prefix=${prefix}', + 'plugindir=${libdir}/enlightenment/modules/everything/plugins' + ] + ) +endif +