import express from 'express';
import { Agent, attach, type RuntimeTrigger } from '@svantic/sdk';
const app = express();
app.use(express.json());
// 1. Build the agent and its capabilities.
const agent = new Agent({
name: 'orders',
description: 'Order automation.',
public_url: 'https://api.acme.com',
});
agent.define_capability({ /* fetch_order */ });
agent.define_capability({ /* cancel_order */ });
// 2. Register triggers on the agent. Forge generates these for you,
// but you can hand-write them too.
agent.add_triggers([
{
type: 'webhook',
path: '/hooks/stripe',
prompt: 'Payment {{id}} of {{amount}} just settled; update the order.',
payload_map: {
id: 'data.object.id',
amount: 'data.object.amount_total',
},
},
{
type: 'schedule',
cron: '0 9 * * MON-FRI',
prompt: 'Email ops the overnight order summary.',
},
{
type: 'emit',
event: 'order_cancelled',
prompt: 'Order {{order_id}} was cancelled; issue the refund.',
},
]);
// 3. Wire everything — A2A endpoints, triggers, Svantic connection.
const handle = await attach(app, {
agent,
mesh: {
client_id: process.env.SVANTIC_CLIENT_ID!,
client_secret: process.env.SVANTIC_CLIENT_SECRET!,
},
});
app.listen(4000);
// 4. Fire emit triggers from anywhere in the service.
app.post('/orders/:id/cancel', async (req, res) => {
await db.orders.cancel(req.params.id);
handle.emit('order_cancelled', { order_id: req.params.id });
res.sendStatus(204);
});
// 5. Teardown (attach also handles SIGTERM / SIGINT automatically).
await handle.detach();