Mercurial Hosting > lang
annotate src/lib/ai/claude/Ai_chat.luan @ 52:27758f3b2d69
add hide_ruby
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 16 Aug 2025 09:56:10 +0900 |
parents | 5ecfdf43f72d |
children | 16e5c14eb800 |
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 | |
38 | 76 local functions = { |
77 get_thread = { | |
78 tool = { | |
79 description = "Get the contents of a thread/chat with Claude on this website. The contents will be JSON in the format of the Claude API." | |
80 input_schema = { | |
81 type = "object" | |
82 properties = { | |
83 thread_id = { | |
84 description = "The ID of the thread" | |
85 type = "string" | |
86 } | |
87 } | |
88 } | |
89 } | |
90 fn = function(input) | |
91 local Chat = require "site:/lib/Chat.luan" | |
41 | 92 local User = require "site:/lib/User.luan" |
38 | 93 local thread_id = input.thread_id or error() |
43 | 94 local chat = Chat.get_by_id(thread_id) or error() |
95 local user = User.current() | |
41 | 96 local is_owner = user ~= nil and user.id == chat.user_id |
97 is_owner or not chat.is_private or error "private" | |
38 | 98 return chat.ai_thread or error() |
99 end | |
100 } | |
101 } | |
102 local tools = {nil} | |
103 for name, f in pairs(functions) do | |
104 f.name = name | |
105 f.tool.name = name | |
106 tools[#tools+1] = f.tool | |
107 end | |
9 | 108 |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
109 function Ai_chat.init(system_prompt) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
110 local thread = { |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
111 system = system_prompt |
38 | 112 tools = tools |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
113 messages = {nil} |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
114 } |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
115 return json_string(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
116 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
117 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
118 function Ai_chat.has_messages(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
119 thread = json_parse(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
120 return #thread.messages > 0 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
121 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
122 |
38 | 123 local function ask(thread,content) |
9 | 124 local messages = thread.messages or error |
6 | 125 messages[#messages+1] = { |
126 role = "user" | |
38 | 127 content = content |
6 | 128 } |
129 --[=[ | |
130 messages[#messages+1] = { | |
131 role = "assistant" | |
132 content = [[ | |
13 | 133 hello |
6 | 134 ]] |
135 } | |
136 if true then | |
25 | 137 return |
6 | 138 end |
139 --]=] | |
20 | 140 -- logger.info(json_string(thread)) |
9 | 141 local resultJson = claude_chat(thread) |
6 | 142 local result = json_parse(resultJson) |
143 -- logger.info(json_string(result)) | |
144 result.type == "message" or error() | |
145 result.role == "assistant" or error() | |
146 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
147 local content = result.content or error() | |
148 messages[#messages+1] = { | |
149 role = "assistant" | |
150 content = content | |
151 } | |
38 | 152 local stop_reason = result.stop_reason or error() |
153 if stop_reason == "end_turn" then | |
154 -- ok | |
155 elseif stop_reason == "tool_use" then | |
156 local response = {nil} | |
157 for _, part in ipairs(content) do | |
158 if part.type == "tool_use" then | |
159 local f = functions[part.name] or error() | |
160 local input = part.input or error() | |
161 response[#response+1] = { | |
162 type = "tool_result" | |
163 tool_use_id = part.id or error() | |
164 content = f.fn(input) | |
165 } | |
166 end | |
167 end | |
168 ask(thread,response) | |
169 else | |
170 error(stop_reason) | |
171 end | |
172 end | |
173 | |
174 function Ai_chat.ask(thread,input) | |
175 thread = json_parse(thread) | |
176 ask(thread,input) | |
9 | 177 return json_string(thread) |
5 | 178 end |
179 | |
19 | 180 return Ai_chat |