KryonOSDocs
Cloud & AI

KryonOS Cloud AI Engine

Invoke KryonOS Cloud AI endpoints from microcontroller JavaScript apps.

17. KryonOS Cloud & AI Engine (Kryon.ai / System.ai)

The KryonOS Cloud AI Engine connects your ESP32 apps directly to the Kryon Cloud Platform (https://kryonos.harislab.tech), enabling advanced Large Language Model intelligence, token streaming, schema extraction, and vision processing directly on low-power microcontrollers.

Both Kryon.ai and System.ai (as well as AI) expose identical methods.


17.1 Real-Time Token Streaming

Streams tokens in real time directly into your user interface as they are generated by the cloud without allocating large text buffers in microcontroller RAM.

Kryon.ai.stream(options)

  • Parameters:
    • options (Object):
      • prompt (String, required): The prompt query or instruction.
      • system (String, optional): System prompt setting persona or behavior.
      • temperature (Float, optional): Sampling temperature (0.0 to 1.0, default 0.3).
      • sanitize (Boolean, optional): Strips Markdown formatting for raw display (default true).
      • onToken (Function, optional): Invoked for each token chunk: function(tokenChunk).
      • onComplete (Function, optional): Invoked upon completion: function(fullText, usage).
      • onError (Function, optional): Invoked on failure: function(errorMessage).
  • Returns: Boolean (true if stream initiated successfully).
// Example: Real-time text generation directly to display
Kryon.ai.stream({
    prompt: "Give me 3 tips for battery conservation on ESP32.",
    system: "You are a concise embedded systems assistant.",
    temperature: 0.3,
    onToken: function(token) {
        // Render each token as it arrives
        System.print(token);
    },
    onComplete: function(fullText, usage) {
        console.log("Stream finished! Remaining quota today:", usage.remaining);
    },
    onError: function(err) {
        console.error("AI Error:", err);
    }
});

17.2 One-Shot Ask

Sends a prompt and returns the complete text answer once finished.

Kryon.ai.ask(prompt, options, callback)

  • Parameters:
    • prompt (String or Object): Text prompt or options object { prompt: "...", system: "...", temperature: 0.3 }.
    • options (Object, optional): Optional { system: "...", temperature: 0.3 }.
    • callback (Function, optional): Callback function(err, response, usage).
  • Returns: String (response text) or null on failure.
// Synchronous style:
var answer = Kryon.ai.ask("What is the speed of light?");
console.log("Answer:", answer);

// Asynchronous callback style:
Kryon.ai.ask("Explain I2C bus arbitration.", function(err, response, usage) {
    if (err) {
        console.error("Failed to query AI:", err);
    } else {
        console.log("AI says:", response);
        console.log("Daily credits left:", usage.remaining);
    }
});

17.3 Schema-Constrained JSON Extraction

Analyzes unstructured text, natural language, or raw sensor feeds and returns a guaranteed structured JavaScript object matching your schema.

Kryon.ai.extract(options, callback)

  • Parameters:
    • options (Object):
      • prompt (String, required): Extraction instruction.
      • input (String, required): Unstructured data or sensor logs.
      • schema (Object or String, required): Expected JSON schema or structure.
    • callback (Function, optional): Callback function(err, structuredObject).
  • Returns: Object (parsed structured JSON object) or null on failure.
// Example: Extract sensor reading values from messy logs
var messyLog = "Room 101 status at 14:32 - Temperature reading is 23.4 Celsius, humidity 65%, fan active.";

Kryon.ai.extract({
    prompt: "Extract temperature, humidity, and fan state.",
    input: messyLog,
    schema: {
        temp_c: "number",
        humidity_pct: "number",
        fan_active: "boolean"
    }
}, function(err, data) {
    if (!err && data) {
        console.log("Temperature:", data.temp_c);
        console.log("Humidity:", data.humidity_pct);
        console.log("Fan Active:", data.fan_active);
    }
});

17.4 Vision Analysis

Analyzes screen buffer or image data and returns visual insights.

Kryon.ai.vision(options, callback)

  • Parameters:
    • options (Object or String): Text prompt or { prompt: "...", image: "<base64_data>" }.
    • callback (Function, optional): Callback function(err, analysisText).
  • Returns: String (analysis text) or null on failure.
Kryon.ai.vision("Describe the UI widgets rendered on screen.", function(err, description) {
    if (!err) {
        console.log("Screen description:", description);
    }
});

17.5 Telemetry & Account Status

Checks connectivity, gateway health, and remaining daily credit quota.

Kryon.ai.status()

  • Returns: Object ->
    {
        connected: Boolean,     // Device has WiFi connection
        gatewayOk: Boolean,     // Cloud gateway reachable
        paired: Boolean,        // Device is claimed by a KryonOS account
        userName: String,       // Account username (e.g. "Haris")
        beamHandle: String,     // KryonBeam handle (e.g. "@haris")
        dailyUsed: Integer,     // Daily AI calls used
        dailyLimit: Integer,    // Daily AI call limit
        remaining: Integer,     // Remaining calls today
        percentUsed: Integer,   // Percentage used
        error: String           // Last error message if any
    }
    
var status = Kryon.ai.status();
console.log("Paired:", status.paired, "User:", status.userName);
console.log("AI Quota: " + status.remaining + "/" + status.dailyLimit + " remaining");