Skip to content
Beta. This surface may change before GA; breaking changes are dated in the changelog.

Reactive multi-agent handoff: watches and Slack/Discord alerts

Agent A changes the accepted refund window from 14 to 30 days. Agent B follows a standing query and re-reads it when the answer moves. A person gets a Slack Block Kit card or Discord embed with a link to Activity.

Use Node.js 24 or newer. Set NICIA_MANAGEMENT_KEY to an organization management key, NICIA_SPACE to a new scratch handle, and NICIA_CHAT_WEBHOOK_URL to an incoming webhook URL for a test channel. Use the URL Slack gives you under https://hooks.slack.com/services/ or Discord gives you under https://discord.com/api/webhooks/. Keep both secrets in your environment. To test a different API host, set NICIA_API_URL.

Terminal window
npm init -y
npm pkg set type=module
npm install @nicia-ai/client@^0.2.0

Save as recipe.ts in an ESM project or run it from examples/watches-and-alerts. The management key provisions the space, mints separate write and read keys, and registers the chat webhook. Nicia formats the chat payload and posts it directly to the channel.

import { NiciaOrgClient, NiciaSpaceClient, pollUntilSettled, type PollOptions } from "@nicia-ai/client";
export async function runHandoff(
baseUrl: string,
managementKey: string,
handle: string,
chatWebhookUrl: string,
polling: PollOptions = { intervalMs: 5_000, timeoutMs: 150_000 },
) {
const org = new NiciaOrgClient({ baseUrl, managementKey });
await org.putSpace(handle, { name: "Watch handoff demo", ifExists: "conflict" });
const writer = await org.mintSpaceKey(handle, { name: "agent-a", capabilities: ["write"] });
const reader = await org.mintSpaceKey(handle, { name: "agent-b", capabilities: ["read"] });
if (!writer.secret || !reader.secret) throw new Error("Expected two new space keys");
const agentA = new NiciaSpaceClient({ baseUrl, handle, spaceKey: writer.secret });
const agentB = new NiciaSpaceClient({ baseUrl, handle, spaceKey: reader.secret });
const admin = new NiciaSpaceClient({ baseUrl, handle, spaceKey: managementKey });
const ruleId = "refund-window";
const beforeText = "Refund requests are accepted within 14 days.";
const afterText = "Refund requests are accepted within 30 days.";
const seeded = await agentA.mergeRecord(ruleId, {
kind: "rule",
label: "Refund window",
fields: { status: "active", body: beforeText },
});
if (seeded.change.status !== "applied") throw new Error("The initial rule is waiting in Review");
const query = { kind: "rule", where: { status: "active" } };
const before = await agentB.queryRecords(query);
if (!before.records.some((record) => record.id === ruleId && record.fields.body === beforeText))
throw new Error("Expected the 14-day policy before creating the watch");
const { watch } = await agentB.createWatch({ question: { kind: "query", query } });
if (watch.resultState !== "current") throw new Error("The watch has no baseline");
const { webhook } = await admin.createWebhook({
url: chatWebhookUrl,
types: ["answer.changed", "proposal.opened"],
});
const update = await agentA.mergeRecord(ruleId, { fields: { body: afterText } });
if (update.change.status !== "applied") throw new Error("The update is waiting in Review");
let after = watch.cursor;
const readEvents = async () => {
const page = await agentB.listEvents({ after, types: "answer.changed", watch: watch.id });
after = page.cursor;
return page;
};
const changed = await pollUntilSettled(
{ poll: readEvents, isSettled: (page) => page.events.some((event) => event.type === "answer.changed") },
polling,
);
const reread = await agentB.queryRecords(query);
if (!reread.records.some((record) => record.id === ruleId && record.fields.body === afterText))
throw new Error("The reader did not catch up to the 30-day policy");
const unrelated = await agentA.mergeRecord("refund-owner", {
kind: "decision",
label: "Refund owner",
fields: { status: "active", body: "Support owns refund approvals." },
});
if (unrelated.change.status !== "applied") throw new Error("The unrelated edit has not landed");
const { head } = await agentB.getWatch(watch.id);
await pollUntilSettled(
{
poll: () => agentB.getWatch(watch.id),
isSettled: ({ watch: latest }) =>
latest.resultState === "current" && BigInt(latest.evaluatedThrough) >= BigInt(head),
},
polling,
);
const quiet = await pollUntilSettled(
{ poll: readEvents, isSettled: (page) => page.events.length > 0 || !page.hasMore },
polling,
);
if (quiet.events.length > 0) throw new Error("The unrelated edit produced a watch event");
return {
watchId: watch.id,
webhookId: webhook.id,
events: changed.events,
before: { body: beforeText, receipt: before.receipt },
after: { body: afterText, receipt: reread.receipt },
unrelatedWatchEvents: quiet.events.length,
};
}
if (import.meta.main) {
const { NICIA_MANAGEMENT_KEY, NICIA_SPACE, NICIA_CHAT_WEBHOOK_URL } = process.env;
if (!NICIA_MANAGEMENT_KEY || !NICIA_SPACE || !NICIA_CHAT_WEBHOOK_URL)
throw new Error("Set NICIA_MANAGEMENT_KEY, a new NICIA_SPACE, and NICIA_CHAT_WEBHOOK_URL");
const baseUrl = process.env.NICIA_API_URL ?? "https://api.nicia.ai";
const result = await runHandoff(baseUrl, NICIA_MANAGEMENT_KEY, NICIA_SPACE, NICIA_CHAT_WEBHOOK_URL);
console.log(JSON.stringify(result, null, 2));
}
Terminal window
node recipe.ts

The output includes the 14-day and 30-day answers and their receipts. The event carries the watch ID and a position. Agent B uses the events cursor to resume and queries the record for its current body. The recipe then adds a decision outside the watched query, waits for the watch to evaluate through that edit, and checks for zero new watch events.

The chat subscription sends answer.changed for all watches in the space and proposal.opened for Review. Agent B’s event reader filters to its own watch. Delivery can lag the stream and repeat on retry. Watches · Webhook delivery

Keep the scratch space until the message arrives. When finished, use the management key to remove the webhook with deleteWebhook(webhookId) or retire the scratch space. Offboarding