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
|
29
|
30 function Ai_chat.output_messages_html(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
|
|
42 for i, message in ipairs(messages) do
|
|
43 if i <= n then
|
|
44 continue
|
|
45 end
|
6
|
46 local role = message.role or error()
|
|
47 local who
|
|
48 if role=="assistant" then
|
|
49 who = "Claude"
|
|
50 elseif role=="user" then
|
|
51 who = "You"
|
|
52 else
|
|
53 error(role)
|
|
54 end
|
|
55 local function output(text)
|
9
|
56 text = html_encode(text)
|
6
|
57 %>
|
7
|
58 <h3><%=who%></h3>
|
29
|
59 <div markdown role="<%=role%>"><%=text%></div>
|
6
|
60 <%
|
|
61 end
|
|
62 local content = message.content or error()
|
|
63 if type(content) == "string" then
|
|
64 output(content)
|
|
65 else
|
|
66 for _, part in ipairs(content) do
|
|
67 if part.type=="text" then
|
|
68 local text = part.text or error()
|
|
69 output(text)
|
|
70 end
|
|
71 end
|
|
72 end
|
|
73 end_for
|
5
|
74 end
|
|
75
|
9
|
76
|
25
|
77 local function ask(thread,input)
|
9
|
78 local messages = thread.messages or error
|
6
|
79 messages[#messages+1] = {
|
|
80 role = "user"
|
|
81 content = input
|
|
82 }
|
|
83 --[=[
|
|
84 messages[#messages+1] = {
|
|
85 role = "assistant"
|
|
86 content = [[
|
13
|
87 hello
|
6
|
88 ]]
|
|
89 }
|
|
90 if true then
|
25
|
91 return
|
6
|
92 end
|
|
93 --]=]
|
20
|
94 -- logger.info(json_string(thread))
|
9
|
95 local resultJson = claude_chat(thread)
|
6
|
96 local result = json_parse(resultJson)
|
|
97 -- logger.info(json_string(result))
|
|
98 result.type == "message" or error()
|
|
99 result.role == "assistant" or error()
|
|
100 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
|
|
101 local content = result.content or error()
|
|
102 messages[#messages+1] = {
|
|
103 role = "assistant"
|
|
104 content = content
|
|
105 }
|
25
|
106 end
|
|
107
|
|
108 function Ai_chat.ask_first(system_prompt,input)
|
|
109 local thread = {
|
|
110 system = system_prompt
|
|
111 messages = {nil}
|
|
112 }
|
|
113 if input ~= nil then
|
|
114 ask(thread,input)
|
|
115 end
|
|
116 return json_string(thread)
|
|
117 end
|
|
118
|
|
119 function Ai_chat.ask_more(thread,input)
|
|
120 thread = json_parse(thread)
|
|
121 ask(thread,input)
|
9
|
122 return json_string(thread)
|
5
|
123 end
|
|
124
|
19
|
125 return Ai_chat
|