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"
|
16
|
13 local languages = require "site:/lib/languages.luan"
|
25
|
14 local Course = require "site:/lib/Course.luan"
|
|
15 local get_course_by_id = Course.get_by_id or error()
|
31
|
16 local Shared = require "site:/lib/Shared.luan"
|
|
17 local voices = Shared.voices 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
|
|
32 language_region = doc.language_region
|
31
|
33 voice = doc.voice
|
34
|
34 show_text = doc.show_text == "true"
|
4
|
35 }
|
|
36 end
|
|
37
|
|
38 local function to_doc(chat)
|
|
39 return {
|
|
40 type = "chat"
|
|
41 id = chat.id
|
|
42 chat_user_id = long(chat.user_id)
|
|
43 chat_updated = long(chat.updated)
|
24
|
44 course_id = long(chat.course_id)
|
4
|
45 name = chat.name or error()
|
25
|
46 ai_thread = chat.ai_thread
|
16
|
47 language = chat.language or error()
|
|
48 language_region = chat.language_region or error()
|
31
|
49 voice = chat.voice or error()
|
34
|
50 show_text = chat.show_text and "true" or "false"
|
4
|
51 }
|
|
52 end
|
|
53
|
16
|
54 local function first_region(language)
|
32
|
55 return languages[language].regions[1].code
|
16
|
56 end
|
|
57
|
4
|
58 function Chat.new(chat)
|
|
59 chat.updated = chat.updated or time_now()
|
16
|
60 chat.language_region = chat.language_region or first_region(chat.language)
|
31
|
61 chat.voice = chat.voice or voices[1].code
|
34
|
62 if chat.show_text==nil then chat.show_text = 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
|
|
81 language_region = chat.language_region
|
31
|
82 voice = chat.voice
|
29
|
83 name = chat.name
|
34
|
84 show_text = chat.show_text
|
29
|
85 }
|
|
86 end
|
|
87
|
4
|
88 function chat.name_html()
|
|
89 return html_encode(chat.name)
|
|
90 end
|
|
91
|
25
|
92 function chat.init_ai() -- return if added message
|
|
93 if chat.ai_thread ~= nil then
|
|
94 return false
|
|
95 end
|
|
96 local course = get_course_by_id(chat.course_id) or error()
|
|
97 local ai_first_message = course.ai_first_message
|
|
98 local ai_thread = Ai_chat.ask_first(course.ai_system_prompt,ai_first_message)
|
13
|
99 run_in_transaction( function()
|
|
100 chat = chat.reload()
|
|
101 chat.ai_thread = ai_thread
|
|
102 chat.save()
|
|
103 end )
|
25
|
104 return ai_first_message ~= nil
|
|
105 end
|
|
106
|
|
107 function chat.output_system_prompt()
|
|
108 Ai_chat.output_system_prompt(chat.ai_thread)
|
|
109 end
|
|
110
|
|
111 function chat.output_messages_html()
|
34
|
112 Ai_chat.output_messages_html(chat.show_text,chat.ai_thread)
|
25
|
113 end
|
|
114
|
|
115 function chat.ask(input)
|
|
116 local old_thread = chat.ai_thread
|
|
117 local ai_thread = Ai_chat.ask_more(old_thread,input)
|
|
118 run_in_transaction( function()
|
|
119 chat = chat.reload()
|
|
120 chat.ai_thread = ai_thread
|
|
121 chat.save()
|
|
122 end )
|
34
|
123 return `Ai_chat.output_messages_html(chat.show_text,ai_thread,old_thread)`
|
5
|
124 end
|
|
125
|
21
|
126 function chat.language_name()
|
|
127 return languages[chat.language].name
|
|
128 end
|
|
129
|
4
|
130 return chat
|
|
131 end
|
|
132
|
|
133 function Chat.search(query,sort,rows)
|
|
134 rows = rows or 1000000
|
|
135 local chats = {}
|
|
136 local docs = Db.search(query,1,rows,{sort=sort})
|
|
137 for _, doc in ipairs(docs) do
|
|
138 chats[#chats+1] = from_doc(doc)
|
|
139 end
|
|
140 return chats
|
|
141 end
|
|
142
|
|
143 function Chat.get_by_id(id)
|
|
144 local doc = Db.get_document("id:"..id)
|
|
145 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
146 end
|
|
147
|
|
148 return Chat
|