Window | AIUI | Rokid

Window represents the current AIUI Agent window. The global window object lets an Agent read the window dimensions, open a configured Widget, or request that the current …

Window

Window represents the current AIUI Agent window. The global window object lets an Agent read the window dimensions, open a configured Widget, or request that the current Agent close.

Read the Window Dimensions

Use innerWidth and innerHeight to read the current viewport dimensions in pixels:

const viewport = {
  width: window.innerWidth,
  height: window.innerHeight,
};

console.log(viewport);

The runtime maintains both properties. Agents should treat them as read-only information.

Open a Widget

Call window.open() to open a Widget declared in the current Agent's app.json widgets list. Omit the .ink extension from the Widget path. Query parameters may follow the path:

window.open('widgets/weather/index?city=hangzhou');

The target argument is optional and defaults to _widget. You can also pass it explicitly:

window.open('widgets/weather/index?city=hangzhou', '_widget');

In AIUI, window.open() opens declared Widgets only. It cannot open Pages, external URLs, or arbitrary Widgets that are not configured in app.json.widgets. The call returns immediately without returning a Widget instance or an opening result. See Widget for declaration details and Routing for the complete routing behavior.

Close the Current Agent

Call window.close() to send the host a request to close the current Agent instance:

window.close();

The method only sends a close request. The host controls when the Agent closes.

Encode an ImageBitmap Directly

const imageData = new ImageData(2, 2);
const bitmap = await createImageBitmap(imageData);
try {
  const blob = await createImageBlob(bitmap, { type: 'image/png' });
  await upload(blob);
} finally {
  bitmap.close();
}

createImageBlob() is a global function that asynchronously encodes an open ImageBitmap as a PNG or JPEG Blob. Use it directly when Canvas composition is not needed. Closing the bitmap after encoding starts does not cancel the operation; passing a bitmap that is already closed rejects with InvalidStateError.

Global Access

In a regular AIUI window, window, self, globalThis, and global refer to the same global object:

console.log(window === globalThis); // true
console.log(window === self); // true
console.log(window === global); // true

AIUI explicitly mounts a subset of Web APIs on window. Do not assume that every directly accessible global symbol also has a corresponding window.xxx property. See the corresponding API pages for exact exposure and limitations.

Agent Workers use a separate global scope and do not provide Window. See AgentWorker for its global object and lifecycle.

API Reference

window.innerWidth

  • Type: Read-only number.
  • Description: Returns the width of the current window viewport in pixels.

window.innerHeight

  • Type: Read-only number.
  • Description: Returns the height of the current window viewport in pixels.

window.open(url, target?)

Requests that the host open a Widget declared by the current Agent.

ParameterTypeRequiredDescription
urlstringYesA Widget path declared in app.json.widgets, with optional query parameters and without the .ink extension.
target'_widget'NoThe opening target. Defaults to _widget, the only currently supported value.

Return value: undefined.

Throws a TypeError when url is empty. Do not pass a target other than _widget; AIUI does not support opening other targets through this method.

window.close()

Requests that the host close the current Agent instance.

Return value: undefined.

window.atob(encodedData)

Decodes a Base64-encoded string into a binary string.

  • Parameter: encodedData, a string containing the Base64 data to decode.
  • Return value: The decoded string.

window.btoa(stringToEncode)

Encodes a binary string as Base64.

  • Parameter: stringToEncode, the binary string to encode.
  • Return value: The Base64-encoded string.

createImageBlob(source, options?)

Asynchronously encodes an ImageBitmap as a PNG or JPEG Blob. source must be open when called. Native external/GPU images without stable readable pixels reject with NotSupportedError, and a full encoding queue rejects with EncodingError.

ParameterTypeRequiredDescription
sourceImageBitmapYesThe bitmap to read and encode.
options.typestringNoOutput MIME type, defaulting to image/png; image/jpeg selects JPEG and other values fall back to PNG.
options.qualitynumberNoJPEG quality from 0 through 1; invalid values use the encoder default and PNG ignores it.

Return value: Promise<Blob> whose type reports the actual output format.