This guide explains how to add a new IO integration (any messaging or events platform) to the SDK. It focuses on:
- minimal required pieces (types, parser, domain object)
- optional custom actions exposed on the request object (reply, react, typing, etc.)
- telemetry, routing metadata, transport alignment, and tests
Notes:
- Not every integration needs a
sendReply. Custom actions are optional and platform-specific. For example, you might addreact(emoji),sendTyping(),openModal(), or others. - Use the smallest viable interface for your platform, then grow as needed.
Add a new trigger and a Data accessor for your integration. Optionally add a service interface and context handle if you want a shared service.
// src/types.ts
export type TriggerType =
| 'webhook'
| 'cron'
| 'manual'
| 'agent'
| 'sms'
| 'queue'
| 'voice'
| 'email'
| 'discord'
| 'telegram'
| 'slack'
| 'your-io'; // <- add your trigger key
// Optional: define a service interface if you want to expose shared actions
export interface YourIOService {
// Example action: reply (entirely optional)
sendReply?(
req: AgentRequest,
ctx: AgentContext,
reply: string | { text?: string }
): Promise<void>;
// Example action: react (entirely optional)
react?(
req: AgentRequest,
ctx: AgentContext,
emoji: string
): Promise<void>;
}
export interface AgentContext {
// ...
slack: SlackService;
yourio?: YourIOService; // <- optional
}
export interface Data {
// ...
yourio(): Promise<YourIO>; // <- add
}Create a minimal domain object that wraps the incoming payload and exposes convenient getters. Add any custom actions you want (reply, react, typing, etc.).
// src/io/yourio.ts
import { inspect } from 'node:util';
import { SpanStatusCode, context, trace } from '@opentelemetry/api';
import { POST } from '../apis/api';
import { getTracer, recordException } from '../router/router';
import { safeStringify } from '../server/util';
import type { AgentContext, AgentRequest, YourIOService } from '../types';
interface YourIOEvent { /* id, channelId, userId, text, ts, threadTs?, ... */ }
interface YourIOPayload { /* webhook payload shape */ }
export interface YourIOReply { text?: string }
export class YourIO implements YourIOService {
constructor(private payload: YourIOPayload) {}
get tenantId(): string { /* derive from payload */ return ''; }
get channel(): string { /* derive channel */ return ''; }
get text(): string { /* derive text */ return ''; }
[inspect.custom]() { return this.toString(); }
toString() { return JSON.stringify(this.payload); }
// Optional: custom action - reply
async sendReply(req: AgentRequest, ctx: AgentContext, reply: string | YourIOReply) {
const tracer = getTracer();
const span = tracer.startSpan('agentuity.yourio.reply', {}, context.active());
try {
const replyObj = typeof reply === 'string' ? { text: reply } : reply;
const payload = {
agentId: ctx.agent.id,
channel: this.channel,
text: replyObj.text,
// thread_ts: req.metadata?.['thread-ts']
};
const resp = await POST('/yourio/reply', safeStringify(payload), {
'Content-Type': 'application/json',
'X-Agentuity-YourIO-Tenant-Id': this.tenantId, // align with backend
});
if (resp.status === 200) {
span.setStatus({ code: SpanStatusCode.OK });
return;
}
throw new Error(`error sending yourio reply: ${resp.response.statusText} (${resp.response.status})`);
} catch (ex) {
recordException(span, ex);
throw ex;
} finally {
span.end();
}
}
// Optional: custom action - react
async react(req: AgentRequest, ctx: AgentContext, emoji: string) {
const tracer = getTracer();
const span = tracer.startSpan('agentuity.yourio.react', {}, context.active());
try {
const payload = { agentId: ctx.agent.id, channel: this.channel, emoji };
const resp = await POST('/yourio/react', safeStringify(payload), {
'Content-Type': 'application/json',
'X-Agentuity-YourIO-Tenant-Id': this.tenantId,
});
if (resp.status === 200) { span.setStatus({ code: SpanStatusCode.OK }); return; }
throw new Error(`error sending yourio reaction: ${resp.response.statusText} (${resp.response.status})`);
} catch (ex) {
recordException(span, ex);
throw ex;
} finally { span.end(); }
}
}
export async function parseYourIO(data: Buffer): Promise<YourIO> {
const payload = JSON.parse(data.toString()) as YourIOPayload;
return new YourIO(payload);
}Notes:
- Keep span names and attributes consistent across IOs (
agentuity.yourio.*). - Align endpoints and headers with your backend (
/yourio/reply,/yourio/react, etc.).
Add a yourio() parser to the DataHandler similar to slack().
// src/router/data.ts
import { parseYourIO, type YourIO } from '../io/yourio';
export class DataHandler implements Data {
// ...
async yourio(): Promise<YourIO> {
if (this.contentType !== 'application/json') {
throw new Error('The content type is not a valid yourio message');
}
const data = await this.data();
return parseYourIO(data);
}
}If your integration has multiple message types (e.g. events vs commands), pass a discriminator from request metadata (req.metadata['msg-type']) into your parseYourIO and model that in your IO class.
If you want a shared service, provide a YourIOService on context.yourio. If your custom actions are implemented directly on the domain object, the service can be omitted entirely.
Reuse established conventions:
metadata['msg-type']: discriminator for webhook subtypes if needed.metadata['thread-ts']: if threads are supported (map to Teams equivalents).
- Start spans with stable names:
agentuity.yourio.reply,agentuity.yourio.react, etc. - Add attributes mirroring other IOs:
@agentuity/agentId,@agentuity/yourioTenantId,@agentuity/yourioMessageType(if applicable),@agentuity/yourioChannel. - Use
recordException(span, ex)in catch andspan.setStatus({ code: SpanStatusCode.OK })on success.
Model tests on Slack/Discord tests:
- Parsing: validates
parseYourIOhandles representative payload(s). - Custom actions: mock transport and assert endpoint, headers, payload, and span behavior.
// test/io/yourio.test.ts (sketch)
import { parseYourIO } from '../../src/io/yourio';
// ...Agents access the IO via req.data.yourio() and can call your custom actions on the parsed object (or via context.yourio if exposed).
// inside your agent handler
const yourio = await req.data.yourio();
await yourio.sendReply?.(req, ctx, { text: 'Hello from Agentuity' });
await yourio.react?.(req, ctx, '👍');- Types:
TriggerType,Data.yourio(), optionalYourIOServiceandAgentContext.yourio - IO:
src/io/yourio.tswith parser and optional custom actions (reply/react/etc.) - Router:
DataHandler.yourio()implementation - Transport: backend endpoint + headers aligned
- Telemetry: span naming/attributes consistent
- Tests: parsing and custom actions behavior
- Add
'your-io'toTriggerType; addyourio(): Promise<YourIO>toData. Optionally defineYourIOServiceand addyourio?: YourIOServicetoAgentContext. - Create
src/io/yourio.ts: define payload types, implementclass YourIOwith getters and any custom actions you need (reply, react, typing), andparseYourIO(Buffer) => YourIO. - Wire
DataHandler.yourio()insrc/router/data.tsto callparseYourIOwith content-type validation and any needed metadata discriminators. - Implement transport calls inside your custom actions and align headers/endpoints with your backend.
- Add telemetry for each action: span name, attributes, success status, and exception recording.
- Write tests for parsing and each custom action (mock the transport).
- Use it in an agent via
const yourio = await req.data.yourio();and call the actions you exposed.