Mercurial Hosting > lang
annotate src/lib/ai/claude/Ai_chat.luan @ 54:16e5c14eb800
AI tools
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 17 Aug 2025 17:47:02 +0900 |
parents | 27758f3b2d69 |
children | f5e72f2d1025 |
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() | |
13 local Claude = require "site:/lib/ai/claude/Claude.luan" | |
14 local claude_chat = Claude.chat or error() | |
15 local Logging = require "luan:logging/Logging.luan" | |
19 | 16 local logger = Logging.logger "claude/Ai_chat" |
5 | 17 |
18 | |
19 | 19 local Ai_chat = {} |
5 | 20 |
19 | 21 function Ai_chat.output_system_prompt(thread) |
9 | 22 thread = json_parse(thread) |
23 local system_prompt = thread.system or error | |
24 system_prompt = html_encode(system_prompt) | |
25 %><%=system_prompt%><% | |
26 end | |
27 | |
52 | 28 function Ai_chat.output_messages_html(assistant_controls,thread,old_thread) |
9 | 29 thread = json_parse(thread) |
30 local messages = thread.messages or error | |
13 | 31 local n = 0 |
32 if old_thread ~= nil then | |
33 old_thread = json_parse(old_thread) | |
34 local old_messages = old_thread.messages or error | |
35 n = #old_messages | |
36 end | |
37 for i, message in ipairs(messages) do | |
38 if i <= n then | |
39 continue | |
40 end | |
6 | 41 local role = message.role or error() |
42 local who | |
43 if role=="assistant" then | |
44 who = "Claude" | |
45 elseif role=="user" then | |
46 who = "You" | |
47 else | |
48 error(role) | |
49 end | |
50 local function output(text) | |
9 | 51 text = html_encode(text) |
6 | 52 %> |
7 | 53 <h3><%=who%></h3> |
34 | 54 <div role="<%=role%>"> |
55 <div message markdown><%=text%></div> | |
56 <% if role=="assistant" then %> | |
52 | 57 <%= assistant_controls %> |
34 | 58 <% end %> |
59 </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 | |
54 | 76 local function get_chat(chat_id) |
77 local Chat = require "site:/lib/Chat.luan" | |
78 local User = require "site:/lib/User.luan" | |
79 local chat = Chat.get_by_id(chat_id) or error() | |
80 local user = User.current() | |
81 local is_owner = user ~= nil and user.id == chat.user_id | |
82 is_owner or not chat.is_private or error "private" | |
83 return chat | |
84 end | |
85 | |
38 | 86 local functions = { |
54 | 87 get_chat = { |
38 | 88 tool = { |
54 | 89 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 | 90 input_schema = { |
91 type = "object" | |
92 properties = { | |
54 | 93 chat_id = { |
94 description = "The ID of the chat" | |
95 type = "integer" | |
38 | 96 } |
97 } | |
98 } | |
99 } | |
100 fn = function(input) | |
54 | 101 local chat_id = input.chat_id or error() |
102 local chat = get_chat(chat_id) | |
38 | 103 return chat.ai_thread or error() |
104 end | |
105 } | |
54 | 106 get_tts_instructions = { |
107 tool = { | |
108 description = "Get the text-to-speech instructions of a chat/thread on this website. These instruction are passed to OpenAI. If there are no instructions, the empty string is returned." | |
109 input_schema = { | |
110 type = "object" | |
111 properties = { | |
112 chat_id = { | |
113 description = "The ID of the chat" | |
114 type = "integer" | |
115 } | |
116 } | |
117 } | |
118 } | |
119 fn = function(input) | |
120 local chat_id = input.chat_id or error() | |
121 local chat = get_chat(chat_id) | |
122 return chat.tts_instructions or error() | |
123 end | |
124 } | |
38 | 125 } |
126 local tools = {nil} | |
127 for name, f in pairs(functions) do | |
128 f.name = name | |
129 f.tool.name = name | |
130 tools[#tools+1] = f.tool | |
131 end | |
9 | 132 |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
133 function Ai_chat.init(system_prompt) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
134 local thread = { |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
135 system = system_prompt |
38 | 136 tools = tools |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
137 messages = {nil} |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
138 } |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
139 return json_string(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
140 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
141 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
142 function Ai_chat.has_messages(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
143 thread = json_parse(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
144 return #thread.messages > 0 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
145 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
146 |
38 | 147 local function ask(thread,content) |
9 | 148 local messages = thread.messages or error |
6 | 149 messages[#messages+1] = { |
150 role = "user" | |
38 | 151 content = content |
6 | 152 } |
153 --[=[ | |
154 messages[#messages+1] = { | |
155 role = "assistant" | |
156 content = [[ | |
13 | 157 hello |
6 | 158 ]] |
159 } | |
160 if true then | |
25 | 161 return |
6 | 162 end |
163 --]=] | |
20 | 164 -- logger.info(json_string(thread)) |
9 | 165 local resultJson = claude_chat(thread) |
6 | 166 local result = json_parse(resultJson) |
167 -- logger.info(json_string(result)) | |
168 result.type == "message" or error() | |
169 result.role == "assistant" or error() | |
170 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
171 local content = result.content or error() | |
172 messages[#messages+1] = { | |
173 role = "assistant" | |
174 content = content | |
175 } | |
38 | 176 local stop_reason = result.stop_reason or error() |
177 if stop_reason == "end_turn" then | |
178 -- ok | |
179 elseif stop_reason == "tool_use" then | |
180 local response = {nil} | |
181 for _, part in ipairs(content) do | |
182 if part.type == "tool_use" then | |
183 local f = functions[part.name] or error() | |
184 local input = part.input or error() | |
185 response[#response+1] = { | |
186 type = "tool_result" | |
187 tool_use_id = part.id or error() | |
188 content = f.fn(input) | |
189 } | |
190 end | |
191 end | |
192 ask(thread,response) | |
193 else | |
194 error(stop_reason) | |
195 end | |
196 end | |
197 | |
198 function Ai_chat.ask(thread,input) | |
199 thread = json_parse(thread) | |
200 ask(thread,input) | |
9 | 201 return json_string(thread) |
5 | 202 end |
203 | |
19 | 204 return Ai_chat |