Mercurial Hosting > lang
annotate src/lib/Chat.luan @ 75:b96cf27e719d default tip
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 28 Aug 2025 14:36:03 -0600 |
parents | 64e35a92d163 |
children |
rev | line source |
---|---|
4 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local ipairs = Luan.ipairs or error() | |
4 local Number = require "luan:Number.luan" | |
5 local long = Number.long or error() | |
6 local Time = require "luan:Time.luan" | |
7 local time_now = Time.now or error() | |
8 local Html = require "luan:Html.luan" | |
9 local html_encode = Html.encode or error() | |
10 local Db = require "site:/lib/Db.luan" | |
5 | 11 local run_in_transaction = Db.run_in_transaction or error() |
25 | 12 local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan" |
13 local Course = require "site:/lib/Course.luan" | |
14 local get_course_by_id = Course.get_by_id or error() | |
31 | 15 local Shared = require "site:/lib/Shared.luan" |
16 local voices = Shared.voices or error() | |
46 | 17 local languages = Shared.languages or error() |
65 | 18 local User = require "site:/lib/User.luan" |
19 local get_user_by_id = User.get_by_id or error() | |
4 | 20 |
21 | |
22 local Chat = {} | |
23 | |
24 local function from_doc(doc) | |
25 doc.type == "chat" or error "wrong type" | |
26 return Chat.new { | |
27 id = doc.id | |
28 user_id = doc.chat_user_id | |
29 updated = doc.chat_updated | |
24 | 30 course_id = doc.course_id |
4 | 31 name = doc.name |
5 | 32 ai_thread = doc.ai_thread |
16 | 33 language = doc.language |
46 | 34 tts_instructions = doc.tts_instructions |
31 | 35 voice = doc.voice |
52 | 36 show_text = doc.show_text |
36 | 37 autoplay = doc.autoplay == "true" |
41 | 38 is_private = doc.is_private == "true" |
52 | 39 has_ruby = doc.has_ruby == "true" |
69 | 40 stt_prompt = doc.stt_prompt or "" |
4 | 41 } |
42 end | |
43 | |
44 local function to_doc(chat) | |
45 return { | |
46 type = "chat" | |
47 id = chat.id | |
48 chat_user_id = long(chat.user_id) | |
49 chat_updated = long(chat.updated) | |
24 | 50 course_id = long(chat.course_id) |
4 | 51 name = chat.name or error() |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
52 ai_thread = chat.ai_thread or error() |
16 | 53 language = chat.language or error() |
69 | 54 tts_instructions = chat.tts_instructions or error() |
31 | 55 voice = chat.voice or error() |
52 | 56 show_text = chat.show_text |
36 | 57 autoplay = chat.autoplay and "true" or "false" |
41 | 58 is_private = chat.is_private and "true" or nil |
52 | 59 has_ruby = chat.has_ruby and "true" or nil |
69 | 60 stt_prompt = chat.stt_prompt or error() |
4 | 61 } |
62 end | |
63 | |
64 function Chat.new(chat) | |
65 chat.updated = chat.updated or time_now() | |
46 | 66 chat.voice = chat.voice or voices[1] |
36 | 67 if chat.autoplay==nil then chat.autoplay = true end |
4 | 68 |
69 function chat.save() | |
70 local doc = to_doc(chat) | |
71 Db.save(doc) | |
72 chat.id = doc.id | |
73 end | |
74 | |
5 | 75 function chat.reload() |
76 return Chat.get_by_id(chat.id) or error(chat.id) | |
77 end | |
78 | |
4 | 79 function chat.delete() |
80 Db.delete("id:"..chat.id) | |
81 end | |
82 | |
29 | 83 function chat.info() |
84 return { | |
85 id = chat.id | |
31 | 86 voice = chat.voice |
46 | 87 tts_instructions = chat.tts_instructions |
29 | 88 name = chat.name |
34 | 89 show_text = chat.show_text |
36 | 90 autoplay = chat.autoplay |
41 | 91 is_private = chat.is_private |
69 | 92 stt_prompt = chat.stt_prompt |
74 | 93 language = chat.language |
29 | 94 } |
95 end | |
96 | |
4 | 97 function chat.name_html() |
98 return html_encode(chat.name) | |
99 end | |
100 | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
101 function chat.init_text() -- return text for textarea |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
102 if Ai_chat.has_messages(chat.ai_thread) then |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
103 return "" |
25 | 104 end |
105 local course = get_course_by_id(chat.course_id) or error() | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
106 return course.ai_first_message or error() |
25 | 107 end |
108 | |
109 function chat.output_system_prompt() | |
110 Ai_chat.output_system_prompt(chat.ai_thread) | |
111 end | |
112 | |
52 | 113 local function option(name,text) |
114 local selected = name==chat.show_text and " selected" or "" | |
115 %> | |
74 | 116 <option value=<%=name%><%=selected%>><%=text%></option> |
52 | 117 <% |
118 end | |
119 | |
120 local function assistant_controls() | |
121 return `%> | |
74 | 122 <div trans=needed>Translating...</div> |
52 | 123 <div controls> |
124 <audio controls preload=none></audio> | |
74 | 125 <select onchange="showSelected(this)"> |
52 | 126 <% |
74 | 127 option("hide_text","Hide text") |
52 | 128 if chat.has_ruby then |
129 option("hide_ruby","Hide pronunciation") | |
130 end | |
74 | 131 option("show_text","Show text") |
132 if chat.language ~= "en" then | |
133 option("show_trans","Show translation") | |
134 end | |
52 | 135 %> |
136 </select> | |
137 </div> | |
138 <% ` | |
139 end | |
140 | |
25 | 141 function chat.output_messages_html() |
52 | 142 Ai_chat.output_messages_html(assistant_controls(),chat.ai_thread) |
25 | 143 end |
144 | |
71 | 145 function chat.ai_add(input) |
25 | 146 local old_thread = chat.ai_thread |
71 | 147 local ai_thread = Ai_chat.add(old_thread,input) |
148 run_in_transaction( function() | |
149 chat = chat.reload() | |
150 chat.ai_thread = ai_thread | |
151 chat.save() | |
152 end ) | |
153 return `Ai_chat.output_messages_html(assistant_controls(),ai_thread,old_thread)` | |
154 end | |
155 | |
156 function chat.ai_respond() | |
157 local old_thread = chat.ai_thread | |
158 local ai_thread = Ai_chat.respond(old_thread) | |
25 | 159 run_in_transaction( function() |
160 chat = chat.reload() | |
161 chat.ai_thread = ai_thread | |
162 chat.save() | |
163 end ) | |
52 | 164 return `Ai_chat.output_messages_html(assistant_controls(),ai_thread,old_thread)` |
5 | 165 end |
166 | |
21 | 167 function chat.language_name() |
46 | 168 return languages[chat.language] |
21 | 169 end |
170 | |
65 | 171 function chat.get_user() |
172 return get_user_by_id(chat.user_id) | |
173 end | |
174 | |
4 | 175 return chat |
176 end | |
177 | |
178 function Chat.search(query,sort,rows) | |
179 rows = rows or 1000000 | |
180 local chats = {} | |
181 local docs = Db.search(query,1,rows,{sort=sort}) | |
182 for _, doc in ipairs(docs) do | |
183 chats[#chats+1] = from_doc(doc) | |
184 end | |
185 return chats | |
186 end | |
187 | |
188 function Chat.get_by_id(id) | |
189 local doc = Db.get_document("id:"..id) | |
190 return doc and doc.type=="chat" and from_doc(doc) or nil | |
191 end | |
192 | |
193 return Chat |