5
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
6
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local type = Luan.type or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local starts_with = String.starts_with or error()
|
|
7 local Html = require "luan:Html.luan"
|
|
8 local html_encode = Html.encode or error()
|
|
9 local Parsers = require "luan:Parsers.luan"
|
|
10 local json_parse = Parsers.json_parse or error()
|
|
11 local json_string = Parsers.json_string or error()
|
|
12 local Claude = require "site:/lib/ai/claude/Claude.luan"
|
|
13 local claude_chat = Claude.chat or error()
|
|
14 local Logging = require "luan:logging/Logging.luan"
|
19
|
15 local logger = Logging.logger "claude/Ai_chat"
|
5
|
16
|
|
17
|
19
|
18 local Ai_chat = {}
|
5
|
19
|
19
|
20 function Ai_chat.output_system_prompt(thread)
|
9
|
21 if thread == nil then
|
|
22 return
|
|
23 end
|
|
24 thread = json_parse(thread)
|
|
25 local system_prompt = thread.system or error
|
|
26 system_prompt = html_encode(system_prompt)
|
|
27 %><%=system_prompt%><%
|
|
28 end
|
|
29
|
34
|
30 function Ai_chat.output_messages_html(show_text,thread,old_thread)
|
9
|
31 if thread == nil then
|
|
32 return
|
|
33 end
|
|
34 thread = json_parse(thread)
|
|
35 local messages = thread.messages or error
|
13
|
36 local n = 0
|
|
37 if old_thread ~= nil then
|
|
38 old_thread = json_parse(old_thread)
|
|
39 local old_messages = old_thread.messages or error
|
|
40 n = #old_messages
|
|
41 end
|
34
|
42 local checked = show_text and "checked" or ""
|
13
|
43 for i, message in ipairs(messages) do
|
|
44 if i <= n then
|
|
45 continue
|
|
46 end
|
6
|
47 local role = message.role or error()
|
|
48 local who
|
|
49 if role=="assistant" then
|
|
50 who = "Claude"
|
|
51 elseif role=="user" then
|
|
52 who = "You"
|
|
53 else
|
|
54 error(role)
|
|
55 end
|
|
56 local function output(text)
|
9
|
57 text = html_encode(text)
|
6
|
58 %>
|
7
|
59 <h3><%=who%></h3>
|
34
|
60 <div role="<%=role%>">
|
|
61 <div message markdown><%=text%></div>
|
|
62 <% if role=="assistant" then %>
|
|
63 <div controls>
|
|
64 <audio controls preload=none></audio>
|
|
65 <label clickable><input type=checkbox name=show_text <%=checked%> >Show text</label>
|
|
66 </div>
|
|
67 <% end %>
|
|
68 </div>
|
6
|
69 <%
|
|
70 end
|
|
71 local content = message.content or error()
|
|
72 if type(content) == "string" then
|
|
73 output(content)
|
|
74 else
|
|
75 for _, part in ipairs(content) do
|
|
76 if part.type=="text" then
|
|
77 local text = part.text or error()
|
|
78 output(text)
|
|
79 end
|
|
80 end
|
|
81 end
|
|
82 end_for
|
5
|
83 end
|
|
84
|
9
|
85
|
25
|
86 local function ask(thread,input)
|
9
|
87 local messages = thread.messages or error
|
6
|
88 messages[#messages+1] = {
|
|
89 role = "user"
|
|
90 content = input
|
|
91 }
|
|
92 --[=[
|
|
93 messages[#messages+1] = {
|
|
94 role = "assistant"
|
|
95 content = [[
|
13
|
96 hello
|
6
|
97 ]]
|
|
98 }
|
|
99 if true then
|
25
|
100 return
|
6
|
101 end
|
|
102 --]=]
|
20
|
103 -- logger.info(json_string(thread))
|
9
|
104 local resultJson = claude_chat(thread)
|
6
|
105 local result = json_parse(resultJson)
|
|
106 -- logger.info(json_string(result))
|
|
107 result.type == "message" or error()
|
|
108 result.role == "assistant" or error()
|
|
109 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
|
|
110 local content = result.content or error()
|
|
111 messages[#messages+1] = {
|
|
112 role = "assistant"
|
|
113 content = content
|
|
114 }
|
25
|
115 end
|
|
116
|
|
117 function Ai_chat.ask_first(system_prompt,input)
|
|
118 local thread = {
|
|
119 system = system_prompt
|
|
120 messages = {nil}
|
|
121 }
|
|
122 if input ~= nil then
|
|
123 ask(thread,input)
|
|
124 end
|
|
125 return json_string(thread)
|
|
126 end
|
|
127
|
|
128 function Ai_chat.ask_more(thread,input)
|
|
129 thread = json_parse(thread)
|
|
130 ask(thread,input)
|
9
|
131 return json_string(thread)
|
5
|
132 end
|
|
133
|
19
|
134 return Ai_chat
|