Mercurial Hosting > lang
annotate src/lib/Chat.luan @ 40:6cdb2c761e08
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 Aug 2025 11:29:02 +0900 |
parents | 2737eeedc1d5 |
children | 2a4c83ce3deb |
rev | line source |
---|---|
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 |
34 | 34 show_text = doc.show_text == "true" |
36 | 35 autoplay = doc.autoplay == "true" |
4 | 36 } |
37 end | |
38 | |
39 local function to_doc(chat) | |
40 return { | |
41 type = "chat" | |
42 id = chat.id | |
43 chat_user_id = long(chat.user_id) | |
44 chat_updated = long(chat.updated) | |
24 | 45 course_id = long(chat.course_id) |
4 | 46 name = chat.name or error() |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
47 ai_thread = chat.ai_thread or error() |
16 | 48 language = chat.language or error() |
49 language_region = chat.language_region or error() | |
31 | 50 voice = chat.voice or error() |
34 | 51 show_text = chat.show_text and "true" or "false" |
36 | 52 autoplay = chat.autoplay and "true" or "false" |
4 | 53 } |
54 end | |
55 | |
16 | 56 local function first_region(language) |
32 | 57 return languages[language].regions[1].code |
16 | 58 end |
59 | |
4 | 60 function Chat.new(chat) |
61 chat.updated = chat.updated or time_now() | |
16 | 62 chat.language_region = chat.language_region or first_region(chat.language) |
31 | 63 chat.voice = chat.voice or voices[1].code |
34 | 64 if chat.show_text==nil then chat.show_text = true end |
36 | 65 if chat.autoplay==nil then chat.autoplay = true end |
4 | 66 |
67 function chat.save() | |
68 local doc = to_doc(chat) | |
69 Db.save(doc) | |
70 chat.id = doc.id | |
71 end | |
72 | |
5 | 73 function chat.reload() |
74 return Chat.get_by_id(chat.id) or error(chat.id) | |
75 end | |
76 | |
4 | 77 function chat.delete() |
78 Db.delete("id:"..chat.id) | |
79 end | |
80 | |
29 | 81 function chat.info() |
82 return { | |
83 id = chat.id | |
84 language_region = chat.language_region | |
31 | 85 voice = chat.voice |
29 | 86 name = chat.name |
34 | 87 show_text = chat.show_text |
36 | 88 autoplay = chat.autoplay |
29 | 89 } |
90 end | |
91 | |
4 | 92 function chat.name_html() |
93 return html_encode(chat.name) | |
94 end | |
95 | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
96 function chat.init_text() -- return text for textarea |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
97 if Ai_chat.has_messages(chat.ai_thread) then |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
98 return "" |
25 | 99 end |
100 local course = get_course_by_id(chat.course_id) or error() | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
101 return course.ai_first_message or error() |
25 | 102 end |
103 | |
104 function chat.output_system_prompt() | |
105 Ai_chat.output_system_prompt(chat.ai_thread) | |
106 end | |
107 | |
108 function chat.output_messages_html() | |
34 | 109 Ai_chat.output_messages_html(chat.show_text,chat.ai_thread) |
25 | 110 end |
111 | |
112 function chat.ask(input) | |
113 local old_thread = chat.ai_thread | |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
114 local ai_thread = Ai_chat.ask(old_thread,input) |
25 | 115 run_in_transaction( function() |
116 chat = chat.reload() | |
117 chat.ai_thread = ai_thread | |
118 chat.save() | |
119 end ) | |
34 | 120 return `Ai_chat.output_messages_html(chat.show_text,ai_thread,old_thread)` |
5 | 121 end |
122 | |
21 | 123 function chat.language_name() |
124 return languages[chat.language].name | |
125 end | |
126 | |
4 | 127 return chat |
128 end | |
129 | |
130 function Chat.search(query,sort,rows) | |
131 rows = rows or 1000000 | |
132 local chats = {} | |
133 local docs = Db.search(query,1,rows,{sort=sort}) | |
134 for _, doc in ipairs(docs) do | |
135 chats[#chats+1] = from_doc(doc) | |
136 end | |
137 return chats | |
138 end | |
139 | |
140 function Chat.get_by_id(id) | |
141 local doc = Db.get_document("id:"..id) | |
142 return doc and doc.type=="chat" and from_doc(doc) or nil | |
143 end | |
144 | |
145 return Chat |