Mercurial Hosting > lang
annotate src/lib/Chat.luan @ 52:27758f3b2d69
add hide_ruby
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 16 Aug 2025 09:56:10 +0900 |
parents | cc20eebaa74a |
children | ecb851fabd75 |
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() |
4 | 18 |
19 | |
20 local Chat = {} | |
21 | |
22 local function from_doc(doc) | |
23 doc.type == "chat" or error "wrong type" | |
24 return Chat.new { | |
25 id = doc.id | |
26 user_id = doc.chat_user_id | |
27 updated = doc.chat_updated | |
24 | 28 course_id = doc.course_id |
4 | 29 name = doc.name |
5 | 30 ai_thread = doc.ai_thread |
16 | 31 language = doc.language |
46 | 32 tts_instructions = doc.tts_instructions |
31 | 33 voice = doc.voice |
52 | 34 show_text = doc.show_text |
36 | 35 autoplay = doc.autoplay == "true" |
41 | 36 is_private = doc.is_private == "true" |
52 | 37 has_ruby = doc.has_ruby == "true" |
4 | 38 } |
39 end | |
40 | |
41 local function to_doc(chat) | |
42 return { | |
43 type = "chat" | |
44 id = chat.id | |
45 chat_user_id = long(chat.user_id) | |
46 chat_updated = long(chat.updated) | |
24 | 47 course_id = long(chat.course_id) |
4 | 48 name = chat.name or error() |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
49 ai_thread = chat.ai_thread or error() |
16 | 50 language = chat.language or error() |
46 | 51 tts_instructions = chat.tts_instructions -- or error() |
31 | 52 voice = chat.voice or error() |
52 | 53 show_text = chat.show_text |
36 | 54 autoplay = chat.autoplay and "true" or "false" |
41 | 55 is_private = chat.is_private and "true" or nil |
52 | 56 has_ruby = chat.has_ruby and "true" or nil |
4 | 57 } |
58 end | |
59 | |
60 function Chat.new(chat) | |
61 chat.updated = chat.updated or time_now() | |
46 | 62 chat.voice = chat.voice or voices[1] |
36 | 63 if chat.autoplay==nil then chat.autoplay = true end |
4 | 64 |
65 function chat.save() | |
66 local doc = to_doc(chat) | |
67 Db.save(doc) | |
68 chat.id = doc.id | |
69 end | |
70 | |
5 | 71 function chat.reload() |
72 return Chat.get_by_id(chat.id) or error(chat.id) | |
73 end | |
74 | |
4 | 75 function chat.delete() |
76 Db.delete("id:"..chat.id) | |
77 end | |
78 | |
29 | 79 function chat.info() |
80 return { | |
81 id = chat.id | |
31 | 82 voice = chat.voice |
46 | 83 tts_instructions = chat.tts_instructions |
29 | 84 name = chat.name |
34 | 85 show_text = chat.show_text |
36 | 86 autoplay = chat.autoplay |
41 | 87 is_private = chat.is_private |
29 | 88 } |
89 end | |
90 | |
4 | 91 function chat.name_html() |
92 return html_encode(chat.name) | |
93 end | |
94 | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
95 function chat.init_text() -- return text for textarea |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
96 if Ai_chat.has_messages(chat.ai_thread) then |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
97 return "" |
25 | 98 end |
99 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
|
100 return course.ai_first_message or error() |
25 | 101 end |
102 | |
103 function chat.output_system_prompt() | |
104 Ai_chat.output_system_prompt(chat.ai_thread) | |
105 end | |
106 | |
52 | 107 local function option(name,text) |
108 local selected = name==chat.show_text and " selected" or "" | |
109 %> | |
110 <option <%=name%><%=selected%>><%=text%></option> | |
111 <% | |
112 end | |
113 | |
114 local function assistant_controls() | |
115 return `%> | |
116 <div controls> | |
117 <audio controls preload=none></audio> | |
118 <select> | |
119 <% | |
120 option("show_text","Show text") | |
121 if chat.has_ruby then | |
122 option("hide_ruby","Hide pronunciation") | |
123 end | |
124 option("hide_text","Hide text") | |
125 %> | |
126 </select> | |
127 </div> | |
128 <% ` | |
129 end | |
130 | |
25 | 131 function chat.output_messages_html() |
52 | 132 Ai_chat.output_messages_html(assistant_controls(),chat.ai_thread) |
25 | 133 end |
134 | |
135 function chat.ask(input) | |
136 local old_thread = chat.ai_thread | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
137 local ai_thread = Ai_chat.ask(old_thread,input) |
25 | 138 run_in_transaction( function() |
139 chat = chat.reload() | |
140 chat.ai_thread = ai_thread | |
141 chat.save() | |
142 end ) | |
52 | 143 return `Ai_chat.output_messages_html(assistant_controls(),ai_thread,old_thread)` |
5 | 144 end |
145 | |
21 | 146 function chat.language_name() |
46 | 147 return languages[chat.language] |
21 | 148 end |
149 | |
4 | 150 return chat |
151 end | |
152 | |
153 function Chat.search(query,sort,rows) | |
154 rows = rows or 1000000 | |
155 local chats = {} | |
156 local docs = Db.search(query,1,rows,{sort=sort}) | |
157 for _, doc in ipairs(docs) do | |
158 chats[#chats+1] = from_doc(doc) | |
159 end | |
160 return chats | |
161 end | |
162 | |
163 function Chat.get_by_id(id) | |
164 local doc = Db.get_document("id:"..id) | |
165 return doc and doc.type=="chat" and from_doc(doc) or nil | |
166 end | |
167 | |
168 return Chat |