|
48 | 48 | refined-contexts) |
49 | 49 | "</contexts>")) |
50 | 50 |
|
| 51 | +(defn ^:private behavior->behavior-str [behavior] |
| 52 | + (case behavior |
| 53 | + "chat" "Help with code changes when applicable, answer questions, and provide explanations." |
| 54 | + "agent" "Help with code changes when applicable but suggest you do the changes itself, answer questions, and provide explanations.")) |
| 55 | + |
51 | 56 | (defn default-model [db] |
52 | 57 | (if-let [ollama-model (first (filter #(string/starts-with? % config/ollama-model-prefix) (vals (:models db))))] |
53 | 58 | ollama-model |
|
76 | 81 | (swap! db* assoc-in [:chats new-id] {:id new-id}) |
77 | 82 | new-id)) |
78 | 83 | _ (messenger/chat-content-received |
79 | | - messenger |
80 | | - {:chat-id chat-id |
81 | | - :request-id request-id |
82 | | - :is-complete false |
83 | | - :role :user |
84 | | - :content {:type :text |
85 | | - :text (str message "\n")}}) |
| 84 | + messenger |
| 85 | + {:chat-id chat-id |
| 86 | + :request-id request-id |
| 87 | + :is-complete false |
| 88 | + :role :user |
| 89 | + :content {:type :text |
| 90 | + :text (str message "\n")}}) |
86 | 91 | _ (when (seq contexts) |
87 | 92 | (messenger/chat-content-received |
88 | | - messenger |
89 | | - {:chat-id chat-id |
90 | | - :request-id request-id |
91 | | - :is-complete false |
92 | | - :role :system |
93 | | - :content {:type :progress |
94 | | - :state :running |
95 | | - :text "Parsing given context"}})) |
| 93 | + messenger |
| 94 | + {:chat-id chat-id |
| 95 | + :request-id request-id |
| 96 | + :is-complete false |
| 97 | + :role :system |
| 98 | + :content {:type :progress |
| 99 | + :state :running |
| 100 | + :text "Parsing given context"}})) |
96 | 101 | db @db* |
97 | 102 | rules (f.rules/all config |
98 | 103 | (:workspace-folders db) |
99 | | - {:behavior (or behavior (:chat-default-behavior db))}) |
| 104 | + {:behavior (behavior->behavior-str (or behavior (:chat-default-behavior db)))}) |
100 | 105 | refined-contexts (raw-contexts->refined contexts) |
101 | 106 | context-str (build-context-str refined-contexts rules) |
102 | 107 | chosen-model (or model (default-model db)) |
|
107 | 112 | received-msgs* (atom "")] |
108 | 113 | (swap! db* update-in [:chats chat-id :messages] (fnil conj []) {:role "user" :content user-prompt}) |
109 | 114 | (messenger/chat-content-received |
110 | | - messenger |
111 | | - {:chat-id chat-id |
112 | | - :request-id request-id |
113 | | - :role :system |
114 | | - :content {:type :progress |
115 | | - :state :running |
116 | | - :text "Waiting model"}}) |
| 115 | + messenger |
| 116 | + {:chat-id chat-id |
| 117 | + :request-id request-id |
| 118 | + :role :system |
| 119 | + :content {:type :progress |
| 120 | + :state :running |
| 121 | + :text "Waiting model"}}) |
117 | 122 | (llm-api/complete! |
118 | | - {:model chosen-model |
119 | | - :model-config (get-in db [:models chosen-model]) |
120 | | - :user-prompt user-prompt |
121 | | - :context context-str |
122 | | - :past-messages past-messages |
123 | | - :config config |
124 | | - :mcp-tools mcp-tools |
125 | | - :on-first-message-received (fn [_] |
126 | | - (messenger/chat-content-received |
127 | | - messenger |
128 | | - {:chat-id chat-id |
129 | | - :request-id request-id |
130 | | - :role :system |
131 | | - :content {:type :progress |
132 | | - :state :running |
133 | | - :text "Generating"}})) |
134 | | - :on-message-received (fn [{:keys [type] :as msg}] |
135 | | - (case type |
136 | | - :text (do |
137 | | - (swap! received-msgs* str (:text msg)) |
138 | | - (messenger/chat-content-received |
139 | | - messenger |
140 | | - {:chat-id chat-id |
141 | | - :request-id request-id |
142 | | - :role :assistant |
143 | | - :content {:type :text |
144 | | - :text (:text msg)}})) |
145 | | - :url (messenger/chat-content-received |
146 | | - messenger |
147 | | - {:chat-id chat-id |
148 | | - :request-id request-id |
149 | | - :role :assistant |
150 | | - :content {:type :url |
151 | | - :title (:title msg) |
152 | | - :url (:url msg)}}) |
153 | | - :finish (do |
154 | | - (swap! db* update-in [:chats chat-id :messages] |
155 | | - (fnil conj []) |
156 | | - {:role "assistant" |
157 | | - :content @received-msgs*}) |
158 | | - (messenger/chat-content-received |
| 123 | + {:model chosen-model |
| 124 | + :model-config (get-in db [:models chosen-model]) |
| 125 | + :user-prompt user-prompt |
| 126 | + :context context-str |
| 127 | + :past-messages past-messages |
| 128 | + :config config |
| 129 | + :mcp-tools mcp-tools |
| 130 | + :on-first-message-received (fn [_] |
| 131 | + (messenger/chat-content-received |
| 132 | + messenger |
| 133 | + {:chat-id chat-id |
| 134 | + :request-id request-id |
| 135 | + :role :system |
| 136 | + :content {:type :progress |
| 137 | + :state :running |
| 138 | + :text "Generating"}})) |
| 139 | + :on-message-received (fn [{:keys [type] :as msg}] |
| 140 | + (case type |
| 141 | + :text (do |
| 142 | + (swap! received-msgs* str (:text msg)) |
| 143 | + (messenger/chat-content-received |
159 | 144 | messenger |
160 | 145 | {:chat-id chat-id |
161 | 146 | :request-id request-id |
162 | | - :role :system |
163 | | - :content {:type :progress |
164 | | - :state :finished}})))) |
165 | | - :on-tool-called (fn [{:keys [name arguments]}] |
166 | | - (messenger/chat-content-received |
167 | | - messenger |
168 | | - {:chat-id chat-id |
169 | | - :request-id request-id |
170 | | - :role :assistant |
171 | | - :content {:type :mcpToolCall |
172 | | - :name name |
173 | | - :arguments arguments |
174 | | - :manual-approval false}}) |
175 | | - (f.mcp/call-tool! name arguments @db*)) |
176 | | - :on-error (fn [{:keys [message exception]}] |
177 | | - (messenger/chat-content-received |
178 | | - messenger |
179 | | - {:chat-id chat-id |
180 | | - :request-id request-id |
181 | | - :role :system |
182 | | - :content {:type :text |
183 | | - :text (str (or message (ex-message exception)) "\n")}}) |
184 | | - (messenger/chat-content-received |
185 | | - messenger |
186 | | - {:chat-id chat-id |
187 | | - :request-id request-id |
188 | | - :role :system |
189 | | - :content {:type :progress |
190 | | - :state :finished}}))}) |
| 147 | + :role :assistant |
| 148 | + :content {:type :text |
| 149 | + :text (:text msg)}})) |
| 150 | + :url (messenger/chat-content-received |
| 151 | + messenger |
| 152 | + {:chat-id chat-id |
| 153 | + :request-id request-id |
| 154 | + :role :assistant |
| 155 | + :content {:type :url |
| 156 | + :title (:title msg) |
| 157 | + :url (:url msg)}}) |
| 158 | + :finish (do |
| 159 | + (swap! db* update-in [:chats chat-id :messages] |
| 160 | + (fnil conj []) |
| 161 | + {:role "assistant" |
| 162 | + :content @received-msgs*}) |
| 163 | + (messenger/chat-content-received |
| 164 | + messenger |
| 165 | + {:chat-id chat-id |
| 166 | + :request-id request-id |
| 167 | + :role :system |
| 168 | + :content {:type :progress |
| 169 | + :state :finished}})))) |
| 170 | + :on-tool-called (fn [{:keys [name arguments]}] |
| 171 | + (messenger/chat-content-received |
| 172 | + messenger |
| 173 | + {:chat-id chat-id |
| 174 | + :request-id request-id |
| 175 | + :role :assistant |
| 176 | + :content {:type :mcpToolCall |
| 177 | + :name name |
| 178 | + :arguments arguments |
| 179 | + :manual-approval false}}) |
| 180 | + (f.mcp/call-tool! name arguments @db*)) |
| 181 | + :on-error (fn [{:keys [message exception]}] |
| 182 | + (messenger/chat-content-received |
| 183 | + messenger |
| 184 | + {:chat-id chat-id |
| 185 | + :request-id request-id |
| 186 | + :role :system |
| 187 | + :content {:type :text |
| 188 | + :text (str (or message (ex-message exception)) "\n")}}) |
| 189 | + (messenger/chat-content-received |
| 190 | + messenger |
| 191 | + {:chat-id chat-id |
| 192 | + :request-id request-id |
| 193 | + :role :system |
| 194 | + :content {:type :progress |
| 195 | + :state :finished}}))}) |
191 | 196 | {:chat-id chat-id |
192 | 197 | :model chosen-model |
193 | 198 | :status :success})) |
|
0 commit comments