Mercurial Hosting > lang
view src/lib/Chat.luan @ 66:2ff1f78bb13e
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 22 Aug 2025 07:25:35 -0600 |
parents | ecb851fabd75 |
children | f5e72f2d1025 |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local Number = require "luan:Number.luan" local long = Number.long or error() local Time = require "luan:Time.luan" local time_now = Time.now or error() local Html = require "luan:Html.luan" local html_encode = Html.encode or error() local Db = require "site:/lib/Db.luan" local run_in_transaction = Db.run_in_transaction or error() local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan" local Course = require "site:/lib/Course.luan" local get_course_by_id = Course.get_by_id or error() local Shared = require "site:/lib/Shared.luan" local voices = Shared.voices or error() local languages = Shared.languages or error() local User = require "site:/lib/User.luan" local get_user_by_id = User.get_by_id or error() local Chat = {} local function from_doc(doc) doc.type == "chat" or error "wrong type" return Chat.new { id = doc.id user_id = doc.chat_user_id updated = doc.chat_updated course_id = doc.course_id name = doc.name ai_thread = doc.ai_thread language = doc.language tts_instructions = doc.tts_instructions voice = doc.voice show_text = doc.show_text autoplay = doc.autoplay == "true" is_private = doc.is_private == "true" has_ruby = doc.has_ruby == "true" } end local function to_doc(chat) return { type = "chat" id = chat.id chat_user_id = long(chat.user_id) chat_updated = long(chat.updated) course_id = long(chat.course_id) name = chat.name or error() ai_thread = chat.ai_thread or error() language = chat.language or error() tts_instructions = chat.tts_instructions -- or error() voice = chat.voice or error() show_text = chat.show_text autoplay = chat.autoplay and "true" or "false" is_private = chat.is_private and "true" or nil has_ruby = chat.has_ruby and "true" or nil } end function Chat.new(chat) chat.updated = chat.updated or time_now() chat.voice = chat.voice or voices[1] if chat.autoplay==nil then chat.autoplay = true end function chat.save() local doc = to_doc(chat) Db.save(doc) chat.id = doc.id end function chat.reload() return Chat.get_by_id(chat.id) or error(chat.id) end function chat.delete() Db.delete("id:"..chat.id) end function chat.info() return { id = chat.id voice = chat.voice tts_instructions = chat.tts_instructions name = chat.name show_text = chat.show_text autoplay = chat.autoplay is_private = chat.is_private } end function chat.name_html() return html_encode(chat.name) end function chat.init_text() -- return text for textarea if Ai_chat.has_messages(chat.ai_thread) then return "" end local course = get_course_by_id(chat.course_id) or error() return course.ai_first_message or error() end function chat.output_system_prompt() Ai_chat.output_system_prompt(chat.ai_thread) end local function option(name,text) local selected = name==chat.show_text and " selected" or "" %> <option <%=name%><%=selected%>><%=text%></option> <% end local function assistant_controls() return `%> <div controls> <audio controls preload=none></audio> <select> <% option("show_text","Show text") if chat.has_ruby then option("hide_ruby","Hide pronunciation") end option("hide_text","Hide text") %> </select> </div> <% ` end function chat.output_messages_html() Ai_chat.output_messages_html(assistant_controls(),chat.ai_thread) end function chat.ask(input) local old_thread = chat.ai_thread local ai_thread = Ai_chat.ask(old_thread,input) run_in_transaction( function() chat = chat.reload() chat.ai_thread = ai_thread chat.save() end ) return `Ai_chat.output_messages_html(assistant_controls(),ai_thread,old_thread)` end function chat.language_name() return languages[chat.language] end function chat.get_user() return get_user_by_id(chat.user_id) end return chat end function Chat.search(query,sort,rows) rows = rows or 1000000 local chats = {} local docs = Db.search(query,1,rows,{sort=sort}) for _, doc in ipairs(docs) do chats[#chats+1] = from_doc(doc) end return chats end function Chat.get_by_id(id) local doc = Db.get_document("id:"..id) return doc and doc.type=="chat" and from_doc(doc) or nil end return Chat