Mercurial Hosting > lang
annotate src/lib/Chat.luan @ 69:f5e72f2d1025
add stt_prompt
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 23 Aug 2025 07:07:59 -0600 |
parents | ecb851fabd75 |
children | 44bec62c49e2 |
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 |
29 | 93 } |
94 end | |
95 | |
4 | 96 function chat.name_html() |
97 return html_encode(chat.name) | |
98 end | |
99 | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
100 function chat.init_text() -- return text for textarea |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
101 if Ai_chat.has_messages(chat.ai_thread) then |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
102 return "" |
25 | 103 end |
104 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
|
105 return course.ai_first_message or error() |
25 | 106 end |
107 | |
108 function chat.output_system_prompt() | |
109 Ai_chat.output_system_prompt(chat.ai_thread) | |
110 end | |
111 | |
52 | 112 local function option(name,text) |
113 local selected = name==chat.show_text and " selected" or "" | |
114 %> | |
115 <option <%=name%><%=selected%>><%=text%></option> | |
116 <% | |
117 end | |
118 | |
119 local function assistant_controls() | |
120 return `%> | |
121 <div controls> | |
122 <audio controls preload=none></audio> | |
123 <select> | |
124 <% | |
125 option("show_text","Show text") | |
126 if chat.has_ruby then | |
127 option("hide_ruby","Hide pronunciation") | |
128 end | |
129 option("hide_text","Hide text") | |
130 %> | |
131 </select> | |
132 </div> | |
133 <% ` | |
134 end | |
135 | |
25 | 136 function chat.output_messages_html() |
52 | 137 Ai_chat.output_messages_html(assistant_controls(),chat.ai_thread) |
25 | 138 end |
139 | |
140 function chat.ask(input) | |
141 local old_thread = chat.ai_thread | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
142 local ai_thread = Ai_chat.ask(old_thread,input) |
25 | 143 run_in_transaction( function() |
144 chat = chat.reload() | |
145 chat.ai_thread = ai_thread | |
146 chat.save() | |
147 end ) | |
52 | 148 return `Ai_chat.output_messages_html(assistant_controls(),ai_thread,old_thread)` |
5 | 149 end |
150 | |
21 | 151 function chat.language_name() |
46 | 152 return languages[chat.language] |
21 | 153 end |
154 | |
65 | 155 function chat.get_user() |
156 return get_user_by_id(chat.user_id) | |
157 end | |
158 | |
4 | 159 return chat |
160 end | |
161 | |
162 function Chat.search(query,sort,rows) | |
163 rows = rows or 1000000 | |
164 local chats = {} | |
165 local docs = Db.search(query,1,rows,{sort=sort}) | |
166 for _, doc in ipairs(docs) do | |
167 chats[#chats+1] = from_doc(doc) | |
168 end | |
169 return chats | |
170 end | |
171 | |
172 function Chat.get_by_id(id) | |
173 local doc = Db.get_document("id:"..id) | |
174 return doc and doc.type=="chat" and from_doc(doc) or nil | |
175 end | |
176 | |
177 return Chat |