Add automation framework, typing, and runner for the autobuyer

Turns the autobuyer from a page scraper into something that acts. A dashboard
button queues a run; a desktop process executes it against the real browser.

lib/automations.ts — automations are declarative step lists nested inside the
firm whose site they drive. Steps are click / type / wait / navigate, and they
inherit the firm's tab pattern and URL, so one firm's automation can't act on
another's tab. Adding a button means adding an entry here; the page renders
buttons from the API and the runner receives steps from the server, so neither
needs editing. Runs key on firm:automation — every firm will plausibly have its
own "buy-accounts", and a bare id would resolve to the wrong one.

clicker/runner.py — the daemon behind the buttons. Claims a queued run, works
through the steps, reports each one back for the page's live log. Only one run
executes at a time: two processes driving one physical mouse would interleave
clicks. Heartbeats on its own thread, because a step can block for tens of
seconds and folding the beat into the main loop would show the runner as offline
in the middle of the run it was executing.

clicker/actions.py — one implementation of the safety checks, shared by the CLI
and the runner. Refuses to act when the element is covered by an overlay, when
coordinates fall off-screen, when the browser can't be confirmed frontmost, or
(for type) when the target isn't an editable field.

Typing: uneven human cadence, and the field is read back afterwards and compared
against what was typed — a field that never took focus fails silently and looks
identical to success otherwise. Non-ASCII is rejected because pyautogui skips
those characters without complaint, and newlines because Enter may submit the
form. Typos are deliberately not simulated: a mistyped digit in a trading form
is a real loss, and the correction is the part that can go wrong.

Extension: opens the firm's page when no tab matches, navigates to a specific
page for a navigate step (skipped when already there, so page state survives),
and retries the locate while a freshly loaded React app mounts — `complete` only
means the document loaded.

Staleness reporting, after it cost three debugging rounds: Chrome doesn't reload
an unpacked extension and Python doesn't reload a running process, so both now
report their version. A stale runner gets a red banner naming both versions and
the automation buttons are disabled, rather than failing mid-run on a step type
it predates.

Scale detection is now conservative: a raw OS/browser width ratio is only
trusted when it lands on a real scaling factor. On this multi-monitor desktop
the previous logic would have silently halved every coordinate.

Verified end to end against the live browser: navigate, locate, and a real
click (run #12, all three steps). API round-trips, claim-once semantics, run
cancellation, the heartbeat online/offline lifecycle, motion geometry and
timing, focus activation, and typing verification all pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-08-28 14:00:33 -05:00
co-authored by Claude Opus 5
parent 54221bbc0c
commit b748f95372
17 changed files with 1463 additions and 99 deletions
+43
View File
@@ -0,0 +1,43 @@
import { NextRequest } from 'next/server';
import { getRun, getRecentRuns, cancelRun } from '@/lib/db';
import { findAutomation } from '@/lib/automations';
import { corsJson, corsPreflight } from '../cors';
function shape(row: NonNullable<ReturnType<typeof getRun>>) {
return {
id: row.id,
automationId: row.automation_id,
automationLabel: findAutomation(row.automation_id)?.automation.label ?? row.automation_id,
status: row.status,
stepIndex: row.step_index,
totalSteps: row.total_steps,
log: (() => { try { return JSON.parse(row.log); } catch { return []; } })(),
error: row.error,
createdAt: row.created_at,
updatedAt: row.updated_at,
};
}
/** `?id=` for one run, otherwise the recent history the dashboard shows. */
export async function GET(req: NextRequest) {
const idParam = req.nextUrl.searchParams.get('id');
if (idParam) {
const row = getRun(Number(idParam));
if (!row) return corsJson({ error: 'No such run' }, { status: 404 });
return corsJson({ run: shape(row) });
}
return corsJson({ runs: getRecentRuns(5).map(shape) });
}
/** Stop button. A queued run never starts; a running one halts at the next step. */
export async function DELETE(req: NextRequest) {
const id = Number(req.nextUrl.searchParams.get('id'));
if (!Number.isInteger(id) || id <= 0) {
return corsJson({ error: '`id` is required' }, { status: 400 });
}
return corsJson({ cancelled: cancelRun(id) });
}
export async function OPTIONS() {
return corsPreflight();
}