|
15 | 15 |
|
16 | 16 | (set! *warn-on-reflection* true) |
17 | 17 |
|
18 | | -(defn ^:private initialize-models! [db* config] |
| 18 | +(defn ^:private sync-models! [db* config on-models-updated] |
19 | 19 | (let [all-models (models/all) |
20 | | - eca-models (reduce |
| 20 | + all-models (reduce |
21 | 21 | (fn [p [provider provider-config]] |
22 | 22 | (merge p |
23 | 23 | (reduce |
24 | 24 | (fn [m [model _model-config]] |
25 | 25 | (let [full-model (str provider "/" model) |
26 | 26 | model-capabilities (merge |
27 | 27 | (or (get all-models full-model) |
28 | | - ;; we guess the capabilities from |
29 | | - ;; the first model with same name |
| 28 | + ;; we guess the capabilities from |
| 29 | + ;; the first model with same name |
30 | 30 | (when-let [found-full-model (first (filter #(= (shared/normalize-model-name model) |
31 | 31 | (shared/normalize-model-name (second (string/split % #"/" 2)))) |
32 | 32 | (keys all-models)))] |
|
38 | 38 | {} |
39 | 39 | (:models provider-config)))) |
40 | 40 | {} |
41 | | - (:providers config))] |
42 | | - (swap! db* update :models merge eca-models)) |
43 | | - (when-let [ollama-models (seq (llm-api/extra-models config))] |
44 | | - (let [models (reduce |
45 | | - (fn [models {:keys [model] :as ollama-model}] |
46 | | - (assoc models |
47 | | - (str config/ollama-model-prefix model) |
48 | | - (select-keys ollama-model [:tools :reason?]))) |
49 | | - {} |
50 | | - ollama-models)] |
51 | | - (swap! db* update :models merge models)))) |
| 41 | + (:providers config)) |
| 42 | + all-models (if-let [local-models (seq (llm-api/local-models config))] |
| 43 | + (let [models (reduce |
| 44 | + (fn [models {:keys [model] :as ollama-model}] |
| 45 | + (assoc models |
| 46 | + (str config/ollama-model-prefix model) |
| 47 | + (select-keys ollama-model [:tools :reason?]))) |
| 48 | + {} |
| 49 | + local-models)] |
| 50 | + (swap! db* update :models merge models)) |
| 51 | + all-models)] |
| 52 | + (swap! db* assoc :models all-models) |
| 53 | + (on-models-updated))) |
52 | 54 |
|
53 | | -(defn initialize [{:keys [db* messenger]} params] |
| 55 | +(defn initialize [{:keys [db*]} params] |
54 | 56 | (logger/logging-task |
55 | 57 | :eca/initialize |
56 | 58 | (reset! config/initialization-config* (shared/map->camel-cased-map (:initialization-options params))) |
57 | 59 | (let [config (config/all @db*)] |
| 60 | + (logger/debug "Considered config: " config) |
58 | 61 | (swap! db* assoc |
59 | 62 | :client-info (:client-info params) |
60 | 63 | :workspace-folders (:workspace-folders params) |
61 | 64 | :client-capabilities (:capabilities params)) |
62 | | - (initialize-models! db* config) |
63 | 65 | (when-not (:pureConfig config) |
64 | 66 | (db/load-db-from-cache! db*)) |
65 | | - (future |
66 | | - (Thread/sleep 1000) ;; wait chat window is open in some editors. |
67 | | - (when-let [error (config/validation-error)] |
68 | | - (messenger/chat-content-received |
69 | | - messenger |
70 | | - {:role "system" |
71 | | - :content {:type :text |
72 | | - :text (format "\nFailed to parse '%s' config, check stderr logs, double check your config and restart\n" |
73 | | - error)}}))) |
74 | | - (logger/debug "Considered config: " config) |
75 | | - {:models (sort (keys (:models @db*))) |
76 | | - :chat-default-model (f.chat/default-model @db* config) |
77 | | - :chat-behaviors (:chat-behaviors @db*) |
78 | | - :chat-default-behavior (or (:defaultBehavior (:chat config)) ;;legacy |
79 | | - (:defaultBehavior config)) |
80 | | - :chat-welcome-message (or (:welcomeMessage (:chat config)) ;;legacy |
81 | | - (:welcomeMessage config))}))) |
| 67 | + |
| 68 | + ;; Deprecated |
| 69 | + ;; For backward compatibility, |
| 70 | + ;; we now return chat config via `config/updated` notification. |
| 71 | + (sync-models! db* config (fn [])) |
| 72 | + (let [db @db*] |
| 73 | + {:models (sort (keys (:models db))) |
| 74 | + :chat-default-model (f.chat/default-model db config) |
| 75 | + :chat-behaviors (:chat-behaviors db) |
| 76 | + :chat-default-behavior (or (:defaultBehavior (:chat config)) ;;legacy |
| 77 | + (:defaultBehavior config)) |
| 78 | + :chat-welcome-message (or (:welcomeMessage (:chat config)) ;;legacy |
| 79 | + (:welcomeMessage config))})))) |
82 | 80 |
|
83 | 81 | (defn initialized [{:keys [db* messenger config]}] |
| 82 | + (let [sync-models-and-notify! (fn [config] |
| 83 | + (let [new-providers-hash (hash (:providers config))] |
| 84 | + (when (not= (:providers-config-hash @db*) new-providers-hash) |
| 85 | + (swap! db* assoc :providers-config-hash new-providers-hash) |
| 86 | + (sync-models! db* config (fn [] |
| 87 | + (let [db @db*] |
| 88 | + (config/notify-fields-changed-only! |
| 89 | + {:chat |
| 90 | + {:models (sort (keys (:models db))) |
| 91 | + :default-model (f.chat/default-model db config) |
| 92 | + :behaviors (:chat-behaviors db) |
| 93 | + :default-behavior (or (:defaultBehavior (:chat config)) ;;legacy |
| 94 | + (:defaultBehavior config)) |
| 95 | + :welcome-message (or (:welcomeMessage (:chat config)) ;;legacy |
| 96 | + (:welcomeMessage config))}} |
| 97 | + messenger |
| 98 | + db*)))))))] |
| 99 | + (swap! db* assoc-in [:config-updated-fns :sync-models] #(sync-models-and-notify! %)) |
| 100 | + (sync-models-and-notify! config)) |
| 101 | + (future |
| 102 | + (Thread/sleep 1000) ;; wait chat window is open in some editors. |
| 103 | + (when-let [error (config/validation-error)] |
| 104 | + (messenger/chat-content-received |
| 105 | + messenger |
| 106 | + {:role "system" |
| 107 | + :content {:type :text |
| 108 | + :text (format "\nFailed to parse '%s' config, check stderr logs, double check your config and restart\n" |
| 109 | + error)}})) |
| 110 | + (config/listen-for-changes! db*)) |
84 | 111 | (future |
85 | 112 | (f.tools/init-servers! db* messenger config))) |
86 | 113 |
|
87 | 114 | (defn shutdown [{:keys [db*]}] |
88 | 115 | (logger/logging-task |
89 | 116 | :eca/shutdown |
90 | 117 | (f.mcp/shutdown! db*) |
91 | | - (reset! db* db/initial-db) |
| 118 | + (swap! db* assoc :stopping true) |
92 | 119 | nil)) |
93 | 120 |
|
94 | 121 | (defn chat-prompt [{:keys [messenger db* config]} params] |
|
0 commit comments