Object MethodNetwork and Integration

AI.ask(prompt, providerId?, apiKey?)

Sends a prompt to the configured AI provider; returns a table with ok, text, error, errorCode, providerId, providerName and modelId (at most 20 calls per minute).

Documented for app version 1.0.51Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

AI is a global table: call its functions with a dot (AI.<method>(...)), not a colon.

When To Use It

Use this to send text or OCR output to the configured cloud provider.

Parameters and Return

prompt is required. providerId is optional; nil uses app settings. Credentials come from app settings; runtime API key overrides are disabled.

Best Combined With

Check ok, text, errorCode and error in the result table. This call does not tap the screen. Visual Decision retries, thresholds and outcome branches require additional Lua logic. Bound request frequency in loops.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

-- Uses the provider configured in the app.
local response = AI.ask("Return a short answer")
if response.ok then print(response.text) else print(response.errorCode, response.error) end

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
-- Uses the provider configured in the app.
local response = AI.ask("Return a short answer")
if response.ok then print(response.text) else print(response.errorCode, response.error) end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
-- Uses the provider configured in the app.
local response = AI.ask("Return a short answer")
if response.ok then print(response.text) else print(response.errorCode, response.error) end
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  -- Uses the provider configured in the app.
  local response = AI.ask("Return a short answer")
  if response.ok then print(response.text) else print(response.errorCode, response.error) end
end)

if not ok then
  print("API step failed: AI.ask: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- Use configured credentials and handle failed responses before continuing.
local function run_ask_step()
  -- Uses the provider configured in the app.
  local response = AI.ask("Return a short answer")
  if response.ok then print(response.text) else print(response.errorCode, response.error) end
end

local ok, err = pcall(run_ask_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
-- Uses the provider configured in the app.
local response = AI.ask("Return a short answer")
if response.ok then print(response.text) else print(response.errorCode, response.error) end