DocsAPI ReferenceTW_inference
Tweeks Engine API Reference
Reference for the TW_* APIs available in Tweeks scripts. Use them when a tweek needs to do more than modify a page — run AI inference, send email, show notifications, or add context menu items.
@grant TW_inferenceRuns a prompt through Tweeks' configured inference model and returns plain text back to the userscript. The call is forwarded through the Tweeks runtime and requires the user to be signed in.
TW_inference(prompt, data?)Submit a prompt and optional structured data, then receive a text completion.
returns Promise<string>- prompt must be a non-empty string
- prompt is capped at 10,000 characters
- prompt + serialized data is capped at 50,000 characters
- server max output is 4,096 tokens
- client-side timeout is 120 seconds
- successful calls consume shared Tweeks quota when FREE_QUOTA_INFERENCE_COST is above zero
// ==UserScript==
// @grant TW_inference
// ==/UserScript==
const summary = await TW.inference(
"Summarize the visible article in three bullets.",
{ title: document.title, url: location.href }
);
console.log(summary);- This API is async-only.
- The service worker rejects unauthenticated requests with a standardized sign-in error and the Tweeks panel shows a sign-in alert.
- The server logs successful usage to inference_logs and counts those rows against quota.
TW_email#✓
@grant TW_emailSends an email to the authenticated Tweeks account, immediately or on a delay. Delivery is handled by Tweeks after the runtime accepts the call.
TW_email(subject, body?, delay?)Deliver a message to the current user's email address, optionally scheduled in the future.
returns Promise<{ success: true; messageId?: string; scheduled: boolean; runId?: string }>- subject must be a non-empty string and is capped at 200 characters
- body must be a string if provided and is capped at 50,000 characters
- delay must be a string like 10s, 5m, 1h, 24h, or 7d
- maximum delay is 30 days
- client-side timeout is 30 seconds
// ==UserScript==
// @grant TW_email
// ==/UserScript==
await GM.email(
"Daily page digest",
document.body.innerText.slice(0, 4000),
"15m"
);- Newlines are stripped from the subject before delivery.
- Unauthenticated calls return a sign-in error.
- Delayed delivery returns a workflow run id instead of sending immediately.
TW_toast#✓
@grant TW_notificationsShows a transient in-extension toast notification through the Tweeks UI layer. Stays inside the extension runtime and does not hit the network.
TW_toast(message, type?, title?, duration?)Display a toast in the Tweeks notification layer and receive its generated id.
returns Promise<{ success: true; toastId: string | null }>- message must be a non-empty string
- type defaults to info
- duration defaults to 4000 ms if omitted
- client-side timeout is 5 seconds
// ==UserScript==
// @grant TW_notifications
// ==/UserScript==
await TW.toast("Saved filters for this page", "success", "Tweeks", 2500);- Grant TW_notifications if you want both toast and alert.
- This API initializes the local menu UI if needed.
TW_alert#✓
@grant TW_notificationsShows a confirm-style modal alert through the Tweeks notification layer. Resolves to the user's confirm or cancel choice.
TW_alert(message, type?, title?, confirmText?, cancelText?)Display an interactive alert and resolve with the user's confirmation choice.
returns Promise<{ confirmed: boolean }>- message must be a non-empty string
- type defaults to info
- confirmText defaults to OK
- client-side timeout is 60 seconds
// ==UserScript==
// @grant TW_notifications
// ==/UserScript==
const { confirmed } = await GM.alert(
"Archive this page state?",
"warning",
"Tweeks",
"Archive",
"Cancel"
);- Grant TW_notifications if you want both alert and toast.
- The content runtime returns confirmed: true only when the user picks the confirm branch.