Mercurial Hosting > lang
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 37:d72b1dff01c9 | 38:238a91f224b1 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | 2 local error = Luan.error |
| 3 local ipairs = Luan.ipairs or error() | 3 local ipairs = Luan.ipairs or error() |
| 4 local pairs = Luan.pairs or error() | |
| 4 local type = Luan.type or error() | 5 local type = Luan.type or error() |
| 5 local String = require "luan:String.luan" | 6 local String = require "luan:String.luan" |
| 6 local starts_with = String.starts_with or error() | 7 local starts_with = String.starts_with or error() |
| 7 local Html = require "luan:Html.luan" | 8 local Html = require "luan:Html.luan" |
| 8 local html_encode = Html.encode or error() | 9 local html_encode = Html.encode or error() |
| 74 end | 75 end |
| 75 end | 76 end |
| 76 end_for | 77 end_for |
| 77 end | 78 end |
| 78 | 79 |
| 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 | |
| 79 | 109 |
| 80 function Ai_chat.init(system_prompt) | 110 function Ai_chat.init(system_prompt) |
| 81 local thread = { | 111 local thread = { |
| 82 system = system_prompt | 112 system = system_prompt |
| 113 tools = tools | |
| 83 messages = {nil} | 114 messages = {nil} |
| 84 } | 115 } |
| 85 return json_string(thread) | 116 return json_string(thread) |
| 86 end | 117 end |
| 87 | 118 |
| 88 function Ai_chat.has_messages(thread) | 119 function Ai_chat.has_messages(thread) |
| 89 thread = json_parse(thread) | 120 thread = json_parse(thread) |
| 90 return #thread.messages > 0 | 121 return #thread.messages > 0 |
| 91 end | 122 end |
| 92 | 123 |
| 93 function Ai_chat.ask(thread,input) | 124 local function ask(thread,content) |
| 94 thread = json_parse(thread) | |
| 95 local messages = thread.messages or error | 125 local messages = thread.messages or error |
| 96 messages[#messages+1] = { | 126 messages[#messages+1] = { |
| 97 role = "user" | 127 role = "user" |
| 98 content = input | 128 content = content |
| 99 } | 129 } |
| 100 --[=[ | 130 --[=[ |
| 101 messages[#messages+1] = { | 131 messages[#messages+1] = { |
| 102 role = "assistant" | 132 role = "assistant" |
| 103 content = [[ | 133 content = [[ |
| 118 local content = result.content or error() | 148 local content = result.content or error() |
| 119 messages[#messages+1] = { | 149 messages[#messages+1] = { |
| 120 role = "assistant" | 150 role = "assistant" |
| 121 content = content | 151 content = content |
| 122 } | 152 } |
| 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) | |
| 123 return json_string(thread) | 178 return json_string(thread) |
| 124 end | 179 end |
| 125 | 180 |
| 126 return Ai_chat | 181 return Ai_chat |
