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