DocsAPI ReferenceTW_inference
Docs

API Reference

Updated 2026-03-22

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_inference

Runs 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.

Extension + server endpoint|Group: TW_inference|Aliases: TW.inference, inference
TW_inference(prompt, data?)
networkauth required

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
example
// ==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);
Notes
  • 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.
Automation

TW_email#

@grant TW_email

Sends an email to the authenticated Tweeks account, immediately or on a delay. Delivery is handled by Tweeks after the runtime accepts the call.

Extension + server endpoint|Group: TW_email|Aliases: TW.email, email
TW_email(subject, body?, delay?)
networkauth required

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
example
// ==UserScript==
// @grant TW_email
// ==/UserScript==

await GM.email(
  "Daily page digest",
  document.body.innerText.slice(0, 4000),
  "15m"
);
Notes
  • 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.
@grant TW_contextMenu

Creates browser context-menu entries owned by the current script. Menus are persisted per script and cleaned up automatically when permissions or script state change.

Browser runtime|Group: TW_contextMenu|Aliases: TW.contextMenu, contextMenu
TW_contextMenu(title, callback, options?)

Create or update a context-menu item and bind its callback through the injected runtime.

returns string | null
  • title is required unless options.type === separator
  • callback must be a function
  • supported options include contexts, documentUrlPatterns, targetUrlPatterns, type, and parentId
  • if documentUrlPatterns are omitted, the script's @match patterns are used when available
example
// ==UserScript==
// @grant TW_contextMenu
// ==/UserScript==

const menuId = TW.contextMenu("Summarize selection", async () => {
  const text = window.getSelection()?.toString() || "";
  console.log(text);
}, {
  contexts: ["selection"],
});
Notes
  • Granting TW_contextMenu implicitly enables TW_removeContextMenu.
  • Menu ids are deterministic per script and title hash.
  • This stays inside the Tweeks browser runtime.
@grant TW_removeContextMenu

Removes a context-menu entry previously registered by the same script. Guarded by script ownership — the runtime refuses to remove menu ids the current script did not create.

Browser runtime|Group: TW_contextMenu|Aliases: TW.removeContextMenu, removeContextMenu, TW_contextMenu, TW.contextMenu, contextMenu
TW_removeContextMenu(menuId)

Remove a menu item owned by the current script and unregister its callback.

returns void
  • menuId must be a string
  • the menu must belong to the current script
  • missing menu items are tolerated during browser removal, but non-owned ids fail
example
// ==UserScript==
// @grant TW_removeContextMenu
// ==/UserScript==

TW.removeContextMenu("ctx_abcd1234_demo");
Notes
  • TW_contextMenu implicitly grants this capability.
  • The service worker tracks menu ownership in session storage for cleanup.
Notifications

TW_toast#

@grant TW_notifications

Shows a transient in-extension toast notification through the Tweeks UI layer. Stays inside the extension runtime and does not hit the network.

On page runtime|Group: TW_notifications|Aliases: TW.notifications, notifications, TW_toast, TW.toast, toast
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
example
// ==UserScript==
// @grant TW_notifications
// ==/UserScript==

await TW.toast("Saved filters for this page", "success", "Tweeks", 2500);
Notes
  • Grant TW_notifications if you want both toast and alert.
  • This API initializes the local menu UI if needed.
Notifications

TW_alert#

@grant TW_notifications

Shows a confirm-style modal alert through the Tweeks notification layer. Resolves to the user's confirm or cancel choice.

On page runtime|Group: TW_notifications|Aliases: TW.notifications, notifications, TW_alert, TW.alert, alert
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
example
// ==UserScript==
// @grant TW_notifications
// ==/UserScript==

const { confirmed } = await GM.alert(
  "Archive this page state?",
  "warning",
  "Tweeks",
  "Archive",
  "Cancel"
);
Notes
  • Grant TW_notifications if you want both alert and toast.
  • The content runtime returns confirmed: true only when the user picks the confirm branch.

Exports

Export the API reference.

Methods, grants, signatures, permissions, examples, and notes for Tweeks scripts in portable formats.

Includes

  • API catalog
  • Permission groups
  • Accepted grants and aliases
  • Examples and notes

Markdown for reading. JSON for tooling.