annotate src/lib/ai/claude/Translator.luan @ 75:b96cf27e719d default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Aug 2025 14:36:03 -0600
parents 64e35a92d163
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
74
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local Parsers = require "luan:Parsers.luan"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local json_parse = Parsers.json_parse or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local json_string = Parsers.json_string or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Claude = require "site:/lib/ai/claude/Claude.luan"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local claude_chat = Claude.chat or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Logging = require "luan:logging/Logging.luan"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local logger = Logging.logger "claude/Translator"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Translator = {}
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 function Translator.translate(text,lang)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local thread = {
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 system = `%>
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 Translate <%=lang%> in the text you get to English.
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 Preserve formatting.
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 The text may also contain English. Just leave that unchanged.
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 <% `
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 messages = {{
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 role = "user"
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 content = text
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 }}
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 temperature = 0
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local resultJson = claude_chat(thread)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local result = json_parse(resultJson)
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 -- logger.info(json_string(result))
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 result.type == "message" or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 result.role == "assistant" or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 local content = result.content or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 #content==1 or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 content = content[1]
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 content.type == "text" or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 return content.text or error()
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 end
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
64e35a92d163 add translation
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return Translator