Form Automation

medium10 minform-automation.ts

A form has to be filled and submitted on a site without an API.

What it shows

  • Add the interaction module so the agent can type, select and submit

The code

form-automation.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { BrowserSession, browserTools } from '@cogitator-ai/browser';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const session = new BrowserSession({ headless: true, viewport: { width: 1920, height: 1080 } });
await session.start();

const formFiller = new Agent({
  name: 'form-filler',
  model: 'google/gemini-3.5-flash-lite',
  instructions: `You are a form automation agent. Navigate to the URL, fill in the fields
with the provided data and submit the form. Prefer browser_fill_form for filling several fields at once.`,
  tools: browserTools(session, { modules: ['navigation', 'interaction', 'extraction'] }),
  temperature: 0.1,
  maxIterations: 15,
});

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });

try {
  const submitted = await cog.run(formFiller, {
    input: `Go to https://httpbin.org/forms/post and fill the form with:
- Customer name: John Doe
- Telephone: 555-1234
- Email: john@example.com
- Size: Medium
Then submit the form.`,
  });
  console.log(submitted.output);

  const echoed = await cog.run(formFiller, {
    input: 'Extract the text of the current page to show what the server received.',
  });
  console.log(echoed.output);
} finally {
  await session.close();
  await cog.close();
}

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/browser playwright
npx playwright install chromium
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx form-automation.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/browser/02-form-automation.ts

Learn more