Mercurial Hosting > lang
annotate src/lib/ai/claude/Ai_chat.luan @ 71:44bec62c49e2 default tip
split ask
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 24 Aug 2025 08:32:08 -0600 |
parents | f5e72f2d1025 |
children |
rev | line source |
---|---|
5 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
6 | 3 local ipairs = Luan.ipairs or error() |
38 | 4 local pairs = Luan.pairs or error() |
6 | 5 local type = Luan.type or error() |
6 local String = require "luan:String.luan" | |
7 local starts_with = String.starts_with or error() | |
8 local Html = require "luan:Html.luan" | |
9 local html_encode = Html.encode or error() | |
10 local Parsers = require "luan:Parsers.luan" | |
11 local json_parse = Parsers.json_parse or error() | |
12 local json_string = Parsers.json_string or error() | |
71 | 13 local Thread = require "luan:Thread.luan" |
6 | 14 local Claude = require "site:/lib/ai/claude/Claude.luan" |
15 local claude_chat = Claude.chat or error() | |
16 local Logging = require "luan:logging/Logging.luan" | |
19 | 17 local logger = Logging.logger "claude/Ai_chat" |
5 | 18 |
19 | |
19 | 20 local Ai_chat = {} |
5 | 21 |
19 | 22 function Ai_chat.output_system_prompt(thread) |
9 | 23 thread = json_parse(thread) |
24 local system_prompt = thread.system or error | |
25 system_prompt = html_encode(system_prompt) | |
26 %><%=system_prompt%><% | |
27 end | |
28 | |
52 | 29 function Ai_chat.output_messages_html(assistant_controls,thread,old_thread) |
9 | 30 thread = json_parse(thread) |
31 local messages = thread.messages or error | |
13 | 32 local n = 0 |
33 if old_thread ~= nil then | |
34 old_thread = json_parse(old_thread) | |
35 local old_messages = old_thread.messages or error | |
36 n = #old_messages | |
37 end | |
38 for i, message in ipairs(messages) do | |
39 if i <= n then | |
40 continue | |
41 end | |
6 | 42 local role = message.role or error() |
43 local who | |
44 if role=="assistant" then | |
45 who = "Claude" | |
46 elseif role=="user" then | |
47 who = "You" | |
48 else | |
49 error(role) | |
50 end | |
51 local function output(text) | |
9 | 52 text = html_encode(text) |
6 | 53 %> |
7 | 54 <h3><%=who%></h3> |
34 | 55 <div role="<%=role%>"> |
56 <div message markdown><%=text%></div> | |
57 <% if role=="assistant" then %> | |
52 | 58 <%= assistant_controls %> |
34 | 59 <% end %> |
60 </div> | |
6 | 61 <% |
62 end | |
63 local content = message.content or error() | |
64 if type(content) == "string" then | |
65 output(content) | |
66 else | |
67 for _, part in ipairs(content) do | |
68 if part.type=="text" then | |
69 local text = part.text or error() | |
70 output(text) | |
71 end | |
72 end | |
73 end | |
74 end_for | |
5 | 75 end |
76 | |
54 | 77 local function get_chat(chat_id) |
78 local Chat = require "site:/lib/Chat.luan" | |
79 local User = require "site:/lib/User.luan" | |
80 local chat = Chat.get_by_id(chat_id) or error() | |
81 local user = User.current() | |
82 local is_owner = user ~= nil and user.id == chat.user_id | |
83 is_owner or not chat.is_private or error "private" | |
84 return chat | |
85 end | |
86 | |
38 | 87 local functions = { |
54 | 88 get_chat = { |
38 | 89 tool = { |
54 | 90 description = "Get the contents of a chat/thread with Claude on this website. The contents will be JSON in the format of the Claude API." |
38 | 91 input_schema = { |
92 type = "object" | |
93 properties = { | |
54 | 94 chat_id = { |
95 description = "The ID of the chat" | |
96 type = "integer" | |
38 | 97 } |
98 } | |
99 } | |
100 } | |
101 fn = function(input) | |
54 | 102 local chat_id = input.chat_id or error() |
103 local chat = get_chat(chat_id) | |
38 | 104 return chat.ai_thread or error() |
105 end | |
106 } | |
54 | 107 get_tts_instructions = { |
108 tool = { | |
69 | 109 description = "Get the text-to-speech instructions of a chat/thread on this website. These instructions are passed to OpenAI. If there are no instructions, the empty string is returned." |
54 | 110 input_schema = { |
111 type = "object" | |
112 properties = { | |
113 chat_id = { | |
114 description = "The ID of the chat" | |
115 type = "integer" | |
116 } | |
117 } | |
118 } | |
119 } | |
120 fn = function(input) | |
121 local chat_id = input.chat_id or error() | |
122 local chat = get_chat(chat_id) | |
123 return chat.tts_instructions or error() | |
124 end | |
125 } | |
69 | 126 get_stt_prompt = { |
127 tool = { | |
128 description = "Get the speech-to-text prompt of a chat/thread on this website. This prompt is passed to OpenAI. If there is no prompt, the empty string is returned." | |
129 input_schema = { | |
130 type = "object" | |
131 properties = { | |
132 chat_id = { | |
133 description = "The ID of the chat" | |
134 type = "integer" | |
135 } | |
136 } | |
137 } | |
138 } | |
139 fn = function(input) | |
140 local chat_id = input.chat_id or error() | |
141 local chat = get_chat(chat_id) | |
142 return chat.stt_prompt or error() | |
143 end | |
144 } | |
38 | 145 } |
146 local tools = {nil} | |
147 for name, f in pairs(functions) do | |
148 f.name = name | |
149 f.tool.name = name | |
150 tools[#tools+1] = f.tool | |
151 end | |
9 | 152 |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
153 function Ai_chat.init(system_prompt) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
154 local thread = { |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
155 system = system_prompt |
38 | 156 tools = tools |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
157 messages = {nil} |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
158 } |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
159 return json_string(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
160 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
161 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
162 function Ai_chat.has_messages(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
163 thread = json_parse(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
164 return #thread.messages > 0 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
165 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
166 |
71 | 167 local function ask(thread) |
9 | 168 local messages = thread.messages or error |
6 | 169 --[=[ |
170 messages[#messages+1] = { | |
171 role = "assistant" | |
172 content = [[ | |
13 | 173 hello |
6 | 174 ]] |
175 } | |
71 | 176 Thread.sleep(2000) |
6 | 177 if true then |
25 | 178 return |
6 | 179 end |
180 --]=] | |
20 | 181 -- logger.info(json_string(thread)) |
9 | 182 local resultJson = claude_chat(thread) |
6 | 183 local result = json_parse(resultJson) |
184 -- logger.info(json_string(result)) | |
185 result.type == "message" or error() | |
186 result.role == "assistant" or error() | |
187 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
188 local content = result.content or error() | |
189 messages[#messages+1] = { | |
190 role = "assistant" | |
191 content = content | |
192 } | |
38 | 193 local stop_reason = result.stop_reason or error() |
194 if stop_reason == "end_turn" then | |
195 -- ok | |
196 elseif stop_reason == "tool_use" then | |
197 local response = {nil} | |
198 for _, part in ipairs(content) do | |
199 if part.type == "tool_use" then | |
200 local f = functions[part.name] or error() | |
201 local input = part.input or error() | |
202 response[#response+1] = { | |
203 type = "tool_result" | |
204 tool_use_id = part.id or error() | |
205 content = f.fn(input) | |
206 } | |
207 end | |
208 end | |
71 | 209 messages[#messages+1] = { |
210 role = "user" | |
211 content = response | |
212 } | |
213 ask(thread) | |
38 | 214 else |
215 error(stop_reason) | |
216 end | |
217 end | |
218 | |
71 | 219 function Ai_chat.add(thread,input) |
38 | 220 thread = json_parse(thread) |
71 | 221 local messages = thread.messages or error |
222 messages[#messages+1] = { | |
223 role = "user" | |
224 content = input | |
225 } | |
226 return json_string(thread) | |
227 end | |
228 | |
229 function Ai_chat.respond(thread) | |
230 thread = json_parse(thread) | |
231 ask(thread) | |
9 | 232 return json_string(thread) |
5 | 233 end |
234 | |
19 | 235 return Ai_chat |