Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(app-events): say an event that needs setup on our side can't be used yet

AdityaUmaleadb49b5918e4
diff --git a/cloudflare/composio-broker/src/app-events.test.ts b/cloudflare/composio-broker/src/app-events.test.ts
index cf8a8f652c..8158bf23e3 100644
--- a/cloudflare/composio-broker/src/app-events.test.ts
+++ b/cloudflare/composio-broker/src/app-events.test.ts
@@ -703,6 +703,14 @@ describe("event types", () => {
expect((await h.call(owner, `/v1/trigger-types/${GMAIL_TYPE.slug.toLowerCase()}`)).status).toBe(200);
expect((await h.call(owner, "/v1/trigger-types/GMAIL_NOT_A_THING")).status).toBe(404);
});
+
+ it("says an event that needs setup on our side can't be used yet, instead of calling it unknown", async () => {
+ const owner = await h.install();
+ const response = await h.call(owner, "/v1/trigger-types/gmail_own_endpoint");
+ expect(response.status).toBe(422);
+ expect(await response.json()).toEqual({ error: "GMAIL_OWN_ENDPOINT can't be used yet: this event needs setup on OpenMausBot's side first. Pick another event this app offers." });
+ expect((await h.call(owner, "/v1/trigger-types/GMAIL_OWN_ENDPOINT")).status).toBe(422);
+ });
});
describe("status events", () => {
diff --git a/cloudflare/composio-broker/src/triggers.ts b/cloudflare/composio-broker/src/triggers.ts
index 9d9d12da54..1a90b7e105 100644
--- a/cloudflare/composio-broker/src/triggers.ts
+++ b/cloudflare/composio-broker/src/triggers.ts
@@ -85,18 +85,22 @@ interface BindingRow {
type RemoteResult = "done" | "missing" | { error: string };
+/** A known event type the shared subscription can never fire: it needs the project's own webhook endpoint first. */
+const NEEDS_SETUP = "needs_setup";
+type TypeLookup = TriggerType | typeof NEEDS_SETUP | null;
+
/** The catalog is the same for every installation. Cache API calls do nothing on a workers.dev host, so each
* isolate keeps its own copy. */
-const typeCache = new Map<string, { value: TriggerType[] | TriggerType | null; expiresAt: number }>();
+const typeCache = new Map<string, { value: TriggerType[] | TypeLookup; expiresAt: number }>();
-function cachedType<T extends TriggerType[] | TriggerType | null>(key: string): T | undefined {
+function cachedType<T extends TriggerType[] | TypeLookup>(key: string): T | undefined {
const entry = typeCache.get(key);
if (entry && Date.now() < entry.expiresAt) return entry.value as T;
typeCache.delete(key);
return undefined;
}
-function rememberType(key: string, value: TriggerType[] | TriggerType | null) {
+function rememberType(key: string, value: TriggerType[] | TypeLookup) {
typeCache.delete(key);
typeCache.set(key, { value, expiresAt: Date.now() + TYPE_CACHE_MS });
for (const oldest of typeCache.keys()) {
@@ -156,8 +160,14 @@ async function typesForToolkit(env: Env, toolkit: string): Promise<TriggerType[]
}
export async function triggerType(env: Env, slug: string): Promise<TriggerType | null> {
+ const type = await lookupTriggerType(env, slug);
+ return type === NEEDS_SETUP ? null : type;
+}
+
+/** Tells a type that needs setup on our side apart from one Composio does not know, so the person hears which. */
+async function lookupTriggerType(env: Env, slug: string): Promise<TypeLookup> {
const key = `type:${slug.toUpperCase()}`;
- const hit = cachedType<TriggerType | null>(key);
+ const hit = cachedType<TypeLookup>(key);
if (hit !== undefined) return hit;
const response = await composioRequest(env, `/triggers_types/${encodeURIComponent(slug)}?toolkit_versions=latest`);
// Composio answers 400, not 404, for a slug it does not know.
@@ -167,7 +177,8 @@ export async function triggerType(env: Env, slug: string): Promise<TriggerType |
return null;
}
if (!response.ok) throw new Error(await upstreamError(response, `Event type unavailable (${response.status})`));
- const type = usableType(await response.json());
+ const item = await response.json();
+ const type = usableType(item) ?? (triggerTypeSchema.safeParse(item).data?.requires_webhook_endpoint_setup === true ? NEEDS_SETUP : null);
rememberType(key, type);
return type;
}
@@ -198,7 +209,10 @@ export async function listTriggerTypes(url: URL, installation: EventInstallation
}
export async function getTriggerType(slug: string, env: Env) {
- const type = await triggerType(env, slug);
+ const type = await lookupTriggerType(env, slug);
+ if (type === NEEDS_SETUP) {
+ return json({ error: `${slug.toUpperCase()} can't be used yet: this event needs setup on OpenMausBot's side first. Pick another event this app offers.` }, 422);
+ }
return type ? json(type) : json({ error: "unknown or unsupported event type" }, 404);
}
diff --git a/server/app-triggers.test.ts b/server/app-triggers.test.ts
index 1ea2bdc5b7..932148a781 100644
--- a/server/app-triggers.test.ts
+++ b/server/app-triggers.test.ts
@@ -168,6 +168,14 @@ describe("AppTriggerManager", () => {
expect((await client.inventory()).triggers.find((binding) => binding.triggerId === trigger.remote.triggerId)?.enabled).toBe(false);
});
+ it("says an event that needs setup on our side can't be used yet, and an unknown one can't be watched", async () => {
+ const triggers = manager();
+ const create = (event: string) => triggers.create({ botId: "bot-1", name: "New mail", prompt: "Summarize it.", app: "gmail", event });
+ await expect(create("GMAIL_OWN_ENDPOINT")).rejects.toThrow("GMAIL_OWN_ENDPOINT can't be used yet: this event needs setup on OpenMausBot's side first. Pick another event this app offers.");
+ await expect(create("GMAIL_NOT_A_THING")).rejects.toThrow("GMAIL_NOT_A_THING is not an event OpenMausBot can watch.");
+ expect(triggers.list()).toEqual([]);
+ });
+
it("keeps an automation pending while the service is unreachable, and finishes it on its own", async () => {
let reachable = false;
const triggers = manager({ broker: brokerClient(() => ({ url: reachable ? broker.url : "http://127.0.0.1:9", token: owner.token })) });