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