Mercurial Hosting > lang
comparison src/lib/ai/claude/Ai_chat.luan @ 71:44bec62c49e2 default tip
split ask
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 24 Aug 2025 08:32:08 -0600 |
parents | f5e72f2d1025 |
children |
comparison
equal
deleted
inserted
replaced
70:4a73af8f2203 | 71:44bec62c49e2 |
---|---|
8 local Html = require "luan:Html.luan" | 8 local Html = require "luan:Html.luan" |
9 local html_encode = Html.encode or error() | 9 local html_encode = Html.encode or error() |
10 local Parsers = require "luan:Parsers.luan" | 10 local Parsers = require "luan:Parsers.luan" |
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 Claude = require "site:/lib/ai/claude/Claude.luan" | 14 local Claude = require "site:/lib/ai/claude/Claude.luan" |
14 local claude_chat = Claude.chat or error() | 15 local claude_chat = Claude.chat or error() |
15 local Logging = require "luan:logging/Logging.luan" | 16 local Logging = require "luan:logging/Logging.luan" |
16 local logger = Logging.logger "claude/Ai_chat" | 17 local logger = Logging.logger "claude/Ai_chat" |
17 | 18 |
161 function Ai_chat.has_messages(thread) | 162 function Ai_chat.has_messages(thread) |
162 thread = json_parse(thread) | 163 thread = json_parse(thread) |
163 return #thread.messages > 0 | 164 return #thread.messages > 0 |
164 end | 165 end |
165 | 166 |
166 local function ask(thread,content) | 167 local function ask(thread) |
167 local messages = thread.messages or error | 168 local messages = thread.messages or error |
168 messages[#messages+1] = { | |
169 role = "user" | |
170 content = content | |
171 } | |
172 --[=[ | 169 --[=[ |
173 messages[#messages+1] = { | 170 messages[#messages+1] = { |
174 role = "assistant" | 171 role = "assistant" |
175 content = [[ | 172 content = [[ |
176 hello | 173 hello |
177 ]] | 174 ]] |
178 } | 175 } |
176 Thread.sleep(2000) | |
179 if true then | 177 if true then |
180 return | 178 return |
181 end | 179 end |
182 --]=] | 180 --]=] |
183 -- logger.info(json_string(thread)) | 181 -- logger.info(json_string(thread)) |
206 tool_use_id = part.id or error() | 204 tool_use_id = part.id or error() |
207 content = f.fn(input) | 205 content = f.fn(input) |
208 } | 206 } |
209 end | 207 end |
210 end | 208 end |
211 ask(thread,response) | 209 messages[#messages+1] = { |
210 role = "user" | |
211 content = response | |
212 } | |
213 ask(thread) | |
212 else | 214 else |
213 error(stop_reason) | 215 error(stop_reason) |
214 end | 216 end |
215 end | 217 end |
216 | 218 |
217 function Ai_chat.ask(thread,input) | 219 function Ai_chat.add(thread,input) |
218 thread = json_parse(thread) | 220 thread = json_parse(thread) |
219 ask(thread,input) | 221 local messages = thread.messages or error |
222 messages[#messages+1] = { | |
223 role = "user" | |
224 content = input | |
225 } | |
220 return json_string(thread) | 226 return json_string(thread) |
221 end | 227 end |
222 | 228 |
229 function Ai_chat.respond(thread) | |
230 thread = json_parse(thread) | |
231 ask(thread) | |
232 return json_string(thread) | |
233 end | |
234 | |
223 return Ai_chat | 235 return Ai_chat |