annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
3 local ipairs = Luan.ipairs or error()
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
4 local pairs = Luan.pairs or error()
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
5 local type = Luan.type or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
6 local String = require "luan:String.luan"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
7 local starts_with = String.starts_with or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
8 local Html = require "luan:Html.luan"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
9 local html_encode = Html.encode or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
10 local Parsers = require "luan:Parsers.luan"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
11 local json_parse = Parsers.json_parse or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
12 local json_string = Parsers.json_string or error()
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
13 local Thread = require "luan:Thread.luan"
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
14 local Claude = require "site:/lib/ai/claude/Claude.luan"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
15 local claude_chat = Claude.chat or error()
74
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
16 local Utils = require "site:/lib/Utils.luan"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
17 local deep_copy = Utils.deep_copy or error()
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
18 local Logging = require "luan:logging/Logging.luan"
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
19 local logger = Logging.logger "claude/Ai_chat"
5
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
22 local Ai_chat = {}
5
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
24 function Ai_chat.output_system_prompt(thread)
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
25 thread = json_parse(thread)
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
26 local system_prompt = thread.system or error
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
27 system_prompt = html_encode(system_prompt)
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
28 %><%=system_prompt%><%
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
29 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
30
52
27758f3b2d69 add hide_ruby
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
31 function Ai_chat.output_messages_html(assistant_controls,thread,old_thread)
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
32 thread = json_parse(thread)
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
33 local messages = thread.messages or error
13
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
34 local n = 0
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
35 if old_thread ~= nil then
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
36 old_thread = json_parse(old_thread)
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
37 local old_messages = old_thread.messages or error
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
38 n = #old_messages
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
39 end
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
40 for i, message in ipairs(messages) do
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
41 if i <= n then
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
42 continue
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
43 end
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
44 local role = message.role or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
45 local who
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
46 if role=="assistant" then
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
47 who = "Claude"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
48 elseif role=="user" then
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
49 who = "You"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
50 else
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
51 error(role)
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
52 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
53 local function output(text)
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
54 text = html_encode(text)
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
55 %>
74
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
56 <div role="<%=role%>" msg="<%=i%>">
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
57 <h3><%=who%></h3>
34
0fb3488a017d show_text
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
58 <div message markdown><%=text%></div>
0fb3488a017d show_text
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
59 <% if role=="assistant" then %>
52
27758f3b2d69 add hide_ruby
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
60 <%= assistant_controls %>
34
0fb3488a017d show_text
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
61 <% end %>
0fb3488a017d show_text
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
62 </div>
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
63 <%
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
64 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
65 local content = message.content or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
66 if type(content) == "string" then
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
67 output(content)
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
68 else
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
69 for _, part in ipairs(content) do
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
70 if part.type=="text" then
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
71 local text = part.text or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
72 output(text)
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
73 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
74 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
75 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
76 end_for
5
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 end
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
79 local function get_chat(chat_id)
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
80 local Chat = require "site:/lib/Chat.luan"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
81 local User = require "site:/lib/User.luan"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
82 local chat = Chat.get_by_id(chat_id) or error()
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
83 local user = User.current()
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
84 local is_owner = user ~= nil and user.id == chat.user_id
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
85 is_owner or not chat.is_private or error "private"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
86 return chat
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
87 end
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
88
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
89 local functions = {
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
90 get_chat = {
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
91 tool = {
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
92 description = "Get the contents of a chat/thread with Claude on this website. The contents will be JSON in the format of the Claude API."
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
93 input_schema = {
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
94 type = "object"
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
95 properties = {
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
96 chat_id = {
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
97 description = "The ID of the chat"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
98 type = "integer"
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
99 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
100 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
101 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
102 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
103 fn = function(input)
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
104 local chat_id = input.chat_id or error()
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
105 local chat = get_chat(chat_id)
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
106 return chat.ai_thread or error()
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
107 end
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
108 }
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
109 get_tts_instructions = {
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
110 tool = {
69
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
111 description = "Get the text-to-speech instructions of a chat/thread on this website. These instructions are passed to OpenAI. If there are no instructions, the empty string is returned."
54
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
112 input_schema = {
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
113 type = "object"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
114 properties = {
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
115 chat_id = {
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
116 description = "The ID of the chat"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
117 type = "integer"
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
118 }
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
119 }
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
120 }
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
121 }
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
122 fn = function(input)
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
123 local chat_id = input.chat_id or error()
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
124 local chat = get_chat(chat_id)
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
125 return chat.tts_instructions or error()
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
126 end
16e5c14eb800 AI tools
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
127 }
69
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
128 get_stt_prompt = {
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
129 tool = {
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
130 description = "Get the speech-to-text prompt of a chat/thread on this website. This prompt is passed to OpenAI. If there is no prompt, the empty string is returned."
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
131 input_schema = {
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
132 type = "object"
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
133 properties = {
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
134 chat_id = {
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
135 description = "The ID of the chat"
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
136 type = "integer"
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
137 }
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
138 }
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
139 }
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
140 }
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
141 fn = function(input)
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
142 local chat_id = input.chat_id or error()
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
143 local chat = get_chat(chat_id)
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
144 return chat.stt_prompt or error()
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
145 end
f5e72f2d1025 add stt_prompt
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
146 }
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
147 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
148 local tools = {nil}
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
149 for name, f in pairs(functions) do
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
150 f.name = name
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
151 f.tool.name = name
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
152 tools[#tools+1] = f.tool
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
153 end
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
154
35
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
155 function Ai_chat.init(system_prompt)
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
156 local thread = {
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
157 system = system_prompt
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
158 tools = tools
35
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
159 messages = {nil}
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
160 }
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
161 return json_string(thread)
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
162 end
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
163
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
164 function Ai_chat.has_messages(thread)
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
165 thread = json_parse(thread)
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
166 return #thread.messages > 0
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
167 end
3117876debca ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
168
74
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
169 local function chit_chat(thread)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
170 thread = deep_copy(thread)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
171 local messages = thread.messages or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
172 for _, message in ipairs(messages) do
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
173 local content = message.content or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
174 if type(content) == "string" then
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
175 content = {{
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
176 type = "text"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
177 text = content
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
178 }}
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
179 message.content = content
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
180 end
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
181 end
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
182 local content = messages[#messages].content or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
183 content[#content].cache_control = { type = "ephemeral" }
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
184 return claude_chat(thread)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
185 end
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
186
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
187 local function ask(thread)
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
188 local messages = thread.messages or error
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
189 --[=[
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
190 messages[#messages+1] = {
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
191 role = "assistant"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
192 content = [[
13
65bd7e245c63 add html
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
193 hello
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
194 ]]
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
195 }
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
196 Thread.sleep(2000)
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
197 if true then
25
3a80ddafe5a4 courses work
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
198 return
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
199 end
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
200 --]=]
20
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
201 -- logger.info(json_string(thread))
74
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents: 71
diff changeset
202 local resultJson = chit_chat(thread)
6
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
203 local result = json_parse(resultJson)
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
204 -- logger.info(json_string(result))
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
205 result.type == "message" or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
206 result.role == "assistant" or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
207 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
208 local content = result.content or error()
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
209 messages[#messages+1] = {
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
210 role = "assistant"
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
211 content = content
025bb19b65b1 use claude
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
212 }
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
213 local stop_reason = result.stop_reason or error()
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
214 if stop_reason == "end_turn" then
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
215 -- ok
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
216 elseif stop_reason == "tool_use" then
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
217 local response = {nil}
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
218 for _, part in ipairs(content) do
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
219 if part.type == "tool_use" then
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
220 local f = functions[part.name] or error()
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
221 local input = part.input or error()
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
222 response[#response+1] = {
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
223 type = "tool_result"
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
224 tool_use_id = part.id or error()
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
225 content = f.fn(input)
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
226 }
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
227 end
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
228 end
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
229 messages[#messages+1] = {
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
230 role = "user"
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
231 content = response
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
232 }
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
233 ask(thread)
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
234 else
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
235 error(stop_reason)
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
236 end
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
237 end
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
238
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
239 function Ai_chat.add(thread,input)
38
238a91f224b1 add get_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
240 thread = json_parse(thread)
71
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
241 local messages = thread.messages or error
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
242 messages[#messages+1] = {
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
243 role = "user"
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
244 content = input
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
245 }
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
246 return json_string(thread)
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
247 end
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
248
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
249 function Ai_chat.respond(thread)
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
250 thread = json_parse(thread)
44bec62c49e2 split ask
Franklin Schmidt <fschmidt@gmail.com>
parents: 69
diff changeset
251 ask(thread)
9
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
252 return json_string(thread)
5
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 end
a970b7a01a74 start ai
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
255 return Ai_chat