view src/lib/ai/claude/Translator.luan @ 74:64e35a92d163

add translation
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Aug 2025 13:31:46 -0600
parents
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Parsers = require "luan:Parsers.luan"
local json_parse = Parsers.json_parse or error()
local json_string = Parsers.json_string or error()
local Claude = require "site:/lib/ai/claude/Claude.luan"
local claude_chat = Claude.chat or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "claude/Translator"


local Translator = {}

function Translator.translate(text,lang)
	local thread = {
		system = `%>
Translate <%=lang%> in the text you get to English.
Preserve formatting.
The text may also contain English.  Just leave that unchanged.
<%		`
		messages = {{
			role = "user"
			content = text
		}}
		temperature = 0
	}
	local resultJson = claude_chat(thread)
	local result = json_parse(resultJson)
	-- logger.info(json_string(result))
	result.type == "message" or error()
	result.role == "assistant" or error()
	result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
	local content = result.content or error()
	#content==1 or error()
	content = content[1]
	content.type == "text" or error()
	return content.text or error()
end

return Translator