An open-source, faster Devin alternative
TypeScript
channels: name the Twilio provider for its vendor, and keep addresses out of source
Brian Reardon3d1c286ddad7
diff --git a/agent/src/index.ts b/agent/src/index.tsindex 89d49bdf..841f15da 100644--- a/agent/src/index.ts+++ b/agent/src/index.ts@@ -94,7 +94,7 @@ export interface McpServerDefinition extends ResourceReference {* authentication; what does not differ is that an inbound message continues the* conversation it belongs to. Agents see the second part and never the first.*/-export type ChannelProvider = "slack" | "sms" | "email";+export type ChannelProvider = "slack" | "twilio" | "email";export type SlackChannelEvent = "app_mention" | "message.im";/** Providers without a mention/DM distinction have exactly one inbound event. */@@ -146,22 +146,36 @@ export interface SlackChannelDefinition extends ChannelDefinitionBase {readonly events: readonly SlackChannelEvent[];}-export interface SmsChannelDefinition extends ChannelDefinitionBase {- readonly type: "sms";- /** The number customers reply to, in E.164. */- readonly from: string;+/**+ * Named for the vendor, as `slack` is, because that is what actually differs:+ * the signature is Twilio's HMAC over the request URL and sorted parameters,+ * and the payload is Twilio's form encoding. A different SMS vendor is a+ * different adapter, not a variant of this one.+ *+ * Twilio WhatsApp rides the same API, webhook, and signature — addresses just+ * carry a `whatsapp:` prefix — so it needs no separate provider.+ *+ * The number itself is not here. It is per-environment operational+ * configuration, bound to a connection in the dashboard, exactly as Slack+ * conversation IDs are.+ */+export interface TwilioChannelDefinition extends ChannelDefinitionBase {+ readonly type: "twilio";readonly events: readonly MessageChannelEvent[];}+/**+ * The address is likewise a per-environment binding, not source: development+ * and production do not share an inbox.+ */export interface EmailChannelDefinition extends ChannelDefinitionBase {readonly type: "email";- readonly address: string;readonly events: readonly MessageChannelEvent[];}export type ChannelDefinition =| SlackChannelDefinition- | SmsChannelDefinition+ | TwilioChannelDefinition| EmailChannelDefinition;export interface ChannelRegistrationDefinition extends ResourceReference {@@ -505,14 +519,12 @@ const PROVIDER_TRIGGERS: Readonly<Record<ChannelProvider, readonly ChannelTrigger[]>> = {slack: ["mention", "direct-message"],- sms: ["message"],+ twilio: ["message"],email: ["message"],};// Suspend quickly by default. A resume costs a moment; an idle runtime costs// for every second nobody is typing, and on SMS or email that is most of them.const DEFAULT_IDLE_SUSPEND_SECONDS = 300;-const E164 = /^\+[1-9]\d{7,14}$/;-const EMAIL_ADDRESS = /^[^@\s]+@[^@\s.]+\.[^@\s]+$/;function resourceIdentifier(value: string, kind: string): string {const id = identifier(value, kind);@@ -617,19 +629,17 @@ export interface SlackChannelInput extends ChannelInputBase {events?: readonly SlackChannelEvent[];}-export interface SmsChannelInput extends ChannelInputBase {- type: "sms";- from: string;+export interface TwilioChannelInput extends ChannelInputBase {+ type: "twilio";}export interface EmailChannelInput extends ChannelInputBase {type: "email";- address: string;}export type ChannelInput =| SlackChannelInput- | SmsChannelInput+ | TwilioChannelInput| EmailChannelInput;function channelCommon(input: ChannelInputBase): {@@ -682,49 +692,22 @@ function replyDestinations(}export function defineChannel(input: SlackChannelInput): SlackChannelDefinition;-export function defineChannel(input: SmsChannelInput): SmsChannelDefinition;+export function defineChannel(input: TwilioChannelInput): TwilioChannelDefinition;export function defineChannel(input: EmailChannelInput): EmailChannelDefinition;export function defineChannel(input: ChannelInput): ChannelDefinition {const common = channelCommon(input);- if (input.type === "sms") {- const from = input.from.trim();- if (!E164.test(from)) {- throw new Error(- `SMS channel ${common.id} needs a from number in E.164, such as +15125550100`,- );- }+ if (input.type === "twilio" || input.type === "email") {return Object.freeze({kind: "channel" as const,version: 1 as const,- type: "sms" as const,+ type: input.type,...common,- from,events: Object.freeze(["message.inbound" as const]),- destinations: Object.freeze(replyDestinations(input, "sms")),+ destinations: Object.freeze(replyDestinations(input, input.type)),routing: Object.freeze(common.routing),idle: Object.freeze(common.idle),- });- }-- if (input.type === "email") {- const address = input.address.trim().toLowerCase();- if (!EMAIL_ADDRESS.test(address)) {- throw new Error(- `Email channel ${common.id} needs a deliverable address, such as [email protected]`,- );- }- return Object.freeze({- kind: "channel" as const,- version: 1 as const,- type: "email" as const,- ...common,- address,- events: Object.freeze(["message.inbound" as const]),- destinations: Object.freeze(replyDestinations(input, "email")),- routing: Object.freeze(common.routing),- idle: Object.freeze(common.idle),- });+ }) as ChannelDefinition;}const scopes = [...new Set(input.scopes.bot.map((scope) => scope.trim()))];diff --git a/cli/src/project.test.ts b/cli/src/project.test.tsindex a3617e7c..735a93d4 100644--- a/cli/src/project.test.ts+++ b/cli/src/project.test.ts@@ -777,7 +777,7 @@ export default function Agent() {}});-test("the compiler accepts SMS and email channels alongside Slack", async () => {+test("the compiler accepts Twilio and email channels alongside Slack", async () => {const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-providers-"));const root = resolve(parent, "app");try {@@ -791,8 +791,7 @@ test("the compiler accepts SMS and email channels alongside Slack", async () =>export default defineChannel({id: "shop-sms",- type: "sms",- from: "+15125550100",+ type: "twilio",idle: { suspendAfterSeconds: 60 },});`,@@ -804,7 +803,6 @@ export default defineChannel({export default defineChannel({id: "support-email",type: "email",- address: "[email protected]",});`,);@@ -820,17 +818,17 @@ export default registerChannel(shopSms, { on: ["message"] });const sms = built.manifest.channels.find((c) => c.id === "shop-sms");const email = built.manifest.channels.find((c) => c.id === "support-email");+ // No number in the manifest: it is per-environment operational config,+ // bound to a connection the same way Slack conversation IDs are.assert.deepEqual(sms, {id: "shop-sms",- type: "sms",+ type: "twilio",routing: { whenAmbiguous: "ask" },idle: { suspendAfterSeconds: 60 },- from: "+15125550100",events: ["message.inbound"],destinations: { reply: { type: "reply" } },});- // Addresses normalize to lower case so a conversation key is stable.- assert.equal(email?.address, "[email protected]");+ assert.equal(email?.type, "email");assert.deepEqual(email?.idle, { suspendAfterSeconds: 300 });assert.deepEqual(built.manifest.channelRegistrations, [{ agentId: "hello-world", channelId: "shop-sms", triggers: ["message"] },@@ -850,7 +848,7 @@ test("the compiler rejects a trigger the provider cannot deliver", async () => {await writeFile(resolve(root, "opencomputer", "channels", "shop-sms.ts"),`import { defineChannel } from "@opencomputer/agent";-export default defineChannel({ id: "shop-sms", type: "sms", from: "+15125550100" });+export default defineChannel({ id: "shop-sms", type: "twilio" });`,);await writeFile(@@ -862,36 +860,31 @@ export default registerChannel(shopSms, { on: ["mention"] }););await assert.rejects(readProjectResources(root),- /trigger mention is not available on a sms channel/,+ /trigger mention is not available on a twilio channel/,);} finally {await rm(parent, { recursive: true, force: true });}});-test("the compiler rejects malformed provider addresses", async () => {- const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-addr-"));+test("the compiler names providers by vendor, not by medium", async () => {+ const parent = await mkdtemp(resolve(tmpdir(), "opencomputer-vendor-"));const root = resolve(parent, "app");try {await initializeAgentProject(root);await mkdir(resolve(root, "opencomputer", "channels"), { recursive: true });- const channel = resolve(root, "opencomputer", "channels", "shop-sms.ts");+ // "sms" is a medium; the signature and payload this has to parse belong to+ // one vendor. A different SMS vendor is a different adapter.await writeFile(- channel,+ resolve(root, "opencomputer", "channels", "shop-sms.ts"),`import { defineChannel } from "@opencomputer/agent";-export default defineChannel({ id: "shop-sms", type: "sms", from: "512-555-0100" });+export default defineChannel({ id: "shop-sms", type: "sms" });`,);- await assert.rejects(readProjectResources(root), /E\.164/);-- await rm(channel);- await writeFile(- resolve(root, "opencomputer", "channels", "support-email.ts"),- `import { defineChannel } from "@opencomputer/agent";-export default defineChannel({ id: "support-email", type: "email", address: "not-an-address" });-`,+ await assert.rejects(+ readProjectResources(root),+ /unsupported channel type "sms". Supported: slack, twilio, email/,);- await assert.rejects(readProjectResources(root), /deliverable email address/);} finally {await rm(parent, { recursive: true, force: true });}diff --git a/cli/src/project.ts b/cli/src/project.tsindex c2684faa..3acdc1da 100644--- a/cli/src/project.ts+++ b/cli/src/project.ts@@ -48,7 +48,7 @@ export interface McpServerManifest {connection?: string;}-export type ChannelProviderManifest = "slack" | "sms" | "email";+export type ChannelProviderManifest = "slack" | "twilio" | "email";export interface ChannelDestinationManifest {type: "conversation" | "reply";@@ -61,10 +61,6 @@ export interface ChannelDefinitionManifest {displayName?: string;/** Slack only. */scopes?: { bot: string[] };- /** SMS only, E.164. */- from?: string;- /** Email only. */- address?: string;events: string[];destinations: Record<string, ChannelDestinationManifest>;routing: { whenAmbiguous: "ask" };@@ -1059,39 +1055,11 @@ function channelDefinition();const common = channelCommonManifest(input, path);- if (type === "sms") {- const from = literalStringValue(- objectProperty(input, "from"),- `${path} SMS from number`,- );- if (!/^\+[1-9]\d{7,14}$/.test(from)) {- throw new Error(- `${path} from must be an E.164 number such as +15125550100`,- );- }- return {- id,- type: "sms",- ...common,- from,- events: ["message.inbound"],- destinations: replyDestinationsManifest(input, path),- };- }-- if (type === "email") {- const address = literalStringValue(- objectProperty(input, "address"),- `${path} email address`,- ).toLowerCase();- if (!/^[^@\s]+@[^@\s.]+\.[^@\s]+$/.test(address)) {- throw new Error(`${path} address must be a deliverable email address`);- }+ if (type === "twilio" || type === "email") {return {id,- type: "email",+ type,...common,- address,events: ["message.inbound"],destinations: replyDestinationsManifest(input, path),};@@ -1099,7 +1067,7 @@ function channelDefinition(if (type !== "slack") {throw new Error(- `${path} has unsupported channel type ${JSON.stringify(type)}. Supported: slack, sms, email`,+ `${path} has unsupported channel type ${JSON.stringify(type)}. Supported: slack, twilio, email`,);}@@ -1221,7 +1189,7 @@ function channelRegistration(};const providerTriggers: Record<string, string[]> = {slack: ["mention", "direct-message"],- sms: ["message"],+ twilio: ["message"],email: ["message"],};const supported = providerTriggers[channel.type] ?? [];diff --git a/docs/agents/channels.mdx b/docs/agents/channels.mdxindex 1622dd87..bc360359 100644--- a/docs/agents/channels.mdx+++ b/docs/agents/channels.mdx@@ -13,13 +13,19 @@ differ is that an inbound message continues the conversation it belongs to.Agents see the second part and never the first — one `useInput().source ==="channel"` branch handles every provider.-| Provider | `type` | Addressed by | Triggers |-| --- | --- | --- | --- |-| Slack | `"slack"` | bot scopes and events | `mention`, `direct-message` |-| SMS | `"sms"` | `from`, an E.164 number | `message` |-| Email | `"email"` | `address` | `message` |+| Provider | `type` | Triggers |+| --- | --- | --- |+| Slack | `"slack"` | `mention`, `direct-message` |+| Twilio (SMS, WhatsApp) | `"twilio"` | `message` |+| Email | `"email"` | `message` |-## Define an SMS or email channel+Providers are named for the vendor rather than the medium, because the vendor is+what actually differs: Twilio signs the request URL and its sorted parameters,+Slack signs a timestamp and the raw body. A different SMS vendor is a different+adapter, not a variant of this one. Twilio WhatsApp needs no separate provider —+same API, same webhook, same signature, with a `whatsapp:` prefix on addresses.++## Define a Twilio or email channelProviders other than Slack address one participant, so they need no scopes andhave a single inbound event:@@ -29,8 +35,7 @@ import { defineChannel } from "@opencomputer/agent";export default defineChannel({id: "shop-sms",- type: "sms",- from: "+15125550100",+ type: "twilio",idle: { suspendAfterSeconds: 120 },});```@@ -41,10 +46,14 @@ import { defineChannel } from "@opencomputer/agent";export default defineChannel({id: "support-email",type: "email",- address: "[email protected]",});```+Note what is **not** in the definition: the phone number and the inbox address.+Those are per-environment operational configuration, bound to a connection in+the dashboard, exactly as Slack workspace and conversation IDs are. Development+and production do not share a number.+Register them the same way as Slack, with the trigger the provider supports:```tsx opencomputer/agents/receptionist/channels/shop-sms.ts@@ -54,7 +63,7 @@ import shopSms from "../../../channels/shop-sms.js";export default registerChannel(shopSms, { on: ["message"] });```-Registering a Slack trigger on an SMS channel is a build error, and the reverse+Registering a Slack trigger on a Twilio channel is a build error, and the reverseis too.## Idle and cost@@ -62,7 +71,7 @@ is too.Agent runtime is billed by wall-clock time, and a conversation spends most ofits life waiting for a person. `idle.suspendAfterSeconds` says how long to holdthe runtime after the last message; an inbound message resumes it. It defaults-to 300 seconds, and the default matters most where replies are slowest — an SMS+to 300 seconds, and the default matters most where replies are slowest — a textconversation that waits hours for an answer should not bill for those hours.## Slack