Mercurial Hosting > lang
comparison src/lib/Chat.luan @ 25:3a80ddafe5a4
courses work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 Aug 2025 00:33:51 -0600 |
parents | 87fe70201aa8 |
children | 505185272dd7 |
comparison
equal
deleted
inserted
replaced
24:87fe70201aa8 | 25:3a80ddafe5a4 |
---|---|
7 local time_now = Time.now or error() | 7 local time_now = Time.now or error() |
8 local Html = require "luan:Html.luan" | 8 local Html = require "luan:Html.luan" |
9 local html_encode = Html.encode or error() | 9 local html_encode = Html.encode or error() |
10 local Db = require "site:/lib/Db.luan" | 10 local Db = require "site:/lib/Db.luan" |
11 local run_in_transaction = Db.run_in_transaction or error() | 11 local run_in_transaction = Db.run_in_transaction or error() |
12 local Ai = require "site:/lib/ai/Ai.luan" | 12 local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan" |
13 local languages = require "site:/lib/languages.luan" | 13 local languages = require "site:/lib/languages.luan" |
14 local Utils = require "site:/lib/Utils.luan" | 14 local Utils = require "site:/lib/Utils.luan" |
15 local get_first = Utils.get_first or error() | 15 local get_first = Utils.get_first or error() |
16 local Course = require "site:/lib/Course.luan" | |
17 local get_course_by_id = Course.get_by_id or error() | |
16 | 18 |
17 | 19 |
18 local Chat = {} | 20 local Chat = {} |
19 | 21 |
20 local function from_doc(doc) | 22 local function from_doc(doc) |
23 id = doc.id | 25 id = doc.id |
24 user_id = doc.chat_user_id | 26 user_id = doc.chat_user_id |
25 updated = doc.chat_updated | 27 updated = doc.chat_updated |
26 course_id = doc.course_id | 28 course_id = doc.course_id |
27 name = doc.name | 29 name = doc.name |
28 ai_name = doc.ai_name | |
29 ai_thread = doc.ai_thread | 30 ai_thread = doc.ai_thread |
30 language = doc.language | 31 language = doc.language |
31 language_region = doc.language_region | 32 language_region = doc.language_region |
32 } | 33 } |
33 end | 34 end |
38 id = chat.id | 39 id = chat.id |
39 chat_user_id = long(chat.user_id) | 40 chat_user_id = long(chat.user_id) |
40 chat_updated = long(chat.updated) | 41 chat_updated = long(chat.updated) |
41 course_id = long(chat.course_id) | 42 course_id = long(chat.course_id) |
42 name = chat.name or error() | 43 name = chat.name or error() |
43 ai_name = chat.ai_name or error() | 44 ai_thread = chat.ai_thread |
44 ai_thread = chat.ai_thread -- or error() | |
45 language = chat.language or error() | 45 language = chat.language or error() |
46 language_region = chat.language_region or error() | 46 language_region = chat.language_region or error() |
47 } | 47 } |
48 end | 48 end |
49 | 49 |
51 return get_first(languages[language].regions) | 51 return get_first(languages[language].regions) |
52 end | 52 end |
53 | 53 |
54 function Chat.new(chat) | 54 function Chat.new(chat) |
55 chat.updated = chat.updated or time_now() | 55 chat.updated = chat.updated or time_now() |
56 chat.ai_name = chat.ai_name or "claude" | |
57 chat.ai = Ai[chat.ai_name]["Ai_chat.luan"] or error() | |
58 chat.language_region = chat.language_region or first_region(chat.language) | 56 chat.language_region = chat.language_region or first_region(chat.language) |
59 | 57 |
60 function chat.save() | 58 function chat.save() |
61 local doc = to_doc(chat) | 59 local doc = to_doc(chat) |
62 Db.save(doc) | 60 Db.save(doc) |
73 | 71 |
74 function chat.name_html() | 72 function chat.name_html() |
75 return html_encode(chat.name) | 73 return html_encode(chat.name) |
76 end | 74 end |
77 | 75 |
78 function chat.output_system_prompt() | 76 function chat.init_ai() -- return if added message |
79 chat.ai.output_system_prompt(chat.ai_thread) | 77 if chat.ai_thread ~= nil then |
80 end | 78 return false |
81 | 79 end |
82 function chat.output_messages_html() | 80 local course = get_course_by_id(chat.course_id) or error() |
83 chat.ai.output_messages_html(chat.language_region,chat.ai_thread) | 81 local ai_first_message = course.ai_first_message |
84 end | 82 local ai_thread = Ai_chat.ask_first(course.ai_system_prompt,ai_first_message) |
85 | |
86 function chat.ask(input) | |
87 local old_thread = chat.ai_thread | |
88 local ai_thread = chat.ai.ask(old_thread,input) | |
89 run_in_transaction( function() | 83 run_in_transaction( function() |
90 chat = chat.reload() | 84 chat = chat.reload() |
91 chat.ai_thread = ai_thread | 85 chat.ai_thread = ai_thread |
92 chat.save() | 86 chat.save() |
93 end ) | 87 end ) |
94 return `chat.ai.output_messages_html(chat.language_region,ai_thread,old_thread)` | 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)` | |
95 end | 108 end |
96 | 109 |
97 function chat.language_name() | 110 function chat.language_name() |
98 return languages[chat.language].name | 111 return languages[chat.language].name |
99 end | 112 end |