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