clanker-agent/packages/companion-channels/src/adapters/webhook.ts
Harivansh Rathi 536241053c refactor: finish companion rename migration
Complete the remaining pi-to-companion rename across companion-os, web, vm-orchestrator, docker, and archived fixtures.

Verification:
- semantic rg sweeps for Pi/piConfig/getPi/.pi runtime references
- npm run check in apps/companion-os (fails in this worktree: biome not found)

Co-authored-by: Codex <noreply@openai.com>
2026-03-10 07:39:32 -05:00

45 lines
1.1 KiB
TypeScript

/**
* companion-channels — Built-in webhook adapter.
*
* POSTs message as JSON. The recipient field is the webhook URL.
*
* Config:
* {
* "type": "webhook",
* "method": "POST",
* "headers": { "Authorization": "Bearer ..." }
* }
*/
import type {
AdapterConfig,
ChannelAdapter,
ChannelMessage,
} from "../types.js";
export function createWebhookAdapter(config: AdapterConfig): ChannelAdapter {
const method = (config.method as string) ?? "POST";
const extraHeaders = (config.headers as Record<string, string>) ?? {};
return {
direction: "outgoing" as const,
async send(message: ChannelMessage): Promise<void> {
const res = await fetch(message.recipient, {
method,
headers: { "Content-Type": "application/json", ...extraHeaders },
body: JSON.stringify({
text: message.text,
source: message.source,
metadata: message.metadata,
timestamp: new Date().toISOString(),
}),
});
if (!res.ok) {
const err = await res.text().catch(() => "unknown error");
throw new Error(`Webhook error ${res.status}: ${err}`);
}
},
};
}