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()
|
|
12 local Ai = require "site:/lib/ai/Ai.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()
|
4
|
16
|
|
17
|
|
18 local Chat = {}
|
|
19
|
|
20 local function from_doc(doc)
|
|
21 doc.type == "chat" or error "wrong type"
|
|
22 return Chat.new {
|
|
23 id = doc.id
|
|
24 user_id = doc.chat_user_id
|
|
25 updated = doc.chat_updated
|
24
|
26 course_id = doc.course_id
|
4
|
27 name = doc.name
|
5
|
28 ai_name = doc.ai_name
|
|
29 ai_thread = doc.ai_thread
|
16
|
30 language = doc.language
|
|
31 language_region = doc.language_region
|
4
|
32 }
|
|
33 end
|
|
34
|
|
35 local function to_doc(chat)
|
|
36 return {
|
|
37 type = "chat"
|
|
38 id = chat.id
|
|
39 chat_user_id = long(chat.user_id)
|
|
40 chat_updated = long(chat.updated)
|
24
|
41 course_id = long(chat.course_id)
|
4
|
42 name = chat.name or error()
|
5
|
43 ai_name = chat.ai_name or error()
|
|
44 ai_thread = chat.ai_thread -- or error()
|
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()
|
6
|
56 chat.ai_name = chat.ai_name or "claude"
|
19
|
57 chat.ai = Ai[chat.ai_name]["Ai_chat.luan"] or error()
|
16
|
58 chat.language_region = chat.language_region or first_region(chat.language)
|
4
|
59
|
|
60 function chat.save()
|
|
61 local doc = to_doc(chat)
|
|
62 Db.save(doc)
|
|
63 chat.id = doc.id
|
|
64 end
|
|
65
|
5
|
66 function chat.reload()
|
|
67 return Chat.get_by_id(chat.id) or error(chat.id)
|
|
68 end
|
|
69
|
4
|
70 function chat.delete()
|
|
71 Db.delete("id:"..chat.id)
|
|
72 end
|
|
73
|
|
74 function chat.name_html()
|
|
75 return html_encode(chat.name)
|
|
76 end
|
|
77
|
9
|
78 function chat.output_system_prompt()
|
|
79 chat.ai.output_system_prompt(chat.ai_thread)
|
|
80 end
|
|
81
|
5
|
82 function chat.output_messages_html()
|
19
|
83 chat.ai.output_messages_html(chat.language_region,chat.ai_thread)
|
5
|
84 end
|
|
85
|
|
86 function chat.ask(input)
|
13
|
87 local old_thread = chat.ai_thread
|
|
88 local ai_thread = chat.ai.ask(old_thread,input)
|
|
89 run_in_transaction( function()
|
|
90 chat = chat.reload()
|
|
91 chat.ai_thread = ai_thread
|
|
92 chat.save()
|
|
93 end )
|
16
|
94 return `chat.ai.output_messages_html(chat.language_region,ai_thread,old_thread)`
|
5
|
95 end
|
|
96
|
21
|
97 function chat.language_name()
|
|
98 return languages[chat.language].name
|
|
99 end
|
|
100
|
4
|
101 return chat
|
|
102 end
|
|
103
|
|
104 function Chat.search(query,sort,rows)
|
|
105 rows = rows or 1000000
|
|
106 local chats = {}
|
|
107 local docs = Db.search(query,1,rows,{sort=sort})
|
|
108 for _, doc in ipairs(docs) do
|
|
109 chats[#chats+1] = from_doc(doc)
|
|
110 end
|
|
111 return chats
|
|
112 end
|
|
113
|
|
114 function Chat.get_by_id(id)
|
|
115 local doc = Db.get_document("id:"..id)
|
|
116 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
117 end
|
|
118
|
|
119 return Chat
|