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