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
|
|
26 name = doc.name
|
5
|
27 ai_name = doc.ai_name
|
|
28 ai_thread = doc.ai_thread
|
16
|
29 language = doc.language
|
|
30 language_region = doc.language_region
|
4
|
31 }
|
|
32 end
|
|
33
|
|
34 local function to_doc(chat)
|
|
35 return {
|
|
36 type = "chat"
|
|
37 id = chat.id
|
|
38 chat_user_id = long(chat.user_id)
|
|
39 chat_updated = long(chat.updated)
|
|
40 name = chat.name or error()
|
5
|
41 ai_name = chat.ai_name or error()
|
|
42 ai_thread = chat.ai_thread -- or error()
|
16
|
43 language = chat.language or error()
|
|
44 language_region = chat.language_region or error()
|
4
|
45 }
|
|
46 end
|
|
47
|
16
|
48 local function first_region(language)
|
|
49 return get_first(languages[language].regions)
|
|
50 end
|
|
51
|
4
|
52 function Chat.new(chat)
|
|
53 chat.updated = chat.updated or time_now()
|
6
|
54 chat.ai_name = chat.ai_name or "claude"
|
5
|
55 chat.ai = Ai[chat.ai_name]["Chat.luan"] or error()
|
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
|
9
|
76 function chat.output_system_prompt()
|
|
77 chat.ai.output_system_prompt(chat.ai_thread)
|
|
78 end
|
|
79
|
5
|
80 function chat.output_messages_html()
|
|
81 chat.ai.output_messages_html(chat.ai_thread)
|
|
82 end
|
|
83
|
|
84 function chat.ask(input)
|
13
|
85 local old_thread = chat.ai_thread
|
|
86 local ai_thread = chat.ai.ask(old_thread,input)
|
|
87 run_in_transaction( function()
|
|
88 chat = chat.reload()
|
|
89 chat.ai_thread = ai_thread
|
|
90 chat.save()
|
|
91 end )
|
16
|
92 return `chat.ai.output_messages_html(chat.language_region,ai_thread,old_thread)`
|
5
|
93 end
|
|
94
|
4
|
95 return chat
|
|
96 end
|
|
97
|
|
98 function Chat.search(query,sort,rows)
|
|
99 rows = rows or 1000000
|
|
100 local chats = {}
|
|
101 local docs = Db.search(query,1,rows,{sort=sort})
|
|
102 for _, doc in ipairs(docs) do
|
|
103 chats[#chats+1] = from_doc(doc)
|
|
104 end
|
|
105 return chats
|
|
106 end
|
|
107
|
|
108 function Chat.get_by_id(id)
|
|
109 local doc = Db.get_document("id:"..id)
|
|
110 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
111 end
|
|
112
|
|
113 return Chat
|