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
|
|
72 function chat.name_html()
|
|
73 return html_encode(chat.name)
|
|
74 end
|
|
75
|
25
|
76 function chat.init_ai() -- return if added message
|
|
77 if chat.ai_thread ~= nil then
|
|
78 return false
|
|
79 end
|
|
80 local course = get_course_by_id(chat.course_id) or error()
|
|
81 local ai_first_message = course.ai_first_message
|
|
82 local ai_thread = Ai_chat.ask_first(course.ai_system_prompt,ai_first_message)
|
13
|
83 run_in_transaction( function()
|
|
84 chat = chat.reload()
|
|
85 chat.ai_thread = ai_thread
|
|
86 chat.save()
|
|
87 end )
|
25
|
88 return ai_first_message ~= nil
|
|
89 end
|
|
90
|
|
91 function chat.output_system_prompt()
|
|
92 Ai_chat.output_system_prompt(chat.ai_thread)
|
|
93 end
|
|
94
|
|
95 function chat.output_messages_html()
|
|
96 Ai_chat.output_messages_html(chat.language_region,chat.ai_thread)
|
|
97 end
|
|
98
|
|
99 function chat.ask(input)
|
|
100 local old_thread = chat.ai_thread
|
|
101 local ai_thread = Ai_chat.ask_more(old_thread,input)
|
|
102 run_in_transaction( function()
|
|
103 chat = chat.reload()
|
|
104 chat.ai_thread = ai_thread
|
|
105 chat.save()
|
|
106 end )
|
|
107 return `Ai_chat.output_messages_html(chat.language_region,ai_thread,old_thread)`
|
5
|
108 end
|
|
109
|
21
|
110 function chat.language_name()
|
|
111 return languages[chat.language].name
|
|
112 end
|
|
113
|
4
|
114 return chat
|
|
115 end
|
|
116
|
|
117 function Chat.search(query,sort,rows)
|
|
118 rows = rows or 1000000
|
|
119 local chats = {}
|
|
120 local docs = Db.search(query,1,rows,{sort=sort})
|
|
121 for _, doc in ipairs(docs) do
|
|
122 chats[#chats+1] = from_doc(doc)
|
|
123 end
|
|
124 return chats
|
|
125 end
|
|
126
|
|
127 function Chat.get_by_id(id)
|
|
128 local doc = Db.get_document("id:"..id)
|
|
129 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
130 end
|
|
131
|
|
132 return Chat
|