일전에 @kkungMinyoung Jeong 님께서 올려주신 Vibe Writing 글에, 숟가락을 얹어보았습니다. HammerSpoon 에서 쓰는 lua 코드에서 OpenRouter 의 google/gemini-2.0-flash-001 모델로 살짝 수정했습니다.

  • config, payload, 응답부 변경

바로바로 한영, 영한 변환하니까, 너무 편하고 좋네요. 다른 언어도 추가하고 그외 다른 활용 요소들도 생각 나서, 앞으로도 이것저것 만져볼 것 같네요.

local config = {
  open_router_api_key = "sk-...",
  open_router_api_url = "https://openrouter.ai/api/v1/chat/completions",
  model = "google/gemini-2.0-flash-001",
}

local function callOpenRouter(text, callback)
  if not config.open_router_api_key or config.open_router_api_key ~= "sk-..." then
    hs.alert.show("Config error: Please set your open_router_api_key in modules/ko-en.lua")
    return
  end

  local instruction = "입력된 문장이 영어일 경우 한국어로, 한국어일 경우 영어로 변환해줘. 의미를 모국어 사용자가 자연스럽게 받아들일 수 있게 정확하고 유창하게 전달하고, 불필요한 문장을 생략하여 명료하게 작성해. 번역어 외의 다른 문장을 추가하지 말고 번역 그 자체만 반환해."

  local payload = {
    model = config.model,
    messages = {
      {role = "assistant", content = instruction},
      {role = "user", content = text}
    },
    max_tokens = 4096,
    stream = false
  }

  local request_body = hs.json.encode(payload)

  local headers = {
    ["Content-Type"] = "application/json",
    ["Authorization"] = "Bearer " .. config.open_router_api_key,
    ["HTTP-Referer"] = "https://hammerspoon.ai",
    ["X-Title"] = "Hammerspoon Translator"
  }

  hs.http.asyncPost(config.open_router_api_url, request_body, headers, function(status, response, _)
    if status == 200 then
      local success, data = pcall(hs.json.decode, response)
      if success and data.choices and #data.choices > 0 and data.choices[1].message and data.choices[1].message.content then
        local translated = data.choices[1].message.content
        print("TR: " .. translated)
        callback(translated)
      else
        local error_message = "API call failed. Response: " .. hs.inspect(data)
        print(error_message)
        hs.alert.show(error_message)
        callback(nil)
      end
    else
      local error_message = "HTTP request failed. Status: " .. status .. ", Response: " .. response
      print(error_message)
      hs.alert.show(error_message)
      callback(nil)
    end
  end)
end
3

If you have a fediverse account, you can quote this note from your own instance. Search https://hackers.pub/ap/notes/0197bf22-85e2-719b-9320-90531f216fea on your instance and quote it. (Note that quoting is not supported in Mastodon.)