Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

feat(slack): let input handlers delegate to defaults (#3332)

Ben Pankowc500167b9f4b
diff --git a/.changeset/delegate-slack-input-defaults.md b/.changeset/delegate-slack-input-defaults.md
new file mode 100644
index 000000000..9fd0094ba
--- /dev/null
+++ b/.changeset/delegate-slack-input-defaults.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Pass `defaultDeliver()` to custom Slack input handlers so they can delegate selected requests to eve's standard thread or private DM delivery.
diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdx
index b088d4b5a..4521b4508 100644
--- a/docs/channels/slack.mdx
+++ b/docs/channels/slack.mdx
@@ -453,6 +453,38 @@ export default slackChannel({
The callback receives the `InputRequest` and current `SessionContext`, so it can switch on `kind`, `action`, or presentation properties. Omit it to keep every request in the thread, or return `"direct-message"` unconditionally to make every request private. If eve cannot resolve the triggering Slack user for a direct message, it logs the undelivered request and posts no fallback, so private delivery fails closed. The existing `onInputResponse` or tool approval response policy still decides whether the person who clicks may respond.
+An authored `input.requested` handler can extend delivery and then hand the requests back to eve through `defaultDeliver()`. For example, an application can add non-sensitive context to the Slack thread before eve renders the actionable controls. The configured `approvalChannel` still sends sensitive reviews to a DM and keeps ordinary requests in the thread:
+
+```ts title="agent/channels/slack.ts"
+import { slackChannel } from "eve/channels/slack";
+import { isPrivate } from "../lib/review-policy";
+
+export default slackChannel({
+ approvalChannel(request) {
+ return isPrivate(request) ? "direct-message" : "thread";
+ },
+ events: {
+ async "input.requested"(event, channel, _ctx, defaultDeliver) {
+ await channel.thread.post({
+ blocks: [
+ {
+ type: "context",
+ elements: [
+ {
+ type: "mrkdwn",
+ text: "A review is needed before this work can continue.",
+ },
+ ],
+ },
+ ],
+ text: "A review is needed before this work can continue.",
+ });
+ await defaultDeliver(event);
+ },
+ },
+});
+```
+
Authorization prompts split public status from private credentials. A sign-in challenge (OAuth URL, device code) is a credential. Anyone who completes it binds their identity to the session's connection. The default `authorization.required` handler posts a public, link-free status in the thread, delivers the actual challenge ephemerally to the triggering user, device code included, and then updates that public status when `authorization.completed` fires. The handler receives a private-delivery context with `postEphemeral`, `postDirectMessage` (needs the `im:write` scope), and `state`. There is, intentionally, no public `post` and no raw API access.
```ts
diff --git a/packages/eve/src/public/channels/slack/defaults.ts b/packages/eve/src/public/channels/slack/defaults.ts
index 697ac787c..663ea74b9 100644
--- a/packages/eve/src/public/channels/slack/defaults.ts
+++ b/packages/eve/src/public/channels/slack/defaults.ts
@@ -210,7 +210,7 @@ function firstNonEmptyLine(text: string): string | undefined {
*/
export function defaultInputRequestedHandler(
approvalChannel?: SlackApprovalChannelResolver,
-): NonNullable<SlackChannelEvents["input.requested"]> {
+): NonNullable<SlackChannelInternalEvents["input.requested"]> {
return async (data, channel, ctx) => {
const directMessageRequests: InputRequest[] = [];
const threadRequests: InputRequest[] = [];
diff --git a/packages/eve/src/public/channels/slack/index.ts b/packages/eve/src/public/channels/slack/index.ts
index e8de47b22..286eee5e7 100644
--- a/packages/eve/src/public/channels/slack/index.ts
+++ b/packages/eve/src/public/channels/slack/index.ts
@@ -29,6 +29,9 @@ export {
type SlackInboundResult,
type SlackInboundResultOrPromise,
type SlackInitialMessage,
+ type SlackInputRequestedDefaultDeliver,
+ type SlackInputRequestedEvent,
+ type SlackInputRequestedHandler,
type SlackInputResponseContext,
type SlackInputResponseResult,
type SlackInputResponseSubmission,
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 60deff75b..881134bd1 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -65,6 +65,12 @@ function slackRespondTypeChecks(
void slackRespondTypeChecks;
+slackChannel({
+ events: {
+ "input.requested"(_event, _channel, _ctx) {},
+ },
+});
+
function getAdapter(channel: unknown): ChannelAdapter<any> {
if (!isCompiledChannel(channel)) {
throw new Error("Expected a CompiledChannel.");
@@ -718,6 +724,86 @@ describe("slackChannel() default event handlers", () => {
expect(parseSlackRequestBody(init as RequestInit)).toMatchObject({ status: "" });
});
+ it("lets an input override delegate selected requests to default delivery", async () => {
+ fetchMock.mockImplementation(async (input: string | URL | Request, init?: RequestInit) => {
+ const operation = String(input).split("/").at(-1);
+ if (operation === "conversations.open") {
+ return Response.json({ channel: { id: "D01" }, ok: true });
+ }
+ if (operation === "chat.getPermalink") {
+ return Response.json({ ok: true, permalink: "https://example.slack.com/message" });
+ }
+ if (operation === "chat.postMessage") {
+ return Response.json({ ok: true, ts: "1700000001.000001" });
+ }
+ throw new Error(`Unexpected Slack request: ${String(input)} ${String(init?.body)}`);
+ });
+ const customPrompts: string[] = [];
+ const adapter = withState(
+ getAdapter(
+ slackChannel({
+ approvalChannel: (request) =>
+ request.prompt.startsWith("Sensitive") ? "direct-message" : "thread",
+ credentials: { botToken: "xoxb-test" },
+ events: {
+ async "input.requested"(event, _channel, _ctx, defaultDeliver) {
+ const privateRequests = event.requests.filter((request) =>
+ request.prompt.startsWith("Sensitive"),
+ );
+ customPrompts.push(
+ ...event.requests
+ .filter((request) => !privateRequests.includes(request))
+ .map((request) => request.prompt),
+ );
+ await defaultDeliver({ ...event, requests: privateRequests });
+ },
+ },
+ }),
+ ),
+ {
+ ...THREAD_STATE,
+ triggeringMessageTs: "1700000000.000002",
+ triggeringUserId: "U01",
+ },
+ );
+ const ctx = buildAdapterContext(adapter, stubAccessor());
+
+ await callEvent(
+ adapter,
+ makeEvent("input.requested", {
+ requests: [
+ {
+ allowFreeform: true,
+ display: "select",
+ kind: "question",
+ prompt: "Ordinary follow-up",
+ requestId: "ordinary",
+ },
+ {
+ allowFreeform: false,
+ display: "select",
+ kind: "question",
+ options: [{ id: "approve", label: "Approve" }],
+ prompt: "Sensitive review",
+ requestId: "sensitive",
+ },
+ ],
+ sequence: 0,
+ stepIndex: 0,
+ turnId: "t1",
+ }),
+ ctx,
+ );
+
+ expect(customPrompts).toEqual(["Ordinary follow-up"]);
+ const posts = fetchMock.mock.calls
+ .filter(([input]) => String(input).endsWith("/chat.postMessage"))
+ .map(([, init]) => parseSlackRequestBody(init as RequestInit));
+ expect(posts.some((body) => body.channel === "D01")).toBe(true);
+ expect(JSON.stringify(posts)).toContain("eve_input:route:C01:1700000000.000001:sensitive");
+ expect(posts.some((body) => JSON.stringify(body).includes("Ordinary follow-up"))).toBe(false);
+ });
+
it("input.requested keeps tool input out of the interactive approval message", async () => {
const adapter = withState(
getAdapter(slackChannel({ credentials: { botToken: "xoxb-test" } })),
diff --git a/packages/eve/src/public/channels/slack/slackChannel.ts b/packages/eve/src/public/channels/slack/slackChannel.ts
index ccff4e732..4ab5fc6c0 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.ts
@@ -144,6 +144,18 @@ type SlackEventHandler<T extends UnstampedMessageStreamEvent["type"]> = (
ctx: SessionContext,
) => void | Promise<void>;
+export type SlackInputRequestedEvent = EventData<"input.requested">;
+
+/** Bound access to eve's standard Slack input delivery. */
+export type SlackInputRequestedDefaultDeliver = (data: SlackInputRequestedEvent) => Promise<void>;
+
+export type SlackInputRequestedHandler = (
+ data: SlackInputRequestedEvent,
+ channel: SlackEventContext,
+ ctx: SessionContext,
+ defaultDeliver: SlackInputRequestedDefaultDeliver,
+) => void | Promise<void>;
+
/**
* Delivery surface handed to `authorization.required` overrides. The
* connection challenge is a credential: anyone who completes the sign-in
@@ -580,7 +592,11 @@ export interface SlackChannelEvents {
readonly "message.appended"?: SlackEventHandler<"message.appended">;
readonly "reasoning.appended"?: SlackEventHandler<"reasoning.appended">;
readonly "reasoning.completed"?: SlackEventHandler<"reasoning.completed">;
- readonly "input.requested"?: SlackEventHandler<"input.requested">;
+ /**
+ * An override may pass selected requests to `defaultDeliver()` while
+ * rendering the rest itself. The default delivery honors `approvalChannel`.
+ */
+ readonly "input.requested"?: SlackInputRequestedHandler;
readonly "turn.failed"?: SlackEventHandler<"turn.failed">;
readonly "turn.completed"?: SlackEventHandler<"turn.completed">;
readonly "turn.cancelled"?: SlackEventHandler<"turn.cancelled">;
@@ -607,9 +623,10 @@ export interface SlackChannelEvents {
*/
export interface SlackChannelInternalEvents extends Omit<
SlackChannelEvents,
- "authorization.required"
+ "authorization.required" | "input.requested"
> {
readonly "authorization.required"?: SlackEventHandler<"authorization.required">;
+ readonly "input.requested"?: SlackEventHandler<"input.requested">;
}
export type SlackApprovalChannel = "direct-message" | "thread";
@@ -875,6 +892,15 @@ export function slackChannel(config: SlackChannelConfig = {}): SlackChannel {
(activityOwnsTypingStatus
? activityOwnedMessageCompleted
: defaultEvents["message.completed"]!);
+ const defaultInputRequested = defaultInputRequestedHandler(config.approvalChannel);
+ const inputRequestedOverride = config.events?.["input.requested"];
+ const inputRequestedHandler =
+ inputRequestedOverride === undefined
+ ? defaultInputRequested
+ : async (data: SlackInputRequestedEvent, channel: SlackEventContext, ctx: SessionContext) =>
+ inputRequestedOverride(data, channel, ctx, async (nextData) => {
+ await defaultInputRequested(nextData, channel, ctx);
+ });
const mergedEvents: SlackChannelInternalEvents = {
...defaultEvents,
...config.events,
@@ -903,8 +929,7 @@ export function slackChannel(config: SlackChannelConfig = {}): SlackChannel {
"reasoning.appended": reasoningHandler,
"actions.requested": actionsHandler,
"message.completed": messageCompletedHandler,
- "input.requested":
- config.events?.["input.requested"] ?? defaultInputRequestedHandler(config.approvalChannel),
+ "input.requested": inputRequestedHandler,
"authorization.required":
authorizationRequiredOverride === undefined
? defaultEvents["authorization.required"]