How to Build an AI Agent

Let’s build an agent together.
How to Build Auction News Marketing Agent
Ever wondered what an AI Agent is? Or how to leverage AI to help your business?
This agent will automatically find news about interesting things sold at auction, then create posts based on the story to share on social media.
The goal is to serve as an introduction to agents and how they work. If you get comfortable with the idea you can use this to start automating every different part of your business, from lead generation and qualification, to customer support, and beyond.
JSON for the Code module:
// ActivePieces Code step
export const code = async (inputs) => {
// 1) Get the raw string from the AI step
let raw = inputs.text;
// If AP passed an object, stringify it
if (typeof raw !== "string") raw = JSON.stringify(raw ?? "");
// 2) Remove code fences and trim
let cleaned = raw.replace(/```json|```/g, "").trim();
// 3) If the whole thing is quoted (e.g., starts and ends with a quote), unquote it
if ((cleaned.startsWith('"') && cleaned.endsWith('"')) ||
(cleaned.startsWith("'") && cleaned.endsWith("'"))) {
cleaned = cleaned.slice(1, -1);
}
// 4) Convert common escaped newlines back to real newlines
cleaned = cleaned.replace(/\\n/g, "\n");
// 5) Try to isolate the first {...} block if there’s extra text around it
const firstBrace = cleaned.indexOf("{");
const lastBrace = cleaned.lastIndexOf("}");
let core = (firstBrace !== -1 && lastBrace !== -1 && lastBrace > firstBrace)
? cleaned.slice(firstBrace, lastBrace + 1)
: cleaned;
// 6) Try parsing; if result is a string that itself looks like JSON, parse again
let data, errorMsg = "";
try {
data = JSON.parse(core);
if (typeof data === "string" && data.trim().startsWith("{")) {
data = JSON.parse(data);
}
} catch (err) {
errorMsg = `JSON parse failed: ${err.message}`;
data = {};
}
// 7) Normalize and return fields for Sheets
return {
item_sold: (data.item_sold ?? "").toString().trim(),
sale_price: (data.sale_price ?? "").toString().trim(),
summary: (data.summary ?? "").toString().trim(),
_debug_raw: raw,
_debug_cleaned: core,
_error: errorMsg
};
};
Let me know how it goes. If you build something neat with it, have an idea, found a mistake… anything, let me know.