Mercurial Hosting > lang
changeset 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 | 2737eeedc1d5 |
files | src/chat.html.luan src/lib/Chat.luan src/lib/ai/claude/Ai_chat.luan src/new_chat.red.luan src/save_course.js.luan |
diffstat | 5 files changed, 29 insertions(+), 46 deletions(-) [+] |
line wrap: on
line diff
--- a/src/chat.html.luan Mon Aug 04 23:06:19 2025 -0600 +++ b/src/chat.html.luan Tue Aug 05 16:41:29 2025 -0600 @@ -3,6 +3,8 @@ local ipairs = Luan.ipairs or error() local Parsers = require "luan:Parsers.luan" local json_string = Parsers.json_string or error() +local Html = require "luan:Html.luan" +local html_encode = Html.encode or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" @@ -22,7 +24,7 @@ if user == nil then return end local chat_id = Http.request.parameters.chat or error() local chat = get_chat_by_id(chat_id) or error() - local added_message = chat.init_ai() + local init_text = chat.init_text() Io.stdout = Http.response.text_writer() %> <!doctype html> @@ -52,7 +54,7 @@ <% chat.output_messages_html() %> </div> <div ask> - <textarea autofocus oninput="fixTextarea(event.target)" onkeydown="textareaKey(event)"></textarea> + <textarea autofocus oninput="fixTextarea(event.target)" onkeydown="textareaKey(event)"><%= html_encode(init_text) %></textarea> <div buttons> <audio controls preload=none></audio> <button record onclick="toggleRecording()">Record</button> @@ -124,9 +126,6 @@ initialized.value = 'yes'; //alert('init'); scrollToEnd(); -<% if added_message then %> - playLastMessage(); -<% end %> } },10); </script>
--- a/src/lib/Chat.luan Mon Aug 04 23:06:19 2025 -0600 +++ b/src/lib/Chat.luan Tue Aug 05 16:41:29 2025 -0600 @@ -43,7 +43,7 @@ chat_updated = long(chat.updated) course_id = long(chat.course_id) name = chat.name or error() - ai_thread = chat.ai_thread + ai_thread = chat.ai_thread or error() language = chat.language or error() language_region = chat.language_region or error() voice = chat.voice or error() @@ -89,19 +89,12 @@ return html_encode(chat.name) end - function chat.init_ai() -- return if added message - if chat.ai_thread ~= nil then - return false + 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() - local ai_first_message = course.ai_first_message - local ai_thread = Ai_chat.ask_first(course.ai_system_prompt,ai_first_message) - run_in_transaction( function() - chat = chat.reload() - chat.ai_thread = ai_thread - chat.save() - end ) - return ai_first_message ~= nil + return course.ai_first_message or error() end function chat.output_system_prompt() @@ -114,7 +107,7 @@ function chat.ask(input) local old_thread = chat.ai_thread - local ai_thread = Ai_chat.ask_more(old_thread,input) + local ai_thread = Ai_chat.ask(old_thread,input) run_in_transaction( function() chat = chat.reload() chat.ai_thread = ai_thread
--- a/src/lib/ai/claude/Ai_chat.luan Mon Aug 04 23:06:19 2025 -0600 +++ b/src/lib/ai/claude/Ai_chat.luan Tue Aug 05 16:41:29 2025 -0600 @@ -18,9 +18,6 @@ local Ai_chat = {} function Ai_chat.output_system_prompt(thread) - if thread == nil then - return - end thread = json_parse(thread) local system_prompt = thread.system or error system_prompt = html_encode(system_prompt) @@ -28,9 +25,6 @@ end function Ai_chat.output_messages_html(show_text,thread,old_thread) - if thread == nil then - return - end thread = json_parse(thread) local messages = thread.messages or error local n = 0 @@ -83,7 +77,21 @@ end -local function ask(thread,input) +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" @@ -112,22 +120,6 @@ role = "assistant" content = content } -end - -function Ai_chat.ask_first(system_prompt,input) - local thread = { - system = system_prompt - messages = {nil} - } - if input ~= nil then - ask(thread,input) - end - return json_string(thread) -end - -function Ai_chat.ask_more(thread,input) - thread = json_parse(thread) - ask(thread,input) return json_string(thread) end
--- a/src/new_chat.red.luan Mon Aug 04 23:06:19 2025 -0600 +++ b/src/new_chat.red.luan Tue Aug 05 16:41:29 2025 -0600 @@ -6,6 +6,8 @@ local Chat = require "site:/lib/Chat.luan" local Course = require "site:/lib/Course.luan" local get_course_by_id = Course.get_by_id or error() +local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan" +local ai_init = Ai_chat.init or error() @@ -18,6 +20,7 @@ course_id = course.id name = course.name language = course.language + ai_thread = ai_init(course.ai_system_prompt) } chat.save() Http.response.send_redirect("chat.html?chat="..chat.id)
--- a/src/save_course.js.luan Mon Aug 04 23:06:19 2025 -0600 +++ b/src/save_course.js.luan Tue Aug 05 16:41:29 2025 -0600 @@ -1,7 +1,5 @@ local Luan = require "luan:Luan.luan" local error = Luan.error -local String = require "luan:String.luan" -local trim = String.trim or error() local Time = require "luan:Time.luan" local time_now = Time.now or error() local Io = require "luan:Io.luan" @@ -31,9 +29,7 @@ end course.name = parameters.name or error() course.ai_system_prompt = parameters.ai_system_prompt or error() - local ai_first_message = parameters.ai_first_message or error() - ai_first_message = trim(ai_first_message) - course.ai_first_message = ai_first_message~="" and ai_first_message or nil + course.ai_first_message = parameters.ai_first_message or error() course.updated = time_now() course.save() end )