Mercurial Hosting > lang
comparison src/lib/Chat.luan @ 16:f5425a3c1898
add languages
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 30 Jul 2025 10:38:27 -0600 |
parents | 65bd7e245c63 |
children | 0351b3d474f8 |
comparison
equal
deleted
inserted
replaced
15:49e9138b5460 | 16:f5425a3c1898 |
---|---|
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 = require "site:/lib/ai/Ai.luan" |
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() | |
13 | 16 |
14 | 17 |
15 local Chat = {} | 18 local Chat = {} |
16 | 19 |
17 local function from_doc(doc) | 20 local function from_doc(doc) |
21 user_id = doc.chat_user_id | 24 user_id = doc.chat_user_id |
22 updated = doc.chat_updated | 25 updated = doc.chat_updated |
23 name = doc.name | 26 name = doc.name |
24 ai_name = doc.ai_name | 27 ai_name = doc.ai_name |
25 ai_thread = doc.ai_thread | 28 ai_thread = doc.ai_thread |
29 language = doc.language | |
30 language_region = doc.language_region | |
26 } | 31 } |
27 end | 32 end |
28 | 33 |
29 local function to_doc(chat) | 34 local function to_doc(chat) |
30 return { | 35 return { |
33 chat_user_id = long(chat.user_id) | 38 chat_user_id = long(chat.user_id) |
34 chat_updated = long(chat.updated) | 39 chat_updated = long(chat.updated) |
35 name = chat.name or error() | 40 name = chat.name or error() |
36 ai_name = chat.ai_name or error() | 41 ai_name = chat.ai_name or error() |
37 ai_thread = chat.ai_thread -- or error() | 42 ai_thread = chat.ai_thread -- or error() |
43 language = chat.language or error() | |
44 language_region = chat.language_region or error() | |
38 } | 45 } |
46 end | |
47 | |
48 local function first_region(language) | |
49 return get_first(languages[language].regions) | |
39 end | 50 end |
40 | 51 |
41 function Chat.new(chat) | 52 function Chat.new(chat) |
42 chat.updated = chat.updated or time_now() | 53 chat.updated = chat.updated or time_now() |
43 chat.ai_name = chat.ai_name or "claude" | 54 chat.ai_name = chat.ai_name or "claude" |
44 chat.ai = Ai[chat.ai_name]["Chat.luan"] or error() | 55 chat.ai = Ai[chat.ai_name]["Chat.luan"] or error() |
56 chat.language_region = chat.language_region or first_region(chat.language) | |
45 | 57 |
46 function chat.save() | 58 function chat.save() |
47 local doc = to_doc(chat) | 59 local doc = to_doc(chat) |
48 Db.save(doc) | 60 Db.save(doc) |
49 chat.id = doc.id | 61 chat.id = doc.id |
75 run_in_transaction( function() | 87 run_in_transaction( function() |
76 chat = chat.reload() | 88 chat = chat.reload() |
77 chat.ai_thread = ai_thread | 89 chat.ai_thread = ai_thread |
78 chat.save() | 90 chat.save() |
79 end ) | 91 end ) |
80 return `chat.ai.output_messages_html(ai_thread,old_thread)` | 92 return `chat.ai.output_messages_html(chat.language_region,ai_thread,old_thread)` |
81 end | 93 end |
82 | 94 |
83 return chat | 95 return chat |
84 end | 96 end |
85 | 97 |