Mercurial Hosting > lang
comparison src/lib/ai/claude/Chat.luan @ 6:025bb19b65b1
use claude
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 17 Jul 2025 23:19:22 -0600 |
| parents | src/lib/ai/chatgpt/Chat.luan@a970b7a01a74 |
| children | 255c36830154 |
comparison
equal
deleted
inserted
replaced
| 5:a970b7a01a74 | 6:025bb19b65b1 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | |
| 2 local error = Luan.error | |
| 3 local ipairs = Luan.ipairs or error() | |
| 4 local type = Luan.type or error() | |
| 5 local String = require "luan:String.luan" | |
| 6 local starts_with = String.starts_with or error() | |
| 7 local Html = require "luan:Html.luan" | |
| 8 local html_encode = Html.encode or error() | |
| 9 local Parsers = require "luan:Parsers.luan" | |
| 10 local json_parse = Parsers.json_parse or error() | |
| 11 local json_string = Parsers.json_string or error() | |
| 12 local Claude = require "site:/lib/ai/claude/Claude.luan" | |
| 13 local claude_chat = Claude.chat or error() | |
| 14 local Logging = require "luan:logging/Logging.luan" | |
| 15 local logger = Logging.logger "claude/Chat" | |
| 16 local Thread = require "luan:Thread.luan" | |
| 17 | |
| 18 | |
| 19 local Chat = {} | |
| 20 | |
| 21 function Chat.output_messages_html(thread) | |
| 22 local messages = thread and json_parse(thread) or {nil} | |
| 23 for _, message in ipairs(messages) do | |
| 24 local role = message.role or error() | |
| 25 local who | |
| 26 if role=="assistant" then | |
| 27 who = "Claude" | |
| 28 elseif role=="user" then | |
| 29 who = "You" | |
| 30 else | |
| 31 error(role) | |
| 32 end | |
| 33 local function output(text) | |
| 34 if not starts_with( text, "[INTERNAL_UPDATE]" ) then | |
| 35 %> | |
| 36 <h3><%=who%></h3> | |
| 37 <div role="<%=role%>"><%=html_encode(text)%></div> | |
| 38 <% | |
| 39 end | |
| 40 end | |
| 41 local content = message.content or error() | |
| 42 if type(content) == "string" then | |
| 43 output(content) | |
| 44 else | |
| 45 for _, part in ipairs(content) do | |
| 46 if part.type=="text" then | |
| 47 local text = part.text or error() | |
| 48 output(text) | |
| 49 end | |
| 50 end | |
| 51 end | |
| 52 end_for | |
| 53 end | |
| 54 | |
| 55 function Chat.ask(thread,input) | |
| 56 local messages = thread and json_parse(thread) or {nil} | |
| 57 messages[#messages+1] = { | |
| 58 role = "user" | |
| 59 content = input | |
| 60 } | |
| 61 --[=[ | |
| 62 messages[#messages+1] = { | |
| 63 role = "assistant" | |
| 64 content = [[ | |
| 65 - 1 | |
| 66 - 2 | |
| 67 - 3 | |
| 68 - 4 | |
| 69 - 5 | |
| 70 - 6 | |
| 71 - 7 | |
| 72 - 8 | |
| 73 - 9 | |
| 74 - 10 | |
| 75 ]] | |
| 76 Thread.sleep(5000) | |
| 77 } | |
| 78 if true then | |
| 79 return json_string(messages) | |
| 80 end | |
| 81 --]=] | |
| 82 local resultJson = claude_chat{ | |
| 83 messages = messages | |
| 84 } | |
| 85 local result = json_parse(resultJson) | |
| 86 -- logger.info(json_string(result)) | |
| 87 result.type == "message" or error() | |
| 88 result.role == "assistant" or error() | |
| 89 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
| 90 local content = result.content or error() | |
| 91 messages[#messages+1] = { | |
| 92 role = "assistant" | |
| 93 content = content | |
| 94 } | |
| 95 return json_string(messages) | |
| 96 end | |
| 97 | |
| 98 return Chat |
