comparison src/lib/ai/claude/Ai_chat.luan @ 74:64e35a92d163

add translation
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Aug 2025 13:31:46 -0600
parents 44bec62c49e2
children
comparison
equal deleted inserted replaced
73:60ebb333b40c 74:64e35a92d163
11 local json_parse = Parsers.json_parse or error() 11 local json_parse = Parsers.json_parse or error()
12 local json_string = Parsers.json_string or error() 12 local json_string = Parsers.json_string or error()
13 local Thread = require "luan:Thread.luan" 13 local Thread = require "luan:Thread.luan"
14 local Claude = require "site:/lib/ai/claude/Claude.luan" 14 local Claude = require "site:/lib/ai/claude/Claude.luan"
15 local claude_chat = Claude.chat or error() 15 local claude_chat = Claude.chat or error()
16 local Utils = require "site:/lib/Utils.luan"
17 local deep_copy = Utils.deep_copy or error()
16 local Logging = require "luan:logging/Logging.luan" 18 local Logging = require "luan:logging/Logging.luan"
17 local logger = Logging.logger "claude/Ai_chat" 19 local logger = Logging.logger "claude/Ai_chat"
18 20
19 21
20 local Ai_chat = {} 22 local Ai_chat = {}
49 error(role) 51 error(role)
50 end 52 end
51 local function output(text) 53 local function output(text)
52 text = html_encode(text) 54 text = html_encode(text)
53 %> 55 %>
54 <h3><%=who%></h3> 56 <div role="<%=role%>" msg="<%=i%>">
55 <div role="<%=role%>"> 57 <h3><%=who%></h3>
56 <div message markdown><%=text%></div> 58 <div message markdown><%=text%></div>
57 <% if role=="assistant" then %> 59 <% if role=="assistant" then %>
58 <%= assistant_controls %> 60 <%= assistant_controls %>
59 <% end %> 61 <% end %>
60 </div> 62 </div>
162 function Ai_chat.has_messages(thread) 164 function Ai_chat.has_messages(thread)
163 thread = json_parse(thread) 165 thread = json_parse(thread)
164 return #thread.messages > 0 166 return #thread.messages > 0
165 end 167 end
166 168
169 local function chit_chat(thread)
170 thread = deep_copy(thread)
171 local messages = thread.messages or error()
172 for _, message in ipairs(messages) do
173 local content = message.content or error()
174 if type(content) == "string" then
175 content = {{
176 type = "text"
177 text = content
178 }}
179 message.content = content
180 end
181 end
182 local content = messages[#messages].content or error()
183 content[#content].cache_control = { type = "ephemeral" }
184 return claude_chat(thread)
185 end
186
167 local function ask(thread) 187 local function ask(thread)
168 local messages = thread.messages or error 188 local messages = thread.messages or error
169 --[=[ 189 --[=[
170 messages[#messages+1] = { 190 messages[#messages+1] = {
171 role = "assistant" 191 role = "assistant"
177 if true then 197 if true then
178 return 198 return
179 end 199 end
180 --]=] 200 --]=]
181 -- logger.info(json_string(thread)) 201 -- logger.info(json_string(thread))
182 local resultJson = claude_chat(thread) 202 local resultJson = chit_chat(thread)
183 local result = json_parse(resultJson) 203 local result = json_parse(resultJson)
184 -- logger.info(json_string(result)) 204 -- logger.info(json_string(result))
185 result.type == "message" or error() 205 result.type == "message" or error()
186 result.role == "assistant" or error() 206 result.role == "assistant" or error()
187 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() 207 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()