dymokomi/luce-ai
Source of release 0.4.1
## The OpenRouter chat-completions client. It builds a request from a model, an
## optional system prompt and a user prompt, sends it over the native HTTPS stack
## (pinning Google's WE1 intermediate, which signs openrouter.ai's leaf), and
## returns the assistant's reply. String in, owned string out, so the Luce editor
## can call it like any other library function.
from luce_http_client import secure as https
import interop
from luce_json import json
pub let invalid = https.invalid
## A module-level buffer for dynamic error text, since an error message may not
## borrow a function local. Single-threaded use only.
var message_buffer: u8[1024]
func fail(text: const u8[], count: usize) -> !:
let n = count if count < message_buffer.length else message_buffer.length
for i in 0..<n:
message_buffer[i] = text[i]
error(invalid, (str)message_buffer[..<n])
## Google Trust Services WE1 (P-256), the intermediate that signs the openrouter.ai
## leaf certificate. Pinned as the trust anchor; update if OpenRouter's issuer
## changes. A production client would instead trust the root and validate the chain.
let we1_x: u8[32] = [0x6f, 0xcd, 0x3a, 0xfe, 0x67, 0x57, 0x47, 0x4c, 0x21, 0x03, 0x85, 0x40, 0xc2, 0x47, 0x5d, 0xbb, 0x58, 0x47, 0x0f, 0x40, 0xc1, 0x5c, 0x17, 0x85, 0xc6, 0x19, 0x37, 0xe7, 0xd5, 0x7c, 0xed, 0x86]
let we1_y: u8[32] = [0x4b, 0x9b, 0x81, 0xd9, 0xd7, 0x1a, 0x13, 0xa5, 0x0a, 0x03, 0xf8, 0x98, 0xc4, 0xc6, 0xe8, 0x9e, 0xff, 0x10, 0x59, 0x8f, 0x2c, 0x26, 0x98, 0xf5, 0xe6, 0x26, 0x25, 0xbb, 0x0f, 0x02, 0xfa, 0x56]
pub func put_str(buf: u8[], pos: usize*, s: str):
for i in 0..<s.length:
buf[*pos + i] = s.bytes[i]
*pos += s.length
## Append `s` as the interior of a JSON string, escaping what must be escaped.
pub func put_json(buf: u8[], pos: usize*, s: str):
for i in 0..<s.length:
let c = s.bytes[i]
if c == 34 or c == 92:
buf[*pos] = 92
buf[*pos + 1] = c
*pos += 2
elif c == 10:
buf[*pos] = 92
buf[*pos + 1] = 110
*pos += 2
elif c == 13:
buf[*pos] = 92
buf[*pos + 1] = 114
*pos += 2
elif c == 9:
buf[*pos] = 92
buf[*pos + 1] = 116
*pos += 2
elif c < 32:
buf[*pos] = 32
*pos += 1
else:
buf[*pos] = c
*pos += 1
## The assistant text of a chat completion: choices[0].message.content. Decoded with
## the JSON library, so nested tool-call objects and every escape parse correctly.
func extract_content(body: str, out: u8[], out_len: usize*) -> bool!:
let document = try json.decode(body)
defer document.release()
let root = (try document.get()).root()
let choices = root.member("choices") else return false
let first = choices.at(0) else return false
let message = first.member("message") else return false
let content = message.member("content") else return false
let text = content.text() else return false
write_msg(out, out_len, text)
return true
## The provider's error text: error.message, present when a request is rejected.
func extract_error(body: str, out: u8[], out_len: usize*) -> bool!:
let document = try json.decode(body)
defer document.release()
let root = (try document.get()).root()
let problem = root.member("error") else return false
let message = problem.member("message") else return false
let text = message.text() else return false
write_msg(out, out_len, text)
return true
pub func write_msg(out: u8[], out_len: usize*, s: str):
let n = s.length if s.length < out.length else out.length
for i in 0..<n:
out[i] = s.bytes[i]
*out_len = n
## Send a chat-completions POST with the bearer key, pinning WE1 as the issuer. Fills
## `output` with the response body and returns the HTTP status; the one place the
## endpoint and trust anchor live, shared by the simple client and the agent.
pub func post(api_key: str, body: const u8[], output: u8[]) -> https.Response!:
var auth: u8[1200]
var ap: usize = 0
put_str(auth, &ap, "Bearer ")
put_str(auth, &ap, api_key)
return try https.fetch("openrouter.ai", 443, we1_x, we1_y, "POST", "/api/v1/chat/completions",
(str)auth[..<ap], "application/json", body, output)
## Ask a model for a completion, writing the reply into `out` and its length into
## `out_len`. `ok` is set when the model answered; otherwise `out` holds an error
## message. Never fails, so it is safe to run on a worker thread.
pub func complete_into(api_key: str, model: str, system: str, prompt: str, out: u8[], out_len: usize*, ok: bool*):
*ok = false
*out_len = 0
if api_key.length == 0:
write_msg(out, out_len, "No OpenRouter API key is configured.")
return
if model.length == 0:
write_msg(out, out_len, "No model is configured for this mode.")
return
var body: u8[16384]
var bp: usize = 0
put_str(body, &bp, "{\"model\":\"")
put_json(body, &bp, model)
put_str(body, &bp, "\",\"messages\":[")
if system.length > 0:
put_str(body, &bp, "{\"role\":\"system\",\"content\":\"")
put_json(body, &bp, system)
put_str(body, &bp, "\"},")
put_str(body, &bp, "{\"role\":\"user\",\"content\":\"")
put_json(body, &bp, prompt)
put_str(body, &bp, "\"}]}")
var response: u8[65536]
let result = post(api_key, body[..<bp], response) catch failure:
write_msg(out, out_len, failure.message)
return
if result.status != 200:
let shown = extract_error((str)response[..<result.length], out, out_len) catch failure:
recover false
if not shown:
write_msg(out, out_len, "OpenRouter request was rejected.")
return
let shown = extract_content((str)response[..<result.length], out, out_len) catch failure:
recover false
if not shown:
write_msg(out, out_len, "No assistant message in the OpenRouter response.")
return
*ok = true
## Ask a model for a completion, returning the assistant text or failing with the
## provider's error message. Blocks; the async task module runs it on a thread.
pub func complete(api_key: str, model: str, system: str, prompt: str) -> interop.Owned[str]!:
var out: u8[65536]
var length: usize = 0
var ok = false
complete_into(api_key, model, system, prompt, out, &length, &ok)
if not ok:
try fail(out, length)
return try interop.copy_text((str)out[..<length])