Mercurial Hosting > lang
annotate src/lib/Chat.luan @ 46:cc20eebaa74a
use openai tts
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Aug 2025 11:27:34 +0900 |
parents | 2a4c83ce3deb |
children | 27758f3b2d69 |
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 |
34 | 34 show_text = doc.show_text == "true" |
36 | 35 autoplay = doc.autoplay == "true" |
41 | 36 is_private = doc.is_private == "true" |
4 | 37 } |
38 end | |
39 | |
40 local function to_doc(chat) | |
41 return { | |
42 type = "chat" | |
43 id = chat.id | |
44 chat_user_id = long(chat.user_id) | |
45 chat_updated = long(chat.updated) | |
24 | 46 course_id = long(chat.course_id) |
4 | 47 name = chat.name or error() |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
48 ai_thread = chat.ai_thread or error() |
16 | 49 language = chat.language or error() |
46 | 50 tts_instructions = chat.tts_instructions -- or error() |
31 | 51 voice = chat.voice or error() |
34 | 52 show_text = chat.show_text and "true" or "false" |
36 | 53 autoplay = chat.autoplay and "true" or "false" |
41 | 54 is_private = chat.is_private and "true" or nil |
4 | 55 } |
56 end | |
57 | |
58 function Chat.new(chat) | |
59 chat.updated = chat.updated or time_now() | |
46 | 60 chat.voice = chat.voice or voices[1] |
34 | 61 if chat.show_text==nil then chat.show_text = true end |
36 | 62 if chat.autoplay==nil then chat.autoplay = true end |
4 | 63 |
64 function chat.save() | |
65 local doc = to_doc(chat) | |
66 Db.save(doc) | |
67 chat.id = doc.id | |
68 end | |
69 | |
5 | 70 function chat.reload() |
71 return Chat.get_by_id(chat.id) or error(chat.id) | |
72 end | |
73 | |
4 | 74 function chat.delete() |
75 Db.delete("id:"..chat.id) | |
76 end | |
77 | |
29 | 78 function chat.info() |
79 return { | |
80 id = chat.id | |
31 | 81 voice = chat.voice |
46 | 82 tts_instructions = chat.tts_instructions |
29 | 83 name = chat.name |
34 | 84 show_text = chat.show_text |
36 | 85 autoplay = chat.autoplay |
41 | 86 is_private = chat.is_private |
29 | 87 } |
88 end | |
89 | |
4 | 90 function chat.name_html() |
91 return html_encode(chat.name) | |
92 end | |
93 | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
94 function chat.init_text() -- return text for textarea |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
95 if Ai_chat.has_messages(chat.ai_thread) then |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
96 return "" |
25 | 97 end |
98 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
|
99 return course.ai_first_message or error() |
25 | 100 end |
101 | |
102 function chat.output_system_prompt() | |
103 Ai_chat.output_system_prompt(chat.ai_thread) | |
104 end | |
105 | |
106 function chat.output_messages_html() | |
34 | 107 Ai_chat.output_messages_html(chat.show_text,chat.ai_thread) |
25 | 108 end |
109 | |
110 function chat.ask(input) | |
111 local old_thread = chat.ai_thread | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
112 local ai_thread = Ai_chat.ask(old_thread,input) |
25 | 113 run_in_transaction( function() |
114 chat = chat.reload() | |
115 chat.ai_thread = ai_thread | |
116 chat.save() | |
117 end ) | |
34 | 118 return `Ai_chat.output_messages_html(chat.show_text,ai_thread,old_thread)` |
5 | 119 end |
120 | |
21 | 121 function chat.language_name() |
46 | 122 return languages[chat.language] |
21 | 123 end |
124 | |
4 | 125 return chat |
126 end | |
127 | |
128 function Chat.search(query,sort,rows) | |
129 rows = rows or 1000000 | |
130 local chats = {} | |
131 local docs = Db.search(query,1,rows,{sort=sort}) | |
132 for _, doc in ipairs(docs) do | |
133 chats[#chats+1] = from_doc(doc) | |
134 end | |
135 return chats | |
136 end | |
137 | |
138 function Chat.get_by_id(id) | |
139 local doc = Db.get_document("id:"..id) | |
140 return doc and doc.type=="chat" and from_doc(doc) or nil | |
141 end | |
142 | |
143 return Chat |