|
| 1 | +defmodule AlexClawWeb.AdminLive.Chat do |
| 2 | + @moduledoc "Interactive chat with memory-backed semantic search context." |
| 3 | + |
| 4 | + use Phoenix.LiveView |
| 5 | + |
| 6 | + alias AlexClaw.{Identity, LLM, Memory} |
| 7 | + |
| 8 | + @impl true |
| 9 | + def mount(_params, _session, socket) do |
| 10 | + providers = LLM.list_provider_choices() |
| 11 | + |
| 12 | + {:ok, |
| 13 | + assign(socket, |
| 14 | + page_title: "Chat", |
| 15 | + messages: [], |
| 16 | + loading: false, |
| 17 | + memory_hits: 0, |
| 18 | + provider: "auto", |
| 19 | + providers: providers |
| 20 | + )} |
| 21 | + end |
| 22 | + |
| 23 | + @impl true |
| 24 | + def handle_event("send", %{"message" => message}, socket) do |
| 25 | + message = String.trim(message) |
| 26 | + |
| 27 | + if message == "" do |
| 28 | + {:noreply, socket} |
| 29 | + else |
| 30 | + user_msg = %{role: :user, content: message, timestamp: DateTime.utc_now()} |
| 31 | + messages = socket.assigns.messages ++ [user_msg] |
| 32 | + provider = socket.assigns.provider |
| 33 | + |
| 34 | + socket = |
| 35 | + socket |
| 36 | + |> assign(messages: messages, loading: true) |
| 37 | + |> start_async(:llm_response, fn -> generate_response(message, provider) end) |
| 38 | + |
| 39 | + {:noreply, socket} |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def handle_event("set_provider", %{"provider" => provider}, socket) do |
| 44 | + {:noreply, assign(socket, provider: provider)} |
| 45 | + end |
| 46 | + |
| 47 | + def handle_event("clear", _params, socket) do |
| 48 | + {:noreply, assign(socket, messages: [], memory_hits: 0)} |
| 49 | + end |
| 50 | + |
| 51 | + @impl true |
| 52 | + def handle_async(:llm_response, {:ok, {:error, reason}}, socket) do |
| 53 | + error_msg = %{ |
| 54 | + role: :system, |
| 55 | + content: "Error: #{inspect(reason)}", |
| 56 | + timestamp: DateTime.utc_now() |
| 57 | + } |
| 58 | + |
| 59 | + messages = socket.assigns.messages ++ [error_msg] |
| 60 | + {:noreply, assign(socket, messages: messages, loading: false)} |
| 61 | + end |
| 62 | + |
| 63 | + def handle_async(:llm_response, {:ok, {response, memory_count}}, socket) do |
| 64 | + ai_msg = %{role: :assistant, content: response, timestamp: DateTime.utc_now()} |
| 65 | + messages = socket.assigns.messages ++ [ai_msg] |
| 66 | + |
| 67 | + user_msg = List.last(Enum.filter(socket.assigns.messages, &(&1.role == :user))) |
| 68 | + |
| 69 | + if user_msg do |
| 70 | + Memory.store(:conversation, "User: #{user_msg.content}", source: "web_chat") |
| 71 | + Memory.store(:conversation, "AlexClaw: #{response}", source: "web_chat") |
| 72 | + end |
| 73 | + |
| 74 | + {:noreply, assign(socket, messages: messages, loading: false, memory_hits: memory_count)} |
| 75 | + end |
| 76 | + |
| 77 | + def handle_async(:llm_response, {:exit, reason}, socket) do |
| 78 | + error_msg = %{ |
| 79 | + role: :system, |
| 80 | + content: "Request failed: #{inspect(reason)}", |
| 81 | + timestamp: DateTime.utc_now() |
| 82 | + } |
| 83 | + |
| 84 | + messages = socket.assigns.messages ++ [error_msg] |
| 85 | + {:noreply, assign(socket, messages: messages, loading: false)} |
| 86 | + end |
| 87 | + |
| 88 | + defp generate_response(query, provider) do |
| 89 | + system = Identity.system_prompt(%{skill: :conversational}) |
| 90 | + |
| 91 | + memory_results = Memory.search(query, limit: 5) |
| 92 | + |
| 93 | + memory_context = |
| 94 | + case memory_results do |
| 95 | + [] -> |
| 96 | + "" |
| 97 | + |
| 98 | + entries -> |
| 99 | + context = |
| 100 | + entries |
| 101 | + |> Enum.map_join("\n---\n", fn e -> |
| 102 | + kind_label = String.upcase(e.kind) |
| 103 | + "[#{kind_label}] #{String.slice(e.content, 0, 500)}" |
| 104 | + end) |
| 105 | + |
| 106 | + "\n\nRelevant knowledge from memory:\n#{context}" |
| 107 | + end |
| 108 | + |
| 109 | + recent = |
| 110 | + case Memory.recent(kind: :conversation, limit: 6) do |
| 111 | + [] -> |
| 112 | + "" |
| 113 | + |
| 114 | + entries -> |
| 115 | + history = |
| 116 | + entries |
| 117 | + |> Enum.reverse() |
| 118 | + |> Enum.map_join("\n", & &1.content) |
| 119 | + |
| 120 | + "\n\nRecent conversation:\n#{history}" |
| 121 | + end |
| 122 | + |
| 123 | + prompt = "#{memory_context}#{recent}\n\nUser: #{query}" |
| 124 | + |
| 125 | + opts = |
| 126 | + case provider do |
| 127 | + "auto" -> [tier: :light] |
| 128 | + name -> [provider: name] |
| 129 | + end |
| 130 | + |
| 131 | + case LLM.complete(prompt, opts ++ [system: system]) do |
| 132 | + {:ok, response} -> {response, length(memory_results)} |
| 133 | + {:error, reason} -> {:error, reason} |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + @impl true |
| 138 | + def render(assigns) do |
| 139 | + ~H""" |
| 140 | + <div class="flex flex-col h-[calc(100vh-8rem)]"> |
| 141 | + <div class="flex items-center justify-between mb-4"> |
| 142 | + <div> |
| 143 | + <h1 class="text-xl font-bold text-white">Chat</h1> |
| 144 | + <p class="text-xs text-gray-500 mt-1"> |
| 145 | + Conversation with semantic memory context |
| 146 | + <%= if @memory_hits > 0 do %> |
| 147 | + — <span class="text-claw-500">{@memory_hits} memory matches</span> on last query |
| 148 | + <% end %> |
| 149 | + </p> |
| 150 | + </div> |
| 151 | + <div class="flex items-center gap-3"> |
| 152 | + <form phx-change="set_provider"> |
| 153 | + <select |
| 154 | + name="provider" |
| 155 | + class="bg-gray-800 border border-gray-700 rounded px-3 py-1.5 text-xs text-gray-300" |
| 156 | + > |
| 157 | + <option |
| 158 | + :for={p <- @providers} |
| 159 | + value={p.value} |
| 160 | + selected={@provider == p.value} |
| 161 | + > |
| 162 | + {p.label} |
| 163 | + </option> |
| 164 | + </select> |
| 165 | + </form> |
| 166 | + <button |
| 167 | + :if={@messages != []} |
| 168 | + phx-click="clear" |
| 169 | + class="px-3 py-1.5 text-xs text-gray-400 hover:text-white bg-gray-800 hover:bg-gray-700 rounded transition" |
| 170 | + > |
| 171 | + Clear |
| 172 | + </button> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | +
|
| 176 | + <div |
| 177 | + class="flex-1 overflow-y-auto bg-gray-900 rounded-lg border border-gray-800 p-4 space-y-4" |
| 178 | + id="chat-messages" |
| 179 | + > |
| 180 | + <div :if={@messages == []} class="flex items-center justify-center h-full"> |
| 181 | + <div class="text-center text-gray-500"> |
| 182 | + <p class="text-lg mb-2">Ask anything.</p> |
| 183 | + <p class="text-xs"> |
| 184 | + Responses are enriched with semantic search across all stored memories — news, CVEs, research, conversations. |
| 185 | + </p> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | +
|
| 189 | + <div :for={msg <- @messages} class={["flex", msg.role == :user && "justify-end"]}> |
| 190 | + <div class={[ |
| 191 | + "max-w-[75%] rounded-lg px-4 py-3 text-sm", |
| 192 | + msg.role == :user && "bg-claw-800 text-white", |
| 193 | + msg.role == :assistant && "bg-gray-800 text-gray-100", |
| 194 | + msg.role == :system && "bg-red-900/30 text-red-300 border border-red-800" |
| 195 | + ]}> |
| 196 | + <div class="whitespace-pre-wrap break-words">{msg.content}</div> |
| 197 | + <div class="text-xs text-gray-500 mt-1"> |
| 198 | + {Calendar.strftime(msg.timestamp, "%H:%M")} |
| 199 | + </div> |
| 200 | + </div> |
| 201 | + </div> |
| 202 | +
|
| 203 | + <div :if={@loading} class="flex"> |
| 204 | + <div class="bg-gray-800 rounded-lg px-4 py-3 text-sm text-gray-400 animate-pulse"> |
| 205 | + Thinking... |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | +
|
| 210 | + <form phx-submit="send" class="mt-4 flex gap-3"> |
| 211 | + <input |
| 212 | + type="text" |
| 213 | + name="message" |
| 214 | + placeholder="Ask about CVEs, security alerts, or anything in memory..." |
| 215 | + autocomplete="off" |
| 216 | + disabled={@loading} |
| 217 | + phx-debounce="100" |
| 218 | + class="flex-1 bg-gray-800 border border-gray-700 rounded-lg px-4 py-3 text-white text-sm focus:border-claw-500 focus:outline-none disabled:opacity-50" |
| 219 | + /> |
| 220 | + <button |
| 221 | + type="submit" |
| 222 | + disabled={@loading} |
| 223 | + class="px-6 py-3 bg-claw-700 hover:bg-claw-600 disabled:bg-gray-700 text-white text-sm rounded-lg transition" |
| 224 | + > |
| 225 | + Send |
| 226 | + </button> |
| 227 | + </form> |
| 228 | + </div> |
| 229 | + """ |
| 230 | + end |
| 231 | +end |
0 commit comments