Skip to content

Automations

An OpenCode automation reads a source with tools.gws.events_list, keeps its cursor in the automation's durable state, and re-reads from it on the next run. The bus event is only a nudge; the cursor is the truth.

Source Method Provider floor Suggested Signals
gws.gmail tools.gws.events_list 1 min 2 min bus
gws.calendar tools.gws.events_list 1 min 10 min bus

A timer recipe

// every 10 minutes
const state = (await tools.gws.events_list({ source: "calendar", account: "work" })) // no cursor: the first window
// keep state.cursor; on the next run:
const next = await tools.gws.events_list({ source: "calendar", account: "work", cursor: state.cursor })
  • Pass the previous answer's cursor to continue; without one, a read starts at since or 24 hours ago.
  • A cursor is bound to the plugin, source, account and scope. Another account's cursor is CURSOR_INVALID; start again without one.
  • Keep the state per account and source. events_list never merges two accounts' windows.
  • gws.gmail suggests every 2 minutes, and gws.calendar every 10; both are reads with no provider cost. Do not poll faster than the provider's floor.
  • A changed event on rpc.gws.changed says a declared source changed after one of the plugin's writes. Re-read the named capability through RPC, or wait for the timer; the event carries no content.
  • A page with failed[] holds the cursor back: the next run reads the same window again. A page with refused[] moved past those items for good.
  • On gws.gmail, a message whose from address matches the signed-in mailbox owner marks that from actor with self: true. While following Gmail history:
  • Permanently deleted non-noise messages (messagesDeleted) return in removals[] with reason: "deleted".
  • Non-noise messages moved to trash or spam (TRASH or SPAM added) return in removals[] with reason: "withheld". Removing TRASH or SPAM re-lists a restored non-noise message as an event (on an inbox-narrowed feed, when INBOX is present).
  • On an inbox-narrowed feed (scope.query set to "in:inbox" or "label:inbox"), non-noise messages that leave INBOX return in removals[] with reason: "withheld", and messages moved into INBOX are listed as events.
  • On the whole-mailbox feed (no scope.query), archiving or returning a message to INBOX re-lists the message with its updated labels and hash, without emitting a removal. Archiving an older message triggers one messages.get fetch to re-list it with its updated labels, so bulk archives spread those fetches across pages.
  • Drafts, Promotions, Social and other noise messages never emit removals.
  • When failed[] holds the cursor back, the page still delivers its other removals[] (excluding any failed id), and the next run delivers them again.

The attention feed

gws.gmail.attention is a snapshot, not an event feed: a client reads it through tools.gws.gmail_attention when it draws the person's attention feed, with no cursor. It returns the unread inbox threads that ask the person, each with its sender, thread id and Gmail permalink and never its body. The action route calls gws.mail.reply or gws.mail.archive with the item's id; both are denied to agents, so an agent replies with gws_reply and archives with gws_label_thread.

Gmail: one search, and the newest matches

gws.gmail takes a scope with two keys:

  • query is one Gmail search, such as "in:inbox" or "label:work is:unread". The plugin adds its noise filter (-in:drafts -category:promotions -category:social) and, when since is given, its after: bound.
  • order is "oldest" (the default: the feed, every match from since forwards) or "newest" (the newest matches of the window, then what arrives).
// the newest 20 inbox mail, with no guessed time window
const page = await tools.gws.events_list({ source: "gmail", account: "work", limit: 20, scope: { query: "in:inbox", order: "newest" } })
// page.events is oldest first, as every page is; render it reversed
  • A newest read carries the newest events that fit its page. When since names a window the page did not carry whole, it says gap: { reason: "window-truncated", since }; without since, its window has no lower bound, so the newest mail in the mailbox is listed.
  • An inbox-only search ("in:inbox" or "label:inbox") follows Gmail history with labelId: "INBOX", for both "oldest" and "newest" orders. Other searches cannot run in history, so they read the search again on the next call; a newest read resumes after the newest event it listed. Pass the cursor as usual.
  • A cursor is bound to the scope it was issued under: one from another query or order is CURSOR_INVALID, and the reader starts again without one.