comparison src/lib/Chat.luan @ 46:cc20eebaa74a

use openai tts
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Aug 2025 11:27:34 +0900
parents 2a4c83ce3deb
children 27758f3b2d69
comparison
equal deleted inserted replaced
45:fef7a5c65cfb 46:cc20eebaa74a
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_chat = require "site:/lib/ai/claude/Ai_chat.luan" 12 local Ai_chat = require "site:/lib/ai/claude/Ai_chat.luan"
13 local languages = require "site:/lib/languages.luan"
14 local Course = require "site:/lib/Course.luan" 13 local Course = require "site:/lib/Course.luan"
15 local get_course_by_id = Course.get_by_id or error() 14 local get_course_by_id = Course.get_by_id or error()
16 local Shared = require "site:/lib/Shared.luan" 15 local Shared = require "site:/lib/Shared.luan"
17 local voices = Shared.voices or error() 16 local voices = Shared.voices or error()
17 local languages = Shared.languages or error()
18 18
19 19
20 local Chat = {} 20 local Chat = {}
21 21
22 local function from_doc(doc) 22 local function from_doc(doc)
27 updated = doc.chat_updated 27 updated = doc.chat_updated
28 course_id = doc.course_id 28 course_id = doc.course_id
29 name = doc.name 29 name = doc.name
30 ai_thread = doc.ai_thread 30 ai_thread = doc.ai_thread
31 language = doc.language 31 language = doc.language
32 language_region = doc.language_region 32 tts_instructions = doc.tts_instructions
33 voice = doc.voice 33 voice = doc.voice
34 show_text = doc.show_text == "true" 34 show_text = doc.show_text == "true"
35 autoplay = doc.autoplay == "true" 35 autoplay = doc.autoplay == "true"
36 is_private = doc.is_private == "true" 36 is_private = doc.is_private == "true"
37 } 37 }
45 chat_updated = long(chat.updated) 45 chat_updated = long(chat.updated)
46 course_id = long(chat.course_id) 46 course_id = long(chat.course_id)
47 name = chat.name or error() 47 name = chat.name or error()
48 ai_thread = chat.ai_thread or error() 48 ai_thread = chat.ai_thread or error()
49 language = chat.language or error() 49 language = chat.language or error()
50 language_region = chat.language_region or error() 50 tts_instructions = chat.tts_instructions -- or error()
51 voice = chat.voice or error() 51 voice = chat.voice or error()
52 show_text = chat.show_text and "true" or "false" 52 show_text = chat.show_text and "true" or "false"
53 autoplay = chat.autoplay and "true" or "false" 53 autoplay = chat.autoplay and "true" or "false"
54 is_private = chat.is_private and "true" or nil 54 is_private = chat.is_private and "true" or nil
55 } 55 }
56 end 56 end
57 57
58 local function first_region(language)
59 return languages[language].regions[1].code
60 end
61
62 function Chat.new(chat) 58 function Chat.new(chat)
63 chat.updated = chat.updated or time_now() 59 chat.updated = chat.updated or time_now()
64 chat.language_region = chat.language_region or first_region(chat.language) 60 chat.voice = chat.voice or voices[1]
65 chat.voice = chat.voice or voices[1].code
66 if chat.show_text==nil then chat.show_text = true end 61 if chat.show_text==nil then chat.show_text = true end
67 if chat.autoplay==nil then chat.autoplay = true end 62 if chat.autoplay==nil then chat.autoplay = true end
68 63
69 function chat.save() 64 function chat.save()
70 local doc = to_doc(chat) 65 local doc = to_doc(chat)
81 end 76 end
82 77
83 function chat.info() 78 function chat.info()
84 return { 79 return {
85 id = chat.id 80 id = chat.id
86 language_region = chat.language_region
87 voice = chat.voice 81 voice = chat.voice
82 tts_instructions = chat.tts_instructions
88 name = chat.name 83 name = chat.name
89 show_text = chat.show_text 84 show_text = chat.show_text
90 autoplay = chat.autoplay 85 autoplay = chat.autoplay
91 is_private = chat.is_private 86 is_private = chat.is_private
92 } 87 }
122 end ) 117 end )
123 return `Ai_chat.output_messages_html(chat.show_text,ai_thread,old_thread)` 118 return `Ai_chat.output_messages_html(chat.show_text,ai_thread,old_thread)`
124 end 119 end
125 120
126 function chat.language_name() 121 function chat.language_name()
127 return languages[chat.language].name 122 return languages[chat.language]
128 end 123 end
129 124
130 return chat 125 return chat
131 end 126 end
132 127