Mercurial Hosting > lang
view src/lib/ai/claude/Ai_chat.luan @ 35:3117876debca
ai_first_message in textarea
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Aug 2025 16:41:29 -0600 |
parents | 0fb3488a017d |
children | 238a91f224b1 |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local type = Luan.type or error() local String = require "luan:String.luan" local starts_with = String.starts_with or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() local Parsers = require "luan:Parsers.luan" local json_parse = Parsers.json_parse or error() local json_string = Parsers.json_string or error() local Claude = require "site:/lib/ai/claude/Claude.luan" local claude_chat = Claude.chat or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "claude/Ai_chat" local Ai_chat = {} function Ai_chat.output_system_prompt(thread) thread = json_parse(thread) local system_prompt = thread.system or error system_prompt = html_encode(system_prompt) %><%=system_prompt%><% end function Ai_chat.output_messages_html(show_text,thread,old_thread) thread = json_parse(thread) local messages = thread.messages or error local n = 0 if old_thread ~= nil then old_thread = json_parse(old_thread) local old_messages = old_thread.messages or error n = #old_messages end local checked = show_text and "checked" or "" for i, message in ipairs(messages) do if i <= n then continue end local role = message.role or error() local who if role=="assistant" then who = "Claude" elseif role=="user" then who = "You" else error(role) end local function output(text) text = html_encode(text) %> <h3><%=who%></h3> <div role="<%=role%>"> <div message markdown><%=text%></div> <% if role=="assistant" then %> <div controls> <audio controls preload=none></audio> <label clickable><input type=checkbox name=show_text <%=checked%> >Show text</label> </div> <% end %> </div> <% end local content = message.content or error() if type(content) == "string" then output(content) else for _, part in ipairs(content) do if part.type=="text" then local text = part.text or error() output(text) end end end end_for end function Ai_chat.init(system_prompt) local thread = { system = system_prompt messages = {nil} } return json_string(thread) end function Ai_chat.has_messages(thread) thread = json_parse(thread) return #thread.messages > 0 end function Ai_chat.ask(thread,input) thread = json_parse(thread) local messages = thread.messages or error messages[#messages+1] = { role = "user" content = input } --[=[ messages[#messages+1] = { role = "assistant" content = [[ hello ]] } if true then return end --]=] -- logger.info(json_string(thread)) local resultJson = claude_chat(thread) local result = json_parse(resultJson) -- logger.info(json_string(result)) result.type == "message" or error() result.role == "assistant" or error() result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() local content = result.content or error() messages[#messages+1] = { role = "assistant" content = content } return json_string(thread) end return Ai_chat