Mercurial Hosting > lang
annotate src/lib/ai/claude/Ai_chat.luan @ 51:4581a20b8124
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 15 Aug 2025 23:24:38 +0900 |
| parents | 5ecfdf43f72d |
| children | 27758f3b2d69 |
| 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 | |
| 34 | 28 function Ai_chat.output_messages_html(show_text,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 | |
| 34 | 37 local checked = show_text and "checked" or "" |
| 13 | 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 %> | |
| 58 <div controls> | |
| 59 <audio controls preload=none></audio> | |
| 60 <label clickable><input type=checkbox name=show_text <%=checked%> >Show text</label> | |
| 61 </div> | |
| 62 <% end %> | |
| 63 </div> | |
| 6 | 64 <% |
| 65 end | |
| 66 local content = message.content or error() | |
| 67 if type(content) == "string" then | |
| 68 output(content) | |
| 69 else | |
| 70 for _, part in ipairs(content) do | |
| 71 if part.type=="text" then | |
| 72 local text = part.text or error() | |
| 73 output(text) | |
| 74 end | |
| 75 end | |
| 76 end | |
| 77 end_for | |
| 5 | 78 end |
| 79 | |
| 38 | 80 local functions = { |
| 81 get_thread = { | |
| 82 tool = { | |
| 83 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." | |
| 84 input_schema = { | |
| 85 type = "object" | |
| 86 properties = { | |
| 87 thread_id = { | |
| 88 description = "The ID of the thread" | |
| 89 type = "string" | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 fn = function(input) | |
| 95 local Chat = require "site:/lib/Chat.luan" | |
| 41 | 96 local User = require "site:/lib/User.luan" |
| 38 | 97 local thread_id = input.thread_id or error() |
| 43 | 98 local chat = Chat.get_by_id(thread_id) or error() |
| 99 local user = User.current() | |
| 41 | 100 local is_owner = user ~= nil and user.id == chat.user_id |
| 101 is_owner or not chat.is_private or error "private" | |
| 38 | 102 return chat.ai_thread or error() |
| 103 end | |
| 104 } | |
| 105 } | |
| 106 local tools = {nil} | |
| 107 for name, f in pairs(functions) do | |
| 108 f.name = name | |
| 109 f.tool.name = name | |
| 110 tools[#tools+1] = f.tool | |
| 111 end | |
| 9 | 112 |
|
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
113 function Ai_chat.init(system_prompt) |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
114 local thread = { |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
115 system = system_prompt |
| 38 | 116 tools = tools |
|
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
117 messages = {nil} |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
118 } |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
119 return json_string(thread) |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
120 end |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
121 |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
122 function Ai_chat.has_messages(thread) |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
123 thread = json_parse(thread) |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
124 return #thread.messages > 0 |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
125 end |
|
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
126 |
| 38 | 127 local function ask(thread,content) |
| 9 | 128 local messages = thread.messages or error |
| 6 | 129 messages[#messages+1] = { |
| 130 role = "user" | |
| 38 | 131 content = content |
| 6 | 132 } |
| 133 --[=[ | |
| 134 messages[#messages+1] = { | |
| 135 role = "assistant" | |
| 136 content = [[ | |
| 13 | 137 hello |
| 6 | 138 ]] |
| 139 } | |
| 140 if true then | |
| 25 | 141 return |
| 6 | 142 end |
| 143 --]=] | |
| 20 | 144 -- logger.info(json_string(thread)) |
| 9 | 145 local resultJson = claude_chat(thread) |
| 6 | 146 local result = json_parse(resultJson) |
| 147 -- logger.info(json_string(result)) | |
| 148 result.type == "message" or error() | |
| 149 result.role == "assistant" or error() | |
| 150 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
| 151 local content = result.content or error() | |
| 152 messages[#messages+1] = { | |
| 153 role = "assistant" | |
| 154 content = content | |
| 155 } | |
| 38 | 156 local stop_reason = result.stop_reason or error() |
| 157 if stop_reason == "end_turn" then | |
| 158 -- ok | |
| 159 elseif stop_reason == "tool_use" then | |
| 160 local response = {nil} | |
| 161 for _, part in ipairs(content) do | |
| 162 if part.type == "tool_use" then | |
| 163 local f = functions[part.name] or error() | |
| 164 local input = part.input or error() | |
| 165 response[#response+1] = { | |
| 166 type = "tool_result" | |
| 167 tool_use_id = part.id or error() | |
| 168 content = f.fn(input) | |
| 169 } | |
| 170 end | |
| 171 end | |
| 172 ask(thread,response) | |
| 173 else | |
| 174 error(stop_reason) | |
| 175 end | |
| 176 end | |
| 177 | |
| 178 function Ai_chat.ask(thread,input) | |
| 179 thread = json_parse(thread) | |
| 180 ask(thread,input) | |
| 9 | 181 return json_string(thread) |
| 5 | 182 end |
| 183 | |
| 19 | 184 return Ai_chat |
