Mercurial Hosting > lang
comparison src/lib/Chat.luan @ 5:a970b7a01a74
start ai
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 16 Jul 2025 15:08:14 -0600 |
parents | b1adec083e44 |
children | 025bb19b65b1 |
comparison
equal
deleted
inserted
replaced
4:b1adec083e44 | 5:a970b7a01a74 |
---|---|
6 local Time = require "luan:Time.luan" | 6 local Time = require "luan:Time.luan" |
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() | |
12 local Ai = require "site:/lib/ai/Ai.luan" | |
11 | 13 |
12 | 14 |
13 local Chat = {} | 15 local Chat = {} |
14 | 16 |
15 local function from_doc(doc) | 17 local function from_doc(doc) |
17 return Chat.new { | 19 return Chat.new { |
18 id = doc.id | 20 id = doc.id |
19 user_id = doc.chat_user_id | 21 user_id = doc.chat_user_id |
20 updated = doc.chat_updated | 22 updated = doc.chat_updated |
21 name = doc.name | 23 name = doc.name |
22 ai_key = doc.ai_key | 24 ai_name = doc.ai_name |
25 ai_thread = doc.ai_thread | |
23 } | 26 } |
24 end | 27 end |
25 | 28 |
26 local function to_doc(chat) | 29 local function to_doc(chat) |
27 return { | 30 return { |
28 type = "chat" | 31 type = "chat" |
29 id = chat.id | 32 id = chat.id |
30 chat_user_id = long(chat.user_id) | 33 chat_user_id = long(chat.user_id) |
31 chat_updated = long(chat.updated) | 34 chat_updated = long(chat.updated) |
32 name = chat.name or error() | 35 name = chat.name or error() |
33 ai_key = chat.ai_key -- or error() | 36 ai_name = chat.ai_name or error() |
37 ai_thread = chat.ai_thread -- or error() | |
34 } | 38 } |
35 end | 39 end |
36 | 40 |
37 function Chat.new(chat) | 41 function Chat.new(chat) |
38 chat.updated = chat.updated or time_now() | 42 chat.updated = chat.updated or time_now() |
43 chat.ai_name = chat.ai_name or "chatgpt" | |
44 chat.ai = Ai[chat.ai_name]["Chat.luan"] or error() | |
39 | 45 |
40 function chat.save() | 46 function chat.save() |
41 local doc = to_doc(chat) | 47 local doc = to_doc(chat) |
42 Db.save(doc) | 48 Db.save(doc) |
43 chat.id = doc.id | 49 chat.id = doc.id |
50 end | |
51 | |
52 function chat.reload() | |
53 return Chat.get_by_id(chat.id) or error(chat.id) | |
44 end | 54 end |
45 | 55 |
46 function chat.delete() | 56 function chat.delete() |
47 Db.delete("id:"..chat.id) | 57 Db.delete("id:"..chat.id) |
48 end | 58 end |
49 | 59 |
50 function chat.name_html() | 60 function chat.name_html() |
51 return html_encode(chat.name) | 61 return html_encode(chat.name) |
62 end | |
63 | |
64 function chat.output_messages_html() | |
65 chat.ai.output_messages_html(chat.ai_thread) | |
66 end | |
67 | |
68 function chat.ask(input) | |
69 local ai_thread = chat.ai.ask(chat.ai_thread,input) | |
70 if chat.ai_thread ~= ai_thread then | |
71 run_in_transaction( function() | |
72 chat = chat.reload() | |
73 chat.ai_thread = ai_thread | |
74 chat.save() | |
75 end ) | |
76 end | |
77 return `chat.ai.output_messages_html(ai_thread)` | |
52 end | 78 end |
53 | 79 |
54 return chat | 80 return chat |
55 end | 81 end |
56 | 82 |